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