1 /*
2  * HID over I2C protocol implementation
3  *
4  * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
5  * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
6  * Copyright (c) 2012 Red Hat, Inc
7  *
8  * This code is partly based on "USB HID support for Linux":
9  *
10  *  Copyright (c) 1999 Andreas Gal
11  *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
12  *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
13  *  Copyright (c) 2007-2008 Oliver Neukum
14  *  Copyright (c) 2006-2010 Jiri Kosina
15  *
16  * This file is subject to the terms and conditions of the GNU General Public
17  * License.  See the file COPYING in the main directory of this archive for
18  * more details.
19  */
20 
21 #include <linux/module.h>
22 #include <linux/i2c.h>
23 #include <linux/interrupt.h>
24 #include <linux/input.h>
25 #include <linux/irq.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/pm.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/device.h>
31 #include <linux/wait.h>
32 #include <linux/err.h>
33 #include <linux/string.h>
34 #include <linux/list.h>
35 #include <linux/jiffies.h>
36 #include <linux/kernel.h>
37 #include <linux/hid.h>
38 #include <linux/mutex.h>
39 #include <linux/acpi.h>
40 #include <linux/of.h>
41 #include <linux/regulator/consumer.h>
42 
43 #include <linux/platform_data/i2c-hid.h>
44 
45 #include "../hid-ids.h"
46 #include "i2c-hid.h"
47 
48 /* quirks to control the device */
49 #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV	BIT(0)
50 #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET	BIT(1)
51 #define I2C_HID_QUIRK_NO_RUNTIME_PM		BIT(2)
52 #define I2C_HID_QUIRK_DELAY_AFTER_SLEEP		BIT(3)
53 #define I2C_HID_QUIRK_BOGUS_IRQ			BIT(4)
54 
55 /* flags */
56 #define I2C_HID_STARTED		0
57 #define I2C_HID_RESET_PENDING	1
58 #define I2C_HID_READ_PENDING	2
59 
60 #define I2C_HID_PWR_ON		0x00
61 #define I2C_HID_PWR_SLEEP	0x01
62 
63 /* debug option */
64 static bool debug;
65 module_param(debug, bool, 0444);
66 MODULE_PARM_DESC(debug, "print a lot of debug information");
67 
68 #define i2c_hid_dbg(ihid, fmt, arg...)					  \
69 do {									  \
70 	if (debug)							  \
71 		dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
72 } while (0)
73 
74 struct i2c_hid_desc {
75 	__le16 wHIDDescLength;
76 	__le16 bcdVersion;
77 	__le16 wReportDescLength;
78 	__le16 wReportDescRegister;
79 	__le16 wInputRegister;
80 	__le16 wMaxInputLength;
81 	__le16 wOutputRegister;
82 	__le16 wMaxOutputLength;
83 	__le16 wCommandRegister;
84 	__le16 wDataRegister;
85 	__le16 wVendorID;
86 	__le16 wProductID;
87 	__le16 wVersionID;
88 	__le32 reserved;
89 } __packed;
90 
91 struct i2c_hid_cmd {
92 	unsigned int registerIndex;
93 	__u8 opcode;
94 	unsigned int length;
95 	bool wait;
96 };
97 
98 union command {
99 	u8 data[0];
100 	struct cmd {
101 		__le16 reg;
102 		__u8 reportTypeID;
103 		__u8 opcode;
104 	} __packed c;
105 };
106 
107 #define I2C_HID_CMD(opcode_) \
108 	.opcode = opcode_, .length = 4, \
109 	.registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
110 
111 /* fetch HID descriptor */
112 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
113 /* fetch report descriptors */
114 static const struct i2c_hid_cmd hid_report_descr_cmd = {
115 		.registerIndex = offsetof(struct i2c_hid_desc,
116 			wReportDescRegister),
117 		.opcode = 0x00,
118 		.length = 2 };
119 /* commands */
120 static const struct i2c_hid_cmd hid_reset_cmd =		{ I2C_HID_CMD(0x01),
121 							  .wait = true };
122 static const struct i2c_hid_cmd hid_get_report_cmd =	{ I2C_HID_CMD(0x02) };
123 static const struct i2c_hid_cmd hid_set_report_cmd =	{ I2C_HID_CMD(0x03) };
124 static const struct i2c_hid_cmd hid_set_power_cmd =	{ I2C_HID_CMD(0x08) };
125 static const struct i2c_hid_cmd hid_no_cmd =		{ .length = 0 };
126 
127 /*
128  * These definitions are not used here, but are defined by the spec.
129  * Keeping them here for documentation purposes.
130  *
131  * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
132  * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
133  * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
134  * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
135  */
136 
137 /* The main device structure */
138 struct i2c_hid {
139 	struct i2c_client	*client;	/* i2c client */
140 	struct hid_device	*hid;	/* pointer to corresponding HID dev */
141 	union {
142 		__u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
143 		struct i2c_hid_desc hdesc;	/* the HID Descriptor */
144 	};
145 	__le16			wHIDDescRegister; /* location of the i2c
146 						   * register of the HID
147 						   * descriptor. */
148 	unsigned int		bufsize;	/* i2c buffer size */
149 	u8			*inbuf;		/* Input buffer */
150 	u8			*rawbuf;	/* Raw Input buffer */
151 	u8			*cmdbuf;	/* Command buffer */
152 	u8			*argsbuf;	/* Command arguments buffer */
153 
154 	unsigned long		flags;		/* device flags */
155 	unsigned long		quirks;		/* Various quirks */
156 
157 	wait_queue_head_t	wait;		/* For waiting the interrupt */
158 
159 	struct i2c_hid_platform_data pdata;
160 
161 	bool			irq_wake_enabled;
162 	struct mutex		reset_lock;
163 
164 	unsigned long		sleep_delay;
165 };
166 
167 static const struct i2c_hid_quirks {
168 	__u16 idVendor;
169 	__u16 idProduct;
170 	__u32 quirks;
171 } i2c_hid_quirks[] = {
172 	{ USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8752,
173 		I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
174 	{ USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8755,
175 		I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
176 	{ I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
177 		I2C_HID_QUIRK_NO_IRQ_AFTER_RESET |
178 		I2C_HID_QUIRK_NO_RUNTIME_PM },
179 	{ I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_4B33,
180 		I2C_HID_QUIRK_DELAY_AFTER_SLEEP },
181 	{ USB_VENDOR_ID_LG, I2C_DEVICE_ID_LG_8001,
182 		I2C_HID_QUIRK_NO_RUNTIME_PM },
183 	{ I2C_VENDOR_ID_GOODIX, I2C_DEVICE_ID_GOODIX_01F0,
184 		I2C_HID_QUIRK_NO_RUNTIME_PM },
185 	{ USB_VENDOR_ID_ELAN, HID_ANY_ID,
186 		 I2C_HID_QUIRK_BOGUS_IRQ },
187 	{ 0, 0 }
188 };
189 
190 /*
191  * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
192  * @idVendor: the 16-bit vendor ID
193  * @idProduct: the 16-bit product ID
194  *
195  * Returns: a u32 quirks value.
196  */
197 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
198 {
199 	u32 quirks = 0;
200 	int n;
201 
202 	for (n = 0; i2c_hid_quirks[n].idVendor; n++)
203 		if (i2c_hid_quirks[n].idVendor == idVendor &&
204 		    (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
205 		     i2c_hid_quirks[n].idProduct == idProduct))
206 			quirks = i2c_hid_quirks[n].quirks;
207 
208 	return quirks;
209 }
210 
211 static int __i2c_hid_command(struct i2c_client *client,
212 		const struct i2c_hid_cmd *command, u8 reportID,
213 		u8 reportType, u8 *args, int args_len,
214 		unsigned char *buf_recv, int data_len)
215 {
216 	struct i2c_hid *ihid = i2c_get_clientdata(client);
217 	union command *cmd = (union command *)ihid->cmdbuf;
218 	int ret;
219 	struct i2c_msg msg[2];
220 	int msg_num = 1;
221 
222 	int length = command->length;
223 	bool wait = command->wait;
224 	unsigned int registerIndex = command->registerIndex;
225 
226 	/* special case for hid_descr_cmd */
227 	if (command == &hid_descr_cmd) {
228 		cmd->c.reg = ihid->wHIDDescRegister;
229 	} else {
230 		cmd->data[0] = ihid->hdesc_buffer[registerIndex];
231 		cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
232 	}
233 
234 	if (length > 2) {
235 		cmd->c.opcode = command->opcode;
236 		cmd->c.reportTypeID = reportID | reportType << 4;
237 	}
238 
239 	memcpy(cmd->data + length, args, args_len);
240 	length += args_len;
241 
242 	i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
243 
244 	msg[0].addr = client->addr;
245 	msg[0].flags = client->flags & I2C_M_TEN;
246 	msg[0].len = length;
247 	msg[0].buf = cmd->data;
248 	if (data_len > 0) {
249 		msg[1].addr = client->addr;
250 		msg[1].flags = client->flags & I2C_M_TEN;
251 		msg[1].flags |= I2C_M_RD;
252 		msg[1].len = data_len;
253 		msg[1].buf = buf_recv;
254 		msg_num = 2;
255 		set_bit(I2C_HID_READ_PENDING, &ihid->flags);
256 	}
257 
258 	if (wait)
259 		set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
260 
261 	ret = i2c_transfer(client->adapter, msg, msg_num);
262 
263 	if (data_len > 0)
264 		clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
265 
266 	if (ret != msg_num)
267 		return ret < 0 ? ret : -EIO;
268 
269 	ret = 0;
270 
271 	if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) {
272 		msleep(100);
273 	} else if (wait) {
274 		i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
275 		if (!wait_event_timeout(ihid->wait,
276 				!test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
277 				msecs_to_jiffies(5000)))
278 			ret = -ENODATA;
279 		i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
280 	}
281 
282 	return ret;
283 }
284 
285 static int i2c_hid_command(struct i2c_client *client,
286 		const struct i2c_hid_cmd *command,
287 		unsigned char *buf_recv, int data_len)
288 {
289 	return __i2c_hid_command(client, command, 0, 0, NULL, 0,
290 				buf_recv, data_len);
291 }
292 
293 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
294 		u8 reportID, unsigned char *buf_recv, int data_len)
295 {
296 	struct i2c_hid *ihid = i2c_get_clientdata(client);
297 	u8 args[3];
298 	int ret;
299 	int args_len = 0;
300 	u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
301 
302 	i2c_hid_dbg(ihid, "%s\n", __func__);
303 
304 	if (reportID >= 0x0F) {
305 		args[args_len++] = reportID;
306 		reportID = 0x0F;
307 	}
308 
309 	args[args_len++] = readRegister & 0xFF;
310 	args[args_len++] = readRegister >> 8;
311 
312 	ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
313 		reportType, args, args_len, buf_recv, data_len);
314 	if (ret) {
315 		dev_err(&client->dev,
316 			"failed to retrieve report from device.\n");
317 		return ret;
318 	}
319 
320 	return 0;
321 }
322 
323 /**
324  * i2c_hid_set_or_send_report: forward an incoming report to the device
325  * @client: the i2c_client of the device
326  * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
327  * @reportID: the report ID
328  * @buf: the actual data to transfer, without the report ID
329  * @len: size of buf
330  * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
331  */
332 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
333 		u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
334 {
335 	struct i2c_hid *ihid = i2c_get_clientdata(client);
336 	u8 *args = ihid->argsbuf;
337 	const struct i2c_hid_cmd *hidcmd;
338 	int ret;
339 	u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
340 	u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
341 	u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
342 	u16 size;
343 	int args_len;
344 	int index = 0;
345 
346 	i2c_hid_dbg(ihid, "%s\n", __func__);
347 
348 	if (data_len > ihid->bufsize)
349 		return -EINVAL;
350 
351 	size =		2			/* size */ +
352 			(reportID ? 1 : 0)	/* reportID */ +
353 			data_len		/* buf */;
354 	args_len =	(reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
355 			2			/* dataRegister */ +
356 			size			/* args */;
357 
358 	if (!use_data && maxOutputLength == 0)
359 		return -ENOSYS;
360 
361 	if (reportID >= 0x0F) {
362 		args[index++] = reportID;
363 		reportID = 0x0F;
364 	}
365 
366 	/*
367 	 * use the data register for feature reports or if the device does not
368 	 * support the output register
369 	 */
370 	if (use_data) {
371 		args[index++] = dataRegister & 0xFF;
372 		args[index++] = dataRegister >> 8;
373 		hidcmd = &hid_set_report_cmd;
374 	} else {
375 		args[index++] = outputRegister & 0xFF;
376 		args[index++] = outputRegister >> 8;
377 		hidcmd = &hid_no_cmd;
378 	}
379 
380 	args[index++] = size & 0xFF;
381 	args[index++] = size >> 8;
382 
383 	if (reportID)
384 		args[index++] = reportID;
385 
386 	memcpy(&args[index], buf, data_len);
387 
388 	ret = __i2c_hid_command(client, hidcmd, reportID,
389 		reportType, args, args_len, NULL, 0);
390 	if (ret) {
391 		dev_err(&client->dev, "failed to set a report to device.\n");
392 		return ret;
393 	}
394 
395 	return data_len;
396 }
397 
398 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
399 {
400 	struct i2c_hid *ihid = i2c_get_clientdata(client);
401 	int ret;
402 	unsigned long now, delay;
403 
404 	i2c_hid_dbg(ihid, "%s\n", __func__);
405 
406 	/*
407 	 * Some devices require to send a command to wakeup before power on.
408 	 * The call will get a return value (EREMOTEIO) but device will be
409 	 * triggered and activated. After that, it goes like a normal device.
410 	 */
411 	if (power_state == I2C_HID_PWR_ON &&
412 	    ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
413 		ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
414 
415 		/* Device was already activated */
416 		if (!ret)
417 			goto set_pwr_exit;
418 	}
419 
420 	if (ihid->quirks & I2C_HID_QUIRK_DELAY_AFTER_SLEEP &&
421 	    power_state == I2C_HID_PWR_ON) {
422 		now = jiffies;
423 		if (time_after(ihid->sleep_delay, now)) {
424 			delay = jiffies_to_usecs(ihid->sleep_delay - now);
425 			usleep_range(delay, delay + 1);
426 		}
427 	}
428 
429 	ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
430 		0, NULL, 0, NULL, 0);
431 
432 	if (ihid->quirks & I2C_HID_QUIRK_DELAY_AFTER_SLEEP &&
433 	    power_state == I2C_HID_PWR_SLEEP)
434 		ihid->sleep_delay = jiffies + msecs_to_jiffies(20);
435 
436 	if (ret)
437 		dev_err(&client->dev, "failed to change power setting.\n");
438 
439 set_pwr_exit:
440 	return ret;
441 }
442 
443 static int i2c_hid_hwreset(struct i2c_client *client)
444 {
445 	struct i2c_hid *ihid = i2c_get_clientdata(client);
446 	int ret;
447 
448 	i2c_hid_dbg(ihid, "%s\n", __func__);
449 
450 	/*
451 	 * This prevents sending feature reports while the device is
452 	 * being reset. Otherwise we may lose the reset complete
453 	 * interrupt.
454 	 */
455 	mutex_lock(&ihid->reset_lock);
456 
457 	ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
458 	if (ret)
459 		goto out_unlock;
460 
461 	/*
462 	 * The HID over I2C specification states that if a DEVICE needs time
463 	 * after the PWR_ON request, it should utilise CLOCK stretching.
464 	 * However, it has been observered that the Windows driver provides a
465 	 * 1ms sleep between the PWR_ON and RESET requests and that some devices
466 	 * rely on this.
467 	 */
468 	usleep_range(1000, 5000);
469 
470 	i2c_hid_dbg(ihid, "resetting...\n");
471 
472 	ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
473 	if (ret) {
474 		dev_err(&client->dev, "failed to reset device.\n");
475 		i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
476 	}
477 
478 out_unlock:
479 	mutex_unlock(&ihid->reset_lock);
480 	return ret;
481 }
482 
483 static void i2c_hid_get_input(struct i2c_hid *ihid)
484 {
485 	int ret;
486 	u32 ret_size;
487 	int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
488 
489 	if (size > ihid->bufsize)
490 		size = ihid->bufsize;
491 
492 	ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
493 	if (ret != size) {
494 		if (ret < 0)
495 			return;
496 
497 		dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
498 			__func__, ret, size);
499 		return;
500 	}
501 
502 	ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
503 
504 	if (!ret_size) {
505 		/* host or device initiated RESET completed */
506 		if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
507 			wake_up(&ihid->wait);
508 		return;
509 	}
510 
511 	if (ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ && ret_size == 0xffff) {
512 		dev_warn_once(&ihid->client->dev, "%s: IRQ triggered but "
513 			      "there's no data\n", __func__);
514 		return;
515 	}
516 
517 	if ((ret_size > size) || (ret_size < 2)) {
518 		dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
519 			__func__, size, ret_size);
520 		return;
521 	}
522 
523 	i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
524 
525 	if (test_bit(I2C_HID_STARTED, &ihid->flags))
526 		hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
527 				ret_size - 2, 1);
528 
529 	return;
530 }
531 
532 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
533 {
534 	struct i2c_hid *ihid = dev_id;
535 
536 	if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
537 		return IRQ_HANDLED;
538 
539 	i2c_hid_get_input(ihid);
540 
541 	return IRQ_HANDLED;
542 }
543 
544 static int i2c_hid_get_report_length(struct hid_report *report)
545 {
546 	return ((report->size - 1) >> 3) + 1 +
547 		report->device->report_enum[report->type].numbered + 2;
548 }
549 
550 /*
551  * Traverse the supplied list of reports and find the longest
552  */
553 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
554 		unsigned int *max)
555 {
556 	struct hid_report *report;
557 	unsigned int size;
558 
559 	/* We should not rely on wMaxInputLength, as some devices may set it to
560 	 * a wrong length. */
561 	list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
562 		size = i2c_hid_get_report_length(report);
563 		if (*max < size)
564 			*max = size;
565 	}
566 }
567 
568 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
569 {
570 	kfree(ihid->inbuf);
571 	kfree(ihid->rawbuf);
572 	kfree(ihid->argsbuf);
573 	kfree(ihid->cmdbuf);
574 	ihid->inbuf = NULL;
575 	ihid->rawbuf = NULL;
576 	ihid->cmdbuf = NULL;
577 	ihid->argsbuf = NULL;
578 	ihid->bufsize = 0;
579 }
580 
581 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
582 {
583 	/* the worst case is computed from the set_report command with a
584 	 * reportID > 15 and the maximum report length */
585 	int args_len = sizeof(__u8) + /* ReportID */
586 		       sizeof(__u8) + /* optional ReportID byte */
587 		       sizeof(__u16) + /* data register */
588 		       sizeof(__u16) + /* size of the report */
589 		       report_size; /* report */
590 
591 	ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
592 	ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
593 	ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
594 	ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
595 
596 	if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
597 		i2c_hid_free_buffers(ihid);
598 		return -ENOMEM;
599 	}
600 
601 	ihid->bufsize = report_size;
602 
603 	return 0;
604 }
605 
606 static int i2c_hid_get_raw_report(struct hid_device *hid,
607 		unsigned char report_number, __u8 *buf, size_t count,
608 		unsigned char report_type)
609 {
610 	struct i2c_client *client = hid->driver_data;
611 	struct i2c_hid *ihid = i2c_get_clientdata(client);
612 	size_t ret_count, ask_count;
613 	int ret;
614 
615 	if (report_type == HID_OUTPUT_REPORT)
616 		return -EINVAL;
617 
618 	/* +2 bytes to include the size of the reply in the query buffer */
619 	ask_count = min(count + 2, (size_t)ihid->bufsize);
620 
621 	ret = i2c_hid_get_report(client,
622 			report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
623 			report_number, ihid->rawbuf, ask_count);
624 
625 	if (ret < 0)
626 		return ret;
627 
628 	ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
629 
630 	if (ret_count <= 2)
631 		return 0;
632 
633 	ret_count = min(ret_count, ask_count);
634 
635 	/* The query buffer contains the size, dropping it in the reply */
636 	count = min(count, ret_count - 2);
637 	memcpy(buf, ihid->rawbuf + 2, count);
638 
639 	return count;
640 }
641 
642 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
643 		size_t count, unsigned char report_type, bool use_data)
644 {
645 	struct i2c_client *client = hid->driver_data;
646 	struct i2c_hid *ihid = i2c_get_clientdata(client);
647 	int report_id = buf[0];
648 	int ret;
649 
650 	if (report_type == HID_INPUT_REPORT)
651 		return -EINVAL;
652 
653 	mutex_lock(&ihid->reset_lock);
654 
655 	if (report_id) {
656 		buf++;
657 		count--;
658 	}
659 
660 	ret = i2c_hid_set_or_send_report(client,
661 				report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
662 				report_id, buf, count, use_data);
663 
664 	if (report_id && ret >= 0)
665 		ret++; /* add report_id to the number of transfered bytes */
666 
667 	mutex_unlock(&ihid->reset_lock);
668 
669 	return ret;
670 }
671 
672 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
673 		size_t count)
674 {
675 	return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
676 			false);
677 }
678 
679 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
680 			       __u8 *buf, size_t len, unsigned char rtype,
681 			       int reqtype)
682 {
683 	switch (reqtype) {
684 	case HID_REQ_GET_REPORT:
685 		return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
686 	case HID_REQ_SET_REPORT:
687 		if (buf[0] != reportnum)
688 			return -EINVAL;
689 		return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
690 	default:
691 		return -EIO;
692 	}
693 }
694 
695 static int i2c_hid_parse(struct hid_device *hid)
696 {
697 	struct i2c_client *client = hid->driver_data;
698 	struct i2c_hid *ihid = i2c_get_clientdata(client);
699 	struct i2c_hid_desc *hdesc = &ihid->hdesc;
700 	unsigned int rsize;
701 	char *rdesc;
702 	int ret;
703 	int tries = 3;
704 	char *use_override;
705 
706 	i2c_hid_dbg(ihid, "entering %s\n", __func__);
707 
708 	rsize = le16_to_cpu(hdesc->wReportDescLength);
709 	if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
710 		dbg_hid("weird size of report descriptor (%u)\n", rsize);
711 		return -EINVAL;
712 	}
713 
714 	do {
715 		ret = i2c_hid_hwreset(client);
716 		if (ret)
717 			msleep(1000);
718 	} while (tries-- > 0 && ret);
719 
720 	if (ret)
721 		return ret;
722 
723 	use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
724 								&rsize);
725 
726 	if (use_override) {
727 		rdesc = use_override;
728 		i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
729 	} else {
730 		rdesc = kzalloc(rsize, GFP_KERNEL);
731 
732 		if (!rdesc) {
733 			dbg_hid("couldn't allocate rdesc memory\n");
734 			return -ENOMEM;
735 		}
736 
737 		i2c_hid_dbg(ihid, "asking HID report descriptor\n");
738 
739 		ret = i2c_hid_command(client, &hid_report_descr_cmd,
740 				      rdesc, rsize);
741 		if (ret) {
742 			hid_err(hid, "reading report descriptor failed\n");
743 			kfree(rdesc);
744 			return -EIO;
745 		}
746 	}
747 
748 	i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
749 
750 	ret = hid_parse_report(hid, rdesc, rsize);
751 	if (!use_override)
752 		kfree(rdesc);
753 
754 	if (ret) {
755 		dbg_hid("parsing report descriptor failed\n");
756 		return ret;
757 	}
758 
759 	return 0;
760 }
761 
762 static int i2c_hid_start(struct hid_device *hid)
763 {
764 	struct i2c_client *client = hid->driver_data;
765 	struct i2c_hid *ihid = i2c_get_clientdata(client);
766 	int ret;
767 	unsigned int bufsize = HID_MIN_BUFFER_SIZE;
768 
769 	i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
770 	i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
771 	i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
772 
773 	if (bufsize > ihid->bufsize) {
774 		disable_irq(client->irq);
775 		i2c_hid_free_buffers(ihid);
776 
777 		ret = i2c_hid_alloc_buffers(ihid, bufsize);
778 		enable_irq(client->irq);
779 
780 		if (ret)
781 			return ret;
782 	}
783 
784 	return 0;
785 }
786 
787 static void i2c_hid_stop(struct hid_device *hid)
788 {
789 	hid->claimed = 0;
790 }
791 
792 static int i2c_hid_open(struct hid_device *hid)
793 {
794 	struct i2c_client *client = hid->driver_data;
795 	struct i2c_hid *ihid = i2c_get_clientdata(client);
796 	int ret = 0;
797 
798 	ret = pm_runtime_get_sync(&client->dev);
799 	if (ret < 0)
800 		return ret;
801 
802 	set_bit(I2C_HID_STARTED, &ihid->flags);
803 	return 0;
804 }
805 
806 static void i2c_hid_close(struct hid_device *hid)
807 {
808 	struct i2c_client *client = hid->driver_data;
809 	struct i2c_hid *ihid = i2c_get_clientdata(client);
810 
811 	clear_bit(I2C_HID_STARTED, &ihid->flags);
812 
813 	/* Save some power */
814 	pm_runtime_put(&client->dev);
815 }
816 
817 static int i2c_hid_power(struct hid_device *hid, int lvl)
818 {
819 	struct i2c_client *client = hid->driver_data;
820 	struct i2c_hid *ihid = i2c_get_clientdata(client);
821 
822 	i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
823 
824 	switch (lvl) {
825 	case PM_HINT_FULLON:
826 		pm_runtime_get_sync(&client->dev);
827 		break;
828 	case PM_HINT_NORMAL:
829 		pm_runtime_put(&client->dev);
830 		break;
831 	}
832 	return 0;
833 }
834 
835 struct hid_ll_driver i2c_hid_ll_driver = {
836 	.parse = i2c_hid_parse,
837 	.start = i2c_hid_start,
838 	.stop = i2c_hid_stop,
839 	.open = i2c_hid_open,
840 	.close = i2c_hid_close,
841 	.power = i2c_hid_power,
842 	.output_report = i2c_hid_output_report,
843 	.raw_request = i2c_hid_raw_request,
844 };
845 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
846 
847 static int i2c_hid_init_irq(struct i2c_client *client)
848 {
849 	struct i2c_hid *ihid = i2c_get_clientdata(client);
850 	unsigned long irqflags = 0;
851 	int ret;
852 
853 	dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
854 
855 	if (!irq_get_trigger_type(client->irq))
856 		irqflags = IRQF_TRIGGER_LOW;
857 
858 	ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
859 				   irqflags | IRQF_ONESHOT, client->name, ihid);
860 	if (ret < 0) {
861 		dev_warn(&client->dev,
862 			"Could not register for %s interrupt, irq = %d,"
863 			" ret = %d\n",
864 			client->name, client->irq, ret);
865 
866 		return ret;
867 	}
868 
869 	return 0;
870 }
871 
872 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
873 {
874 	struct i2c_client *client = ihid->client;
875 	struct i2c_hid_desc *hdesc = &ihid->hdesc;
876 	unsigned int dsize;
877 	int ret;
878 
879 	/* i2c hid fetch using a fixed descriptor size (30 bytes) */
880 	if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
881 		i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
882 		ihid->hdesc =
883 			*i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
884 	} else {
885 		i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
886 		ret = i2c_hid_command(client, &hid_descr_cmd,
887 				      ihid->hdesc_buffer,
888 				      sizeof(struct i2c_hid_desc));
889 		if (ret) {
890 			dev_err(&client->dev, "hid_descr_cmd failed\n");
891 			return -ENODEV;
892 		}
893 	}
894 
895 	/* Validate the length of HID descriptor, the 4 first bytes:
896 	 * bytes 0-1 -> length
897 	 * bytes 2-3 -> bcdVersion (has to be 1.00) */
898 	/* check bcdVersion == 1.0 */
899 	if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
900 		dev_err(&client->dev,
901 			"unexpected HID descriptor bcdVersion (0x%04hx)\n",
902 			le16_to_cpu(hdesc->bcdVersion));
903 		return -ENODEV;
904 	}
905 
906 	/* Descriptor length should be 30 bytes as per the specification */
907 	dsize = le16_to_cpu(hdesc->wHIDDescLength);
908 	if (dsize != sizeof(struct i2c_hid_desc)) {
909 		dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
910 			dsize);
911 		return -ENODEV;
912 	}
913 	i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
914 	return 0;
915 }
916 
917 #ifdef CONFIG_ACPI
918 static const struct acpi_device_id i2c_hid_acpi_blacklist[] = {
919 	/*
920 	 * The CHPN0001 ACPI device, which is used to describe the Chipone
921 	 * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible.
922 	 */
923 	{"CHPN0001", 0 },
924 	{ },
925 };
926 
927 static int i2c_hid_acpi_pdata(struct i2c_client *client,
928 		struct i2c_hid_platform_data *pdata)
929 {
930 	static guid_t i2c_hid_guid =
931 		GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
932 			  0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
933 	union acpi_object *obj;
934 	struct acpi_device *adev;
935 	acpi_handle handle;
936 
937 	handle = ACPI_HANDLE(&client->dev);
938 	if (!handle || acpi_bus_get_device(handle, &adev)) {
939 		dev_err(&client->dev, "Error could not get ACPI device\n");
940 		return -ENODEV;
941 	}
942 
943 	if (acpi_match_device_ids(adev, i2c_hid_acpi_blacklist) == 0)
944 		return -ENODEV;
945 
946 	obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
947 				      ACPI_TYPE_INTEGER);
948 	if (!obj) {
949 		dev_err(&client->dev, "Error _DSM call to get HID descriptor address failed\n");
950 		return -ENODEV;
951 	}
952 
953 	pdata->hid_descriptor_address = obj->integer.value;
954 	ACPI_FREE(obj);
955 
956 	return 0;
957 }
958 
959 static void i2c_hid_acpi_fix_up_power(struct device *dev)
960 {
961 	struct acpi_device *adev;
962 
963 	adev = ACPI_COMPANION(dev);
964 	if (adev)
965 		acpi_device_fix_up_power(adev);
966 }
967 
968 static const struct acpi_device_id i2c_hid_acpi_match[] = {
969 	{"ACPI0C50", 0 },
970 	{"PNP0C50", 0 },
971 	{ },
972 };
973 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
974 #else
975 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
976 		struct i2c_hid_platform_data *pdata)
977 {
978 	return -ENODEV;
979 }
980 
981 static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
982 #endif
983 
984 #ifdef CONFIG_OF
985 static int i2c_hid_of_probe(struct i2c_client *client,
986 		struct i2c_hid_platform_data *pdata)
987 {
988 	struct device *dev = &client->dev;
989 	u32 val;
990 	int ret;
991 
992 	ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
993 	if (ret) {
994 		dev_err(&client->dev, "HID register address not provided\n");
995 		return -ENODEV;
996 	}
997 	if (val >> 16) {
998 		dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
999 			val);
1000 		return -EINVAL;
1001 	}
1002 	pdata->hid_descriptor_address = val;
1003 
1004 	return 0;
1005 }
1006 
1007 static const struct of_device_id i2c_hid_of_match[] = {
1008 	{ .compatible = "hid-over-i2c" },
1009 	{},
1010 };
1011 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
1012 #else
1013 static inline int i2c_hid_of_probe(struct i2c_client *client,
1014 		struct i2c_hid_platform_data *pdata)
1015 {
1016 	return -ENODEV;
1017 }
1018 #endif
1019 
1020 static void i2c_hid_fwnode_probe(struct i2c_client *client,
1021 				 struct i2c_hid_platform_data *pdata)
1022 {
1023 	u32 val;
1024 
1025 	if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms",
1026 				      &val))
1027 		pdata->post_power_delay_ms = val;
1028 }
1029 
1030 static int i2c_hid_probe(struct i2c_client *client,
1031 			 const struct i2c_device_id *dev_id)
1032 {
1033 	int ret;
1034 	struct i2c_hid *ihid;
1035 	struct hid_device *hid;
1036 	__u16 hidRegister;
1037 	struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
1038 
1039 	dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
1040 
1041 	if (!client->irq) {
1042 		dev_err(&client->dev,
1043 			"HID over i2c has not been provided an Int IRQ\n");
1044 		return -EINVAL;
1045 	}
1046 
1047 	if (client->irq < 0) {
1048 		if (client->irq != -EPROBE_DEFER)
1049 			dev_err(&client->dev,
1050 				"HID over i2c doesn't have a valid IRQ\n");
1051 		return client->irq;
1052 	}
1053 
1054 	ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
1055 	if (!ihid)
1056 		return -ENOMEM;
1057 
1058 	if (client->dev.of_node) {
1059 		ret = i2c_hid_of_probe(client, &ihid->pdata);
1060 		if (ret)
1061 			return ret;
1062 	} else if (!platform_data) {
1063 		ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1064 		if (ret)
1065 			return ret;
1066 	} else {
1067 		ihid->pdata = *platform_data;
1068 	}
1069 
1070 	/* Parse platform agnostic common properties from ACPI / device tree */
1071 	i2c_hid_fwnode_probe(client, &ihid->pdata);
1072 
1073 	ihid->pdata.supplies[0].supply = "vdd";
1074 	ihid->pdata.supplies[1].supply = "vddl";
1075 
1076 	ret = devm_regulator_bulk_get(&client->dev,
1077 				      ARRAY_SIZE(ihid->pdata.supplies),
1078 				      ihid->pdata.supplies);
1079 	if (ret)
1080 		return ret;
1081 
1082 	ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1083 				    ihid->pdata.supplies);
1084 	if (ret < 0)
1085 		return ret;
1086 
1087 	if (ihid->pdata.post_power_delay_ms)
1088 		msleep(ihid->pdata.post_power_delay_ms);
1089 
1090 	i2c_set_clientdata(client, ihid);
1091 
1092 	ihid->client = client;
1093 
1094 	hidRegister = ihid->pdata.hid_descriptor_address;
1095 	ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1096 
1097 	init_waitqueue_head(&ihid->wait);
1098 	mutex_init(&ihid->reset_lock);
1099 
1100 	/* we need to allocate the command buffer without knowing the maximum
1101 	 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1102 	 * real computation later. */
1103 	ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1104 	if (ret < 0)
1105 		goto err_regulator;
1106 
1107 	i2c_hid_acpi_fix_up_power(&client->dev);
1108 
1109 	pm_runtime_get_noresume(&client->dev);
1110 	pm_runtime_set_active(&client->dev);
1111 	pm_runtime_enable(&client->dev);
1112 	device_enable_async_suspend(&client->dev);
1113 
1114 	/* Make sure there is something at this address */
1115 	ret = i2c_smbus_read_byte(client);
1116 	if (ret < 0) {
1117 		dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1118 		ret = -ENXIO;
1119 		goto err_pm;
1120 	}
1121 
1122 	ret = i2c_hid_fetch_hid_descriptor(ihid);
1123 	if (ret < 0)
1124 		goto err_pm;
1125 
1126 	ret = i2c_hid_init_irq(client);
1127 	if (ret < 0)
1128 		goto err_pm;
1129 
1130 	hid = hid_allocate_device();
1131 	if (IS_ERR(hid)) {
1132 		ret = PTR_ERR(hid);
1133 		goto err_irq;
1134 	}
1135 
1136 	ihid->hid = hid;
1137 
1138 	hid->driver_data = client;
1139 	hid->ll_driver = &i2c_hid_ll_driver;
1140 	hid->dev.parent = &client->dev;
1141 	hid->bus = BUS_I2C;
1142 	hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1143 	hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1144 	hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1145 
1146 	snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
1147 		 client->name, hid->vendor, hid->product);
1148 	strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1149 
1150 	ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1151 
1152 	ret = hid_add_device(hid);
1153 	if (ret) {
1154 		if (ret != -ENODEV)
1155 			hid_err(client, "can't add hid device: %d\n", ret);
1156 		goto err_mem_free;
1157 	}
1158 
1159 	if (!(ihid->quirks & I2C_HID_QUIRK_NO_RUNTIME_PM))
1160 		pm_runtime_put(&client->dev);
1161 
1162 	return 0;
1163 
1164 err_mem_free:
1165 	hid_destroy_device(hid);
1166 
1167 err_irq:
1168 	free_irq(client->irq, ihid);
1169 
1170 err_pm:
1171 	pm_runtime_put_noidle(&client->dev);
1172 	pm_runtime_disable(&client->dev);
1173 
1174 err_regulator:
1175 	regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1176 			       ihid->pdata.supplies);
1177 	i2c_hid_free_buffers(ihid);
1178 	return ret;
1179 }
1180 
1181 static int i2c_hid_remove(struct i2c_client *client)
1182 {
1183 	struct i2c_hid *ihid = i2c_get_clientdata(client);
1184 	struct hid_device *hid;
1185 
1186 	if (!(ihid->quirks & I2C_HID_QUIRK_NO_RUNTIME_PM))
1187 		pm_runtime_get_sync(&client->dev);
1188 	pm_runtime_disable(&client->dev);
1189 	pm_runtime_set_suspended(&client->dev);
1190 	pm_runtime_put_noidle(&client->dev);
1191 
1192 	hid = ihid->hid;
1193 	hid_destroy_device(hid);
1194 
1195 	free_irq(client->irq, ihid);
1196 
1197 	if (ihid->bufsize)
1198 		i2c_hid_free_buffers(ihid);
1199 
1200 	regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1201 			       ihid->pdata.supplies);
1202 
1203 	return 0;
1204 }
1205 
1206 static void i2c_hid_shutdown(struct i2c_client *client)
1207 {
1208 	struct i2c_hid *ihid = i2c_get_clientdata(client);
1209 
1210 	i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1211 	free_irq(client->irq, ihid);
1212 }
1213 
1214 #ifdef CONFIG_PM_SLEEP
1215 static int i2c_hid_suspend(struct device *dev)
1216 {
1217 	struct i2c_client *client = to_i2c_client(dev);
1218 	struct i2c_hid *ihid = i2c_get_clientdata(client);
1219 	struct hid_device *hid = ihid->hid;
1220 	int ret;
1221 	int wake_status;
1222 
1223 	if (hid->driver && hid->driver->suspend) {
1224 		/*
1225 		 * Wake up the device so that IO issues in
1226 		 * HID driver's suspend code can succeed.
1227 		 */
1228 		ret = pm_runtime_resume(dev);
1229 		if (ret < 0)
1230 			return ret;
1231 
1232 		ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1233 		if (ret < 0)
1234 			return ret;
1235 	}
1236 
1237 	if (!pm_runtime_suspended(dev)) {
1238 		/* Save some power */
1239 		i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1240 
1241 		disable_irq(client->irq);
1242 	}
1243 
1244 	if (device_may_wakeup(&client->dev)) {
1245 		wake_status = enable_irq_wake(client->irq);
1246 		if (!wake_status)
1247 			ihid->irq_wake_enabled = true;
1248 		else
1249 			hid_warn(hid, "Failed to enable irq wake: %d\n",
1250 				wake_status);
1251 	} else {
1252 		regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1253 				       ihid->pdata.supplies);
1254 	}
1255 
1256 	return 0;
1257 }
1258 
1259 static int i2c_hid_resume(struct device *dev)
1260 {
1261 	int ret;
1262 	struct i2c_client *client = to_i2c_client(dev);
1263 	struct i2c_hid *ihid = i2c_get_clientdata(client);
1264 	struct hid_device *hid = ihid->hid;
1265 	int wake_status;
1266 
1267 	if (!device_may_wakeup(&client->dev)) {
1268 		ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1269 					    ihid->pdata.supplies);
1270 		if (ret)
1271 			hid_warn(hid, "Failed to enable supplies: %d\n", ret);
1272 
1273 		if (ihid->pdata.post_power_delay_ms)
1274 			msleep(ihid->pdata.post_power_delay_ms);
1275 	} else if (ihid->irq_wake_enabled) {
1276 		wake_status = disable_irq_wake(client->irq);
1277 		if (!wake_status)
1278 			ihid->irq_wake_enabled = false;
1279 		else
1280 			hid_warn(hid, "Failed to disable irq wake: %d\n",
1281 				wake_status);
1282 	}
1283 
1284 	/* We'll resume to full power */
1285 	pm_runtime_disable(dev);
1286 	pm_runtime_set_active(dev);
1287 	pm_runtime_enable(dev);
1288 
1289 	enable_irq(client->irq);
1290 
1291 	/* Instead of resetting device, simply powers the device on. This
1292 	 * solves "incomplete reports" on Raydium devices 2386:3118 and
1293 	 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1294 	 * data after a suspend/resume.
1295 	 */
1296 	ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
1297 	if (ret)
1298 		return ret;
1299 
1300 	if (hid->driver && hid->driver->reset_resume) {
1301 		ret = hid->driver->reset_resume(hid);
1302 		return ret;
1303 	}
1304 
1305 	return 0;
1306 }
1307 #endif
1308 
1309 #ifdef CONFIG_PM
1310 static int i2c_hid_runtime_suspend(struct device *dev)
1311 {
1312 	struct i2c_client *client = to_i2c_client(dev);
1313 
1314 	i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1315 	disable_irq(client->irq);
1316 	return 0;
1317 }
1318 
1319 static int i2c_hid_runtime_resume(struct device *dev)
1320 {
1321 	struct i2c_client *client = to_i2c_client(dev);
1322 
1323 	enable_irq(client->irq);
1324 	i2c_hid_set_power(client, I2C_HID_PWR_ON);
1325 	return 0;
1326 }
1327 #endif
1328 
1329 static const struct dev_pm_ops i2c_hid_pm = {
1330 	SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1331 	SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
1332 			   NULL)
1333 };
1334 
1335 static const struct i2c_device_id i2c_hid_id_table[] = {
1336 	{ "hid", 0 },
1337 	{ "hid-over-i2c", 0 },
1338 	{ },
1339 };
1340 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1341 
1342 
1343 static struct i2c_driver i2c_hid_driver = {
1344 	.driver = {
1345 		.name	= "i2c_hid",
1346 		.pm	= &i2c_hid_pm,
1347 		.acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1348 		.of_match_table = of_match_ptr(i2c_hid_of_match),
1349 	},
1350 
1351 	.probe		= i2c_hid_probe,
1352 	.remove		= i2c_hid_remove,
1353 	.shutdown	= i2c_hid_shutdown,
1354 	.id_table	= i2c_hid_id_table,
1355 };
1356 
1357 module_i2c_driver(i2c_hid_driver);
1358 
1359 MODULE_DESCRIPTION("HID over I2C core driver");
1360 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1361 MODULE_LICENSE("GPL");
1362