1 // SPDX-License-Identifier: GPL-2.0-only
3 * hid-cp2112.c - Silicon Labs HID USB to SMBus master bridge
4 * Copyright (c) 2013,2014 Uplogix, Inc.
5 * David Barksdale <dbarksdale@uplogix.com>
9 * The Silicon Labs CP2112 chip is a USB HID device which provides an
10 * SMBus controller for talking to slave devices and 8 GPIO pins. The
11 * host communicates with the CP2112 via raw HID reports.
14 * https://www.silabs.com/Support%20Documents/TechnicalDocs/CP2112.pdf
15 * Programming Interface Specification:
16 * https://www.silabs.com/documents/public/application-notes/an495-cp2112-interface-specification.pdf
19 #include <linux/gpio/consumer.h>
20 #include <linux/gpio/machine.h>
21 #include <linux/gpio/driver.h>
22 #include <linux/hid.h>
23 #include <linux/hidraw.h>
24 #include <linux/i2c.h>
25 #include <linux/module.h>
26 #include <linux/nls.h>
27 #include <linux/usb/ch9.h>
30 #define CP2112_REPORT_MAX_LENGTH 64
31 #define CP2112_GPIO_CONFIG_LENGTH 5
32 #define CP2112_GPIO_GET_LENGTH 2
33 #define CP2112_GPIO_SET_LENGTH 3
36 CP2112_GPIO_CONFIG
= 0x02,
37 CP2112_GPIO_GET
= 0x03,
38 CP2112_GPIO_SET
= 0x04,
39 CP2112_GET_VERSION_INFO
= 0x05,
40 CP2112_SMBUS_CONFIG
= 0x06,
41 CP2112_DATA_READ_REQUEST
= 0x10,
42 CP2112_DATA_WRITE_READ_REQUEST
= 0x11,
43 CP2112_DATA_READ_FORCE_SEND
= 0x12,
44 CP2112_DATA_READ_RESPONSE
= 0x13,
45 CP2112_DATA_WRITE_REQUEST
= 0x14,
46 CP2112_TRANSFER_STATUS_REQUEST
= 0x15,
47 CP2112_TRANSFER_STATUS_RESPONSE
= 0x16,
48 CP2112_CANCEL_TRANSFER
= 0x17,
49 CP2112_LOCK_BYTE
= 0x20,
50 CP2112_USB_CONFIG
= 0x21,
51 CP2112_MANUFACTURER_STRING
= 0x22,
52 CP2112_PRODUCT_STRING
= 0x23,
53 CP2112_SERIAL_STRING
= 0x24,
59 STATUS0_COMPLETE
= 0x02,
64 STATUS1_TIMEOUT_NACK
= 0x00,
65 STATUS1_TIMEOUT_BUS
= 0x01,
66 STATUS1_ARBITRATION_LOST
= 0x02,
67 STATUS1_READ_INCOMPLETE
= 0x03,
68 STATUS1_WRITE_INCOMPLETE
= 0x04,
69 STATUS1_SUCCESS
= 0x05,
72 struct cp2112_smbus_config_report
{
73 u8 report
; /* CP2112_SMBUS_CONFIG */
74 __be32 clock_speed
; /* Hz */
75 u8 device_address
; /* Stored in the upper 7 bits */
76 u8 auto_send_read
; /* 1 = enabled, 0 = disabled */
77 __be16 write_timeout
; /* ms, 0 = no timeout */
78 __be16 read_timeout
; /* ms, 0 = no timeout */
79 u8 scl_low_timeout
; /* 1 = enabled, 0 = disabled */
80 __be16 retry_time
; /* # of retries, 0 = no limit */
83 struct cp2112_usb_config_report
{
84 u8 report
; /* CP2112_USB_CONFIG */
85 __le16 vid
; /* Vendor ID */
86 __le16 pid
; /* Product ID */
87 u8 max_power
; /* Power requested in 2mA units */
88 u8 power_mode
; /* 0x00 = bus powered
89 0x01 = self powered & regulator off
90 0x02 = self powered & regulator on */
93 u8 mask
; /* What fields to program */
96 struct cp2112_read_req_report
{
97 u8 report
; /* CP2112_DATA_READ_REQUEST */
102 struct cp2112_write_read_req_report
{
103 u8 report
; /* CP2112_DATA_WRITE_READ_REQUEST */
106 u8 target_address_length
;
107 u8 target_address
[16];
110 struct cp2112_write_req_report
{
111 u8 report
; /* CP2112_DATA_WRITE_REQUEST */
117 struct cp2112_force_read_report
{
118 u8 report
; /* CP2112_DATA_READ_FORCE_SEND */
122 struct cp2112_xfer_status_report
{
123 u8 report
; /* CP2112_TRANSFER_STATUS_RESPONSE */
124 u8 status0
; /* STATUS0_* */
125 u8 status1
; /* STATUS1_* */
130 struct cp2112_string_report
{
131 u8 dummy
; /* force .string to be aligned */
132 u8 report
; /* CP2112_*_STRING */
133 u8 length
; /* length in bytes of everyting after .report */
134 u8 type
; /* USB_DT_STRING */
135 wchar_t string
[30]; /* UTF16_LITTLE_ENDIAN string */
138 /* Number of times to request transfer status before giving up waiting for a
139 transfer to complete. This may need to be changed if SMBUS clock, retries,
140 or read/write/scl_low timeout settings are changed. */
141 static const int XFER_STATUS_RETRIES
= 10;
143 /* Time in ms to wait for a CP2112_DATA_READ_RESPONSE or
144 CP2112_TRANSFER_STATUS_RESPONSE. */
145 static const int RESPONSE_TIMEOUT
= 50;
147 static const struct hid_device_id cp2112_devices
[] = {
148 { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL
, USB_DEVICE_ID_CYGNAL_CP2112
) },
151 MODULE_DEVICE_TABLE(hid
, cp2112_devices
);
153 struct cp2112_device
{
154 struct i2c_adapter adap
;
155 struct hid_device
*hdev
;
156 wait_queue_head_t wait
;
168 struct gpio_desc
*desc
[8];
170 struct delayed_work gpio_poll_worker
;
171 unsigned long irq_mask
;
175 static int gpio_push_pull
= 0xFF;
176 module_param(gpio_push_pull
, int, S_IRUGO
| S_IWUSR
);
177 MODULE_PARM_DESC(gpio_push_pull
, "GPIO push-pull configuration bitmask");
179 static int cp2112_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
181 struct cp2112_device
*dev
= gpiochip_get_data(chip
);
182 struct hid_device
*hdev
= dev
->hdev
;
183 u8
*buf
= dev
->in_out_buffer
;
186 mutex_lock(&dev
->lock
);
188 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
189 CP2112_GPIO_CONFIG_LENGTH
, HID_FEATURE_REPORT
,
191 if (ret
!= CP2112_GPIO_CONFIG_LENGTH
) {
192 hid_err(hdev
, "error requesting GPIO config: %d\n", ret
);
198 buf
[1] &= ~(1 << offset
);
199 buf
[2] = gpio_push_pull
;
201 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
202 CP2112_GPIO_CONFIG_LENGTH
, HID_FEATURE_REPORT
,
204 if (ret
!= CP2112_GPIO_CONFIG_LENGTH
) {
205 hid_err(hdev
, "error setting GPIO config: %d\n", ret
);
214 mutex_unlock(&dev
->lock
);
218 static void cp2112_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
220 struct cp2112_device
*dev
= gpiochip_get_data(chip
);
221 struct hid_device
*hdev
= dev
->hdev
;
222 u8
*buf
= dev
->in_out_buffer
;
225 mutex_lock(&dev
->lock
);
227 buf
[0] = CP2112_GPIO_SET
;
228 buf
[1] = value
? 0xff : 0;
229 buf
[2] = 1 << offset
;
231 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_SET
, buf
,
232 CP2112_GPIO_SET_LENGTH
, HID_FEATURE_REPORT
,
235 hid_err(hdev
, "error setting GPIO values: %d\n", ret
);
237 mutex_unlock(&dev
->lock
);
240 static int cp2112_gpio_get_all(struct gpio_chip
*chip
)
242 struct cp2112_device
*dev
= gpiochip_get_data(chip
);
243 struct hid_device
*hdev
= dev
->hdev
;
244 u8
*buf
= dev
->in_out_buffer
;
247 mutex_lock(&dev
->lock
);
249 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_GET
, buf
,
250 CP2112_GPIO_GET_LENGTH
, HID_FEATURE_REPORT
,
252 if (ret
!= CP2112_GPIO_GET_LENGTH
) {
253 hid_err(hdev
, "error requesting GPIO values: %d\n", ret
);
254 ret
= ret
< 0 ? ret
: -EIO
;
261 mutex_unlock(&dev
->lock
);
266 static int cp2112_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
270 ret
= cp2112_gpio_get_all(chip
);
274 return (ret
>> offset
) & 1;
277 static int cp2112_gpio_direction_output(struct gpio_chip
*chip
,
278 unsigned offset
, int value
)
280 struct cp2112_device
*dev
= gpiochip_get_data(chip
);
281 struct hid_device
*hdev
= dev
->hdev
;
282 u8
*buf
= dev
->in_out_buffer
;
285 mutex_lock(&dev
->lock
);
287 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
288 CP2112_GPIO_CONFIG_LENGTH
, HID_FEATURE_REPORT
,
290 if (ret
!= CP2112_GPIO_CONFIG_LENGTH
) {
291 hid_err(hdev
, "error requesting GPIO config: %d\n", ret
);
295 buf
[1] |= 1 << offset
;
296 buf
[2] = gpio_push_pull
;
298 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
299 CP2112_GPIO_CONFIG_LENGTH
, HID_FEATURE_REPORT
,
302 hid_err(hdev
, "error setting GPIO config: %d\n", ret
);
306 mutex_unlock(&dev
->lock
);
309 * Set gpio value when output direction is already set,
310 * as specified in AN495, Rev. 0.2, cpt. 4.4
312 cp2112_gpio_set(chip
, offset
, value
);
317 mutex_unlock(&dev
->lock
);
318 return ret
< 0 ? ret
: -EIO
;
321 static int cp2112_hid_get(struct hid_device
*hdev
, unsigned char report_number
,
322 u8
*data
, size_t count
, unsigned char report_type
)
327 buf
= kmalloc(count
, GFP_KERNEL
);
331 ret
= hid_hw_raw_request(hdev
, report_number
, buf
, count
,
332 report_type
, HID_REQ_GET_REPORT
);
333 memcpy(data
, buf
, count
);
338 static int cp2112_hid_output(struct hid_device
*hdev
, u8
*data
, size_t count
,
339 unsigned char report_type
)
344 buf
= kmemdup(data
, count
, GFP_KERNEL
);
348 if (report_type
== HID_OUTPUT_REPORT
)
349 ret
= hid_hw_output_report(hdev
, buf
, count
);
351 ret
= hid_hw_raw_request(hdev
, buf
[0], buf
, count
, report_type
,
358 static int cp2112_wait(struct cp2112_device
*dev
, atomic_t
*avail
)
362 /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a
363 * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to
364 * come in cp2112_raw_event or timeout. There will only be one of these
365 * in flight at any one time. The timeout is extremely large and is a
366 * last resort if the CP2112 has died. If we do timeout we don't expect
367 * to receive the response which would cause data races, it's not like
368 * we can do anything about it anyway.
370 ret
= wait_event_interruptible_timeout(dev
->wait
,
371 atomic_read(avail
), msecs_to_jiffies(RESPONSE_TIMEOUT
));
372 if (-ERESTARTSYS
== ret
)
377 atomic_set(avail
, 0);
381 static int cp2112_xfer_status(struct cp2112_device
*dev
)
383 struct hid_device
*hdev
= dev
->hdev
;
387 buf
[0] = CP2112_TRANSFER_STATUS_REQUEST
;
389 atomic_set(&dev
->xfer_avail
, 0);
391 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
393 hid_warn(hdev
, "Error requesting status: %d\n", ret
);
397 ret
= cp2112_wait(dev
, &dev
->xfer_avail
);
401 return dev
->xfer_status
;
404 static int cp2112_read(struct cp2112_device
*dev
, u8
*data
, size_t size
)
406 struct hid_device
*hdev
= dev
->hdev
;
407 struct cp2112_force_read_report report
;
410 if (size
> sizeof(dev
->read_data
))
411 size
= sizeof(dev
->read_data
);
412 report
.report
= CP2112_DATA_READ_FORCE_SEND
;
413 report
.length
= cpu_to_be16(size
);
415 atomic_set(&dev
->read_avail
, 0);
417 ret
= cp2112_hid_output(hdev
, &report
.report
, sizeof(report
),
420 hid_warn(hdev
, "Error requesting data: %d\n", ret
);
424 ret
= cp2112_wait(dev
, &dev
->read_avail
);
428 hid_dbg(hdev
, "read %d of %zd bytes requested\n",
429 dev
->read_length
, size
);
431 if (size
> dev
->read_length
)
432 size
= dev
->read_length
;
434 memcpy(data
, dev
->read_data
, size
);
435 return dev
->read_length
;
438 static int cp2112_read_req(void *buf
, u8 slave_address
, u16 length
)
440 struct cp2112_read_req_report
*report
= buf
;
442 if (length
< 1 || length
> 512)
445 report
->report
= CP2112_DATA_READ_REQUEST
;
446 report
->slave_address
= slave_address
<< 1;
447 report
->length
= cpu_to_be16(length
);
448 return sizeof(*report
);
451 static int cp2112_write_read_req(void *buf
, u8 slave_address
, u16 length
,
452 u8 command
, u8
*data
, u8 data_length
)
454 struct cp2112_write_read_req_report
*report
= buf
;
456 if (length
< 1 || length
> 512
457 || data_length
> sizeof(report
->target_address
) - 1)
460 report
->report
= CP2112_DATA_WRITE_READ_REQUEST
;
461 report
->slave_address
= slave_address
<< 1;
462 report
->length
= cpu_to_be16(length
);
463 report
->target_address_length
= data_length
+ 1;
464 report
->target_address
[0] = command
;
465 memcpy(&report
->target_address
[1], data
, data_length
);
466 return data_length
+ 6;
469 static int cp2112_write_req(void *buf
, u8 slave_address
, u8 command
, u8
*data
,
472 struct cp2112_write_req_report
*report
= buf
;
474 if (data_length
> sizeof(report
->data
) - 1)
477 report
->report
= CP2112_DATA_WRITE_REQUEST
;
478 report
->slave_address
= slave_address
<< 1;
479 report
->length
= data_length
+ 1;
480 report
->data
[0] = command
;
481 memcpy(&report
->data
[1], data
, data_length
);
482 return data_length
+ 4;
485 static int cp2112_i2c_write_req(void *buf
, u8 slave_address
, u8
*data
,
488 struct cp2112_write_req_report
*report
= buf
;
490 if (data_length
> sizeof(report
->data
))
493 report
->report
= CP2112_DATA_WRITE_REQUEST
;
494 report
->slave_address
= slave_address
<< 1;
495 report
->length
= data_length
;
496 memcpy(report
->data
, data
, data_length
);
497 return data_length
+ 3;
500 static int cp2112_i2c_write_read_req(void *buf
, u8 slave_address
,
501 u8
*addr
, int addr_length
,
504 struct cp2112_write_read_req_report
*report
= buf
;
506 if (read_length
< 1 || read_length
> 512 ||
507 addr_length
> sizeof(report
->target_address
))
510 report
->report
= CP2112_DATA_WRITE_READ_REQUEST
;
511 report
->slave_address
= slave_address
<< 1;
512 report
->length
= cpu_to_be16(read_length
);
513 report
->target_address_length
= addr_length
;
514 memcpy(report
->target_address
, addr
, addr_length
);
515 return addr_length
+ 5;
518 static int cp2112_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
521 struct cp2112_device
*dev
= (struct cp2112_device
*)adap
->algo_data
;
522 struct hid_device
*hdev
= dev
->hdev
;
525 ssize_t read_length
= 0;
527 unsigned int retries
;
530 hid_dbg(hdev
, "I2C %d messages\n", num
);
533 if (msgs
->flags
& I2C_M_RD
) {
534 hid_dbg(hdev
, "I2C read %#04x len %d\n",
535 msgs
->addr
, msgs
->len
);
536 read_length
= msgs
->len
;
537 read_buf
= msgs
->buf
;
538 count
= cp2112_read_req(buf
, msgs
->addr
, msgs
->len
);
540 hid_dbg(hdev
, "I2C write %#04x len %d\n",
541 msgs
->addr
, msgs
->len
);
542 count
= cp2112_i2c_write_req(buf
, msgs
->addr
,
543 msgs
->buf
, msgs
->len
);
547 } else if (dev
->hwversion
> 1 && /* no repeated start in rev 1 */
549 msgs
[0].addr
== msgs
[1].addr
&&
550 !(msgs
[0].flags
& I2C_M_RD
) && (msgs
[1].flags
& I2C_M_RD
)) {
551 hid_dbg(hdev
, "I2C write-read %#04x wlen %d rlen %d\n",
552 msgs
[0].addr
, msgs
[0].len
, msgs
[1].len
);
553 read_length
= msgs
[1].len
;
554 read_buf
= msgs
[1].buf
;
555 count
= cp2112_i2c_write_read_req(buf
, msgs
[0].addr
,
556 msgs
[0].buf
, msgs
[0].len
, msgs
[1].len
);
561 "Multi-message I2C transactions not supported\n");
565 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
567 hid_err(hdev
, "power management error: %d\n", ret
);
571 ret
= cp2112_hid_output(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
573 hid_warn(hdev
, "Error starting transaction: %d\n", ret
);
577 for (retries
= 0; retries
< XFER_STATUS_RETRIES
; ++retries
) {
578 ret
= cp2112_xfer_status(dev
);
586 if (XFER_STATUS_RETRIES
<= retries
) {
587 hid_warn(hdev
, "Transfer timed out, cancelling.\n");
588 buf
[0] = CP2112_CANCEL_TRANSFER
;
591 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
593 hid_warn(hdev
, "Error cancelling transaction: %d\n",
600 for (count
= 0; count
< read_length
;) {
601 ret
= cp2112_read(dev
, read_buf
+ count
, read_length
- count
);
605 hid_err(hdev
, "read returned 0\n");
610 if (count
> read_length
) {
612 * The hardware returned too much data.
613 * This is mostly harmless because cp2112_read()
614 * has a limit check so didn't overrun our
615 * buffer. Nevertheless, we return an error
616 * because something is seriously wrong and
617 * it shouldn't go unnoticed.
619 hid_err(hdev
, "long read: %d > %zd\n",
620 ret
, read_length
- count
+ ret
);
626 /* return the number of transferred messages */
630 hid_hw_power(hdev
, PM_HINT_NORMAL
);
631 hid_dbg(hdev
, "I2C transfer finished: %d\n", ret
);
635 static int cp2112_xfer(struct i2c_adapter
*adap
, u16 addr
,
636 unsigned short flags
, char read_write
, u8 command
,
637 int size
, union i2c_smbus_data
*data
)
639 struct cp2112_device
*dev
= (struct cp2112_device
*)adap
->algo_data
;
640 struct hid_device
*hdev
= dev
->hdev
;
644 size_t read_length
= 0;
645 unsigned int retries
;
648 hid_dbg(hdev
, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n",
649 read_write
== I2C_SMBUS_WRITE
? "write" : "read",
650 addr
, flags
, command
, size
);
656 if (I2C_SMBUS_READ
== read_write
)
657 count
= cp2112_read_req(buf
, addr
, read_length
);
659 count
= cp2112_write_req(buf
, addr
, command
, NULL
,
662 case I2C_SMBUS_BYTE_DATA
:
665 if (I2C_SMBUS_READ
== read_write
)
666 count
= cp2112_write_read_req(buf
, addr
, read_length
,
669 count
= cp2112_write_req(buf
, addr
, command
,
672 case I2C_SMBUS_WORD_DATA
:
674 word
= cpu_to_le16(data
->word
);
676 if (I2C_SMBUS_READ
== read_write
)
677 count
= cp2112_write_read_req(buf
, addr
, read_length
,
680 count
= cp2112_write_req(buf
, addr
, command
,
683 case I2C_SMBUS_PROC_CALL
:
684 size
= I2C_SMBUS_WORD_DATA
;
685 read_write
= I2C_SMBUS_READ
;
687 word
= cpu_to_le16(data
->word
);
689 count
= cp2112_write_read_req(buf
, addr
, read_length
, command
,
692 case I2C_SMBUS_I2C_BLOCK_DATA
:
693 if (read_write
== I2C_SMBUS_READ
) {
694 read_length
= data
->block
[0];
695 count
= cp2112_write_read_req(buf
, addr
, read_length
,
698 count
= cp2112_write_req(buf
, addr
, command
,
703 case I2C_SMBUS_BLOCK_DATA
:
704 if (I2C_SMBUS_READ
== read_write
) {
705 count
= cp2112_write_read_req(buf
, addr
,
709 count
= cp2112_write_req(buf
, addr
, command
,
714 case I2C_SMBUS_BLOCK_PROC_CALL
:
715 size
= I2C_SMBUS_BLOCK_DATA
;
716 read_write
= I2C_SMBUS_READ
;
718 count
= cp2112_write_read_req(buf
, addr
, I2C_SMBUS_BLOCK_MAX
,
719 command
, data
->block
,
723 hid_warn(hdev
, "Unsupported transaction %d\n", size
);
730 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
732 hid_err(hdev
, "power management error: %d\n", ret
);
736 ret
= cp2112_hid_output(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
738 hid_warn(hdev
, "Error starting transaction: %d\n", ret
);
742 for (retries
= 0; retries
< XFER_STATUS_RETRIES
; ++retries
) {
743 ret
= cp2112_xfer_status(dev
);
751 if (XFER_STATUS_RETRIES
<= retries
) {
752 hid_warn(hdev
, "Transfer timed out, cancelling.\n");
753 buf
[0] = CP2112_CANCEL_TRANSFER
;
756 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
758 hid_warn(hdev
, "Error cancelling transaction: %d\n",
765 if (I2C_SMBUS_WRITE
== read_write
) {
770 if (I2C_SMBUS_BLOCK_DATA
== size
)
773 ret
= cp2112_read(dev
, buf
, read_length
);
776 if (ret
!= read_length
) {
777 hid_warn(hdev
, "short read: %d < %zd\n", ret
, read_length
);
784 case I2C_SMBUS_BYTE_DATA
:
787 case I2C_SMBUS_WORD_DATA
:
788 data
->word
= le16_to_cpup((__le16
*)buf
);
790 case I2C_SMBUS_I2C_BLOCK_DATA
:
791 memcpy(data
->block
+ 1, buf
, read_length
);
793 case I2C_SMBUS_BLOCK_DATA
:
794 if (read_length
> I2C_SMBUS_BLOCK_MAX
) {
799 memcpy(data
->block
, buf
, read_length
);
805 hid_hw_power(hdev
, PM_HINT_NORMAL
);
806 hid_dbg(hdev
, "transfer finished: %d\n", ret
);
810 static u32
cp2112_functionality(struct i2c_adapter
*adap
)
812 return I2C_FUNC_I2C
|
813 I2C_FUNC_SMBUS_BYTE
|
814 I2C_FUNC_SMBUS_BYTE_DATA
|
815 I2C_FUNC_SMBUS_WORD_DATA
|
816 I2C_FUNC_SMBUS_BLOCK_DATA
|
817 I2C_FUNC_SMBUS_I2C_BLOCK
|
818 I2C_FUNC_SMBUS_PROC_CALL
|
819 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
;
822 static const struct i2c_algorithm smbus_algorithm
= {
823 .master_xfer
= cp2112_i2c_xfer
,
824 .smbus_xfer
= cp2112_xfer
,
825 .functionality
= cp2112_functionality
,
828 static int cp2112_get_usb_config(struct hid_device
*hdev
,
829 struct cp2112_usb_config_report
*cfg
)
833 ret
= cp2112_hid_get(hdev
, CP2112_USB_CONFIG
, (u8
*)cfg
, sizeof(*cfg
),
835 if (ret
!= sizeof(*cfg
)) {
836 hid_err(hdev
, "error reading usb config: %d\n", ret
);
845 static int cp2112_set_usb_config(struct hid_device
*hdev
,
846 struct cp2112_usb_config_report
*cfg
)
850 BUG_ON(cfg
->report
!= CP2112_USB_CONFIG
);
852 ret
= cp2112_hid_output(hdev
, (u8
*)cfg
, sizeof(*cfg
),
854 if (ret
!= sizeof(*cfg
)) {
855 hid_err(hdev
, "error writing usb config: %d\n", ret
);
864 static void chmod_sysfs_attrs(struct hid_device
*hdev
);
866 #define CP2112_CONFIG_ATTR(name, store, format, ...) \
867 static ssize_t name##_store(struct device *kdev, \
868 struct device_attribute *attr, const char *buf, \
871 struct hid_device *hdev = to_hid_device(kdev); \
872 struct cp2112_usb_config_report cfg; \
873 int ret = cp2112_get_usb_config(hdev, &cfg); \
877 ret = cp2112_set_usb_config(hdev, &cfg); \
880 chmod_sysfs_attrs(hdev); \
883 static ssize_t name##_show(struct device *kdev, \
884 struct device_attribute *attr, char *buf) \
886 struct hid_device *hdev = to_hid_device(kdev); \
887 struct cp2112_usb_config_report cfg; \
888 int ret = cp2112_get_usb_config(hdev, &cfg); \
891 return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \
893 static DEVICE_ATTR_RW(name);
895 CP2112_CONFIG_ATTR(vendor_id
, ({
898 if (sscanf(buf
, "%hi", &vid
) != 1)
901 cfg
.vid
= cpu_to_le16(vid
);
903 }), "0x%04x\n", le16_to_cpu(cfg
.vid
));
905 CP2112_CONFIG_ATTR(product_id
, ({
908 if (sscanf(buf
, "%hi", &pid
) != 1)
911 cfg
.pid
= cpu_to_le16(pid
);
913 }), "0x%04x\n", le16_to_cpu(cfg
.pid
));
915 CP2112_CONFIG_ATTR(max_power
, ({
918 if (sscanf(buf
, "%i", &mA
) != 1)
921 cfg
.max_power
= (mA
+ 1) / 2;
923 }), "%u mA\n", cfg
.max_power
* 2);
925 CP2112_CONFIG_ATTR(power_mode
, ({
926 if (sscanf(buf
, "%hhi", &cfg
.power_mode
) != 1)
930 }), "%u\n", cfg
.power_mode
);
932 CP2112_CONFIG_ATTR(release_version
, ({
933 if (sscanf(buf
, "%hhi.%hhi", &cfg
.release_major
, &cfg
.release_minor
)
938 }), "%u.%u\n", cfg
.release_major
, cfg
.release_minor
);
940 #undef CP2112_CONFIG_ATTR
942 struct cp2112_pstring_attribute
{
943 struct device_attribute attr
;
944 unsigned char report
;
947 static ssize_t
pstr_store(struct device
*kdev
,
948 struct device_attribute
*kattr
, const char *buf
,
951 struct hid_device
*hdev
= to_hid_device(kdev
);
952 struct cp2112_pstring_attribute
*attr
=
953 container_of(kattr
, struct cp2112_pstring_attribute
, attr
);
954 struct cp2112_string_report report
;
957 memset(&report
, 0, sizeof(report
));
959 ret
= utf8s_to_utf16s(buf
, count
, UTF16_LITTLE_ENDIAN
,
960 report
.string
, ARRAY_SIZE(report
.string
));
961 report
.report
= attr
->report
;
962 report
.length
= ret
* sizeof(report
.string
[0]) + 2;
963 report
.type
= USB_DT_STRING
;
965 ret
= cp2112_hid_output(hdev
, &report
.report
, report
.length
+ 1,
967 if (ret
!= report
.length
+ 1) {
968 hid_err(hdev
, "error writing %s string: %d\n", kattr
->attr
.name
,
975 chmod_sysfs_attrs(hdev
);
979 static ssize_t
pstr_show(struct device
*kdev
,
980 struct device_attribute
*kattr
, char *buf
)
982 struct hid_device
*hdev
= to_hid_device(kdev
);
983 struct cp2112_pstring_attribute
*attr
=
984 container_of(kattr
, struct cp2112_pstring_attribute
, attr
);
985 struct cp2112_string_report report
;
989 ret
= cp2112_hid_get(hdev
, attr
->report
, &report
.report
,
990 sizeof(report
) - 1, HID_FEATURE_REPORT
);
992 hid_err(hdev
, "error reading %s string: %d\n", kattr
->attr
.name
,
999 if (report
.length
< 2) {
1000 hid_err(hdev
, "invalid %s string length: %d\n",
1001 kattr
->attr
.name
, report
.length
);
1005 length
= report
.length
> ret
- 1 ? ret
- 1 : report
.length
;
1006 length
= (length
- 2) / sizeof(report
.string
[0]);
1007 ret
= utf16s_to_utf8s(report
.string
, length
, UTF16_LITTLE_ENDIAN
, buf
,
1013 #define CP2112_PSTR_ATTR(name, _report) \
1014 static struct cp2112_pstring_attribute dev_attr_##name = { \
1015 .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \
1016 .report = _report, \
1019 CP2112_PSTR_ATTR(manufacturer
, CP2112_MANUFACTURER_STRING
);
1020 CP2112_PSTR_ATTR(product
, CP2112_PRODUCT_STRING
);
1021 CP2112_PSTR_ATTR(serial
, CP2112_SERIAL_STRING
);
1023 #undef CP2112_PSTR_ATTR
1025 static const struct attribute_group cp2112_attr_group
= {
1026 .attrs
= (struct attribute
*[]){
1027 &dev_attr_vendor_id
.attr
,
1028 &dev_attr_product_id
.attr
,
1029 &dev_attr_max_power
.attr
,
1030 &dev_attr_power_mode
.attr
,
1031 &dev_attr_release_version
.attr
,
1032 &dev_attr_manufacturer
.attr
.attr
,
1033 &dev_attr_product
.attr
.attr
,
1034 &dev_attr_serial
.attr
.attr
,
1039 /* Chmoding our sysfs attributes is simply a way to expose which fields in the
1040 * PROM have already been programmed. We do not depend on this preventing
1041 * writing to these attributes since the CP2112 will simply ignore writes to
1042 * already-programmed fields. This is why there is no sense in fixing this
1045 static void chmod_sysfs_attrs(struct hid_device
*hdev
)
1047 struct attribute
**attr
;
1051 ret
= cp2112_hid_get(hdev
, CP2112_LOCK_BYTE
, buf
, sizeof(buf
),
1052 HID_FEATURE_REPORT
);
1053 if (ret
!= sizeof(buf
)) {
1054 hid_err(hdev
, "error reading lock byte: %d\n", ret
);
1058 for (attr
= cp2112_attr_group
.attrs
; *attr
; ++attr
) {
1059 umode_t mode
= (buf
[1] & 1) ? S_IWUSR
| S_IRUGO
: S_IRUGO
;
1060 ret
= sysfs_chmod_file(&hdev
->dev
.kobj
, *attr
, mode
);
1062 hid_err(hdev
, "error chmoding sysfs file %s\n",
1068 static void cp2112_gpio_irq_ack(struct irq_data
*d
)
1072 static void cp2112_gpio_irq_mask(struct irq_data
*d
)
1074 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1075 struct cp2112_device
*dev
= gpiochip_get_data(gc
);
1077 __clear_bit(d
->hwirq
, &dev
->irq_mask
);
1080 static void cp2112_gpio_irq_unmask(struct irq_data
*d
)
1082 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1083 struct cp2112_device
*dev
= gpiochip_get_data(gc
);
1085 __set_bit(d
->hwirq
, &dev
->irq_mask
);
1088 static void cp2112_gpio_poll_callback(struct work_struct
*work
)
1090 struct cp2112_device
*dev
= container_of(work
, struct cp2112_device
,
1091 gpio_poll_worker
.work
);
1094 u8 virqs
= (u8
)dev
->irq_mask
;
1098 ret
= cp2112_gpio_get_all(&dev
->gc
);
1099 if (ret
== -ENODEV
) /* the hardware has been disconnected */
1107 virq
= ffs(virqs
) - 1;
1108 virqs
&= ~BIT(virq
);
1110 if (!dev
->gc
.to_irq
)
1113 irq
= dev
->gc
.to_irq(&dev
->gc
, virq
);
1115 d
= irq_get_irq_data(irq
);
1119 irq_type
= irqd_get_trigger_type(d
);
1121 if (gpio_mask
& BIT(virq
)) {
1124 if (irq_type
& IRQ_TYPE_LEVEL_HIGH
)
1125 handle_nested_irq(irq
);
1127 if ((irq_type
& IRQ_TYPE_EDGE_RISING
) &&
1128 !(dev
->gpio_prev_state
& BIT(virq
)))
1129 handle_nested_irq(irq
);
1133 if (irq_type
& IRQ_TYPE_LEVEL_LOW
)
1134 handle_nested_irq(irq
);
1136 if ((irq_type
& IRQ_TYPE_EDGE_FALLING
) &&
1137 (dev
->gpio_prev_state
& BIT(virq
)))
1138 handle_nested_irq(irq
);
1142 dev
->gpio_prev_state
= gpio_mask
;
1146 schedule_delayed_work(&dev
->gpio_poll_worker
, 10);
1150 static unsigned int cp2112_gpio_irq_startup(struct irq_data
*d
)
1152 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1153 struct cp2112_device
*dev
= gpiochip_get_data(gc
);
1155 INIT_DELAYED_WORK(&dev
->gpio_poll_worker
, cp2112_gpio_poll_callback
);
1157 if (!dev
->gpio_poll
) {
1158 dev
->gpio_poll
= true;
1159 schedule_delayed_work(&dev
->gpio_poll_worker
, 0);
1162 cp2112_gpio_irq_unmask(d
);
1166 static void cp2112_gpio_irq_shutdown(struct irq_data
*d
)
1168 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1169 struct cp2112_device
*dev
= gpiochip_get_data(gc
);
1171 cancel_delayed_work_sync(&dev
->gpio_poll_worker
);
1174 static int cp2112_gpio_irq_type(struct irq_data
*d
, unsigned int type
)
1179 static int __maybe_unused
cp2112_allocate_irq(struct cp2112_device
*dev
,
1187 dev
->desc
[pin
] = gpiochip_request_own_desc(&dev
->gc
, pin
,
1191 if (IS_ERR(dev
->desc
[pin
])) {
1192 dev_err(dev
->gc
.parent
, "Failed to request GPIO\n");
1193 return PTR_ERR(dev
->desc
[pin
]);
1196 ret
= cp2112_gpio_direction_input(&dev
->gc
, pin
);
1198 dev_err(dev
->gc
.parent
, "Failed to set GPIO to input dir\n");
1202 ret
= gpiochip_lock_as_irq(&dev
->gc
, pin
);
1204 dev_err(dev
->gc
.parent
, "Failed to lock GPIO as interrupt\n");
1208 ret
= gpiod_to_irq(dev
->desc
[pin
]);
1210 dev_err(dev
->gc
.parent
, "Failed to translate GPIO to IRQ\n");
1217 gpiochip_unlock_as_irq(&dev
->gc
, pin
);
1219 gpiochip_free_own_desc(dev
->desc
[pin
]);
1220 dev
->desc
[pin
] = NULL
;
1224 static int cp2112_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
1226 struct cp2112_device
*dev
;
1228 struct cp2112_smbus_config_report config
;
1229 struct gpio_irq_chip
*girq
;
1232 dev
= devm_kzalloc(&hdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
1236 dev
->in_out_buffer
= devm_kzalloc(&hdev
->dev
, CP2112_REPORT_MAX_LENGTH
,
1238 if (!dev
->in_out_buffer
)
1241 mutex_init(&dev
->lock
);
1243 ret
= hid_parse(hdev
);
1245 hid_err(hdev
, "parse failed\n");
1249 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
1251 hid_err(hdev
, "hw start failed\n");
1255 ret
= hid_hw_open(hdev
);
1257 hid_err(hdev
, "hw open failed\n");
1261 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
1263 hid_err(hdev
, "power management error: %d\n", ret
);
1267 ret
= cp2112_hid_get(hdev
, CP2112_GET_VERSION_INFO
, buf
, sizeof(buf
),
1268 HID_FEATURE_REPORT
);
1269 if (ret
!= sizeof(buf
)) {
1270 hid_err(hdev
, "error requesting version\n");
1273 goto err_power_normal
;
1276 hid_info(hdev
, "Part Number: 0x%02X Device Version: 0x%02X\n",
1279 ret
= cp2112_hid_get(hdev
, CP2112_SMBUS_CONFIG
, (u8
*)&config
,
1280 sizeof(config
), HID_FEATURE_REPORT
);
1281 if (ret
!= sizeof(config
)) {
1282 hid_err(hdev
, "error requesting SMBus config\n");
1285 goto err_power_normal
;
1288 config
.retry_time
= cpu_to_be16(1);
1290 ret
= cp2112_hid_output(hdev
, (u8
*)&config
, sizeof(config
),
1291 HID_FEATURE_REPORT
);
1292 if (ret
!= sizeof(config
)) {
1293 hid_err(hdev
, "error setting SMBus config\n");
1296 goto err_power_normal
;
1299 hid_set_drvdata(hdev
, (void *)dev
);
1301 dev
->adap
.owner
= THIS_MODULE
;
1302 dev
->adap
.class = I2C_CLASS_HWMON
;
1303 dev
->adap
.algo
= &smbus_algorithm
;
1304 dev
->adap
.algo_data
= dev
;
1305 dev
->adap
.dev
.parent
= &hdev
->dev
;
1306 snprintf(dev
->adap
.name
, sizeof(dev
->adap
.name
),
1307 "CP2112 SMBus Bridge on hidraw%d",
1308 ((struct hidraw
*)hdev
->hidraw
)->minor
);
1309 dev
->hwversion
= buf
[2];
1310 init_waitqueue_head(&dev
->wait
);
1312 hid_device_io_start(hdev
);
1313 ret
= i2c_add_adapter(&dev
->adap
);
1314 hid_device_io_stop(hdev
);
1317 hid_err(hdev
, "error registering i2c adapter\n");
1318 goto err_power_normal
;
1321 hid_dbg(hdev
, "adapter registered\n");
1323 dev
->gc
.label
= "cp2112_gpio";
1324 dev
->gc
.direction_input
= cp2112_gpio_direction_input
;
1325 dev
->gc
.direction_output
= cp2112_gpio_direction_output
;
1326 dev
->gc
.set
= cp2112_gpio_set
;
1327 dev
->gc
.get
= cp2112_gpio_get
;
1330 dev
->gc
.can_sleep
= 1;
1331 dev
->gc
.parent
= &hdev
->dev
;
1333 dev
->irq
.name
= "cp2112-gpio";
1334 dev
->irq
.irq_startup
= cp2112_gpio_irq_startup
;
1335 dev
->irq
.irq_shutdown
= cp2112_gpio_irq_shutdown
;
1336 dev
->irq
.irq_ack
= cp2112_gpio_irq_ack
;
1337 dev
->irq
.irq_mask
= cp2112_gpio_irq_mask
;
1338 dev
->irq
.irq_unmask
= cp2112_gpio_irq_unmask
;
1339 dev
->irq
.irq_set_type
= cp2112_gpio_irq_type
;
1340 dev
->irq
.flags
= IRQCHIP_MASK_ON_SUSPEND
;
1342 girq
= &dev
->gc
.irq
;
1343 girq
->chip
= &dev
->irq
;
1344 /* The event comes from the outside so no parent handler */
1345 girq
->parent_handler
= NULL
;
1346 girq
->num_parents
= 0;
1347 girq
->parents
= NULL
;
1348 girq
->default_type
= IRQ_TYPE_NONE
;
1349 girq
->handler
= handle_simple_irq
;
1351 ret
= gpiochip_add_data(&dev
->gc
, dev
);
1353 hid_err(hdev
, "error registering gpio chip\n");
1357 ret
= sysfs_create_group(&hdev
->dev
.kobj
, &cp2112_attr_group
);
1359 hid_err(hdev
, "error creating sysfs attrs\n");
1360 goto err_gpiochip_remove
;
1363 chmod_sysfs_attrs(hdev
);
1364 hid_hw_power(hdev
, PM_HINT_NORMAL
);
1368 err_gpiochip_remove
:
1369 gpiochip_remove(&dev
->gc
);
1371 i2c_del_adapter(&dev
->adap
);
1373 hid_hw_power(hdev
, PM_HINT_NORMAL
);
1381 static void cp2112_remove(struct hid_device
*hdev
)
1383 struct cp2112_device
*dev
= hid_get_drvdata(hdev
);
1386 sysfs_remove_group(&hdev
->dev
.kobj
, &cp2112_attr_group
);
1387 i2c_del_adapter(&dev
->adap
);
1389 if (dev
->gpio_poll
) {
1390 dev
->gpio_poll
= false;
1391 cancel_delayed_work_sync(&dev
->gpio_poll_worker
);
1394 for (i
= 0; i
< ARRAY_SIZE(dev
->desc
); i
++) {
1395 gpiochip_unlock_as_irq(&dev
->gc
, i
);
1396 gpiochip_free_own_desc(dev
->desc
[i
]);
1399 gpiochip_remove(&dev
->gc
);
1400 /* i2c_del_adapter has finished removing all i2c devices from our
1401 * adapter. Well behaved devices should no longer call our cp2112_xfer
1402 * and should have waited for any pending calls to finish. It has also
1403 * waited for device_unregister(&adap->dev) to complete. Therefore we
1404 * can safely free our struct cp2112_device.
1410 static int cp2112_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
1413 struct cp2112_device
*dev
= hid_get_drvdata(hdev
);
1414 struct cp2112_xfer_status_report
*xfer
= (void *)data
;
1417 case CP2112_TRANSFER_STATUS_RESPONSE
:
1418 hid_dbg(hdev
, "xfer status: %02x %02x %04x %04x\n",
1419 xfer
->status0
, xfer
->status1
,
1420 be16_to_cpu(xfer
->retries
), be16_to_cpu(xfer
->length
));
1422 switch (xfer
->status0
) {
1424 dev
->xfer_status
= -EAGAIN
;
1427 dev
->xfer_status
= -EBUSY
;
1429 case STATUS0_COMPLETE
:
1430 dev
->xfer_status
= be16_to_cpu(xfer
->length
);
1433 switch (xfer
->status1
) {
1434 case STATUS1_TIMEOUT_NACK
:
1435 case STATUS1_TIMEOUT_BUS
:
1436 dev
->xfer_status
= -ETIMEDOUT
;
1439 dev
->xfer_status
= -EIO
;
1444 dev
->xfer_status
= -EINVAL
;
1448 atomic_set(&dev
->xfer_avail
, 1);
1450 case CP2112_DATA_READ_RESPONSE
:
1451 hid_dbg(hdev
, "read response: %02x %02x\n", data
[1], data
[2]);
1453 dev
->read_length
= data
[2];
1454 if (dev
->read_length
> sizeof(dev
->read_data
))
1455 dev
->read_length
= sizeof(dev
->read_data
);
1457 memcpy(dev
->read_data
, &data
[3], dev
->read_length
);
1458 atomic_set(&dev
->read_avail
, 1);
1461 hid_err(hdev
, "unknown report\n");
1466 wake_up_interruptible(&dev
->wait
);
1470 static struct hid_driver cp2112_driver
= {
1472 .id_table
= cp2112_devices
,
1473 .probe
= cp2112_probe
,
1474 .remove
= cp2112_remove
,
1475 .raw_event
= cp2112_raw_event
,
1478 module_hid_driver(cp2112_driver
);
1479 MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge");
1480 MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
1481 MODULE_LICENSE("GPL");