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