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