blob: 7af77818efc3f3232015c8df9333c367579cd57b [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
Linus Walleij47513c22015-12-08 16:26:28 +010027#include <linux/gpio/driver.h>
David Barksdalee932d812014-02-04 12:42:48 -060028#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
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +010035#define CP2112_REPORT_MAX_LENGTH 64
36#define CP2112_GPIO_CONFIG_LENGTH 5
37#define CP2112_GPIO_GET_LENGTH 2
38#define CP2112_GPIO_SET_LENGTH 3
39
David Barksdalee932d812014-02-04 12:42:48 -060040enum {
41 CP2112_GPIO_CONFIG = 0x02,
42 CP2112_GPIO_GET = 0x03,
43 CP2112_GPIO_SET = 0x04,
44 CP2112_GET_VERSION_INFO = 0x05,
45 CP2112_SMBUS_CONFIG = 0x06,
46 CP2112_DATA_READ_REQUEST = 0x10,
47 CP2112_DATA_WRITE_READ_REQUEST = 0x11,
48 CP2112_DATA_READ_FORCE_SEND = 0x12,
49 CP2112_DATA_READ_RESPONSE = 0x13,
50 CP2112_DATA_WRITE_REQUEST = 0x14,
51 CP2112_TRANSFER_STATUS_REQUEST = 0x15,
52 CP2112_TRANSFER_STATUS_RESPONSE = 0x16,
53 CP2112_CANCEL_TRANSFER = 0x17,
54 CP2112_LOCK_BYTE = 0x20,
55 CP2112_USB_CONFIG = 0x21,
56 CP2112_MANUFACTURER_STRING = 0x22,
57 CP2112_PRODUCT_STRING = 0x23,
58 CP2112_SERIAL_STRING = 0x24,
59};
60
61enum {
62 STATUS0_IDLE = 0x00,
63 STATUS0_BUSY = 0x01,
64 STATUS0_COMPLETE = 0x02,
65 STATUS0_ERROR = 0x03,
66};
67
68enum {
69 STATUS1_TIMEOUT_NACK = 0x00,
70 STATUS1_TIMEOUT_BUS = 0x01,
71 STATUS1_ARBITRATION_LOST = 0x02,
72 STATUS1_READ_INCOMPLETE = 0x03,
73 STATUS1_WRITE_INCOMPLETE = 0x04,
74 STATUS1_SUCCESS = 0x05,
75};
76
77struct cp2112_smbus_config_report {
78 u8 report; /* CP2112_SMBUS_CONFIG */
79 __be32 clock_speed; /* Hz */
80 u8 device_address; /* Stored in the upper 7 bits */
81 u8 auto_send_read; /* 1 = enabled, 0 = disabled */
82 __be16 write_timeout; /* ms, 0 = no timeout */
83 __be16 read_timeout; /* ms, 0 = no timeout */
84 u8 scl_low_timeout; /* 1 = enabled, 0 = disabled */
85 __be16 retry_time; /* # of retries, 0 = no limit */
86} __packed;
87
88struct cp2112_usb_config_report {
89 u8 report; /* CP2112_USB_CONFIG */
90 __le16 vid; /* Vendor ID */
91 __le16 pid; /* Product ID */
92 u8 max_power; /* Power requested in 2mA units */
93 u8 power_mode; /* 0x00 = bus powered
94 0x01 = self powered & regulator off
95 0x02 = self powered & regulator on */
96 u8 release_major;
97 u8 release_minor;
98 u8 mask; /* What fields to program */
99} __packed;
100
101struct cp2112_read_req_report {
102 u8 report; /* CP2112_DATA_READ_REQUEST */
103 u8 slave_address;
104 __be16 length;
105} __packed;
106
107struct cp2112_write_read_req_report {
108 u8 report; /* CP2112_DATA_WRITE_READ_REQUEST */
109 u8 slave_address;
110 __be16 length;
111 u8 target_address_length;
112 u8 target_address[16];
113} __packed;
114
115struct cp2112_write_req_report {
116 u8 report; /* CP2112_DATA_WRITE_REQUEST */
117 u8 slave_address;
118 u8 length;
119 u8 data[61];
120} __packed;
121
122struct cp2112_force_read_report {
123 u8 report; /* CP2112_DATA_READ_FORCE_SEND */
124 __be16 length;
125} __packed;
126
127struct cp2112_xfer_status_report {
128 u8 report; /* CP2112_TRANSFER_STATUS_RESPONSE */
129 u8 status0; /* STATUS0_* */
130 u8 status1; /* STATUS1_* */
131 __be16 retries;
132 __be16 length;
133} __packed;
134
135struct cp2112_string_report {
136 u8 dummy; /* force .string to be aligned */
137 u8 report; /* CP2112_*_STRING */
138 u8 length; /* length in bytes of everyting after .report */
139 u8 type; /* USB_DT_STRING */
140 wchar_t string[30]; /* UTF16_LITTLE_ENDIAN string */
141} __packed;
142
143/* Number of times to request transfer status before giving up waiting for a
144 transfer to complete. This may need to be changed if SMBUS clock, retries,
145 or read/write/scl_low timeout settings are changed. */
146static const int XFER_STATUS_RETRIES = 10;
147
148/* Time in ms to wait for a CP2112_DATA_READ_RESPONSE or
149 CP2112_TRANSFER_STATUS_RESPONSE. */
150static const int RESPONSE_TIMEOUT = 50;
151
152static const struct hid_device_id cp2112_devices[] = {
153 { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_CP2112) },
154 { }
155};
156MODULE_DEVICE_TABLE(hid, cp2112_devices);
157
158struct cp2112_device {
159 struct i2c_adapter adap;
160 struct hid_device *hdev;
161 wait_queue_head_t wait;
162 u8 read_data[61];
163 u8 read_length;
Ellen Wang44eda782015-07-09 21:55:06 -0700164 u8 hwversion;
David Barksdalee932d812014-02-04 12:42:48 -0600165 int xfer_status;
166 atomic_t read_avail;
167 atomic_t xfer_avail;
168 struct gpio_chip gc;
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100169 u8 *in_out_buffer;
Johan Hovolda18c4582017-01-30 11:26:38 +0100170 struct mutex lock;
David Barksdalee932d812014-02-04 12:42:48 -0600171};
172
173static int gpio_push_pull = 0xFF;
174module_param(gpio_push_pull, int, S_IRUGO | S_IWUSR);
175MODULE_PARM_DESC(gpio_push_pull, "GPIO push-pull configuration bitmask");
176
177static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
178{
Linus Walleij47513c22015-12-08 16:26:28 +0100179 struct cp2112_device *dev = gpiochip_get_data(chip);
David Barksdalee932d812014-02-04 12:42:48 -0600180 struct hid_device *hdev = dev->hdev;
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100181 u8 *buf = dev->in_out_buffer;
David Barksdalee932d812014-02-04 12:42:48 -0600182 int ret;
183
Johan Hovolda18c4582017-01-30 11:26:38 +0100184 mutex_lock(&dev->lock);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100185
Jiri Kosina490051a2014-02-18 00:39:39 +0100186 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100187 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
188 HID_REQ_GET_REPORT);
189 if (ret != CP2112_GPIO_CONFIG_LENGTH) {
David Barksdalee932d812014-02-04 12:42:48 -0600190 hid_err(hdev, "error requesting GPIO config: %d\n", ret);
Sébastien Szymanskida733892017-11-10 10:01:43 +0100191 if (ret >= 0)
192 ret = -EIO;
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100193 goto exit;
David Barksdalee932d812014-02-04 12:42:48 -0600194 }
195
196 buf[1] &= ~(1 << offset);
197 buf[2] = gpio_push_pull;
198
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100199 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
200 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
201 HID_REQ_SET_REPORT);
Sébastien Szymanskida733892017-11-10 10:01:43 +0100202 if (ret != CP2112_GPIO_CONFIG_LENGTH) {
David Barksdalee932d812014-02-04 12:42:48 -0600203 hid_err(hdev, "error setting GPIO config: %d\n", ret);
Sébastien Szymanskida733892017-11-10 10:01:43 +0100204 if (ret >= 0)
205 ret = -EIO;
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100206 goto exit;
David Barksdalee932d812014-02-04 12:42:48 -0600207 }
208
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100209 ret = 0;
210
211exit:
Johan Hovolda18c4582017-01-30 11:26:38 +0100212 mutex_unlock(&dev->lock);
Sébastien Szymanskida733892017-11-10 10:01:43 +0100213 return ret;
David Barksdalee932d812014-02-04 12:42:48 -0600214}
215
216static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
217{
Linus Walleij47513c22015-12-08 16:26:28 +0100218 struct cp2112_device *dev = gpiochip_get_data(chip);
David Barksdalee932d812014-02-04 12:42:48 -0600219 struct hid_device *hdev = dev->hdev;
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100220 u8 *buf = dev->in_out_buffer;
David Barksdalee932d812014-02-04 12:42:48 -0600221 int ret;
222
Johan Hovolda18c4582017-01-30 11:26:38 +0100223 mutex_lock(&dev->lock);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100224
David Barksdalee932d812014-02-04 12:42:48 -0600225 buf[0] = CP2112_GPIO_SET;
226 buf[1] = value ? 0xff : 0;
227 buf[2] = 1 << offset;
228
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100229 ret = hid_hw_raw_request(hdev, CP2112_GPIO_SET, buf,
230 CP2112_GPIO_SET_LENGTH, HID_FEATURE_REPORT,
231 HID_REQ_SET_REPORT);
David Barksdalee932d812014-02-04 12:42:48 -0600232 if (ret < 0)
233 hid_err(hdev, "error setting GPIO values: %d\n", ret);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100234
Johan Hovolda18c4582017-01-30 11:26:38 +0100235 mutex_unlock(&dev->lock);
David Barksdalee932d812014-02-04 12:42:48 -0600236}
237
238static int cp2112_gpio_get(struct gpio_chip *chip, unsigned offset)
239{
Linus Walleij47513c22015-12-08 16:26:28 +0100240 struct cp2112_device *dev = gpiochip_get_data(chip);
David Barksdalee932d812014-02-04 12:42:48 -0600241 struct hid_device *hdev = dev->hdev;
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100242 u8 *buf = dev->in_out_buffer;
David Barksdalee932d812014-02-04 12:42:48 -0600243 int ret;
244
Johan Hovolda18c4582017-01-30 11:26:38 +0100245 mutex_lock(&dev->lock);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100246
247 ret = hid_hw_raw_request(hdev, CP2112_GPIO_GET, buf,
248 CP2112_GPIO_GET_LENGTH, HID_FEATURE_REPORT,
249 HID_REQ_GET_REPORT);
250 if (ret != CP2112_GPIO_GET_LENGTH) {
David Barksdalee932d812014-02-04 12:42:48 -0600251 hid_err(hdev, "error requesting GPIO values: %d\n", ret);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100252 ret = ret < 0 ? ret : -EIO;
253 goto exit;
David Barksdalee932d812014-02-04 12:42:48 -0600254 }
255
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100256 ret = (buf[1] >> offset) & 1;
257
258exit:
Johan Hovolda18c4582017-01-30 11:26:38 +0100259 mutex_unlock(&dev->lock);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100260
261 return ret;
David Barksdalee932d812014-02-04 12:42:48 -0600262}
263
264static int cp2112_gpio_direction_output(struct gpio_chip *chip,
265 unsigned offset, int value)
266{
Linus Walleij47513c22015-12-08 16:26:28 +0100267 struct cp2112_device *dev = gpiochip_get_data(chip);
David Barksdalee932d812014-02-04 12:42:48 -0600268 struct hid_device *hdev = dev->hdev;
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100269 u8 *buf = dev->in_out_buffer;
David Barksdalee932d812014-02-04 12:42:48 -0600270 int ret;
271
Johan Hovolda18c4582017-01-30 11:26:38 +0100272 mutex_lock(&dev->lock);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100273
Jiri Kosina490051a2014-02-18 00:39:39 +0100274 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100275 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
276 HID_REQ_GET_REPORT);
277 if (ret != CP2112_GPIO_CONFIG_LENGTH) {
David Barksdalee932d812014-02-04 12:42:48 -0600278 hid_err(hdev, "error requesting GPIO config: %d\n", ret);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100279 goto fail;
David Barksdalee932d812014-02-04 12:42:48 -0600280 }
281
282 buf[1] |= 1 << offset;
283 buf[2] = gpio_push_pull;
284
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100285 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
286 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
287 HID_REQ_SET_REPORT);
David Barksdalee932d812014-02-04 12:42:48 -0600288 if (ret < 0) {
289 hid_err(hdev, "error setting GPIO config: %d\n", ret);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100290 goto fail;
David Barksdalee932d812014-02-04 12:42:48 -0600291 }
292
Johan Hovolda18c4582017-01-30 11:26:38 +0100293 mutex_unlock(&dev->lock);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100294
Antonio Borneobeb9d002014-06-29 14:13:48 +0800295 /*
296 * Set gpio value when output direction is already set,
297 * as specified in AN495, Rev. 0.2, cpt. 4.4
298 */
299 cp2112_gpio_set(chip, offset, value);
300
David Barksdalee932d812014-02-04 12:42:48 -0600301 return 0;
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100302
303fail:
Johan Hovolda18c4582017-01-30 11:26:38 +0100304 mutex_unlock(&dev->lock);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100305 return ret < 0 ? ret : -EIO;
David Barksdalee932d812014-02-04 12:42:48 -0600306}
307
308static int cp2112_hid_get(struct hid_device *hdev, unsigned char report_number,
309 u8 *data, size_t count, unsigned char report_type)
310{
311 u8 *buf;
312 int ret;
313
314 buf = kmalloc(count, GFP_KERNEL);
315 if (!buf)
316 return -ENOMEM;
317
Jiri Kosina490051a2014-02-18 00:39:39 +0100318 ret = hid_hw_raw_request(hdev, report_number, buf, count,
319 report_type, HID_REQ_GET_REPORT);
David Barksdalee932d812014-02-04 12:42:48 -0600320 memcpy(data, buf, count);
321 kfree(buf);
322 return ret;
323}
324
325static int cp2112_hid_output(struct hid_device *hdev, u8 *data, size_t count,
326 unsigned char report_type)
327{
328 u8 *buf;
329 int ret;
330
331 buf = kmemdup(data, count, GFP_KERNEL);
332 if (!buf)
333 return -ENOMEM;
334
Benjamin Tissoires866e4792014-03-08 22:52:41 -0500335 if (report_type == HID_OUTPUT_REPORT)
336 ret = hid_hw_output_report(hdev, buf, count);
337 else
338 ret = hid_hw_raw_request(hdev, buf[0], buf, count, report_type,
339 HID_REQ_SET_REPORT);
340
David Barksdalee932d812014-02-04 12:42:48 -0600341 kfree(buf);
342 return ret;
343}
344
345static int cp2112_wait(struct cp2112_device *dev, atomic_t *avail)
346{
347 int ret = 0;
348
349 /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a
350 * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to
351 * come in cp2112_raw_event or timeout. There will only be one of these
352 * in flight at any one time. The timeout is extremely large and is a
353 * last resort if the CP2112 has died. If we do timeout we don't expect
354 * to receive the response which would cause data races, it's not like
355 * we can do anything about it anyway.
356 */
357 ret = wait_event_interruptible_timeout(dev->wait,
358 atomic_read(avail), msecs_to_jiffies(RESPONSE_TIMEOUT));
359 if (-ERESTARTSYS == ret)
360 return ret;
361 if (!ret)
362 return -ETIMEDOUT;
363
364 atomic_set(avail, 0);
365 return 0;
366}
367
368static int cp2112_xfer_status(struct cp2112_device *dev)
369{
370 struct hid_device *hdev = dev->hdev;
371 u8 buf[2];
372 int ret;
373
374 buf[0] = CP2112_TRANSFER_STATUS_REQUEST;
375 buf[1] = 0x01;
376 atomic_set(&dev->xfer_avail, 0);
377
378 ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
379 if (ret < 0) {
380 hid_warn(hdev, "Error requesting status: %d\n", ret);
381 return ret;
382 }
383
384 ret = cp2112_wait(dev, &dev->xfer_avail);
385 if (ret)
386 return ret;
387
388 return dev->xfer_status;
389}
390
391static int cp2112_read(struct cp2112_device *dev, u8 *data, size_t size)
392{
393 struct hid_device *hdev = dev->hdev;
394 struct cp2112_force_read_report report;
395 int ret;
396
Antonio Borneo6debce62015-06-21 14:20:25 +0800397 if (size > sizeof(dev->read_data))
398 size = sizeof(dev->read_data);
David Barksdalee932d812014-02-04 12:42:48 -0600399 report.report = CP2112_DATA_READ_FORCE_SEND;
400 report.length = cpu_to_be16(size);
401
402 atomic_set(&dev->read_avail, 0);
403
404 ret = cp2112_hid_output(hdev, &report.report, sizeof(report),
405 HID_OUTPUT_REPORT);
406 if (ret < 0) {
407 hid_warn(hdev, "Error requesting data: %d\n", ret);
408 return ret;
409 }
410
411 ret = cp2112_wait(dev, &dev->read_avail);
412 if (ret)
413 return ret;
414
Jiri Kosina5a673fc2014-02-17 23:44:54 +0100415 hid_dbg(hdev, "read %d of %zd bytes requested\n",
David Barksdalee932d812014-02-04 12:42:48 -0600416 dev->read_length, size);
417
418 if (size > dev->read_length)
419 size = dev->read_length;
420
421 memcpy(data, dev->read_data, size);
422 return dev->read_length;
423}
424
425static int cp2112_read_req(void *buf, u8 slave_address, u16 length)
426{
427 struct cp2112_read_req_report *report = buf;
428
429 if (length < 1 || length > 512)
430 return -EINVAL;
431
432 report->report = CP2112_DATA_READ_REQUEST;
433 report->slave_address = slave_address << 1;
434 report->length = cpu_to_be16(length);
435 return sizeof(*report);
436}
437
438static int cp2112_write_read_req(void *buf, u8 slave_address, u16 length,
439 u8 command, u8 *data, u8 data_length)
440{
441 struct cp2112_write_read_req_report *report = buf;
442
443 if (length < 1 || length > 512
444 || data_length > sizeof(report->target_address) - 1)
445 return -EINVAL;
446
447 report->report = CP2112_DATA_WRITE_READ_REQUEST;
448 report->slave_address = slave_address << 1;
449 report->length = cpu_to_be16(length);
450 report->target_address_length = data_length + 1;
451 report->target_address[0] = command;
452 memcpy(&report->target_address[1], data, data_length);
453 return data_length + 6;
454}
455
456static int cp2112_write_req(void *buf, u8 slave_address, u8 command, u8 *data,
457 u8 data_length)
458{
459 struct cp2112_write_req_report *report = buf;
460
461 if (data_length > sizeof(report->data) - 1)
462 return -EINVAL;
463
464 report->report = CP2112_DATA_WRITE_REQUEST;
465 report->slave_address = slave_address << 1;
466 report->length = data_length + 1;
467 report->data[0] = command;
468 memcpy(&report->data[1], data, data_length);
469 return data_length + 4;
470}
471
Antonio Borneob9029342014-07-08 07:25:39 +0800472static int cp2112_i2c_write_req(void *buf, u8 slave_address, u8 *data,
473 u8 data_length)
474{
475 struct cp2112_write_req_report *report = buf;
476
477 if (data_length > sizeof(report->data))
478 return -EINVAL;
479
480 report->report = CP2112_DATA_WRITE_REQUEST;
481 report->slave_address = slave_address << 1;
482 report->length = data_length;
483 memcpy(report->data, data, data_length);
484 return data_length + 3;
485}
486
Ellen Wang44eda782015-07-09 21:55:06 -0700487static int cp2112_i2c_write_read_req(void *buf, u8 slave_address,
488 u8 *addr, int addr_length,
489 int read_length)
490{
491 struct cp2112_write_read_req_report *report = buf;
492
493 if (read_length < 1 || read_length > 512 ||
494 addr_length > sizeof(report->target_address))
495 return -EINVAL;
496
497 report->report = CP2112_DATA_WRITE_READ_REQUEST;
498 report->slave_address = slave_address << 1;
499 report->length = cpu_to_be16(read_length);
500 report->target_address_length = addr_length;
501 memcpy(report->target_address, addr, addr_length);
502 return addr_length + 5;
503}
504
Antonio Borneob9029342014-07-08 07:25:39 +0800505static int cp2112_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
506 int num)
507{
508 struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data;
509 struct hid_device *hdev = dev->hdev;
510 u8 buf[64];
511 ssize_t count;
Ellen Wang44eda782015-07-09 21:55:06 -0700512 ssize_t read_length = 0;
513 u8 *read_buf = NULL;
Antonio Borneob9029342014-07-08 07:25:39 +0800514 unsigned int retries;
515 int ret;
516
517 hid_dbg(hdev, "I2C %d messages\n", num);
518
Ellen Wang44eda782015-07-09 21:55:06 -0700519 if (num == 1) {
520 if (msgs->flags & I2C_M_RD) {
521 hid_dbg(hdev, "I2C read %#04x len %d\n",
522 msgs->addr, msgs->len);
523 read_length = msgs->len;
524 read_buf = msgs->buf;
525 count = cp2112_read_req(buf, msgs->addr, msgs->len);
526 } else {
527 hid_dbg(hdev, "I2C write %#04x len %d\n",
528 msgs->addr, msgs->len);
529 count = cp2112_i2c_write_req(buf, msgs->addr,
530 msgs->buf, msgs->len);
531 }
532 if (count < 0)
533 return count;
534 } else if (dev->hwversion > 1 && /* no repeated start in rev 1 */
535 num == 2 &&
536 msgs[0].addr == msgs[1].addr &&
537 !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) {
538 hid_dbg(hdev, "I2C write-read %#04x wlen %d rlen %d\n",
539 msgs[0].addr, msgs[0].len, msgs[1].len);
540 read_length = msgs[1].len;
541 read_buf = msgs[1].buf;
542 count = cp2112_i2c_write_read_req(buf, msgs[0].addr,
543 msgs[0].buf, msgs[0].len, msgs[1].len);
544 if (count < 0)
545 return count;
546 } else {
Antonio Borneob9029342014-07-08 07:25:39 +0800547 hid_err(hdev,
548 "Multi-message I2C transactions not supported\n");
549 return -EOPNOTSUPP;
550 }
551
Antonio Borneob9029342014-07-08 07:25:39 +0800552 ret = hid_hw_power(hdev, PM_HINT_FULLON);
553 if (ret < 0) {
554 hid_err(hdev, "power management error: %d\n", ret);
555 return ret;
556 }
557
558 ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT);
559 if (ret < 0) {
560 hid_warn(hdev, "Error starting transaction: %d\n", ret);
561 goto power_normal;
562 }
563
564 for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) {
565 ret = cp2112_xfer_status(dev);
566 if (-EBUSY == ret)
567 continue;
568 if (ret < 0)
569 goto power_normal;
570 break;
571 }
572
573 if (XFER_STATUS_RETRIES <= retries) {
574 hid_warn(hdev, "Transfer timed out, cancelling.\n");
575 buf[0] = CP2112_CANCEL_TRANSFER;
576 buf[1] = 0x01;
577
578 ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
579 if (ret < 0)
580 hid_warn(hdev, "Error cancelling transaction: %d\n",
581 ret);
582
583 ret = -ETIMEDOUT;
584 goto power_normal;
585 }
586
Ellen Wang44eda782015-07-09 21:55:06 -0700587 for (count = 0; count < read_length;) {
588 ret = cp2112_read(dev, read_buf + count, read_length - count);
Ellen Wang5ddfb122015-07-08 11:17:39 -0700589 if (ret < 0)
590 goto power_normal;
591 if (ret == 0) {
592 hid_err(hdev, "read returned 0\n");
593 ret = -EIO;
594 goto power_normal;
595 }
596 count += ret;
Ellen Wang44eda782015-07-09 21:55:06 -0700597 if (count > read_length) {
Ellen Wang5ddfb122015-07-08 11:17:39 -0700598 /*
599 * The hardware returned too much data.
600 * This is mostly harmless because cp2112_read()
601 * has a limit check so didn't overrun our
602 * buffer. Nevertheless, we return an error
603 * because something is seriously wrong and
604 * it shouldn't go unnoticed.
605 */
606 hid_err(hdev, "long read: %d > %zd\n",
Ellen Wang44eda782015-07-09 21:55:06 -0700607 ret, read_length - count + ret);
Ellen Wang5ddfb122015-07-08 11:17:39 -0700608 ret = -EIO;
609 goto power_normal;
610 }
Antonio Borneob9029342014-07-08 07:25:39 +0800611 }
612
Antonio Borneob9029342014-07-08 07:25:39 +0800613 /* return the number of transferred messages */
Ellen Wang44eda782015-07-09 21:55:06 -0700614 ret = num;
Antonio Borneob9029342014-07-08 07:25:39 +0800615
616power_normal:
617 hid_hw_power(hdev, PM_HINT_NORMAL);
618 hid_dbg(hdev, "I2C transfer finished: %d\n", ret);
619 return ret;
620}
621
David Barksdalee932d812014-02-04 12:42:48 -0600622static int cp2112_xfer(struct i2c_adapter *adap, u16 addr,
623 unsigned short flags, char read_write, u8 command,
624 int size, union i2c_smbus_data *data)
625{
626 struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data;
627 struct hid_device *hdev = dev->hdev;
628 u8 buf[64];
Ellen Wang29e2d6d2015-07-09 22:04:31 -0700629 __le16 word;
Jiri Kosina0438ee72014-02-18 09:43:53 +0100630 ssize_t count;
David Barksdalee932d812014-02-04 12:42:48 -0600631 size_t read_length = 0;
632 unsigned int retries;
633 int ret;
634
635 hid_dbg(hdev, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n",
636 read_write == I2C_SMBUS_WRITE ? "write" : "read",
637 addr, flags, command, size);
638
639 switch (size) {
640 case I2C_SMBUS_BYTE:
641 read_length = 1;
642
643 if (I2C_SMBUS_READ == read_write)
644 count = cp2112_read_req(buf, addr, read_length);
645 else
Ellen Wang6d00d152015-07-13 15:23:54 -0700646 count = cp2112_write_req(buf, addr, command, NULL,
David Barksdalee932d812014-02-04 12:42:48 -0600647 0);
648 break;
649 case I2C_SMBUS_BYTE_DATA:
650 read_length = 1;
651
652 if (I2C_SMBUS_READ == read_write)
653 count = cp2112_write_read_req(buf, addr, read_length,
654 command, NULL, 0);
655 else
656 count = cp2112_write_req(buf, addr, command,
657 &data->byte, 1);
658 break;
659 case I2C_SMBUS_WORD_DATA:
660 read_length = 2;
Ellen Wang29e2d6d2015-07-09 22:04:31 -0700661 word = cpu_to_le16(data->word);
David Barksdalee932d812014-02-04 12:42:48 -0600662
663 if (I2C_SMBUS_READ == read_write)
664 count = cp2112_write_read_req(buf, addr, read_length,
665 command, NULL, 0);
666 else
667 count = cp2112_write_req(buf, addr, command,
668 (u8 *)&word, 2);
669 break;
670 case I2C_SMBUS_PROC_CALL:
671 size = I2C_SMBUS_WORD_DATA;
672 read_write = I2C_SMBUS_READ;
673 read_length = 2;
Ellen Wang29e2d6d2015-07-09 22:04:31 -0700674 word = cpu_to_le16(data->word);
David Barksdalee932d812014-02-04 12:42:48 -0600675
676 count = cp2112_write_read_req(buf, addr, read_length, command,
677 (u8 *)&word, 2);
678 break;
679 case I2C_SMBUS_I2C_BLOCK_DATA:
680 size = I2C_SMBUS_BLOCK_DATA;
681 /* fallthrough */
682 case I2C_SMBUS_BLOCK_DATA:
683 if (I2C_SMBUS_READ == read_write) {
684 count = cp2112_write_read_req(buf, addr,
685 I2C_SMBUS_BLOCK_MAX,
686 command, NULL, 0);
687 } else {
688 count = cp2112_write_req(buf, addr, command,
689 data->block,
690 data->block[0] + 1);
691 }
692 break;
693 case I2C_SMBUS_BLOCK_PROC_CALL:
694 size = I2C_SMBUS_BLOCK_DATA;
695 read_write = I2C_SMBUS_READ;
696
697 count = cp2112_write_read_req(buf, addr, I2C_SMBUS_BLOCK_MAX,
698 command, data->block,
699 data->block[0] + 1);
700 break;
701 default:
702 hid_warn(hdev, "Unsupported transaction %d\n", size);
703 return -EOPNOTSUPP;
704 }
705
706 if (count < 0)
707 return count;
708
709 ret = hid_hw_power(hdev, PM_HINT_FULLON);
710 if (ret < 0) {
711 hid_err(hdev, "power management error: %d\n", ret);
712 return ret;
713 }
714
715 ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT);
716 if (ret < 0) {
717 hid_warn(hdev, "Error starting transaction: %d\n", ret);
718 goto power_normal;
719 }
720
721 for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) {
722 ret = cp2112_xfer_status(dev);
723 if (-EBUSY == ret)
724 continue;
725 if (ret < 0)
726 goto power_normal;
727 break;
728 }
729
730 if (XFER_STATUS_RETRIES <= retries) {
731 hid_warn(hdev, "Transfer timed out, cancelling.\n");
732 buf[0] = CP2112_CANCEL_TRANSFER;
733 buf[1] = 0x01;
734
735 ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
736 if (ret < 0)
737 hid_warn(hdev, "Error cancelling transaction: %d\n",
738 ret);
739
740 ret = -ETIMEDOUT;
741 goto power_normal;
742 }
743
744 if (I2C_SMBUS_WRITE == read_write) {
745 ret = 0;
746 goto power_normal;
747 }
748
749 if (I2C_SMBUS_BLOCK_DATA == size)
750 read_length = ret;
751
752 ret = cp2112_read(dev, buf, read_length);
753 if (ret < 0)
754 goto power_normal;
755 if (ret != read_length) {
Jiri Kosina5a673fc2014-02-17 23:44:54 +0100756 hid_warn(hdev, "short read: %d < %zd\n", ret, read_length);
David Barksdalee932d812014-02-04 12:42:48 -0600757 ret = -EIO;
758 goto power_normal;
759 }
760
761 switch (size) {
762 case I2C_SMBUS_BYTE:
763 case I2C_SMBUS_BYTE_DATA:
764 data->byte = buf[0];
765 break;
766 case I2C_SMBUS_WORD_DATA:
Ellen Wang29e2d6d2015-07-09 22:04:31 -0700767 data->word = le16_to_cpup((__le16 *)buf);
David Barksdalee932d812014-02-04 12:42:48 -0600768 break;
769 case I2C_SMBUS_BLOCK_DATA:
770 if (read_length > I2C_SMBUS_BLOCK_MAX) {
771 ret = -EPROTO;
772 goto power_normal;
773 }
774
775 memcpy(data->block, buf, read_length);
776 break;
777 }
778
779 ret = 0;
780power_normal:
781 hid_hw_power(hdev, PM_HINT_NORMAL);
782 hid_dbg(hdev, "transfer finished: %d\n", ret);
783 return ret;
784}
785
786static u32 cp2112_functionality(struct i2c_adapter *adap)
787{
Antonio Borneob9029342014-07-08 07:25:39 +0800788 return I2C_FUNC_I2C |
789 I2C_FUNC_SMBUS_BYTE |
David Barksdalee932d812014-02-04 12:42:48 -0600790 I2C_FUNC_SMBUS_BYTE_DATA |
791 I2C_FUNC_SMBUS_WORD_DATA |
792 I2C_FUNC_SMBUS_BLOCK_DATA |
793 I2C_FUNC_SMBUS_I2C_BLOCK |
794 I2C_FUNC_SMBUS_PROC_CALL |
795 I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
796}
797
798static const struct i2c_algorithm smbus_algorithm = {
Antonio Borneob9029342014-07-08 07:25:39 +0800799 .master_xfer = cp2112_i2c_xfer,
David Barksdalee932d812014-02-04 12:42:48 -0600800 .smbus_xfer = cp2112_xfer,
801 .functionality = cp2112_functionality,
802};
803
804static int cp2112_get_usb_config(struct hid_device *hdev,
805 struct cp2112_usb_config_report *cfg)
806{
807 int ret;
808
809 ret = cp2112_hid_get(hdev, CP2112_USB_CONFIG, (u8 *)cfg, sizeof(*cfg),
810 HID_FEATURE_REPORT);
811 if (ret != sizeof(*cfg)) {
812 hid_err(hdev, "error reading usb config: %d\n", ret);
813 if (ret < 0)
814 return ret;
815 return -EIO;
816 }
817
818 return 0;
819}
820
821static int cp2112_set_usb_config(struct hid_device *hdev,
822 struct cp2112_usb_config_report *cfg)
823{
824 int ret;
825
826 BUG_ON(cfg->report != CP2112_USB_CONFIG);
827
828 ret = cp2112_hid_output(hdev, (u8 *)cfg, sizeof(*cfg),
829 HID_FEATURE_REPORT);
830 if (ret != sizeof(*cfg)) {
831 hid_err(hdev, "error writing usb config: %d\n", ret);
832 if (ret < 0)
833 return ret;
834 return -EIO;
835 }
836
837 return 0;
838}
839
840static void chmod_sysfs_attrs(struct hid_device *hdev);
841
842#define CP2112_CONFIG_ATTR(name, store, format, ...) \
843static ssize_t name##_store(struct device *kdev, \
844 struct device_attribute *attr, const char *buf, \
845 size_t count) \
846{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +0800847 struct hid_device *hdev = to_hid_device(kdev); \
David Barksdalee932d812014-02-04 12:42:48 -0600848 struct cp2112_usb_config_report cfg; \
849 int ret = cp2112_get_usb_config(hdev, &cfg); \
850 if (ret) \
851 return ret; \
852 store; \
853 ret = cp2112_set_usb_config(hdev, &cfg); \
854 if (ret) \
855 return ret; \
856 chmod_sysfs_attrs(hdev); \
857 return count; \
858} \
859static ssize_t name##_show(struct device *kdev, \
860 struct device_attribute *attr, char *buf) \
861{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +0800862 struct hid_device *hdev = to_hid_device(kdev); \
David Barksdalee932d812014-02-04 12:42:48 -0600863 struct cp2112_usb_config_report cfg; \
864 int ret = cp2112_get_usb_config(hdev, &cfg); \
865 if (ret) \
866 return ret; \
867 return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \
868} \
Jiri Kosinac3c041b2014-02-17 23:40:20 +0100869static DEVICE_ATTR_RW(name);
David Barksdalee932d812014-02-04 12:42:48 -0600870
871CP2112_CONFIG_ATTR(vendor_id, ({
872 u16 vid;
873
874 if (sscanf(buf, "%hi", &vid) != 1)
875 return -EINVAL;
876
877 cfg.vid = cpu_to_le16(vid);
878 cfg.mask = 0x01;
879}), "0x%04x\n", le16_to_cpu(cfg.vid));
880
881CP2112_CONFIG_ATTR(product_id, ({
882 u16 pid;
883
884 if (sscanf(buf, "%hi", &pid) != 1)
885 return -EINVAL;
886
887 cfg.pid = cpu_to_le16(pid);
888 cfg.mask = 0x02;
889}), "0x%04x\n", le16_to_cpu(cfg.pid));
890
891CP2112_CONFIG_ATTR(max_power, ({
892 int mA;
893
894 if (sscanf(buf, "%i", &mA) != 1)
895 return -EINVAL;
896
897 cfg.max_power = (mA + 1) / 2;
898 cfg.mask = 0x04;
899}), "%u mA\n", cfg.max_power * 2);
900
901CP2112_CONFIG_ATTR(power_mode, ({
902 if (sscanf(buf, "%hhi", &cfg.power_mode) != 1)
903 return -EINVAL;
904
905 cfg.mask = 0x08;
906}), "%u\n", cfg.power_mode);
907
908CP2112_CONFIG_ATTR(release_version, ({
909 if (sscanf(buf, "%hhi.%hhi", &cfg.release_major, &cfg.release_minor)
910 != 2)
911 return -EINVAL;
912
913 cfg.mask = 0x10;
914}), "%u.%u\n", cfg.release_major, cfg.release_minor);
915
916#undef CP2112_CONFIG_ATTR
917
918struct cp2112_pstring_attribute {
919 struct device_attribute attr;
920 unsigned char report;
921};
922
923static ssize_t pstr_store(struct device *kdev,
924 struct device_attribute *kattr, const char *buf,
925 size_t count)
926{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800927 struct hid_device *hdev = to_hid_device(kdev);
David Barksdalee932d812014-02-04 12:42:48 -0600928 struct cp2112_pstring_attribute *attr =
929 container_of(kattr, struct cp2112_pstring_attribute, attr);
930 struct cp2112_string_report report;
931 int ret;
932
933 memset(&report, 0, sizeof(report));
934
935 ret = utf8s_to_utf16s(buf, count, UTF16_LITTLE_ENDIAN,
936 report.string, ARRAY_SIZE(report.string));
937 report.report = attr->report;
938 report.length = ret * sizeof(report.string[0]) + 2;
939 report.type = USB_DT_STRING;
940
941 ret = cp2112_hid_output(hdev, &report.report, report.length + 1,
942 HID_FEATURE_REPORT);
943 if (ret != report.length + 1) {
944 hid_err(hdev, "error writing %s string: %d\n", kattr->attr.name,
945 ret);
946 if (ret < 0)
947 return ret;
948 return -EIO;
949 }
950
951 chmod_sysfs_attrs(hdev);
952 return count;
953}
954
955static ssize_t pstr_show(struct device *kdev,
956 struct device_attribute *kattr, char *buf)
957{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800958 struct hid_device *hdev = to_hid_device(kdev);
David Barksdalee932d812014-02-04 12:42:48 -0600959 struct cp2112_pstring_attribute *attr =
960 container_of(kattr, struct cp2112_pstring_attribute, attr);
961 struct cp2112_string_report report;
962 u8 length;
963 int ret;
964
965 ret = cp2112_hid_get(hdev, attr->report, &report.report,
966 sizeof(report) - 1, HID_FEATURE_REPORT);
967 if (ret < 3) {
968 hid_err(hdev, "error reading %s string: %d\n", kattr->attr.name,
969 ret);
970 if (ret < 0)
971 return ret;
972 return -EIO;
973 }
974
975 if (report.length < 2) {
976 hid_err(hdev, "invalid %s string length: %d\n",
977 kattr->attr.name, report.length);
978 return -EIO;
979 }
980
981 length = report.length > ret - 1 ? ret - 1 : report.length;
982 length = (length - 2) / sizeof(report.string[0]);
983 ret = utf16s_to_utf8s(report.string, length, UTF16_LITTLE_ENDIAN, buf,
984 PAGE_SIZE - 1);
985 buf[ret++] = '\n';
986 return ret;
987}
988
989#define CP2112_PSTR_ATTR(name, _report) \
Jiri Kosinac3c041b2014-02-17 23:40:20 +0100990static struct cp2112_pstring_attribute dev_attr_##name = { \
David Barksdalee932d812014-02-04 12:42:48 -0600991 .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \
992 .report = _report, \
993};
994
995CP2112_PSTR_ATTR(manufacturer, CP2112_MANUFACTURER_STRING);
996CP2112_PSTR_ATTR(product, CP2112_PRODUCT_STRING);
997CP2112_PSTR_ATTR(serial, CP2112_SERIAL_STRING);
998
999#undef CP2112_PSTR_ATTR
1000
1001static const struct attribute_group cp2112_attr_group = {
1002 .attrs = (struct attribute *[]){
1003 &dev_attr_vendor_id.attr,
1004 &dev_attr_product_id.attr,
1005 &dev_attr_max_power.attr,
1006 &dev_attr_power_mode.attr,
1007 &dev_attr_release_version.attr,
1008 &dev_attr_manufacturer.attr.attr,
1009 &dev_attr_product.attr.attr,
1010 &dev_attr_serial.attr.attr,
1011 NULL
1012 }
1013};
1014
1015/* Chmoding our sysfs attributes is simply a way to expose which fields in the
1016 * PROM have already been programmed. We do not depend on this preventing
1017 * writing to these attributes since the CP2112 will simply ignore writes to
1018 * already-programmed fields. This is why there is no sense in fixing this
1019 * racy behaviour.
1020 */
1021static void chmod_sysfs_attrs(struct hid_device *hdev)
1022{
1023 struct attribute **attr;
1024 u8 buf[2];
1025 int ret;
1026
1027 ret = cp2112_hid_get(hdev, CP2112_LOCK_BYTE, buf, sizeof(buf),
1028 HID_FEATURE_REPORT);
1029 if (ret != sizeof(buf)) {
1030 hid_err(hdev, "error reading lock byte: %d\n", ret);
1031 return;
1032 }
1033
1034 for (attr = cp2112_attr_group.attrs; *attr; ++attr) {
1035 umode_t mode = (buf[1] & 1) ? S_IWUSR | S_IRUGO : S_IRUGO;
1036 ret = sysfs_chmod_file(&hdev->dev.kobj, *attr, mode);
1037 if (ret < 0)
1038 hid_err(hdev, "error chmoding sysfs file %s\n",
1039 (*attr)->name);
1040 buf[1] >>= 1;
1041 }
1042}
1043
1044static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id)
1045{
1046 struct cp2112_device *dev;
1047 u8 buf[3];
1048 struct cp2112_smbus_config_report config;
1049 int ret;
1050
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +01001051 dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL);
1052 if (!dev)
1053 return -ENOMEM;
1054
1055 dev->in_out_buffer = devm_kzalloc(&hdev->dev, CP2112_REPORT_MAX_LENGTH,
1056 GFP_KERNEL);
1057 if (!dev->in_out_buffer)
1058 return -ENOMEM;
1059
Johan Hovolda18c4582017-01-30 11:26:38 +01001060 mutex_init(&dev->lock);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +01001061
David Barksdalee932d812014-02-04 12:42:48 -06001062 ret = hid_parse(hdev);
1063 if (ret) {
1064 hid_err(hdev, "parse failed\n");
1065 return ret;
1066 }
1067
1068 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1069 if (ret) {
1070 hid_err(hdev, "hw start failed\n");
1071 return ret;
1072 }
1073
1074 ret = hid_hw_open(hdev);
1075 if (ret) {
1076 hid_err(hdev, "hw open failed\n");
1077 goto err_hid_stop;
1078 }
1079
1080 ret = hid_hw_power(hdev, PM_HINT_FULLON);
1081 if (ret < 0) {
1082 hid_err(hdev, "power management error: %d\n", ret);
1083 goto err_hid_close;
1084 }
1085
1086 ret = cp2112_hid_get(hdev, CP2112_GET_VERSION_INFO, buf, sizeof(buf),
1087 HID_FEATURE_REPORT);
1088 if (ret != sizeof(buf)) {
1089 hid_err(hdev, "error requesting version\n");
1090 if (ret >= 0)
1091 ret = -EIO;
1092 goto err_power_normal;
1093 }
1094
1095 hid_info(hdev, "Part Number: 0x%02X Device Version: 0x%02X\n",
1096 buf[1], buf[2]);
1097
1098 ret = cp2112_hid_get(hdev, CP2112_SMBUS_CONFIG, (u8 *)&config,
1099 sizeof(config), HID_FEATURE_REPORT);
1100 if (ret != sizeof(config)) {
1101 hid_err(hdev, "error requesting SMBus config\n");
1102 if (ret >= 0)
1103 ret = -EIO;
1104 goto err_power_normal;
1105 }
1106
1107 config.retry_time = cpu_to_be16(1);
1108
1109 ret = cp2112_hid_output(hdev, (u8 *)&config, sizeof(config),
1110 HID_FEATURE_REPORT);
1111 if (ret != sizeof(config)) {
1112 hid_err(hdev, "error setting SMBus config\n");
1113 if (ret >= 0)
1114 ret = -EIO;
1115 goto err_power_normal;
1116 }
1117
David Barksdalee932d812014-02-04 12:42:48 -06001118 hid_set_drvdata(hdev, (void *)dev);
1119 dev->hdev = hdev;
1120 dev->adap.owner = THIS_MODULE;
1121 dev->adap.class = I2C_CLASS_HWMON;
1122 dev->adap.algo = &smbus_algorithm;
1123 dev->adap.algo_data = dev;
1124 dev->adap.dev.parent = &hdev->dev;
1125 snprintf(dev->adap.name, sizeof(dev->adap.name),
1126 "CP2112 SMBus Bridge on hiddev%d", hdev->minor);
Ellen Wang44eda782015-07-09 21:55:06 -07001127 dev->hwversion = buf[2];
David Barksdalee932d812014-02-04 12:42:48 -06001128 init_waitqueue_head(&dev->wait);
1129
1130 hid_device_io_start(hdev);
1131 ret = i2c_add_adapter(&dev->adap);
1132 hid_device_io_stop(hdev);
1133
1134 if (ret) {
1135 hid_err(hdev, "error registering i2c adapter\n");
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +01001136 goto err_power_normal;
David Barksdalee932d812014-02-04 12:42:48 -06001137 }
1138
1139 hid_dbg(hdev, "adapter registered\n");
1140
1141 dev->gc.label = "cp2112_gpio";
1142 dev->gc.direction_input = cp2112_gpio_direction_input;
1143 dev->gc.direction_output = cp2112_gpio_direction_output;
1144 dev->gc.set = cp2112_gpio_set;
1145 dev->gc.get = cp2112_gpio_get;
1146 dev->gc.base = -1;
1147 dev->gc.ngpio = 8;
1148 dev->gc.can_sleep = 1;
Linus Walleij58383c72015-11-04 09:56:26 +01001149 dev->gc.parent = &hdev->dev;
David Barksdalee932d812014-02-04 12:42:48 -06001150
Linus Walleij47513c22015-12-08 16:26:28 +01001151 ret = gpiochip_add_data(&dev->gc, dev);
David Barksdalee932d812014-02-04 12:42:48 -06001152 if (ret < 0) {
1153 hid_err(hdev, "error registering gpio chip\n");
1154 goto err_free_i2c;
1155 }
1156
1157 ret = sysfs_create_group(&hdev->dev.kobj, &cp2112_attr_group);
1158 if (ret < 0) {
1159 hid_err(hdev, "error creating sysfs attrs\n");
1160 goto err_gpiochip_remove;
1161 }
1162
1163 chmod_sysfs_attrs(hdev);
1164 hid_hw_power(hdev, PM_HINT_NORMAL);
1165
1166 return ret;
1167
1168err_gpiochip_remove:
abdoulaye berthe88d5e522014-07-12 22:30:14 +02001169 gpiochip_remove(&dev->gc);
David Barksdalee932d812014-02-04 12:42:48 -06001170err_free_i2c:
1171 i2c_del_adapter(&dev->adap);
David Barksdalee932d812014-02-04 12:42:48 -06001172err_power_normal:
1173 hid_hw_power(hdev, PM_HINT_NORMAL);
1174err_hid_close:
1175 hid_hw_close(hdev);
1176err_hid_stop:
1177 hid_hw_stop(hdev);
1178 return ret;
1179}
1180
1181static void cp2112_remove(struct hid_device *hdev)
1182{
1183 struct cp2112_device *dev = hid_get_drvdata(hdev);
1184
1185 sysfs_remove_group(&hdev->dev.kobj, &cp2112_attr_group);
abdoulaye berthe88d5e522014-07-12 22:30:14 +02001186 gpiochip_remove(&dev->gc);
David Barksdalee932d812014-02-04 12:42:48 -06001187 i2c_del_adapter(&dev->adap);
1188 /* i2c_del_adapter has finished removing all i2c devices from our
1189 * adapter. Well behaved devices should no longer call our cp2112_xfer
1190 * and should have waited for any pending calls to finish. It has also
1191 * waited for device_unregister(&adap->dev) to complete. Therefore we
1192 * can safely free our struct cp2112_device.
1193 */
1194 hid_hw_close(hdev);
1195 hid_hw_stop(hdev);
David Barksdalee932d812014-02-04 12:42:48 -06001196}
1197
1198static int cp2112_raw_event(struct hid_device *hdev, struct hid_report *report,
1199 u8 *data, int size)
1200{
1201 struct cp2112_device *dev = hid_get_drvdata(hdev);
1202 struct cp2112_xfer_status_report *xfer = (void *)data;
1203
1204 switch (data[0]) {
1205 case CP2112_TRANSFER_STATUS_RESPONSE:
1206 hid_dbg(hdev, "xfer status: %02x %02x %04x %04x\n",
1207 xfer->status0, xfer->status1,
1208 be16_to_cpu(xfer->retries), be16_to_cpu(xfer->length));
1209
1210 switch (xfer->status0) {
1211 case STATUS0_IDLE:
1212 dev->xfer_status = -EAGAIN;
1213 break;
1214 case STATUS0_BUSY:
1215 dev->xfer_status = -EBUSY;
1216 break;
1217 case STATUS0_COMPLETE:
1218 dev->xfer_status = be16_to_cpu(xfer->length);
1219 break;
1220 case STATUS0_ERROR:
1221 switch (xfer->status1) {
1222 case STATUS1_TIMEOUT_NACK:
1223 case STATUS1_TIMEOUT_BUS:
1224 dev->xfer_status = -ETIMEDOUT;
1225 break;
1226 default:
1227 dev->xfer_status = -EIO;
1228 break;
1229 }
1230 break;
1231 default:
1232 dev->xfer_status = -EINVAL;
1233 break;
1234 }
1235
1236 atomic_set(&dev->xfer_avail, 1);
1237 break;
1238 case CP2112_DATA_READ_RESPONSE:
1239 hid_dbg(hdev, "read response: %02x %02x\n", data[1], data[2]);
1240
1241 dev->read_length = data[2];
1242 if (dev->read_length > sizeof(dev->read_data))
1243 dev->read_length = sizeof(dev->read_data);
1244
1245 memcpy(dev->read_data, &data[3], dev->read_length);
1246 atomic_set(&dev->read_avail, 1);
1247 break;
1248 default:
1249 hid_err(hdev, "unknown report\n");
1250
1251 return 0;
1252 }
1253
1254 wake_up_interruptible(&dev->wait);
1255 return 1;
1256}
1257
1258static struct hid_driver cp2112_driver = {
1259 .name = "cp2112",
1260 .id_table = cp2112_devices,
1261 .probe = cp2112_probe,
1262 .remove = cp2112_remove,
1263 .raw_event = cp2112_raw_event,
1264};
1265
1266module_hid_driver(cp2112_driver);
1267MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge");
1268MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
1269MODULE_LICENSE("GPL");
1270