blob: 078026f63b6f40999a2aa1feb8023d3905a6044c [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
Benjamin Tissoires13de9cc2016-11-21 12:07:54 +010027#include <linux/gpio.h>
Linus Walleij47513c22015-12-08 16:26:28 +010028#include <linux/gpio/driver.h>
David Barksdalee932d812014-02-04 12:42:48 -060029#include <linux/hid.h>
Jaejoong Kim42cb6b32017-03-03 17:54:00 +090030#include <linux/hidraw.h>
David Barksdalee932d812014-02-04 12:42:48 -060031#include <linux/i2c.h>
32#include <linux/module.h>
33#include <linux/nls.h>
34#include <linux/usb/ch9.h>
35#include "hid-ids.h"
36
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +010037#define CP2112_REPORT_MAX_LENGTH 64
38#define CP2112_GPIO_CONFIG_LENGTH 5
39#define CP2112_GPIO_GET_LENGTH 2
40#define CP2112_GPIO_SET_LENGTH 3
41
David Barksdalee932d812014-02-04 12:42:48 -060042enum {
43 CP2112_GPIO_CONFIG = 0x02,
44 CP2112_GPIO_GET = 0x03,
45 CP2112_GPIO_SET = 0x04,
46 CP2112_GET_VERSION_INFO = 0x05,
47 CP2112_SMBUS_CONFIG = 0x06,
48 CP2112_DATA_READ_REQUEST = 0x10,
49 CP2112_DATA_WRITE_READ_REQUEST = 0x11,
50 CP2112_DATA_READ_FORCE_SEND = 0x12,
51 CP2112_DATA_READ_RESPONSE = 0x13,
52 CP2112_DATA_WRITE_REQUEST = 0x14,
53 CP2112_TRANSFER_STATUS_REQUEST = 0x15,
54 CP2112_TRANSFER_STATUS_RESPONSE = 0x16,
55 CP2112_CANCEL_TRANSFER = 0x17,
56 CP2112_LOCK_BYTE = 0x20,
57 CP2112_USB_CONFIG = 0x21,
58 CP2112_MANUFACTURER_STRING = 0x22,
59 CP2112_PRODUCT_STRING = 0x23,
60 CP2112_SERIAL_STRING = 0x24,
61};
62
63enum {
64 STATUS0_IDLE = 0x00,
65 STATUS0_BUSY = 0x01,
66 STATUS0_COMPLETE = 0x02,
67 STATUS0_ERROR = 0x03,
68};
69
70enum {
71 STATUS1_TIMEOUT_NACK = 0x00,
72 STATUS1_TIMEOUT_BUS = 0x01,
73 STATUS1_ARBITRATION_LOST = 0x02,
74 STATUS1_READ_INCOMPLETE = 0x03,
75 STATUS1_WRITE_INCOMPLETE = 0x04,
76 STATUS1_SUCCESS = 0x05,
77};
78
79struct cp2112_smbus_config_report {
80 u8 report; /* CP2112_SMBUS_CONFIG */
81 __be32 clock_speed; /* Hz */
82 u8 device_address; /* Stored in the upper 7 bits */
83 u8 auto_send_read; /* 1 = enabled, 0 = disabled */
84 __be16 write_timeout; /* ms, 0 = no timeout */
85 __be16 read_timeout; /* ms, 0 = no timeout */
86 u8 scl_low_timeout; /* 1 = enabled, 0 = disabled */
87 __be16 retry_time; /* # of retries, 0 = no limit */
88} __packed;
89
90struct cp2112_usb_config_report {
91 u8 report; /* CP2112_USB_CONFIG */
92 __le16 vid; /* Vendor ID */
93 __le16 pid; /* Product ID */
94 u8 max_power; /* Power requested in 2mA units */
95 u8 power_mode; /* 0x00 = bus powered
96 0x01 = self powered & regulator off
97 0x02 = self powered & regulator on */
98 u8 release_major;
99 u8 release_minor;
100 u8 mask; /* What fields to program */
101} __packed;
102
103struct cp2112_read_req_report {
104 u8 report; /* CP2112_DATA_READ_REQUEST */
105 u8 slave_address;
106 __be16 length;
107} __packed;
108
109struct cp2112_write_read_req_report {
110 u8 report; /* CP2112_DATA_WRITE_READ_REQUEST */
111 u8 slave_address;
112 __be16 length;
113 u8 target_address_length;
114 u8 target_address[16];
115} __packed;
116
117struct cp2112_write_req_report {
118 u8 report; /* CP2112_DATA_WRITE_REQUEST */
119 u8 slave_address;
120 u8 length;
121 u8 data[61];
122} __packed;
123
124struct cp2112_force_read_report {
125 u8 report; /* CP2112_DATA_READ_FORCE_SEND */
126 __be16 length;
127} __packed;
128
129struct cp2112_xfer_status_report {
130 u8 report; /* CP2112_TRANSFER_STATUS_RESPONSE */
131 u8 status0; /* STATUS0_* */
132 u8 status1; /* STATUS1_* */
133 __be16 retries;
134 __be16 length;
135} __packed;
136
137struct cp2112_string_report {
138 u8 dummy; /* force .string to be aligned */
139 u8 report; /* CP2112_*_STRING */
140 u8 length; /* length in bytes of everyting after .report */
141 u8 type; /* USB_DT_STRING */
142 wchar_t string[30]; /* UTF16_LITTLE_ENDIAN string */
143} __packed;
144
145/* Number of times to request transfer status before giving up waiting for a
146 transfer to complete. This may need to be changed if SMBUS clock, retries,
147 or read/write/scl_low timeout settings are changed. */
148static const int XFER_STATUS_RETRIES = 10;
149
150/* Time in ms to wait for a CP2112_DATA_READ_RESPONSE or
151 CP2112_TRANSFER_STATUS_RESPONSE. */
152static const int RESPONSE_TIMEOUT = 50;
153
154static const struct hid_device_id cp2112_devices[] = {
155 { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_CP2112) },
156 { }
157};
158MODULE_DEVICE_TABLE(hid, cp2112_devices);
159
160struct cp2112_device {
161 struct i2c_adapter adap;
162 struct hid_device *hdev;
163 wait_queue_head_t wait;
164 u8 read_data[61];
165 u8 read_length;
Ellen Wang44eda782015-07-09 21:55:06 -0700166 u8 hwversion;
David Barksdalee932d812014-02-04 12:42:48 -0600167 int xfer_status;
168 atomic_t read_avail;
169 atomic_t xfer_avail;
170 struct gpio_chip gc;
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100171 u8 *in_out_buffer;
Johan Hovold7a7b5df2017-01-30 11:26:38 +0100172 struct mutex lock;
Benjamin Tissoires13de9cc2016-11-21 12:07:54 +0100173
174 struct gpio_desc *desc[8];
175 bool gpio_poll;
176 struct delayed_work gpio_poll_worker;
177 unsigned long irq_mask;
178 u8 gpio_prev_state;
David Barksdalee932d812014-02-04 12:42:48 -0600179};
180
181static int gpio_push_pull = 0xFF;
182module_param(gpio_push_pull, int, S_IRUGO | S_IWUSR);
183MODULE_PARM_DESC(gpio_push_pull, "GPIO push-pull configuration bitmask");
184
185static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
186{
Linus Walleij47513c22015-12-08 16:26:28 +0100187 struct cp2112_device *dev = gpiochip_get_data(chip);
David Barksdalee932d812014-02-04 12:42:48 -0600188 struct hid_device *hdev = dev->hdev;
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100189 u8 *buf = dev->in_out_buffer;
David Barksdalee932d812014-02-04 12:42:48 -0600190 int ret;
191
Johan Hovold7a7b5df2017-01-30 11:26:38 +0100192 mutex_lock(&dev->lock);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100193
Jiri Kosina490051a2014-02-18 00:39:39 +0100194 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100195 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
196 HID_REQ_GET_REPORT);
197 if (ret != CP2112_GPIO_CONFIG_LENGTH) {
David Barksdalee932d812014-02-04 12:42:48 -0600198 hid_err(hdev, "error requesting GPIO config: %d\n", ret);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100199 goto exit;
David Barksdalee932d812014-02-04 12:42:48 -0600200 }
201
202 buf[1] &= ~(1 << offset);
203 buf[2] = gpio_push_pull;
204
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100205 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
206 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
207 HID_REQ_SET_REPORT);
David Barksdalee932d812014-02-04 12:42:48 -0600208 if (ret < 0) {
209 hid_err(hdev, "error setting GPIO config: %d\n", ret);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100210 goto exit;
David Barksdalee932d812014-02-04 12:42:48 -0600211 }
212
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100213 ret = 0;
214
215exit:
Johan Hovold7a7b5df2017-01-30 11:26:38 +0100216 mutex_unlock(&dev->lock);
Johan Hovold8e9faa12017-01-30 11:26:39 +0100217 return ret < 0 ? ret : -EIO;
David Barksdalee932d812014-02-04 12:42:48 -0600218}
219
220static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
221{
Linus Walleij47513c22015-12-08 16:26:28 +0100222 struct cp2112_device *dev = gpiochip_get_data(chip);
David Barksdalee932d812014-02-04 12:42:48 -0600223 struct hid_device *hdev = dev->hdev;
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100224 u8 *buf = dev->in_out_buffer;
David Barksdalee932d812014-02-04 12:42:48 -0600225 int ret;
226
Johan Hovold7a7b5df2017-01-30 11:26:38 +0100227 mutex_lock(&dev->lock);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100228
David Barksdalee932d812014-02-04 12:42:48 -0600229 buf[0] = CP2112_GPIO_SET;
230 buf[1] = value ? 0xff : 0;
231 buf[2] = 1 << offset;
232
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100233 ret = hid_hw_raw_request(hdev, CP2112_GPIO_SET, buf,
234 CP2112_GPIO_SET_LENGTH, HID_FEATURE_REPORT,
235 HID_REQ_SET_REPORT);
David Barksdalee932d812014-02-04 12:42:48 -0600236 if (ret < 0)
237 hid_err(hdev, "error setting GPIO values: %d\n", ret);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100238
Johan Hovold7a7b5df2017-01-30 11:26:38 +0100239 mutex_unlock(&dev->lock);
David Barksdalee932d812014-02-04 12:42:48 -0600240}
241
Benjamin Tissoires13de9cc2016-11-21 12:07:54 +0100242static int cp2112_gpio_get_all(struct gpio_chip *chip)
David Barksdalee932d812014-02-04 12:42:48 -0600243{
Linus Walleij47513c22015-12-08 16:26:28 +0100244 struct cp2112_device *dev = gpiochip_get_data(chip);
David Barksdalee932d812014-02-04 12:42:48 -0600245 struct hid_device *hdev = dev->hdev;
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100246 u8 *buf = dev->in_out_buffer;
David Barksdalee932d812014-02-04 12:42:48 -0600247 int ret;
248
Johan Hovold7a7b5df2017-01-30 11:26:38 +0100249 mutex_lock(&dev->lock);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100250
251 ret = hid_hw_raw_request(hdev, CP2112_GPIO_GET, buf,
252 CP2112_GPIO_GET_LENGTH, HID_FEATURE_REPORT,
253 HID_REQ_GET_REPORT);
254 if (ret != CP2112_GPIO_GET_LENGTH) {
David Barksdalee932d812014-02-04 12:42:48 -0600255 hid_err(hdev, "error requesting GPIO values: %d\n", ret);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100256 ret = ret < 0 ? ret : -EIO;
257 goto exit;
David Barksdalee932d812014-02-04 12:42:48 -0600258 }
259
Benjamin Tissoires13de9cc2016-11-21 12:07:54 +0100260 ret = buf[1];
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100261
262exit:
Johan Hovold7a7b5df2017-01-30 11:26:38 +0100263 mutex_unlock(&dev->lock);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100264
265 return ret;
David Barksdalee932d812014-02-04 12:42:48 -0600266}
267
Benjamin Tissoires13de9cc2016-11-21 12:07:54 +0100268static int cp2112_gpio_get(struct gpio_chip *chip, unsigned int offset)
269{
270 int ret;
271
272 ret = cp2112_gpio_get_all(chip);
273 if (ret < 0)
274 return ret;
275
276 return (ret >> offset) & 1;
277}
278
David Barksdalee932d812014-02-04 12:42:48 -0600279static int cp2112_gpio_direction_output(struct gpio_chip *chip,
280 unsigned offset, int value)
281{
Linus Walleij47513c22015-12-08 16:26:28 +0100282 struct cp2112_device *dev = gpiochip_get_data(chip);
David Barksdalee932d812014-02-04 12:42:48 -0600283 struct hid_device *hdev = dev->hdev;
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100284 u8 *buf = dev->in_out_buffer;
David Barksdalee932d812014-02-04 12:42:48 -0600285 int ret;
286
Johan Hovold7a7b5df2017-01-30 11:26:38 +0100287 mutex_lock(&dev->lock);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100288
Jiri Kosina490051a2014-02-18 00:39:39 +0100289 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100290 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
291 HID_REQ_GET_REPORT);
292 if (ret != CP2112_GPIO_CONFIG_LENGTH) {
David Barksdalee932d812014-02-04 12:42:48 -0600293 hid_err(hdev, "error requesting GPIO config: %d\n", ret);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100294 goto fail;
David Barksdalee932d812014-02-04 12:42:48 -0600295 }
296
297 buf[1] |= 1 << offset;
298 buf[2] = gpio_push_pull;
299
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100300 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
301 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
302 HID_REQ_SET_REPORT);
David Barksdalee932d812014-02-04 12:42:48 -0600303 if (ret < 0) {
304 hid_err(hdev, "error setting GPIO config: %d\n", ret);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100305 goto fail;
David Barksdalee932d812014-02-04 12:42:48 -0600306 }
307
Johan Hovold7a7b5df2017-01-30 11:26:38 +0100308 mutex_unlock(&dev->lock);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100309
Antonio Borneobeb9d002014-06-29 14:13:48 +0800310 /*
311 * Set gpio value when output direction is already set,
312 * as specified in AN495, Rev. 0.2, cpt. 4.4
313 */
314 cp2112_gpio_set(chip, offset, value);
315
David Barksdalee932d812014-02-04 12:42:48 -0600316 return 0;
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100317
318fail:
Johan Hovold7a7b5df2017-01-30 11:26:38 +0100319 mutex_unlock(&dev->lock);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +0100320 return ret < 0 ? ret : -EIO;
David Barksdalee932d812014-02-04 12:42:48 -0600321}
322
323static int cp2112_hid_get(struct hid_device *hdev, unsigned char report_number,
324 u8 *data, size_t count, unsigned char report_type)
325{
326 u8 *buf;
327 int ret;
328
329 buf = kmalloc(count, GFP_KERNEL);
330 if (!buf)
331 return -ENOMEM;
332
Jiri Kosina490051a2014-02-18 00:39:39 +0100333 ret = hid_hw_raw_request(hdev, report_number, buf, count,
334 report_type, HID_REQ_GET_REPORT);
David Barksdalee932d812014-02-04 12:42:48 -0600335 memcpy(data, buf, count);
336 kfree(buf);
337 return ret;
338}
339
340static int cp2112_hid_output(struct hid_device *hdev, u8 *data, size_t count,
341 unsigned char report_type)
342{
343 u8 *buf;
344 int ret;
345
346 buf = kmemdup(data, count, GFP_KERNEL);
347 if (!buf)
348 return -ENOMEM;
349
Benjamin Tissoires866e4792014-03-08 22:52:41 -0500350 if (report_type == HID_OUTPUT_REPORT)
351 ret = hid_hw_output_report(hdev, buf, count);
352 else
353 ret = hid_hw_raw_request(hdev, buf[0], buf, count, report_type,
354 HID_REQ_SET_REPORT);
355
David Barksdalee932d812014-02-04 12:42:48 -0600356 kfree(buf);
357 return ret;
358}
359
360static int cp2112_wait(struct cp2112_device *dev, atomic_t *avail)
361{
362 int ret = 0;
363
364 /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a
365 * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to
366 * come in cp2112_raw_event or timeout. There will only be one of these
367 * in flight at any one time. The timeout is extremely large and is a
368 * last resort if the CP2112 has died. If we do timeout we don't expect
369 * to receive the response which would cause data races, it's not like
370 * we can do anything about it anyway.
371 */
372 ret = wait_event_interruptible_timeout(dev->wait,
373 atomic_read(avail), msecs_to_jiffies(RESPONSE_TIMEOUT));
374 if (-ERESTARTSYS == ret)
375 return ret;
376 if (!ret)
377 return -ETIMEDOUT;
378
379 atomic_set(avail, 0);
380 return 0;
381}
382
383static int cp2112_xfer_status(struct cp2112_device *dev)
384{
385 struct hid_device *hdev = dev->hdev;
386 u8 buf[2];
387 int ret;
388
389 buf[0] = CP2112_TRANSFER_STATUS_REQUEST;
390 buf[1] = 0x01;
391 atomic_set(&dev->xfer_avail, 0);
392
393 ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
394 if (ret < 0) {
395 hid_warn(hdev, "Error requesting status: %d\n", ret);
396 return ret;
397 }
398
399 ret = cp2112_wait(dev, &dev->xfer_avail);
400 if (ret)
401 return ret;
402
403 return dev->xfer_status;
404}
405
406static int cp2112_read(struct cp2112_device *dev, u8 *data, size_t size)
407{
408 struct hid_device *hdev = dev->hdev;
409 struct cp2112_force_read_report report;
410 int ret;
411
Antonio Borneo6debce62015-06-21 14:20:25 +0800412 if (size > sizeof(dev->read_data))
413 size = sizeof(dev->read_data);
David Barksdalee932d812014-02-04 12:42:48 -0600414 report.report = CP2112_DATA_READ_FORCE_SEND;
415 report.length = cpu_to_be16(size);
416
417 atomic_set(&dev->read_avail, 0);
418
419 ret = cp2112_hid_output(hdev, &report.report, sizeof(report),
420 HID_OUTPUT_REPORT);
421 if (ret < 0) {
422 hid_warn(hdev, "Error requesting data: %d\n", ret);
423 return ret;
424 }
425
426 ret = cp2112_wait(dev, &dev->read_avail);
427 if (ret)
428 return ret;
429
Jiri Kosina5a673fc2014-02-17 23:44:54 +0100430 hid_dbg(hdev, "read %d of %zd bytes requested\n",
David Barksdalee932d812014-02-04 12:42:48 -0600431 dev->read_length, size);
432
433 if (size > dev->read_length)
434 size = dev->read_length;
435
436 memcpy(data, dev->read_data, size);
437 return dev->read_length;
438}
439
440static int cp2112_read_req(void *buf, u8 slave_address, u16 length)
441{
442 struct cp2112_read_req_report *report = buf;
443
444 if (length < 1 || length > 512)
445 return -EINVAL;
446
447 report->report = CP2112_DATA_READ_REQUEST;
448 report->slave_address = slave_address << 1;
449 report->length = cpu_to_be16(length);
450 return sizeof(*report);
451}
452
453static int cp2112_write_read_req(void *buf, u8 slave_address, u16 length,
454 u8 command, u8 *data, u8 data_length)
455{
456 struct cp2112_write_read_req_report *report = buf;
457
458 if (length < 1 || length > 512
459 || data_length > sizeof(report->target_address) - 1)
460 return -EINVAL;
461
462 report->report = CP2112_DATA_WRITE_READ_REQUEST;
463 report->slave_address = slave_address << 1;
464 report->length = cpu_to_be16(length);
465 report->target_address_length = data_length + 1;
466 report->target_address[0] = command;
467 memcpy(&report->target_address[1], data, data_length);
468 return data_length + 6;
469}
470
471static int cp2112_write_req(void *buf, u8 slave_address, u8 command, u8 *data,
472 u8 data_length)
473{
474 struct cp2112_write_req_report *report = buf;
475
476 if (data_length > sizeof(report->data) - 1)
477 return -EINVAL;
478
479 report->report = CP2112_DATA_WRITE_REQUEST;
480 report->slave_address = slave_address << 1;
481 report->length = data_length + 1;
482 report->data[0] = command;
483 memcpy(&report->data[1], data, data_length);
484 return data_length + 4;
485}
486
Antonio Borneob9029342014-07-08 07:25:39 +0800487static int cp2112_i2c_write_req(void *buf, u8 slave_address, u8 *data,
488 u8 data_length)
489{
490 struct cp2112_write_req_report *report = buf;
491
492 if (data_length > sizeof(report->data))
493 return -EINVAL;
494
495 report->report = CP2112_DATA_WRITE_REQUEST;
496 report->slave_address = slave_address << 1;
497 report->length = data_length;
498 memcpy(report->data, data, data_length);
499 return data_length + 3;
500}
501
Ellen Wang44eda782015-07-09 21:55:06 -0700502static int cp2112_i2c_write_read_req(void *buf, u8 slave_address,
503 u8 *addr, int addr_length,
504 int read_length)
505{
506 struct cp2112_write_read_req_report *report = buf;
507
508 if (read_length < 1 || read_length > 512 ||
509 addr_length > sizeof(report->target_address))
510 return -EINVAL;
511
512 report->report = CP2112_DATA_WRITE_READ_REQUEST;
513 report->slave_address = slave_address << 1;
514 report->length = cpu_to_be16(read_length);
515 report->target_address_length = addr_length;
516 memcpy(report->target_address, addr, addr_length);
517 return addr_length + 5;
518}
519
Antonio Borneob9029342014-07-08 07:25:39 +0800520static int cp2112_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
521 int num)
522{
523 struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data;
524 struct hid_device *hdev = dev->hdev;
525 u8 buf[64];
526 ssize_t count;
Ellen Wang44eda782015-07-09 21:55:06 -0700527 ssize_t read_length = 0;
528 u8 *read_buf = NULL;
Antonio Borneob9029342014-07-08 07:25:39 +0800529 unsigned int retries;
530 int ret;
531
532 hid_dbg(hdev, "I2C %d messages\n", num);
533
Ellen Wang44eda782015-07-09 21:55:06 -0700534 if (num == 1) {
535 if (msgs->flags & I2C_M_RD) {
536 hid_dbg(hdev, "I2C read %#04x len %d\n",
537 msgs->addr, msgs->len);
538 read_length = msgs->len;
539 read_buf = msgs->buf;
540 count = cp2112_read_req(buf, msgs->addr, msgs->len);
541 } else {
542 hid_dbg(hdev, "I2C write %#04x len %d\n",
543 msgs->addr, msgs->len);
544 count = cp2112_i2c_write_req(buf, msgs->addr,
545 msgs->buf, msgs->len);
546 }
547 if (count < 0)
548 return count;
549 } else if (dev->hwversion > 1 && /* no repeated start in rev 1 */
550 num == 2 &&
551 msgs[0].addr == msgs[1].addr &&
552 !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) {
553 hid_dbg(hdev, "I2C write-read %#04x wlen %d rlen %d\n",
554 msgs[0].addr, msgs[0].len, msgs[1].len);
555 read_length = msgs[1].len;
556 read_buf = msgs[1].buf;
557 count = cp2112_i2c_write_read_req(buf, msgs[0].addr,
558 msgs[0].buf, msgs[0].len, msgs[1].len);
559 if (count < 0)
560 return count;
561 } else {
Antonio Borneob9029342014-07-08 07:25:39 +0800562 hid_err(hdev,
563 "Multi-message I2C transactions not supported\n");
564 return -EOPNOTSUPP;
565 }
566
Antonio Borneob9029342014-07-08 07:25:39 +0800567 ret = hid_hw_power(hdev, PM_HINT_FULLON);
568 if (ret < 0) {
569 hid_err(hdev, "power management error: %d\n", ret);
570 return ret;
571 }
572
573 ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT);
574 if (ret < 0) {
575 hid_warn(hdev, "Error starting transaction: %d\n", ret);
576 goto power_normal;
577 }
578
579 for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) {
580 ret = cp2112_xfer_status(dev);
581 if (-EBUSY == ret)
582 continue;
583 if (ret < 0)
584 goto power_normal;
585 break;
586 }
587
588 if (XFER_STATUS_RETRIES <= retries) {
589 hid_warn(hdev, "Transfer timed out, cancelling.\n");
590 buf[0] = CP2112_CANCEL_TRANSFER;
591 buf[1] = 0x01;
592
593 ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
594 if (ret < 0)
595 hid_warn(hdev, "Error cancelling transaction: %d\n",
596 ret);
597
598 ret = -ETIMEDOUT;
599 goto power_normal;
600 }
601
Ellen Wang44eda782015-07-09 21:55:06 -0700602 for (count = 0; count < read_length;) {
603 ret = cp2112_read(dev, read_buf + count, read_length - count);
Ellen Wang5ddfb122015-07-08 11:17:39 -0700604 if (ret < 0)
605 goto power_normal;
606 if (ret == 0) {
607 hid_err(hdev, "read returned 0\n");
608 ret = -EIO;
609 goto power_normal;
610 }
611 count += ret;
Ellen Wang44eda782015-07-09 21:55:06 -0700612 if (count > read_length) {
Ellen Wang5ddfb122015-07-08 11:17:39 -0700613 /*
614 * The hardware returned too much data.
615 * This is mostly harmless because cp2112_read()
616 * has a limit check so didn't overrun our
617 * buffer. Nevertheless, we return an error
618 * because something is seriously wrong and
619 * it shouldn't go unnoticed.
620 */
621 hid_err(hdev, "long read: %d > %zd\n",
Ellen Wang44eda782015-07-09 21:55:06 -0700622 ret, read_length - count + ret);
Ellen Wang5ddfb122015-07-08 11:17:39 -0700623 ret = -EIO;
624 goto power_normal;
625 }
Antonio Borneob9029342014-07-08 07:25:39 +0800626 }
627
Antonio Borneob9029342014-07-08 07:25:39 +0800628 /* return the number of transferred messages */
Ellen Wang44eda782015-07-09 21:55:06 -0700629 ret = num;
Antonio Borneob9029342014-07-08 07:25:39 +0800630
631power_normal:
632 hid_hw_power(hdev, PM_HINT_NORMAL);
633 hid_dbg(hdev, "I2C transfer finished: %d\n", ret);
634 return ret;
635}
636
David Barksdalee932d812014-02-04 12:42:48 -0600637static int cp2112_xfer(struct i2c_adapter *adap, u16 addr,
638 unsigned short flags, char read_write, u8 command,
639 int size, union i2c_smbus_data *data)
640{
641 struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data;
642 struct hid_device *hdev = dev->hdev;
643 u8 buf[64];
Ellen Wang29e2d6d2015-07-09 22:04:31 -0700644 __le16 word;
Jiri Kosina0438ee72014-02-18 09:43:53 +0100645 ssize_t count;
David Barksdalee932d812014-02-04 12:42:48 -0600646 size_t read_length = 0;
647 unsigned int retries;
648 int ret;
649
650 hid_dbg(hdev, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n",
651 read_write == I2C_SMBUS_WRITE ? "write" : "read",
652 addr, flags, command, size);
653
654 switch (size) {
655 case I2C_SMBUS_BYTE:
656 read_length = 1;
657
658 if (I2C_SMBUS_READ == read_write)
659 count = cp2112_read_req(buf, addr, read_length);
660 else
Ellen Wang6d00d152015-07-13 15:23:54 -0700661 count = cp2112_write_req(buf, addr, command, NULL,
David Barksdalee932d812014-02-04 12:42:48 -0600662 0);
663 break;
664 case I2C_SMBUS_BYTE_DATA:
665 read_length = 1;
666
667 if (I2C_SMBUS_READ == read_write)
668 count = cp2112_write_read_req(buf, addr, read_length,
669 command, NULL, 0);
670 else
671 count = cp2112_write_req(buf, addr, command,
672 &data->byte, 1);
673 break;
674 case I2C_SMBUS_WORD_DATA:
675 read_length = 2;
Ellen Wang29e2d6d2015-07-09 22:04:31 -0700676 word = cpu_to_le16(data->word);
David Barksdalee932d812014-02-04 12:42:48 -0600677
678 if (I2C_SMBUS_READ == read_write)
679 count = cp2112_write_read_req(buf, addr, read_length,
680 command, NULL, 0);
681 else
682 count = cp2112_write_req(buf, addr, command,
683 (u8 *)&word, 2);
684 break;
685 case I2C_SMBUS_PROC_CALL:
686 size = I2C_SMBUS_WORD_DATA;
687 read_write = I2C_SMBUS_READ;
688 read_length = 2;
Ellen Wang29e2d6d2015-07-09 22:04:31 -0700689 word = cpu_to_le16(data->word);
David Barksdalee932d812014-02-04 12:42:48 -0600690
691 count = cp2112_write_read_req(buf, addr, read_length, command,
692 (u8 *)&word, 2);
693 break;
694 case I2C_SMBUS_I2C_BLOCK_DATA:
695 size = I2C_SMBUS_BLOCK_DATA;
696 /* fallthrough */
697 case I2C_SMBUS_BLOCK_DATA:
698 if (I2C_SMBUS_READ == read_write) {
699 count = cp2112_write_read_req(buf, addr,
700 I2C_SMBUS_BLOCK_MAX,
701 command, NULL, 0);
702 } else {
703 count = cp2112_write_req(buf, addr, command,
704 data->block,
705 data->block[0] + 1);
706 }
707 break;
708 case I2C_SMBUS_BLOCK_PROC_CALL:
709 size = I2C_SMBUS_BLOCK_DATA;
710 read_write = I2C_SMBUS_READ;
711
712 count = cp2112_write_read_req(buf, addr, I2C_SMBUS_BLOCK_MAX,
713 command, data->block,
714 data->block[0] + 1);
715 break;
716 default:
717 hid_warn(hdev, "Unsupported transaction %d\n", size);
718 return -EOPNOTSUPP;
719 }
720
721 if (count < 0)
722 return count;
723
724 ret = hid_hw_power(hdev, PM_HINT_FULLON);
725 if (ret < 0) {
726 hid_err(hdev, "power management error: %d\n", ret);
727 return ret;
728 }
729
730 ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT);
731 if (ret < 0) {
732 hid_warn(hdev, "Error starting transaction: %d\n", ret);
733 goto power_normal;
734 }
735
736 for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) {
737 ret = cp2112_xfer_status(dev);
738 if (-EBUSY == ret)
739 continue;
740 if (ret < 0)
741 goto power_normal;
742 break;
743 }
744
745 if (XFER_STATUS_RETRIES <= retries) {
746 hid_warn(hdev, "Transfer timed out, cancelling.\n");
747 buf[0] = CP2112_CANCEL_TRANSFER;
748 buf[1] = 0x01;
749
750 ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
751 if (ret < 0)
752 hid_warn(hdev, "Error cancelling transaction: %d\n",
753 ret);
754
755 ret = -ETIMEDOUT;
756 goto power_normal;
757 }
758
759 if (I2C_SMBUS_WRITE == read_write) {
760 ret = 0;
761 goto power_normal;
762 }
763
764 if (I2C_SMBUS_BLOCK_DATA == size)
765 read_length = ret;
766
767 ret = cp2112_read(dev, buf, read_length);
768 if (ret < 0)
769 goto power_normal;
770 if (ret != read_length) {
Jiri Kosina5a673fc2014-02-17 23:44:54 +0100771 hid_warn(hdev, "short read: %d < %zd\n", ret, read_length);
David Barksdalee932d812014-02-04 12:42:48 -0600772 ret = -EIO;
773 goto power_normal;
774 }
775
776 switch (size) {
777 case I2C_SMBUS_BYTE:
778 case I2C_SMBUS_BYTE_DATA:
779 data->byte = buf[0];
780 break;
781 case I2C_SMBUS_WORD_DATA:
Ellen Wang29e2d6d2015-07-09 22:04:31 -0700782 data->word = le16_to_cpup((__le16 *)buf);
David Barksdalee932d812014-02-04 12:42:48 -0600783 break;
784 case I2C_SMBUS_BLOCK_DATA:
785 if (read_length > I2C_SMBUS_BLOCK_MAX) {
786 ret = -EPROTO;
787 goto power_normal;
788 }
789
790 memcpy(data->block, buf, read_length);
791 break;
792 }
793
794 ret = 0;
795power_normal:
796 hid_hw_power(hdev, PM_HINT_NORMAL);
797 hid_dbg(hdev, "transfer finished: %d\n", ret);
798 return ret;
799}
800
801static u32 cp2112_functionality(struct i2c_adapter *adap)
802{
Antonio Borneob9029342014-07-08 07:25:39 +0800803 return I2C_FUNC_I2C |
804 I2C_FUNC_SMBUS_BYTE |
David Barksdalee932d812014-02-04 12:42:48 -0600805 I2C_FUNC_SMBUS_BYTE_DATA |
806 I2C_FUNC_SMBUS_WORD_DATA |
807 I2C_FUNC_SMBUS_BLOCK_DATA |
808 I2C_FUNC_SMBUS_I2C_BLOCK |
809 I2C_FUNC_SMBUS_PROC_CALL |
810 I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
811}
812
813static const struct i2c_algorithm smbus_algorithm = {
Antonio Borneob9029342014-07-08 07:25:39 +0800814 .master_xfer = cp2112_i2c_xfer,
David Barksdalee932d812014-02-04 12:42:48 -0600815 .smbus_xfer = cp2112_xfer,
816 .functionality = cp2112_functionality,
817};
818
819static int cp2112_get_usb_config(struct hid_device *hdev,
820 struct cp2112_usb_config_report *cfg)
821{
822 int ret;
823
824 ret = cp2112_hid_get(hdev, CP2112_USB_CONFIG, (u8 *)cfg, sizeof(*cfg),
825 HID_FEATURE_REPORT);
826 if (ret != sizeof(*cfg)) {
827 hid_err(hdev, "error reading usb config: %d\n", ret);
828 if (ret < 0)
829 return ret;
830 return -EIO;
831 }
832
833 return 0;
834}
835
836static int cp2112_set_usb_config(struct hid_device *hdev,
837 struct cp2112_usb_config_report *cfg)
838{
839 int ret;
840
841 BUG_ON(cfg->report != CP2112_USB_CONFIG);
842
843 ret = cp2112_hid_output(hdev, (u8 *)cfg, sizeof(*cfg),
844 HID_FEATURE_REPORT);
845 if (ret != sizeof(*cfg)) {
846 hid_err(hdev, "error writing usb config: %d\n", ret);
847 if (ret < 0)
848 return ret;
849 return -EIO;
850 }
851
852 return 0;
853}
854
855static void chmod_sysfs_attrs(struct hid_device *hdev);
856
857#define CP2112_CONFIG_ATTR(name, store, format, ...) \
858static ssize_t name##_store(struct device *kdev, \
859 struct device_attribute *attr, const char *buf, \
860 size_t count) \
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 store; \
868 ret = cp2112_set_usb_config(hdev, &cfg); \
869 if (ret) \
870 return ret; \
871 chmod_sysfs_attrs(hdev); \
872 return count; \
873} \
874static ssize_t name##_show(struct device *kdev, \
875 struct device_attribute *attr, char *buf) \
876{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +0800877 struct hid_device *hdev = to_hid_device(kdev); \
David Barksdalee932d812014-02-04 12:42:48 -0600878 struct cp2112_usb_config_report cfg; \
879 int ret = cp2112_get_usb_config(hdev, &cfg); \
880 if (ret) \
881 return ret; \
882 return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \
883} \
Jiri Kosinac3c041b2014-02-17 23:40:20 +0100884static DEVICE_ATTR_RW(name);
David Barksdalee932d812014-02-04 12:42:48 -0600885
886CP2112_CONFIG_ATTR(vendor_id, ({
887 u16 vid;
888
889 if (sscanf(buf, "%hi", &vid) != 1)
890 return -EINVAL;
891
892 cfg.vid = cpu_to_le16(vid);
893 cfg.mask = 0x01;
894}), "0x%04x\n", le16_to_cpu(cfg.vid));
895
896CP2112_CONFIG_ATTR(product_id, ({
897 u16 pid;
898
899 if (sscanf(buf, "%hi", &pid) != 1)
900 return -EINVAL;
901
902 cfg.pid = cpu_to_le16(pid);
903 cfg.mask = 0x02;
904}), "0x%04x\n", le16_to_cpu(cfg.pid));
905
906CP2112_CONFIG_ATTR(max_power, ({
907 int mA;
908
909 if (sscanf(buf, "%i", &mA) != 1)
910 return -EINVAL;
911
912 cfg.max_power = (mA + 1) / 2;
913 cfg.mask = 0x04;
914}), "%u mA\n", cfg.max_power * 2);
915
916CP2112_CONFIG_ATTR(power_mode, ({
917 if (sscanf(buf, "%hhi", &cfg.power_mode) != 1)
918 return -EINVAL;
919
920 cfg.mask = 0x08;
921}), "%u\n", cfg.power_mode);
922
923CP2112_CONFIG_ATTR(release_version, ({
924 if (sscanf(buf, "%hhi.%hhi", &cfg.release_major, &cfg.release_minor)
925 != 2)
926 return -EINVAL;
927
928 cfg.mask = 0x10;
929}), "%u.%u\n", cfg.release_major, cfg.release_minor);
930
931#undef CP2112_CONFIG_ATTR
932
933struct cp2112_pstring_attribute {
934 struct device_attribute attr;
935 unsigned char report;
936};
937
938static ssize_t pstr_store(struct device *kdev,
939 struct device_attribute *kattr, const char *buf,
940 size_t count)
941{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800942 struct hid_device *hdev = to_hid_device(kdev);
David Barksdalee932d812014-02-04 12:42:48 -0600943 struct cp2112_pstring_attribute *attr =
944 container_of(kattr, struct cp2112_pstring_attribute, attr);
945 struct cp2112_string_report report;
946 int ret;
947
948 memset(&report, 0, sizeof(report));
949
950 ret = utf8s_to_utf16s(buf, count, UTF16_LITTLE_ENDIAN,
951 report.string, ARRAY_SIZE(report.string));
952 report.report = attr->report;
953 report.length = ret * sizeof(report.string[0]) + 2;
954 report.type = USB_DT_STRING;
955
956 ret = cp2112_hid_output(hdev, &report.report, report.length + 1,
957 HID_FEATURE_REPORT);
958 if (ret != report.length + 1) {
959 hid_err(hdev, "error writing %s string: %d\n", kattr->attr.name,
960 ret);
961 if (ret < 0)
962 return ret;
963 return -EIO;
964 }
965
966 chmod_sysfs_attrs(hdev);
967 return count;
968}
969
970static ssize_t pstr_show(struct device *kdev,
971 struct device_attribute *kattr, char *buf)
972{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800973 struct hid_device *hdev = to_hid_device(kdev);
David Barksdalee932d812014-02-04 12:42:48 -0600974 struct cp2112_pstring_attribute *attr =
975 container_of(kattr, struct cp2112_pstring_attribute, attr);
976 struct cp2112_string_report report;
977 u8 length;
978 int ret;
979
980 ret = cp2112_hid_get(hdev, attr->report, &report.report,
981 sizeof(report) - 1, HID_FEATURE_REPORT);
982 if (ret < 3) {
983 hid_err(hdev, "error reading %s string: %d\n", kattr->attr.name,
984 ret);
985 if (ret < 0)
986 return ret;
987 return -EIO;
988 }
989
990 if (report.length < 2) {
991 hid_err(hdev, "invalid %s string length: %d\n",
992 kattr->attr.name, report.length);
993 return -EIO;
994 }
995
996 length = report.length > ret - 1 ? ret - 1 : report.length;
997 length = (length - 2) / sizeof(report.string[0]);
998 ret = utf16s_to_utf8s(report.string, length, UTF16_LITTLE_ENDIAN, buf,
999 PAGE_SIZE - 1);
1000 buf[ret++] = '\n';
1001 return ret;
1002}
1003
1004#define CP2112_PSTR_ATTR(name, _report) \
Jiri Kosinac3c041b2014-02-17 23:40:20 +01001005static struct cp2112_pstring_attribute dev_attr_##name = { \
David Barksdalee932d812014-02-04 12:42:48 -06001006 .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \
1007 .report = _report, \
1008};
1009
1010CP2112_PSTR_ATTR(manufacturer, CP2112_MANUFACTURER_STRING);
1011CP2112_PSTR_ATTR(product, CP2112_PRODUCT_STRING);
1012CP2112_PSTR_ATTR(serial, CP2112_SERIAL_STRING);
1013
1014#undef CP2112_PSTR_ATTR
1015
1016static const struct attribute_group cp2112_attr_group = {
1017 .attrs = (struct attribute *[]){
1018 &dev_attr_vendor_id.attr,
1019 &dev_attr_product_id.attr,
1020 &dev_attr_max_power.attr,
1021 &dev_attr_power_mode.attr,
1022 &dev_attr_release_version.attr,
1023 &dev_attr_manufacturer.attr.attr,
1024 &dev_attr_product.attr.attr,
1025 &dev_attr_serial.attr.attr,
1026 NULL
1027 }
1028};
1029
1030/* Chmoding our sysfs attributes is simply a way to expose which fields in the
1031 * PROM have already been programmed. We do not depend on this preventing
1032 * writing to these attributes since the CP2112 will simply ignore writes to
1033 * already-programmed fields. This is why there is no sense in fixing this
1034 * racy behaviour.
1035 */
1036static void chmod_sysfs_attrs(struct hid_device *hdev)
1037{
1038 struct attribute **attr;
1039 u8 buf[2];
1040 int ret;
1041
1042 ret = cp2112_hid_get(hdev, CP2112_LOCK_BYTE, buf, sizeof(buf),
1043 HID_FEATURE_REPORT);
1044 if (ret != sizeof(buf)) {
1045 hid_err(hdev, "error reading lock byte: %d\n", ret);
1046 return;
1047 }
1048
1049 for (attr = cp2112_attr_group.attrs; *attr; ++attr) {
1050 umode_t mode = (buf[1] & 1) ? S_IWUSR | S_IRUGO : S_IRUGO;
1051 ret = sysfs_chmod_file(&hdev->dev.kobj, *attr, mode);
1052 if (ret < 0)
1053 hid_err(hdev, "error chmoding sysfs file %s\n",
1054 (*attr)->name);
1055 buf[1] >>= 1;
1056 }
1057}
1058
Benjamin Tissoires13de9cc2016-11-21 12:07:54 +01001059static void cp2112_gpio_irq_ack(struct irq_data *d)
1060{
1061}
1062
1063static void cp2112_gpio_irq_mask(struct irq_data *d)
1064{
1065 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1066 struct cp2112_device *dev = gpiochip_get_data(gc);
1067
1068 __clear_bit(d->hwirq, &dev->irq_mask);
1069}
1070
1071static void cp2112_gpio_irq_unmask(struct irq_data *d)
1072{
1073 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1074 struct cp2112_device *dev = gpiochip_get_data(gc);
1075
1076 __set_bit(d->hwirq, &dev->irq_mask);
1077}
1078
1079static void cp2112_gpio_poll_callback(struct work_struct *work)
1080{
1081 struct cp2112_device *dev = container_of(work, struct cp2112_device,
1082 gpio_poll_worker.work);
1083 struct irq_data *d;
1084 u8 gpio_mask;
1085 u8 virqs = (u8)dev->irq_mask;
1086 u32 irq_type;
1087 int irq, virq, ret;
1088
1089 ret = cp2112_gpio_get_all(&dev->gc);
1090 if (ret == -ENODEV) /* the hardware has been disconnected */
1091 return;
1092 if (ret < 0)
1093 goto exit;
1094
1095 gpio_mask = ret;
1096
1097 while (virqs) {
1098 virq = ffs(virqs) - 1;
1099 virqs &= ~BIT(virq);
1100
1101 if (!dev->gc.to_irq)
1102 break;
1103
1104 irq = dev->gc.to_irq(&dev->gc, virq);
1105
1106 d = irq_get_irq_data(irq);
1107 if (!d)
1108 continue;
1109
1110 irq_type = irqd_get_trigger_type(d);
1111
1112 if (gpio_mask & BIT(virq)) {
1113 /* Level High */
1114
1115 if (irq_type & IRQ_TYPE_LEVEL_HIGH)
1116 handle_nested_irq(irq);
1117
1118 if ((irq_type & IRQ_TYPE_EDGE_RISING) &&
1119 !(dev->gpio_prev_state & BIT(virq)))
1120 handle_nested_irq(irq);
1121 } else {
1122 /* Level Low */
1123
1124 if (irq_type & IRQ_TYPE_LEVEL_LOW)
1125 handle_nested_irq(irq);
1126
1127 if ((irq_type & IRQ_TYPE_EDGE_FALLING) &&
1128 (dev->gpio_prev_state & BIT(virq)))
1129 handle_nested_irq(irq);
1130 }
1131 }
1132
1133 dev->gpio_prev_state = gpio_mask;
1134
1135exit:
1136 if (dev->gpio_poll)
1137 schedule_delayed_work(&dev->gpio_poll_worker, 10);
1138}
1139
1140
1141static unsigned int cp2112_gpio_irq_startup(struct irq_data *d)
1142{
1143 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1144 struct cp2112_device *dev = gpiochip_get_data(gc);
1145
1146 INIT_DELAYED_WORK(&dev->gpio_poll_worker, cp2112_gpio_poll_callback);
1147
1148 cp2112_gpio_direction_input(gc, d->hwirq);
1149
1150 if (!dev->gpio_poll) {
1151 dev->gpio_poll = true;
1152 schedule_delayed_work(&dev->gpio_poll_worker, 0);
1153 }
1154
1155 cp2112_gpio_irq_unmask(d);
1156 return 0;
1157}
1158
1159static void cp2112_gpio_irq_shutdown(struct irq_data *d)
1160{
1161 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1162 struct cp2112_device *dev = gpiochip_get_data(gc);
1163
1164 cancel_delayed_work_sync(&dev->gpio_poll_worker);
1165}
1166
1167static int cp2112_gpio_irq_type(struct irq_data *d, unsigned int type)
1168{
1169 return 0;
1170}
1171
1172static struct irq_chip cp2112_gpio_irqchip = {
1173 .name = "cp2112-gpio",
1174 .irq_startup = cp2112_gpio_irq_startup,
1175 .irq_shutdown = cp2112_gpio_irq_shutdown,
1176 .irq_ack = cp2112_gpio_irq_ack,
1177 .irq_mask = cp2112_gpio_irq_mask,
1178 .irq_unmask = cp2112_gpio_irq_unmask,
1179 .irq_set_type = cp2112_gpio_irq_type,
1180};
1181
1182static int __maybe_unused cp2112_allocate_irq(struct cp2112_device *dev,
1183 int pin)
1184{
1185 int ret;
1186
1187 if (dev->desc[pin])
1188 return -EINVAL;
1189
1190 dev->desc[pin] = gpiochip_request_own_desc(&dev->gc, pin,
1191 "HID/I2C:Event");
1192 if (IS_ERR(dev->desc[pin])) {
1193 dev_err(dev->gc.parent, "Failed to request GPIO\n");
1194 return PTR_ERR(dev->desc[pin]);
1195 }
1196
1197 ret = gpiochip_lock_as_irq(&dev->gc, pin);
1198 if (ret) {
1199 dev_err(dev->gc.parent, "Failed to lock GPIO as interrupt\n");
1200 goto err_desc;
1201 }
1202
1203 ret = gpiod_to_irq(dev->desc[pin]);
1204 if (ret < 0) {
1205 dev_err(dev->gc.parent, "Failed to translate GPIO to IRQ\n");
1206 goto err_lock;
1207 }
1208
1209 return ret;
1210
1211err_lock:
1212 gpiochip_unlock_as_irq(&dev->gc, pin);
1213err_desc:
1214 gpiochip_free_own_desc(dev->desc[pin]);
1215 dev->desc[pin] = NULL;
1216 return ret;
1217}
1218
David Barksdalee932d812014-02-04 12:42:48 -06001219static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id)
1220{
1221 struct cp2112_device *dev;
1222 u8 buf[3];
1223 struct cp2112_smbus_config_report config;
1224 int ret;
1225
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +01001226 dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL);
1227 if (!dev)
1228 return -ENOMEM;
1229
1230 dev->in_out_buffer = devm_kzalloc(&hdev->dev, CP2112_REPORT_MAX_LENGTH,
1231 GFP_KERNEL);
1232 if (!dev->in_out_buffer)
1233 return -ENOMEM;
1234
Johan Hovold7a7b5df2017-01-30 11:26:38 +01001235 mutex_init(&dev->lock);
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +01001236
David Barksdalee932d812014-02-04 12:42:48 -06001237 ret = hid_parse(hdev);
1238 if (ret) {
1239 hid_err(hdev, "parse failed\n");
1240 return ret;
1241 }
1242
1243 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1244 if (ret) {
1245 hid_err(hdev, "hw start failed\n");
1246 return ret;
1247 }
1248
1249 ret = hid_hw_open(hdev);
1250 if (ret) {
1251 hid_err(hdev, "hw open failed\n");
1252 goto err_hid_stop;
1253 }
1254
1255 ret = hid_hw_power(hdev, PM_HINT_FULLON);
1256 if (ret < 0) {
1257 hid_err(hdev, "power management error: %d\n", ret);
1258 goto err_hid_close;
1259 }
1260
1261 ret = cp2112_hid_get(hdev, CP2112_GET_VERSION_INFO, buf, sizeof(buf),
1262 HID_FEATURE_REPORT);
1263 if (ret != sizeof(buf)) {
1264 hid_err(hdev, "error requesting version\n");
1265 if (ret >= 0)
1266 ret = -EIO;
1267 goto err_power_normal;
1268 }
1269
1270 hid_info(hdev, "Part Number: 0x%02X Device Version: 0x%02X\n",
1271 buf[1], buf[2]);
1272
1273 ret = cp2112_hid_get(hdev, CP2112_SMBUS_CONFIG, (u8 *)&config,
1274 sizeof(config), HID_FEATURE_REPORT);
1275 if (ret != sizeof(config)) {
1276 hid_err(hdev, "error requesting SMBus config\n");
1277 if (ret >= 0)
1278 ret = -EIO;
1279 goto err_power_normal;
1280 }
1281
1282 config.retry_time = cpu_to_be16(1);
1283
1284 ret = cp2112_hid_output(hdev, (u8 *)&config, sizeof(config),
1285 HID_FEATURE_REPORT);
1286 if (ret != sizeof(config)) {
1287 hid_err(hdev, "error setting SMBus config\n");
1288 if (ret >= 0)
1289 ret = -EIO;
1290 goto err_power_normal;
1291 }
1292
David Barksdalee932d812014-02-04 12:42:48 -06001293 hid_set_drvdata(hdev, (void *)dev);
1294 dev->hdev = hdev;
1295 dev->adap.owner = THIS_MODULE;
1296 dev->adap.class = I2C_CLASS_HWMON;
1297 dev->adap.algo = &smbus_algorithm;
1298 dev->adap.algo_data = dev;
1299 dev->adap.dev.parent = &hdev->dev;
1300 snprintf(dev->adap.name, sizeof(dev->adap.name),
Jaejoong Kim42cb6b32017-03-03 17:54:00 +09001301 "CP2112 SMBus Bridge on hidraw%d",
1302 ((struct hidraw *)hdev->hidraw)->minor);
Ellen Wang44eda782015-07-09 21:55:06 -07001303 dev->hwversion = buf[2];
David Barksdalee932d812014-02-04 12:42:48 -06001304 init_waitqueue_head(&dev->wait);
1305
1306 hid_device_io_start(hdev);
1307 ret = i2c_add_adapter(&dev->adap);
1308 hid_device_io_stop(hdev);
1309
1310 if (ret) {
1311 hid_err(hdev, "error registering i2c adapter\n");
Benjamin Tissoires1ffb3c42016-11-21 11:48:39 +01001312 goto err_power_normal;
David Barksdalee932d812014-02-04 12:42:48 -06001313 }
1314
1315 hid_dbg(hdev, "adapter registered\n");
1316
1317 dev->gc.label = "cp2112_gpio";
1318 dev->gc.direction_input = cp2112_gpio_direction_input;
1319 dev->gc.direction_output = cp2112_gpio_direction_output;
1320 dev->gc.set = cp2112_gpio_set;
1321 dev->gc.get = cp2112_gpio_get;
1322 dev->gc.base = -1;
1323 dev->gc.ngpio = 8;
1324 dev->gc.can_sleep = 1;
Linus Walleij58383c782015-11-04 09:56:26 +01001325 dev->gc.parent = &hdev->dev;
David Barksdalee932d812014-02-04 12:42:48 -06001326
Linus Walleij47513c22015-12-08 16:26:28 +01001327 ret = gpiochip_add_data(&dev->gc, dev);
David Barksdalee932d812014-02-04 12:42:48 -06001328 if (ret < 0) {
1329 hid_err(hdev, "error registering gpio chip\n");
1330 goto err_free_i2c;
1331 }
1332
1333 ret = sysfs_create_group(&hdev->dev.kobj, &cp2112_attr_group);
1334 if (ret < 0) {
1335 hid_err(hdev, "error creating sysfs attrs\n");
1336 goto err_gpiochip_remove;
1337 }
1338
1339 chmod_sysfs_attrs(hdev);
1340 hid_hw_power(hdev, PM_HINT_NORMAL);
1341
Benjamin Tissoires13de9cc2016-11-21 12:07:54 +01001342 ret = gpiochip_irqchip_add(&dev->gc, &cp2112_gpio_irqchip, 0,
1343 handle_simple_irq, IRQ_TYPE_NONE);
1344 if (ret) {
1345 dev_err(dev->gc.parent, "failed to add IRQ chip\n");
1346 goto err_sysfs_remove;
1347 }
1348
David Barksdalee932d812014-02-04 12:42:48 -06001349 return ret;
1350
Benjamin Tissoires13de9cc2016-11-21 12:07:54 +01001351err_sysfs_remove:
1352 sysfs_remove_group(&hdev->dev.kobj, &cp2112_attr_group);
David Barksdalee932d812014-02-04 12:42:48 -06001353err_gpiochip_remove:
abdoulaye berthe88d5e522014-07-12 22:30:14 +02001354 gpiochip_remove(&dev->gc);
David Barksdalee932d812014-02-04 12:42:48 -06001355err_free_i2c:
1356 i2c_del_adapter(&dev->adap);
David Barksdalee932d812014-02-04 12:42:48 -06001357err_power_normal:
1358 hid_hw_power(hdev, PM_HINT_NORMAL);
1359err_hid_close:
1360 hid_hw_close(hdev);
1361err_hid_stop:
1362 hid_hw_stop(hdev);
1363 return ret;
1364}
1365
1366static void cp2112_remove(struct hid_device *hdev)
1367{
1368 struct cp2112_device *dev = hid_get_drvdata(hdev);
Benjamin Tissoires13de9cc2016-11-21 12:07:54 +01001369 int i;
David Barksdalee932d812014-02-04 12:42:48 -06001370
1371 sysfs_remove_group(&hdev->dev.kobj, &cp2112_attr_group);
David Barksdalee932d812014-02-04 12:42:48 -06001372 i2c_del_adapter(&dev->adap);
Benjamin Tissoires13de9cc2016-11-21 12:07:54 +01001373
1374 if (dev->gpio_poll) {
1375 dev->gpio_poll = false;
1376 cancel_delayed_work_sync(&dev->gpio_poll_worker);
1377 }
1378
1379 for (i = 0; i < ARRAY_SIZE(dev->desc); i++) {
1380 gpiochip_unlock_as_irq(&dev->gc, i);
1381 gpiochip_free_own_desc(dev->desc[i]);
1382 }
1383
1384 gpiochip_remove(&dev->gc);
David Barksdalee932d812014-02-04 12:42:48 -06001385 /* i2c_del_adapter has finished removing all i2c devices from our
1386 * adapter. Well behaved devices should no longer call our cp2112_xfer
1387 * and should have waited for any pending calls to finish. It has also
1388 * waited for device_unregister(&adap->dev) to complete. Therefore we
1389 * can safely free our struct cp2112_device.
1390 */
1391 hid_hw_close(hdev);
1392 hid_hw_stop(hdev);
David Barksdalee932d812014-02-04 12:42:48 -06001393}
1394
1395static int cp2112_raw_event(struct hid_device *hdev, struct hid_report *report,
1396 u8 *data, int size)
1397{
1398 struct cp2112_device *dev = hid_get_drvdata(hdev);
1399 struct cp2112_xfer_status_report *xfer = (void *)data;
1400
1401 switch (data[0]) {
1402 case CP2112_TRANSFER_STATUS_RESPONSE:
1403 hid_dbg(hdev, "xfer status: %02x %02x %04x %04x\n",
1404 xfer->status0, xfer->status1,
1405 be16_to_cpu(xfer->retries), be16_to_cpu(xfer->length));
1406
1407 switch (xfer->status0) {
1408 case STATUS0_IDLE:
1409 dev->xfer_status = -EAGAIN;
1410 break;
1411 case STATUS0_BUSY:
1412 dev->xfer_status = -EBUSY;
1413 break;
1414 case STATUS0_COMPLETE:
1415 dev->xfer_status = be16_to_cpu(xfer->length);
1416 break;
1417 case STATUS0_ERROR:
1418 switch (xfer->status1) {
1419 case STATUS1_TIMEOUT_NACK:
1420 case STATUS1_TIMEOUT_BUS:
1421 dev->xfer_status = -ETIMEDOUT;
1422 break;
1423 default:
1424 dev->xfer_status = -EIO;
1425 break;
1426 }
1427 break;
1428 default:
1429 dev->xfer_status = -EINVAL;
1430 break;
1431 }
1432
1433 atomic_set(&dev->xfer_avail, 1);
1434 break;
1435 case CP2112_DATA_READ_RESPONSE:
1436 hid_dbg(hdev, "read response: %02x %02x\n", data[1], data[2]);
1437
1438 dev->read_length = data[2];
1439 if (dev->read_length > sizeof(dev->read_data))
1440 dev->read_length = sizeof(dev->read_data);
1441
1442 memcpy(dev->read_data, &data[3], dev->read_length);
1443 atomic_set(&dev->read_avail, 1);
1444 break;
1445 default:
1446 hid_err(hdev, "unknown report\n");
1447
1448 return 0;
1449 }
1450
1451 wake_up_interruptible(&dev->wait);
1452 return 1;
1453}
1454
1455static struct hid_driver cp2112_driver = {
1456 .name = "cp2112",
1457 .id_table = cp2112_devices,
1458 .probe = cp2112_probe,
1459 .remove = cp2112_remove,
1460 .raw_event = cp2112_raw_event,
1461};
1462
1463module_hid_driver(cp2112_driver);
1464MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge");
1465MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
1466MODULE_LICENSE("GPL");
1467