19ee3e066SJulian Sax /* 29ee3e066SJulian Sax * HID over I2C protocol implementation 39ee3e066SJulian Sax * 49ee3e066SJulian Sax * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com> 59ee3e066SJulian Sax * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France 69ee3e066SJulian Sax * Copyright (c) 2012 Red Hat, Inc 79ee3e066SJulian Sax * 89ee3e066SJulian Sax * This code is partly based on "USB HID support for Linux": 99ee3e066SJulian Sax * 109ee3e066SJulian Sax * Copyright (c) 1999 Andreas Gal 119ee3e066SJulian Sax * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz> 129ee3e066SJulian Sax * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc 139ee3e066SJulian Sax * Copyright (c) 2007-2008 Oliver Neukum 149ee3e066SJulian Sax * Copyright (c) 2006-2010 Jiri Kosina 159ee3e066SJulian Sax * 169ee3e066SJulian Sax * This file is subject to the terms and conditions of the GNU General Public 179ee3e066SJulian Sax * License. See the file COPYING in the main directory of this archive for 189ee3e066SJulian Sax * more details. 199ee3e066SJulian Sax */ 209ee3e066SJulian Sax 219ee3e066SJulian Sax #include <linux/module.h> 229ee3e066SJulian Sax #include <linux/i2c.h> 239ee3e066SJulian Sax #include <linux/interrupt.h> 249ee3e066SJulian Sax #include <linux/input.h> 259ee3e066SJulian Sax #include <linux/irq.h> 269ee3e066SJulian Sax #include <linux/delay.h> 279ee3e066SJulian Sax #include <linux/slab.h> 289ee3e066SJulian Sax #include <linux/pm.h> 29d08999ccSRaul E Rangel #include <linux/pm_wakeirq.h> 309ee3e066SJulian Sax #include <linux/device.h> 319ee3e066SJulian Sax #include <linux/wait.h> 329ee3e066SJulian Sax #include <linux/err.h> 339ee3e066SJulian Sax #include <linux/string.h> 349ee3e066SJulian Sax #include <linux/list.h> 359ee3e066SJulian Sax #include <linux/jiffies.h> 369ee3e066SJulian Sax #include <linux/kernel.h> 379ee3e066SJulian Sax #include <linux/hid.h> 389ee3e066SJulian Sax #include <linux/mutex.h> 39dbe0dd5fSDmitry Torokhov #include <asm/unaligned.h> 409ee3e066SJulian Sax 419ee3e066SJulian Sax #include "../hid-ids.h" 429ee3e066SJulian Sax #include "i2c-hid.h" 439ee3e066SJulian Sax 449ee3e066SJulian Sax /* quirks to control the device */ 459ee3e066SJulian Sax #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0) 469ee3e066SJulian Sax #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1) 471475af25SKai-Heng Feng #define I2C_HID_QUIRK_BOGUS_IRQ BIT(4) 48fd70466dSKai-Heng Feng #define I2C_HID_QUIRK_RESET_ON_RESUME BIT(5) 49fd091376SPavel Balan #define I2C_HID_QUIRK_BAD_INPUT_SIZE BIT(6) 50ca66a677SJohnny Chuang #define I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET BIT(7) 51fd091376SPavel Balan 52dbe0dd5fSDmitry Torokhov /* Command opcodes */ 53dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_RESET 0x01 54dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_GET_REPORT 0x02 55dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_SET_REPORT 0x03 56dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_GET_IDLE 0x04 57dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_SET_IDLE 0x05 58dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_GET_PROTOCOL 0x06 59dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_SET_PROTOCOL 0x07 60dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_SET_POWER 0x08 619ee3e066SJulian Sax 629ee3e066SJulian Sax /* flags */ 639ee3e066SJulian Sax #define I2C_HID_STARTED 0 649ee3e066SJulian Sax #define I2C_HID_RESET_PENDING 1 659ee3e066SJulian Sax #define I2C_HID_READ_PENDING 2 669ee3e066SJulian Sax 679ee3e066SJulian Sax #define I2C_HID_PWR_ON 0x00 689ee3e066SJulian Sax #define I2C_HID_PWR_SLEEP 0x01 699ee3e066SJulian Sax 7034ba3657SThomas Weißschuh #define i2c_hid_dbg(ihid, ...) dev_dbg(&(ihid)->client->dev, __VA_ARGS__) 719ee3e066SJulian Sax 729ee3e066SJulian Sax struct i2c_hid_desc { 739ee3e066SJulian Sax __le16 wHIDDescLength; 749ee3e066SJulian Sax __le16 bcdVersion; 759ee3e066SJulian Sax __le16 wReportDescLength; 769ee3e066SJulian Sax __le16 wReportDescRegister; 779ee3e066SJulian Sax __le16 wInputRegister; 789ee3e066SJulian Sax __le16 wMaxInputLength; 799ee3e066SJulian Sax __le16 wOutputRegister; 809ee3e066SJulian Sax __le16 wMaxOutputLength; 819ee3e066SJulian Sax __le16 wCommandRegister; 829ee3e066SJulian Sax __le16 wDataRegister; 839ee3e066SJulian Sax __le16 wVendorID; 849ee3e066SJulian Sax __le16 wProductID; 859ee3e066SJulian Sax __le16 wVersionID; 869ee3e066SJulian Sax __le32 reserved; 879ee3e066SJulian Sax } __packed; 889ee3e066SJulian Sax 899ee3e066SJulian Sax /* The main device structure */ 909ee3e066SJulian Sax struct i2c_hid { 919ee3e066SJulian Sax struct i2c_client *client; /* i2c client */ 929ee3e066SJulian Sax struct hid_device *hid; /* pointer to corresponding HID dev */ 939ee3e066SJulian Sax struct i2c_hid_desc hdesc; /* the HID Descriptor */ 949ee3e066SJulian Sax __le16 wHIDDescRegister; /* location of the i2c 959ee3e066SJulian Sax * register of the HID 969ee3e066SJulian Sax * descriptor. */ 979ee3e066SJulian Sax unsigned int bufsize; /* i2c buffer size */ 989ee3e066SJulian Sax u8 *inbuf; /* Input buffer */ 999ee3e066SJulian Sax u8 *rawbuf; /* Raw Input buffer */ 1009ee3e066SJulian Sax u8 *cmdbuf; /* Command buffer */ 1019ee3e066SJulian Sax 1029ee3e066SJulian Sax unsigned long flags; /* device flags */ 1039ee3e066SJulian Sax unsigned long quirks; /* Various quirks */ 1049ee3e066SJulian Sax 1059ee3e066SJulian Sax wait_queue_head_t wait; /* For waiting the interrupt */ 1069ee3e066SJulian Sax 1079ee3e066SJulian Sax struct mutex reset_lock; 108b33752c3SDouglas Anderson 109b33752c3SDouglas Anderson struct i2chid_ops *ops; 1109ee3e066SJulian Sax }; 1119ee3e066SJulian Sax 1129ee3e066SJulian Sax static const struct i2c_hid_quirks { 1139ee3e066SJulian Sax __u16 idVendor; 1149ee3e066SJulian Sax __u16 idProduct; 1159ee3e066SJulian Sax __u32 quirks; 1169ee3e066SJulian Sax } i2c_hid_quirks[] = { 117b20bef4bSHungNien Chen { USB_VENDOR_ID_WEIDA, HID_ANY_ID, 1189ee3e066SJulian Sax I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV }, 1199ee3e066SJulian Sax { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288, 12067b18dfbSKai-Heng Feng I2C_HID_QUIRK_NO_IRQ_AFTER_RESET }, 121fc6a31b0SHans de Goede { I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15, 122fc6a31b0SHans de Goede I2C_HID_QUIRK_NO_IRQ_AFTER_RESET }, 1230c843223SAaron Ma { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118, 1240c843223SAaron Ma I2C_HID_QUIRK_NO_IRQ_AFTER_RESET }, 125fd70466dSKai-Heng Feng { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID, 126fd70466dSKai-Heng Feng I2C_HID_QUIRK_RESET_ON_RESUME }, 127538f6740SDaniel Playfair Cal { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393, 128538f6740SDaniel Playfair Cal I2C_HID_QUIRK_RESET_ON_RESUME }, 129fd091376SPavel Balan { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720, 130fd091376SPavel Balan I2C_HID_QUIRK_BAD_INPUT_SIZE }, 131ca66a677SJohnny Chuang /* 132ca66a677SJohnny Chuang * Sending the wakeup after reset actually break ELAN touchscreen controller 133ca66a677SJohnny Chuang */ 134ca66a677SJohnny Chuang { USB_VENDOR_ID_ELAN, HID_ANY_ID, 13578653706SJim Broadus I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET | 13678653706SJim Broadus I2C_HID_QUIRK_BOGUS_IRQ }, 1379ee3e066SJulian Sax { 0, 0 } 1389ee3e066SJulian Sax }; 1399ee3e066SJulian Sax 1409ee3e066SJulian Sax /* 1419ee3e066SJulian Sax * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device 1429ee3e066SJulian Sax * @idVendor: the 16-bit vendor ID 1439ee3e066SJulian Sax * @idProduct: the 16-bit product ID 1449ee3e066SJulian Sax * 1459ee3e066SJulian Sax * Returns: a u32 quirks value. 1469ee3e066SJulian Sax */ 1479ee3e066SJulian Sax static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct) 1489ee3e066SJulian Sax { 1499ee3e066SJulian Sax u32 quirks = 0; 1509ee3e066SJulian Sax int n; 1519ee3e066SJulian Sax 1529ee3e066SJulian Sax for (n = 0; i2c_hid_quirks[n].idVendor; n++) 1539ee3e066SJulian Sax if (i2c_hid_quirks[n].idVendor == idVendor && 1549ee3e066SJulian Sax (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID || 1559ee3e066SJulian Sax i2c_hid_quirks[n].idProduct == idProduct)) 1569ee3e066SJulian Sax quirks = i2c_hid_quirks[n].quirks; 1579ee3e066SJulian Sax 1589ee3e066SJulian Sax return quirks; 1599ee3e066SJulian Sax } 1609ee3e066SJulian Sax 161dbe0dd5fSDmitry Torokhov static int i2c_hid_xfer(struct i2c_hid *ihid, 162dbe0dd5fSDmitry Torokhov u8 *send_buf, int send_len, u8 *recv_buf, int recv_len) 163dbe0dd5fSDmitry Torokhov { 164dbe0dd5fSDmitry Torokhov struct i2c_client *client = ihid->client; 165dbe0dd5fSDmitry Torokhov struct i2c_msg msgs[2] = { 0 }; 166dbe0dd5fSDmitry Torokhov int n = 0; 167dbe0dd5fSDmitry Torokhov int ret; 168dbe0dd5fSDmitry Torokhov 169dbe0dd5fSDmitry Torokhov if (send_len) { 170dbe0dd5fSDmitry Torokhov i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", 171dbe0dd5fSDmitry Torokhov __func__, send_len, send_buf); 172dbe0dd5fSDmitry Torokhov 173dbe0dd5fSDmitry Torokhov msgs[n].addr = client->addr; 1741c4d6cd4SDmitry Torokhov msgs[n].flags = (client->flags & I2C_M_TEN) | I2C_M_DMA_SAFE; 175dbe0dd5fSDmitry Torokhov msgs[n].len = send_len; 176dbe0dd5fSDmitry Torokhov msgs[n].buf = send_buf; 177dbe0dd5fSDmitry Torokhov n++; 178dbe0dd5fSDmitry Torokhov } 179dbe0dd5fSDmitry Torokhov 180dbe0dd5fSDmitry Torokhov if (recv_len) { 181dbe0dd5fSDmitry Torokhov msgs[n].addr = client->addr; 1821c4d6cd4SDmitry Torokhov msgs[n].flags = (client->flags & I2C_M_TEN) | 1831c4d6cd4SDmitry Torokhov I2C_M_RD | I2C_M_DMA_SAFE; 184dbe0dd5fSDmitry Torokhov msgs[n].len = recv_len; 185dbe0dd5fSDmitry Torokhov msgs[n].buf = recv_buf; 186dbe0dd5fSDmitry Torokhov n++; 187dbe0dd5fSDmitry Torokhov 188dbe0dd5fSDmitry Torokhov set_bit(I2C_HID_READ_PENDING, &ihid->flags); 189dbe0dd5fSDmitry Torokhov } 190dbe0dd5fSDmitry Torokhov 191dbe0dd5fSDmitry Torokhov ret = i2c_transfer(client->adapter, msgs, n); 192dbe0dd5fSDmitry Torokhov 193dbe0dd5fSDmitry Torokhov if (recv_len) 194dbe0dd5fSDmitry Torokhov clear_bit(I2C_HID_READ_PENDING, &ihid->flags); 195dbe0dd5fSDmitry Torokhov 196dbe0dd5fSDmitry Torokhov if (ret != n) 197dbe0dd5fSDmitry Torokhov return ret < 0 ? ret : -EIO; 198dbe0dd5fSDmitry Torokhov 199dbe0dd5fSDmitry Torokhov return 0; 200dbe0dd5fSDmitry Torokhov } 201dbe0dd5fSDmitry Torokhov 2028399bd01SDmitry Torokhov static int i2c_hid_read_register(struct i2c_hid *ihid, __le16 reg, 2038399bd01SDmitry Torokhov void *buf, size_t len) 2048399bd01SDmitry Torokhov { 2058399bd01SDmitry Torokhov *(__le16 *)ihid->cmdbuf = reg; 2068399bd01SDmitry Torokhov 2078399bd01SDmitry Torokhov return i2c_hid_xfer(ihid, ihid->cmdbuf, sizeof(__le16), buf, len); 2088399bd01SDmitry Torokhov } 2098399bd01SDmitry Torokhov 210dbe0dd5fSDmitry Torokhov static size_t i2c_hid_encode_command(u8 *buf, u8 opcode, 211dbe0dd5fSDmitry Torokhov int report_type, int report_id) 212dbe0dd5fSDmitry Torokhov { 213dbe0dd5fSDmitry Torokhov size_t length = 0; 214dbe0dd5fSDmitry Torokhov 215dbe0dd5fSDmitry Torokhov if (report_id < 0x0F) { 216dbe0dd5fSDmitry Torokhov buf[length++] = report_type << 4 | report_id; 217dbe0dd5fSDmitry Torokhov buf[length++] = opcode; 218dbe0dd5fSDmitry Torokhov } else { 219dbe0dd5fSDmitry Torokhov buf[length++] = report_type << 4 | 0x0F; 220dbe0dd5fSDmitry Torokhov buf[length++] = opcode; 221dbe0dd5fSDmitry Torokhov buf[length++] = report_id; 222dbe0dd5fSDmitry Torokhov } 223dbe0dd5fSDmitry Torokhov 224dbe0dd5fSDmitry Torokhov return length; 225dbe0dd5fSDmitry Torokhov } 226dbe0dd5fSDmitry Torokhov 22785df7133SDmitry Torokhov static int i2c_hid_get_report(struct i2c_hid *ihid, 22885df7133SDmitry Torokhov u8 report_type, u8 report_id, 22985df7133SDmitry Torokhov u8 *recv_buf, size_t recv_len) 2309ee3e066SJulian Sax { 23185df7133SDmitry Torokhov size_t length = 0; 23285df7133SDmitry Torokhov size_t ret_count; 23385df7133SDmitry Torokhov int error; 2349ee3e066SJulian Sax 2359ee3e066SJulian Sax i2c_hid_dbg(ihid, "%s\n", __func__); 2369ee3e066SJulian Sax 23785df7133SDmitry Torokhov /* Command register goes first */ 23885df7133SDmitry Torokhov *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister; 23985df7133SDmitry Torokhov length += sizeof(__le16); 24085df7133SDmitry Torokhov /* Next is GET_REPORT command */ 24185df7133SDmitry Torokhov length += i2c_hid_encode_command(ihid->cmdbuf + length, 24285df7133SDmitry Torokhov I2C_HID_OPCODE_GET_REPORT, 24385df7133SDmitry Torokhov report_type, report_id); 24485df7133SDmitry Torokhov /* 24585df7133SDmitry Torokhov * Device will send report data through data register. Because 24685df7133SDmitry Torokhov * command can be either 2 or 3 bytes destination for the data 24785df7133SDmitry Torokhov * register may be not aligned. 24885df7133SDmitry Torokhov */ 24985df7133SDmitry Torokhov put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister), 25085df7133SDmitry Torokhov ihid->cmdbuf + length); 25185df7133SDmitry Torokhov length += sizeof(__le16); 2529ee3e066SJulian Sax 25385df7133SDmitry Torokhov /* 25485df7133SDmitry Torokhov * In addition to report data device will supply data length 25585df7133SDmitry Torokhov * in the first 2 bytes of the response, so adjust . 25685df7133SDmitry Torokhov */ 25785df7133SDmitry Torokhov error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, 25885df7133SDmitry Torokhov ihid->rawbuf, recv_len + sizeof(__le16)); 25985df7133SDmitry Torokhov if (error) { 260d34c6105SDmitry Torokhov dev_err(&ihid->client->dev, 26185df7133SDmitry Torokhov "failed to set a report to device: %d\n", error); 26285df7133SDmitry Torokhov return error; 2639ee3e066SJulian Sax } 2649ee3e066SJulian Sax 26585df7133SDmitry Torokhov /* The buffer is sufficiently aligned */ 26685df7133SDmitry Torokhov ret_count = le16_to_cpup((__le16 *)ihid->rawbuf); 26785df7133SDmitry Torokhov 26885df7133SDmitry Torokhov /* Check for empty report response */ 26985df7133SDmitry Torokhov if (ret_count <= sizeof(__le16)) 2709ee3e066SJulian Sax return 0; 27185df7133SDmitry Torokhov 27285df7133SDmitry Torokhov recv_len = min(recv_len, ret_count - sizeof(__le16)); 27385df7133SDmitry Torokhov memcpy(recv_buf, ihid->rawbuf + sizeof(__le16), recv_len); 27485df7133SDmitry Torokhov 27585df7133SDmitry Torokhov if (report_id && recv_len != 0 && recv_buf[0] != report_id) { 27685df7133SDmitry Torokhov dev_err(&ihid->client->dev, 27785df7133SDmitry Torokhov "device returned incorrect report (%d vs %d expected)\n", 27885df7133SDmitry Torokhov recv_buf[0], report_id); 27985df7133SDmitry Torokhov return -EINVAL; 28085df7133SDmitry Torokhov } 28185df7133SDmitry Torokhov 28285df7133SDmitry Torokhov return recv_len; 2839ee3e066SJulian Sax } 2849ee3e066SJulian Sax 285dbe0dd5fSDmitry Torokhov static size_t i2c_hid_format_report(u8 *buf, int report_id, 286dbe0dd5fSDmitry Torokhov const u8 *data, size_t size) 287dbe0dd5fSDmitry Torokhov { 288dbe0dd5fSDmitry Torokhov size_t length = sizeof(__le16); /* reserve space to store size */ 289dbe0dd5fSDmitry Torokhov 290dbe0dd5fSDmitry Torokhov if (report_id) 291dbe0dd5fSDmitry Torokhov buf[length++] = report_id; 292dbe0dd5fSDmitry Torokhov 293dbe0dd5fSDmitry Torokhov memcpy(buf + length, data, size); 294dbe0dd5fSDmitry Torokhov length += size; 295dbe0dd5fSDmitry Torokhov 296dbe0dd5fSDmitry Torokhov /* Store overall size in the beginning of the buffer */ 297dbe0dd5fSDmitry Torokhov put_unaligned_le16(length, buf); 298dbe0dd5fSDmitry Torokhov 299dbe0dd5fSDmitry Torokhov return length; 300dbe0dd5fSDmitry Torokhov } 301dbe0dd5fSDmitry Torokhov 3029ee3e066SJulian Sax /** 3039ee3e066SJulian Sax * i2c_hid_set_or_send_report: forward an incoming report to the device 304d34c6105SDmitry Torokhov * @ihid: the i2c hid device 305dbe0dd5fSDmitry Torokhov * @report_type: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT 306dbe0dd5fSDmitry Torokhov * @report_id: the report ID 3079ee3e066SJulian Sax * @buf: the actual data to transfer, without the report ID 308ca43ab1eSXiaofei Tan * @data_len: size of buf 309dbe0dd5fSDmitry Torokhov * @do_set: true: use SET_REPORT HID command, false: send plain OUTPUT report 3109ee3e066SJulian Sax */ 311dbe0dd5fSDmitry Torokhov static int i2c_hid_set_or_send_report(struct i2c_hid *ihid, 312dbe0dd5fSDmitry Torokhov u8 report_type, u8 report_id, 313dbe0dd5fSDmitry Torokhov const u8 *buf, size_t data_len, 314dbe0dd5fSDmitry Torokhov bool do_set) 3159ee3e066SJulian Sax { 316dbe0dd5fSDmitry Torokhov size_t length = 0; 317dbe0dd5fSDmitry Torokhov int error; 3189ee3e066SJulian Sax 3199ee3e066SJulian Sax i2c_hid_dbg(ihid, "%s\n", __func__); 3209ee3e066SJulian Sax 3219ee3e066SJulian Sax if (data_len > ihid->bufsize) 3229ee3e066SJulian Sax return -EINVAL; 3239ee3e066SJulian Sax 324dbe0dd5fSDmitry Torokhov if (!do_set && le16_to_cpu(ihid->hdesc.wMaxOutputLength) == 0) 3259ee3e066SJulian Sax return -ENOSYS; 3269ee3e066SJulian Sax 327dbe0dd5fSDmitry Torokhov if (do_set) { 328dbe0dd5fSDmitry Torokhov /* Command register goes first */ 329dbe0dd5fSDmitry Torokhov *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister; 330dbe0dd5fSDmitry Torokhov length += sizeof(__le16); 331dbe0dd5fSDmitry Torokhov /* Next is SET_REPORT command */ 332dbe0dd5fSDmitry Torokhov length += i2c_hid_encode_command(ihid->cmdbuf + length, 333dbe0dd5fSDmitry Torokhov I2C_HID_OPCODE_SET_REPORT, 334dbe0dd5fSDmitry Torokhov report_type, report_id); 3359ee3e066SJulian Sax /* 336dbe0dd5fSDmitry Torokhov * Report data will go into the data register. Because 337dbe0dd5fSDmitry Torokhov * command can be either 2 or 3 bytes destination for 338dbe0dd5fSDmitry Torokhov * the data register may be not aligned. 3399ee3e066SJulian Sax */ 340dbe0dd5fSDmitry Torokhov put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister), 341dbe0dd5fSDmitry Torokhov ihid->cmdbuf + length); 342dbe0dd5fSDmitry Torokhov length += sizeof(__le16); 3439ee3e066SJulian Sax } else { 344dbe0dd5fSDmitry Torokhov /* 345dbe0dd5fSDmitry Torokhov * With simple "send report" all data goes into the output 346dbe0dd5fSDmitry Torokhov * register. 347dbe0dd5fSDmitry Torokhov */ 348269ecc0cSYang Li *(__le16 *)ihid->cmdbuf = ihid->hdesc.wOutputRegister; 349dbe0dd5fSDmitry Torokhov length += sizeof(__le16); 3509ee3e066SJulian Sax } 3519ee3e066SJulian Sax 352dbe0dd5fSDmitry Torokhov length += i2c_hid_format_report(ihid->cmdbuf + length, 353dbe0dd5fSDmitry Torokhov report_id, buf, data_len); 3549ee3e066SJulian Sax 355dbe0dd5fSDmitry Torokhov error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0); 356dbe0dd5fSDmitry Torokhov if (error) { 357d34c6105SDmitry Torokhov dev_err(&ihid->client->dev, 358dbe0dd5fSDmitry Torokhov "failed to set a report to device: %d\n", error); 359dbe0dd5fSDmitry Torokhov return error; 3609ee3e066SJulian Sax } 3619ee3e066SJulian Sax 3629ee3e066SJulian Sax return data_len; 3639ee3e066SJulian Sax } 3649ee3e066SJulian Sax 365acb8dd95SDmitry Torokhov static int i2c_hid_set_power_command(struct i2c_hid *ihid, int power_state) 366acb8dd95SDmitry Torokhov { 367acb8dd95SDmitry Torokhov size_t length; 368acb8dd95SDmitry Torokhov 369acb8dd95SDmitry Torokhov /* SET_POWER uses command register */ 370acb8dd95SDmitry Torokhov *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister; 371acb8dd95SDmitry Torokhov length = sizeof(__le16); 372acb8dd95SDmitry Torokhov 373acb8dd95SDmitry Torokhov /* Now the command itself */ 374acb8dd95SDmitry Torokhov length += i2c_hid_encode_command(ihid->cmdbuf + length, 375acb8dd95SDmitry Torokhov I2C_HID_OPCODE_SET_POWER, 376acb8dd95SDmitry Torokhov 0, power_state); 377acb8dd95SDmitry Torokhov 378acb8dd95SDmitry Torokhov return i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0); 379acb8dd95SDmitry Torokhov } 380acb8dd95SDmitry Torokhov 381d34c6105SDmitry Torokhov static int i2c_hid_set_power(struct i2c_hid *ihid, int power_state) 3829ee3e066SJulian Sax { 3839ee3e066SJulian Sax int ret; 3849ee3e066SJulian Sax 3859ee3e066SJulian Sax i2c_hid_dbg(ihid, "%s\n", __func__); 3869ee3e066SJulian Sax 3879ee3e066SJulian Sax /* 3889ee3e066SJulian Sax * Some devices require to send a command to wakeup before power on. 3899ee3e066SJulian Sax * The call will get a return value (EREMOTEIO) but device will be 3909ee3e066SJulian Sax * triggered and activated. After that, it goes like a normal device. 3919ee3e066SJulian Sax */ 3929ee3e066SJulian Sax if (power_state == I2C_HID_PWR_ON && 3939ee3e066SJulian Sax ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) { 394acb8dd95SDmitry Torokhov ret = i2c_hid_set_power_command(ihid, I2C_HID_PWR_ON); 3959ee3e066SJulian Sax 3969ee3e066SJulian Sax /* Device was already activated */ 3979ee3e066SJulian Sax if (!ret) 3989ee3e066SJulian Sax goto set_pwr_exit; 3999ee3e066SJulian Sax } 4009ee3e066SJulian Sax 401acb8dd95SDmitry Torokhov ret = i2c_hid_set_power_command(ihid, power_state); 4029ee3e066SJulian Sax if (ret) 403d34c6105SDmitry Torokhov dev_err(&ihid->client->dev, 404d34c6105SDmitry Torokhov "failed to change power setting.\n"); 4059ee3e066SJulian Sax 4069ee3e066SJulian Sax set_pwr_exit: 407eef40162SHans de Goede 408eef40162SHans de Goede /* 409eef40162SHans de Goede * The HID over I2C specification states that if a DEVICE needs time 410eef40162SHans de Goede * after the PWR_ON request, it should utilise CLOCK stretching. 411eef40162SHans de Goede * However, it has been observered that the Windows driver provides a 412eef40162SHans de Goede * 1ms sleep between the PWR_ON and RESET requests. 413eef40162SHans de Goede * According to Goodix Windows even waits 60 ms after (other?) 414eef40162SHans de Goede * PWR_ON requests. Testing has confirmed that several devices 415eef40162SHans de Goede * will not work properly without a delay after a PWR_ON request. 416eef40162SHans de Goede */ 417eef40162SHans de Goede if (!ret && power_state == I2C_HID_PWR_ON) 418eef40162SHans de Goede msleep(60); 419eef40162SHans de Goede 4209ee3e066SJulian Sax return ret; 4219ee3e066SJulian Sax } 4229ee3e066SJulian Sax 423b26fc316SDmitry Torokhov static int i2c_hid_execute_reset(struct i2c_hid *ihid) 424b26fc316SDmitry Torokhov { 42550c5249fSDmitry Torokhov size_t length = 0; 426b26fc316SDmitry Torokhov int ret; 427b26fc316SDmitry Torokhov 428b26fc316SDmitry Torokhov i2c_hid_dbg(ihid, "resetting...\n"); 429b26fc316SDmitry Torokhov 43050c5249fSDmitry Torokhov /* Prepare reset command. Command register goes first. */ 43150c5249fSDmitry Torokhov *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister; 43250c5249fSDmitry Torokhov length += sizeof(__le16); 43350c5249fSDmitry Torokhov /* Next is RESET command itself */ 43450c5249fSDmitry Torokhov length += i2c_hid_encode_command(ihid->cmdbuf + length, 43550c5249fSDmitry Torokhov I2C_HID_OPCODE_RESET, 0, 0); 43650c5249fSDmitry Torokhov 437b26fc316SDmitry Torokhov set_bit(I2C_HID_RESET_PENDING, &ihid->flags); 438b26fc316SDmitry Torokhov 43950c5249fSDmitry Torokhov ret = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0); 440b26fc316SDmitry Torokhov if (ret) { 441b26fc316SDmitry Torokhov dev_err(&ihid->client->dev, "failed to reset device.\n"); 442b26fc316SDmitry Torokhov goto out; 443b26fc316SDmitry Torokhov } 444b26fc316SDmitry Torokhov 445b26fc316SDmitry Torokhov if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) { 446b26fc316SDmitry Torokhov msleep(100); 447b26fc316SDmitry Torokhov goto out; 448b26fc316SDmitry Torokhov } 449b26fc316SDmitry Torokhov 450b26fc316SDmitry Torokhov i2c_hid_dbg(ihid, "%s: waiting...\n", __func__); 451b26fc316SDmitry Torokhov if (!wait_event_timeout(ihid->wait, 452b26fc316SDmitry Torokhov !test_bit(I2C_HID_RESET_PENDING, &ihid->flags), 453b26fc316SDmitry Torokhov msecs_to_jiffies(5000))) { 454b26fc316SDmitry Torokhov ret = -ENODATA; 455b26fc316SDmitry Torokhov goto out; 456b26fc316SDmitry Torokhov } 457b26fc316SDmitry Torokhov i2c_hid_dbg(ihid, "%s: finished.\n", __func__); 458b26fc316SDmitry Torokhov 459b26fc316SDmitry Torokhov out: 460b26fc316SDmitry Torokhov clear_bit(I2C_HID_RESET_PENDING, &ihid->flags); 461b26fc316SDmitry Torokhov return ret; 462b26fc316SDmitry Torokhov } 463b26fc316SDmitry Torokhov 464d34c6105SDmitry Torokhov static int i2c_hid_hwreset(struct i2c_hid *ihid) 4659ee3e066SJulian Sax { 4669ee3e066SJulian Sax int ret; 4679ee3e066SJulian Sax 4689ee3e066SJulian Sax i2c_hid_dbg(ihid, "%s\n", __func__); 4699ee3e066SJulian Sax 4709ee3e066SJulian Sax /* 4719ee3e066SJulian Sax * This prevents sending feature reports while the device is 4729ee3e066SJulian Sax * being reset. Otherwise we may lose the reset complete 4739ee3e066SJulian Sax * interrupt. 4749ee3e066SJulian Sax */ 4759ee3e066SJulian Sax mutex_lock(&ihid->reset_lock); 4769ee3e066SJulian Sax 477d34c6105SDmitry Torokhov ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON); 4789ee3e066SJulian Sax if (ret) 4799ee3e066SJulian Sax goto out_unlock; 4809ee3e066SJulian Sax 481b26fc316SDmitry Torokhov ret = i2c_hid_execute_reset(ihid); 4829ee3e066SJulian Sax if (ret) { 483b26fc316SDmitry Torokhov dev_err(&ihid->client->dev, 484b26fc316SDmitry Torokhov "failed to reset device: %d\n", ret); 485d34c6105SDmitry Torokhov i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP); 48643b7029fSHans de Goede goto out_unlock; 4879ee3e066SJulian Sax } 4889ee3e066SJulian Sax 48943b7029fSHans de Goede /* At least some SIS devices need this after reset */ 490ca66a677SJohnny Chuang if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET)) 491d34c6105SDmitry Torokhov ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON); 49243b7029fSHans de Goede 4939ee3e066SJulian Sax out_unlock: 4949ee3e066SJulian Sax mutex_unlock(&ihid->reset_lock); 4959ee3e066SJulian Sax return ret; 4969ee3e066SJulian Sax } 4979ee3e066SJulian Sax 4989ee3e066SJulian Sax static void i2c_hid_get_input(struct i2c_hid *ihid) 4999ee3e066SJulian Sax { 50086fc3fd2SDmitry Torokhov u16 size = le16_to_cpu(ihid->hdesc.wMaxInputLength); 50186fc3fd2SDmitry Torokhov u16 ret_size; 5029ee3e066SJulian Sax int ret; 5039ee3e066SJulian Sax 5049ee3e066SJulian Sax if (size > ihid->bufsize) 5059ee3e066SJulian Sax size = ihid->bufsize; 5069ee3e066SJulian Sax 5079ee3e066SJulian Sax ret = i2c_master_recv(ihid->client, ihid->inbuf, size); 5089ee3e066SJulian Sax if (ret != size) { 5099ee3e066SJulian Sax if (ret < 0) 5109ee3e066SJulian Sax return; 5119ee3e066SJulian Sax 5129ee3e066SJulian Sax dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n", 5139ee3e066SJulian Sax __func__, ret, size); 5149ee3e066SJulian Sax return; 5159ee3e066SJulian Sax } 5169ee3e066SJulian Sax 51786fc3fd2SDmitry Torokhov /* Receiving buffer is properly aligned */ 51886fc3fd2SDmitry Torokhov ret_size = le16_to_cpup((__le16 *)ihid->inbuf); 5199ee3e066SJulian Sax if (!ret_size) { 5209ee3e066SJulian Sax /* host or device initiated RESET completed */ 5219ee3e066SJulian Sax if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags)) 5229ee3e066SJulian Sax wake_up(&ihid->wait); 5239ee3e066SJulian Sax return; 5249ee3e066SJulian Sax } 5259ee3e066SJulian Sax 52686fc3fd2SDmitry Torokhov if ((ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ) && ret_size == 0xffff) { 52786fc3fd2SDmitry Torokhov dev_warn_once(&ihid->client->dev, 52886fc3fd2SDmitry Torokhov "%s: IRQ triggered but there's no data\n", 52986fc3fd2SDmitry Torokhov __func__); 5301475af25SKai-Heng Feng return; 5311475af25SKai-Heng Feng } 5321475af25SKai-Heng Feng 53386fc3fd2SDmitry Torokhov if (ret_size > size || ret_size < sizeof(__le16)) { 534fd091376SPavel Balan if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) { 53586fc3fd2SDmitry Torokhov *(__le16 *)ihid->inbuf = cpu_to_le16(size); 536fd091376SPavel Balan ret_size = size; 537fd091376SPavel Balan } else { 53886fc3fd2SDmitry Torokhov dev_err(&ihid->client->dev, 53986fc3fd2SDmitry Torokhov "%s: incomplete report (%d/%d)\n", 5409ee3e066SJulian Sax __func__, size, ret_size); 5419ee3e066SJulian Sax return; 5429ee3e066SJulian Sax } 543fd091376SPavel Balan } 5449ee3e066SJulian Sax 5459ee3e066SJulian Sax i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf); 5469ee3e066SJulian Sax 547d951ae1cSMatthias Kaehlcke if (test_bit(I2C_HID_STARTED, &ihid->flags)) { 5489984fbf5SDmitry Torokhov if (ihid->hid->group != HID_GROUP_RMI) 549d951ae1cSMatthias Kaehlcke pm_wakeup_event(&ihid->client->dev, 0); 550d951ae1cSMatthias Kaehlcke 55186fc3fd2SDmitry Torokhov hid_input_report(ihid->hid, HID_INPUT_REPORT, 55286fc3fd2SDmitry Torokhov ihid->inbuf + sizeof(__le16), 55386fc3fd2SDmitry Torokhov ret_size - sizeof(__le16), 1); 554d951ae1cSMatthias Kaehlcke } 5559ee3e066SJulian Sax 5569ee3e066SJulian Sax return; 5579ee3e066SJulian Sax } 5589ee3e066SJulian Sax 5599ee3e066SJulian Sax static irqreturn_t i2c_hid_irq(int irq, void *dev_id) 5609ee3e066SJulian Sax { 5619ee3e066SJulian Sax struct i2c_hid *ihid = dev_id; 5629ee3e066SJulian Sax 5639ee3e066SJulian Sax if (test_bit(I2C_HID_READ_PENDING, &ihid->flags)) 5649ee3e066SJulian Sax return IRQ_HANDLED; 5659ee3e066SJulian Sax 5669ee3e066SJulian Sax i2c_hid_get_input(ihid); 5679ee3e066SJulian Sax 5689ee3e066SJulian Sax return IRQ_HANDLED; 5699ee3e066SJulian Sax } 5709ee3e066SJulian Sax 5719ee3e066SJulian Sax static int i2c_hid_get_report_length(struct hid_report *report) 5729ee3e066SJulian Sax { 5739ee3e066SJulian Sax return ((report->size - 1) >> 3) + 1 + 5749ee3e066SJulian Sax report->device->report_enum[report->type].numbered + 2; 5759ee3e066SJulian Sax } 5769ee3e066SJulian Sax 5779ee3e066SJulian Sax /* 5789ee3e066SJulian Sax * Traverse the supplied list of reports and find the longest 5799ee3e066SJulian Sax */ 5809ee3e066SJulian Sax static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type, 5819ee3e066SJulian Sax unsigned int *max) 5829ee3e066SJulian Sax { 5839ee3e066SJulian Sax struct hid_report *report; 5849ee3e066SJulian Sax unsigned int size; 5859ee3e066SJulian Sax 5869ee3e066SJulian Sax /* We should not rely on wMaxInputLength, as some devices may set it to 5879ee3e066SJulian Sax * a wrong length. */ 5889ee3e066SJulian Sax list_for_each_entry(report, &hid->report_enum[type].report_list, list) { 5899ee3e066SJulian Sax size = i2c_hid_get_report_length(report); 5909ee3e066SJulian Sax if (*max < size) 5919ee3e066SJulian Sax *max = size; 5929ee3e066SJulian Sax } 5939ee3e066SJulian Sax } 5949ee3e066SJulian Sax 5959ee3e066SJulian Sax static void i2c_hid_free_buffers(struct i2c_hid *ihid) 5969ee3e066SJulian Sax { 5979ee3e066SJulian Sax kfree(ihid->inbuf); 5989ee3e066SJulian Sax kfree(ihid->rawbuf); 5999ee3e066SJulian Sax kfree(ihid->cmdbuf); 6009ee3e066SJulian Sax ihid->inbuf = NULL; 6019ee3e066SJulian Sax ihid->rawbuf = NULL; 6029ee3e066SJulian Sax ihid->cmdbuf = NULL; 6039ee3e066SJulian Sax ihid->bufsize = 0; 6049ee3e066SJulian Sax } 6059ee3e066SJulian Sax 6069ee3e066SJulian Sax static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size) 6079ee3e066SJulian Sax { 608dbe0dd5fSDmitry Torokhov /* 609dbe0dd5fSDmitry Torokhov * The worst case is computed from the set_report command with a 610dbe0dd5fSDmitry Torokhov * reportID > 15 and the maximum report length. 611dbe0dd5fSDmitry Torokhov */ 612dbe0dd5fSDmitry Torokhov int cmd_len = sizeof(__le16) + /* command register */ 613dbe0dd5fSDmitry Torokhov sizeof(u8) + /* encoded report type/ID */ 614dbe0dd5fSDmitry Torokhov sizeof(u8) + /* opcode */ 615dbe0dd5fSDmitry Torokhov sizeof(u8) + /* optional 3rd byte report ID */ 616dbe0dd5fSDmitry Torokhov sizeof(__le16) + /* data register */ 617dbe0dd5fSDmitry Torokhov sizeof(__le16) + /* report data size */ 618dbe0dd5fSDmitry Torokhov sizeof(u8) + /* report ID if numbered report */ 619dbe0dd5fSDmitry Torokhov report_size; 6209ee3e066SJulian Sax 6219ee3e066SJulian Sax ihid->inbuf = kzalloc(report_size, GFP_KERNEL); 6229ee3e066SJulian Sax ihid->rawbuf = kzalloc(report_size, GFP_KERNEL); 623dbe0dd5fSDmitry Torokhov ihid->cmdbuf = kzalloc(cmd_len, GFP_KERNEL); 6249ee3e066SJulian Sax 625dbe0dd5fSDmitry Torokhov if (!ihid->inbuf || !ihid->rawbuf || !ihid->cmdbuf) { 6269ee3e066SJulian Sax i2c_hid_free_buffers(ihid); 6279ee3e066SJulian Sax return -ENOMEM; 6289ee3e066SJulian Sax } 6299ee3e066SJulian Sax 6309ee3e066SJulian Sax ihid->bufsize = report_size; 6319ee3e066SJulian Sax 6329ee3e066SJulian Sax return 0; 6339ee3e066SJulian Sax } 6349ee3e066SJulian Sax 6359ee3e066SJulian Sax static int i2c_hid_get_raw_report(struct hid_device *hid, 63685df7133SDmitry Torokhov u8 report_type, u8 report_id, 63785df7133SDmitry Torokhov u8 *buf, size_t count) 6389ee3e066SJulian Sax { 6399ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 6409ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 64185df7133SDmitry Torokhov int ret_count; 6429ee3e066SJulian Sax 6439ee3e066SJulian Sax if (report_type == HID_OUTPUT_REPORT) 6449ee3e066SJulian Sax return -EINVAL; 6459ee3e066SJulian Sax 646a5e5e03eSDmitry Torokhov /* 647a5e5e03eSDmitry Torokhov * In case of unnumbered reports the response from the device will 648a5e5e03eSDmitry Torokhov * not have the report ID that the upper layers expect, so we need 649a5e5e03eSDmitry Torokhov * to stash it the buffer ourselves and adjust the data size. 650a5e5e03eSDmitry Torokhov */ 65185df7133SDmitry Torokhov if (!report_id) { 652a5e5e03eSDmitry Torokhov buf[0] = 0; 653a5e5e03eSDmitry Torokhov buf++; 654a5e5e03eSDmitry Torokhov count--; 655a5e5e03eSDmitry Torokhov } 656a5e5e03eSDmitry Torokhov 65785df7133SDmitry Torokhov ret_count = i2c_hid_get_report(ihid, 6589ee3e066SJulian Sax report_type == HID_FEATURE_REPORT ? 0x03 : 0x01, 65985df7133SDmitry Torokhov report_id, buf, count); 6609ee3e066SJulian Sax 66185df7133SDmitry Torokhov if (ret_count > 0 && !report_id) 66285df7133SDmitry Torokhov ret_count++; 6639ee3e066SJulian Sax 66485df7133SDmitry Torokhov return ret_count; 6659ee3e066SJulian Sax } 6669ee3e066SJulian Sax 66785df7133SDmitry Torokhov static int i2c_hid_output_raw_report(struct hid_device *hid, u8 report_type, 66885df7133SDmitry Torokhov const u8 *buf, size_t count, bool do_set) 6699ee3e066SJulian Sax { 6709ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 6719ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 6729ee3e066SJulian Sax int report_id = buf[0]; 6739ee3e066SJulian Sax int ret; 6749ee3e066SJulian Sax 6759ee3e066SJulian Sax if (report_type == HID_INPUT_REPORT) 6769ee3e066SJulian Sax return -EINVAL; 6779ee3e066SJulian Sax 6789ee3e066SJulian Sax mutex_lock(&ihid->reset_lock); 6799ee3e066SJulian Sax 680a5e5e03eSDmitry Torokhov /* 681a5e5e03eSDmitry Torokhov * Note that both numbered and unnumbered reports passed here 682a5e5e03eSDmitry Torokhov * are supposed to have report ID stored in the 1st byte of the 683a5e5e03eSDmitry Torokhov * buffer, so we strip it off unconditionally before passing payload 684a5e5e03eSDmitry Torokhov * to i2c_hid_set_or_send_report which takes care of encoding 685a5e5e03eSDmitry Torokhov * everything properly. 686a5e5e03eSDmitry Torokhov */ 687d34c6105SDmitry Torokhov ret = i2c_hid_set_or_send_report(ihid, 6889ee3e066SJulian Sax report_type == HID_FEATURE_REPORT ? 0x03 : 0x02, 689dbe0dd5fSDmitry Torokhov report_id, buf + 1, count - 1, do_set); 6909ee3e066SJulian Sax 691a5e5e03eSDmitry Torokhov if (ret >= 0) 692a5e5e03eSDmitry Torokhov ret++; /* add report_id to the number of transferred bytes */ 6939ee3e066SJulian Sax 6949ee3e066SJulian Sax mutex_unlock(&ihid->reset_lock); 6959ee3e066SJulian Sax 6969ee3e066SJulian Sax return ret; 6979ee3e066SJulian Sax } 6989ee3e066SJulian Sax 699dbe0dd5fSDmitry Torokhov static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count) 7009ee3e066SJulian Sax { 70185df7133SDmitry Torokhov return i2c_hid_output_raw_report(hid, HID_OUTPUT_REPORT, buf, count, 7029ee3e066SJulian Sax false); 7039ee3e066SJulian Sax } 7049ee3e066SJulian Sax 7059ee3e066SJulian Sax static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum, 7069ee3e066SJulian Sax __u8 *buf, size_t len, unsigned char rtype, 7079ee3e066SJulian Sax int reqtype) 7089ee3e066SJulian Sax { 7099ee3e066SJulian Sax switch (reqtype) { 7109ee3e066SJulian Sax case HID_REQ_GET_REPORT: 71185df7133SDmitry Torokhov return i2c_hid_get_raw_report(hid, rtype, reportnum, buf, len); 7129ee3e066SJulian Sax case HID_REQ_SET_REPORT: 7139ee3e066SJulian Sax if (buf[0] != reportnum) 7149ee3e066SJulian Sax return -EINVAL; 71585df7133SDmitry Torokhov return i2c_hid_output_raw_report(hid, rtype, buf, len, true); 7169ee3e066SJulian Sax default: 7179ee3e066SJulian Sax return -EIO; 7189ee3e066SJulian Sax } 7199ee3e066SJulian Sax } 7209ee3e066SJulian Sax 7219ee3e066SJulian Sax static int i2c_hid_parse(struct hid_device *hid) 7229ee3e066SJulian Sax { 7239ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 7249ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 7259ee3e066SJulian Sax struct i2c_hid_desc *hdesc = &ihid->hdesc; 7269ee3e066SJulian Sax unsigned int rsize; 7279ee3e066SJulian Sax char *rdesc; 7289ee3e066SJulian Sax int ret; 7299ee3e066SJulian Sax int tries = 3; 7309ee3e066SJulian Sax char *use_override; 7319ee3e066SJulian Sax 7329ee3e066SJulian Sax i2c_hid_dbg(ihid, "entering %s\n", __func__); 7339ee3e066SJulian Sax 7349ee3e066SJulian Sax rsize = le16_to_cpu(hdesc->wReportDescLength); 7359ee3e066SJulian Sax if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) { 7369ee3e066SJulian Sax dbg_hid("weird size of report descriptor (%u)\n", rsize); 7379ee3e066SJulian Sax return -EINVAL; 7389ee3e066SJulian Sax } 7399ee3e066SJulian Sax 7409ee3e066SJulian Sax do { 741d34c6105SDmitry Torokhov ret = i2c_hid_hwreset(ihid); 7429ee3e066SJulian Sax if (ret) 7439ee3e066SJulian Sax msleep(1000); 7449ee3e066SJulian Sax } while (tries-- > 0 && ret); 7459ee3e066SJulian Sax 7469ee3e066SJulian Sax if (ret) 7479ee3e066SJulian Sax return ret; 7489ee3e066SJulian Sax 7499ee3e066SJulian Sax use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name, 7509ee3e066SJulian Sax &rsize); 7519ee3e066SJulian Sax 7529ee3e066SJulian Sax if (use_override) { 7539ee3e066SJulian Sax rdesc = use_override; 7549ee3e066SJulian Sax i2c_hid_dbg(ihid, "Using a HID report descriptor override\n"); 7559ee3e066SJulian Sax } else { 7569ee3e066SJulian Sax rdesc = kzalloc(rsize, GFP_KERNEL); 7579ee3e066SJulian Sax 7589ee3e066SJulian Sax if (!rdesc) { 7599ee3e066SJulian Sax dbg_hid("couldn't allocate rdesc memory\n"); 7609ee3e066SJulian Sax return -ENOMEM; 7619ee3e066SJulian Sax } 7629ee3e066SJulian Sax 7639ee3e066SJulian Sax i2c_hid_dbg(ihid, "asking HID report descriptor\n"); 7649ee3e066SJulian Sax 7658399bd01SDmitry Torokhov ret = i2c_hid_read_register(ihid, 7668399bd01SDmitry Torokhov ihid->hdesc.wReportDescRegister, 7679ee3e066SJulian Sax rdesc, rsize); 7689ee3e066SJulian Sax if (ret) { 7699ee3e066SJulian Sax hid_err(hid, "reading report descriptor failed\n"); 7709ee3e066SJulian Sax kfree(rdesc); 7719ee3e066SJulian Sax return -EIO; 7729ee3e066SJulian Sax } 7739ee3e066SJulian Sax } 7749ee3e066SJulian Sax 7759ee3e066SJulian Sax i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc); 7769ee3e066SJulian Sax 7779ee3e066SJulian Sax ret = hid_parse_report(hid, rdesc, rsize); 7789ee3e066SJulian Sax if (!use_override) 7799ee3e066SJulian Sax kfree(rdesc); 7809ee3e066SJulian Sax 7819ee3e066SJulian Sax if (ret) { 7829ee3e066SJulian Sax dbg_hid("parsing report descriptor failed\n"); 7839ee3e066SJulian Sax return ret; 7849ee3e066SJulian Sax } 7859ee3e066SJulian Sax 7869ee3e066SJulian Sax return 0; 7879ee3e066SJulian Sax } 7889ee3e066SJulian Sax 7899ee3e066SJulian Sax static int i2c_hid_start(struct hid_device *hid) 7909ee3e066SJulian Sax { 7919ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 7929ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 7939ee3e066SJulian Sax int ret; 7949ee3e066SJulian Sax unsigned int bufsize = HID_MIN_BUFFER_SIZE; 7959ee3e066SJulian Sax 7969ee3e066SJulian Sax i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize); 7979ee3e066SJulian Sax i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize); 7989ee3e066SJulian Sax i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize); 7999ee3e066SJulian Sax 8009ee3e066SJulian Sax if (bufsize > ihid->bufsize) { 8019ee3e066SJulian Sax disable_irq(client->irq); 8029ee3e066SJulian Sax i2c_hid_free_buffers(ihid); 8039ee3e066SJulian Sax 8049ee3e066SJulian Sax ret = i2c_hid_alloc_buffers(ihid, bufsize); 8059ee3e066SJulian Sax enable_irq(client->irq); 8069ee3e066SJulian Sax 8079ee3e066SJulian Sax if (ret) 8089ee3e066SJulian Sax return ret; 8099ee3e066SJulian Sax } 8109ee3e066SJulian Sax 8119ee3e066SJulian Sax return 0; 8129ee3e066SJulian Sax } 8139ee3e066SJulian Sax 8149ee3e066SJulian Sax static void i2c_hid_stop(struct hid_device *hid) 8159ee3e066SJulian Sax { 8169ee3e066SJulian Sax hid->claimed = 0; 8179ee3e066SJulian Sax } 8189ee3e066SJulian Sax 8199ee3e066SJulian Sax static int i2c_hid_open(struct hid_device *hid) 8209ee3e066SJulian Sax { 8219ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 8229ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 8239ee3e066SJulian Sax 8249ee3e066SJulian Sax set_bit(I2C_HID_STARTED, &ihid->flags); 8259ee3e066SJulian Sax return 0; 8269ee3e066SJulian Sax } 8279ee3e066SJulian Sax 8289ee3e066SJulian Sax static void i2c_hid_close(struct hid_device *hid) 8299ee3e066SJulian Sax { 8309ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 8319ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 8329ee3e066SJulian Sax 8339ee3e066SJulian Sax clear_bit(I2C_HID_STARTED, &ihid->flags); 8349ee3e066SJulian Sax } 8359ee3e066SJulian Sax 83652d22534SThomas Weißschuh static const struct hid_ll_driver i2c_hid_ll_driver = { 8379ee3e066SJulian Sax .parse = i2c_hid_parse, 8389ee3e066SJulian Sax .start = i2c_hid_start, 8399ee3e066SJulian Sax .stop = i2c_hid_stop, 8409ee3e066SJulian Sax .open = i2c_hid_open, 8419ee3e066SJulian Sax .close = i2c_hid_close, 8429ee3e066SJulian Sax .output_report = i2c_hid_output_report, 8439ee3e066SJulian Sax .raw_request = i2c_hid_raw_request, 8449ee3e066SJulian Sax }; 8459ee3e066SJulian Sax 8469ee3e066SJulian Sax static int i2c_hid_init_irq(struct i2c_client *client) 8479ee3e066SJulian Sax { 8489ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 8499ee3e066SJulian Sax unsigned long irqflags = 0; 8509ee3e066SJulian Sax int ret; 8519ee3e066SJulian Sax 852f639e0b6SThomas Weißschuh i2c_hid_dbg(ihid, "Requesting IRQ: %d\n", client->irq); 8539ee3e066SJulian Sax 8549ee3e066SJulian Sax if (!irq_get_trigger_type(client->irq)) 8559ee3e066SJulian Sax irqflags = IRQF_TRIGGER_LOW; 8569ee3e066SJulian Sax 8579ee3e066SJulian Sax ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq, 858*675cd877SDouglas Anderson irqflags | IRQF_ONESHOT | IRQF_NO_AUTOEN, 859*675cd877SDouglas Anderson client->name, ihid); 8609ee3e066SJulian Sax if (ret < 0) { 8619ee3e066SJulian Sax dev_warn(&client->dev, 8629ee3e066SJulian Sax "Could not register for %s interrupt, irq = %d," 8639ee3e066SJulian Sax " ret = %d\n", 8649ee3e066SJulian Sax client->name, client->irq, ret); 8659ee3e066SJulian Sax 8669ee3e066SJulian Sax return ret; 8679ee3e066SJulian Sax } 8689ee3e066SJulian Sax 8699ee3e066SJulian Sax return 0; 8709ee3e066SJulian Sax } 8719ee3e066SJulian Sax 8729ee3e066SJulian Sax static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid) 8739ee3e066SJulian Sax { 8749ee3e066SJulian Sax struct i2c_client *client = ihid->client; 8759ee3e066SJulian Sax struct i2c_hid_desc *hdesc = &ihid->hdesc; 8769ee3e066SJulian Sax unsigned int dsize; 8778399bd01SDmitry Torokhov int error; 8789ee3e066SJulian Sax 8799ee3e066SJulian Sax /* i2c hid fetch using a fixed descriptor size (30 bytes) */ 8809ee3e066SJulian Sax if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) { 8819ee3e066SJulian Sax i2c_hid_dbg(ihid, "Using a HID descriptor override\n"); 8829ee3e066SJulian Sax ihid->hdesc = 8839ee3e066SJulian Sax *i2c_hid_get_dmi_i2c_hid_desc_override(client->name); 8849ee3e066SJulian Sax } else { 8859ee3e066SJulian Sax i2c_hid_dbg(ihid, "Fetching the HID descriptor\n"); 8868399bd01SDmitry Torokhov error = i2c_hid_read_register(ihid, 8878399bd01SDmitry Torokhov ihid->wHIDDescRegister, 8888399bd01SDmitry Torokhov &ihid->hdesc, 8898399bd01SDmitry Torokhov sizeof(ihid->hdesc)); 8908399bd01SDmitry Torokhov if (error) { 8918399bd01SDmitry Torokhov dev_err(&ihid->client->dev, 8928399bd01SDmitry Torokhov "failed to fetch HID descriptor: %d\n", 8938399bd01SDmitry Torokhov error); 8949ee3e066SJulian Sax return -ENODEV; 8959ee3e066SJulian Sax } 8969ee3e066SJulian Sax } 8979ee3e066SJulian Sax 8989ee3e066SJulian Sax /* Validate the length of HID descriptor, the 4 first bytes: 8999ee3e066SJulian Sax * bytes 0-1 -> length 9009ee3e066SJulian Sax * bytes 2-3 -> bcdVersion (has to be 1.00) */ 9019ee3e066SJulian Sax /* check bcdVersion == 1.0 */ 9029ee3e066SJulian Sax if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) { 903d34c6105SDmitry Torokhov dev_err(&ihid->client->dev, 9049ee3e066SJulian Sax "unexpected HID descriptor bcdVersion (0x%04hx)\n", 9059ee3e066SJulian Sax le16_to_cpu(hdesc->bcdVersion)); 9069ee3e066SJulian Sax return -ENODEV; 9079ee3e066SJulian Sax } 9089ee3e066SJulian Sax 9099ee3e066SJulian Sax /* Descriptor length should be 30 bytes as per the specification */ 9109ee3e066SJulian Sax dsize = le16_to_cpu(hdesc->wHIDDescLength); 9119ee3e066SJulian Sax if (dsize != sizeof(struct i2c_hid_desc)) { 912d34c6105SDmitry Torokhov dev_err(&ihid->client->dev, 913d34c6105SDmitry Torokhov "weird size of HID descriptor (%u)\n", dsize); 9149ee3e066SJulian Sax return -ENODEV; 9159ee3e066SJulian Sax } 916551117c5SDmitry Torokhov i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, &ihid->hdesc); 9179ee3e066SJulian Sax return 0; 9189ee3e066SJulian Sax } 9199ee3e066SJulian Sax 920b33752c3SDouglas Anderson static int i2c_hid_core_power_up(struct i2c_hid *ihid) 9219ee3e066SJulian Sax { 922b33752c3SDouglas Anderson if (!ihid->ops->power_up) 9239ee3e066SJulian Sax return 0; 924b33752c3SDouglas Anderson 925b33752c3SDouglas Anderson return ihid->ops->power_up(ihid->ops); 9269ee3e066SJulian Sax } 9279ee3e066SJulian Sax 928b33752c3SDouglas Anderson static void i2c_hid_core_power_down(struct i2c_hid *ihid) 9299ee3e066SJulian Sax { 930b33752c3SDouglas Anderson if (!ihid->ops->power_down) 931b33752c3SDouglas Anderson return; 9329ee3e066SJulian Sax 933b33752c3SDouglas Anderson ihid->ops->power_down(ihid->ops); 9349ee3e066SJulian Sax } 9359ee3e066SJulian Sax 936b33752c3SDouglas Anderson static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid) 937203c38fbSKai-Heng Feng { 938b33752c3SDouglas Anderson if (!ihid->ops->shutdown_tail) 939b33752c3SDouglas Anderson return; 940b33752c3SDouglas Anderson 941b33752c3SDouglas Anderson ihid->ops->shutdown_tail(ihid->ops); 942203c38fbSKai-Heng Feng } 943203c38fbSKai-Heng Feng 944*675cd877SDouglas Anderson /** 945*675cd877SDouglas Anderson * i2c_hid_core_initial_power_up() - First time power up of the i2c-hid device. 946*675cd877SDouglas Anderson * @ihid: The ihid object created during probe. 947*675cd877SDouglas Anderson * 948*675cd877SDouglas Anderson * This function is called at probe time. 949*675cd877SDouglas Anderson * 950*675cd877SDouglas Anderson * The initial power on is where we do some basic validation that the device 951*675cd877SDouglas Anderson * exists, where we fetch the HID descriptor, and where we create the actual 952*675cd877SDouglas Anderson * HID devices. 953*675cd877SDouglas Anderson * 954*675cd877SDouglas Anderson * Return: 0 or error code. 955*675cd877SDouglas Anderson */ 956*675cd877SDouglas Anderson static int i2c_hid_core_initial_power_up(struct i2c_hid *ihid) 957*675cd877SDouglas Anderson { 958*675cd877SDouglas Anderson struct i2c_client *client = ihid->client; 959*675cd877SDouglas Anderson struct hid_device *hid = ihid->hid; 960*675cd877SDouglas Anderson int ret; 961*675cd877SDouglas Anderson 962*675cd877SDouglas Anderson ret = i2c_hid_core_power_up(ihid); 963*675cd877SDouglas Anderson if (ret) 964*675cd877SDouglas Anderson return ret; 965*675cd877SDouglas Anderson 966*675cd877SDouglas Anderson /* Make sure there is something at this address */ 967*675cd877SDouglas Anderson ret = i2c_smbus_read_byte(client); 968*675cd877SDouglas Anderson if (ret < 0) { 969*675cd877SDouglas Anderson i2c_hid_dbg(ihid, "nothing at this address: %d\n", ret); 970*675cd877SDouglas Anderson ret = -ENXIO; 971*675cd877SDouglas Anderson goto err; 972*675cd877SDouglas Anderson } 973*675cd877SDouglas Anderson 974*675cd877SDouglas Anderson ret = i2c_hid_fetch_hid_descriptor(ihid); 975*675cd877SDouglas Anderson if (ret < 0) { 976*675cd877SDouglas Anderson dev_err(&client->dev, 977*675cd877SDouglas Anderson "Failed to fetch the HID Descriptor\n"); 978*675cd877SDouglas Anderson goto err; 979*675cd877SDouglas Anderson } 980*675cd877SDouglas Anderson 981*675cd877SDouglas Anderson enable_irq(client->irq); 982*675cd877SDouglas Anderson 983*675cd877SDouglas Anderson hid->version = le16_to_cpu(ihid->hdesc.bcdVersion); 984*675cd877SDouglas Anderson hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID); 985*675cd877SDouglas Anderson hid->product = le16_to_cpu(ihid->hdesc.wProductID); 986*675cd877SDouglas Anderson 987*675cd877SDouglas Anderson hid->initial_quirks |= i2c_hid_get_dmi_quirks(hid->vendor, 988*675cd877SDouglas Anderson hid->product); 989*675cd877SDouglas Anderson 990*675cd877SDouglas Anderson snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X", 991*675cd877SDouglas Anderson client->name, (u16)hid->vendor, (u16)hid->product); 992*675cd877SDouglas Anderson strscpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys)); 993*675cd877SDouglas Anderson 994*675cd877SDouglas Anderson ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product); 995*675cd877SDouglas Anderson 996*675cd877SDouglas Anderson ret = hid_add_device(hid); 997*675cd877SDouglas Anderson if (ret) { 998*675cd877SDouglas Anderson if (ret != -ENODEV) 999*675cd877SDouglas Anderson hid_err(client, "can't add hid device: %d\n", ret); 1000*675cd877SDouglas Anderson goto err; 1001*675cd877SDouglas Anderson } 1002*675cd877SDouglas Anderson 1003*675cd877SDouglas Anderson return 0; 1004*675cd877SDouglas Anderson 1005*675cd877SDouglas Anderson err: 1006*675cd877SDouglas Anderson i2c_hid_core_power_down(ihid); 1007*675cd877SDouglas Anderson return ret; 1008*675cd877SDouglas Anderson } 1009*675cd877SDouglas Anderson 1010b33752c3SDouglas Anderson int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops, 1011b60d3c80SAlistair Francis u16 hid_descriptor_address, u32 quirks) 10129ee3e066SJulian Sax { 10139ee3e066SJulian Sax int ret; 10149ee3e066SJulian Sax struct i2c_hid *ihid; 10159ee3e066SJulian Sax struct hid_device *hid; 10169ee3e066SJulian Sax 10179ee3e066SJulian Sax dbg_hid("HID probe called for i2c 0x%02x\n", client->addr); 10189ee3e066SJulian Sax 10199ee3e066SJulian Sax if (!client->irq) { 10209ee3e066SJulian Sax dev_err(&client->dev, 10219ee3e066SJulian Sax "HID over i2c has not been provided an Int IRQ\n"); 10229ee3e066SJulian Sax return -EINVAL; 10239ee3e066SJulian Sax } 10249ee3e066SJulian Sax 10259ee3e066SJulian Sax if (client->irq < 0) { 10269ee3e066SJulian Sax if (client->irq != -EPROBE_DEFER) 10279ee3e066SJulian Sax dev_err(&client->dev, 10289ee3e066SJulian Sax "HID over i2c doesn't have a valid IRQ\n"); 10299ee3e066SJulian Sax return client->irq; 10309ee3e066SJulian Sax } 10319ee3e066SJulian Sax 10329ee3e066SJulian Sax ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL); 10339ee3e066SJulian Sax if (!ihid) 10349ee3e066SJulian Sax return -ENOMEM; 10359ee3e066SJulian Sax 10369ee3e066SJulian Sax i2c_set_clientdata(client, ihid); 10379ee3e066SJulian Sax 1038*675cd877SDouglas Anderson ihid->ops = ops; 10399ee3e066SJulian Sax ihid->client = client; 1040b33752c3SDouglas Anderson ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address); 10419ee3e066SJulian Sax 10429ee3e066SJulian Sax init_waitqueue_head(&ihid->wait); 10439ee3e066SJulian Sax mutex_init(&ihid->reset_lock); 10449ee3e066SJulian Sax 10459ee3e066SJulian Sax /* we need to allocate the command buffer without knowing the maximum 10469ee3e066SJulian Sax * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the 10479ee3e066SJulian Sax * real computation later. */ 10489ee3e066SJulian Sax ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE); 10499ee3e066SJulian Sax if (ret < 0) 1050*675cd877SDouglas Anderson return ret; 10519ee3e066SJulian Sax device_enable_async_suspend(&client->dev); 10529ee3e066SJulian Sax 10539ee3e066SJulian Sax ret = i2c_hid_init_irq(client); 10549ee3e066SJulian Sax if (ret < 0) 1055*675cd877SDouglas Anderson goto err_buffers_allocated; 10569ee3e066SJulian Sax 10579ee3e066SJulian Sax hid = hid_allocate_device(); 10589ee3e066SJulian Sax if (IS_ERR(hid)) { 10599ee3e066SJulian Sax ret = PTR_ERR(hid); 10609ee3e066SJulian Sax goto err_irq; 10619ee3e066SJulian Sax } 10629ee3e066SJulian Sax 10639ee3e066SJulian Sax ihid->hid = hid; 10649ee3e066SJulian Sax 10659ee3e066SJulian Sax hid->driver_data = client; 10669ee3e066SJulian Sax hid->ll_driver = &i2c_hid_ll_driver; 10679ee3e066SJulian Sax hid->dev.parent = &client->dev; 10689ee3e066SJulian Sax hid->bus = BUS_I2C; 106903a86105SDmitry Torokhov hid->initial_quirks = quirks; 107003a86105SDmitry Torokhov 1071*675cd877SDouglas Anderson ret = i2c_hid_core_initial_power_up(ihid); 1072*675cd877SDouglas Anderson if (ret) 10739ee3e066SJulian Sax goto err_mem_free; 10749ee3e066SJulian Sax 10759ee3e066SJulian Sax return 0; 10769ee3e066SJulian Sax 10779ee3e066SJulian Sax err_mem_free: 10789ee3e066SJulian Sax hid_destroy_device(hid); 10799ee3e066SJulian Sax 10809ee3e066SJulian Sax err_irq: 10819ee3e066SJulian Sax free_irq(client->irq, ihid); 10829ee3e066SJulian Sax 1083*675cd877SDouglas Anderson err_buffers_allocated: 10849ee3e066SJulian Sax i2c_hid_free_buffers(ihid); 1085*675cd877SDouglas Anderson 10869ee3e066SJulian Sax return ret; 10879ee3e066SJulian Sax } 1088b33752c3SDouglas Anderson EXPORT_SYMBOL_GPL(i2c_hid_core_probe); 10899ee3e066SJulian Sax 1090ed5c2f5fSUwe Kleine-König void i2c_hid_core_remove(struct i2c_client *client) 10919ee3e066SJulian Sax { 10929ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 10939ee3e066SJulian Sax struct hid_device *hid; 10949ee3e066SJulian Sax 1095*675cd877SDouglas Anderson i2c_hid_core_power_down(ihid); 1096*675cd877SDouglas Anderson 10979ee3e066SJulian Sax hid = ihid->hid; 10989ee3e066SJulian Sax hid_destroy_device(hid); 10999ee3e066SJulian Sax 11009ee3e066SJulian Sax free_irq(client->irq, ihid); 11019ee3e066SJulian Sax 11029ee3e066SJulian Sax if (ihid->bufsize) 11039ee3e066SJulian Sax i2c_hid_free_buffers(ihid); 11049ee3e066SJulian Sax } 1105b33752c3SDouglas Anderson EXPORT_SYMBOL_GPL(i2c_hid_core_remove); 11069ee3e066SJulian Sax 1107b33752c3SDouglas Anderson void i2c_hid_core_shutdown(struct i2c_client *client) 11089ee3e066SJulian Sax { 11099ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 11109ee3e066SJulian Sax 1111d34c6105SDmitry Torokhov i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP); 11129ee3e066SJulian Sax free_irq(client->irq, ihid); 11135c7e02a8SHans de Goede 1114b33752c3SDouglas Anderson i2c_hid_core_shutdown_tail(ihid); 11159ee3e066SJulian Sax } 1116b33752c3SDouglas Anderson EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown); 11179ee3e066SJulian Sax 1118b33752c3SDouglas Anderson static int i2c_hid_core_suspend(struct device *dev) 11199ee3e066SJulian Sax { 11209ee3e066SJulian Sax struct i2c_client *client = to_i2c_client(dev); 11219ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 11229ee3e066SJulian Sax struct hid_device *hid = ihid->hid; 11239ee3e066SJulian Sax int ret; 11249ee3e066SJulian Sax 11259e356208SBenjamin Tissoires ret = hid_driver_suspend(hid, PMSG_SUSPEND); 11269ee3e066SJulian Sax if (ret < 0) 11279ee3e066SJulian Sax return ret; 11289ee3e066SJulian Sax 11299ee3e066SJulian Sax /* Save some power */ 1130d34c6105SDmitry Torokhov i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP); 11319ee3e066SJulian Sax 11329ee3e066SJulian Sax disable_irq(client->irq); 11339ee3e066SJulian Sax 1134d08999ccSRaul E Rangel if (!device_may_wakeup(&client->dev)) 1135b33752c3SDouglas Anderson i2c_hid_core_power_down(ihid); 11369ee3e066SJulian Sax 11379ee3e066SJulian Sax return 0; 11389ee3e066SJulian Sax } 11399ee3e066SJulian Sax 1140b33752c3SDouglas Anderson static int i2c_hid_core_resume(struct device *dev) 11419ee3e066SJulian Sax { 11429ee3e066SJulian Sax int ret; 11439ee3e066SJulian Sax struct i2c_client *client = to_i2c_client(dev); 11449ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 11459ee3e066SJulian Sax struct hid_device *hid = ihid->hid; 11469ee3e066SJulian Sax 1147d08999ccSRaul E Rangel if (!device_may_wakeup(&client->dev)) 1148b33752c3SDouglas Anderson i2c_hid_core_power_up(ihid); 11499ee3e066SJulian Sax 11509ee3e066SJulian Sax enable_irq(client->irq); 11519ee3e066SJulian Sax 11529ee3e066SJulian Sax /* Instead of resetting device, simply powers the device on. This 11539ee3e066SJulian Sax * solves "incomplete reports" on Raydium devices 2386:3118 and 11544e7be68eSJiri Kosina * 2386:4B33 and fixes various SIS touchscreens no longer sending 11554e7be68eSJiri Kosina * data after a suspend/resume. 1156fd70466dSKai-Heng Feng * 1157fd70466dSKai-Heng Feng * However some ALPS touchpads generate IRQ storm without reset, so 1158fd70466dSKai-Heng Feng * let's still reset them here. 11599ee3e066SJulian Sax */ 1160fd70466dSKai-Heng Feng if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME) 1161d34c6105SDmitry Torokhov ret = i2c_hid_hwreset(ihid); 1162fd70466dSKai-Heng Feng else 1163d34c6105SDmitry Torokhov ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON); 1164fd70466dSKai-Heng Feng 11659ee3e066SJulian Sax if (ret) 11669ee3e066SJulian Sax return ret; 11679ee3e066SJulian Sax 11689e356208SBenjamin Tissoires return hid_driver_reset_resume(hid); 11699ee3e066SJulian Sax } 11709ee3e066SJulian Sax 1171b33752c3SDouglas Anderson const struct dev_pm_ops i2c_hid_core_pm = { 1172a889ee12SDouglas Anderson SYSTEM_SLEEP_PM_OPS(i2c_hid_core_suspend, i2c_hid_core_resume) 11739ee3e066SJulian Sax }; 1174b33752c3SDouglas Anderson EXPORT_SYMBOL_GPL(i2c_hid_core_pm); 11759ee3e066SJulian Sax 11769ee3e066SJulian Sax MODULE_DESCRIPTION("HID over I2C core driver"); 11779ee3e066SJulian Sax MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); 11789ee3e066SJulian Sax MODULE_LICENSE("GPL"); 1179