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