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