Merge tag 'v3.10.107' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
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>
4a200c3b 35#include <linux/hid.h>
7a7d6d9c 36#include <linux/mutex.h>
92241e67 37#include <linux/acpi.h>
4a200c3b
BT
38
39#include <linux/i2c/i2c-hid.h>
40
41/* flags */
42#define I2C_HID_STARTED (1 << 0)
43#define I2C_HID_RESET_PENDING (1 << 1)
44#define I2C_HID_READ_PENDING (1 << 2)
45
46#define I2C_HID_PWR_ON 0x00
47#define I2C_HID_PWR_SLEEP 0x01
48
49/* debug option */
ee8e8806 50static bool debug;
4a200c3b
BT
51module_param(debug, bool, 0444);
52MODULE_PARM_DESC(debug, "print a lot of debug information");
53
fa738644
BT
54#define i2c_hid_dbg(ihid, fmt, arg...) \
55do { \
56 if (debug) \
57 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
58} while (0)
4a200c3b
BT
59
60struct i2c_hid_desc {
61 __le16 wHIDDescLength;
62 __le16 bcdVersion;
63 __le16 wReportDescLength;
64 __le16 wReportDescRegister;
65 __le16 wInputRegister;
66 __le16 wMaxInputLength;
67 __le16 wOutputRegister;
68 __le16 wMaxOutputLength;
69 __le16 wCommandRegister;
70 __le16 wDataRegister;
71 __le16 wVendorID;
72 __le16 wProductID;
73 __le16 wVersionID;
27174cff 74 __le32 reserved;
4a200c3b
BT
75} __packed;
76
77struct i2c_hid_cmd {
78 unsigned int registerIndex;
79 __u8 opcode;
80 unsigned int length;
81 bool wait;
82};
83
84union command {
85 u8 data[0];
86 struct cmd {
87 __le16 reg;
88 __u8 reportTypeID;
89 __u8 opcode;
90 } __packed c;
91};
92
93#define I2C_HID_CMD(opcode_) \
94 .opcode = opcode_, .length = 4, \
95 .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister)
96
97/* fetch HID descriptor */
98static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 };
99/* fetch report descriptors */
100static const struct i2c_hid_cmd hid_report_descr_cmd = {
101 .registerIndex = offsetof(struct i2c_hid_desc,
102 wReportDescRegister),
103 .opcode = 0x00,
104 .length = 2 };
105/* commands */
106static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
107 .wait = true };
108static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
109static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
4a200c3b 110static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
6bf6c8bf
BT
111
112/*
113 * These definitions are not used here, but are defined by the spec.
114 * Keeping them here for documentation purposes.
115 *
116 * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) };
117 * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) };
118 * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) };
119 * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
120 */
4a200c3b 121
7a7d6d9c
BT
122static DEFINE_MUTEX(i2c_hid_open_mut);
123
4a200c3b
BT
124/* The main device structure */
125struct i2c_hid {
126 struct i2c_client *client; /* i2c client */
127 struct hid_device *hid; /* pointer to corresponding HID dev */
128 union {
129 __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)];
130 struct i2c_hid_desc hdesc; /* the HID Descriptor */
131 };
132 __le16 wHIDDescRegister; /* location of the i2c
133 * register of the HID
134 * descriptor. */
135 unsigned int bufsize; /* i2c buffer size */
136 char *inbuf; /* Input buffer */
caa853b3 137 char *rawbuf; /* Raw Input buffer */
4a200c3b
BT
138 char *cmdbuf; /* Command buffer */
139 char *argsbuf; /* Command arguments buffer */
140
141 unsigned long flags; /* device flags */
142
4a200c3b 143 wait_queue_head_t wait; /* For waiting the interrupt */
92241e67
MW
144
145 struct i2c_hid_platform_data pdata;
4a200c3b
BT
146};
147
148static int __i2c_hid_command(struct i2c_client *client,
149 const struct i2c_hid_cmd *command, u8 reportID,
150 u8 reportType, u8 *args, int args_len,
151 unsigned char *buf_recv, int data_len)
152{
153 struct i2c_hid *ihid = i2c_get_clientdata(client);
154 union command *cmd = (union command *)ihid->cmdbuf;
155 int ret;
156 struct i2c_msg msg[2];
157 int msg_num = 1;
158
159 int length = command->length;
160 bool wait = command->wait;
161 unsigned int registerIndex = command->registerIndex;
162
163 /* special case for hid_descr_cmd */
164 if (command == &hid_descr_cmd) {
165 cmd->c.reg = ihid->wHIDDescRegister;
166 } else {
167 cmd->data[0] = ihid->hdesc_buffer[registerIndex];
168 cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1];
169 }
170
171 if (length > 2) {
172 cmd->c.opcode = command->opcode;
173 cmd->c.reportTypeID = reportID | reportType << 4;
174 }
175
176 memcpy(cmd->data + length, args, args_len);
177 length += args_len;
178
179 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data);
180
181 msg[0].addr = client->addr;
182 msg[0].flags = client->flags & I2C_M_TEN;
183 msg[0].len = length;
184 msg[0].buf = cmd->data;
185 if (data_len > 0) {
186 msg[1].addr = client->addr;
187 msg[1].flags = client->flags & I2C_M_TEN;
188 msg[1].flags |= I2C_M_RD;
189 msg[1].len = data_len;
190 msg[1].buf = buf_recv;
191 msg_num = 2;
192 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
193 }
194
195 if (wait)
196 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
197
198 ret = i2c_transfer(client->adapter, msg, msg_num);
199
200 if (data_len > 0)
201 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
202
203 if (ret != msg_num)
204 return ret < 0 ? ret : -EIO;
205
206 ret = 0;
207
208 if (wait) {
209 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
210 if (!wait_event_timeout(ihid->wait,
211 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
212 msecs_to_jiffies(5000)))
213 ret = -ENODATA;
214 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
215 }
216
217 return ret;
218}
219
220static int i2c_hid_command(struct i2c_client *client,
221 const struct i2c_hid_cmd *command,
222 unsigned char *buf_recv, int data_len)
223{
224 return __i2c_hid_command(client, command, 0, 0, NULL, 0,
225 buf_recv, data_len);
226}
227
228static int i2c_hid_get_report(struct i2c_client *client, u8 reportType,
229 u8 reportID, unsigned char *buf_recv, int data_len)
230{
231 struct i2c_hid *ihid = i2c_get_clientdata(client);
232 u8 args[3];
233 int ret;
234 int args_len = 0;
235 u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
236
237 i2c_hid_dbg(ihid, "%s\n", __func__);
238
239 if (reportID >= 0x0F) {
240 args[args_len++] = reportID;
241 reportID = 0x0F;
242 }
243
244 args[args_len++] = readRegister & 0xFF;
245 args[args_len++] = readRegister >> 8;
246
247 ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID,
248 reportType, args, args_len, buf_recv, data_len);
249 if (ret) {
250 dev_err(&client->dev,
251 "failed to retrieve report from device.\n");
317b204a 252 return ret;
4a200c3b
BT
253 }
254
255 return 0;
256}
257
258static int i2c_hid_set_report(struct i2c_client *client, u8 reportType,
259 u8 reportID, unsigned char *buf, size_t data_len)
260{
261 struct i2c_hid *ihid = i2c_get_clientdata(client);
262 u8 *args = ihid->argsbuf;
263 int ret;
264 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
265
266 /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */
267 u16 size = 2 /* size */ +
268 (reportID ? 1 : 0) /* reportID */ +
269 data_len /* buf */;
270 int args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ +
271 2 /* dataRegister */ +
272 size /* args */;
273 int index = 0;
274
275 i2c_hid_dbg(ihid, "%s\n", __func__);
276
277 if (reportID >= 0x0F) {
278 args[index++] = reportID;
279 reportID = 0x0F;
280 }
281
282 args[index++] = dataRegister & 0xFF;
283 args[index++] = dataRegister >> 8;
284
285 args[index++] = size & 0xFF;
286 args[index++] = size >> 8;
287
288 if (reportID)
289 args[index++] = reportID;
290
291 memcpy(&args[index], buf, data_len);
292
293 ret = __i2c_hid_command(client, &hid_set_report_cmd, reportID,
294 reportType, args, args_len, NULL, 0);
295 if (ret) {
296 dev_err(&client->dev, "failed to set a report to device.\n");
317b204a 297 return ret;
4a200c3b
BT
298 }
299
300 return data_len;
301}
302
303static int i2c_hid_set_power(struct i2c_client *client, int power_state)
304{
305 struct i2c_hid *ihid = i2c_get_clientdata(client);
306 int ret;
307
308 i2c_hid_dbg(ihid, "%s\n", __func__);
309
310 ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state,
311 0, NULL, 0, NULL, 0);
312 if (ret)
313 dev_err(&client->dev, "failed to change power setting.\n");
314
315 return ret;
316}
317
318static int i2c_hid_hwreset(struct i2c_client *client)
319{
320 struct i2c_hid *ihid = i2c_get_clientdata(client);
321 int ret;
322
323 i2c_hid_dbg(ihid, "%s\n", __func__);
324
325 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
326 if (ret)
327 return ret;
328
02f1f3f6
BM
329 /*
330 * The HID over I2C specification states that if a DEVICE needs time
331 * after the PWR_ON request, it should utilise CLOCK stretching.
332 * However, it has been observered that the Windows driver provides a
333 * 1ms sleep between the PWR_ON and RESET requests and that some devices
334 * rely on this.
335 */
336 usleep_range(1000, 5000);
337
4a200c3b
BT
338 i2c_hid_dbg(ihid, "resetting...\n");
339
340 ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0);
341 if (ret) {
342 dev_err(&client->dev, "failed to reset device.\n");
343 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
344 return ret;
345 }
346
347 return 0;
348}
349
317b204a 350static void i2c_hid_get_input(struct i2c_hid *ihid)
4a200c3b
BT
351{
352 int ret, ret_size;
de13322c
SF
353 int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
354
355 if (size > ihid->bufsize)
356 size = ihid->bufsize;
4a200c3b
BT
357
358 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
359 if (ret != size) {
360 if (ret < 0)
317b204a 361 return;
4a200c3b
BT
362
363 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
364 __func__, ret, size);
317b204a 365 return;
4a200c3b
BT
366 }
367
368 ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
369
370 if (!ret_size) {
371 /* host or device initiated RESET completed */
372 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
373 wake_up(&ihid->wait);
317b204a 374 return;
4a200c3b
BT
375 }
376
377 if (ret_size > size) {
378 dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
379 __func__, size, ret_size);
317b204a 380 return;
4a200c3b
BT
381 }
382
383 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
384
385 if (test_bit(I2C_HID_STARTED, &ihid->flags))
386 hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
387 ret_size - 2, 1);
388
317b204a 389 return;
4a200c3b
BT
390}
391
392static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
393{
394 struct i2c_hid *ihid = dev_id;
395
396 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
397 return IRQ_HANDLED;
398
399 i2c_hid_get_input(ihid);
400
401 return IRQ_HANDLED;
402}
403
404static int i2c_hid_get_report_length(struct hid_report *report)
405{
406 return ((report->size - 1) >> 3) + 1 +
407 report->device->report_enum[report->type].numbered + 2;
408}
409
410static void i2c_hid_init_report(struct hid_report *report, u8 *buffer,
411 size_t bufsize)
412{
413 struct hid_device *hid = report->device;
414 struct i2c_client *client = hid->driver_data;
415 struct i2c_hid *ihid = i2c_get_clientdata(client);
416 unsigned int size, ret_size;
417
418 size = i2c_hid_get_report_length(report);
c737bcf9 419 if (i2c_hid_get_report(client,
4a200c3b 420 report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
c737bcf9
BT
421 report->id, buffer, size))
422 return;
4a200c3b
BT
423
424 i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, ihid->inbuf);
425
426 ret_size = buffer[0] | (buffer[1] << 8);
427
428 if (ret_size != size) {
429 dev_err(&client->dev, "error in %s size:%d / ret_size:%d\n",
430 __func__, size, ret_size);
431 return;
432 }
433
434 /* hid->driver_lock is held as we are in probe function,
435 * we just need to setup the input fields, so using
436 * hid_report_raw_event is safe. */
437 hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1);
438}
439
440/*
441 * Initialize all reports
442 */
443static void i2c_hid_init_reports(struct hid_device *hid)
444{
445 struct hid_report *report;
446 struct i2c_client *client = hid->driver_data;
447 struct i2c_hid *ihid = i2c_get_clientdata(client);
448 u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL);
449
317b204a
BT
450 if (!inbuf) {
451 dev_err(&client->dev, "can not retrieve initial reports\n");
4a200c3b 452 return;
317b204a 453 }
4a200c3b
BT
454
455 list_for_each_entry(report,
456 &hid->report_enum[HID_INPUT_REPORT].report_list, list)
457 i2c_hid_init_report(report, inbuf, ihid->bufsize);
458
459 list_for_each_entry(report,
460 &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
461 i2c_hid_init_report(report, inbuf, ihid->bufsize);
462
463 kfree(inbuf);
464}
465
466/*
467 * Traverse the supplied list of reports and find the longest
468 */
469static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
470 unsigned int *max)
471{
472 struct hid_report *report;
473 unsigned int size;
474
475 /* We should not rely on wMaxInputLength, as some devices may set it to
476 * a wrong length. */
477 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
478 size = i2c_hid_get_report_length(report);
479 if (*max < size)
480 *max = size;
481 }
482}
483
29b45787
BT
484static void i2c_hid_free_buffers(struct i2c_hid *ihid)
485{
486 kfree(ihid->inbuf);
caa853b3 487 kfree(ihid->rawbuf);
29b45787
BT
488 kfree(ihid->argsbuf);
489 kfree(ihid->cmdbuf);
490 ihid->inbuf = NULL;
caa853b3 491 ihid->rawbuf = NULL;
29b45787
BT
492 ihid->cmdbuf = NULL;
493 ihid->argsbuf = NULL;
494 ihid->bufsize = 0;
495}
496
497static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
4a200c3b
BT
498{
499 /* the worst case is computed from the set_report command with a
500 * reportID > 15 and the maximum report length */
501 int args_len = sizeof(__u8) + /* optional ReportID byte */
502 sizeof(__u16) + /* data register */
503 sizeof(__u16) + /* size of the report */
29b45787 504 report_size; /* report */
4a200c3b 505
29b45787 506 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
caa853b3 507 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
4a200c3b 508 ihid->argsbuf = kzalloc(args_len, GFP_KERNEL);
4a200c3b
BT
509 ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL);
510
caa853b3 511 if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) {
29b45787 512 i2c_hid_free_buffers(ihid);
4a200c3b
BT
513 return -ENOMEM;
514 }
515
29b45787 516 ihid->bufsize = report_size;
4a200c3b 517
29b45787 518 return 0;
4a200c3b
BT
519}
520
521static int i2c_hid_get_raw_report(struct hid_device *hid,
522 unsigned char report_number, __u8 *buf, size_t count,
523 unsigned char report_type)
524{
525 struct i2c_client *client = hid->driver_data;
526 struct i2c_hid *ihid = i2c_get_clientdata(client);
e5b50fe7 527 size_t ret_count, ask_count;
4a200c3b
BT
528 int ret;
529
530 if (report_type == HID_OUTPUT_REPORT)
531 return -EINVAL;
532
e5b50fe7
BT
533 /* +2 bytes to include the size of the reply in the query buffer */
534 ask_count = min(count + 2, (size_t)ihid->bufsize);
4a200c3b
BT
535
536 ret = i2c_hid_get_report(client,
537 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
caa853b3 538 report_number, ihid->rawbuf, ask_count);
4a200c3b
BT
539
540 if (ret < 0)
541 return ret;
542
caa853b3 543 ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8);
4a200c3b 544
9afd09a1 545 if (ret_count <= 2)
e5b50fe7
BT
546 return 0;
547
548 ret_count = min(ret_count, ask_count);
549
550 /* The query buffer contains the size, dropping it in the reply */
551 count = min(count, ret_count - 2);
caa853b3 552 memcpy(buf, ihid->rawbuf + 2, count);
4a200c3b
BT
553
554 return count;
555}
556
557static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
558 size_t count, unsigned char report_type)
559{
560 struct i2c_client *client = hid->driver_data;
561 int report_id = buf[0];
c284979a 562 int ret;
4a200c3b
BT
563
564 if (report_type == HID_INPUT_REPORT)
565 return -EINVAL;
566
c284979a
BT
567 if (report_id) {
568 buf++;
569 count--;
570 }
571
572 ret = i2c_hid_set_report(client,
4a200c3b
BT
573 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
574 report_id, buf, count);
c284979a
BT
575
576 if (report_id && ret >= 0)
577 ret++; /* add report_id to the number of transfered bytes */
578
579 return ret;
4a200c3b
BT
580}
581
545bef68
BT
582static void i2c_hid_request(struct hid_device *hid, struct hid_report *rep,
583 int reqtype)
584{
585 struct i2c_client *client = hid->driver_data;
545bef68
BT
586 char *buf;
587 int ret;
7c4d5773 588 int len = i2c_hid_get_report_length(rep) - 2;
545bef68 589
7c4d5773 590 buf = kzalloc(len, GFP_KERNEL);
545bef68
BT
591 if (!buf)
592 return;
593
594 switch (reqtype) {
595 case HID_REQ_GET_REPORT:
7c4d5773 596 ret = i2c_hid_get_raw_report(hid, rep->id, buf, len, rep->type);
545bef68
BT
597 if (ret < 0)
598 dev_err(&client->dev, "%s: unable to get report: %d\n",
599 __func__, ret);
600 else
601 hid_input_report(hid, rep->type, buf, ret, 0);
602 break;
603 case HID_REQ_SET_REPORT:
604 hid_output_report(rep, buf);
7c4d5773 605 i2c_hid_output_raw_report(hid, buf, len, rep->type);
545bef68
BT
606 break;
607 }
608
609 kfree(buf);
610}
611
4a200c3b
BT
612static int i2c_hid_parse(struct hid_device *hid)
613{
614 struct i2c_client *client = hid->driver_data;
615 struct i2c_hid *ihid = i2c_get_clientdata(client);
616 struct i2c_hid_desc *hdesc = &ihid->hdesc;
617 unsigned int rsize;
618 char *rdesc;
619 int ret;
620 int tries = 3;
621
622 i2c_hid_dbg(ihid, "entering %s\n", __func__);
623
624 rsize = le16_to_cpu(hdesc->wReportDescLength);
625 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
626 dbg_hid("weird size of report descriptor (%u)\n", rsize);
627 return -EINVAL;
628 }
629
630 do {
631 ret = i2c_hid_hwreset(client);
632 if (ret)
633 msleep(1000);
634 } while (tries-- > 0 && ret);
635
636 if (ret)
637 return ret;
638
639 rdesc = kzalloc(rsize, GFP_KERNEL);
640
641 if (!rdesc) {
642 dbg_hid("couldn't allocate rdesc memory\n");
643 return -ENOMEM;
644 }
645
646 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
647
648 ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize);
649 if (ret) {
650 hid_err(hid, "reading report descriptor failed\n");
651 kfree(rdesc);
652 return -EIO;
653 }
654
655 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
656
657 ret = hid_parse_report(hid, rdesc, rsize);
658 kfree(rdesc);
659 if (ret) {
660 dbg_hid("parsing report descriptor failed\n");
661 return ret;
662 }
663
664 return 0;
665}
666
667static int i2c_hid_start(struct hid_device *hid)
668{
669 struct i2c_client *client = hid->driver_data;
670 struct i2c_hid *ihid = i2c_get_clientdata(client);
671 int ret;
29b45787 672 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
4a200c3b 673
29b45787
BT
674 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
675 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
676 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
4a200c3b 677
29b45787 678 if (bufsize > ihid->bufsize) {
4a200c3b
BT
679 i2c_hid_free_buffers(ihid);
680
29b45787 681 ret = i2c_hid_alloc_buffers(ihid, bufsize);
4a200c3b 682
29b45787 683 if (ret)
4a200c3b 684 return ret;
4a200c3b
BT
685 }
686
687 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
688 i2c_hid_init_reports(hid);
689
690 return 0;
691}
692
693static void i2c_hid_stop(struct hid_device *hid)
694{
695 struct i2c_client *client = hid->driver_data;
696 struct i2c_hid *ihid = i2c_get_clientdata(client);
697
698 hid->claimed = 0;
699
700 i2c_hid_free_buffers(ihid);
701}
702
703static int i2c_hid_open(struct hid_device *hid)
704{
705 struct i2c_client *client = hid->driver_data;
706 struct i2c_hid *ihid = i2c_get_clientdata(client);
7a7d6d9c 707 int ret = 0;
4a200c3b 708
7a7d6d9c 709 mutex_lock(&i2c_hid_open_mut);
4a200c3b
BT
710 if (!hid->open++) {
711 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
712 if (ret) {
713 hid->open--;
7a7d6d9c 714 goto done;
4a200c3b
BT
715 }
716 set_bit(I2C_HID_STARTED, &ihid->flags);
717 }
7a7d6d9c
BT
718done:
719 mutex_unlock(&i2c_hid_open_mut);
720 return ret;
4a200c3b
BT
721}
722
723static void i2c_hid_close(struct hid_device *hid)
724{
725 struct i2c_client *client = hid->driver_data;
726 struct i2c_hid *ihid = i2c_get_clientdata(client);
727
728 /* protecting hid->open to make sure we don't restart
729 * data acquistion due to a resumption we no longer
730 * care about
731 */
7a7d6d9c 732 mutex_lock(&i2c_hid_open_mut);
4a200c3b
BT
733 if (!--hid->open) {
734 clear_bit(I2C_HID_STARTED, &ihid->flags);
735
736 /* Save some power */
737 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
738 }
7a7d6d9c 739 mutex_unlock(&i2c_hid_open_mut);
4a200c3b
BT
740}
741
742static int i2c_hid_power(struct hid_device *hid, int lvl)
743{
744 struct i2c_client *client = hid->driver_data;
745 struct i2c_hid *ihid = i2c_get_clientdata(client);
746 int ret = 0;
747
748 i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl);
749
750 switch (lvl) {
751 case PM_HINT_FULLON:
752 ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
753 break;
754 case PM_HINT_NORMAL:
755 ret = i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
756 break;
757 }
758 return ret;
759}
760
761static int i2c_hid_hidinput_input_event(struct input_dev *dev,
762 unsigned int type, unsigned int code, int value)
763{
764 struct hid_device *hid = input_get_drvdata(dev);
765 struct hid_field *field;
766 int offset;
767
768 if (type == EV_FF)
769 return input_ff_event(dev, type, code, value);
770
771 if (type != EV_LED)
772 return -1;
773
774 offset = hidinput_find_field(hid, type, code, &field);
775
776 if (offset == -1) {
777 hid_warn(dev, "event field not found\n");
778 return -1;
779 }
780
317b204a 781 return hid_set_field(field, offset, value);
4a200c3b
BT
782}
783
784static struct hid_ll_driver i2c_hid_ll_driver = {
785 .parse = i2c_hid_parse,
786 .start = i2c_hid_start,
787 .stop = i2c_hid_stop,
788 .open = i2c_hid_open,
789 .close = i2c_hid_close,
790 .power = i2c_hid_power,
545bef68 791 .request = i2c_hid_request,
4a200c3b
BT
792 .hidinput_input_event = i2c_hid_hidinput_input_event,
793};
794
0fe763c5 795static int i2c_hid_init_irq(struct i2c_client *client)
4a200c3b
BT
796{
797 struct i2c_hid *ihid = i2c_get_clientdata(client);
798 int ret;
799
800 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
801
802 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
803 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
804 client->name, ihid);
805 if (ret < 0) {
9972dcc2 806 dev_warn(&client->dev,
4a200c3b
BT
807 "Could not register for %s interrupt, irq = %d,"
808 " ret = %d\n",
9972dcc2 809 client->name, client->irq, ret);
4a200c3b
BT
810
811 return ret;
812 }
813
4a200c3b
BT
814 return 0;
815}
816
0fe763c5 817static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
4a200c3b
BT
818{
819 struct i2c_client *client = ihid->client;
820 struct i2c_hid_desc *hdesc = &ihid->hdesc;
821 unsigned int dsize;
822 int ret;
823
824 /* Fetch the length of HID description, retrieve the 4 first bytes:
825 * bytes 0-1 -> length
826 * bytes 2-3 -> bcdVersion (has to be 1.00) */
827 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer, 4);
828
829 i2c_hid_dbg(ihid, "%s, ihid->hdesc_buffer: %*ph\n",
830 __func__, 4, ihid->hdesc_buffer);
831
832 if (ret) {
9972dcc2
BT
833 dev_err(&client->dev,
834 "unable to fetch the size of HID descriptor (ret=%d)\n",
4a200c3b
BT
835 ret);
836 return -ENODEV;
837 }
838
839 dsize = le16_to_cpu(hdesc->wHIDDescLength);
27174cff
BT
840 /*
841 * the size of the HID descriptor should at least contain
842 * its size and the bcdVersion (4 bytes), and should not be greater
843 * than sizeof(struct i2c_hid_desc) as we directly fill this struct
844 * through i2c_hid_command.
845 */
846 if (dsize < 4 || dsize > sizeof(struct i2c_hid_desc)) {
4a200c3b
BT
847 dev_err(&client->dev, "weird size of HID descriptor (%u)\n",
848 dsize);
849 return -ENODEV;
850 }
851
852 /* check bcdVersion == 1.0 */
853 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
854 dev_err(&client->dev,
9972dcc2 855 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
4a200c3b
BT
856 le16_to_cpu(hdesc->bcdVersion));
857 return -ENODEV;
858 }
859
860 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
861
862 ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer,
863 dsize);
864 if (ret) {
865 dev_err(&client->dev, "hid_descr_cmd Fail\n");
866 return -ENODEV;
867 }
868
869 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer);
870
871 return 0;
872}
873
92241e67
MW
874#ifdef CONFIG_ACPI
875static int i2c_hid_acpi_pdata(struct i2c_client *client,
876 struct i2c_hid_platform_data *pdata)
877{
878 static u8 i2c_hid_guid[] = {
879 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
880 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
881 };
882 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
883 union acpi_object params[4], *obj;
884 struct acpi_object_list input;
885 struct acpi_device *adev;
886 acpi_handle handle;
887
888 handle = ACPI_HANDLE(&client->dev);
889 if (!handle || acpi_bus_get_device(handle, &adev))
890 return -ENODEV;
891
892 input.count = ARRAY_SIZE(params);
893 input.pointer = params;
894
895 params[0].type = ACPI_TYPE_BUFFER;
896 params[0].buffer.length = sizeof(i2c_hid_guid);
897 params[0].buffer.pointer = i2c_hid_guid;
898 params[1].type = ACPI_TYPE_INTEGER;
899 params[1].integer.value = 1;
900 params[2].type = ACPI_TYPE_INTEGER;
901 params[2].integer.value = 1; /* HID function */
902 params[3].type = ACPI_TYPE_INTEGER;
903 params[3].integer.value = 0;
904
905 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_DSM", &input, &buf))) {
906 dev_err(&client->dev, "device _DSM execution failed\n");
907 return -ENODEV;
908 }
909
910 obj = (union acpi_object *)buf.pointer;
911 if (obj->type != ACPI_TYPE_INTEGER) {
912 dev_err(&client->dev, "device _DSM returned invalid type: %d\n",
913 obj->type);
914 kfree(buf.pointer);
915 return -EINVAL;
916 }
917
918 pdata->hid_descriptor_address = obj->integer.value;
919
920 kfree(buf.pointer);
921 return 0;
922}
923
924static const struct acpi_device_id i2c_hid_acpi_match[] = {
925 {"ACPI0C50", 0 },
926 {"PNP0C50", 0 },
927 { },
928};
929MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match);
930#else
931static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
932 struct i2c_hid_platform_data *pdata)
933{
934 return -ENODEV;
935}
936#endif
937
0fe763c5
GKH
938static int i2c_hid_probe(struct i2c_client *client,
939 const struct i2c_device_id *dev_id)
4a200c3b
BT
940{
941 int ret;
942 struct i2c_hid *ihid;
943 struct hid_device *hid;
944 __u16 hidRegister;
945 struct i2c_hid_platform_data *platform_data = client->dev.platform_data;
946
947 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
948
4a200c3b
BT
949 if (!client->irq) {
950 dev_err(&client->dev,
951 "HID over i2c has not been provided an Int IRQ\n");
952 return -EINVAL;
953 }
954
955 ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
956 if (!ihid)
957 return -ENOMEM;
958
92241e67
MW
959 if (!platform_data) {
960 ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
961 if (ret) {
962 dev_err(&client->dev,
963 "HID register address not provided\n");
964 goto err;
965 }
966 } else {
967 ihid->pdata = *platform_data;
968 }
969
4a200c3b
BT
970 i2c_set_clientdata(client, ihid);
971
972 ihid->client = client;
973
92241e67 974 hidRegister = ihid->pdata.hid_descriptor_address;
4a200c3b
BT
975 ihid->wHIDDescRegister = cpu_to_le16(hidRegister);
976
977 init_waitqueue_head(&ihid->wait);
978
979 /* we need to allocate the command buffer without knowing the maximum
980 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the
981 * real computation later. */
29b45787
BT
982 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
983 if (ret < 0)
984 goto err;
4a200c3b
BT
985
986 ret = i2c_hid_fetch_hid_descriptor(ihid);
987 if (ret < 0)
988 goto err;
989
990 ret = i2c_hid_init_irq(client);
991 if (ret < 0)
992 goto err;
993
994 hid = hid_allocate_device();
995 if (IS_ERR(hid)) {
996 ret = PTR_ERR(hid);
8a1bbb53 997 goto err_irq;
4a200c3b
BT
998 }
999
1000 ihid->hid = hid;
1001
1002 hid->driver_data = client;
1003 hid->ll_driver = &i2c_hid_ll_driver;
1004 hid->hid_get_raw_report = i2c_hid_get_raw_report;
1005 hid->hid_output_raw_report = i2c_hid_output_raw_report;
1006 hid->dev.parent = &client->dev;
92241e67 1007 ACPI_HANDLE_SET(&hid->dev, ACPI_HANDLE(&client->dev));
4a200c3b
BT
1008 hid->bus = BUS_I2C;
1009 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1010 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1011 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1012
9972dcc2 1013 snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX",
4a200c3b
BT
1014 client->name, hid->vendor, hid->product);
1015
1016 ret = hid_add_device(hid);
1017 if (ret) {
1018 if (ret != -ENODEV)
1019 hid_err(client, "can't add hid device: %d\n", ret);
1020 goto err_mem_free;
1021 }
1022
1023 return 0;
1024
1025err_mem_free:
1026 hid_destroy_device(hid);
1027
8a1bbb53
BT
1028err_irq:
1029 free_irq(client->irq, ihid);
4a200c3b 1030
8a1bbb53 1031err:
3c626024 1032 i2c_hid_free_buffers(ihid);
4a200c3b
BT
1033 kfree(ihid);
1034 return ret;
1035}
1036
0fe763c5 1037static int i2c_hid_remove(struct i2c_client *client)
4a200c3b
BT
1038{
1039 struct i2c_hid *ihid = i2c_get_clientdata(client);
1040 struct hid_device *hid;
1041
4a200c3b
BT
1042 hid = ihid->hid;
1043 hid_destroy_device(hid);
1044
1045 free_irq(client->irq, ihid);
1046
134ebfd8
BT
1047 if (ihid->bufsize)
1048 i2c_hid_free_buffers(ihid);
1049
4a200c3b
BT
1050 kfree(ihid);
1051
1052 return 0;
1053}
1054
1055#ifdef CONFIG_PM_SLEEP
1056static int i2c_hid_suspend(struct device *dev)
1057{
1058 struct i2c_client *client = to_i2c_client(dev);
4a200c3b
BT
1059
1060 if (device_may_wakeup(&client->dev))
8a1bbb53 1061 enable_irq_wake(client->irq);
4a200c3b
BT
1062
1063 /* Save some power */
1064 i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1065
1066 return 0;
1067}
1068
1069static int i2c_hid_resume(struct device *dev)
1070{
1071 int ret;
1072 struct i2c_client *client = to_i2c_client(dev);
1073
1074 ret = i2c_hid_hwreset(client);
1075 if (ret)
1076 return ret;
1077
1078 if (device_may_wakeup(&client->dev))
1079 disable_irq_wake(client->irq);
1080
1081 return 0;
1082}
1083#endif
1084
1085static SIMPLE_DEV_PM_OPS(i2c_hid_pm, i2c_hid_suspend, i2c_hid_resume);
1086
1087static const struct i2c_device_id i2c_hid_id_table[] = {
24ebb37e 1088 { "hid", 0 },
4a200c3b
BT
1089 { },
1090};
1091MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table);
1092
1093
1094static struct i2c_driver i2c_hid_driver = {
1095 .driver = {
1096 .name = "i2c_hid",
1097 .owner = THIS_MODULE,
1098 .pm = &i2c_hid_pm,
92241e67 1099 .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
4a200c3b
BT
1100 },
1101
1102 .probe = i2c_hid_probe,
0fe763c5 1103 .remove = i2c_hid_remove,
4a200c3b
BT
1104
1105 .id_table = i2c_hid_id_table,
1106};
1107
1108module_i2c_driver(i2c_hid_driver);
1109
1110MODULE_DESCRIPTION("HID over I2C core driver");
1111MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1112MODULE_LICENSE("GPL");