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