]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hid/i2c-hid/i2c-hid.c
HID: usbhid: quirk for Formosa IR receiver
[mirror_ubuntu-bionic-kernel.git] / drivers / hid / i2c-hid / i2c-hid.c
CommitLineData
4a200c3b
BT
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>
4a200c3b 35#include <linux/hid.h>
7a7d6d9c 36#include <linux/mutex.h>
4a200c3b
BT
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 */
ee8e8806 49static bool debug;
4a200c3b
BT
50module_param(debug, bool, 0444);
51MODULE_PARM_DESC(debug, "print a lot of debug information");
52
fa738644
BT
53#define i2c_hid_dbg(ihid, fmt, arg...) \
54do { \
55 if (debug) \
56 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
57} while (0)
4a200c3b
BT
58
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;
27174cff 73 __le32 reserved;
4a200c3b
BT
74} __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) };
4a200c3b 109static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
6bf6c8bf
BT
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 */
4a200c3b 120
7a7d6d9c
BT
121static DEFINE_MUTEX(i2c_hid_open_mut);
122
4a200c3b
BT
123/* 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
4a200c3b
BT
141 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");
317b204a 248 return ret;
4a200c3b
BT
249 }
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");
317b204a 293 return ret;
4a200c3b
BT
294 }
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
317b204a 337static void i2c_hid_get_input(struct i2c_hid *ihid)
4a200c3b
BT
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)
317b204a 345 return;
4a200c3b
BT
346
347 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
348 __func__, ret, size);
317b204a 349 return;
4a200c3b
BT
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);
317b204a 358 return;
4a200c3b
BT
359 }
360
361 if (ret_size > size) {
362 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
363 __func__, size, ret_size);
317b204a 364 return;
4a200c3b
BT
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
317b204a 373 return;
4a200c3b
BT
374}
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);
c737bcf9 403 if (i2c_hid_get_report(client,
4a200c3b 404 report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
c737bcf9
BT
405 report->id, buffer, size))
406 return;
4a200c3b
BT
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 */
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
317b204a
BT
434 if (!inbuf) {
435 dev_err(&client->dev, "can not retrieve initial reports\n");
4a200c3b 436 return;
317b204a 437 }
4a200c3b
BT
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 */
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
29b45787
BT
468static 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
479static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
4a200c3b
BT
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 */
29b45787 486 report_size; /* report */
4a200c3b 487
29b45787 488 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
4a200c3b 489 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
4a200c3b
BT
490 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
491
29b45787
BT
492 if (!ihid->inbuf || !ihid->argsbuf || !ihid->cmdbuf) {
493 i2c_hid_free_buffers(ihid);
4a200c3b
BT
494 return -ENOMEM;
495 }
496
29b45787 497 ihid->bufsize = report_size;
4a200c3b 498
29b45787 499 return 0;
4a200c3b
BT
500}
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);
e5b50fe7 508 size_t ret_count, ask_count;
4a200c3b
BT
509 int ret;
510
511 if (report_type == HID_OUTPUT_REPORT)
512 return -EINVAL;
513
e5b50fe7
BT
514 /* +2 bytes to include the size of the reply in the query buffer */
515 ask_count = min(count + 2, (size_t)ihid->bufsize);
4a200c3b
BT
516
517 ret = i2c_hid_get_report(client,
518 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
e5b50fe7 519 report_number, ihid->inbuf, ask_count);
4a200c3b
BT
520
521 if (ret < 0)
522 return ret;
523
e5b50fe7 524 ret_count = ihid->inbuf[0] | (ihid->inbuf[1] << 8);
4a200c3b 525
9afd09a1 526 if (ret_count <= 2)
e5b50fe7
BT
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);
4a200c3b
BT
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;
29b45787 612 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
4a200c3b 613
29b45787
BT
614 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
615 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
616 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
4a200c3b 617
29b45787 618 if (bufsize > ihid->bufsize) {
4a200c3b
BT
619 i2c_hid_free_buffers(ihid);
620
29b45787 621 ret = i2c_hid_alloc_buffers(ihid, bufsize);
4a200c3b 622
29b45787 623 if (ret)
4a200c3b 624 return ret;
4a200c3b
BT
625 }
626
627 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
628 i2c_hid_init_reports(hid);
629
630 return 0;
631}
632
633static void i2c_hid_stop(struct hid_device *hid)
634{
635 struct i2c_client *client = hid->driver_data;
636 struct i2c_hid *ihid = i2c_get_clientdata(client);
637
638 hid->claimed = 0;
639
640 i2c_hid_free_buffers(ihid);
641}
642
643static int i2c_hid_open(struct hid_device *hid)
644{
645 struct i2c_client *client = hid->driver_data;
646 struct i2c_hid *ihid = i2c_get_clientdata(client);
7a7d6d9c 647 int ret = 0;
4a200c3b 648
7a7d6d9c 649 mutex_lock(&i2c_hid_open_mut);
4a200c3b
BT
650 if (!hid->open++) {
651 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
652 if (ret) {
653 hid->open--;
7a7d6d9c 654 goto done;
4a200c3b
BT
655 }
656 set_bit(I2C_HID_STARTED, &ihid->flags);
657 }
7a7d6d9c
BT
658done:
659 mutex_unlock(&i2c_hid_open_mut);
660 return ret;
4a200c3b
BT
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 */
7a7d6d9c 672 mutex_lock(&i2c_hid_open_mut);
4a200c3b
BT
673 if (!--hid->open) {
674 clear_bit(I2C_HID_STARTED, &ihid->flags);
675
676 /* Save some power */
677 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
678 }
7a7d6d9c 679 mutex_unlock(&i2c_hid_open_mut);
4a200c3b
BT
680}
681
682static int i2c_hid_power(struct hid_device *hid, int lvl)
683{
684 struct i2c_client *client = hid->driver_data;
685 struct i2c_hid *ihid = i2c_get_clientdata(client);
686 int ret = 0;
687
688 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
689
690 switch (lvl) {
691 case PM_HINT_FULLON:
692 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
693 break;
694 case PM_HINT_NORMAL:
695 ret = i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
696 break;
697 }
698 return ret;
699}
700
701static int i2c_hid_hidinput_input_event(struct input_dev *dev,
702 unsigned int type, unsigned int code, int value)
703{
704 struct hid_device *hid = input_get_drvdata(dev);
705 struct hid_field *field;
706 int offset;
707
708 if (type == EV_FF)
709 return input_ff_event(dev, type, code, value);
710
711 if (type != EV_LED)
712 return -1;
713
714 offset = hidinput_find_field(hid, type, code, &field);
715
716 if (offset == -1) {
717 hid_warn(dev, "event field not found\n");
718 return -1;
719 }
720
317b204a 721 return hid_set_field(field, offset, value);
4a200c3b
BT
722}
723
724static struct hid_ll_driver i2c_hid_ll_driver = {
725 .parse = i2c_hid_parse,
726 .start = i2c_hid_start,
727 .stop = i2c_hid_stop,
728 .open = i2c_hid_open,
729 .close = i2c_hid_close,
730 .power = i2c_hid_power,
731 .hidinput_input_event = i2c_hid_hidinput_input_event,
732};
733
734static int __devinit i2c_hid_init_irq(struct i2c_client *client)
735{
736 struct i2c_hid *ihid = i2c_get_clientdata(client);
737 int ret;
738
739 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
740
741 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
742 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
743 client->name, ihid);
744 if (ret < 0) {
9972dcc2 745 dev_warn(&client->dev,
4a200c3b
BT
746 "Could not register for %s interrupt, irq = %d,"
747 " ret = %d\n",
9972dcc2 748 client->name, client->irq, ret);
4a200c3b
BT
749
750 return ret;
751 }
752
4a200c3b
BT
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) {
9972dcc2
BT
772 dev_err(&client->dev,
773 "unable to fetch the size of HID descriptor (ret=%d)\n",
4a200c3b
BT
774 ret);
775 return -ENODEV;
776 }
777
778 dsize = le16_to_cpu(hdesc->wHIDDescLength);
27174cff
BT
779 /*
780 * the size of the HID descriptor should at least contain
781 * its size and the bcdVersion (4 bytes), and should not be greater
782 * than sizeof(struct i2c_hid_desc) as we directly fill this struct
783 * through i2c_hid_command.
784 */
785 if (dsize < 4 || dsize > sizeof(struct i2c_hid_desc)) {
4a200c3b
BT
786 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
787 dsize);
788 return -ENODEV;
789 }
790
791 /* check bcdVersion == 1.0 */
792 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
793 dev_err(&client->dev,
9972dcc2 794 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
4a200c3b
BT
795 le16_to_cpu(hdesc->bcdVersion));
796 return -ENODEV;
797 }
798
799 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
800
801 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
802 dsize);
803 if (ret) {
804 dev_err(&client->dev, "hid_descr_cmd Fail\n");
805 return -ENODEV;
806 }
807
808 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
809
810 return 0;
811}
812
813static int __devinit i2c_hid_probe(struct i2c_client *client,
814 const struct i2c_device_id *dev_id)
815{
816 int ret;
817 struct i2c_hid *ihid;
818 struct hid_device *hid;
819 __u16 hidRegister;
820 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
821
822 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
823
824 if (!platform_data) {
825 dev_err(&client->dev, "HID register address not provided\n");
826 return -EINVAL;
827 }
828
829 if (!client->irq) {
830 dev_err(&client->dev,
831 "HID over i2c has not been provided an Int IRQ\n");
832 return -EINVAL;
833 }
834
835 ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
836 if (!ihid)
837 return -ENOMEM;
838
839 i2c_set_clientdata(client, ihid);
840
841 ihid->client = client;
842
843 hidRegister = platform_data->hid_descriptor_address;
844 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
845
846 init_waitqueue_head(&ihid->wait);
847
848 /* we need to allocate the command buffer without knowing the maximum
849 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
850 * real computation later. */
29b45787
BT
851 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
852 if (ret < 0)
853 goto err;
4a200c3b
BT
854
855 ret = i2c_hid_fetch_hid_descriptor(ihid);
856 if (ret < 0)
857 goto err;
858
859 ret = i2c_hid_init_irq(client);
860 if (ret < 0)
861 goto err;
862
863 hid = hid_allocate_device();
864 if (IS_ERR(hid)) {
865 ret = PTR_ERR(hid);
8a1bbb53 866 goto err_irq;
4a200c3b
BT
867 }
868
869 ihid->hid = hid;
870
871 hid->driver_data = client;
872 hid->ll_driver = &i2c_hid_ll_driver;
873 hid->hid_get_raw_report = i2c_hid_get_raw_report;
874 hid->hid_output_raw_report = i2c_hid_output_raw_report;
875 hid->dev.parent = &client->dev;
876 hid->bus = BUS_I2C;
877 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
878 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
879 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
880
9972dcc2 881 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
4a200c3b
BT
882 client->name, hid->vendor, hid->product);
883
884 ret = hid_add_device(hid);
885 if (ret) {
886 if (ret != -ENODEV)
887 hid_err(client, "can't add hid device: %d\n", ret);
888 goto err_mem_free;
889 }
890
891 return 0;
892
893err_mem_free:
894 hid_destroy_device(hid);
895
8a1bbb53
BT
896err_irq:
897 free_irq(client->irq, ihid);
4a200c3b 898
8a1bbb53 899err:
3c626024 900 i2c_hid_free_buffers(ihid);
4a200c3b
BT
901 kfree(ihid);
902 return ret;
903}
904
905static int __devexit i2c_hid_remove(struct i2c_client *client)
906{
907 struct i2c_hid *ihid = i2c_get_clientdata(client);
908 struct hid_device *hid;
909
4a200c3b
BT
910 hid = ihid->hid;
911 hid_destroy_device(hid);
912
913 free_irq(client->irq, ihid);
914
134ebfd8
BT
915 if (ihid->bufsize)
916 i2c_hid_free_buffers(ihid);
917
4a200c3b
BT
918 kfree(ihid);
919
920 return 0;
921}
922
923#ifdef CONFIG_PM_SLEEP
924static int i2c_hid_suspend(struct device *dev)
925{
926 struct i2c_client *client = to_i2c_client(dev);
4a200c3b
BT
927
928 if (device_may_wakeup(&client->dev))
8a1bbb53 929 enable_irq_wake(client->irq);
4a200c3b
BT
930
931 /* Save some power */
932 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
933
934 return 0;
935}
936
937static int i2c_hid_resume(struct device *dev)
938{
939 int ret;
940 struct i2c_client *client = to_i2c_client(dev);
941
942 ret = i2c_hid_hwreset(client);
943 if (ret)
944 return ret;
945
946 if (device_may_wakeup(&client->dev))
947 disable_irq_wake(client->irq);
948
949 return 0;
950}
951#endif
952
953static SIMPLE_DEV_PM_OPS(i2c_hid_pm, i2c_hid_suspend, i2c_hid_resume);
954
955static const struct i2c_device_id i2c_hid_id_table[] = {
24ebb37e 956 { "hid", 0 },
4a200c3b
BT
957 { },
958};
959MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
960
961
962static struct i2c_driver i2c_hid_driver = {
963 .driver = {
964 .name = "i2c_hid",
965 .owner = THIS_MODULE,
966 .pm = &i2c_hid_pm,
967 },
968
969 .probe = i2c_hid_probe,
970 .remove = __devexit_p(i2c_hid_remove),
971
972 .id_table = i2c_hid_id_table,
973};
974
975module_i2c_driver(i2c_hid_driver);
976
977MODULE_DESCRIPTION("HID over I2C core driver");
978MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
979MODULE_LICENSE("GPL");