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