2 * hid-cp2112.c - Silicon Labs HID USB to SMBus master bridge
3 * Copyright (c) 2013,2014 Uplogix, Inc.
4 * David Barksdale <dbarksdale@uplogix.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * The Silicon Labs CP2112 chip is a USB HID device which provides an
18 * SMBus controller for talking to slave devices and 8 GPIO pins. The
19 * host communicates with the CP2112 via raw HID reports.
22 * http://www.silabs.com/Support%20Documents/TechnicalDocs/CP2112.pdf
23 * Programming Interface Specification:
24 * http://www.silabs.com/Support%20Documents/TechnicalDocs/AN495.pdf
27 #include <linux/gpio/driver.h>
28 #include <linux/hid.h>
29 #include <linux/i2c.h>
30 #include <linux/module.h>
31 #include <linux/nls.h>
32 #include <linux/usb/ch9.h>
35 #define CP2112_REPORT_MAX_LENGTH 64
36 #define CP2112_GPIO_CONFIG_LENGTH 5
37 #define CP2112_GPIO_GET_LENGTH 2
38 #define CP2112_GPIO_SET_LENGTH 3
41 CP2112_GPIO_CONFIG
= 0x02,
42 CP2112_GPIO_GET
= 0x03,
43 CP2112_GPIO_SET
= 0x04,
44 CP2112_GET_VERSION_INFO
= 0x05,
45 CP2112_SMBUS_CONFIG
= 0x06,
46 CP2112_DATA_READ_REQUEST
= 0x10,
47 CP2112_DATA_WRITE_READ_REQUEST
= 0x11,
48 CP2112_DATA_READ_FORCE_SEND
= 0x12,
49 CP2112_DATA_READ_RESPONSE
= 0x13,
50 CP2112_DATA_WRITE_REQUEST
= 0x14,
51 CP2112_TRANSFER_STATUS_REQUEST
= 0x15,
52 CP2112_TRANSFER_STATUS_RESPONSE
= 0x16,
53 CP2112_CANCEL_TRANSFER
= 0x17,
54 CP2112_LOCK_BYTE
= 0x20,
55 CP2112_USB_CONFIG
= 0x21,
56 CP2112_MANUFACTURER_STRING
= 0x22,
57 CP2112_PRODUCT_STRING
= 0x23,
58 CP2112_SERIAL_STRING
= 0x24,
64 STATUS0_COMPLETE
= 0x02,
69 STATUS1_TIMEOUT_NACK
= 0x00,
70 STATUS1_TIMEOUT_BUS
= 0x01,
71 STATUS1_ARBITRATION_LOST
= 0x02,
72 STATUS1_READ_INCOMPLETE
= 0x03,
73 STATUS1_WRITE_INCOMPLETE
= 0x04,
74 STATUS1_SUCCESS
= 0x05,
77 struct cp2112_smbus_config_report
{
78 u8 report
; /* CP2112_SMBUS_CONFIG */
79 __be32 clock_speed
; /* Hz */
80 u8 device_address
; /* Stored in the upper 7 bits */
81 u8 auto_send_read
; /* 1 = enabled, 0 = disabled */
82 __be16 write_timeout
; /* ms, 0 = no timeout */
83 __be16 read_timeout
; /* ms, 0 = no timeout */
84 u8 scl_low_timeout
; /* 1 = enabled, 0 = disabled */
85 __be16 retry_time
; /* # of retries, 0 = no limit */
88 struct cp2112_usb_config_report
{
89 u8 report
; /* CP2112_USB_CONFIG */
90 __le16 vid
; /* Vendor ID */
91 __le16 pid
; /* Product ID */
92 u8 max_power
; /* Power requested in 2mA units */
93 u8 power_mode
; /* 0x00 = bus powered
94 0x01 = self powered & regulator off
95 0x02 = self powered & regulator on */
98 u8 mask
; /* What fields to program */
101 struct cp2112_read_req_report
{
102 u8 report
; /* CP2112_DATA_READ_REQUEST */
107 struct cp2112_write_read_req_report
{
108 u8 report
; /* CP2112_DATA_WRITE_READ_REQUEST */
111 u8 target_address_length
;
112 u8 target_address
[16];
115 struct cp2112_write_req_report
{
116 u8 report
; /* CP2112_DATA_WRITE_REQUEST */
122 struct cp2112_force_read_report
{
123 u8 report
; /* CP2112_DATA_READ_FORCE_SEND */
127 struct cp2112_xfer_status_report
{
128 u8 report
; /* CP2112_TRANSFER_STATUS_RESPONSE */
129 u8 status0
; /* STATUS0_* */
130 u8 status1
; /* STATUS1_* */
135 struct cp2112_string_report
{
136 u8 dummy
; /* force .string to be aligned */
137 u8 report
; /* CP2112_*_STRING */
138 u8 length
; /* length in bytes of everyting after .report */
139 u8 type
; /* USB_DT_STRING */
140 wchar_t string
[30]; /* UTF16_LITTLE_ENDIAN string */
143 /* Number of times to request transfer status before giving up waiting for a
144 transfer to complete. This may need to be changed if SMBUS clock, retries,
145 or read/write/scl_low timeout settings are changed. */
146 static const int XFER_STATUS_RETRIES
= 10;
148 /* Time in ms to wait for a CP2112_DATA_READ_RESPONSE or
149 CP2112_TRANSFER_STATUS_RESPONSE. */
150 static const int RESPONSE_TIMEOUT
= 50;
152 static const struct hid_device_id cp2112_devices
[] = {
153 { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL
, USB_DEVICE_ID_CYGNAL_CP2112
) },
156 MODULE_DEVICE_TABLE(hid
, cp2112_devices
);
158 struct cp2112_device
{
159 struct i2c_adapter adap
;
160 struct hid_device
*hdev
;
161 wait_queue_head_t wait
;
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(struct gpio_chip
*chip
, unsigned offset
)
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
;
256 ret
= (buf
[1] >> offset
) & 1;
259 mutex_unlock(&dev
->lock
);
264 static int cp2112_gpio_direction_output(struct gpio_chip
*chip
,
265 unsigned offset
, int value
)
267 struct cp2112_device
*dev
= gpiochip_get_data(chip
);
268 struct hid_device
*hdev
= dev
->hdev
;
269 u8
*buf
= dev
->in_out_buffer
;
272 mutex_lock(&dev
->lock
);
274 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
275 CP2112_GPIO_CONFIG_LENGTH
, HID_FEATURE_REPORT
,
277 if (ret
!= CP2112_GPIO_CONFIG_LENGTH
) {
278 hid_err(hdev
, "error requesting GPIO config: %d\n", ret
);
282 buf
[1] |= 1 << offset
;
283 buf
[2] = gpio_push_pull
;
285 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
286 CP2112_GPIO_CONFIG_LENGTH
, HID_FEATURE_REPORT
,
289 hid_err(hdev
, "error setting GPIO config: %d\n", ret
);
293 mutex_unlock(&dev
->lock
);
296 * Set gpio value when output direction is already set,
297 * as specified in AN495, Rev. 0.2, cpt. 4.4
299 cp2112_gpio_set(chip
, offset
, value
);
304 mutex_unlock(&dev
->lock
);
305 return ret
< 0 ? ret
: -EIO
;
308 static int cp2112_hid_get(struct hid_device
*hdev
, unsigned char report_number
,
309 u8
*data
, size_t count
, unsigned char report_type
)
314 buf
= kmalloc(count
, GFP_KERNEL
);
318 ret
= hid_hw_raw_request(hdev
, report_number
, buf
, count
,
319 report_type
, HID_REQ_GET_REPORT
);
320 memcpy(data
, buf
, count
);
325 static int cp2112_hid_output(struct hid_device
*hdev
, u8
*data
, size_t count
,
326 unsigned char report_type
)
331 buf
= kmemdup(data
, count
, GFP_KERNEL
);
335 if (report_type
== HID_OUTPUT_REPORT
)
336 ret
= hid_hw_output_report(hdev
, buf
, count
);
338 ret
= hid_hw_raw_request(hdev
, buf
[0], buf
, count
, report_type
,
345 static int cp2112_wait(struct cp2112_device
*dev
, atomic_t
*avail
)
349 /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a
350 * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to
351 * come in cp2112_raw_event or timeout. There will only be one of these
352 * in flight at any one time. The timeout is extremely large and is a
353 * last resort if the CP2112 has died. If we do timeout we don't expect
354 * to receive the response which would cause data races, it's not like
355 * we can do anything about it anyway.
357 ret
= wait_event_interruptible_timeout(dev
->wait
,
358 atomic_read(avail
), msecs_to_jiffies(RESPONSE_TIMEOUT
));
359 if (-ERESTARTSYS
== ret
)
364 atomic_set(avail
, 0);
368 static int cp2112_xfer_status(struct cp2112_device
*dev
)
370 struct hid_device
*hdev
= dev
->hdev
;
374 buf
[0] = CP2112_TRANSFER_STATUS_REQUEST
;
376 atomic_set(&dev
->xfer_avail
, 0);
378 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
380 hid_warn(hdev
, "Error requesting status: %d\n", ret
);
384 ret
= cp2112_wait(dev
, &dev
->xfer_avail
);
388 return dev
->xfer_status
;
391 static int cp2112_read(struct cp2112_device
*dev
, u8
*data
, size_t size
)
393 struct hid_device
*hdev
= dev
->hdev
;
394 struct cp2112_force_read_report report
;
397 if (size
> sizeof(dev
->read_data
))
398 size
= sizeof(dev
->read_data
);
399 report
.report
= CP2112_DATA_READ_FORCE_SEND
;
400 report
.length
= cpu_to_be16(size
);
402 atomic_set(&dev
->read_avail
, 0);
404 ret
= cp2112_hid_output(hdev
, &report
.report
, sizeof(report
),
407 hid_warn(hdev
, "Error requesting data: %d\n", ret
);
411 ret
= cp2112_wait(dev
, &dev
->read_avail
);
415 hid_dbg(hdev
, "read %d of %zd bytes requested\n",
416 dev
->read_length
, size
);
418 if (size
> dev
->read_length
)
419 size
= dev
->read_length
;
421 memcpy(data
, dev
->read_data
, size
);
422 return dev
->read_length
;
425 static int cp2112_read_req(void *buf
, u8 slave_address
, u16 length
)
427 struct cp2112_read_req_report
*report
= buf
;
429 if (length
< 1 || length
> 512)
432 report
->report
= CP2112_DATA_READ_REQUEST
;
433 report
->slave_address
= slave_address
<< 1;
434 report
->length
= cpu_to_be16(length
);
435 return sizeof(*report
);
438 static int cp2112_write_read_req(void *buf
, u8 slave_address
, u16 length
,
439 u8 command
, u8
*data
, u8 data_length
)
441 struct cp2112_write_read_req_report
*report
= buf
;
443 if (length
< 1 || length
> 512
444 || data_length
> sizeof(report
->target_address
) - 1)
447 report
->report
= CP2112_DATA_WRITE_READ_REQUEST
;
448 report
->slave_address
= slave_address
<< 1;
449 report
->length
= cpu_to_be16(length
);
450 report
->target_address_length
= data_length
+ 1;
451 report
->target_address
[0] = command
;
452 memcpy(&report
->target_address
[1], data
, data_length
);
453 return data_length
+ 6;
456 static int cp2112_write_req(void *buf
, u8 slave_address
, u8 command
, u8
*data
,
459 struct cp2112_write_req_report
*report
= buf
;
461 if (data_length
> sizeof(report
->data
) - 1)
464 report
->report
= CP2112_DATA_WRITE_REQUEST
;
465 report
->slave_address
= slave_address
<< 1;
466 report
->length
= data_length
+ 1;
467 report
->data
[0] = command
;
468 memcpy(&report
->data
[1], data
, data_length
);
469 return data_length
+ 4;
472 static int cp2112_i2c_write_req(void *buf
, u8 slave_address
, u8
*data
,
475 struct cp2112_write_req_report
*report
= buf
;
477 if (data_length
> sizeof(report
->data
))
480 report
->report
= CP2112_DATA_WRITE_REQUEST
;
481 report
->slave_address
= slave_address
<< 1;
482 report
->length
= data_length
;
483 memcpy(report
->data
, data
, data_length
);
484 return data_length
+ 3;
487 static int cp2112_i2c_write_read_req(void *buf
, u8 slave_address
,
488 u8
*addr
, int addr_length
,
491 struct cp2112_write_read_req_report
*report
= buf
;
493 if (read_length
< 1 || read_length
> 512 ||
494 addr_length
> sizeof(report
->target_address
))
497 report
->report
= CP2112_DATA_WRITE_READ_REQUEST
;
498 report
->slave_address
= slave_address
<< 1;
499 report
->length
= cpu_to_be16(read_length
);
500 report
->target_address_length
= addr_length
;
501 memcpy(report
->target_address
, addr
, addr_length
);
502 return addr_length
+ 5;
505 static int cp2112_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
508 struct cp2112_device
*dev
= (struct cp2112_device
*)adap
->algo_data
;
509 struct hid_device
*hdev
= dev
->hdev
;
512 ssize_t read_length
= 0;
514 unsigned int retries
;
517 hid_dbg(hdev
, "I2C %d messages\n", num
);
520 if (msgs
->flags
& I2C_M_RD
) {
521 hid_dbg(hdev
, "I2C read %#04x len %d\n",
522 msgs
->addr
, msgs
->len
);
523 read_length
= msgs
->len
;
524 read_buf
= msgs
->buf
;
525 count
= cp2112_read_req(buf
, msgs
->addr
, msgs
->len
);
527 hid_dbg(hdev
, "I2C write %#04x len %d\n",
528 msgs
->addr
, msgs
->len
);
529 count
= cp2112_i2c_write_req(buf
, msgs
->addr
,
530 msgs
->buf
, msgs
->len
);
534 } else if (dev
->hwversion
> 1 && /* no repeated start in rev 1 */
536 msgs
[0].addr
== msgs
[1].addr
&&
537 !(msgs
[0].flags
& I2C_M_RD
) && (msgs
[1].flags
& I2C_M_RD
)) {
538 hid_dbg(hdev
, "I2C write-read %#04x wlen %d rlen %d\n",
539 msgs
[0].addr
, msgs
[0].len
, msgs
[1].len
);
540 read_length
= msgs
[1].len
;
541 read_buf
= msgs
[1].buf
;
542 count
= cp2112_i2c_write_read_req(buf
, msgs
[0].addr
,
543 msgs
[0].buf
, msgs
[0].len
, msgs
[1].len
);
548 "Multi-message I2C transactions not supported\n");
552 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
554 hid_err(hdev
, "power management error: %d\n", ret
);
558 ret
= cp2112_hid_output(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
560 hid_warn(hdev
, "Error starting transaction: %d\n", ret
);
564 for (retries
= 0; retries
< XFER_STATUS_RETRIES
; ++retries
) {
565 ret
= cp2112_xfer_status(dev
);
573 if (XFER_STATUS_RETRIES
<= retries
) {
574 hid_warn(hdev
, "Transfer timed out, cancelling.\n");
575 buf
[0] = CP2112_CANCEL_TRANSFER
;
578 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
580 hid_warn(hdev
, "Error cancelling transaction: %d\n",
587 for (count
= 0; count
< read_length
;) {
588 ret
= cp2112_read(dev
, read_buf
+ count
, read_length
- count
);
592 hid_err(hdev
, "read returned 0\n");
597 if (count
> read_length
) {
599 * The hardware returned too much data.
600 * This is mostly harmless because cp2112_read()
601 * has a limit check so didn't overrun our
602 * buffer. Nevertheless, we return an error
603 * because something is seriously wrong and
604 * it shouldn't go unnoticed.
606 hid_err(hdev
, "long read: %d > %zd\n",
607 ret
, read_length
- count
+ ret
);
613 /* return the number of transferred messages */
617 hid_hw_power(hdev
, PM_HINT_NORMAL
);
618 hid_dbg(hdev
, "I2C transfer finished: %d\n", ret
);
622 static int cp2112_xfer(struct i2c_adapter
*adap
, u16 addr
,
623 unsigned short flags
, char read_write
, u8 command
,
624 int size
, union i2c_smbus_data
*data
)
626 struct cp2112_device
*dev
= (struct cp2112_device
*)adap
->algo_data
;
627 struct hid_device
*hdev
= dev
->hdev
;
631 size_t read_length
= 0;
632 unsigned int retries
;
635 hid_dbg(hdev
, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n",
636 read_write
== I2C_SMBUS_WRITE
? "write" : "read",
637 addr
, flags
, command
, size
);
643 if (I2C_SMBUS_READ
== read_write
)
644 count
= cp2112_read_req(buf
, addr
, read_length
);
646 count
= cp2112_write_req(buf
, addr
, command
, NULL
,
649 case I2C_SMBUS_BYTE_DATA
:
652 if (I2C_SMBUS_READ
== read_write
)
653 count
= cp2112_write_read_req(buf
, addr
, read_length
,
656 count
= cp2112_write_req(buf
, addr
, command
,
659 case I2C_SMBUS_WORD_DATA
:
661 word
= cpu_to_le16(data
->word
);
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_PROC_CALL
:
671 size
= I2C_SMBUS_WORD_DATA
;
672 read_write
= I2C_SMBUS_READ
;
674 word
= cpu_to_le16(data
->word
);
676 count
= cp2112_write_read_req(buf
, addr
, read_length
, command
,
679 case I2C_SMBUS_I2C_BLOCK_DATA
:
680 size
= I2C_SMBUS_BLOCK_DATA
;
682 case I2C_SMBUS_BLOCK_DATA
:
683 if (I2C_SMBUS_READ
== read_write
) {
684 count
= cp2112_write_read_req(buf
, addr
,
688 count
= cp2112_write_req(buf
, addr
, command
,
693 case I2C_SMBUS_BLOCK_PROC_CALL
:
694 size
= I2C_SMBUS_BLOCK_DATA
;
695 read_write
= I2C_SMBUS_READ
;
697 count
= cp2112_write_read_req(buf
, addr
, I2C_SMBUS_BLOCK_MAX
,
698 command
, data
->block
,
702 hid_warn(hdev
, "Unsupported transaction %d\n", size
);
709 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
711 hid_err(hdev
, "power management error: %d\n", ret
);
715 ret
= cp2112_hid_output(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
717 hid_warn(hdev
, "Error starting transaction: %d\n", ret
);
721 for (retries
= 0; retries
< XFER_STATUS_RETRIES
; ++retries
) {
722 ret
= cp2112_xfer_status(dev
);
730 if (XFER_STATUS_RETRIES
<= retries
) {
731 hid_warn(hdev
, "Transfer timed out, cancelling.\n");
732 buf
[0] = CP2112_CANCEL_TRANSFER
;
735 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
737 hid_warn(hdev
, "Error cancelling transaction: %d\n",
744 if (I2C_SMBUS_WRITE
== read_write
) {
749 if (I2C_SMBUS_BLOCK_DATA
== size
)
752 ret
= cp2112_read(dev
, buf
, read_length
);
755 if (ret
!= read_length
) {
756 hid_warn(hdev
, "short read: %d < %zd\n", ret
, read_length
);
763 case I2C_SMBUS_BYTE_DATA
:
766 case I2C_SMBUS_WORD_DATA
:
767 data
->word
= le16_to_cpup((__le16
*)buf
);
769 case I2C_SMBUS_BLOCK_DATA
:
770 if (read_length
> I2C_SMBUS_BLOCK_MAX
) {
775 memcpy(data
->block
, buf
, read_length
);
781 hid_hw_power(hdev
, PM_HINT_NORMAL
);
782 hid_dbg(hdev
, "transfer finished: %d\n", ret
);
786 static u32
cp2112_functionality(struct i2c_adapter
*adap
)
788 return I2C_FUNC_I2C
|
789 I2C_FUNC_SMBUS_BYTE
|
790 I2C_FUNC_SMBUS_BYTE_DATA
|
791 I2C_FUNC_SMBUS_WORD_DATA
|
792 I2C_FUNC_SMBUS_BLOCK_DATA
|
793 I2C_FUNC_SMBUS_I2C_BLOCK
|
794 I2C_FUNC_SMBUS_PROC_CALL
|
795 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
;
798 static const struct i2c_algorithm smbus_algorithm
= {
799 .master_xfer
= cp2112_i2c_xfer
,
800 .smbus_xfer
= cp2112_xfer
,
801 .functionality
= cp2112_functionality
,
804 static int cp2112_get_usb_config(struct hid_device
*hdev
,
805 struct cp2112_usb_config_report
*cfg
)
809 ret
= cp2112_hid_get(hdev
, CP2112_USB_CONFIG
, (u8
*)cfg
, sizeof(*cfg
),
811 if (ret
!= sizeof(*cfg
)) {
812 hid_err(hdev
, "error reading usb config: %d\n", ret
);
821 static int cp2112_set_usb_config(struct hid_device
*hdev
,
822 struct cp2112_usb_config_report
*cfg
)
826 BUG_ON(cfg
->report
!= CP2112_USB_CONFIG
);
828 ret
= cp2112_hid_output(hdev
, (u8
*)cfg
, sizeof(*cfg
),
830 if (ret
!= sizeof(*cfg
)) {
831 hid_err(hdev
, "error writing usb config: %d\n", ret
);
840 static void chmod_sysfs_attrs(struct hid_device
*hdev
);
842 #define CP2112_CONFIG_ATTR(name, store, format, ...) \
843 static ssize_t name##_store(struct device *kdev, \
844 struct device_attribute *attr, const char *buf, \
847 struct hid_device *hdev = to_hid_device(kdev); \
848 struct cp2112_usb_config_report cfg; \
849 int ret = cp2112_get_usb_config(hdev, &cfg); \
853 ret = cp2112_set_usb_config(hdev, &cfg); \
856 chmod_sysfs_attrs(hdev); \
859 static ssize_t name##_show(struct device *kdev, \
860 struct device_attribute *attr, char *buf) \
862 struct hid_device *hdev = to_hid_device(kdev); \
863 struct cp2112_usb_config_report cfg; \
864 int ret = cp2112_get_usb_config(hdev, &cfg); \
867 return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \
869 static DEVICE_ATTR_RW(name);
871 CP2112_CONFIG_ATTR(vendor_id
, ({
874 if (sscanf(buf
, "%hi", &vid
) != 1)
877 cfg
.vid
= cpu_to_le16(vid
);
879 }), "0x%04x\n", le16_to_cpu(cfg
.vid
));
881 CP2112_CONFIG_ATTR(product_id
, ({
884 if (sscanf(buf
, "%hi", &pid
) != 1)
887 cfg
.pid
= cpu_to_le16(pid
);
889 }), "0x%04x\n", le16_to_cpu(cfg
.pid
));
891 CP2112_CONFIG_ATTR(max_power
, ({
894 if (sscanf(buf
, "%i", &mA
) != 1)
897 cfg
.max_power
= (mA
+ 1) / 2;
899 }), "%u mA\n", cfg
.max_power
* 2);
901 CP2112_CONFIG_ATTR(power_mode
, ({
902 if (sscanf(buf
, "%hhi", &cfg
.power_mode
) != 1)
906 }), "%u\n", cfg
.power_mode
);
908 CP2112_CONFIG_ATTR(release_version
, ({
909 if (sscanf(buf
, "%hhi.%hhi", &cfg
.release_major
, &cfg
.release_minor
)
914 }), "%u.%u\n", cfg
.release_major
, cfg
.release_minor
);
916 #undef CP2112_CONFIG_ATTR
918 struct cp2112_pstring_attribute
{
919 struct device_attribute attr
;
920 unsigned char report
;
923 static ssize_t
pstr_store(struct device
*kdev
,
924 struct device_attribute
*kattr
, const char *buf
,
927 struct hid_device
*hdev
= to_hid_device(kdev
);
928 struct cp2112_pstring_attribute
*attr
=
929 container_of(kattr
, struct cp2112_pstring_attribute
, attr
);
930 struct cp2112_string_report report
;
933 memset(&report
, 0, sizeof(report
));
935 ret
= utf8s_to_utf16s(buf
, count
, UTF16_LITTLE_ENDIAN
,
936 report
.string
, ARRAY_SIZE(report
.string
));
937 report
.report
= attr
->report
;
938 report
.length
= ret
* sizeof(report
.string
[0]) + 2;
939 report
.type
= USB_DT_STRING
;
941 ret
= cp2112_hid_output(hdev
, &report
.report
, report
.length
+ 1,
943 if (ret
!= report
.length
+ 1) {
944 hid_err(hdev
, "error writing %s string: %d\n", kattr
->attr
.name
,
951 chmod_sysfs_attrs(hdev
);
955 static ssize_t
pstr_show(struct device
*kdev
,
956 struct device_attribute
*kattr
, char *buf
)
958 struct hid_device
*hdev
= to_hid_device(kdev
);
959 struct cp2112_pstring_attribute
*attr
=
960 container_of(kattr
, struct cp2112_pstring_attribute
, attr
);
961 struct cp2112_string_report report
;
965 ret
= cp2112_hid_get(hdev
, attr
->report
, &report
.report
,
966 sizeof(report
) - 1, HID_FEATURE_REPORT
);
968 hid_err(hdev
, "error reading %s string: %d\n", kattr
->attr
.name
,
975 if (report
.length
< 2) {
976 hid_err(hdev
, "invalid %s string length: %d\n",
977 kattr
->attr
.name
, report
.length
);
981 length
= report
.length
> ret
- 1 ? ret
- 1 : report
.length
;
982 length
= (length
- 2) / sizeof(report
.string
[0]);
983 ret
= utf16s_to_utf8s(report
.string
, length
, UTF16_LITTLE_ENDIAN
, buf
,
989 #define CP2112_PSTR_ATTR(name, _report) \
990 static struct cp2112_pstring_attribute dev_attr_##name = { \
991 .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \
995 CP2112_PSTR_ATTR(manufacturer
, CP2112_MANUFACTURER_STRING
);
996 CP2112_PSTR_ATTR(product
, CP2112_PRODUCT_STRING
);
997 CP2112_PSTR_ATTR(serial
, CP2112_SERIAL_STRING
);
999 #undef CP2112_PSTR_ATTR
1001 static const struct attribute_group cp2112_attr_group
= {
1002 .attrs
= (struct attribute
*[]){
1003 &dev_attr_vendor_id
.attr
,
1004 &dev_attr_product_id
.attr
,
1005 &dev_attr_max_power
.attr
,
1006 &dev_attr_power_mode
.attr
,
1007 &dev_attr_release_version
.attr
,
1008 &dev_attr_manufacturer
.attr
.attr
,
1009 &dev_attr_product
.attr
.attr
,
1010 &dev_attr_serial
.attr
.attr
,
1015 /* Chmoding our sysfs attributes is simply a way to expose which fields in the
1016 * PROM have already been programmed. We do not depend on this preventing
1017 * writing to these attributes since the CP2112 will simply ignore writes to
1018 * already-programmed fields. This is why there is no sense in fixing this
1021 static void chmod_sysfs_attrs(struct hid_device
*hdev
)
1023 struct attribute
**attr
;
1027 ret
= cp2112_hid_get(hdev
, CP2112_LOCK_BYTE
, buf
, sizeof(buf
),
1028 HID_FEATURE_REPORT
);
1029 if (ret
!= sizeof(buf
)) {
1030 hid_err(hdev
, "error reading lock byte: %d\n", ret
);
1034 for (attr
= cp2112_attr_group
.attrs
; *attr
; ++attr
) {
1035 umode_t mode
= (buf
[1] & 1) ? S_IWUSR
| S_IRUGO
: S_IRUGO
;
1036 ret
= sysfs_chmod_file(&hdev
->dev
.kobj
, *attr
, mode
);
1038 hid_err(hdev
, "error chmoding sysfs file %s\n",
1044 static int cp2112_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
1046 struct cp2112_device
*dev
;
1048 struct cp2112_smbus_config_report config
;
1051 dev
= devm_kzalloc(&hdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
1055 dev
->in_out_buffer
= devm_kzalloc(&hdev
->dev
, CP2112_REPORT_MAX_LENGTH
,
1057 if (!dev
->in_out_buffer
)
1060 mutex_init(&dev
->lock
);
1062 ret
= hid_parse(hdev
);
1064 hid_err(hdev
, "parse failed\n");
1068 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
1070 hid_err(hdev
, "hw start failed\n");
1074 ret
= hid_hw_open(hdev
);
1076 hid_err(hdev
, "hw open failed\n");
1080 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
1082 hid_err(hdev
, "power management error: %d\n", ret
);
1086 ret
= cp2112_hid_get(hdev
, CP2112_GET_VERSION_INFO
, buf
, sizeof(buf
),
1087 HID_FEATURE_REPORT
);
1088 if (ret
!= sizeof(buf
)) {
1089 hid_err(hdev
, "error requesting version\n");
1092 goto err_power_normal
;
1095 hid_info(hdev
, "Part Number: 0x%02X Device Version: 0x%02X\n",
1098 ret
= cp2112_hid_get(hdev
, CP2112_SMBUS_CONFIG
, (u8
*)&config
,
1099 sizeof(config
), HID_FEATURE_REPORT
);
1100 if (ret
!= sizeof(config
)) {
1101 hid_err(hdev
, "error requesting SMBus config\n");
1104 goto err_power_normal
;
1107 config
.retry_time
= cpu_to_be16(1);
1109 ret
= cp2112_hid_output(hdev
, (u8
*)&config
, sizeof(config
),
1110 HID_FEATURE_REPORT
);
1111 if (ret
!= sizeof(config
)) {
1112 hid_err(hdev
, "error setting SMBus config\n");
1115 goto err_power_normal
;
1118 hid_set_drvdata(hdev
, (void *)dev
);
1120 dev
->adap
.owner
= THIS_MODULE
;
1121 dev
->adap
.class = I2C_CLASS_HWMON
;
1122 dev
->adap
.algo
= &smbus_algorithm
;
1123 dev
->adap
.algo_data
= dev
;
1124 dev
->adap
.dev
.parent
= &hdev
->dev
;
1125 snprintf(dev
->adap
.name
, sizeof(dev
->adap
.name
),
1126 "CP2112 SMBus Bridge on hiddev%d", hdev
->minor
);
1127 dev
->hwversion
= buf
[2];
1128 init_waitqueue_head(&dev
->wait
);
1130 hid_device_io_start(hdev
);
1131 ret
= i2c_add_adapter(&dev
->adap
);
1132 hid_device_io_stop(hdev
);
1135 hid_err(hdev
, "error registering i2c adapter\n");
1136 goto err_power_normal
;
1139 hid_dbg(hdev
, "adapter registered\n");
1141 dev
->gc
.label
= "cp2112_gpio";
1142 dev
->gc
.direction_input
= cp2112_gpio_direction_input
;
1143 dev
->gc
.direction_output
= cp2112_gpio_direction_output
;
1144 dev
->gc
.set
= cp2112_gpio_set
;
1145 dev
->gc
.get
= cp2112_gpio_get
;
1148 dev
->gc
.can_sleep
= 1;
1149 dev
->gc
.parent
= &hdev
->dev
;
1151 ret
= gpiochip_add_data(&dev
->gc
, dev
);
1153 hid_err(hdev
, "error registering gpio chip\n");
1157 ret
= sysfs_create_group(&hdev
->dev
.kobj
, &cp2112_attr_group
);
1159 hid_err(hdev
, "error creating sysfs attrs\n");
1160 goto err_gpiochip_remove
;
1163 chmod_sysfs_attrs(hdev
);
1164 hid_hw_power(hdev
, PM_HINT_NORMAL
);
1168 err_gpiochip_remove
:
1169 gpiochip_remove(&dev
->gc
);
1171 i2c_del_adapter(&dev
->adap
);
1173 hid_hw_power(hdev
, PM_HINT_NORMAL
);
1181 static void cp2112_remove(struct hid_device
*hdev
)
1183 struct cp2112_device
*dev
= hid_get_drvdata(hdev
);
1185 sysfs_remove_group(&hdev
->dev
.kobj
, &cp2112_attr_group
);
1186 gpiochip_remove(&dev
->gc
);
1187 i2c_del_adapter(&dev
->adap
);
1188 /* i2c_del_adapter has finished removing all i2c devices from our
1189 * adapter. Well behaved devices should no longer call our cp2112_xfer
1190 * and should have waited for any pending calls to finish. It has also
1191 * waited for device_unregister(&adap->dev) to complete. Therefore we
1192 * can safely free our struct cp2112_device.
1198 static int cp2112_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
1201 struct cp2112_device
*dev
= hid_get_drvdata(hdev
);
1202 struct cp2112_xfer_status_report
*xfer
= (void *)data
;
1205 case CP2112_TRANSFER_STATUS_RESPONSE
:
1206 hid_dbg(hdev
, "xfer status: %02x %02x %04x %04x\n",
1207 xfer
->status0
, xfer
->status1
,
1208 be16_to_cpu(xfer
->retries
), be16_to_cpu(xfer
->length
));
1210 switch (xfer
->status0
) {
1212 dev
->xfer_status
= -EAGAIN
;
1215 dev
->xfer_status
= -EBUSY
;
1217 case STATUS0_COMPLETE
:
1218 dev
->xfer_status
= be16_to_cpu(xfer
->length
);
1221 switch (xfer
->status1
) {
1222 case STATUS1_TIMEOUT_NACK
:
1223 case STATUS1_TIMEOUT_BUS
:
1224 dev
->xfer_status
= -ETIMEDOUT
;
1227 dev
->xfer_status
= -EIO
;
1232 dev
->xfer_status
= -EINVAL
;
1236 atomic_set(&dev
->xfer_avail
, 1);
1238 case CP2112_DATA_READ_RESPONSE
:
1239 hid_dbg(hdev
, "read response: %02x %02x\n", data
[1], data
[2]);
1241 dev
->read_length
= data
[2];
1242 if (dev
->read_length
> sizeof(dev
->read_data
))
1243 dev
->read_length
= sizeof(dev
->read_data
);
1245 memcpy(dev
->read_data
, &data
[3], dev
->read_length
);
1246 atomic_set(&dev
->read_avail
, 1);
1249 hid_err(hdev
, "unknown report\n");
1254 wake_up_interruptible(&dev
->wait
);
1258 static struct hid_driver cp2112_driver
= {
1260 .id_table
= cp2112_devices
,
1261 .probe
= cp2112_probe
,
1262 .remove
= cp2112_remove
,
1263 .raw_event
= cp2112_raw_event
,
1266 module_hid_driver(cp2112_driver
);
1267 MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge");
1268 MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
1269 MODULE_LICENSE("GPL");