net: DCB: Validate DCB_ATTR_DCB_BUFFER argument
[linux/fpc-iii.git] / drivers / hid / i2c-hid / i2c-hid-core.c
blob592176aff02703a9c40015625c962c3ac416216d
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/irq.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/pm.h>
29 #include <linux/device.h>
30 #include <linux/wait.h>
31 #include <linux/err.h>
32 #include <linux/string.h>
33 #include <linux/list.h>
34 #include <linux/jiffies.h>
35 #include <linux/kernel.h>
36 #include <linux/hid.h>
37 #include <linux/mutex.h>
38 #include <linux/acpi.h>
39 #include <linux/of.h>
40 #include <linux/regulator/consumer.h>
42 #include <linux/platform_data/i2c-hid.h>
44 #include "../hid-ids.h"
45 #include "i2c-hid.h"
47 /* quirks to control the device */
48 #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0)
49 #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1)
50 #define I2C_HID_QUIRK_BOGUS_IRQ BIT(4)
51 #define I2C_HID_QUIRK_RESET_ON_RESUME BIT(5)
52 #define I2C_HID_QUIRK_BAD_INPUT_SIZE BIT(6)
55 /* flags */
56 #define I2C_HID_STARTED 0
57 #define I2C_HID_RESET_PENDING 1
58 #define I2C_HID_READ_PENDING 2
60 #define I2C_HID_PWR_ON 0x00
61 #define I2C_HID_PWR_SLEEP 0x01
63 /* debug option */
64 static bool debug;
65 module_param(debug, bool, 0444);
66 MODULE_PARM_DESC(debug, "print a lot of debug information");
68 #define i2c_hid_dbg(ihid, fmt, arg...) \
69 do { \
70 if (debug) \
71 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
72 } while (0)
74 struct i2c_hid_desc {
75 __le16 wHIDDescLength;
76 __le16 bcdVersion;
77 __le16 wReportDescLength;
78 __le16 wReportDescRegister;
79 __le16 wInputRegister;
80 __le16 wMaxInputLength;
81 __le16 wOutputRegister;
82 __le16 wMaxOutputLength;
83 __le16 wCommandRegister;
84 __le16 wDataRegister;
85 __le16 wVendorID;
86 __le16 wProductID;
87 __le16 wVersionID;
88 __le32 reserved;
89 } __packed;
91 struct i2c_hid_cmd {
92 unsigned int registerIndex;
93 __u8 opcode;
94 unsigned int length;
95 bool wait;
98 union command {
99 u8 data[0];
100 struct cmd {
101 __le16 reg;
102 __u8 reportTypeID;
103 __u8 opcode;
104 } __packed c;
107 #define I2C_HID_CMD(opcode_) \
108 .opcode = opcode_, .length = 4, \
109 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
111 /* fetch HID descriptor */
112 static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
113 /* fetch report descriptors */
114 static const struct i2c_hid_cmd hid_report_descr_cmd = {
115 .registerIndex = offsetof(struct i2c_hid_desc,
116 wReportDescRegister),
117 .opcode = 0x00,
118 .length = 2 };
119 /* commands */
120 static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
121 .wait = true };
122 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
123 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
124 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
125 static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
128 * These definitions are not used here, but are defined by the spec.
129 * Keeping them here for documentation purposes.
131 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
132 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
133 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
134 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
137 /* The main device structure */
138 struct i2c_hid {
139 struct i2c_client *client; /* i2c client */
140 struct hid_device *hid; /* pointer to corresponding HID dev */
141 union {
142 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
143 struct i2c_hid_desc hdesc; /* the HID Descriptor */
145 __le16 wHIDDescRegister; /* location of the i2c
146 * register of the HID
147 * descriptor. */
148 unsigned int bufsize; /* i2c buffer size */
149 u8 *inbuf; /* Input buffer */
150 u8 *rawbuf; /* Raw Input buffer */
151 u8 *cmdbuf; /* Command buffer */
152 u8 *argsbuf; /* Command arguments buffer */
154 unsigned long flags; /* device flags */
155 unsigned long quirks; /* Various quirks */
157 wait_queue_head_t wait; /* For waiting the interrupt */
159 struct i2c_hid_platform_data pdata;
161 bool irq_wake_enabled;
162 struct mutex reset_lock;
164 unsigned long sleep_delay;
167 static const struct i2c_hid_quirks {
168 __u16 idVendor;
169 __u16 idProduct;
170 __u32 quirks;
171 } i2c_hid_quirks[] = {
172 { USB_VENDOR_ID_WEIDA, HID_ANY_ID,
173 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
174 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
175 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
176 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
177 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
178 { USB_VENDOR_ID_ELAN, HID_ANY_ID,
179 I2C_HID_QUIRK_BOGUS_IRQ },
180 { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
181 I2C_HID_QUIRK_RESET_ON_RESUME },
182 { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
183 I2C_HID_QUIRK_RESET_ON_RESUME },
184 { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
185 I2C_HID_QUIRK_BAD_INPUT_SIZE },
186 { 0, 0 }
190 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
191 * @idVendor: the 16-bit vendor ID
192 * @idProduct: the 16-bit product ID
194 * Returns: a u32 quirks value.
196 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
198 u32 quirks = 0;
199 int n;
201 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
202 if (i2c_hid_quirks[n].idVendor == idVendor &&
203 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
204 i2c_hid_quirks[n].idProduct == idProduct))
205 quirks = i2c_hid_quirks[n].quirks;
207 return quirks;
210 static int __i2c_hid_command(struct i2c_client *client,
211 const struct i2c_hid_cmd *command, u8 reportID,
212 u8 reportType, u8 *args, int args_len,
213 unsigned char *buf_recv, int data_len)
215 struct i2c_hid *ihid = i2c_get_clientdata(client);
216 union command *cmd = (union command *)ihid->cmdbuf;
217 int ret;
218 struct i2c_msg msg[2];
219 int msg_num = 1;
221 int length = command->length;
222 bool wait = command->wait;
223 unsigned int registerIndex = command->registerIndex;
225 /* special case for hid_descr_cmd */
226 if (command == &hid_descr_cmd) {
227 cmd->c.reg = ihid->wHIDDescRegister;
228 } else {
229 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
230 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
233 if (length > 2) {
234 cmd->c.opcode = command->opcode;
235 cmd->c.reportTypeID = reportID | reportType << 4;
238 memcpy(cmd->data + length, args, args_len);
239 length += args_len;
241 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
243 msg[0].addr = client->addr;
244 msg[0].flags = client->flags & I2C_M_TEN;
245 msg[0].len = length;
246 msg[0].buf = cmd->data;
247 if (data_len > 0) {
248 msg[1].addr = client->addr;
249 msg[1].flags = client->flags & I2C_M_TEN;
250 msg[1].flags |= I2C_M_RD;
251 msg[1].len = data_len;
252 msg[1].buf = buf_recv;
253 msg_num = 2;
254 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
257 if (wait)
258 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
260 ret = i2c_transfer(client->adapter, msg, msg_num);
262 if (data_len > 0)
263 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
265 if (ret != msg_num)
266 return ret < 0 ? ret : -EIO;
268 ret = 0;
270 if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) {
271 msleep(100);
272 } else if (wait) {
273 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
274 if (!wait_event_timeout(ihid->wait,
275 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
276 msecs_to_jiffies(5000)))
277 ret = -ENODATA;
278 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
281 return ret;
284 static int i2c_hid_command(struct i2c_client *client,
285 const struct i2c_hid_cmd *command,
286 unsigned char *buf_recv, int data_len)
288 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
289 buf_recv, data_len);
292 static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
293 u8 reportID, unsigned char *buf_recv, int data_len)
295 struct i2c_hid *ihid = i2c_get_clientdata(client);
296 u8 args[3];
297 int ret;
298 int args_len = 0;
299 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
301 i2c_hid_dbg(ihid, "%s\n", __func__);
303 if (reportID >= 0x0F) {
304 args[args_len++] = reportID;
305 reportID = 0x0F;
308 args[args_len++] = readRegister & 0xFF;
309 args[args_len++] = readRegister >> 8;
311 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
312 reportType, args, args_len, buf_recv, data_len);
313 if (ret) {
314 dev_err(&client->dev,
315 "failed to retrieve report from device.\n");
316 return ret;
319 return 0;
323 * i2c_hid_set_or_send_report: forward an incoming report to the device
324 * @client: the i2c_client of the device
325 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
326 * @reportID: the report ID
327 * @buf: the actual data to transfer, without the report ID
328 * @len: size of buf
329 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
331 static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
332 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
334 struct i2c_hid *ihid = i2c_get_clientdata(client);
335 u8 *args = ihid->argsbuf;
336 const struct i2c_hid_cmd *hidcmd;
337 int ret;
338 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
339 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
340 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
341 u16 size;
342 int args_len;
343 int index = 0;
345 i2c_hid_dbg(ihid, "%s\n", __func__);
347 if (data_len > ihid->bufsize)
348 return -EINVAL;
350 size = 2 /* size */ +
351 (reportID ? 1 : 0) /* reportID */ +
352 data_len /* buf */;
353 args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
354 2 /* dataRegister */ +
355 size /* args */;
357 if (!use_data && maxOutputLength == 0)
358 return -ENOSYS;
360 if (reportID >= 0x0F) {
361 args[index++] = reportID;
362 reportID = 0x0F;
366 * use the data register for feature reports or if the device does not
367 * support the output register
369 if (use_data) {
370 args[index++] = dataRegister & 0xFF;
371 args[index++] = dataRegister >> 8;
372 hidcmd = &hid_set_report_cmd;
373 } else {
374 args[index++] = outputRegister & 0xFF;
375 args[index++] = outputRegister >> 8;
376 hidcmd = &hid_no_cmd;
379 args[index++] = size & 0xFF;
380 args[index++] = size >> 8;
382 if (reportID)
383 args[index++] = reportID;
385 memcpy(&args[index], buf, data_len);
387 ret = __i2c_hid_command(client, hidcmd, reportID,
388 reportType, args, args_len, NULL, 0);
389 if (ret) {
390 dev_err(&client->dev, "failed to set a report to device.\n");
391 return ret;
394 return data_len;
397 static int i2c_hid_set_power(struct i2c_client *client, int power_state)
399 struct i2c_hid *ihid = i2c_get_clientdata(client);
400 int ret;
402 i2c_hid_dbg(ihid, "%s\n", __func__);
405 * Some devices require to send a command to wakeup before power on.
406 * The call will get a return value (EREMOTEIO) but device will be
407 * triggered and activated. After that, it goes like a normal device.
409 if (power_state == I2C_HID_PWR_ON &&
410 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
411 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
413 /* Device was already activated */
414 if (!ret)
415 goto set_pwr_exit;
418 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
419 0, NULL, 0, NULL, 0);
421 if (ret)
422 dev_err(&client->dev, "failed to change power setting.\n");
424 set_pwr_exit:
427 * The HID over I2C specification states that if a DEVICE needs time
428 * after the PWR_ON request, it should utilise CLOCK stretching.
429 * However, it has been observered that the Windows driver provides a
430 * 1ms sleep between the PWR_ON and RESET requests.
431 * According to Goodix Windows even waits 60 ms after (other?)
432 * PWR_ON requests. Testing has confirmed that several devices
433 * will not work properly without a delay after a PWR_ON request.
435 if (!ret && power_state == I2C_HID_PWR_ON)
436 msleep(60);
438 return ret;
441 static int i2c_hid_hwreset(struct i2c_client *client)
443 struct i2c_hid *ihid = i2c_get_clientdata(client);
444 int ret;
446 i2c_hid_dbg(ihid, "%s\n", __func__);
449 * This prevents sending feature reports while the device is
450 * being reset. Otherwise we may lose the reset complete
451 * interrupt.
453 mutex_lock(&ihid->reset_lock);
455 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
456 if (ret)
457 goto out_unlock;
459 i2c_hid_dbg(ihid, "resetting...\n");
461 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
462 if (ret) {
463 dev_err(&client->dev, "failed to reset device.\n");
464 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
465 goto out_unlock;
468 /* At least some SIS devices need this after reset */
469 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
471 out_unlock:
472 mutex_unlock(&ihid->reset_lock);
473 return ret;
476 static void i2c_hid_get_input(struct i2c_hid *ihid)
478 int ret;
479 u32 ret_size;
480 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
482 if (size > ihid->bufsize)
483 size = ihid->bufsize;
485 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
486 if (ret != size) {
487 if (ret < 0)
488 return;
490 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
491 __func__, ret, size);
492 return;
495 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
497 if (!ret_size) {
498 /* host or device initiated RESET completed */
499 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
500 wake_up(&ihid->wait);
501 return;
504 if (ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ && ret_size == 0xffff) {
505 dev_warn_once(&ihid->client->dev, "%s: IRQ triggered but "
506 "there's no data\n", __func__);
507 return;
510 if ((ret_size > size) || (ret_size < 2)) {
511 if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
512 ihid->inbuf[0] = size & 0xff;
513 ihid->inbuf[1] = size >> 8;
514 ret_size = size;
515 } else {
516 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
517 __func__, size, ret_size);
518 return;
522 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
524 if (test_bit(I2C_HID_STARTED, &ihid->flags))
525 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
526 ret_size - 2, 1);
528 return;
531 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
533 struct i2c_hid *ihid = dev_id;
535 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
536 return IRQ_HANDLED;
538 i2c_hid_get_input(ihid);
540 return IRQ_HANDLED;
543 static int i2c_hid_get_report_length(struct hid_report *report)
545 return ((report->size - 1) >> 3) + 1 +
546 report->device->report_enum[report->type].numbered + 2;
550 * Traverse the supplied list of reports and find the longest
552 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
553 unsigned int *max)
555 struct hid_report *report;
556 unsigned int size;
558 /* We should not rely on wMaxInputLength, as some devices may set it to
559 * a wrong length. */
560 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
561 size = i2c_hid_get_report_length(report);
562 if (*max < size)
563 *max = size;
567 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
569 kfree(ihid->inbuf);
570 kfree(ihid->rawbuf);
571 kfree(ihid->argsbuf);
572 kfree(ihid->cmdbuf);
573 ihid->inbuf = NULL;
574 ihid->rawbuf = NULL;
575 ihid->cmdbuf = NULL;
576 ihid->argsbuf = NULL;
577 ihid->bufsize = 0;
580 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
582 /* the worst case is computed from the set_report command with a
583 * reportID > 15 and the maximum report length */
584 int args_len = sizeof(__u8) + /* ReportID */
585 sizeof(__u8) + /* optional ReportID byte */
586 sizeof(__u16) + /* data register */
587 sizeof(__u16) + /* size of the report */
588 report_size; /* report */
590 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
591 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
592 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
593 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
595 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
596 i2c_hid_free_buffers(ihid);
597 return -ENOMEM;
600 ihid->bufsize = report_size;
602 return 0;
605 static int i2c_hid_get_raw_report(struct hid_device *hid,
606 unsigned char report_number, __u8 *buf, size_t count,
607 unsigned char report_type)
609 struct i2c_client *client = hid->driver_data;
610 struct i2c_hid *ihid = i2c_get_clientdata(client);
611 size_t ret_count, ask_count;
612 int ret;
614 if (report_type == HID_OUTPUT_REPORT)
615 return -EINVAL;
617 /* +2 bytes to include the size of the reply in the query buffer */
618 ask_count = min(count + 2, (size_t)ihid->bufsize);
620 ret = i2c_hid_get_report(client,
621 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
622 report_number, ihid->rawbuf, ask_count);
624 if (ret < 0)
625 return ret;
627 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
629 if (ret_count <= 2)
630 return 0;
632 ret_count = min(ret_count, ask_count);
634 /* The query buffer contains the size, dropping it in the reply */
635 count = min(count, ret_count - 2);
636 memcpy(buf, ihid->rawbuf + 2, count);
638 return count;
641 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
642 size_t count, unsigned char report_type, bool use_data)
644 struct i2c_client *client = hid->driver_data;
645 struct i2c_hid *ihid = i2c_get_clientdata(client);
646 int report_id = buf[0];
647 int ret;
649 if (report_type == HID_INPUT_REPORT)
650 return -EINVAL;
652 mutex_lock(&ihid->reset_lock);
654 if (report_id) {
655 buf++;
656 count--;
659 ret = i2c_hid_set_or_send_report(client,
660 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
661 report_id, buf, count, use_data);
663 if (report_id && ret >= 0)
664 ret++; /* add report_id to the number of transfered bytes */
666 mutex_unlock(&ihid->reset_lock);
668 return ret;
671 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
672 size_t count)
674 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
675 false);
678 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
679 __u8 *buf, size_t len, unsigned char rtype,
680 int reqtype)
682 switch (reqtype) {
683 case HID_REQ_GET_REPORT:
684 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
685 case HID_REQ_SET_REPORT:
686 if (buf[0] != reportnum)
687 return -EINVAL;
688 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
689 default:
690 return -EIO;
694 static int i2c_hid_parse(struct hid_device *hid)
696 struct i2c_client *client = hid->driver_data;
697 struct i2c_hid *ihid = i2c_get_clientdata(client);
698 struct i2c_hid_desc *hdesc = &ihid->hdesc;
699 unsigned int rsize;
700 char *rdesc;
701 int ret;
702 int tries = 3;
703 char *use_override;
705 i2c_hid_dbg(ihid, "entering %s\n", __func__);
707 rsize = le16_to_cpu(hdesc->wReportDescLength);
708 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
709 dbg_hid("weird size of report descriptor (%u)\n", rsize);
710 return -EINVAL;
713 do {
714 ret = i2c_hid_hwreset(client);
715 if (ret)
716 msleep(1000);
717 } while (tries-- > 0 && ret);
719 if (ret)
720 return ret;
722 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
723 &rsize);
725 if (use_override) {
726 rdesc = use_override;
727 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
728 } else {
729 rdesc = kzalloc(rsize, GFP_KERNEL);
731 if (!rdesc) {
732 dbg_hid("couldn't allocate rdesc memory\n");
733 return -ENOMEM;
736 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
738 ret = i2c_hid_command(client, &hid_report_descr_cmd,
739 rdesc, rsize);
740 if (ret) {
741 hid_err(hid, "reading report descriptor failed\n");
742 kfree(rdesc);
743 return -EIO;
747 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
749 ret = hid_parse_report(hid, rdesc, rsize);
750 if (!use_override)
751 kfree(rdesc);
753 if (ret) {
754 dbg_hid("parsing report descriptor failed\n");
755 return ret;
758 return 0;
761 static int i2c_hid_start(struct hid_device *hid)
763 struct i2c_client *client = hid->driver_data;
764 struct i2c_hid *ihid = i2c_get_clientdata(client);
765 int ret;
766 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
768 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
769 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
770 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
772 if (bufsize > ihid->bufsize) {
773 disable_irq(client->irq);
774 i2c_hid_free_buffers(ihid);
776 ret = i2c_hid_alloc_buffers(ihid, bufsize);
777 enable_irq(client->irq);
779 if (ret)
780 return ret;
783 return 0;
786 static void i2c_hid_stop(struct hid_device *hid)
788 hid->claimed = 0;
791 static int i2c_hid_open(struct hid_device *hid)
793 struct i2c_client *client = hid->driver_data;
794 struct i2c_hid *ihid = i2c_get_clientdata(client);
796 set_bit(I2C_HID_STARTED, &ihid->flags);
797 return 0;
800 static void i2c_hid_close(struct hid_device *hid)
802 struct i2c_client *client = hid->driver_data;
803 struct i2c_hid *ihid = i2c_get_clientdata(client);
805 clear_bit(I2C_HID_STARTED, &ihid->flags);
808 struct hid_ll_driver i2c_hid_ll_driver = {
809 .parse = i2c_hid_parse,
810 .start = i2c_hid_start,
811 .stop = i2c_hid_stop,
812 .open = i2c_hid_open,
813 .close = i2c_hid_close,
814 .output_report = i2c_hid_output_report,
815 .raw_request = i2c_hid_raw_request,
817 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
819 static int i2c_hid_init_irq(struct i2c_client *client)
821 struct i2c_hid *ihid = i2c_get_clientdata(client);
822 unsigned long irqflags = 0;
823 int ret;
825 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
827 if (!irq_get_trigger_type(client->irq))
828 irqflags = IRQF_TRIGGER_LOW;
830 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
831 irqflags | IRQF_ONESHOT, client->name, ihid);
832 if (ret < 0) {
833 dev_warn(&client->dev,
834 "Could not register for %s interrupt, irq = %d,"
835 " ret = %d\n",
836 client->name, client->irq, ret);
838 return ret;
841 return 0;
844 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
846 struct i2c_client *client = ihid->client;
847 struct i2c_hid_desc *hdesc = &ihid->hdesc;
848 unsigned int dsize;
849 int ret;
851 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
852 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
853 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
854 ihid->hdesc =
855 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
856 } else {
857 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
858 ret = i2c_hid_command(client, &hid_descr_cmd,
859 ihid->hdesc_buffer,
860 sizeof(struct i2c_hid_desc));
861 if (ret) {
862 dev_err(&client->dev, "hid_descr_cmd failed\n");
863 return -ENODEV;
867 /* Validate the length of HID descriptor, the 4 first bytes:
868 * bytes 0-1 -> length
869 * bytes 2-3 -> bcdVersion (has to be 1.00) */
870 /* check bcdVersion == 1.0 */
871 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
872 dev_err(&client->dev,
873 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
874 le16_to_cpu(hdesc->bcdVersion));
875 return -ENODEV;
878 /* Descriptor length should be 30 bytes as per the specification */
879 dsize = le16_to_cpu(hdesc->wHIDDescLength);
880 if (dsize != sizeof(struct i2c_hid_desc)) {
881 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
882 dsize);
883 return -ENODEV;
885 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
886 return 0;
889 #ifdef CONFIG_ACPI
890 static const struct acpi_device_id i2c_hid_acpi_blacklist[] = {
892 * The CHPN0001 ACPI device, which is used to describe the Chipone
893 * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible.
895 {"CHPN0001", 0 },
896 { },
899 static int i2c_hid_acpi_pdata(struct i2c_client *client,
900 struct i2c_hid_platform_data *pdata)
902 static guid_t i2c_hid_guid =
903 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
904 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
905 union acpi_object *obj;
906 struct acpi_device *adev;
907 acpi_handle handle;
909 handle = ACPI_HANDLE(&client->dev);
910 if (!handle || acpi_bus_get_device(handle, &adev)) {
911 dev_err(&client->dev, "Error could not get ACPI device\n");
912 return -ENODEV;
915 if (acpi_match_device_ids(adev, i2c_hid_acpi_blacklist) == 0)
916 return -ENODEV;
918 obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
919 ACPI_TYPE_INTEGER);
920 if (!obj) {
921 dev_err(&client->dev, "Error _DSM call to get HID descriptor address failed\n");
922 return -ENODEV;
925 pdata->hid_descriptor_address = obj->integer.value;
926 ACPI_FREE(obj);
928 return 0;
931 static void i2c_hid_acpi_fix_up_power(struct device *dev)
933 struct acpi_device *adev;
935 adev = ACPI_COMPANION(dev);
936 if (adev)
937 acpi_device_fix_up_power(adev);
940 static const struct acpi_device_id i2c_hid_acpi_match[] = {
941 {"ACPI0C50", 0 },
942 {"PNP0C50", 0 },
943 { },
945 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
946 #else
947 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
948 struct i2c_hid_platform_data *pdata)
950 return -ENODEV;
953 static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
954 #endif
956 #ifdef CONFIG_OF
957 static int i2c_hid_of_probe(struct i2c_client *client,
958 struct i2c_hid_platform_data *pdata)
960 struct device *dev = &client->dev;
961 u32 val;
962 int ret;
964 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
965 if (ret) {
966 dev_err(&client->dev, "HID register address not provided\n");
967 return -ENODEV;
969 if (val >> 16) {
970 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
971 val);
972 return -EINVAL;
974 pdata->hid_descriptor_address = val;
976 return 0;
979 static const struct of_device_id i2c_hid_of_match[] = {
980 { .compatible = "hid-over-i2c" },
983 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
984 #else
985 static inline int i2c_hid_of_probe(struct i2c_client *client,
986 struct i2c_hid_platform_data *pdata)
988 return -ENODEV;
990 #endif
992 static void i2c_hid_fwnode_probe(struct i2c_client *client,
993 struct i2c_hid_platform_data *pdata)
995 u32 val;
997 if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms",
998 &val))
999 pdata->post_power_delay_ms = val;
1002 static int i2c_hid_probe(struct i2c_client *client,
1003 const struct i2c_device_id *dev_id)
1005 int ret;
1006 struct i2c_hid *ihid;
1007 struct hid_device *hid;
1008 __u16 hidRegister;
1009 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
1011 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
1013 if (!client->irq) {
1014 dev_err(&client->dev,
1015 "HID over i2c has not been provided an Int IRQ\n");
1016 return -EINVAL;
1019 if (client->irq < 0) {
1020 if (client->irq != -EPROBE_DEFER)
1021 dev_err(&client->dev,
1022 "HID over i2c doesn't have a valid IRQ\n");
1023 return client->irq;
1026 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
1027 if (!ihid)
1028 return -ENOMEM;
1030 if (client->dev.of_node) {
1031 ret = i2c_hid_of_probe(client, &ihid->pdata);
1032 if (ret)
1033 return ret;
1034 } else if (!platform_data) {
1035 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1036 if (ret)
1037 return ret;
1038 } else {
1039 ihid->pdata = *platform_data;
1042 /* Parse platform agnostic common properties from ACPI / device tree */
1043 i2c_hid_fwnode_probe(client, &ihid->pdata);
1045 ihid->pdata.supplies[0].supply = "vdd";
1046 ihid->pdata.supplies[1].supply = "vddl";
1048 ret = devm_regulator_bulk_get(&client->dev,
1049 ARRAY_SIZE(ihid->pdata.supplies),
1050 ihid->pdata.supplies);
1051 if (ret)
1052 return ret;
1054 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1055 ihid->pdata.supplies);
1056 if (ret < 0)
1057 return ret;
1059 if (ihid->pdata.post_power_delay_ms)
1060 msleep(ihid->pdata.post_power_delay_ms);
1062 i2c_set_clientdata(client, ihid);
1064 ihid->client = client;
1066 hidRegister = ihid->pdata.hid_descriptor_address;
1067 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1069 init_waitqueue_head(&ihid->wait);
1070 mutex_init(&ihid->reset_lock);
1072 /* we need to allocate the command buffer without knowing the maximum
1073 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1074 * real computation later. */
1075 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1076 if (ret < 0)
1077 goto err_regulator;
1079 i2c_hid_acpi_fix_up_power(&client->dev);
1081 device_enable_async_suspend(&client->dev);
1083 /* Make sure there is something at this address */
1084 ret = i2c_smbus_read_byte(client);
1085 if (ret < 0) {
1086 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1087 ret = -ENXIO;
1088 goto err_regulator;
1091 ret = i2c_hid_fetch_hid_descriptor(ihid);
1092 if (ret < 0)
1093 goto err_regulator;
1095 ret = i2c_hid_init_irq(client);
1096 if (ret < 0)
1097 goto err_regulator;
1099 hid = hid_allocate_device();
1100 if (IS_ERR(hid)) {
1101 ret = PTR_ERR(hid);
1102 goto err_irq;
1105 ihid->hid = hid;
1107 hid->driver_data = client;
1108 hid->ll_driver = &i2c_hid_ll_driver;
1109 hid->dev.parent = &client->dev;
1110 hid->bus = BUS_I2C;
1111 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1112 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1113 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1115 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
1116 client->name, hid->vendor, hid->product);
1117 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1119 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1121 ret = hid_add_device(hid);
1122 if (ret) {
1123 if (ret != -ENODEV)
1124 hid_err(client, "can't add hid device: %d\n", ret);
1125 goto err_mem_free;
1128 return 0;
1130 err_mem_free:
1131 hid_destroy_device(hid);
1133 err_irq:
1134 free_irq(client->irq, ihid);
1136 err_regulator:
1137 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1138 ihid->pdata.supplies);
1139 i2c_hid_free_buffers(ihid);
1140 return ret;
1143 static int i2c_hid_remove(struct i2c_client *client)
1145 struct i2c_hid *ihid = i2c_get_clientdata(client);
1146 struct hid_device *hid;
1148 hid = ihid->hid;
1149 hid_destroy_device(hid);
1151 free_irq(client->irq, ihid);
1153 if (ihid->bufsize)
1154 i2c_hid_free_buffers(ihid);
1156 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1157 ihid->pdata.supplies);
1159 return 0;
1162 static void i2c_hid_shutdown(struct i2c_client *client)
1164 struct i2c_hid *ihid = i2c_get_clientdata(client);
1166 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1167 free_irq(client->irq, ihid);
1170 #ifdef CONFIG_PM_SLEEP
1171 static int i2c_hid_suspend(struct device *dev)
1173 struct i2c_client *client = to_i2c_client(dev);
1174 struct i2c_hid *ihid = i2c_get_clientdata(client);
1175 struct hid_device *hid = ihid->hid;
1176 int ret;
1177 int wake_status;
1179 if (hid->driver && hid->driver->suspend) {
1180 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1181 if (ret < 0)
1182 return ret;
1185 /* Save some power */
1186 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1188 disable_irq(client->irq);
1190 if (device_may_wakeup(&client->dev)) {
1191 wake_status = enable_irq_wake(client->irq);
1192 if (!wake_status)
1193 ihid->irq_wake_enabled = true;
1194 else
1195 hid_warn(hid, "Failed to enable irq wake: %d\n",
1196 wake_status);
1197 } else {
1198 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1199 ihid->pdata.supplies);
1202 return 0;
1205 static int i2c_hid_resume(struct device *dev)
1207 int ret;
1208 struct i2c_client *client = to_i2c_client(dev);
1209 struct i2c_hid *ihid = i2c_get_clientdata(client);
1210 struct hid_device *hid = ihid->hid;
1211 int wake_status;
1213 if (!device_may_wakeup(&client->dev)) {
1214 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1215 ihid->pdata.supplies);
1216 if (ret)
1217 hid_warn(hid, "Failed to enable supplies: %d\n", ret);
1219 if (ihid->pdata.post_power_delay_ms)
1220 msleep(ihid->pdata.post_power_delay_ms);
1221 } else if (ihid->irq_wake_enabled) {
1222 wake_status = disable_irq_wake(client->irq);
1223 if (!wake_status)
1224 ihid->irq_wake_enabled = false;
1225 else
1226 hid_warn(hid, "Failed to disable irq wake: %d\n",
1227 wake_status);
1230 enable_irq(client->irq);
1232 /* Instead of resetting device, simply powers the device on. This
1233 * solves "incomplete reports" on Raydium devices 2386:3118 and
1234 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1235 * data after a suspend/resume.
1237 * However some ALPS touchpads generate IRQ storm without reset, so
1238 * let's still reset them here.
1240 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
1241 ret = i2c_hid_hwreset(client);
1242 else
1243 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
1245 if (ret)
1246 return ret;
1248 if (hid->driver && hid->driver->reset_resume) {
1249 ret = hid->driver->reset_resume(hid);
1250 return ret;
1253 return 0;
1255 #endif
1257 static const struct dev_pm_ops i2c_hid_pm = {
1258 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1261 static const struct i2c_device_id i2c_hid_id_table[] = {
1262 { "hid", 0 },
1263 { "hid-over-i2c", 0 },
1264 { },
1266 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1269 static struct i2c_driver i2c_hid_driver = {
1270 .driver = {
1271 .name = "i2c_hid",
1272 .pm = &i2c_hid_pm,
1273 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1274 .of_match_table = of_match_ptr(i2c_hid_of_match),
1277 .probe = i2c_hid_probe,
1278 .remove = i2c_hid_remove,
1279 .shutdown = i2c_hid_shutdown,
1280 .id_table = i2c_hid_id_table,
1283 module_i2c_driver(i2c_hid_driver);
1285 MODULE_DESCRIPTION("HID over I2C core driver");
1286 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1287 MODULE_LICENSE("GPL");