1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * hid-cp2112.c - Silicon Labs HID USB to SMBus master bridge
4 * Copyright (c) 2013,2014 Uplogix, Inc.
5 * David Barksdale <dbarksdale@uplogix.com>
6 */
7
8 /*
9 * The Silicon Labs CP2112 chip is a USB HID device which provides an
10 * SMBus controller for talking to slave devices and 8 GPIO pins. The
11 * host communicates with the CP2112 via raw HID reports.
12 *
13 * Data Sheet:
14 * https://www.silabs.com/Support%20Documents/TechnicalDocs/CP2112.pdf
15 * Programming Interface Specification:
16 * https://www.silabs.com/documents/public/application-notes/an495-cp2112-interface-specification.pdf
17 */
18
19 #include <linux/bitops.h>
20 #include <linux/gpio/driver.h>
21 #include <linux/hid.h>
22 #include <linux/hidraw.h>
23 #include <linux/i2c.h>
24 #include <linux/module.h>
25 #include <linux/nls.h>
26 #include <linux/string_choices.h>
27 #include <linux/usb/ch9.h>
28 #include "hid-ids.h"
29
30 #define CP2112_REPORT_MAX_LENGTH 64
31 #define CP2112_GPIO_CONFIG_LENGTH 5
32 #define CP2112_GPIO_GET_LENGTH 2
33 #define CP2112_GPIO_SET_LENGTH 3
34 #define CP2112_GPIO_MAX_GPIO 8
35 #define CP2112_GPIO_ALL_GPIO_MASK GENMASK(7, 0)
36
37 enum {
38 CP2112_GPIO_CONFIG = 0x02,
39 CP2112_GPIO_GET = 0x03,
40 CP2112_GPIO_SET = 0x04,
41 CP2112_GET_VERSION_INFO = 0x05,
42 CP2112_SMBUS_CONFIG = 0x06,
43 CP2112_DATA_READ_REQUEST = 0x10,
44 CP2112_DATA_WRITE_READ_REQUEST = 0x11,
45 CP2112_DATA_READ_FORCE_SEND = 0x12,
46 CP2112_DATA_READ_RESPONSE = 0x13,
47 CP2112_DATA_WRITE_REQUEST = 0x14,
48 CP2112_TRANSFER_STATUS_REQUEST = 0x15,
49 CP2112_TRANSFER_STATUS_RESPONSE = 0x16,
50 CP2112_CANCEL_TRANSFER = 0x17,
51 CP2112_LOCK_BYTE = 0x20,
52 CP2112_USB_CONFIG = 0x21,
53 CP2112_MANUFACTURER_STRING = 0x22,
54 CP2112_PRODUCT_STRING = 0x23,
55 CP2112_SERIAL_STRING = 0x24,
56 };
57
58 enum {
59 STATUS0_IDLE = 0x00,
60 STATUS0_BUSY = 0x01,
61 STATUS0_COMPLETE = 0x02,
62 STATUS0_ERROR = 0x03,
63 };
64
65 enum {
66 STATUS1_TIMEOUT_NACK = 0x00,
67 STATUS1_TIMEOUT_BUS = 0x01,
68 STATUS1_ARBITRATION_LOST = 0x02,
69 STATUS1_READ_INCOMPLETE = 0x03,
70 STATUS1_WRITE_INCOMPLETE = 0x04,
71 STATUS1_SUCCESS = 0x05,
72 };
73
74 struct cp2112_smbus_config_report {
75 u8 report; /* CP2112_SMBUS_CONFIG */
76 __be32 clock_speed; /* Hz */
77 u8 device_address; /* Stored in the upper 7 bits */
78 u8 auto_send_read; /* 1 = enabled, 0 = disabled */
79 __be16 write_timeout; /* ms, 0 = no timeout */
80 __be16 read_timeout; /* ms, 0 = no timeout */
81 u8 scl_low_timeout; /* 1 = enabled, 0 = disabled */
82 __be16 retry_time; /* # of retries, 0 = no limit */
83 } __packed;
84
85 struct cp2112_usb_config_report {
86 u8 report; /* CP2112_USB_CONFIG */
87 __le16 vid; /* Vendor ID */
88 __le16 pid; /* Product ID */
89 u8 max_power; /* Power requested in 2mA units */
90 u8 power_mode; /* 0x00 = bus powered
91 0x01 = self powered & regulator off
92 0x02 = self powered & regulator on */
93 u8 release_major;
94 u8 release_minor;
95 u8 mask; /* What fields to program */
96 } __packed;
97
98 struct cp2112_read_req_report {
99 u8 report; /* CP2112_DATA_READ_REQUEST */
100 u8 slave_address;
101 __be16 length;
102 } __packed;
103
104 struct cp2112_write_read_req_report {
105 u8 report; /* CP2112_DATA_WRITE_READ_REQUEST */
106 u8 slave_address;
107 __be16 length;
108 u8 target_address_length;
109 u8 target_address[16];
110 } __packed;
111
112 struct cp2112_write_req_report {
113 u8 report; /* CP2112_DATA_WRITE_REQUEST */
114 u8 slave_address;
115 u8 length;
116 u8 data[61];
117 } __packed;
118
119 struct cp2112_force_read_report {
120 u8 report; /* CP2112_DATA_READ_FORCE_SEND */
121 __be16 length;
122 } __packed;
123
124 struct cp2112_xfer_status_report {
125 u8 report; /* CP2112_TRANSFER_STATUS_RESPONSE */
126 u8 status0; /* STATUS0_* */
127 u8 status1; /* STATUS1_* */
128 __be16 retries;
129 __be16 length;
130 } __packed;
131
132 struct cp2112_string_report {
133 u8 dummy; /* force .string to be aligned */
134 struct_group_attr(contents, __packed,
135 u8 report; /* CP2112_*_STRING */
136 u8 length; /* length in bytes of everything after .report */
137 u8 type; /* USB_DT_STRING */
138 wchar_t string[30]; /* UTF16_LITTLE_ENDIAN string */
139 );
140 } __packed;
141
142 /* Number of times to request transfer status before giving up waiting for a
143 transfer to complete. This may need to be changed if SMBUS clock, retries,
144 or read/write/scl_low timeout settings are changed. */
145 static const int XFER_STATUS_RETRIES = 10;
146
147 /* Time in ms to wait for a CP2112_DATA_READ_RESPONSE or
148 CP2112_TRANSFER_STATUS_RESPONSE. */
149 static const int RESPONSE_TIMEOUT = 50;
150
151 static const struct hid_device_id cp2112_devices[] = {
152 { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_CP2112) },
153 { }
154 };
155 MODULE_DEVICE_TABLE(hid, cp2112_devices);
156
157 struct cp2112_device {
158 struct i2c_adapter adap;
159 struct hid_device *hdev;
160 wait_queue_head_t wait;
161 u8 read_data[61];
162 u8 read_length;
163 u8 hwversion;
164 int xfer_status;
165 atomic_t read_avail;
166 atomic_t xfer_avail;
167 struct gpio_chip gc;
168 u8 *in_out_buffer;
169 struct mutex lock;
170
171 bool gpio_poll;
172 struct delayed_work gpio_poll_worker;
173 unsigned long irq_mask;
174 u8 gpio_prev_state;
175 };
176
177 static int gpio_push_pull = CP2112_GPIO_ALL_GPIO_MASK;
178 module_param(gpio_push_pull, int, 0644);
179 MODULE_PARM_DESC(gpio_push_pull, "GPIO push-pull configuration bitmask");
180
cp2112_gpio_direction_input(struct gpio_chip * chip,unsigned offset)181 static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
182 {
183 struct cp2112_device *dev = gpiochip_get_data(chip);
184 struct hid_device *hdev = dev->hdev;
185 u8 *buf = dev->in_out_buffer;
186 int ret;
187
188 mutex_lock(&dev->lock);
189
190 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
191 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
192 HID_REQ_GET_REPORT);
193 if (ret != CP2112_GPIO_CONFIG_LENGTH) {
194 hid_err(hdev, "error requesting GPIO config: %d\n", ret);
195 if (ret >= 0)
196 ret = -EIO;
197 goto exit;
198 }
199
200 buf[1] &= ~BIT(offset);
201 buf[2] = gpio_push_pull;
202
203 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
204 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
205 HID_REQ_SET_REPORT);
206 if (ret != CP2112_GPIO_CONFIG_LENGTH) {
207 hid_err(hdev, "error setting GPIO config: %d\n", ret);
208 if (ret >= 0)
209 ret = -EIO;
210 goto exit;
211 }
212
213 ret = 0;
214
215 exit:
216 mutex_unlock(&dev->lock);
217 return ret;
218 }
219
cp2112_gpio_set(struct gpio_chip * chip,unsigned offset,int value)220 static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
221 {
222 struct cp2112_device *dev = gpiochip_get_data(chip);
223 struct hid_device *hdev = dev->hdev;
224 u8 *buf = dev->in_out_buffer;
225 int ret;
226
227 mutex_lock(&dev->lock);
228
229 buf[0] = CP2112_GPIO_SET;
230 buf[1] = value ? CP2112_GPIO_ALL_GPIO_MASK : 0;
231 buf[2] = BIT(offset);
232
233 ret = hid_hw_raw_request(hdev, CP2112_GPIO_SET, buf,
234 CP2112_GPIO_SET_LENGTH, HID_FEATURE_REPORT,
235 HID_REQ_SET_REPORT);
236 if (ret < 0)
237 hid_err(hdev, "error setting GPIO values: %d\n", ret);
238
239 mutex_unlock(&dev->lock);
240 }
241
cp2112_gpio_get_all(struct gpio_chip * chip)242 static int cp2112_gpio_get_all(struct gpio_chip *chip)
243 {
244 struct cp2112_device *dev = gpiochip_get_data(chip);
245 struct hid_device *hdev = dev->hdev;
246 u8 *buf = dev->in_out_buffer;
247 int ret;
248
249 mutex_lock(&dev->lock);
250
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) {
255 hid_err(hdev, "error requesting GPIO values: %d\n", ret);
256 ret = ret < 0 ? ret : -EIO;
257 goto exit;
258 }
259
260 ret = buf[1];
261
262 exit:
263 mutex_unlock(&dev->lock);
264
265 return ret;
266 }
267
cp2112_gpio_get(struct gpio_chip * chip,unsigned int offset)268 static 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
cp2112_gpio_direction_output(struct gpio_chip * chip,unsigned offset,int value)279 static int cp2112_gpio_direction_output(struct gpio_chip *chip,
280 unsigned offset, int value)
281 {
282 struct cp2112_device *dev = gpiochip_get_data(chip);
283 struct hid_device *hdev = dev->hdev;
284 u8 *buf = dev->in_out_buffer;
285 int ret;
286
287 mutex_lock(&dev->lock);
288
289 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
290 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
291 HID_REQ_GET_REPORT);
292 if (ret != CP2112_GPIO_CONFIG_LENGTH) {
293 hid_err(hdev, "error requesting GPIO config: %d\n", ret);
294 goto fail;
295 }
296
297 buf[1] |= 1 << offset;
298 buf[2] = gpio_push_pull;
299
300 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
301 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
302 HID_REQ_SET_REPORT);
303 if (ret < 0) {
304 hid_err(hdev, "error setting GPIO config: %d\n", ret);
305 goto fail;
306 }
307
308 mutex_unlock(&dev->lock);
309
310 /*
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
316 return 0;
317
318 fail:
319 mutex_unlock(&dev->lock);
320 return ret < 0 ? ret : -EIO;
321 }
322
cp2112_hid_get(struct hid_device * hdev,unsigned char report_number,u8 * data,size_t count,unsigned char report_type)323 static 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
333 ret = hid_hw_raw_request(hdev, report_number, buf, count,
334 report_type, HID_REQ_GET_REPORT);
335 memcpy(data, buf, count);
336 kfree(buf);
337 return ret;
338 }
339
cp2112_hid_output(struct hid_device * hdev,u8 * data,size_t count,unsigned char report_type)340 static 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
350 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
356 kfree(buf);
357 return ret;
358 }
359
cp2112_wait(struct cp2112_device * dev,atomic_t * avail)360 static 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
cp2112_xfer_status(struct cp2112_device * dev)383 static 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
cp2112_read(struct cp2112_device * dev,u8 * data,size_t size)406 static 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
412 if (size > sizeof(dev->read_data))
413 size = sizeof(dev->read_data);
414 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
430 hid_dbg(hdev, "read %d of %zd bytes requested\n",
431 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
cp2112_read_req(void * buf,u8 slave_address,u16 length)440 static 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
cp2112_write_read_req(void * buf,u8 slave_address,u16 length,u8 command,u8 * data,u8 data_length)453 static 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
cp2112_write_req(void * buf,u8 slave_address,u8 command,u8 * data,u8 data_length)471 static 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
cp2112_i2c_write_req(void * buf,u8 slave_address,u8 * data,u8 data_length)487 static 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
cp2112_i2c_write_read_req(void * buf,u8 slave_address,u8 * addr,int addr_length,int read_length)502 static 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
cp2112_i2c_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)520 static 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;
527 ssize_t read_length = 0;
528 u8 *read_buf = NULL;
529 unsigned int retries;
530 int ret;
531
532 hid_dbg(hdev, "I2C %d messages\n", num);
533
534 if (num == 1) {
535 hid_dbg(hdev, "I2C %s %#04x len %d\n",
536 str_read_write(msgs->flags & I2C_M_RD), msgs->addr, msgs->len);
537 if (msgs->flags & I2C_M_RD) {
538 read_length = msgs->len;
539 read_buf = msgs->buf;
540 count = cp2112_read_req(buf, msgs->addr, msgs->len);
541 } else {
542 count = cp2112_i2c_write_req(buf, msgs->addr,
543 msgs->buf, msgs->len);
544 }
545 if (count < 0)
546 return count;
547 } else if (dev->hwversion > 1 && /* no repeated start in rev 1 */
548 num == 2 &&
549 msgs[0].addr == msgs[1].addr &&
550 !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) {
551 hid_dbg(hdev, "I2C write-read %#04x wlen %d rlen %d\n",
552 msgs[0].addr, msgs[0].len, msgs[1].len);
553 read_length = msgs[1].len;
554 read_buf = msgs[1].buf;
555 count = cp2112_i2c_write_read_req(buf, msgs[0].addr,
556 msgs[0].buf, msgs[0].len, msgs[1].len);
557 if (count < 0)
558 return count;
559 } else {
560 hid_err(hdev,
561 "Multi-message I2C transactions not supported\n");
562 return -EOPNOTSUPP;
563 }
564
565 ret = hid_hw_power(hdev, PM_HINT_FULLON);
566 if (ret < 0) {
567 hid_err(hdev, "power management error: %d\n", ret);
568 return ret;
569 }
570
571 ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT);
572 if (ret < 0) {
573 hid_warn(hdev, "Error starting transaction: %d\n", ret);
574 goto power_normal;
575 }
576
577 for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) {
578 ret = cp2112_xfer_status(dev);
579 if (-EBUSY == ret)
580 continue;
581 if (ret < 0)
582 goto power_normal;
583 break;
584 }
585
586 if (XFER_STATUS_RETRIES <= retries) {
587 hid_warn(hdev, "Transfer timed out, cancelling.\n");
588 buf[0] = CP2112_CANCEL_TRANSFER;
589 buf[1] = 0x01;
590
591 ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
592 if (ret < 0)
593 hid_warn(hdev, "Error cancelling transaction: %d\n",
594 ret);
595
596 ret = -ETIMEDOUT;
597 goto power_normal;
598 }
599
600 for (count = 0; count < read_length;) {
601 ret = cp2112_read(dev, read_buf + count, read_length - count);
602 if (ret < 0)
603 goto power_normal;
604 if (ret == 0) {
605 hid_err(hdev, "read returned 0\n");
606 ret = -EIO;
607 goto power_normal;
608 }
609 count += ret;
610 if (count > read_length) {
611 /*
612 * The hardware returned too much data.
613 * This is mostly harmless because cp2112_read()
614 * has a limit check so didn't overrun our
615 * buffer. Nevertheless, we return an error
616 * because something is seriously wrong and
617 * it shouldn't go unnoticed.
618 */
619 hid_err(hdev, "long read: %d > %zd\n",
620 ret, read_length - count + ret);
621 ret = -EIO;
622 goto power_normal;
623 }
624 }
625
626 /* return the number of transferred messages */
627 ret = num;
628
629 power_normal:
630 hid_hw_power(hdev, PM_HINT_NORMAL);
631 hid_dbg(hdev, "I2C transfer finished: %d\n", ret);
632 return ret;
633 }
634
cp2112_xfer(struct i2c_adapter * adap,u16 addr,unsigned short flags,char read_write,u8 command,int size,union i2c_smbus_data * data)635 static int cp2112_xfer(struct i2c_adapter *adap, u16 addr,
636 unsigned short flags, char read_write, u8 command,
637 int size, union i2c_smbus_data *data)
638 {
639 struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data;
640 struct hid_device *hdev = dev->hdev;
641 u8 buf[64];
642 __le16 word;
643 ssize_t count;
644 size_t read_length = 0;
645 unsigned int retries;
646 int ret;
647
648 hid_dbg(hdev, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n",
649 str_write_read(read_write == I2C_SMBUS_WRITE),
650 addr, flags, command, size);
651
652 switch (size) {
653 case I2C_SMBUS_BYTE:
654 read_length = 1;
655
656 if (I2C_SMBUS_READ == read_write)
657 count = cp2112_read_req(buf, addr, read_length);
658 else
659 count = cp2112_write_req(buf, addr, command, NULL,
660 0);
661 break;
662 case I2C_SMBUS_BYTE_DATA:
663 read_length = 1;
664
665 if (I2C_SMBUS_READ == read_write)
666 count = cp2112_write_read_req(buf, addr, read_length,
667 command, NULL, 0);
668 else
669 count = cp2112_write_req(buf, addr, command,
670 &data->byte, 1);
671 break;
672 case I2C_SMBUS_WORD_DATA:
673 read_length = 2;
674 word = cpu_to_le16(data->word);
675
676 if (I2C_SMBUS_READ == read_write)
677 count = cp2112_write_read_req(buf, addr, read_length,
678 command, NULL, 0);
679 else
680 count = cp2112_write_req(buf, addr, command,
681 (u8 *)&word, 2);
682 break;
683 case I2C_SMBUS_PROC_CALL:
684 size = I2C_SMBUS_WORD_DATA;
685 read_write = I2C_SMBUS_READ;
686 read_length = 2;
687 word = cpu_to_le16(data->word);
688
689 count = cp2112_write_read_req(buf, addr, read_length, command,
690 (u8 *)&word, 2);
691 break;
692 case I2C_SMBUS_I2C_BLOCK_DATA:
693 if (read_write == I2C_SMBUS_READ) {
694 read_length = data->block[0];
695 count = cp2112_write_read_req(buf, addr, read_length,
696 command, NULL, 0);
697 } else {
698 count = cp2112_write_req(buf, addr, command,
699 data->block + 1,
700 data->block[0]);
701 }
702 break;
703 case I2C_SMBUS_BLOCK_DATA:
704 if (I2C_SMBUS_READ == read_write) {
705 count = cp2112_write_read_req(buf, addr,
706 I2C_SMBUS_BLOCK_MAX,
707 command, NULL, 0);
708 } else {
709 count = cp2112_write_req(buf, addr, command,
710 data->block,
711 data->block[0] + 1);
712 }
713 break;
714 case I2C_SMBUS_BLOCK_PROC_CALL:
715 size = I2C_SMBUS_BLOCK_DATA;
716 read_write = I2C_SMBUS_READ;
717
718 count = cp2112_write_read_req(buf, addr, I2C_SMBUS_BLOCK_MAX,
719 command, data->block,
720 data->block[0] + 1);
721 break;
722 default:
723 hid_warn(hdev, "Unsupported transaction %d\n", size);
724 return -EOPNOTSUPP;
725 }
726
727 if (count < 0)
728 return count;
729
730 ret = hid_hw_power(hdev, PM_HINT_FULLON);
731 if (ret < 0) {
732 hid_err(hdev, "power management error: %d\n", ret);
733 return ret;
734 }
735
736 ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT);
737 if (ret < 0) {
738 hid_warn(hdev, "Error starting transaction: %d\n", ret);
739 goto power_normal;
740 }
741
742 for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) {
743 ret = cp2112_xfer_status(dev);
744 if (-EBUSY == ret)
745 continue;
746 if (ret < 0)
747 goto power_normal;
748 break;
749 }
750
751 if (XFER_STATUS_RETRIES <= retries) {
752 hid_warn(hdev, "Transfer timed out, cancelling.\n");
753 buf[0] = CP2112_CANCEL_TRANSFER;
754 buf[1] = 0x01;
755
756 ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
757 if (ret < 0)
758 hid_warn(hdev, "Error cancelling transaction: %d\n",
759 ret);
760
761 ret = -ETIMEDOUT;
762 goto power_normal;
763 }
764
765 if (I2C_SMBUS_WRITE == read_write) {
766 ret = 0;
767 goto power_normal;
768 }
769
770 if (I2C_SMBUS_BLOCK_DATA == size)
771 read_length = ret;
772
773 ret = cp2112_read(dev, buf, read_length);
774 if (ret < 0)
775 goto power_normal;
776 if (ret != read_length) {
777 hid_warn(hdev, "short read: %d < %zd\n", ret, read_length);
778 ret = -EIO;
779 goto power_normal;
780 }
781
782 switch (size) {
783 case I2C_SMBUS_BYTE:
784 case I2C_SMBUS_BYTE_DATA:
785 data->byte = buf[0];
786 break;
787 case I2C_SMBUS_WORD_DATA:
788 data->word = le16_to_cpup((__le16 *)buf);
789 break;
790 case I2C_SMBUS_I2C_BLOCK_DATA:
791 if (read_length > I2C_SMBUS_BLOCK_MAX) {
792 ret = -EINVAL;
793 goto power_normal;
794 }
795
796 memcpy(data->block + 1, buf, read_length);
797 break;
798 case I2C_SMBUS_BLOCK_DATA:
799 if (read_length > I2C_SMBUS_BLOCK_MAX) {
800 ret = -EPROTO;
801 goto power_normal;
802 }
803
804 memcpy(data->block, buf, read_length);
805 break;
806 }
807
808 ret = 0;
809 power_normal:
810 hid_hw_power(hdev, PM_HINT_NORMAL);
811 hid_dbg(hdev, "transfer finished: %d\n", ret);
812 return ret;
813 }
814
cp2112_functionality(struct i2c_adapter * adap)815 static u32 cp2112_functionality(struct i2c_adapter *adap)
816 {
817 return I2C_FUNC_I2C |
818 I2C_FUNC_SMBUS_BYTE |
819 I2C_FUNC_SMBUS_BYTE_DATA |
820 I2C_FUNC_SMBUS_WORD_DATA |
821 I2C_FUNC_SMBUS_BLOCK_DATA |
822 I2C_FUNC_SMBUS_I2C_BLOCK |
823 I2C_FUNC_SMBUS_PROC_CALL |
824 I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
825 }
826
827 static const struct i2c_algorithm smbus_algorithm = {
828 .master_xfer = cp2112_i2c_xfer,
829 .smbus_xfer = cp2112_xfer,
830 .functionality = cp2112_functionality,
831 };
832
cp2112_get_usb_config(struct hid_device * hdev,struct cp2112_usb_config_report * cfg)833 static int cp2112_get_usb_config(struct hid_device *hdev,
834 struct cp2112_usb_config_report *cfg)
835 {
836 int ret;
837
838 ret = cp2112_hid_get(hdev, CP2112_USB_CONFIG, (u8 *)cfg, sizeof(*cfg),
839 HID_FEATURE_REPORT);
840 if (ret != sizeof(*cfg)) {
841 hid_err(hdev, "error reading usb config: %d\n", ret);
842 if (ret < 0)
843 return ret;
844 return -EIO;
845 }
846
847 return 0;
848 }
849
cp2112_set_usb_config(struct hid_device * hdev,struct cp2112_usb_config_report * cfg)850 static int cp2112_set_usb_config(struct hid_device *hdev,
851 struct cp2112_usb_config_report *cfg)
852 {
853 int ret;
854
855 if (WARN_ON(cfg->report != CP2112_USB_CONFIG))
856 return -EINVAL;
857
858 ret = cp2112_hid_output(hdev, (u8 *)cfg, sizeof(*cfg),
859 HID_FEATURE_REPORT);
860 if (ret != sizeof(*cfg)) {
861 hid_err(hdev, "error writing usb config: %d\n", ret);
862 if (ret < 0)
863 return ret;
864 return -EIO;
865 }
866
867 return 0;
868 }
869
870 static void chmod_sysfs_attrs(struct hid_device *hdev);
871
872 #define CP2112_CONFIG_ATTR(name, store, format, ...) \
873 static ssize_t name##_store(struct device *kdev, \
874 struct device_attribute *attr, const char *buf, \
875 size_t count) \
876 { \
877 struct hid_device *hdev = to_hid_device(kdev); \
878 struct cp2112_usb_config_report cfg; \
879 int ret = cp2112_get_usb_config(hdev, &cfg); \
880 if (ret) \
881 return ret; \
882 store; \
883 ret = cp2112_set_usb_config(hdev, &cfg); \
884 if (ret) \
885 return ret; \
886 chmod_sysfs_attrs(hdev); \
887 return count; \
888 } \
889 static ssize_t name##_show(struct device *kdev, \
890 struct device_attribute *attr, char *buf) \
891 { \
892 struct hid_device *hdev = to_hid_device(kdev); \
893 struct cp2112_usb_config_report cfg; \
894 int ret = cp2112_get_usb_config(hdev, &cfg); \
895 if (ret) \
896 return ret; \
897 return sysfs_emit(buf, format, ##__VA_ARGS__); \
898 } \
899 static DEVICE_ATTR_RW(name);
900
901 CP2112_CONFIG_ATTR(vendor_id, ({
902 u16 vid;
903
904 if (sscanf(buf, "%hi", &vid) != 1)
905 return -EINVAL;
906
907 cfg.vid = cpu_to_le16(vid);
908 cfg.mask = 0x01;
909 }), "0x%04x\n", le16_to_cpu(cfg.vid));
910
911 CP2112_CONFIG_ATTR(product_id, ({
912 u16 pid;
913
914 if (sscanf(buf, "%hi", &pid) != 1)
915 return -EINVAL;
916
917 cfg.pid = cpu_to_le16(pid);
918 cfg.mask = 0x02;
919 }), "0x%04x\n", le16_to_cpu(cfg.pid));
920
921 CP2112_CONFIG_ATTR(max_power, ({
922 int mA;
923
924 if (sscanf(buf, "%i", &mA) != 1)
925 return -EINVAL;
926
927 cfg.max_power = (mA + 1) / 2;
928 cfg.mask = 0x04;
929 }), "%u mA\n", cfg.max_power * 2);
930
931 CP2112_CONFIG_ATTR(power_mode, ({
932 if (sscanf(buf, "%hhi", &cfg.power_mode) != 1)
933 return -EINVAL;
934
935 cfg.mask = 0x08;
936 }), "%u\n", cfg.power_mode);
937
938 CP2112_CONFIG_ATTR(release_version, ({
939 if (sscanf(buf, "%hhi.%hhi", &cfg.release_major, &cfg.release_minor)
940 != 2)
941 return -EINVAL;
942
943 cfg.mask = 0x10;
944 }), "%u.%u\n", cfg.release_major, cfg.release_minor);
945
946 #undef CP2112_CONFIG_ATTR
947
pstr_store(struct device * kdev,struct device_attribute * kattr,const char * buf,size_t count,int number)948 static ssize_t pstr_store(struct device *kdev, struct device_attribute *kattr,
949 const char *buf, size_t count, int number)
950 {
951 struct hid_device *hdev = to_hid_device(kdev);
952 struct cp2112_string_report report;
953 int ret;
954
955 memset(&report, 0, sizeof(report));
956
957 ret = utf8s_to_utf16s(buf, count, UTF16_LITTLE_ENDIAN,
958 report.string, ARRAY_SIZE(report.string));
959 report.report = number;
960 report.length = ret * sizeof(report.string[0]) + 2;
961 report.type = USB_DT_STRING;
962
963 ret = cp2112_hid_output(hdev, &report.report, report.length + 1,
964 HID_FEATURE_REPORT);
965 if (ret != report.length + 1) {
966 hid_err(hdev, "error writing %s string: %d\n", kattr->attr.name,
967 ret);
968 if (ret < 0)
969 return ret;
970 return -EIO;
971 }
972
973 chmod_sysfs_attrs(hdev);
974 return count;
975 }
976
pstr_show(struct device * kdev,struct device_attribute * kattr,char * buf,int number)977 static ssize_t pstr_show(struct device *kdev, struct device_attribute *kattr,
978 char *buf, int number)
979 {
980 struct hid_device *hdev = to_hid_device(kdev);
981 struct cp2112_string_report report;
982 u8 length;
983 int ret;
984
985 ret = cp2112_hid_get(hdev, number, (u8 *)&report.contents,
986 sizeof(report.contents), HID_FEATURE_REPORT);
987 if (ret < 3) {
988 hid_err(hdev, "error reading %s string: %d\n", kattr->attr.name,
989 ret);
990 if (ret < 0)
991 return ret;
992 return -EIO;
993 }
994
995 if (report.length < 2) {
996 hid_err(hdev, "invalid %s string length: %d\n",
997 kattr->attr.name, report.length);
998 return -EIO;
999 }
1000
1001 length = report.length > ret - 1 ? ret - 1 : report.length;
1002 length = (length - 2) / sizeof(report.string[0]);
1003 ret = utf16s_to_utf8s(report.string, length, UTF16_LITTLE_ENDIAN, buf,
1004 PAGE_SIZE - 1);
1005 buf[ret++] = '\n';
1006 return ret;
1007 }
1008
1009 #define CP2112_PSTR_ATTR(name, _report) \
1010 static ssize_t name##_store(struct device *kdev, struct device_attribute *kattr, \
1011 const char *buf, size_t count) \
1012 { \
1013 return pstr_store(kdev, kattr, buf, count, _report); \
1014 } \
1015 static ssize_t name##_show(struct device *kdev, struct device_attribute *kattr, char *buf) \
1016 { \
1017 return pstr_show(kdev, kattr, buf, _report); \
1018 } \
1019 static DEVICE_ATTR_RW(name);
1020
1021 CP2112_PSTR_ATTR(manufacturer, CP2112_MANUFACTURER_STRING);
1022 CP2112_PSTR_ATTR(product, CP2112_PRODUCT_STRING);
1023 CP2112_PSTR_ATTR(serial, CP2112_SERIAL_STRING);
1024
1025 #undef CP2112_PSTR_ATTR
1026
1027 static const struct attribute_group cp2112_attr_group = {
1028 .attrs = (struct attribute *[]){
1029 &dev_attr_vendor_id.attr,
1030 &dev_attr_product_id.attr,
1031 &dev_attr_max_power.attr,
1032 &dev_attr_power_mode.attr,
1033 &dev_attr_release_version.attr,
1034 &dev_attr_manufacturer.attr,
1035 &dev_attr_product.attr,
1036 &dev_attr_serial.attr,
1037 NULL
1038 }
1039 };
1040
1041 /* Chmoding our sysfs attributes is simply a way to expose which fields in the
1042 * PROM have already been programmed. We do not depend on this preventing
1043 * writing to these attributes since the CP2112 will simply ignore writes to
1044 * already-programmed fields. This is why there is no sense in fixing this
1045 * racy behaviour.
1046 */
chmod_sysfs_attrs(struct hid_device * hdev)1047 static void chmod_sysfs_attrs(struct hid_device *hdev)
1048 {
1049 struct attribute **attr;
1050 u8 buf[2];
1051 int ret;
1052
1053 ret = cp2112_hid_get(hdev, CP2112_LOCK_BYTE, buf, sizeof(buf),
1054 HID_FEATURE_REPORT);
1055 if (ret != sizeof(buf)) {
1056 hid_err(hdev, "error reading lock byte: %d\n", ret);
1057 return;
1058 }
1059
1060 for (attr = cp2112_attr_group.attrs; *attr; ++attr) {
1061 umode_t mode = (buf[1] & 1) ? 0644 : 0444;
1062 ret = sysfs_chmod_file(&hdev->dev.kobj, *attr, mode);
1063 if (ret < 0)
1064 hid_err(hdev, "error chmoding sysfs file %s\n",
1065 (*attr)->name);
1066 buf[1] >>= 1;
1067 }
1068 }
1069
cp2112_gpio_irq_ack(struct irq_data * d)1070 static void cp2112_gpio_irq_ack(struct irq_data *d)
1071 {
1072 }
1073
cp2112_gpio_irq_mask(struct irq_data * d)1074 static void cp2112_gpio_irq_mask(struct irq_data *d)
1075 {
1076 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1077 struct cp2112_device *dev = gpiochip_get_data(gc);
1078 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1079
1080 __clear_bit(hwirq, &dev->irq_mask);
1081 gpiochip_disable_irq(gc, hwirq);
1082 }
1083
cp2112_gpio_irq_unmask(struct irq_data * d)1084 static void cp2112_gpio_irq_unmask(struct irq_data *d)
1085 {
1086 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1087 struct cp2112_device *dev = gpiochip_get_data(gc);
1088 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1089
1090 gpiochip_enable_irq(gc, hwirq);
1091 __set_bit(hwirq, &dev->irq_mask);
1092 }
1093
cp2112_gpio_poll_callback(struct work_struct * work)1094 static void cp2112_gpio_poll_callback(struct work_struct *work)
1095 {
1096 struct cp2112_device *dev = container_of(work, struct cp2112_device,
1097 gpio_poll_worker.work);
1098 u8 gpio_mask;
1099 u32 irq_type;
1100 int irq, virq, ret;
1101
1102 ret = cp2112_gpio_get_all(&dev->gc);
1103 if (ret == -ENODEV) /* the hardware has been disconnected */
1104 return;
1105 if (ret < 0)
1106 goto exit;
1107
1108 gpio_mask = ret;
1109 for_each_set_bit(virq, &dev->irq_mask, CP2112_GPIO_MAX_GPIO) {
1110 irq = irq_find_mapping(dev->gc.irq.domain, virq);
1111 if (!irq)
1112 continue;
1113
1114 irq_type = irq_get_trigger_type(irq);
1115 if (!irq_type)
1116 continue;
1117
1118 if (gpio_mask & BIT(virq)) {
1119 /* Level High */
1120
1121 if (irq_type & IRQ_TYPE_LEVEL_HIGH)
1122 handle_nested_irq(irq);
1123
1124 if ((irq_type & IRQ_TYPE_EDGE_RISING) &&
1125 !(dev->gpio_prev_state & BIT(virq)))
1126 handle_nested_irq(irq);
1127 } else {
1128 /* Level Low */
1129
1130 if (irq_type & IRQ_TYPE_LEVEL_LOW)
1131 handle_nested_irq(irq);
1132
1133 if ((irq_type & IRQ_TYPE_EDGE_FALLING) &&
1134 (dev->gpio_prev_state & BIT(virq)))
1135 handle_nested_irq(irq);
1136 }
1137 }
1138
1139 dev->gpio_prev_state = gpio_mask;
1140
1141 exit:
1142 if (dev->gpio_poll)
1143 schedule_delayed_work(&dev->gpio_poll_worker, 10);
1144 }
1145
1146
cp2112_gpio_irq_startup(struct irq_data * d)1147 static unsigned int cp2112_gpio_irq_startup(struct irq_data *d)
1148 {
1149 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1150 struct cp2112_device *dev = gpiochip_get_data(gc);
1151
1152 if (!dev->gpio_poll) {
1153 dev->gpio_poll = true;
1154 schedule_delayed_work(&dev->gpio_poll_worker, 0);
1155 }
1156
1157 cp2112_gpio_irq_unmask(d);
1158 return 0;
1159 }
1160
cp2112_gpio_irq_shutdown(struct irq_data * d)1161 static void cp2112_gpio_irq_shutdown(struct irq_data *d)
1162 {
1163 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1164 struct cp2112_device *dev = gpiochip_get_data(gc);
1165
1166 cp2112_gpio_irq_mask(d);
1167
1168 if (!dev->irq_mask) {
1169 dev->gpio_poll = false;
1170 cancel_delayed_work_sync(&dev->gpio_poll_worker);
1171 }
1172 }
1173
cp2112_gpio_irq_type(struct irq_data * d,unsigned int type)1174 static int cp2112_gpio_irq_type(struct irq_data *d, unsigned int type)
1175 {
1176 return 0;
1177 }
1178
1179 static const struct irq_chip cp2112_gpio_irqchip = {
1180 .name = "cp2112-gpio",
1181 .irq_startup = cp2112_gpio_irq_startup,
1182 .irq_shutdown = cp2112_gpio_irq_shutdown,
1183 .irq_ack = cp2112_gpio_irq_ack,
1184 .irq_mask = cp2112_gpio_irq_mask,
1185 .irq_unmask = cp2112_gpio_irq_unmask,
1186 .irq_set_type = cp2112_gpio_irq_type,
1187 .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_IMMUTABLE,
1188 GPIOCHIP_IRQ_RESOURCE_HELPERS,
1189 };
1190
cp2112_probe(struct hid_device * hdev,const struct hid_device_id * id)1191 static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id)
1192 {
1193 struct cp2112_device *dev;
1194 u8 buf[3];
1195 struct cp2112_smbus_config_report config;
1196 struct gpio_irq_chip *girq;
1197 int ret;
1198
1199 dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL);
1200 if (!dev)
1201 return -ENOMEM;
1202
1203 dev->in_out_buffer = devm_kzalloc(&hdev->dev, CP2112_REPORT_MAX_LENGTH,
1204 GFP_KERNEL);
1205 if (!dev->in_out_buffer)
1206 return -ENOMEM;
1207
1208 mutex_init(&dev->lock);
1209
1210 ret = hid_parse(hdev);
1211 if (ret) {
1212 hid_err(hdev, "parse failed\n");
1213 return ret;
1214 }
1215
1216 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1217 if (ret) {
1218 hid_err(hdev, "hw start failed\n");
1219 return ret;
1220 }
1221
1222 ret = hid_hw_open(hdev);
1223 if (ret) {
1224 hid_err(hdev, "hw open failed\n");
1225 goto err_hid_stop;
1226 }
1227
1228 ret = hid_hw_power(hdev, PM_HINT_FULLON);
1229 if (ret < 0) {
1230 hid_err(hdev, "power management error: %d\n", ret);
1231 goto err_hid_close;
1232 }
1233
1234 ret = cp2112_hid_get(hdev, CP2112_GET_VERSION_INFO, buf, sizeof(buf),
1235 HID_FEATURE_REPORT);
1236 if (ret != sizeof(buf)) {
1237 hid_err(hdev, "error requesting version\n");
1238 if (ret >= 0)
1239 ret = -EIO;
1240 goto err_power_normal;
1241 }
1242
1243 hid_info(hdev, "Part Number: 0x%02X Device Version: 0x%02X\n",
1244 buf[1], buf[2]);
1245
1246 ret = cp2112_hid_get(hdev, CP2112_SMBUS_CONFIG, (u8 *)&config,
1247 sizeof(config), HID_FEATURE_REPORT);
1248 if (ret != sizeof(config)) {
1249 hid_err(hdev, "error requesting SMBus config\n");
1250 if (ret >= 0)
1251 ret = -EIO;
1252 goto err_power_normal;
1253 }
1254
1255 config.retry_time = cpu_to_be16(1);
1256
1257 ret = cp2112_hid_output(hdev, (u8 *)&config, sizeof(config),
1258 HID_FEATURE_REPORT);
1259 if (ret != sizeof(config)) {
1260 hid_err(hdev, "error setting SMBus config\n");
1261 if (ret >= 0)
1262 ret = -EIO;
1263 goto err_power_normal;
1264 }
1265
1266 hid_set_drvdata(hdev, (void *)dev);
1267 dev->hdev = hdev;
1268 dev->adap.owner = THIS_MODULE;
1269 dev->adap.class = I2C_CLASS_HWMON;
1270 dev->adap.algo = &smbus_algorithm;
1271 dev->adap.algo_data = dev;
1272 dev->adap.dev.parent = &hdev->dev;
1273 snprintf(dev->adap.name, sizeof(dev->adap.name),
1274 "CP2112 SMBus Bridge on hidraw%d",
1275 ((struct hidraw *)hdev->hidraw)->minor);
1276 dev->hwversion = buf[2];
1277 init_waitqueue_head(&dev->wait);
1278
1279 hid_device_io_start(hdev);
1280 ret = i2c_add_adapter(&dev->adap);
1281 hid_device_io_stop(hdev);
1282
1283 if (ret) {
1284 hid_err(hdev, "error registering i2c adapter\n");
1285 goto err_power_normal;
1286 }
1287
1288 hid_dbg(hdev, "adapter registered\n");
1289
1290 dev->gc.label = "cp2112_gpio";
1291 dev->gc.direction_input = cp2112_gpio_direction_input;
1292 dev->gc.direction_output = cp2112_gpio_direction_output;
1293 dev->gc.set = cp2112_gpio_set;
1294 dev->gc.get = cp2112_gpio_get;
1295 dev->gc.base = -1;
1296 dev->gc.ngpio = CP2112_GPIO_MAX_GPIO;
1297 dev->gc.can_sleep = 1;
1298 dev->gc.parent = &hdev->dev;
1299
1300 girq = &dev->gc.irq;
1301 gpio_irq_chip_set_chip(girq, &cp2112_gpio_irqchip);
1302 /* The event comes from the outside so no parent handler */
1303 girq->parent_handler = NULL;
1304 girq->num_parents = 0;
1305 girq->parents = NULL;
1306 girq->default_type = IRQ_TYPE_NONE;
1307 girq->handler = handle_simple_irq;
1308 girq->threaded = true;
1309
1310 INIT_DELAYED_WORK(&dev->gpio_poll_worker, cp2112_gpio_poll_callback);
1311
1312 ret = gpiochip_add_data(&dev->gc, dev);
1313 if (ret < 0) {
1314 hid_err(hdev, "error registering gpio chip\n");
1315 goto err_free_i2c;
1316 }
1317
1318 ret = sysfs_create_group(&hdev->dev.kobj, &cp2112_attr_group);
1319 if (ret < 0) {
1320 hid_err(hdev, "error creating sysfs attrs\n");
1321 goto err_gpiochip_remove;
1322 }
1323
1324 chmod_sysfs_attrs(hdev);
1325 hid_hw_power(hdev, PM_HINT_NORMAL);
1326
1327 return ret;
1328
1329 err_gpiochip_remove:
1330 gpiochip_remove(&dev->gc);
1331 err_free_i2c:
1332 i2c_del_adapter(&dev->adap);
1333 err_power_normal:
1334 hid_hw_power(hdev, PM_HINT_NORMAL);
1335 err_hid_close:
1336 hid_hw_close(hdev);
1337 err_hid_stop:
1338 hid_hw_stop(hdev);
1339 return ret;
1340 }
1341
cp2112_remove(struct hid_device * hdev)1342 static void cp2112_remove(struct hid_device *hdev)
1343 {
1344 struct cp2112_device *dev = hid_get_drvdata(hdev);
1345
1346 sysfs_remove_group(&hdev->dev.kobj, &cp2112_attr_group);
1347 i2c_del_adapter(&dev->adap);
1348
1349 if (dev->gpio_poll) {
1350 dev->gpio_poll = false;
1351 cancel_delayed_work_sync(&dev->gpio_poll_worker);
1352 }
1353
1354 gpiochip_remove(&dev->gc);
1355 /* i2c_del_adapter has finished removing all i2c devices from our
1356 * adapter. Well behaved devices should no longer call our cp2112_xfer
1357 * and should have waited for any pending calls to finish. It has also
1358 * waited for device_unregister(&adap->dev) to complete. Therefore we
1359 * can safely free our struct cp2112_device.
1360 */
1361 hid_hw_close(hdev);
1362 hid_hw_stop(hdev);
1363 }
1364
cp2112_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * data,int size)1365 static int cp2112_raw_event(struct hid_device *hdev, struct hid_report *report,
1366 u8 *data, int size)
1367 {
1368 struct cp2112_device *dev = hid_get_drvdata(hdev);
1369 struct cp2112_xfer_status_report *xfer = (void *)data;
1370
1371 switch (data[0]) {
1372 case CP2112_TRANSFER_STATUS_RESPONSE:
1373 hid_dbg(hdev, "xfer status: %02x %02x %04x %04x\n",
1374 xfer->status0, xfer->status1,
1375 be16_to_cpu(xfer->retries), be16_to_cpu(xfer->length));
1376
1377 switch (xfer->status0) {
1378 case STATUS0_IDLE:
1379 dev->xfer_status = -EAGAIN;
1380 break;
1381 case STATUS0_BUSY:
1382 dev->xfer_status = -EBUSY;
1383 break;
1384 case STATUS0_COMPLETE:
1385 dev->xfer_status = be16_to_cpu(xfer->length);
1386 break;
1387 case STATUS0_ERROR:
1388 switch (xfer->status1) {
1389 case STATUS1_TIMEOUT_NACK:
1390 case STATUS1_TIMEOUT_BUS:
1391 dev->xfer_status = -ETIMEDOUT;
1392 break;
1393 default:
1394 dev->xfer_status = -EIO;
1395 break;
1396 }
1397 break;
1398 default:
1399 dev->xfer_status = -EINVAL;
1400 break;
1401 }
1402
1403 atomic_set(&dev->xfer_avail, 1);
1404 break;
1405 case CP2112_DATA_READ_RESPONSE:
1406 hid_dbg(hdev, "read response: %02x %02x\n", data[1], data[2]);
1407
1408 dev->read_length = data[2];
1409 if (dev->read_length > sizeof(dev->read_data))
1410 dev->read_length = sizeof(dev->read_data);
1411
1412 memcpy(dev->read_data, &data[3], dev->read_length);
1413 atomic_set(&dev->read_avail, 1);
1414 break;
1415 default:
1416 hid_err(hdev, "unknown report\n");
1417
1418 return 0;
1419 }
1420
1421 wake_up_interruptible(&dev->wait);
1422 return 1;
1423 }
1424
1425 static struct hid_driver cp2112_driver = {
1426 .name = "cp2112",
1427 .id_table = cp2112_devices,
1428 .probe = cp2112_probe,
1429 .remove = cp2112_remove,
1430 .raw_event = cp2112_raw_event,
1431 };
1432
1433 module_hid_driver(cp2112_driver);
1434 MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge");
1435 MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
1436 MODULE_LICENSE("GPL");
1437
1438