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/gpio/driver.h>
29 #include <linux/hid.h>
30 #include <linux/hidraw.h>
31 #include <linux/i2c.h>
32 #include <linux/module.h>
33 #include <linux/nls.h>
34 #include <linux/usb/ch9.h>
37 #define CP2112_REPORT_MAX_LENGTH 64
38 #define CP2112_GPIO_CONFIG_LENGTH 5
39 #define CP2112_GPIO_GET_LENGTH 2
40 #define CP2112_GPIO_SET_LENGTH 3
43 CP2112_GPIO_CONFIG
= 0x02,
44 CP2112_GPIO_GET
= 0x03,
45 CP2112_GPIO_SET
= 0x04,
46 CP2112_GET_VERSION_INFO
= 0x05,
47 CP2112_SMBUS_CONFIG
= 0x06,
48 CP2112_DATA_READ_REQUEST
= 0x10,
49 CP2112_DATA_WRITE_READ_REQUEST
= 0x11,
50 CP2112_DATA_READ_FORCE_SEND
= 0x12,
51 CP2112_DATA_READ_RESPONSE
= 0x13,
52 CP2112_DATA_WRITE_REQUEST
= 0x14,
53 CP2112_TRANSFER_STATUS_REQUEST
= 0x15,
54 CP2112_TRANSFER_STATUS_RESPONSE
= 0x16,
55 CP2112_CANCEL_TRANSFER
= 0x17,
56 CP2112_LOCK_BYTE
= 0x20,
57 CP2112_USB_CONFIG
= 0x21,
58 CP2112_MANUFACTURER_STRING
= 0x22,
59 CP2112_PRODUCT_STRING
= 0x23,
60 CP2112_SERIAL_STRING
= 0x24,
66 STATUS0_COMPLETE
= 0x02,
71 STATUS1_TIMEOUT_NACK
= 0x00,
72 STATUS1_TIMEOUT_BUS
= 0x01,
73 STATUS1_ARBITRATION_LOST
= 0x02,
74 STATUS1_READ_INCOMPLETE
= 0x03,
75 STATUS1_WRITE_INCOMPLETE
= 0x04,
76 STATUS1_SUCCESS
= 0x05,
79 struct cp2112_smbus_config_report
{
80 u8 report
; /* CP2112_SMBUS_CONFIG */
81 __be32 clock_speed
; /* Hz */
82 u8 device_address
; /* Stored in the upper 7 bits */
83 u8 auto_send_read
; /* 1 = enabled, 0 = disabled */
84 __be16 write_timeout
; /* ms, 0 = no timeout */
85 __be16 read_timeout
; /* ms, 0 = no timeout */
86 u8 scl_low_timeout
; /* 1 = enabled, 0 = disabled */
87 __be16 retry_time
; /* # of retries, 0 = no limit */
90 struct cp2112_usb_config_report
{
91 u8 report
; /* CP2112_USB_CONFIG */
92 __le16 vid
; /* Vendor ID */
93 __le16 pid
; /* Product ID */
94 u8 max_power
; /* Power requested in 2mA units */
95 u8 power_mode
; /* 0x00 = bus powered
96 0x01 = self powered & regulator off
97 0x02 = self powered & regulator on */
100 u8 mask
; /* What fields to program */
103 struct cp2112_read_req_report
{
104 u8 report
; /* CP2112_DATA_READ_REQUEST */
109 struct cp2112_write_read_req_report
{
110 u8 report
; /* CP2112_DATA_WRITE_READ_REQUEST */
113 u8 target_address_length
;
114 u8 target_address
[16];
117 struct cp2112_write_req_report
{
118 u8 report
; /* CP2112_DATA_WRITE_REQUEST */
124 struct cp2112_force_read_report
{
125 u8 report
; /* CP2112_DATA_READ_FORCE_SEND */
129 struct cp2112_xfer_status_report
{
130 u8 report
; /* CP2112_TRANSFER_STATUS_RESPONSE */
131 u8 status0
; /* STATUS0_* */
132 u8 status1
; /* STATUS1_* */
137 struct cp2112_string_report
{
138 u8 dummy
; /* force .string to be aligned */
139 u8 report
; /* CP2112_*_STRING */
140 u8 length
; /* length in bytes of everyting after .report */
141 u8 type
; /* USB_DT_STRING */
142 wchar_t string
[30]; /* UTF16_LITTLE_ENDIAN string */
145 /* Number of times to request transfer status before giving up waiting for a
146 transfer to complete. This may need to be changed if SMBUS clock, retries,
147 or read/write/scl_low timeout settings are changed. */
148 static const int XFER_STATUS_RETRIES
= 10;
150 /* Time in ms to wait for a CP2112_DATA_READ_RESPONSE or
151 CP2112_TRANSFER_STATUS_RESPONSE. */
152 static const int RESPONSE_TIMEOUT
= 50;
154 static const struct hid_device_id cp2112_devices
[] = {
155 { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL
, USB_DEVICE_ID_CYGNAL_CP2112
) },
158 MODULE_DEVICE_TABLE(hid
, cp2112_devices
);
160 struct cp2112_device
{
161 struct i2c_adapter adap
;
162 struct hid_device
*hdev
;
163 wait_queue_head_t wait
;
174 struct gpio_desc
*desc
[8];
176 struct delayed_work gpio_poll_worker
;
177 unsigned long irq_mask
;
181 static int gpio_push_pull
= 0xFF;
182 module_param(gpio_push_pull
, int, S_IRUGO
| S_IWUSR
);
183 MODULE_PARM_DESC(gpio_push_pull
, "GPIO push-pull configuration bitmask");
185 static int cp2112_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
187 struct cp2112_device
*dev
= gpiochip_get_data(chip
);
188 struct hid_device
*hdev
= dev
->hdev
;
189 u8
*buf
= dev
->in_out_buffer
;
192 mutex_lock(&dev
->lock
);
194 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
195 CP2112_GPIO_CONFIG_LENGTH
, HID_FEATURE_REPORT
,
197 if (ret
!= CP2112_GPIO_CONFIG_LENGTH
) {
198 hid_err(hdev
, "error requesting GPIO config: %d\n", ret
);
204 buf
[1] &= ~(1 << offset
);
205 buf
[2] = gpio_push_pull
;
207 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
208 CP2112_GPIO_CONFIG_LENGTH
, HID_FEATURE_REPORT
,
210 if (ret
!= CP2112_GPIO_CONFIG_LENGTH
) {
211 hid_err(hdev
, "error setting GPIO config: %d\n", ret
);
220 mutex_unlock(&dev
->lock
);
224 static void cp2112_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
226 struct cp2112_device
*dev
= gpiochip_get_data(chip
);
227 struct hid_device
*hdev
= dev
->hdev
;
228 u8
*buf
= dev
->in_out_buffer
;
231 mutex_lock(&dev
->lock
);
233 buf
[0] = CP2112_GPIO_SET
;
234 buf
[1] = value
? 0xff : 0;
235 buf
[2] = 1 << offset
;
237 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_SET
, buf
,
238 CP2112_GPIO_SET_LENGTH
, HID_FEATURE_REPORT
,
241 hid_err(hdev
, "error setting GPIO values: %d\n", ret
);
243 mutex_unlock(&dev
->lock
);
246 static int cp2112_gpio_get_all(struct gpio_chip
*chip
)
248 struct cp2112_device
*dev
= gpiochip_get_data(chip
);
249 struct hid_device
*hdev
= dev
->hdev
;
250 u8
*buf
= dev
->in_out_buffer
;
253 mutex_lock(&dev
->lock
);
255 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_GET
, buf
,
256 CP2112_GPIO_GET_LENGTH
, HID_FEATURE_REPORT
,
258 if (ret
!= CP2112_GPIO_GET_LENGTH
) {
259 hid_err(hdev
, "error requesting GPIO values: %d\n", ret
);
260 ret
= ret
< 0 ? ret
: -EIO
;
267 mutex_unlock(&dev
->lock
);
272 static int cp2112_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
276 ret
= cp2112_gpio_get_all(chip
);
280 return (ret
>> offset
) & 1;
283 static int cp2112_gpio_direction_output(struct gpio_chip
*chip
,
284 unsigned offset
, int value
)
286 struct cp2112_device
*dev
= gpiochip_get_data(chip
);
287 struct hid_device
*hdev
= dev
->hdev
;
288 u8
*buf
= dev
->in_out_buffer
;
291 mutex_lock(&dev
->lock
);
293 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
294 CP2112_GPIO_CONFIG_LENGTH
, HID_FEATURE_REPORT
,
296 if (ret
!= CP2112_GPIO_CONFIG_LENGTH
) {
297 hid_err(hdev
, "error requesting GPIO config: %d\n", ret
);
301 buf
[1] |= 1 << offset
;
302 buf
[2] = gpio_push_pull
;
304 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
305 CP2112_GPIO_CONFIG_LENGTH
, HID_FEATURE_REPORT
,
308 hid_err(hdev
, "error setting GPIO config: %d\n", ret
);
312 mutex_unlock(&dev
->lock
);
315 * Set gpio value when output direction is already set,
316 * as specified in AN495, Rev. 0.2, cpt. 4.4
318 cp2112_gpio_set(chip
, offset
, value
);
323 mutex_unlock(&dev
->lock
);
324 return ret
< 0 ? ret
: -EIO
;
327 static int cp2112_hid_get(struct hid_device
*hdev
, unsigned char report_number
,
328 u8
*data
, size_t count
, unsigned char report_type
)
333 buf
= kmalloc(count
, GFP_KERNEL
);
337 ret
= hid_hw_raw_request(hdev
, report_number
, buf
, count
,
338 report_type
, HID_REQ_GET_REPORT
);
339 memcpy(data
, buf
, count
);
344 static int cp2112_hid_output(struct hid_device
*hdev
, u8
*data
, size_t count
,
345 unsigned char report_type
)
350 buf
= kmemdup(data
, count
, GFP_KERNEL
);
354 if (report_type
== HID_OUTPUT_REPORT
)
355 ret
= hid_hw_output_report(hdev
, buf
, count
);
357 ret
= hid_hw_raw_request(hdev
, buf
[0], buf
, count
, report_type
,
364 static int cp2112_wait(struct cp2112_device
*dev
, atomic_t
*avail
)
368 /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a
369 * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to
370 * come in cp2112_raw_event or timeout. There will only be one of these
371 * in flight at any one time. The timeout is extremely large and is a
372 * last resort if the CP2112 has died. If we do timeout we don't expect
373 * to receive the response which would cause data races, it's not like
374 * we can do anything about it anyway.
376 ret
= wait_event_interruptible_timeout(dev
->wait
,
377 atomic_read(avail
), msecs_to_jiffies(RESPONSE_TIMEOUT
));
378 if (-ERESTARTSYS
== ret
)
383 atomic_set(avail
, 0);
387 static int cp2112_xfer_status(struct cp2112_device
*dev
)
389 struct hid_device
*hdev
= dev
->hdev
;
393 buf
[0] = CP2112_TRANSFER_STATUS_REQUEST
;
395 atomic_set(&dev
->xfer_avail
, 0);
397 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
399 hid_warn(hdev
, "Error requesting status: %d\n", ret
);
403 ret
= cp2112_wait(dev
, &dev
->xfer_avail
);
407 return dev
->xfer_status
;
410 static int cp2112_read(struct cp2112_device
*dev
, u8
*data
, size_t size
)
412 struct hid_device
*hdev
= dev
->hdev
;
413 struct cp2112_force_read_report report
;
416 if (size
> sizeof(dev
->read_data
))
417 size
= sizeof(dev
->read_data
);
418 report
.report
= CP2112_DATA_READ_FORCE_SEND
;
419 report
.length
= cpu_to_be16(size
);
421 atomic_set(&dev
->read_avail
, 0);
423 ret
= cp2112_hid_output(hdev
, &report
.report
, sizeof(report
),
426 hid_warn(hdev
, "Error requesting data: %d\n", ret
);
430 ret
= cp2112_wait(dev
, &dev
->read_avail
);
434 hid_dbg(hdev
, "read %d of %zd bytes requested\n",
435 dev
->read_length
, size
);
437 if (size
> dev
->read_length
)
438 size
= dev
->read_length
;
440 memcpy(data
, dev
->read_data
, size
);
441 return dev
->read_length
;
444 static int cp2112_read_req(void *buf
, u8 slave_address
, u16 length
)
446 struct cp2112_read_req_report
*report
= buf
;
448 if (length
< 1 || length
> 512)
451 report
->report
= CP2112_DATA_READ_REQUEST
;
452 report
->slave_address
= slave_address
<< 1;
453 report
->length
= cpu_to_be16(length
);
454 return sizeof(*report
);
457 static int cp2112_write_read_req(void *buf
, u8 slave_address
, u16 length
,
458 u8 command
, u8
*data
, u8 data_length
)
460 struct cp2112_write_read_req_report
*report
= buf
;
462 if (length
< 1 || length
> 512
463 || data_length
> sizeof(report
->target_address
) - 1)
466 report
->report
= CP2112_DATA_WRITE_READ_REQUEST
;
467 report
->slave_address
= slave_address
<< 1;
468 report
->length
= cpu_to_be16(length
);
469 report
->target_address_length
= data_length
+ 1;
470 report
->target_address
[0] = command
;
471 memcpy(&report
->target_address
[1], data
, data_length
);
472 return data_length
+ 6;
475 static int cp2112_write_req(void *buf
, u8 slave_address
, u8 command
, u8
*data
,
478 struct cp2112_write_req_report
*report
= buf
;
480 if (data_length
> sizeof(report
->data
) - 1)
483 report
->report
= CP2112_DATA_WRITE_REQUEST
;
484 report
->slave_address
= slave_address
<< 1;
485 report
->length
= data_length
+ 1;
486 report
->data
[0] = command
;
487 memcpy(&report
->data
[1], data
, data_length
);
488 return data_length
+ 4;
491 static int cp2112_i2c_write_req(void *buf
, u8 slave_address
, u8
*data
,
494 struct cp2112_write_req_report
*report
= buf
;
496 if (data_length
> sizeof(report
->data
))
499 report
->report
= CP2112_DATA_WRITE_REQUEST
;
500 report
->slave_address
= slave_address
<< 1;
501 report
->length
= data_length
;
502 memcpy(report
->data
, data
, data_length
);
503 return data_length
+ 3;
506 static int cp2112_i2c_write_read_req(void *buf
, u8 slave_address
,
507 u8
*addr
, int addr_length
,
510 struct cp2112_write_read_req_report
*report
= buf
;
512 if (read_length
< 1 || read_length
> 512 ||
513 addr_length
> sizeof(report
->target_address
))
516 report
->report
= CP2112_DATA_WRITE_READ_REQUEST
;
517 report
->slave_address
= slave_address
<< 1;
518 report
->length
= cpu_to_be16(read_length
);
519 report
->target_address_length
= addr_length
;
520 memcpy(report
->target_address
, addr
, addr_length
);
521 return addr_length
+ 5;
524 static int cp2112_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
527 struct cp2112_device
*dev
= (struct cp2112_device
*)adap
->algo_data
;
528 struct hid_device
*hdev
= dev
->hdev
;
531 ssize_t read_length
= 0;
533 unsigned int retries
;
536 hid_dbg(hdev
, "I2C %d messages\n", num
);
539 if (msgs
->flags
& I2C_M_RD
) {
540 hid_dbg(hdev
, "I2C read %#04x len %d\n",
541 msgs
->addr
, msgs
->len
);
542 read_length
= msgs
->len
;
543 read_buf
= msgs
->buf
;
544 count
= cp2112_read_req(buf
, msgs
->addr
, msgs
->len
);
546 hid_dbg(hdev
, "I2C write %#04x len %d\n",
547 msgs
->addr
, msgs
->len
);
548 count
= cp2112_i2c_write_req(buf
, msgs
->addr
,
549 msgs
->buf
, msgs
->len
);
553 } else if (dev
->hwversion
> 1 && /* no repeated start in rev 1 */
555 msgs
[0].addr
== msgs
[1].addr
&&
556 !(msgs
[0].flags
& I2C_M_RD
) && (msgs
[1].flags
& I2C_M_RD
)) {
557 hid_dbg(hdev
, "I2C write-read %#04x wlen %d rlen %d\n",
558 msgs
[0].addr
, msgs
[0].len
, msgs
[1].len
);
559 read_length
= msgs
[1].len
;
560 read_buf
= msgs
[1].buf
;
561 count
= cp2112_i2c_write_read_req(buf
, msgs
[0].addr
,
562 msgs
[0].buf
, msgs
[0].len
, msgs
[1].len
);
567 "Multi-message I2C transactions not supported\n");
571 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
573 hid_err(hdev
, "power management error: %d\n", ret
);
577 ret
= cp2112_hid_output(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
579 hid_warn(hdev
, "Error starting transaction: %d\n", ret
);
583 for (retries
= 0; retries
< XFER_STATUS_RETRIES
; ++retries
) {
584 ret
= cp2112_xfer_status(dev
);
592 if (XFER_STATUS_RETRIES
<= retries
) {
593 hid_warn(hdev
, "Transfer timed out, cancelling.\n");
594 buf
[0] = CP2112_CANCEL_TRANSFER
;
597 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
599 hid_warn(hdev
, "Error cancelling transaction: %d\n",
606 for (count
= 0; count
< read_length
;) {
607 ret
= cp2112_read(dev
, read_buf
+ count
, read_length
- count
);
611 hid_err(hdev
, "read returned 0\n");
616 if (count
> read_length
) {
618 * The hardware returned too much data.
619 * This is mostly harmless because cp2112_read()
620 * has a limit check so didn't overrun our
621 * buffer. Nevertheless, we return an error
622 * because something is seriously wrong and
623 * it shouldn't go unnoticed.
625 hid_err(hdev
, "long read: %d > %zd\n",
626 ret
, read_length
- count
+ ret
);
632 /* return the number of transferred messages */
636 hid_hw_power(hdev
, PM_HINT_NORMAL
);
637 hid_dbg(hdev
, "I2C transfer finished: %d\n", ret
);
641 static int cp2112_xfer(struct i2c_adapter
*adap
, u16 addr
,
642 unsigned short flags
, char read_write
, u8 command
,
643 int size
, union i2c_smbus_data
*data
)
645 struct cp2112_device
*dev
= (struct cp2112_device
*)adap
->algo_data
;
646 struct hid_device
*hdev
= dev
->hdev
;
650 size_t read_length
= 0;
651 unsigned int retries
;
654 hid_dbg(hdev
, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n",
655 read_write
== I2C_SMBUS_WRITE
? "write" : "read",
656 addr
, flags
, command
, size
);
662 if (I2C_SMBUS_READ
== read_write
)
663 count
= cp2112_read_req(buf
, addr
, read_length
);
665 count
= cp2112_write_req(buf
, addr
, command
, NULL
,
668 case I2C_SMBUS_BYTE_DATA
:
671 if (I2C_SMBUS_READ
== read_write
)
672 count
= cp2112_write_read_req(buf
, addr
, read_length
,
675 count
= cp2112_write_req(buf
, addr
, command
,
678 case I2C_SMBUS_WORD_DATA
:
680 word
= cpu_to_le16(data
->word
);
682 if (I2C_SMBUS_READ
== read_write
)
683 count
= cp2112_write_read_req(buf
, addr
, read_length
,
686 count
= cp2112_write_req(buf
, addr
, command
,
689 case I2C_SMBUS_PROC_CALL
:
690 size
= I2C_SMBUS_WORD_DATA
;
691 read_write
= I2C_SMBUS_READ
;
693 word
= cpu_to_le16(data
->word
);
695 count
= cp2112_write_read_req(buf
, addr
, read_length
, command
,
698 case I2C_SMBUS_I2C_BLOCK_DATA
:
699 size
= I2C_SMBUS_BLOCK_DATA
;
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_BLOCK_DATA
:
789 if (read_length
> I2C_SMBUS_BLOCK_MAX
) {
794 memcpy(data
->block
, buf
, read_length
);
800 hid_hw_power(hdev
, PM_HINT_NORMAL
);
801 hid_dbg(hdev
, "transfer finished: %d\n", ret
);
805 static u32
cp2112_functionality(struct i2c_adapter
*adap
)
807 return I2C_FUNC_I2C
|
808 I2C_FUNC_SMBUS_BYTE
|
809 I2C_FUNC_SMBUS_BYTE_DATA
|
810 I2C_FUNC_SMBUS_WORD_DATA
|
811 I2C_FUNC_SMBUS_BLOCK_DATA
|
812 I2C_FUNC_SMBUS_I2C_BLOCK
|
813 I2C_FUNC_SMBUS_PROC_CALL
|
814 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
;
817 static const struct i2c_algorithm smbus_algorithm
= {
818 .master_xfer
= cp2112_i2c_xfer
,
819 .smbus_xfer
= cp2112_xfer
,
820 .functionality
= cp2112_functionality
,
823 static int cp2112_get_usb_config(struct hid_device
*hdev
,
824 struct cp2112_usb_config_report
*cfg
)
828 ret
= cp2112_hid_get(hdev
, CP2112_USB_CONFIG
, (u8
*)cfg
, sizeof(*cfg
),
830 if (ret
!= sizeof(*cfg
)) {
831 hid_err(hdev
, "error reading usb config: %d\n", ret
);
840 static int cp2112_set_usb_config(struct hid_device
*hdev
,
841 struct cp2112_usb_config_report
*cfg
)
845 BUG_ON(cfg
->report
!= CP2112_USB_CONFIG
);
847 ret
= cp2112_hid_output(hdev
, (u8
*)cfg
, sizeof(*cfg
),
849 if (ret
!= sizeof(*cfg
)) {
850 hid_err(hdev
, "error writing usb config: %d\n", ret
);
859 static void chmod_sysfs_attrs(struct hid_device
*hdev
);
861 #define CP2112_CONFIG_ATTR(name, store, format, ...) \
862 static ssize_t name##_store(struct device *kdev, \
863 struct device_attribute *attr, const char *buf, \
866 struct hid_device *hdev = to_hid_device(kdev); \
867 struct cp2112_usb_config_report cfg; \
868 int ret = cp2112_get_usb_config(hdev, &cfg); \
872 ret = cp2112_set_usb_config(hdev, &cfg); \
875 chmod_sysfs_attrs(hdev); \
878 static ssize_t name##_show(struct device *kdev, \
879 struct device_attribute *attr, char *buf) \
881 struct hid_device *hdev = to_hid_device(kdev); \
882 struct cp2112_usb_config_report cfg; \
883 int ret = cp2112_get_usb_config(hdev, &cfg); \
886 return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \
888 static DEVICE_ATTR_RW(name);
890 CP2112_CONFIG_ATTR(vendor_id
, ({
893 if (sscanf(buf
, "%hi", &vid
) != 1)
896 cfg
.vid
= cpu_to_le16(vid
);
898 }), "0x%04x\n", le16_to_cpu(cfg
.vid
));
900 CP2112_CONFIG_ATTR(product_id
, ({
903 if (sscanf(buf
, "%hi", &pid
) != 1)
906 cfg
.pid
= cpu_to_le16(pid
);
908 }), "0x%04x\n", le16_to_cpu(cfg
.pid
));
910 CP2112_CONFIG_ATTR(max_power
, ({
913 if (sscanf(buf
, "%i", &mA
) != 1)
916 cfg
.max_power
= (mA
+ 1) / 2;
918 }), "%u mA\n", cfg
.max_power
* 2);
920 CP2112_CONFIG_ATTR(power_mode
, ({
921 if (sscanf(buf
, "%hhi", &cfg
.power_mode
) != 1)
925 }), "%u\n", cfg
.power_mode
);
927 CP2112_CONFIG_ATTR(release_version
, ({
928 if (sscanf(buf
, "%hhi.%hhi", &cfg
.release_major
, &cfg
.release_minor
)
933 }), "%u.%u\n", cfg
.release_major
, cfg
.release_minor
);
935 #undef CP2112_CONFIG_ATTR
937 struct cp2112_pstring_attribute
{
938 struct device_attribute attr
;
939 unsigned char report
;
942 static ssize_t
pstr_store(struct device
*kdev
,
943 struct device_attribute
*kattr
, const char *buf
,
946 struct hid_device
*hdev
= to_hid_device(kdev
);
947 struct cp2112_pstring_attribute
*attr
=
948 container_of(kattr
, struct cp2112_pstring_attribute
, attr
);
949 struct cp2112_string_report report
;
952 memset(&report
, 0, sizeof(report
));
954 ret
= utf8s_to_utf16s(buf
, count
, UTF16_LITTLE_ENDIAN
,
955 report
.string
, ARRAY_SIZE(report
.string
));
956 report
.report
= attr
->report
;
957 report
.length
= ret
* sizeof(report
.string
[0]) + 2;
958 report
.type
= USB_DT_STRING
;
960 ret
= cp2112_hid_output(hdev
, &report
.report
, report
.length
+ 1,
962 if (ret
!= report
.length
+ 1) {
963 hid_err(hdev
, "error writing %s string: %d\n", kattr
->attr
.name
,
970 chmod_sysfs_attrs(hdev
);
974 static ssize_t
pstr_show(struct device
*kdev
,
975 struct device_attribute
*kattr
, char *buf
)
977 struct hid_device
*hdev
= to_hid_device(kdev
);
978 struct cp2112_pstring_attribute
*attr
=
979 container_of(kattr
, struct cp2112_pstring_attribute
, attr
);
980 struct cp2112_string_report report
;
984 ret
= cp2112_hid_get(hdev
, attr
->report
, &report
.report
,
985 sizeof(report
) - 1, HID_FEATURE_REPORT
);
987 hid_err(hdev
, "error reading %s string: %d\n", kattr
->attr
.name
,
994 if (report
.length
< 2) {
995 hid_err(hdev
, "invalid %s string length: %d\n",
996 kattr
->attr
.name
, report
.length
);
1000 length
= report
.length
> ret
- 1 ? ret
- 1 : report
.length
;
1001 length
= (length
- 2) / sizeof(report
.string
[0]);
1002 ret
= utf16s_to_utf8s(report
.string
, length
, UTF16_LITTLE_ENDIAN
, buf
,
1008 #define CP2112_PSTR_ATTR(name, _report) \
1009 static struct cp2112_pstring_attribute dev_attr_##name = { \
1010 .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \
1011 .report = _report, \
1014 CP2112_PSTR_ATTR(manufacturer
, CP2112_MANUFACTURER_STRING
);
1015 CP2112_PSTR_ATTR(product
, CP2112_PRODUCT_STRING
);
1016 CP2112_PSTR_ATTR(serial
, CP2112_SERIAL_STRING
);
1018 #undef CP2112_PSTR_ATTR
1020 static const struct attribute_group cp2112_attr_group
= {
1021 .attrs
= (struct attribute
*[]){
1022 &dev_attr_vendor_id
.attr
,
1023 &dev_attr_product_id
.attr
,
1024 &dev_attr_max_power
.attr
,
1025 &dev_attr_power_mode
.attr
,
1026 &dev_attr_release_version
.attr
,
1027 &dev_attr_manufacturer
.attr
.attr
,
1028 &dev_attr_product
.attr
.attr
,
1029 &dev_attr_serial
.attr
.attr
,
1034 /* Chmoding our sysfs attributes is simply a way to expose which fields in the
1035 * PROM have already been programmed. We do not depend on this preventing
1036 * writing to these attributes since the CP2112 will simply ignore writes to
1037 * already-programmed fields. This is why there is no sense in fixing this
1040 static void chmod_sysfs_attrs(struct hid_device
*hdev
)
1042 struct attribute
**attr
;
1046 ret
= cp2112_hid_get(hdev
, CP2112_LOCK_BYTE
, buf
, sizeof(buf
),
1047 HID_FEATURE_REPORT
);
1048 if (ret
!= sizeof(buf
)) {
1049 hid_err(hdev
, "error reading lock byte: %d\n", ret
);
1053 for (attr
= cp2112_attr_group
.attrs
; *attr
; ++attr
) {
1054 umode_t mode
= (buf
[1] & 1) ? S_IWUSR
| S_IRUGO
: S_IRUGO
;
1055 ret
= sysfs_chmod_file(&hdev
->dev
.kobj
, *attr
, mode
);
1057 hid_err(hdev
, "error chmoding sysfs file %s\n",
1063 static void cp2112_gpio_irq_ack(struct irq_data
*d
)
1067 static void cp2112_gpio_irq_mask(struct irq_data
*d
)
1069 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1070 struct cp2112_device
*dev
= gpiochip_get_data(gc
);
1072 __clear_bit(d
->hwirq
, &dev
->irq_mask
);
1075 static void cp2112_gpio_irq_unmask(struct irq_data
*d
)
1077 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1078 struct cp2112_device
*dev
= gpiochip_get_data(gc
);
1080 __set_bit(d
->hwirq
, &dev
->irq_mask
);
1083 static void cp2112_gpio_poll_callback(struct work_struct
*work
)
1085 struct cp2112_device
*dev
= container_of(work
, struct cp2112_device
,
1086 gpio_poll_worker
.work
);
1089 u8 virqs
= (u8
)dev
->irq_mask
;
1093 ret
= cp2112_gpio_get_all(&dev
->gc
);
1094 if (ret
== -ENODEV
) /* the hardware has been disconnected */
1102 virq
= ffs(virqs
) - 1;
1103 virqs
&= ~BIT(virq
);
1105 if (!dev
->gc
.to_irq
)
1108 irq
= dev
->gc
.to_irq(&dev
->gc
, virq
);
1110 d
= irq_get_irq_data(irq
);
1114 irq_type
= irqd_get_trigger_type(d
);
1116 if (gpio_mask
& BIT(virq
)) {
1119 if (irq_type
& IRQ_TYPE_LEVEL_HIGH
)
1120 handle_nested_irq(irq
);
1122 if ((irq_type
& IRQ_TYPE_EDGE_RISING
) &&
1123 !(dev
->gpio_prev_state
& BIT(virq
)))
1124 handle_nested_irq(irq
);
1128 if (irq_type
& IRQ_TYPE_LEVEL_LOW
)
1129 handle_nested_irq(irq
);
1131 if ((irq_type
& IRQ_TYPE_EDGE_FALLING
) &&
1132 (dev
->gpio_prev_state
& BIT(virq
)))
1133 handle_nested_irq(irq
);
1137 dev
->gpio_prev_state
= gpio_mask
;
1141 schedule_delayed_work(&dev
->gpio_poll_worker
, 10);
1145 static unsigned int cp2112_gpio_irq_startup(struct irq_data
*d
)
1147 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1148 struct cp2112_device
*dev
= gpiochip_get_data(gc
);
1150 INIT_DELAYED_WORK(&dev
->gpio_poll_worker
, cp2112_gpio_poll_callback
);
1152 cp2112_gpio_direction_input(gc
, d
->hwirq
);
1154 if (!dev
->gpio_poll
) {
1155 dev
->gpio_poll
= true;
1156 schedule_delayed_work(&dev
->gpio_poll_worker
, 0);
1159 cp2112_gpio_irq_unmask(d
);
1163 static void cp2112_gpio_irq_shutdown(struct irq_data
*d
)
1165 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1166 struct cp2112_device
*dev
= gpiochip_get_data(gc
);
1168 cancel_delayed_work_sync(&dev
->gpio_poll_worker
);
1171 static int cp2112_gpio_irq_type(struct irq_data
*d
, unsigned int type
)
1176 static struct irq_chip cp2112_gpio_irqchip
= {
1177 .name
= "cp2112-gpio",
1178 .irq_startup
= cp2112_gpio_irq_startup
,
1179 .irq_shutdown
= cp2112_gpio_irq_shutdown
,
1180 .irq_ack
= cp2112_gpio_irq_ack
,
1181 .irq_mask
= cp2112_gpio_irq_mask
,
1182 .irq_unmask
= cp2112_gpio_irq_unmask
,
1183 .irq_set_type
= cp2112_gpio_irq_type
,
1186 static int __maybe_unused
cp2112_allocate_irq(struct cp2112_device
*dev
,
1194 dev
->desc
[pin
] = gpiochip_request_own_desc(&dev
->gc
, pin
,
1196 if (IS_ERR(dev
->desc
[pin
])) {
1197 dev_err(dev
->gc
.parent
, "Failed to request GPIO\n");
1198 return PTR_ERR(dev
->desc
[pin
]);
1201 ret
= gpiochip_lock_as_irq(&dev
->gc
, pin
);
1203 dev_err(dev
->gc
.parent
, "Failed to lock GPIO as interrupt\n");
1207 ret
= gpiod_to_irq(dev
->desc
[pin
]);
1209 dev_err(dev
->gc
.parent
, "Failed to translate GPIO to IRQ\n");
1216 gpiochip_unlock_as_irq(&dev
->gc
, pin
);
1218 gpiochip_free_own_desc(dev
->desc
[pin
]);
1219 dev
->desc
[pin
] = NULL
;
1223 static int cp2112_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
1225 struct cp2112_device
*dev
;
1227 struct cp2112_smbus_config_report config
;
1230 dev
= devm_kzalloc(&hdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
1234 dev
->in_out_buffer
= devm_kzalloc(&hdev
->dev
, CP2112_REPORT_MAX_LENGTH
,
1236 if (!dev
->in_out_buffer
)
1239 mutex_init(&dev
->lock
);
1241 ret
= hid_parse(hdev
);
1243 hid_err(hdev
, "parse failed\n");
1247 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
1249 hid_err(hdev
, "hw start failed\n");
1253 ret
= hid_hw_open(hdev
);
1255 hid_err(hdev
, "hw open failed\n");
1259 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
1261 hid_err(hdev
, "power management error: %d\n", ret
);
1265 ret
= cp2112_hid_get(hdev
, CP2112_GET_VERSION_INFO
, buf
, sizeof(buf
),
1266 HID_FEATURE_REPORT
);
1267 if (ret
!= sizeof(buf
)) {
1268 hid_err(hdev
, "error requesting version\n");
1271 goto err_power_normal
;
1274 hid_info(hdev
, "Part Number: 0x%02X Device Version: 0x%02X\n",
1277 ret
= cp2112_hid_get(hdev
, CP2112_SMBUS_CONFIG
, (u8
*)&config
,
1278 sizeof(config
), HID_FEATURE_REPORT
);
1279 if (ret
!= sizeof(config
)) {
1280 hid_err(hdev
, "error requesting SMBus config\n");
1283 goto err_power_normal
;
1286 config
.retry_time
= cpu_to_be16(1);
1288 ret
= cp2112_hid_output(hdev
, (u8
*)&config
, sizeof(config
),
1289 HID_FEATURE_REPORT
);
1290 if (ret
!= sizeof(config
)) {
1291 hid_err(hdev
, "error setting SMBus config\n");
1294 goto err_power_normal
;
1297 hid_set_drvdata(hdev
, (void *)dev
);
1299 dev
->adap
.owner
= THIS_MODULE
;
1300 dev
->adap
.class = I2C_CLASS_HWMON
;
1301 dev
->adap
.algo
= &smbus_algorithm
;
1302 dev
->adap
.algo_data
= dev
;
1303 dev
->adap
.dev
.parent
= &hdev
->dev
;
1304 snprintf(dev
->adap
.name
, sizeof(dev
->adap
.name
),
1305 "CP2112 SMBus Bridge on hidraw%d",
1306 ((struct hidraw
*)hdev
->hidraw
)->minor
);
1307 dev
->hwversion
= buf
[2];
1308 init_waitqueue_head(&dev
->wait
);
1310 hid_device_io_start(hdev
);
1311 ret
= i2c_add_adapter(&dev
->adap
);
1312 hid_device_io_stop(hdev
);
1315 hid_err(hdev
, "error registering i2c adapter\n");
1316 goto err_power_normal
;
1319 hid_dbg(hdev
, "adapter registered\n");
1321 dev
->gc
.label
= "cp2112_gpio";
1322 dev
->gc
.direction_input
= cp2112_gpio_direction_input
;
1323 dev
->gc
.direction_output
= cp2112_gpio_direction_output
;
1324 dev
->gc
.set
= cp2112_gpio_set
;
1325 dev
->gc
.get
= cp2112_gpio_get
;
1328 dev
->gc
.can_sleep
= 1;
1329 dev
->gc
.parent
= &hdev
->dev
;
1331 ret
= gpiochip_add_data(&dev
->gc
, dev
);
1333 hid_err(hdev
, "error registering gpio chip\n");
1337 ret
= sysfs_create_group(&hdev
->dev
.kobj
, &cp2112_attr_group
);
1339 hid_err(hdev
, "error creating sysfs attrs\n");
1340 goto err_gpiochip_remove
;
1343 chmod_sysfs_attrs(hdev
);
1344 hid_hw_power(hdev
, PM_HINT_NORMAL
);
1346 ret
= gpiochip_irqchip_add(&dev
->gc
, &cp2112_gpio_irqchip
, 0,
1347 handle_simple_irq
, IRQ_TYPE_NONE
);
1349 dev_err(dev
->gc
.parent
, "failed to add IRQ chip\n");
1350 goto err_sysfs_remove
;
1356 sysfs_remove_group(&hdev
->dev
.kobj
, &cp2112_attr_group
);
1357 err_gpiochip_remove
:
1358 gpiochip_remove(&dev
->gc
);
1360 i2c_del_adapter(&dev
->adap
);
1362 hid_hw_power(hdev
, PM_HINT_NORMAL
);
1370 static void cp2112_remove(struct hid_device
*hdev
)
1372 struct cp2112_device
*dev
= hid_get_drvdata(hdev
);
1375 sysfs_remove_group(&hdev
->dev
.kobj
, &cp2112_attr_group
);
1376 i2c_del_adapter(&dev
->adap
);
1378 if (dev
->gpio_poll
) {
1379 dev
->gpio_poll
= false;
1380 cancel_delayed_work_sync(&dev
->gpio_poll_worker
);
1383 for (i
= 0; i
< ARRAY_SIZE(dev
->desc
); i
++) {
1384 gpiochip_unlock_as_irq(&dev
->gc
, i
);
1385 gpiochip_free_own_desc(dev
->desc
[i
]);
1388 gpiochip_remove(&dev
->gc
);
1389 /* i2c_del_adapter has finished removing all i2c devices from our
1390 * adapter. Well behaved devices should no longer call our cp2112_xfer
1391 * and should have waited for any pending calls to finish. It has also
1392 * waited for device_unregister(&adap->dev) to complete. Therefore we
1393 * can safely free our struct cp2112_device.
1399 static int cp2112_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
1402 struct cp2112_device
*dev
= hid_get_drvdata(hdev
);
1403 struct cp2112_xfer_status_report
*xfer
= (void *)data
;
1406 case CP2112_TRANSFER_STATUS_RESPONSE
:
1407 hid_dbg(hdev
, "xfer status: %02x %02x %04x %04x\n",
1408 xfer
->status0
, xfer
->status1
,
1409 be16_to_cpu(xfer
->retries
), be16_to_cpu(xfer
->length
));
1411 switch (xfer
->status0
) {
1413 dev
->xfer_status
= -EAGAIN
;
1416 dev
->xfer_status
= -EBUSY
;
1418 case STATUS0_COMPLETE
:
1419 dev
->xfer_status
= be16_to_cpu(xfer
->length
);
1422 switch (xfer
->status1
) {
1423 case STATUS1_TIMEOUT_NACK
:
1424 case STATUS1_TIMEOUT_BUS
:
1425 dev
->xfer_status
= -ETIMEDOUT
;
1428 dev
->xfer_status
= -EIO
;
1433 dev
->xfer_status
= -EINVAL
;
1437 atomic_set(&dev
->xfer_avail
, 1);
1439 case CP2112_DATA_READ_RESPONSE
:
1440 hid_dbg(hdev
, "read response: %02x %02x\n", data
[1], data
[2]);
1442 dev
->read_length
= data
[2];
1443 if (dev
->read_length
> sizeof(dev
->read_data
))
1444 dev
->read_length
= sizeof(dev
->read_data
);
1446 memcpy(dev
->read_data
, &data
[3], dev
->read_length
);
1447 atomic_set(&dev
->read_avail
, 1);
1450 hid_err(hdev
, "unknown report\n");
1455 wake_up_interruptible(&dev
->wait
);
1459 static struct hid_driver cp2112_driver
= {
1461 .id_table
= cp2112_devices
,
1462 .probe
= cp2112_probe
,
1463 .remove
= cp2112_remove
,
1464 .raw_event
= cp2112_raw_event
,
1467 module_hid_driver(cp2112_driver
);
1468 MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge");
1469 MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
1470 MODULE_LICENSE("GPL");