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