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