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