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