GNU Linux-libre 5.10.219-gnu1
[releases.git] / drivers / hid / hid-cp2112.c
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/gpio/consumer.h>
20 #include <linux/gpio/machine.h>
21 #include <linux/gpio/driver.h>
22 #include <linux/hid.h>
23 #include <linux/hidraw.h>
24 #include <linux/i2c.h>
25 #include <linux/module.h>
26 #include <linux/nls.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
35 enum {
36         CP2112_GPIO_CONFIG              = 0x02,
37         CP2112_GPIO_GET                 = 0x03,
38         CP2112_GPIO_SET                 = 0x04,
39         CP2112_GET_VERSION_INFO         = 0x05,
40         CP2112_SMBUS_CONFIG             = 0x06,
41         CP2112_DATA_READ_REQUEST        = 0x10,
42         CP2112_DATA_WRITE_READ_REQUEST  = 0x11,
43         CP2112_DATA_READ_FORCE_SEND     = 0x12,
44         CP2112_DATA_READ_RESPONSE       = 0x13,
45         CP2112_DATA_WRITE_REQUEST       = 0x14,
46         CP2112_TRANSFER_STATUS_REQUEST  = 0x15,
47         CP2112_TRANSFER_STATUS_RESPONSE = 0x16,
48         CP2112_CANCEL_TRANSFER          = 0x17,
49         CP2112_LOCK_BYTE                = 0x20,
50         CP2112_USB_CONFIG               = 0x21,
51         CP2112_MANUFACTURER_STRING      = 0x22,
52         CP2112_PRODUCT_STRING           = 0x23,
53         CP2112_SERIAL_STRING            = 0x24,
54 };
55
56 enum {
57         STATUS0_IDLE            = 0x00,
58         STATUS0_BUSY            = 0x01,
59         STATUS0_COMPLETE        = 0x02,
60         STATUS0_ERROR           = 0x03,
61 };
62
63 enum {
64         STATUS1_TIMEOUT_NACK            = 0x00,
65         STATUS1_TIMEOUT_BUS             = 0x01,
66         STATUS1_ARBITRATION_LOST        = 0x02,
67         STATUS1_READ_INCOMPLETE         = 0x03,
68         STATUS1_WRITE_INCOMPLETE        = 0x04,
69         STATUS1_SUCCESS                 = 0x05,
70 };
71
72 struct cp2112_smbus_config_report {
73         u8 report;              /* CP2112_SMBUS_CONFIG */
74         __be32 clock_speed;     /* Hz */
75         u8 device_address;      /* Stored in the upper 7 bits */
76         u8 auto_send_read;      /* 1 = enabled, 0 = disabled */
77         __be16 write_timeout;   /* ms, 0 = no timeout */
78         __be16 read_timeout;    /* ms, 0 = no timeout */
79         u8 scl_low_timeout;     /* 1 = enabled, 0 = disabled */
80         __be16 retry_time;      /* # of retries, 0 = no limit */
81 } __packed;
82
83 struct cp2112_usb_config_report {
84         u8 report;      /* CP2112_USB_CONFIG */
85         __le16 vid;     /* Vendor ID */
86         __le16 pid;     /* Product ID */
87         u8 max_power;   /* Power requested in 2mA units */
88         u8 power_mode;  /* 0x00 = bus powered
89                            0x01 = self powered & regulator off
90                            0x02 = self powered & regulator on */
91         u8 release_major;
92         u8 release_minor;
93         u8 mask;        /* What fields to program */
94 } __packed;
95
96 struct cp2112_read_req_report {
97         u8 report;      /* CP2112_DATA_READ_REQUEST */
98         u8 slave_address;
99         __be16 length;
100 } __packed;
101
102 struct cp2112_write_read_req_report {
103         u8 report;      /* CP2112_DATA_WRITE_READ_REQUEST */
104         u8 slave_address;
105         __be16 length;
106         u8 target_address_length;
107         u8 target_address[16];
108 } __packed;
109
110 struct cp2112_write_req_report {
111         u8 report;      /* CP2112_DATA_WRITE_REQUEST */
112         u8 slave_address;
113         u8 length;
114         u8 data[61];
115 } __packed;
116
117 struct cp2112_force_read_report {
118         u8 report;      /* CP2112_DATA_READ_FORCE_SEND */
119         __be16 length;
120 } __packed;
121
122 struct cp2112_xfer_status_report {
123         u8 report;      /* CP2112_TRANSFER_STATUS_RESPONSE */
124         u8 status0;     /* STATUS0_* */
125         u8 status1;     /* STATUS1_* */
126         __be16 retries;
127         __be16 length;
128 } __packed;
129
130 struct cp2112_string_report {
131         u8 dummy;               /* force .string to be aligned */
132         u8 report;              /* CP2112_*_STRING */
133         u8 length;              /* length in bytes of everyting after .report */
134         u8 type;                /* USB_DT_STRING */
135         wchar_t string[30];     /* UTF16_LITTLE_ENDIAN string */
136 } __packed;
137
138 /* Number of times to request transfer status before giving up waiting for a
139    transfer to complete. This may need to be changed if SMBUS clock, retries,
140    or read/write/scl_low timeout settings are changed. */
141 static const int XFER_STATUS_RETRIES = 10;
142
143 /* Time in ms to wait for a CP2112_DATA_READ_RESPONSE or
144    CP2112_TRANSFER_STATUS_RESPONSE. */
145 static const int RESPONSE_TIMEOUT = 50;
146
147 static const struct hid_device_id cp2112_devices[] = {
148         { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_CP2112) },
149         { }
150 };
151 MODULE_DEVICE_TABLE(hid, cp2112_devices);
152
153 struct cp2112_device {
154         struct i2c_adapter adap;
155         struct hid_device *hdev;
156         wait_queue_head_t wait;
157         u8 read_data[61];
158         u8 read_length;
159         u8 hwversion;
160         int xfer_status;
161         atomic_t read_avail;
162         atomic_t xfer_avail;
163         struct gpio_chip gc;
164         struct irq_chip irq;
165         u8 *in_out_buffer;
166         struct mutex lock;
167
168         struct gpio_desc *desc[8];
169         bool gpio_poll;
170         struct delayed_work gpio_poll_worker;
171         unsigned long irq_mask;
172         u8 gpio_prev_state;
173 };
174
175 static int gpio_push_pull = 0xFF;
176 module_param(gpio_push_pull, int, S_IRUGO | S_IWUSR);
177 MODULE_PARM_DESC(gpio_push_pull, "GPIO push-pull configuration bitmask");
178
179 static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
180 {
181         struct cp2112_device *dev = gpiochip_get_data(chip);
182         struct hid_device *hdev = dev->hdev;
183         u8 *buf = dev->in_out_buffer;
184         int ret;
185
186         mutex_lock(&dev->lock);
187
188         ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
189                                  CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
190                                  HID_REQ_GET_REPORT);
191         if (ret != CP2112_GPIO_CONFIG_LENGTH) {
192                 hid_err(hdev, "error requesting GPIO config: %d\n", ret);
193                 if (ret >= 0)
194                         ret = -EIO;
195                 goto exit;
196         }
197
198         buf[1] &= ~(1 << offset);
199         buf[2] = gpio_push_pull;
200
201         ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
202                                  CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
203                                  HID_REQ_SET_REPORT);
204         if (ret != CP2112_GPIO_CONFIG_LENGTH) {
205                 hid_err(hdev, "error setting GPIO config: %d\n", ret);
206                 if (ret >= 0)
207                         ret = -EIO;
208                 goto exit;
209         }
210
211         ret = 0;
212
213 exit:
214         mutex_unlock(&dev->lock);
215         return ret;
216 }
217
218 static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
219 {
220         struct cp2112_device *dev = gpiochip_get_data(chip);
221         struct hid_device *hdev = dev->hdev;
222         u8 *buf = dev->in_out_buffer;
223         int ret;
224
225         mutex_lock(&dev->lock);
226
227         buf[0] = CP2112_GPIO_SET;
228         buf[1] = value ? 0xff : 0;
229         buf[2] = 1 << offset;
230
231         ret = hid_hw_raw_request(hdev, CP2112_GPIO_SET, buf,
232                                  CP2112_GPIO_SET_LENGTH, HID_FEATURE_REPORT,
233                                  HID_REQ_SET_REPORT);
234         if (ret < 0)
235                 hid_err(hdev, "error setting GPIO values: %d\n", ret);
236
237         mutex_unlock(&dev->lock);
238 }
239
240 static int cp2112_gpio_get_all(struct gpio_chip *chip)
241 {
242         struct cp2112_device *dev = gpiochip_get_data(chip);
243         struct hid_device *hdev = dev->hdev;
244         u8 *buf = dev->in_out_buffer;
245         int ret;
246
247         mutex_lock(&dev->lock);
248
249         ret = hid_hw_raw_request(hdev, CP2112_GPIO_GET, buf,
250                                  CP2112_GPIO_GET_LENGTH, HID_FEATURE_REPORT,
251                                  HID_REQ_GET_REPORT);
252         if (ret != CP2112_GPIO_GET_LENGTH) {
253                 hid_err(hdev, "error requesting GPIO values: %d\n", ret);
254                 ret = ret < 0 ? ret : -EIO;
255                 goto exit;
256         }
257
258         ret = buf[1];
259
260 exit:
261         mutex_unlock(&dev->lock);
262
263         return ret;
264 }
265
266 static int cp2112_gpio_get(struct gpio_chip *chip, unsigned int offset)
267 {
268         int ret;
269
270         ret = cp2112_gpio_get_all(chip);
271         if (ret < 0)
272                 return ret;
273
274         return (ret >> offset) & 1;
275 }
276
277 static int cp2112_gpio_direction_output(struct gpio_chip *chip,
278                                         unsigned offset, int value)
279 {
280         struct cp2112_device *dev = gpiochip_get_data(chip);
281         struct hid_device *hdev = dev->hdev;
282         u8 *buf = dev->in_out_buffer;
283         int ret;
284
285         mutex_lock(&dev->lock);
286
287         ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
288                                  CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
289                                  HID_REQ_GET_REPORT);
290         if (ret != CP2112_GPIO_CONFIG_LENGTH) {
291                 hid_err(hdev, "error requesting GPIO config: %d\n", ret);
292                 goto fail;
293         }
294
295         buf[1] |= 1 << offset;
296         buf[2] = gpio_push_pull;
297
298         ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
299                                  CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
300                                  HID_REQ_SET_REPORT);
301         if (ret < 0) {
302                 hid_err(hdev, "error setting GPIO config: %d\n", ret);
303                 goto fail;
304         }
305
306         mutex_unlock(&dev->lock);
307
308         /*
309          * Set gpio value when output direction is already set,
310          * as specified in AN495, Rev. 0.2, cpt. 4.4
311          */
312         cp2112_gpio_set(chip, offset, value);
313
314         return 0;
315
316 fail:
317         mutex_unlock(&dev->lock);
318         return ret < 0 ? ret : -EIO;
319 }
320
321 static int cp2112_hid_get(struct hid_device *hdev, unsigned char report_number,
322                           u8 *data, size_t count, unsigned char report_type)
323 {
324         u8 *buf;
325         int ret;
326
327         buf = kmalloc(count, GFP_KERNEL);
328         if (!buf)
329                 return -ENOMEM;
330
331         ret = hid_hw_raw_request(hdev, report_number, buf, count,
332                                        report_type, HID_REQ_GET_REPORT);
333         memcpy(data, buf, count);
334         kfree(buf);
335         return ret;
336 }
337
338 static int cp2112_hid_output(struct hid_device *hdev, u8 *data, size_t count,
339                              unsigned char report_type)
340 {
341         u8 *buf;
342         int ret;
343
344         buf = kmemdup(data, count, GFP_KERNEL);
345         if (!buf)
346                 return -ENOMEM;
347
348         if (report_type == HID_OUTPUT_REPORT)
349                 ret = hid_hw_output_report(hdev, buf, count);
350         else
351                 ret = hid_hw_raw_request(hdev, buf[0], buf, count, report_type,
352                                 HID_REQ_SET_REPORT);
353
354         kfree(buf);
355         return ret;
356 }
357
358 static int cp2112_wait(struct cp2112_device *dev, atomic_t *avail)
359 {
360         int ret = 0;
361
362         /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a
363          * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to
364          * come in cp2112_raw_event or timeout. There will only be one of these
365          * in flight at any one time. The timeout is extremely large and is a
366          * last resort if the CP2112 has died. If we do timeout we don't expect
367          * to receive the response which would cause data races, it's not like
368          * we can do anything about it anyway.
369          */
370         ret = wait_event_interruptible_timeout(dev->wait,
371                 atomic_read(avail), msecs_to_jiffies(RESPONSE_TIMEOUT));
372         if (-ERESTARTSYS == ret)
373                 return ret;
374         if (!ret)
375                 return -ETIMEDOUT;
376
377         atomic_set(avail, 0);
378         return 0;
379 }
380
381 static int cp2112_xfer_status(struct cp2112_device *dev)
382 {
383         struct hid_device *hdev = dev->hdev;
384         u8 buf[2];
385         int ret;
386
387         buf[0] = CP2112_TRANSFER_STATUS_REQUEST;
388         buf[1] = 0x01;
389         atomic_set(&dev->xfer_avail, 0);
390
391         ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
392         if (ret < 0) {
393                 hid_warn(hdev, "Error requesting status: %d\n", ret);
394                 return ret;
395         }
396
397         ret = cp2112_wait(dev, &dev->xfer_avail);
398         if (ret)
399                 return ret;
400
401         return dev->xfer_status;
402 }
403
404 static int cp2112_read(struct cp2112_device *dev, u8 *data, size_t size)
405 {
406         struct hid_device *hdev = dev->hdev;
407         struct cp2112_force_read_report report;
408         int ret;
409
410         if (size > sizeof(dev->read_data))
411                 size = sizeof(dev->read_data);
412         report.report = CP2112_DATA_READ_FORCE_SEND;
413         report.length = cpu_to_be16(size);
414
415         atomic_set(&dev->read_avail, 0);
416
417         ret = cp2112_hid_output(hdev, &report.report, sizeof(report),
418                                 HID_OUTPUT_REPORT);
419         if (ret < 0) {
420                 hid_warn(hdev, "Error requesting data: %d\n", ret);
421                 return ret;
422         }
423
424         ret = cp2112_wait(dev, &dev->read_avail);
425         if (ret)
426                 return ret;
427
428         hid_dbg(hdev, "read %d of %zd bytes requested\n",
429                 dev->read_length, size);
430
431         if (size > dev->read_length)
432                 size = dev->read_length;
433
434         memcpy(data, dev->read_data, size);
435         return dev->read_length;
436 }
437
438 static int cp2112_read_req(void *buf, u8 slave_address, u16 length)
439 {
440         struct cp2112_read_req_report *report = buf;
441
442         if (length < 1 || length > 512)
443                 return -EINVAL;
444
445         report->report = CP2112_DATA_READ_REQUEST;
446         report->slave_address = slave_address << 1;
447         report->length = cpu_to_be16(length);
448         return sizeof(*report);
449 }
450
451 static int cp2112_write_read_req(void *buf, u8 slave_address, u16 length,
452                                  u8 command, u8 *data, u8 data_length)
453 {
454         struct cp2112_write_read_req_report *report = buf;
455
456         if (length < 1 || length > 512
457             || data_length > sizeof(report->target_address) - 1)
458                 return -EINVAL;
459
460         report->report = CP2112_DATA_WRITE_READ_REQUEST;
461         report->slave_address = slave_address << 1;
462         report->length = cpu_to_be16(length);
463         report->target_address_length = data_length + 1;
464         report->target_address[0] = command;
465         memcpy(&report->target_address[1], data, data_length);
466         return data_length + 6;
467 }
468
469 static int cp2112_write_req(void *buf, u8 slave_address, u8 command, u8 *data,
470                             u8 data_length)
471 {
472         struct cp2112_write_req_report *report = buf;
473
474         if (data_length > sizeof(report->data) - 1)
475                 return -EINVAL;
476
477         report->report = CP2112_DATA_WRITE_REQUEST;
478         report->slave_address = slave_address << 1;
479         report->length = data_length + 1;
480         report->data[0] = command;
481         memcpy(&report->data[1], data, data_length);
482         return data_length + 4;
483 }
484
485 static int cp2112_i2c_write_req(void *buf, u8 slave_address, u8 *data,
486                                 u8 data_length)
487 {
488         struct cp2112_write_req_report *report = buf;
489
490         if (data_length > sizeof(report->data))
491                 return -EINVAL;
492
493         report->report = CP2112_DATA_WRITE_REQUEST;
494         report->slave_address = slave_address << 1;
495         report->length = data_length;
496         memcpy(report->data, data, data_length);
497         return data_length + 3;
498 }
499
500 static int cp2112_i2c_write_read_req(void *buf, u8 slave_address,
501                                      u8 *addr, int addr_length,
502                                      int read_length)
503 {
504         struct cp2112_write_read_req_report *report = buf;
505
506         if (read_length < 1 || read_length > 512 ||
507             addr_length > sizeof(report->target_address))
508                 return -EINVAL;
509
510         report->report = CP2112_DATA_WRITE_READ_REQUEST;
511         report->slave_address = slave_address << 1;
512         report->length = cpu_to_be16(read_length);
513         report->target_address_length = addr_length;
514         memcpy(report->target_address, addr, addr_length);
515         return addr_length + 5;
516 }
517
518 static int cp2112_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
519                            int num)
520 {
521         struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data;
522         struct hid_device *hdev = dev->hdev;
523         u8 buf[64];
524         ssize_t count;
525         ssize_t read_length = 0;
526         u8 *read_buf = NULL;
527         unsigned int retries;
528         int ret;
529
530         hid_dbg(hdev, "I2C %d messages\n", num);
531
532         if (num == 1) {
533                 if (msgs->flags & I2C_M_RD) {
534                         hid_dbg(hdev, "I2C read %#04x len %d\n",
535                                 msgs->addr, msgs->len);
536                         read_length = msgs->len;
537                         read_buf = msgs->buf;
538                         count = cp2112_read_req(buf, msgs->addr, msgs->len);
539                 } else {
540                         hid_dbg(hdev, "I2C write %#04x len %d\n",
541                                 msgs->addr, msgs->len);
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
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                 read_write == I2C_SMBUS_WRITE ? "write" : "read",
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
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
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
850 static int cp2112_set_usb_config(struct hid_device *hdev,
851                                  struct cp2112_usb_config_report *cfg)
852 {
853         int ret;
854
855         BUG_ON(cfg->report != CP2112_USB_CONFIG);
856
857         ret = cp2112_hid_output(hdev, (u8 *)cfg, sizeof(*cfg),
858                                 HID_FEATURE_REPORT);
859         if (ret != sizeof(*cfg)) {
860                 hid_err(hdev, "error writing usb config: %d\n", ret);
861                 if (ret < 0)
862                         return ret;
863                 return -EIO;
864         }
865
866         return 0;
867 }
868
869 static void chmod_sysfs_attrs(struct hid_device *hdev);
870
871 #define CP2112_CONFIG_ATTR(name, store, format, ...) \
872 static ssize_t name##_store(struct device *kdev, \
873                             struct device_attribute *attr, const char *buf, \
874                             size_t count) \
875 { \
876         struct hid_device *hdev = to_hid_device(kdev); \
877         struct cp2112_usb_config_report cfg; \
878         int ret = cp2112_get_usb_config(hdev, &cfg); \
879         if (ret) \
880                 return ret; \
881         store; \
882         ret = cp2112_set_usb_config(hdev, &cfg); \
883         if (ret) \
884                 return ret; \
885         chmod_sysfs_attrs(hdev); \
886         return count; \
887 } \
888 static ssize_t name##_show(struct device *kdev, \
889                            struct device_attribute *attr, char *buf) \
890 { \
891         struct hid_device *hdev = to_hid_device(kdev); \
892         struct cp2112_usb_config_report cfg; \
893         int ret = cp2112_get_usb_config(hdev, &cfg); \
894         if (ret) \
895                 return ret; \
896         return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \
897 } \
898 static DEVICE_ATTR_RW(name);
899
900 CP2112_CONFIG_ATTR(vendor_id, ({
901         u16 vid;
902
903         if (sscanf(buf, "%hi", &vid) != 1)
904                 return -EINVAL;
905
906         cfg.vid = cpu_to_le16(vid);
907         cfg.mask = 0x01;
908 }), "0x%04x\n", le16_to_cpu(cfg.vid));
909
910 CP2112_CONFIG_ATTR(product_id, ({
911         u16 pid;
912
913         if (sscanf(buf, "%hi", &pid) != 1)
914                 return -EINVAL;
915
916         cfg.pid = cpu_to_le16(pid);
917         cfg.mask = 0x02;
918 }), "0x%04x\n", le16_to_cpu(cfg.pid));
919
920 CP2112_CONFIG_ATTR(max_power, ({
921         int mA;
922
923         if (sscanf(buf, "%i", &mA) != 1)
924                 return -EINVAL;
925
926         cfg.max_power = (mA + 1) / 2;
927         cfg.mask = 0x04;
928 }), "%u mA\n", cfg.max_power * 2);
929
930 CP2112_CONFIG_ATTR(power_mode, ({
931         if (sscanf(buf, "%hhi", &cfg.power_mode) != 1)
932                 return -EINVAL;
933
934         cfg.mask = 0x08;
935 }), "%u\n", cfg.power_mode);
936
937 CP2112_CONFIG_ATTR(release_version, ({
938         if (sscanf(buf, "%hhi.%hhi", &cfg.release_major, &cfg.release_minor)
939             != 2)
940                 return -EINVAL;
941
942         cfg.mask = 0x10;
943 }), "%u.%u\n", cfg.release_major, cfg.release_minor);
944
945 #undef CP2112_CONFIG_ATTR
946
947 struct cp2112_pstring_attribute {
948         struct device_attribute attr;
949         unsigned char report;
950 };
951
952 static ssize_t pstr_store(struct device *kdev,
953                           struct device_attribute *kattr, const char *buf,
954                           size_t count)
955 {
956         struct hid_device *hdev = to_hid_device(kdev);
957         struct cp2112_pstring_attribute *attr =
958                 container_of(kattr, struct cp2112_pstring_attribute, attr);
959         struct cp2112_string_report report;
960         int ret;
961
962         memset(&report, 0, sizeof(report));
963
964         ret = utf8s_to_utf16s(buf, count, UTF16_LITTLE_ENDIAN,
965                               report.string, ARRAY_SIZE(report.string));
966         report.report = attr->report;
967         report.length = ret * sizeof(report.string[0]) + 2;
968         report.type = USB_DT_STRING;
969
970         ret = cp2112_hid_output(hdev, &report.report, report.length + 1,
971                                 HID_FEATURE_REPORT);
972         if (ret != report.length + 1) {
973                 hid_err(hdev, "error writing %s string: %d\n", kattr->attr.name,
974                         ret);
975                 if (ret < 0)
976                         return ret;
977                 return -EIO;
978         }
979
980         chmod_sysfs_attrs(hdev);
981         return count;
982 }
983
984 static ssize_t pstr_show(struct device *kdev,
985                          struct device_attribute *kattr, char *buf)
986 {
987         struct hid_device *hdev = to_hid_device(kdev);
988         struct cp2112_pstring_attribute *attr =
989                 container_of(kattr, struct cp2112_pstring_attribute, attr);
990         struct cp2112_string_report report;
991         u8 length;
992         int ret;
993
994         ret = cp2112_hid_get(hdev, attr->report, &report.report,
995                              sizeof(report) - 1, HID_FEATURE_REPORT);
996         if (ret < 3) {
997                 hid_err(hdev, "error reading %s string: %d\n", kattr->attr.name,
998                         ret);
999                 if (ret < 0)
1000                         return ret;
1001                 return -EIO;
1002         }
1003
1004         if (report.length < 2) {
1005                 hid_err(hdev, "invalid %s string length: %d\n",
1006                         kattr->attr.name, report.length);
1007                 return -EIO;
1008         }
1009
1010         length = report.length > ret - 1 ? ret - 1 : report.length;
1011         length = (length - 2) / sizeof(report.string[0]);
1012         ret = utf16s_to_utf8s(report.string, length, UTF16_LITTLE_ENDIAN, buf,
1013                               PAGE_SIZE - 1);
1014         buf[ret++] = '\n';
1015         return ret;
1016 }
1017
1018 #define CP2112_PSTR_ATTR(name, _report) \
1019 static struct cp2112_pstring_attribute dev_attr_##name = { \
1020         .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \
1021         .report = _report, \
1022 };
1023
1024 CP2112_PSTR_ATTR(manufacturer,  CP2112_MANUFACTURER_STRING);
1025 CP2112_PSTR_ATTR(product,       CP2112_PRODUCT_STRING);
1026 CP2112_PSTR_ATTR(serial,        CP2112_SERIAL_STRING);
1027
1028 #undef CP2112_PSTR_ATTR
1029
1030 static const struct attribute_group cp2112_attr_group = {
1031         .attrs = (struct attribute *[]){
1032                 &dev_attr_vendor_id.attr,
1033                 &dev_attr_product_id.attr,
1034                 &dev_attr_max_power.attr,
1035                 &dev_attr_power_mode.attr,
1036                 &dev_attr_release_version.attr,
1037                 &dev_attr_manufacturer.attr.attr,
1038                 &dev_attr_product.attr.attr,
1039                 &dev_attr_serial.attr.attr,
1040                 NULL
1041         }
1042 };
1043
1044 /* Chmoding our sysfs attributes is simply a way to expose which fields in the
1045  * PROM have already been programmed. We do not depend on this preventing
1046  * writing to these attributes since the CP2112 will simply ignore writes to
1047  * already-programmed fields. This is why there is no sense in fixing this
1048  * racy behaviour.
1049  */
1050 static void chmod_sysfs_attrs(struct hid_device *hdev)
1051 {
1052         struct attribute **attr;
1053         u8 buf[2];
1054         int ret;
1055
1056         ret = cp2112_hid_get(hdev, CP2112_LOCK_BYTE, buf, sizeof(buf),
1057                              HID_FEATURE_REPORT);
1058         if (ret != sizeof(buf)) {
1059                 hid_err(hdev, "error reading lock byte: %d\n", ret);
1060                 return;
1061         }
1062
1063         for (attr = cp2112_attr_group.attrs; *attr; ++attr) {
1064                 umode_t mode = (buf[1] & 1) ? S_IWUSR | S_IRUGO : S_IRUGO;
1065                 ret = sysfs_chmod_file(&hdev->dev.kobj, *attr, mode);
1066                 if (ret < 0)
1067                         hid_err(hdev, "error chmoding sysfs file %s\n",
1068                                 (*attr)->name);
1069                 buf[1] >>= 1;
1070         }
1071 }
1072
1073 static void cp2112_gpio_irq_ack(struct irq_data *d)
1074 {
1075 }
1076
1077 static void cp2112_gpio_irq_mask(struct irq_data *d)
1078 {
1079         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1080         struct cp2112_device *dev = gpiochip_get_data(gc);
1081
1082         __clear_bit(d->hwirq, &dev->irq_mask);
1083 }
1084
1085 static void cp2112_gpio_irq_unmask(struct irq_data *d)
1086 {
1087         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1088         struct cp2112_device *dev = gpiochip_get_data(gc);
1089
1090         __set_bit(d->hwirq, &dev->irq_mask);
1091 }
1092
1093 static void cp2112_gpio_poll_callback(struct work_struct *work)
1094 {
1095         struct cp2112_device *dev = container_of(work, struct cp2112_device,
1096                                                  gpio_poll_worker.work);
1097         struct irq_data *d;
1098         u8 gpio_mask;
1099         u8 virqs = (u8)dev->irq_mask;
1100         u32 irq_type;
1101         int irq, virq, ret;
1102
1103         ret = cp2112_gpio_get_all(&dev->gc);
1104         if (ret == -ENODEV) /* the hardware has been disconnected */
1105                 return;
1106         if (ret < 0)
1107                 goto exit;
1108
1109         gpio_mask = ret;
1110
1111         while (virqs) {
1112                 virq = ffs(virqs) - 1;
1113                 virqs &= ~BIT(virq);
1114
1115                 if (!dev->gc.to_irq)
1116                         break;
1117
1118                 irq = dev->gc.to_irq(&dev->gc, virq);
1119
1120                 d = irq_get_irq_data(irq);
1121                 if (!d)
1122                         continue;
1123
1124                 irq_type = irqd_get_trigger_type(d);
1125
1126                 if (gpio_mask & BIT(virq)) {
1127                         /* Level High */
1128
1129                         if (irq_type & IRQ_TYPE_LEVEL_HIGH)
1130                                 handle_nested_irq(irq);
1131
1132                         if ((irq_type & IRQ_TYPE_EDGE_RISING) &&
1133                             !(dev->gpio_prev_state & BIT(virq)))
1134                                 handle_nested_irq(irq);
1135                 } else {
1136                         /* Level Low */
1137
1138                         if (irq_type & IRQ_TYPE_LEVEL_LOW)
1139                                 handle_nested_irq(irq);
1140
1141                         if ((irq_type & IRQ_TYPE_EDGE_FALLING) &&
1142                             (dev->gpio_prev_state & BIT(virq)))
1143                                 handle_nested_irq(irq);
1144                 }
1145         }
1146
1147         dev->gpio_prev_state = gpio_mask;
1148
1149 exit:
1150         if (dev->gpio_poll)
1151                 schedule_delayed_work(&dev->gpio_poll_worker, 10);
1152 }
1153
1154
1155 static unsigned int cp2112_gpio_irq_startup(struct irq_data *d)
1156 {
1157         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1158         struct cp2112_device *dev = gpiochip_get_data(gc);
1159
1160         if (!dev->gpio_poll) {
1161                 dev->gpio_poll = true;
1162                 schedule_delayed_work(&dev->gpio_poll_worker, 0);
1163         }
1164
1165         cp2112_gpio_irq_unmask(d);
1166         return 0;
1167 }
1168
1169 static void cp2112_gpio_irq_shutdown(struct irq_data *d)
1170 {
1171         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1172         struct cp2112_device *dev = gpiochip_get_data(gc);
1173
1174         if (!dev->irq_mask) {
1175                 dev->gpio_poll = false;
1176                 cancel_delayed_work_sync(&dev->gpio_poll_worker);
1177         }
1178 }
1179
1180 static int cp2112_gpio_irq_type(struct irq_data *d, unsigned int type)
1181 {
1182         return 0;
1183 }
1184
1185 static int __maybe_unused cp2112_allocate_irq(struct cp2112_device *dev,
1186                                               int pin)
1187 {
1188         int ret;
1189
1190         if (dev->desc[pin])
1191                 return -EINVAL;
1192
1193         dev->desc[pin] = gpiochip_request_own_desc(&dev->gc, pin,
1194                                                    "HID/I2C:Event",
1195                                                    GPIO_ACTIVE_HIGH,
1196                                                    GPIOD_IN);
1197         if (IS_ERR(dev->desc[pin])) {
1198                 dev_err(dev->gc.parent, "Failed to request GPIO\n");
1199                 return PTR_ERR(dev->desc[pin]);
1200         }
1201
1202         ret = cp2112_gpio_direction_input(&dev->gc, pin);
1203         if (ret < 0) {
1204                 dev_err(dev->gc.parent, "Failed to set GPIO to input dir\n");
1205                 goto err_desc;
1206         }
1207
1208         ret = gpiochip_lock_as_irq(&dev->gc, pin);
1209         if (ret) {
1210                 dev_err(dev->gc.parent, "Failed to lock GPIO as interrupt\n");
1211                 goto err_desc;
1212         }
1213
1214         ret = gpiod_to_irq(dev->desc[pin]);
1215         if (ret < 0) {
1216                 dev_err(dev->gc.parent, "Failed to translate GPIO to IRQ\n");
1217                 goto err_lock;
1218         }
1219
1220         return ret;
1221
1222 err_lock:
1223         gpiochip_unlock_as_irq(&dev->gc, pin);
1224 err_desc:
1225         gpiochip_free_own_desc(dev->desc[pin]);
1226         dev->desc[pin] = NULL;
1227         return ret;
1228 }
1229
1230 static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id)
1231 {
1232         struct cp2112_device *dev;
1233         u8 buf[3];
1234         struct cp2112_smbus_config_report config;
1235         struct gpio_irq_chip *girq;
1236         int ret;
1237
1238         dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL);
1239         if (!dev)
1240                 return -ENOMEM;
1241
1242         dev->in_out_buffer = devm_kzalloc(&hdev->dev, CP2112_REPORT_MAX_LENGTH,
1243                                           GFP_KERNEL);
1244         if (!dev->in_out_buffer)
1245                 return -ENOMEM;
1246
1247         mutex_init(&dev->lock);
1248
1249         ret = hid_parse(hdev);
1250         if (ret) {
1251                 hid_err(hdev, "parse failed\n");
1252                 return ret;
1253         }
1254
1255         ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1256         if (ret) {
1257                 hid_err(hdev, "hw start failed\n");
1258                 return ret;
1259         }
1260
1261         ret = hid_hw_open(hdev);
1262         if (ret) {
1263                 hid_err(hdev, "hw open failed\n");
1264                 goto err_hid_stop;
1265         }
1266
1267         ret = hid_hw_power(hdev, PM_HINT_FULLON);
1268         if (ret < 0) {
1269                 hid_err(hdev, "power management error: %d\n", ret);
1270                 goto err_hid_close;
1271         }
1272
1273         ret = cp2112_hid_get(hdev, CP2112_GET_VERSION_INFO, buf, sizeof(buf),
1274                              HID_FEATURE_REPORT);
1275         if (ret != sizeof(buf)) {
1276                 hid_err(hdev, "error requesting version\n");
1277                 if (ret >= 0)
1278                         ret = -EIO;
1279                 goto err_power_normal;
1280         }
1281
1282         hid_info(hdev, "Part Number: 0x%02X Device Version: 0x%02X\n",
1283                  buf[1], buf[2]);
1284
1285         ret = cp2112_hid_get(hdev, CP2112_SMBUS_CONFIG, (u8 *)&config,
1286                              sizeof(config), HID_FEATURE_REPORT);
1287         if (ret != sizeof(config)) {
1288                 hid_err(hdev, "error requesting SMBus config\n");
1289                 if (ret >= 0)
1290                         ret = -EIO;
1291                 goto err_power_normal;
1292         }
1293
1294         config.retry_time = cpu_to_be16(1);
1295
1296         ret = cp2112_hid_output(hdev, (u8 *)&config, sizeof(config),
1297                                 HID_FEATURE_REPORT);
1298         if (ret != sizeof(config)) {
1299                 hid_err(hdev, "error setting SMBus config\n");
1300                 if (ret >= 0)
1301                         ret = -EIO;
1302                 goto err_power_normal;
1303         }
1304
1305         hid_set_drvdata(hdev, (void *)dev);
1306         dev->hdev               = hdev;
1307         dev->adap.owner         = THIS_MODULE;
1308         dev->adap.class         = I2C_CLASS_HWMON;
1309         dev->adap.algo          = &smbus_algorithm;
1310         dev->adap.algo_data     = dev;
1311         dev->adap.dev.parent    = &hdev->dev;
1312         snprintf(dev->adap.name, sizeof(dev->adap.name),
1313                  "CP2112 SMBus Bridge on hidraw%d",
1314                  ((struct hidraw *)hdev->hidraw)->minor);
1315         dev->hwversion = buf[2];
1316         init_waitqueue_head(&dev->wait);
1317
1318         hid_device_io_start(hdev);
1319         ret = i2c_add_adapter(&dev->adap);
1320         hid_device_io_stop(hdev);
1321
1322         if (ret) {
1323                 hid_err(hdev, "error registering i2c adapter\n");
1324                 goto err_power_normal;
1325         }
1326
1327         hid_dbg(hdev, "adapter registered\n");
1328
1329         dev->gc.label                   = "cp2112_gpio";
1330         dev->gc.direction_input         = cp2112_gpio_direction_input;
1331         dev->gc.direction_output        = cp2112_gpio_direction_output;
1332         dev->gc.set                     = cp2112_gpio_set;
1333         dev->gc.get                     = cp2112_gpio_get;
1334         dev->gc.base                    = -1;
1335         dev->gc.ngpio                   = 8;
1336         dev->gc.can_sleep               = 1;
1337         dev->gc.parent                  = &hdev->dev;
1338
1339         dev->irq.name = "cp2112-gpio";
1340         dev->irq.irq_startup = cp2112_gpio_irq_startup;
1341         dev->irq.irq_shutdown = cp2112_gpio_irq_shutdown;
1342         dev->irq.irq_ack = cp2112_gpio_irq_ack;
1343         dev->irq.irq_mask = cp2112_gpio_irq_mask;
1344         dev->irq.irq_unmask = cp2112_gpio_irq_unmask;
1345         dev->irq.irq_set_type = cp2112_gpio_irq_type;
1346         dev->irq.flags = IRQCHIP_MASK_ON_SUSPEND;
1347
1348         girq = &dev->gc.irq;
1349         girq->chip = &dev->irq;
1350         /* The event comes from the outside so no parent handler */
1351         girq->parent_handler = NULL;
1352         girq->num_parents = 0;
1353         girq->parents = NULL;
1354         girq->default_type = IRQ_TYPE_NONE;
1355         girq->handler = handle_simple_irq;
1356         girq->threaded = true;
1357
1358         INIT_DELAYED_WORK(&dev->gpio_poll_worker, cp2112_gpio_poll_callback);
1359
1360         ret = gpiochip_add_data(&dev->gc, dev);
1361         if (ret < 0) {
1362                 hid_err(hdev, "error registering gpio chip\n");
1363                 goto err_free_i2c;
1364         }
1365
1366         ret = sysfs_create_group(&hdev->dev.kobj, &cp2112_attr_group);
1367         if (ret < 0) {
1368                 hid_err(hdev, "error creating sysfs attrs\n");
1369                 goto err_gpiochip_remove;
1370         }
1371
1372         chmod_sysfs_attrs(hdev);
1373         hid_hw_power(hdev, PM_HINT_NORMAL);
1374
1375         return ret;
1376
1377 err_gpiochip_remove:
1378         gpiochip_remove(&dev->gc);
1379 err_free_i2c:
1380         i2c_del_adapter(&dev->adap);
1381 err_power_normal:
1382         hid_hw_power(hdev, PM_HINT_NORMAL);
1383 err_hid_close:
1384         hid_hw_close(hdev);
1385 err_hid_stop:
1386         hid_hw_stop(hdev);
1387         return ret;
1388 }
1389
1390 static void cp2112_remove(struct hid_device *hdev)
1391 {
1392         struct cp2112_device *dev = hid_get_drvdata(hdev);
1393         int i;
1394
1395         sysfs_remove_group(&hdev->dev.kobj, &cp2112_attr_group);
1396         i2c_del_adapter(&dev->adap);
1397
1398         if (dev->gpio_poll) {
1399                 dev->gpio_poll = false;
1400                 cancel_delayed_work_sync(&dev->gpio_poll_worker);
1401         }
1402
1403         for (i = 0; i < ARRAY_SIZE(dev->desc); i++) {
1404                 gpiochip_unlock_as_irq(&dev->gc, i);
1405                 gpiochip_free_own_desc(dev->desc[i]);
1406         }
1407
1408         gpiochip_remove(&dev->gc);
1409         /* i2c_del_adapter has finished removing all i2c devices from our
1410          * adapter. Well behaved devices should no longer call our cp2112_xfer
1411          * and should have waited for any pending calls to finish. It has also
1412          * waited for device_unregister(&adap->dev) to complete. Therefore we
1413          * can safely free our struct cp2112_device.
1414          */
1415         hid_hw_close(hdev);
1416         hid_hw_stop(hdev);
1417 }
1418
1419 static int cp2112_raw_event(struct hid_device *hdev, struct hid_report *report,
1420                             u8 *data, int size)
1421 {
1422         struct cp2112_device *dev = hid_get_drvdata(hdev);
1423         struct cp2112_xfer_status_report *xfer = (void *)data;
1424
1425         switch (data[0]) {
1426         case CP2112_TRANSFER_STATUS_RESPONSE:
1427                 hid_dbg(hdev, "xfer status: %02x %02x %04x %04x\n",
1428                         xfer->status0, xfer->status1,
1429                         be16_to_cpu(xfer->retries), be16_to_cpu(xfer->length));
1430
1431                 switch (xfer->status0) {
1432                 case STATUS0_IDLE:
1433                         dev->xfer_status = -EAGAIN;
1434                         break;
1435                 case STATUS0_BUSY:
1436                         dev->xfer_status = -EBUSY;
1437                         break;
1438                 case STATUS0_COMPLETE:
1439                         dev->xfer_status = be16_to_cpu(xfer->length);
1440                         break;
1441                 case STATUS0_ERROR:
1442                         switch (xfer->status1) {
1443                         case STATUS1_TIMEOUT_NACK:
1444                         case STATUS1_TIMEOUT_BUS:
1445                                 dev->xfer_status = -ETIMEDOUT;
1446                                 break;
1447                         default:
1448                                 dev->xfer_status = -EIO;
1449                                 break;
1450                         }
1451                         break;
1452                 default:
1453                         dev->xfer_status = -EINVAL;
1454                         break;
1455                 }
1456
1457                 atomic_set(&dev->xfer_avail, 1);
1458                 break;
1459         case CP2112_DATA_READ_RESPONSE:
1460                 hid_dbg(hdev, "read response: %02x %02x\n", data[1], data[2]);
1461
1462                 dev->read_length = data[2];
1463                 if (dev->read_length > sizeof(dev->read_data))
1464                         dev->read_length = sizeof(dev->read_data);
1465
1466                 memcpy(dev->read_data, &data[3], dev->read_length);
1467                 atomic_set(&dev->read_avail, 1);
1468                 break;
1469         default:
1470                 hid_err(hdev, "unknown report\n");
1471
1472                 return 0;
1473         }
1474
1475         wake_up_interruptible(&dev->wait);
1476         return 1;
1477 }
1478
1479 static struct hid_driver cp2112_driver = {
1480         .name           = "cp2112",
1481         .id_table       = cp2112_devices,
1482         .probe          = cp2112_probe,
1483         .remove         = cp2112_remove,
1484         .raw_event      = cp2112_raw_event,
1485 };
1486
1487 module_hid_driver(cp2112_driver);
1488 MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge");
1489 MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
1490 MODULE_LICENSE("GPL");
1491