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