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