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 * http://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.h>
20 #include <linux/gpio/driver.h>
21 #include <linux/hid.h>
22 #include <linux/hidraw.h>
23 #include <linux/i2c.h>
24 #include <linux/module.h>
25 #include <linux/nls.h>
26 #include <linux/usb/ch9.h>
29 #define CP2112_REPORT_MAX_LENGTH 64
30 #define CP2112_GPIO_CONFIG_LENGTH 5
31 #define CP2112_GPIO_GET_LENGTH 2
32 #define CP2112_GPIO_SET_LENGTH 3
35 CP2112_GPIO_CONFIG
= 0x02,
36 CP2112_GPIO_GET
= 0x03,
37 CP2112_GPIO_SET
= 0x04,
38 CP2112_GET_VERSION_INFO
= 0x05,
39 CP2112_SMBUS_CONFIG
= 0x06,
40 CP2112_DATA_READ_REQUEST
= 0x10,
41 CP2112_DATA_WRITE_READ_REQUEST
= 0x11,
42 CP2112_DATA_READ_FORCE_SEND
= 0x12,
43 CP2112_DATA_READ_RESPONSE
= 0x13,
44 CP2112_DATA_WRITE_REQUEST
= 0x14,
45 CP2112_TRANSFER_STATUS_REQUEST
= 0x15,
46 CP2112_TRANSFER_STATUS_RESPONSE
= 0x16,
47 CP2112_CANCEL_TRANSFER
= 0x17,
48 CP2112_LOCK_BYTE
= 0x20,
49 CP2112_USB_CONFIG
= 0x21,
50 CP2112_MANUFACTURER_STRING
= 0x22,
51 CP2112_PRODUCT_STRING
= 0x23,
52 CP2112_SERIAL_STRING
= 0x24,
58 STATUS0_COMPLETE
= 0x02,
63 STATUS1_TIMEOUT_NACK
= 0x00,
64 STATUS1_TIMEOUT_BUS
= 0x01,
65 STATUS1_ARBITRATION_LOST
= 0x02,
66 STATUS1_READ_INCOMPLETE
= 0x03,
67 STATUS1_WRITE_INCOMPLETE
= 0x04,
68 STATUS1_SUCCESS
= 0x05,
71 struct cp2112_smbus_config_report
{
72 u8 report
; /* CP2112_SMBUS_CONFIG */
73 __be32 clock_speed
; /* Hz */
74 u8 device_address
; /* Stored in the upper 7 bits */
75 u8 auto_send_read
; /* 1 = enabled, 0 = disabled */
76 __be16 write_timeout
; /* ms, 0 = no timeout */
77 __be16 read_timeout
; /* ms, 0 = no timeout */
78 u8 scl_low_timeout
; /* 1 = enabled, 0 = disabled */
79 __be16 retry_time
; /* # of retries, 0 = no limit */
82 struct cp2112_usb_config_report
{
83 u8 report
; /* CP2112_USB_CONFIG */
84 __le16 vid
; /* Vendor ID */
85 __le16 pid
; /* Product ID */
86 u8 max_power
; /* Power requested in 2mA units */
87 u8 power_mode
; /* 0x00 = bus powered
88 0x01 = self powered & regulator off
89 0x02 = self powered & regulator on */
92 u8 mask
; /* What fields to program */
95 struct cp2112_read_req_report
{
96 u8 report
; /* CP2112_DATA_READ_REQUEST */
101 struct cp2112_write_read_req_report
{
102 u8 report
; /* CP2112_DATA_WRITE_READ_REQUEST */
105 u8 target_address_length
;
106 u8 target_address
[16];
109 struct cp2112_write_req_report
{
110 u8 report
; /* CP2112_DATA_WRITE_REQUEST */
116 struct cp2112_force_read_report
{
117 u8 report
; /* CP2112_DATA_READ_FORCE_SEND */
121 struct cp2112_xfer_status_report
{
122 u8 report
; /* CP2112_TRANSFER_STATUS_RESPONSE */
123 u8 status0
; /* STATUS0_* */
124 u8 status1
; /* STATUS1_* */
129 struct cp2112_string_report
{
130 u8 dummy
; /* force .string to be aligned */
131 u8 report
; /* CP2112_*_STRING */
132 u8 length
; /* length in bytes of everyting after .report */
133 u8 type
; /* USB_DT_STRING */
134 wchar_t string
[30]; /* UTF16_LITTLE_ENDIAN string */
137 /* Number of times to request transfer status before giving up waiting for a
138 transfer to complete. This may need to be changed if SMBUS clock, retries,
139 or read/write/scl_low timeout settings are changed. */
140 static const int XFER_STATUS_RETRIES
= 10;
142 /* Time in ms to wait for a CP2112_DATA_READ_RESPONSE or
143 CP2112_TRANSFER_STATUS_RESPONSE. */
144 static const int RESPONSE_TIMEOUT
= 50;
146 static const struct hid_device_id cp2112_devices
[] = {
147 { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL
, USB_DEVICE_ID_CYGNAL_CP2112
) },
150 MODULE_DEVICE_TABLE(hid
, cp2112_devices
);
152 struct cp2112_device
{
153 struct i2c_adapter adap
;
154 struct hid_device
*hdev
;
155 wait_queue_head_t wait
;
166 struct gpio_desc
*desc
[8];
168 struct delayed_work gpio_poll_worker
;
169 unsigned long irq_mask
;
173 static int gpio_push_pull
= 0xFF;
174 module_param(gpio_push_pull
, int, S_IRUGO
| S_IWUSR
);
175 MODULE_PARM_DESC(gpio_push_pull
, "GPIO push-pull configuration bitmask");
177 static int cp2112_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
179 struct cp2112_device
*dev
= gpiochip_get_data(chip
);
180 struct hid_device
*hdev
= dev
->hdev
;
181 u8
*buf
= dev
->in_out_buffer
;
184 mutex_lock(&dev
->lock
);
186 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
187 CP2112_GPIO_CONFIG_LENGTH
, HID_FEATURE_REPORT
,
189 if (ret
!= CP2112_GPIO_CONFIG_LENGTH
) {
190 hid_err(hdev
, "error requesting GPIO config: %d\n", ret
);
196 buf
[1] &= ~(1 << offset
);
197 buf
[2] = gpio_push_pull
;
199 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
200 CP2112_GPIO_CONFIG_LENGTH
, HID_FEATURE_REPORT
,
202 if (ret
!= CP2112_GPIO_CONFIG_LENGTH
) {
203 hid_err(hdev
, "error setting GPIO config: %d\n", ret
);
212 mutex_unlock(&dev
->lock
);
216 static void cp2112_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
218 struct cp2112_device
*dev
= gpiochip_get_data(chip
);
219 struct hid_device
*hdev
= dev
->hdev
;
220 u8
*buf
= dev
->in_out_buffer
;
223 mutex_lock(&dev
->lock
);
225 buf
[0] = CP2112_GPIO_SET
;
226 buf
[1] = value
? 0xff : 0;
227 buf
[2] = 1 << offset
;
229 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_SET
, buf
,
230 CP2112_GPIO_SET_LENGTH
, HID_FEATURE_REPORT
,
233 hid_err(hdev
, "error setting GPIO values: %d\n", ret
);
235 mutex_unlock(&dev
->lock
);
238 static int cp2112_gpio_get_all(struct gpio_chip
*chip
)
240 struct cp2112_device
*dev
= gpiochip_get_data(chip
);
241 struct hid_device
*hdev
= dev
->hdev
;
242 u8
*buf
= dev
->in_out_buffer
;
245 mutex_lock(&dev
->lock
);
247 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_GET
, buf
,
248 CP2112_GPIO_GET_LENGTH
, HID_FEATURE_REPORT
,
250 if (ret
!= CP2112_GPIO_GET_LENGTH
) {
251 hid_err(hdev
, "error requesting GPIO values: %d\n", ret
);
252 ret
= ret
< 0 ? ret
: -EIO
;
259 mutex_unlock(&dev
->lock
);
264 static int cp2112_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
268 ret
= cp2112_gpio_get_all(chip
);
272 return (ret
>> offset
) & 1;
275 static int cp2112_gpio_direction_output(struct gpio_chip
*chip
,
276 unsigned offset
, int value
)
278 struct cp2112_device
*dev
= gpiochip_get_data(chip
);
279 struct hid_device
*hdev
= dev
->hdev
;
280 u8
*buf
= dev
->in_out_buffer
;
283 mutex_lock(&dev
->lock
);
285 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
286 CP2112_GPIO_CONFIG_LENGTH
, HID_FEATURE_REPORT
,
288 if (ret
!= CP2112_GPIO_CONFIG_LENGTH
) {
289 hid_err(hdev
, "error requesting GPIO config: %d\n", ret
);
293 buf
[1] |= 1 << offset
;
294 buf
[2] = gpio_push_pull
;
296 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
297 CP2112_GPIO_CONFIG_LENGTH
, HID_FEATURE_REPORT
,
300 hid_err(hdev
, "error setting GPIO config: %d\n", ret
);
304 mutex_unlock(&dev
->lock
);
307 * Set gpio value when output direction is already set,
308 * as specified in AN495, Rev. 0.2, cpt. 4.4
310 cp2112_gpio_set(chip
, offset
, value
);
315 mutex_unlock(&dev
->lock
);
316 return ret
< 0 ? ret
: -EIO
;
319 static int cp2112_hid_get(struct hid_device
*hdev
, unsigned char report_number
,
320 u8
*data
, size_t count
, unsigned char report_type
)
325 buf
= kmalloc(count
, GFP_KERNEL
);
329 ret
= hid_hw_raw_request(hdev
, report_number
, buf
, count
,
330 report_type
, HID_REQ_GET_REPORT
);
331 memcpy(data
, buf
, count
);
336 static int cp2112_hid_output(struct hid_device
*hdev
, u8
*data
, size_t count
,
337 unsigned char report_type
)
342 buf
= kmemdup(data
, count
, GFP_KERNEL
);
346 if (report_type
== HID_OUTPUT_REPORT
)
347 ret
= hid_hw_output_report(hdev
, buf
, count
);
349 ret
= hid_hw_raw_request(hdev
, buf
[0], buf
, count
, report_type
,
356 static int cp2112_wait(struct cp2112_device
*dev
, atomic_t
*avail
)
360 /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a
361 * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to
362 * come in cp2112_raw_event or timeout. There will only be one of these
363 * in flight at any one time. The timeout is extremely large and is a
364 * last resort if the CP2112 has died. If we do timeout we don't expect
365 * to receive the response which would cause data races, it's not like
366 * we can do anything about it anyway.
368 ret
= wait_event_interruptible_timeout(dev
->wait
,
369 atomic_read(avail
), msecs_to_jiffies(RESPONSE_TIMEOUT
));
370 if (-ERESTARTSYS
== ret
)
375 atomic_set(avail
, 0);
379 static int cp2112_xfer_status(struct cp2112_device
*dev
)
381 struct hid_device
*hdev
= dev
->hdev
;
385 buf
[0] = CP2112_TRANSFER_STATUS_REQUEST
;
387 atomic_set(&dev
->xfer_avail
, 0);
389 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
391 hid_warn(hdev
, "Error requesting status: %d\n", ret
);
395 ret
= cp2112_wait(dev
, &dev
->xfer_avail
);
399 return dev
->xfer_status
;
402 static int cp2112_read(struct cp2112_device
*dev
, u8
*data
, size_t size
)
404 struct hid_device
*hdev
= dev
->hdev
;
405 struct cp2112_force_read_report report
;
408 if (size
> sizeof(dev
->read_data
))
409 size
= sizeof(dev
->read_data
);
410 report
.report
= CP2112_DATA_READ_FORCE_SEND
;
411 report
.length
= cpu_to_be16(size
);
413 atomic_set(&dev
->read_avail
, 0);
415 ret
= cp2112_hid_output(hdev
, &report
.report
, sizeof(report
),
418 hid_warn(hdev
, "Error requesting data: %d\n", ret
);
422 ret
= cp2112_wait(dev
, &dev
->read_avail
);
426 hid_dbg(hdev
, "read %d of %zd bytes requested\n",
427 dev
->read_length
, size
);
429 if (size
> dev
->read_length
)
430 size
= dev
->read_length
;
432 memcpy(data
, dev
->read_data
, size
);
433 return dev
->read_length
;
436 static int cp2112_read_req(void *buf
, u8 slave_address
, u16 length
)
438 struct cp2112_read_req_report
*report
= buf
;
440 if (length
< 1 || length
> 512)
443 report
->report
= CP2112_DATA_READ_REQUEST
;
444 report
->slave_address
= slave_address
<< 1;
445 report
->length
= cpu_to_be16(length
);
446 return sizeof(*report
);
449 static int cp2112_write_read_req(void *buf
, u8 slave_address
, u16 length
,
450 u8 command
, u8
*data
, u8 data_length
)
452 struct cp2112_write_read_req_report
*report
= buf
;
454 if (length
< 1 || length
> 512
455 || data_length
> sizeof(report
->target_address
) - 1)
458 report
->report
= CP2112_DATA_WRITE_READ_REQUEST
;
459 report
->slave_address
= slave_address
<< 1;
460 report
->length
= cpu_to_be16(length
);
461 report
->target_address_length
= data_length
+ 1;
462 report
->target_address
[0] = command
;
463 memcpy(&report
->target_address
[1], data
, data_length
);
464 return data_length
+ 6;
467 static int cp2112_write_req(void *buf
, u8 slave_address
, u8 command
, u8
*data
,
470 struct cp2112_write_req_report
*report
= buf
;
472 if (data_length
> sizeof(report
->data
) - 1)
475 report
->report
= CP2112_DATA_WRITE_REQUEST
;
476 report
->slave_address
= slave_address
<< 1;
477 report
->length
= data_length
+ 1;
478 report
->data
[0] = command
;
479 memcpy(&report
->data
[1], data
, data_length
);
480 return data_length
+ 4;
483 static int cp2112_i2c_write_req(void *buf
, u8 slave_address
, u8
*data
,
486 struct cp2112_write_req_report
*report
= buf
;
488 if (data_length
> sizeof(report
->data
))
491 report
->report
= CP2112_DATA_WRITE_REQUEST
;
492 report
->slave_address
= slave_address
<< 1;
493 report
->length
= data_length
;
494 memcpy(report
->data
, data
, data_length
);
495 return data_length
+ 3;
498 static int cp2112_i2c_write_read_req(void *buf
, u8 slave_address
,
499 u8
*addr
, int addr_length
,
502 struct cp2112_write_read_req_report
*report
= buf
;
504 if (read_length
< 1 || read_length
> 512 ||
505 addr_length
> sizeof(report
->target_address
))
508 report
->report
= CP2112_DATA_WRITE_READ_REQUEST
;
509 report
->slave_address
= slave_address
<< 1;
510 report
->length
= cpu_to_be16(read_length
);
511 report
->target_address_length
= addr_length
;
512 memcpy(report
->target_address
, addr
, addr_length
);
513 return addr_length
+ 5;
516 static int cp2112_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
519 struct cp2112_device
*dev
= (struct cp2112_device
*)adap
->algo_data
;
520 struct hid_device
*hdev
= dev
->hdev
;
523 ssize_t read_length
= 0;
525 unsigned int retries
;
528 hid_dbg(hdev
, "I2C %d messages\n", num
);
531 if (msgs
->flags
& I2C_M_RD
) {
532 hid_dbg(hdev
, "I2C read %#04x len %d\n",
533 msgs
->addr
, msgs
->len
);
534 read_length
= msgs
->len
;
535 read_buf
= msgs
->buf
;
536 count
= cp2112_read_req(buf
, msgs
->addr
, msgs
->len
);
538 hid_dbg(hdev
, "I2C write %#04x len %d\n",
539 msgs
->addr
, msgs
->len
);
540 count
= cp2112_i2c_write_req(buf
, msgs
->addr
,
541 msgs
->buf
, msgs
->len
);
545 } else if (dev
->hwversion
> 1 && /* no repeated start in rev 1 */
547 msgs
[0].addr
== msgs
[1].addr
&&
548 !(msgs
[0].flags
& I2C_M_RD
) && (msgs
[1].flags
& I2C_M_RD
)) {
549 hid_dbg(hdev
, "I2C write-read %#04x wlen %d rlen %d\n",
550 msgs
[0].addr
, msgs
[0].len
, msgs
[1].len
);
551 read_length
= msgs
[1].len
;
552 read_buf
= msgs
[1].buf
;
553 count
= cp2112_i2c_write_read_req(buf
, msgs
[0].addr
,
554 msgs
[0].buf
, msgs
[0].len
, msgs
[1].len
);
559 "Multi-message I2C transactions not supported\n");
563 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
565 hid_err(hdev
, "power management error: %d\n", ret
);
569 ret
= cp2112_hid_output(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
571 hid_warn(hdev
, "Error starting transaction: %d\n", ret
);
575 for (retries
= 0; retries
< XFER_STATUS_RETRIES
; ++retries
) {
576 ret
= cp2112_xfer_status(dev
);
584 if (XFER_STATUS_RETRIES
<= retries
) {
585 hid_warn(hdev
, "Transfer timed out, cancelling.\n");
586 buf
[0] = CP2112_CANCEL_TRANSFER
;
589 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
591 hid_warn(hdev
, "Error cancelling transaction: %d\n",
598 for (count
= 0; count
< read_length
;) {
599 ret
= cp2112_read(dev
, read_buf
+ count
, read_length
- count
);
603 hid_err(hdev
, "read returned 0\n");
608 if (count
> read_length
) {
610 * The hardware returned too much data.
611 * This is mostly harmless because cp2112_read()
612 * has a limit check so didn't overrun our
613 * buffer. Nevertheless, we return an error
614 * because something is seriously wrong and
615 * it shouldn't go unnoticed.
617 hid_err(hdev
, "long read: %d > %zd\n",
618 ret
, read_length
- count
+ ret
);
624 /* return the number of transferred messages */
628 hid_hw_power(hdev
, PM_HINT_NORMAL
);
629 hid_dbg(hdev
, "I2C transfer finished: %d\n", ret
);
633 static int cp2112_xfer(struct i2c_adapter
*adap
, u16 addr
,
634 unsigned short flags
, char read_write
, u8 command
,
635 int size
, union i2c_smbus_data
*data
)
637 struct cp2112_device
*dev
= (struct cp2112_device
*)adap
->algo_data
;
638 struct hid_device
*hdev
= dev
->hdev
;
642 size_t read_length
= 0;
643 unsigned int retries
;
646 hid_dbg(hdev
, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n",
647 read_write
== I2C_SMBUS_WRITE
? "write" : "read",
648 addr
, flags
, command
, size
);
654 if (I2C_SMBUS_READ
== read_write
)
655 count
= cp2112_read_req(buf
, addr
, read_length
);
657 count
= cp2112_write_req(buf
, addr
, command
, NULL
,
660 case I2C_SMBUS_BYTE_DATA
:
663 if (I2C_SMBUS_READ
== read_write
)
664 count
= cp2112_write_read_req(buf
, addr
, read_length
,
667 count
= cp2112_write_req(buf
, addr
, command
,
670 case I2C_SMBUS_WORD_DATA
:
672 word
= cpu_to_le16(data
->word
);
674 if (I2C_SMBUS_READ
== read_write
)
675 count
= cp2112_write_read_req(buf
, addr
, read_length
,
678 count
= cp2112_write_req(buf
, addr
, command
,
681 case I2C_SMBUS_PROC_CALL
:
682 size
= I2C_SMBUS_WORD_DATA
;
683 read_write
= I2C_SMBUS_READ
;
685 word
= cpu_to_le16(data
->word
);
687 count
= cp2112_write_read_req(buf
, addr
, read_length
, command
,
690 case I2C_SMBUS_I2C_BLOCK_DATA
:
691 if (read_write
== I2C_SMBUS_READ
) {
692 read_length
= data
->block
[0];
693 count
= cp2112_write_read_req(buf
, addr
, read_length
,
696 count
= cp2112_write_req(buf
, addr
, command
,
701 case I2C_SMBUS_BLOCK_DATA
:
702 if (I2C_SMBUS_READ
== read_write
) {
703 count
= cp2112_write_read_req(buf
, addr
,
707 count
= cp2112_write_req(buf
, addr
, command
,
712 case I2C_SMBUS_BLOCK_PROC_CALL
:
713 size
= I2C_SMBUS_BLOCK_DATA
;
714 read_write
= I2C_SMBUS_READ
;
716 count
= cp2112_write_read_req(buf
, addr
, I2C_SMBUS_BLOCK_MAX
,
717 command
, data
->block
,
721 hid_warn(hdev
, "Unsupported transaction %d\n", size
);
728 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
730 hid_err(hdev
, "power management error: %d\n", ret
);
734 ret
= cp2112_hid_output(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
736 hid_warn(hdev
, "Error starting transaction: %d\n", ret
);
740 for (retries
= 0; retries
< XFER_STATUS_RETRIES
; ++retries
) {
741 ret
= cp2112_xfer_status(dev
);
749 if (XFER_STATUS_RETRIES
<= retries
) {
750 hid_warn(hdev
, "Transfer timed out, cancelling.\n");
751 buf
[0] = CP2112_CANCEL_TRANSFER
;
754 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
756 hid_warn(hdev
, "Error cancelling transaction: %d\n",
763 if (I2C_SMBUS_WRITE
== read_write
) {
768 if (I2C_SMBUS_BLOCK_DATA
== size
)
771 ret
= cp2112_read(dev
, buf
, read_length
);
774 if (ret
!= read_length
) {
775 hid_warn(hdev
, "short read: %d < %zd\n", ret
, read_length
);
782 case I2C_SMBUS_BYTE_DATA
:
785 case I2C_SMBUS_WORD_DATA
:
786 data
->word
= le16_to_cpup((__le16
*)buf
);
788 case I2C_SMBUS_I2C_BLOCK_DATA
:
789 memcpy(data
->block
+ 1, buf
, read_length
);
791 case I2C_SMBUS_BLOCK_DATA
:
792 if (read_length
> I2C_SMBUS_BLOCK_MAX
) {
797 memcpy(data
->block
, buf
, read_length
);
803 hid_hw_power(hdev
, PM_HINT_NORMAL
);
804 hid_dbg(hdev
, "transfer finished: %d\n", ret
);
808 static u32
cp2112_functionality(struct i2c_adapter
*adap
)
810 return I2C_FUNC_I2C
|
811 I2C_FUNC_SMBUS_BYTE
|
812 I2C_FUNC_SMBUS_BYTE_DATA
|
813 I2C_FUNC_SMBUS_WORD_DATA
|
814 I2C_FUNC_SMBUS_BLOCK_DATA
|
815 I2C_FUNC_SMBUS_I2C_BLOCK
|
816 I2C_FUNC_SMBUS_PROC_CALL
|
817 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
;
820 static const struct i2c_algorithm smbus_algorithm
= {
821 .master_xfer
= cp2112_i2c_xfer
,
822 .smbus_xfer
= cp2112_xfer
,
823 .functionality
= cp2112_functionality
,
826 static int cp2112_get_usb_config(struct hid_device
*hdev
,
827 struct cp2112_usb_config_report
*cfg
)
831 ret
= cp2112_hid_get(hdev
, CP2112_USB_CONFIG
, (u8
*)cfg
, sizeof(*cfg
),
833 if (ret
!= sizeof(*cfg
)) {
834 hid_err(hdev
, "error reading usb config: %d\n", ret
);
843 static int cp2112_set_usb_config(struct hid_device
*hdev
,
844 struct cp2112_usb_config_report
*cfg
)
848 BUG_ON(cfg
->report
!= CP2112_USB_CONFIG
);
850 ret
= cp2112_hid_output(hdev
, (u8
*)cfg
, sizeof(*cfg
),
852 if (ret
!= sizeof(*cfg
)) {
853 hid_err(hdev
, "error writing usb config: %d\n", ret
);
862 static void chmod_sysfs_attrs(struct hid_device
*hdev
);
864 #define CP2112_CONFIG_ATTR(name, store, format, ...) \
865 static ssize_t name##_store(struct device *kdev, \
866 struct device_attribute *attr, const char *buf, \
869 struct hid_device *hdev = to_hid_device(kdev); \
870 struct cp2112_usb_config_report cfg; \
871 int ret = cp2112_get_usb_config(hdev, &cfg); \
875 ret = cp2112_set_usb_config(hdev, &cfg); \
878 chmod_sysfs_attrs(hdev); \
881 static ssize_t name##_show(struct device *kdev, \
882 struct device_attribute *attr, char *buf) \
884 struct hid_device *hdev = to_hid_device(kdev); \
885 struct cp2112_usb_config_report cfg; \
886 int ret = cp2112_get_usb_config(hdev, &cfg); \
889 return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \
891 static DEVICE_ATTR_RW(name);
893 CP2112_CONFIG_ATTR(vendor_id
, ({
896 if (sscanf(buf
, "%hi", &vid
) != 1)
899 cfg
.vid
= cpu_to_le16(vid
);
901 }), "0x%04x\n", le16_to_cpu(cfg
.vid
));
903 CP2112_CONFIG_ATTR(product_id
, ({
906 if (sscanf(buf
, "%hi", &pid
) != 1)
909 cfg
.pid
= cpu_to_le16(pid
);
911 }), "0x%04x\n", le16_to_cpu(cfg
.pid
));
913 CP2112_CONFIG_ATTR(max_power
, ({
916 if (sscanf(buf
, "%i", &mA
) != 1)
919 cfg
.max_power
= (mA
+ 1) / 2;
921 }), "%u mA\n", cfg
.max_power
* 2);
923 CP2112_CONFIG_ATTR(power_mode
, ({
924 if (sscanf(buf
, "%hhi", &cfg
.power_mode
) != 1)
928 }), "%u\n", cfg
.power_mode
);
930 CP2112_CONFIG_ATTR(release_version
, ({
931 if (sscanf(buf
, "%hhi.%hhi", &cfg
.release_major
, &cfg
.release_minor
)
936 }), "%u.%u\n", cfg
.release_major
, cfg
.release_minor
);
938 #undef CP2112_CONFIG_ATTR
940 struct cp2112_pstring_attribute
{
941 struct device_attribute attr
;
942 unsigned char report
;
945 static ssize_t
pstr_store(struct device
*kdev
,
946 struct device_attribute
*kattr
, const char *buf
,
949 struct hid_device
*hdev
= to_hid_device(kdev
);
950 struct cp2112_pstring_attribute
*attr
=
951 container_of(kattr
, struct cp2112_pstring_attribute
, attr
);
952 struct cp2112_string_report report
;
955 memset(&report
, 0, sizeof(report
));
957 ret
= utf8s_to_utf16s(buf
, count
, UTF16_LITTLE_ENDIAN
,
958 report
.string
, ARRAY_SIZE(report
.string
));
959 report
.report
= attr
->report
;
960 report
.length
= ret
* sizeof(report
.string
[0]) + 2;
961 report
.type
= USB_DT_STRING
;
963 ret
= cp2112_hid_output(hdev
, &report
.report
, report
.length
+ 1,
965 if (ret
!= report
.length
+ 1) {
966 hid_err(hdev
, "error writing %s string: %d\n", kattr
->attr
.name
,
973 chmod_sysfs_attrs(hdev
);
977 static ssize_t
pstr_show(struct device
*kdev
,
978 struct device_attribute
*kattr
, char *buf
)
980 struct hid_device
*hdev
= to_hid_device(kdev
);
981 struct cp2112_pstring_attribute
*attr
=
982 container_of(kattr
, struct cp2112_pstring_attribute
, attr
);
983 struct cp2112_string_report report
;
987 ret
= cp2112_hid_get(hdev
, attr
->report
, &report
.report
,
988 sizeof(report
) - 1, HID_FEATURE_REPORT
);
990 hid_err(hdev
, "error reading %s string: %d\n", kattr
->attr
.name
,
997 if (report
.length
< 2) {
998 hid_err(hdev
, "invalid %s string length: %d\n",
999 kattr
->attr
.name
, report
.length
);
1003 length
= report
.length
> ret
- 1 ? ret
- 1 : report
.length
;
1004 length
= (length
- 2) / sizeof(report
.string
[0]);
1005 ret
= utf16s_to_utf8s(report
.string
, length
, UTF16_LITTLE_ENDIAN
, buf
,
1011 #define CP2112_PSTR_ATTR(name, _report) \
1012 static struct cp2112_pstring_attribute dev_attr_##name = { \
1013 .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \
1014 .report = _report, \
1017 CP2112_PSTR_ATTR(manufacturer
, CP2112_MANUFACTURER_STRING
);
1018 CP2112_PSTR_ATTR(product
, CP2112_PRODUCT_STRING
);
1019 CP2112_PSTR_ATTR(serial
, CP2112_SERIAL_STRING
);
1021 #undef CP2112_PSTR_ATTR
1023 static const struct attribute_group cp2112_attr_group
= {
1024 .attrs
= (struct attribute
*[]){
1025 &dev_attr_vendor_id
.attr
,
1026 &dev_attr_product_id
.attr
,
1027 &dev_attr_max_power
.attr
,
1028 &dev_attr_power_mode
.attr
,
1029 &dev_attr_release_version
.attr
,
1030 &dev_attr_manufacturer
.attr
.attr
,
1031 &dev_attr_product
.attr
.attr
,
1032 &dev_attr_serial
.attr
.attr
,
1037 /* Chmoding our sysfs attributes is simply a way to expose which fields in the
1038 * PROM have already been programmed. We do not depend on this preventing
1039 * writing to these attributes since the CP2112 will simply ignore writes to
1040 * already-programmed fields. This is why there is no sense in fixing this
1043 static void chmod_sysfs_attrs(struct hid_device
*hdev
)
1045 struct attribute
**attr
;
1049 ret
= cp2112_hid_get(hdev
, CP2112_LOCK_BYTE
, buf
, sizeof(buf
),
1050 HID_FEATURE_REPORT
);
1051 if (ret
!= sizeof(buf
)) {
1052 hid_err(hdev
, "error reading lock byte: %d\n", ret
);
1056 for (attr
= cp2112_attr_group
.attrs
; *attr
; ++attr
) {
1057 umode_t mode
= (buf
[1] & 1) ? S_IWUSR
| S_IRUGO
: S_IRUGO
;
1058 ret
= sysfs_chmod_file(&hdev
->dev
.kobj
, *attr
, mode
);
1060 hid_err(hdev
, "error chmoding sysfs file %s\n",
1066 static void cp2112_gpio_irq_ack(struct irq_data
*d
)
1070 static void cp2112_gpio_irq_mask(struct irq_data
*d
)
1072 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1073 struct cp2112_device
*dev
= gpiochip_get_data(gc
);
1075 __clear_bit(d
->hwirq
, &dev
->irq_mask
);
1078 static void cp2112_gpio_irq_unmask(struct irq_data
*d
)
1080 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1081 struct cp2112_device
*dev
= gpiochip_get_data(gc
);
1083 __set_bit(d
->hwirq
, &dev
->irq_mask
);
1086 static void cp2112_gpio_poll_callback(struct work_struct
*work
)
1088 struct cp2112_device
*dev
= container_of(work
, struct cp2112_device
,
1089 gpio_poll_worker
.work
);
1092 u8 virqs
= (u8
)dev
->irq_mask
;
1096 ret
= cp2112_gpio_get_all(&dev
->gc
);
1097 if (ret
== -ENODEV
) /* the hardware has been disconnected */
1105 virq
= ffs(virqs
) - 1;
1106 virqs
&= ~BIT(virq
);
1108 if (!dev
->gc
.to_irq
)
1111 irq
= dev
->gc
.to_irq(&dev
->gc
, virq
);
1113 d
= irq_get_irq_data(irq
);
1117 irq_type
= irqd_get_trigger_type(d
);
1119 if (gpio_mask
& BIT(virq
)) {
1122 if (irq_type
& IRQ_TYPE_LEVEL_HIGH
)
1123 handle_nested_irq(irq
);
1125 if ((irq_type
& IRQ_TYPE_EDGE_RISING
) &&
1126 !(dev
->gpio_prev_state
& BIT(virq
)))
1127 handle_nested_irq(irq
);
1131 if (irq_type
& IRQ_TYPE_LEVEL_LOW
)
1132 handle_nested_irq(irq
);
1134 if ((irq_type
& IRQ_TYPE_EDGE_FALLING
) &&
1135 (dev
->gpio_prev_state
& BIT(virq
)))
1136 handle_nested_irq(irq
);
1140 dev
->gpio_prev_state
= gpio_mask
;
1144 schedule_delayed_work(&dev
->gpio_poll_worker
, 10);
1148 static unsigned int cp2112_gpio_irq_startup(struct irq_data
*d
)
1150 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1151 struct cp2112_device
*dev
= gpiochip_get_data(gc
);
1153 INIT_DELAYED_WORK(&dev
->gpio_poll_worker
, cp2112_gpio_poll_callback
);
1155 cp2112_gpio_direction_input(gc
, d
->hwirq
);
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 struct irq_chip cp2112_gpio_irqchip
= {
1180 .name
= "cp2112-gpio",
1181 .irq_startup
= cp2112_gpio_irq_startup
,
1182 .irq_shutdown
= cp2112_gpio_irq_shutdown
,
1183 .irq_ack
= cp2112_gpio_irq_ack
,
1184 .irq_mask
= cp2112_gpio_irq_mask
,
1185 .irq_unmask
= cp2112_gpio_irq_unmask
,
1186 .irq_set_type
= cp2112_gpio_irq_type
,
1189 static int __maybe_unused
cp2112_allocate_irq(struct cp2112_device
*dev
,
1197 dev
->desc
[pin
] = gpiochip_request_own_desc(&dev
->gc
, pin
,
1198 "HID/I2C:Event", 0);
1199 if (IS_ERR(dev
->desc
[pin
])) {
1200 dev_err(dev
->gc
.parent
, "Failed to request GPIO\n");
1201 return PTR_ERR(dev
->desc
[pin
]);
1204 ret
= gpiochip_lock_as_irq(&dev
->gc
, pin
);
1206 dev_err(dev
->gc
.parent
, "Failed to lock GPIO as interrupt\n");
1210 ret
= gpiod_to_irq(dev
->desc
[pin
]);
1212 dev_err(dev
->gc
.parent
, "Failed to translate GPIO to IRQ\n");
1219 gpiochip_unlock_as_irq(&dev
->gc
, pin
);
1221 gpiochip_free_own_desc(dev
->desc
[pin
]);
1222 dev
->desc
[pin
] = NULL
;
1226 static int cp2112_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
1228 struct cp2112_device
*dev
;
1230 struct cp2112_smbus_config_report config
;
1233 dev
= devm_kzalloc(&hdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
1237 dev
->in_out_buffer
= devm_kzalloc(&hdev
->dev
, CP2112_REPORT_MAX_LENGTH
,
1239 if (!dev
->in_out_buffer
)
1242 mutex_init(&dev
->lock
);
1244 ret
= hid_parse(hdev
);
1246 hid_err(hdev
, "parse failed\n");
1250 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
1252 hid_err(hdev
, "hw start failed\n");
1256 ret
= hid_hw_open(hdev
);
1258 hid_err(hdev
, "hw open failed\n");
1262 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
1264 hid_err(hdev
, "power management error: %d\n", ret
);
1268 ret
= cp2112_hid_get(hdev
, CP2112_GET_VERSION_INFO
, buf
, sizeof(buf
),
1269 HID_FEATURE_REPORT
);
1270 if (ret
!= sizeof(buf
)) {
1271 hid_err(hdev
, "error requesting version\n");
1274 goto err_power_normal
;
1277 hid_info(hdev
, "Part Number: 0x%02X Device Version: 0x%02X\n",
1280 ret
= cp2112_hid_get(hdev
, CP2112_SMBUS_CONFIG
, (u8
*)&config
,
1281 sizeof(config
), HID_FEATURE_REPORT
);
1282 if (ret
!= sizeof(config
)) {
1283 hid_err(hdev
, "error requesting SMBus config\n");
1286 goto err_power_normal
;
1289 config
.retry_time
= cpu_to_be16(1);
1291 ret
= cp2112_hid_output(hdev
, (u8
*)&config
, sizeof(config
),
1292 HID_FEATURE_REPORT
);
1293 if (ret
!= sizeof(config
)) {
1294 hid_err(hdev
, "error setting SMBus config\n");
1297 goto err_power_normal
;
1300 hid_set_drvdata(hdev
, (void *)dev
);
1302 dev
->adap
.owner
= THIS_MODULE
;
1303 dev
->adap
.class = I2C_CLASS_HWMON
;
1304 dev
->adap
.algo
= &smbus_algorithm
;
1305 dev
->adap
.algo_data
= dev
;
1306 dev
->adap
.dev
.parent
= &hdev
->dev
;
1307 snprintf(dev
->adap
.name
, sizeof(dev
->adap
.name
),
1308 "CP2112 SMBus Bridge on hidraw%d",
1309 ((struct hidraw
*)hdev
->hidraw
)->minor
);
1310 dev
->hwversion
= buf
[2];
1311 init_waitqueue_head(&dev
->wait
);
1313 hid_device_io_start(hdev
);
1314 ret
= i2c_add_adapter(&dev
->adap
);
1315 hid_device_io_stop(hdev
);
1318 hid_err(hdev
, "error registering i2c adapter\n");
1319 goto err_power_normal
;
1322 hid_dbg(hdev
, "adapter registered\n");
1324 dev
->gc
.label
= "cp2112_gpio";
1325 dev
->gc
.direction_input
= cp2112_gpio_direction_input
;
1326 dev
->gc
.direction_output
= cp2112_gpio_direction_output
;
1327 dev
->gc
.set
= cp2112_gpio_set
;
1328 dev
->gc
.get
= cp2112_gpio_get
;
1331 dev
->gc
.can_sleep
= 1;
1332 dev
->gc
.parent
= &hdev
->dev
;
1334 ret
= gpiochip_add_data(&dev
->gc
, dev
);
1336 hid_err(hdev
, "error registering gpio chip\n");
1340 ret
= sysfs_create_group(&hdev
->dev
.kobj
, &cp2112_attr_group
);
1342 hid_err(hdev
, "error creating sysfs attrs\n");
1343 goto err_gpiochip_remove
;
1346 chmod_sysfs_attrs(hdev
);
1347 hid_hw_power(hdev
, PM_HINT_NORMAL
);
1349 ret
= gpiochip_irqchip_add(&dev
->gc
, &cp2112_gpio_irqchip
, 0,
1350 handle_simple_irq
, IRQ_TYPE_NONE
);
1352 dev_err(dev
->gc
.parent
, "failed to add IRQ chip\n");
1353 goto err_sysfs_remove
;
1359 sysfs_remove_group(&hdev
->dev
.kobj
, &cp2112_attr_group
);
1360 err_gpiochip_remove
:
1361 gpiochip_remove(&dev
->gc
);
1363 i2c_del_adapter(&dev
->adap
);
1365 hid_hw_power(hdev
, PM_HINT_NORMAL
);
1373 static void cp2112_remove(struct hid_device
*hdev
)
1375 struct cp2112_device
*dev
= hid_get_drvdata(hdev
);
1378 sysfs_remove_group(&hdev
->dev
.kobj
, &cp2112_attr_group
);
1379 i2c_del_adapter(&dev
->adap
);
1381 if (dev
->gpio_poll
) {
1382 dev
->gpio_poll
= false;
1383 cancel_delayed_work_sync(&dev
->gpio_poll_worker
);
1386 for (i
= 0; i
< ARRAY_SIZE(dev
->desc
); i
++) {
1387 gpiochip_unlock_as_irq(&dev
->gc
, i
);
1388 gpiochip_free_own_desc(dev
->desc
[i
]);
1391 gpiochip_remove(&dev
->gc
);
1392 /* i2c_del_adapter has finished removing all i2c devices from our
1393 * adapter. Well behaved devices should no longer call our cp2112_xfer
1394 * and should have waited for any pending calls to finish. It has also
1395 * waited for device_unregister(&adap->dev) to complete. Therefore we
1396 * can safely free our struct cp2112_device.
1402 static int cp2112_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
1405 struct cp2112_device
*dev
= hid_get_drvdata(hdev
);
1406 struct cp2112_xfer_status_report
*xfer
= (void *)data
;
1409 case CP2112_TRANSFER_STATUS_RESPONSE
:
1410 hid_dbg(hdev
, "xfer status: %02x %02x %04x %04x\n",
1411 xfer
->status0
, xfer
->status1
,
1412 be16_to_cpu(xfer
->retries
), be16_to_cpu(xfer
->length
));
1414 switch (xfer
->status0
) {
1416 dev
->xfer_status
= -EAGAIN
;
1419 dev
->xfer_status
= -EBUSY
;
1421 case STATUS0_COMPLETE
:
1422 dev
->xfer_status
= be16_to_cpu(xfer
->length
);
1425 switch (xfer
->status1
) {
1426 case STATUS1_TIMEOUT_NACK
:
1427 case STATUS1_TIMEOUT_BUS
:
1428 dev
->xfer_status
= -ETIMEDOUT
;
1431 dev
->xfer_status
= -EIO
;
1436 dev
->xfer_status
= -EINVAL
;
1440 atomic_set(&dev
->xfer_avail
, 1);
1442 case CP2112_DATA_READ_RESPONSE
:
1443 hid_dbg(hdev
, "read response: %02x %02x\n", data
[1], data
[2]);
1445 dev
->read_length
= data
[2];
1446 if (dev
->read_length
> sizeof(dev
->read_data
))
1447 dev
->read_length
= sizeof(dev
->read_data
);
1449 memcpy(dev
->read_data
, &data
[3], dev
->read_length
);
1450 atomic_set(&dev
->read_avail
, 1);
1453 hid_err(hdev
, "unknown report\n");
1458 wake_up_interruptible(&dev
->wait
);
1462 static struct hid_driver cp2112_driver
= {
1464 .id_table
= cp2112_devices
,
1465 .probe
= cp2112_probe
,
1466 .remove
= cp2112_remove
,
1467 .raw_event
= cp2112_raw_event
,
1470 module_hid_driver(cp2112_driver
);
1471 MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge");
1472 MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
1473 MODULE_LICENSE("GPL");