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