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/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/i2c/i2c-hid.h>
44
45#include "../hid-ids.h"
46
47
48#define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0)
49
50
51#define I2C_HID_STARTED 0
52#define I2C_HID_RESET_PENDING 1
53#define I2C_HID_READ_PENDING 2
54
55#define I2C_HID_PWR_ON 0x00
56#define I2C_HID_PWR_SLEEP 0x01
57
58
59static bool debug;
60module_param(debug, bool, 0444);
61MODULE_PARM_DESC(debug, "print a lot of debug information");
62
63#define i2c_hid_dbg(ihid, fmt, arg...) \
64do { \
65 if (debug) \
66 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
67} while (0)
68
69struct i2c_hid_desc {
70 __le16 wHIDDescLength;
71 __le16 bcdVersion;
72 __le16 wReportDescLength;
73 __le16 wReportDescRegister;
74 __le16 wInputRegister;
75 __le16 wMaxInputLength;
76 __le16 wOutputRegister;
77 __le16 wMaxOutputLength;
78 __le16 wCommandRegister;
79 __le16 wDataRegister;
80 __le16 wVendorID;
81 __le16 wProductID;
82 __le16 wVersionID;
83 __le32 reserved;
84} __packed;
85
86struct i2c_hid_cmd {
87 unsigned int registerIndex;
88 __u8 opcode;
89 unsigned int length;
90 bool wait;
91};
92
93union command {
94 u8 data[0];
95 struct cmd {
96 __le16 reg;
97 __u8 reportTypeID;
98 __u8 opcode;
99 } __packed c;
100};
101
102#define I2C_HID_CMD(opcode_) \
103 .opcode = opcode_, .length = 4, \
104 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
105
106
107static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
108
109static const struct i2c_hid_cmd hid_report_descr_cmd = {
110 .registerIndex = offsetof(struct i2c_hid_desc,
111 wReportDescRegister),
112 .opcode = 0x00,
113 .length = 2 };
114
115static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
116 .wait = true };
117static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
118static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
119static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
120static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
121
122
123
124
125
126
127
128
129
130
131
132static DEFINE_MUTEX(i2c_hid_open_mut);
133
134
135struct i2c_hid {
136 struct i2c_client *client;
137 struct hid_device *hid;
138 union {
139 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
140 struct i2c_hid_desc hdesc;
141 };
142 __le16 wHIDDescRegister;
143
144
145 unsigned int bufsize;
146 char *inbuf;
147 char *rawbuf;
148 char *cmdbuf;
149 char *argsbuf;
150
151 unsigned long flags;
152 unsigned long quirks;
153
154 wait_queue_head_t wait;
155
156 struct i2c_hid_platform_data pdata;
157
158 bool irq_wake_enabled;
159 struct mutex reset_lock;
160};
161
162static const struct i2c_hid_quirks {
163 __u16 idVendor;
164 __u16 idProduct;
165 __u32 quirks;
166} i2c_hid_quirks[] = {
167 { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8752,
168 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
169 { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8755,
170 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
171 { 0, 0 }
172};
173
174
175
176
177
178
179
180
181static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
182{
183 u32 quirks = 0;
184 int n;
185
186 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
187 if (i2c_hid_quirks[n].idVendor == idVendor &&
188 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
189 i2c_hid_quirks[n].idProduct == idProduct))
190 quirks = i2c_hid_quirks[n].quirks;
191
192 return quirks;
193}
194
195static int __i2c_hid_command(struct i2c_client *client,
196 const struct i2c_hid_cmd *command, u8 reportID,
197 u8 reportType, u8 *args, int args_len,
198 unsigned char *buf_recv, int data_len)
199{
200 struct i2c_hid *ihid = i2c_get_clientdata(client);
201 union command *cmd = (union command *)ihid->cmdbuf;
202 int ret;
203 struct i2c_msg msg[2];
204 int msg_num = 1;
205
206 int length = command->length;
207 bool wait = command->wait;
208 unsigned int registerIndex = command->registerIndex;
209
210
211 if (command == &hid_descr_cmd) {
212 cmd->c.reg = ihid->wHIDDescRegister;
213 } else {
214 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
215 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
216 }
217
218 if (length > 2) {
219 cmd->c.opcode = command->opcode;
220 cmd->c.reportTypeID = reportID | reportType << 4;
221 }
222
223 memcpy(cmd->data + length, args, args_len);
224 length += args_len;
225
226 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
227
228 msg[0].addr = client->addr;
229 msg[0].flags = client->flags & I2C_M_TEN;
230 msg[0].len = length;
231 msg[0].buf = cmd->data;
232 if (data_len > 0) {
233 msg[1].addr = client->addr;
234 msg[1].flags = client->flags & I2C_M_TEN;
235 msg[1].flags |= I2C_M_RD;
236 msg[1].len = data_len;
237 msg[1].buf = buf_recv;
238 msg_num = 2;
239 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
240 }
241
242 if (wait)
243 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
244
245 ret = i2c_transfer(client->adapter, msg, msg_num);
246
247 if (data_len > 0)
248 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
249
250 if (ret != msg_num)
251 return ret < 0 ? ret : -EIO;
252
253 ret = 0;
254
255 if (wait) {
256 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
257 if (!wait_event_timeout(ihid->wait,
258 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
259 msecs_to_jiffies(5000)))
260 ret = -ENODATA;
261 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
262 }
263
264 return ret;
265}
266
267static int i2c_hid_command(struct i2c_client *client,
268 const struct i2c_hid_cmd *command,
269 unsigned char *buf_recv, int data_len)
270{
271 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
272 buf_recv, data_len);
273}
274
275static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
276 u8 reportID, unsigned char *buf_recv, int data_len)
277{
278 struct i2c_hid *ihid = i2c_get_clientdata(client);
279 u8 args[3];
280 int ret;
281 int args_len = 0;
282 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
283
284 i2c_hid_dbg(ihid, "%s\n", __func__);
285
286 if (reportID >= 0x0F) {
287 args[args_len++] = reportID;
288 reportID = 0x0F;
289 }
290
291 args[args_len++] = readRegister & 0xFF;
292 args[args_len++] = readRegister >> 8;
293
294 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
295 reportType, args, args_len, buf_recv, data_len);
296 if (ret) {
297 dev_err(&client->dev,
298 "failed to retrieve report from device.\n");
299 return ret;
300 }
301
302 return 0;
303}
304
305
306
307
308
309
310
311
312
313
314static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
315 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
316{
317 struct i2c_hid *ihid = i2c_get_clientdata(client);
318 u8 *args = ihid->argsbuf;
319 const struct i2c_hid_cmd *hidcmd;
320 int ret;
321 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
322 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
323 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
324 u16 size;
325 int args_len;
326 int index = 0;
327
328 i2c_hid_dbg(ihid, "%s\n", __func__);
329
330 if (data_len > ihid->bufsize)
331 return -EINVAL;
332
333 size = 2 +
334 (reportID ? 1 : 0) +
335 data_len ;
336 args_len = (reportID >= 0x0F ? 1 : 0) +
337 2 +
338 size ;
339
340 if (!use_data && maxOutputLength == 0)
341 return -ENOSYS;
342
343 if (reportID >= 0x0F) {
344 args[index++] = reportID;
345 reportID = 0x0F;
346 }
347
348
349
350
351
352 if (use_data) {
353 args[index++] = dataRegister & 0xFF;
354 args[index++] = dataRegister >> 8;
355 hidcmd = &hid_set_report_cmd;
356 } else {
357 args[index++] = outputRegister & 0xFF;
358 args[index++] = outputRegister >> 8;
359 hidcmd = &hid_no_cmd;
360 }
361
362 args[index++] = size & 0xFF;
363 args[index++] = size >> 8;
364
365 if (reportID)
366 args[index++] = reportID;
367
368 memcpy(&args[index], buf, data_len);
369
370 ret = __i2c_hid_command(client, hidcmd, reportID,
371 reportType, args, args_len, NULL, 0);
372 if (ret) {
373 dev_err(&client->dev, "failed to set a report to device.\n");
374 return ret;
375 }
376
377 return data_len;
378}
379
380static int i2c_hid_set_power(struct i2c_client *client, int power_state)
381{
382 struct i2c_hid *ihid = i2c_get_clientdata(client);
383 int ret;
384
385 i2c_hid_dbg(ihid, "%s\n", __func__);
386
387
388
389
390
391
392 if (power_state == I2C_HID_PWR_ON &&
393 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
394 ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0);
395
396
397 if (!ret)
398 goto set_pwr_exit;
399 }
400
401 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
402 0, NULL, 0, NULL, 0);
403
404 if (ret)
405 dev_err(&client->dev, "failed to change power setting.\n");
406
407set_pwr_exit:
408 return ret;
409}
410
411static int i2c_hid_hwreset(struct i2c_client *client)
412{
413 struct i2c_hid *ihid = i2c_get_clientdata(client);
414 int ret;
415
416 i2c_hid_dbg(ihid, "%s\n", __func__);
417
418
419
420
421
422
423 mutex_lock(&ihid->reset_lock);
424
425 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
426 if (ret)
427 goto out_unlock;
428
429
430
431
432
433
434
435
436 usleep_range(1000, 5000);
437
438 i2c_hid_dbg(ihid, "resetting...\n");
439
440 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
441 if (ret) {
442 dev_err(&client->dev, "failed to reset device.\n");
443 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
444 }
445
446out_unlock:
447 mutex_unlock(&ihid->reset_lock);
448 return ret;
449}
450
451static void i2c_hid_get_input(struct i2c_hid *ihid)
452{
453 int ret, ret_size;
454 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
455
456 if (size > ihid->bufsize)
457 size = ihid->bufsize;
458
459 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
460 if (ret != size) {
461 if (ret < 0)
462 return;
463
464 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
465 __func__, ret, size);
466 return;
467 }
468
469 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
470
471 if (!ret_size) {
472
473 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
474 wake_up(&ihid->wait);
475 return;
476 }
477
478 if (ret_size > size) {
479 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
480 __func__, size, ret_size);
481 return;
482 }
483
484 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
485
486 if (test_bit(I2C_HID_STARTED, &ihid->flags))
487 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
488 ret_size - 2, 1);
489
490 return;
491}
492
493static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
494{
495 struct i2c_hid *ihid = dev_id;
496
497 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
498 return IRQ_HANDLED;
499
500 i2c_hid_get_input(ihid);
501
502 return IRQ_HANDLED;
503}
504
505static int i2c_hid_get_report_length(struct hid_report *report)
506{
507 return ((report->size - 1) >> 3) + 1 +
508 report->device->report_enum[report->type].numbered + 2;
509}
510
511
512
513
514static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
515 unsigned int *max)
516{
517 struct hid_report *report;
518 unsigned int size;
519
520
521
522 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
523 size = i2c_hid_get_report_length(report);
524 if (*max < size)
525 *max = size;
526 }
527}
528
529static void i2c_hid_free_buffers(struct i2c_hid *ihid)
530{
531 kfree(ihid->inbuf);
532 kfree(ihid->rawbuf);
533 kfree(ihid->argsbuf);
534 kfree(ihid->cmdbuf);
535 ihid->inbuf = NULL;
536 ihid->rawbuf = NULL;
537 ihid->cmdbuf = NULL;
538 ihid->argsbuf = NULL;
539 ihid->bufsize = 0;
540}
541
542static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
543{
544
545
546 int args_len = sizeof(__u8) +
547 sizeof(__u16) +
548 sizeof(__u16) +
549 report_size;
550
551 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
552 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
553 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
554 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
555
556 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
557 i2c_hid_free_buffers(ihid);
558 return -ENOMEM;
559 }
560
561 ihid->bufsize = report_size;
562
563 return 0;
564}
565
566static int i2c_hid_get_raw_report(struct hid_device *hid,
567 unsigned char report_number, __u8 *buf, size_t count,
568 unsigned char report_type)
569{
570 struct i2c_client *client = hid->driver_data;
571 struct i2c_hid *ihid = i2c_get_clientdata(client);
572 size_t ret_count, ask_count;
573 int ret;
574
575 if (report_type == HID_OUTPUT_REPORT)
576 return -EINVAL;
577
578
579 ask_count = min(count + 2, (size_t)ihid->bufsize);
580
581 ret = i2c_hid_get_report(client,
582 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
583 report_number, ihid->rawbuf, ask_count);
584
585 if (ret < 0)
586 return ret;
587
588 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
589
590 if (ret_count <= 2)
591 return 0;
592
593 ret_count = min(ret_count, ask_count);
594
595
596 count = min(count, ret_count - 2);
597 memcpy(buf, ihid->rawbuf + 2, count);
598
599 return count;
600}
601
602static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
603 size_t count, unsigned char report_type, bool use_data)
604{
605 struct i2c_client *client = hid->driver_data;
606 struct i2c_hid *ihid = i2c_get_clientdata(client);
607 int report_id = buf[0];
608 int ret;
609
610 if (report_type == HID_INPUT_REPORT)
611 return -EINVAL;
612
613 mutex_lock(&ihid->reset_lock);
614
615 if (report_id) {
616 buf++;
617 count--;
618 }
619
620 ret = i2c_hid_set_or_send_report(client,
621 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
622 report_id, buf, count, use_data);
623
624 if (report_id && ret >= 0)
625 ret++;
626
627 mutex_unlock(&ihid->reset_lock);
628
629 return ret;
630}
631
632static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
633 size_t count)
634{
635 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
636 false);
637}
638
639static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
640 __u8 *buf, size_t len, unsigned char rtype,
641 int reqtype)
642{
643 switch (reqtype) {
644 case HID_REQ_GET_REPORT:
645 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
646 case HID_REQ_SET_REPORT:
647 if (buf[0] != reportnum)
648 return -EINVAL;
649 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
650 default:
651 return -EIO;
652 }
653}
654
655static int i2c_hid_parse(struct hid_device *hid)
656{
657 struct i2c_client *client = hid->driver_data;
658 struct i2c_hid *ihid = i2c_get_clientdata(client);
659 struct i2c_hid_desc *hdesc = &ihid->hdesc;
660 unsigned int rsize;
661 char *rdesc;
662 int ret;
663 int tries = 3;
664
665 i2c_hid_dbg(ihid, "entering %s\n", __func__);
666
667 rsize = le16_to_cpu(hdesc->wReportDescLength);
668 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
669 dbg_hid("weird size of report descriptor (%u)\n", rsize);
670 return -EINVAL;
671 }
672
673 do {
674 ret = i2c_hid_hwreset(client);
675 if (ret)
676 msleep(1000);
677 } while (tries-- > 0 && ret);
678
679 if (ret)
680 return ret;
681
682 rdesc = kzalloc(rsize, GFP_KERNEL);
683
684 if (!rdesc) {
685 dbg_hid("couldn't allocate rdesc memory\n");
686 return -ENOMEM;
687 }
688
689 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
690
691 ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
692 if (ret) {
693 hid_err(hid, "reading report descriptor failed\n");
694 kfree(rdesc);
695 return -EIO;
696 }
697
698 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
699
700 ret = hid_parse_report(hid, rdesc, rsize);
701 kfree(rdesc);
702 if (ret) {
703 dbg_hid("parsing report descriptor failed\n");
704 return ret;
705 }
706
707 return 0;
708}
709
710static int i2c_hid_start(struct hid_device *hid)
711{
712 struct i2c_client *client = hid->driver_data;
713 struct i2c_hid *ihid = i2c_get_clientdata(client);
714 int ret;
715 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
716
717 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
718 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
719 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
720
721 if (bufsize > ihid->bufsize) {
722 disable_irq(client->irq);
723 i2c_hid_free_buffers(ihid);
724
725 ret = i2c_hid_alloc_buffers(ihid, bufsize);
726 enable_irq(client->irq);
727
728 if (ret)
729 return ret;
730 }
731
732 return 0;
733}
734
735static void i2c_hid_stop(struct hid_device *hid)
736{
737 hid->claimed = 0;
738}
739
740static int i2c_hid_open(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 = 0;
745
746 mutex_lock(&i2c_hid_open_mut);
747 if (!hid->open++) {
748 ret = pm_runtime_get_sync(&client->dev);
749 if (ret < 0) {
750 hid->open--;
751 goto done;
752 }
753 set_bit(I2C_HID_STARTED, &ihid->flags);
754 }
755done:
756 mutex_unlock(&i2c_hid_open_mut);
757 return ret < 0 ? ret : 0;
758}
759
760static void i2c_hid_close(struct hid_device *hid)
761{
762 struct i2c_client *client = hid->driver_data;
763 struct i2c_hid *ihid = i2c_get_clientdata(client);
764
765
766
767
768
769 mutex_lock(&i2c_hid_open_mut);
770 if (!--hid->open) {
771 clear_bit(I2C_HID_STARTED, &ihid->flags);
772
773
774 pm_runtime_put(&client->dev);
775 }
776 mutex_unlock(&i2c_hid_open_mut);
777}
778
779static 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
797static 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
808static int i2c_hid_init_irq(struct i2c_client *client)
809{
810 struct i2c_hid *ihid = i2c_get_clientdata(client);
811 unsigned long irqflags = 0;
812 int ret;
813
814 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
815
816 if (!irq_get_trigger_type(client->irq))
817 irqflags = IRQF_TRIGGER_LOW;
818
819 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
820 irqflags | IRQF_ONESHOT, client->name, ihid);
821 if (ret < 0) {
822 dev_warn(&client->dev,
823 "Could not register for %s interrupt, irq = %d,"
824 " ret = %d\n",
825 client->name, client->irq, ret);
826
827 return ret;
828 }
829
830 return 0;
831}
832
833static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
834{
835 struct i2c_client *client = ihid->client;
836 struct i2c_hid_desc *hdesc = &ihid->hdesc;
837 unsigned int dsize;
838 int ret;
839
840
841 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
842 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
843 sizeof(struct i2c_hid_desc));
844 if (ret) {
845 dev_err(&client->dev, "hid_descr_cmd failed\n");
846 return -ENODEV;
847 }
848
849
850
851
852
853 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
854 dev_err(&client->dev,
855 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
856 le16_to_cpu(hdesc->bcdVersion));
857 return -ENODEV;
858 }
859
860
861 dsize = le16_to_cpu(hdesc->wHIDDescLength);
862 if (dsize != sizeof(struct i2c_hid_desc)) {
863 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
864 dsize);
865 return -ENODEV;
866 }
867 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
868 return 0;
869}
870
871#ifdef CONFIG_ACPI
872static int i2c_hid_acpi_pdata(struct i2c_client *client,
873 struct i2c_hid_platform_data *pdata)
874{
875 static u8 i2c_hid_guid[] = {
876 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
877 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
878 };
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
900static 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
909static const struct acpi_device_id i2c_hid_acpi_match[] = {
910 {"ACPI0C50", 0 },
911 {"PNP0C50", 0 },
912 { },
913};
914MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
915#else
916static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
917 struct i2c_hid_platform_data *pdata)
918{
919 return -ENODEV;
920}
921
922static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
923#endif
924
925#ifdef CONFIG_OF
926static 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
953static const struct of_device_id i2c_hid_of_match[] = {
954 { .compatible = "hid-over-i2c" },
955 {},
956};
957MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
958#else
959static 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
966static 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
1038
1039
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
1091err_mem_free:
1092 hid_destroy_device(hid);
1093
1094err_irq:
1095 free_irq(client->irq, ihid);
1096
1097err_pm:
1098 pm_runtime_put_noidle(&client->dev);
1099 pm_runtime_disable(&client->dev);
1100
1101err_regulator:
1102 regulator_disable(ihid->pdata.supply);
1103
1104err:
1105 i2c_hid_free_buffers(ihid);
1106 kfree(ihid);
1107 return ret;
1108}
1109
1110static 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
1135static 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
1144static 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
1155
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
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
1189static 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
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 if (hid->driver && hid->driver->reset_resume) {
1223 ret = hid->driver->reset_resume(hid);
1224 return ret;
1225 }
1226
1227 return 0;
1228}
1229#endif
1230
1231#ifdef CONFIG_PM
1232static int i2c_hid_runtime_suspend(struct device *dev)
1233{
1234 struct i2c_client *client = to_i2c_client(dev);
1235
1236 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1237 disable_irq(client->irq);
1238 return 0;
1239}
1240
1241static int i2c_hid_runtime_resume(struct device *dev)
1242{
1243 struct i2c_client *client = to_i2c_client(dev);
1244
1245 enable_irq(client->irq);
1246 i2c_hid_set_power(client, I2C_HID_PWR_ON);
1247 return 0;
1248}
1249#endif
1250
1251static const struct dev_pm_ops i2c_hid_pm = {
1252 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1253 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
1254 NULL)
1255};
1256
1257static const struct i2c_device_id i2c_hid_id_table[] = {
1258 { "hid", 0 },
1259 { "hid-over-i2c", 0 },
1260 { },
1261};
1262MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1263
1264
1265static 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
1279module_i2c_driver(i2c_hid_driver);
1280
1281MODULE_DESCRIPTION("HID over I2C core driver");
1282MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1283MODULE_LICENSE("GPL");
1284