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