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