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