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

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of linux/drivers/hid/hid-cp2112.c