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