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