blob: 75398cb84fde28d78e3e71dfaf3fcd00c953ea3a [file] [log] [blame]
David Barksdalee932d812014-02-04 12:42:48 -06001/*
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>
5 *
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.
9 *
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.
14 */
15
16/*
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.
20 *
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
25 */
26
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"
34
35enum {
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,
54};
55
56enum {
57 STATUS0_IDLE = 0x00,
58 STATUS0_BUSY = 0x01,
59 STATUS0_COMPLETE = 0x02,
60 STATUS0_ERROR = 0x03,
61};
62
63enum {
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,
70};
71
72struct 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;
82
83struct 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;
95
96struct cp2112_read_req_report {
97 u8 report; /* CP2112_DATA_READ_REQUEST */
98 u8 slave_address;
99 __be16 length;
100} __packed;
101
102struct 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;
109
110struct cp2112_write_req_report {
111 u8 report; /* CP2112_DATA_WRITE_REQUEST */
112 u8 slave_address;
113 u8 length;
114 u8 data[61];
115} __packed;
116
117struct cp2112_force_read_report {
118 u8 report; /* CP2112_DATA_READ_FORCE_SEND */
119 __be16 length;
120} __packed;
121
122struct 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;
129
130struct 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;
137
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. */
141static const int XFER_STATUS_RETRIES = 10;
142
143/* Time in ms to wait for a CP2112_DATA_READ_RESPONSE or
144 CP2112_TRANSFER_STATUS_RESPONSE. */
145static const int RESPONSE_TIMEOUT = 50;
146
147static const struct hid_device_id cp2112_devices[] = {
148 { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_CP2112) },
149 { }
150};
151MODULE_DEVICE_TABLE(hid, cp2112_devices);
152
153struct 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;
163};
164
165static int gpio_push_pull = 0xFF;
166module_param(gpio_push_pull, int, S_IRUGO | S_IWUSR);
167MODULE_PARM_DESC(gpio_push_pull, "GPIO push-pull configuration bitmask");
168
169static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
170{
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;
176
Jiri Kosina490051a2014-02-18 00:39:39 +0100177 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
178 sizeof(buf), HID_FEATURE_REPORT,
179 HID_REQ_GET_REPORT);
David Barksdalee932d812014-02-04 12:42:48 -0600180 if (ret != sizeof(buf)) {
181 hid_err(hdev, "error requesting GPIO config: %d\n", ret);
182 return ret;
183 }
184
185 buf[1] &= ~(1 << offset);
186 buf[2] = gpio_push_pull;
187
Benjamin Tissoires293e4832014-03-08 22:52:40 -0500188 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, sizeof(buf),
189 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
David Barksdalee932d812014-02-04 12:42:48 -0600190 if (ret < 0) {
191 hid_err(hdev, "error setting GPIO config: %d\n", ret);
192 return ret;
193 }
194
195 return 0;
196}
197
198static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
199{
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;
205
206 buf[0] = CP2112_GPIO_SET;
207 buf[1] = value ? 0xff : 0;
208 buf[2] = 1 << offset;
209
Benjamin Tissoires293e4832014-03-08 22:52:40 -0500210 ret = hid_hw_raw_request(hdev, CP2112_GPIO_SET, buf, sizeof(buf),
211 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
David Barksdalee932d812014-02-04 12:42:48 -0600212 if (ret < 0)
213 hid_err(hdev, "error setting GPIO values: %d\n", ret);
214}
215
216static int cp2112_gpio_get(struct gpio_chip *chip, unsigned offset)
217{
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;
223
Jiri Kosina490051a2014-02-18 00:39:39 +0100224 ret = hid_hw_raw_request(hdev, CP2112_GPIO_GET, buf, sizeof(buf),
225 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
David Barksdalee932d812014-02-04 12:42:48 -0600226 if (ret != sizeof(buf)) {
227 hid_err(hdev, "error requesting GPIO values: %d\n", ret);
228 return ret;
229 }
230
231 return (buf[1] >> offset) & 1;
232}
233
234static int cp2112_gpio_direction_output(struct gpio_chip *chip,
235 unsigned offset, int value)
236{
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;
242
Jiri Kosina490051a2014-02-18 00:39:39 +0100243 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
244 sizeof(buf), HID_FEATURE_REPORT,
245 HID_REQ_GET_REPORT);
David Barksdalee932d812014-02-04 12:42:48 -0600246 if (ret != sizeof(buf)) {
247 hid_err(hdev, "error requesting GPIO config: %d\n", ret);
248 return ret;
249 }
250
251 buf[1] |= 1 << offset;
252 buf[2] = gpio_push_pull;
253
Benjamin Tissoires293e4832014-03-08 22:52:40 -0500254 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, sizeof(buf),
255 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
David Barksdalee932d812014-02-04 12:42:48 -0600256 if (ret < 0) {
257 hid_err(hdev, "error setting GPIO config: %d\n", ret);
258 return ret;
259 }
260
Antonio Borneobeb9d002014-06-29 14:13:48 +0800261 /*
262 * Set gpio value when output direction is already set,
263 * as specified in AN495, Rev. 0.2, cpt. 4.4
264 */
265 cp2112_gpio_set(chip, offset, value);
266
David Barksdalee932d812014-02-04 12:42:48 -0600267 return 0;
268}
269
270static int cp2112_hid_get(struct hid_device *hdev, unsigned char report_number,
271 u8 *data, size_t count, unsigned char report_type)
272{
273 u8 *buf;
274 int ret;
275
276 buf = kmalloc(count, GFP_KERNEL);
277 if (!buf)
278 return -ENOMEM;
279
Jiri Kosina490051a2014-02-18 00:39:39 +0100280 ret = hid_hw_raw_request(hdev, report_number, buf, count,
281 report_type, HID_REQ_GET_REPORT);
David Barksdalee932d812014-02-04 12:42:48 -0600282 memcpy(data, buf, count);
283 kfree(buf);
284 return ret;
285}
286
287static int cp2112_hid_output(struct hid_device *hdev, u8 *data, size_t count,
288 unsigned char report_type)
289{
290 u8 *buf;
291 int ret;
292
293 buf = kmemdup(data, count, GFP_KERNEL);
294 if (!buf)
295 return -ENOMEM;
296
Benjamin Tissoires866e4792014-03-08 22:52:41 -0500297 if (report_type == HID_OUTPUT_REPORT)
298 ret = hid_hw_output_report(hdev, buf, count);
299 else
300 ret = hid_hw_raw_request(hdev, buf[0], buf, count, report_type,
301 HID_REQ_SET_REPORT);
302
David Barksdalee932d812014-02-04 12:42:48 -0600303 kfree(buf);
304 return ret;
305}
306
307static int cp2112_wait(struct cp2112_device *dev, atomic_t *avail)
308{
309 int ret = 0;
310
311 /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a
312 * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to
313 * come in cp2112_raw_event or timeout. There will only be one of these
314 * in flight at any one time. The timeout is extremely large and is a
315 * last resort if the CP2112 has died. If we do timeout we don't expect
316 * to receive the response which would cause data races, it's not like
317 * we can do anything about it anyway.
318 */
319 ret = wait_event_interruptible_timeout(dev->wait,
320 atomic_read(avail), msecs_to_jiffies(RESPONSE_TIMEOUT));
321 if (-ERESTARTSYS == ret)
322 return ret;
323 if (!ret)
324 return -ETIMEDOUT;
325
326 atomic_set(avail, 0);
327 return 0;
328}
329
330static int cp2112_xfer_status(struct cp2112_device *dev)
331{
332 struct hid_device *hdev = dev->hdev;
333 u8 buf[2];
334 int ret;
335
336 buf[0] = CP2112_TRANSFER_STATUS_REQUEST;
337 buf[1] = 0x01;
338 atomic_set(&dev->xfer_avail, 0);
339
340 ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
341 if (ret < 0) {
342 hid_warn(hdev, "Error requesting status: %d\n", ret);
343 return ret;
344 }
345
346 ret = cp2112_wait(dev, &dev->xfer_avail);
347 if (ret)
348 return ret;
349
350 return dev->xfer_status;
351}
352
353static int cp2112_read(struct cp2112_device *dev, u8 *data, size_t size)
354{
355 struct hid_device *hdev = dev->hdev;
356 struct cp2112_force_read_report report;
357 int ret;
358
Antonio Borneo6debce62015-06-21 14:20:25 +0800359 if (size > sizeof(dev->read_data))
360 size = sizeof(dev->read_data);
David Barksdalee932d812014-02-04 12:42:48 -0600361 report.report = CP2112_DATA_READ_FORCE_SEND;
362 report.length = cpu_to_be16(size);
363
364 atomic_set(&dev->read_avail, 0);
365
366 ret = cp2112_hid_output(hdev, &report.report, sizeof(report),
367 HID_OUTPUT_REPORT);
368 if (ret < 0) {
369 hid_warn(hdev, "Error requesting data: %d\n", ret);
370 return ret;
371 }
372
373 ret = cp2112_wait(dev, &dev->read_avail);
374 if (ret)
375 return ret;
376
Jiri Kosina5a673fc2014-02-17 23:44:54 +0100377 hid_dbg(hdev, "read %d of %zd bytes requested\n",
David Barksdalee932d812014-02-04 12:42:48 -0600378 dev->read_length, size);
379
380 if (size > dev->read_length)
381 size = dev->read_length;
382
383 memcpy(data, dev->read_data, size);
384 return dev->read_length;
385}
386
387static int cp2112_read_req(void *buf, u8 slave_address, u16 length)
388{
389 struct cp2112_read_req_report *report = buf;
390
391 if (length < 1 || length > 512)
392 return -EINVAL;
393
394 report->report = CP2112_DATA_READ_REQUEST;
395 report->slave_address = slave_address << 1;
396 report->length = cpu_to_be16(length);
397 return sizeof(*report);
398}
399
400static int cp2112_write_read_req(void *buf, u8 slave_address, u16 length,
401 u8 command, u8 *data, u8 data_length)
402{
403 struct cp2112_write_read_req_report *report = buf;
404
405 if (length < 1 || length > 512
406 || data_length > sizeof(report->target_address) - 1)
407 return -EINVAL;
408
409 report->report = CP2112_DATA_WRITE_READ_REQUEST;
410 report->slave_address = slave_address << 1;
411 report->length = cpu_to_be16(length);
412 report->target_address_length = data_length + 1;
413 report->target_address[0] = command;
414 memcpy(&report->target_address[1], data, data_length);
415 return data_length + 6;
416}
417
418static int cp2112_write_req(void *buf, u8 slave_address, u8 command, u8 *data,
419 u8 data_length)
420{
421 struct cp2112_write_req_report *report = buf;
422
423 if (data_length > sizeof(report->data) - 1)
424 return -EINVAL;
425
426 report->report = CP2112_DATA_WRITE_REQUEST;
427 report->slave_address = slave_address << 1;
428 report->length = data_length + 1;
429 report->data[0] = command;
430 memcpy(&report->data[1], data, data_length);
431 return data_length + 4;
432}
433
Antonio Borneob9029342014-07-08 07:25:39 +0800434static int cp2112_i2c_write_req(void *buf, u8 slave_address, u8 *data,
435 u8 data_length)
436{
437 struct cp2112_write_req_report *report = buf;
438
439 if (data_length > sizeof(report->data))
440 return -EINVAL;
441
442 report->report = CP2112_DATA_WRITE_REQUEST;
443 report->slave_address = slave_address << 1;
444 report->length = data_length;
445 memcpy(report->data, data, data_length);
446 return data_length + 3;
447}
448
449static int cp2112_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
450 int num)
451{
452 struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data;
453 struct hid_device *hdev = dev->hdev;
454 u8 buf[64];
455 ssize_t count;
456 unsigned int retries;
457 int ret;
458
459 hid_dbg(hdev, "I2C %d messages\n", num);
460
461 if (num != 1) {
462 hid_err(hdev,
463 "Multi-message I2C transactions not supported\n");
464 return -EOPNOTSUPP;
465 }
466
467 if (msgs->flags & I2C_M_RD)
468 count = cp2112_read_req(buf, msgs->addr, msgs->len);
469 else
470 count = cp2112_i2c_write_req(buf, msgs->addr, msgs->buf,
471 msgs->len);
472
473 if (count < 0)
474 return count;
475
476 ret = hid_hw_power(hdev, PM_HINT_FULLON);
477 if (ret < 0) {
478 hid_err(hdev, "power management error: %d\n", ret);
479 return ret;
480 }
481
482 ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT);
483 if (ret < 0) {
484 hid_warn(hdev, "Error starting transaction: %d\n", ret);
485 goto power_normal;
486 }
487
488 for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) {
489 ret = cp2112_xfer_status(dev);
490 if (-EBUSY == ret)
491 continue;
492 if (ret < 0)
493 goto power_normal;
494 break;
495 }
496
497 if (XFER_STATUS_RETRIES <= retries) {
498 hid_warn(hdev, "Transfer timed out, cancelling.\n");
499 buf[0] = CP2112_CANCEL_TRANSFER;
500 buf[1] = 0x01;
501
502 ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
503 if (ret < 0)
504 hid_warn(hdev, "Error cancelling transaction: %d\n",
505 ret);
506
507 ret = -ETIMEDOUT;
508 goto power_normal;
509 }
510
511 if (!(msgs->flags & I2C_M_RD))
512 goto finish;
513
Ellen Wang5ddfb122015-07-08 11:17:39 -0700514 for (count = 0; count < msgs->len;) {
515 ret = cp2112_read(dev, msgs->buf + count, msgs->len - count);
516 if (ret < 0)
517 goto power_normal;
518 if (ret == 0) {
519 hid_err(hdev, "read returned 0\n");
520 ret = -EIO;
521 goto power_normal;
522 }
523 count += ret;
524 if (count > msgs->len) {
525 /*
526 * The hardware returned too much data.
527 * This is mostly harmless because cp2112_read()
528 * has a limit check so didn't overrun our
529 * buffer. Nevertheless, we return an error
530 * because something is seriously wrong and
531 * it shouldn't go unnoticed.
532 */
533 hid_err(hdev, "long read: %d > %zd\n",
534 ret, msgs->len - count + ret);
535 ret = -EIO;
536 goto power_normal;
537 }
Antonio Borneob9029342014-07-08 07:25:39 +0800538 }
539
540finish:
541 /* return the number of transferred messages */
542 ret = 1;
543
544power_normal:
545 hid_hw_power(hdev, PM_HINT_NORMAL);
546 hid_dbg(hdev, "I2C transfer finished: %d\n", ret);
547 return ret;
548}
549
David Barksdalee932d812014-02-04 12:42:48 -0600550static int cp2112_xfer(struct i2c_adapter *adap, u16 addr,
551 unsigned short flags, char read_write, u8 command,
552 int size, union i2c_smbus_data *data)
553{
554 struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data;
555 struct hid_device *hdev = dev->hdev;
556 u8 buf[64];
557 __be16 word;
Jiri Kosina0438ee72014-02-18 09:43:53 +0100558 ssize_t count;
David Barksdalee932d812014-02-04 12:42:48 -0600559 size_t read_length = 0;
560 unsigned int retries;
561 int ret;
562
563 hid_dbg(hdev, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n",
564 read_write == I2C_SMBUS_WRITE ? "write" : "read",
565 addr, flags, command, size);
566
567 switch (size) {
568 case I2C_SMBUS_BYTE:
569 read_length = 1;
570
571 if (I2C_SMBUS_READ == read_write)
572 count = cp2112_read_req(buf, addr, read_length);
573 else
574 count = cp2112_write_req(buf, addr, data->byte, NULL,
575 0);
576 break;
577 case I2C_SMBUS_BYTE_DATA:
578 read_length = 1;
579
580 if (I2C_SMBUS_READ == read_write)
581 count = cp2112_write_read_req(buf, addr, read_length,
582 command, NULL, 0);
583 else
584 count = cp2112_write_req(buf, addr, command,
585 &data->byte, 1);
586 break;
587 case I2C_SMBUS_WORD_DATA:
588 read_length = 2;
589 word = cpu_to_be16(data->word);
590
591 if (I2C_SMBUS_READ == read_write)
592 count = cp2112_write_read_req(buf, addr, read_length,
593 command, NULL, 0);
594 else
595 count = cp2112_write_req(buf, addr, command,
596 (u8 *)&word, 2);
597 break;
598 case I2C_SMBUS_PROC_CALL:
599 size = I2C_SMBUS_WORD_DATA;
600 read_write = I2C_SMBUS_READ;
601 read_length = 2;
602 word = cpu_to_be16(data->word);
603
604 count = cp2112_write_read_req(buf, addr, read_length, command,
605 (u8 *)&word, 2);
606 break;
607 case I2C_SMBUS_I2C_BLOCK_DATA:
608 size = I2C_SMBUS_BLOCK_DATA;
609 /* fallthrough */
610 case I2C_SMBUS_BLOCK_DATA:
611 if (I2C_SMBUS_READ == read_write) {
612 count = cp2112_write_read_req(buf, addr,
613 I2C_SMBUS_BLOCK_MAX,
614 command, NULL, 0);
615 } else {
616 count = cp2112_write_req(buf, addr, command,
617 data->block,
618 data->block[0] + 1);
619 }
620 break;
621 case I2C_SMBUS_BLOCK_PROC_CALL:
622 size = I2C_SMBUS_BLOCK_DATA;
623 read_write = I2C_SMBUS_READ;
624
625 count = cp2112_write_read_req(buf, addr, I2C_SMBUS_BLOCK_MAX,
626 command, data->block,
627 data->block[0] + 1);
628 break;
629 default:
630 hid_warn(hdev, "Unsupported transaction %d\n", size);
631 return -EOPNOTSUPP;
632 }
633
634 if (count < 0)
635 return count;
636
637 ret = hid_hw_power(hdev, PM_HINT_FULLON);
638 if (ret < 0) {
639 hid_err(hdev, "power management error: %d\n", ret);
640 return ret;
641 }
642
643 ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT);
644 if (ret < 0) {
645 hid_warn(hdev, "Error starting transaction: %d\n", ret);
646 goto power_normal;
647 }
648
649 for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) {
650 ret = cp2112_xfer_status(dev);
651 if (-EBUSY == ret)
652 continue;
653 if (ret < 0)
654 goto power_normal;
655 break;
656 }
657
658 if (XFER_STATUS_RETRIES <= retries) {
659 hid_warn(hdev, "Transfer timed out, cancelling.\n");
660 buf[0] = CP2112_CANCEL_TRANSFER;
661 buf[1] = 0x01;
662
663 ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
664 if (ret < 0)
665 hid_warn(hdev, "Error cancelling transaction: %d\n",
666 ret);
667
668 ret = -ETIMEDOUT;
669 goto power_normal;
670 }
671
672 if (I2C_SMBUS_WRITE == read_write) {
673 ret = 0;
674 goto power_normal;
675 }
676
677 if (I2C_SMBUS_BLOCK_DATA == size)
678 read_length = ret;
679
680 ret = cp2112_read(dev, buf, read_length);
681 if (ret < 0)
682 goto power_normal;
683 if (ret != read_length) {
Jiri Kosina5a673fc2014-02-17 23:44:54 +0100684 hid_warn(hdev, "short read: %d < %zd\n", ret, read_length);
David Barksdalee932d812014-02-04 12:42:48 -0600685 ret = -EIO;
686 goto power_normal;
687 }
688
689 switch (size) {
690 case I2C_SMBUS_BYTE:
691 case I2C_SMBUS_BYTE_DATA:
692 data->byte = buf[0];
693 break;
694 case I2C_SMBUS_WORD_DATA:
695 data->word = be16_to_cpup((__be16 *)buf);
696 break;
697 case I2C_SMBUS_BLOCK_DATA:
698 if (read_length > I2C_SMBUS_BLOCK_MAX) {
699 ret = -EPROTO;
700 goto power_normal;
701 }
702
703 memcpy(data->block, buf, read_length);
704 break;
705 }
706
707 ret = 0;
708power_normal:
709 hid_hw_power(hdev, PM_HINT_NORMAL);
710 hid_dbg(hdev, "transfer finished: %d\n", ret);
711 return ret;
712}
713
714static u32 cp2112_functionality(struct i2c_adapter *adap)
715{
Antonio Borneob9029342014-07-08 07:25:39 +0800716 return I2C_FUNC_I2C |
717 I2C_FUNC_SMBUS_BYTE |
David Barksdalee932d812014-02-04 12:42:48 -0600718 I2C_FUNC_SMBUS_BYTE_DATA |
719 I2C_FUNC_SMBUS_WORD_DATA |
720 I2C_FUNC_SMBUS_BLOCK_DATA |
721 I2C_FUNC_SMBUS_I2C_BLOCK |
722 I2C_FUNC_SMBUS_PROC_CALL |
723 I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
724}
725
726static const struct i2c_algorithm smbus_algorithm = {
Antonio Borneob9029342014-07-08 07:25:39 +0800727 .master_xfer = cp2112_i2c_xfer,
David Barksdalee932d812014-02-04 12:42:48 -0600728 .smbus_xfer = cp2112_xfer,
729 .functionality = cp2112_functionality,
730};
731
732static int cp2112_get_usb_config(struct hid_device *hdev,
733 struct cp2112_usb_config_report *cfg)
734{
735 int ret;
736
737 ret = cp2112_hid_get(hdev, CP2112_USB_CONFIG, (u8 *)cfg, sizeof(*cfg),
738 HID_FEATURE_REPORT);
739 if (ret != sizeof(*cfg)) {
740 hid_err(hdev, "error reading usb config: %d\n", ret);
741 if (ret < 0)
742 return ret;
743 return -EIO;
744 }
745
746 return 0;
747}
748
749static int cp2112_set_usb_config(struct hid_device *hdev,
750 struct cp2112_usb_config_report *cfg)
751{
752 int ret;
753
754 BUG_ON(cfg->report != CP2112_USB_CONFIG);
755
756 ret = cp2112_hid_output(hdev, (u8 *)cfg, sizeof(*cfg),
757 HID_FEATURE_REPORT);
758 if (ret != sizeof(*cfg)) {
759 hid_err(hdev, "error writing usb config: %d\n", ret);
760 if (ret < 0)
761 return ret;
762 return -EIO;
763 }
764
765 return 0;
766}
767
768static void chmod_sysfs_attrs(struct hid_device *hdev);
769
770#define CP2112_CONFIG_ATTR(name, store, format, ...) \
771static ssize_t name##_store(struct device *kdev, \
772 struct device_attribute *attr, const char *buf, \
773 size_t count) \
774{ \
775 struct hid_device *hdev = container_of(kdev, struct hid_device, dev); \
776 struct cp2112_usb_config_report cfg; \
777 int ret = cp2112_get_usb_config(hdev, &cfg); \
778 if (ret) \
779 return ret; \
780 store; \
781 ret = cp2112_set_usb_config(hdev, &cfg); \
782 if (ret) \
783 return ret; \
784 chmod_sysfs_attrs(hdev); \
785 return count; \
786} \
787static ssize_t name##_show(struct device *kdev, \
788 struct device_attribute *attr, char *buf) \
789{ \
790 struct hid_device *hdev = container_of(kdev, struct hid_device, dev); \
791 struct cp2112_usb_config_report cfg; \
792 int ret = cp2112_get_usb_config(hdev, &cfg); \
793 if (ret) \
794 return ret; \
795 return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \
796} \
Jiri Kosinac3c041b2014-02-17 23:40:20 +0100797static DEVICE_ATTR_RW(name);
David Barksdalee932d812014-02-04 12:42:48 -0600798
799CP2112_CONFIG_ATTR(vendor_id, ({
800 u16 vid;
801
802 if (sscanf(buf, "%hi", &vid) != 1)
803 return -EINVAL;
804
805 cfg.vid = cpu_to_le16(vid);
806 cfg.mask = 0x01;
807}), "0x%04x\n", le16_to_cpu(cfg.vid));
808
809CP2112_CONFIG_ATTR(product_id, ({
810 u16 pid;
811
812 if (sscanf(buf, "%hi", &pid) != 1)
813 return -EINVAL;
814
815 cfg.pid = cpu_to_le16(pid);
816 cfg.mask = 0x02;
817}), "0x%04x\n", le16_to_cpu(cfg.pid));
818
819CP2112_CONFIG_ATTR(max_power, ({
820 int mA;
821
822 if (sscanf(buf, "%i", &mA) != 1)
823 return -EINVAL;
824
825 cfg.max_power = (mA + 1) / 2;
826 cfg.mask = 0x04;
827}), "%u mA\n", cfg.max_power * 2);
828
829CP2112_CONFIG_ATTR(power_mode, ({
830 if (sscanf(buf, "%hhi", &cfg.power_mode) != 1)
831 return -EINVAL;
832
833 cfg.mask = 0x08;
834}), "%u\n", cfg.power_mode);
835
836CP2112_CONFIG_ATTR(release_version, ({
837 if (sscanf(buf, "%hhi.%hhi", &cfg.release_major, &cfg.release_minor)
838 != 2)
839 return -EINVAL;
840
841 cfg.mask = 0x10;
842}), "%u.%u\n", cfg.release_major, cfg.release_minor);
843
844#undef CP2112_CONFIG_ATTR
845
846struct cp2112_pstring_attribute {
847 struct device_attribute attr;
848 unsigned char report;
849};
850
851static ssize_t pstr_store(struct device *kdev,
852 struct device_attribute *kattr, const char *buf,
853 size_t count)
854{
855 struct hid_device *hdev = container_of(kdev, struct hid_device, dev);
856 struct cp2112_pstring_attribute *attr =
857 container_of(kattr, struct cp2112_pstring_attribute, attr);
858 struct cp2112_string_report report;
859 int ret;
860
861 memset(&report, 0, sizeof(report));
862
863 ret = utf8s_to_utf16s(buf, count, UTF16_LITTLE_ENDIAN,
864 report.string, ARRAY_SIZE(report.string));
865 report.report = attr->report;
866 report.length = ret * sizeof(report.string[0]) + 2;
867 report.type = USB_DT_STRING;
868
869 ret = cp2112_hid_output(hdev, &report.report, report.length + 1,
870 HID_FEATURE_REPORT);
871 if (ret != report.length + 1) {
872 hid_err(hdev, "error writing %s string: %d\n", kattr->attr.name,
873 ret);
874 if (ret < 0)
875 return ret;
876 return -EIO;
877 }
878
879 chmod_sysfs_attrs(hdev);
880 return count;
881}
882
883static ssize_t pstr_show(struct device *kdev,
884 struct device_attribute *kattr, char *buf)
885{
886 struct hid_device *hdev = container_of(kdev, struct hid_device, dev);
887 struct cp2112_pstring_attribute *attr =
888 container_of(kattr, struct cp2112_pstring_attribute, attr);
889 struct cp2112_string_report report;
890 u8 length;
891 int ret;
892
893 ret = cp2112_hid_get(hdev, attr->report, &report.report,
894 sizeof(report) - 1, HID_FEATURE_REPORT);
895 if (ret < 3) {
896 hid_err(hdev, "error reading %s string: %d\n", kattr->attr.name,
897 ret);
898 if (ret < 0)
899 return ret;
900 return -EIO;
901 }
902
903 if (report.length < 2) {
904 hid_err(hdev, "invalid %s string length: %d\n",
905 kattr->attr.name, report.length);
906 return -EIO;
907 }
908
909 length = report.length > ret - 1 ? ret - 1 : report.length;
910 length = (length - 2) / sizeof(report.string[0]);
911 ret = utf16s_to_utf8s(report.string, length, UTF16_LITTLE_ENDIAN, buf,
912 PAGE_SIZE - 1);
913 buf[ret++] = '\n';
914 return ret;
915}
916
917#define CP2112_PSTR_ATTR(name, _report) \
Jiri Kosinac3c041b2014-02-17 23:40:20 +0100918static struct cp2112_pstring_attribute dev_attr_##name = { \
David Barksdalee932d812014-02-04 12:42:48 -0600919 .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \
920 .report = _report, \
921};
922
923CP2112_PSTR_ATTR(manufacturer, CP2112_MANUFACTURER_STRING);
924CP2112_PSTR_ATTR(product, CP2112_PRODUCT_STRING);
925CP2112_PSTR_ATTR(serial, CP2112_SERIAL_STRING);
926
927#undef CP2112_PSTR_ATTR
928
929static const struct attribute_group cp2112_attr_group = {
930 .attrs = (struct attribute *[]){
931 &dev_attr_vendor_id.attr,
932 &dev_attr_product_id.attr,
933 &dev_attr_max_power.attr,
934 &dev_attr_power_mode.attr,
935 &dev_attr_release_version.attr,
936 &dev_attr_manufacturer.attr.attr,
937 &dev_attr_product.attr.attr,
938 &dev_attr_serial.attr.attr,
939 NULL
940 }
941};
942
943/* Chmoding our sysfs attributes is simply a way to expose which fields in the
944 * PROM have already been programmed. We do not depend on this preventing
945 * writing to these attributes since the CP2112 will simply ignore writes to
946 * already-programmed fields. This is why there is no sense in fixing this
947 * racy behaviour.
948 */
949static void chmod_sysfs_attrs(struct hid_device *hdev)
950{
951 struct attribute **attr;
952 u8 buf[2];
953 int ret;
954
955 ret = cp2112_hid_get(hdev, CP2112_LOCK_BYTE, buf, sizeof(buf),
956 HID_FEATURE_REPORT);
957 if (ret != sizeof(buf)) {
958 hid_err(hdev, "error reading lock byte: %d\n", ret);
959 return;
960 }
961
962 for (attr = cp2112_attr_group.attrs; *attr; ++attr) {
963 umode_t mode = (buf[1] & 1) ? S_IWUSR | S_IRUGO : S_IRUGO;
964 ret = sysfs_chmod_file(&hdev->dev.kobj, *attr, mode);
965 if (ret < 0)
966 hid_err(hdev, "error chmoding sysfs file %s\n",
967 (*attr)->name);
968 buf[1] >>= 1;
969 }
970}
971
972static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id)
973{
974 struct cp2112_device *dev;
975 u8 buf[3];
976 struct cp2112_smbus_config_report config;
977 int ret;
978
979 ret = hid_parse(hdev);
980 if (ret) {
981 hid_err(hdev, "parse failed\n");
982 return ret;
983 }
984
985 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
986 if (ret) {
987 hid_err(hdev, "hw start failed\n");
988 return ret;
989 }
990
991 ret = hid_hw_open(hdev);
992 if (ret) {
993 hid_err(hdev, "hw open failed\n");
994 goto err_hid_stop;
995 }
996
997 ret = hid_hw_power(hdev, PM_HINT_FULLON);
998 if (ret < 0) {
999 hid_err(hdev, "power management error: %d\n", ret);
1000 goto err_hid_close;
1001 }
1002
1003 ret = cp2112_hid_get(hdev, CP2112_GET_VERSION_INFO, buf, sizeof(buf),
1004 HID_FEATURE_REPORT);
1005 if (ret != sizeof(buf)) {
1006 hid_err(hdev, "error requesting version\n");
1007 if (ret >= 0)
1008 ret = -EIO;
1009 goto err_power_normal;
1010 }
1011
1012 hid_info(hdev, "Part Number: 0x%02X Device Version: 0x%02X\n",
1013 buf[1], buf[2]);
1014
1015 ret = cp2112_hid_get(hdev, CP2112_SMBUS_CONFIG, (u8 *)&config,
1016 sizeof(config), HID_FEATURE_REPORT);
1017 if (ret != sizeof(config)) {
1018 hid_err(hdev, "error requesting SMBus config\n");
1019 if (ret >= 0)
1020 ret = -EIO;
1021 goto err_power_normal;
1022 }
1023
1024 config.retry_time = cpu_to_be16(1);
1025
1026 ret = cp2112_hid_output(hdev, (u8 *)&config, sizeof(config),
1027 HID_FEATURE_REPORT);
1028 if (ret != sizeof(config)) {
1029 hid_err(hdev, "error setting SMBus config\n");
1030 if (ret >= 0)
1031 ret = -EIO;
1032 goto err_power_normal;
1033 }
1034
1035 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1036 if (!dev) {
1037 ret = -ENOMEM;
1038 goto err_power_normal;
1039 }
1040
1041 hid_set_drvdata(hdev, (void *)dev);
1042 dev->hdev = hdev;
1043 dev->adap.owner = THIS_MODULE;
1044 dev->adap.class = I2C_CLASS_HWMON;
1045 dev->adap.algo = &smbus_algorithm;
1046 dev->adap.algo_data = dev;
1047 dev->adap.dev.parent = &hdev->dev;
1048 snprintf(dev->adap.name, sizeof(dev->adap.name),
1049 "CP2112 SMBus Bridge on hiddev%d", hdev->minor);
1050 init_waitqueue_head(&dev->wait);
1051
1052 hid_device_io_start(hdev);
1053 ret = i2c_add_adapter(&dev->adap);
1054 hid_device_io_stop(hdev);
1055
1056 if (ret) {
1057 hid_err(hdev, "error registering i2c adapter\n");
1058 goto err_free_dev;
1059 }
1060
1061 hid_dbg(hdev, "adapter registered\n");
1062
1063 dev->gc.label = "cp2112_gpio";
1064 dev->gc.direction_input = cp2112_gpio_direction_input;
1065 dev->gc.direction_output = cp2112_gpio_direction_output;
1066 dev->gc.set = cp2112_gpio_set;
1067 dev->gc.get = cp2112_gpio_get;
1068 dev->gc.base = -1;
1069 dev->gc.ngpio = 8;
1070 dev->gc.can_sleep = 1;
1071 dev->gc.dev = &hdev->dev;
1072
1073 ret = gpiochip_add(&dev->gc);
1074 if (ret < 0) {
1075 hid_err(hdev, "error registering gpio chip\n");
1076 goto err_free_i2c;
1077 }
1078
1079 ret = sysfs_create_group(&hdev->dev.kobj, &cp2112_attr_group);
1080 if (ret < 0) {
1081 hid_err(hdev, "error creating sysfs attrs\n");
1082 goto err_gpiochip_remove;
1083 }
1084
1085 chmod_sysfs_attrs(hdev);
1086 hid_hw_power(hdev, PM_HINT_NORMAL);
1087
1088 return ret;
1089
1090err_gpiochip_remove:
abdoulaye berthe88d5e522014-07-12 22:30:14 +02001091 gpiochip_remove(&dev->gc);
David Barksdalee932d812014-02-04 12:42:48 -06001092err_free_i2c:
1093 i2c_del_adapter(&dev->adap);
1094err_free_dev:
1095 kfree(dev);
1096err_power_normal:
1097 hid_hw_power(hdev, PM_HINT_NORMAL);
1098err_hid_close:
1099 hid_hw_close(hdev);
1100err_hid_stop:
1101 hid_hw_stop(hdev);
1102 return ret;
1103}
1104
1105static void cp2112_remove(struct hid_device *hdev)
1106{
1107 struct cp2112_device *dev = hid_get_drvdata(hdev);
1108
1109 sysfs_remove_group(&hdev->dev.kobj, &cp2112_attr_group);
abdoulaye berthe88d5e522014-07-12 22:30:14 +02001110 gpiochip_remove(&dev->gc);
David Barksdalee932d812014-02-04 12:42:48 -06001111 i2c_del_adapter(&dev->adap);
1112 /* i2c_del_adapter has finished removing all i2c devices from our
1113 * adapter. Well behaved devices should no longer call our cp2112_xfer
1114 * and should have waited for any pending calls to finish. It has also
1115 * waited for device_unregister(&adap->dev) to complete. Therefore we
1116 * can safely free our struct cp2112_device.
1117 */
1118 hid_hw_close(hdev);
1119 hid_hw_stop(hdev);
1120 kfree(dev);
1121}
1122
1123static int cp2112_raw_event(struct hid_device *hdev, struct hid_report *report,
1124 u8 *data, int size)
1125{
1126 struct cp2112_device *dev = hid_get_drvdata(hdev);
1127 struct cp2112_xfer_status_report *xfer = (void *)data;
1128
1129 switch (data[0]) {
1130 case CP2112_TRANSFER_STATUS_RESPONSE:
1131 hid_dbg(hdev, "xfer status: %02x %02x %04x %04x\n",
1132 xfer->status0, xfer->status1,
1133 be16_to_cpu(xfer->retries), be16_to_cpu(xfer->length));
1134
1135 switch (xfer->status0) {
1136 case STATUS0_IDLE:
1137 dev->xfer_status = -EAGAIN;
1138 break;
1139 case STATUS0_BUSY:
1140 dev->xfer_status = -EBUSY;
1141 break;
1142 case STATUS0_COMPLETE:
1143 dev->xfer_status = be16_to_cpu(xfer->length);
1144 break;
1145 case STATUS0_ERROR:
1146 switch (xfer->status1) {
1147 case STATUS1_TIMEOUT_NACK:
1148 case STATUS1_TIMEOUT_BUS:
1149 dev->xfer_status = -ETIMEDOUT;
1150 break;
1151 default:
1152 dev->xfer_status = -EIO;
1153 break;
1154 }
1155 break;
1156 default:
1157 dev->xfer_status = -EINVAL;
1158 break;
1159 }
1160
1161 atomic_set(&dev->xfer_avail, 1);
1162 break;
1163 case CP2112_DATA_READ_RESPONSE:
1164 hid_dbg(hdev, "read response: %02x %02x\n", data[1], data[2]);
1165
1166 dev->read_length = data[2];
1167 if (dev->read_length > sizeof(dev->read_data))
1168 dev->read_length = sizeof(dev->read_data);
1169
1170 memcpy(dev->read_data, &data[3], dev->read_length);
1171 atomic_set(&dev->read_avail, 1);
1172 break;
1173 default:
1174 hid_err(hdev, "unknown report\n");
1175
1176 return 0;
1177 }
1178
1179 wake_up_interruptible(&dev->wait);
1180 return 1;
1181}
1182
1183static struct hid_driver cp2112_driver = {
1184 .name = "cp2112",
1185 .id_table = cp2112_devices,
1186 .probe = cp2112_probe,
1187 .remove = cp2112_remove,
1188 .raw_event = cp2112_raw_event,
1189};
1190
1191module_hid_driver(cp2112_driver);
1192MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge");
1193MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
1194MODULE_LICENSE("GPL");
1195