mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / hid / i2c-hid / i2c-hid.c
blob0c65412cf5d4a3b0d68964dc20befc20153e2c81
1 /*
2 * HID over I2C protocol implementation
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
8 * This code is partly based on "USB HID support for Linux":
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
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.
21 #include <linux/module.h>
22 #include <linux/i2c.h>
23 #include <linux/interrupt.h>
24 #include <linux/input.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <linux/pm.h>
28 #include <linux/device.h>
29 #include <linux/wait.h>
30 #include <linux/err.h>
31 #include <linux/string.h>
32 #include <linux/list.h>
33 #include <linux/jiffies.h>
34 #include <linux/kernel.h>
35 #include <linux/hid.h>
36 #include <linux/mutex.h>
37 #include <linux/acpi.h>
38 #include <linux/of.h>
40 #include <linux/i2c/i2c-hid.h>
42 /* flags */
43 #define I2C_HID_STARTED (1 << 0)
44 #define I2C_HID_RESET_PENDING (1 << 1)
45 #define I2C_HID_READ_PENDING (1 << 2)
47 #define I2C_HID_PWR_ON 0x00
48 #define I2C_HID_PWR_SLEEP 0x01
50 /* debug option */
51 static bool debug;
52 module_param(debug, bool, 0444);
53 MODULE_PARM_DESC(debug, "print a lot of debug information");
55 #define i2c_hid_dbg(ihid, fmt, arg...) \
56 do { \
57 if (debug) \
58 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
59 } while (0)
61 struct i2c_hid_desc {
62 __le16 wHIDDescLength;
63 __le16 bcdVersion;
64 __le16 wReportDescLength;
65 __le16 wReportDescRegister;
66 __le16 wInputRegister;
67 __le16 wMaxInputLength;
68 __le16 wOutputRegister;
69 __le16 wMaxOutputLength;
70 __le16 wCommandRegister;
71 __le16 wDataRegister;
72 __le16 wVendorID;
73 __le16 wProductID;
74 __le16 wVersionID;
75 __le32 reserved;
76 } __packed;
78 struct i2c_hid_cmd {
79 unsigned int registerIndex;
80 __u8 opcode;
81 unsigned int length;
82 bool wait;
85 union command {
86 u8 data[0];
87 struct cmd {
88 __le16 reg;
89 __u8 reportTypeID;
90 __u8 opcode;
91 } __packed c;
94 #define I2C_HID_CMD(opcode_) \
95 .opcode = opcode_, .length = 4, \
96 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
98 /* fetch HID descriptor */
99 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
100 /* fetch report descriptors */
101 static const struct i2c_hid_cmd hid_report_descr_cmd = {
102 .registerIndex = offsetof(struct i2c_hid_desc,
103 wReportDescRegister),
104 .opcode = 0x00,
105 .length = 2 };
106 /* commands */
107 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
108 .wait = true };
109 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
110 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
111 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
112 static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
115 * These definitions are not used here, but are defined by the spec.
116 * Keeping them here for documentation purposes.
118 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
119 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
120 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
121 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
124 static DEFINE_MUTEX(i2c_hid_open_mut);
126 /* The main device structure */
127 struct i2c_hid {
128 struct i2c_client *client; /* i2c client */
129 struct hid_device *hid; /* pointer to corresponding HID dev */
130 union {
131 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
132 struct i2c_hid_desc hdesc; /* the HID Descriptor */
134 __le16 wHIDDescRegister; /* location of the i2c
135 * register of the HID
136 * descriptor. */
137 unsigned int bufsize; /* i2c buffer size */
138 char *inbuf; /* Input buffer */
139 char *rawbuf; /* Raw Input buffer */
140 char *cmdbuf; /* Command buffer */
141 char *argsbuf; /* Command arguments buffer */
143 unsigned long flags; /* device flags */
145 wait_queue_head_t wait; /* For waiting the interrupt */
147 struct i2c_hid_platform_data pdata;
150 static int __i2c_hid_command(struct i2c_client *client,
151 const struct i2c_hid_cmd *command, u8 reportID,
152 u8 reportType, u8 *args, int args_len,
153 unsigned char *buf_recv, int data_len)
155 struct i2c_hid *ihid = i2c_get_clientdata(client);
156 union command *cmd = (union command *)ihid->cmdbuf;
157 int ret;
158 struct i2c_msg msg[2];
159 int msg_num = 1;
161 int length = command->length;
162 bool wait = command->wait;
163 unsigned int registerIndex = command->registerIndex;
165 /* special case for hid_descr_cmd */
166 if (command == &hid_descr_cmd) {
167 cmd->c.reg = ihid->wHIDDescRegister;
168 } else {
169 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
170 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
173 if (length > 2) {
174 cmd->c.opcode = command->opcode;
175 cmd->c.reportTypeID = reportID | reportType << 4;
178 memcpy(cmd->data + length, args, args_len);
179 length += args_len;
181 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
183 msg[0].addr = client->addr;
184 msg[0].flags = client->flags & I2C_M_TEN;
185 msg[0].len = length;
186 msg[0].buf = cmd->data;
187 if (data_len > 0) {
188 msg[1].addr = client->addr;
189 msg[1].flags = client->flags & I2C_M_TEN;
190 msg[1].flags |= I2C_M_RD;
191 msg[1].len = data_len;
192 msg[1].buf = buf_recv;
193 msg_num = 2;
194 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
197 if (wait)
198 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
200 ret = i2c_transfer(client->adapter, msg, msg_num);
202 if (data_len > 0)
203 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
205 if (ret != msg_num)
206 return ret < 0 ? ret : -EIO;
208 ret = 0;
210 if (wait) {
211 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
212 if (!wait_event_timeout(ihid->wait,
213 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
214 msecs_to_jiffies(5000)))
215 ret = -ENODATA;
216 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
219 return ret;
222 static int i2c_hid_command(struct i2c_client *client,
223 const struct i2c_hid_cmd *command,
224 unsigned char *buf_recv, int data_len)
226 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
227 buf_recv, data_len);
230 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
231 u8 reportID, unsigned char *buf_recv, int data_len)
233 struct i2c_hid *ihid = i2c_get_clientdata(client);
234 u8 args[3];
235 int ret;
236 int args_len = 0;
237 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
239 i2c_hid_dbg(ihid, "%s\n", __func__);
241 if (reportID >= 0x0F) {
242 args[args_len++] = reportID;
243 reportID = 0x0F;
246 args[args_len++] = readRegister & 0xFF;
247 args[args_len++] = readRegister >> 8;
249 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
250 reportType, args, args_len, buf_recv, data_len);
251 if (ret) {
252 dev_err(&client->dev,
253 "failed to retrieve report from device.\n");
254 return ret;
257 return 0;
260 static int i2c_hid_set_report(struct i2c_client *client, u8 reportType,
261 u8 reportID, unsigned char *buf, size_t data_len)
263 struct i2c_hid *ihid = i2c_get_clientdata(client);
264 u8 *args = ihid->argsbuf;
265 const struct i2c_hid_cmd * hidcmd = &hid_set_report_cmd;
266 int ret;
267 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
268 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
269 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
271 /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */
272 u16 size = 2 /* size */ +
273 (reportID ? 1 : 0) /* reportID */ +
274 data_len /* buf */;
275 int args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
276 2 /* dataRegister */ +
277 size /* args */;
278 int index = 0;
280 i2c_hid_dbg(ihid, "%s\n", __func__);
282 if (reportID >= 0x0F) {
283 args[index++] = reportID;
284 reportID = 0x0F;
288 * use the data register for feature reports or if the device does not
289 * support the output register
291 if (reportType == 0x03 || maxOutputLength == 0) {
292 args[index++] = dataRegister & 0xFF;
293 args[index++] = dataRegister >> 8;
294 } else {
295 args[index++] = outputRegister & 0xFF;
296 args[index++] = outputRegister >> 8;
297 hidcmd = &hid_no_cmd;
300 args[index++] = size & 0xFF;
301 args[index++] = size >> 8;
303 if (reportID)
304 args[index++] = reportID;
306 memcpy(&args[index], buf, data_len);
308 ret = __i2c_hid_command(client, hidcmd, reportID,
309 reportType, args, args_len, NULL, 0);
310 if (ret) {
311 dev_err(&client->dev, "failed to set a report to device.\n");
312 return ret;
315 return data_len;
318 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
320 struct i2c_hid *ihid = i2c_get_clientdata(client);
321 int ret;
323 i2c_hid_dbg(ihid, "%s\n", __func__);
325 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
326 0, NULL, 0, NULL, 0);
327 if (ret)
328 dev_err(&client->dev, "failed to change power setting.\n");
330 return ret;
333 static int i2c_hid_hwreset(struct i2c_client *client)
335 struct i2c_hid *ihid = i2c_get_clientdata(client);
336 int ret;
338 i2c_hid_dbg(ihid, "%s\n", __func__);
340 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
341 if (ret)
342 return ret;
344 i2c_hid_dbg(ihid, "resetting...\n");
346 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
347 if (ret) {
348 dev_err(&client->dev, "failed to reset device.\n");
349 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
350 return ret;
353 return 0;
356 static void i2c_hid_get_input(struct i2c_hid *ihid)
358 int ret, ret_size;
359 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
361 if (size > ihid->bufsize)
362 size = ihid->bufsize;
364 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
365 if (ret != size) {
366 if (ret < 0)
367 return;
369 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
370 __func__, ret, size);
371 return;
374 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
376 if (!ret_size) {
377 /* host or device initiated RESET completed */
378 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
379 wake_up(&ihid->wait);
380 return;
383 if (ret_size > size) {
384 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
385 __func__, size, ret_size);
386 return;
389 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
391 if (test_bit(I2C_HID_STARTED, &ihid->flags))
392 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
393 ret_size - 2, 1);
395 return;
398 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
400 struct i2c_hid *ihid = dev_id;
402 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
403 return IRQ_HANDLED;
405 i2c_hid_get_input(ihid);
407 return IRQ_HANDLED;
410 static int i2c_hid_get_report_length(struct hid_report *report)
412 return ((report->size - 1) >> 3) + 1 +
413 report->device->report_enum[report->type].numbered + 2;
416 static void i2c_hid_init_report(struct hid_report *report, u8 *buffer,
417 size_t bufsize)
419 struct hid_device *hid = report->device;
420 struct i2c_client *client = hid->driver_data;
421 struct i2c_hid *ihid = i2c_get_clientdata(client);
422 unsigned int size, ret_size;
424 size = i2c_hid_get_report_length(report);
425 if (i2c_hid_get_report(client,
426 report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
427 report->id, buffer, size))
428 return;
430 i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, ihid->inbuf);
432 ret_size = buffer[0] | (buffer[1] << 8);
434 if (ret_size != size) {
435 dev_err(&client->dev, "error in %s size:%d / ret_size:%d\n",
436 __func__, size, ret_size);
437 return;
440 /* hid->driver_lock is held as we are in probe function,
441 * we just need to setup the input fields, so using
442 * hid_report_raw_event is safe. */
443 hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1);
447 * Initialize all reports
449 static void i2c_hid_init_reports(struct hid_device *hid)
451 struct hid_report *report;
452 struct i2c_client *client = hid->driver_data;
453 struct i2c_hid *ihid = i2c_get_clientdata(client);
454 u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL);
456 if (!inbuf) {
457 dev_err(&client->dev, "can not retrieve initial reports\n");
458 return;
461 list_for_each_entry(report,
462 &hid->report_enum[HID_INPUT_REPORT].report_list, list)
463 i2c_hid_init_report(report, inbuf, ihid->bufsize);
465 list_for_each_entry(report,
466 &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
467 i2c_hid_init_report(report, inbuf, ihid->bufsize);
469 kfree(inbuf);
473 * Traverse the supplied list of reports and find the longest
475 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
476 unsigned int *max)
478 struct hid_report *report;
479 unsigned int size;
481 /* We should not rely on wMaxInputLength, as some devices may set it to
482 * a wrong length. */
483 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
484 size = i2c_hid_get_report_length(report);
485 if (*max < size)
486 *max = size;
490 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
492 kfree(ihid->inbuf);
493 kfree(ihid->rawbuf);
494 kfree(ihid->argsbuf);
495 kfree(ihid->cmdbuf);
496 ihid->inbuf = NULL;
497 ihid->rawbuf = NULL;
498 ihid->cmdbuf = NULL;
499 ihid->argsbuf = NULL;
500 ihid->bufsize = 0;
503 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
505 /* the worst case is computed from the set_report command with a
506 * reportID > 15 and the maximum report length */
507 int args_len = sizeof(__u8) + /* optional ReportID byte */
508 sizeof(__u16) + /* data register */
509 sizeof(__u16) + /* size of the report */
510 report_size; /* report */
512 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
513 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
514 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
515 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
517 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
518 i2c_hid_free_buffers(ihid);
519 return -ENOMEM;
522 ihid->bufsize = report_size;
524 return 0;
527 static int i2c_hid_get_raw_report(struct hid_device *hid,
528 unsigned char report_number, __u8 *buf, size_t count,
529 unsigned char report_type)
531 struct i2c_client *client = hid->driver_data;
532 struct i2c_hid *ihid = i2c_get_clientdata(client);
533 size_t ret_count, ask_count;
534 int ret;
536 if (report_type == HID_OUTPUT_REPORT)
537 return -EINVAL;
539 /* +2 bytes to include the size of the reply in the query buffer */
540 ask_count = min(count + 2, (size_t)ihid->bufsize);
542 ret = i2c_hid_get_report(client,
543 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
544 report_number, ihid->rawbuf, ask_count);
546 if (ret < 0)
547 return ret;
549 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
551 if (ret_count <= 2)
552 return 0;
554 ret_count = min(ret_count, ask_count);
556 /* The query buffer contains the size, dropping it in the reply */
557 count = min(count, ret_count - 2);
558 memcpy(buf, ihid->rawbuf + 2, count);
560 return count;
563 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
564 size_t count, unsigned char report_type)
566 struct i2c_client *client = hid->driver_data;
567 int report_id = buf[0];
568 int ret;
570 if (report_type == HID_INPUT_REPORT)
571 return -EINVAL;
573 if (report_id) {
574 buf++;
575 count--;
578 ret = i2c_hid_set_report(client,
579 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
580 report_id, buf, count);
582 if (report_id && ret >= 0)
583 ret++; /* add report_id to the number of transfered bytes */
585 return ret;
588 static void i2c_hid_request(struct hid_device *hid, struct hid_report *rep,
589 int reqtype)
591 struct i2c_client *client = hid->driver_data;
592 char *buf;
593 int ret;
594 int len = i2c_hid_get_report_length(rep) - 2;
596 buf = kzalloc(len, GFP_KERNEL);
597 if (!buf)
598 return;
600 switch (reqtype) {
601 case HID_REQ_GET_REPORT:
602 ret = i2c_hid_get_raw_report(hid, rep->id, buf, len, rep->type);
603 if (ret < 0)
604 dev_err(&client->dev, "%s: unable to get report: %d\n",
605 __func__, ret);
606 else
607 hid_input_report(hid, rep->type, buf, ret, 0);
608 break;
609 case HID_REQ_SET_REPORT:
610 hid_output_report(rep, buf);
611 i2c_hid_output_raw_report(hid, buf, len, rep->type);
612 break;
615 kfree(buf);
618 static int i2c_hid_parse(struct hid_device *hid)
620 struct i2c_client *client = hid->driver_data;
621 struct i2c_hid *ihid = i2c_get_clientdata(client);
622 struct i2c_hid_desc *hdesc = &ihid->hdesc;
623 unsigned int rsize;
624 char *rdesc;
625 int ret;
626 int tries = 3;
628 i2c_hid_dbg(ihid, "entering %s\n", __func__);
630 rsize = le16_to_cpu(hdesc->wReportDescLength);
631 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
632 dbg_hid("weird size of report descriptor (%u)\n", rsize);
633 return -EINVAL;
636 do {
637 ret = i2c_hid_hwreset(client);
638 if (ret)
639 msleep(1000);
640 } while (tries-- > 0 && ret);
642 if (ret)
643 return ret;
645 rdesc = kzalloc(rsize, GFP_KERNEL);
647 if (!rdesc) {
648 dbg_hid("couldn't allocate rdesc memory\n");
649 return -ENOMEM;
652 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
654 ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
655 if (ret) {
656 hid_err(hid, "reading report descriptor failed\n");
657 kfree(rdesc);
658 return -EIO;
661 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
663 ret = hid_parse_report(hid, rdesc, rsize);
664 kfree(rdesc);
665 if (ret) {
666 dbg_hid("parsing report descriptor failed\n");
667 return ret;
670 return 0;
673 static int i2c_hid_start(struct hid_device *hid)
675 struct i2c_client *client = hid->driver_data;
676 struct i2c_hid *ihid = i2c_get_clientdata(client);
677 int ret;
678 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
680 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
681 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
682 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
684 if (bufsize > ihid->bufsize) {
685 i2c_hid_free_buffers(ihid);
687 ret = i2c_hid_alloc_buffers(ihid, bufsize);
689 if (ret)
690 return ret;
693 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
694 i2c_hid_init_reports(hid);
696 return 0;
699 static void i2c_hid_stop(struct hid_device *hid)
701 struct i2c_client *client = hid->driver_data;
702 struct i2c_hid *ihid = i2c_get_clientdata(client);
704 hid->claimed = 0;
706 i2c_hid_free_buffers(ihid);
709 static int i2c_hid_open(struct hid_device *hid)
711 struct i2c_client *client = hid->driver_data;
712 struct i2c_hid *ihid = i2c_get_clientdata(client);
713 int ret = 0;
715 mutex_lock(&i2c_hid_open_mut);
716 if (!hid->open++) {
717 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
718 if (ret) {
719 hid->open--;
720 goto done;
722 set_bit(I2C_HID_STARTED, &ihid->flags);
724 done:
725 mutex_unlock(&i2c_hid_open_mut);
726 return ret;
729 static void i2c_hid_close(struct hid_device *hid)
731 struct i2c_client *client = hid->driver_data;
732 struct i2c_hid *ihid = i2c_get_clientdata(client);
734 /* protecting hid->open to make sure we don't restart
735 * data acquistion due to a resumption we no longer
736 * care about
738 mutex_lock(&i2c_hid_open_mut);
739 if (!--hid->open) {
740 clear_bit(I2C_HID_STARTED, &ihid->flags);
742 /* Save some power */
743 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
745 mutex_unlock(&i2c_hid_open_mut);
748 static int i2c_hid_power(struct hid_device *hid, int lvl)
750 struct i2c_client *client = hid->driver_data;
751 struct i2c_hid *ihid = i2c_get_clientdata(client);
752 int ret = 0;
754 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
756 switch (lvl) {
757 case PM_HINT_FULLON:
758 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
759 break;
760 case PM_HINT_NORMAL:
761 ret = i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
762 break;
764 return ret;
767 static struct hid_ll_driver i2c_hid_ll_driver = {
768 .parse = i2c_hid_parse,
769 .start = i2c_hid_start,
770 .stop = i2c_hid_stop,
771 .open = i2c_hid_open,
772 .close = i2c_hid_close,
773 .power = i2c_hid_power,
774 .request = i2c_hid_request,
777 static int i2c_hid_init_irq(struct i2c_client *client)
779 struct i2c_hid *ihid = i2c_get_clientdata(client);
780 int ret;
782 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
784 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
785 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
786 client->name, ihid);
787 if (ret < 0) {
788 dev_warn(&client->dev,
789 "Could not register for %s interrupt, irq = %d,"
790 " ret = %d\n",
791 client->name, client->irq, ret);
793 return ret;
796 return 0;
799 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
801 struct i2c_client *client = ihid->client;
802 struct i2c_hid_desc *hdesc = &ihid->hdesc;
803 unsigned int dsize;
804 int ret;
806 /* Fetch the length of HID description, retrieve the 4 first bytes:
807 * bytes 0-1 -> length
808 * bytes 2-3 -> bcdVersion (has to be 1.00) */
809 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer, 4);
811 i2c_hid_dbg(ihid, "%s, ihid->hdesc_buffer: %4ph\n", __func__,
812 ihid->hdesc_buffer);
814 if (ret) {
815 dev_err(&client->dev,
816 "unable to fetch the size of HID descriptor (ret=%d)\n",
817 ret);
818 return -ENODEV;
821 dsize = le16_to_cpu(hdesc->wHIDDescLength);
823 * the size of the HID descriptor should at least contain
824 * its size and the bcdVersion (4 bytes), and should not be greater
825 * than sizeof(struct i2c_hid_desc) as we directly fill this struct
826 * through i2c_hid_command.
828 if (dsize < 4 || dsize > sizeof(struct i2c_hid_desc)) {
829 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
830 dsize);
831 return -ENODEV;
834 /* check bcdVersion == 1.0 */
835 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
836 dev_err(&client->dev,
837 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
838 le16_to_cpu(hdesc->bcdVersion));
839 return -ENODEV;
842 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
844 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
845 dsize);
846 if (ret) {
847 dev_err(&client->dev, "hid_descr_cmd Fail\n");
848 return -ENODEV;
851 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
853 return 0;
856 #ifdef CONFIG_ACPI
857 static int i2c_hid_acpi_pdata(struct i2c_client *client,
858 struct i2c_hid_platform_data *pdata)
860 static u8 i2c_hid_guid[] = {
861 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
862 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
864 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
865 union acpi_object params[4], *obj;
866 struct acpi_object_list input;
867 struct acpi_device *adev;
868 acpi_handle handle;
870 handle = ACPI_HANDLE(&client->dev);
871 if (!handle || acpi_bus_get_device(handle, &adev))
872 return -ENODEV;
874 input.count = ARRAY_SIZE(params);
875 input.pointer = params;
877 params[0].type = ACPI_TYPE_BUFFER;
878 params[0].buffer.length = sizeof(i2c_hid_guid);
879 params[0].buffer.pointer = i2c_hid_guid;
880 params[1].type = ACPI_TYPE_INTEGER;
881 params[1].integer.value = 1;
882 params[2].type = ACPI_TYPE_INTEGER;
883 params[2].integer.value = 1; /* HID function */
884 params[3].type = ACPI_TYPE_PACKAGE;
885 params[3].package.count = 0;
886 params[3].package.elements = NULL;
888 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_DSM", &input, &buf))) {
889 dev_err(&client->dev, "device _DSM execution failed\n");
890 return -ENODEV;
893 obj = (union acpi_object *)buf.pointer;
894 if (obj->type != ACPI_TYPE_INTEGER) {
895 dev_err(&client->dev, "device _DSM returned invalid type: %d\n",
896 obj->type);
897 kfree(buf.pointer);
898 return -EINVAL;
901 pdata->hid_descriptor_address = obj->integer.value;
903 kfree(buf.pointer);
904 return 0;
907 static const struct acpi_device_id i2c_hid_acpi_match[] = {
908 {"ACPI0C50", 0 },
909 {"PNP0C50", 0 },
910 { },
912 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
913 #else
914 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
915 struct i2c_hid_platform_data *pdata)
917 return -ENODEV;
919 #endif
921 #ifdef CONFIG_OF
922 static int i2c_hid_of_probe(struct i2c_client *client,
923 struct i2c_hid_platform_data *pdata)
925 struct device *dev = &client->dev;
926 u32 val;
927 int ret;
929 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
930 if (ret) {
931 dev_err(&client->dev, "HID register address not provided\n");
932 return -ENODEV;
934 if (val >> 16) {
935 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
936 val);
937 return -EINVAL;
939 pdata->hid_descriptor_address = val;
941 return 0;
944 static const struct of_device_id i2c_hid_of_match[] = {
945 { .compatible = "hid-over-i2c" },
948 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
949 #else
950 static inline int i2c_hid_of_probe(struct i2c_client *client,
951 struct i2c_hid_platform_data *pdata)
953 return -ENODEV;
955 #endif
957 static int i2c_hid_probe(struct i2c_client *client,
958 const struct i2c_device_id *dev_id)
960 int ret;
961 struct i2c_hid *ihid;
962 struct hid_device *hid;
963 __u16 hidRegister;
964 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
966 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
968 if (!client->irq) {
969 dev_err(&client->dev,
970 "HID over i2c has not been provided an Int IRQ\n");
971 return -EINVAL;
974 ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
975 if (!ihid)
976 return -ENOMEM;
978 if (client->dev.of_node) {
979 ret = i2c_hid_of_probe(client, &ihid->pdata);
980 if (ret)
981 goto err;
982 } else if (!platform_data) {
983 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
984 if (ret) {
985 dev_err(&client->dev,
986 "HID register address not provided\n");
987 goto err;
989 } else {
990 ihid->pdata = *platform_data;
993 i2c_set_clientdata(client, ihid);
995 ihid->client = client;
997 hidRegister = ihid->pdata.hid_descriptor_address;
998 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1000 init_waitqueue_head(&ihid->wait);
1002 /* we need to allocate the command buffer without knowing the maximum
1003 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1004 * real computation later. */
1005 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1006 if (ret < 0)
1007 goto err;
1009 ret = i2c_hid_fetch_hid_descriptor(ihid);
1010 if (ret < 0)
1011 goto err;
1013 ret = i2c_hid_init_irq(client);
1014 if (ret < 0)
1015 goto err;
1017 hid = hid_allocate_device();
1018 if (IS_ERR(hid)) {
1019 ret = PTR_ERR(hid);
1020 goto err_irq;
1023 ihid->hid = hid;
1025 hid->driver_data = client;
1026 hid->ll_driver = &i2c_hid_ll_driver;
1027 hid->hid_get_raw_report = i2c_hid_get_raw_report;
1028 hid->hid_output_raw_report = i2c_hid_output_raw_report;
1029 hid->dev.parent = &client->dev;
1030 ACPI_HANDLE_SET(&hid->dev, ACPI_HANDLE(&client->dev));
1031 hid->bus = BUS_I2C;
1032 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1033 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1034 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1036 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
1037 client->name, hid->vendor, hid->product);
1039 ret = hid_add_device(hid);
1040 if (ret) {
1041 if (ret != -ENODEV)
1042 hid_err(client, "can't add hid device: %d\n", ret);
1043 goto err_mem_free;
1046 return 0;
1048 err_mem_free:
1049 hid_destroy_device(hid);
1051 err_irq:
1052 free_irq(client->irq, ihid);
1054 err:
1055 i2c_hid_free_buffers(ihid);
1056 kfree(ihid);
1057 return ret;
1060 static int i2c_hid_remove(struct i2c_client *client)
1062 struct i2c_hid *ihid = i2c_get_clientdata(client);
1063 struct hid_device *hid;
1065 hid = ihid->hid;
1066 hid_destroy_device(hid);
1068 free_irq(client->irq, ihid);
1070 if (ihid->bufsize)
1071 i2c_hid_free_buffers(ihid);
1073 kfree(ihid);
1075 return 0;
1078 static void i2c_hid_shutdown(struct i2c_client *client)
1080 struct i2c_hid *ihid = i2c_get_clientdata(client);
1082 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1083 free_irq(client->irq, ihid);
1086 #ifdef CONFIG_PM_SLEEP
1087 static int i2c_hid_suspend(struct device *dev)
1089 struct i2c_client *client = to_i2c_client(dev);
1091 if (device_may_wakeup(&client->dev))
1092 enable_irq_wake(client->irq);
1094 /* Save some power */
1095 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1097 return 0;
1100 static int i2c_hid_resume(struct device *dev)
1102 int ret;
1103 struct i2c_client *client = to_i2c_client(dev);
1105 ret = i2c_hid_hwreset(client);
1106 if (ret)
1107 return ret;
1109 if (device_may_wakeup(&client->dev))
1110 disable_irq_wake(client->irq);
1112 return 0;
1114 #endif
1116 static SIMPLE_DEV_PM_OPS(i2c_hid_pm, i2c_hid_suspend, i2c_hid_resume);
1118 static const struct i2c_device_id i2c_hid_id_table[] = {
1119 { "hid", 0 },
1120 { },
1122 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1125 static struct i2c_driver i2c_hid_driver = {
1126 .driver = {
1127 .name = "i2c_hid",
1128 .owner = THIS_MODULE,
1129 .pm = &i2c_hid_pm,
1130 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1131 .of_match_table = of_match_ptr(i2c_hid_of_match),
1134 .probe = i2c_hid_probe,
1135 .remove = i2c_hid_remove,
1136 .shutdown = i2c_hid_shutdown,
1137 .id_table = i2c_hid_id_table,
1140 module_i2c_driver(i2c_hid_driver);
1142 MODULE_DESCRIPTION("HID over I2C core driver");
1143 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1144 MODULE_LICENSE("GPL");