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