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/i2c.h>
31 #include <linux/module.h>
32 #include <linux/nls.h>
33 #include <linux/usb/ch9.h>
36 #define CP2112_REPORT_MAX_LENGTH 64
37 #define CP2112_GPIO_CONFIG_LENGTH 5
38 #define CP2112_GPIO_GET_LENGTH 2
39 #define CP2112_GPIO_SET_LENGTH 3
42 CP2112_GPIO_CONFIG
= 0x02,
43 CP2112_GPIO_GET
= 0x03,
44 CP2112_GPIO_SET
= 0x04,
45 CP2112_GET_VERSION_INFO
= 0x05,
46 CP2112_SMBUS_CONFIG
= 0x06,
47 CP2112_DATA_READ_REQUEST
= 0x10,
48 CP2112_DATA_WRITE_READ_REQUEST
= 0x11,
49 CP2112_DATA_READ_FORCE_SEND
= 0x12,
50 CP2112_DATA_READ_RESPONSE
= 0x13,
51 CP2112_DATA_WRITE_REQUEST
= 0x14,
52 CP2112_TRANSFER_STATUS_REQUEST
= 0x15,
53 CP2112_TRANSFER_STATUS_RESPONSE
= 0x16,
54 CP2112_CANCEL_TRANSFER
= 0x17,
55 CP2112_LOCK_BYTE
= 0x20,
56 CP2112_USB_CONFIG
= 0x21,
57 CP2112_MANUFACTURER_STRING
= 0x22,
58 CP2112_PRODUCT_STRING
= 0x23,
59 CP2112_SERIAL_STRING
= 0x24,
65 STATUS0_COMPLETE
= 0x02,
70 STATUS1_TIMEOUT_NACK
= 0x00,
71 STATUS1_TIMEOUT_BUS
= 0x01,
72 STATUS1_ARBITRATION_LOST
= 0x02,
73 STATUS1_READ_INCOMPLETE
= 0x03,
74 STATUS1_WRITE_INCOMPLETE
= 0x04,
75 STATUS1_SUCCESS
= 0x05,
78 struct cp2112_smbus_config_report
{
79 u8 report
; /* CP2112_SMBUS_CONFIG */
80 __be32 clock_speed
; /* Hz */
81 u8 device_address
; /* Stored in the upper 7 bits */
82 u8 auto_send_read
; /* 1 = enabled, 0 = disabled */
83 __be16 write_timeout
; /* ms, 0 = no timeout */
84 __be16 read_timeout
; /* ms, 0 = no timeout */
85 u8 scl_low_timeout
; /* 1 = enabled, 0 = disabled */
86 __be16 retry_time
; /* # of retries, 0 = no limit */
89 struct cp2112_usb_config_report
{
90 u8 report
; /* CP2112_USB_CONFIG */
91 __le16 vid
; /* Vendor ID */
92 __le16 pid
; /* Product ID */
93 u8 max_power
; /* Power requested in 2mA units */
94 u8 power_mode
; /* 0x00 = bus powered
95 0x01 = self powered & regulator off
96 0x02 = self powered & regulator on */
99 u8 mask
; /* What fields to program */
102 struct cp2112_read_req_report
{
103 u8 report
; /* CP2112_DATA_READ_REQUEST */
108 struct cp2112_write_read_req_report
{
109 u8 report
; /* CP2112_DATA_WRITE_READ_REQUEST */
112 u8 target_address_length
;
113 u8 target_address
[16];
116 struct cp2112_write_req_report
{
117 u8 report
; /* CP2112_DATA_WRITE_REQUEST */
123 struct cp2112_force_read_report
{
124 u8 report
; /* CP2112_DATA_READ_FORCE_SEND */
128 struct cp2112_xfer_status_report
{
129 u8 report
; /* CP2112_TRANSFER_STATUS_RESPONSE */
130 u8 status0
; /* STATUS0_* */
131 u8 status1
; /* STATUS1_* */
136 struct cp2112_string_report
{
137 u8 dummy
; /* force .string to be aligned */
138 u8 report
; /* CP2112_*_STRING */
139 u8 length
; /* length in bytes of everyting after .report */
140 u8 type
; /* USB_DT_STRING */
141 wchar_t string
[30]; /* UTF16_LITTLE_ENDIAN string */
144 /* Number of times to request transfer status before giving up waiting for a
145 transfer to complete. This may need to be changed if SMBUS clock, retries,
146 or read/write/scl_low timeout settings are changed. */
147 static const int XFER_STATUS_RETRIES
= 10;
149 /* Time in ms to wait for a CP2112_DATA_READ_RESPONSE or
150 CP2112_TRANSFER_STATUS_RESPONSE. */
151 static const int RESPONSE_TIMEOUT
= 50;
153 static const struct hid_device_id cp2112_devices
[] = {
154 { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL
, USB_DEVICE_ID_CYGNAL_CP2112
) },
157 MODULE_DEVICE_TABLE(hid
, cp2112_devices
);
159 struct cp2112_device
{
160 struct i2c_adapter adap
;
161 struct hid_device
*hdev
;
162 wait_queue_head_t wait
;
173 struct gpio_desc
*desc
[8];
175 struct delayed_work gpio_poll_worker
;
176 unsigned long irq_mask
;
180 static int gpio_push_pull
= 0xFF;
181 module_param(gpio_push_pull
, int, S_IRUGO
| S_IWUSR
);
182 MODULE_PARM_DESC(gpio_push_pull
, "GPIO push-pull configuration bitmask");
184 static int cp2112_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
186 struct cp2112_device
*dev
= gpiochip_get_data(chip
);
187 struct hid_device
*hdev
= dev
->hdev
;
188 u8
*buf
= dev
->in_out_buffer
;
192 spin_lock_irqsave(&dev
->lock
, flags
);
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
);
202 buf
[1] &= ~(1 << offset
);
203 buf
[2] = gpio_push_pull
;
205 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
206 CP2112_GPIO_CONFIG_LENGTH
, HID_FEATURE_REPORT
,
209 hid_err(hdev
, "error setting GPIO config: %d\n", ret
);
216 spin_unlock_irqrestore(&dev
->lock
, flags
);
217 return ret
<= 0 ? ret
: -EIO
;
220 static void cp2112_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
222 struct cp2112_device
*dev
= gpiochip_get_data(chip
);
223 struct hid_device
*hdev
= dev
->hdev
;
224 u8
*buf
= dev
->in_out_buffer
;
228 spin_lock_irqsave(&dev
->lock
, flags
);
230 buf
[0] = CP2112_GPIO_SET
;
231 buf
[1] = value
? 0xff : 0;
232 buf
[2] = 1 << offset
;
234 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_SET
, buf
,
235 CP2112_GPIO_SET_LENGTH
, HID_FEATURE_REPORT
,
238 hid_err(hdev
, "error setting GPIO values: %d\n", ret
);
240 spin_unlock_irqrestore(&dev
->lock
, flags
);
243 static int cp2112_gpio_get_all(struct gpio_chip
*chip
)
245 struct cp2112_device
*dev
= gpiochip_get_data(chip
);
246 struct hid_device
*hdev
= dev
->hdev
;
247 u8
*buf
= dev
->in_out_buffer
;
251 spin_lock_irqsave(&dev
->lock
, flags
);
253 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_GET
, buf
,
254 CP2112_GPIO_GET_LENGTH
, HID_FEATURE_REPORT
,
256 if (ret
!= CP2112_GPIO_GET_LENGTH
) {
257 hid_err(hdev
, "error requesting GPIO values: %d\n", ret
);
258 ret
= ret
< 0 ? ret
: -EIO
;
265 spin_unlock_irqrestore(&dev
->lock
, flags
);
270 static int cp2112_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
274 ret
= cp2112_gpio_get_all(chip
);
278 return (ret
>> offset
) & 1;
281 static int cp2112_gpio_direction_output(struct gpio_chip
*chip
,
282 unsigned offset
, int value
)
284 struct cp2112_device
*dev
= gpiochip_get_data(chip
);
285 struct hid_device
*hdev
= dev
->hdev
;
286 u8
*buf
= dev
->in_out_buffer
;
290 spin_lock_irqsave(&dev
->lock
, flags
);
292 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
293 CP2112_GPIO_CONFIG_LENGTH
, HID_FEATURE_REPORT
,
295 if (ret
!= CP2112_GPIO_CONFIG_LENGTH
) {
296 hid_err(hdev
, "error requesting GPIO config: %d\n", ret
);
300 buf
[1] |= 1 << offset
;
301 buf
[2] = gpio_push_pull
;
303 ret
= hid_hw_raw_request(hdev
, CP2112_GPIO_CONFIG
, buf
,
304 CP2112_GPIO_CONFIG_LENGTH
, HID_FEATURE_REPORT
,
307 hid_err(hdev
, "error setting GPIO config: %d\n", ret
);
311 spin_unlock_irqrestore(&dev
->lock
, flags
);
314 * Set gpio value when output direction is already set,
315 * as specified in AN495, Rev. 0.2, cpt. 4.4
317 cp2112_gpio_set(chip
, offset
, value
);
322 spin_unlock_irqrestore(&dev
->lock
, flags
);
323 return ret
< 0 ? ret
: -EIO
;
326 static int cp2112_hid_get(struct hid_device
*hdev
, unsigned char report_number
,
327 u8
*data
, size_t count
, unsigned char report_type
)
332 buf
= kmalloc(count
, GFP_KERNEL
);
336 ret
= hid_hw_raw_request(hdev
, report_number
, buf
, count
,
337 report_type
, HID_REQ_GET_REPORT
);
338 memcpy(data
, buf
, count
);
343 static int cp2112_hid_output(struct hid_device
*hdev
, u8
*data
, size_t count
,
344 unsigned char report_type
)
349 buf
= kmemdup(data
, count
, GFP_KERNEL
);
353 if (report_type
== HID_OUTPUT_REPORT
)
354 ret
= hid_hw_output_report(hdev
, buf
, count
);
356 ret
= hid_hw_raw_request(hdev
, buf
[0], buf
, count
, report_type
,
363 static int cp2112_wait(struct cp2112_device
*dev
, atomic_t
*avail
)
367 /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a
368 * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to
369 * come in cp2112_raw_event or timeout. There will only be one of these
370 * in flight at any one time. The timeout is extremely large and is a
371 * last resort if the CP2112 has died. If we do timeout we don't expect
372 * to receive the response which would cause data races, it's not like
373 * we can do anything about it anyway.
375 ret
= wait_event_interruptible_timeout(dev
->wait
,
376 atomic_read(avail
), msecs_to_jiffies(RESPONSE_TIMEOUT
));
377 if (-ERESTARTSYS
== ret
)
382 atomic_set(avail
, 0);
386 static int cp2112_xfer_status(struct cp2112_device
*dev
)
388 struct hid_device
*hdev
= dev
->hdev
;
392 buf
[0] = CP2112_TRANSFER_STATUS_REQUEST
;
394 atomic_set(&dev
->xfer_avail
, 0);
396 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
398 hid_warn(hdev
, "Error requesting status: %d\n", ret
);
402 ret
= cp2112_wait(dev
, &dev
->xfer_avail
);
406 return dev
->xfer_status
;
409 static int cp2112_read(struct cp2112_device
*dev
, u8
*data
, size_t size
)
411 struct hid_device
*hdev
= dev
->hdev
;
412 struct cp2112_force_read_report report
;
415 if (size
> sizeof(dev
->read_data
))
416 size
= sizeof(dev
->read_data
);
417 report
.report
= CP2112_DATA_READ_FORCE_SEND
;
418 report
.length
= cpu_to_be16(size
);
420 atomic_set(&dev
->read_avail
, 0);
422 ret
= cp2112_hid_output(hdev
, &report
.report
, sizeof(report
),
425 hid_warn(hdev
, "Error requesting data: %d\n", ret
);
429 ret
= cp2112_wait(dev
, &dev
->read_avail
);
433 hid_dbg(hdev
, "read %d of %zd bytes requested\n",
434 dev
->read_length
, size
);
436 if (size
> dev
->read_length
)
437 size
= dev
->read_length
;
439 memcpy(data
, dev
->read_data
, size
);
440 return dev
->read_length
;
443 static int cp2112_read_req(void *buf
, u8 slave_address
, u16 length
)
445 struct cp2112_read_req_report
*report
= buf
;
447 if (length
< 1 || length
> 512)
450 report
->report
= CP2112_DATA_READ_REQUEST
;
451 report
->slave_address
= slave_address
<< 1;
452 report
->length
= cpu_to_be16(length
);
453 return sizeof(*report
);
456 static int cp2112_write_read_req(void *buf
, u8 slave_address
, u16 length
,
457 u8 command
, u8
*data
, u8 data_length
)
459 struct cp2112_write_read_req_report
*report
= buf
;
461 if (length
< 1 || length
> 512
462 || data_length
> sizeof(report
->target_address
) - 1)
465 report
->report
= CP2112_DATA_WRITE_READ_REQUEST
;
466 report
->slave_address
= slave_address
<< 1;
467 report
->length
= cpu_to_be16(length
);
468 report
->target_address_length
= data_length
+ 1;
469 report
->target_address
[0] = command
;
470 memcpy(&report
->target_address
[1], data
, data_length
);
471 return data_length
+ 6;
474 static int cp2112_write_req(void *buf
, u8 slave_address
, u8 command
, u8
*data
,
477 struct cp2112_write_req_report
*report
= buf
;
479 if (data_length
> sizeof(report
->data
) - 1)
482 report
->report
= CP2112_DATA_WRITE_REQUEST
;
483 report
->slave_address
= slave_address
<< 1;
484 report
->length
= data_length
+ 1;
485 report
->data
[0] = command
;
486 memcpy(&report
->data
[1], data
, data_length
);
487 return data_length
+ 4;
490 static int cp2112_i2c_write_req(void *buf
, u8 slave_address
, u8
*data
,
493 struct cp2112_write_req_report
*report
= buf
;
495 if (data_length
> sizeof(report
->data
))
498 report
->report
= CP2112_DATA_WRITE_REQUEST
;
499 report
->slave_address
= slave_address
<< 1;
500 report
->length
= data_length
;
501 memcpy(report
->data
, data
, data_length
);
502 return data_length
+ 3;
505 static int cp2112_i2c_write_read_req(void *buf
, u8 slave_address
,
506 u8
*addr
, int addr_length
,
509 struct cp2112_write_read_req_report
*report
= buf
;
511 if (read_length
< 1 || read_length
> 512 ||
512 addr_length
> sizeof(report
->target_address
))
515 report
->report
= CP2112_DATA_WRITE_READ_REQUEST
;
516 report
->slave_address
= slave_address
<< 1;
517 report
->length
= cpu_to_be16(read_length
);
518 report
->target_address_length
= addr_length
;
519 memcpy(report
->target_address
, addr
, addr_length
);
520 return addr_length
+ 5;
523 static int cp2112_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
526 struct cp2112_device
*dev
= (struct cp2112_device
*)adap
->algo_data
;
527 struct hid_device
*hdev
= dev
->hdev
;
530 ssize_t read_length
= 0;
532 unsigned int retries
;
535 hid_dbg(hdev
, "I2C %d messages\n", num
);
538 if (msgs
->flags
& I2C_M_RD
) {
539 hid_dbg(hdev
, "I2C read %#04x len %d\n",
540 msgs
->addr
, msgs
->len
);
541 read_length
= msgs
->len
;
542 read_buf
= msgs
->buf
;
543 count
= cp2112_read_req(buf
, msgs
->addr
, msgs
->len
);
545 hid_dbg(hdev
, "I2C write %#04x len %d\n",
546 msgs
->addr
, msgs
->len
);
547 count
= cp2112_i2c_write_req(buf
, msgs
->addr
,
548 msgs
->buf
, msgs
->len
);
552 } else if (dev
->hwversion
> 1 && /* no repeated start in rev 1 */
554 msgs
[0].addr
== msgs
[1].addr
&&
555 !(msgs
[0].flags
& I2C_M_RD
) && (msgs
[1].flags
& I2C_M_RD
)) {
556 hid_dbg(hdev
, "I2C write-read %#04x wlen %d rlen %d\n",
557 msgs
[0].addr
, msgs
[0].len
, msgs
[1].len
);
558 read_length
= msgs
[1].len
;
559 read_buf
= msgs
[1].buf
;
560 count
= cp2112_i2c_write_read_req(buf
, msgs
[0].addr
,
561 msgs
[0].buf
, msgs
[0].len
, msgs
[1].len
);
566 "Multi-message I2C transactions not supported\n");
570 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
572 hid_err(hdev
, "power management error: %d\n", ret
);
576 ret
= cp2112_hid_output(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
578 hid_warn(hdev
, "Error starting transaction: %d\n", ret
);
582 for (retries
= 0; retries
< XFER_STATUS_RETRIES
; ++retries
) {
583 ret
= cp2112_xfer_status(dev
);
591 if (XFER_STATUS_RETRIES
<= retries
) {
592 hid_warn(hdev
, "Transfer timed out, cancelling.\n");
593 buf
[0] = CP2112_CANCEL_TRANSFER
;
596 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
598 hid_warn(hdev
, "Error cancelling transaction: %d\n",
605 for (count
= 0; count
< read_length
;) {
606 ret
= cp2112_read(dev
, read_buf
+ count
, read_length
- count
);
610 hid_err(hdev
, "read returned 0\n");
615 if (count
> read_length
) {
617 * The hardware returned too much data.
618 * This is mostly harmless because cp2112_read()
619 * has a limit check so didn't overrun our
620 * buffer. Nevertheless, we return an error
621 * because something is seriously wrong and
622 * it shouldn't go unnoticed.
624 hid_err(hdev
, "long read: %d > %zd\n",
625 ret
, read_length
- count
+ ret
);
631 /* return the number of transferred messages */
635 hid_hw_power(hdev
, PM_HINT_NORMAL
);
636 hid_dbg(hdev
, "I2C transfer finished: %d\n", ret
);
640 static int cp2112_xfer(struct i2c_adapter
*adap
, u16 addr
,
641 unsigned short flags
, char read_write
, u8 command
,
642 int size
, union i2c_smbus_data
*data
)
644 struct cp2112_device
*dev
= (struct cp2112_device
*)adap
->algo_data
;
645 struct hid_device
*hdev
= dev
->hdev
;
649 size_t read_length
= 0;
650 unsigned int retries
;
653 hid_dbg(hdev
, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n",
654 read_write
== I2C_SMBUS_WRITE
? "write" : "read",
655 addr
, flags
, command
, size
);
661 if (I2C_SMBUS_READ
== read_write
)
662 count
= cp2112_read_req(buf
, addr
, read_length
);
664 count
= cp2112_write_req(buf
, addr
, command
, NULL
,
667 case I2C_SMBUS_BYTE_DATA
:
670 if (I2C_SMBUS_READ
== read_write
)
671 count
= cp2112_write_read_req(buf
, addr
, read_length
,
674 count
= cp2112_write_req(buf
, addr
, command
,
677 case I2C_SMBUS_WORD_DATA
:
679 word
= cpu_to_le16(data
->word
);
681 if (I2C_SMBUS_READ
== read_write
)
682 count
= cp2112_write_read_req(buf
, addr
, read_length
,
685 count
= cp2112_write_req(buf
, addr
, command
,
688 case I2C_SMBUS_PROC_CALL
:
689 size
= I2C_SMBUS_WORD_DATA
;
690 read_write
= I2C_SMBUS_READ
;
692 word
= cpu_to_le16(data
->word
);
694 count
= cp2112_write_read_req(buf
, addr
, read_length
, command
,
697 case I2C_SMBUS_I2C_BLOCK_DATA
:
698 size
= I2C_SMBUS_BLOCK_DATA
;
700 case I2C_SMBUS_BLOCK_DATA
:
701 if (I2C_SMBUS_READ
== read_write
) {
702 count
= cp2112_write_read_req(buf
, addr
,
706 count
= cp2112_write_req(buf
, addr
, command
,
711 case I2C_SMBUS_BLOCK_PROC_CALL
:
712 size
= I2C_SMBUS_BLOCK_DATA
;
713 read_write
= I2C_SMBUS_READ
;
715 count
= cp2112_write_read_req(buf
, addr
, I2C_SMBUS_BLOCK_MAX
,
716 command
, data
->block
,
720 hid_warn(hdev
, "Unsupported transaction %d\n", size
);
727 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
729 hid_err(hdev
, "power management error: %d\n", ret
);
733 ret
= cp2112_hid_output(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
735 hid_warn(hdev
, "Error starting transaction: %d\n", ret
);
739 for (retries
= 0; retries
< XFER_STATUS_RETRIES
; ++retries
) {
740 ret
= cp2112_xfer_status(dev
);
748 if (XFER_STATUS_RETRIES
<= retries
) {
749 hid_warn(hdev
, "Transfer timed out, cancelling.\n");
750 buf
[0] = CP2112_CANCEL_TRANSFER
;
753 ret
= cp2112_hid_output(hdev
, buf
, 2, HID_OUTPUT_REPORT
);
755 hid_warn(hdev
, "Error cancelling transaction: %d\n",
762 if (I2C_SMBUS_WRITE
== read_write
) {
767 if (I2C_SMBUS_BLOCK_DATA
== size
)
770 ret
= cp2112_read(dev
, buf
, read_length
);
773 if (ret
!= read_length
) {
774 hid_warn(hdev
, "short read: %d < %zd\n", ret
, read_length
);
781 case I2C_SMBUS_BYTE_DATA
:
784 case I2C_SMBUS_WORD_DATA
:
785 data
->word
= le16_to_cpup((__le16
*)buf
);
787 case I2C_SMBUS_BLOCK_DATA
:
788 if (read_length
> I2C_SMBUS_BLOCK_MAX
) {
793 memcpy(data
->block
, buf
, read_length
);
799 hid_hw_power(hdev
, PM_HINT_NORMAL
);
800 hid_dbg(hdev
, "transfer finished: %d\n", ret
);
804 static u32
cp2112_functionality(struct i2c_adapter
*adap
)
806 return I2C_FUNC_I2C
|
807 I2C_FUNC_SMBUS_BYTE
|
808 I2C_FUNC_SMBUS_BYTE_DATA
|
809 I2C_FUNC_SMBUS_WORD_DATA
|
810 I2C_FUNC_SMBUS_BLOCK_DATA
|
811 I2C_FUNC_SMBUS_I2C_BLOCK
|
812 I2C_FUNC_SMBUS_PROC_CALL
|
813 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
;
816 static const struct i2c_algorithm smbus_algorithm
= {
817 .master_xfer
= cp2112_i2c_xfer
,
818 .smbus_xfer
= cp2112_xfer
,
819 .functionality
= cp2112_functionality
,
822 static int cp2112_get_usb_config(struct hid_device
*hdev
,
823 struct cp2112_usb_config_report
*cfg
)
827 ret
= cp2112_hid_get(hdev
, CP2112_USB_CONFIG
, (u8
*)cfg
, sizeof(*cfg
),
829 if (ret
!= sizeof(*cfg
)) {
830 hid_err(hdev
, "error reading usb config: %d\n", ret
);
839 static int cp2112_set_usb_config(struct hid_device
*hdev
,
840 struct cp2112_usb_config_report
*cfg
)
844 BUG_ON(cfg
->report
!= CP2112_USB_CONFIG
);
846 ret
= cp2112_hid_output(hdev
, (u8
*)cfg
, sizeof(*cfg
),
848 if (ret
!= sizeof(*cfg
)) {
849 hid_err(hdev
, "error writing usb config: %d\n", ret
);
858 static void chmod_sysfs_attrs(struct hid_device
*hdev
);
860 #define CP2112_CONFIG_ATTR(name, store, format, ...) \
861 static ssize_t name##_store(struct device *kdev, \
862 struct device_attribute *attr, const char *buf, \
865 struct hid_device *hdev = to_hid_device(kdev); \
866 struct cp2112_usb_config_report cfg; \
867 int ret = cp2112_get_usb_config(hdev, &cfg); \
871 ret = cp2112_set_usb_config(hdev, &cfg); \
874 chmod_sysfs_attrs(hdev); \
877 static ssize_t name##_show(struct device *kdev, \
878 struct device_attribute *attr, char *buf) \
880 struct hid_device *hdev = to_hid_device(kdev); \
881 struct cp2112_usb_config_report cfg; \
882 int ret = cp2112_get_usb_config(hdev, &cfg); \
885 return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \
887 static DEVICE_ATTR_RW(name);
889 CP2112_CONFIG_ATTR(vendor_id
, ({
892 if (sscanf(buf
, "%hi", &vid
) != 1)
895 cfg
.vid
= cpu_to_le16(vid
);
897 }), "0x%04x\n", le16_to_cpu(cfg
.vid
));
899 CP2112_CONFIG_ATTR(product_id
, ({
902 if (sscanf(buf
, "%hi", &pid
) != 1)
905 cfg
.pid
= cpu_to_le16(pid
);
907 }), "0x%04x\n", le16_to_cpu(cfg
.pid
));
909 CP2112_CONFIG_ATTR(max_power
, ({
912 if (sscanf(buf
, "%i", &mA
) != 1)
915 cfg
.max_power
= (mA
+ 1) / 2;
917 }), "%u mA\n", cfg
.max_power
* 2);
919 CP2112_CONFIG_ATTR(power_mode
, ({
920 if (sscanf(buf
, "%hhi", &cfg
.power_mode
) != 1)
924 }), "%u\n", cfg
.power_mode
);
926 CP2112_CONFIG_ATTR(release_version
, ({
927 if (sscanf(buf
, "%hhi.%hhi", &cfg
.release_major
, &cfg
.release_minor
)
932 }), "%u.%u\n", cfg
.release_major
, cfg
.release_minor
);
934 #undef CP2112_CONFIG_ATTR
936 struct cp2112_pstring_attribute
{
937 struct device_attribute attr
;
938 unsigned char report
;
941 static ssize_t
pstr_store(struct device
*kdev
,
942 struct device_attribute
*kattr
, const char *buf
,
945 struct hid_device
*hdev
= to_hid_device(kdev
);
946 struct cp2112_pstring_attribute
*attr
=
947 container_of(kattr
, struct cp2112_pstring_attribute
, attr
);
948 struct cp2112_string_report report
;
951 memset(&report
, 0, sizeof(report
));
953 ret
= utf8s_to_utf16s(buf
, count
, UTF16_LITTLE_ENDIAN
,
954 report
.string
, ARRAY_SIZE(report
.string
));
955 report
.report
= attr
->report
;
956 report
.length
= ret
* sizeof(report
.string
[0]) + 2;
957 report
.type
= USB_DT_STRING
;
959 ret
= cp2112_hid_output(hdev
, &report
.report
, report
.length
+ 1,
961 if (ret
!= report
.length
+ 1) {
962 hid_err(hdev
, "error writing %s string: %d\n", kattr
->attr
.name
,
969 chmod_sysfs_attrs(hdev
);
973 static ssize_t
pstr_show(struct device
*kdev
,
974 struct device_attribute
*kattr
, char *buf
)
976 struct hid_device
*hdev
= to_hid_device(kdev
);
977 struct cp2112_pstring_attribute
*attr
=
978 container_of(kattr
, struct cp2112_pstring_attribute
, attr
);
979 struct cp2112_string_report report
;
983 ret
= cp2112_hid_get(hdev
, attr
->report
, &report
.report
,
984 sizeof(report
) - 1, HID_FEATURE_REPORT
);
986 hid_err(hdev
, "error reading %s string: %d\n", kattr
->attr
.name
,
993 if (report
.length
< 2) {
994 hid_err(hdev
, "invalid %s string length: %d\n",
995 kattr
->attr
.name
, report
.length
);
999 length
= report
.length
> ret
- 1 ? ret
- 1 : report
.length
;
1000 length
= (length
- 2) / sizeof(report
.string
[0]);
1001 ret
= utf16s_to_utf8s(report
.string
, length
, UTF16_LITTLE_ENDIAN
, buf
,
1007 #define CP2112_PSTR_ATTR(name, _report) \
1008 static struct cp2112_pstring_attribute dev_attr_##name = { \
1009 .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \
1010 .report = _report, \
1013 CP2112_PSTR_ATTR(manufacturer
, CP2112_MANUFACTURER_STRING
);
1014 CP2112_PSTR_ATTR(product
, CP2112_PRODUCT_STRING
);
1015 CP2112_PSTR_ATTR(serial
, CP2112_SERIAL_STRING
);
1017 #undef CP2112_PSTR_ATTR
1019 static const struct attribute_group cp2112_attr_group
= {
1020 .attrs
= (struct attribute
*[]){
1021 &dev_attr_vendor_id
.attr
,
1022 &dev_attr_product_id
.attr
,
1023 &dev_attr_max_power
.attr
,
1024 &dev_attr_power_mode
.attr
,
1025 &dev_attr_release_version
.attr
,
1026 &dev_attr_manufacturer
.attr
.attr
,
1027 &dev_attr_product
.attr
.attr
,
1028 &dev_attr_serial
.attr
.attr
,
1033 /* Chmoding our sysfs attributes is simply a way to expose which fields in the
1034 * PROM have already been programmed. We do not depend on this preventing
1035 * writing to these attributes since the CP2112 will simply ignore writes to
1036 * already-programmed fields. This is why there is no sense in fixing this
1039 static void chmod_sysfs_attrs(struct hid_device
*hdev
)
1041 struct attribute
**attr
;
1045 ret
= cp2112_hid_get(hdev
, CP2112_LOCK_BYTE
, buf
, sizeof(buf
),
1046 HID_FEATURE_REPORT
);
1047 if (ret
!= sizeof(buf
)) {
1048 hid_err(hdev
, "error reading lock byte: %d\n", ret
);
1052 for (attr
= cp2112_attr_group
.attrs
; *attr
; ++attr
) {
1053 umode_t mode
= (buf
[1] & 1) ? S_IWUSR
| S_IRUGO
: S_IRUGO
;
1054 ret
= sysfs_chmod_file(&hdev
->dev
.kobj
, *attr
, mode
);
1056 hid_err(hdev
, "error chmoding sysfs file %s\n",
1062 static void cp2112_gpio_irq_ack(struct irq_data
*d
)
1066 static void cp2112_gpio_irq_mask(struct irq_data
*d
)
1068 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1069 struct cp2112_device
*dev
= gpiochip_get_data(gc
);
1071 __clear_bit(d
->hwirq
, &dev
->irq_mask
);
1074 static void cp2112_gpio_irq_unmask(struct irq_data
*d
)
1076 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1077 struct cp2112_device
*dev
= gpiochip_get_data(gc
);
1079 __set_bit(d
->hwirq
, &dev
->irq_mask
);
1082 static void cp2112_gpio_poll_callback(struct work_struct
*work
)
1084 struct cp2112_device
*dev
= container_of(work
, struct cp2112_device
,
1085 gpio_poll_worker
.work
);
1088 u8 virqs
= (u8
)dev
->irq_mask
;
1092 ret
= cp2112_gpio_get_all(&dev
->gc
);
1093 if (ret
== -ENODEV
) /* the hardware has been disconnected */
1101 virq
= ffs(virqs
) - 1;
1102 virqs
&= ~BIT(virq
);
1104 if (!dev
->gc
.to_irq
)
1107 irq
= dev
->gc
.to_irq(&dev
->gc
, virq
);
1109 d
= irq_get_irq_data(irq
);
1113 irq_type
= irqd_get_trigger_type(d
);
1115 if (gpio_mask
& BIT(virq
)) {
1118 if (irq_type
& IRQ_TYPE_LEVEL_HIGH
)
1119 handle_nested_irq(irq
);
1121 if ((irq_type
& IRQ_TYPE_EDGE_RISING
) &&
1122 !(dev
->gpio_prev_state
& BIT(virq
)))
1123 handle_nested_irq(irq
);
1127 if (irq_type
& IRQ_TYPE_LEVEL_LOW
)
1128 handle_nested_irq(irq
);
1130 if ((irq_type
& IRQ_TYPE_EDGE_FALLING
) &&
1131 (dev
->gpio_prev_state
& BIT(virq
)))
1132 handle_nested_irq(irq
);
1136 dev
->gpio_prev_state
= gpio_mask
;
1140 schedule_delayed_work(&dev
->gpio_poll_worker
, 10);
1144 static unsigned int cp2112_gpio_irq_startup(struct irq_data
*d
)
1146 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1147 struct cp2112_device
*dev
= gpiochip_get_data(gc
);
1149 INIT_DELAYED_WORK(&dev
->gpio_poll_worker
, cp2112_gpio_poll_callback
);
1151 cp2112_gpio_direction_input(gc
, d
->hwirq
);
1153 if (!dev
->gpio_poll
) {
1154 dev
->gpio_poll
= true;
1155 schedule_delayed_work(&dev
->gpio_poll_worker
, 0);
1158 cp2112_gpio_irq_unmask(d
);
1162 static void cp2112_gpio_irq_shutdown(struct irq_data
*d
)
1164 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1165 struct cp2112_device
*dev
= gpiochip_get_data(gc
);
1167 cancel_delayed_work_sync(&dev
->gpio_poll_worker
);
1170 static int cp2112_gpio_irq_type(struct irq_data
*d
, unsigned int type
)
1175 static struct irq_chip cp2112_gpio_irqchip
= {
1176 .name
= "cp2112-gpio",
1177 .irq_startup
= cp2112_gpio_irq_startup
,
1178 .irq_shutdown
= cp2112_gpio_irq_shutdown
,
1179 .irq_ack
= cp2112_gpio_irq_ack
,
1180 .irq_mask
= cp2112_gpio_irq_mask
,
1181 .irq_unmask
= cp2112_gpio_irq_unmask
,
1182 .irq_set_type
= cp2112_gpio_irq_type
,
1185 static int __maybe_unused
cp2112_allocate_irq(struct cp2112_device
*dev
,
1193 dev
->desc
[pin
] = gpiochip_request_own_desc(&dev
->gc
, pin
,
1195 if (IS_ERR(dev
->desc
[pin
])) {
1196 dev_err(dev
->gc
.parent
, "Failed to request GPIO\n");
1197 return PTR_ERR(dev
->desc
[pin
]);
1200 ret
= gpiochip_lock_as_irq(&dev
->gc
, pin
);
1202 dev_err(dev
->gc
.parent
, "Failed to lock GPIO as interrupt\n");
1206 ret
= gpiod_to_irq(dev
->desc
[pin
]);
1208 dev_err(dev
->gc
.parent
, "Failed to translate GPIO to IRQ\n");
1215 gpiochip_unlock_as_irq(&dev
->gc
, pin
);
1217 gpiochip_free_own_desc(dev
->desc
[pin
]);
1218 dev
->desc
[pin
] = NULL
;
1222 static int cp2112_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
1224 struct cp2112_device
*dev
;
1226 struct cp2112_smbus_config_report config
;
1229 dev
= devm_kzalloc(&hdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
1233 dev
->in_out_buffer
= devm_kzalloc(&hdev
->dev
, CP2112_REPORT_MAX_LENGTH
,
1235 if (!dev
->in_out_buffer
)
1238 spin_lock_init(&dev
->lock
);
1240 ret
= hid_parse(hdev
);
1242 hid_err(hdev
, "parse failed\n");
1246 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
1248 hid_err(hdev
, "hw start failed\n");
1252 ret
= hid_hw_open(hdev
);
1254 hid_err(hdev
, "hw open failed\n");
1258 ret
= hid_hw_power(hdev
, PM_HINT_FULLON
);
1260 hid_err(hdev
, "power management error: %d\n", ret
);
1264 ret
= cp2112_hid_get(hdev
, CP2112_GET_VERSION_INFO
, buf
, sizeof(buf
),
1265 HID_FEATURE_REPORT
);
1266 if (ret
!= sizeof(buf
)) {
1267 hid_err(hdev
, "error requesting version\n");
1270 goto err_power_normal
;
1273 hid_info(hdev
, "Part Number: 0x%02X Device Version: 0x%02X\n",
1276 ret
= cp2112_hid_get(hdev
, CP2112_SMBUS_CONFIG
, (u8
*)&config
,
1277 sizeof(config
), HID_FEATURE_REPORT
);
1278 if (ret
!= sizeof(config
)) {
1279 hid_err(hdev
, "error requesting SMBus config\n");
1282 goto err_power_normal
;
1285 config
.retry_time
= cpu_to_be16(1);
1287 ret
= cp2112_hid_output(hdev
, (u8
*)&config
, sizeof(config
),
1288 HID_FEATURE_REPORT
);
1289 if (ret
!= sizeof(config
)) {
1290 hid_err(hdev
, "error setting SMBus config\n");
1293 goto err_power_normal
;
1296 hid_set_drvdata(hdev
, (void *)dev
);
1298 dev
->adap
.owner
= THIS_MODULE
;
1299 dev
->adap
.class = I2C_CLASS_HWMON
;
1300 dev
->adap
.algo
= &smbus_algorithm
;
1301 dev
->adap
.algo_data
= dev
;
1302 dev
->adap
.dev
.parent
= &hdev
->dev
;
1303 snprintf(dev
->adap
.name
, sizeof(dev
->adap
.name
),
1304 "CP2112 SMBus Bridge on hiddev%d", hdev
->minor
);
1305 dev
->hwversion
= buf
[2];
1306 init_waitqueue_head(&dev
->wait
);
1308 hid_device_io_start(hdev
);
1309 ret
= i2c_add_adapter(&dev
->adap
);
1310 hid_device_io_stop(hdev
);
1313 hid_err(hdev
, "error registering i2c adapter\n");
1314 goto err_power_normal
;
1317 hid_dbg(hdev
, "adapter registered\n");
1319 dev
->gc
.label
= "cp2112_gpio";
1320 dev
->gc
.direction_input
= cp2112_gpio_direction_input
;
1321 dev
->gc
.direction_output
= cp2112_gpio_direction_output
;
1322 dev
->gc
.set
= cp2112_gpio_set
;
1323 dev
->gc
.get
= cp2112_gpio_get
;
1326 dev
->gc
.can_sleep
= 1;
1327 dev
->gc
.parent
= &hdev
->dev
;
1329 ret
= gpiochip_add_data(&dev
->gc
, dev
);
1331 hid_err(hdev
, "error registering gpio chip\n");
1335 ret
= sysfs_create_group(&hdev
->dev
.kobj
, &cp2112_attr_group
);
1337 hid_err(hdev
, "error creating sysfs attrs\n");
1338 goto err_gpiochip_remove
;
1341 chmod_sysfs_attrs(hdev
);
1342 hid_hw_power(hdev
, PM_HINT_NORMAL
);
1344 ret
= gpiochip_irqchip_add(&dev
->gc
, &cp2112_gpio_irqchip
, 0,
1345 handle_simple_irq
, IRQ_TYPE_NONE
);
1347 dev_err(dev
->gc
.parent
, "failed to add IRQ chip\n");
1348 goto err_sysfs_remove
;
1354 sysfs_remove_group(&hdev
->dev
.kobj
, &cp2112_attr_group
);
1355 err_gpiochip_remove
:
1356 gpiochip_remove(&dev
->gc
);
1358 i2c_del_adapter(&dev
->adap
);
1360 hid_hw_power(hdev
, PM_HINT_NORMAL
);
1368 static void cp2112_remove(struct hid_device
*hdev
)
1370 struct cp2112_device
*dev
= hid_get_drvdata(hdev
);
1373 sysfs_remove_group(&hdev
->dev
.kobj
, &cp2112_attr_group
);
1374 i2c_del_adapter(&dev
->adap
);
1376 if (dev
->gpio_poll
) {
1377 dev
->gpio_poll
= false;
1378 cancel_delayed_work_sync(&dev
->gpio_poll_worker
);
1381 for (i
= 0; i
< ARRAY_SIZE(dev
->desc
); i
++) {
1382 gpiochip_unlock_as_irq(&dev
->gc
, i
);
1383 gpiochip_free_own_desc(dev
->desc
[i
]);
1386 gpiochip_remove(&dev
->gc
);
1387 /* i2c_del_adapter has finished removing all i2c devices from our
1388 * adapter. Well behaved devices should no longer call our cp2112_xfer
1389 * and should have waited for any pending calls to finish. It has also
1390 * waited for device_unregister(&adap->dev) to complete. Therefore we
1391 * can safely free our struct cp2112_device.
1397 static int cp2112_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
1400 struct cp2112_device
*dev
= hid_get_drvdata(hdev
);
1401 struct cp2112_xfer_status_report
*xfer
= (void *)data
;
1404 case CP2112_TRANSFER_STATUS_RESPONSE
:
1405 hid_dbg(hdev
, "xfer status: %02x %02x %04x %04x\n",
1406 xfer
->status0
, xfer
->status1
,
1407 be16_to_cpu(xfer
->retries
), be16_to_cpu(xfer
->length
));
1409 switch (xfer
->status0
) {
1411 dev
->xfer_status
= -EAGAIN
;
1414 dev
->xfer_status
= -EBUSY
;
1416 case STATUS0_COMPLETE
:
1417 dev
->xfer_status
= be16_to_cpu(xfer
->length
);
1420 switch (xfer
->status1
) {
1421 case STATUS1_TIMEOUT_NACK
:
1422 case STATUS1_TIMEOUT_BUS
:
1423 dev
->xfer_status
= -ETIMEDOUT
;
1426 dev
->xfer_status
= -EIO
;
1431 dev
->xfer_status
= -EINVAL
;
1435 atomic_set(&dev
->xfer_avail
, 1);
1437 case CP2112_DATA_READ_RESPONSE
:
1438 hid_dbg(hdev
, "read response: %02x %02x\n", data
[1], data
[2]);
1440 dev
->read_length
= data
[2];
1441 if (dev
->read_length
> sizeof(dev
->read_data
))
1442 dev
->read_length
= sizeof(dev
->read_data
);
1444 memcpy(dev
->read_data
, &data
[3], dev
->read_length
);
1445 atomic_set(&dev
->read_avail
, 1);
1448 hid_err(hdev
, "unknown report\n");
1453 wake_up_interruptible(&dev
->wait
);
1457 static struct hid_driver cp2112_driver
= {
1459 .id_table
= cp2112_devices
,
1460 .probe
= cp2112_probe
,
1461 .remove
= cp2112_remove
,
1462 .raw_event
= cp2112_raw_event
,
1465 module_hid_driver(cp2112_driver
);
1466 MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge");
1467 MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
1468 MODULE_LICENSE("GPL");