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