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