blob: 5774ebf4298c7ba1d4f8ddbbbe42cbb9d857f7be [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>
Benjamin Tissoires7a7d6d92012-12-12 18:00:02 +010036#include <linux/mutex.h>
Benjamin Tissoires4a200c32012-11-12 15:42:59 +010037
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 */
Benjamin Tissoiresee8e8802012-12-04 16:27:45 +010049static bool debug;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +010050module_param(debug, bool, 0444);
51MODULE_PARM_DESC(debug, "print a lot of debug information");
52
Benjamin Tissoiresfa7386442012-12-04 16:27:46 +010053#define i2c_hid_dbg(ihid, fmt, arg...) \
54do { \
55 if (debug) \
56 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
57} while (0)
Benjamin Tissoires4a200c32012-11-12 15:42:59 +010058
59struct 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;
Benjamin Tissoires27174cf2012-12-05 15:02:53 +010073 __le32 reserved;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +010074} __packed;
75
76struct i2c_hid_cmd {
77 unsigned int registerIndex;
78 __u8 opcode;
79 unsigned int length;
80 bool wait;
81};
82
83union 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 */
97static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
98/* fetch report descriptors */
99static 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 */
105static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
106 .wait = true };
107static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
108static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100109static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
Benjamin Tissoires6bf6c8b2012-12-04 16:27:47 +0100110
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 */
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100120
Benjamin Tissoires7a7d6d92012-12-12 18:00:02 +0100121static DEFINE_MUTEX(i2c_hid_open_mut);
122
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100123/* The main device structure */
124struct 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
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100141 wait_queue_head_t wait; /* For waiting the interrupt */
142};
143
144static 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
216static 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
224static 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");
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100248 return ret;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100249 }
250
251 return 0;
252}
253
254static 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");
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100293 return ret;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100294 }
295
296 return data_len;
297}
298
299static 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
314static 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
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100337static void i2c_hid_get_input(struct i2c_hid *ihid)
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100338{
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)
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100345 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100346
347 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
348 __func__, ret, size);
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100349 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100350 }
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);
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100358 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100359 }
360
361 if (ret_size > size) {
362 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
363 __func__, size, ret_size);
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100364 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100365 }
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
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100373 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100374}
375
376static 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
388static 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
394static 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);
Benjamin Tissoiresc737bcf2012-12-04 16:27:50 +0100403 if (i2c_hid_get_report(client,
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100404 report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
Benjamin Tissoiresc737bcf2012-12-04 16:27:50 +0100405 report->id, buffer, size))
406 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100407
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 */
427static 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
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100434 if (!inbuf) {
435 dev_err(&client->dev, "can not retrieve initial reports\n");
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100436 return;
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100437 }
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100438
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 */
453static 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
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100468static 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;
Benjamin Tissoires29b45782012-12-05 15:02:54 +0100476 ihid->bufsize = 0;
477}
478
479static 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;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100500}
501
502static 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);
Benjamin Tissoirese5b50fe2012-12-05 15:02:56 +0100508 size_t ret_count, ask_count;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100509 int ret;
510
511 if (report_type == HID_OUTPUT_REPORT)
512 return -EINVAL;
513
Benjamin Tissoirese5b50fe2012-12-05 15:02:56 +0100514 /* +2 bytes to include the size of the reply in the query buffer */
515 ask_count = min(count + 2, (size_t)ihid->bufsize);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100516
517 ret = i2c_hid_get_report(client,
518 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
Benjamin Tissoirese5b50fe2012-12-05 15:02:56 +0100519 report_number, ihid->inbuf, ask_count);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100520
521 if (ret < 0)
522 return ret;
523
Benjamin Tissoirese5b50fe2012-12-05 15:02:56 +0100524 ret_count = ihid->inbuf[0] | (ihid->inbuf[1] << 8);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100525
Jiri Kosina9afd09a2012-12-06 10:59:28 +0100526 if (ret_count <= 2)
Benjamin Tissoirese5b50fe2012-12-05 15:02:56 +0100527 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);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100533 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];
Benjamin Tissoiresc2849792013-01-31 17:50:02 +0100543 int ret;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100544
545 if (report_type == HID_INPUT_REPORT)
546 return -EINVAL;
547
Benjamin Tissoiresc2849792013-01-31 17:50:02 +0100548 if (report_id) {
549 buf++;
550 count--;
551 }
552
553 ret = i2c_hid_set_report(client,
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100554 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
555 report_id, buf, count);
Benjamin Tissoiresc2849792013-01-31 17:50:02 +0100556
557 if (report_id && ret >= 0)
558 ret++; /* add report_id to the number of transfered bytes */
559
560 return ret;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100561}
562
563static 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
618static 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;
Benjamin Tissoires29b45782012-12-05 15:02:54 +0100623 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100624
Benjamin Tissoires29b45782012-12-05 15:02:54 +0100625 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);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100628
Benjamin Tissoires29b45782012-12-05 15:02:54 +0100629 if (bufsize > ihid->bufsize) {
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100630 i2c_hid_free_buffers(ihid);
631
Benjamin Tissoires29b45782012-12-05 15:02:54 +0100632 ret = i2c_hid_alloc_buffers(ihid, bufsize);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100633
Benjamin Tissoires29b45782012-12-05 15:02:54 +0100634 if (ret)
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100635 return ret;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100636 }
637
638 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
639 i2c_hid_init_reports(hid);
640
641 return 0;
642}
643
644static 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
654static 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);
Benjamin Tissoires7a7d6d92012-12-12 18:00:02 +0100658 int ret = 0;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100659
Benjamin Tissoires7a7d6d92012-12-12 18:00:02 +0100660 mutex_lock(&i2c_hid_open_mut);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100661 if (!hid->open++) {
662 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
663 if (ret) {
664 hid->open--;
Benjamin Tissoires7a7d6d92012-12-12 18:00:02 +0100665 goto done;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100666 }
667 set_bit(I2C_HID_STARTED, &ihid->flags);
668 }
Benjamin Tissoires7a7d6d92012-12-12 18:00:02 +0100669done:
670 mutex_unlock(&i2c_hid_open_mut);
671 return ret;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100672}
673
674static 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 */
Benjamin Tissoires7a7d6d92012-12-12 18:00:02 +0100683 mutex_lock(&i2c_hid_open_mut);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100684 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 }
Benjamin Tissoires7a7d6d92012-12-12 18:00:02 +0100690 mutex_unlock(&i2c_hid_open_mut);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100691}
692
693static 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
712static 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
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100732 return hid_set_field(field, offset, value);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100733}
734
735static 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
745static int __devinit 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) {
Benjamin Tissoires9972dcc2012-12-04 16:27:49 +0100756 dev_warn(&client->dev,
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100757 "Could not register for %s interrupt, irq = %d,"
758 " ret = %d\n",
Benjamin Tissoires9972dcc2012-12-04 16:27:49 +0100759 client->name, client->irq, ret);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100760
761 return ret;
762 }
763
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100764 return 0;
765}
766
767static int __devinit 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) {
Benjamin Tissoires9972dcc2012-12-04 16:27:49 +0100783 dev_err(&client->dev,
784 "unable to fetch the size of HID descriptor (ret=%d)\n",
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100785 ret);
786 return -ENODEV;
787 }
788
789 dsize = le16_to_cpu(hdesc->wHIDDescLength);
Benjamin Tissoires27174cf2012-12-05 15:02:53 +0100790 /*
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)) {
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100797 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,
Benjamin Tissoires9972dcc2012-12-04 16:27:49 +0100805 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100806 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
824static int __devinit 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. */
Benjamin Tissoires29b45782012-12-05 15:02:54 +0100862 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
863 if (ret < 0)
864 goto err;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100865
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);
Benjamin Tissoires8a1bbb52012-12-05 15:02:55 +0100877 goto err_irq;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100878 }
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
Benjamin Tissoires9972dcc2012-12-04 16:27:49 +0100892 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100893 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
904err_mem_free:
905 hid_destroy_device(hid);
906
Benjamin Tissoires8a1bbb52012-12-05 15:02:55 +0100907err_irq:
908 free_irq(client->irq, ihid);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100909
Benjamin Tissoires8a1bbb52012-12-05 15:02:55 +0100910err:
Jiri Kosina3c626022012-11-20 17:09:40 +0100911 i2c_hid_free_buffers(ihid);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100912 kfree(ihid);
913 return ret;
914}
915
916static int __devexit i2c_hid_remove(struct i2c_client *client)
917{
918 struct i2c_hid *ihid = i2c_get_clientdata(client);
919 struct hid_device *hid;
920
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100921 hid = ihid->hid;
922 hid_destroy_device(hid);
923
924 free_irq(client->irq, ihid);
925
Benjamin Tissoires134ebfd2012-12-04 16:27:54 +0100926 if (ihid->bufsize)
927 i2c_hid_free_buffers(ihid);
928
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100929 kfree(ihid);
930
931 return 0;
932}
933
934#ifdef CONFIG_PM_SLEEP
935static int i2c_hid_suspend(struct device *dev)
936{
937 struct i2c_client *client = to_i2c_client(dev);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100938
939 if (device_may_wakeup(&client->dev))
Benjamin Tissoires8a1bbb52012-12-05 15:02:55 +0100940 enable_irq_wake(client->irq);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100941
942 /* Save some power */
943 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
944
945 return 0;
946}
947
948static 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
964static SIMPLE_DEV_PM_OPS(i2c_hid_pm, i2c_hid_suspend, i2c_hid_resume);
965
966static const struct i2c_device_id i2c_hid_id_table[] = {
Benjamin Tissoires24ebb372012-12-04 16:27:42 +0100967 { "hid", 0 },
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100968 { },
969};
970MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
971
972
973static 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 = __devexit_p(i2c_hid_remove),
982
983 .id_table = i2c_hid_id_table,
984};
985
986module_i2c_driver(i2c_hid_driver);
987
988MODULE_DESCRIPTION("HID over I2C core driver");
989MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
990MODULE_LICENSE("GPL");