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/pm_runtime.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> 399ee3e066SJulian Sax #include <linux/acpi.h> 409ee3e066SJulian Sax #include <linux/of.h> 419ee3e066SJulian Sax #include <linux/regulator/consumer.h> 429ee3e066SJulian Sax 439ee3e066SJulian Sax #include <linux/platform_data/i2c-hid.h> 449ee3e066SJulian Sax 459ee3e066SJulian Sax #include "../hid-ids.h" 469ee3e066SJulian Sax #include "i2c-hid.h" 479ee3e066SJulian Sax 489ee3e066SJulian Sax /* quirks to control the device */ 499ee3e066SJulian Sax #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0) 509ee3e066SJulian Sax #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1) 514e7be68eSJiri Kosina #define I2C_HID_QUIRK_NO_RUNTIME_PM BIT(2) 5200b790eaSKai-Heng Feng #define I2C_HID_QUIRK_DELAY_AFTER_SLEEP BIT(3) 531475af25SKai-Heng Feng #define I2C_HID_QUIRK_BOGUS_IRQ BIT(4) 549ee3e066SJulian Sax 559ee3e066SJulian Sax /* flags */ 569ee3e066SJulian Sax #define I2C_HID_STARTED 0 579ee3e066SJulian Sax #define I2C_HID_RESET_PENDING 1 589ee3e066SJulian Sax #define I2C_HID_READ_PENDING 2 599ee3e066SJulian Sax 609ee3e066SJulian Sax #define I2C_HID_PWR_ON 0x00 619ee3e066SJulian Sax #define I2C_HID_PWR_SLEEP 0x01 629ee3e066SJulian Sax 639ee3e066SJulian Sax /* debug option */ 649ee3e066SJulian Sax static bool debug; 659ee3e066SJulian Sax module_param(debug, bool, 0444); 669ee3e066SJulian Sax MODULE_PARM_DESC(debug, "print a lot of debug information"); 679ee3e066SJulian Sax 689ee3e066SJulian Sax #define i2c_hid_dbg(ihid, fmt, arg...) \ 699ee3e066SJulian Sax do { \ 709ee3e066SJulian Sax if (debug) \ 719ee3e066SJulian Sax dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \ 729ee3e066SJulian Sax } while (0) 739ee3e066SJulian Sax 749ee3e066SJulian Sax struct i2c_hid_desc { 759ee3e066SJulian Sax __le16 wHIDDescLength; 769ee3e066SJulian Sax __le16 bcdVersion; 779ee3e066SJulian Sax __le16 wReportDescLength; 789ee3e066SJulian Sax __le16 wReportDescRegister; 799ee3e066SJulian Sax __le16 wInputRegister; 809ee3e066SJulian Sax __le16 wMaxInputLength; 819ee3e066SJulian Sax __le16 wOutputRegister; 829ee3e066SJulian Sax __le16 wMaxOutputLength; 839ee3e066SJulian Sax __le16 wCommandRegister; 849ee3e066SJulian Sax __le16 wDataRegister; 859ee3e066SJulian Sax __le16 wVendorID; 869ee3e066SJulian Sax __le16 wProductID; 879ee3e066SJulian Sax __le16 wVersionID; 889ee3e066SJulian Sax __le32 reserved; 899ee3e066SJulian Sax } __packed; 909ee3e066SJulian Sax 919ee3e066SJulian Sax struct i2c_hid_cmd { 929ee3e066SJulian Sax unsigned int registerIndex; 939ee3e066SJulian Sax __u8 opcode; 949ee3e066SJulian Sax unsigned int length; 959ee3e066SJulian Sax bool wait; 969ee3e066SJulian Sax }; 979ee3e066SJulian Sax 989ee3e066SJulian Sax union command { 999ee3e066SJulian Sax u8 data[0]; 1009ee3e066SJulian Sax struct cmd { 1019ee3e066SJulian Sax __le16 reg; 1029ee3e066SJulian Sax __u8 reportTypeID; 1039ee3e066SJulian Sax __u8 opcode; 1049ee3e066SJulian Sax } __packed c; 1059ee3e066SJulian Sax }; 1069ee3e066SJulian Sax 1079ee3e066SJulian Sax #define I2C_HID_CMD(opcode_) \ 1089ee3e066SJulian Sax .opcode = opcode_, .length = 4, \ 1099ee3e066SJulian Sax .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister) 1109ee3e066SJulian Sax 1119ee3e066SJulian Sax /* fetch HID descriptor */ 1129ee3e066SJulian Sax static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 }; 1139ee3e066SJulian Sax /* fetch report descriptors */ 1149ee3e066SJulian Sax static const struct i2c_hid_cmd hid_report_descr_cmd = { 1159ee3e066SJulian Sax .registerIndex = offsetof(struct i2c_hid_desc, 1169ee3e066SJulian Sax wReportDescRegister), 1179ee3e066SJulian Sax .opcode = 0x00, 1189ee3e066SJulian Sax .length = 2 }; 1199ee3e066SJulian Sax /* commands */ 1209ee3e066SJulian Sax static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01), 1219ee3e066SJulian Sax .wait = true }; 1229ee3e066SJulian Sax static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) }; 1239ee3e066SJulian Sax static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) }; 1249ee3e066SJulian Sax static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) }; 1259ee3e066SJulian Sax static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 }; 1269ee3e066SJulian Sax 1279ee3e066SJulian Sax /* 1289ee3e066SJulian Sax * These definitions are not used here, but are defined by the spec. 1299ee3e066SJulian Sax * Keeping them here for documentation purposes. 1309ee3e066SJulian Sax * 1319ee3e066SJulian Sax * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) }; 1329ee3e066SJulian Sax * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) }; 1339ee3e066SJulian Sax * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) }; 1349ee3e066SJulian Sax * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) }; 1359ee3e066SJulian Sax */ 1369ee3e066SJulian Sax 1379ee3e066SJulian Sax /* The main device structure */ 1389ee3e066SJulian Sax struct i2c_hid { 1399ee3e066SJulian Sax struct i2c_client *client; /* i2c client */ 1409ee3e066SJulian Sax struct hid_device *hid; /* pointer to corresponding HID dev */ 1419ee3e066SJulian Sax union { 1429ee3e066SJulian Sax __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)]; 1439ee3e066SJulian Sax struct i2c_hid_desc hdesc; /* the HID Descriptor */ 1449ee3e066SJulian Sax }; 1459ee3e066SJulian Sax __le16 wHIDDescRegister; /* location of the i2c 1469ee3e066SJulian Sax * register of the HID 1479ee3e066SJulian Sax * descriptor. */ 1489ee3e066SJulian Sax unsigned int bufsize; /* i2c buffer size */ 1499ee3e066SJulian Sax u8 *inbuf; /* Input buffer */ 1509ee3e066SJulian Sax u8 *rawbuf; /* Raw Input buffer */ 1519ee3e066SJulian Sax u8 *cmdbuf; /* Command buffer */ 1529ee3e066SJulian Sax u8 *argsbuf; /* Command arguments buffer */ 1539ee3e066SJulian Sax 1549ee3e066SJulian Sax unsigned long flags; /* device flags */ 1559ee3e066SJulian Sax unsigned long quirks; /* Various quirks */ 1569ee3e066SJulian Sax 1579ee3e066SJulian Sax wait_queue_head_t wait; /* For waiting the interrupt */ 1589ee3e066SJulian Sax 1599ee3e066SJulian Sax struct i2c_hid_platform_data pdata; 1609ee3e066SJulian Sax 1619ee3e066SJulian Sax bool irq_wake_enabled; 1629ee3e066SJulian Sax struct mutex reset_lock; 16300b790eaSKai-Heng Feng 16400b790eaSKai-Heng Feng unsigned long sleep_delay; 1659ee3e066SJulian Sax }; 1669ee3e066SJulian Sax 1679ee3e066SJulian Sax static const struct i2c_hid_quirks { 1689ee3e066SJulian Sax __u16 idVendor; 1699ee3e066SJulian Sax __u16 idProduct; 1709ee3e066SJulian Sax __u32 quirks; 1719ee3e066SJulian Sax } i2c_hid_quirks[] = { 172*b20bef4bSHungNien Chen { USB_VENDOR_ID_WEIDA, HID_ANY_ID, 1739ee3e066SJulian Sax I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV }, 1749ee3e066SJulian Sax { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288, 1754e7be68eSJiri Kosina I2C_HID_QUIRK_NO_IRQ_AFTER_RESET | 1764e7be68eSJiri Kosina I2C_HID_QUIRK_NO_RUNTIME_PM }, 17700b790eaSKai-Heng Feng { I2C_VENDOR_ID_RAYDIUM, I2C_PRODUCT_ID_RAYDIUM_4B33, 17800b790eaSKai-Heng Feng I2C_HID_QUIRK_DELAY_AFTER_SLEEP }, 17986c31524SKai-Heng Feng { USB_VENDOR_ID_LG, I2C_DEVICE_ID_LG_8001, 18086c31524SKai-Heng Feng I2C_HID_QUIRK_NO_RUNTIME_PM }, 18177ae0d8eSKai-Heng Feng { I2C_VENDOR_ID_GOODIX, I2C_DEVICE_ID_GOODIX_01F0, 18277ae0d8eSKai-Heng Feng I2C_HID_QUIRK_NO_RUNTIME_PM }, 1831475af25SKai-Heng Feng { USB_VENDOR_ID_ELAN, HID_ANY_ID, 1841475af25SKai-Heng Feng I2C_HID_QUIRK_BOGUS_IRQ }, 1859ee3e066SJulian Sax { 0, 0 } 1869ee3e066SJulian Sax }; 1879ee3e066SJulian Sax 1889ee3e066SJulian Sax /* 1899ee3e066SJulian Sax * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device 1909ee3e066SJulian Sax * @idVendor: the 16-bit vendor ID 1919ee3e066SJulian Sax * @idProduct: the 16-bit product ID 1929ee3e066SJulian Sax * 1939ee3e066SJulian Sax * Returns: a u32 quirks value. 1949ee3e066SJulian Sax */ 1959ee3e066SJulian Sax static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct) 1969ee3e066SJulian Sax { 1979ee3e066SJulian Sax u32 quirks = 0; 1989ee3e066SJulian Sax int n; 1999ee3e066SJulian Sax 2009ee3e066SJulian Sax for (n = 0; i2c_hid_quirks[n].idVendor; n++) 2019ee3e066SJulian Sax if (i2c_hid_quirks[n].idVendor == idVendor && 2029ee3e066SJulian Sax (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID || 2039ee3e066SJulian Sax i2c_hid_quirks[n].idProduct == idProduct)) 2049ee3e066SJulian Sax quirks = i2c_hid_quirks[n].quirks; 2059ee3e066SJulian Sax 2069ee3e066SJulian Sax return quirks; 2079ee3e066SJulian Sax } 2089ee3e066SJulian Sax 2099ee3e066SJulian Sax static int __i2c_hid_command(struct i2c_client *client, 2109ee3e066SJulian Sax const struct i2c_hid_cmd *command, u8 reportID, 2119ee3e066SJulian Sax u8 reportType, u8 *args, int args_len, 2129ee3e066SJulian Sax unsigned char *buf_recv, int data_len) 2139ee3e066SJulian Sax { 2149ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 2159ee3e066SJulian Sax union command *cmd = (union command *)ihid->cmdbuf; 2169ee3e066SJulian Sax int ret; 2179ee3e066SJulian Sax struct i2c_msg msg[2]; 2189ee3e066SJulian Sax int msg_num = 1; 2199ee3e066SJulian Sax 2209ee3e066SJulian Sax int length = command->length; 2219ee3e066SJulian Sax bool wait = command->wait; 2229ee3e066SJulian Sax unsigned int registerIndex = command->registerIndex; 2239ee3e066SJulian Sax 2249ee3e066SJulian Sax /* special case for hid_descr_cmd */ 2259ee3e066SJulian Sax if (command == &hid_descr_cmd) { 2269ee3e066SJulian Sax cmd->c.reg = ihid->wHIDDescRegister; 2279ee3e066SJulian Sax } else { 2289ee3e066SJulian Sax cmd->data[0] = ihid->hdesc_buffer[registerIndex]; 2299ee3e066SJulian Sax cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1]; 2309ee3e066SJulian Sax } 2319ee3e066SJulian Sax 2329ee3e066SJulian Sax if (length > 2) { 2339ee3e066SJulian Sax cmd->c.opcode = command->opcode; 2349ee3e066SJulian Sax cmd->c.reportTypeID = reportID | reportType << 4; 2359ee3e066SJulian Sax } 2369ee3e066SJulian Sax 2379ee3e066SJulian Sax memcpy(cmd->data + length, args, args_len); 2389ee3e066SJulian Sax length += args_len; 2399ee3e066SJulian Sax 2409ee3e066SJulian Sax i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data); 2419ee3e066SJulian Sax 2429ee3e066SJulian Sax msg[0].addr = client->addr; 2439ee3e066SJulian Sax msg[0].flags = client->flags & I2C_M_TEN; 2449ee3e066SJulian Sax msg[0].len = length; 2459ee3e066SJulian Sax msg[0].buf = cmd->data; 2469ee3e066SJulian Sax if (data_len > 0) { 2479ee3e066SJulian Sax msg[1].addr = client->addr; 2489ee3e066SJulian Sax msg[1].flags = client->flags & I2C_M_TEN; 2499ee3e066SJulian Sax msg[1].flags |= I2C_M_RD; 2509ee3e066SJulian Sax msg[1].len = data_len; 2519ee3e066SJulian Sax msg[1].buf = buf_recv; 2529ee3e066SJulian Sax msg_num = 2; 2539ee3e066SJulian Sax set_bit(I2C_HID_READ_PENDING, &ihid->flags); 2549ee3e066SJulian Sax } 2559ee3e066SJulian Sax 2569ee3e066SJulian Sax if (wait) 2579ee3e066SJulian Sax set_bit(I2C_HID_RESET_PENDING, &ihid->flags); 2589ee3e066SJulian Sax 2599ee3e066SJulian Sax ret = i2c_transfer(client->adapter, msg, msg_num); 2609ee3e066SJulian Sax 2619ee3e066SJulian Sax if (data_len > 0) 2629ee3e066SJulian Sax clear_bit(I2C_HID_READ_PENDING, &ihid->flags); 2639ee3e066SJulian Sax 2649ee3e066SJulian Sax if (ret != msg_num) 2659ee3e066SJulian Sax return ret < 0 ? ret : -EIO; 2669ee3e066SJulian Sax 2679ee3e066SJulian Sax ret = 0; 2689ee3e066SJulian Sax 2699ee3e066SJulian Sax if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) { 2709ee3e066SJulian Sax msleep(100); 2719ee3e066SJulian Sax } else if (wait) { 2729ee3e066SJulian Sax i2c_hid_dbg(ihid, "%s: waiting...\n", __func__); 2739ee3e066SJulian Sax if (!wait_event_timeout(ihid->wait, 2749ee3e066SJulian Sax !test_bit(I2C_HID_RESET_PENDING, &ihid->flags), 2759ee3e066SJulian Sax msecs_to_jiffies(5000))) 2769ee3e066SJulian Sax ret = -ENODATA; 2779ee3e066SJulian Sax i2c_hid_dbg(ihid, "%s: finished.\n", __func__); 2789ee3e066SJulian Sax } 2799ee3e066SJulian Sax 2809ee3e066SJulian Sax return ret; 2819ee3e066SJulian Sax } 2829ee3e066SJulian Sax 2839ee3e066SJulian Sax static int i2c_hid_command(struct i2c_client *client, 2849ee3e066SJulian Sax const struct i2c_hid_cmd *command, 2859ee3e066SJulian Sax unsigned char *buf_recv, int data_len) 2869ee3e066SJulian Sax { 2879ee3e066SJulian Sax return __i2c_hid_command(client, command, 0, 0, NULL, 0, 2889ee3e066SJulian Sax buf_recv, data_len); 2899ee3e066SJulian Sax } 2909ee3e066SJulian Sax 2919ee3e066SJulian Sax static int i2c_hid_get_report(struct i2c_client *client, u8 reportType, 2929ee3e066SJulian Sax u8 reportID, unsigned char *buf_recv, int data_len) 2939ee3e066SJulian Sax { 2949ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 2959ee3e066SJulian Sax u8 args[3]; 2969ee3e066SJulian Sax int ret; 2979ee3e066SJulian Sax int args_len = 0; 2989ee3e066SJulian Sax u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister); 2999ee3e066SJulian Sax 3009ee3e066SJulian Sax i2c_hid_dbg(ihid, "%s\n", __func__); 3019ee3e066SJulian Sax 3029ee3e066SJulian Sax if (reportID >= 0x0F) { 3039ee3e066SJulian Sax args[args_len++] = reportID; 3049ee3e066SJulian Sax reportID = 0x0F; 3059ee3e066SJulian Sax } 3069ee3e066SJulian Sax 3079ee3e066SJulian Sax args[args_len++] = readRegister & 0xFF; 3089ee3e066SJulian Sax args[args_len++] = readRegister >> 8; 3099ee3e066SJulian Sax 3109ee3e066SJulian Sax ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID, 3119ee3e066SJulian Sax reportType, args, args_len, buf_recv, data_len); 3129ee3e066SJulian Sax if (ret) { 3139ee3e066SJulian Sax dev_err(&client->dev, 3149ee3e066SJulian Sax "failed to retrieve report from device.\n"); 3159ee3e066SJulian Sax return ret; 3169ee3e066SJulian Sax } 3179ee3e066SJulian Sax 3189ee3e066SJulian Sax return 0; 3199ee3e066SJulian Sax } 3209ee3e066SJulian Sax 3219ee3e066SJulian Sax /** 3229ee3e066SJulian Sax * i2c_hid_set_or_send_report: forward an incoming report to the device 3239ee3e066SJulian Sax * @client: the i2c_client of the device 3249ee3e066SJulian Sax * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT 3259ee3e066SJulian Sax * @reportID: the report ID 3269ee3e066SJulian Sax * @buf: the actual data to transfer, without the report ID 3279ee3e066SJulian Sax * @len: size of buf 3289ee3e066SJulian Sax * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report 3299ee3e066SJulian Sax */ 3309ee3e066SJulian Sax static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType, 3319ee3e066SJulian Sax u8 reportID, unsigned char *buf, size_t data_len, bool use_data) 3329ee3e066SJulian Sax { 3339ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 3349ee3e066SJulian Sax u8 *args = ihid->argsbuf; 3359ee3e066SJulian Sax const struct i2c_hid_cmd *hidcmd; 3369ee3e066SJulian Sax int ret; 3379ee3e066SJulian Sax u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister); 3389ee3e066SJulian Sax u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister); 3399ee3e066SJulian Sax u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength); 3409ee3e066SJulian Sax u16 size; 3419ee3e066SJulian Sax int args_len; 3429ee3e066SJulian Sax int index = 0; 3439ee3e066SJulian Sax 3449ee3e066SJulian Sax i2c_hid_dbg(ihid, "%s\n", __func__); 3459ee3e066SJulian Sax 3469ee3e066SJulian Sax if (data_len > ihid->bufsize) 3479ee3e066SJulian Sax return -EINVAL; 3489ee3e066SJulian Sax 3499ee3e066SJulian Sax size = 2 /* size */ + 3509ee3e066SJulian Sax (reportID ? 1 : 0) /* reportID */ + 3519ee3e066SJulian Sax data_len /* buf */; 3529ee3e066SJulian Sax args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ + 3539ee3e066SJulian Sax 2 /* dataRegister */ + 3549ee3e066SJulian Sax size /* args */; 3559ee3e066SJulian Sax 3569ee3e066SJulian Sax if (!use_data && maxOutputLength == 0) 3579ee3e066SJulian Sax return -ENOSYS; 3589ee3e066SJulian Sax 3599ee3e066SJulian Sax if (reportID >= 0x0F) { 3609ee3e066SJulian Sax args[index++] = reportID; 3619ee3e066SJulian Sax reportID = 0x0F; 3629ee3e066SJulian Sax } 3639ee3e066SJulian Sax 3649ee3e066SJulian Sax /* 3659ee3e066SJulian Sax * use the data register for feature reports or if the device does not 3669ee3e066SJulian Sax * support the output register 3679ee3e066SJulian Sax */ 3689ee3e066SJulian Sax if (use_data) { 3699ee3e066SJulian Sax args[index++] = dataRegister & 0xFF; 3709ee3e066SJulian Sax args[index++] = dataRegister >> 8; 3719ee3e066SJulian Sax hidcmd = &hid_set_report_cmd; 3729ee3e066SJulian Sax } else { 3739ee3e066SJulian Sax args[index++] = outputRegister & 0xFF; 3749ee3e066SJulian Sax args[index++] = outputRegister >> 8; 3759ee3e066SJulian Sax hidcmd = &hid_no_cmd; 3769ee3e066SJulian Sax } 3779ee3e066SJulian Sax 3789ee3e066SJulian Sax args[index++] = size & 0xFF; 3799ee3e066SJulian Sax args[index++] = size >> 8; 3809ee3e066SJulian Sax 3819ee3e066SJulian Sax if (reportID) 3829ee3e066SJulian Sax args[index++] = reportID; 3839ee3e066SJulian Sax 3849ee3e066SJulian Sax memcpy(&args[index], buf, data_len); 3859ee3e066SJulian Sax 3869ee3e066SJulian Sax ret = __i2c_hid_command(client, hidcmd, reportID, 3879ee3e066SJulian Sax reportType, args, args_len, NULL, 0); 3889ee3e066SJulian Sax if (ret) { 3899ee3e066SJulian Sax dev_err(&client->dev, "failed to set a report to device.\n"); 3909ee3e066SJulian Sax return ret; 3919ee3e066SJulian Sax } 3929ee3e066SJulian Sax 3939ee3e066SJulian Sax return data_len; 3949ee3e066SJulian Sax } 3959ee3e066SJulian Sax 3969ee3e066SJulian Sax static int i2c_hid_set_power(struct i2c_client *client, int power_state) 3979ee3e066SJulian Sax { 3989ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 3999ee3e066SJulian Sax int ret; 40000b790eaSKai-Heng Feng unsigned long now, delay; 4019ee3e066SJulian Sax 4029ee3e066SJulian Sax i2c_hid_dbg(ihid, "%s\n", __func__); 4039ee3e066SJulian Sax 4049ee3e066SJulian Sax /* 4059ee3e066SJulian Sax * Some devices require to send a command to wakeup before power on. 4069ee3e066SJulian Sax * The call will get a return value (EREMOTEIO) but device will be 4079ee3e066SJulian Sax * triggered and activated. After that, it goes like a normal device. 4089ee3e066SJulian Sax */ 4099ee3e066SJulian Sax if (power_state == I2C_HID_PWR_ON && 4109ee3e066SJulian Sax ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) { 4119ee3e066SJulian Sax ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0); 4129ee3e066SJulian Sax 4139ee3e066SJulian Sax /* Device was already activated */ 4149ee3e066SJulian Sax if (!ret) 4159ee3e066SJulian Sax goto set_pwr_exit; 4169ee3e066SJulian Sax } 4179ee3e066SJulian Sax 41800b790eaSKai-Heng Feng if (ihid->quirks & I2C_HID_QUIRK_DELAY_AFTER_SLEEP && 41900b790eaSKai-Heng Feng power_state == I2C_HID_PWR_ON) { 42000b790eaSKai-Heng Feng now = jiffies; 42100b790eaSKai-Heng Feng if (time_after(ihid->sleep_delay, now)) { 42200b790eaSKai-Heng Feng delay = jiffies_to_usecs(ihid->sleep_delay - now); 42300b790eaSKai-Heng Feng usleep_range(delay, delay + 1); 42400b790eaSKai-Heng Feng } 42500b790eaSKai-Heng Feng } 42600b790eaSKai-Heng Feng 4279ee3e066SJulian Sax ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state, 4289ee3e066SJulian Sax 0, NULL, 0, NULL, 0); 4299ee3e066SJulian Sax 43000b790eaSKai-Heng Feng if (ihid->quirks & I2C_HID_QUIRK_DELAY_AFTER_SLEEP && 43100b790eaSKai-Heng Feng power_state == I2C_HID_PWR_SLEEP) 43200b790eaSKai-Heng Feng ihid->sleep_delay = jiffies + msecs_to_jiffies(20); 43300b790eaSKai-Heng Feng 4349ee3e066SJulian Sax if (ret) 4359ee3e066SJulian Sax dev_err(&client->dev, "failed to change power setting.\n"); 4369ee3e066SJulian Sax 4379ee3e066SJulian Sax set_pwr_exit: 4389ee3e066SJulian Sax return ret; 4399ee3e066SJulian Sax } 4409ee3e066SJulian Sax 4419ee3e066SJulian Sax static int i2c_hid_hwreset(struct i2c_client *client) 4429ee3e066SJulian Sax { 4439ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 4449ee3e066SJulian Sax int ret; 4459ee3e066SJulian Sax 4469ee3e066SJulian Sax i2c_hid_dbg(ihid, "%s\n", __func__); 4479ee3e066SJulian Sax 4489ee3e066SJulian Sax /* 4499ee3e066SJulian Sax * This prevents sending feature reports while the device is 4509ee3e066SJulian Sax * being reset. Otherwise we may lose the reset complete 4519ee3e066SJulian Sax * interrupt. 4529ee3e066SJulian Sax */ 4539ee3e066SJulian Sax mutex_lock(&ihid->reset_lock); 4549ee3e066SJulian Sax 4559ee3e066SJulian Sax ret = i2c_hid_set_power(client, I2C_HID_PWR_ON); 4569ee3e066SJulian Sax if (ret) 4579ee3e066SJulian Sax goto out_unlock; 4589ee3e066SJulian Sax 4599ee3e066SJulian Sax /* 4609ee3e066SJulian Sax * The HID over I2C specification states that if a DEVICE needs time 4619ee3e066SJulian Sax * after the PWR_ON request, it should utilise CLOCK stretching. 4629ee3e066SJulian Sax * However, it has been observered that the Windows driver provides a 4639ee3e066SJulian Sax * 1ms sleep between the PWR_ON and RESET requests and that some devices 4649ee3e066SJulian Sax * rely on this. 4659ee3e066SJulian Sax */ 4669ee3e066SJulian Sax usleep_range(1000, 5000); 4679ee3e066SJulian Sax 4689ee3e066SJulian Sax i2c_hid_dbg(ihid, "resetting...\n"); 4699ee3e066SJulian Sax 4709ee3e066SJulian Sax ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0); 4719ee3e066SJulian Sax if (ret) { 4729ee3e066SJulian Sax dev_err(&client->dev, "failed to reset device.\n"); 4739ee3e066SJulian Sax i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); 4749ee3e066SJulian Sax } 4759ee3e066SJulian Sax 4769ee3e066SJulian Sax out_unlock: 4779ee3e066SJulian Sax mutex_unlock(&ihid->reset_lock); 4789ee3e066SJulian Sax return ret; 4799ee3e066SJulian Sax } 4809ee3e066SJulian Sax 4819ee3e066SJulian Sax static void i2c_hid_get_input(struct i2c_hid *ihid) 4829ee3e066SJulian Sax { 4839ee3e066SJulian Sax int ret; 4849ee3e066SJulian Sax u32 ret_size; 4859ee3e066SJulian Sax int size = le16_to_cpu(ihid->hdesc.wMaxInputLength); 4869ee3e066SJulian Sax 4879ee3e066SJulian Sax if (size > ihid->bufsize) 4889ee3e066SJulian Sax size = ihid->bufsize; 4899ee3e066SJulian Sax 4909ee3e066SJulian Sax ret = i2c_master_recv(ihid->client, ihid->inbuf, size); 4919ee3e066SJulian Sax if (ret != size) { 4929ee3e066SJulian Sax if (ret < 0) 4939ee3e066SJulian Sax return; 4949ee3e066SJulian Sax 4959ee3e066SJulian Sax dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n", 4969ee3e066SJulian Sax __func__, ret, size); 4979ee3e066SJulian Sax return; 4989ee3e066SJulian Sax } 4999ee3e066SJulian Sax 5009ee3e066SJulian Sax ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8; 5019ee3e066SJulian Sax 5029ee3e066SJulian Sax if (!ret_size) { 5039ee3e066SJulian Sax /* host or device initiated RESET completed */ 5049ee3e066SJulian Sax if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags)) 5059ee3e066SJulian Sax wake_up(&ihid->wait); 5069ee3e066SJulian Sax return; 5079ee3e066SJulian Sax } 5089ee3e066SJulian Sax 5091475af25SKai-Heng Feng if (ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ && ret_size == 0xffff) { 5101475af25SKai-Heng Feng dev_warn_once(&ihid->client->dev, "%s: IRQ triggered but " 5111475af25SKai-Heng Feng "there's no data\n", __func__); 5121475af25SKai-Heng Feng return; 5131475af25SKai-Heng Feng } 5141475af25SKai-Heng Feng 5159ee3e066SJulian Sax if ((ret_size > size) || (ret_size < 2)) { 5169ee3e066SJulian Sax dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n", 5179ee3e066SJulian Sax __func__, size, ret_size); 5189ee3e066SJulian Sax return; 5199ee3e066SJulian Sax } 5209ee3e066SJulian Sax 5219ee3e066SJulian Sax i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf); 5229ee3e066SJulian Sax 5239ee3e066SJulian Sax if (test_bit(I2C_HID_STARTED, &ihid->flags)) 5249ee3e066SJulian Sax hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2, 5259ee3e066SJulian Sax ret_size - 2, 1); 5269ee3e066SJulian Sax 5279ee3e066SJulian Sax return; 5289ee3e066SJulian Sax } 5299ee3e066SJulian Sax 5309ee3e066SJulian Sax static irqreturn_t i2c_hid_irq(int irq, void *dev_id) 5319ee3e066SJulian Sax { 5329ee3e066SJulian Sax struct i2c_hid *ihid = dev_id; 5339ee3e066SJulian Sax 5349ee3e066SJulian Sax if (test_bit(I2C_HID_READ_PENDING, &ihid->flags)) 5359ee3e066SJulian Sax return IRQ_HANDLED; 5369ee3e066SJulian Sax 5379ee3e066SJulian Sax i2c_hid_get_input(ihid); 5389ee3e066SJulian Sax 5399ee3e066SJulian Sax return IRQ_HANDLED; 5409ee3e066SJulian Sax } 5419ee3e066SJulian Sax 5429ee3e066SJulian Sax static int i2c_hid_get_report_length(struct hid_report *report) 5439ee3e066SJulian Sax { 5449ee3e066SJulian Sax return ((report->size - 1) >> 3) + 1 + 5459ee3e066SJulian Sax report->device->report_enum[report->type].numbered + 2; 5469ee3e066SJulian Sax } 5479ee3e066SJulian Sax 5489ee3e066SJulian Sax /* 5499ee3e066SJulian Sax * Traverse the supplied list of reports and find the longest 5509ee3e066SJulian Sax */ 5519ee3e066SJulian Sax static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type, 5529ee3e066SJulian Sax unsigned int *max) 5539ee3e066SJulian Sax { 5549ee3e066SJulian Sax struct hid_report *report; 5559ee3e066SJulian Sax unsigned int size; 5569ee3e066SJulian Sax 5579ee3e066SJulian Sax /* We should not rely on wMaxInputLength, as some devices may set it to 5589ee3e066SJulian Sax * a wrong length. */ 5599ee3e066SJulian Sax list_for_each_entry(report, &hid->report_enum[type].report_list, list) { 5609ee3e066SJulian Sax size = i2c_hid_get_report_length(report); 5619ee3e066SJulian Sax if (*max < size) 5629ee3e066SJulian Sax *max = size; 5639ee3e066SJulian Sax } 5649ee3e066SJulian Sax } 5659ee3e066SJulian Sax 5669ee3e066SJulian Sax static void i2c_hid_free_buffers(struct i2c_hid *ihid) 5679ee3e066SJulian Sax { 5689ee3e066SJulian Sax kfree(ihid->inbuf); 5699ee3e066SJulian Sax kfree(ihid->rawbuf); 5709ee3e066SJulian Sax kfree(ihid->argsbuf); 5719ee3e066SJulian Sax kfree(ihid->cmdbuf); 5729ee3e066SJulian Sax ihid->inbuf = NULL; 5739ee3e066SJulian Sax ihid->rawbuf = NULL; 5749ee3e066SJulian Sax ihid->cmdbuf = NULL; 5759ee3e066SJulian Sax ihid->argsbuf = NULL; 5769ee3e066SJulian Sax ihid->bufsize = 0; 5779ee3e066SJulian Sax } 5789ee3e066SJulian Sax 5799ee3e066SJulian Sax static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size) 5809ee3e066SJulian Sax { 5819ee3e066SJulian Sax /* the worst case is computed from the set_report command with a 5829ee3e066SJulian Sax * reportID > 15 and the maximum report length */ 5839ee3e066SJulian Sax int args_len = sizeof(__u8) + /* ReportID */ 5849ee3e066SJulian Sax sizeof(__u8) + /* optional ReportID byte */ 5859ee3e066SJulian Sax sizeof(__u16) + /* data register */ 5869ee3e066SJulian Sax sizeof(__u16) + /* size of the report */ 5879ee3e066SJulian Sax report_size; /* report */ 5889ee3e066SJulian Sax 5899ee3e066SJulian Sax ihid->inbuf = kzalloc(report_size, GFP_KERNEL); 5909ee3e066SJulian Sax ihid->rawbuf = kzalloc(report_size, GFP_KERNEL); 5919ee3e066SJulian Sax ihid->argsbuf = kzalloc(args_len, GFP_KERNEL); 5929ee3e066SJulian Sax ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL); 5939ee3e066SJulian Sax 5949ee3e066SJulian Sax if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) { 5959ee3e066SJulian Sax i2c_hid_free_buffers(ihid); 5969ee3e066SJulian Sax return -ENOMEM; 5979ee3e066SJulian Sax } 5989ee3e066SJulian Sax 5999ee3e066SJulian Sax ihid->bufsize = report_size; 6009ee3e066SJulian Sax 6019ee3e066SJulian Sax return 0; 6029ee3e066SJulian Sax } 6039ee3e066SJulian Sax 6049ee3e066SJulian Sax static int i2c_hid_get_raw_report(struct hid_device *hid, 6059ee3e066SJulian Sax unsigned char report_number, __u8 *buf, size_t count, 6069ee3e066SJulian Sax unsigned char report_type) 6079ee3e066SJulian Sax { 6089ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 6099ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 6109ee3e066SJulian Sax size_t ret_count, ask_count; 6119ee3e066SJulian Sax int ret; 6129ee3e066SJulian Sax 6139ee3e066SJulian Sax if (report_type == HID_OUTPUT_REPORT) 6149ee3e066SJulian Sax return -EINVAL; 6159ee3e066SJulian Sax 6169ee3e066SJulian Sax /* +2 bytes to include the size of the reply in the query buffer */ 6179ee3e066SJulian Sax ask_count = min(count + 2, (size_t)ihid->bufsize); 6189ee3e066SJulian Sax 6199ee3e066SJulian Sax ret = i2c_hid_get_report(client, 6209ee3e066SJulian Sax report_type == HID_FEATURE_REPORT ? 0x03 : 0x01, 6219ee3e066SJulian Sax report_number, ihid->rawbuf, ask_count); 6229ee3e066SJulian Sax 6239ee3e066SJulian Sax if (ret < 0) 6249ee3e066SJulian Sax return ret; 6259ee3e066SJulian Sax 6269ee3e066SJulian Sax ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8); 6279ee3e066SJulian Sax 6289ee3e066SJulian Sax if (ret_count <= 2) 6299ee3e066SJulian Sax return 0; 6309ee3e066SJulian Sax 6319ee3e066SJulian Sax ret_count = min(ret_count, ask_count); 6329ee3e066SJulian Sax 6339ee3e066SJulian Sax /* The query buffer contains the size, dropping it in the reply */ 6349ee3e066SJulian Sax count = min(count, ret_count - 2); 6359ee3e066SJulian Sax memcpy(buf, ihid->rawbuf + 2, count); 6369ee3e066SJulian Sax 6379ee3e066SJulian Sax return count; 6389ee3e066SJulian Sax } 6399ee3e066SJulian Sax 6409ee3e066SJulian Sax static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf, 6419ee3e066SJulian Sax size_t count, unsigned char report_type, bool use_data) 6429ee3e066SJulian Sax { 6439ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 6449ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 6459ee3e066SJulian Sax int report_id = buf[0]; 6469ee3e066SJulian Sax int ret; 6479ee3e066SJulian Sax 6489ee3e066SJulian Sax if (report_type == HID_INPUT_REPORT) 6499ee3e066SJulian Sax return -EINVAL; 6509ee3e066SJulian Sax 6519ee3e066SJulian Sax mutex_lock(&ihid->reset_lock); 6529ee3e066SJulian Sax 6539ee3e066SJulian Sax if (report_id) { 6549ee3e066SJulian Sax buf++; 6559ee3e066SJulian Sax count--; 6569ee3e066SJulian Sax } 6579ee3e066SJulian Sax 6589ee3e066SJulian Sax ret = i2c_hid_set_or_send_report(client, 6599ee3e066SJulian Sax report_type == HID_FEATURE_REPORT ? 0x03 : 0x02, 6609ee3e066SJulian Sax report_id, buf, count, use_data); 6619ee3e066SJulian Sax 6629ee3e066SJulian Sax if (report_id && ret >= 0) 6639ee3e066SJulian Sax ret++; /* add report_id to the number of transfered bytes */ 6649ee3e066SJulian Sax 6659ee3e066SJulian Sax mutex_unlock(&ihid->reset_lock); 6669ee3e066SJulian Sax 6679ee3e066SJulian Sax return ret; 6689ee3e066SJulian Sax } 6699ee3e066SJulian Sax 6709ee3e066SJulian Sax static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf, 6719ee3e066SJulian Sax size_t count) 6729ee3e066SJulian Sax { 6739ee3e066SJulian Sax return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT, 6749ee3e066SJulian Sax false); 6759ee3e066SJulian Sax } 6769ee3e066SJulian Sax 6779ee3e066SJulian Sax static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum, 6789ee3e066SJulian Sax __u8 *buf, size_t len, unsigned char rtype, 6799ee3e066SJulian Sax int reqtype) 6809ee3e066SJulian Sax { 6819ee3e066SJulian Sax switch (reqtype) { 6829ee3e066SJulian Sax case HID_REQ_GET_REPORT: 6839ee3e066SJulian Sax return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype); 6849ee3e066SJulian Sax case HID_REQ_SET_REPORT: 6859ee3e066SJulian Sax if (buf[0] != reportnum) 6869ee3e066SJulian Sax return -EINVAL; 6879ee3e066SJulian Sax return i2c_hid_output_raw_report(hid, buf, len, rtype, true); 6889ee3e066SJulian Sax default: 6899ee3e066SJulian Sax return -EIO; 6909ee3e066SJulian Sax } 6919ee3e066SJulian Sax } 6929ee3e066SJulian Sax 6939ee3e066SJulian Sax static int i2c_hid_parse(struct hid_device *hid) 6949ee3e066SJulian Sax { 6959ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 6969ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 6979ee3e066SJulian Sax struct i2c_hid_desc *hdesc = &ihid->hdesc; 6989ee3e066SJulian Sax unsigned int rsize; 6999ee3e066SJulian Sax char *rdesc; 7009ee3e066SJulian Sax int ret; 7019ee3e066SJulian Sax int tries = 3; 7029ee3e066SJulian Sax char *use_override; 7039ee3e066SJulian Sax 7049ee3e066SJulian Sax i2c_hid_dbg(ihid, "entering %s\n", __func__); 7059ee3e066SJulian Sax 7069ee3e066SJulian Sax rsize = le16_to_cpu(hdesc->wReportDescLength); 7079ee3e066SJulian Sax if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) { 7089ee3e066SJulian Sax dbg_hid("weird size of report descriptor (%u)\n", rsize); 7099ee3e066SJulian Sax return -EINVAL; 7109ee3e066SJulian Sax } 7119ee3e066SJulian Sax 7129ee3e066SJulian Sax do { 7139ee3e066SJulian Sax ret = i2c_hid_hwreset(client); 7149ee3e066SJulian Sax if (ret) 7159ee3e066SJulian Sax msleep(1000); 7169ee3e066SJulian Sax } while (tries-- > 0 && ret); 7179ee3e066SJulian Sax 7189ee3e066SJulian Sax if (ret) 7199ee3e066SJulian Sax return ret; 7209ee3e066SJulian Sax 7219ee3e066SJulian Sax use_override = i2c_hid_get_dmi_hid_report_desc_override(client->name, 7229ee3e066SJulian Sax &rsize); 7239ee3e066SJulian Sax 7249ee3e066SJulian Sax if (use_override) { 7259ee3e066SJulian Sax rdesc = use_override; 7269ee3e066SJulian Sax i2c_hid_dbg(ihid, "Using a HID report descriptor override\n"); 7279ee3e066SJulian Sax } else { 7289ee3e066SJulian Sax rdesc = kzalloc(rsize, GFP_KERNEL); 7299ee3e066SJulian Sax 7309ee3e066SJulian Sax if (!rdesc) { 7319ee3e066SJulian Sax dbg_hid("couldn't allocate rdesc memory\n"); 7329ee3e066SJulian Sax return -ENOMEM; 7339ee3e066SJulian Sax } 7349ee3e066SJulian Sax 7359ee3e066SJulian Sax i2c_hid_dbg(ihid, "asking HID report descriptor\n"); 7369ee3e066SJulian Sax 7379ee3e066SJulian Sax ret = i2c_hid_command(client, &hid_report_descr_cmd, 7389ee3e066SJulian Sax rdesc, rsize); 7399ee3e066SJulian Sax if (ret) { 7409ee3e066SJulian Sax hid_err(hid, "reading report descriptor failed\n"); 7419ee3e066SJulian Sax kfree(rdesc); 7429ee3e066SJulian Sax return -EIO; 7439ee3e066SJulian Sax } 7449ee3e066SJulian Sax } 7459ee3e066SJulian Sax 7469ee3e066SJulian Sax i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc); 7479ee3e066SJulian Sax 7489ee3e066SJulian Sax ret = hid_parse_report(hid, rdesc, rsize); 7499ee3e066SJulian Sax if (!use_override) 7509ee3e066SJulian Sax kfree(rdesc); 7519ee3e066SJulian Sax 7529ee3e066SJulian Sax if (ret) { 7539ee3e066SJulian Sax dbg_hid("parsing report descriptor failed\n"); 7549ee3e066SJulian Sax return ret; 7559ee3e066SJulian Sax } 7569ee3e066SJulian Sax 7579ee3e066SJulian Sax return 0; 7589ee3e066SJulian Sax } 7599ee3e066SJulian Sax 7609ee3e066SJulian Sax static int i2c_hid_start(struct hid_device *hid) 7619ee3e066SJulian Sax { 7629ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 7639ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 7649ee3e066SJulian Sax int ret; 7659ee3e066SJulian Sax unsigned int bufsize = HID_MIN_BUFFER_SIZE; 7669ee3e066SJulian Sax 7679ee3e066SJulian Sax i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize); 7689ee3e066SJulian Sax i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize); 7699ee3e066SJulian Sax i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize); 7709ee3e066SJulian Sax 7719ee3e066SJulian Sax if (bufsize > ihid->bufsize) { 7729ee3e066SJulian Sax disable_irq(client->irq); 7739ee3e066SJulian Sax i2c_hid_free_buffers(ihid); 7749ee3e066SJulian Sax 7759ee3e066SJulian Sax ret = i2c_hid_alloc_buffers(ihid, bufsize); 7769ee3e066SJulian Sax enable_irq(client->irq); 7779ee3e066SJulian Sax 7789ee3e066SJulian Sax if (ret) 7799ee3e066SJulian Sax return ret; 7809ee3e066SJulian Sax } 7819ee3e066SJulian Sax 7829ee3e066SJulian Sax return 0; 7839ee3e066SJulian Sax } 7849ee3e066SJulian Sax 7859ee3e066SJulian Sax static void i2c_hid_stop(struct hid_device *hid) 7869ee3e066SJulian Sax { 7879ee3e066SJulian Sax hid->claimed = 0; 7889ee3e066SJulian Sax } 7899ee3e066SJulian Sax 7909ee3e066SJulian Sax static int i2c_hid_open(struct hid_device *hid) 7919ee3e066SJulian Sax { 7929ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 7939ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 7949ee3e066SJulian Sax int ret = 0; 7959ee3e066SJulian Sax 7969ee3e066SJulian Sax ret = pm_runtime_get_sync(&client->dev); 7979ee3e066SJulian Sax if (ret < 0) 7989ee3e066SJulian Sax return ret; 7999ee3e066SJulian Sax 8009ee3e066SJulian Sax set_bit(I2C_HID_STARTED, &ihid->flags); 8019ee3e066SJulian Sax return 0; 8029ee3e066SJulian Sax } 8039ee3e066SJulian Sax 8049ee3e066SJulian Sax static void i2c_hid_close(struct hid_device *hid) 8059ee3e066SJulian Sax { 8069ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 8079ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 8089ee3e066SJulian Sax 8099ee3e066SJulian Sax clear_bit(I2C_HID_STARTED, &ihid->flags); 8109ee3e066SJulian Sax 8119ee3e066SJulian Sax /* Save some power */ 8129ee3e066SJulian Sax pm_runtime_put(&client->dev); 8139ee3e066SJulian Sax } 8149ee3e066SJulian Sax 8159ee3e066SJulian Sax static int i2c_hid_power(struct hid_device *hid, int lvl) 8169ee3e066SJulian Sax { 8179ee3e066SJulian Sax struct i2c_client *client = hid->driver_data; 8189ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 8199ee3e066SJulian Sax 8209ee3e066SJulian Sax i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl); 8219ee3e066SJulian Sax 8229ee3e066SJulian Sax switch (lvl) { 8239ee3e066SJulian Sax case PM_HINT_FULLON: 8249ee3e066SJulian Sax pm_runtime_get_sync(&client->dev); 8259ee3e066SJulian Sax break; 8269ee3e066SJulian Sax case PM_HINT_NORMAL: 8279ee3e066SJulian Sax pm_runtime_put(&client->dev); 8289ee3e066SJulian Sax break; 8299ee3e066SJulian Sax } 8309ee3e066SJulian Sax return 0; 8319ee3e066SJulian Sax } 8329ee3e066SJulian Sax 8339ee3e066SJulian Sax struct hid_ll_driver i2c_hid_ll_driver = { 8349ee3e066SJulian Sax .parse = i2c_hid_parse, 8359ee3e066SJulian Sax .start = i2c_hid_start, 8369ee3e066SJulian Sax .stop = i2c_hid_stop, 8379ee3e066SJulian Sax .open = i2c_hid_open, 8389ee3e066SJulian Sax .close = i2c_hid_close, 8399ee3e066SJulian Sax .power = i2c_hid_power, 8409ee3e066SJulian Sax .output_report = i2c_hid_output_report, 8419ee3e066SJulian Sax .raw_request = i2c_hid_raw_request, 8429ee3e066SJulian Sax }; 8439ee3e066SJulian Sax EXPORT_SYMBOL_GPL(i2c_hid_ll_driver); 8449ee3e066SJulian Sax 8459ee3e066SJulian Sax static int i2c_hid_init_irq(struct i2c_client *client) 8469ee3e066SJulian Sax { 8479ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 8489ee3e066SJulian Sax unsigned long irqflags = 0; 8499ee3e066SJulian Sax int ret; 8509ee3e066SJulian Sax 8519ee3e066SJulian Sax dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq); 8529ee3e066SJulian Sax 8539ee3e066SJulian Sax if (!irq_get_trigger_type(client->irq)) 8549ee3e066SJulian Sax irqflags = IRQF_TRIGGER_LOW; 8559ee3e066SJulian Sax 8569ee3e066SJulian Sax ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq, 8579ee3e066SJulian Sax irqflags | IRQF_ONESHOT, client->name, ihid); 8589ee3e066SJulian Sax if (ret < 0) { 8599ee3e066SJulian Sax dev_warn(&client->dev, 8609ee3e066SJulian Sax "Could not register for %s interrupt, irq = %d," 8619ee3e066SJulian Sax " ret = %d\n", 8629ee3e066SJulian Sax client->name, client->irq, ret); 8639ee3e066SJulian Sax 8649ee3e066SJulian Sax return ret; 8659ee3e066SJulian Sax } 8669ee3e066SJulian Sax 8679ee3e066SJulian Sax return 0; 8689ee3e066SJulian Sax } 8699ee3e066SJulian Sax 8709ee3e066SJulian Sax static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid) 8719ee3e066SJulian Sax { 8729ee3e066SJulian Sax struct i2c_client *client = ihid->client; 8739ee3e066SJulian Sax struct i2c_hid_desc *hdesc = &ihid->hdesc; 8749ee3e066SJulian Sax unsigned int dsize; 8759ee3e066SJulian Sax int ret; 8769ee3e066SJulian Sax 8779ee3e066SJulian Sax /* i2c hid fetch using a fixed descriptor size (30 bytes) */ 8789ee3e066SJulian Sax if (i2c_hid_get_dmi_i2c_hid_desc_override(client->name)) { 8799ee3e066SJulian Sax i2c_hid_dbg(ihid, "Using a HID descriptor override\n"); 8809ee3e066SJulian Sax ihid->hdesc = 8819ee3e066SJulian Sax *i2c_hid_get_dmi_i2c_hid_desc_override(client->name); 8829ee3e066SJulian Sax } else { 8839ee3e066SJulian Sax i2c_hid_dbg(ihid, "Fetching the HID descriptor\n"); 8849ee3e066SJulian Sax ret = i2c_hid_command(client, &hid_descr_cmd, 8859ee3e066SJulian Sax ihid->hdesc_buffer, 8869ee3e066SJulian Sax sizeof(struct i2c_hid_desc)); 8879ee3e066SJulian Sax if (ret) { 8889ee3e066SJulian Sax dev_err(&client->dev, "hid_descr_cmd failed\n"); 8899ee3e066SJulian Sax return -ENODEV; 8909ee3e066SJulian Sax } 8919ee3e066SJulian Sax } 8929ee3e066SJulian Sax 8939ee3e066SJulian Sax /* Validate the length of HID descriptor, the 4 first bytes: 8949ee3e066SJulian Sax * bytes 0-1 -> length 8959ee3e066SJulian Sax * bytes 2-3 -> bcdVersion (has to be 1.00) */ 8969ee3e066SJulian Sax /* check bcdVersion == 1.0 */ 8979ee3e066SJulian Sax if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) { 8989ee3e066SJulian Sax dev_err(&client->dev, 8999ee3e066SJulian Sax "unexpected HID descriptor bcdVersion (0x%04hx)\n", 9009ee3e066SJulian Sax le16_to_cpu(hdesc->bcdVersion)); 9019ee3e066SJulian Sax return -ENODEV; 9029ee3e066SJulian Sax } 9039ee3e066SJulian Sax 9049ee3e066SJulian Sax /* Descriptor length should be 30 bytes as per the specification */ 9059ee3e066SJulian Sax dsize = le16_to_cpu(hdesc->wHIDDescLength); 9069ee3e066SJulian Sax if (dsize != sizeof(struct i2c_hid_desc)) { 9079ee3e066SJulian Sax dev_err(&client->dev, "weird size of HID descriptor (%u)\n", 9089ee3e066SJulian Sax dsize); 9099ee3e066SJulian Sax return -ENODEV; 9109ee3e066SJulian Sax } 9119ee3e066SJulian Sax i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer); 9129ee3e066SJulian Sax return 0; 9139ee3e066SJulian Sax } 9149ee3e066SJulian Sax 9159ee3e066SJulian Sax #ifdef CONFIG_ACPI 9169ee3e066SJulian Sax static const struct acpi_device_id i2c_hid_acpi_blacklist[] = { 9179ee3e066SJulian Sax /* 9189ee3e066SJulian Sax * The CHPN0001 ACPI device, which is used to describe the Chipone 9199ee3e066SJulian Sax * ICN8505 controller, has a _CID of PNP0C50 but is not HID compatible. 9209ee3e066SJulian Sax */ 9219ee3e066SJulian Sax {"CHPN0001", 0 }, 9229ee3e066SJulian Sax { }, 9239ee3e066SJulian Sax }; 9249ee3e066SJulian Sax 9259ee3e066SJulian Sax static int i2c_hid_acpi_pdata(struct i2c_client *client, 9269ee3e066SJulian Sax struct i2c_hid_platform_data *pdata) 9279ee3e066SJulian Sax { 9289ee3e066SJulian Sax static guid_t i2c_hid_guid = 9299ee3e066SJulian Sax GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555, 9309ee3e066SJulian Sax 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE); 9319ee3e066SJulian Sax union acpi_object *obj; 9329ee3e066SJulian Sax struct acpi_device *adev; 9339ee3e066SJulian Sax acpi_handle handle; 9349ee3e066SJulian Sax 9359ee3e066SJulian Sax handle = ACPI_HANDLE(&client->dev); 9369ee3e066SJulian Sax if (!handle || acpi_bus_get_device(handle, &adev)) { 9379ee3e066SJulian Sax dev_err(&client->dev, "Error could not get ACPI device\n"); 9389ee3e066SJulian Sax return -ENODEV; 9399ee3e066SJulian Sax } 9409ee3e066SJulian Sax 9419ee3e066SJulian Sax if (acpi_match_device_ids(adev, i2c_hid_acpi_blacklist) == 0) 9429ee3e066SJulian Sax return -ENODEV; 9439ee3e066SJulian Sax 9449ee3e066SJulian Sax obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL, 9459ee3e066SJulian Sax ACPI_TYPE_INTEGER); 9469ee3e066SJulian Sax if (!obj) { 9479ee3e066SJulian Sax dev_err(&client->dev, "Error _DSM call to get HID descriptor address failed\n"); 9489ee3e066SJulian Sax return -ENODEV; 9499ee3e066SJulian Sax } 9509ee3e066SJulian Sax 9519ee3e066SJulian Sax pdata->hid_descriptor_address = obj->integer.value; 9529ee3e066SJulian Sax ACPI_FREE(obj); 9539ee3e066SJulian Sax 9549ee3e066SJulian Sax return 0; 9559ee3e066SJulian Sax } 9569ee3e066SJulian Sax 9579ee3e066SJulian Sax static void i2c_hid_acpi_fix_up_power(struct device *dev) 9589ee3e066SJulian Sax { 9599ee3e066SJulian Sax struct acpi_device *adev; 9609ee3e066SJulian Sax 9619ee3e066SJulian Sax adev = ACPI_COMPANION(dev); 9629ee3e066SJulian Sax if (adev) 9639ee3e066SJulian Sax acpi_device_fix_up_power(adev); 9649ee3e066SJulian Sax } 9659ee3e066SJulian Sax 9669ee3e066SJulian Sax static const struct acpi_device_id i2c_hid_acpi_match[] = { 9679ee3e066SJulian Sax {"ACPI0C50", 0 }, 9689ee3e066SJulian Sax {"PNP0C50", 0 }, 9699ee3e066SJulian Sax { }, 9709ee3e066SJulian Sax }; 9719ee3e066SJulian Sax MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match); 9729ee3e066SJulian Sax #else 9739ee3e066SJulian Sax static inline int i2c_hid_acpi_pdata(struct i2c_client *client, 9749ee3e066SJulian Sax struct i2c_hid_platform_data *pdata) 9759ee3e066SJulian Sax { 9769ee3e066SJulian Sax return -ENODEV; 9779ee3e066SJulian Sax } 9789ee3e066SJulian Sax 9799ee3e066SJulian Sax static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {} 9809ee3e066SJulian Sax #endif 9819ee3e066SJulian Sax 9829ee3e066SJulian Sax #ifdef CONFIG_OF 9839ee3e066SJulian Sax static int i2c_hid_of_probe(struct i2c_client *client, 9849ee3e066SJulian Sax struct i2c_hid_platform_data *pdata) 9859ee3e066SJulian Sax { 9869ee3e066SJulian Sax struct device *dev = &client->dev; 9879ee3e066SJulian Sax u32 val; 9889ee3e066SJulian Sax int ret; 9899ee3e066SJulian Sax 9909ee3e066SJulian Sax ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val); 9919ee3e066SJulian Sax if (ret) { 9929ee3e066SJulian Sax dev_err(&client->dev, "HID register address not provided\n"); 9939ee3e066SJulian Sax return -ENODEV; 9949ee3e066SJulian Sax } 9959ee3e066SJulian Sax if (val >> 16) { 9969ee3e066SJulian Sax dev_err(&client->dev, "Bad HID register address: 0x%08x\n", 9979ee3e066SJulian Sax val); 9989ee3e066SJulian Sax return -EINVAL; 9999ee3e066SJulian Sax } 10009ee3e066SJulian Sax pdata->hid_descriptor_address = val; 10019ee3e066SJulian Sax 10029ee3e066SJulian Sax return 0; 10039ee3e066SJulian Sax } 10049ee3e066SJulian Sax 10059ee3e066SJulian Sax static const struct of_device_id i2c_hid_of_match[] = { 10069ee3e066SJulian Sax { .compatible = "hid-over-i2c" }, 10079ee3e066SJulian Sax {}, 10089ee3e066SJulian Sax }; 10099ee3e066SJulian Sax MODULE_DEVICE_TABLE(of, i2c_hid_of_match); 10109ee3e066SJulian Sax #else 10119ee3e066SJulian Sax static inline int i2c_hid_of_probe(struct i2c_client *client, 10129ee3e066SJulian Sax struct i2c_hid_platform_data *pdata) 10139ee3e066SJulian Sax { 10149ee3e066SJulian Sax return -ENODEV; 10159ee3e066SJulian Sax } 10169ee3e066SJulian Sax #endif 10179ee3e066SJulian Sax 10189ee3e066SJulian Sax static void i2c_hid_fwnode_probe(struct i2c_client *client, 10199ee3e066SJulian Sax struct i2c_hid_platform_data *pdata) 10209ee3e066SJulian Sax { 10219ee3e066SJulian Sax u32 val; 10229ee3e066SJulian Sax 10239ee3e066SJulian Sax if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms", 10249ee3e066SJulian Sax &val)) 10259ee3e066SJulian Sax pdata->post_power_delay_ms = val; 10269ee3e066SJulian Sax } 10279ee3e066SJulian Sax 10289ee3e066SJulian Sax static int i2c_hid_probe(struct i2c_client *client, 10299ee3e066SJulian Sax const struct i2c_device_id *dev_id) 10309ee3e066SJulian Sax { 10319ee3e066SJulian Sax int ret; 10329ee3e066SJulian Sax struct i2c_hid *ihid; 10339ee3e066SJulian Sax struct hid_device *hid; 10349ee3e066SJulian Sax __u16 hidRegister; 10359ee3e066SJulian Sax struct i2c_hid_platform_data *platform_data = client->dev.platform_data; 10369ee3e066SJulian Sax 10379ee3e066SJulian Sax dbg_hid("HID probe called for i2c 0x%02x\n", client->addr); 10389ee3e066SJulian Sax 10399ee3e066SJulian Sax if (!client->irq) { 10409ee3e066SJulian Sax dev_err(&client->dev, 10419ee3e066SJulian Sax "HID over i2c has not been provided an Int IRQ\n"); 10429ee3e066SJulian Sax return -EINVAL; 10439ee3e066SJulian Sax } 10449ee3e066SJulian Sax 10459ee3e066SJulian Sax if (client->irq < 0) { 10469ee3e066SJulian Sax if (client->irq != -EPROBE_DEFER) 10479ee3e066SJulian Sax dev_err(&client->dev, 10489ee3e066SJulian Sax "HID over i2c doesn't have a valid IRQ\n"); 10499ee3e066SJulian Sax return client->irq; 10509ee3e066SJulian Sax } 10519ee3e066SJulian Sax 10529ee3e066SJulian Sax ihid = devm_kzalloc(&client->dev, sizeof(*ihid), GFP_KERNEL); 10539ee3e066SJulian Sax if (!ihid) 10549ee3e066SJulian Sax return -ENOMEM; 10559ee3e066SJulian Sax 10569ee3e066SJulian Sax if (client->dev.of_node) { 10579ee3e066SJulian Sax ret = i2c_hid_of_probe(client, &ihid->pdata); 10589ee3e066SJulian Sax if (ret) 10599ee3e066SJulian Sax return ret; 10609ee3e066SJulian Sax } else if (!platform_data) { 10619ee3e066SJulian Sax ret = i2c_hid_acpi_pdata(client, &ihid->pdata); 10629ee3e066SJulian Sax if (ret) 10639ee3e066SJulian Sax return ret; 10649ee3e066SJulian Sax } else { 10659ee3e066SJulian Sax ihid->pdata = *platform_data; 10669ee3e066SJulian Sax } 10679ee3e066SJulian Sax 10689ee3e066SJulian Sax /* Parse platform agnostic common properties from ACPI / device tree */ 10699ee3e066SJulian Sax i2c_hid_fwnode_probe(client, &ihid->pdata); 10709ee3e066SJulian Sax 10719ee3e066SJulian Sax ihid->pdata.supplies[0].supply = "vdd"; 10729ee3e066SJulian Sax ihid->pdata.supplies[1].supply = "vddl"; 10739ee3e066SJulian Sax 10749ee3e066SJulian Sax ret = devm_regulator_bulk_get(&client->dev, 10759ee3e066SJulian Sax ARRAY_SIZE(ihid->pdata.supplies), 10769ee3e066SJulian Sax ihid->pdata.supplies); 10779ee3e066SJulian Sax if (ret) 10789ee3e066SJulian Sax return ret; 10799ee3e066SJulian Sax 10809ee3e066SJulian Sax ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies), 10819ee3e066SJulian Sax ihid->pdata.supplies); 10829ee3e066SJulian Sax if (ret < 0) 10839ee3e066SJulian Sax return ret; 10849ee3e066SJulian Sax 10859ee3e066SJulian Sax if (ihid->pdata.post_power_delay_ms) 10869ee3e066SJulian Sax msleep(ihid->pdata.post_power_delay_ms); 10879ee3e066SJulian Sax 10889ee3e066SJulian Sax i2c_set_clientdata(client, ihid); 10899ee3e066SJulian Sax 10909ee3e066SJulian Sax ihid->client = client; 10919ee3e066SJulian Sax 10929ee3e066SJulian Sax hidRegister = ihid->pdata.hid_descriptor_address; 10939ee3e066SJulian Sax ihid->wHIDDescRegister = cpu_to_le16(hidRegister); 10949ee3e066SJulian Sax 10959ee3e066SJulian Sax init_waitqueue_head(&ihid->wait); 10969ee3e066SJulian Sax mutex_init(&ihid->reset_lock); 10979ee3e066SJulian Sax 10989ee3e066SJulian Sax /* we need to allocate the command buffer without knowing the maximum 10999ee3e066SJulian Sax * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the 11009ee3e066SJulian Sax * real computation later. */ 11019ee3e066SJulian Sax ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE); 11029ee3e066SJulian Sax if (ret < 0) 11039ee3e066SJulian Sax goto err_regulator; 11049ee3e066SJulian Sax 11059ee3e066SJulian Sax i2c_hid_acpi_fix_up_power(&client->dev); 11069ee3e066SJulian Sax 11079ee3e066SJulian Sax pm_runtime_get_noresume(&client->dev); 11089ee3e066SJulian Sax pm_runtime_set_active(&client->dev); 11099ee3e066SJulian Sax pm_runtime_enable(&client->dev); 11109ee3e066SJulian Sax device_enable_async_suspend(&client->dev); 11119ee3e066SJulian Sax 11129ee3e066SJulian Sax /* Make sure there is something at this address */ 11139ee3e066SJulian Sax ret = i2c_smbus_read_byte(client); 11149ee3e066SJulian Sax if (ret < 0) { 11159ee3e066SJulian Sax dev_dbg(&client->dev, "nothing at this address: %d\n", ret); 11169ee3e066SJulian Sax ret = -ENXIO; 11179ee3e066SJulian Sax goto err_pm; 11189ee3e066SJulian Sax } 11199ee3e066SJulian Sax 11209ee3e066SJulian Sax ret = i2c_hid_fetch_hid_descriptor(ihid); 11219ee3e066SJulian Sax if (ret < 0) 11229ee3e066SJulian Sax goto err_pm; 11239ee3e066SJulian Sax 11249ee3e066SJulian Sax ret = i2c_hid_init_irq(client); 11259ee3e066SJulian Sax if (ret < 0) 11269ee3e066SJulian Sax goto err_pm; 11279ee3e066SJulian Sax 11289ee3e066SJulian Sax hid = hid_allocate_device(); 11299ee3e066SJulian Sax if (IS_ERR(hid)) { 11309ee3e066SJulian Sax ret = PTR_ERR(hid); 11319ee3e066SJulian Sax goto err_irq; 11329ee3e066SJulian Sax } 11339ee3e066SJulian Sax 11349ee3e066SJulian Sax ihid->hid = hid; 11359ee3e066SJulian Sax 11369ee3e066SJulian Sax hid->driver_data = client; 11379ee3e066SJulian Sax hid->ll_driver = &i2c_hid_ll_driver; 11389ee3e066SJulian Sax hid->dev.parent = &client->dev; 11399ee3e066SJulian Sax hid->bus = BUS_I2C; 11409ee3e066SJulian Sax hid->version = le16_to_cpu(ihid->hdesc.bcdVersion); 11419ee3e066SJulian Sax hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID); 11429ee3e066SJulian Sax hid->product = le16_to_cpu(ihid->hdesc.wProductID); 11439ee3e066SJulian Sax 11449ee3e066SJulian Sax snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX", 11459ee3e066SJulian Sax client->name, hid->vendor, hid->product); 11469ee3e066SJulian Sax strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys)); 11479ee3e066SJulian Sax 11489ee3e066SJulian Sax ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product); 11499ee3e066SJulian Sax 11509ee3e066SJulian Sax ret = hid_add_device(hid); 11519ee3e066SJulian Sax if (ret) { 11529ee3e066SJulian Sax if (ret != -ENODEV) 11539ee3e066SJulian Sax hid_err(client, "can't add hid device: %d\n", ret); 11549ee3e066SJulian Sax goto err_mem_free; 11559ee3e066SJulian Sax } 11569ee3e066SJulian Sax 11574e7be68eSJiri Kosina if (!(ihid->quirks & I2C_HID_QUIRK_NO_RUNTIME_PM)) 11589ee3e066SJulian Sax pm_runtime_put(&client->dev); 11594e7be68eSJiri Kosina 11609ee3e066SJulian Sax return 0; 11619ee3e066SJulian Sax 11629ee3e066SJulian Sax err_mem_free: 11639ee3e066SJulian Sax hid_destroy_device(hid); 11649ee3e066SJulian Sax 11659ee3e066SJulian Sax err_irq: 11669ee3e066SJulian Sax free_irq(client->irq, ihid); 11679ee3e066SJulian Sax 11689ee3e066SJulian Sax err_pm: 11699ee3e066SJulian Sax pm_runtime_put_noidle(&client->dev); 11709ee3e066SJulian Sax pm_runtime_disable(&client->dev); 11719ee3e066SJulian Sax 11729ee3e066SJulian Sax err_regulator: 11739ee3e066SJulian Sax regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies), 11749ee3e066SJulian Sax ihid->pdata.supplies); 11759ee3e066SJulian Sax i2c_hid_free_buffers(ihid); 11769ee3e066SJulian Sax return ret; 11779ee3e066SJulian Sax } 11789ee3e066SJulian Sax 11799ee3e066SJulian Sax static int i2c_hid_remove(struct i2c_client *client) 11809ee3e066SJulian Sax { 11819ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 11829ee3e066SJulian Sax struct hid_device *hid; 11839ee3e066SJulian Sax 11844e7be68eSJiri Kosina if (!(ihid->quirks & I2C_HID_QUIRK_NO_RUNTIME_PM)) 11859ee3e066SJulian Sax pm_runtime_get_sync(&client->dev); 11869ee3e066SJulian Sax pm_runtime_disable(&client->dev); 11879ee3e066SJulian Sax pm_runtime_set_suspended(&client->dev); 11889ee3e066SJulian Sax pm_runtime_put_noidle(&client->dev); 11899ee3e066SJulian Sax 11909ee3e066SJulian Sax hid = ihid->hid; 11919ee3e066SJulian Sax hid_destroy_device(hid); 11929ee3e066SJulian Sax 11939ee3e066SJulian Sax free_irq(client->irq, ihid); 11949ee3e066SJulian Sax 11959ee3e066SJulian Sax if (ihid->bufsize) 11969ee3e066SJulian Sax i2c_hid_free_buffers(ihid); 11979ee3e066SJulian Sax 11989ee3e066SJulian Sax regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies), 11999ee3e066SJulian Sax ihid->pdata.supplies); 12009ee3e066SJulian Sax 12019ee3e066SJulian Sax return 0; 12029ee3e066SJulian Sax } 12039ee3e066SJulian Sax 12049ee3e066SJulian Sax static void i2c_hid_shutdown(struct i2c_client *client) 12059ee3e066SJulian Sax { 12069ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 12079ee3e066SJulian Sax 12089ee3e066SJulian Sax i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); 12099ee3e066SJulian Sax free_irq(client->irq, ihid); 12109ee3e066SJulian Sax } 12119ee3e066SJulian Sax 12129ee3e066SJulian Sax #ifdef CONFIG_PM_SLEEP 12139ee3e066SJulian Sax static int i2c_hid_suspend(struct device *dev) 12149ee3e066SJulian Sax { 12159ee3e066SJulian Sax struct i2c_client *client = to_i2c_client(dev); 12169ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 12179ee3e066SJulian Sax struct hid_device *hid = ihid->hid; 12189ee3e066SJulian Sax int ret; 12199ee3e066SJulian Sax int wake_status; 12209ee3e066SJulian Sax 12219ee3e066SJulian Sax if (hid->driver && hid->driver->suspend) { 12229ee3e066SJulian Sax /* 12239ee3e066SJulian Sax * Wake up the device so that IO issues in 12249ee3e066SJulian Sax * HID driver's suspend code can succeed. 12259ee3e066SJulian Sax */ 12269ee3e066SJulian Sax ret = pm_runtime_resume(dev); 12279ee3e066SJulian Sax if (ret < 0) 12289ee3e066SJulian Sax return ret; 12299ee3e066SJulian Sax 12309ee3e066SJulian Sax ret = hid->driver->suspend(hid, PMSG_SUSPEND); 12319ee3e066SJulian Sax if (ret < 0) 12329ee3e066SJulian Sax return ret; 12339ee3e066SJulian Sax } 12349ee3e066SJulian Sax 12359ee3e066SJulian Sax if (!pm_runtime_suspended(dev)) { 12369ee3e066SJulian Sax /* Save some power */ 12379ee3e066SJulian Sax i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); 12389ee3e066SJulian Sax 12399ee3e066SJulian Sax disable_irq(client->irq); 12409ee3e066SJulian Sax } 12419ee3e066SJulian Sax 12429ee3e066SJulian Sax if (device_may_wakeup(&client->dev)) { 12439ee3e066SJulian Sax wake_status = enable_irq_wake(client->irq); 12449ee3e066SJulian Sax if (!wake_status) 12459ee3e066SJulian Sax ihid->irq_wake_enabled = true; 12469ee3e066SJulian Sax else 12479ee3e066SJulian Sax hid_warn(hid, "Failed to enable irq wake: %d\n", 12489ee3e066SJulian Sax wake_status); 12499ee3e066SJulian Sax } else { 12509ee3e066SJulian Sax regulator_bulk_disable(ARRAY_SIZE(ihid->pdata.supplies), 12519ee3e066SJulian Sax ihid->pdata.supplies); 12529ee3e066SJulian Sax } 12539ee3e066SJulian Sax 12549ee3e066SJulian Sax return 0; 12559ee3e066SJulian Sax } 12569ee3e066SJulian Sax 12579ee3e066SJulian Sax static int i2c_hid_resume(struct device *dev) 12589ee3e066SJulian Sax { 12599ee3e066SJulian Sax int ret; 12609ee3e066SJulian Sax struct i2c_client *client = to_i2c_client(dev); 12619ee3e066SJulian Sax struct i2c_hid *ihid = i2c_get_clientdata(client); 12629ee3e066SJulian Sax struct hid_device *hid = ihid->hid; 12639ee3e066SJulian Sax int wake_status; 12649ee3e066SJulian Sax 12659ee3e066SJulian Sax if (!device_may_wakeup(&client->dev)) { 12669ee3e066SJulian Sax ret = regulator_bulk_enable(ARRAY_SIZE(ihid->pdata.supplies), 12679ee3e066SJulian Sax ihid->pdata.supplies); 12689ee3e066SJulian Sax if (ret) 12699ee3e066SJulian Sax hid_warn(hid, "Failed to enable supplies: %d\n", ret); 12709ee3e066SJulian Sax 12719ee3e066SJulian Sax if (ihid->pdata.post_power_delay_ms) 12729ee3e066SJulian Sax msleep(ihid->pdata.post_power_delay_ms); 12739ee3e066SJulian Sax } else if (ihid->irq_wake_enabled) { 12749ee3e066SJulian Sax wake_status = disable_irq_wake(client->irq); 12759ee3e066SJulian Sax if (!wake_status) 12769ee3e066SJulian Sax ihid->irq_wake_enabled = false; 12779ee3e066SJulian Sax else 12789ee3e066SJulian Sax hid_warn(hid, "Failed to disable irq wake: %d\n", 12799ee3e066SJulian Sax wake_status); 12809ee3e066SJulian Sax } 12819ee3e066SJulian Sax 12829ee3e066SJulian Sax /* We'll resume to full power */ 12839ee3e066SJulian Sax pm_runtime_disable(dev); 12849ee3e066SJulian Sax pm_runtime_set_active(dev); 12859ee3e066SJulian Sax pm_runtime_enable(dev); 12869ee3e066SJulian Sax 12879ee3e066SJulian Sax enable_irq(client->irq); 12889ee3e066SJulian Sax 12899ee3e066SJulian Sax /* Instead of resetting device, simply powers the device on. This 12909ee3e066SJulian Sax * solves "incomplete reports" on Raydium devices 2386:3118 and 12914e7be68eSJiri Kosina * 2386:4B33 and fixes various SIS touchscreens no longer sending 12924e7be68eSJiri Kosina * data after a suspend/resume. 12939ee3e066SJulian Sax */ 12949ee3e066SJulian Sax ret = i2c_hid_set_power(client, I2C_HID_PWR_ON); 12959ee3e066SJulian Sax if (ret) 12969ee3e066SJulian Sax return ret; 12979ee3e066SJulian Sax 12989ee3e066SJulian Sax if (hid->driver && hid->driver->reset_resume) { 12999ee3e066SJulian Sax ret = hid->driver->reset_resume(hid); 13009ee3e066SJulian Sax return ret; 13019ee3e066SJulian Sax } 13029ee3e066SJulian Sax 13039ee3e066SJulian Sax return 0; 13049ee3e066SJulian Sax } 13059ee3e066SJulian Sax #endif 13069ee3e066SJulian Sax 13079ee3e066SJulian Sax #ifdef CONFIG_PM 13089ee3e066SJulian Sax static int i2c_hid_runtime_suspend(struct device *dev) 13099ee3e066SJulian Sax { 13109ee3e066SJulian Sax struct i2c_client *client = to_i2c_client(dev); 13119ee3e066SJulian Sax 13129ee3e066SJulian Sax i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); 13139ee3e066SJulian Sax disable_irq(client->irq); 13149ee3e066SJulian Sax return 0; 13159ee3e066SJulian Sax } 13169ee3e066SJulian Sax 13179ee3e066SJulian Sax static int i2c_hid_runtime_resume(struct device *dev) 13189ee3e066SJulian Sax { 13199ee3e066SJulian Sax struct i2c_client *client = to_i2c_client(dev); 13209ee3e066SJulian Sax 13219ee3e066SJulian Sax enable_irq(client->irq); 13229ee3e066SJulian Sax i2c_hid_set_power(client, I2C_HID_PWR_ON); 13239ee3e066SJulian Sax return 0; 13249ee3e066SJulian Sax } 13259ee3e066SJulian Sax #endif 13269ee3e066SJulian Sax 13279ee3e066SJulian Sax static const struct dev_pm_ops i2c_hid_pm = { 13289ee3e066SJulian Sax SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume) 13299ee3e066SJulian Sax SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume, 13309ee3e066SJulian Sax NULL) 13319ee3e066SJulian Sax }; 13329ee3e066SJulian Sax 13339ee3e066SJulian Sax static const struct i2c_device_id i2c_hid_id_table[] = { 13349ee3e066SJulian Sax { "hid", 0 }, 13359ee3e066SJulian Sax { "hid-over-i2c", 0 }, 13369ee3e066SJulian Sax { }, 13379ee3e066SJulian Sax }; 13389ee3e066SJulian Sax MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table); 13399ee3e066SJulian Sax 13409ee3e066SJulian Sax 13419ee3e066SJulian Sax static struct i2c_driver i2c_hid_driver = { 13429ee3e066SJulian Sax .driver = { 13439ee3e066SJulian Sax .name = "i2c_hid", 13449ee3e066SJulian Sax .pm = &i2c_hid_pm, 13459ee3e066SJulian Sax .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match), 13469ee3e066SJulian Sax .of_match_table = of_match_ptr(i2c_hid_of_match), 13479ee3e066SJulian Sax }, 13489ee3e066SJulian Sax 13499ee3e066SJulian Sax .probe = i2c_hid_probe, 13509ee3e066SJulian Sax .remove = i2c_hid_remove, 13519ee3e066SJulian Sax .shutdown = i2c_hid_shutdown, 13529ee3e066SJulian Sax .id_table = i2c_hid_id_table, 13539ee3e066SJulian Sax }; 13549ee3e066SJulian Sax 13559ee3e066SJulian Sax module_i2c_driver(i2c_hid_driver); 13569ee3e066SJulian Sax 13579ee3e066SJulian Sax MODULE_DESCRIPTION("HID over I2C core driver"); 13589ee3e066SJulian Sax MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); 13599ee3e066SJulian Sax MODULE_LICENSE("GPL"); 1360