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