]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hid/i2c-hid/i2c-hid.c
pwm: mediatek: Improve precision in rate calculation
[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 */
03b59dce
AM
147 u8 *inbuf; /* Input buffer */
148 u8 *rawbuf; /* Raw Input buffer */
149 u8 *cmdbuf; /* Command buffer */
150 u8 *argsbuf; /* Command arguments buffer */
4a200c3b
BT
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 457{
03b59dce
AM
458 int ret;
459 u32 ret_size;
6d00f37e
SF
460 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
461
462 if (size > ihid->bufsize)
463 size = ihid->bufsize;
4a200c3b
BT
464
465 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
466 if (ret != size) {
467 if (ret < 0)
317b204a 468 return;
4a200c3b
BT
469
470 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
471 __func__, ret, size);
317b204a 472 return;
4a200c3b
BT
473 }
474
475 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
476
477 if (!ret_size) {
478 /* host or device initiated RESET completed */
479 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
480 wake_up(&ihid->wait);
317b204a 481 return;
4a200c3b
BT
482 }
483
03b59dce 484 if ((ret_size > size) || (ret_size <= 2)) {
4a200c3b
BT
485 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
486 __func__, size, ret_size);
317b204a 487 return;
4a200c3b
BT
488 }
489
490 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
491
492 if (test_bit(I2C_HID_STARTED, &ihid->flags))
493 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
494 ret_size - 2, 1);
495
317b204a 496 return;
4a200c3b
BT
497}
498
499static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
500{
501 struct i2c_hid *ihid = dev_id;
502
503 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
504 return IRQ_HANDLED;
505
506 i2c_hid_get_input(ihid);
507
508 return IRQ_HANDLED;
509}
510
511static int i2c_hid_get_report_length(struct hid_report *report)
512{
513 return ((report->size - 1) >> 3) + 1 +
514 report->device->report_enum[report->type].numbered + 2;
515}
516
4a200c3b
BT
517/*
518 * Traverse the supplied list of reports and find the longest
519 */
520static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
521 unsigned int *max)
522{
523 struct hid_report *report;
524 unsigned int size;
525
526 /* We should not rely on wMaxInputLength, as some devices may set it to
527 * a wrong length. */
528 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
529 size = i2c_hid_get_report_length(report);
530 if (*max < size)
531 *max = size;
532 }
533}
534
29b45787
BT
535static void i2c_hid_free_buffers(struct i2c_hid *ihid)
536{
537 kfree(ihid->inbuf);
6296f4a8 538 kfree(ihid->rawbuf);
29b45787
BT
539 kfree(ihid->argsbuf);
540 kfree(ihid->cmdbuf);
541 ihid->inbuf = NULL;
6296f4a8 542 ihid->rawbuf = NULL;
29b45787
BT
543 ihid->cmdbuf = NULL;
544 ihid->argsbuf = NULL;
545 ihid->bufsize = 0;
546}
547
548static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
4a200c3b
BT
549{
550 /* the worst case is computed from the set_report command with a
551 * reportID > 15 and the maximum report length */
8320caee
AS
552 int args_len = sizeof(__u8) + /* ReportID */
553 sizeof(__u8) + /* optional ReportID byte */
4a200c3b
BT
554 sizeof(__u16) + /* data register */
555 sizeof(__u16) + /* size of the report */
29b45787 556 report_size; /* report */
4a200c3b 557
29b45787 558 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
6296f4a8 559 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
4a200c3b 560 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
4a200c3b
BT
561 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
562
6296f4a8 563 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
29b45787 564 i2c_hid_free_buffers(ihid);
4a200c3b
BT
565 return -ENOMEM;
566 }
567
29b45787 568 ihid->bufsize = report_size;
4a200c3b 569
29b45787 570 return 0;
4a200c3b
BT
571}
572
573static int i2c_hid_get_raw_report(struct hid_device *hid,
574 unsigned char report_number, __u8 *buf, size_t count,
575 unsigned char report_type)
576{
577 struct i2c_client *client = hid->driver_data;
578 struct i2c_hid *ihid = i2c_get_clientdata(client);
e5b50fe7 579 size_t ret_count, ask_count;
4a200c3b
BT
580 int ret;
581
582 if (report_type == HID_OUTPUT_REPORT)
583 return -EINVAL;
584
e5b50fe7
BT
585 /* +2 bytes to include the size of the reply in the query buffer */
586 ask_count = min(count + 2, (size_t)ihid->bufsize);
4a200c3b
BT
587
588 ret = i2c_hid_get_report(client,
589 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
6296f4a8 590 report_number, ihid->rawbuf, ask_count);
4a200c3b
BT
591
592 if (ret < 0)
593 return ret;
594
6296f4a8 595 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
4a200c3b 596
9afd09a1 597 if (ret_count <= 2)
e5b50fe7
BT
598 return 0;
599
600 ret_count = min(ret_count, ask_count);
601
602 /* The query buffer contains the size, dropping it in the reply */
603 count = min(count, ret_count - 2);
6296f4a8 604 memcpy(buf, ihid->rawbuf + 2, count);
4a200c3b
BT
605
606 return count;
607}
608
609static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
9b5a9ae8 610 size_t count, unsigned char report_type, bool use_data)
4a200c3b
BT
611{
612 struct i2c_client *client = hid->driver_data;
9a327405 613 struct i2c_hid *ihid = i2c_get_clientdata(client);
4a200c3b 614 int report_id = buf[0];
c284979a 615 int ret;
4a200c3b
BT
616
617 if (report_type == HID_INPUT_REPORT)
618 return -EINVAL;
619
9a327405
MW
620 mutex_lock(&ihid->reset_lock);
621
c284979a
BT
622 if (report_id) {
623 buf++;
624 count--;
625 }
626
9b5a9ae8 627 ret = i2c_hid_set_or_send_report(client,
4a200c3b 628 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
9b5a9ae8 629 report_id, buf, count, use_data);
c284979a
BT
630
631 if (report_id && ret >= 0)
632 ret++; /* add report_id to the number of transfered bytes */
633
9a327405
MW
634 mutex_unlock(&ihid->reset_lock);
635
c284979a 636 return ret;
4a200c3b
BT
637}
638
9b5a9ae8
BT
639static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
640 size_t count)
545bef68 641{
9b5a9ae8
BT
642 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
643 false);
644}
545bef68 645
9b5a9ae8
BT
646static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
647 __u8 *buf, size_t len, unsigned char rtype,
648 int reqtype)
649{
545bef68
BT
650 switch (reqtype) {
651 case HID_REQ_GET_REPORT:
9b5a9ae8 652 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
545bef68 653 case HID_REQ_SET_REPORT:
9b5a9ae8
BT
654 if (buf[0] != reportnum)
655 return -EINVAL;
656 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
657 default:
658 return -EIO;
545bef68 659 }
545bef68
BT
660}
661
4a200c3b
BT
662static int i2c_hid_parse(struct hid_device *hid)
663{
664 struct i2c_client *client = hid->driver_data;
665 struct i2c_hid *ihid = i2c_get_clientdata(client);
666 struct i2c_hid_desc *hdesc = &ihid->hdesc;
667 unsigned int rsize;
668 char *rdesc;
669 int ret;
670 int tries = 3;
671
672 i2c_hid_dbg(ihid, "entering %s\n", __func__);
673
674 rsize = le16_to_cpu(hdesc->wReportDescLength);
675 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
676 dbg_hid("weird size of report descriptor (%u)\n", rsize);
677 return -EINVAL;
678 }
679
680 do {
681 ret = i2c_hid_hwreset(client);
682 if (ret)
683 msleep(1000);
684 } while (tries-- > 0 && ret);
685
686 if (ret)
687 return ret;
688
689 rdesc = kzalloc(rsize, GFP_KERNEL);
690
691 if (!rdesc) {
692 dbg_hid("couldn't allocate rdesc memory\n");
693 return -ENOMEM;
694 }
695
696 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
697
698 ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
699 if (ret) {
700 hid_err(hid, "reading report descriptor failed\n");
701 kfree(rdesc);
702 return -EIO;
703 }
704
705 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
706
707 ret = hid_parse_report(hid, rdesc, rsize);
708 kfree(rdesc);
709 if (ret) {
710 dbg_hid("parsing report descriptor failed\n");
711 return ret;
712 }
713
714 return 0;
715}
716
717static int i2c_hid_start(struct hid_device *hid)
718{
719 struct i2c_client *client = hid->driver_data;
720 struct i2c_hid *ihid = i2c_get_clientdata(client);
721 int ret;
29b45787 722 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
4a200c3b 723
29b45787
BT
724 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
725 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
726 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
4a200c3b 727
29b45787 728 if (bufsize > ihid->bufsize) {
8cd16166 729 disable_irq(client->irq);
4a200c3b
BT
730 i2c_hid_free_buffers(ihid);
731
29b45787 732 ret = i2c_hid_alloc_buffers(ihid, bufsize);
8cd16166 733 enable_irq(client->irq);
4a200c3b 734
29b45787 735 if (ret)
4a200c3b 736 return ret;
4a200c3b
BT
737 }
738
4a200c3b
BT
739 return 0;
740}
741
742static void i2c_hid_stop(struct hid_device *hid)
743{
4a200c3b 744 hid->claimed = 0;
4a200c3b
BT
745}
746
747static int i2c_hid_open(struct hid_device *hid)
748{
749 struct i2c_client *client = hid->driver_data;
750 struct i2c_hid *ihid = i2c_get_clientdata(client);
7a7d6d9c 751 int ret = 0;
4a200c3b 752
85ae9113
DT
753 ret = pm_runtime_get_sync(&client->dev);
754 if (ret < 0)
755 return ret;
756
757 set_bit(I2C_HID_STARTED, &ihid->flags);
758 return 0;
4a200c3b
BT
759}
760
761static void i2c_hid_close(struct hid_device *hid)
762{
763 struct i2c_client *client = hid->driver_data;
764 struct i2c_hid *ihid = i2c_get_clientdata(client);
765
85ae9113 766 clear_bit(I2C_HID_STARTED, &ihid->flags);
4a200c3b 767
85ae9113
DT
768 /* Save some power */
769 pm_runtime_put(&client->dev);
4a200c3b
BT
770}
771
772static int i2c_hid_power(struct hid_device *hid, int lvl)
773{
774 struct i2c_client *client = hid->driver_data;
775 struct i2c_hid *ihid = i2c_get_clientdata(client);
4a200c3b
BT
776
777 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
778
779 switch (lvl) {
780 case PM_HINT_FULLON:
34f439e4 781 pm_runtime_get_sync(&client->dev);
4a200c3b
BT
782 break;
783 case PM_HINT_NORMAL:
34f439e4 784 pm_runtime_put(&client->dev);
4a200c3b
BT
785 break;
786 }
34f439e4 787 return 0;
4a200c3b
BT
788}
789
fc2237a7 790struct hid_ll_driver i2c_hid_ll_driver = {
4a200c3b
BT
791 .parse = i2c_hid_parse,
792 .start = i2c_hid_start,
793 .stop = i2c_hid_stop,
794 .open = i2c_hid_open,
795 .close = i2c_hid_close,
796 .power = i2c_hid_power,
9b5a9ae8
BT
797 .output_report = i2c_hid_output_report,
798 .raw_request = i2c_hid_raw_request,
4a200c3b 799};
fc2237a7 800EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
4a200c3b 801
0fe763c5 802static int i2c_hid_init_irq(struct i2c_client *client)
4a200c3b
BT
803{
804 struct i2c_hid *ihid = i2c_get_clientdata(client);
00f7fea5 805 unsigned long irqflags = 0;
4a200c3b
BT
806 int ret;
807
ba18a931 808 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
4a200c3b 809
00f7fea5
BT
810 if (!irq_get_trigger_type(client->irq))
811 irqflags = IRQF_TRIGGER_LOW;
812
ba18a931 813 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
00f7fea5 814 irqflags | IRQF_ONESHOT, client->name, ihid);
4a200c3b 815 if (ret < 0) {
9972dcc2 816 dev_warn(&client->dev,
4a200c3b
BT
817 "Could not register for %s interrupt, irq = %d,"
818 " ret = %d\n",
ba18a931 819 client->name, client->irq, ret);
4a200c3b
BT
820
821 return ret;
822 }
823
4a200c3b
BT
824 return 0;
825}
826
0fe763c5 827static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
4a200c3b
BT
828{
829 struct i2c_client *client = ihid->client;
830 struct i2c_hid_desc *hdesc = &ihid->hdesc;
831 unsigned int dsize;
832 int ret;
833
f58b8487
AP
834 /* i2c hid fetch using a fixed descriptor size (30 bytes) */
835 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
836 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
837 sizeof(struct i2c_hid_desc));
4a200c3b 838 if (ret) {
f58b8487 839 dev_err(&client->dev, "hid_descr_cmd failed\n");
4a200c3b
BT
840 return -ENODEV;
841 }
842
f58b8487
AP
843 /* Validate the length of HID descriptor, the 4 first bytes:
844 * bytes 0-1 -> length
845 * bytes 2-3 -> bcdVersion (has to be 1.00) */
4a200c3b
BT
846 /* check bcdVersion == 1.0 */
847 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
848 dev_err(&client->dev,
9972dcc2 849 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
4a200c3b
BT
850 le16_to_cpu(hdesc->bcdVersion));
851 return -ENODEV;
852 }
853
f58b8487
AP
854 /* Descriptor length should be 30 bytes as per the specification */
855 dsize = le16_to_cpu(hdesc->wHIDDescLength);
856 if (dsize != sizeof(struct i2c_hid_desc)) {
857 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
858 dsize);
4a200c3b
BT
859 return -ENODEV;
860 }
4a200c3b 861 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
4a200c3b
BT
862 return 0;
863}
864
92241e67
MW
865#ifdef CONFIG_ACPI
866static int i2c_hid_acpi_pdata(struct i2c_client *client,
867 struct i2c_hid_platform_data *pdata)
868{
94116f81
AS
869 static guid_t i2c_hid_guid =
870 GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555,
871 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE);
ea547d7d 872 union acpi_object *obj;
92241e67
MW
873 struct acpi_device *adev;
874 acpi_handle handle;
875
876 handle = ACPI_HANDLE(&client->dev);
877 if (!handle || acpi_bus_get_device(handle, &adev))
878 return -ENODEV;
879
94116f81 880 obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL,
ea547d7d
JL
881 ACPI_TYPE_INTEGER);
882 if (!obj) {
92241e67
MW
883 dev_err(&client->dev, "device _DSM execution failed\n");
884 return -ENODEV;
885 }
886
ea547d7d
JL
887 pdata->hid_descriptor_address = obj->integer.value;
888 ACPI_FREE(obj);
92241e67 889
ba18a931 890 return 0;
92241e67
MW
891}
892
f3d3eab6
HG
893static void i2c_hid_acpi_fix_up_power(struct device *dev)
894{
895 acpi_handle handle = ACPI_HANDLE(dev);
896 struct acpi_device *adev;
897
898 if (handle && acpi_bus_get_device(handle, &adev) == 0)
899 acpi_device_fix_up_power(adev);
900}
901
92241e67
MW
902static const struct acpi_device_id i2c_hid_acpi_match[] = {
903 {"ACPI0C50", 0 },
904 {"PNP0C50", 0 },
905 { },
906};
907MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
908#else
909static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
910 struct i2c_hid_platform_data *pdata)
911{
912 return -ENODEV;
913}
f3d3eab6
HG
914
915static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {}
92241e67
MW
916#endif
917
3d7d248c
BT
918#ifdef CONFIG_OF
919static int i2c_hid_of_probe(struct i2c_client *client,
920 struct i2c_hid_platform_data *pdata)
921{
922 struct device *dev = &client->dev;
923 u32 val;
924 int ret;
925
926 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
927 if (ret) {
928 dev_err(&client->dev, "HID register address not provided\n");
929 return -ENODEV;
930 }
931 if (val >> 16) {
932 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
933 val);
934 return -EINVAL;
935 }
936 pdata->hid_descriptor_address = val;
937
572d3c64
BN
938 ret = of_property_read_u32(dev->of_node, "post-power-on-delay-ms",
939 &val);
940 if (!ret)
941 pdata->post_power_delay_ms = val;
942
3d7d248c
BT
943 return 0;
944}
945
946static const struct of_device_id i2c_hid_of_match[] = {
947 { .compatible = "hid-over-i2c" },
948 {},
949};
950MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
951#else
952static inline int i2c_hid_of_probe(struct i2c_client *client,
953 struct i2c_hid_platform_data *pdata)
954{
955 return -ENODEV;
956}
957#endif
958
0fe763c5
GKH
959static int i2c_hid_probe(struct i2c_client *client,
960 const struct i2c_device_id *dev_id)
4a200c3b
BT
961{
962 int ret;
963 struct i2c_hid *ihid;
964 struct hid_device *hid;
965 __u16 hidRegister;
966 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
967
968 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
969
ba18a931
DA
970 if (!client->irq) {
971 dev_err(&client->dev,
972 "HID over i2c has not been provided an Int IRQ\n");
973 return -EINVAL;
974 }
975
93d26aea
DA
976 if (client->irq < 0) {
977 if (client->irq != -EPROBE_DEFER)
978 dev_err(&client->dev,
979 "HID over i2c doesn't have a valid IRQ\n");
980 return client->irq;
981 }
982
4a200c3b
BT
983 ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
984 if (!ihid)
985 return -ENOMEM;
986
3d7d248c
BT
987 if (client->dev.of_node) {
988 ret = i2c_hid_of_probe(client, &ihid->pdata);
989 if (ret)
990 goto err;
991 } else if (!platform_data) {
92241e67
MW
992 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
993 if (ret) {
994 dev_err(&client->dev,
995 "HID register address not provided\n");
996 goto err;
997 }
998 } else {
999 ihid->pdata = *platform_data;
1000 }
1001
572d3c64
BN
1002 ihid->pdata.supply = devm_regulator_get(&client->dev, "vdd");
1003 if (IS_ERR(ihid->pdata.supply)) {
1004 ret = PTR_ERR(ihid->pdata.supply);
1005 if (ret != -EPROBE_DEFER)
1006 dev_err(&client->dev, "Failed to get regulator: %d\n",
1007 ret);
d3d9adfe 1008 goto err;
572d3c64
BN
1009 }
1010
1011 ret = regulator_enable(ihid->pdata.supply);
1012 if (ret < 0) {
1013 dev_err(&client->dev, "Failed to enable regulator: %d\n",
1014 ret);
1015 goto err;
1016 }
1017 if (ihid->pdata.post_power_delay_ms)
1018 msleep(ihid->pdata.post_power_delay_ms);
1019
4a200c3b
BT
1020 i2c_set_clientdata(client, ihid);
1021
1022 ihid->client = client;
1023
92241e67 1024 hidRegister = ihid->pdata.hid_descriptor_address;
4a200c3b
BT
1025 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1026
1027 init_waitqueue_head(&ihid->wait);
9a327405 1028 mutex_init(&ihid->reset_lock);
4a200c3b
BT
1029
1030 /* we need to allocate the command buffer without knowing the maximum
1031 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1032 * real computation later. */
29b45787
BT
1033 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1034 if (ret < 0)
572d3c64 1035 goto err_regulator;
4a200c3b 1036
f3d3eab6
HG
1037 i2c_hid_acpi_fix_up_power(&client->dev);
1038
34f439e4
MW
1039 pm_runtime_get_noresume(&client->dev);
1040 pm_runtime_set_active(&client->dev);
1041 pm_runtime_enable(&client->dev);
982e42d6 1042 device_enable_async_suspend(&client->dev);
34f439e4 1043
4a200c3b
BT
1044 ret = i2c_hid_fetch_hid_descriptor(ihid);
1045 if (ret < 0)
34f439e4 1046 goto err_pm;
4a200c3b
BT
1047
1048 ret = i2c_hid_init_irq(client);
1049 if (ret < 0)
34f439e4 1050 goto err_pm;
4a200c3b
BT
1051
1052 hid = hid_allocate_device();
1053 if (IS_ERR(hid)) {
1054 ret = PTR_ERR(hid);
8a1bbb53 1055 goto err_irq;
4a200c3b
BT
1056 }
1057
1058 ihid->hid = hid;
1059
1060 hid->driver_data = client;
1061 hid->ll_driver = &i2c_hid_ll_driver;
4a200c3b
BT
1062 hid->dev.parent = &client->dev;
1063 hid->bus = BUS_I2C;
1064 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1065 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1066 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1067
9972dcc2 1068 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
4a200c3b 1069 client->name, hid->vendor, hid->product);
f3984edc 1070 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
4a200c3b 1071
71af01a8
HC
1072 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1073
4a200c3b
BT
1074 ret = hid_add_device(hid);
1075 if (ret) {
1076 if (ret != -ENODEV)
1077 hid_err(client, "can't add hid device: %d\n", ret);
1078 goto err_mem_free;
1079 }
1080
34f439e4 1081 pm_runtime_put(&client->dev);
4a200c3b
BT
1082 return 0;
1083
1084err_mem_free:
1085 hid_destroy_device(hid);
1086
8a1bbb53 1087err_irq:
ba18a931 1088 free_irq(client->irq, ihid);
4a200c3b 1089
34f439e4
MW
1090err_pm:
1091 pm_runtime_put_noidle(&client->dev);
1092 pm_runtime_disable(&client->dev);
1093
572d3c64
BN
1094err_regulator:
1095 regulator_disable(ihid->pdata.supply);
1096
8a1bbb53 1097err:
3c626024 1098 i2c_hid_free_buffers(ihid);
4a200c3b
BT
1099 kfree(ihid);
1100 return ret;
1101}
1102
0fe763c5 1103static int i2c_hid_remove(struct i2c_client *client)
4a200c3b
BT
1104{
1105 struct i2c_hid *ihid = i2c_get_clientdata(client);
1106 struct hid_device *hid;
1107
34f439e4
MW
1108 pm_runtime_get_sync(&client->dev);
1109 pm_runtime_disable(&client->dev);
1110 pm_runtime_set_suspended(&client->dev);
1111 pm_runtime_put_noidle(&client->dev);
1112
4a200c3b
BT
1113 hid = ihid->hid;
1114 hid_destroy_device(hid);
1115
ba18a931 1116 free_irq(client->irq, ihid);
4a200c3b 1117
134ebfd8
BT
1118 if (ihid->bufsize)
1119 i2c_hid_free_buffers(ihid);
1120
572d3c64
BN
1121 regulator_disable(ihid->pdata.supply);
1122
4a200c3b
BT
1123 kfree(ihid);
1124
1125 return 0;
1126}
1127
d9f448e3
GZ
1128static void i2c_hid_shutdown(struct i2c_client *client)
1129{
1130 struct i2c_hid *ihid = i2c_get_clientdata(client);
1131
1132 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1133 free_irq(client->irq, ihid);
1134}
1135
4a200c3b
BT
1136#ifdef CONFIG_PM_SLEEP
1137static int i2c_hid_suspend(struct device *dev)
1138{
1139 struct i2c_client *client = to_i2c_client(dev);
109571cf
AD
1140 struct i2c_hid *ihid = i2c_get_clientdata(client);
1141 struct hid_device *hid = ihid->hid;
01714a6f 1142 int ret;
d1c48038 1143 int wake_status;
4a200c3b 1144
01714a6f
DA
1145 if (hid->driver && hid->driver->suspend) {
1146 /*
1147 * Wake up the device so that IO issues in
1148 * HID driver's suspend code can succeed.
1149 */
1150 ret = pm_runtime_resume(dev);
1151 if (ret < 0)
1152 return ret;
1153
af4739c2 1154 ret = hid->driver->suspend(hid, PMSG_SUSPEND);
01714a6f
DA
1155 if (ret < 0)
1156 return ret;
1157 }
1158
1159 if (!pm_runtime_suspended(dev)) {
1160 /* Save some power */
1161 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1162
ba18a931 1163 disable_irq(client->irq);
01714a6f 1164 }
af4739c2 1165
d1c48038 1166 if (device_may_wakeup(&client->dev)) {
ba18a931 1167 wake_status = enable_irq_wake(client->irq);
d1c48038
AD
1168 if (!wake_status)
1169 ihid->irq_wake_enabled = true;
1170 else
1171 hid_warn(hid, "Failed to enable irq wake: %d\n",
1172 wake_status);
572d3c64
BN
1173 } else {
1174 ret = regulator_disable(ihid->pdata.supply);
1175 if (ret < 0)
1176 hid_warn(hid, "Failed to disable supply: %d\n", ret);
d1c48038 1177 }
4a200c3b 1178
01714a6f 1179 return 0;
4a200c3b
BT
1180}
1181
1182static int i2c_hid_resume(struct device *dev)
1183{
1184 int ret;
1185 struct i2c_client *client = to_i2c_client(dev);
109571cf
AD
1186 struct i2c_hid *ihid = i2c_get_clientdata(client);
1187 struct hid_device *hid = ihid->hid;
d1c48038 1188 int wake_status;
4a200c3b 1189
572d3c64
BN
1190 if (!device_may_wakeup(&client->dev)) {
1191 ret = regulator_enable(ihid->pdata.supply);
1192 if (ret < 0)
1193 hid_warn(hid, "Failed to enable supply: %d\n", ret);
1194 if (ihid->pdata.post_power_delay_ms)
1195 msleep(ihid->pdata.post_power_delay_ms);
1196 } else if (ihid->irq_wake_enabled) {
ba18a931 1197 wake_status = disable_irq_wake(client->irq);
d1c48038
AD
1198 if (!wake_status)
1199 ihid->irq_wake_enabled = false;
1200 else
1201 hid_warn(hid, "Failed to disable irq wake: %d\n",
1202 wake_status);
1203 }
4a200c3b 1204
01714a6f
DA
1205 /* We'll resume to full power */
1206 pm_runtime_disable(dev);
1207 pm_runtime_set_active(dev);
1208 pm_runtime_enable(dev);
1209
ba18a931 1210 enable_irq(client->irq);
01714a6f
DA
1211 ret = i2c_hid_hwreset(client);
1212 if (ret)
1213 return ret;
1214
109571cf
AD
1215 if (hid->driver && hid->driver->reset_resume) {
1216 ret = hid->driver->reset_resume(hid);
1217 return ret;
1218 }
1219
4a200c3b
BT
1220 return 0;
1221}
1222#endif
1223
721564a9 1224#ifdef CONFIG_PM
34f439e4
MW
1225static int i2c_hid_runtime_suspend(struct device *dev)
1226{
1227 struct i2c_client *client = to_i2c_client(dev);
1228
1229 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
ba18a931 1230 disable_irq(client->irq);
34f439e4
MW
1231 return 0;
1232}
1233
1234static int i2c_hid_runtime_resume(struct device *dev)
1235{
1236 struct i2c_client *client = to_i2c_client(dev);
1237
ba18a931 1238 enable_irq(client->irq);
34f439e4
MW
1239 i2c_hid_set_power(client, I2C_HID_PWR_ON);
1240 return 0;
1241}
1242#endif
1243
1244static const struct dev_pm_ops i2c_hid_pm = {
1245 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume)
1246 SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume,
1247 NULL)
1248};
4a200c3b
BT
1249
1250static const struct i2c_device_id i2c_hid_id_table[] = {
24ebb37e 1251 { "hid", 0 },
1dcdde98 1252 { "hid-over-i2c", 0 },
4a200c3b
BT
1253 { },
1254};
1255MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1256
1257
1258static struct i2c_driver i2c_hid_driver = {
1259 .driver = {
1260 .name = "i2c_hid",
4a200c3b 1261 .pm = &i2c_hid_pm,
92241e67 1262 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
3d7d248c 1263 .of_match_table = of_match_ptr(i2c_hid_of_match),
4a200c3b
BT
1264 },
1265
1266 .probe = i2c_hid_probe,
0fe763c5 1267 .remove = i2c_hid_remove,
d9f448e3 1268 .shutdown = i2c_hid_shutdown,
4a200c3b
BT
1269 .id_table = i2c_hid_id_table,
1270};
1271
1272module_i2c_driver(i2c_hid_driver);
1273
1274MODULE_DESCRIPTION("HID over I2C core driver");
1275MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1276MODULE_LICENSE("GPL");