0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 #include <linux/module.h>
0022 #include <linux/i2c.h>
0023 #include <linux/interrupt.h>
0024 #include <linux/input.h>
0025 #include <linux/irq.h>
0026 #include <linux/delay.h>
0027 #include <linux/slab.h>
0028 #include <linux/pm.h>
0029 #include <linux/device.h>
0030 #include <linux/wait.h>
0031 #include <linux/err.h>
0032 #include <linux/string.h>
0033 #include <linux/list.h>
0034 #include <linux/jiffies.h>
0035 #include <linux/kernel.h>
0036 #include <linux/hid.h>
0037 #include <linux/mutex.h>
0038 #include <asm/unaligned.h>
0039
0040 #include "../hid-ids.h"
0041 #include "i2c-hid.h"
0042
0043
0044 #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0)
0045 #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1)
0046 #define I2C_HID_QUIRK_BOGUS_IRQ BIT(4)
0047 #define I2C_HID_QUIRK_RESET_ON_RESUME BIT(5)
0048 #define I2C_HID_QUIRK_BAD_INPUT_SIZE BIT(6)
0049 #define I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET BIT(7)
0050
0051
0052 #define I2C_HID_OPCODE_RESET 0x01
0053 #define I2C_HID_OPCODE_GET_REPORT 0x02
0054 #define I2C_HID_OPCODE_SET_REPORT 0x03
0055 #define I2C_HID_OPCODE_GET_IDLE 0x04
0056 #define I2C_HID_OPCODE_SET_IDLE 0x05
0057 #define I2C_HID_OPCODE_GET_PROTOCOL 0x06
0058 #define I2C_HID_OPCODE_SET_PROTOCOL 0x07
0059 #define I2C_HID_OPCODE_SET_POWER 0x08
0060
0061
0062 #define I2C_HID_STARTED 0
0063 #define I2C_HID_RESET_PENDING 1
0064 #define I2C_HID_READ_PENDING 2
0065
0066 #define I2C_HID_PWR_ON 0x00
0067 #define I2C_HID_PWR_SLEEP 0x01
0068
0069
0070 static bool debug;
0071 module_param(debug, bool, 0444);
0072 MODULE_PARM_DESC(debug, "print a lot of debug information");
0073
0074 #define i2c_hid_dbg(ihid, fmt, arg...) \
0075 do { \
0076 if (debug) \
0077 dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \
0078 } while (0)
0079
0080 struct i2c_hid_desc {
0081 __le16 wHIDDescLength;
0082 __le16 bcdVersion;
0083 __le16 wReportDescLength;
0084 __le16 wReportDescRegister;
0085 __le16 wInputRegister;
0086 __le16 wMaxInputLength;
0087 __le16 wOutputRegister;
0088 __le16 wMaxOutputLength;
0089 __le16 wCommandRegister;
0090 __le16 wDataRegister;
0091 __le16 wVendorID;
0092 __le16 wProductID;
0093 __le16 wVersionID;
0094 __le32 reserved;
0095 } __packed;
0096
0097
0098 struct i2c_hid {
0099 struct i2c_client *client;
0100 struct hid_device *hid;
0101 struct i2c_hid_desc hdesc;
0102 __le16 wHIDDescRegister;
0103
0104
0105 unsigned int bufsize;
0106 u8 *inbuf;
0107 u8 *rawbuf;
0108 u8 *cmdbuf;
0109
0110 unsigned long flags;
0111 unsigned long quirks;
0112
0113 wait_queue_head_t wait;
0114
0115 bool irq_wake_enabled;
0116 struct mutex reset_lock;
0117
0118 struct i2chid_ops *ops;
0119 };
0120
0121 static const struct i2c_hid_quirks {
0122 __u16 idVendor;
0123 __u16 idProduct;
0124 __u32 quirks;
0125 } i2c_hid_quirks[] = {
0126 { USB_VENDOR_ID_WEIDA, HID_ANY_ID,
0127 I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV },
0128 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288,
0129 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
0130 { I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15,
0131 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
0132 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118,
0133 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET },
0134 { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID,
0135 I2C_HID_QUIRK_RESET_ON_RESUME },
0136 { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393,
0137 I2C_HID_QUIRK_RESET_ON_RESUME },
0138 { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720,
0139 I2C_HID_QUIRK_BAD_INPUT_SIZE },
0140
0141
0142
0143 { USB_VENDOR_ID_ELAN, HID_ANY_ID,
0144 I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET |
0145 I2C_HID_QUIRK_BOGUS_IRQ },
0146 { 0, 0 }
0147 };
0148
0149
0150
0151
0152
0153
0154
0155
0156 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct)
0157 {
0158 u32 quirks = 0;
0159 int n;
0160
0161 for (n = 0; i2c_hid_quirks[n].idVendor; n++)
0162 if (i2c_hid_quirks[n].idVendor == idVendor &&
0163 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID ||
0164 i2c_hid_quirks[n].idProduct == idProduct))
0165 quirks = i2c_hid_quirks[n].quirks;
0166
0167 return quirks;
0168 }
0169
0170 static int i2c_hid_xfer(struct i2c_hid *ihid,
0171 u8 *send_buf, int send_len, u8 *recv_buf, int recv_len)
0172 {
0173 struct i2c_client *client = ihid->client;
0174 struct i2c_msg msgs[2] = { 0 };
0175 int n = 0;
0176 int ret;
0177
0178 if (send_len) {
0179 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n",
0180 __func__, send_len, send_buf);
0181
0182 msgs[n].addr = client->addr;
0183 msgs[n].flags = (client->flags & I2C_M_TEN) | I2C_M_DMA_SAFE;
0184 msgs[n].len = send_len;
0185 msgs[n].buf = send_buf;
0186 n++;
0187 }
0188
0189 if (recv_len) {
0190 msgs[n].addr = client->addr;
0191 msgs[n].flags = (client->flags & I2C_M_TEN) |
0192 I2C_M_RD | I2C_M_DMA_SAFE;
0193 msgs[n].len = recv_len;
0194 msgs[n].buf = recv_buf;
0195 n++;
0196
0197 set_bit(I2C_HID_READ_PENDING, &ihid->flags);
0198 }
0199
0200 ret = i2c_transfer(client->adapter, msgs, n);
0201
0202 if (recv_len)
0203 clear_bit(I2C_HID_READ_PENDING, &ihid->flags);
0204
0205 if (ret != n)
0206 return ret < 0 ? ret : -EIO;
0207
0208 return 0;
0209 }
0210
0211 static int i2c_hid_read_register(struct i2c_hid *ihid, __le16 reg,
0212 void *buf, size_t len)
0213 {
0214 *(__le16 *)ihid->cmdbuf = reg;
0215
0216 return i2c_hid_xfer(ihid, ihid->cmdbuf, sizeof(__le16), buf, len);
0217 }
0218
0219 static size_t i2c_hid_encode_command(u8 *buf, u8 opcode,
0220 int report_type, int report_id)
0221 {
0222 size_t length = 0;
0223
0224 if (report_id < 0x0F) {
0225 buf[length++] = report_type << 4 | report_id;
0226 buf[length++] = opcode;
0227 } else {
0228 buf[length++] = report_type << 4 | 0x0F;
0229 buf[length++] = opcode;
0230 buf[length++] = report_id;
0231 }
0232
0233 return length;
0234 }
0235
0236 static int i2c_hid_get_report(struct i2c_hid *ihid,
0237 u8 report_type, u8 report_id,
0238 u8 *recv_buf, size_t recv_len)
0239 {
0240 size_t length = 0;
0241 size_t ret_count;
0242 int error;
0243
0244 i2c_hid_dbg(ihid, "%s\n", __func__);
0245
0246
0247 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
0248 length += sizeof(__le16);
0249
0250 length += i2c_hid_encode_command(ihid->cmdbuf + length,
0251 I2C_HID_OPCODE_GET_REPORT,
0252 report_type, report_id);
0253
0254
0255
0256
0257
0258 put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
0259 ihid->cmdbuf + length);
0260 length += sizeof(__le16);
0261
0262
0263
0264
0265
0266 error = i2c_hid_xfer(ihid, ihid->cmdbuf, length,
0267 ihid->rawbuf, recv_len + sizeof(__le16));
0268 if (error) {
0269 dev_err(&ihid->client->dev,
0270 "failed to set a report to device: %d\n", error);
0271 return error;
0272 }
0273
0274
0275 ret_count = le16_to_cpup((__le16 *)ihid->rawbuf);
0276
0277
0278 if (ret_count <= sizeof(__le16))
0279 return 0;
0280
0281 recv_len = min(recv_len, ret_count - sizeof(__le16));
0282 memcpy(recv_buf, ihid->rawbuf + sizeof(__le16), recv_len);
0283
0284 if (report_id && recv_len != 0 && recv_buf[0] != report_id) {
0285 dev_err(&ihid->client->dev,
0286 "device returned incorrect report (%d vs %d expected)\n",
0287 recv_buf[0], report_id);
0288 return -EINVAL;
0289 }
0290
0291 return recv_len;
0292 }
0293
0294 static size_t i2c_hid_format_report(u8 *buf, int report_id,
0295 const u8 *data, size_t size)
0296 {
0297 size_t length = sizeof(__le16);
0298
0299 if (report_id)
0300 buf[length++] = report_id;
0301
0302 memcpy(buf + length, data, size);
0303 length += size;
0304
0305
0306 put_unaligned_le16(length, buf);
0307
0308 return length;
0309 }
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320 static int i2c_hid_set_or_send_report(struct i2c_hid *ihid,
0321 u8 report_type, u8 report_id,
0322 const u8 *buf, size_t data_len,
0323 bool do_set)
0324 {
0325 size_t length = 0;
0326 int error;
0327
0328 i2c_hid_dbg(ihid, "%s\n", __func__);
0329
0330 if (data_len > ihid->bufsize)
0331 return -EINVAL;
0332
0333 if (!do_set && le16_to_cpu(ihid->hdesc.wMaxOutputLength) == 0)
0334 return -ENOSYS;
0335
0336 if (do_set) {
0337
0338 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
0339 length += sizeof(__le16);
0340
0341 length += i2c_hid_encode_command(ihid->cmdbuf + length,
0342 I2C_HID_OPCODE_SET_REPORT,
0343 report_type, report_id);
0344
0345
0346
0347
0348
0349 put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister),
0350 ihid->cmdbuf + length);
0351 length += sizeof(__le16);
0352 } else {
0353
0354
0355
0356
0357 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wOutputRegister;
0358 length += sizeof(__le16);
0359 }
0360
0361 length += i2c_hid_format_report(ihid->cmdbuf + length,
0362 report_id, buf, data_len);
0363
0364 error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
0365 if (error) {
0366 dev_err(&ihid->client->dev,
0367 "failed to set a report to device: %d\n", error);
0368 return error;
0369 }
0370
0371 return data_len;
0372 }
0373
0374 static int i2c_hid_set_power_command(struct i2c_hid *ihid, int power_state)
0375 {
0376 size_t length;
0377
0378
0379 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
0380 length = sizeof(__le16);
0381
0382
0383 length += i2c_hid_encode_command(ihid->cmdbuf + length,
0384 I2C_HID_OPCODE_SET_POWER,
0385 0, power_state);
0386
0387 return i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
0388 }
0389
0390 static int i2c_hid_set_power(struct i2c_hid *ihid, int power_state)
0391 {
0392 int ret;
0393
0394 i2c_hid_dbg(ihid, "%s\n", __func__);
0395
0396
0397
0398
0399
0400
0401 if (power_state == I2C_HID_PWR_ON &&
0402 ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) {
0403 ret = i2c_hid_set_power_command(ihid, I2C_HID_PWR_ON);
0404
0405
0406 if (!ret)
0407 goto set_pwr_exit;
0408 }
0409
0410 ret = i2c_hid_set_power_command(ihid, power_state);
0411 if (ret)
0412 dev_err(&ihid->client->dev,
0413 "failed to change power setting.\n");
0414
0415 set_pwr_exit:
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426 if (!ret && power_state == I2C_HID_PWR_ON)
0427 msleep(60);
0428
0429 return ret;
0430 }
0431
0432 static int i2c_hid_execute_reset(struct i2c_hid *ihid)
0433 {
0434 size_t length = 0;
0435 int ret;
0436
0437 i2c_hid_dbg(ihid, "resetting...\n");
0438
0439
0440 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister;
0441 length += sizeof(__le16);
0442
0443 length += i2c_hid_encode_command(ihid->cmdbuf + length,
0444 I2C_HID_OPCODE_RESET, 0, 0);
0445
0446 set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
0447
0448 ret = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0);
0449 if (ret) {
0450 dev_err(&ihid->client->dev, "failed to reset device.\n");
0451 goto out;
0452 }
0453
0454 if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) {
0455 msleep(100);
0456 goto out;
0457 }
0458
0459 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
0460 if (!wait_event_timeout(ihid->wait,
0461 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
0462 msecs_to_jiffies(5000))) {
0463 ret = -ENODATA;
0464 goto out;
0465 }
0466 i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
0467
0468 out:
0469 clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
0470 return ret;
0471 }
0472
0473 static int i2c_hid_hwreset(struct i2c_hid *ihid)
0474 {
0475 int ret;
0476
0477 i2c_hid_dbg(ihid, "%s\n", __func__);
0478
0479
0480
0481
0482
0483
0484 mutex_lock(&ihid->reset_lock);
0485
0486 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
0487 if (ret)
0488 goto out_unlock;
0489
0490 ret = i2c_hid_execute_reset(ihid);
0491 if (ret) {
0492 dev_err(&ihid->client->dev,
0493 "failed to reset device: %d\n", ret);
0494 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
0495 goto out_unlock;
0496 }
0497
0498
0499 if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET))
0500 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
0501
0502 out_unlock:
0503 mutex_unlock(&ihid->reset_lock);
0504 return ret;
0505 }
0506
0507 static void i2c_hid_get_input(struct i2c_hid *ihid)
0508 {
0509 u16 size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
0510 u16 ret_size;
0511 int ret;
0512
0513 if (size > ihid->bufsize)
0514 size = ihid->bufsize;
0515
0516 ret = i2c_master_recv(ihid->client, ihid->inbuf, size);
0517 if (ret != size) {
0518 if (ret < 0)
0519 return;
0520
0521 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n",
0522 __func__, ret, size);
0523 return;
0524 }
0525
0526
0527 ret_size = le16_to_cpup((__le16 *)ihid->inbuf);
0528 if (!ret_size) {
0529
0530 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
0531 wake_up(&ihid->wait);
0532 return;
0533 }
0534
0535 if ((ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ) && ret_size == 0xffff) {
0536 dev_warn_once(&ihid->client->dev,
0537 "%s: IRQ triggered but there's no data\n",
0538 __func__);
0539 return;
0540 }
0541
0542 if (ret_size > size || ret_size < sizeof(__le16)) {
0543 if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
0544 *(__le16 *)ihid->inbuf = cpu_to_le16(size);
0545 ret_size = size;
0546 } else {
0547 dev_err(&ihid->client->dev,
0548 "%s: incomplete report (%d/%d)\n",
0549 __func__, size, ret_size);
0550 return;
0551 }
0552 }
0553
0554 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf);
0555
0556 if (test_bit(I2C_HID_STARTED, &ihid->flags)) {
0557 pm_wakeup_event(&ihid->client->dev, 0);
0558
0559 hid_input_report(ihid->hid, HID_INPUT_REPORT,
0560 ihid->inbuf + sizeof(__le16),
0561 ret_size - sizeof(__le16), 1);
0562 }
0563
0564 return;
0565 }
0566
0567 static irqreturn_t i2c_hid_irq(int irq, void *dev_id)
0568 {
0569 struct i2c_hid *ihid = dev_id;
0570
0571 if (test_bit(I2C_HID_READ_PENDING, &ihid->flags))
0572 return IRQ_HANDLED;
0573
0574 i2c_hid_get_input(ihid);
0575
0576 return IRQ_HANDLED;
0577 }
0578
0579 static int i2c_hid_get_report_length(struct hid_report *report)
0580 {
0581 return ((report->size - 1) >> 3) + 1 +
0582 report->device->report_enum[report->type].numbered + 2;
0583 }
0584
0585
0586
0587
0588 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type,
0589 unsigned int *max)
0590 {
0591 struct hid_report *report;
0592 unsigned int size;
0593
0594
0595
0596 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
0597 size = i2c_hid_get_report_length(report);
0598 if (*max < size)
0599 *max = size;
0600 }
0601 }
0602
0603 static void i2c_hid_free_buffers(struct i2c_hid *ihid)
0604 {
0605 kfree(ihid->inbuf);
0606 kfree(ihid->rawbuf);
0607 kfree(ihid->cmdbuf);
0608 ihid->inbuf = NULL;
0609 ihid->rawbuf = NULL;
0610 ihid->cmdbuf = NULL;
0611 ihid->bufsize = 0;
0612 }
0613
0614 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size)
0615 {
0616
0617
0618
0619
0620 int cmd_len = sizeof(__le16) +
0621 sizeof(u8) +
0622 sizeof(u8) +
0623 sizeof(u8) +
0624 sizeof(__le16) +
0625 sizeof(__le16) +
0626 sizeof(u8) +
0627 report_size;
0628
0629 ihid->inbuf = kzalloc(report_size, GFP_KERNEL);
0630 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL);
0631 ihid->cmdbuf = kzalloc(cmd_len, GFP_KERNEL);
0632
0633 if (!ihid->inbuf || !ihid->rawbuf || !ihid->cmdbuf) {
0634 i2c_hid_free_buffers(ihid);
0635 return -ENOMEM;
0636 }
0637
0638 ihid->bufsize = report_size;
0639
0640 return 0;
0641 }
0642
0643 static int i2c_hid_get_raw_report(struct hid_device *hid,
0644 u8 report_type, u8 report_id,
0645 u8 *buf, size_t count)
0646 {
0647 struct i2c_client *client = hid->driver_data;
0648 struct i2c_hid *ihid = i2c_get_clientdata(client);
0649 int ret_count;
0650
0651 if (report_type == HID_OUTPUT_REPORT)
0652 return -EINVAL;
0653
0654
0655
0656
0657
0658
0659 if (!report_id) {
0660 buf[0] = 0;
0661 buf++;
0662 count--;
0663 }
0664
0665 ret_count = i2c_hid_get_report(ihid,
0666 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
0667 report_id, buf, count);
0668
0669 if (ret_count > 0 && !report_id)
0670 ret_count++;
0671
0672 return ret_count;
0673 }
0674
0675 static int i2c_hid_output_raw_report(struct hid_device *hid, u8 report_type,
0676 const u8 *buf, size_t count, bool do_set)
0677 {
0678 struct i2c_client *client = hid->driver_data;
0679 struct i2c_hid *ihid = i2c_get_clientdata(client);
0680 int report_id = buf[0];
0681 int ret;
0682
0683 if (report_type == HID_INPUT_REPORT)
0684 return -EINVAL;
0685
0686 mutex_lock(&ihid->reset_lock);
0687
0688
0689
0690
0691
0692
0693
0694
0695 ret = i2c_hid_set_or_send_report(ihid,
0696 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
0697 report_id, buf + 1, count - 1, do_set);
0698
0699 if (ret >= 0)
0700 ret++;
0701
0702 mutex_unlock(&ihid->reset_lock);
0703
0704 return ret;
0705 }
0706
0707 static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count)
0708 {
0709 return i2c_hid_output_raw_report(hid, HID_OUTPUT_REPORT, buf, count,
0710 false);
0711 }
0712
0713 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
0714 __u8 *buf, size_t len, unsigned char rtype,
0715 int reqtype)
0716 {
0717 switch (reqtype) {
0718 case HID_REQ_GET_REPORT:
0719 return i2c_hid_get_raw_report(hid, rtype, reportnum, buf, len);
0720 case HID_REQ_SET_REPORT:
0721 if (buf[0] != reportnum)
0722 return -EINVAL;
0723 return i2c_hid_output_raw_report(hid, rtype, buf, len, true);
0724 default:
0725 return -EIO;
0726 }
0727 }
0728
0729 static int i2c_hid_parse(struct hid_device *hid)
0730 {
0731 struct i2c_client *client = hid->driver_data;
0732 struct i2c_hid *ihid = i2c_get_clientdata(client);
0733 struct i2c_hid_desc *hdesc = &ihid->hdesc;
0734 unsigned int rsize;
0735 char *rdesc;
0736 int ret;
0737 int tries = 3;
0738 char *use_override;
0739
0740 i2c_hid_dbg(ihid, "entering %s\n", __func__);
0741
0742 rsize = le16_to_cpu(hdesc->wReportDescLength);
0743 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
0744 dbg_hid("weird size of report descriptor (%u)\n", rsize);
0745 return -EINVAL;
0746 }
0747
0748 do {
0749 ret = i2c_hid_hwreset(ihid);
0750 if (ret)
0751 msleep(1000);
0752 } while (tries-- > 0 && ret);
0753
0754 if (ret)
0755 return ret;
0756
0757 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name,
0758 &rsize);
0759
0760 if (use_override) {
0761 rdesc = use_override;
0762 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n");
0763 } else {
0764 rdesc = kzalloc(rsize, GFP_KERNEL);
0765
0766 if (!rdesc) {
0767 dbg_hid("couldn't allocate rdesc memory\n");
0768 return -ENOMEM;
0769 }
0770
0771 i2c_hid_dbg(ihid, "asking HID report descriptor\n");
0772
0773 ret = i2c_hid_read_register(ihid,
0774 ihid->hdesc.wReportDescRegister,
0775 rdesc, rsize);
0776 if (ret) {
0777 hid_err(hid, "reading report descriptor failed\n");
0778 kfree(rdesc);
0779 return -EIO;
0780 }
0781 }
0782
0783 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc);
0784
0785 ret = hid_parse_report(hid, rdesc, rsize);
0786 if (!use_override)
0787 kfree(rdesc);
0788
0789 if (ret) {
0790 dbg_hid("parsing report descriptor failed\n");
0791 return ret;
0792 }
0793
0794 return 0;
0795 }
0796
0797 static int i2c_hid_start(struct hid_device *hid)
0798 {
0799 struct i2c_client *client = hid->driver_data;
0800 struct i2c_hid *ihid = i2c_get_clientdata(client);
0801 int ret;
0802 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
0803
0804 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
0805 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
0806 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
0807
0808 if (bufsize > ihid->bufsize) {
0809 disable_irq(client->irq);
0810 i2c_hid_free_buffers(ihid);
0811
0812 ret = i2c_hid_alloc_buffers(ihid, bufsize);
0813 enable_irq(client->irq);
0814
0815 if (ret)
0816 return ret;
0817 }
0818
0819 return 0;
0820 }
0821
0822 static void i2c_hid_stop(struct hid_device *hid)
0823 {
0824 hid->claimed = 0;
0825 }
0826
0827 static int i2c_hid_open(struct hid_device *hid)
0828 {
0829 struct i2c_client *client = hid->driver_data;
0830 struct i2c_hid *ihid = i2c_get_clientdata(client);
0831
0832 set_bit(I2C_HID_STARTED, &ihid->flags);
0833 return 0;
0834 }
0835
0836 static void i2c_hid_close(struct hid_device *hid)
0837 {
0838 struct i2c_client *client = hid->driver_data;
0839 struct i2c_hid *ihid = i2c_get_clientdata(client);
0840
0841 clear_bit(I2C_HID_STARTED, &ihid->flags);
0842 }
0843
0844 struct hid_ll_driver i2c_hid_ll_driver = {
0845 .parse = i2c_hid_parse,
0846 .start = i2c_hid_start,
0847 .stop = i2c_hid_stop,
0848 .open = i2c_hid_open,
0849 .close = i2c_hid_close,
0850 .output_report = i2c_hid_output_report,
0851 .raw_request = i2c_hid_raw_request,
0852 };
0853 EXPORT_SYMBOL_GPL(i2c_hid_ll_driver);
0854
0855 static int i2c_hid_init_irq(struct i2c_client *client)
0856 {
0857 struct i2c_hid *ihid = i2c_get_clientdata(client);
0858 unsigned long irqflags = 0;
0859 int ret;
0860
0861 dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
0862
0863 if (!irq_get_trigger_type(client->irq))
0864 irqflags = IRQF_TRIGGER_LOW;
0865
0866 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
0867 irqflags | IRQF_ONESHOT, client->name, ihid);
0868 if (ret < 0) {
0869 dev_warn(&client->dev,
0870 "Could not register for %s interrupt, irq = %d,"
0871 " ret = %d\n",
0872 client->name, client->irq, ret);
0873
0874 return ret;
0875 }
0876
0877 return 0;
0878 }
0879
0880 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
0881 {
0882 struct i2c_client *client = ihid->client;
0883 struct i2c_hid_desc *hdesc = &ihid->hdesc;
0884 unsigned int dsize;
0885 int error;
0886
0887
0888 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) {
0889 i2c_hid_dbg(ihid, "Using a HID descriptor override\n");
0890 ihid->hdesc =
0891 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name);
0892 } else {
0893 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n");
0894 error = i2c_hid_read_register(ihid,
0895 ihid->wHIDDescRegister,
0896 &ihid->hdesc,
0897 sizeof(ihid->hdesc));
0898 if (error) {
0899 dev_err(&ihid->client->dev,
0900 "failed to fetch HID descriptor: %d\n",
0901 error);
0902 return -ENODEV;
0903 }
0904 }
0905
0906
0907
0908
0909
0910 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) {
0911 dev_err(&ihid->client->dev,
0912 "unexpected HID descriptor bcdVersion (0x%04hx)\n",
0913 le16_to_cpu(hdesc->bcdVersion));
0914 return -ENODEV;
0915 }
0916
0917
0918 dsize = le16_to_cpu(hdesc->wHIDDescLength);
0919 if (dsize != sizeof(struct i2c_hid_desc)) {
0920 dev_err(&ihid->client->dev,
0921 "weird size of HID descriptor (%u)\n", dsize);
0922 return -ENODEV;
0923 }
0924 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, &ihid->hdesc);
0925 return 0;
0926 }
0927
0928 static int i2c_hid_core_power_up(struct i2c_hid *ihid)
0929 {
0930 if (!ihid->ops->power_up)
0931 return 0;
0932
0933 return ihid->ops->power_up(ihid->ops);
0934 }
0935
0936 static void i2c_hid_core_power_down(struct i2c_hid *ihid)
0937 {
0938 if (!ihid->ops->power_down)
0939 return;
0940
0941 ihid->ops->power_down(ihid->ops);
0942 }
0943
0944 static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid)
0945 {
0946 if (!ihid->ops->shutdown_tail)
0947 return;
0948
0949 ihid->ops->shutdown_tail(ihid->ops);
0950 }
0951
0952 int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
0953 u16 hid_descriptor_address, u32 quirks)
0954 {
0955 int ret;
0956 struct i2c_hid *ihid;
0957 struct hid_device *hid;
0958
0959 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
0960
0961 if (!client->irq) {
0962 dev_err(&client->dev,
0963 "HID over i2c has not been provided an Int IRQ\n");
0964 return -EINVAL;
0965 }
0966
0967 if (client->irq < 0) {
0968 if (client->irq != -EPROBE_DEFER)
0969 dev_err(&client->dev,
0970 "HID over i2c doesn't have a valid IRQ\n");
0971 return client->irq;
0972 }
0973
0974 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL);
0975 if (!ihid)
0976 return -ENOMEM;
0977
0978 ihid->ops = ops;
0979
0980 ret = i2c_hid_core_power_up(ihid);
0981 if (ret)
0982 return ret;
0983
0984 i2c_set_clientdata(client, ihid);
0985
0986 ihid->client = client;
0987
0988 ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address);
0989
0990 init_waitqueue_head(&ihid->wait);
0991 mutex_init(&ihid->reset_lock);
0992
0993
0994
0995
0996 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE);
0997 if (ret < 0)
0998 goto err_powered;
0999
1000 device_enable_async_suspend(&client->dev);
1001
1002
1003 ret = i2c_smbus_read_byte(client);
1004 if (ret < 0) {
1005 dev_dbg(&client->dev, "nothing at this address: %d\n", ret);
1006 ret = -ENXIO;
1007 goto err_powered;
1008 }
1009
1010 ret = i2c_hid_fetch_hid_descriptor(ihid);
1011 if (ret < 0) {
1012 dev_err(&client->dev,
1013 "Failed to fetch the HID Descriptor\n");
1014 goto err_powered;
1015 }
1016
1017 ret = i2c_hid_init_irq(client);
1018 if (ret < 0)
1019 goto err_powered;
1020
1021 hid = hid_allocate_device();
1022 if (IS_ERR(hid)) {
1023 ret = PTR_ERR(hid);
1024 goto err_irq;
1025 }
1026
1027 ihid->hid = hid;
1028
1029 hid->driver_data = client;
1030 hid->ll_driver = &i2c_hid_ll_driver;
1031 hid->dev.parent = &client->dev;
1032 hid->bus = BUS_I2C;
1033 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
1034 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
1035 hid->product = le16_to_cpu(ihid->hdesc.wProductID);
1036
1037 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
1038 client->name, (u16)hid->vendor, (u16)hid->product);
1039 strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys));
1040
1041 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product);
1042
1043 ret = hid_add_device(hid);
1044 if (ret) {
1045 if (ret != -ENODEV)
1046 hid_err(client, "can't add hid device: %d\n", ret);
1047 goto err_mem_free;
1048 }
1049
1050 hid->quirks |= quirks;
1051
1052 return 0;
1053
1054 err_mem_free:
1055 hid_destroy_device(hid);
1056
1057 err_irq:
1058 free_irq(client->irq, ihid);
1059
1060 err_powered:
1061 i2c_hid_core_power_down(ihid);
1062 i2c_hid_free_buffers(ihid);
1063 return ret;
1064 }
1065 EXPORT_SYMBOL_GPL(i2c_hid_core_probe);
1066
1067 int i2c_hid_core_remove(struct i2c_client *client)
1068 {
1069 struct i2c_hid *ihid = i2c_get_clientdata(client);
1070 struct hid_device *hid;
1071
1072 hid = ihid->hid;
1073 hid_destroy_device(hid);
1074
1075 free_irq(client->irq, ihid);
1076
1077 if (ihid->bufsize)
1078 i2c_hid_free_buffers(ihid);
1079
1080 i2c_hid_core_power_down(ihid);
1081
1082 return 0;
1083 }
1084 EXPORT_SYMBOL_GPL(i2c_hid_core_remove);
1085
1086 void i2c_hid_core_shutdown(struct i2c_client *client)
1087 {
1088 struct i2c_hid *ihid = i2c_get_clientdata(client);
1089
1090 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
1091 free_irq(client->irq, ihid);
1092
1093 i2c_hid_core_shutdown_tail(ihid);
1094 }
1095 EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown);
1096
1097 #ifdef CONFIG_PM_SLEEP
1098 static int i2c_hid_core_suspend(struct device *dev)
1099 {
1100 struct i2c_client *client = to_i2c_client(dev);
1101 struct i2c_hid *ihid = i2c_get_clientdata(client);
1102 struct hid_device *hid = ihid->hid;
1103 int ret;
1104 int wake_status;
1105
1106 ret = hid_driver_suspend(hid, PMSG_SUSPEND);
1107 if (ret < 0)
1108 return ret;
1109
1110
1111 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
1112
1113 disable_irq(client->irq);
1114
1115 if (device_may_wakeup(&client->dev)) {
1116 wake_status = enable_irq_wake(client->irq);
1117 if (!wake_status)
1118 ihid->irq_wake_enabled = true;
1119 else
1120 hid_warn(hid, "Failed to enable irq wake: %d\n",
1121 wake_status);
1122 } else {
1123 i2c_hid_core_power_down(ihid);
1124 }
1125
1126 return 0;
1127 }
1128
1129 static int i2c_hid_core_resume(struct device *dev)
1130 {
1131 int ret;
1132 struct i2c_client *client = to_i2c_client(dev);
1133 struct i2c_hid *ihid = i2c_get_clientdata(client);
1134 struct hid_device *hid = ihid->hid;
1135 int wake_status;
1136
1137 if (!device_may_wakeup(&client->dev)) {
1138 i2c_hid_core_power_up(ihid);
1139 } else if (ihid->irq_wake_enabled) {
1140 wake_status = disable_irq_wake(client->irq);
1141 if (!wake_status)
1142 ihid->irq_wake_enabled = false;
1143 else
1144 hid_warn(hid, "Failed to disable irq wake: %d\n",
1145 wake_status);
1146 }
1147
1148 enable_irq(client->irq);
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME)
1159 ret = i2c_hid_hwreset(ihid);
1160 else
1161 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON);
1162
1163 if (ret)
1164 return ret;
1165
1166 return hid_driver_reset_resume(hid);
1167 }
1168 #endif
1169
1170 const struct dev_pm_ops i2c_hid_core_pm = {
1171 SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_core_suspend, i2c_hid_core_resume)
1172 };
1173 EXPORT_SYMBOL_GPL(i2c_hid_core_pm);
1174
1175 MODULE_DESCRIPTION("HID over I2C core driver");
1176 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1177 MODULE_LICENSE("GPL");