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/pm_wakeirq.h> 30 #include <linux/device.h> 31 #include <linux/wait.h> 32 #include <linux/err.h> 33 #include <linux/string.h> 34 #include <linux/list.h> 35 #include <linux/jiffies.h> 36 #include <linux/kernel.h> 37 #include <linux/hid.h> 38 #include <linux/mutex.h> 39 #include <asm/unaligned.h> 40 41 #include <drm/drm_panel.h> 42 43 #include "../hid-ids.h" 44 #include "i2c-hid.h" 45 46 /* quirks to control the device */ 47 #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(0) 48 #define I2C_HID_QUIRK_BOGUS_IRQ BIT(1) 49 #define I2C_HID_QUIRK_RESET_ON_RESUME BIT(2) 50 #define I2C_HID_QUIRK_BAD_INPUT_SIZE BIT(3) 51 #define I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET BIT(4) 52 #define I2C_HID_QUIRK_NO_SLEEP_ON_SUSPEND BIT(5) 53 54 /* Command opcodes */ 55 #define I2C_HID_OPCODE_RESET 0x01 56 #define I2C_HID_OPCODE_GET_REPORT 0x02 57 #define I2C_HID_OPCODE_SET_REPORT 0x03 58 #define I2C_HID_OPCODE_GET_IDLE 0x04 59 #define I2C_HID_OPCODE_SET_IDLE 0x05 60 #define I2C_HID_OPCODE_GET_PROTOCOL 0x06 61 #define I2C_HID_OPCODE_SET_PROTOCOL 0x07 62 #define I2C_HID_OPCODE_SET_POWER 0x08 63 64 /* flags */ 65 #define I2C_HID_STARTED 0 66 #define I2C_HID_RESET_PENDING 1 67 68 #define I2C_HID_PWR_ON 0x00 69 #define I2C_HID_PWR_SLEEP 0x01 70 71 #define i2c_hid_dbg(ihid, ...) dev_dbg(&(ihid)->client->dev, __VA_ARGS__) 72 73 struct i2c_hid_desc { 74 __le16 wHIDDescLength; 75 __le16 bcdVersion; 76 __le16 wReportDescLength; 77 __le16 wReportDescRegister; 78 __le16 wInputRegister; 79 __le16 wMaxInputLength; 80 __le16 wOutputRegister; 81 __le16 wMaxOutputLength; 82 __le16 wCommandRegister; 83 __le16 wDataRegister; 84 __le16 wVendorID; 85 __le16 wProductID; 86 __le16 wVersionID; 87 __le32 reserved; 88 } __packed; 89 90 /* The main device structure */ 91 struct i2c_hid { 92 struct i2c_client *client; /* i2c client */ 93 struct hid_device *hid; /* pointer to corresponding HID dev */ 94 struct i2c_hid_desc hdesc; /* the HID Descriptor */ 95 __le16 wHIDDescRegister; /* location of the i2c 96 * register of the HID 97 * descriptor. */ 98 unsigned int bufsize; /* i2c buffer size */ 99 u8 *inbuf; /* Input buffer */ 100 u8 *rawbuf; /* Raw Input buffer */ 101 u8 *cmdbuf; /* Command buffer */ 102 103 unsigned long flags; /* device flags */ 104 unsigned long quirks; /* Various quirks */ 105 106 wait_queue_head_t wait; /* For waiting the interrupt */ 107 108 struct mutex reset_lock; 109 110 struct i2chid_ops *ops; 111 struct drm_panel_follower panel_follower; 112 struct work_struct panel_follower_prepare_work; 113 bool is_panel_follower; 114 bool prepare_work_finished; 115 }; 116 117 static const struct i2c_hid_quirks { 118 __u16 idVendor; 119 __u16 idProduct; 120 __u32 quirks; 121 } i2c_hid_quirks[] = { 122 { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288, 123 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET }, 124 { I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15, 125 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET }, 126 { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118, 127 I2C_HID_QUIRK_NO_IRQ_AFTER_RESET }, 128 { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID, 129 I2C_HID_QUIRK_RESET_ON_RESUME }, 130 { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393, 131 I2C_HID_QUIRK_RESET_ON_RESUME }, 132 { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720, 133 I2C_HID_QUIRK_BAD_INPUT_SIZE }, 134 { I2C_VENDOR_ID_CIRQUE, I2C_PRODUCT_ID_CIRQUE_1063, 135 I2C_HID_QUIRK_NO_SLEEP_ON_SUSPEND }, 136 /* 137 * Sending the wakeup after reset actually break ELAN touchscreen controller 138 */ 139 { USB_VENDOR_ID_ELAN, HID_ANY_ID, 140 I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET | 141 I2C_HID_QUIRK_BOGUS_IRQ }, 142 { 0, 0 } 143 }; 144 145 /* 146 * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device 147 * @idVendor: the 16-bit vendor ID 148 * @idProduct: the 16-bit product ID 149 * 150 * Returns: a u32 quirks value. 151 */ 152 static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct) 153 { 154 u32 quirks = 0; 155 int n; 156 157 for (n = 0; i2c_hid_quirks[n].idVendor; n++) 158 if (i2c_hid_quirks[n].idVendor == idVendor && 159 (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID || 160 i2c_hid_quirks[n].idProduct == idProduct)) 161 quirks = i2c_hid_quirks[n].quirks; 162 163 return quirks; 164 } 165 166 static int i2c_hid_xfer(struct i2c_hid *ihid, 167 u8 *send_buf, int send_len, u8 *recv_buf, int recv_len) 168 { 169 struct i2c_client *client = ihid->client; 170 struct i2c_msg msgs[2] = { 0 }; 171 int n = 0; 172 int ret; 173 174 if (send_len) { 175 i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", 176 __func__, send_len, send_buf); 177 178 msgs[n].addr = client->addr; 179 msgs[n].flags = (client->flags & I2C_M_TEN) | I2C_M_DMA_SAFE; 180 msgs[n].len = send_len; 181 msgs[n].buf = send_buf; 182 n++; 183 } 184 185 if (recv_len) { 186 msgs[n].addr = client->addr; 187 msgs[n].flags = (client->flags & I2C_M_TEN) | 188 I2C_M_RD | I2C_M_DMA_SAFE; 189 msgs[n].len = recv_len; 190 msgs[n].buf = recv_buf; 191 n++; 192 } 193 194 ret = i2c_transfer(client->adapter, msgs, n); 195 196 if (ret != n) 197 return ret < 0 ? ret : -EIO; 198 199 return 0; 200 } 201 202 static int i2c_hid_read_register(struct i2c_hid *ihid, __le16 reg, 203 void *buf, size_t len) 204 { 205 *(__le16 *)ihid->cmdbuf = reg; 206 207 return i2c_hid_xfer(ihid, ihid->cmdbuf, sizeof(__le16), buf, len); 208 } 209 210 static size_t i2c_hid_encode_command(u8 *buf, u8 opcode, 211 int report_type, int report_id) 212 { 213 size_t length = 0; 214 215 if (report_id < 0x0F) { 216 buf[length++] = report_type << 4 | report_id; 217 buf[length++] = opcode; 218 } else { 219 buf[length++] = report_type << 4 | 0x0F; 220 buf[length++] = opcode; 221 buf[length++] = report_id; 222 } 223 224 return length; 225 } 226 227 static int i2c_hid_get_report(struct i2c_hid *ihid, 228 u8 report_type, u8 report_id, 229 u8 *recv_buf, size_t recv_len) 230 { 231 size_t length = 0; 232 size_t ret_count; 233 int error; 234 235 i2c_hid_dbg(ihid, "%s\n", __func__); 236 237 /* Command register goes first */ 238 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister; 239 length += sizeof(__le16); 240 /* Next is GET_REPORT command */ 241 length += i2c_hid_encode_command(ihid->cmdbuf + length, 242 I2C_HID_OPCODE_GET_REPORT, 243 report_type, report_id); 244 /* 245 * Device will send report data through data register. Because 246 * command can be either 2 or 3 bytes destination for the data 247 * register may be not aligned. 248 */ 249 put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister), 250 ihid->cmdbuf + length); 251 length += sizeof(__le16); 252 253 /* 254 * In addition to report data device will supply data length 255 * in the first 2 bytes of the response, so adjust . 256 */ 257 error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, 258 ihid->rawbuf, recv_len + sizeof(__le16)); 259 if (error) { 260 dev_err(&ihid->client->dev, 261 "failed to set a report to device: %d\n", error); 262 return error; 263 } 264 265 /* The buffer is sufficiently aligned */ 266 ret_count = le16_to_cpup((__le16 *)ihid->rawbuf); 267 268 /* Check for empty report response */ 269 if (ret_count <= sizeof(__le16)) 270 return 0; 271 272 recv_len = min(recv_len, ret_count - sizeof(__le16)); 273 memcpy(recv_buf, ihid->rawbuf + sizeof(__le16), recv_len); 274 275 if (report_id && recv_len != 0 && recv_buf[0] != report_id) { 276 dev_err(&ihid->client->dev, 277 "device returned incorrect report (%d vs %d expected)\n", 278 recv_buf[0], report_id); 279 return -EINVAL; 280 } 281 282 return recv_len; 283 } 284 285 static size_t i2c_hid_format_report(u8 *buf, int report_id, 286 const u8 *data, size_t size) 287 { 288 size_t length = sizeof(__le16); /* reserve space to store size */ 289 290 if (report_id) 291 buf[length++] = report_id; 292 293 memcpy(buf + length, data, size); 294 length += size; 295 296 /* Store overall size in the beginning of the buffer */ 297 put_unaligned_le16(length, buf); 298 299 return length; 300 } 301 302 /** 303 * i2c_hid_set_or_send_report: forward an incoming report to the device 304 * @ihid: the i2c hid device 305 * @report_type: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT 306 * @report_id: the report ID 307 * @buf: the actual data to transfer, without the report ID 308 * @data_len: size of buf 309 * @do_set: true: use SET_REPORT HID command, false: send plain OUTPUT report 310 */ 311 static int i2c_hid_set_or_send_report(struct i2c_hid *ihid, 312 u8 report_type, u8 report_id, 313 const u8 *buf, size_t data_len, 314 bool do_set) 315 { 316 size_t length = 0; 317 int error; 318 319 i2c_hid_dbg(ihid, "%s\n", __func__); 320 321 if (data_len > ihid->bufsize) 322 return -EINVAL; 323 324 if (!do_set && le16_to_cpu(ihid->hdesc.wMaxOutputLength) == 0) 325 return -ENOSYS; 326 327 if (do_set) { 328 /* Command register goes first */ 329 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister; 330 length += sizeof(__le16); 331 /* Next is SET_REPORT command */ 332 length += i2c_hid_encode_command(ihid->cmdbuf + length, 333 I2C_HID_OPCODE_SET_REPORT, 334 report_type, report_id); 335 /* 336 * Report data will go into the data register. Because 337 * command can be either 2 or 3 bytes destination for 338 * the data register may be not aligned. 339 */ 340 put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister), 341 ihid->cmdbuf + length); 342 length += sizeof(__le16); 343 } else { 344 /* 345 * With simple "send report" all data goes into the output 346 * register. 347 */ 348 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wOutputRegister; 349 length += sizeof(__le16); 350 } 351 352 length += i2c_hid_format_report(ihid->cmdbuf + length, 353 report_id, buf, data_len); 354 355 error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0); 356 if (error) { 357 dev_err(&ihid->client->dev, 358 "failed to set a report to device: %d\n", error); 359 return error; 360 } 361 362 return data_len; 363 } 364 365 static int i2c_hid_set_power_command(struct i2c_hid *ihid, int power_state) 366 { 367 size_t length; 368 369 /* SET_POWER uses command register */ 370 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister; 371 length = sizeof(__le16); 372 373 /* Now the command itself */ 374 length += i2c_hid_encode_command(ihid->cmdbuf + length, 375 I2C_HID_OPCODE_SET_POWER, 376 0, power_state); 377 378 return i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0); 379 } 380 381 static int i2c_hid_set_power(struct i2c_hid *ihid, int power_state) 382 { 383 int ret; 384 385 i2c_hid_dbg(ihid, "%s\n", __func__); 386 387 /* 388 * Some devices require to send a command to wakeup before power on. 389 * The call will get a return value (EREMOTEIO) but device will be 390 * triggered and activated. After that, it goes like a normal device. 391 */ 392 if (power_state == I2C_HID_PWR_ON) { 393 ret = i2c_hid_set_power_command(ihid, I2C_HID_PWR_ON); 394 395 /* Device was already activated */ 396 if (!ret) 397 goto set_pwr_exit; 398 } 399 400 ret = i2c_hid_set_power_command(ihid, power_state); 401 if (ret) 402 dev_err(&ihid->client->dev, 403 "failed to change power setting.\n"); 404 405 set_pwr_exit: 406 407 /* 408 * The HID over I2C specification states that if a DEVICE needs time 409 * after the PWR_ON request, it should utilise CLOCK stretching. 410 * However, it has been observered that the Windows driver provides a 411 * 1ms sleep between the PWR_ON and RESET requests. 412 * According to Goodix Windows even waits 60 ms after (other?) 413 * PWR_ON requests. Testing has confirmed that several devices 414 * will not work properly without a delay after a PWR_ON request. 415 */ 416 if (!ret && power_state == I2C_HID_PWR_ON) 417 msleep(60); 418 419 return ret; 420 } 421 422 static int i2c_hid_start_hwreset(struct i2c_hid *ihid) 423 { 424 size_t length = 0; 425 int ret; 426 427 i2c_hid_dbg(ihid, "%s\n", __func__); 428 429 /* 430 * This prevents sending feature reports while the device is 431 * being reset. Otherwise we may lose the reset complete 432 * interrupt. 433 */ 434 lockdep_assert_held(&ihid->reset_lock); 435 436 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON); 437 if (ret) 438 return ret; 439 440 /* Prepare reset command. Command register goes first. */ 441 *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister; 442 length += sizeof(__le16); 443 /* Next is RESET command itself */ 444 length += i2c_hid_encode_command(ihid->cmdbuf + length, 445 I2C_HID_OPCODE_RESET, 0, 0); 446 447 set_bit(I2C_HID_RESET_PENDING, &ihid->flags); 448 449 ret = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0); 450 if (ret) { 451 dev_err(&ihid->client->dev, 452 "failed to reset device: %d\n", ret); 453 goto err_clear_reset; 454 } 455 456 return 0; 457 458 err_clear_reset: 459 clear_bit(I2C_HID_RESET_PENDING, &ihid->flags); 460 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP); 461 return ret; 462 } 463 464 static int i2c_hid_finish_hwreset(struct i2c_hid *ihid) 465 { 466 int ret = 0; 467 468 i2c_hid_dbg(ihid, "%s: waiting...\n", __func__); 469 470 if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) { 471 msleep(100); 472 clear_bit(I2C_HID_RESET_PENDING, &ihid->flags); 473 } else if (!wait_event_timeout(ihid->wait, 474 !test_bit(I2C_HID_RESET_PENDING, &ihid->flags), 475 msecs_to_jiffies(1000))) { 476 dev_warn(&ihid->client->dev, "device did not ack reset within 1000 ms\n"); 477 clear_bit(I2C_HID_RESET_PENDING, &ihid->flags); 478 } 479 i2c_hid_dbg(ihid, "%s: finished.\n", __func__); 480 481 /* At least some SIS devices need this after reset */ 482 if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET)) 483 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON); 484 485 return ret; 486 } 487 488 static void i2c_hid_get_input(struct i2c_hid *ihid) 489 { 490 u16 size = le16_to_cpu(ihid->hdesc.wMaxInputLength); 491 u16 ret_size; 492 int ret; 493 494 if (size > ihid->bufsize) 495 size = ihid->bufsize; 496 497 ret = i2c_master_recv(ihid->client, ihid->inbuf, size); 498 if (ret != size) { 499 if (ret < 0) 500 return; 501 502 dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n", 503 __func__, ret, size); 504 return; 505 } 506 507 /* Receiving buffer is properly aligned */ 508 ret_size = le16_to_cpup((__le16 *)ihid->inbuf); 509 if (!ret_size) { 510 /* host or device initiated RESET completed */ 511 if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags)) 512 wake_up(&ihid->wait); 513 return; 514 } 515 516 if ((ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ) && ret_size == 0xffff) { 517 dev_warn_once(&ihid->client->dev, 518 "%s: IRQ triggered but there's no data\n", 519 __func__); 520 return; 521 } 522 523 if (ret_size > size || ret_size < sizeof(__le16)) { 524 if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) { 525 *(__le16 *)ihid->inbuf = cpu_to_le16(size); 526 ret_size = size; 527 } else { 528 dev_err(&ihid->client->dev, 529 "%s: incomplete report (%d/%d)\n", 530 __func__, size, ret_size); 531 return; 532 } 533 } 534 535 i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf); 536 537 if (test_bit(I2C_HID_STARTED, &ihid->flags)) { 538 if (ihid->hid->group != HID_GROUP_RMI) 539 pm_wakeup_event(&ihid->client->dev, 0); 540 541 hid_input_report(ihid->hid, HID_INPUT_REPORT, 542 ihid->inbuf + sizeof(__le16), 543 ret_size - sizeof(__le16), 1); 544 } 545 546 return; 547 } 548 549 static irqreturn_t i2c_hid_irq(int irq, void *dev_id) 550 { 551 struct i2c_hid *ihid = dev_id; 552 553 i2c_hid_get_input(ihid); 554 555 return IRQ_HANDLED; 556 } 557 558 static int i2c_hid_get_report_length(struct hid_report *report) 559 { 560 return ((report->size - 1) >> 3) + 1 + 561 report->device->report_enum[report->type].numbered + 2; 562 } 563 564 /* 565 * Traverse the supplied list of reports and find the longest 566 */ 567 static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type, 568 unsigned int *max) 569 { 570 struct hid_report *report; 571 unsigned int size; 572 573 /* We should not rely on wMaxInputLength, as some devices may set it to 574 * a wrong length. */ 575 list_for_each_entry(report, &hid->report_enum[type].report_list, list) { 576 size = i2c_hid_get_report_length(report); 577 if (*max < size) 578 *max = size; 579 } 580 } 581 582 static void i2c_hid_free_buffers(struct i2c_hid *ihid) 583 { 584 kfree(ihid->inbuf); 585 kfree(ihid->rawbuf); 586 kfree(ihid->cmdbuf); 587 ihid->inbuf = NULL; 588 ihid->rawbuf = NULL; 589 ihid->cmdbuf = NULL; 590 ihid->bufsize = 0; 591 } 592 593 static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size) 594 { 595 /* 596 * The worst case is computed from the set_report command with a 597 * reportID > 15 and the maximum report length. 598 */ 599 int cmd_len = sizeof(__le16) + /* command register */ 600 sizeof(u8) + /* encoded report type/ID */ 601 sizeof(u8) + /* opcode */ 602 sizeof(u8) + /* optional 3rd byte report ID */ 603 sizeof(__le16) + /* data register */ 604 sizeof(__le16) + /* report data size */ 605 sizeof(u8) + /* report ID if numbered report */ 606 report_size; 607 608 ihid->inbuf = kzalloc(report_size, GFP_KERNEL); 609 ihid->rawbuf = kzalloc(report_size, GFP_KERNEL); 610 ihid->cmdbuf = kzalloc(cmd_len, GFP_KERNEL); 611 612 if (!ihid->inbuf || !ihid->rawbuf || !ihid->cmdbuf) { 613 i2c_hid_free_buffers(ihid); 614 return -ENOMEM; 615 } 616 617 ihid->bufsize = report_size; 618 619 return 0; 620 } 621 622 static int i2c_hid_get_raw_report(struct hid_device *hid, 623 u8 report_type, u8 report_id, 624 u8 *buf, size_t count) 625 { 626 struct i2c_client *client = hid->driver_data; 627 struct i2c_hid *ihid = i2c_get_clientdata(client); 628 int ret_count; 629 630 if (report_type == HID_OUTPUT_REPORT) 631 return -EINVAL; 632 633 /* 634 * In case of unnumbered reports the response from the device will 635 * not have the report ID that the upper layers expect, so we need 636 * to stash it the buffer ourselves and adjust the data size. 637 */ 638 if (!report_id) { 639 buf[0] = 0; 640 buf++; 641 count--; 642 } 643 644 ret_count = i2c_hid_get_report(ihid, 645 report_type == HID_FEATURE_REPORT ? 0x03 : 0x01, 646 report_id, buf, count); 647 648 if (ret_count > 0 && !report_id) 649 ret_count++; 650 651 return ret_count; 652 } 653 654 static int i2c_hid_output_raw_report(struct hid_device *hid, u8 report_type, 655 const u8 *buf, size_t count, bool do_set) 656 { 657 struct i2c_client *client = hid->driver_data; 658 struct i2c_hid *ihid = i2c_get_clientdata(client); 659 int report_id = buf[0]; 660 int ret; 661 662 if (report_type == HID_INPUT_REPORT) 663 return -EINVAL; 664 665 mutex_lock(&ihid->reset_lock); 666 667 /* 668 * Note that both numbered and unnumbered reports passed here 669 * are supposed to have report ID stored in the 1st byte of the 670 * buffer, so we strip it off unconditionally before passing payload 671 * to i2c_hid_set_or_send_report which takes care of encoding 672 * everything properly. 673 */ 674 ret = i2c_hid_set_or_send_report(ihid, 675 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02, 676 report_id, buf + 1, count - 1, do_set); 677 678 if (ret >= 0) 679 ret++; /* add report_id to the number of transferred bytes */ 680 681 mutex_unlock(&ihid->reset_lock); 682 683 return ret; 684 } 685 686 static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count) 687 { 688 return i2c_hid_output_raw_report(hid, HID_OUTPUT_REPORT, buf, count, 689 false); 690 } 691 692 static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum, 693 __u8 *buf, size_t len, unsigned char rtype, 694 int reqtype) 695 { 696 switch (reqtype) { 697 case HID_REQ_GET_REPORT: 698 return i2c_hid_get_raw_report(hid, rtype, reportnum, buf, len); 699 case HID_REQ_SET_REPORT: 700 if (buf[0] != reportnum) 701 return -EINVAL; 702 return i2c_hid_output_raw_report(hid, rtype, buf, len, true); 703 default: 704 return -EIO; 705 } 706 } 707 708 static int i2c_hid_parse(struct hid_device *hid) 709 { 710 struct i2c_client *client = hid->driver_data; 711 struct i2c_hid *ihid = i2c_get_clientdata(client); 712 struct i2c_hid_desc *hdesc = &ihid->hdesc; 713 char *rdesc = NULL, *use_override = NULL; 714 unsigned int rsize; 715 int ret; 716 int tries = 3; 717 718 i2c_hid_dbg(ihid, "entering %s\n", __func__); 719 720 rsize = le16_to_cpu(hdesc->wReportDescLength); 721 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) { 722 dbg_hid("weird size of report descriptor (%u)\n", rsize); 723 return -EINVAL; 724 } 725 726 mutex_lock(&ihid->reset_lock); 727 do { 728 ret = i2c_hid_start_hwreset(ihid); 729 if (ret == 0) 730 ret = i2c_hid_finish_hwreset(ihid); 731 else 732 msleep(1000); 733 } while (tries-- > 0 && ret); 734 mutex_unlock(&ihid->reset_lock); 735 736 if (ret) 737 return ret; 738 739 use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name, 740 &rsize); 741 742 if (use_override) { 743 rdesc = use_override; 744 i2c_hid_dbg(ihid, "Using a HID report descriptor override\n"); 745 } else { 746 rdesc = kzalloc(rsize, GFP_KERNEL); 747 if (!rdesc) 748 return -ENOMEM; 749 750 i2c_hid_dbg(ihid, "asking HID report descriptor\n"); 751 752 ret = i2c_hid_read_register(ihid, 753 ihid->hdesc.wReportDescRegister, 754 rdesc, rsize); 755 if (ret) { 756 hid_err(hid, "reading report descriptor failed\n"); 757 goto out; 758 } 759 } 760 761 i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc); 762 763 ret = hid_parse_report(hid, rdesc, rsize); 764 if (ret) 765 dbg_hid("parsing report descriptor failed\n"); 766 767 out: 768 if (!use_override) 769 kfree(rdesc); 770 771 return ret; 772 } 773 774 static int i2c_hid_start(struct hid_device *hid) 775 { 776 struct i2c_client *client = hid->driver_data; 777 struct i2c_hid *ihid = i2c_get_clientdata(client); 778 int ret; 779 unsigned int bufsize = HID_MIN_BUFFER_SIZE; 780 781 i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize); 782 i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize); 783 i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize); 784 785 if (bufsize > ihid->bufsize) { 786 disable_irq(client->irq); 787 i2c_hid_free_buffers(ihid); 788 789 ret = i2c_hid_alloc_buffers(ihid, bufsize); 790 enable_irq(client->irq); 791 792 if (ret) 793 return ret; 794 } 795 796 return 0; 797 } 798 799 static void i2c_hid_stop(struct hid_device *hid) 800 { 801 hid->claimed = 0; 802 } 803 804 static int i2c_hid_open(struct hid_device *hid) 805 { 806 struct i2c_client *client = hid->driver_data; 807 struct i2c_hid *ihid = i2c_get_clientdata(client); 808 809 set_bit(I2C_HID_STARTED, &ihid->flags); 810 return 0; 811 } 812 813 static void i2c_hid_close(struct hid_device *hid) 814 { 815 struct i2c_client *client = hid->driver_data; 816 struct i2c_hid *ihid = i2c_get_clientdata(client); 817 818 clear_bit(I2C_HID_STARTED, &ihid->flags); 819 } 820 821 static const struct hid_ll_driver i2c_hid_ll_driver = { 822 .parse = i2c_hid_parse, 823 .start = i2c_hid_start, 824 .stop = i2c_hid_stop, 825 .open = i2c_hid_open, 826 .close = i2c_hid_close, 827 .output_report = i2c_hid_output_report, 828 .raw_request = i2c_hid_raw_request, 829 }; 830 831 static int i2c_hid_init_irq(struct i2c_client *client) 832 { 833 struct i2c_hid *ihid = i2c_get_clientdata(client); 834 unsigned long irqflags = 0; 835 int ret; 836 837 i2c_hid_dbg(ihid, "Requesting IRQ: %d\n", client->irq); 838 839 if (!irq_get_trigger_type(client->irq)) 840 irqflags = IRQF_TRIGGER_LOW; 841 842 ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq, 843 irqflags | IRQF_ONESHOT | IRQF_NO_AUTOEN, 844 client->name, ihid); 845 if (ret < 0) { 846 dev_warn(&client->dev, 847 "Could not register for %s interrupt, irq = %d," 848 " ret = %d\n", 849 client->name, client->irq, ret); 850 851 return ret; 852 } 853 854 return 0; 855 } 856 857 static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid) 858 { 859 struct i2c_client *client = ihid->client; 860 struct i2c_hid_desc *hdesc = &ihid->hdesc; 861 unsigned int dsize; 862 int error; 863 864 /* i2c hid fetch using a fixed descriptor size (30 bytes) */ 865 if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) { 866 i2c_hid_dbg(ihid, "Using a HID descriptor override\n"); 867 ihid->hdesc = 868 *i2c_hid_get_dmi_i2c_hid_desc_override(client->name); 869 } else { 870 i2c_hid_dbg(ihid, "Fetching the HID descriptor\n"); 871 error = i2c_hid_read_register(ihid, 872 ihid->wHIDDescRegister, 873 &ihid->hdesc, 874 sizeof(ihid->hdesc)); 875 if (error) { 876 dev_err(&ihid->client->dev, 877 "failed to fetch HID descriptor: %d\n", 878 error); 879 return -ENODEV; 880 } 881 } 882 883 /* Validate the length of HID descriptor, the 4 first bytes: 884 * bytes 0-1 -> length 885 * bytes 2-3 -> bcdVersion (has to be 1.00) */ 886 /* check bcdVersion == 1.0 */ 887 if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) { 888 dev_err(&ihid->client->dev, 889 "unexpected HID descriptor bcdVersion (0x%04hx)\n", 890 le16_to_cpu(hdesc->bcdVersion)); 891 return -ENODEV; 892 } 893 894 /* Descriptor length should be 30 bytes as per the specification */ 895 dsize = le16_to_cpu(hdesc->wHIDDescLength); 896 if (dsize != sizeof(struct i2c_hid_desc)) { 897 dev_err(&ihid->client->dev, 898 "weird size of HID descriptor (%u)\n", dsize); 899 return -ENODEV; 900 } 901 i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, &ihid->hdesc); 902 return 0; 903 } 904 905 static int i2c_hid_core_power_up(struct i2c_hid *ihid) 906 { 907 if (!ihid->ops->power_up) 908 return 0; 909 910 return ihid->ops->power_up(ihid->ops); 911 } 912 913 static void i2c_hid_core_power_down(struct i2c_hid *ihid) 914 { 915 if (!ihid->ops->power_down) 916 return; 917 918 ihid->ops->power_down(ihid->ops); 919 } 920 921 static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid) 922 { 923 if (!ihid->ops->shutdown_tail) 924 return; 925 926 ihid->ops->shutdown_tail(ihid->ops); 927 } 928 929 static int i2c_hid_core_suspend(struct i2c_hid *ihid, bool force_poweroff) 930 { 931 struct i2c_client *client = ihid->client; 932 struct hid_device *hid = ihid->hid; 933 int ret; 934 935 ret = hid_driver_suspend(hid, PMSG_SUSPEND); 936 if (ret < 0) 937 return ret; 938 939 /* Save some power */ 940 if (!(ihid->quirks & I2C_HID_QUIRK_NO_SLEEP_ON_SUSPEND)) 941 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP); 942 943 disable_irq(client->irq); 944 945 if (force_poweroff || !device_may_wakeup(&client->dev)) 946 i2c_hid_core_power_down(ihid); 947 948 return 0; 949 } 950 951 static int i2c_hid_core_resume(struct i2c_hid *ihid) 952 { 953 struct i2c_client *client = ihid->client; 954 struct hid_device *hid = ihid->hid; 955 int ret; 956 957 if (!device_may_wakeup(&client->dev)) 958 i2c_hid_core_power_up(ihid); 959 960 enable_irq(client->irq); 961 962 /* Instead of resetting device, simply powers the device on. This 963 * solves "incomplete reports" on Raydium devices 2386:3118 and 964 * 2386:4B33 and fixes various SIS touchscreens no longer sending 965 * data after a suspend/resume. 966 * 967 * However some ALPS touchpads generate IRQ storm without reset, so 968 * let's still reset them here. 969 */ 970 if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME) { 971 mutex_lock(&ihid->reset_lock); 972 ret = i2c_hid_start_hwreset(ihid); 973 if (ret == 0) 974 ret = i2c_hid_finish_hwreset(ihid); 975 mutex_unlock(&ihid->reset_lock); 976 } else { 977 ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON); 978 } 979 980 if (ret) 981 return ret; 982 983 return hid_driver_reset_resume(hid); 984 } 985 986 /* 987 * Check that the device exists and parse the HID descriptor. 988 */ 989 static int __i2c_hid_core_probe(struct i2c_hid *ihid) 990 { 991 struct i2c_client *client = ihid->client; 992 struct hid_device *hid = ihid->hid; 993 int ret; 994 995 /* Make sure there is something at this address */ 996 ret = i2c_smbus_read_byte(client); 997 if (ret < 0) { 998 i2c_hid_dbg(ihid, "nothing at this address: %d\n", ret); 999 return -ENXIO; 1000 } 1001 1002 ret = i2c_hid_fetch_hid_descriptor(ihid); 1003 if (ret < 0) { 1004 dev_err(&client->dev, 1005 "Failed to fetch the HID Descriptor\n"); 1006 return ret; 1007 } 1008 1009 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion); 1010 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID); 1011 hid->product = le16_to_cpu(ihid->hdesc.wProductID); 1012 1013 hid->initial_quirks |= i2c_hid_get_dmi_quirks(hid->vendor, 1014 hid->product); 1015 1016 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X", 1017 client->name, (u16)hid->vendor, (u16)hid->product); 1018 strscpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys)); 1019 1020 ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product); 1021 1022 return 0; 1023 } 1024 1025 static int i2c_hid_core_register_hid(struct i2c_hid *ihid) 1026 { 1027 struct i2c_client *client = ihid->client; 1028 struct hid_device *hid = ihid->hid; 1029 int ret; 1030 1031 enable_irq(client->irq); 1032 1033 ret = hid_add_device(hid); 1034 if (ret) { 1035 if (ret != -ENODEV) 1036 hid_err(client, "can't add hid device: %d\n", ret); 1037 disable_irq(client->irq); 1038 return ret; 1039 } 1040 1041 return 0; 1042 } 1043 1044 static int i2c_hid_core_probe_panel_follower(struct i2c_hid *ihid) 1045 { 1046 int ret; 1047 1048 ret = i2c_hid_core_power_up(ihid); 1049 if (ret) 1050 return ret; 1051 1052 ret = __i2c_hid_core_probe(ihid); 1053 if (ret) 1054 goto err_power_down; 1055 1056 ret = i2c_hid_core_register_hid(ihid); 1057 if (ret) 1058 goto err_power_down; 1059 1060 return 0; 1061 1062 err_power_down: 1063 i2c_hid_core_power_down(ihid); 1064 1065 return ret; 1066 } 1067 1068 static void ihid_core_panel_prepare_work(struct work_struct *work) 1069 { 1070 struct i2c_hid *ihid = container_of(work, struct i2c_hid, 1071 panel_follower_prepare_work); 1072 struct hid_device *hid = ihid->hid; 1073 int ret; 1074 1075 /* 1076 * hid->version is set on the first power up. If it's still zero then 1077 * this is the first power on so we should perform initial power up 1078 * steps. 1079 */ 1080 if (!hid->version) 1081 ret = i2c_hid_core_probe_panel_follower(ihid); 1082 else 1083 ret = i2c_hid_core_resume(ihid); 1084 1085 if (ret) 1086 dev_warn(&ihid->client->dev, "Power on failed: %d\n", ret); 1087 else 1088 WRITE_ONCE(ihid->prepare_work_finished, true); 1089 1090 /* 1091 * The work APIs provide a number of memory ordering guarantees 1092 * including one that says that memory writes before schedule_work() 1093 * are always visible to the work function, but they don't appear to 1094 * guarantee that a write that happened in the work is visible after 1095 * cancel_work_sync(). We'll add a write memory barrier here to match 1096 * with i2c_hid_core_panel_unpreparing() to ensure that our write to 1097 * prepare_work_finished is visible there. 1098 */ 1099 smp_wmb(); 1100 } 1101 1102 static int i2c_hid_core_panel_prepared(struct drm_panel_follower *follower) 1103 { 1104 struct i2c_hid *ihid = container_of(follower, struct i2c_hid, panel_follower); 1105 1106 /* 1107 * Powering on a touchscreen can be a slow process. Queue the work to 1108 * the system workqueue so we don't block the panel's power up. 1109 */ 1110 WRITE_ONCE(ihid->prepare_work_finished, false); 1111 schedule_work(&ihid->panel_follower_prepare_work); 1112 1113 return 0; 1114 } 1115 1116 static int i2c_hid_core_panel_unpreparing(struct drm_panel_follower *follower) 1117 { 1118 struct i2c_hid *ihid = container_of(follower, struct i2c_hid, panel_follower); 1119 1120 cancel_work_sync(&ihid->panel_follower_prepare_work); 1121 1122 /* Match with ihid_core_panel_prepare_work() */ 1123 smp_rmb(); 1124 if (!READ_ONCE(ihid->prepare_work_finished)) 1125 return 0; 1126 1127 return i2c_hid_core_suspend(ihid, true); 1128 } 1129 1130 static const struct drm_panel_follower_funcs i2c_hid_core_panel_follower_funcs = { 1131 .panel_prepared = i2c_hid_core_panel_prepared, 1132 .panel_unpreparing = i2c_hid_core_panel_unpreparing, 1133 }; 1134 1135 static int i2c_hid_core_register_panel_follower(struct i2c_hid *ihid) 1136 { 1137 struct device *dev = &ihid->client->dev; 1138 int ret; 1139 1140 ihid->panel_follower.funcs = &i2c_hid_core_panel_follower_funcs; 1141 1142 /* 1143 * If we're not in control of our own power up/power down then we can't 1144 * do the logic to manage wakeups. Give a warning if a user thought 1145 * that was possible then force the capability off. 1146 */ 1147 if (device_can_wakeup(dev)) { 1148 dev_warn(dev, "Can't wakeup if following panel\n"); 1149 device_set_wakeup_capable(dev, false); 1150 } 1151 1152 ret = drm_panel_add_follower(dev, &ihid->panel_follower); 1153 if (ret) 1154 return ret; 1155 1156 return 0; 1157 } 1158 1159 int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops, 1160 u16 hid_descriptor_address, u32 quirks) 1161 { 1162 int ret; 1163 struct i2c_hid *ihid; 1164 struct hid_device *hid; 1165 1166 dbg_hid("HID probe called for i2c 0x%02x\n", client->addr); 1167 1168 if (!client->irq) { 1169 dev_err(&client->dev, 1170 "HID over i2c has not been provided an Int IRQ\n"); 1171 return -EINVAL; 1172 } 1173 1174 if (client->irq < 0) { 1175 if (client->irq != -EPROBE_DEFER) 1176 dev_err(&client->dev, 1177 "HID over i2c doesn't have a valid IRQ\n"); 1178 return client->irq; 1179 } 1180 1181 ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL); 1182 if (!ihid) 1183 return -ENOMEM; 1184 1185 i2c_set_clientdata(client, ihid); 1186 1187 ihid->ops = ops; 1188 ihid->client = client; 1189 ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address); 1190 ihid->is_panel_follower = drm_is_panel_follower(&client->dev); 1191 1192 init_waitqueue_head(&ihid->wait); 1193 mutex_init(&ihid->reset_lock); 1194 INIT_WORK(&ihid->panel_follower_prepare_work, ihid_core_panel_prepare_work); 1195 1196 /* we need to allocate the command buffer without knowing the maximum 1197 * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the 1198 * real computation later. */ 1199 ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE); 1200 if (ret < 0) 1201 return ret; 1202 device_enable_async_suspend(&client->dev); 1203 1204 hid = hid_allocate_device(); 1205 if (IS_ERR(hid)) { 1206 ret = PTR_ERR(hid); 1207 goto err_free_buffers; 1208 } 1209 1210 ihid->hid = hid; 1211 1212 hid->driver_data = client; 1213 hid->ll_driver = &i2c_hid_ll_driver; 1214 hid->dev.parent = &client->dev; 1215 hid->bus = BUS_I2C; 1216 hid->initial_quirks = quirks; 1217 1218 /* Power on and probe unless device is a panel follower. */ 1219 if (!ihid->is_panel_follower) { 1220 ret = i2c_hid_core_power_up(ihid); 1221 if (ret < 0) 1222 goto err_destroy_device; 1223 1224 ret = __i2c_hid_core_probe(ihid); 1225 if (ret < 0) 1226 goto err_power_down; 1227 } 1228 1229 ret = i2c_hid_init_irq(client); 1230 if (ret < 0) 1231 goto err_power_down; 1232 1233 /* 1234 * If we're a panel follower, we'll register when the panel turns on; 1235 * otherwise we do it right away. 1236 */ 1237 if (ihid->is_panel_follower) 1238 ret = i2c_hid_core_register_panel_follower(ihid); 1239 else 1240 ret = i2c_hid_core_register_hid(ihid); 1241 if (ret) 1242 goto err_free_irq; 1243 1244 return 0; 1245 1246 err_free_irq: 1247 free_irq(client->irq, ihid); 1248 err_power_down: 1249 if (!ihid->is_panel_follower) 1250 i2c_hid_core_power_down(ihid); 1251 err_destroy_device: 1252 hid_destroy_device(hid); 1253 err_free_buffers: 1254 i2c_hid_free_buffers(ihid); 1255 1256 return ret; 1257 } 1258 EXPORT_SYMBOL_GPL(i2c_hid_core_probe); 1259 1260 void i2c_hid_core_remove(struct i2c_client *client) 1261 { 1262 struct i2c_hid *ihid = i2c_get_clientdata(client); 1263 struct hid_device *hid; 1264 1265 /* 1266 * If we're a follower, the act of unfollowing will cause us to be 1267 * powered down. Otherwise we need to manually do it. 1268 */ 1269 if (ihid->is_panel_follower) 1270 drm_panel_remove_follower(&ihid->panel_follower); 1271 else 1272 i2c_hid_core_suspend(ihid, true); 1273 1274 hid = ihid->hid; 1275 hid_destroy_device(hid); 1276 1277 free_irq(client->irq, ihid); 1278 1279 if (ihid->bufsize) 1280 i2c_hid_free_buffers(ihid); 1281 } 1282 EXPORT_SYMBOL_GPL(i2c_hid_core_remove); 1283 1284 void i2c_hid_core_shutdown(struct i2c_client *client) 1285 { 1286 struct i2c_hid *ihid = i2c_get_clientdata(client); 1287 1288 i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP); 1289 free_irq(client->irq, ihid); 1290 1291 i2c_hid_core_shutdown_tail(ihid); 1292 } 1293 EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown); 1294 1295 static int i2c_hid_core_pm_suspend(struct device *dev) 1296 { 1297 struct i2c_client *client = to_i2c_client(dev); 1298 struct i2c_hid *ihid = i2c_get_clientdata(client); 1299 1300 if (ihid->is_panel_follower) 1301 return 0; 1302 1303 return i2c_hid_core_suspend(ihid, false); 1304 } 1305 1306 static int i2c_hid_core_pm_resume(struct device *dev) 1307 { 1308 struct i2c_client *client = to_i2c_client(dev); 1309 struct i2c_hid *ihid = i2c_get_clientdata(client); 1310 1311 if (ihid->is_panel_follower) 1312 return 0; 1313 1314 return i2c_hid_core_resume(ihid); 1315 } 1316 1317 const struct dev_pm_ops i2c_hid_core_pm = { 1318 SYSTEM_SLEEP_PM_OPS(i2c_hid_core_pm_suspend, i2c_hid_core_pm_resume) 1319 }; 1320 EXPORT_SYMBOL_GPL(i2c_hid_core_pm); 1321 1322 MODULE_DESCRIPTION("HID over I2C core driver"); 1323 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); 1324 MODULE_LICENSE("GPL"); 1325