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