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
;
165 static int gpio_push_pull
= 0xFF;
166 module_param(gpio_push_pull
, int, S_IRUGO
| S_IWUSR
);
167 MODULE_PARM_DESC(gpio_push_pull
, "GPIO push-pull configuration bitmask");
169 static int cp2112_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
171 struct cp2112_device
*dev
= container_of(chip
, struct cp2112_device
,
173 struct hid_device
*hdev
= dev
->hdev
;
177 ret
= hdev
->hid_get_raw_report(hdev
, CP2112_GPIO_CONFIG
, buf
,
178 sizeof(buf
), HID_FEATURE_REPORT
);
179 if (ret
!= sizeof(buf
)) {
180 hid_err(hdev
, "error requesting GPIO config: %d\n", ret
);
184 buf
[1] &= ~(1 << offset
);
185 buf
[2] = gpio_push_pull
;
187 ret
= hdev
->hid_output_raw_report(hdev
, buf
, sizeof(buf
),
190 hid_err(hdev
, "error setting GPIO config: %d\n", ret
);
197 static void cp2112_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
199 struct cp2112_device
*dev
= container_of(chip
, struct cp2112_device
,
201 struct hid_device
*hdev
= dev
->hdev
;
205 buf
[0] = CP2112_GPIO_SET
;
206 buf
[1] = value
? 0xff : 0;
207 buf
[2] = 1 << offset
;
209 ret
= hdev
->hid_output_raw_report(hdev
, buf
, sizeof(buf
),
212 hid_err(hdev
, "error setting GPIO values: %d\n", ret
);
215 static int cp2112_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
217 struct cp2112_device
*dev
= container_of(chip
, struct cp2112_device
,
219 struct hid_device
*hdev
= dev
->hdev
;
223 ret
= hdev
->hid_get_raw_report(hdev
, CP2112_GPIO_GET
, buf
, sizeof(buf
),
225 if (ret
!= sizeof(buf
)) {
226 hid_err(hdev
, "error requesting GPIO values: %d\n", ret
);
230 return (buf
[1] >> offset
) & 1;
233 static int cp2112_gpio_direction_output(struct gpio_chip
*chip
,
234 unsigned offset
, int value
)
236 struct cp2112_device
*dev
= container_of(chip
, struct cp2112_device
,
238 struct hid_device
*hdev
= dev
->hdev
;
242 cp2112_gpio_set(chip
, offset
, value
);
244 ret
= hdev
->hid_get_raw_report(hdev
, CP2112_GPIO_CONFIG
, buf
,
245 sizeof(buf
), HID_FEATURE_REPORT
);
246 if (ret
!= sizeof(buf
)) {
247 hid_err(hdev
, "error requesting GPIO config: %d\n", ret
);
251 buf
[1] |= 1 << offset
;
252 buf
[2] = gpio_push_pull
;
254 ret
= hdev
->hid_output_raw_report(hdev
, buf
, sizeof(buf
),
257 hid_err(hdev
, "error setting GPIO config: %d\n", ret
);
264 static int cp2112_hid_get(struct hid_device
*hdev
, unsigned char report_number
,
265 u8
*data
, size_t count
, unsigned char report_type
)
270 buf
= kmalloc(count
, GFP_KERNEL
);
274 ret
= hdev
->hid_get_raw_report(hdev
, report_number
, buf
, count
,
276 memcpy(data
, buf
, count
);
281 static int cp2112_hid_output(struct hid_device
*hdev
, u8
*data
, size_t count
,
282 unsigned char report_type
)
287 buf
= kmemdup(data
, count
, GFP_KERNEL
);
291 ret
= hdev
->hid_output_raw_report(hdev
, buf
, count
, report_type
);
296 static int cp2112_wait(struct cp2112_device
*dev
, atomic_t
*avail
)
300 /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a
301 * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to
302 * come in cp2112_raw_event or timeout. There will only be one of these
303 * in flight at any one time. The timeout is extremely large and is a
304 * last resort if the CP2112 has died. If we do timeout we don't expect
305 * to receive the response which would cause data races, it's not like
306 * we can do anything about it anyway.
308 ret
= wait_event_interruptible_timeout(dev
->wait
,
309 atomic_read(avail
), msecs_to_jiffies(RESPONSE_TIMEOUT
));
310 if (-ERESTARTSYS
== ret
)
315 atomic_set(avail
, 0);
319 static int cp2112_xfer_status(struct cp2112_device
*dev
)
321 struct hid_device
*hdev
= dev
->hdev
;
325 buf
[0] = CP2112_TRANSFER_STATUS_REQUEST
;
327 atomic_set(&dev
->xfer_avail
, 0);
329 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
331 hid_warn(hdev
, "Error requesting status: %d\n", ret
);
335 ret
= cp2112_wait(dev
, &dev
->xfer_avail
);
339 return dev
->xfer_status
;
342 static int cp2112_read(struct cp2112_device
*dev
, u8
*data
, size_t size
)
344 struct hid_device
*hdev
= dev
->hdev
;
345 struct cp2112_force_read_report report
;
348 report
.report
= CP2112_DATA_READ_FORCE_SEND
;
349 report
.length
= cpu_to_be16(size
);
351 atomic_set(&dev
->read_avail
, 0);
353 ret
= cp2112_hid_output(hdev
, &report
.report
, sizeof(report
),
356 hid_warn(hdev
, "Error requesting data: %d\n", ret
);
360 ret
= cp2112_wait(dev
, &dev
->read_avail
);
364 hid_dbg(hdev
, "read %d of %zd bytes requested\n",
365 dev
->read_length
, size
);
367 if (size
> dev
->read_length
)
368 size
= dev
->read_length
;
370 memcpy(data
, dev
->read_data
, size
);
371 return dev
->read_length
;
374 static int cp2112_read_req(void *buf
, u8 slave_address
, u16 length
)
376 struct cp2112_read_req_report
*report
= buf
;
378 if (length
< 1 || length
> 512)
381 report
->report
= CP2112_DATA_READ_REQUEST
;
382 report
->slave_address
= slave_address
<< 1;
383 report
->length
= cpu_to_be16(length
);
384 return sizeof(*report
);
387 static int cp2112_write_read_req(void *buf
, u8 slave_address
, u16 length
,
388 u8 command
, u8
*data
, u8 data_length
)
390 struct cp2112_write_read_req_report
*report
= buf
;
392 if (length
< 1 || length
> 512
393 || data_length
> sizeof(report
->target_address
) - 1)
396 report
->report
= CP2112_DATA_WRITE_READ_REQUEST
;
397 report
->slave_address
= slave_address
<< 1;
398 report
->length
= cpu_to_be16(length
);
399 report
->target_address_length
= data_length
+ 1;
400 report
->target_address
[0] = command
;
401 memcpy(&report
->target_address
[1], data
, data_length
);
402 return data_length
+ 6;
405 static int cp2112_write_req(void *buf
, u8 slave_address
, u8 command
, u8
*data
,
408 struct cp2112_write_req_report
*report
= buf
;
410 if (data_length
> sizeof(report
->data
) - 1)
413 report
->report
= CP2112_DATA_WRITE_REQUEST
;
414 report
->slave_address
= slave_address
<< 1;
415 report
->length
= data_length
+ 1;
416 report
->data
[0] = command
;
417 memcpy(&report
->data
[1], data
, data_length
);
418 return data_length
+ 4;
421 static int cp2112_xfer(struct i2c_adapter
*adap
, u16 addr
,
422 unsigned short flags
, char read_write
, u8 command
,
423 int size
, union i2c_smbus_data
*data
)
425 struct cp2112_device
*dev
= (struct cp2112_device
*)adap
->algo_data
;
426 struct hid_device
*hdev
= dev
->hdev
;
430 size_t read_length
= 0;
431 unsigned int retries
;
434 hid_dbg(hdev
, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n",
435 read_write
== I2C_SMBUS_WRITE
? "write" : "read",
436 addr
, flags
, command
, size
);
442 if (I2C_SMBUS_READ
== read_write
)
443 count
= cp2112_read_req(buf
, addr
, read_length
);
445 count
= cp2112_write_req(buf
, addr
, data
->byte
, NULL
,
448 case I2C_SMBUS_BYTE_DATA
:
451 if (I2C_SMBUS_READ
== read_write
)
452 count
= cp2112_write_read_req(buf
, addr
, read_length
,
455 count
= cp2112_write_req(buf
, addr
, command
,
458 case I2C_SMBUS_WORD_DATA
:
460 word
= cpu_to_be16(data
->word
);
462 if (I2C_SMBUS_READ
== read_write
)
463 count
= cp2112_write_read_req(buf
, addr
, read_length
,
466 count
= cp2112_write_req(buf
, addr
, command
,
469 case I2C_SMBUS_PROC_CALL
:
470 size
= I2C_SMBUS_WORD_DATA
;
471 read_write
= I2C_SMBUS_READ
;
473 word
= cpu_to_be16(data
->word
);
475 count
= cp2112_write_read_req(buf
, addr
, read_length
, command
,
478 case I2C_SMBUS_I2C_BLOCK_DATA
:
479 size
= I2C_SMBUS_BLOCK_DATA
;
481 case I2C_SMBUS_BLOCK_DATA
:
482 if (I2C_SMBUS_READ
== read_write
) {
483 count
= cp2112_write_read_req(buf
, addr
,
487 count
= cp2112_write_req(buf
, addr
, command
,
492 case I2C_SMBUS_BLOCK_PROC_CALL
:
493 size
= I2C_SMBUS_BLOCK_DATA
;
494 read_write
= I2C_SMBUS_READ
;
496 count
= cp2112_write_read_req(buf
, addr
, I2C_SMBUS_BLOCK_MAX
,
497 command
, data
->block
,
501 hid_warn(hdev
, "Unsupported transaction %d\n", size
);
508 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
510 hid_err(hdev
, "power management error: %d\n", ret
);
514 ret
= cp2112_hid_output(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
516 hid_warn(hdev
, "Error starting transaction: %d\n", ret
);
520 for (retries
= 0; retries
< XFER_STATUS_RETRIES
; ++retries
) {
521 ret
= cp2112_xfer_status(dev
);
529 if (XFER_STATUS_RETRIES
<= retries
) {
530 hid_warn(hdev
, "Transfer timed out, cancelling.\n");
531 buf
[0] = CP2112_CANCEL_TRANSFER
;
534 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
536 hid_warn(hdev
, "Error cancelling transaction: %d\n",
543 if (I2C_SMBUS_WRITE
== read_write
) {
548 if (I2C_SMBUS_BLOCK_DATA
== size
)
551 ret
= cp2112_read(dev
, buf
, read_length
);
554 if (ret
!= read_length
) {
555 hid_warn(hdev
, "short read: %d < %zd\n", ret
, read_length
);
562 case I2C_SMBUS_BYTE_DATA
:
565 case I2C_SMBUS_WORD_DATA
:
566 data
->word
= be16_to_cpup((__be16
*)buf
);
568 case I2C_SMBUS_BLOCK_DATA
:
569 if (read_length
> I2C_SMBUS_BLOCK_MAX
) {
574 memcpy(data
->block
, buf
, read_length
);
580 hid_hw_power(hdev
, PM_HINT_NORMAL
);
581 hid_dbg(hdev
, "transfer finished: %d\n", ret
);
585 static u32
cp2112_functionality(struct i2c_adapter
*adap
)
587 return I2C_FUNC_SMBUS_BYTE
|
588 I2C_FUNC_SMBUS_BYTE_DATA
|
589 I2C_FUNC_SMBUS_WORD_DATA
|
590 I2C_FUNC_SMBUS_BLOCK_DATA
|
591 I2C_FUNC_SMBUS_I2C_BLOCK
|
592 I2C_FUNC_SMBUS_PROC_CALL
|
593 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
;
596 static const struct i2c_algorithm smbus_algorithm
= {
597 .smbus_xfer
= cp2112_xfer
,
598 .functionality
= cp2112_functionality
,
601 static int cp2112_get_usb_config(struct hid_device
*hdev
,
602 struct cp2112_usb_config_report
*cfg
)
606 ret
= cp2112_hid_get(hdev
, CP2112_USB_CONFIG
, (u8
*)cfg
, sizeof(*cfg
),
608 if (ret
!= sizeof(*cfg
)) {
609 hid_err(hdev
, "error reading usb config: %d\n", ret
);
618 static int cp2112_set_usb_config(struct hid_device
*hdev
,
619 struct cp2112_usb_config_report
*cfg
)
623 BUG_ON(cfg
->report
!= CP2112_USB_CONFIG
);
625 ret
= cp2112_hid_output(hdev
, (u8
*)cfg
, sizeof(*cfg
),
627 if (ret
!= sizeof(*cfg
)) {
628 hid_err(hdev
, "error writing usb config: %d\n", ret
);
637 static void chmod_sysfs_attrs(struct hid_device
*hdev
);
639 #define CP2112_CONFIG_ATTR(name, store, format, ...) \
640 static ssize_t name##_store(struct device *kdev, \
641 struct device_attribute *attr, const char *buf, \
644 struct hid_device *hdev = container_of(kdev, struct hid_device, dev); \
645 struct cp2112_usb_config_report cfg; \
646 int ret = cp2112_get_usb_config(hdev, &cfg); \
650 ret = cp2112_set_usb_config(hdev, &cfg); \
653 chmod_sysfs_attrs(hdev); \
656 static ssize_t name##_show(struct device *kdev, \
657 struct device_attribute *attr, char *buf) \
659 struct hid_device *hdev = container_of(kdev, struct hid_device, dev); \
660 struct cp2112_usb_config_report cfg; \
661 int ret = cp2112_get_usb_config(hdev, &cfg); \
664 return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \
666 static DEVICE_ATTR_RW(name);
668 CP2112_CONFIG_ATTR(vendor_id
, ({
671 if (sscanf(buf
, "%hi", &vid
) != 1)
674 cfg
.vid
= cpu_to_le16(vid
);
676 }), "0x%04x\n", le16_to_cpu(cfg
.vid
));
678 CP2112_CONFIG_ATTR(product_id
, ({
681 if (sscanf(buf
, "%hi", &pid
) != 1)
684 cfg
.pid
= cpu_to_le16(pid
);
686 }), "0x%04x\n", le16_to_cpu(cfg
.pid
));
688 CP2112_CONFIG_ATTR(max_power
, ({
691 if (sscanf(buf
, "%i", &mA
) != 1)
694 cfg
.max_power
= (mA
+ 1) / 2;
696 }), "%u mA\n", cfg
.max_power
* 2);
698 CP2112_CONFIG_ATTR(power_mode
, ({
699 if (sscanf(buf
, "%hhi", &cfg
.power_mode
) != 1)
703 }), "%u\n", cfg
.power_mode
);
705 CP2112_CONFIG_ATTR(release_version
, ({
706 if (sscanf(buf
, "%hhi.%hhi", &cfg
.release_major
, &cfg
.release_minor
)
711 }), "%u.%u\n", cfg
.release_major
, cfg
.release_minor
);
713 #undef CP2112_CONFIG_ATTR
715 struct cp2112_pstring_attribute
{
716 struct device_attribute attr
;
717 unsigned char report
;
720 static ssize_t
pstr_store(struct device
*kdev
,
721 struct device_attribute
*kattr
, const char *buf
,
724 struct hid_device
*hdev
= container_of(kdev
, struct hid_device
, dev
);
725 struct cp2112_pstring_attribute
*attr
=
726 container_of(kattr
, struct cp2112_pstring_attribute
, attr
);
727 struct cp2112_string_report report
;
730 memset(&report
, 0, sizeof(report
));
732 ret
= utf8s_to_utf16s(buf
, count
, UTF16_LITTLE_ENDIAN
,
733 report
.string
, ARRAY_SIZE(report
.string
));
734 report
.report
= attr
->report
;
735 report
.length
= ret
* sizeof(report
.string
[0]) + 2;
736 report
.type
= USB_DT_STRING
;
738 ret
= cp2112_hid_output(hdev
, &report
.report
, report
.length
+ 1,
740 if (ret
!= report
.length
+ 1) {
741 hid_err(hdev
, "error writing %s string: %d\n", kattr
->attr
.name
,
748 chmod_sysfs_attrs(hdev
);
752 static ssize_t
pstr_show(struct device
*kdev
,
753 struct device_attribute
*kattr
, char *buf
)
755 struct hid_device
*hdev
= container_of(kdev
, struct hid_device
, dev
);
756 struct cp2112_pstring_attribute
*attr
=
757 container_of(kattr
, struct cp2112_pstring_attribute
, attr
);
758 struct cp2112_string_report report
;
762 ret
= cp2112_hid_get(hdev
, attr
->report
, &report
.report
,
763 sizeof(report
) - 1, HID_FEATURE_REPORT
);
765 hid_err(hdev
, "error reading %s string: %d\n", kattr
->attr
.name
,
772 if (report
.length
< 2) {
773 hid_err(hdev
, "invalid %s string length: %d\n",
774 kattr
->attr
.name
, report
.length
);
778 length
= report
.length
> ret
- 1 ? ret
- 1 : report
.length
;
779 length
= (length
- 2) / sizeof(report
.string
[0]);
780 ret
= utf16s_to_utf8s(report
.string
, length
, UTF16_LITTLE_ENDIAN
, buf
,
786 #define CP2112_PSTR_ATTR(name, _report) \
787 static struct cp2112_pstring_attribute dev_attr_##name = { \
788 .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \
792 CP2112_PSTR_ATTR(manufacturer
, CP2112_MANUFACTURER_STRING
);
793 CP2112_PSTR_ATTR(product
, CP2112_PRODUCT_STRING
);
794 CP2112_PSTR_ATTR(serial
, CP2112_SERIAL_STRING
);
796 #undef CP2112_PSTR_ATTR
798 static const struct attribute_group cp2112_attr_group
= {
799 .attrs
= (struct attribute
*[]){
800 &dev_attr_vendor_id
.attr
,
801 &dev_attr_product_id
.attr
,
802 &dev_attr_max_power
.attr
,
803 &dev_attr_power_mode
.attr
,
804 &dev_attr_release_version
.attr
,
805 &dev_attr_manufacturer
.attr
.attr
,
806 &dev_attr_product
.attr
.attr
,
807 &dev_attr_serial
.attr
.attr
,
812 /* Chmoding our sysfs attributes is simply a way to expose which fields in the
813 * PROM have already been programmed. We do not depend on this preventing
814 * writing to these attributes since the CP2112 will simply ignore writes to
815 * already-programmed fields. This is why there is no sense in fixing this
818 static void chmod_sysfs_attrs(struct hid_device
*hdev
)
820 struct attribute
**attr
;
824 ret
= cp2112_hid_get(hdev
, CP2112_LOCK_BYTE
, buf
, sizeof(buf
),
826 if (ret
!= sizeof(buf
)) {
827 hid_err(hdev
, "error reading lock byte: %d\n", ret
);
831 for (attr
= cp2112_attr_group
.attrs
; *attr
; ++attr
) {
832 umode_t mode
= (buf
[1] & 1) ? S_IWUSR
| S_IRUGO
: S_IRUGO
;
833 ret
= sysfs_chmod_file(&hdev
->dev
.kobj
, *attr
, mode
);
835 hid_err(hdev
, "error chmoding sysfs file %s\n",
841 static int cp2112_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
843 struct cp2112_device
*dev
;
845 struct cp2112_smbus_config_report config
;
848 ret
= hid_parse(hdev
);
850 hid_err(hdev
, "parse failed\n");
854 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
856 hid_err(hdev
, "hw start failed\n");
860 ret
= hid_hw_open(hdev
);
862 hid_err(hdev
, "hw open failed\n");
866 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
868 hid_err(hdev
, "power management error: %d\n", ret
);
872 ret
= cp2112_hid_get(hdev
, CP2112_GET_VERSION_INFO
, buf
, sizeof(buf
),
874 if (ret
!= sizeof(buf
)) {
875 hid_err(hdev
, "error requesting version\n");
878 goto err_power_normal
;
881 hid_info(hdev
, "Part Number: 0x%02X Device Version: 0x%02X\n",
884 ret
= cp2112_hid_get(hdev
, CP2112_SMBUS_CONFIG
, (u8
*)&config
,
885 sizeof(config
), HID_FEATURE_REPORT
);
886 if (ret
!= sizeof(config
)) {
887 hid_err(hdev
, "error requesting SMBus config\n");
890 goto err_power_normal
;
893 config
.retry_time
= cpu_to_be16(1);
895 ret
= cp2112_hid_output(hdev
, (u8
*)&config
, sizeof(config
),
897 if (ret
!= sizeof(config
)) {
898 hid_err(hdev
, "error setting SMBus config\n");
901 goto err_power_normal
;
904 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
907 goto err_power_normal
;
910 hid_set_drvdata(hdev
, (void *)dev
);
912 dev
->adap
.owner
= THIS_MODULE
;
913 dev
->adap
.class = I2C_CLASS_HWMON
;
914 dev
->adap
.algo
= &smbus_algorithm
;
915 dev
->adap
.algo_data
= dev
;
916 dev
->adap
.dev
.parent
= &hdev
->dev
;
917 snprintf(dev
->adap
.name
, sizeof(dev
->adap
.name
),
918 "CP2112 SMBus Bridge on hiddev%d", hdev
->minor
);
919 init_waitqueue_head(&dev
->wait
);
921 hid_device_io_start(hdev
);
922 ret
= i2c_add_adapter(&dev
->adap
);
923 hid_device_io_stop(hdev
);
926 hid_err(hdev
, "error registering i2c adapter\n");
930 hid_dbg(hdev
, "adapter registered\n");
932 dev
->gc
.label
= "cp2112_gpio";
933 dev
->gc
.direction_input
= cp2112_gpio_direction_input
;
934 dev
->gc
.direction_output
= cp2112_gpio_direction_output
;
935 dev
->gc
.set
= cp2112_gpio_set
;
936 dev
->gc
.get
= cp2112_gpio_get
;
939 dev
->gc
.can_sleep
= 1;
940 dev
->gc
.dev
= &hdev
->dev
;
942 ret
= gpiochip_add(&dev
->gc
);
944 hid_err(hdev
, "error registering gpio chip\n");
948 ret
= sysfs_create_group(&hdev
->dev
.kobj
, &cp2112_attr_group
);
950 hid_err(hdev
, "error creating sysfs attrs\n");
951 goto err_gpiochip_remove
;
954 chmod_sysfs_attrs(hdev
);
955 hid_hw_power(hdev
, PM_HINT_NORMAL
);
960 if (gpiochip_remove(&dev
->gc
) < 0)
961 hid_err(hdev
, "error removing gpio chip\n");
963 i2c_del_adapter(&dev
->adap
);
967 hid_hw_power(hdev
, PM_HINT_NORMAL
);
975 static void cp2112_remove(struct hid_device
*hdev
)
977 struct cp2112_device
*dev
= hid_get_drvdata(hdev
);
979 sysfs_remove_group(&hdev
->dev
.kobj
, &cp2112_attr_group
);
980 if (gpiochip_remove(&dev
->gc
))
981 hid_err(hdev
, "unable to remove gpio chip\n");
982 i2c_del_adapter(&dev
->adap
);
983 /* i2c_del_adapter has finished removing all i2c devices from our
984 * adapter. Well behaved devices should no longer call our cp2112_xfer
985 * and should have waited for any pending calls to finish. It has also
986 * waited for device_unregister(&adap->dev) to complete. Therefore we
987 * can safely free our struct cp2112_device.
994 static int cp2112_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
997 struct cp2112_device
*dev
= hid_get_drvdata(hdev
);
998 struct cp2112_xfer_status_report
*xfer
= (void *)data
;
1001 case CP2112_TRANSFER_STATUS_RESPONSE
:
1002 hid_dbg(hdev
, "xfer status: %02x %02x %04x %04x\n",
1003 xfer
->status0
, xfer
->status1
,
1004 be16_to_cpu(xfer
->retries
), be16_to_cpu(xfer
->length
));
1006 switch (xfer
->status0
) {
1008 dev
->xfer_status
= -EAGAIN
;
1011 dev
->xfer_status
= -EBUSY
;
1013 case STATUS0_COMPLETE
:
1014 dev
->xfer_status
= be16_to_cpu(xfer
->length
);
1017 switch (xfer
->status1
) {
1018 case STATUS1_TIMEOUT_NACK
:
1019 case STATUS1_TIMEOUT_BUS
:
1020 dev
->xfer_status
= -ETIMEDOUT
;
1023 dev
->xfer_status
= -EIO
;
1028 dev
->xfer_status
= -EINVAL
;
1032 atomic_set(&dev
->xfer_avail
, 1);
1034 case CP2112_DATA_READ_RESPONSE
:
1035 hid_dbg(hdev
, "read response: %02x %02x\n", data
[1], data
[2]);
1037 dev
->read_length
= data
[2];
1038 if (dev
->read_length
> sizeof(dev
->read_data
))
1039 dev
->read_length
= sizeof(dev
->read_data
);
1041 memcpy(dev
->read_data
, &data
[3], dev
->read_length
);
1042 atomic_set(&dev
->read_avail
, 1);
1045 hid_err(hdev
, "unknown report\n");
1050 wake_up_interruptible(&dev
->wait
);
1054 static struct hid_driver cp2112_driver
= {
1056 .id_table
= cp2112_devices
,
1057 .probe
= cp2112_probe
,
1058 .remove
= cp2112_remove
,
1059 .raw_event
= cp2112_raw_event
,
1062 module_hid_driver(cp2112_driver
);
1063 MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge");
1064 MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
1065 MODULE_LICENSE("GPL");