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