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