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 /*
624 * In case of unnumbered reports the response from the device will
625 * not have the report ID that the upper layers expect, so we need
626 * to stash it the buffer ourselves and adjust the data size.
627 */
628 if (!report_number) {
629 buf[0] = 0;
630 buf++;
631 count--;
632 }
633
634 /* +2 bytes to include the size of the reply in the query buffer */
635 ask_count = min(count + 2, (size_t)ihid->bufsize);
636
637 ret = i2c_hid_get_report(client,
638 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
639 report_number, ihid->rawbuf, ask_count);
640
641 if (ret < 0)
642 return ret;
643
644 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
645
646 if (ret_count <= 2)
647 return 0;
648
649 ret_count = min(ret_count, ask_count);
650
651 /* The query buffer contains the size, dropping it in the reply */
652 count = min(count, ret_count - 2);
653 memcpy(buf, ihid->rawbuf + 2, count);
654
655 if (!report_number)
656 count++;
657
658 return count;
659 }
660
i2c_hid_output_raw_report(struct hid_device * hid,__u8 * buf,size_t count,unsigned char report_type,bool use_data)661 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
662 size_t count, unsigned char report_type, bool use_data)
663 {
664 struct i2c_client *client = hid->driver_data;
665 struct i2c_hid *ihid = i2c_get_clientdata(client);
666 int report_id = buf[0];
667 int ret;
668
669 if (report_type == HID_INPUT_REPORT)
670 return -EINVAL;
671
672 mutex_lock(&ihid->reset_lock);
673
674 /*
675 * Note that both numbered and unnumbered reports passed here
676 * are supposed to have report ID stored in the 1st byte of the
677 * buffer, so we strip it off unconditionally before passing payload
678 * to i2c_hid_set_or_send_report which takes care of encoding
679 * everything properly.
680 */
681 ret = i2c_hid_set_or_send_report(client,
682 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
683 report_id, buf + 1, count - 1, use_data);
684
685 if (ret >= 0)
686 ret++; /* add report_id to the number of transferred bytes */
687
688 mutex_unlock(&ihid->reset_lock);
689
690 return ret;
691 }
692
i2c_hid_output_report(struct hid_device * hid,__u8 * buf,size_t count)693 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
694 size_t count)
695 {
696 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
697 false);
698 }
699
i2c_hid_raw_request(struct hid_device * hid,unsigned char reportnum,__u8 * buf,size_t len,unsigned char rtype,int reqtype)700 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
701 __u8 *buf, size_t len, unsigned char rtype,
702 int reqtype)
703 {
704 switch (reqtype) {
705 case HID_REQ_GET_REPORT:
706 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
707 case HID_REQ_SET_REPORT:
708 if (buf[0] != reportnum)
709 return -EINVAL;
710 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
711 default:
712 return -EIO;
713 }
714 }
715
i2c_hid_parse(struct hid_device * hid)716 static int i2c_hid_parse(struct hid_device *hid)
717 {
718 struct i2c_client *client = hid->driver_data;
719 struct i2c_hid *ihid = i2c_get_clientdata(client);
720 struct i2c_hid_desc *hdesc = &ihid->hdesc;
721 unsigned int rsize;
722 char *rdesc;
723 int ret;
724 int tries = 3;
725 char *use_override;
726
727 i2c_hid_dbg(ihid, "entering %s\n", __func__);
728
729 rsize = le16_to_cpu(hdesc->wReportDescLength);
730 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
731 dbg_hid("weird size of report descriptor (%u)\n", rsize);
732 return -EINVAL;
733 }
734
735 do {
736 ret = i2c_hid_hwreset(client);
737 if (ret)
738 msleep(1000);
739 } while (tries-- > 0 && ret);
740
741 if (ret)
742 return ret;
743
744 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
745 &rsize);
746
747 if (use_override) {
748 rdesc = use_override;
749 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
750 } else {
751 rdesc = kzalloc(rsize, GFP_KERNEL);
752
753 if (!rdesc) {
754 dbg_hid("couldn't allocate rdesc memory\n");
755 return -ENOMEM;
756 }
757
758 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
759
760 ret = i2c_hid_command(client, &hid_report_descr_cmd,
761 rdesc, rsize);
762 if (ret) {
763 hid_err(hid, "reading report descriptor failed\n");
764 kfree(rdesc);
765 return -EIO;
766 }
767 }
768
769 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
770
771 ret = hid_parse_report(hid, rdesc, rsize);
772 if (!use_override)
773 kfree(rdesc);
774
775 if (ret) {
776 dbg_hid("parsing report descriptor failed\n");
777 return ret;
778 }
779
780 return 0;
781 }
782
i2c_hid_start(struct hid_device * hid)783 static int i2c_hid_start(struct hid_device *hid)
784 {
785 struct i2c_client *client = hid->driver_data;
786 struct i2c_hid *ihid = i2c_get_clientdata(client);
787 int ret;
788 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
789
790 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
791 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
792 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
793
794 if (bufsize > ihid->bufsize) {
795 disable_irq(client->irq);
796 i2c_hid_free_buffers(ihid);
797
798 ret = i2c_hid_alloc_buffers(ihid, bufsize);
799 enable_irq(client->irq);
800
801 if (ret)
802 return ret;
803 }
804
805 return 0;
806 }
807
i2c_hid_stop(struct hid_device * hid)808 static void i2c_hid_stop(struct hid_device *hid)
809 {
810 hid->claimed = 0;
811 }
812
i2c_hid_open(struct hid_device * hid)813 static int i2c_hid_open(struct hid_device *hid)
814 {
815 struct i2c_client *client = hid->driver_data;
816 struct i2c_hid *ihid = i2c_get_clientdata(client);
817
818 set_bit(I2C_HID_STARTED, &ihid->flags);
819 return 0;
820 }
821
i2c_hid_close(struct hid_device * hid)822 static void i2c_hid_close(struct hid_device *hid)
823 {
824 struct i2c_client *client = hid->driver_data;
825 struct i2c_hid *ihid = i2c_get_clientdata(client);
826
827 clear_bit(I2C_HID_STARTED, &ihid->flags);
828 }
829
830 struct hid_ll_driver i2c_hid_ll_driver = {
831 .parse = i2c_hid_parse,
832 .start = i2c_hid_start,
833 .stop = i2c_hid_stop,
834 .open = i2c_hid_open,
835 .close = i2c_hid_close,
836 .output_report = i2c_hid_output_report,
837 .raw_request = i2c_hid_raw_request,
838 };
839 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
840
i2c_hid_init_irq(struct i2c_client * client)841 static int i2c_hid_init_irq(struct i2c_client *client)
842 {
843 struct i2c_hid *ihid = i2c_get_clientdata(client);
844 unsigned long irqflags = 0;
845 int ret;
846
847 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
848
849 if (!irq_get_trigger_type(client->irq))
850 irqflags = IRQF_TRIGGER_LOW;
851
852 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
853 irqflags | IRQF_ONESHOT, client->name, ihid);
854 if (ret < 0) {
855 dev_warn(&client->dev,
856 "Could not register for %s interrupt, irq = %d,"
857 " ret = %d\n",
858 client->name, client->irq, ret);
859
860 return ret;
861 }
862
863 return 0;
864 }
865
i2c_hid_fetch_hid_descriptor(struct i2c_hid * ihid)866 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
867 {
868 struct i2c_client *client = ihid->client;
869 struct i2c_hid_desc *hdesc = &ihid->hdesc;
870 unsigned int dsize;
871 int ret;
872
873 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
874 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
875 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
876 ihid->hdesc =
877 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
878 } else {
879 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
880 ret = i2c_hid_command(client, &hid_descr_cmd,
881 ihid->hdesc_buffer,
882 sizeof(struct i2c_hid_desc));
883 if (ret) {
884 dev_err(&client->dev, "hid_descr_cmd failed\n");
885 return -ENODEV;
886 }
887 }
888
889 /* Validate the length of HID descriptor, the 4 first bytes:
890 * bytes 0-1 -> length
891 * bytes 2-3 -> bcdVersion (has to be 1.00) */
892 /* check bcdVersion == 1.0 */
893 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
894 dev_err(&client->dev,
895 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
896 le16_to_cpu(hdesc->bcdVersion));
897 return -ENODEV;
898 }
899
900 /* Descriptor length should be 30 bytes as per the specification */
901 dsize = le16_to_cpu(hdesc->wHIDDescLength);
902 if (dsize != sizeof(struct i2c_hid_desc)) {
903 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
904 dsize);
905 return -ENODEV;
906 }
907 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
908 return 0;
909 }
910
911 #ifdef CONFIG_ACPI
912 static const struct acpi_device_id i2c_hid_acpi_blacklist[] = {
913 /*
914 * The CHPN0001 ACPI device, which is used to describe the Chipone
915 * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible.
916 */
917 {"CHPN0001", 0 },
918 { },
919 };
920
i2c_hid_acpi_pdata(struct i2c_client * client,struct i2c_hid_platform_data * pdata)921 static int i2c_hid_acpi_pdata(struct i2c_client *client,
922 struct i2c_hid_platform_data *pdata)
923 {
924 static guid_t i2c_hid_guid =
925 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
926 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
927 union acpi_object *obj;
928 struct acpi_device *adev;
929 acpi_handle handle;
930
931 handle = ACPI_HANDLE(&client->dev);
932 if (!handle || acpi_bus_get_device(handle, &adev)) {
933 dev_err(&client->dev, "Error could not get ACPI device\n");
934 return -ENODEV;
935 }
936
937 if (acpi_match_device_ids(adev, i2c_hid_acpi_blacklist) == 0)
938 return -ENODEV;
939
940 obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
941 ACPI_TYPE_INTEGER);
942 if (!obj) {
943 dev_err(&client->dev, "Error _DSM call to get HID descriptor address failed\n");
944 return -ENODEV;
945 }
946
947 pdata->hid_descriptor_address = obj->integer.value;
948 ACPI_FREE(obj);
949
950 return 0;
951 }
952
i2c_hid_acpi_fix_up_power(struct device * dev)953 static void i2c_hid_acpi_fix_up_power(struct device *dev)
954 {
955 struct acpi_device *adev;
956
957 adev = ACPI_COMPANION(dev);
958 if (adev)
959 acpi_device_fix_up_power(adev);
960 }
961
i2c_hid_acpi_enable_wakeup(struct device * dev)962 static void i2c_hid_acpi_enable_wakeup(struct device *dev)
963 {
964 if (acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0) {
965 device_set_wakeup_capable(dev, true);
966 device_set_wakeup_enable(dev, false);
967 }
968 }
969
i2c_hid_acpi_shutdown(struct device * dev)970 static void i2c_hid_acpi_shutdown(struct device *dev)
971 {
972 acpi_device_set_power(ACPI_COMPANION(dev), ACPI_STATE_D3_COLD);
973 }
974
975 static const struct acpi_device_id i2c_hid_acpi_match[] = {
976 {"ACPI0C50", 0 },
977 {"PNP0C50", 0 },
978 { },
979 };
980 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
981 #else
i2c_hid_acpi_pdata(struct i2c_client * client,struct i2c_hid_platform_data * pdata)982 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
983 struct i2c_hid_platform_data *pdata)
984 {
985 return -ENODEV;
986 }
987
i2c_hid_acpi_fix_up_power(struct device * dev)988 static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
989
i2c_hid_acpi_enable_wakeup(struct device * dev)990 static inline void i2c_hid_acpi_enable_wakeup(struct device *dev) {}
991
i2c_hid_acpi_shutdown(struct device * dev)992 static inline void i2c_hid_acpi_shutdown(struct device *dev) {}
993 #endif
994
995 #ifdef CONFIG_OF
i2c_hid_of_probe(struct i2c_client * client,struct i2c_hid_platform_data * pdata)996 static int i2c_hid_of_probe(struct i2c_client *client,
997 struct i2c_hid_platform_data *pdata)
998 {
999 struct device *dev = &client->dev;
1000 u32 val;
1001 int ret;
1002
1003 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
1004 if (ret) {
1005 dev_err(&client->dev, "HID register address not provided\n");
1006 return -ENODEV;
1007 }
1008 if (val >> 16) {
1009 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
1010 val);
1011 return -EINVAL;
1012 }
1013 pdata->hid_descriptor_address = val;
1014
1015 return 0;
1016 }
1017
1018 static const struct of_device_id i2c_hid_of_match[] = {
1019 { .compatible = "hid-over-i2c" },
1020 {},
1021 };
1022 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
1023 #else
i2c_hid_of_probe(struct i2c_client * client,struct i2c_hid_platform_data * pdata)1024 static inline int i2c_hid_of_probe(struct i2c_client *client,
1025 struct i2c_hid_platform_data *pdata)
1026 {
1027 return -ENODEV;
1028 }
1029 #endif
1030
i2c_hid_fwnode_probe(struct i2c_client * client,struct i2c_hid_platform_data * pdata)1031 static void i2c_hid_fwnode_probe(struct i2c_client *client,
1032 struct i2c_hid_platform_data *pdata)
1033 {
1034 u32 val;
1035
1036 if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms",
1037 &val))
1038 pdata->post_power_delay_ms = val;
1039 }
1040
i2c_hid_probe(struct i2c_client * client,const struct i2c_device_id * dev_id)1041 static int i2c_hid_probe(struct i2c_client *client,
1042 const struct i2c_device_id *dev_id)
1043 {
1044 int ret;
1045 struct i2c_hid *ihid;
1046 struct hid_device *hid;
1047 __u16 hidRegister;
1048 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
1049
1050 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
1051
1052 if (!client->irq) {
1053 dev_err(&client->dev,
1054 "HID over i2c has not been provided an Int IRQ\n");
1055 return -EINVAL;
1056 }
1057
1058 if (client->irq < 0) {
1059 if (client->irq != -EPROBE_DEFER)
1060 dev_err(&client->dev,
1061 "HID over i2c doesn't have a valid IRQ\n");
1062 return client->irq;
1063 }
1064
1065 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
1066 if (!ihid)
1067 return -ENOMEM;
1068
1069 if (client->dev.of_node) {
1070 ret = i2c_hid_of_probe(client, &ihid->pdata);
1071 if (ret)
1072 return ret;
1073 } else if (!platform_data) {
1074 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1075 if (ret)
1076 return ret;
1077 } else {
1078 ihid->pdata = *platform_data;
1079 }
1080
1081 /* Parse platform agnostic common properties from ACPI / device tree */
1082 i2c_hid_fwnode_probe(client, &ihid->pdata);
1083
1084 ihid->pdata.supplies[0].supply = "vdd";
1085 ihid->pdata.supplies[1].supply = "vddl";
1086
1087 ret = devm_regulator_bulk_get(&client->dev,
1088 ARRAY_SIZE(ihid->pdata.supplies),
1089 ihid->pdata.supplies);
1090 if (ret)
1091 return ret;
1092
1093 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1094 ihid->pdata.supplies);
1095 if (ret < 0)
1096 return ret;
1097
1098 if (ihid->pdata.post_power_delay_ms)
1099 msleep(ihid->pdata.post_power_delay_ms);
1100
1101 i2c_set_clientdata(client, ihid);
1102
1103 ihid->client = client;
1104
1105 hidRegister = ihid->pdata.hid_descriptor_address;
1106 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1107
1108 init_waitqueue_head(&ihid->wait);
1109 mutex_init(&ihid->reset_lock);
1110
1111 /* we need to allocate the command buffer without knowing the maximum
1112 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1113 * real computation later. */
1114 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1115 if (ret < 0)
1116 goto err_regulator;
1117
1118 i2c_hid_acpi_fix_up_power(&client->dev);
1119
1120 i2c_hid_acpi_enable_wakeup(&client->dev);
1121
1122 device_enable_async_suspend(&client->dev);
1123
1124 /* Make sure there is something at this address */
1125 ret = i2c_smbus_read_byte(client);
1126 if (ret < 0) {
1127 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1128 ret = -ENXIO;
1129 goto err_regulator;
1130 }
1131
1132 ret = i2c_hid_fetch_hid_descriptor(ihid);
1133 if (ret < 0)
1134 goto err_regulator;
1135
1136 ret = i2c_hid_init_irq(client);
1137 if (ret < 0)
1138 goto err_regulator;
1139
1140 hid = hid_allocate_device();
1141 if (IS_ERR(hid)) {
1142 ret = PTR_ERR(hid);
1143 goto err_irq;
1144 }
1145
1146 ihid->hid = hid;
1147
1148 hid->driver_data = client;
1149 hid->ll_driver = &i2c_hid_ll_driver;
1150 hid->dev.parent = &client->dev;
1151 hid->bus = BUS_I2C;
1152 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1153 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1154 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1155
1156 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1157 client->name, (u16)hid->vendor, (u16)hid->product);
1158 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1159
1160 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1161
1162 ret = hid_add_device(hid);
1163 if (ret) {
1164 if (ret != -ENODEV)
1165 hid_err(client, "can't add hid device: %d\n", ret);
1166 goto err_mem_free;
1167 }
1168
1169 return 0;
1170
1171 err_mem_free:
1172 hid_destroy_device(hid);
1173
1174 err_irq:
1175 free_irq(client->irq, ihid);
1176
1177 err_regulator:
1178 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1179 ihid->pdata.supplies);
1180 i2c_hid_free_buffers(ihid);
1181 return ret;
1182 }
1183
i2c_hid_remove(struct i2c_client * client)1184 static int i2c_hid_remove(struct i2c_client *client)
1185 {
1186 struct i2c_hid *ihid = i2c_get_clientdata(client);
1187 struct hid_device *hid;
1188
1189 hid = ihid->hid;
1190 hid_destroy_device(hid);
1191
1192 free_irq(client->irq, ihid);
1193
1194 if (ihid->bufsize)
1195 i2c_hid_free_buffers(ihid);
1196
1197 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1198 ihid->pdata.supplies);
1199
1200 return 0;
1201 }
1202
i2c_hid_shutdown(struct i2c_client * client)1203 static void i2c_hid_shutdown(struct i2c_client *client)
1204 {
1205 struct i2c_hid *ihid = i2c_get_clientdata(client);
1206
1207 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1208 free_irq(client->irq, ihid);
1209
1210 i2c_hid_acpi_shutdown(&client->dev);
1211 }
1212
1213 #ifdef CONFIG_PM_SLEEP
i2c_hid_suspend(struct device * dev)1214 static int i2c_hid_suspend(struct device *dev)
1215 {
1216 struct i2c_client *client = to_i2c_client(dev);
1217 struct i2c_hid *ihid = i2c_get_clientdata(client);
1218 struct hid_device *hid = ihid->hid;
1219 int ret;
1220 int wake_status;
1221
1222 if (hid->driver && hid->driver->suspend) {
1223 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1224 if (ret < 0)
1225 return ret;
1226 }
1227
1228 /* Save some power */
1229 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1230
1231 disable_irq(client->irq);
1232
1233 if (device_may_wakeup(&client->dev)) {
1234 wake_status = enable_irq_wake(client->irq);
1235 if (!wake_status)
1236 ihid->irq_wake_enabled = true;
1237 else
1238 hid_warn(hid, "Failed to enable irq wake: %d\n",
1239 wake_status);
1240 } else {
1241 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1242 ihid->pdata.supplies);
1243 }
1244
1245 return 0;
1246 }
1247
i2c_hid_resume(struct device * dev)1248 static int i2c_hid_resume(struct device *dev)
1249 {
1250 int ret;
1251 struct i2c_client *client = to_i2c_client(dev);
1252 struct i2c_hid *ihid = i2c_get_clientdata(client);
1253 struct hid_device *hid = ihid->hid;
1254 int wake_status;
1255
1256 if (!device_may_wakeup(&client->dev)) {
1257 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1258 ihid->pdata.supplies);
1259 if (ret)
1260 hid_warn(hid, "Failed to enable supplies: %d\n", ret);
1261
1262 if (ihid->pdata.post_power_delay_ms)
1263 msleep(ihid->pdata.post_power_delay_ms);
1264 } else if (ihid->irq_wake_enabled) {
1265 wake_status = disable_irq_wake(client->irq);
1266 if (!wake_status)
1267 ihid->irq_wake_enabled = false;
1268 else
1269 hid_warn(hid, "Failed to disable irq wake: %d\n",
1270 wake_status);
1271 }
1272
1273 enable_irq(client->irq);
1274
1275 /* Instead of resetting device, simply powers the device on. This
1276 * solves "incomplete reports" on Raydium devices 2386:3118 and
1277 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1278 * data after a suspend/resume.
1279 *
1280 * However some ALPS touchpads generate IRQ storm without reset, so
1281 * let's still reset them here.
1282 */
1283 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
1284 ret = i2c_hid_hwreset(client);
1285 else
1286 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
1287
1288 if (ret)
1289 return ret;
1290
1291 if (hid->driver && hid->driver->reset_resume) {
1292 ret = hid->driver->reset_resume(hid);
1293 return ret;
1294 }
1295
1296 return 0;
1297 }
1298 #endif
1299
1300 static const struct dev_pm_ops i2c_hid_pm = {
1301 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1302 };
1303
1304 static const struct i2c_device_id i2c_hid_id_table[] = {
1305 { "hid", 0 },
1306 { "hid-over-i2c", 0 },
1307 { },
1308 };
1309 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1310
1311
1312 static struct i2c_driver i2c_hid_driver = {
1313 .driver = {
1314 .name = "i2c_hid",
1315 .pm = &i2c_hid_pm,
1316 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1317 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1318 .of_match_table = of_match_ptr(i2c_hid_of_match),
1319 },
1320
1321 .probe = i2c_hid_probe,
1322 .remove = i2c_hid_remove,
1323 .shutdown = i2c_hid_shutdown,
1324 .id_table = i2c_hid_id_table,
1325 };
1326
1327 module_i2c_driver(i2c_hid_driver);
1328
1329 MODULE_DESCRIPTION("HID over I2C core driver");
1330 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1331 MODULE_LICENSE("GPL");
1332