blob: b48f49d1f0dd7e3dee438a58f85da5172fd97464 [file] [log] [blame]
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001/*
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>
25#include <linux/delay.h>
26#include <linux/slab.h>
27#include <linux/pm.h>
28#include <linux/device.h>
29#include <linux/wait.h>
30#include <linux/err.h>
31#include <linux/string.h>
32#include <linux/list.h>
33#include <linux/jiffies.h>
34#include <linux/kernel.h>
Benjamin Tissoires4a200c32012-11-12 15:42:59 +010035#include <linux/hid.h>
Benjamin Tissoires7a7d6d92012-12-12 18:00:02 +010036#include <linux/mutex.h>
Mika Westerberg92241e62013-01-09 16:43:08 +020037#include <linux/acpi.h>
Benjamin Tissoires3d7d2482013-06-13 09:50:35 +020038#include <linux/of.h>
Benjamin Tissoires4a200c32012-11-12 15:42:59 +010039
40#include <linux/i2c/i2c-hid.h>
41
42/* flags */
43#define I2C_HID_STARTED (1 << 0)
44#define I2C_HID_RESET_PENDING (1 << 1)
45#define I2C_HID_READ_PENDING (1 << 2)
46
47#define I2C_HID_PWR_ON 0x00
48#define I2C_HID_PWR_SLEEP 0x01
49
50/* debug option */
Benjamin Tissoiresee8e8802012-12-04 16:27:45 +010051static bool debug;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +010052module_param(debug, bool, 0444);
53MODULE_PARM_DESC(debug, "print a lot of debug information");
54
Benjamin Tissoiresfa7386442012-12-04 16:27:46 +010055#define i2c_hid_dbg(ihid, fmt, arg...) \
56do { \
57 if (debug) \
58 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
59} while (0)
Benjamin Tissoires4a200c32012-11-12 15:42:59 +010060
61struct i2c_hid_desc {
62 __le16 wHIDDescLength;
63 __le16 bcdVersion;
64 __le16 wReportDescLength;
65 __le16 wReportDescRegister;
66 __le16 wInputRegister;
67 __le16 wMaxInputLength;
68 __le16 wOutputRegister;
69 __le16 wMaxOutputLength;
70 __le16 wCommandRegister;
71 __le16 wDataRegister;
72 __le16 wVendorID;
73 __le16 wProductID;
74 __le16 wVersionID;
Benjamin Tissoires27174cf2012-12-05 15:02:53 +010075 __le32 reserved;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +010076} __packed;
77
78struct i2c_hid_cmd {
79 unsigned int registerIndex;
80 __u8 opcode;
81 unsigned int length;
82 bool wait;
83};
84
85union command {
86 u8 data[0];
87 struct cmd {
88 __le16 reg;
89 __u8 reportTypeID;
90 __u8 opcode;
91 } __packed c;
92};
93
94#define I2C_HID_CMD(opcode_) \
95 .opcode = opcode_, .length = 4, \
96 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
97
98/* fetch HID descriptor */
99static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
100/* fetch report descriptors */
101static const struct i2c_hid_cmd hid_report_descr_cmd = {
102 .registerIndex = offsetof(struct i2c_hid_desc,
103 wReportDescRegister),
104 .opcode = 0x00,
105 .length = 2 };
106/* commands */
107static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
108 .wait = true };
109static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
110static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100111static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
Andrew Duggan811adb92013-06-17 16:15:06 -0700112static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 };
Benjamin Tissoires6bf6c8b2012-12-04 16:27:47 +0100113
114/*
115 * These definitions are not used here, but are defined by the spec.
116 * Keeping them here for documentation purposes.
117 *
118 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
119 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
120 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
121 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
122 */
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100123
Benjamin Tissoires7a7d6d92012-12-12 18:00:02 +0100124static DEFINE_MUTEX(i2c_hid_open_mut);
125
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100126/* The main device structure */
127struct i2c_hid {
128 struct i2c_client *client; /* i2c client */
129 struct hid_device *hid; /* pointer to corresponding HID dev */
130 union {
131 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
132 struct i2c_hid_desc hdesc; /* the HID Descriptor */
133 };
134 __le16 wHIDDescRegister; /* location of the i2c
135 * register of the HID
136 * descriptor. */
137 unsigned int bufsize; /* i2c buffer size */
138 char *inbuf; /* Input buffer */
139 char *cmdbuf; /* Command buffer */
140 char *argsbuf; /* Command arguments buffer */
141
142 unsigned long flags; /* device flags */
143
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100144 wait_queue_head_t wait; /* For waiting the interrupt */
Mika Westerberg92241e62013-01-09 16:43:08 +0200145
146 struct i2c_hid_platform_data pdata;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100147};
148
149static int __i2c_hid_command(struct i2c_client *client,
150 const struct i2c_hid_cmd *command, u8 reportID,
151 u8 reportType, u8 *args, int args_len,
152 unsigned char *buf_recv, int data_len)
153{
154 struct i2c_hid *ihid = i2c_get_clientdata(client);
155 union command *cmd = (union command *)ihid->cmdbuf;
156 int ret;
157 struct i2c_msg msg[2];
158 int msg_num = 1;
159
160 int length = command->length;
161 bool wait = command->wait;
162 unsigned int registerIndex = command->registerIndex;
163
164 /* special case for hid_descr_cmd */
165 if (command == &hid_descr_cmd) {
166 cmd->c.reg = ihid->wHIDDescRegister;
167 } else {
168 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
169 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
170 }
171
172 if (length > 2) {
173 cmd->c.opcode = command->opcode;
174 cmd->c.reportTypeID = reportID | reportType << 4;
175 }
176
177 memcpy(cmd->data + length, args, args_len);
178 length += args_len;
179
180 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
181
182 msg[0].addr = client->addr;
183 msg[0].flags = client->flags & I2C_M_TEN;
184 msg[0].len = length;
185 msg[0].buf = cmd->data;
186 if (data_len > 0) {
187 msg[1].addr = client->addr;
188 msg[1].flags = client->flags & I2C_M_TEN;
189 msg[1].flags |= I2C_M_RD;
190 msg[1].len = data_len;
191 msg[1].buf = buf_recv;
192 msg_num = 2;
193 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
194 }
195
196 if (wait)
197 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
198
199 ret = i2c_transfer(client->adapter, msg, msg_num);
200
201 if (data_len > 0)
202 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
203
204 if (ret != msg_num)
205 return ret < 0 ? ret : -EIO;
206
207 ret = 0;
208
209 if (wait) {
210 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
211 if (!wait_event_timeout(ihid->wait,
212 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
213 msecs_to_jiffies(5000)))
214 ret = -ENODATA;
215 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
216 }
217
218 return ret;
219}
220
221static int i2c_hid_command(struct i2c_client *client,
222 const struct i2c_hid_cmd *command,
223 unsigned char *buf_recv, int data_len)
224{
225 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
226 buf_recv, data_len);
227}
228
229static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
230 u8 reportID, unsigned char *buf_recv, int data_len)
231{
232 struct i2c_hid *ihid = i2c_get_clientdata(client);
233 u8 args[3];
234 int ret;
235 int args_len = 0;
236 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
237
238 i2c_hid_dbg(ihid, "%s\n", __func__);
239
240 if (reportID >= 0x0F) {
241 args[args_len++] = reportID;
242 reportID = 0x0F;
243 }
244
245 args[args_len++] = readRegister & 0xFF;
246 args[args_len++] = readRegister >> 8;
247
248 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
249 reportType, args, args_len, buf_recv, data_len);
250 if (ret) {
251 dev_err(&client->dev,
252 "failed to retrieve report from device.\n");
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100253 return ret;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100254 }
255
256 return 0;
257}
258
Benjamin Tissoires9b5a9ae2014-02-10 12:58:49 -0500259/**
260 * i2c_hid_set_or_send_report: forward an incoming report to the device
261 * @client: the i2c_client of the device
262 * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT
263 * @reportID: the report ID
264 * @buf: the actual data to transfer, without the report ID
265 * @len: size of buf
266 * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report
267 */
268static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType,
269 u8 reportID, unsigned char *buf, size_t data_len, bool use_data)
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100270{
271 struct i2c_hid *ihid = i2c_get_clientdata(client);
272 u8 *args = ihid->argsbuf;
Benjamin Tissoires9b5a9ae2014-02-10 12:58:49 -0500273 const struct i2c_hid_cmd *hidcmd;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100274 int ret;
275 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
Andrew Duggan811adb92013-06-17 16:15:06 -0700276 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
277 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100278
279 /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */
280 u16 size = 2 /* size */ +
281 (reportID ? 1 : 0) /* reportID */ +
282 data_len /* buf */;
283 int args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
284 2 /* dataRegister */ +
285 size /* args */;
286 int index = 0;
287
288 i2c_hid_dbg(ihid, "%s\n", __func__);
289
Benjamin Tissoires9b5a9ae2014-02-10 12:58:49 -0500290 if (!use_data && maxOutputLength == 0)
291 return -ENOSYS;
292
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100293 if (reportID >= 0x0F) {
294 args[index++] = reportID;
295 reportID = 0x0F;
296 }
297
Andrew Duggan811adb92013-06-17 16:15:06 -0700298 /*
299 * use the data register for feature reports or if the device does not
300 * support the output register
301 */
Benjamin Tissoires9b5a9ae2014-02-10 12:58:49 -0500302 if (use_data) {
Andrew Duggan811adb92013-06-17 16:15:06 -0700303 args[index++] = dataRegister & 0xFF;
304 args[index++] = dataRegister >> 8;
Benjamin Tissoires9b5a9ae2014-02-10 12:58:49 -0500305 hidcmd = &hid_set_report_cmd;
Andrew Duggan811adb92013-06-17 16:15:06 -0700306 } else {
307 args[index++] = outputRegister & 0xFF;
308 args[index++] = outputRegister >> 8;
309 hidcmd = &hid_no_cmd;
310 }
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100311
312 args[index++] = size & 0xFF;
313 args[index++] = size >> 8;
314
315 if (reportID)
316 args[index++] = reportID;
317
318 memcpy(&args[index], buf, data_len);
319
Andrew Duggan811adb92013-06-17 16:15:06 -0700320 ret = __i2c_hid_command(client, hidcmd, reportID,
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100321 reportType, args, args_len, NULL, 0);
322 if (ret) {
323 dev_err(&client->dev, "failed to set a report to device.\n");
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100324 return ret;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100325 }
326
327 return data_len;
328}
329
330static int i2c_hid_set_power(struct i2c_client *client, int power_state)
331{
332 struct i2c_hid *ihid = i2c_get_clientdata(client);
333 int ret;
334
335 i2c_hid_dbg(ihid, "%s\n", __func__);
336
337 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
338 0, NULL, 0, NULL, 0);
339 if (ret)
340 dev_err(&client->dev, "failed to change power setting.\n");
341
342 return ret;
343}
344
345static int i2c_hid_hwreset(struct i2c_client *client)
346{
347 struct i2c_hid *ihid = i2c_get_clientdata(client);
348 int ret;
349
350 i2c_hid_dbg(ihid, "%s\n", __func__);
351
352 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
353 if (ret)
354 return ret;
355
356 i2c_hid_dbg(ihid, "resetting...\n");
357
358 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
359 if (ret) {
360 dev_err(&client->dev, "failed to reset device.\n");
361 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
362 return ret;
363 }
364
365 return 0;
366}
367
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100368static void i2c_hid_get_input(struct i2c_hid *ihid)
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100369{
370 int ret, ret_size;
371 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
372
373 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
374 if (ret != size) {
375 if (ret < 0)
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100376 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100377
378 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
379 __func__, ret, size);
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100380 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100381 }
382
383 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
384
385 if (!ret_size) {
386 /* host or device initiated RESET completed */
387 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
388 wake_up(&ihid->wait);
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100389 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100390 }
391
392 if (ret_size > size) {
393 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
394 __func__, size, ret_size);
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100395 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100396 }
397
398 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
399
400 if (test_bit(I2C_HID_STARTED, &ihid->flags))
401 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
402 ret_size - 2, 1);
403
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100404 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100405}
406
407static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
408{
409 struct i2c_hid *ihid = dev_id;
410
411 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
412 return IRQ_HANDLED;
413
414 i2c_hid_get_input(ihid);
415
416 return IRQ_HANDLED;
417}
418
419static int i2c_hid_get_report_length(struct hid_report *report)
420{
421 return ((report->size - 1) >> 3) + 1 +
422 report->device->report_enum[report->type].numbered + 2;
423}
424
425static void i2c_hid_init_report(struct hid_report *report, u8 *buffer,
426 size_t bufsize)
427{
428 struct hid_device *hid = report->device;
429 struct i2c_client *client = hid->driver_data;
430 struct i2c_hid *ihid = i2c_get_clientdata(client);
431 unsigned int size, ret_size;
432
433 size = i2c_hid_get_report_length(report);
Benjamin Tissoiresc737bcf2012-12-04 16:27:50 +0100434 if (i2c_hid_get_report(client,
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100435 report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
Benjamin Tissoiresc737bcf2012-12-04 16:27:50 +0100436 report->id, buffer, size))
437 return;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100438
439 i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, ihid->inbuf);
440
441 ret_size = buffer[0] | (buffer[1] << 8);
442
443 if (ret_size != size) {
444 dev_err(&client->dev, "error in %s size:%d / ret_size:%d\n",
445 __func__, size, ret_size);
446 return;
447 }
448
449 /* hid->driver_lock is held as we are in probe function,
450 * we just need to setup the input fields, so using
451 * hid_report_raw_event is safe. */
452 hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1);
453}
454
455/*
456 * Initialize all reports
457 */
458static void i2c_hid_init_reports(struct hid_device *hid)
459{
460 struct hid_report *report;
461 struct i2c_client *client = hid->driver_data;
462 struct i2c_hid *ihid = i2c_get_clientdata(client);
463 u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL);
464
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100465 if (!inbuf) {
466 dev_err(&client->dev, "can not retrieve initial reports\n");
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100467 return;
Benjamin Tissoires317b2042012-12-04 16:27:48 +0100468 }
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100469
470 list_for_each_entry(report,
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100471 &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
472 i2c_hid_init_report(report, inbuf, ihid->bufsize);
473
474 kfree(inbuf);
475}
476
477/*
478 * Traverse the supplied list of reports and find the longest
479 */
480static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
481 unsigned int *max)
482{
483 struct hid_report *report;
484 unsigned int size;
485
486 /* We should not rely on wMaxInputLength, as some devices may set it to
487 * a wrong length. */
488 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
489 size = i2c_hid_get_report_length(report);
490 if (*max < size)
491 *max = size;
492 }
493}
494
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100495static void i2c_hid_free_buffers(struct i2c_hid *ihid)
496{
497 kfree(ihid->inbuf);
498 kfree(ihid->argsbuf);
499 kfree(ihid->cmdbuf);
500 ihid->inbuf = NULL;
501 ihid->cmdbuf = NULL;
502 ihid->argsbuf = NULL;
Benjamin Tissoires29b45782012-12-05 15:02:54 +0100503 ihid->bufsize = 0;
504}
505
506static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
507{
508 /* the worst case is computed from the set_report command with a
509 * reportID > 15 and the maximum report length */
510 int args_len = sizeof(__u8) + /* optional ReportID byte */
511 sizeof(__u16) + /* data register */
512 sizeof(__u16) + /* size of the report */
513 report_size; /* report */
514
515 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
516 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
517 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
518
519 if (!ihid->inbuf || !ihid->argsbuf || !ihid->cmdbuf) {
520 i2c_hid_free_buffers(ihid);
521 return -ENOMEM;
522 }
523
524 ihid->bufsize = report_size;
525
526 return 0;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100527}
528
529static int i2c_hid_get_raw_report(struct hid_device *hid,
530 unsigned char report_number, __u8 *buf, size_t count,
531 unsigned char report_type)
532{
533 struct i2c_client *client = hid->driver_data;
534 struct i2c_hid *ihid = i2c_get_clientdata(client);
Benjamin Tissoirese5b50fe2012-12-05 15:02:56 +0100535 size_t ret_count, ask_count;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100536 int ret;
537
538 if (report_type == HID_OUTPUT_REPORT)
539 return -EINVAL;
540
Benjamin Tissoirese5b50fe2012-12-05 15:02:56 +0100541 /* +2 bytes to include the size of the reply in the query buffer */
542 ask_count = min(count + 2, (size_t)ihid->bufsize);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100543
544 ret = i2c_hid_get_report(client,
545 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
Benjamin Tissoirese5b50fe2012-12-05 15:02:56 +0100546 report_number, ihid->inbuf, ask_count);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100547
548 if (ret < 0)
549 return ret;
550
Benjamin Tissoirese5b50fe2012-12-05 15:02:56 +0100551 ret_count = ihid->inbuf[0] | (ihid->inbuf[1] << 8);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100552
Jiri Kosina9afd09a2012-12-06 10:59:28 +0100553 if (ret_count <= 2)
Benjamin Tissoirese5b50fe2012-12-05 15:02:56 +0100554 return 0;
555
556 ret_count = min(ret_count, ask_count);
557
558 /* The query buffer contains the size, dropping it in the reply */
559 count = min(count, ret_count - 2);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100560 memcpy(buf, ihid->inbuf + 2, count);
561
562 return count;
563}
564
565static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
Benjamin Tissoires9b5a9ae2014-02-10 12:58:49 -0500566 size_t count, unsigned char report_type, bool use_data)
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100567{
568 struct i2c_client *client = hid->driver_data;
569 int report_id = buf[0];
Benjamin Tissoiresc2849792013-01-31 17:50:02 +0100570 int ret;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100571
572 if (report_type == HID_INPUT_REPORT)
573 return -EINVAL;
574
Benjamin Tissoiresc2849792013-01-31 17:50:02 +0100575 if (report_id) {
576 buf++;
577 count--;
578 }
579
Benjamin Tissoires9b5a9ae2014-02-10 12:58:49 -0500580 ret = i2c_hid_set_or_send_report(client,
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100581 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
Benjamin Tissoires9b5a9ae2014-02-10 12:58:49 -0500582 report_id, buf, count, use_data);
Benjamin Tissoiresc2849792013-01-31 17:50:02 +0100583
584 if (report_id && ret >= 0)
585 ret++; /* add report_id to the number of transfered bytes */
586
587 return ret;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100588}
589
Benjamin Tissoires9b5a9ae2014-02-10 12:58:49 -0500590static int __i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
591 size_t count, unsigned char report_type)
592{
593 struct i2c_client *client = hid->driver_data;
594 struct i2c_hid *ihid = i2c_get_clientdata(client);
595 bool data = true; /* SET_REPORT */
596
597 if (report_type == HID_OUTPUT_REPORT)
598 data = le16_to_cpu(ihid->hdesc.wMaxOutputLength) == 0;
599
600 return i2c_hid_output_raw_report(hid, buf, count, report_type, data);
601}
602
603static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf,
604 size_t count)
605{
606 return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT,
607 false);
608}
609
610static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
611 __u8 *buf, size_t len, unsigned char rtype,
612 int reqtype)
613{
614 switch (reqtype) {
615 case HID_REQ_GET_REPORT:
616 return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype);
617 case HID_REQ_SET_REPORT:
618 if (buf[0] != reportnum)
619 return -EINVAL;
620 return i2c_hid_output_raw_report(hid, buf, len, rtype, true);
621 default:
622 return -EIO;
623 }
624}
625
Benjamin Tissoires545bef62013-02-25 11:31:50 +0100626static void i2c_hid_request(struct hid_device *hid, struct hid_report *rep,
627 int reqtype)
628{
629 struct i2c_client *client = hid->driver_data;
Benjamin Tissoires545bef62013-02-25 11:31:50 +0100630 char *buf;
631 int ret;
Huzefa Kankroliwala7c4d5772013-04-03 05:45:21 -0700632 int len = i2c_hid_get_report_length(rep) - 2;
Benjamin Tissoires545bef62013-02-25 11:31:50 +0100633
Huzefa Kankroliwala7c4d5772013-04-03 05:45:21 -0700634 buf = kzalloc(len, GFP_KERNEL);
Benjamin Tissoires545bef62013-02-25 11:31:50 +0100635 if (!buf)
636 return;
637
638 switch (reqtype) {
639 case HID_REQ_GET_REPORT:
Huzefa Kankroliwala7c4d5772013-04-03 05:45:21 -0700640 ret = i2c_hid_get_raw_report(hid, rep->id, buf, len, rep->type);
Benjamin Tissoires545bef62013-02-25 11:31:50 +0100641 if (ret < 0)
642 dev_err(&client->dev, "%s: unable to get report: %d\n",
643 __func__, ret);
644 else
645 hid_input_report(hid, rep->type, buf, ret, 0);
646 break;
647 case HID_REQ_SET_REPORT:
648 hid_output_report(rep, buf);
Benjamin Tissoires9b5a9ae2014-02-10 12:58:49 -0500649 i2c_hid_output_raw_report(hid, buf, len, rep->type, true);
Benjamin Tissoires545bef62013-02-25 11:31:50 +0100650 break;
651 }
652
653 kfree(buf);
654}
655
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100656static int i2c_hid_parse(struct hid_device *hid)
657{
658 struct i2c_client *client = hid->driver_data;
659 struct i2c_hid *ihid = i2c_get_clientdata(client);
660 struct i2c_hid_desc *hdesc = &ihid->hdesc;
661 unsigned int rsize;
662 char *rdesc;
663 int ret;
664 int tries = 3;
665
666 i2c_hid_dbg(ihid, "entering %s\n", __func__);
667
668 rsize = le16_to_cpu(hdesc->wReportDescLength);
669 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
670 dbg_hid("weird size of report descriptor (%u)\n", rsize);
671 return -EINVAL;
672 }
673
674 do {
675 ret = i2c_hid_hwreset(client);
676 if (ret)
677 msleep(1000);
678 } while (tries-- > 0 && ret);
679
680 if (ret)
681 return ret;
682
683 rdesc = kzalloc(rsize, GFP_KERNEL);
684
685 if (!rdesc) {
686 dbg_hid("couldn't allocate rdesc memory\n");
687 return -ENOMEM;
688 }
689
690 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
691
692 ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
693 if (ret) {
694 hid_err(hid, "reading report descriptor failed\n");
695 kfree(rdesc);
696 return -EIO;
697 }
698
699 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
700
701 ret = hid_parse_report(hid, rdesc, rsize);
702 kfree(rdesc);
703 if (ret) {
704 dbg_hid("parsing report descriptor failed\n");
705 return ret;
706 }
707
708 return 0;
709}
710
711static int i2c_hid_start(struct hid_device *hid)
712{
713 struct i2c_client *client = hid->driver_data;
714 struct i2c_hid *ihid = i2c_get_clientdata(client);
715 int ret;
Benjamin Tissoires29b45782012-12-05 15:02:54 +0100716 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100717
Benjamin Tissoires29b45782012-12-05 15:02:54 +0100718 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
719 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
720 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100721
Benjamin Tissoires29b45782012-12-05 15:02:54 +0100722 if (bufsize > ihid->bufsize) {
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100723 i2c_hid_free_buffers(ihid);
724
Benjamin Tissoires29b45782012-12-05 15:02:54 +0100725 ret = i2c_hid_alloc_buffers(ihid, bufsize);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100726
Benjamin Tissoires29b45782012-12-05 15:02:54 +0100727 if (ret)
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100728 return ret;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100729 }
730
731 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
732 i2c_hid_init_reports(hid);
733
734 return 0;
735}
736
737static void i2c_hid_stop(struct hid_device *hid)
738{
739 struct i2c_client *client = hid->driver_data;
740 struct i2c_hid *ihid = i2c_get_clientdata(client);
741
742 hid->claimed = 0;
743
744 i2c_hid_free_buffers(ihid);
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);
Benjamin Tissoires7a7d6d92012-12-12 18:00:02 +0100751 int ret = 0;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100752
Benjamin Tissoires7a7d6d92012-12-12 18:00:02 +0100753 mutex_lock(&i2c_hid_open_mut);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100754 if (!hid->open++) {
755 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
756 if (ret) {
757 hid->open--;
Benjamin Tissoires7a7d6d92012-12-12 18:00:02 +0100758 goto done;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100759 }
760 set_bit(I2C_HID_STARTED, &ihid->flags);
761 }
Benjamin Tissoires7a7d6d92012-12-12 18:00:02 +0100762done:
763 mutex_unlock(&i2c_hid_open_mut);
764 return ret;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100765}
766
767static void i2c_hid_close(struct hid_device *hid)
768{
769 struct i2c_client *client = hid->driver_data;
770 struct i2c_hid *ihid = i2c_get_clientdata(client);
771
772 /* protecting hid->open to make sure we don't restart
773 * data acquistion due to a resumption we no longer
774 * care about
775 */
Benjamin Tissoires7a7d6d92012-12-12 18:00:02 +0100776 mutex_lock(&i2c_hid_open_mut);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100777 if (!--hid->open) {
778 clear_bit(I2C_HID_STARTED, &ihid->flags);
779
780 /* Save some power */
781 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
782 }
Benjamin Tissoires7a7d6d92012-12-12 18:00:02 +0100783 mutex_unlock(&i2c_hid_open_mut);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100784}
785
786static int i2c_hid_power(struct hid_device *hid, int lvl)
787{
788 struct i2c_client *client = hid->driver_data;
789 struct i2c_hid *ihid = i2c_get_clientdata(client);
790 int ret = 0;
791
792 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
793
794 switch (lvl) {
795 case PM_HINT_FULLON:
796 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
797 break;
798 case PM_HINT_NORMAL:
799 ret = i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
800 break;
801 }
802 return ret;
803}
804
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100805static struct hid_ll_driver i2c_hid_ll_driver = {
806 .parse = i2c_hid_parse,
807 .start = i2c_hid_start,
808 .stop = i2c_hid_stop,
809 .open = i2c_hid_open,
810 .close = i2c_hid_close,
811 .power = i2c_hid_power,
Benjamin Tissoires545bef62013-02-25 11:31:50 +0100812 .request = i2c_hid_request,
Benjamin Tissoires9b5a9ae2014-02-10 12:58:49 -0500813 .output_report = i2c_hid_output_report,
814 .raw_request = i2c_hid_raw_request,
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100815};
816
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800817static int i2c_hid_init_irq(struct i2c_client *client)
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100818{
819 struct i2c_hid *ihid = i2c_get_clientdata(client);
820 int ret;
821
822 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
823
824 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
825 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
826 client->name, ihid);
827 if (ret < 0) {
Benjamin Tissoires9972dcc2012-12-04 16:27:49 +0100828 dev_warn(&client->dev,
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100829 "Could not register for %s interrupt, irq = %d,"
830 " ret = %d\n",
Benjamin Tissoires9972dcc2012-12-04 16:27:49 +0100831 client->name, client->irq, ret);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100832
833 return ret;
834 }
835
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100836 return 0;
837}
838
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800839static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100840{
841 struct i2c_client *client = ihid->client;
842 struct i2c_hid_desc *hdesc = &ihid->hdesc;
843 unsigned int dsize;
844 int ret;
845
846 /* Fetch the length of HID description, retrieve the 4 first bytes:
847 * bytes 0-1 -> length
848 * bytes 2-3 -> bcdVersion (has to be 1.00) */
849 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer, 4);
850
Andy Shevchenko4858bfe2013-08-02 14:07:15 +0300851 i2c_hid_dbg(ihid, "%s, ihid->hdesc_buffer: %4ph\n", __func__,
852 ihid->hdesc_buffer);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100853
854 if (ret) {
Benjamin Tissoires9972dcc2012-12-04 16:27:49 +0100855 dev_err(&client->dev,
856 "unable to fetch the size of HID descriptor (ret=%d)\n",
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100857 ret);
858 return -ENODEV;
859 }
860
861 dsize = le16_to_cpu(hdesc->wHIDDescLength);
Benjamin Tissoires27174cf2012-12-05 15:02:53 +0100862 /*
863 * the size of the HID descriptor should at least contain
864 * its size and the bcdVersion (4 bytes), and should not be greater
865 * than sizeof(struct i2c_hid_desc) as we directly fill this struct
866 * through i2c_hid_command.
867 */
868 if (dsize < 4 || dsize > sizeof(struct i2c_hid_desc)) {
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100869 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
870 dsize);
871 return -ENODEV;
872 }
873
874 /* check bcdVersion == 1.0 */
875 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
876 dev_err(&client->dev,
Benjamin Tissoires9972dcc2012-12-04 16:27:49 +0100877 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100878 le16_to_cpu(hdesc->bcdVersion));
879 return -ENODEV;
880 }
881
882 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
883
884 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
885 dsize);
886 if (ret) {
887 dev_err(&client->dev, "hid_descr_cmd Fail\n");
888 return -ENODEV;
889 }
890
891 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
892
893 return 0;
894}
895
Mika Westerberg92241e62013-01-09 16:43:08 +0200896#ifdef CONFIG_ACPI
897static int i2c_hid_acpi_pdata(struct i2c_client *client,
898 struct i2c_hid_platform_data *pdata)
899{
900 static u8 i2c_hid_guid[] = {
901 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
902 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
903 };
Zhang Rui74da2762013-09-03 08:32:11 +0800904 union acpi_object params[4];
Mika Westerberg92241e62013-01-09 16:43:08 +0200905 struct acpi_object_list input;
906 struct acpi_device *adev;
Zhang Rui74da2762013-09-03 08:32:11 +0800907 unsigned long long value;
Mika Westerberg92241e62013-01-09 16:43:08 +0200908 acpi_handle handle;
909
910 handle = ACPI_HANDLE(&client->dev);
911 if (!handle || acpi_bus_get_device(handle, &adev))
912 return -ENODEV;
913
914 input.count = ARRAY_SIZE(params);
915 input.pointer = params;
916
917 params[0].type = ACPI_TYPE_BUFFER;
918 params[0].buffer.length = sizeof(i2c_hid_guid);
919 params[0].buffer.pointer = i2c_hid_guid;
920 params[1].type = ACPI_TYPE_INTEGER;
921 params[1].integer.value = 1;
922 params[2].type = ACPI_TYPE_INTEGER;
923 params[2].integer.value = 1; /* HID function */
Mika Westerberg75ba8992013-08-19 14:01:17 +0300924 params[3].type = ACPI_TYPE_PACKAGE;
925 params[3].package.count = 0;
926 params[3].package.elements = NULL;
Mika Westerberg92241e62013-01-09 16:43:08 +0200927
Zhang Rui74da2762013-09-03 08:32:11 +0800928 if (ACPI_FAILURE(acpi_evaluate_integer(handle, "_DSM", &input,
929 &value))) {
Mika Westerberg92241e62013-01-09 16:43:08 +0200930 dev_err(&client->dev, "device _DSM execution failed\n");
931 return -ENODEV;
932 }
933
Zhang Rui74da2762013-09-03 08:32:11 +0800934 pdata->hid_descriptor_address = value;
Mika Westerberg92241e62013-01-09 16:43:08 +0200935
Mika Westerberg92241e62013-01-09 16:43:08 +0200936 return 0;
937}
938
939static const struct acpi_device_id i2c_hid_acpi_match[] = {
940 {"ACPI0C50", 0 },
941 {"PNP0C50", 0 },
942 { },
943};
944MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
945#else
946static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
947 struct i2c_hid_platform_data *pdata)
948{
949 return -ENODEV;
950}
951#endif
952
Benjamin Tissoires3d7d2482013-06-13 09:50:35 +0200953#ifdef CONFIG_OF
954static int i2c_hid_of_probe(struct i2c_client *client,
955 struct i2c_hid_platform_data *pdata)
956{
957 struct device *dev = &client->dev;
958 u32 val;
959 int ret;
960
961 ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
962 if (ret) {
963 dev_err(&client->dev, "HID register address not provided\n");
964 return -ENODEV;
965 }
966 if (val >> 16) {
967 dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
968 val);
969 return -EINVAL;
970 }
971 pdata->hid_descriptor_address = val;
972
973 return 0;
974}
975
976static const struct of_device_id i2c_hid_of_match[] = {
977 { .compatible = "hid-over-i2c" },
978 {},
979};
980MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
981#else
982static inline int i2c_hid_of_probe(struct i2c_client *client,
983 struct i2c_hid_platform_data *pdata)
984{
985 return -ENODEV;
986}
987#endif
988
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800989static int i2c_hid_probe(struct i2c_client *client,
990 const struct i2c_device_id *dev_id)
Benjamin Tissoires4a200c32012-11-12 15:42:59 +0100991{
992 int ret;
993 struct i2c_hid *ihid;
994 struct hid_device *hid;
995 __u16 hidRegister;
996 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
997
998 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
999
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001000 if (!client->irq) {
1001 dev_err(&client->dev,
1002 "HID over i2c has not been provided an Int IRQ\n");
1003 return -EINVAL;
1004 }
1005
1006 ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
1007 if (!ihid)
1008 return -ENOMEM;
1009
Benjamin Tissoires3d7d2482013-06-13 09:50:35 +02001010 if (client->dev.of_node) {
1011 ret = i2c_hid_of_probe(client, &ihid->pdata);
1012 if (ret)
1013 goto err;
1014 } else if (!platform_data) {
Mika Westerberg92241e62013-01-09 16:43:08 +02001015 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
1016 if (ret) {
1017 dev_err(&client->dev,
1018 "HID register address not provided\n");
1019 goto err;
1020 }
1021 } else {
1022 ihid->pdata = *platform_data;
1023 }
1024
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001025 i2c_set_clientdata(client, ihid);
1026
1027 ihid->client = client;
1028
Mika Westerberg92241e62013-01-09 16:43:08 +02001029 hidRegister = ihid->pdata.hid_descriptor_address;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001030 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
1031
1032 init_waitqueue_head(&ihid->wait);
1033
1034 /* we need to allocate the command buffer without knowing the maximum
1035 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
1036 * real computation later. */
Benjamin Tissoires29b45782012-12-05 15:02:54 +01001037 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
1038 if (ret < 0)
1039 goto err;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001040
1041 ret = i2c_hid_fetch_hid_descriptor(ihid);
1042 if (ret < 0)
1043 goto err;
1044
1045 ret = i2c_hid_init_irq(client);
1046 if (ret < 0)
1047 goto err;
1048
1049 hid = hid_allocate_device();
1050 if (IS_ERR(hid)) {
1051 ret = PTR_ERR(hid);
Benjamin Tissoires8a1bbb52012-12-05 15:02:55 +01001052 goto err_irq;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001053 }
1054
1055 ihid->hid = hid;
1056
1057 hid->driver_data = client;
1058 hid->ll_driver = &i2c_hid_ll_driver;
Benjamin Tissoires9b5a9ae2014-02-10 12:58:49 -05001059 hid->hid_output_raw_report = __i2c_hid_output_raw_report;
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001060 hid->dev.parent = &client->dev;
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001061 ACPI_COMPANION_SET(&hid->dev, ACPI_COMPANION(&client->dev));
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001062 hid->bus = BUS_I2C;
1063 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1064 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1065 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1066
Benjamin Tissoires9972dcc2012-12-04 16:27:49 +01001067 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001068 client->name, hid->vendor, hid->product);
1069
1070 ret = hid_add_device(hid);
1071 if (ret) {
1072 if (ret != -ENODEV)
1073 hid_err(client, "can't add hid device: %d\n", ret);
1074 goto err_mem_free;
1075 }
1076
1077 return 0;
1078
1079err_mem_free:
1080 hid_destroy_device(hid);
1081
Benjamin Tissoires8a1bbb52012-12-05 15:02:55 +01001082err_irq:
1083 free_irq(client->irq, ihid);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001084
Benjamin Tissoires8a1bbb52012-12-05 15:02:55 +01001085err:
Jiri Kosina3c626022012-11-20 17:09:40 +01001086 i2c_hid_free_buffers(ihid);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001087 kfree(ihid);
1088 return ret;
1089}
1090
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -08001091static int i2c_hid_remove(struct i2c_client *client)
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001092{
1093 struct i2c_hid *ihid = i2c_get_clientdata(client);
1094 struct hid_device *hid;
1095
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001096 hid = ihid->hid;
1097 hid_destroy_device(hid);
1098
1099 free_irq(client->irq, ihid);
1100
Benjamin Tissoires134ebfd2012-12-04 16:27:54 +01001101 if (ihid->bufsize)
1102 i2c_hid_free_buffers(ihid);
1103
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001104 kfree(ihid);
1105
1106 return 0;
1107}
1108
1109#ifdef CONFIG_PM_SLEEP
1110static int i2c_hid_suspend(struct device *dev)
1111{
1112 struct i2c_client *client = to_i2c_client(dev);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001113
Mika Westerberg94037ef2013-11-13 13:34:18 +02001114 disable_irq(client->irq);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001115 if (device_may_wakeup(&client->dev))
Benjamin Tissoires8a1bbb52012-12-05 15:02:55 +01001116 enable_irq_wake(client->irq);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001117
1118 /* Save some power */
1119 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1120
1121 return 0;
1122}
1123
1124static int i2c_hid_resume(struct device *dev)
1125{
1126 int ret;
1127 struct i2c_client *client = to_i2c_client(dev);
1128
Mika Westerberg94037ef2013-11-13 13:34:18 +02001129 enable_irq(client->irq);
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001130 ret = i2c_hid_hwreset(client);
1131 if (ret)
1132 return ret;
1133
1134 if (device_may_wakeup(&client->dev))
1135 disable_irq_wake(client->irq);
1136
1137 return 0;
1138}
1139#endif
1140
1141static SIMPLE_DEV_PM_OPS(i2c_hid_pm, i2c_hid_suspend, i2c_hid_resume);
1142
1143static const struct i2c_device_id i2c_hid_id_table[] = {
Benjamin Tissoires24ebb372012-12-04 16:27:42 +01001144 { "hid", 0 },
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001145 { },
1146};
1147MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1148
1149
1150static struct i2c_driver i2c_hid_driver = {
1151 .driver = {
1152 .name = "i2c_hid",
1153 .owner = THIS_MODULE,
1154 .pm = &i2c_hid_pm,
Mika Westerberg92241e62013-01-09 16:43:08 +02001155 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
Benjamin Tissoires3d7d2482013-06-13 09:50:35 +02001156 .of_match_table = of_match_ptr(i2c_hid_of_match),
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001157 },
1158
1159 .probe = i2c_hid_probe,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -08001160 .remove = i2c_hid_remove,
Benjamin Tissoires4a200c32012-11-12 15:42:59 +01001161
1162 .id_table = i2c_hid_id_table,
1163};
1164
1165module_i2c_driver(i2c_hid_driver);
1166
1167MODULE_DESCRIPTION("HID over I2C core driver");
1168MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1169MODULE_LICENSE("GPL");