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