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