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