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 /*
636 * In case of unnumbered reports the response from the device will
637 * not have the report ID that the upper layers expect, so we need
638 * to stash it the buffer ourselves and adjust the data size.
639 */
640 if (!report_number) {
641 buf[0] = 0;
642 buf++;
643 count--;
644 }
645
646 /* +2 bytes to include the size of the reply in the query buffer */
647 ask_count = min(count + 2, (size_t)ihid->bufsize);
648
649 ret = i2c_hid_get_report(client,
650 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
651 report_number, ihid->rawbuf, ask_count);
652
653 if (ret < 0)
654 return ret;
655
656 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
657
658 if (ret_count <= 2)
659 return 0;
660
661 ret_count = min(ret_count, ask_count);
662
663 /* The query buffer contains the size, dropping it in the reply */
664 count = min(count, ret_count - 2);
665 memcpy(buf, ihid->rawbuf + 2, count);
666
667 if (!report_number)
668 count++;
669
670 return count;
671 }
672
i2c_hid_output_raw_report(struct hid_device * hid,__u8 * buf,size_t count,unsigned char report_type,bool use_data)673 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
674 size_t count, unsigned char report_type, bool use_data)
675 {
676 struct i2c_client *client = hid->driver_data;
677 struct i2c_hid *ihid = i2c_get_clientdata(client);
678 int report_id = buf[0];
679 int ret;
680
681 if (report_type == HID_INPUT_REPORT)
682 return -EINVAL;
683
684 mutex_lock(&ihid->reset_lock);
685
686 /*
687 * Note that both numbered and unnumbered reports passed here
688 * are supposed to have report ID stored in the 1st byte of the
689 * buffer, so we strip it off unconditionally before passing payload
690 * to i2c_hid_set_or_send_report which takes care of encoding
691 * everything properly.
692 */
693 ret = i2c_hid_set_or_send_report(client,
694 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
695 report_id, buf + 1, count - 1, use_data);
696
697 if (ret >= 0)
698 ret++; /* add report_id to the number of transferred bytes */
699
700 mutex_unlock(&ihid->reset_lock);
701
702 return ret;
703 }
704
i2c_hid_output_report(struct hid_device * hid,__u8 * buf,size_t count)705 static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
706 size_t count)
707 {
708 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
709 false);
710 }
711
i2c_hid_raw_request(struct hid_device * hid,unsigned char reportnum,__u8 * buf,size_t len,unsigned char rtype,int reqtype)712 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
713 __u8 *buf, size_t len, unsigned char rtype,
714 int reqtype)
715 {
716 switch (reqtype) {
717 case HID_REQ_GET_REPORT:
718 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
719 case HID_REQ_SET_REPORT:
720 if (buf[0] != reportnum)
721 return -EINVAL;
722 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
723 default:
724 return -EIO;
725 }
726 }
727
i2c_hid_parse(struct hid_device * hid)728 static int i2c_hid_parse(struct hid_device *hid)
729 {
730 struct i2c_client *client = hid->driver_data;
731 struct i2c_hid *ihid = i2c_get_clientdata(client);
732 struct i2c_hid_desc *hdesc = &ihid->hdesc;
733 unsigned int rsize;
734 char *rdesc;
735 int ret;
736 int tries = 3;
737 char *use_override;
738
739 i2c_hid_dbg(ihid, "entering %s\n", __func__);
740
741 rsize = le16_to_cpu(hdesc->wReportDescLength);
742 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
743 dbg_hid("weird size of report descriptor (%u)\n", rsize);
744 return -EINVAL;
745 }
746
747 do {
748 ret = i2c_hid_hwreset(client);
749 if (ret)
750 msleep(1000);
751 } while (tries-- > 0 && ret);
752
753 if (ret)
754 return ret;
755
756 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
757 &rsize);
758
759 if (use_override) {
760 rdesc = use_override;
761 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
762 } else {
763 rdesc = kzalloc(rsize, GFP_KERNEL);
764
765 if (!rdesc) {
766 dbg_hid("couldn't allocate rdesc memory\n");
767 return -ENOMEM;
768 }
769
770 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
771
772 ret = i2c_hid_command(client, &hid_report_descr_cmd,
773 rdesc, rsize);
774 if (ret) {
775 hid_err(hid, "reading report descriptor failed\n");
776 kfree(rdesc);
777 return -EIO;
778 }
779 }
780
781 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
782
783 ret = hid_parse_report(hid, rdesc, rsize);
784 if (!use_override)
785 kfree(rdesc);
786
787 if (ret) {
788 dbg_hid("parsing report descriptor failed\n");
789 return ret;
790 }
791
792 return 0;
793 }
794
i2c_hid_start(struct hid_device * hid)795 static int i2c_hid_start(struct hid_device *hid)
796 {
797 struct i2c_client *client = hid->driver_data;
798 struct i2c_hid *ihid = i2c_get_clientdata(client);
799 int ret;
800 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
801
802 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
803 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
804 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
805
806 if (bufsize > ihid->bufsize) {
807 disable_irq(client->irq);
808 i2c_hid_free_buffers(ihid);
809
810 ret = i2c_hid_alloc_buffers(ihid, bufsize);
811 enable_irq(client->irq);
812
813 if (ret)
814 return ret;
815 }
816
817 return 0;
818 }
819
i2c_hid_stop(struct hid_device * hid)820 static void i2c_hid_stop(struct hid_device *hid)
821 {
822 hid->claimed = 0;
823 }
824
i2c_hid_open(struct hid_device * hid)825 static int i2c_hid_open(struct hid_device *hid)
826 {
827 struct i2c_client *client = hid->driver_data;
828 struct i2c_hid *ihid = i2c_get_clientdata(client);
829 int ret = 0;
830
831 ret = pm_runtime_get_sync(&client->dev);
832 if (ret < 0)
833 return ret;
834
835 set_bit(I2C_HID_STARTED, &ihid->flags);
836 return 0;
837 }
838
i2c_hid_close(struct hid_device * hid)839 static void i2c_hid_close(struct hid_device *hid)
840 {
841 struct i2c_client *client = hid->driver_data;
842 struct i2c_hid *ihid = i2c_get_clientdata(client);
843
844 clear_bit(I2C_HID_STARTED, &ihid->flags);
845
846 /* Save some power */
847 pm_runtime_put(&client->dev);
848 }
849
i2c_hid_power(struct hid_device * hid,int lvl)850 static int i2c_hid_power(struct hid_device *hid, int lvl)
851 {
852 struct i2c_client *client = hid->driver_data;
853 struct i2c_hid *ihid = i2c_get_clientdata(client);
854
855 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
856
857 switch (lvl) {
858 case PM_HINT_FULLON:
859 pm_runtime_get_sync(&client->dev);
860 break;
861 case PM_HINT_NORMAL:
862 pm_runtime_put(&client->dev);
863 break;
864 }
865 return 0;
866 }
867
868 struct hid_ll_driver i2c_hid_ll_driver = {
869 .parse = i2c_hid_parse,
870 .start = i2c_hid_start,
871 .stop = i2c_hid_stop,
872 .open = i2c_hid_open,
873 .close = i2c_hid_close,
874 .power = i2c_hid_power,
875 .output_report = i2c_hid_output_report,
876 .raw_request = i2c_hid_raw_request,
877 };
878 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
879
i2c_hid_init_irq(struct i2c_client * client)880 static int i2c_hid_init_irq(struct i2c_client *client)
881 {
882 struct i2c_hid *ihid = i2c_get_clientdata(client);
883 unsigned long irqflags = 0;
884 int ret;
885
886 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
887
888 if (!irq_get_trigger_type(client->irq))
889 irqflags = IRQF_TRIGGER_LOW;
890
891 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
892 irqflags | IRQF_ONESHOT, client->name, ihid);
893 if (ret < 0) {
894 dev_warn(&client->dev,
895 "Could not register for %s interrupt, irq = %d,"
896 " ret = %d\n",
897 client->name, client->irq, ret);
898
899 return ret;
900 }
901
902 return 0;
903 }
904
i2c_hid_fetch_hid_descriptor(struct i2c_hid * ihid)905 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
906 {
907 struct i2c_client *client = ihid->client;
908 struct i2c_hid_desc *hdesc = &ihid->hdesc;
909 unsigned int dsize;
910 int ret;
911
912 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
913 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
914 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
915 ihid->hdesc =
916 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
917 } else {
918 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
919 ret = i2c_hid_command(client, &hid_descr_cmd,
920 ihid->hdesc_buffer,
921 sizeof(struct i2c_hid_desc));
922 if (ret) {
923 dev_err(&client->dev, "hid_descr_cmd failed\n");
924 return -ENODEV;
925 }
926 }
927
928 /* Validate the length of HID descriptor, the 4 first bytes:
929 * bytes 0-1 -> length
930 * bytes 2-3 -> bcdVersion (has to be 1.00) */
931 /* check bcdVersion == 1.0 */
932 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
933 dev_err(&client->dev,
934 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
935 le16_to_cpu(hdesc->bcdVersion));
936 return -ENODEV;
937 }
938
939 /* Descriptor length should be 30 bytes as per the specification */
940 dsize = le16_to_cpu(hdesc->wHIDDescLength);
941 if (dsize != sizeof(struct i2c_hid_desc)) {
942 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
943 dsize);
944 return -ENODEV;
945 }
946 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
947 return 0;
948 }
949
950 #ifdef CONFIG_ACPI
951 static const struct acpi_device_id i2c_hid_acpi_blacklist[] = {
952 /*
953 * The CHPN0001 ACPI device, which is used to describe the Chipone
954 * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible.
955 */
956 {"CHPN0001", 0 },
957 { },
958 };
959
i2c_hid_acpi_pdata(struct i2c_client * client,struct i2c_hid_platform_data * pdata)960 static int i2c_hid_acpi_pdata(struct i2c_client *client,
961 struct i2c_hid_platform_data *pdata)
962 {
963 static guid_t i2c_hid_guid =
964 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
965 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
966 union acpi_object *obj;
967 struct acpi_device *adev;
968 acpi_handle handle;
969
970 handle = ACPI_HANDLE(&client->dev);
971 if (!handle || acpi_bus_get_device(handle, &adev)) {
972 dev_err(&client->dev, "Error could not get ACPI device\n");
973 return -ENODEV;
974 }
975
976 if (acpi_match_device_ids(adev, i2c_hid_acpi_blacklist) == 0)
977 return -ENODEV;
978
979 obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
980 ACPI_TYPE_INTEGER);
981 if (!obj) {
982 dev_err(&client->dev, "Error _DSM call to get HID descriptor address failed\n");
983 return -ENODEV;
984 }
985
986 pdata->hid_descriptor_address = obj->integer.value;
987 ACPI_FREE(obj);
988
989 return 0;
990 }
991
i2c_hid_acpi_fix_up_power(struct device * dev)992 static void i2c_hid_acpi_fix_up_power(struct device *dev)
993 {
994 struct acpi_device *adev;
995
996 adev = ACPI_COMPANION(dev);
997 if (adev)
998 acpi_device_fix_up_power(adev);
999 }
1000
1001 static const struct acpi_device_id i2c_hid_acpi_match[] = {
1002 {"ACPI0C50", 0 },
1003 {"PNP0C50", 0 },
1004 { },
1005 };
1006 MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
1007 #else
i2c_hid_acpi_pdata(struct i2c_client * client,struct i2c_hid_platform_data * pdata)1008 static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
1009 struct i2c_hid_platform_data *pdata)
1010 {
1011 return -ENODEV;
1012 }
1013
i2c_hid_acpi_fix_up_power(struct device * dev)1014 static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
1015 #endif
1016
1017 #ifdef CONFIG_OF
i2c_hid_of_probe(struct i2c_client * client,struct i2c_hid_platform_data * pdata)1018 static int i2c_hid_of_probe(struct i2c_client *client,
1019 struct i2c_hid_platform_data *pdata)
1020 {
1021 struct device *dev = &client->dev;
1022 u32 val;
1023 int ret;
1024
1025 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
1026 if (ret) {
1027 dev_err(&client->dev, "HID register address not provided\n");
1028 return -ENODEV;
1029 }
1030 if (val >> 16) {
1031 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
1032 val);
1033 return -EINVAL;
1034 }
1035 pdata->hid_descriptor_address = val;
1036
1037 return 0;
1038 }
1039
1040 static const struct of_device_id i2c_hid_of_match[] = {
1041 { .compatible = "hid-over-i2c" },
1042 {},
1043 };
1044 MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
1045 #else
i2c_hid_of_probe(struct i2c_client * client,struct i2c_hid_platform_data * pdata)1046 static inline int i2c_hid_of_probe(struct i2c_client *client,
1047 struct i2c_hid_platform_data *pdata)
1048 {
1049 return -ENODEV;
1050 }
1051 #endif
1052
i2c_hid_fwnode_probe(struct i2c_client * client,struct i2c_hid_platform_data * pdata)1053 static void i2c_hid_fwnode_probe(struct i2c_client *client,
1054 struct i2c_hid_platform_data *pdata)
1055 {
1056 u32 val;
1057
1058 if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms",
1059 &val))
1060 pdata->post_power_delay_ms = val;
1061 }
1062
i2c_hid_probe(struct i2c_client * client,const struct i2c_device_id * dev_id)1063 static int i2c_hid_probe(struct i2c_client *client,
1064 const struct i2c_device_id *dev_id)
1065 {
1066 int ret;
1067 struct i2c_hid *ihid;
1068 struct hid_device *hid;
1069 __u16 hidRegister;
1070 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
1071
1072 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
1073
1074 if (!client->irq) {
1075 dev_err(&client->dev,
1076 "HID over i2c has not been provided an Int IRQ\n");
1077 return -EINVAL;
1078 }
1079
1080 if (client->irq < 0) {
1081 if (client->irq != -EPROBE_DEFER)
1082 dev_err(&client->dev,
1083 "HID over i2c doesn't have a valid IRQ\n");
1084 return client->irq;
1085 }
1086
1087 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
1088 if (!ihid)
1089 return -ENOMEM;
1090
1091 if (client->dev.of_node) {
1092 ret = i2c_hid_of_probe(client, &ihid->pdata);
1093 if (ret)
1094 return ret;
1095 } else if (!platform_data) {
1096 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1097 if (ret)
1098 return ret;
1099 } else {
1100 ihid->pdata = *platform_data;
1101 }
1102
1103 /* Parse platform agnostic common properties from ACPI / device tree */
1104 i2c_hid_fwnode_probe(client, &ihid->pdata);
1105
1106 ihid->pdata.supplies[0].supply = "vdd";
1107 ihid->pdata.supplies[1].supply = "vddl";
1108
1109 ret = devm_regulator_bulk_get(&client->dev,
1110 ARRAY_SIZE(ihid->pdata.supplies),
1111 ihid->pdata.supplies);
1112 if (ret)
1113 return ret;
1114
1115 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1116 ihid->pdata.supplies);
1117 if (ret < 0)
1118 return ret;
1119
1120 if (ihid->pdata.post_power_delay_ms)
1121 msleep(ihid->pdata.post_power_delay_ms);
1122
1123 i2c_set_clientdata(client, ihid);
1124
1125 ihid->client = client;
1126
1127 hidRegister = ihid->pdata.hid_descriptor_address;
1128 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1129
1130 init_waitqueue_head(&ihid->wait);
1131 mutex_init(&ihid->reset_lock);
1132
1133 /* we need to allocate the command buffer without knowing the maximum
1134 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1135 * real computation later. */
1136 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1137 if (ret < 0)
1138 goto err_regulator;
1139
1140 i2c_hid_acpi_fix_up_power(&client->dev);
1141
1142 pm_runtime_get_noresume(&client->dev);
1143 pm_runtime_set_active(&client->dev);
1144 pm_runtime_enable(&client->dev);
1145 device_enable_async_suspend(&client->dev);
1146
1147 /* Make sure there is something at this address */
1148 ret = i2c_smbus_read_byte(client);
1149 if (ret < 0) {
1150 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1151 ret = -ENXIO;
1152 goto err_pm;
1153 }
1154
1155 ret = i2c_hid_fetch_hid_descriptor(ihid);
1156 if (ret < 0)
1157 goto err_pm;
1158
1159 ret = i2c_hid_init_irq(client);
1160 if (ret < 0)
1161 goto err_pm;
1162
1163 hid = hid_allocate_device();
1164 if (IS_ERR(hid)) {
1165 ret = PTR_ERR(hid);
1166 goto err_irq;
1167 }
1168
1169 ihid->hid = hid;
1170
1171 hid->driver_data = client;
1172 hid->ll_driver = &i2c_hid_ll_driver;
1173 hid->dev.parent = &client->dev;
1174 hid->bus = BUS_I2C;
1175 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1176 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1177 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1178
1179 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1180 client->name, (u16)hid->vendor, (u16)hid->product);
1181 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1182
1183 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1184
1185 ret = hid_add_device(hid);
1186 if (ret) {
1187 if (ret != -ENODEV)
1188 hid_err(client, "can't add hid device: %d\n", ret);
1189 goto err_mem_free;
1190 }
1191
1192 if (!(ihid->quirks & I2C_HID_QUIRK_NO_RUNTIME_PM))
1193 pm_runtime_put(&client->dev);
1194
1195 return 0;
1196
1197 err_mem_free:
1198 hid_destroy_device(hid);
1199
1200 err_irq:
1201 free_irq(client->irq, ihid);
1202
1203 err_pm:
1204 pm_runtime_put_noidle(&client->dev);
1205 pm_runtime_disable(&client->dev);
1206
1207 err_regulator:
1208 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1209 ihid->pdata.supplies);
1210 i2c_hid_free_buffers(ihid);
1211 return ret;
1212 }
1213
i2c_hid_remove(struct i2c_client * client)1214 static int i2c_hid_remove(struct i2c_client *client)
1215 {
1216 struct i2c_hid *ihid = i2c_get_clientdata(client);
1217 struct hid_device *hid;
1218
1219 if (!(ihid->quirks & I2C_HID_QUIRK_NO_RUNTIME_PM))
1220 pm_runtime_get_sync(&client->dev);
1221 pm_runtime_disable(&client->dev);
1222 pm_runtime_set_suspended(&client->dev);
1223 pm_runtime_put_noidle(&client->dev);
1224
1225 hid = ihid->hid;
1226 hid_destroy_device(hid);
1227
1228 free_irq(client->irq, ihid);
1229
1230 if (ihid->bufsize)
1231 i2c_hid_free_buffers(ihid);
1232
1233 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1234 ihid->pdata.supplies);
1235
1236 return 0;
1237 }
1238
i2c_hid_shutdown(struct i2c_client * client)1239 static void i2c_hid_shutdown(struct i2c_client *client)
1240 {
1241 struct i2c_hid *ihid = i2c_get_clientdata(client);
1242
1243 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1244 free_irq(client->irq, ihid);
1245 }
1246
1247 #ifdef CONFIG_PM_SLEEP
i2c_hid_suspend(struct device * dev)1248 static int i2c_hid_suspend(struct device *dev)
1249 {
1250 struct i2c_client *client = to_i2c_client(dev);
1251 struct i2c_hid *ihid = i2c_get_clientdata(client);
1252 struct hid_device *hid = ihid->hid;
1253 int ret;
1254 int wake_status;
1255
1256 if (hid->driver && hid->driver->suspend) {
1257 /*
1258 * Wake up the device so that IO issues in
1259 * HID driver's suspend code can succeed.
1260 */
1261 ret = pm_runtime_resume(dev);
1262 if (ret < 0)
1263 return ret;
1264
1265 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
1266 if (ret < 0)
1267 return ret;
1268 }
1269
1270 if (!pm_runtime_suspended(dev)) {
1271 /* Save some power */
1272 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1273
1274 disable_irq(client->irq);
1275 }
1276
1277 if (device_may_wakeup(&client->dev)) {
1278 wake_status = enable_irq_wake(client->irq);
1279 if (!wake_status)
1280 ihid->irq_wake_enabled = true;
1281 else
1282 hid_warn(hid, "Failed to enable irq wake: %d\n",
1283 wake_status);
1284 } else {
1285 regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies),
1286 ihid->pdata.supplies);
1287 }
1288
1289 return 0;
1290 }
1291
i2c_hid_resume(struct device * dev)1292 static int i2c_hid_resume(struct device *dev)
1293 {
1294 int ret;
1295 struct i2c_client *client = to_i2c_client(dev);
1296 struct i2c_hid *ihid = i2c_get_clientdata(client);
1297 struct hid_device *hid = ihid->hid;
1298 int wake_status;
1299
1300 if (!device_may_wakeup(&client->dev)) {
1301 ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies),
1302 ihid->pdata.supplies);
1303 if (ret)
1304 hid_warn(hid, "Failed to enable supplies: %d\n", ret);
1305
1306 if (ihid->pdata.post_power_delay_ms)
1307 msleep(ihid->pdata.post_power_delay_ms);
1308 } else if (ihid->irq_wake_enabled) {
1309 wake_status = disable_irq_wake(client->irq);
1310 if (!wake_status)
1311 ihid->irq_wake_enabled = false;
1312 else
1313 hid_warn(hid, "Failed to disable irq wake: %d\n",
1314 wake_status);
1315 }
1316
1317 /* We'll resume to full power */
1318 pm_runtime_disable(dev);
1319 pm_runtime_set_active(dev);
1320 pm_runtime_enable(dev);
1321
1322 enable_irq(client->irq);
1323
1324 /* Instead of resetting device, simply powers the device on. This
1325 * solves "incomplete reports" on Raydium devices 2386:3118 and
1326 * 2386:4B33 and fixes various SIS touchscreens no longer sending
1327 * data after a suspend/resume.
1328 *
1329 * However some ALPS touchpads generate IRQ storm without reset, so
1330 * let's still reset them here.
1331 */
1332 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
1333 ret = i2c_hid_hwreset(client);
1334 else
1335 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
1336
1337 if (ret)
1338 return ret;
1339
1340 if (hid->driver && hid->driver->reset_resume) {
1341 ret = hid->driver->reset_resume(hid);
1342 return ret;
1343 }
1344
1345 return 0;
1346 }
1347 #endif
1348
1349 #ifdef CONFIG_PM
i2c_hid_runtime_suspend(struct device * dev)1350 static int i2c_hid_runtime_suspend(struct device *dev)
1351 {
1352 struct i2c_client *client = to_i2c_client(dev);
1353
1354 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1355 disable_irq(client->irq);
1356 return 0;
1357 }
1358
i2c_hid_runtime_resume(struct device * dev)1359 static int i2c_hid_runtime_resume(struct device *dev)
1360 {
1361 struct i2c_client *client = to_i2c_client(dev);
1362
1363 enable_irq(client->irq);
1364 i2c_hid_set_power(client, I2C_HID_PWR_ON);
1365 return 0;
1366 }
1367 #endif
1368
1369 static const struct dev_pm_ops i2c_hid_pm = {
1370 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1371 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
1372 NULL)
1373 };
1374
1375 static const struct i2c_device_id i2c_hid_id_table[] = {
1376 { "hid", 0 },
1377 { "hid-over-i2c", 0 },
1378 { },
1379 };
1380 MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1381
1382
1383 static struct i2c_driver i2c_hid_driver = {
1384 .driver = {
1385 .name = "i2c_hid",
1386 .pm = &i2c_hid_pm,
1387 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
1388 .of_match_table = of_match_ptr(i2c_hid_of_match),
1389 },
1390
1391 .probe = i2c_hid_probe,
1392 .remove = i2c_hid_remove,
1393 .shutdown = i2c_hid_shutdown,
1394 .id_table = i2c_hid_id_table,
1395 };
1396
1397 module_i2c_driver(i2c_hid_driver);
1398
1399 MODULE_DESCRIPTION("HID over I2C core driver");
1400 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1401 MODULE_LICENSE("GPL");
1402