source: src/linux/universal/linux-4.9/drivers/hid/i2c-hid/i2c-hid.c @ 31859

Last change on this file since 31859 was 31859, checked in by brainslayer, 3 months ago

kernel update

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