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