blob: 7062df21da52b8743ca0a2c86e9ef75455240314 [file] [log] [blame]
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001/*
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>
Benjamin Tissoires4a200c32012-11-12 15:42:59 +010035#include <linux/hid.h>
36
37#include <linux/i2c/i2c-hid.h>
38
39/* flags */
40#define I2C_HID_STARTED (1 << 0)
41#define I2C_HID_RESET_PENDING (1 << 1)
42#define I2C_HID_READ_PENDING (1 << 2)
43
44#define I2C_HID_PWR_ON 0x00
45#define I2C_HID_PWR_SLEEP 0x01
46
47/* debug option */
Benjamin Tissoiresee8e8802012-12-04 16:27:45 +010048static bool debug;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +010049module_param(debug, bool, 0444);
50MODULE_PARM_DESC(debug, "print a lot of debug information");
51
Benjamin Tissoiresfa738642012-12-04 16:27:46 +010052#define i2c_hid_dbg(ihid, fmt, arg...) \
53do { \
54 if (debug) \
55 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
56} while (0)
Benjamin Tissoires4a200c32012-11-12 15:42:59 +010057
58struct i2c_hid_desc {
59 __le16 wHIDDescLength;
60 __le16 bcdVersion;
61 __le16 wReportDescLength;
62 __le16 wReportDescRegister;
63 __le16 wInputRegister;
64 __le16 wMaxInputLength;
65 __le16 wOutputRegister;
66 __le16 wMaxOutputLength;
67 __le16 wCommandRegister;
68 __le16 wDataRegister;
69 __le16 wVendorID;
70 __le16 wProductID;
71 __le16 wVersionID;
72} __packed;
73
74struct i2c_hid_cmd {
75 unsigned int registerIndex;
76 __u8 opcode;
77 unsigned int length;
78 bool wait;
79};
80
81union command {
82 u8 data[0];
83 struct cmd {
84 __le16 reg;
85 __u8 reportTypeID;
86 __u8 opcode;
87 } __packed c;
88};
89
90#define I2C_HID_CMD(opcode_) \
91 .opcode = opcode_, .length = 4, \
92 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
93
94/* fetch HID descriptor */
95static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
96/* fetch report descriptors */
97static const struct i2c_hid_cmd hid_report_descr_cmd = {
98 .registerIndex = offsetof(struct i2c_hid_desc,
99 wReportDescRegister),
100 .opcode = 0x00,
101 .length = 2 };
102/* commands */
103static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
104 .wait = true };
105static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
106static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100107static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
Benjamin Tissoires6bf6c8b2012-12-04 16:27:47 +0100108
109/*
110 * These definitions are not used here, but are defined by the spec.
111 * Keeping them here for documentation purposes.
112 *
113 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
114 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
115 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
116 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
117 */
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100118
119/* The main device structure */
120struct i2c_hid {
121 struct i2c_client *client; /* i2c client */
122 struct hid_device *hid; /* pointer to corresponding HID dev */
123 union {
124 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
125 struct i2c_hid_desc hdesc; /* the HID Descriptor */
126 };
127 __le16 wHIDDescRegister; /* location of the i2c
128 * register of the HID
129 * descriptor. */
130 unsigned int bufsize; /* i2c buffer size */
131 char *inbuf; /* Input buffer */
132 char *cmdbuf; /* Command buffer */
133 char *argsbuf; /* Command arguments buffer */
134
135 unsigned long flags; /* device flags */
136
137 int irq; /* the interrupt line irq */
138
139 wait_queue_head_t wait; /* For waiting the interrupt */
140};
141
142static int __i2c_hid_command(struct i2c_client *client,
143 const struct i2c_hid_cmd *command, u8 reportID,
144 u8 reportType, u8 *args, int args_len,
145 unsigned char *buf_recv, int data_len)
146{
147 struct i2c_hid *ihid = i2c_get_clientdata(client);
148 union command *cmd = (union command *)ihid->cmdbuf;
149 int ret;
150 struct i2c_msg msg[2];
151 int msg_num = 1;
152
153 int length = command->length;
154 bool wait = command->wait;
155 unsigned int registerIndex = command->registerIndex;
156
157 /* special case for hid_descr_cmd */
158 if (command == &hid_descr_cmd) {
159 cmd->c.reg = ihid->wHIDDescRegister;
160 } else {
161 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
162 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
163 }
164
165 if (length > 2) {
166 cmd->c.opcode = command->opcode;
167 cmd->c.reportTypeID = reportID | reportType << 4;
168 }
169
170 memcpy(cmd->data + length, args, args_len);
171 length += args_len;
172
173 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
174
175 msg[0].addr = client->addr;
176 msg[0].flags = client->flags & I2C_M_TEN;
177 msg[0].len = length;
178 msg[0].buf = cmd->data;
179 if (data_len > 0) {
180 msg[1].addr = client->addr;
181 msg[1].flags = client->flags & I2C_M_TEN;
182 msg[1].flags |= I2C_M_RD;
183 msg[1].len = data_len;
184 msg[1].buf = buf_recv;
185 msg_num = 2;
186 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
187 }
188
189 if (wait)
190 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
191
192 ret = i2c_transfer(client->adapter, msg, msg_num);
193
194 if (data_len > 0)
195 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
196
197 if (ret != msg_num)
198 return ret < 0 ? ret : -EIO;
199
200 ret = 0;
201
202 if (wait) {
203 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
204 if (!wait_event_timeout(ihid->wait,
205 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
206 msecs_to_jiffies(5000)))
207 ret = -ENODATA;
208 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
209 }
210
211 return ret;
212}
213
214static int i2c_hid_command(struct i2c_client *client,
215 const struct i2c_hid_cmd *command,
216 unsigned char *buf_recv, int data_len)
217{
218 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
219 buf_recv, data_len);
220}
221
222static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
223 u8 reportID, unsigned char *buf_recv, int data_len)
224{
225 struct i2c_hid *ihid = i2c_get_clientdata(client);
226 u8 args[3];
227 int ret;
228 int args_len = 0;
229 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
230
231 i2c_hid_dbg(ihid, "%s\n", __func__);
232
233 if (reportID >= 0x0F) {
234 args[args_len++] = reportID;
235 reportID = 0x0F;
236 }
237
238 args[args_len++] = readRegister & 0xFF;
239 args[args_len++] = readRegister >> 8;
240
241 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
242 reportType, args, args_len, buf_recv, data_len);
243 if (ret) {
244 dev_err(&client->dev,
245 "failed to retrieve report from device.\n");
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100246 return ret;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100247 }
248
249 return 0;
250}
251
252static int i2c_hid_set_report(struct i2c_client *client, u8 reportType,
253 u8 reportID, unsigned char *buf, size_t data_len)
254{
255 struct i2c_hid *ihid = i2c_get_clientdata(client);
256 u8 *args = ihid->argsbuf;
257 int ret;
258 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
259
260 /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */
261 u16 size = 2 /* size */ +
262 (reportID ? 1 : 0) /* reportID */ +
263 data_len /* buf */;
264 int args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
265 2 /* dataRegister */ +
266 size /* args */;
267 int index = 0;
268
269 i2c_hid_dbg(ihid, "%s\n", __func__);
270
271 if (reportID >= 0x0F) {
272 args[index++] = reportID;
273 reportID = 0x0F;
274 }
275
276 args[index++] = dataRegister & 0xFF;
277 args[index++] = dataRegister >> 8;
278
279 args[index++] = size & 0xFF;
280 args[index++] = size >> 8;
281
282 if (reportID)
283 args[index++] = reportID;
284
285 memcpy(&args[index], buf, data_len);
286
287 ret = __i2c_hid_command(client, &hid_set_report_cmd, reportID,
288 reportType, args, args_len, NULL, 0);
289 if (ret) {
290 dev_err(&client->dev, "failed to set a report to device.\n");
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100291 return ret;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100292 }
293
294 return data_len;
295}
296
297static int i2c_hid_set_power(struct i2c_client *client, int power_state)
298{
299 struct i2c_hid *ihid = i2c_get_clientdata(client);
300 int ret;
301
302 i2c_hid_dbg(ihid, "%s\n", __func__);
303
304 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
305 0, NULL, 0, NULL, 0);
306 if (ret)
307 dev_err(&client->dev, "failed to change power setting.\n");
308
309 return ret;
310}
311
312static int i2c_hid_hwreset(struct i2c_client *client)
313{
314 struct i2c_hid *ihid = i2c_get_clientdata(client);
315 int ret;
316
317 i2c_hid_dbg(ihid, "%s\n", __func__);
318
319 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
320 if (ret)
321 return ret;
322
323 i2c_hid_dbg(ihid, "resetting...\n");
324
325 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
326 if (ret) {
327 dev_err(&client->dev, "failed to reset device.\n");
328 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
329 return ret;
330 }
331
332 return 0;
333}
334
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100335static void i2c_hid_get_input(struct i2c_hid *ihid)
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100336{
337 int ret, ret_size;
338 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
339
340 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
341 if (ret != size) {
342 if (ret < 0)
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100343 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100344
345 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
346 __func__, ret, size);
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100347 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100348 }
349
350 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
351
352 if (!ret_size) {
353 /* host or device initiated RESET completed */
354 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
355 wake_up(&ihid->wait);
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100356 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100357 }
358
359 if (ret_size > size) {
360 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
361 __func__, size, ret_size);
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100362 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100363 }
364
365 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
366
367 if (test_bit(I2C_HID_STARTED, &ihid->flags))
368 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
369 ret_size - 2, 1);
370
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100371 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100372}
373
374static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
375{
376 struct i2c_hid *ihid = dev_id;
377
378 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
379 return IRQ_HANDLED;
380
381 i2c_hid_get_input(ihid);
382
383 return IRQ_HANDLED;
384}
385
386static int i2c_hid_get_report_length(struct hid_report *report)
387{
388 return ((report->size - 1) >> 3) + 1 +
389 report->device->report_enum[report->type].numbered + 2;
390}
391
392static void i2c_hid_init_report(struct hid_report *report, u8 *buffer,
393 size_t bufsize)
394{
395 struct hid_device *hid = report->device;
396 struct i2c_client *client = hid->driver_data;
397 struct i2c_hid *ihid = i2c_get_clientdata(client);
398 unsigned int size, ret_size;
399
400 size = i2c_hid_get_report_length(report);
Benjamin Tissoiresc737bcf2012-12-04 16:27:50 +0100401 if (i2c_hid_get_report(client,
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100402 report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
Benjamin Tissoiresc737bcf2012-12-04 16:27:50 +0100403 report->id, buffer, size))
404 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100405
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 */
425static 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
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100432 if (!inbuf) {
433 dev_err(&client->dev, "can not retrieve initial reports\n");
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100434 return;
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100435 }
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100436
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 */
451static 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
466static 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
500static 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
510static 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
538static 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
552static 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
607static 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
636static 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
646static 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
663static 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
680static 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
699static 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
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100719 return hid_set_field(field, offset, value);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100720}
721
722static 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
732static 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) {
Benjamin Tissoires9972dcc2012-12-04 16:27:49 +0100743 dev_warn(&client->dev,
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100744 "Could not register for %s interrupt, irq = %d,"
745 " ret = %d\n",
Benjamin Tissoires9972dcc2012-12-04 16:27:49 +0100746 client->name, client->irq, ret);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100747
748 return ret;
749 }
750
751 ihid->irq = client->irq;
752
753 return 0;
754}
755
756static 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) {
Benjamin Tissoires9972dcc2012-12-04 16:27:49 +0100772 dev_err(&client->dev,
773 "unable to fetch the size of HID descriptor (ret=%d)\n",
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100774 ret);
775 return -ENODEV;
776 }
777
778 dsize = le16_to_cpu(hdesc->wHIDDescLength);
779 if (!dsize || dsize > HID_MAX_DESCRIPTOR_SIZE) {
780 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
781 dsize);
782 return -ENODEV;
783 }
784
785 /* check bcdVersion == 1.0 */
786 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
787 dev_err(&client->dev,
Benjamin Tissoires9972dcc2012-12-04 16:27:49 +0100788 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100789 le16_to_cpu(hdesc->bcdVersion));
790 return -ENODEV;
791 }
792
793 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
794
795 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
796 dsize);
797 if (ret) {
798 dev_err(&client->dev, "hid_descr_cmd Fail\n");
799 return -ENODEV;
800 }
801
802 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
803
804 return 0;
805}
806
807static int __devinit i2c_hid_probe(struct i2c_client *client,
808 const struct i2c_device_id *dev_id)
809{
810 int ret;
811 struct i2c_hid *ihid;
812 struct hid_device *hid;
813 __u16 hidRegister;
814 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
815
816 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
817
818 if (!platform_data) {
819 dev_err(&client->dev, "HID register address not provided\n");
820 return -EINVAL;
821 }
822
823 if (!client->irq) {
824 dev_err(&client->dev,
825 "HID over i2c has not been provided an Int IRQ\n");
826 return -EINVAL;
827 }
828
829 ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
830 if (!ihid)
831 return -ENOMEM;
832
833 i2c_set_clientdata(client, ihid);
834
835 ihid->client = client;
836
837 hidRegister = platform_data->hid_descriptor_address;
838 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
839
840 init_waitqueue_head(&ihid->wait);
841
842 /* we need to allocate the command buffer without knowing the maximum
843 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
844 * real computation later. */
845 ihid->bufsize = HID_MIN_BUFFER_SIZE;
846 i2c_hid_alloc_buffers(ihid);
847
848 ret = i2c_hid_fetch_hid_descriptor(ihid);
849 if (ret < 0)
850 goto err;
851
852 ret = i2c_hid_init_irq(client);
853 if (ret < 0)
854 goto err;
855
856 hid = hid_allocate_device();
857 if (IS_ERR(hid)) {
858 ret = PTR_ERR(hid);
859 goto err;
860 }
861
862 ihid->hid = hid;
863
864 hid->driver_data = client;
865 hid->ll_driver = &i2c_hid_ll_driver;
866 hid->hid_get_raw_report = i2c_hid_get_raw_report;
867 hid->hid_output_raw_report = i2c_hid_output_raw_report;
868 hid->dev.parent = &client->dev;
869 hid->bus = BUS_I2C;
870 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
871 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
872 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
873
Benjamin Tissoires9972dcc2012-12-04 16:27:49 +0100874 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100875 client->name, hid->vendor, hid->product);
876
877 ret = hid_add_device(hid);
878 if (ret) {
879 if (ret != -ENODEV)
880 hid_err(client, "can't add hid device: %d\n", ret);
881 goto err_mem_free;
882 }
883
884 return 0;
885
886err_mem_free:
887 hid_destroy_device(hid);
888
889err:
890 if (ihid->irq)
891 free_irq(ihid->irq, ihid);
892
Jiri Kosina3c626022012-11-20 17:09:40 +0100893 i2c_hid_free_buffers(ihid);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100894 kfree(ihid);
895 return ret;
896}
897
898static int __devexit i2c_hid_remove(struct i2c_client *client)
899{
900 struct i2c_hid *ihid = i2c_get_clientdata(client);
901 struct hid_device *hid;
902
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100903 hid = ihid->hid;
904 hid_destroy_device(hid);
905
906 free_irq(client->irq, ihid);
907
Benjamin Tissoires134ebfd2012-12-04 16:27:54 +0100908 if (ihid->bufsize)
909 i2c_hid_free_buffers(ihid);
910
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100911 kfree(ihid);
912
913 return 0;
914}
915
916#ifdef CONFIG_PM_SLEEP
917static int i2c_hid_suspend(struct device *dev)
918{
919 struct i2c_client *client = to_i2c_client(dev);
920 struct i2c_hid *ihid = i2c_get_clientdata(client);
921
922 if (device_may_wakeup(&client->dev))
923 enable_irq_wake(ihid->irq);
924
925 /* Save some power */
926 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
927
928 return 0;
929}
930
931static int i2c_hid_resume(struct device *dev)
932{
933 int ret;
934 struct i2c_client *client = to_i2c_client(dev);
935
936 ret = i2c_hid_hwreset(client);
937 if (ret)
938 return ret;
939
940 if (device_may_wakeup(&client->dev))
941 disable_irq_wake(client->irq);
942
943 return 0;
944}
945#endif
946
947static SIMPLE_DEV_PM_OPS(i2c_hid_pm, i2c_hid_suspend, i2c_hid_resume);
948
949static const struct i2c_device_id i2c_hid_id_table[] = {
Benjamin Tissoires24ebb372012-12-04 16:27:42 +0100950 { "hid", 0 },
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100951 { },
952};
953MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
954
955
956static struct i2c_driver i2c_hid_driver = {
957 .driver = {
958 .name = "i2c_hid",
959 .owner = THIS_MODULE,
960 .pm = &i2c_hid_pm,
961 },
962
963 .probe = i2c_hid_probe,
964 .remove = __devexit_p(i2c_hid_remove),
965
966 .id_table = i2c_hid_id_table,
967};
968
969module_i2c_driver(i2c_hid_driver);
970
971MODULE_DESCRIPTION("HID over I2C core driver");
972MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
973MODULE_LICENSE("GPL");