Loading...
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/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>
41
42#include <linux/platform_data/i2c-hid.h>
43
44#include "../hid-ids.h"
45#include "i2c-hid.h"
46
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)
53
54
55/* flags */
56#define I2C_HID_STARTED 0
57#define I2C_HID_RESET_PENDING 1
58#define I2C_HID_READ_PENDING 2
59
60#define I2C_HID_PWR_ON 0x00
61#define I2C_HID_PWR_SLEEP 0x01
62
63/* debug option */
64static bool debug;
65module_param(debug, bool, 0444);
66MODULE_PARM_DESC(debug, "print a lot of debug information");
67
68#define i2c_hid_dbg(ihid, fmt, arg...) \
69do { \
70 if (debug) \
71 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
72} while (0)
73
74struct 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;
90
91struct i2c_hid_cmd {
92 unsigned int registerIndex;
93 __u8 opcode;
94 unsigned int length;
95 bool wait;
96};
97
98union command {
99 u8 data[0];
100 struct cmd {
101 __le16 reg;
102 __u8 reportTypeID;
103 __u8 opcode;
104 } __packed c;
105};
106
107#define I2C_HID_CMD(opcode_) \
108 .opcode = opcode_, .length = 4, \
109 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
110
111/* fetch HID descriptor */
112static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
113/* fetch report descriptors */
114static 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 */
120static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
121 .wait = true };
122static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
123static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
124static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
125static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
126
127/*
128 * These definitions are not used here, but are defined by the spec.
129 * Keeping them here for documentation purposes.
130 *
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) };
135 */
136
137/* The main device structure */
138struct 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 */
144 };
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 */
153
154 unsigned long flags; /* device flags */
155 unsigned long quirks; /* Various quirks */
156
157 wait_queue_head_t wait; /* For waiting the interrupt */
158
159 struct i2c_hid_platform_data pdata;
160
161 bool irq_wake_enabled;
162 struct mutex reset_lock;
163};
164
165static const struct i2c_hid_quirks {
166 __u16 idVendor;
167 __u16 idProduct;
168 __u32 quirks;
169} i2c_hid_quirks[] = {
170 { USB_VENDOR_ID_WEIDA, HID_ANY_ID,
171 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
172 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
173 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
174 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
175 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
176 { USB_VENDOR_ID_ELAN, HID_ANY_ID,
177 I2C_HID_QUIRK_BOGUS_IRQ },
178 { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
179 I2C_HID_QUIRK_RESET_ON_RESUME },
180 { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
181 I2C_HID_QUIRK_RESET_ON_RESUME },
182 { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
183 I2C_HID_QUIRK_BAD_INPUT_SIZE },
184 { 0, 0 }
185};
186
187/*
188 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
189 * @idVendor: the 16-bit vendor ID
190 * @idProduct: the 16-bit product ID
191 *
192 * Returns: a u32 quirks value.
193 */
194static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
195{
196 u32 quirks = 0;
197 int n;
198
199 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
200 if (i2c_hid_quirks[n].idVendor == idVendor &&
201 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
202 i2c_hid_quirks[n].idProduct == idProduct))
203 quirks = i2c_hid_quirks[n].quirks;
204
205 return quirks;
206}
207
208static int __i2c_hid_command(struct i2c_client *client,
209 const struct i2c_hid_cmd *command, u8 reportID,
210 u8 reportType, u8 *args, int args_len,
211 unsigned char *buf_recv, int data_len)
212{
213 struct i2c_hid *ihid = i2c_get_clientdata(client);
214 union command *cmd = (union command *)ihid->cmdbuf;
215 int ret;
216 struct i2c_msg msg[2];
217 int msg_num = 1;
218
219 int length = command->length;
220 bool wait = command->wait;
221 unsigned int registerIndex = command->registerIndex;
222
223 /* special case for hid_descr_cmd */
224 if (command == &hid_descr_cmd) {
225 cmd->c.reg = ihid->wHIDDescRegister;
226 } else {
227 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
228 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
229 }
230
231 if (length > 2) {
232 cmd->c.opcode = command->opcode;
233 cmd->c.reportTypeID = reportID | reportType << 4;
234 }
235
236 memcpy(cmd->data + length, args, args_len);
237 length += args_len;
238
239 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
240
241 msg[0].addr = client->addr;
242 msg[0].flags = client->flags & I2C_M_TEN;
243 msg[0].len = length;
244 msg[0].buf = cmd->data;
245 if (data_len > 0) {
246 msg[1].addr = client->addr;
247 msg[1].flags = client->flags & I2C_M_TEN;
248 msg[1].flags |= I2C_M_RD;
249 msg[1].len = data_len;
250 msg[1].buf = buf_recv;
251 msg_num = 2;
252 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
253 }
254
255 if (wait)
256 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
257
258 ret = i2c_transfer(client->adapter, msg, msg_num);
259
260 if (data_len > 0)
261 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
262
263 if (ret != msg_num)
264 return ret < 0 ? ret : -EIO;
265
266 ret = 0;
267
268 if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) {
269 msleep(100);
270 } else if (wait) {
271 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
272 if (!wait_event_timeout(ihid->wait,
273 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
274 msecs_to_jiffies(5000)))
275 ret = -ENODATA;
276 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
277 }
278
279 return ret;
280}
281
282static int i2c_hid_command(struct i2c_client *client,
283 const struct i2c_hid_cmd *command,
284 unsigned char *buf_recv, int data_len)
285{
286 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
287 buf_recv, data_len);
288}
289
290static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
291 u8 reportID, unsigned char *buf_recv, int data_len)
292{
293 struct i2c_hid *ihid = i2c_get_clientdata(client);
294 u8 args[3];
295 int ret;
296 int args_len = 0;
297 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
298
299 i2c_hid_dbg(ihid, "%s\n", __func__);
300
301 if (reportID >= 0x0F) {
302 args[args_len++] = reportID;
303 reportID = 0x0F;
304 }
305
306 args[args_len++] = readRegister & 0xFF;
307 args[args_len++] = readRegister >> 8;
308
309 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
310 reportType, args, args_len, buf_recv, data_len);
311 if (ret) {
312 dev_err(&client->dev,
313 "failed to retrieve report from device.\n");
314 return ret;
315 }
316
317 return 0;
318}
319
320/**
321 * i2c_hid_set_or_send_report: forward an incoming report to the device
322 * @client: the i2c_client of the device
323 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
324 * @reportID: the report ID
325 * @buf: the actual data to transfer, without the report ID
326 * @len: size of buf
327 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
328 */
329static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
330 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
331{
332 struct i2c_hid *ihid = i2c_get_clientdata(client);
333 u8 *args = ihid->argsbuf;
334 const struct i2c_hid_cmd *hidcmd;
335 int ret;
336 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
337 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
338 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
339 u16 size;
340 int args_len;
341 int index = 0;
342
343 i2c_hid_dbg(ihid, "%s\n", __func__);
344
345 if (data_len > ihid->bufsize)
346 return -EINVAL;
347
348 size = 2 /* size */ +
349 (reportID ? 1 : 0) /* reportID */ +
350 data_len /* buf */;
351 args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
352 2 /* dataRegister */ +
353 size /* args */;
354
355 if (!use_data && maxOutputLength == 0)
356 return -ENOSYS;
357
358 if (reportID >= 0x0F) {
359 args[index++] = reportID;
360 reportID = 0x0F;
361 }
362
363 /*
364 * use the data register for feature reports or if the device does not
365 * support the output register
366 */
367 if (use_data) {
368 args[index++] = dataRegister & 0xFF;
369 args[index++] = dataRegister >> 8;
370 hidcmd = &hid_set_report_cmd;
371 } else {
372 args[index++] = outputRegister & 0xFF;
373 args[index++] = outputRegister >> 8;
374 hidcmd = &hid_no_cmd;
375 }
376
377 args[index++] = size & 0xFF;
378 args[index++] = size >> 8;
379
380 if (reportID)
381 args[index++] = reportID;
382
383 memcpy(&args[index], buf, data_len);
384
385 ret = __i2c_hid_command(client, hidcmd, reportID,
386 reportType, args, args_len, NULL, 0);
387 if (ret) {
388 dev_err(&client->dev, "failed to set a report to device.\n");
389 return ret;
390 }
391
392 return data_len;
393}
394
395static int i2c_hid_set_power(struct i2c_client *client, int power_state)
396{
397 struct i2c_hid *ihid = i2c_get_clientdata(client);
398 int ret;
399
400 i2c_hid_dbg(ihid, "%s\n", __func__);
401
402 /*
403 * Some devices require to send a command to wakeup before power on.
404 * The call will get a return value (EREMOTEIO) but device will be
405 * triggered and activated. After that, it goes like a normal device.
406 */
407 if (power_state == I2C_HID_PWR_ON &&
408 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
409 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
410
411 /* Device was already activated */
412 if (!ret)
413 goto set_pwr_exit;
414 }
415
416 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
417 0, NULL, 0, NULL, 0);
418
419 if (ret)
420 dev_err(&client->dev, "failed to change power setting.\n");
421
422set_pwr_exit:
423
424 /*
425 * The HID over I2C specification states that if a DEVICE needs time
426 * after the PWR_ON request, it should utilise CLOCK stretching.
427 * However, it has been observered that the Windows driver provides a
428 * 1ms sleep between the PWR_ON and RESET requests.
429 * According to Goodix Windows even waits 60 ms after (other?)
430 * PWR_ON requests. Testing has confirmed that several devices
431 * will not work properly without a delay after a PWR_ON request.
432 */
433 if (!ret && power_state == I2C_HID_PWR_ON)
434 msleep(60);
435
436 return ret;
437}
438
439static int i2c_hid_hwreset(struct i2c_client *client)
440{
441 struct i2c_hid *ihid = i2c_get_clientdata(client);
442 int ret;
443
444 i2c_hid_dbg(ihid, "%s\n", __func__);
445
446 /*
447 * This prevents sending feature reports while the device is
448 * being reset. Otherwise we may lose the reset complete
449 * interrupt.
450 */
451 mutex_lock(&ihid->reset_lock);
452
453 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
454 if (ret)
455 goto out_unlock;
456
457 i2c_hid_dbg(ihid, "resetting...\n");
458
459 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
460 if (ret) {
461 dev_err(&client->dev, "failed to reset device.\n");
462 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
463 goto out_unlock;
464 }
465
466 /* At least some SIS devices need this after reset */
467 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
468
469out_unlock:
470 mutex_unlock(&ihid->reset_lock);
471 return ret;
472}
473
474static void i2c_hid_get_input(struct i2c_hid *ihid)
475{
476 int ret;
477 u32 ret_size;
478 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
479
480 if (size > ihid->bufsize)
481 size = ihid->bufsize;
482
483 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
484 if (ret != size) {
485 if (ret < 0)
486 return;
487
488 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
489 __func__, ret, size);
490 return;
491 }
492
493 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
494
495 if (!ret_size) {
496 /* host or device initiated RESET completed */
497 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
498 wake_up(&ihid->wait);
499 return;
500 }
501
502 if (ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ && ret_size == 0xffff) {
503 dev_warn_once(&ihid->client->dev, "%s: IRQ triggered but "
504 "there's no data\n", __func__);
505 return;
506 }
507
508 if ((ret_size > size) || (ret_size < 2)) {
509 if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
510 ihid->inbuf[0] = size & 0xff;
511 ihid->inbuf[1] = size >> 8;
512 ret_size = size;
513 } else {
514 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
515 __func__, size, ret_size);
516 return;
517 }
518 }
519
520 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
521
522 if (test_bit(I2C_HID_STARTED, &ihid->flags))
523 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
524 ret_size - 2, 1);
525
526 return;
527}
528
529static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
530{
531 struct i2c_hid *ihid = dev_id;
532
533 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
534 return IRQ_HANDLED;
535
536 i2c_hid_get_input(ihid);
537
538 return IRQ_HANDLED;
539}
540
541static int i2c_hid_get_report_length(struct hid_report *report)
542{
543 return ((report->size - 1) >> 3) + 1 +
544 report->device->report_enum[report->type].numbered + 2;
545}
546
547/*
548 * Traverse the supplied list of reports and find the longest
549 */
550static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
551 unsigned int *max)
552{
553 struct hid_report *report;
554 unsigned int size;
555
556 /* We should not rely on wMaxInputLength, as some devices may set it to
557 * a wrong length. */
558 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
559 size = i2c_hid_get_report_length(report);
560 if (*max < size)
561 *max = size;
562 }
563}
564
565static void i2c_hid_free_buffers(struct i2c_hid *ihid)
566{
567 kfree(ihid->inbuf);
568 kfree(ihid->rawbuf);
569 kfree(ihid->argsbuf);
570 kfree(ihid->cmdbuf);
571 ihid->inbuf = NULL;
572 ihid->rawbuf = NULL;
573 ihid->cmdbuf = NULL;
574 ihid->argsbuf = NULL;
575 ihid->bufsize = 0;
576}
577
578static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
579{
580 /* the worst case is computed from the set_report command with a
581 * reportID > 15 and the maximum report length */
582 int args_len = sizeof(__u8) + /* ReportID */
583 sizeof(__u8) + /* optional ReportID byte */
584 sizeof(__u16) + /* data register */
585 sizeof(__u16) + /* size of the report */
586 report_size; /* report */
587
588 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
589 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
590 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
591 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
592
593 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
594 i2c_hid_free_buffers(ihid);
595 return -ENOMEM;
596 }
597
598 ihid->bufsize = report_size;
599
600 return 0;
601}
602
603static int i2c_hid_get_raw_report(struct hid_device *hid,
604 unsigned char report_number, __u8 *buf, size_t count,
605 unsigned char report_type)
606{
607 struct i2c_client *client = hid->driver_data;
608 struct i2c_hid *ihid = i2c_get_clientdata(client);
609 size_t ret_count, ask_count;
610 int ret;
611
612 if (report_type == HID_OUTPUT_REPORT)
613 return -EINVAL;
614
615 /* +2 bytes to include the size of the reply in the query buffer */
616 ask_count = min(count + 2, (size_t)ihid->bufsize);
617
618 ret = i2c_hid_get_report(client,
619 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
620 report_number, ihid->rawbuf, ask_count);
621
622 if (ret < 0)
623 return ret;
624
625 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
626
627 if (ret_count <= 2)
628 return 0;
629
630 ret_count = min(ret_count, ask_count);
631
632 /* The query buffer contains the size, dropping it in the reply */
633 count = min(count, ret_count - 2);
634 memcpy(buf, ihid->rawbuf + 2, count);
635
636 return count;
637}
638
639static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
640 size_t count, unsigned char report_type, bool use_data)
641{
642 struct i2c_client *client = hid->driver_data;
643 struct i2c_hid *ihid = i2c_get_clientdata(client);
644 int report_id = buf[0];
645 int ret;
646
647 if (report_type == HID_INPUT_REPORT)
648 return -EINVAL;
649
650 mutex_lock(&ihid->reset_lock);
651
652 if (report_id) {
653 buf++;
654 count--;
655 }
656
657 ret = i2c_hid_set_or_send_report(client,
658 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
659 report_id, buf, count, use_data);
660
661 if (report_id && ret >= 0)
662 ret++; /* add report_id to the number of transfered bytes */
663
664 mutex_unlock(&ihid->reset_lock);
665
666 return ret;
667}
668
669static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
670 size_t count)
671{
672 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
673 false);
674}
675
676static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
677 __u8 *buf, size_t len, unsigned char rtype,
678 int reqtype)
679{
680 switch (reqtype) {
681 case HID_REQ_GET_REPORT:
682 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
683 case HID_REQ_SET_REPORT:
684 if (buf[0] != reportnum)
685 return -EINVAL;
686 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
687 default:
688 return -EIO;
689 }
690}
691
692static int i2c_hid_parse(struct hid_device *hid)
693{
694 struct i2c_client *client = hid->driver_data;
695 struct i2c_hid *ihid = i2c_get_clientdata(client);
696 struct i2c_hid_desc *hdesc = &ihid->hdesc;
697 unsigned int rsize;
698 char *rdesc;
699 int ret;
700 int tries = 3;
701 char *use_override;
702
703 i2c_hid_dbg(ihid, "entering %s\n", __func__);
704
705 rsize = le16_to_cpu(hdesc->wReportDescLength);
706 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
707 dbg_hid("weird size of report descriptor (%u)\n", rsize);
708 return -EINVAL;
709 }
710
711 do {
712 ret = i2c_hid_hwreset(client);
713 if (ret)
714 msleep(1000);
715 } while (tries-- > 0 && ret);
716
717 if (ret)
718 return ret;
719
720 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
721 &rsize);
722
723 if (use_override) {
724 rdesc = use_override;
725 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
726 } else {
727 rdesc = kzalloc(rsize, GFP_KERNEL);
728
729 if (!rdesc) {
730 dbg_hid("couldn't allocate rdesc memory\n");
731 return -ENOMEM;
732 }
733
734 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
735
736 ret = i2c_hid_command(client, &hid_report_descr_cmd,
737 rdesc, rsize);
738 if (ret) {
739 hid_err(hid, "reading report descriptor failed\n");
740 kfree(rdesc);
741 return -EIO;
742 }
743 }
744
745 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
746
747 ret = hid_parse_report(hid, rdesc, rsize);
748 if (!use_override)
749 kfree(rdesc);
750
751 if (ret) {
752 dbg_hid("parsing report descriptor failed\n");
753 return ret;
754 }
755
756 return 0;
757}
758
759static int i2c_hid_start(struct hid_device *hid)
760{
761 struct i2c_client *client = hid->driver_data;
762 struct i2c_hid *ihid = i2c_get_clientdata(client);
763 int ret;
764 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
765
766 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
767 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
768 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
769
770 if (bufsize > ihid->bufsize) {
771 disable_irq(client->irq);
772 i2c_hid_free_buffers(ihid);
773
774 ret = i2c_hid_alloc_buffers(ihid, bufsize);
775 enable_irq(client->irq);
776
777 if (ret)
778 return ret;
779 }
780
781 return 0;
782}
783
784static void i2c_hid_stop(struct hid_device *hid)
785{
786 hid->claimed = 0;
787}
788
789static int i2c_hid_open(struct hid_device *hid)
790{
791 struct i2c_client *client = hid->driver_data;
792 struct i2c_hid *ihid = i2c_get_clientdata(client);
793
794 set_bit(I2C_HID_STARTED, &ihid->flags);
795 return 0;
796}
797
798static void i2c_hid_close(struct hid_device *hid)
799{
800 struct i2c_client *client = hid->driver_data;
801 struct i2c_hid *ihid = i2c_get_clientdata(client);
802
803 clear_bit(I2C_HID_STARTED, &ihid->flags);
804}
805
806struct hid_ll_driver i2c_hid_ll_driver = {
807 .parse = i2c_hid_parse,
808 .start = i2c_hid_start,
809 .stop = i2c_hid_stop,
810 .open = i2c_hid_open,
811 .close = i2c_hid_close,
812 .output_report = i2c_hid_output_report,
813 .raw_request = i2c_hid_raw_request,
814};
815EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
816
817static int i2c_hid_init_irq(struct i2c_client *client)
818{
819 struct i2c_hid *ihid = i2c_get_clientdata(client);
820 unsigned long irqflags = 0;
821 int ret;
822
823 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
824
825 if (!irq_get_trigger_type(client->irq))
826 irqflags = IRQF_TRIGGER_LOW;
827
828 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
829 irqflags | IRQF_ONESHOT, client->name, ihid);
830 if (ret < 0) {
831 dev_warn(&client->dev,
832 "Could not register for %s interrupt, irq = %d,"
833 " ret = %d\n",
834 client->name, client->irq, ret);
835
836 return ret;
837 }
838
839 return 0;
840}
841
842static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
843{
844 struct i2c_client *client = ihid->client;
845 struct i2c_hid_desc *hdesc = &ihid->hdesc;
846 unsigned int dsize;
847 int ret;
848
849 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
850 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
851 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
852 ihid->hdesc =
853 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
854 } else {
855 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
856 ret = i2c_hid_command(client, &hid_descr_cmd,
857 ihid->hdesc_buffer,
858 sizeof(struct i2c_hid_desc));
859 if (ret) {
860 dev_err(&client->dev, "hid_descr_cmd failed\n");
861 return -ENODEV;
862 }
863 }
864
865 /* Validate the length of HID descriptor, the 4 first bytes:
866 * bytes 0-1 -> length
867 * bytes 2-3 -> bcdVersion (has to be 1.00) */
868 /* check bcdVersion == 1.0 */
869 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
870 dev_err(&client->dev,
871 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
872 le16_to_cpu(hdesc->bcdVersion));
873 return -ENODEV;
874 }
875
876 /* Descriptor length should be 30 bytes as per the specification */
877 dsize = le16_to_cpu(hdesc->wHIDDescLength);
878 if (dsize != sizeof(struct i2c_hid_desc)) {
879 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
880 dsize);
881 return -ENODEV;
882 }
883 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
884 return 0;
885}
886
887#ifdef CONFIG_ACPI
888static const struct acpi_device_id i2c_hid_acpi_blacklist[] = {
889 /*
890 * The CHPN0001 ACPI device, which is used to describe the Chipone
891 * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible.
892 */
893 {"CHPN0001", 0 },
894 { },
895};
896
897static int i2c_hid_acpi_pdata(struct i2c_client *client,
898 struct i2c_hid_platform_data *pdata)
899{
900 static guid_t i2c_hid_guid =
901 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
902 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
903 union acpi_object *obj;
904 struct acpi_device *adev;
905 acpi_handle handle;
906
907 handle = ACPI_HANDLE(&client->dev);
908 if (!handle || acpi_bus_get_device(handle, &adev)) {
909 dev_err(&client->dev, "Error could not get ACPI device\n");
910 return -ENODEV;
911 }
912
913 if (acpi_match_device_ids(adev, i2c_hid_acpi_blacklist) == 0)
914 return -ENODEV;
915
916 obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
917 ACPI_TYPE_INTEGER);
918 if (!obj) {
919 dev_err(&client->dev, "Error _DSM call to get HID descriptor address failed\n");
920 return -ENODEV;
921 }
922
923 pdata->hid_descriptor_address = obj->integer.value;
924 ACPI_FREE(obj);
925
926 return 0;
927}
928
929static void i2c_hid_acpi_fix_up_power(struct device *dev)
930{
931 struct acpi_device *adev;
932
933 adev = ACPI_COMPANION(dev);
934 if (adev)
935 acpi_device_fix_up_power(adev);
936}
937
938static const struct acpi_device_id i2c_hid_acpi_match[] = {
939 {"ACPI0C50", 0 },
940 {"PNP0C50", 0 },
941 { },
942};
943MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
944#else
945static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
946 struct i2c_hid_platform_data *pdata)
947{
948 return -ENODEV;
949}
950
951static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
952#endif
953
954#ifdef CONFIG_OF
955static int i2c_hid_of_probe(struct i2c_client *client,
956 struct i2c_hid_platform_data *pdata)
957{
958 struct device *dev = &client->dev;
959 u32 val;
960 int ret;
961
962 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
963 if (ret) {
964 dev_err(&client->dev, "HID register address not provided\n");
965 return -ENODEV;
966 }
967 if (val >> 16) {
968 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
969 val);
970 return -EINVAL;
971 }
972 pdata->hid_descriptor_address = val;
973
974 return 0;
975}
976
977static const struct of_device_id i2c_hid_of_match[] = {
978 { .compatible = "hid-over-i2c" },
979 {},
980};
981MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
982#else
983static inline int i2c_hid_of_probe(struct i2c_client *client,
984 struct i2c_hid_platform_data *pdata)
985{
986 return -ENODEV;
987}
988#endif
989
990static void i2c_hid_fwnode_probe(struct i2c_client *client,
991 struct i2c_hid_platform_data *pdata)
992{
993 u32 val;
994
995 if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms",
996 &val))
997 pdata->post_power_delay_ms = val;
998}
999
1000static int i2c_hid_probe(struct i2c_client *client,
1001 const struct i2c_device_id *dev_id)
1002{
1003 int ret;
1004 struct i2c_hid *ihid;
1005 struct hid_device *hid;
1006 __u16 hidRegister;
1007 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
1008
1009 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
1010
1011 if (!client->irq) {
1012 dev_err(&client->dev,
1013 "HID over i2c has not been provided an Int IRQ\n");
1014 return -EINVAL;
1015 }
1016
1017 if (client->irq < 0) {
1018 if (client->irq != -EPROBE_DEFER)
1019 dev_err(&client->dev,
1020 "HID over i2c doesn't have a valid IRQ\n");
1021 return client->irq;
1022 }
1023
1024 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
1025 if (!ihid)
1026 return -ENOMEM;
1027
1028 if (client->dev.of_node) {
1029 ret = i2c_hid_of_probe(client, &ihid->pdata);
1030 if (ret)
1031 return ret;
1032 } else if (!platform_data) {
1033 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1034 if (ret)
1035 return ret;
1036 } else {
1037 ihid->pdata = *platform_data;
1038 }
1039
1040 /* Parse platform agnostic common properties from ACPI / device tree */
1041 i2c_hid_fwnode_probe(client, &ihid->pdata);
1042
1043 ihid->pdata.supplies[0].supply = "vdd";
1044 ihid->pdata.supplies[1].supply = "vddl";
1045
1046 ret = devm_regulator_bulk_get(&client->dev,
1047 ARRAY_SIZE(ihid->pdata.supplies),
1048 ihid->pdata.supplies);
1049 if (ret)
1050 return ret;
1051
1052 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1053 ihid->pdata.supplies);
1054 if (ret < 0)
1055 return ret;
1056
1057 if (ihid->pdata.post_power_delay_ms)
1058 msleep(ihid->pdata.post_power_delay_ms);
1059
1060 i2c_set_clientdata(client, ihid);
1061
1062 ihid->client = client;
1063
1064 hidRegister = ihid->pdata.hid_descriptor_address;
1065 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1066
1067 init_waitqueue_head(&ihid->wait);
1068 mutex_init(&ihid->reset_lock);
1069
1070 /* we need to allocate the command buffer without knowing the maximum
1071 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1072 * real computation later. */
1073 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1074 if (ret < 0)
1075 goto err_regulator;
1076
1077 i2c_hid_acpi_fix_up_power(&client->dev);
1078
1079 device_enable_async_suspend(&client->dev);
1080
1081 /* Make sure there is something at this address */
1082 ret = i2c_smbus_read_byte(client);
1083 if (ret < 0) {
1084 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1085 ret = -ENXIO;
1086 goto err_regulator;
1087 }
1088
1089 ret = i2c_hid_fetch_hid_descriptor(ihid);
1090 if (ret < 0)
1091 goto err_regulator;
1092
1093 ret = i2c_hid_init_irq(client);
1094 if (ret < 0)
1095 goto err_regulator;
1096
1097 hid = hid_allocate_device();
1098 if (IS_ERR(hid)) {
1099 ret = PTR_ERR(hid);
1100 goto err_irq;
1101 }
1102
1103 ihid->hid = hid;
1104
1105 hid->driver_data = client;
1106 hid->ll_driver = &i2c_hid_ll_driver;
1107 hid->dev.parent = &client->dev;
1108 hid->bus = BUS_I2C;
1109 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1110 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1111 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1112
1113 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
1114 client->name, hid->vendor, hid->product);
1115 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1116
1117 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1118
1119 ret = hid_add_device(hid);
1120 if (ret) {
1121 if (ret != -ENODEV)
1122 hid_err(client, "can't add hid device: %d\n", ret);
1123 goto err_mem_free;
1124 }
1125
1126 return 0;
1127
1128err_mem_free:
1129 hid_destroy_device(hid);
1130
1131err_irq:
1132 free_irq(client->irq, ihid);
1133
1134err_regulator:
1135 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1136 ihid->pdata.supplies);
1137 i2c_hid_free_buffers(ihid);
1138 return ret;
1139}
1140
1141static int i2c_hid_remove(struct i2c_client *client)
1142{
1143 struct i2c_hid *ihid = i2c_get_clientdata(client);
1144 struct hid_device *hid;
1145
1146 hid = ihid->hid;
1147 hid_destroy_device(hid);
1148
1149 free_irq(client->irq, ihid);
1150
1151 if (ihid->bufsize)
1152 i2c_hid_free_buffers(ihid);
1153
1154 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1155 ihid->pdata.supplies);
1156
1157 return 0;
1158}
1159
1160static void i2c_hid_shutdown(struct i2c_client *client)
1161{
1162 struct i2c_hid *ihid = i2c_get_clientdata(client);
1163
1164 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1165 free_irq(client->irq, ihid);
1166}
1167
1168#ifdef CONFIG_PM_SLEEP
1169static int i2c_hid_suspend(struct device *dev)
1170{
1171 struct i2c_client *client = to_i2c_client(dev);
1172 struct i2c_hid *ihid = i2c_get_clientdata(client);
1173 struct hid_device *hid = ihid->hid;
1174 int ret;
1175 int wake_status;
1176
1177 if (hid->driver && hid->driver->suspend) {
1178 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1179 if (ret < 0)
1180 return ret;
1181 }
1182
1183 /* Save some power */
1184 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1185
1186 disable_irq(client->irq);
1187
1188 if (device_may_wakeup(&client->dev)) {
1189 wake_status = enable_irq_wake(client->irq);
1190 if (!wake_status)
1191 ihid->irq_wake_enabled = true;
1192 else
1193 hid_warn(hid, "Failed to enable irq wake: %d\n",
1194 wake_status);
1195 } else {
1196 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1197 ihid->pdata.supplies);
1198 }
1199
1200 return 0;
1201}
1202
1203static int i2c_hid_resume(struct device *dev)
1204{
1205 int ret;
1206 struct i2c_client *client = to_i2c_client(dev);
1207 struct i2c_hid *ihid = i2c_get_clientdata(client);
1208 struct hid_device *hid = ihid->hid;
1209 int wake_status;
1210
1211 if (!device_may_wakeup(&client->dev)) {
1212 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1213 ihid->pdata.supplies);
1214 if (ret)
1215 hid_warn(hid, "Failed to enable supplies: %d\n", ret);
1216
1217 if (ihid->pdata.post_power_delay_ms)
1218 msleep(ihid->pdata.post_power_delay_ms);
1219 } else if (ihid->irq_wake_enabled) {
1220 wake_status = disable_irq_wake(client->irq);
1221 if (!wake_status)
1222 ihid->irq_wake_enabled = false;
1223 else
1224 hid_warn(hid, "Failed to disable irq wake: %d\n",
1225 wake_status);
1226 }
1227
1228 enable_irq(client->irq);
1229
1230 /* Instead of resetting device, simply powers the device on. This
1231 * solves "incomplete reports" on Raydium devices 2386:3118 and
1232 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1233 * data after a suspend/resume.
1234 *
1235 * However some ALPS touchpads generate IRQ storm without reset, so
1236 * let's still reset them here.
1237 */
1238 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
1239 ret = i2c_hid_hwreset(client);
1240 else
1241 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
1242
1243 if (ret)
1244 return ret;
1245
1246 if (hid->driver && hid->driver->reset_resume) {
1247 ret = hid->driver->reset_resume(hid);
1248 return ret;
1249 }
1250
1251 return 0;
1252}
1253#endif
1254
1255static const struct dev_pm_ops i2c_hid_pm = {
1256 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1257};
1258
1259static const struct i2c_device_id i2c_hid_id_table[] = {
1260 { "hid", 0 },
1261 { "hid-over-i2c", 0 },
1262 { },
1263};
1264MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1265
1266
1267static struct i2c_driver i2c_hid_driver = {
1268 .driver = {
1269 .name = "i2c_hid",
1270 .pm = &i2c_hid_pm,
1271 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1272 .of_match_table = of_match_ptr(i2c_hid_of_match),
1273 },
1274
1275 .probe = i2c_hid_probe,
1276 .remove = i2c_hid_remove,
1277 .shutdown = i2c_hid_shutdown,
1278 .id_table = i2c_hid_id_table,
1279};
1280
1281module_i2c_driver(i2c_hid_driver);
1282
1283MODULE_DESCRIPTION("HID over I2C core driver");
1284MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1285MODULE_LICENSE("GPL");
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/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>
41
42#include <linux/platform_data/i2c-hid.h>
43
44#include "../hid-ids.h"
45#include "i2c-hid.h"
46
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
52/* flags */
53#define I2C_HID_STARTED 0
54#define I2C_HID_RESET_PENDING 1
55#define I2C_HID_READ_PENDING 2
56
57#define I2C_HID_PWR_ON 0x00
58#define I2C_HID_PWR_SLEEP 0x01
59
60/* debug option */
61static bool debug;
62module_param(debug, bool, 0444);
63MODULE_PARM_DESC(debug, "print a lot of debug information");
64
65#define i2c_hid_dbg(ihid, fmt, arg...) \
66do { \
67 if (debug) \
68 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
69} while (0)
70
71struct i2c_hid_desc {
72 __le16 wHIDDescLength;
73 __le16 bcdVersion;
74 __le16 wReportDescLength;
75 __le16 wReportDescRegister;
76 __le16 wInputRegister;
77 __le16 wMaxInputLength;
78 __le16 wOutputRegister;
79 __le16 wMaxOutputLength;
80 __le16 wCommandRegister;
81 __le16 wDataRegister;
82 __le16 wVendorID;
83 __le16 wProductID;
84 __le16 wVersionID;
85 __le32 reserved;
86} __packed;
87
88struct i2c_hid_cmd {
89 unsigned int registerIndex;
90 __u8 opcode;
91 unsigned int length;
92 bool wait;
93};
94
95union command {
96 u8 data[0];
97 struct cmd {
98 __le16 reg;
99 __u8 reportTypeID;
100 __u8 opcode;
101 } __packed c;
102};
103
104#define I2C_HID_CMD(opcode_) \
105 .opcode = opcode_, .length = 4, \
106 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
107
108/* fetch HID descriptor */
109static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
110/* fetch report descriptors */
111static const struct i2c_hid_cmd hid_report_descr_cmd = {
112 .registerIndex = offsetof(struct i2c_hid_desc,
113 wReportDescRegister),
114 .opcode = 0x00,
115 .length = 2 };
116/* commands */
117static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
118 .wait = true };
119static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
120static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
121static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
122static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
123
124/*
125 * These definitions are not used here, but are defined by the spec.
126 * Keeping them here for documentation purposes.
127 *
128 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
129 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
130 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
131 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
132 */
133
134/* The main device structure */
135struct i2c_hid {
136 struct i2c_client *client; /* i2c client */
137 struct hid_device *hid; /* pointer to corresponding HID dev */
138 union {
139 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
140 struct i2c_hid_desc hdesc; /* the HID Descriptor */
141 };
142 __le16 wHIDDescRegister; /* location of the i2c
143 * register of the HID
144 * descriptor. */
145 unsigned int bufsize; /* i2c buffer size */
146 u8 *inbuf; /* Input buffer */
147 u8 *rawbuf; /* Raw Input buffer */
148 u8 *cmdbuf; /* Command buffer */
149 u8 *argsbuf; /* Command arguments buffer */
150
151 unsigned long flags; /* device flags */
152 unsigned long quirks; /* Various quirks */
153
154 wait_queue_head_t wait; /* For waiting the interrupt */
155
156 struct i2c_hid_platform_data pdata;
157
158 bool irq_wake_enabled;
159 struct mutex reset_lock;
160
161 unsigned long sleep_delay;
162};
163
164static const struct i2c_hid_quirks {
165 __u16 idVendor;
166 __u16 idProduct;
167 __u32 quirks;
168} i2c_hid_quirks[] = {
169 { USB_VENDOR_ID_WEIDA, HID_ANY_ID,
170 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
171 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
172 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
173 { USB_VENDOR_ID_ELAN, HID_ANY_ID,
174 I2C_HID_QUIRK_BOGUS_IRQ },
175 { 0, 0 }
176};
177
178/*
179 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device
180 * @idVendor: the 16-bit vendor ID
181 * @idProduct: the 16-bit product ID
182 *
183 * Returns: a u32 quirks value.
184 */
185static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
186{
187 u32 quirks = 0;
188 int n;
189
190 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
191 if (i2c_hid_quirks[n].idVendor == idVendor &&
192 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
193 i2c_hid_quirks[n].idProduct == idProduct))
194 quirks = i2c_hid_quirks[n].quirks;
195
196 return quirks;
197}
198
199static int __i2c_hid_command(struct i2c_client *client,
200 const struct i2c_hid_cmd *command, u8 reportID,
201 u8 reportType, u8 *args, int args_len,
202 unsigned char *buf_recv, int data_len)
203{
204 struct i2c_hid *ihid = i2c_get_clientdata(client);
205 union command *cmd = (union command *)ihid->cmdbuf;
206 int ret;
207 struct i2c_msg msg[2];
208 int msg_num = 1;
209
210 int length = command->length;
211 bool wait = command->wait;
212 unsigned int registerIndex = command->registerIndex;
213
214 /* special case for hid_descr_cmd */
215 if (command == &hid_descr_cmd) {
216 cmd->c.reg = ihid->wHIDDescRegister;
217 } else {
218 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
219 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
220 }
221
222 if (length > 2) {
223 cmd->c.opcode = command->opcode;
224 cmd->c.reportTypeID = reportID | reportType << 4;
225 }
226
227 memcpy(cmd->data + length, args, args_len);
228 length += args_len;
229
230 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
231
232 msg[0].addr = client->addr;
233 msg[0].flags = client->flags & I2C_M_TEN;
234 msg[0].len = length;
235 msg[0].buf = cmd->data;
236 if (data_len > 0) {
237 msg[1].addr = client->addr;
238 msg[1].flags = client->flags & I2C_M_TEN;
239 msg[1].flags |= I2C_M_RD;
240 msg[1].len = data_len;
241 msg[1].buf = buf_recv;
242 msg_num = 2;
243 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
244 }
245
246 if (wait)
247 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
248
249 ret = i2c_transfer(client->adapter, msg, msg_num);
250
251 if (data_len > 0)
252 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
253
254 if (ret != msg_num)
255 return ret < 0 ? ret : -EIO;
256
257 ret = 0;
258
259 if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) {
260 msleep(100);
261 } else if (wait) {
262 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
263 if (!wait_event_timeout(ihid->wait,
264 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
265 msecs_to_jiffies(5000)))
266 ret = -ENODATA;
267 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
268 }
269
270 return ret;
271}
272
273static int i2c_hid_command(struct i2c_client *client,
274 const struct i2c_hid_cmd *command,
275 unsigned char *buf_recv, int data_len)
276{
277 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
278 buf_recv, data_len);
279}
280
281static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
282 u8 reportID, unsigned char *buf_recv, int data_len)
283{
284 struct i2c_hid *ihid = i2c_get_clientdata(client);
285 u8 args[3];
286 int ret;
287 int args_len = 0;
288 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
289
290 i2c_hid_dbg(ihid, "%s\n", __func__);
291
292 if (reportID >= 0x0F) {
293 args[args_len++] = reportID;
294 reportID = 0x0F;
295 }
296
297 args[args_len++] = readRegister & 0xFF;
298 args[args_len++] = readRegister >> 8;
299
300 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
301 reportType, args, args_len, buf_recv, data_len);
302 if (ret) {
303 dev_err(&client->dev,
304 "failed to retrieve report from device.\n");
305 return ret;
306 }
307
308 return 0;
309}
310
311/**
312 * i2c_hid_set_or_send_report: forward an incoming report to the device
313 * @client: the i2c_client of the device
314 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
315 * @reportID: the report ID
316 * @buf: the actual data to transfer, without the report ID
317 * @len: size of buf
318 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
319 */
320static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
321 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
322{
323 struct i2c_hid *ihid = i2c_get_clientdata(client);
324 u8 *args = ihid->argsbuf;
325 const struct i2c_hid_cmd *hidcmd;
326 int ret;
327 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
328 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
329 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
330 u16 size;
331 int args_len;
332 int index = 0;
333
334 i2c_hid_dbg(ihid, "%s\n", __func__);
335
336 if (data_len > ihid->bufsize)
337 return -EINVAL;
338
339 size = 2 /* size */ +
340 (reportID ? 1 : 0) /* reportID */ +
341 data_len /* buf */;
342 args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
343 2 /* dataRegister */ +
344 size /* args */;
345
346 if (!use_data && maxOutputLength == 0)
347 return -ENOSYS;
348
349 if (reportID >= 0x0F) {
350 args[index++] = reportID;
351 reportID = 0x0F;
352 }
353
354 /*
355 * use the data register for feature reports or if the device does not
356 * support the output register
357 */
358 if (use_data) {
359 args[index++] = dataRegister & 0xFF;
360 args[index++] = dataRegister >> 8;
361 hidcmd = &hid_set_report_cmd;
362 } else {
363 args[index++] = outputRegister & 0xFF;
364 args[index++] = outputRegister >> 8;
365 hidcmd = &hid_no_cmd;
366 }
367
368 args[index++] = size & 0xFF;
369 args[index++] = size >> 8;
370
371 if (reportID)
372 args[index++] = reportID;
373
374 memcpy(&args[index], buf, data_len);
375
376 ret = __i2c_hid_command(client, hidcmd, reportID,
377 reportType, args, args_len, NULL, 0);
378 if (ret) {
379 dev_err(&client->dev, "failed to set a report to device.\n");
380 return ret;
381 }
382
383 return data_len;
384}
385
386static int i2c_hid_set_power(struct i2c_client *client, int power_state)
387{
388 struct i2c_hid *ihid = i2c_get_clientdata(client);
389 int ret;
390
391 i2c_hid_dbg(ihid, "%s\n", __func__);
392
393 /*
394 * Some devices require to send a command to wakeup before power on.
395 * The call will get a return value (EREMOTEIO) but device will be
396 * triggered and activated. After that, it goes like a normal device.
397 */
398 if (power_state == I2C_HID_PWR_ON &&
399 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
400 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
401
402 /* Device was already activated */
403 if (!ret)
404 goto set_pwr_exit;
405 }
406
407 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
408 0, NULL, 0, NULL, 0);
409
410 if (ret)
411 dev_err(&client->dev, "failed to change power setting.\n");
412
413set_pwr_exit:
414 return ret;
415}
416
417static int i2c_hid_hwreset(struct i2c_client *client)
418{
419 struct i2c_hid *ihid = i2c_get_clientdata(client);
420 int ret;
421
422 i2c_hid_dbg(ihid, "%s\n", __func__);
423
424 /*
425 * This prevents sending feature reports while the device is
426 * being reset. Otherwise we may lose the reset complete
427 * interrupt.
428 */
429 mutex_lock(&ihid->reset_lock);
430
431 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
432 if (ret)
433 goto out_unlock;
434
435 /*
436 * The HID over I2C specification states that if a DEVICE needs time
437 * after the PWR_ON request, it should utilise CLOCK stretching.
438 * However, it has been observered that the Windows driver provides a
439 * 1ms sleep between the PWR_ON and RESET requests and that some devices
440 * rely on this.
441 */
442 usleep_range(1000, 5000);
443
444 i2c_hid_dbg(ihid, "resetting...\n");
445
446 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
447 if (ret) {
448 dev_err(&client->dev, "failed to reset device.\n");
449 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
450 goto out_unlock;
451 }
452
453 /* At least some SIS devices need this after reset */
454 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
455
456out_unlock:
457 mutex_unlock(&ihid->reset_lock);
458 return ret;
459}
460
461static void i2c_hid_get_input(struct i2c_hid *ihid)
462{
463 int ret;
464 u32 ret_size;
465 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
466
467 if (size > ihid->bufsize)
468 size = ihid->bufsize;
469
470 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
471 if (ret != size) {
472 if (ret < 0)
473 return;
474
475 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
476 __func__, ret, size);
477 return;
478 }
479
480 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
481
482 if (!ret_size) {
483 /* host or device initiated RESET completed */
484 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
485 wake_up(&ihid->wait);
486 return;
487 }
488
489 if (ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ && ret_size == 0xffff) {
490 dev_warn_once(&ihid->client->dev, "%s: IRQ triggered but "
491 "there's no data\n", __func__);
492 return;
493 }
494
495 if ((ret_size > size) || (ret_size < 2)) {
496 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
497 __func__, size, ret_size);
498 return;
499 }
500
501 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
502
503 if (test_bit(I2C_HID_STARTED, &ihid->flags))
504 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
505 ret_size - 2, 1);
506
507 return;
508}
509
510static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
511{
512 struct i2c_hid *ihid = dev_id;
513
514 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
515 return IRQ_HANDLED;
516
517 i2c_hid_get_input(ihid);
518
519 return IRQ_HANDLED;
520}
521
522static int i2c_hid_get_report_length(struct hid_report *report)
523{
524 return ((report->size - 1) >> 3) + 1 +
525 report->device->report_enum[report->type].numbered + 2;
526}
527
528/*
529 * Traverse the supplied list of reports and find the longest
530 */
531static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
532 unsigned int *max)
533{
534 struct hid_report *report;
535 unsigned int size;
536
537 /* We should not rely on wMaxInputLength, as some devices may set it to
538 * a wrong length. */
539 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
540 size = i2c_hid_get_report_length(report);
541 if (*max < size)
542 *max = size;
543 }
544}
545
546static void i2c_hid_free_buffers(struct i2c_hid *ihid)
547{
548 kfree(ihid->inbuf);
549 kfree(ihid->rawbuf);
550 kfree(ihid->argsbuf);
551 kfree(ihid->cmdbuf);
552 ihid->inbuf = NULL;
553 ihid->rawbuf = NULL;
554 ihid->cmdbuf = NULL;
555 ihid->argsbuf = NULL;
556 ihid->bufsize = 0;
557}
558
559static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
560{
561 /* the worst case is computed from the set_report command with a
562 * reportID > 15 and the maximum report length */
563 int args_len = sizeof(__u8) + /* ReportID */
564 sizeof(__u8) + /* optional ReportID byte */
565 sizeof(__u16) + /* data register */
566 sizeof(__u16) + /* size of the report */
567 report_size; /* report */
568
569 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
570 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
571 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
572 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
573
574 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
575 i2c_hid_free_buffers(ihid);
576 return -ENOMEM;
577 }
578
579 ihid->bufsize = report_size;
580
581 return 0;
582}
583
584static int i2c_hid_get_raw_report(struct hid_device *hid,
585 unsigned char report_number, __u8 *buf, size_t count,
586 unsigned char report_type)
587{
588 struct i2c_client *client = hid->driver_data;
589 struct i2c_hid *ihid = i2c_get_clientdata(client);
590 size_t ret_count, ask_count;
591 int ret;
592
593 if (report_type == HID_OUTPUT_REPORT)
594 return -EINVAL;
595
596 /* +2 bytes to include the size of the reply in the query buffer */
597 ask_count = min(count + 2, (size_t)ihid->bufsize);
598
599 ret = i2c_hid_get_report(client,
600 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
601 report_number, ihid->rawbuf, ask_count);
602
603 if (ret < 0)
604 return ret;
605
606 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
607
608 if (ret_count <= 2)
609 return 0;
610
611 ret_count = min(ret_count, ask_count);
612
613 /* The query buffer contains the size, dropping it in the reply */
614 count = min(count, ret_count - 2);
615 memcpy(buf, ihid->rawbuf + 2, count);
616
617 return count;
618}
619
620static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
621 size_t count, unsigned char report_type, bool use_data)
622{
623 struct i2c_client *client = hid->driver_data;
624 struct i2c_hid *ihid = i2c_get_clientdata(client);
625 int report_id = buf[0];
626 int ret;
627
628 if (report_type == HID_INPUT_REPORT)
629 return -EINVAL;
630
631 mutex_lock(&ihid->reset_lock);
632
633 if (report_id) {
634 buf++;
635 count--;
636 }
637
638 ret = i2c_hid_set_or_send_report(client,
639 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
640 report_id, buf, count, use_data);
641
642 if (report_id && ret >= 0)
643 ret++; /* add report_id to the number of transfered bytes */
644
645 mutex_unlock(&ihid->reset_lock);
646
647 return ret;
648}
649
650static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
651 size_t count)
652{
653 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
654 false);
655}
656
657static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
658 __u8 *buf, size_t len, unsigned char rtype,
659 int reqtype)
660{
661 switch (reqtype) {
662 case HID_REQ_GET_REPORT:
663 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
664 case HID_REQ_SET_REPORT:
665 if (buf[0] != reportnum)
666 return -EINVAL;
667 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
668 default:
669 return -EIO;
670 }
671}
672
673static int i2c_hid_parse(struct hid_device *hid)
674{
675 struct i2c_client *client = hid->driver_data;
676 struct i2c_hid *ihid = i2c_get_clientdata(client);
677 struct i2c_hid_desc *hdesc = &ihid->hdesc;
678 unsigned int rsize;
679 char *rdesc;
680 int ret;
681 int tries = 3;
682 char *use_override;
683
684 i2c_hid_dbg(ihid, "entering %s\n", __func__);
685
686 rsize = le16_to_cpu(hdesc->wReportDescLength);
687 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
688 dbg_hid("weird size of report descriptor (%u)\n", rsize);
689 return -EINVAL;
690 }
691
692 do {
693 ret = i2c_hid_hwreset(client);
694 if (ret)
695 msleep(1000);
696 } while (tries-- > 0 && ret);
697
698 if (ret)
699 return ret;
700
701 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
702 &rsize);
703
704 if (use_override) {
705 rdesc = use_override;
706 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
707 } else {
708 rdesc = kzalloc(rsize, GFP_KERNEL);
709
710 if (!rdesc) {
711 dbg_hid("couldn't allocate rdesc memory\n");
712 return -ENOMEM;
713 }
714
715 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
716
717 ret = i2c_hid_command(client, &hid_report_descr_cmd,
718 rdesc, rsize);
719 if (ret) {
720 hid_err(hid, "reading report descriptor failed\n");
721 kfree(rdesc);
722 return -EIO;
723 }
724 }
725
726 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
727
728 ret = hid_parse_report(hid, rdesc, rsize);
729 if (!use_override)
730 kfree(rdesc);
731
732 if (ret) {
733 dbg_hid("parsing report descriptor failed\n");
734 return ret;
735 }
736
737 return 0;
738}
739
740static int i2c_hid_start(struct hid_device *hid)
741{
742 struct i2c_client *client = hid->driver_data;
743 struct i2c_hid *ihid = i2c_get_clientdata(client);
744 int ret;
745 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
746
747 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
748 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
749 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
750
751 if (bufsize > ihid->bufsize) {
752 disable_irq(client->irq);
753 i2c_hid_free_buffers(ihid);
754
755 ret = i2c_hid_alloc_buffers(ihid, bufsize);
756 enable_irq(client->irq);
757
758 if (ret)
759 return ret;
760 }
761
762 return 0;
763}
764
765static void i2c_hid_stop(struct hid_device *hid)
766{
767 hid->claimed = 0;
768}
769
770static int i2c_hid_open(struct hid_device *hid)
771{
772 struct i2c_client *client = hid->driver_data;
773 struct i2c_hid *ihid = i2c_get_clientdata(client);
774
775 set_bit(I2C_HID_STARTED, &ihid->flags);
776 return 0;
777}
778
779static void i2c_hid_close(struct hid_device *hid)
780{
781 struct i2c_client *client = hid->driver_data;
782 struct i2c_hid *ihid = i2c_get_clientdata(client);
783
784 clear_bit(I2C_HID_STARTED, &ihid->flags);
785}
786
787struct hid_ll_driver i2c_hid_ll_driver = {
788 .parse = i2c_hid_parse,
789 .start = i2c_hid_start,
790 .stop = i2c_hid_stop,
791 .open = i2c_hid_open,
792 .close = i2c_hid_close,
793 .output_report = i2c_hid_output_report,
794 .raw_request = i2c_hid_raw_request,
795};
796EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
797
798static int i2c_hid_init_irq(struct i2c_client *client)
799{
800 struct i2c_hid *ihid = i2c_get_clientdata(client);
801 unsigned long irqflags = 0;
802 int ret;
803
804 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
805
806 if (!irq_get_trigger_type(client->irq))
807 irqflags = IRQF_TRIGGER_LOW;
808
809 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
810 irqflags | IRQF_ONESHOT, client->name, ihid);
811 if (ret < 0) {
812 dev_warn(&client->dev,
813 "Could not register for %s interrupt, irq = %d,"
814 " ret = %d\n",
815 client->name, client->irq, ret);
816
817 return ret;
818 }
819
820 return 0;
821}
822
823static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
824{
825 struct i2c_client *client = ihid->client;
826 struct i2c_hid_desc *hdesc = &ihid->hdesc;
827 unsigned int dsize;
828 int ret;
829
830 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
831 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
832 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
833 ihid->hdesc =
834 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
835 } else {
836 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
837 ret = i2c_hid_command(client, &hid_descr_cmd,
838 ihid->hdesc_buffer,
839 sizeof(struct i2c_hid_desc));
840 if (ret) {
841 dev_err(&client->dev, "hid_descr_cmd failed\n");
842 return -ENODEV;
843 }
844 }
845
846 /* Validate the length of HID descriptor, the 4 first bytes:
847 * bytes 0-1 -> length
848 * bytes 2-3 -> bcdVersion (has to be 1.00) */
849 /* check bcdVersion == 1.0 */
850 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
851 dev_err(&client->dev,
852 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
853 le16_to_cpu(hdesc->bcdVersion));
854 return -ENODEV;
855 }
856
857 /* Descriptor length should be 30 bytes as per the specification */
858 dsize = le16_to_cpu(hdesc->wHIDDescLength);
859 if (dsize != sizeof(struct i2c_hid_desc)) {
860 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
861 dsize);
862 return -ENODEV;
863 }
864 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
865 return 0;
866}
867
868#ifdef CONFIG_ACPI
869static const struct acpi_device_id i2c_hid_acpi_blacklist[] = {
870 /*
871 * The CHPN0001 ACPI device, which is used to describe the Chipone
872 * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible.
873 */
874 {"CHPN0001", 0 },
875 { },
876};
877
878static int i2c_hid_acpi_pdata(struct i2c_client *client,
879 struct i2c_hid_platform_data *pdata)
880{
881 static guid_t i2c_hid_guid =
882 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
883 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
884 union acpi_object *obj;
885 struct acpi_device *adev;
886 acpi_handle handle;
887
888 handle = ACPI_HANDLE(&client->dev);
889 if (!handle || acpi_bus_get_device(handle, &adev)) {
890 dev_err(&client->dev, "Error could not get ACPI device\n");
891 return -ENODEV;
892 }
893
894 if (acpi_match_device_ids(adev, i2c_hid_acpi_blacklist) == 0)
895 return -ENODEV;
896
897 obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
898 ACPI_TYPE_INTEGER);
899 if (!obj) {
900 dev_err(&client->dev, "Error _DSM call to get HID descriptor address failed\n");
901 return -ENODEV;
902 }
903
904 pdata->hid_descriptor_address = obj->integer.value;
905 ACPI_FREE(obj);
906
907 return 0;
908}
909
910static void i2c_hid_acpi_fix_up_power(struct device *dev)
911{
912 struct acpi_device *adev;
913
914 adev = ACPI_COMPANION(dev);
915 if (adev)
916 acpi_device_fix_up_power(adev);
917}
918
919static const struct acpi_device_id i2c_hid_acpi_match[] = {
920 {"ACPI0C50", 0 },
921 {"PNP0C50", 0 },
922 { },
923};
924MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
925#else
926static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
927 struct i2c_hid_platform_data *pdata)
928{
929 return -ENODEV;
930}
931
932static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
933#endif
934
935#ifdef CONFIG_OF
936static int i2c_hid_of_probe(struct i2c_client *client,
937 struct i2c_hid_platform_data *pdata)
938{
939 struct device *dev = &client->dev;
940 u32 val;
941 int ret;
942
943 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
944 if (ret) {
945 dev_err(&client->dev, "HID register address not provided\n");
946 return -ENODEV;
947 }
948 if (val >> 16) {
949 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
950 val);
951 return -EINVAL;
952 }
953 pdata->hid_descriptor_address = val;
954
955 return 0;
956}
957
958static const struct of_device_id i2c_hid_of_match[] = {
959 { .compatible = "hid-over-i2c" },
960 {},
961};
962MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
963#else
964static inline int i2c_hid_of_probe(struct i2c_client *client,
965 struct i2c_hid_platform_data *pdata)
966{
967 return -ENODEV;
968}
969#endif
970
971static void i2c_hid_fwnode_probe(struct i2c_client *client,
972 struct i2c_hid_platform_data *pdata)
973{
974 u32 val;
975
976 if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms",
977 &val))
978 pdata->post_power_delay_ms = val;
979}
980
981static int i2c_hid_probe(struct i2c_client *client,
982 const struct i2c_device_id *dev_id)
983{
984 int ret;
985 struct i2c_hid *ihid;
986 struct hid_device *hid;
987 __u16 hidRegister;
988 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
989
990 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
991
992 if (!client->irq) {
993 dev_err(&client->dev,
994 "HID over i2c has not been provided an Int IRQ\n");
995 return -EINVAL;
996 }
997
998 if (client->irq < 0) {
999 if (client->irq != -EPROBE_DEFER)
1000 dev_err(&client->dev,
1001 "HID over i2c doesn't have a valid IRQ\n");
1002 return client->irq;
1003 }
1004
1005 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
1006 if (!ihid)
1007 return -ENOMEM;
1008
1009 if (client->dev.of_node) {
1010 ret = i2c_hid_of_probe(client, &ihid->pdata);
1011 if (ret)
1012 return ret;
1013 } else if (!platform_data) {
1014 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1015 if (ret)
1016 return ret;
1017 } else {
1018 ihid->pdata = *platform_data;
1019 }
1020
1021 /* Parse platform agnostic common properties from ACPI / device tree */
1022 i2c_hid_fwnode_probe(client, &ihid->pdata);
1023
1024 ihid->pdata.supplies[0].supply = "vdd";
1025 ihid->pdata.supplies[1].supply = "vddl";
1026
1027 ret = devm_regulator_bulk_get(&client->dev,
1028 ARRAY_SIZE(ihid->pdata.supplies),
1029 ihid->pdata.supplies);
1030 if (ret)
1031 return ret;
1032
1033 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1034 ihid->pdata.supplies);
1035 if (ret < 0)
1036 return ret;
1037
1038 if (ihid->pdata.post_power_delay_ms)
1039 msleep(ihid->pdata.post_power_delay_ms);
1040
1041 i2c_set_clientdata(client, ihid);
1042
1043 ihid->client = client;
1044
1045 hidRegister = ihid->pdata.hid_descriptor_address;
1046 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1047
1048 init_waitqueue_head(&ihid->wait);
1049 mutex_init(&ihid->reset_lock);
1050
1051 /* we need to allocate the command buffer without knowing the maximum
1052 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1053 * real computation later. */
1054 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1055 if (ret < 0)
1056 goto err_regulator;
1057
1058 i2c_hid_acpi_fix_up_power(&client->dev);
1059
1060 device_enable_async_suspend(&client->dev);
1061
1062 /* Make sure there is something at this address */
1063 ret = i2c_smbus_read_byte(client);
1064 if (ret < 0) {
1065 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1066 ret = -ENXIO;
1067 goto err_regulator;
1068 }
1069
1070 ret = i2c_hid_fetch_hid_descriptor(ihid);
1071 if (ret < 0)
1072 goto err_regulator;
1073
1074 ret = i2c_hid_init_irq(client);
1075 if (ret < 0)
1076 goto err_regulator;
1077
1078 hid = hid_allocate_device();
1079 if (IS_ERR(hid)) {
1080 ret = PTR_ERR(hid);
1081 goto err_irq;
1082 }
1083
1084 ihid->hid = hid;
1085
1086 hid->driver_data = client;
1087 hid->ll_driver = &i2c_hid_ll_driver;
1088 hid->dev.parent = &client->dev;
1089 hid->bus = BUS_I2C;
1090 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1091 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1092 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1093
1094 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
1095 client->name, hid->vendor, hid->product);
1096 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1097
1098 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1099
1100 ret = hid_add_device(hid);
1101 if (ret) {
1102 if (ret != -ENODEV)
1103 hid_err(client, "can't add hid device: %d\n", ret);
1104 goto err_mem_free;
1105 }
1106
1107 return 0;
1108
1109err_mem_free:
1110 hid_destroy_device(hid);
1111
1112err_irq:
1113 free_irq(client->irq, ihid);
1114
1115err_regulator:
1116 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1117 ihid->pdata.supplies);
1118 i2c_hid_free_buffers(ihid);
1119 return ret;
1120}
1121
1122static int i2c_hid_remove(struct i2c_client *client)
1123{
1124 struct i2c_hid *ihid = i2c_get_clientdata(client);
1125 struct hid_device *hid;
1126
1127 hid = ihid->hid;
1128 hid_destroy_device(hid);
1129
1130 free_irq(client->irq, ihid);
1131
1132 if (ihid->bufsize)
1133 i2c_hid_free_buffers(ihid);
1134
1135 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1136 ihid->pdata.supplies);
1137
1138 return 0;
1139}
1140
1141static void i2c_hid_shutdown(struct i2c_client *client)
1142{
1143 struct i2c_hid *ihid = i2c_get_clientdata(client);
1144
1145 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1146 free_irq(client->irq, ihid);
1147}
1148
1149#ifdef CONFIG_PM_SLEEP
1150static int i2c_hid_suspend(struct device *dev)
1151{
1152 struct i2c_client *client = to_i2c_client(dev);
1153 struct i2c_hid *ihid = i2c_get_clientdata(client);
1154 struct hid_device *hid = ihid->hid;
1155 int ret;
1156 int wake_status;
1157
1158 if (hid->driver && hid->driver->suspend) {
1159 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1160 if (ret < 0)
1161 return ret;
1162 }
1163
1164 /* Save some power */
1165 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1166
1167 disable_irq(client->irq);
1168
1169 if (device_may_wakeup(&client->dev)) {
1170 wake_status = enable_irq_wake(client->irq);
1171 if (!wake_status)
1172 ihid->irq_wake_enabled = true;
1173 else
1174 hid_warn(hid, "Failed to enable irq wake: %d\n",
1175 wake_status);
1176 } else {
1177 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1178 ihid->pdata.supplies);
1179 }
1180
1181 return 0;
1182}
1183
1184static int i2c_hid_resume(struct device *dev)
1185{
1186 int ret;
1187 struct i2c_client *client = to_i2c_client(dev);
1188 struct i2c_hid *ihid = i2c_get_clientdata(client);
1189 struct hid_device *hid = ihid->hid;
1190 int wake_status;
1191
1192 if (!device_may_wakeup(&client->dev)) {
1193 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1194 ihid->pdata.supplies);
1195 if (ret)
1196 hid_warn(hid, "Failed to enable supplies: %d\n", ret);
1197
1198 if (ihid->pdata.post_power_delay_ms)
1199 msleep(ihid->pdata.post_power_delay_ms);
1200 } else if (ihid->irq_wake_enabled) {
1201 wake_status = disable_irq_wake(client->irq);
1202 if (!wake_status)
1203 ihid->irq_wake_enabled = false;
1204 else
1205 hid_warn(hid, "Failed to disable irq wake: %d\n",
1206 wake_status);
1207 }
1208
1209 enable_irq(client->irq);
1210
1211 /* Instead of resetting device, simply powers the device on. This
1212 * solves "incomplete reports" on Raydium devices 2386:3118 and
1213 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1214 * data after a suspend/resume.
1215 */
1216 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
1217 if (ret)
1218 return ret;
1219
1220 if (hid->driver && hid->driver->reset_resume) {
1221 ret = hid->driver->reset_resume(hid);
1222 return ret;
1223 }
1224
1225 return 0;
1226}
1227#endif
1228
1229static const struct dev_pm_ops i2c_hid_pm = {
1230 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1231};
1232
1233static const struct i2c_device_id i2c_hid_id_table[] = {
1234 { "hid", 0 },
1235 { "hid-over-i2c", 0 },
1236 { },
1237};
1238MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1239
1240
1241static struct i2c_driver i2c_hid_driver = {
1242 .driver = {
1243 .name = "i2c_hid",
1244 .pm = &i2c_hid_pm,
1245 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1246 .of_match_table = of_match_ptr(i2c_hid_of_match),
1247 },
1248
1249 .probe = i2c_hid_probe,
1250 .remove = i2c_hid_remove,
1251 .shutdown = i2c_hid_shutdown,
1252 .id_table = i2c_hid_id_table,
1253};
1254
1255module_i2c_driver(i2c_hid_driver);
1256
1257MODULE_DESCRIPTION("HID over I2C core driver");
1258MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1259MODULE_LICENSE("GPL");