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> 299ee3e066SJulian Sax #include <linux/device.h> 309ee3e066SJulian Sax #include <linux/wait.h> 319ee3e066SJulian Sax #include <linux/err.h> 329ee3e066SJulian Sax #include <linux/string.h> 339ee3e066SJulian Sax #include <linux/list.h> 349ee3e066SJulian Sax #include <linux/jiffies.h> 359ee3e066SJulian Sax #include <linux/kernel.h> 369ee3e066SJulian Sax #include <linux/hid.h> 379ee3e066SJulian Sax #include <linux/mutex.h> 38dbe0dd5fSDmitry Torokhov #include <asm/unaligned.h> 399ee3e066SJulian Sax 409ee3e066SJulian Sax #include "../hid-ids.h" 419ee3e066SJulian Sax #include "i2c-hid.h" 429ee3e066SJulian Sax 439ee3e066SJulian Sax /* quirks to control the device */ 449ee3e066SJulian Sax #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0) 459ee3e066SJulian Sax #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1) 461475af25SKai-Heng Feng #define I2C_HID_QUIRK_BOGUS_IRQ BIT(4) 47fd70466dSKai-Heng Feng #define I2C_HID_QUIRK_RESET_ON_RESUME BIT(5) 48fd091376SPavel Balan #define I2C_HID_QUIRK_BAD_INPUT_SIZE BIT(6) 49ca66a677SJohnny Chuang #define I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET BIT(7) 50fd091376SPavel Balan 51dbe0dd5fSDmitry Torokhov /* Command opcodes */ 52dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_RESET 0x01 53dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_GET_REPORT 0x02 54dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_SET_REPORT 0x03 55dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_GET_IDLE 0x04 56dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_SET_IDLE 0x05 57dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_GET_PROTOCOL 0x06 58dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_SET_PROTOCOL 0x07 59dbe0dd5fSDmitry Torokhov #define I2C_HID_OPCODE_SET_POWER 0x08 609ee3e066SJulian Sax 619ee3e066SJulian Sax /* flags */ 629ee3e066SJulian Sax #define I2C_HID_STARTED 0 639ee3e066SJulian Sax #define I2C_HID_RESET_PENDING 1 649ee3e066SJulian Sax #define I2C_HID_READ_PENDING 2 659ee3e066SJulian Sax 669ee3e066SJulian Sax #define I2C_HID_PWR_ON 0x00 679ee3e066SJulian Sax #define I2C_HID_PWR_SLEEP 0x01 689ee3e066SJulian Sax 699ee3e066SJulian Sax /* debug option */ 709ee3e066SJulian Sax static bool debug; 719ee3e066SJulian Sax module_param(debug, bool, 0444); 729ee3e066SJulian Sax MODULE_PARM_DESC(debug, "print a lot of debug information"); 739ee3e066SJulian Sax 749ee3e066SJulian Sax #define i2c_hid_dbg(ihid, fmt, arg...) \ 759ee3e066SJulian Sax do { \ 769ee3e066SJulian Sax if (debug) \ 779ee3e066SJulian Sax dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \ 789ee3e066SJulian Sax } while (0) 799ee3e066SJulian Sax 809ee3e066SJulian Sax struct i2c_hid_desc { 819ee3e066SJulian Sax __le16 wHIDDescLength; 829ee3e066SJulian Sax __le16 bcdVersion; 839ee3e066SJulian Sax __le16 wReportDescLength; 849ee3e066SJulian Sax __le16 wReportDescRegister; 859ee3e066SJulian Sax __le16 wInputRegister; 869ee3e066SJulian Sax __le16 wMaxInputLength; 879ee3e066SJulian Sax __le16 wOutputRegister; 889ee3e066SJulian Sax __le16 wMaxOutputLength; 899ee3e066SJulian Sax __le16 wCommandRegister; 909ee3e066SJulian Sax __le16 wDataRegister; 919ee3e066SJulian Sax __le16 wVendorID; 929ee3e066SJulian Sax __le16 wProductID; 939ee3e066SJulian Sax __le16 wVersionID; 949ee3e066SJulian Sax __le32 reserved; 959ee3e066SJulian Sax } __packed; 969ee3e066SJulian Sax 979ee3e066SJulian Sax /* The main device structure */ 989ee3e066SJulian Sax struct i2c_hid { 999ee3e066SJulian Sax struct i2c_client *client; /* i2c client */ 1009ee3e066SJulian Sax struct hid_device *hid; /* pointer to corresponding HID dev */ 1019ee3e066SJulian Sax struct i2c_hid_desc hdesc; /* the HID Descriptor */ 1029ee3e066SJulian Sax __le16 wHIDDescRegister; /* location of the i2c 1039ee3e066SJulian Sax * register of the HID 1049ee3e066SJulian Sax * descriptor. */ 1059ee3e066SJulian Sax unsigned int bufsize; /* i2c buffer size */ 1069ee3e066SJulian Sax u8 *inbuf; /* Input buffer */ 1079ee3e066SJulian Sax u8 *rawbuf; /* Raw Input buffer */ 1089ee3e066SJulian Sax u8 *cmdbuf; /* Command buffer */ 1099ee3e066SJulian Sax 1109ee3e066SJulian Sax unsigned long flags; /* device flags */ 1119ee3e066SJulian Sax unsigned long quirks; /* Various quirks */ 1129ee3e066SJulian Sax 1139ee3e066SJulian Sax wait_queue_head_t wait; /* For waiting the interrupt */ 1149ee3e066SJulian Sax 1159ee3e066SJulian Sax bool irq_wake_enabled; 1169ee3e066SJulian Sax struct mutex reset_lock; 117b33752c3SDouglas Anderson 118b33752c3SDouglas Anderson struct i2chid_ops *ops; 1199ee3e066SJulian Sax }; 1209ee3e066SJulian Sax 1219ee3e066SJulian Sax static const struct i2c_hid_quirks { 1229ee3e066SJulian Sax __u16 idVendor; 1239ee3e066SJulian Sax __u16 idProduct; 1249ee3e066SJulian Sax __u32 quirks; 1259ee3e066SJulian Sax } i2c_hid_quirks[] = { 126b20bef4bSHungNien Chen { USB_VENDOR_ID_WEIDA, HID_ANY_ID, 1279ee3e066SJulian Sax I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV }, 1289ee3e066SJulian Sax { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288, 12967b18dfbSKai-Heng Feng I2C_HID_QUIRK_NO_IRQ_AFTER_RESET }, 130fc6a31b0SHans de Goede { I2C_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_VOYO_WINPAD_A15, 131fc6a31b0SHans de Goede I2C_HID_QUIRK_NO_IRQ_AFTER_RESET }, 1320c843223SAaron Ma { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_3118, 1330c843223SAaron Ma I2C_HID_QUIRK_NO_IRQ_AFTER_RESET }, 134fd70466dSKai-Heng Feng { USB_VENDOR_ID_ALPS_JP, HID_ANY_ID, 135fd70466dSKai-Heng Feng I2C_HID_QUIRK_RESET_ON_RESUME }, 136538f6740SDaniel Playfair Cal { I2C_VENDOR_ID_SYNAPTICS, I2C_PRODUCT_ID_SYNAPTICS_SYNA2393, 137538f6740SDaniel Playfair Cal I2C_HID_QUIRK_RESET_ON_RESUME }, 138fd091376SPavel Balan { USB_VENDOR_ID_ITE, I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720, 139fd091376SPavel Balan I2C_HID_QUIRK_BAD_INPUT_SIZE }, 140ca66a677SJohnny Chuang /* 141ca66a677SJohnny Chuang * Sending the wakeup after reset actually break ELAN touchscreen controller 142ca66a677SJohnny Chuang */ 143ca66a677SJohnny Chuang { USB_VENDOR_ID_ELAN, HID_ANY_ID, 14478653706SJim Broadus I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET | 14578653706SJim Broadus I2C_HID_QUIRK_BOGUS_IRQ }, 1469ee3e066SJulian Sax { 0, 0 } 1479ee3e066SJulian Sax }; 1489ee3e066SJulian Sax 1499ee3e066SJulian Sax /* 1509ee3e066SJulian Sax * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device 1519ee3e066SJulian Sax * @idVendor: the 16-bit vendor ID 1529ee3e066SJulian Sax * @idProduct: the 16-bit product ID 1539ee3e066SJulian Sax * 1549ee3e066SJulian Sax * Returns: a u32 quirks value. 1559ee3e066SJulian Sax */ 1569ee3e066SJulian Sax static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct) 1579ee3e066SJulian Sax { 1589ee3e066SJulian Sax u32 quirks = 0; 1599ee3e066SJulian Sax int n; 1609ee3e066SJulian Sax 1619ee3e066SJulian Sax for (n = 0; i2c_hid_quirks[n].idVendor; n++) 1629ee3e066SJulian Sax if (i2c_hid_quirks[n].idVendor == idVendor && 1639ee3e066SJulian Sax (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID || 1649ee3e066SJulian Sax i2c_hid_quirks[n].idProduct == idProduct)) 1659ee3e066SJulian Sax quirks = i2c_hid_quirks[n].quirks; 1669ee3e066SJulian Sax 1679ee3e066SJulian Sax return quirks; 1689ee3e066SJulian Sax } 1699ee3e066SJulian Sax 170dbe0dd5fSDmitry Torokhov static int i2c_hid_xfer(struct i2c_hid *ihid, 171dbe0dd5fSDmitry Torokhov u8 *send_buf, int send_len, u8 *recv_buf, int recv_len) 172dbe0dd5fSDmitry Torokhov { 173dbe0dd5fSDmitry Torokhov struct i2c_client *client = ihid->client; 174dbe0dd5fSDmitry Torokhov struct i2c_msg msgs[2] = { 0 }; 175dbe0dd5fSDmitry Torokhov int n = 0; 176dbe0dd5fSDmitry Torokhov int ret; 177dbe0dd5fSDmitry Torokhov 178dbe0dd5fSDmitry Torokhov if (send_len) { 179dbe0dd5fSDmitry Torokhov i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", 180dbe0dd5fSDmitry Torokhov __func__, send_len, send_buf); 181dbe0dd5fSDmitry Torokhov 182dbe0dd5fSDmitry Torokhov msgs[n].addr = client->addr; 183*1c4d6cd4SDmitry Torokhov msgs[n].flags = (client->flags & I2C_M_TEN) | I2C_M_DMA_SAFE; 184dbe0dd5fSDmitry Torokhov msgs[n].len = send_len; 185dbe0dd5fSDmitry Torokhov msgs[n].buf = send_buf; 186dbe0dd5fSDmitry Torokhov n++; 187dbe0dd5fSDmitry Torokhov } 188dbe0dd5fSDmitry Torokhov 189dbe0dd5fSDmitry Torokhov if (recv_len) { 190dbe0dd5fSDmitry Torokhov msgs[n].addr = client->addr; 191*1c4d6cd4SDmitry Torokhov msgs[n].flags = (client->flags & I2C_M_TEN) | 192*1c4d6cd4SDmitry Torokhov I2C_M_RD | I2C_M_DMA_SAFE; 193dbe0dd5fSDmitry Torokhov msgs[n].len = recv_len; 194dbe0dd5fSDmitry Torokhov msgs[n].buf = recv_buf; 195dbe0dd5fSDmitry Torokhov n++; 196dbe0dd5fSDmitry Torokhov 197dbe0dd5fSDmitry Torokhov set_bit(I2C_HID_READ_PENDING, &ihid->flags); 198dbe0dd5fSDmitry Torokhov } 199dbe0dd5fSDmitry Torokhov 200dbe0dd5fSDmitry Torokhov ret = i2c_transfer(client->adapter, msgs, n); 201dbe0dd5fSDmitry Torokhov 202dbe0dd5fSDmitry Torokhov if (recv_len) 203dbe0dd5fSDmitry Torokhov clear_bit(I2C_HID_READ_PENDING, &ihid->flags); 204dbe0dd5fSDmitry Torokhov 205dbe0dd5fSDmitry Torokhov if (ret != n) 206dbe0dd5fSDmitry Torokhov return ret < 0 ? ret : -EIO; 207dbe0dd5fSDmitry Torokhov 208dbe0dd5fSDmitry Torokhov return 0; 209dbe0dd5fSDmitry Torokhov } 210dbe0dd5fSDmitry Torokhov 2118399bd01SDmitry Torokhov static int i2c_hid_read_register(struct i2c_hid *ihid, __le16 reg, 2128399bd01SDmitry Torokhov void *buf, size_t len) 2138399bd01SDmitry Torokhov { 2148399bd01SDmitry Torokhov *(__le16 *)ihid->cmdbuf = reg; 2158399bd01SDmitry Torokhov 2168399bd01SDmitry Torokhov return i2c_hid_xfer(ihid, ihid->cmdbuf, sizeof(__le16), buf, len); 2178399bd01SDmitry Torokhov } 2188399bd01SDmitry Torokhov 219dbe0dd5fSDmitry Torokhov static size_t i2c_hid_encode_command(u8 *buf, u8 opcode, 220dbe0dd5fSDmitry Torokhov int report_type, int report_id) 221dbe0dd5fSDmitry Torokhov { 222dbe0dd5fSDmitry Torokhov size_t length = 0; 223dbe0dd5fSDmitry Torokhov 224dbe0dd5fSDmitry Torokhov if (report_id < 0x0F) { 225dbe0dd5fSDmitry Torokhov buf[length++] = report_type << 4 | report_id; 226dbe0dd5fSDmitry Torokhov buf[length++] = opcode; 227dbe0dd5fSDmitry Torokhov } else { 228dbe0dd5fSDmitry Torokhov buf[length++] = report_type << 4 | 0x0F; 229dbe0dd5fSDmitry Torokhov buf[length++] = opcode; 230dbe0dd5fSDmitry Torokhov buf[length++] = report_id; 231dbe0dd5fSDmitry Torokhov } 232dbe0dd5fSDmitry Torokhov 233dbe0dd5fSDmitry Torokhov return length; 234dbe0dd5fSDmitry Torokhov } 235dbe0dd5fSDmitry Torokhov 23685df7133SDmitry Torokhov static int i2c_hid_get_report(struct i2c_hid *ihid, 23785df7133SDmitry Torokhov u8 report_type, u8 report_id, 23885df7133SDmitry Torokhov u8 *recv_buf, size_t recv_len) 2399ee3e066SJulian Sax { 24085df7133SDmitry Torokhov size_t length = 0; 24185df7133SDmitry Torokhov size_t ret_count; 24285df7133SDmitry Torokhov int error; 2439ee3e066SJulian Sax 2449ee3e066SJulian Sax i2c_hid_dbg(ihid, "%s\n", __func__); 2459ee3e066SJulian Sax 24685df7133SDmitry Torokhov /* Command register goes first */ 24785df7133SDmitry Torokhov *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister; 24885df7133SDmitry Torokhov length += sizeof(__le16); 24985df7133SDmitry Torokhov /* Next is GET_REPORT command */ 25085df7133SDmitry Torokhov length += i2c_hid_encode_command(ihid->cmdbuf + length, 25185df7133SDmitry Torokhov I2C_HID_OPCODE_GET_REPORT, 25285df7133SDmitry Torokhov report_type, report_id); 25385df7133SDmitry Torokhov /* 25485df7133SDmitry Torokhov * Device will send report data through data register. Because 25585df7133SDmitry Torokhov * command can be either 2 or 3 bytes destination for the data 25685df7133SDmitry Torokhov * register may be not aligned. 25785df7133SDmitry Torokhov */ 25885df7133SDmitry Torokhov put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister), 25985df7133SDmitry Torokhov ihid->cmdbuf + length); 26085df7133SDmitry Torokhov length += sizeof(__le16); 2619ee3e066SJulian Sax 26285df7133SDmitry Torokhov /* 26385df7133SDmitry Torokhov * In addition to report data device will supply data length 26485df7133SDmitry Torokhov * in the first 2 bytes of the response, so adjust . 26585df7133SDmitry Torokhov */ 26685df7133SDmitry Torokhov error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, 26785df7133SDmitry Torokhov ihid->rawbuf, recv_len + sizeof(__le16)); 26885df7133SDmitry Torokhov if (error) { 269d34c6105SDmitry Torokhov dev_err(&ihid->client->dev, 27085df7133SDmitry Torokhov "failed to set a report to device: %d\n", error); 27185df7133SDmitry Torokhov return error; 2729ee3e066SJulian Sax } 2739ee3e066SJulian Sax 27485df7133SDmitry Torokhov /* The buffer is sufficiently aligned */ 27585df7133SDmitry Torokhov ret_count = le16_to_cpup((__le16 *)ihid->rawbuf); 27685df7133SDmitry Torokhov 27785df7133SDmitry Torokhov /* Check for empty report response */ 27885df7133SDmitry Torokhov if (ret_count <= sizeof(__le16)) 2799ee3e066SJulian Sax return 0; 28085df7133SDmitry Torokhov 28185df7133SDmitry Torokhov recv_len = min(recv_len, ret_count - sizeof(__le16)); 28285df7133SDmitry Torokhov memcpy(recv_buf, ihid->rawbuf + sizeof(__le16), recv_len); 28385df7133SDmitry Torokhov 28485df7133SDmitry Torokhov if (report_id && recv_len != 0 && recv_buf[0] != report_id) { 28585df7133SDmitry Torokhov dev_err(&ihid->client->dev, 28685df7133SDmitry Torokhov "device returned incorrect report (%d vs %d expected)\n", 28785df7133SDmitry Torokhov recv_buf[0], report_id); 28885df7133SDmitry Torokhov return -EINVAL; 28985df7133SDmitry Torokhov } 29085df7133SDmitry Torokhov 29185df7133SDmitry Torokhov return recv_len; 2929ee3e066SJulian Sax } 2939ee3e066SJulian Sax 294dbe0dd5fSDmitry Torokhov static size_t i2c_hid_format_report(u8 *buf, int report_id, 295dbe0dd5fSDmitry Torokhov const u8 *data, size_t size) 296dbe0dd5fSDmitry Torokhov { 297dbe0dd5fSDmitry Torokhov size_t length = sizeof(__le16); /* reserve space to store size */ 298dbe0dd5fSDmitry Torokhov 299dbe0dd5fSDmitry Torokhov if (report_id) 300dbe0dd5fSDmitry Torokhov buf[length++] = report_id; 301dbe0dd5fSDmitry Torokhov 302dbe0dd5fSDmitry Torokhov memcpy(buf + length, data, size); 303dbe0dd5fSDmitry Torokhov length += size; 304dbe0dd5fSDmitry Torokhov 305dbe0dd5fSDmitry Torokhov /* Store overall size in the beginning of the buffer */ 306dbe0dd5fSDmitry Torokhov put_unaligned_le16(length, buf); 307dbe0dd5fSDmitry Torokhov 308dbe0dd5fSDmitry Torokhov return length; 309dbe0dd5fSDmitry Torokhov } 310dbe0dd5fSDmitry Torokhov 3119ee3e066SJulian Sax /** 3129ee3e066SJulian Sax * i2c_hid_set_or_send_report: forward an incoming report to the device 313d34c6105SDmitry Torokhov * @ihid: the i2c hid device 314dbe0dd5fSDmitry Torokhov * @report_type: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT 315dbe0dd5fSDmitry Torokhov * @report_id: the report ID 3169ee3e066SJulian Sax * @buf: the actual data to transfer, without the report ID 317ca43ab1eSXiaofei Tan * @data_len: size of buf 318dbe0dd5fSDmitry Torokhov * @do_set: true: use SET_REPORT HID command, false: send plain OUTPUT report 3199ee3e066SJulian Sax */ 320dbe0dd5fSDmitry Torokhov static int i2c_hid_set_or_send_report(struct i2c_hid *ihid, 321dbe0dd5fSDmitry Torokhov u8 report_type, u8 report_id, 322dbe0dd5fSDmitry Torokhov const u8 *buf, size_t data_len, 323dbe0dd5fSDmitry Torokhov bool do_set) 3249ee3e066SJulian Sax { 325dbe0dd5fSDmitry Torokhov size_t length = 0; 326dbe0dd5fSDmitry Torokhov int error; 3279ee3e066SJulian Sax 3289ee3e066SJulian Sax i2c_hid_dbg(ihid, "%s\n", __func__); 3299ee3e066SJulian Sax 3309ee3e066SJulian Sax if (data_len > ihid->bufsize) 3319ee3e066SJulian Sax return -EINVAL; 3329ee3e066SJulian Sax 333dbe0dd5fSDmitry Torokhov if (!do_set && le16_to_cpu(ihid->hdesc.wMaxOutputLength) == 0) 3349ee3e066SJulian Sax return -ENOSYS; 3359ee3e066SJulian Sax 336dbe0dd5fSDmitry Torokhov if (do_set) { 337dbe0dd5fSDmitry Torokhov /* Command register goes first */ 338dbe0dd5fSDmitry Torokhov *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister; 339dbe0dd5fSDmitry Torokhov length += sizeof(__le16); 340dbe0dd5fSDmitry Torokhov /* Next is SET_REPORT command */ 341dbe0dd5fSDmitry Torokhov length += i2c_hid_encode_command(ihid->cmdbuf + length, 342dbe0dd5fSDmitry Torokhov I2C_HID_OPCODE_SET_REPORT, 343dbe0dd5fSDmitry Torokhov report_type, report_id); 3449ee3e066SJulian Sax /* 345dbe0dd5fSDmitry Torokhov * Report data will go into the data register. Because 346dbe0dd5fSDmitry Torokhov * command can be either 2 or 3 bytes destination for 347dbe0dd5fSDmitry Torokhov * the data register may be not aligned. 3489ee3e066SJulian Sax */ 349dbe0dd5fSDmitry Torokhov put_unaligned_le16(le16_to_cpu(ihid->hdesc.wDataRegister), 350dbe0dd5fSDmitry Torokhov ihid->cmdbuf + length); 351dbe0dd5fSDmitry Torokhov length += sizeof(__le16); 3529ee3e066SJulian Sax } else { 353dbe0dd5fSDmitry Torokhov /* 354dbe0dd5fSDmitry Torokhov * With simple "send report" all data goes into the output 355dbe0dd5fSDmitry Torokhov * register. 356dbe0dd5fSDmitry Torokhov */ 357dbe0dd5fSDmitry Torokhov *(__le16 *)ihid->cmdbuf = ihid->hdesc.wOutputRegister;; 358dbe0dd5fSDmitry Torokhov length += sizeof(__le16); 3599ee3e066SJulian Sax } 3609ee3e066SJulian Sax 361dbe0dd5fSDmitry Torokhov length += i2c_hid_format_report(ihid->cmdbuf + length, 362dbe0dd5fSDmitry Torokhov report_id, buf, data_len); 3639ee3e066SJulian Sax 364dbe0dd5fSDmitry Torokhov error = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0); 365dbe0dd5fSDmitry Torokhov if (error) { 366d34c6105SDmitry Torokhov dev_err(&ihid->client->dev, 367dbe0dd5fSDmitry Torokhov "failed to set a report to device: %d\n", error); 368dbe0dd5fSDmitry Torokhov return error; 3699ee3e066SJulian Sax } 3709ee3e066SJulian Sax 3719ee3e066SJulian Sax return data_len; 3729ee3e066SJulian Sax } 3739ee3e066SJulian Sax 374acb8dd95SDmitry Torokhov static int i2c_hid_set_power_command(struct i2c_hid *ihid, int power_state) 375acb8dd95SDmitry Torokhov { 376acb8dd95SDmitry Torokhov size_t length; 377acb8dd95SDmitry Torokhov 378acb8dd95SDmitry Torokhov /* SET_POWER uses command register */ 379acb8dd95SDmitry Torokhov *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister; 380acb8dd95SDmitry Torokhov length = sizeof(__le16); 381acb8dd95SDmitry Torokhov 382acb8dd95SDmitry Torokhov /* Now the command itself */ 383acb8dd95SDmitry Torokhov length += i2c_hid_encode_command(ihid->cmdbuf + length, 384acb8dd95SDmitry Torokhov I2C_HID_OPCODE_SET_POWER, 385acb8dd95SDmitry Torokhov 0, power_state); 386acb8dd95SDmitry Torokhov 387acb8dd95SDmitry Torokhov return i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0); 388acb8dd95SDmitry Torokhov } 389acb8dd95SDmitry Torokhov 390d34c6105SDmitry Torokhov static int i2c_hid_set_power(struct i2c_hid *ihid, int power_state) 3919ee3e066SJulian Sax { 3929ee3e066SJulian Sax int ret; 3939ee3e066SJulian Sax 3949ee3e066SJulian Sax i2c_hid_dbg(ihid, "%s\n", __func__); 3959ee3e066SJulian Sax 3969ee3e066SJulian Sax /* 3979ee3e066SJulian Sax * Some devices require to send a command to wakeup before power on. 3989ee3e066SJulian Sax * The call will get a return value (EREMOTEIO) but device will be 3999ee3e066SJulian Sax * triggered and activated. After that, it goes like a normal device. 4009ee3e066SJulian Sax */ 4019ee3e066SJulian Sax if (power_state == I2C_HID_PWR_ON && 4029ee3e066SJulian Sax ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) { 403acb8dd95SDmitry Torokhov ret = i2c_hid_set_power_command(ihid, I2C_HID_PWR_ON); 4049ee3e066SJulian Sax 4059ee3e066SJulian Sax /* Device was already activated */ 4069ee3e066SJulian Sax if (!ret) 4079ee3e066SJulian Sax goto set_pwr_exit; 4089ee3e066SJulian Sax } 4099ee3e066SJulian Sax 410acb8dd95SDmitry Torokhov ret = i2c_hid_set_power_command(ihid, power_state); 4119ee3e066SJulian Sax if (ret) 412d34c6105SDmitry Torokhov dev_err(&ihid->client->dev, 413d34c6105SDmitry Torokhov "failed to change power setting.\n"); 4149ee3e066SJulian Sax 4159ee3e066SJulian Sax set_pwr_exit: 416eef40162SHans de Goede 417eef40162SHans de Goede /* 418eef40162SHans de Goede * The HID over I2C specification states that if a DEVICE needs time 419eef40162SHans de Goede * after the PWR_ON request, it should utilise CLOCK stretching. 420eef40162SHans de Goede * However, it has been observered that the Windows driver provides a 421eef40162SHans de Goede * 1ms sleep between the PWR_ON and RESET requests. 422eef40162SHans de Goede * According to Goodix Windows even waits 60 ms after (other?) 423eef40162SHans de Goede * PWR_ON requests. Testing has confirmed that several devices 424eef40162SHans de Goede * will not work properly without a delay after a PWR_ON request. 425eef40162SHans de Goede */ 426eef40162SHans de Goede if (!ret && power_state == I2C_HID_PWR_ON) 427eef40162SHans de Goede msleep(60); 428eef40162SHans de Goede 4299ee3e066SJulian Sax return ret; 4309ee3e066SJulian Sax } 4319ee3e066SJulian Sax 432b26fc316SDmitry Torokhov static int i2c_hid_execute_reset(struct i2c_hid *ihid) 433b26fc316SDmitry Torokhov { 43450c5249fSDmitry Torokhov size_t length = 0; 435b26fc316SDmitry Torokhov int ret; 436b26fc316SDmitry Torokhov 437b26fc316SDmitry Torokhov i2c_hid_dbg(ihid, "resetting...\n"); 438b26fc316SDmitry Torokhov 43950c5249fSDmitry Torokhov /* Prepare reset command. Command register goes first. */ 44050c5249fSDmitry Torokhov *(__le16 *)ihid->cmdbuf = ihid->hdesc.wCommandRegister; 44150c5249fSDmitry Torokhov length += sizeof(__le16); 44250c5249fSDmitry Torokhov /* Next is RESET command itself */ 44350c5249fSDmitry Torokhov length += i2c_hid_encode_command(ihid->cmdbuf + length, 44450c5249fSDmitry Torokhov I2C_HID_OPCODE_RESET, 0, 0); 44550c5249fSDmitry Torokhov 446b26fc316SDmitry Torokhov set_bit(I2C_HID_RESET_PENDING, &ihid->flags); 447b26fc316SDmitry Torokhov 44850c5249fSDmitry Torokhov ret = i2c_hid_xfer(ihid, ihid->cmdbuf, length, NULL, 0); 449b26fc316SDmitry Torokhov if (ret) { 450b26fc316SDmitry Torokhov dev_err(&ihid->client->dev, "failed to reset device.\n"); 451b26fc316SDmitry Torokhov goto out; 452b26fc316SDmitry Torokhov } 453b26fc316SDmitry Torokhov 454b26fc316SDmitry Torokhov if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) { 455b26fc316SDmitry Torokhov msleep(100); 456b26fc316SDmitry Torokhov goto out; 457b26fc316SDmitry Torokhov } 458b26fc316SDmitry Torokhov 459b26fc316SDmitry Torokhov i2c_hid_dbg(ihid, "%s: waiting...\n", __func__); 460b26fc316SDmitry Torokhov if (!wait_event_timeout(ihid->wait, 461b26fc316SDmitry Torokhov !test_bit(I2C_HID_RESET_PENDING, &ihid->flags), 462b26fc316SDmitry Torokhov msecs_to_jiffies(5000))) { 463b26fc316SDmitry Torokhov ret = -ENODATA; 464b26fc316SDmitry Torokhov goto out; 465b26fc316SDmitry Torokhov } 466b26fc316SDmitry Torokhov i2c_hid_dbg(ihid, "%s: finished.\n", __func__); 467b26fc316SDmitry Torokhov 468b26fc316SDmitry Torokhov out: 469b26fc316SDmitry Torokhov clear_bit(I2C_HID_RESET_PENDING, &ihid->flags); 470b26fc316SDmitry Torokhov return ret; 471b26fc316SDmitry Torokhov } 472b26fc316SDmitry Torokhov 473d34c6105SDmitry Torokhov static int i2c_hid_hwreset(struct i2c_hid *ihid) 4749ee3e066SJulian Sax { 4759ee3e066SJulian Sax int ret; 4769ee3e066SJulian Sax 4779ee3e066SJulian Sax i2c_hid_dbg(ihid, "%s\n", __func__); 4789ee3e066SJulian Sax 4799ee3e066SJulian Sax /* 4809ee3e066SJulian Sax * This prevents sending feature reports while the device is 4819ee3e066SJulian Sax * being reset. Otherwise we may lose the reset complete 4829ee3e066SJulian Sax * interrupt. 4839ee3e066SJulian Sax */ 4849ee3e066SJulian Sax mutex_lock(&ihid->reset_lock); 4859ee3e066SJulian Sax 486d34c6105SDmitry Torokhov ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON); 4879ee3e066SJulian Sax if (ret) 4889ee3e066SJulian Sax goto out_unlock; 4899ee3e066SJulian Sax 490b26fc316SDmitry Torokhov ret = i2c_hid_execute_reset(ihid); 4919ee3e066SJulian Sax if (ret) { 492b26fc316SDmitry Torokhov dev_err(&ihid->client->dev, 493b26fc316SDmitry Torokhov "failed to reset device: %d\n", ret); 494d34c6105SDmitry Torokhov i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP); 49543b7029fSHans de Goede goto out_unlock; 4969ee3e066SJulian Sax } 4979ee3e066SJulian Sax 49843b7029fSHans de Goede /* At least some SIS devices need this after reset */ 499ca66a677SJohnny Chuang if (!(ihid->quirks & I2C_HID_QUIRK_NO_WAKEUP_AFTER_RESET)) 500d34c6105SDmitry Torokhov ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON); 50143b7029fSHans de Goede 5029ee3e066SJulian Sax out_unlock: 5039ee3e066SJulian Sax mutex_unlock(&ihid->reset_lock); 5049ee3e066SJulian Sax return ret; 5059ee3e066SJulian Sax } 5069ee3e066SJulian Sax 5079ee3e066SJulian Sax static void i2c_hid_get_input(struct i2c_hid *ihid) 5089ee3e066SJulian Sax { 50986fc3fd2SDmitry Torokhov u16 size = le16_to_cpu(ihid->hdesc.wMaxInputLength); 51086fc3fd2SDmitry Torokhov u16 ret_size; 5119ee3e066SJulian Sax int ret; 5129ee3e066SJulian Sax 5139ee3e066SJulian Sax if (size > ihid->bufsize) 5149ee3e066SJulian Sax size = ihid->bufsize; 5159ee3e066SJulian Sax 5169ee3e066SJulian Sax ret = i2c_master_recv(ihid->client, ihid->inbuf, size); 5179ee3e066SJulian Sax if (ret != size) { 5189ee3e066SJulian Sax if (ret < 0) 5199ee3e066SJulian Sax return; 5209ee3e066SJulian Sax 5219ee3e066SJulian Sax dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n", 5229ee3e066SJulian Sax __func__, ret, size); 5239ee3e066SJulian Sax return; 5249ee3e066SJulian Sax } 5259ee3e066SJulian Sax 52686fc3fd2SDmitry Torokhov /* Receiving buffer is properly aligned */ 52786fc3fd2SDmitry Torokhov ret_size = le16_to_cpup((__le16 *)ihid->inbuf); 5289ee3e066SJulian Sax if (!ret_size) { 5299ee3e066SJulian Sax /* host or device initiated RESET completed */ 5309ee3e066SJulian Sax if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags)) 5319ee3e066SJulian Sax wake_up(&ihid->wait); 5329ee3e066SJulian Sax return; 5339ee3e066SJulian Sax } 5349ee3e066SJulian Sax 53586fc3fd2SDmitry Torokhov if ((ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ) && ret_size == 0xffff) { 53686fc3fd2SDmitry Torokhov dev_warn_once(&ihid->client->dev, 53786fc3fd2SDmitry Torokhov "%s: IRQ triggered but there's no data\n", 53886fc3fd2SDmitry Torokhov __func__); 5391475af25SKai-Heng Feng return; 5401475af25SKai-Heng Feng } 5411475af25SKai-Heng Feng 54286fc3fd2SDmitry Torokhov if (ret_size > size || ret_size < sizeof(__le16)) { 543fd091376SPavel Balan if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) { 54486fc3fd2SDmitry Torokhov *(__le16 *)ihid->inbuf = cpu_to_le16(size); 545fd091376SPavel Balan ret_size = size; 546fd091376SPavel Balan } else { 54786fc3fd2SDmitry Torokhov dev_err(&ihid->client->dev, 54886fc3fd2SDmitry Torokhov "%s: incomplete report (%d/%d)\n", 5499ee3e066SJulian Sax __func__, size, ret_size); 5509ee3e066SJulian Sax return; 5519ee3e066SJulian Sax } 552fd091376SPavel Balan } 5539ee3e066SJulian Sax 5549ee3e066SJulian Sax i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf); 5559ee3e066SJulian Sax 556d951ae1cSMatthias Kaehlcke if (test_bit(I2C_HID_STARTED, &ihid->flags)) { 557d951ae1cSMatthias Kaehlcke pm_wakeup_event(&ihid->client->dev, 0); 558d951ae1cSMatthias Kaehlcke 55986fc3fd2SDmitry Torokhov hid_input_report(ihid->hid, HID_INPUT_REPORT, 56086fc3fd2SDmitry Torokhov ihid->inbuf + sizeof(__le16), 56186fc3fd2SDmitry Torokhov ret_size - sizeof(__le16), 1); 562d951ae1cSMatthias Kaehlcke } 5639ee3e066SJulian Sax 5649ee3e066SJulian Sax return; 5659ee3e066SJulian Sax } 5669ee3e066SJulian Sax 5679ee3e066SJulian Sax static irqreturn_t i2c_hid_irq(int irq, void *dev_id) 5689ee3e066SJulian Sax { 5699ee3e066SJulian Sax struct i2c_hid *ihid = dev_id; 5709ee3e066SJulian Sax 5719ee3e066SJulian Sax if (test_bit(I2C_HID_READ_PENDING, &ihid->flags)) 5729ee3e066SJulian Sax return IRQ_HANDLED; 5739ee3e066SJulian Sax 5749ee3e066SJulian Sax i2c_hid_get_input(ihid); 5759ee3e066SJulian Sax 5769ee3e066SJulian Sax return IRQ_HANDLED; 5779ee3e066SJulian Sax } 5789ee3e066SJulian Sax 5799ee3e066SJulian Sax static int i2c_hid_get_report_length(struct hid_report *report) 5809ee3e066SJulian Sax { 5819ee3e066SJulian Sax return ((report->size - 1) >> 3) + 1 + 5829ee3e066SJulian Sax report->device->report_enum[report->type].numbered + 2; 5839ee3e066SJulian Sax } 5849ee3e066SJulian Sax 5859ee3e066SJulian Sax /* 5869ee3e066SJulian Sax * Traverse the supplied list of reports and find the longest 5879ee3e066SJulian Sax */ 5889ee3e066SJulian Sax static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type, 5899ee3e066SJulian Sax unsigned int *max) 5909ee3e066SJulian Sax { 5919ee3e066SJulian Sax struct hid_report *report; 5929ee3e066SJulian Sax unsigned int size; 5939ee3e066SJulian Sax 5949ee3e066SJulian Sax /* We should not rely on wMaxInputLength, as some devices may set it to 5959ee3e066SJulian Sax * a wrong length. */ 5969ee3e066SJulian Sax list_for_each_entry(report, &hid->report_enum[type].report_list, list) { 5979ee3e066SJulian Sax size = i2c_hid_get_report_length(report); 5989ee3e066SJulian Sax if (*max < size) 5999ee3e066SJulian Sax *max = size; 6009ee3e066SJulian Sax } 6019ee3e066SJulian Sax } 6029ee3e066SJulian Sax 6039ee3e066SJulian Sax static void i2c_hid_free_buffers(struct i2c_hid *ihid) 6049ee3e066SJulian Sax { 6059ee3e066SJulian Sax kfree(ihid->inbuf); 6069ee3e066SJulian Sax kfree(ihid->rawbuf); 6079ee3e066SJulian Sax kfree(ihid->cmdbuf); 6089ee3e066SJulian Sax ihid->inbuf = NULL; 6099ee3e066SJulian Sax ihid->rawbuf = NULL; 6109ee3e066SJulian Sax ihid->cmdbuf = NULL; 6119ee3e066SJulian Sax ihid->bufsize = 0; 6129ee3e066SJulian Sax } 6139ee3e066SJulian Sax 6149ee3e066SJulian Sax static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size) 6159ee3e066SJulian Sax { 616dbe0dd5fSDmitry Torokhov /* 617dbe0dd5fSDmitry Torokhov * The worst case is computed from the set_report command with a 618dbe0dd5fSDmitry Torokhov * reportID > 15 and the maximum report length. 619dbe0dd5fSDmitry Torokhov */ 620dbe0dd5fSDmitry Torokhov int cmd_len = sizeof(__le16) + /* command register */ 621dbe0dd5fSDmitry Torokhov sizeof(u8) + /* encoded report type/ID */ 622dbe0dd5fSDmitry Torokhov sizeof(u8) + /* opcode */ 623dbe0dd5fSDmitry Torokhov sizeof(u8) + /* optional 3rd byte report ID */ 624dbe0dd5fSDmitry Torokhov sizeof(__le16) + /* data register */ 625dbe0dd5fSDmitry Torokhov sizeof(__le16) + /* report data size */ 626dbe0dd5fSDmitry Torokhov sizeof(u8) + /* report ID if numbered report */ 627dbe0dd5fSDmitry Torokhov report_size; 6289ee3e066SJulian Sax 6299ee3e066SJulian Sax ihid->inbuf = kzalloc(report_size, GFP_KERNEL); 6309ee3e066SJulian Sax ihid->rawbuf = kzalloc(report_size, GFP_KERNEL); 631dbe0dd5fSDmitry Torokhov ihid->cmdbuf = kzalloc(cmd_len, GFP_KERNEL); 6329ee3e066SJulian Sax 633dbe0dd5fSDmitry Torokhov if (!ihid->inbuf || !ihid->rawbuf || !ihid->cmdbuf) { 6349ee3e066SJulian Sax i2c_hid_free_buffers(ihid); 6359ee3e066SJulian Sax return -ENOMEM; 6369ee3e066SJulian Sax } 6379ee3e066SJulian Sax 6389ee3e066SJulian Sax ihid->bufsize = report_size; 6399ee3e066SJulian Sax 6409ee3e066SJulian Sax return 0; 6419ee3e066SJulian Sax } 6429ee3e066SJulian Sax 6439ee3e066SJulian Sax static int i2c_hid_get_raw_report(struct hid_device *hid, 64485df7133SDmitry Torokhov u8 report_type, u8 report_id, 64585df7133SDmitry Torokhov u8 *buf, size_t count) 6469ee3e066SJulian Sax { 6479ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 6489ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 64985df7133SDmitry Torokhov int ret_count; 6509ee3e066SJulian Sax 6519ee3e066SJulian Sax if (report_type == HID_OUTPUT_REPORT) 6529ee3e066SJulian Sax return -EINVAL; 6539ee3e066SJulian Sax 654a5e5e03eSDmitry Torokhov /* 655a5e5e03eSDmitry Torokhov * In case of unnumbered reports the response from the device will 656a5e5e03eSDmitry Torokhov * not have the report ID that the upper layers expect, so we need 657a5e5e03eSDmitry Torokhov * to stash it the buffer ourselves and adjust the data size. 658a5e5e03eSDmitry Torokhov */ 65985df7133SDmitry Torokhov if (!report_id) { 660a5e5e03eSDmitry Torokhov buf[0] = 0; 661a5e5e03eSDmitry Torokhov buf++; 662a5e5e03eSDmitry Torokhov count--; 663a5e5e03eSDmitry Torokhov } 664a5e5e03eSDmitry Torokhov 66585df7133SDmitry Torokhov ret_count = i2c_hid_get_report(ihid, 6669ee3e066SJulian Sax report_type == HID_FEATURE_REPORT ? 0x03 : 0x01, 66785df7133SDmitry Torokhov report_id, buf, count); 6689ee3e066SJulian Sax 66985df7133SDmitry Torokhov if (ret_count > 0 && !report_id) 67085df7133SDmitry Torokhov ret_count++; 6719ee3e066SJulian Sax 67285df7133SDmitry Torokhov return ret_count; 6739ee3e066SJulian Sax } 6749ee3e066SJulian Sax 67585df7133SDmitry Torokhov static int i2c_hid_output_raw_report(struct hid_device *hid, u8 report_type, 67685df7133SDmitry Torokhov const u8 *buf, size_t count, bool do_set) 6779ee3e066SJulian Sax { 6789ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 6799ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 6809ee3e066SJulian Sax int report_id = buf[0]; 6819ee3e066SJulian Sax int ret; 6829ee3e066SJulian Sax 6839ee3e066SJulian Sax if (report_type == HID_INPUT_REPORT) 6849ee3e066SJulian Sax return -EINVAL; 6859ee3e066SJulian Sax 6869ee3e066SJulian Sax mutex_lock(&ihid->reset_lock); 6879ee3e066SJulian Sax 688a5e5e03eSDmitry Torokhov /* 689a5e5e03eSDmitry Torokhov * Note that both numbered and unnumbered reports passed here 690a5e5e03eSDmitry Torokhov * are supposed to have report ID stored in the 1st byte of the 691a5e5e03eSDmitry Torokhov * buffer, so we strip it off unconditionally before passing payload 692a5e5e03eSDmitry Torokhov * to i2c_hid_set_or_send_report which takes care of encoding 693a5e5e03eSDmitry Torokhov * everything properly. 694a5e5e03eSDmitry Torokhov */ 695d34c6105SDmitry Torokhov ret = i2c_hid_set_or_send_report(ihid, 6969ee3e066SJulian Sax report_type == HID_FEATURE_REPORT ? 0x03 : 0x02, 697dbe0dd5fSDmitry Torokhov report_id, buf + 1, count - 1, do_set); 6989ee3e066SJulian Sax 699a5e5e03eSDmitry Torokhov if (ret >= 0) 700a5e5e03eSDmitry Torokhov ret++; /* add report_id to the number of transferred bytes */ 7019ee3e066SJulian Sax 7029ee3e066SJulian Sax mutex_unlock(&ihid->reset_lock); 7039ee3e066SJulian Sax 7049ee3e066SJulian Sax return ret; 7059ee3e066SJulian Sax } 7069ee3e066SJulian Sax 707dbe0dd5fSDmitry Torokhov static int i2c_hid_output_report(struct hid_device *hid, u8 *buf, size_t count) 7089ee3e066SJulian Sax { 70985df7133SDmitry Torokhov return i2c_hid_output_raw_report(hid, HID_OUTPUT_REPORT, buf, count, 7109ee3e066SJulian Sax false); 7119ee3e066SJulian Sax } 7129ee3e066SJulian Sax 7139ee3e066SJulian Sax static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum, 7149ee3e066SJulian Sax __u8 *buf, size_t len, unsigned char rtype, 7159ee3e066SJulian Sax int reqtype) 7169ee3e066SJulian Sax { 7179ee3e066SJulian Sax switch (reqtype) { 7189ee3e066SJulian Sax case HID_REQ_GET_REPORT: 71985df7133SDmitry Torokhov return i2c_hid_get_raw_report(hid, rtype, reportnum, buf, len); 7209ee3e066SJulian Sax case HID_REQ_SET_REPORT: 7219ee3e066SJulian Sax if (buf[0] != reportnum) 7229ee3e066SJulian Sax return -EINVAL; 72385df7133SDmitry Torokhov return i2c_hid_output_raw_report(hid, rtype, buf, len, true); 7249ee3e066SJulian Sax default: 7259ee3e066SJulian Sax return -EIO; 7269ee3e066SJulian Sax } 7279ee3e066SJulian Sax } 7289ee3e066SJulian Sax 7299ee3e066SJulian Sax static int i2c_hid_parse(struct hid_device *hid) 7309ee3e066SJulian Sax { 7319ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 7329ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 7339ee3e066SJulian Sax struct i2c_hid_desc *hdesc = &ihid->hdesc; 7349ee3e066SJulian Sax unsigned int rsize; 7359ee3e066SJulian Sax char *rdesc; 7369ee3e066SJulian Sax int ret; 7379ee3e066SJulian Sax int tries = 3; 7389ee3e066SJulian Sax char *use_override; 7399ee3e066SJulian Sax 7409ee3e066SJulian Sax i2c_hid_dbg(ihid, "entering %s\n", __func__); 7419ee3e066SJulian Sax 7429ee3e066SJulian Sax rsize = le16_to_cpu(hdesc->wReportDescLength); 7439ee3e066SJulian Sax if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) { 7449ee3e066SJulian Sax dbg_hid("weird size of report descriptor (%u)\n", rsize); 7459ee3e066SJulian Sax return -EINVAL; 7469ee3e066SJulian Sax } 7479ee3e066SJulian Sax 7489ee3e066SJulian Sax do { 749d34c6105SDmitry Torokhov ret = i2c_hid_hwreset(ihid); 7509ee3e066SJulian Sax if (ret) 7519ee3e066SJulian Sax msleep(1000); 7529ee3e066SJulian Sax } while (tries-- > 0 && ret); 7539ee3e066SJulian Sax 7549ee3e066SJulian Sax if (ret) 7559ee3e066SJulian Sax return ret; 7569ee3e066SJulian Sax 7579ee3e066SJulian Sax use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name, 7589ee3e066SJulian Sax &rsize); 7599ee3e066SJulian Sax 7609ee3e066SJulian Sax if (use_override) { 7619ee3e066SJulian Sax rdesc = use_override; 7629ee3e066SJulian Sax i2c_hid_dbg(ihid, "Using a HID report descriptor override\n"); 7639ee3e066SJulian Sax } else { 7649ee3e066SJulian Sax rdesc = kzalloc(rsize, GFP_KERNEL); 7659ee3e066SJulian Sax 7669ee3e066SJulian Sax if (!rdesc) { 7679ee3e066SJulian Sax dbg_hid("couldn't allocate rdesc memory\n"); 7689ee3e066SJulian Sax return -ENOMEM; 7699ee3e066SJulian Sax } 7709ee3e066SJulian Sax 7719ee3e066SJulian Sax i2c_hid_dbg(ihid, "asking HID report descriptor\n"); 7729ee3e066SJulian Sax 7738399bd01SDmitry Torokhov ret = i2c_hid_read_register(ihid, 7748399bd01SDmitry Torokhov ihid->hdesc.wReportDescRegister, 7759ee3e066SJulian Sax rdesc, rsize); 7769ee3e066SJulian Sax if (ret) { 7779ee3e066SJulian Sax hid_err(hid, "reading report descriptor failed\n"); 7789ee3e066SJulian Sax kfree(rdesc); 7799ee3e066SJulian Sax return -EIO; 7809ee3e066SJulian Sax } 7819ee3e066SJulian Sax } 7829ee3e066SJulian Sax 7839ee3e066SJulian Sax i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc); 7849ee3e066SJulian Sax 7859ee3e066SJulian Sax ret = hid_parse_report(hid, rdesc, rsize); 7869ee3e066SJulian Sax if (!use_override) 7879ee3e066SJulian Sax kfree(rdesc); 7889ee3e066SJulian Sax 7899ee3e066SJulian Sax if (ret) { 7909ee3e066SJulian Sax dbg_hid("parsing report descriptor failed\n"); 7919ee3e066SJulian Sax return ret; 7929ee3e066SJulian Sax } 7939ee3e066SJulian Sax 7949ee3e066SJulian Sax return 0; 7959ee3e066SJulian Sax } 7969ee3e066SJulian Sax 7979ee3e066SJulian Sax static int i2c_hid_start(struct hid_device *hid) 7989ee3e066SJulian Sax { 7999ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 8009ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 8019ee3e066SJulian Sax int ret; 8029ee3e066SJulian Sax unsigned int bufsize = HID_MIN_BUFFER_SIZE; 8039ee3e066SJulian Sax 8049ee3e066SJulian Sax i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize); 8059ee3e066SJulian Sax i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize); 8069ee3e066SJulian Sax i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize); 8079ee3e066SJulian Sax 8089ee3e066SJulian Sax if (bufsize > ihid->bufsize) { 8099ee3e066SJulian Sax disable_irq(client->irq); 8109ee3e066SJulian Sax i2c_hid_free_buffers(ihid); 8119ee3e066SJulian Sax 8129ee3e066SJulian Sax ret = i2c_hid_alloc_buffers(ihid, bufsize); 8139ee3e066SJulian Sax enable_irq(client->irq); 8149ee3e066SJulian Sax 8159ee3e066SJulian Sax if (ret) 8169ee3e066SJulian Sax return ret; 8179ee3e066SJulian Sax } 8189ee3e066SJulian Sax 8199ee3e066SJulian Sax return 0; 8209ee3e066SJulian Sax } 8219ee3e066SJulian Sax 8229ee3e066SJulian Sax static void i2c_hid_stop(struct hid_device *hid) 8239ee3e066SJulian Sax { 8249ee3e066SJulian Sax hid->claimed = 0; 8259ee3e066SJulian Sax } 8269ee3e066SJulian Sax 8279ee3e066SJulian Sax static int i2c_hid_open(struct hid_device *hid) 8289ee3e066SJulian Sax { 8299ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 8309ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 8319ee3e066SJulian Sax 8329ee3e066SJulian Sax set_bit(I2C_HID_STARTED, &ihid->flags); 8339ee3e066SJulian Sax return 0; 8349ee3e066SJulian Sax } 8359ee3e066SJulian Sax 8369ee3e066SJulian Sax static void i2c_hid_close(struct hid_device *hid) 8379ee3e066SJulian Sax { 8389ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 8399ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 8409ee3e066SJulian Sax 8419ee3e066SJulian Sax clear_bit(I2C_HID_STARTED, &ihid->flags); 8429ee3e066SJulian Sax } 8439ee3e066SJulian Sax 8449ee3e066SJulian Sax struct hid_ll_driver i2c_hid_ll_driver = { 8459ee3e066SJulian Sax .parse = i2c_hid_parse, 8469ee3e066SJulian Sax .start = i2c_hid_start, 8479ee3e066SJulian Sax .stop = i2c_hid_stop, 8489ee3e066SJulian Sax .open = i2c_hid_open, 8499ee3e066SJulian Sax .close = i2c_hid_close, 8509ee3e066SJulian Sax .output_report = i2c_hid_output_report, 8519ee3e066SJulian Sax .raw_request = i2c_hid_raw_request, 8529ee3e066SJulian Sax }; 8539ee3e066SJulian Sax EXPORT_SYMBOL_GPL(i2c_hid_ll_driver); 8549ee3e066SJulian Sax 8559ee3e066SJulian Sax static int i2c_hid_init_irq(struct i2c_client *client) 8569ee3e066SJulian Sax { 8579ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 8589ee3e066SJulian Sax unsigned long irqflags = 0; 8599ee3e066SJulian Sax int ret; 8609ee3e066SJulian Sax 8619ee3e066SJulian Sax dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq); 8629ee3e066SJulian Sax 8639ee3e066SJulian Sax if (!irq_get_trigger_type(client->irq)) 8649ee3e066SJulian Sax irqflags = IRQF_TRIGGER_LOW; 8659ee3e066SJulian Sax 8669ee3e066SJulian Sax ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq, 8679ee3e066SJulian Sax irqflags | IRQF_ONESHOT, client->name, ihid); 8689ee3e066SJulian Sax if (ret < 0) { 8699ee3e066SJulian Sax dev_warn(&client->dev, 8709ee3e066SJulian Sax "Could not register for %s interrupt, irq = %d," 8719ee3e066SJulian Sax " ret = %d\n", 8729ee3e066SJulian Sax client->name, client->irq, ret); 8739ee3e066SJulian Sax 8749ee3e066SJulian Sax return ret; 8759ee3e066SJulian Sax } 8769ee3e066SJulian Sax 8779ee3e066SJulian Sax return 0; 8789ee3e066SJulian Sax } 8799ee3e066SJulian Sax 8809ee3e066SJulian Sax static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid) 8819ee3e066SJulian Sax { 8829ee3e066SJulian Sax struct i2c_client *client = ihid->client; 8839ee3e066SJulian Sax struct i2c_hid_desc *hdesc = &ihid->hdesc; 8849ee3e066SJulian Sax unsigned int dsize; 8858399bd01SDmitry Torokhov int error; 8869ee3e066SJulian Sax 8879ee3e066SJulian Sax /* i2c hid fetch using a fixed descriptor size (30 bytes) */ 8889ee3e066SJulian Sax if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) { 8899ee3e066SJulian Sax i2c_hid_dbg(ihid, "Using a HID descriptor override\n"); 8909ee3e066SJulian Sax ihid->hdesc = 8919ee3e066SJulian Sax *i2c_hid_get_dmi_i2c_hid_desc_override(client->name); 8929ee3e066SJulian Sax } else { 8939ee3e066SJulian Sax i2c_hid_dbg(ihid, "Fetching the HID descriptor\n"); 8948399bd01SDmitry Torokhov error = i2c_hid_read_register(ihid, 8958399bd01SDmitry Torokhov ihid->wHIDDescRegister, 8968399bd01SDmitry Torokhov &ihid->hdesc, 8978399bd01SDmitry Torokhov sizeof(ihid->hdesc)); 8988399bd01SDmitry Torokhov if (error) { 8998399bd01SDmitry Torokhov dev_err(&ihid->client->dev, 9008399bd01SDmitry Torokhov "failed to fetch HID descriptor: %d\n", 9018399bd01SDmitry Torokhov error); 9029ee3e066SJulian Sax return -ENODEV; 9039ee3e066SJulian Sax } 9049ee3e066SJulian Sax } 9059ee3e066SJulian Sax 9069ee3e066SJulian Sax /* Validate the length of HID descriptor, the 4 first bytes: 9079ee3e066SJulian Sax * bytes 0-1 -> length 9089ee3e066SJulian Sax * bytes 2-3 -> bcdVersion (has to be 1.00) */ 9099ee3e066SJulian Sax /* check bcdVersion == 1.0 */ 9109ee3e066SJulian Sax if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) { 911d34c6105SDmitry Torokhov dev_err(&ihid->client->dev, 9129ee3e066SJulian Sax "unexpected HID descriptor bcdVersion (0x%04hx)\n", 9139ee3e066SJulian Sax le16_to_cpu(hdesc->bcdVersion)); 9149ee3e066SJulian Sax return -ENODEV; 9159ee3e066SJulian Sax } 9169ee3e066SJulian Sax 9179ee3e066SJulian Sax /* Descriptor length should be 30 bytes as per the specification */ 9189ee3e066SJulian Sax dsize = le16_to_cpu(hdesc->wHIDDescLength); 9199ee3e066SJulian Sax if (dsize != sizeof(struct i2c_hid_desc)) { 920d34c6105SDmitry Torokhov dev_err(&ihid->client->dev, 921d34c6105SDmitry Torokhov "weird size of HID descriptor (%u)\n", dsize); 9229ee3e066SJulian Sax return -ENODEV; 9239ee3e066SJulian Sax } 924551117c5SDmitry Torokhov i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, &ihid->hdesc); 9259ee3e066SJulian Sax return 0; 9269ee3e066SJulian Sax } 9279ee3e066SJulian Sax 928b33752c3SDouglas Anderson static int i2c_hid_core_power_up(struct i2c_hid *ihid) 9299ee3e066SJulian Sax { 930b33752c3SDouglas Anderson if (!ihid->ops->power_up) 9319ee3e066SJulian Sax return 0; 932b33752c3SDouglas Anderson 933b33752c3SDouglas Anderson return ihid->ops->power_up(ihid->ops); 9349ee3e066SJulian Sax } 9359ee3e066SJulian Sax 936b33752c3SDouglas Anderson static void i2c_hid_core_power_down(struct i2c_hid *ihid) 9379ee3e066SJulian Sax { 938b33752c3SDouglas Anderson if (!ihid->ops->power_down) 939b33752c3SDouglas Anderson return; 9409ee3e066SJulian Sax 941b33752c3SDouglas Anderson ihid->ops->power_down(ihid->ops); 9429ee3e066SJulian Sax } 9439ee3e066SJulian Sax 944b33752c3SDouglas Anderson static void i2c_hid_core_shutdown_tail(struct i2c_hid *ihid) 945203c38fbSKai-Heng Feng { 946b33752c3SDouglas Anderson if (!ihid->ops->shutdown_tail) 947b33752c3SDouglas Anderson return; 948b33752c3SDouglas Anderson 949b33752c3SDouglas Anderson ihid->ops->shutdown_tail(ihid->ops); 950203c38fbSKai-Heng Feng } 951203c38fbSKai-Heng Feng 952b33752c3SDouglas Anderson int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops, 953b60d3c80SAlistair Francis u16 hid_descriptor_address, u32 quirks) 9549ee3e066SJulian Sax { 9559ee3e066SJulian Sax int ret; 9569ee3e066SJulian Sax struct i2c_hid *ihid; 9579ee3e066SJulian Sax struct hid_device *hid; 9589ee3e066SJulian Sax 9599ee3e066SJulian Sax dbg_hid("HID probe called for i2c 0x%02x\n", client->addr); 9609ee3e066SJulian Sax 9619ee3e066SJulian Sax if (!client->irq) { 9629ee3e066SJulian Sax dev_err(&client->dev, 9639ee3e066SJulian Sax "HID over i2c has not been provided an Int IRQ\n"); 9649ee3e066SJulian Sax return -EINVAL; 9659ee3e066SJulian Sax } 9669ee3e066SJulian Sax 9679ee3e066SJulian Sax if (client->irq < 0) { 9689ee3e066SJulian Sax if (client->irq != -EPROBE_DEFER) 9699ee3e066SJulian Sax dev_err(&client->dev, 9709ee3e066SJulian Sax "HID over i2c doesn't have a valid IRQ\n"); 9719ee3e066SJulian Sax return client->irq; 9729ee3e066SJulian Sax } 9739ee3e066SJulian Sax 9749ee3e066SJulian Sax ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL); 9759ee3e066SJulian Sax if (!ihid) 9769ee3e066SJulian Sax return -ENOMEM; 9779ee3e066SJulian Sax 978b33752c3SDouglas Anderson ihid->ops = ops; 979b33752c3SDouglas Anderson 980b33752c3SDouglas Anderson ret = i2c_hid_core_power_up(ihid); 9819ee3e066SJulian Sax if (ret) 9829ee3e066SJulian Sax return ret; 9839ee3e066SJulian Sax 9849ee3e066SJulian Sax i2c_set_clientdata(client, ihid); 9859ee3e066SJulian Sax 9869ee3e066SJulian Sax ihid->client = client; 9879ee3e066SJulian Sax 988b33752c3SDouglas Anderson ihid->wHIDDescRegister = cpu_to_le16(hid_descriptor_address); 9899ee3e066SJulian Sax 9909ee3e066SJulian Sax init_waitqueue_head(&ihid->wait); 9919ee3e066SJulian Sax mutex_init(&ihid->reset_lock); 9929ee3e066SJulian Sax 9939ee3e066SJulian Sax /* we need to allocate the command buffer without knowing the maximum 9949ee3e066SJulian Sax * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the 9959ee3e066SJulian Sax * real computation later. */ 9969ee3e066SJulian Sax ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE); 9979ee3e066SJulian Sax if (ret < 0) 998b33752c3SDouglas Anderson goto err_powered; 999203c38fbSKai-Heng Feng 10009ee3e066SJulian Sax device_enable_async_suspend(&client->dev); 10019ee3e066SJulian Sax 10029ee3e066SJulian Sax /* Make sure there is something at this address */ 10039ee3e066SJulian Sax ret = i2c_smbus_read_byte(client); 10049ee3e066SJulian Sax if (ret < 0) { 10059ee3e066SJulian Sax dev_dbg(&client->dev, "nothing at this address: %d\n", ret); 10069ee3e066SJulian Sax ret = -ENXIO; 1007b33752c3SDouglas Anderson goto err_powered; 10089ee3e066SJulian Sax } 10099ee3e066SJulian Sax 10109ee3e066SJulian Sax ret = i2c_hid_fetch_hid_descriptor(ihid); 1011afdd34c5SCoiby Xu if (ret < 0) { 1012afdd34c5SCoiby Xu dev_err(&client->dev, 1013afdd34c5SCoiby Xu "Failed to fetch the HID Descriptor\n"); 1014b33752c3SDouglas Anderson goto err_powered; 1015afdd34c5SCoiby Xu } 10169ee3e066SJulian Sax 10179ee3e066SJulian Sax ret = i2c_hid_init_irq(client); 10189ee3e066SJulian Sax if (ret < 0) 1019b33752c3SDouglas Anderson goto err_powered; 10209ee3e066SJulian Sax 10219ee3e066SJulian Sax hid = hid_allocate_device(); 10229ee3e066SJulian Sax if (IS_ERR(hid)) { 10239ee3e066SJulian Sax ret = PTR_ERR(hid); 10249ee3e066SJulian Sax goto err_irq; 10259ee3e066SJulian Sax } 10269ee3e066SJulian Sax 10279ee3e066SJulian Sax ihid->hid = hid; 10289ee3e066SJulian Sax 10299ee3e066SJulian Sax hid->driver_data = client; 10309ee3e066SJulian Sax hid->ll_driver = &i2c_hid_ll_driver; 10319ee3e066SJulian Sax hid->dev.parent = &client->dev; 10329ee3e066SJulian Sax hid->bus = BUS_I2C; 10339ee3e066SJulian Sax hid->version = le16_to_cpu(ihid->hdesc.bcdVersion); 10349ee3e066SJulian Sax hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID); 10359ee3e066SJulian Sax hid->product = le16_to_cpu(ihid->hdesc.wProductID); 10369ee3e066SJulian Sax 1037dc5f9f55SArnd Bergmann snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X", 1038dc5f9f55SArnd Bergmann client->name, (u16)hid->vendor, (u16)hid->product); 10399ee3e066SJulian Sax strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys)); 10409ee3e066SJulian Sax 10419ee3e066SJulian Sax ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product); 10429ee3e066SJulian Sax 10439ee3e066SJulian Sax ret = hid_add_device(hid); 10449ee3e066SJulian Sax if (ret) { 10459ee3e066SJulian Sax if (ret != -ENODEV) 10469ee3e066SJulian Sax hid_err(client, "can't add hid device: %d\n", ret); 10479ee3e066SJulian Sax goto err_mem_free; 10489ee3e066SJulian Sax } 10499ee3e066SJulian Sax 1050b60d3c80SAlistair Francis hid->quirks |= quirks; 1051b60d3c80SAlistair Francis 10529ee3e066SJulian Sax return 0; 10539ee3e066SJulian Sax 10549ee3e066SJulian Sax err_mem_free: 10559ee3e066SJulian Sax hid_destroy_device(hid); 10569ee3e066SJulian Sax 10579ee3e066SJulian Sax err_irq: 10589ee3e066SJulian Sax free_irq(client->irq, ihid); 10599ee3e066SJulian Sax 1060b33752c3SDouglas Anderson err_powered: 1061b33752c3SDouglas Anderson i2c_hid_core_power_down(ihid); 10629ee3e066SJulian Sax i2c_hid_free_buffers(ihid); 10639ee3e066SJulian Sax return ret; 10649ee3e066SJulian Sax } 1065b33752c3SDouglas Anderson EXPORT_SYMBOL_GPL(i2c_hid_core_probe); 10669ee3e066SJulian Sax 1067b33752c3SDouglas Anderson int i2c_hid_core_remove(struct i2c_client *client) 10689ee3e066SJulian Sax { 10699ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 10709ee3e066SJulian Sax struct hid_device *hid; 10719ee3e066SJulian Sax 10729ee3e066SJulian Sax hid = ihid->hid; 10739ee3e066SJulian Sax hid_destroy_device(hid); 10749ee3e066SJulian Sax 10759ee3e066SJulian Sax free_irq(client->irq, ihid); 10769ee3e066SJulian Sax 10779ee3e066SJulian Sax if (ihid->bufsize) 10789ee3e066SJulian Sax i2c_hid_free_buffers(ihid); 10799ee3e066SJulian Sax 1080b33752c3SDouglas Anderson i2c_hid_core_power_down(ihid); 10819ee3e066SJulian Sax 10829ee3e066SJulian Sax return 0; 10839ee3e066SJulian Sax } 1084b33752c3SDouglas Anderson EXPORT_SYMBOL_GPL(i2c_hid_core_remove); 10859ee3e066SJulian Sax 1086b33752c3SDouglas Anderson void i2c_hid_core_shutdown(struct i2c_client *client) 10879ee3e066SJulian Sax { 10889ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 10899ee3e066SJulian Sax 1090d34c6105SDmitry Torokhov i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP); 10919ee3e066SJulian Sax free_irq(client->irq, ihid); 10925c7e02a8SHans de Goede 1093b33752c3SDouglas Anderson i2c_hid_core_shutdown_tail(ihid); 10949ee3e066SJulian Sax } 1095b33752c3SDouglas Anderson EXPORT_SYMBOL_GPL(i2c_hid_core_shutdown); 10969ee3e066SJulian Sax 10979ee3e066SJulian Sax #ifdef CONFIG_PM_SLEEP 1098b33752c3SDouglas Anderson static int i2c_hid_core_suspend(struct device *dev) 10999ee3e066SJulian Sax { 11009ee3e066SJulian Sax struct i2c_client *client = to_i2c_client(dev); 11019ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 11029ee3e066SJulian Sax struct hid_device *hid = ihid->hid; 11039ee3e066SJulian Sax int ret; 11049ee3e066SJulian Sax int wake_status; 11059ee3e066SJulian Sax 11069e356208SBenjamin Tissoires ret = hid_driver_suspend(hid, PMSG_SUSPEND); 11079ee3e066SJulian Sax if (ret < 0) 11089ee3e066SJulian Sax return ret; 11099ee3e066SJulian Sax 11109ee3e066SJulian Sax /* Save some power */ 1111d34c6105SDmitry Torokhov i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP); 11129ee3e066SJulian Sax 11139ee3e066SJulian Sax disable_irq(client->irq); 11149ee3e066SJulian Sax 11159ee3e066SJulian Sax if (device_may_wakeup(&client->dev)) { 11169ee3e066SJulian Sax wake_status = enable_irq_wake(client->irq); 11179ee3e066SJulian Sax if (!wake_status) 11189ee3e066SJulian Sax ihid->irq_wake_enabled = true; 11199ee3e066SJulian Sax else 11209ee3e066SJulian Sax hid_warn(hid, "Failed to enable irq wake: %d\n", 11219ee3e066SJulian Sax wake_status); 11229ee3e066SJulian Sax } else { 1123b33752c3SDouglas Anderson i2c_hid_core_power_down(ihid); 11249ee3e066SJulian Sax } 11259ee3e066SJulian Sax 11269ee3e066SJulian Sax return 0; 11279ee3e066SJulian Sax } 11289ee3e066SJulian Sax 1129b33752c3SDouglas Anderson static int i2c_hid_core_resume(struct device *dev) 11309ee3e066SJulian Sax { 11319ee3e066SJulian Sax int ret; 11329ee3e066SJulian Sax struct i2c_client *client = to_i2c_client(dev); 11339ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 11349ee3e066SJulian Sax struct hid_device *hid = ihid->hid; 11359ee3e066SJulian Sax int wake_status; 11369ee3e066SJulian Sax 11379ee3e066SJulian Sax if (!device_may_wakeup(&client->dev)) { 1138b33752c3SDouglas Anderson i2c_hid_core_power_up(ihid); 11399ee3e066SJulian Sax } else if (ihid->irq_wake_enabled) { 11409ee3e066SJulian Sax wake_status = disable_irq_wake(client->irq); 11419ee3e066SJulian Sax if (!wake_status) 11429ee3e066SJulian Sax ihid->irq_wake_enabled = false; 11439ee3e066SJulian Sax else 11449ee3e066SJulian Sax hid_warn(hid, "Failed to disable irq wake: %d\n", 11459ee3e066SJulian Sax wake_status); 11469ee3e066SJulian Sax } 11479ee3e066SJulian Sax 11489ee3e066SJulian Sax enable_irq(client->irq); 11499ee3e066SJulian Sax 11509ee3e066SJulian Sax /* Instead of resetting device, simply powers the device on. This 11519ee3e066SJulian Sax * solves "incomplete reports" on Raydium devices 2386:3118 and 11524e7be68eSJiri Kosina * 2386:4B33 and fixes various SIS touchscreens no longer sending 11534e7be68eSJiri Kosina * data after a suspend/resume. 1154fd70466dSKai-Heng Feng * 1155fd70466dSKai-Heng Feng * However some ALPS touchpads generate IRQ storm without reset, so 1156fd70466dSKai-Heng Feng * let's still reset them here. 11579ee3e066SJulian Sax */ 1158fd70466dSKai-Heng Feng if (ihid->quirks & I2C_HID_QUIRK_RESET_ON_RESUME) 1159d34c6105SDmitry Torokhov ret = i2c_hid_hwreset(ihid); 1160fd70466dSKai-Heng Feng else 1161d34c6105SDmitry Torokhov ret = i2c_hid_set_power(ihid, I2C_HID_PWR_ON); 1162fd70466dSKai-Heng Feng 11639ee3e066SJulian Sax if (ret) 11649ee3e066SJulian Sax return ret; 11659ee3e066SJulian Sax 11669e356208SBenjamin Tissoires return hid_driver_reset_resume(hid); 11679ee3e066SJulian Sax } 11689ee3e066SJulian Sax #endif 11699ee3e066SJulian Sax 1170b33752c3SDouglas Anderson const struct dev_pm_ops i2c_hid_core_pm = { 1171b33752c3SDouglas Anderson SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_core_suspend, i2c_hid_core_resume) 11729ee3e066SJulian Sax }; 1173b33752c3SDouglas Anderson EXPORT_SYMBOL_GPL(i2c_hid_core_pm); 11749ee3e066SJulian Sax 11759ee3e066SJulian Sax MODULE_DESCRIPTION("HID over I2C core driver"); 11769ee3e066SJulian Sax MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); 11779ee3e066SJulian Sax MODULE_LICENSE("GPL"); 1178