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