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.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>
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
;
166 static int gpio_push_pull
= 0xFF;
167 module_param(gpio_push_pull
, int, S_IRUGO
| S_IWUSR
);
168 MODULE_PARM_DESC(gpio_push_pull
, "GPIO push-pull configuration bitmask");
170 static int cp2112_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
172 struct cp2112_device
*dev
= container_of(chip
, struct cp2112_device
,
174 struct hid_device
*hdev
= dev
->hdev
;
178 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
179 sizeof(buf
), HID_FEATURE_REPORT
,
181 if (ret
!= sizeof(buf
)) {
182 hid_err(hdev
, "error requesting GPIO config: %d\n", ret
);
186 buf
[1] &= ~(1 << offset
);
187 buf
[2] = gpio_push_pull
;
189 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
, sizeof(buf
),
190 HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
192 hid_err(hdev
, "error setting GPIO config: %d\n", ret
);
199 static void cp2112_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
201 struct cp2112_device
*dev
= container_of(chip
, struct cp2112_device
,
203 struct hid_device
*hdev
= dev
->hdev
;
207 buf
[0] = CP2112_GPIO_SET
;
208 buf
[1] = value
? 0xff : 0;
209 buf
[2] = 1 << offset
;
211 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_SET
, buf
, sizeof(buf
),
212 HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
214 hid_err(hdev
, "error setting GPIO values: %d\n", ret
);
217 static int cp2112_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
219 struct cp2112_device
*dev
= container_of(chip
, struct cp2112_device
,
221 struct hid_device
*hdev
= dev
->hdev
;
225 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_GET
, buf
, sizeof(buf
),
226 HID_FEATURE_REPORT
, HID_REQ_GET_REPORT
);
227 if (ret
!= sizeof(buf
)) {
228 hid_err(hdev
, "error requesting GPIO values: %d\n", ret
);
232 return (buf
[1] >> offset
) & 1;
235 static int cp2112_gpio_direction_output(struct gpio_chip
*chip
,
236 unsigned offset
, int value
)
238 struct cp2112_device
*dev
= container_of(chip
, struct cp2112_device
,
240 struct hid_device
*hdev
= dev
->hdev
;
244 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
245 sizeof(buf
), HID_FEATURE_REPORT
,
247 if (ret
!= sizeof(buf
)) {
248 hid_err(hdev
, "error requesting GPIO config: %d\n", ret
);
252 buf
[1] |= 1 << offset
;
253 buf
[2] = gpio_push_pull
;
255 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
, sizeof(buf
),
256 HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
258 hid_err(hdev
, "error setting GPIO config: %d\n", ret
);
263 * Set gpio value when output direction is already set,
264 * as specified in AN495, Rev. 0.2, cpt. 4.4
266 cp2112_gpio_set(chip
, offset
, value
);
271 static int cp2112_hid_get(struct hid_device
*hdev
, unsigned char report_number
,
272 u8
*data
, size_t count
, unsigned char report_type
)
277 buf
= kmalloc(count
, GFP_KERNEL
);
281 ret
= hid_hw_raw_request(hdev
, report_number
, buf
, count
,
282 report_type
, HID_REQ_GET_REPORT
);
283 memcpy(data
, buf
, count
);
288 static int cp2112_hid_output(struct hid_device
*hdev
, u8
*data
, size_t count
,
289 unsigned char report_type
)
294 buf
= kmemdup(data
, count
, GFP_KERNEL
);
298 if (report_type
== HID_OUTPUT_REPORT
)
299 ret
= hid_hw_output_report(hdev
, buf
, count
);
301 ret
= hid_hw_raw_request(hdev
, buf
[0], buf
, count
, report_type
,
308 static int cp2112_wait(struct cp2112_device
*dev
, atomic_t
*avail
)
312 /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a
313 * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to
314 * come in cp2112_raw_event or timeout. There will only be one of these
315 * in flight at any one time. The timeout is extremely large and is a
316 * last resort if the CP2112 has died. If we do timeout we don't expect
317 * to receive the response which would cause data races, it's not like
318 * we can do anything about it anyway.
320 ret
= wait_event_interruptible_timeout(dev
->wait
,
321 atomic_read(avail
), msecs_to_jiffies(RESPONSE_TIMEOUT
));
322 if (-ERESTARTSYS
== ret
)
327 atomic_set(avail
, 0);
331 static int cp2112_xfer_status(struct cp2112_device
*dev
)
333 struct hid_device
*hdev
= dev
->hdev
;
337 buf
[0] = CP2112_TRANSFER_STATUS_REQUEST
;
339 atomic_set(&dev
->xfer_avail
, 0);
341 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
343 hid_warn(hdev
, "Error requesting status: %d\n", ret
);
347 ret
= cp2112_wait(dev
, &dev
->xfer_avail
);
351 return dev
->xfer_status
;
354 static int cp2112_read(struct cp2112_device
*dev
, u8
*data
, size_t size
)
356 struct hid_device
*hdev
= dev
->hdev
;
357 struct cp2112_force_read_report report
;
360 if (size
> sizeof(dev
->read_data
))
361 size
= sizeof(dev
->read_data
);
362 report
.report
= CP2112_DATA_READ_FORCE_SEND
;
363 report
.length
= cpu_to_be16(size
);
365 atomic_set(&dev
->read_avail
, 0);
367 ret
= cp2112_hid_output(hdev
, &report
.report
, sizeof(report
),
370 hid_warn(hdev
, "Error requesting data: %d\n", ret
);
374 ret
= cp2112_wait(dev
, &dev
->read_avail
);
378 hid_dbg(hdev
, "read %d of %zd bytes requested\n",
379 dev
->read_length
, size
);
381 if (size
> dev
->read_length
)
382 size
= dev
->read_length
;
384 memcpy(data
, dev
->read_data
, size
);
385 return dev
->read_length
;
388 static int cp2112_read_req(void *buf
, u8 slave_address
, u16 length
)
390 struct cp2112_read_req_report
*report
= buf
;
392 if (length
< 1 || length
> 512)
395 report
->report
= CP2112_DATA_READ_REQUEST
;
396 report
->slave_address
= slave_address
<< 1;
397 report
->length
= cpu_to_be16(length
);
398 return sizeof(*report
);
401 static int cp2112_write_read_req(void *buf
, u8 slave_address
, u16 length
,
402 u8 command
, u8
*data
, u8 data_length
)
404 struct cp2112_write_read_req_report
*report
= buf
;
406 if (length
< 1 || length
> 512
407 || data_length
> sizeof(report
->target_address
) - 1)
410 report
->report
= CP2112_DATA_WRITE_READ_REQUEST
;
411 report
->slave_address
= slave_address
<< 1;
412 report
->length
= cpu_to_be16(length
);
413 report
->target_address_length
= data_length
+ 1;
414 report
->target_address
[0] = command
;
415 memcpy(&report
->target_address
[1], data
, data_length
);
416 return data_length
+ 6;
419 static int cp2112_write_req(void *buf
, u8 slave_address
, u8 command
, u8
*data
,
422 struct cp2112_write_req_report
*report
= buf
;
424 if (data_length
> sizeof(report
->data
) - 1)
427 report
->report
= CP2112_DATA_WRITE_REQUEST
;
428 report
->slave_address
= slave_address
<< 1;
429 report
->length
= data_length
+ 1;
430 report
->data
[0] = command
;
431 memcpy(&report
->data
[1], data
, data_length
);
432 return data_length
+ 4;
435 static int cp2112_i2c_write_req(void *buf
, u8 slave_address
, u8
*data
,
438 struct cp2112_write_req_report
*report
= buf
;
440 if (data_length
> sizeof(report
->data
))
443 report
->report
= CP2112_DATA_WRITE_REQUEST
;
444 report
->slave_address
= slave_address
<< 1;
445 report
->length
= data_length
;
446 memcpy(report
->data
, data
, data_length
);
447 return data_length
+ 3;
450 static int cp2112_i2c_write_read_req(void *buf
, u8 slave_address
,
451 u8
*addr
, int addr_length
,
454 struct cp2112_write_read_req_report
*report
= buf
;
456 if (read_length
< 1 || read_length
> 512 ||
457 addr_length
> sizeof(report
->target_address
))
460 report
->report
= CP2112_DATA_WRITE_READ_REQUEST
;
461 report
->slave_address
= slave_address
<< 1;
462 report
->length
= cpu_to_be16(read_length
);
463 report
->target_address_length
= addr_length
;
464 memcpy(report
->target_address
, addr
, addr_length
);
465 return addr_length
+ 5;
468 static int cp2112_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
471 struct cp2112_device
*dev
= (struct cp2112_device
*)adap
->algo_data
;
472 struct hid_device
*hdev
= dev
->hdev
;
475 ssize_t read_length
= 0;
477 unsigned int retries
;
480 hid_dbg(hdev
, "I2C %d messages\n", num
);
483 if (msgs
->flags
& I2C_M_RD
) {
484 hid_dbg(hdev
, "I2C read %#04x len %d\n",
485 msgs
->addr
, msgs
->len
);
486 read_length
= msgs
->len
;
487 read_buf
= msgs
->buf
;
488 count
= cp2112_read_req(buf
, msgs
->addr
, msgs
->len
);
490 hid_dbg(hdev
, "I2C write %#04x len %d\n",
491 msgs
->addr
, msgs
->len
);
492 count
= cp2112_i2c_write_req(buf
, msgs
->addr
,
493 msgs
->buf
, msgs
->len
);
497 } else if (dev
->hwversion
> 1 && /* no repeated start in rev 1 */
499 msgs
[0].addr
== msgs
[1].addr
&&
500 !(msgs
[0].flags
& I2C_M_RD
) && (msgs
[1].flags
& I2C_M_RD
)) {
501 hid_dbg(hdev
, "I2C write-read %#04x wlen %d rlen %d\n",
502 msgs
[0].addr
, msgs
[0].len
, msgs
[1].len
);
503 read_length
= msgs
[1].len
;
504 read_buf
= msgs
[1].buf
;
505 count
= cp2112_i2c_write_read_req(buf
, msgs
[0].addr
,
506 msgs
[0].buf
, msgs
[0].len
, msgs
[1].len
);
511 "Multi-message I2C transactions not supported\n");
515 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
517 hid_err(hdev
, "power management error: %d\n", ret
);
521 ret
= cp2112_hid_output(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
523 hid_warn(hdev
, "Error starting transaction: %d\n", ret
);
527 for (retries
= 0; retries
< XFER_STATUS_RETRIES
; ++retries
) {
528 ret
= cp2112_xfer_status(dev
);
536 if (XFER_STATUS_RETRIES
<= retries
) {
537 hid_warn(hdev
, "Transfer timed out, cancelling.\n");
538 buf
[0] = CP2112_CANCEL_TRANSFER
;
541 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
543 hid_warn(hdev
, "Error cancelling transaction: %d\n",
550 for (count
= 0; count
< read_length
;) {
551 ret
= cp2112_read(dev
, read_buf
+ count
, read_length
- count
);
555 hid_err(hdev
, "read returned 0\n");
560 if (count
> read_length
) {
562 * The hardware returned too much data.
563 * This is mostly harmless because cp2112_read()
564 * has a limit check so didn't overrun our
565 * buffer. Nevertheless, we return an error
566 * because something is seriously wrong and
567 * it shouldn't go unnoticed.
569 hid_err(hdev
, "long read: %d > %zd\n",
570 ret
, read_length
- count
+ ret
);
576 /* return the number of transferred messages */
580 hid_hw_power(hdev
, PM_HINT_NORMAL
);
581 hid_dbg(hdev
, "I2C transfer finished: %d\n", ret
);
585 static int cp2112_xfer(struct i2c_adapter
*adap
, u16 addr
,
586 unsigned short flags
, char read_write
, u8 command
,
587 int size
, union i2c_smbus_data
*data
)
589 struct cp2112_device
*dev
= (struct cp2112_device
*)adap
->algo_data
;
590 struct hid_device
*hdev
= dev
->hdev
;
594 size_t read_length
= 0;
595 unsigned int retries
;
598 hid_dbg(hdev
, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n",
599 read_write
== I2C_SMBUS_WRITE
? "write" : "read",
600 addr
, flags
, command
, size
);
606 if (I2C_SMBUS_READ
== read_write
)
607 count
= cp2112_read_req(buf
, addr
, read_length
);
609 count
= cp2112_write_req(buf
, addr
, command
, NULL
,
612 case I2C_SMBUS_BYTE_DATA
:
615 if (I2C_SMBUS_READ
== read_write
)
616 count
= cp2112_write_read_req(buf
, addr
, read_length
,
619 count
= cp2112_write_req(buf
, addr
, command
,
622 case I2C_SMBUS_WORD_DATA
:
624 word
= cpu_to_le16(data
->word
);
626 if (I2C_SMBUS_READ
== read_write
)
627 count
= cp2112_write_read_req(buf
, addr
, read_length
,
630 count
= cp2112_write_req(buf
, addr
, command
,
633 case I2C_SMBUS_PROC_CALL
:
634 size
= I2C_SMBUS_WORD_DATA
;
635 read_write
= I2C_SMBUS_READ
;
637 word
= cpu_to_le16(data
->word
);
639 count
= cp2112_write_read_req(buf
, addr
, read_length
, command
,
642 case I2C_SMBUS_I2C_BLOCK_DATA
:
643 size
= I2C_SMBUS_BLOCK_DATA
;
645 case I2C_SMBUS_BLOCK_DATA
:
646 if (I2C_SMBUS_READ
== read_write
) {
647 count
= cp2112_write_read_req(buf
, addr
,
651 count
= cp2112_write_req(buf
, addr
, command
,
656 case I2C_SMBUS_BLOCK_PROC_CALL
:
657 size
= I2C_SMBUS_BLOCK_DATA
;
658 read_write
= I2C_SMBUS_READ
;
660 count
= cp2112_write_read_req(buf
, addr
, I2C_SMBUS_BLOCK_MAX
,
661 command
, data
->block
,
665 hid_warn(hdev
, "Unsupported transaction %d\n", size
);
672 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
674 hid_err(hdev
, "power management error: %d\n", ret
);
678 ret
= cp2112_hid_output(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
680 hid_warn(hdev
, "Error starting transaction: %d\n", ret
);
684 for (retries
= 0; retries
< XFER_STATUS_RETRIES
; ++retries
) {
685 ret
= cp2112_xfer_status(dev
);
693 if (XFER_STATUS_RETRIES
<= retries
) {
694 hid_warn(hdev
, "Transfer timed out, cancelling.\n");
695 buf
[0] = CP2112_CANCEL_TRANSFER
;
698 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
700 hid_warn(hdev
, "Error cancelling transaction: %d\n",
707 if (I2C_SMBUS_WRITE
== read_write
) {
712 if (I2C_SMBUS_BLOCK_DATA
== size
)
715 ret
= cp2112_read(dev
, buf
, read_length
);
718 if (ret
!= read_length
) {
719 hid_warn(hdev
, "short read: %d < %zd\n", ret
, read_length
);
726 case I2C_SMBUS_BYTE_DATA
:
729 case I2C_SMBUS_WORD_DATA
:
730 data
->word
= le16_to_cpup((__le16
*)buf
);
732 case I2C_SMBUS_BLOCK_DATA
:
733 if (read_length
> I2C_SMBUS_BLOCK_MAX
) {
738 memcpy(data
->block
, buf
, read_length
);
744 hid_hw_power(hdev
, PM_HINT_NORMAL
);
745 hid_dbg(hdev
, "transfer finished: %d\n", ret
);
749 static u32
cp2112_functionality(struct i2c_adapter
*adap
)
751 return I2C_FUNC_I2C
|
752 I2C_FUNC_SMBUS_BYTE
|
753 I2C_FUNC_SMBUS_BYTE_DATA
|
754 I2C_FUNC_SMBUS_WORD_DATA
|
755 I2C_FUNC_SMBUS_BLOCK_DATA
|
756 I2C_FUNC_SMBUS_I2C_BLOCK
|
757 I2C_FUNC_SMBUS_PROC_CALL
|
758 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
;
761 static const struct i2c_algorithm smbus_algorithm
= {
762 .master_xfer
= cp2112_i2c_xfer
,
763 .smbus_xfer
= cp2112_xfer
,
764 .functionality
= cp2112_functionality
,
767 static int cp2112_get_usb_config(struct hid_device
*hdev
,
768 struct cp2112_usb_config_report
*cfg
)
772 ret
= cp2112_hid_get(hdev
, CP2112_USB_CONFIG
, (u8
*)cfg
, sizeof(*cfg
),
774 if (ret
!= sizeof(*cfg
)) {
775 hid_err(hdev
, "error reading usb config: %d\n", ret
);
784 static int cp2112_set_usb_config(struct hid_device
*hdev
,
785 struct cp2112_usb_config_report
*cfg
)
789 BUG_ON(cfg
->report
!= CP2112_USB_CONFIG
);
791 ret
= cp2112_hid_output(hdev
, (u8
*)cfg
, sizeof(*cfg
),
793 if (ret
!= sizeof(*cfg
)) {
794 hid_err(hdev
, "error writing usb config: %d\n", ret
);
803 static void chmod_sysfs_attrs(struct hid_device
*hdev
);
805 #define CP2112_CONFIG_ATTR(name, store, format, ...) \
806 static ssize_t name##_store(struct device *kdev, \
807 struct device_attribute *attr, const char *buf, \
810 struct hid_device *hdev = container_of(kdev, struct hid_device, dev); \
811 struct cp2112_usb_config_report cfg; \
812 int ret = cp2112_get_usb_config(hdev, &cfg); \
816 ret = cp2112_set_usb_config(hdev, &cfg); \
819 chmod_sysfs_attrs(hdev); \
822 static ssize_t name##_show(struct device *kdev, \
823 struct device_attribute *attr, char *buf) \
825 struct hid_device *hdev = container_of(kdev, struct hid_device, dev); \
826 struct cp2112_usb_config_report cfg; \
827 int ret = cp2112_get_usb_config(hdev, &cfg); \
830 return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \
832 static DEVICE_ATTR_RW(name);
834 CP2112_CONFIG_ATTR(vendor_id
, ({
837 if (sscanf(buf
, "%hi", &vid
) != 1)
840 cfg
.vid
= cpu_to_le16(vid
);
842 }), "0x%04x\n", le16_to_cpu(cfg
.vid
));
844 CP2112_CONFIG_ATTR(product_id
, ({
847 if (sscanf(buf
, "%hi", &pid
) != 1)
850 cfg
.pid
= cpu_to_le16(pid
);
852 }), "0x%04x\n", le16_to_cpu(cfg
.pid
));
854 CP2112_CONFIG_ATTR(max_power
, ({
857 if (sscanf(buf
, "%i", &mA
) != 1)
860 cfg
.max_power
= (mA
+ 1) / 2;
862 }), "%u mA\n", cfg
.max_power
* 2);
864 CP2112_CONFIG_ATTR(power_mode
, ({
865 if (sscanf(buf
, "%hhi", &cfg
.power_mode
) != 1)
869 }), "%u\n", cfg
.power_mode
);
871 CP2112_CONFIG_ATTR(release_version
, ({
872 if (sscanf(buf
, "%hhi.%hhi", &cfg
.release_major
, &cfg
.release_minor
)
877 }), "%u.%u\n", cfg
.release_major
, cfg
.release_minor
);
879 #undef CP2112_CONFIG_ATTR
881 struct cp2112_pstring_attribute
{
882 struct device_attribute attr
;
883 unsigned char report
;
886 static ssize_t
pstr_store(struct device
*kdev
,
887 struct device_attribute
*kattr
, const char *buf
,
890 struct hid_device
*hdev
= container_of(kdev
, struct hid_device
, dev
);
891 struct cp2112_pstring_attribute
*attr
=
892 container_of(kattr
, struct cp2112_pstring_attribute
, attr
);
893 struct cp2112_string_report report
;
896 memset(&report
, 0, sizeof(report
));
898 ret
= utf8s_to_utf16s(buf
, count
, UTF16_LITTLE_ENDIAN
,
899 report
.string
, ARRAY_SIZE(report
.string
));
900 report
.report
= attr
->report
;
901 report
.length
= ret
* sizeof(report
.string
[0]) + 2;
902 report
.type
= USB_DT_STRING
;
904 ret
= cp2112_hid_output(hdev
, &report
.report
, report
.length
+ 1,
906 if (ret
!= report
.length
+ 1) {
907 hid_err(hdev
, "error writing %s string: %d\n", kattr
->attr
.name
,
914 chmod_sysfs_attrs(hdev
);
918 static ssize_t
pstr_show(struct device
*kdev
,
919 struct device_attribute
*kattr
, char *buf
)
921 struct hid_device
*hdev
= container_of(kdev
, struct hid_device
, dev
);
922 struct cp2112_pstring_attribute
*attr
=
923 container_of(kattr
, struct cp2112_pstring_attribute
, attr
);
924 struct cp2112_string_report report
;
928 ret
= cp2112_hid_get(hdev
, attr
->report
, &report
.report
,
929 sizeof(report
) - 1, HID_FEATURE_REPORT
);
931 hid_err(hdev
, "error reading %s string: %d\n", kattr
->attr
.name
,
938 if (report
.length
< 2) {
939 hid_err(hdev
, "invalid %s string length: %d\n",
940 kattr
->attr
.name
, report
.length
);
944 length
= report
.length
> ret
- 1 ? ret
- 1 : report
.length
;
945 length
= (length
- 2) / sizeof(report
.string
[0]);
946 ret
= utf16s_to_utf8s(report
.string
, length
, UTF16_LITTLE_ENDIAN
, buf
,
952 #define CP2112_PSTR_ATTR(name, _report) \
953 static struct cp2112_pstring_attribute dev_attr_##name = { \
954 .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \
958 CP2112_PSTR_ATTR(manufacturer
, CP2112_MANUFACTURER_STRING
);
959 CP2112_PSTR_ATTR(product
, CP2112_PRODUCT_STRING
);
960 CP2112_PSTR_ATTR(serial
, CP2112_SERIAL_STRING
);
962 #undef CP2112_PSTR_ATTR
964 static const struct attribute_group cp2112_attr_group
= {
965 .attrs
= (struct attribute
*[]){
966 &dev_attr_vendor_id
.attr
,
967 &dev_attr_product_id
.attr
,
968 &dev_attr_max_power
.attr
,
969 &dev_attr_power_mode
.attr
,
970 &dev_attr_release_version
.attr
,
971 &dev_attr_manufacturer
.attr
.attr
,
972 &dev_attr_product
.attr
.attr
,
973 &dev_attr_serial
.attr
.attr
,
978 /* Chmoding our sysfs attributes is simply a way to expose which fields in the
979 * PROM have already been programmed. We do not depend on this preventing
980 * writing to these attributes since the CP2112 will simply ignore writes to
981 * already-programmed fields. This is why there is no sense in fixing this
984 static void chmod_sysfs_attrs(struct hid_device
*hdev
)
986 struct attribute
**attr
;
990 ret
= cp2112_hid_get(hdev
, CP2112_LOCK_BYTE
, buf
, sizeof(buf
),
992 if (ret
!= sizeof(buf
)) {
993 hid_err(hdev
, "error reading lock byte: %d\n", ret
);
997 for (attr
= cp2112_attr_group
.attrs
; *attr
; ++attr
) {
998 umode_t mode
= (buf
[1] & 1) ? S_IWUSR
| S_IRUGO
: S_IRUGO
;
999 ret
= sysfs_chmod_file(&hdev
->dev
.kobj
, *attr
, mode
);
1001 hid_err(hdev
, "error chmoding sysfs file %s\n",
1007 static int cp2112_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
1009 struct cp2112_device
*dev
;
1011 struct cp2112_smbus_config_report config
;
1014 ret
= hid_parse(hdev
);
1016 hid_err(hdev
, "parse failed\n");
1020 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
1022 hid_err(hdev
, "hw start failed\n");
1026 ret
= hid_hw_open(hdev
);
1028 hid_err(hdev
, "hw open failed\n");
1032 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
1034 hid_err(hdev
, "power management error: %d\n", ret
);
1038 ret
= cp2112_hid_get(hdev
, CP2112_GET_VERSION_INFO
, buf
, sizeof(buf
),
1039 HID_FEATURE_REPORT
);
1040 if (ret
!= sizeof(buf
)) {
1041 hid_err(hdev
, "error requesting version\n");
1044 goto err_power_normal
;
1047 hid_info(hdev
, "Part Number: 0x%02X Device Version: 0x%02X\n",
1050 ret
= cp2112_hid_get(hdev
, CP2112_SMBUS_CONFIG
, (u8
*)&config
,
1051 sizeof(config
), HID_FEATURE_REPORT
);
1052 if (ret
!= sizeof(config
)) {
1053 hid_err(hdev
, "error requesting SMBus config\n");
1056 goto err_power_normal
;
1059 config
.retry_time
= cpu_to_be16(1);
1061 ret
= cp2112_hid_output(hdev
, (u8
*)&config
, sizeof(config
),
1062 HID_FEATURE_REPORT
);
1063 if (ret
!= sizeof(config
)) {
1064 hid_err(hdev
, "error setting SMBus config\n");
1067 goto err_power_normal
;
1070 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1073 goto err_power_normal
;
1076 hid_set_drvdata(hdev
, (void *)dev
);
1078 dev
->adap
.owner
= THIS_MODULE
;
1079 dev
->adap
.class = I2C_CLASS_HWMON
;
1080 dev
->adap
.algo
= &smbus_algorithm
;
1081 dev
->adap
.algo_data
= dev
;
1082 dev
->adap
.dev
.parent
= &hdev
->dev
;
1083 snprintf(dev
->adap
.name
, sizeof(dev
->adap
.name
),
1084 "CP2112 SMBus Bridge on hiddev%d", hdev
->minor
);
1085 dev
->hwversion
= buf
[2];
1086 init_waitqueue_head(&dev
->wait
);
1088 hid_device_io_start(hdev
);
1089 ret
= i2c_add_adapter(&dev
->adap
);
1090 hid_device_io_stop(hdev
);
1093 hid_err(hdev
, "error registering i2c adapter\n");
1097 hid_dbg(hdev
, "adapter registered\n");
1099 dev
->gc
.label
= "cp2112_gpio";
1100 dev
->gc
.direction_input
= cp2112_gpio_direction_input
;
1101 dev
->gc
.direction_output
= cp2112_gpio_direction_output
;
1102 dev
->gc
.set
= cp2112_gpio_set
;
1103 dev
->gc
.get
= cp2112_gpio_get
;
1106 dev
->gc
.can_sleep
= 1;
1107 dev
->gc
.dev
= &hdev
->dev
;
1109 ret
= gpiochip_add(&dev
->gc
);
1111 hid_err(hdev
, "error registering gpio chip\n");
1115 ret
= sysfs_create_group(&hdev
->dev
.kobj
, &cp2112_attr_group
);
1117 hid_err(hdev
, "error creating sysfs attrs\n");
1118 goto err_gpiochip_remove
;
1121 chmod_sysfs_attrs(hdev
);
1122 hid_hw_power(hdev
, PM_HINT_NORMAL
);
1126 err_gpiochip_remove
:
1127 gpiochip_remove(&dev
->gc
);
1129 i2c_del_adapter(&dev
->adap
);
1133 hid_hw_power(hdev
, PM_HINT_NORMAL
);
1141 static void cp2112_remove(struct hid_device
*hdev
)
1143 struct cp2112_device
*dev
= hid_get_drvdata(hdev
);
1145 sysfs_remove_group(&hdev
->dev
.kobj
, &cp2112_attr_group
);
1146 gpiochip_remove(&dev
->gc
);
1147 i2c_del_adapter(&dev
->adap
);
1148 /* i2c_del_adapter has finished removing all i2c devices from our
1149 * adapter. Well behaved devices should no longer call our cp2112_xfer
1150 * and should have waited for any pending calls to finish. It has also
1151 * waited for device_unregister(&adap->dev) to complete. Therefore we
1152 * can safely free our struct cp2112_device.
1159 static int cp2112_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
1162 struct cp2112_device
*dev
= hid_get_drvdata(hdev
);
1163 struct cp2112_xfer_status_report
*xfer
= (void *)data
;
1166 case CP2112_TRANSFER_STATUS_RESPONSE
:
1167 hid_dbg(hdev
, "xfer status: %02x %02x %04x %04x\n",
1168 xfer
->status0
, xfer
->status1
,
1169 be16_to_cpu(xfer
->retries
), be16_to_cpu(xfer
->length
));
1171 switch (xfer
->status0
) {
1173 dev
->xfer_status
= -EAGAIN
;
1176 dev
->xfer_status
= -EBUSY
;
1178 case STATUS0_COMPLETE
:
1179 dev
->xfer_status
= be16_to_cpu(xfer
->length
);
1182 switch (xfer
->status1
) {
1183 case STATUS1_TIMEOUT_NACK
:
1184 case STATUS1_TIMEOUT_BUS
:
1185 dev
->xfer_status
= -ETIMEDOUT
;
1188 dev
->xfer_status
= -EIO
;
1193 dev
->xfer_status
= -EINVAL
;
1197 atomic_set(&dev
->xfer_avail
, 1);
1199 case CP2112_DATA_READ_RESPONSE
:
1200 hid_dbg(hdev
, "read response: %02x %02x\n", data
[1], data
[2]);
1202 dev
->read_length
= data
[2];
1203 if (dev
->read_length
> sizeof(dev
->read_data
))
1204 dev
->read_length
= sizeof(dev
->read_data
);
1206 memcpy(dev
->read_data
, &data
[3], dev
->read_length
);
1207 atomic_set(&dev
->read_avail
, 1);
1210 hid_err(hdev
, "unknown report\n");
1215 wake_up_interruptible(&dev
->wait
);
1219 static struct hid_driver cp2112_driver
= {
1221 .id_table
= cp2112_devices
,
1222 .probe
= cp2112_probe
,
1223 .remove
= cp2112_remove
,
1224 .raw_event
= cp2112_raw_event
,
1227 module_hid_driver(cp2112_driver
);
1228 MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge");
1229 MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
1230 MODULE_LICENSE("GPL");