net: qmi_wwan: add Olivetti Olicard 500
[linux/fpc-iii.git] / drivers / hid / hid-cp2112.c
blob56be85a9a77c358d84a3aad0007f127d4992fe6d
1 /*
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
13 * more details.
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.
21 * Data Sheet:
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>
33 #include "hid-ids.h"
35 enum {
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,
56 enum {
57 STATUS0_IDLE = 0x00,
58 STATUS0_BUSY = 0x01,
59 STATUS0_COMPLETE = 0x02,
60 STATUS0_ERROR = 0x03,
63 enum {
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 */
81 } __packed;
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 */
91 u8 release_major;
92 u8 release_minor;
93 u8 mask; /* What fields to program */
94 } __packed;
96 struct cp2112_read_req_report {
97 u8 report; /* CP2112_DATA_READ_REQUEST */
98 u8 slave_address;
99 __be16 length;
100 } __packed;
102 struct cp2112_write_read_req_report {
103 u8 report; /* CP2112_DATA_WRITE_READ_REQUEST */
104 u8 slave_address;
105 __be16 length;
106 u8 target_address_length;
107 u8 target_address[16];
108 } __packed;
110 struct cp2112_write_req_report {
111 u8 report; /* CP2112_DATA_WRITE_REQUEST */
112 u8 slave_address;
113 u8 length;
114 u8 data[61];
115 } __packed;
117 struct cp2112_force_read_report {
118 u8 report; /* CP2112_DATA_READ_FORCE_SEND */
119 __be16 length;
120 } __packed;
122 struct cp2112_xfer_status_report {
123 u8 report; /* CP2112_TRANSFER_STATUS_RESPONSE */
124 u8 status0; /* STATUS0_* */
125 u8 status1; /* STATUS1_* */
126 __be16 retries;
127 __be16 length;
128 } __packed;
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 */
136 } __packed;
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;
157 u8 read_data[61];
158 u8 read_length;
159 int xfer_status;
160 atomic_t read_avail;
161 atomic_t xfer_avail;
162 struct gpio_chip gc;
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,
172 gc);
173 struct hid_device *hdev = dev->hdev;
174 u8 buf[5];
175 int ret;
177 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
178 sizeof(buf), HID_FEATURE_REPORT,
179 HID_REQ_GET_REPORT);
180 if (ret != sizeof(buf)) {
181 hid_err(hdev, "error requesting GPIO config: %d\n", ret);
182 return ret;
185 buf[1] &= ~(1 << offset);
186 buf[2] = gpio_push_pull;
188 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, sizeof(buf),
189 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
190 if (ret < 0) {
191 hid_err(hdev, "error setting GPIO config: %d\n", ret);
192 return ret;
195 return 0;
198 static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
200 struct cp2112_device *dev = container_of(chip, struct cp2112_device,
201 gc);
202 struct hid_device *hdev = dev->hdev;
203 u8 buf[3];
204 int ret;
206 buf[0] = CP2112_GPIO_SET;
207 buf[1] = value ? 0xff : 0;
208 buf[2] = 1 << offset;
210 ret = hid_hw_raw_request(hdev, CP2112_GPIO_SET, buf, sizeof(buf),
211 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
212 if (ret < 0)
213 hid_err(hdev, "error setting GPIO values: %d\n", ret);
216 static int cp2112_gpio_get(struct gpio_chip *chip, unsigned offset)
218 struct cp2112_device *dev = container_of(chip, struct cp2112_device,
219 gc);
220 struct hid_device *hdev = dev->hdev;
221 u8 buf[2];
222 int ret;
224 ret = hid_hw_raw_request(hdev, CP2112_GPIO_GET, buf, sizeof(buf),
225 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
226 if (ret != sizeof(buf)) {
227 hid_err(hdev, "error requesting GPIO values: %d\n", ret);
228 return ret;
231 return (buf[1] >> offset) & 1;
234 static int cp2112_gpio_direction_output(struct gpio_chip *chip,
235 unsigned offset, int value)
237 struct cp2112_device *dev = container_of(chip, struct cp2112_device,
238 gc);
239 struct hid_device *hdev = dev->hdev;
240 u8 buf[5];
241 int ret;
243 cp2112_gpio_set(chip, offset, value);
245 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
246 sizeof(buf), HID_FEATURE_REPORT,
247 HID_REQ_GET_REPORT);
248 if (ret != sizeof(buf)) {
249 hid_err(hdev, "error requesting GPIO config: %d\n", ret);
250 return ret;
253 buf[1] |= 1 << offset;
254 buf[2] = gpio_push_pull;
256 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, sizeof(buf),
257 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
258 if (ret < 0) {
259 hid_err(hdev, "error setting GPIO config: %d\n", ret);
260 return ret;
263 return 0;
266 static int cp2112_hid_get(struct hid_device *hdev, unsigned char report_number,
267 u8 *data, size_t count, unsigned char report_type)
269 u8 *buf;
270 int ret;
272 buf = kmalloc(count, GFP_KERNEL);
273 if (!buf)
274 return -ENOMEM;
276 ret = hid_hw_raw_request(hdev, report_number, buf, count,
277 report_type, HID_REQ_GET_REPORT);
278 memcpy(data, buf, count);
279 kfree(buf);
280 return ret;
283 static int cp2112_hid_output(struct hid_device *hdev, u8 *data, size_t count,
284 unsigned char report_type)
286 u8 *buf;
287 int ret;
289 buf = kmemdup(data, count, GFP_KERNEL);
290 if (!buf)
291 return -ENOMEM;
293 if (report_type == HID_OUTPUT_REPORT)
294 ret = hid_hw_output_report(hdev, buf, count);
295 else
296 ret = hid_hw_raw_request(hdev, buf[0], buf, count, report_type,
297 HID_REQ_SET_REPORT);
299 kfree(buf);
300 return ret;
303 static int cp2112_wait(struct cp2112_device *dev, atomic_t *avail)
305 int ret = 0;
307 /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a
308 * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to
309 * come in cp2112_raw_event or timeout. There will only be one of these
310 * in flight at any one time. The timeout is extremely large and is a
311 * last resort if the CP2112 has died. If we do timeout we don't expect
312 * to receive the response which would cause data races, it's not like
313 * we can do anything about it anyway.
315 ret = wait_event_interruptible_timeout(dev->wait,
316 atomic_read(avail), msecs_to_jiffies(RESPONSE_TIMEOUT));
317 if (-ERESTARTSYS == ret)
318 return ret;
319 if (!ret)
320 return -ETIMEDOUT;
322 atomic_set(avail, 0);
323 return 0;
326 static int cp2112_xfer_status(struct cp2112_device *dev)
328 struct hid_device *hdev = dev->hdev;
329 u8 buf[2];
330 int ret;
332 buf[0] = CP2112_TRANSFER_STATUS_REQUEST;
333 buf[1] = 0x01;
334 atomic_set(&dev->xfer_avail, 0);
336 ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
337 if (ret < 0) {
338 hid_warn(hdev, "Error requesting status: %d\n", ret);
339 return ret;
342 ret = cp2112_wait(dev, &dev->xfer_avail);
343 if (ret)
344 return ret;
346 return dev->xfer_status;
349 static int cp2112_read(struct cp2112_device *dev, u8 *data, size_t size)
351 struct hid_device *hdev = dev->hdev;
352 struct cp2112_force_read_report report;
353 int ret;
355 report.report = CP2112_DATA_READ_FORCE_SEND;
356 report.length = cpu_to_be16(size);
358 atomic_set(&dev->read_avail, 0);
360 ret = cp2112_hid_output(hdev, &report.report, sizeof(report),
361 HID_OUTPUT_REPORT);
362 if (ret < 0) {
363 hid_warn(hdev, "Error requesting data: %d\n", ret);
364 return ret;
367 ret = cp2112_wait(dev, &dev->read_avail);
368 if (ret)
369 return ret;
371 hid_dbg(hdev, "read %d of %zd bytes requested\n",
372 dev->read_length, size);
374 if (size > dev->read_length)
375 size = dev->read_length;
377 memcpy(data, dev->read_data, size);
378 return dev->read_length;
381 static int cp2112_read_req(void *buf, u8 slave_address, u16 length)
383 struct cp2112_read_req_report *report = buf;
385 if (length < 1 || length > 512)
386 return -EINVAL;
388 report->report = CP2112_DATA_READ_REQUEST;
389 report->slave_address = slave_address << 1;
390 report->length = cpu_to_be16(length);
391 return sizeof(*report);
394 static int cp2112_write_read_req(void *buf, u8 slave_address, u16 length,
395 u8 command, u8 *data, u8 data_length)
397 struct cp2112_write_read_req_report *report = buf;
399 if (length < 1 || length > 512
400 || data_length > sizeof(report->target_address) - 1)
401 return -EINVAL;
403 report->report = CP2112_DATA_WRITE_READ_REQUEST;
404 report->slave_address = slave_address << 1;
405 report->length = cpu_to_be16(length);
406 report->target_address_length = data_length + 1;
407 report->target_address[0] = command;
408 memcpy(&report->target_address[1], data, data_length);
409 return data_length + 6;
412 static int cp2112_write_req(void *buf, u8 slave_address, u8 command, u8 *data,
413 u8 data_length)
415 struct cp2112_write_req_report *report = buf;
417 if (data_length > sizeof(report->data) - 1)
418 return -EINVAL;
420 report->report = CP2112_DATA_WRITE_REQUEST;
421 report->slave_address = slave_address << 1;
422 report->length = data_length + 1;
423 report->data[0] = command;
424 memcpy(&report->data[1], data, data_length);
425 return data_length + 4;
428 static int cp2112_xfer(struct i2c_adapter *adap, u16 addr,
429 unsigned short flags, char read_write, u8 command,
430 int size, union i2c_smbus_data *data)
432 struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data;
433 struct hid_device *hdev = dev->hdev;
434 u8 buf[64];
435 __be16 word;
436 ssize_t count;
437 size_t read_length = 0;
438 unsigned int retries;
439 int ret;
441 hid_dbg(hdev, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n",
442 read_write == I2C_SMBUS_WRITE ? "write" : "read",
443 addr, flags, command, size);
445 switch (size) {
446 case I2C_SMBUS_BYTE:
447 read_length = 1;
449 if (I2C_SMBUS_READ == read_write)
450 count = cp2112_read_req(buf, addr, read_length);
451 else
452 count = cp2112_write_req(buf, addr, data->byte, NULL,
454 break;
455 case I2C_SMBUS_BYTE_DATA:
456 read_length = 1;
458 if (I2C_SMBUS_READ == read_write)
459 count = cp2112_write_read_req(buf, addr, read_length,
460 command, NULL, 0);
461 else
462 count = cp2112_write_req(buf, addr, command,
463 &data->byte, 1);
464 break;
465 case I2C_SMBUS_WORD_DATA:
466 read_length = 2;
467 word = cpu_to_be16(data->word);
469 if (I2C_SMBUS_READ == read_write)
470 count = cp2112_write_read_req(buf, addr, read_length,
471 command, NULL, 0);
472 else
473 count = cp2112_write_req(buf, addr, command,
474 (u8 *)&word, 2);
475 break;
476 case I2C_SMBUS_PROC_CALL:
477 size = I2C_SMBUS_WORD_DATA;
478 read_write = I2C_SMBUS_READ;
479 read_length = 2;
480 word = cpu_to_be16(data->word);
482 count = cp2112_write_read_req(buf, addr, read_length, command,
483 (u8 *)&word, 2);
484 break;
485 case I2C_SMBUS_I2C_BLOCK_DATA:
486 size = I2C_SMBUS_BLOCK_DATA;
487 /* fallthrough */
488 case I2C_SMBUS_BLOCK_DATA:
489 if (I2C_SMBUS_READ == read_write) {
490 count = cp2112_write_read_req(buf, addr,
491 I2C_SMBUS_BLOCK_MAX,
492 command, NULL, 0);
493 } else {
494 count = cp2112_write_req(buf, addr, command,
495 data->block,
496 data->block[0] + 1);
498 break;
499 case I2C_SMBUS_BLOCK_PROC_CALL:
500 size = I2C_SMBUS_BLOCK_DATA;
501 read_write = I2C_SMBUS_READ;
503 count = cp2112_write_read_req(buf, addr, I2C_SMBUS_BLOCK_MAX,
504 command, data->block,
505 data->block[0] + 1);
506 break;
507 default:
508 hid_warn(hdev, "Unsupported transaction %d\n", size);
509 return -EOPNOTSUPP;
512 if (count < 0)
513 return count;
515 ret = hid_hw_power(hdev, PM_HINT_FULLON);
516 if (ret < 0) {
517 hid_err(hdev, "power management error: %d\n", ret);
518 return ret;
521 ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT);
522 if (ret < 0) {
523 hid_warn(hdev, "Error starting transaction: %d\n", ret);
524 goto power_normal;
527 for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) {
528 ret = cp2112_xfer_status(dev);
529 if (-EBUSY == ret)
530 continue;
531 if (ret < 0)
532 goto power_normal;
533 break;
536 if (XFER_STATUS_RETRIES <= retries) {
537 hid_warn(hdev, "Transfer timed out, cancelling.\n");
538 buf[0] = CP2112_CANCEL_TRANSFER;
539 buf[1] = 0x01;
541 ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
542 if (ret < 0)
543 hid_warn(hdev, "Error cancelling transaction: %d\n",
544 ret);
546 ret = -ETIMEDOUT;
547 goto power_normal;
550 if (I2C_SMBUS_WRITE == read_write) {
551 ret = 0;
552 goto power_normal;
555 if (I2C_SMBUS_BLOCK_DATA == size)
556 read_length = ret;
558 ret = cp2112_read(dev, buf, read_length);
559 if (ret < 0)
560 goto power_normal;
561 if (ret != read_length) {
562 hid_warn(hdev, "short read: %d < %zd\n", ret, read_length);
563 ret = -EIO;
564 goto power_normal;
567 switch (size) {
568 case I2C_SMBUS_BYTE:
569 case I2C_SMBUS_BYTE_DATA:
570 data->byte = buf[0];
571 break;
572 case I2C_SMBUS_WORD_DATA:
573 data->word = be16_to_cpup((__be16 *)buf);
574 break;
575 case I2C_SMBUS_BLOCK_DATA:
576 if (read_length > I2C_SMBUS_BLOCK_MAX) {
577 ret = -EPROTO;
578 goto power_normal;
581 memcpy(data->block, buf, read_length);
582 break;
585 ret = 0;
586 power_normal:
587 hid_hw_power(hdev, PM_HINT_NORMAL);
588 hid_dbg(hdev, "transfer finished: %d\n", ret);
589 return ret;
592 static u32 cp2112_functionality(struct i2c_adapter *adap)
594 return I2C_FUNC_SMBUS_BYTE |
595 I2C_FUNC_SMBUS_BYTE_DATA |
596 I2C_FUNC_SMBUS_WORD_DATA |
597 I2C_FUNC_SMBUS_BLOCK_DATA |
598 I2C_FUNC_SMBUS_I2C_BLOCK |
599 I2C_FUNC_SMBUS_PROC_CALL |
600 I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
603 static const struct i2c_algorithm smbus_algorithm = {
604 .smbus_xfer = cp2112_xfer,
605 .functionality = cp2112_functionality,
608 static int cp2112_get_usb_config(struct hid_device *hdev,
609 struct cp2112_usb_config_report *cfg)
611 int ret;
613 ret = cp2112_hid_get(hdev, CP2112_USB_CONFIG, (u8 *)cfg, sizeof(*cfg),
614 HID_FEATURE_REPORT);
615 if (ret != sizeof(*cfg)) {
616 hid_err(hdev, "error reading usb config: %d\n", ret);
617 if (ret < 0)
618 return ret;
619 return -EIO;
622 return 0;
625 static int cp2112_set_usb_config(struct hid_device *hdev,
626 struct cp2112_usb_config_report *cfg)
628 int ret;
630 BUG_ON(cfg->report != CP2112_USB_CONFIG);
632 ret = cp2112_hid_output(hdev, (u8 *)cfg, sizeof(*cfg),
633 HID_FEATURE_REPORT);
634 if (ret != sizeof(*cfg)) {
635 hid_err(hdev, "error writing usb config: %d\n", ret);
636 if (ret < 0)
637 return ret;
638 return -EIO;
641 return 0;
644 static void chmod_sysfs_attrs(struct hid_device *hdev);
646 #define CP2112_CONFIG_ATTR(name, store, format, ...) \
647 static ssize_t name##_store(struct device *kdev, \
648 struct device_attribute *attr, const char *buf, \
649 size_t count) \
651 struct hid_device *hdev = container_of(kdev, struct hid_device, dev); \
652 struct cp2112_usb_config_report cfg; \
653 int ret = cp2112_get_usb_config(hdev, &cfg); \
654 if (ret) \
655 return ret; \
656 store; \
657 ret = cp2112_set_usb_config(hdev, &cfg); \
658 if (ret) \
659 return ret; \
660 chmod_sysfs_attrs(hdev); \
661 return count; \
663 static ssize_t name##_show(struct device *kdev, \
664 struct device_attribute *attr, char *buf) \
666 struct hid_device *hdev = container_of(kdev, struct hid_device, dev); \
667 struct cp2112_usb_config_report cfg; \
668 int ret = cp2112_get_usb_config(hdev, &cfg); \
669 if (ret) \
670 return ret; \
671 return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \
673 static DEVICE_ATTR_RW(name);
675 CP2112_CONFIG_ATTR(vendor_id, ({
676 u16 vid;
678 if (sscanf(buf, "%hi", &vid) != 1)
679 return -EINVAL;
681 cfg.vid = cpu_to_le16(vid);
682 cfg.mask = 0x01;
683 }), "0x%04x\n", le16_to_cpu(cfg.vid));
685 CP2112_CONFIG_ATTR(product_id, ({
686 u16 pid;
688 if (sscanf(buf, "%hi", &pid) != 1)
689 return -EINVAL;
691 cfg.pid = cpu_to_le16(pid);
692 cfg.mask = 0x02;
693 }), "0x%04x\n", le16_to_cpu(cfg.pid));
695 CP2112_CONFIG_ATTR(max_power, ({
696 int mA;
698 if (sscanf(buf, "%i", &mA) != 1)
699 return -EINVAL;
701 cfg.max_power = (mA + 1) / 2;
702 cfg.mask = 0x04;
703 }), "%u mA\n", cfg.max_power * 2);
705 CP2112_CONFIG_ATTR(power_mode, ({
706 if (sscanf(buf, "%hhi", &cfg.power_mode) != 1)
707 return -EINVAL;
709 cfg.mask = 0x08;
710 }), "%u\n", cfg.power_mode);
712 CP2112_CONFIG_ATTR(release_version, ({
713 if (sscanf(buf, "%hhi.%hhi", &cfg.release_major, &cfg.release_minor)
714 != 2)
715 return -EINVAL;
717 cfg.mask = 0x10;
718 }), "%u.%u\n", cfg.release_major, cfg.release_minor);
720 #undef CP2112_CONFIG_ATTR
722 struct cp2112_pstring_attribute {
723 struct device_attribute attr;
724 unsigned char report;
727 static ssize_t pstr_store(struct device *kdev,
728 struct device_attribute *kattr, const char *buf,
729 size_t count)
731 struct hid_device *hdev = container_of(kdev, struct hid_device, dev);
732 struct cp2112_pstring_attribute *attr =
733 container_of(kattr, struct cp2112_pstring_attribute, attr);
734 struct cp2112_string_report report;
735 int ret;
737 memset(&report, 0, sizeof(report));
739 ret = utf8s_to_utf16s(buf, count, UTF16_LITTLE_ENDIAN,
740 report.string, ARRAY_SIZE(report.string));
741 report.report = attr->report;
742 report.length = ret * sizeof(report.string[0]) + 2;
743 report.type = USB_DT_STRING;
745 ret = cp2112_hid_output(hdev, &report.report, report.length + 1,
746 HID_FEATURE_REPORT);
747 if (ret != report.length + 1) {
748 hid_err(hdev, "error writing %s string: %d\n", kattr->attr.name,
749 ret);
750 if (ret < 0)
751 return ret;
752 return -EIO;
755 chmod_sysfs_attrs(hdev);
756 return count;
759 static ssize_t pstr_show(struct device *kdev,
760 struct device_attribute *kattr, char *buf)
762 struct hid_device *hdev = container_of(kdev, struct hid_device, dev);
763 struct cp2112_pstring_attribute *attr =
764 container_of(kattr, struct cp2112_pstring_attribute, attr);
765 struct cp2112_string_report report;
766 u8 length;
767 int ret;
769 ret = cp2112_hid_get(hdev, attr->report, &report.report,
770 sizeof(report) - 1, HID_FEATURE_REPORT);
771 if (ret < 3) {
772 hid_err(hdev, "error reading %s string: %d\n", kattr->attr.name,
773 ret);
774 if (ret < 0)
775 return ret;
776 return -EIO;
779 if (report.length < 2) {
780 hid_err(hdev, "invalid %s string length: %d\n",
781 kattr->attr.name, report.length);
782 return -EIO;
785 length = report.length > ret - 1 ? ret - 1 : report.length;
786 length = (length - 2) / sizeof(report.string[0]);
787 ret = utf16s_to_utf8s(report.string, length, UTF16_LITTLE_ENDIAN, buf,
788 PAGE_SIZE - 1);
789 buf[ret++] = '\n';
790 return ret;
793 #define CP2112_PSTR_ATTR(name, _report) \
794 static struct cp2112_pstring_attribute dev_attr_##name = { \
795 .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \
796 .report = _report, \
799 CP2112_PSTR_ATTR(manufacturer, CP2112_MANUFACTURER_STRING);
800 CP2112_PSTR_ATTR(product, CP2112_PRODUCT_STRING);
801 CP2112_PSTR_ATTR(serial, CP2112_SERIAL_STRING);
803 #undef CP2112_PSTR_ATTR
805 static const struct attribute_group cp2112_attr_group = {
806 .attrs = (struct attribute *[]){
807 &dev_attr_vendor_id.attr,
808 &dev_attr_product_id.attr,
809 &dev_attr_max_power.attr,
810 &dev_attr_power_mode.attr,
811 &dev_attr_release_version.attr,
812 &dev_attr_manufacturer.attr.attr,
813 &dev_attr_product.attr.attr,
814 &dev_attr_serial.attr.attr,
815 NULL
819 /* Chmoding our sysfs attributes is simply a way to expose which fields in the
820 * PROM have already been programmed. We do not depend on this preventing
821 * writing to these attributes since the CP2112 will simply ignore writes to
822 * already-programmed fields. This is why there is no sense in fixing this
823 * racy behaviour.
825 static void chmod_sysfs_attrs(struct hid_device *hdev)
827 struct attribute **attr;
828 u8 buf[2];
829 int ret;
831 ret = cp2112_hid_get(hdev, CP2112_LOCK_BYTE, buf, sizeof(buf),
832 HID_FEATURE_REPORT);
833 if (ret != sizeof(buf)) {
834 hid_err(hdev, "error reading lock byte: %d\n", ret);
835 return;
838 for (attr = cp2112_attr_group.attrs; *attr; ++attr) {
839 umode_t mode = (buf[1] & 1) ? S_IWUSR | S_IRUGO : S_IRUGO;
840 ret = sysfs_chmod_file(&hdev->dev.kobj, *attr, mode);
841 if (ret < 0)
842 hid_err(hdev, "error chmoding sysfs file %s\n",
843 (*attr)->name);
844 buf[1] >>= 1;
848 static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id)
850 struct cp2112_device *dev;
851 u8 buf[3];
852 struct cp2112_smbus_config_report config;
853 int ret;
855 ret = hid_parse(hdev);
856 if (ret) {
857 hid_err(hdev, "parse failed\n");
858 return ret;
861 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
862 if (ret) {
863 hid_err(hdev, "hw start failed\n");
864 return ret;
867 ret = hid_hw_open(hdev);
868 if (ret) {
869 hid_err(hdev, "hw open failed\n");
870 goto err_hid_stop;
873 ret = hid_hw_power(hdev, PM_HINT_FULLON);
874 if (ret < 0) {
875 hid_err(hdev, "power management error: %d\n", ret);
876 goto err_hid_close;
879 ret = cp2112_hid_get(hdev, CP2112_GET_VERSION_INFO, buf, sizeof(buf),
880 HID_FEATURE_REPORT);
881 if (ret != sizeof(buf)) {
882 hid_err(hdev, "error requesting version\n");
883 if (ret >= 0)
884 ret = -EIO;
885 goto err_power_normal;
888 hid_info(hdev, "Part Number: 0x%02X Device Version: 0x%02X\n",
889 buf[1], buf[2]);
891 ret = cp2112_hid_get(hdev, CP2112_SMBUS_CONFIG, (u8 *)&config,
892 sizeof(config), HID_FEATURE_REPORT);
893 if (ret != sizeof(config)) {
894 hid_err(hdev, "error requesting SMBus config\n");
895 if (ret >= 0)
896 ret = -EIO;
897 goto err_power_normal;
900 config.retry_time = cpu_to_be16(1);
902 ret = cp2112_hid_output(hdev, (u8 *)&config, sizeof(config),
903 HID_FEATURE_REPORT);
904 if (ret != sizeof(config)) {
905 hid_err(hdev, "error setting SMBus config\n");
906 if (ret >= 0)
907 ret = -EIO;
908 goto err_power_normal;
911 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
912 if (!dev) {
913 ret = -ENOMEM;
914 goto err_power_normal;
917 hid_set_drvdata(hdev, (void *)dev);
918 dev->hdev = hdev;
919 dev->adap.owner = THIS_MODULE;
920 dev->adap.class = I2C_CLASS_HWMON;
921 dev->adap.algo = &smbus_algorithm;
922 dev->adap.algo_data = dev;
923 dev->adap.dev.parent = &hdev->dev;
924 snprintf(dev->adap.name, sizeof(dev->adap.name),
925 "CP2112 SMBus Bridge on hiddev%d", hdev->minor);
926 init_waitqueue_head(&dev->wait);
928 hid_device_io_start(hdev);
929 ret = i2c_add_adapter(&dev->adap);
930 hid_device_io_stop(hdev);
932 if (ret) {
933 hid_err(hdev, "error registering i2c adapter\n");
934 goto err_free_dev;
937 hid_dbg(hdev, "adapter registered\n");
939 dev->gc.label = "cp2112_gpio";
940 dev->gc.direction_input = cp2112_gpio_direction_input;
941 dev->gc.direction_output = cp2112_gpio_direction_output;
942 dev->gc.set = cp2112_gpio_set;
943 dev->gc.get = cp2112_gpio_get;
944 dev->gc.base = -1;
945 dev->gc.ngpio = 8;
946 dev->gc.can_sleep = 1;
947 dev->gc.dev = &hdev->dev;
949 ret = gpiochip_add(&dev->gc);
950 if (ret < 0) {
951 hid_err(hdev, "error registering gpio chip\n");
952 goto err_free_i2c;
955 ret = sysfs_create_group(&hdev->dev.kobj, &cp2112_attr_group);
956 if (ret < 0) {
957 hid_err(hdev, "error creating sysfs attrs\n");
958 goto err_gpiochip_remove;
961 chmod_sysfs_attrs(hdev);
962 hid_hw_power(hdev, PM_HINT_NORMAL);
964 return ret;
966 err_gpiochip_remove:
967 if (gpiochip_remove(&dev->gc) < 0)
968 hid_err(hdev, "error removing gpio chip\n");
969 err_free_i2c:
970 i2c_del_adapter(&dev->adap);
971 err_free_dev:
972 kfree(dev);
973 err_power_normal:
974 hid_hw_power(hdev, PM_HINT_NORMAL);
975 err_hid_close:
976 hid_hw_close(hdev);
977 err_hid_stop:
978 hid_hw_stop(hdev);
979 return ret;
982 static void cp2112_remove(struct hid_device *hdev)
984 struct cp2112_device *dev = hid_get_drvdata(hdev);
986 sysfs_remove_group(&hdev->dev.kobj, &cp2112_attr_group);
987 if (gpiochip_remove(&dev->gc))
988 hid_err(hdev, "unable to remove gpio chip\n");
989 i2c_del_adapter(&dev->adap);
990 /* i2c_del_adapter has finished removing all i2c devices from our
991 * adapter. Well behaved devices should no longer call our cp2112_xfer
992 * and should have waited for any pending calls to finish. It has also
993 * waited for device_unregister(&adap->dev) to complete. Therefore we
994 * can safely free our struct cp2112_device.
996 hid_hw_close(hdev);
997 hid_hw_stop(hdev);
998 kfree(dev);
1001 static int cp2112_raw_event(struct hid_device *hdev, struct hid_report *report,
1002 u8 *data, int size)
1004 struct cp2112_device *dev = hid_get_drvdata(hdev);
1005 struct cp2112_xfer_status_report *xfer = (void *)data;
1007 switch (data[0]) {
1008 case CP2112_TRANSFER_STATUS_RESPONSE:
1009 hid_dbg(hdev, "xfer status: %02x %02x %04x %04x\n",
1010 xfer->status0, xfer->status1,
1011 be16_to_cpu(xfer->retries), be16_to_cpu(xfer->length));
1013 switch (xfer->status0) {
1014 case STATUS0_IDLE:
1015 dev->xfer_status = -EAGAIN;
1016 break;
1017 case STATUS0_BUSY:
1018 dev->xfer_status = -EBUSY;
1019 break;
1020 case STATUS0_COMPLETE:
1021 dev->xfer_status = be16_to_cpu(xfer->length);
1022 break;
1023 case STATUS0_ERROR:
1024 switch (xfer->status1) {
1025 case STATUS1_TIMEOUT_NACK:
1026 case STATUS1_TIMEOUT_BUS:
1027 dev->xfer_status = -ETIMEDOUT;
1028 break;
1029 default:
1030 dev->xfer_status = -EIO;
1031 break;
1033 break;
1034 default:
1035 dev->xfer_status = -EINVAL;
1036 break;
1039 atomic_set(&dev->xfer_avail, 1);
1040 break;
1041 case CP2112_DATA_READ_RESPONSE:
1042 hid_dbg(hdev, "read response: %02x %02x\n", data[1], data[2]);
1044 dev->read_length = data[2];
1045 if (dev->read_length > sizeof(dev->read_data))
1046 dev->read_length = sizeof(dev->read_data);
1048 memcpy(dev->read_data, &data[3], dev->read_length);
1049 atomic_set(&dev->read_avail, 1);
1050 break;
1051 default:
1052 hid_err(hdev, "unknown report\n");
1054 return 0;
1057 wake_up_interruptible(&dev->wait);
1058 return 1;
1061 static struct hid_driver cp2112_driver = {
1062 .name = "cp2112",
1063 .id_table = cp2112_devices,
1064 .probe = cp2112_probe,
1065 .remove = cp2112_remove,
1066 .raw_event = cp2112_raw_event,
1069 module_hid_driver(cp2112_driver);
1070 MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge");
1071 MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
1072 MODULE_LICENSE("GPL");