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