Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * HID over I2C protocol implementation |
| 3 | * |
| 4 | * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com> |
| 5 | * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France |
| 6 | * Copyright (c) 2012 Red Hat, Inc |
| 7 | * |
| 8 | * This code is partly based on "USB HID support for Linux": |
| 9 | * |
| 10 | * Copyright (c) 1999 Andreas Gal |
| 11 | * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz> |
| 12 | * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc |
| 13 | * Copyright (c) 2007-2008 Oliver Neukum |
| 14 | * Copyright (c) 2006-2010 Jiri Kosina |
| 15 | * |
| 16 | * This file is subject to the terms and conditions of the GNU General Public |
| 17 | * License. See the file COPYING in the main directory of this archive for |
| 18 | * more details. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/i2c.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/input.h> |
Benjamin Tissoires | 00f7fea | 2016-11-21 15:21:27 +0100 | [diff] [blame] | 25 | #include <linux/irq.h> |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 26 | #include <linux/delay.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/pm.h> |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 29 | #include <linux/pm_runtime.h> |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 30 | #include <linux/device.h> |
| 31 | #include <linux/wait.h> |
| 32 | #include <linux/err.h> |
| 33 | #include <linux/string.h> |
| 34 | #include <linux/list.h> |
| 35 | #include <linux/jiffies.h> |
| 36 | #include <linux/kernel.h> |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 37 | #include <linux/hid.h> |
Benjamin Tissoires | 7a7d6d9 | 2012-12-12 18:00:02 +0100 | [diff] [blame] | 38 | #include <linux/mutex.h> |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 39 | #include <linux/acpi.h> |
Benjamin Tissoires | 3d7d248 | 2013-06-13 09:50:35 +0200 | [diff] [blame] | 40 | #include <linux/of.h> |
Jiri Kosina | ba1660f | 2016-12-09 13:37:07 +0100 | [diff] [blame] | 41 | #include <linux/regulator/consumer.h> |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 42 | |
Wolfram Sang | 91b9ae4 | 2017-05-21 22:30:33 +0200 | [diff] [blame] | 43 | #include <linux/platform_data/i2c-hid.h> |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 44 | |
HungNien Chen | 71af01a | 2016-11-10 11:47:13 +0800 | [diff] [blame] | 45 | #include "../hid-ids.h" |
| 46 | |
| 47 | /* quirks to control the device */ |
| 48 | #define I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV BIT(0) |
Hans de Goede | 402946a | 2017-11-07 13:28:00 +0100 | [diff] [blame] | 49 | #define I2C_HID_QUIRK_NO_IRQ_AFTER_RESET BIT(1) |
Aaron Ma | 3e83eda | 2018-04-09 15:41:31 +0800 | [diff] [blame] | 50 | #define I2C_HID_QUIRK_RESEND_REPORT_DESCR BIT(2) |
HungNien Chen | 71af01a | 2016-11-10 11:47:13 +0800 | [diff] [blame] | 51 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 52 | /* flags */ |
Dan Carpenter | c8fd51d | 2015-05-14 11:33:30 +0300 | [diff] [blame] | 53 | #define I2C_HID_STARTED 0 |
| 54 | #define I2C_HID_RESET_PENDING 1 |
| 55 | #define I2C_HID_READ_PENDING 2 |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 56 | |
| 57 | #define I2C_HID_PWR_ON 0x00 |
| 58 | #define I2C_HID_PWR_SLEEP 0x01 |
| 59 | |
| 60 | /* debug option */ |
Benjamin Tissoires | ee8e880 | 2012-12-04 16:27:45 +0100 | [diff] [blame] | 61 | static bool debug; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 62 | module_param(debug, bool, 0444); |
| 63 | MODULE_PARM_DESC(debug, "print a lot of debug information"); |
| 64 | |
Benjamin Tissoires | fa738644 | 2012-12-04 16:27:46 +0100 | [diff] [blame] | 65 | #define i2c_hid_dbg(ihid, fmt, arg...) \ |
| 66 | do { \ |
| 67 | if (debug) \ |
| 68 | dev_printk(KERN_DEBUG, &(ihid)->client->dev, fmt, ##arg); \ |
| 69 | } while (0) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 70 | |
| 71 | struct i2c_hid_desc { |
| 72 | __le16 wHIDDescLength; |
| 73 | __le16 bcdVersion; |
| 74 | __le16 wReportDescLength; |
| 75 | __le16 wReportDescRegister; |
| 76 | __le16 wInputRegister; |
| 77 | __le16 wMaxInputLength; |
| 78 | __le16 wOutputRegister; |
| 79 | __le16 wMaxOutputLength; |
| 80 | __le16 wCommandRegister; |
| 81 | __le16 wDataRegister; |
| 82 | __le16 wVendorID; |
| 83 | __le16 wProductID; |
| 84 | __le16 wVersionID; |
Benjamin Tissoires | 27174cf | 2012-12-05 15:02:53 +0100 | [diff] [blame] | 85 | __le32 reserved; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 86 | } __packed; |
| 87 | |
| 88 | struct i2c_hid_cmd { |
| 89 | unsigned int registerIndex; |
| 90 | __u8 opcode; |
| 91 | unsigned int length; |
| 92 | bool wait; |
| 93 | }; |
| 94 | |
| 95 | union command { |
| 96 | u8 data[0]; |
| 97 | struct cmd { |
| 98 | __le16 reg; |
| 99 | __u8 reportTypeID; |
| 100 | __u8 opcode; |
| 101 | } __packed c; |
| 102 | }; |
| 103 | |
| 104 | #define I2C_HID_CMD(opcode_) \ |
| 105 | .opcode = opcode_, .length = 4, \ |
| 106 | .registerIndex = offsetof(struct i2c_hid_desc, wCommandRegister) |
| 107 | |
| 108 | /* fetch HID descriptor */ |
| 109 | static const struct i2c_hid_cmd hid_descr_cmd = { .length = 2 }; |
| 110 | /* fetch report descriptors */ |
| 111 | static const struct i2c_hid_cmd hid_report_descr_cmd = { |
| 112 | .registerIndex = offsetof(struct i2c_hid_desc, |
| 113 | wReportDescRegister), |
| 114 | .opcode = 0x00, |
| 115 | .length = 2 }; |
| 116 | /* commands */ |
| 117 | static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01), |
| 118 | .wait = true }; |
| 119 | static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) }; |
| 120 | static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) }; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 121 | static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) }; |
Andrew Duggan | 811adb9 | 2013-06-17 16:15:06 -0700 | [diff] [blame] | 122 | static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 }; |
Benjamin Tissoires | 6bf6c8b | 2012-12-04 16:27:47 +0100 | [diff] [blame] | 123 | |
| 124 | /* |
| 125 | * These definitions are not used here, but are defined by the spec. |
| 126 | * Keeping them here for documentation purposes. |
| 127 | * |
| 128 | * static const struct i2c_hid_cmd hid_get_idle_cmd = { I2C_HID_CMD(0x04) }; |
| 129 | * static const struct i2c_hid_cmd hid_set_idle_cmd = { I2C_HID_CMD(0x05) }; |
| 130 | * static const struct i2c_hid_cmd hid_get_protocol_cmd = { I2C_HID_CMD(0x06) }; |
| 131 | * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) }; |
| 132 | */ |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 133 | |
Benjamin Tissoires | 7a7d6d9 | 2012-12-12 18:00:02 +0100 | [diff] [blame] | 134 | static DEFINE_MUTEX(i2c_hid_open_mut); |
| 135 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 136 | /* The main device structure */ |
| 137 | struct i2c_hid { |
| 138 | struct i2c_client *client; /* i2c client */ |
| 139 | struct hid_device *hid; /* pointer to corresponding HID dev */ |
| 140 | union { |
| 141 | __u8 hdesc_buffer[sizeof(struct i2c_hid_desc)]; |
| 142 | struct i2c_hid_desc hdesc; /* the HID Descriptor */ |
| 143 | }; |
| 144 | __le16 wHIDDescRegister; /* location of the i2c |
| 145 | * register of the HID |
| 146 | * descriptor. */ |
| 147 | unsigned int bufsize; /* i2c buffer size */ |
Aaron Ma | ac75a04 | 2018-01-08 10:41:40 +0800 | [diff] [blame] | 148 | u8 *inbuf; /* Input buffer */ |
| 149 | u8 *rawbuf; /* Raw Input buffer */ |
| 150 | u8 *cmdbuf; /* Command buffer */ |
| 151 | u8 *argsbuf; /* Command arguments buffer */ |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 152 | |
| 153 | unsigned long flags; /* device flags */ |
HungNien Chen | 71af01a | 2016-11-10 11:47:13 +0800 | [diff] [blame] | 154 | unsigned long quirks; /* Various quirks */ |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 155 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 156 | wait_queue_head_t wait; /* For waiting the interrupt */ |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 157 | |
| 158 | struct i2c_hid_platform_data pdata; |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 159 | |
| 160 | bool irq_wake_enabled; |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 161 | struct mutex reset_lock; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 162 | }; |
| 163 | |
HungNien Chen | 71af01a | 2016-11-10 11:47:13 +0800 | [diff] [blame] | 164 | static const struct i2c_hid_quirks { |
| 165 | __u16 idVendor; |
| 166 | __u16 idProduct; |
| 167 | __u32 quirks; |
| 168 | } i2c_hid_quirks[] = { |
| 169 | { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8752, |
| 170 | I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV }, |
| 171 | { USB_VENDOR_ID_WEIDA, USB_DEVICE_ID_WEIDA_8755, |
| 172 | I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV }, |
Hans de Goede | 402946a | 2017-11-07 13:28:00 +0100 | [diff] [blame] | 173 | { I2C_VENDOR_ID_HANTICK, I2C_PRODUCT_ID_HANTICK_5288, |
| 174 | I2C_HID_QUIRK_NO_IRQ_AFTER_RESET }, |
Aaron Ma | 3e83eda | 2018-04-09 15:41:31 +0800 | [diff] [blame] | 175 | { I2C_VENDOR_ID_RAYD, I2C_PRODUCT_ID_RAYD_3118, |
| 176 | I2C_HID_QUIRK_RESEND_REPORT_DESCR }, |
HungNien Chen | 71af01a | 2016-11-10 11:47:13 +0800 | [diff] [blame] | 177 | { 0, 0 } |
| 178 | }; |
| 179 | |
| 180 | /* |
| 181 | * i2c_hid_lookup_quirk: return any quirks associated with a I2C HID device |
| 182 | * @idVendor: the 16-bit vendor ID |
| 183 | * @idProduct: the 16-bit product ID |
| 184 | * |
| 185 | * Returns: a u32 quirks value. |
| 186 | */ |
| 187 | static u32 i2c_hid_lookup_quirk(const u16 idVendor, const u16 idProduct) |
| 188 | { |
| 189 | u32 quirks = 0; |
| 190 | int n; |
| 191 | |
| 192 | for (n = 0; i2c_hid_quirks[n].idVendor; n++) |
| 193 | if (i2c_hid_quirks[n].idVendor == idVendor && |
| 194 | (i2c_hid_quirks[n].idProduct == (__u16)HID_ANY_ID || |
| 195 | i2c_hid_quirks[n].idProduct == idProduct)) |
| 196 | quirks = i2c_hid_quirks[n].quirks; |
| 197 | |
| 198 | return quirks; |
| 199 | } |
| 200 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 201 | static int __i2c_hid_command(struct i2c_client *client, |
| 202 | const struct i2c_hid_cmd *command, u8 reportID, |
| 203 | u8 reportType, u8 *args, int args_len, |
| 204 | unsigned char *buf_recv, int data_len) |
| 205 | { |
| 206 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 207 | union command *cmd = (union command *)ihid->cmdbuf; |
| 208 | int ret; |
| 209 | struct i2c_msg msg[2]; |
| 210 | int msg_num = 1; |
| 211 | |
| 212 | int length = command->length; |
| 213 | bool wait = command->wait; |
| 214 | unsigned int registerIndex = command->registerIndex; |
| 215 | |
| 216 | /* special case for hid_descr_cmd */ |
| 217 | if (command == &hid_descr_cmd) { |
| 218 | cmd->c.reg = ihid->wHIDDescRegister; |
| 219 | } else { |
| 220 | cmd->data[0] = ihid->hdesc_buffer[registerIndex]; |
| 221 | cmd->data[1] = ihid->hdesc_buffer[registerIndex + 1]; |
| 222 | } |
| 223 | |
| 224 | if (length > 2) { |
| 225 | cmd->c.opcode = command->opcode; |
| 226 | cmd->c.reportTypeID = reportID | reportType << 4; |
| 227 | } |
| 228 | |
| 229 | memcpy(cmd->data + length, args, args_len); |
| 230 | length += args_len; |
| 231 | |
| 232 | i2c_hid_dbg(ihid, "%s: cmd=%*ph\n", __func__, length, cmd->data); |
| 233 | |
| 234 | msg[0].addr = client->addr; |
| 235 | msg[0].flags = client->flags & I2C_M_TEN; |
| 236 | msg[0].len = length; |
| 237 | msg[0].buf = cmd->data; |
| 238 | if (data_len > 0) { |
| 239 | msg[1].addr = client->addr; |
| 240 | msg[1].flags = client->flags & I2C_M_TEN; |
| 241 | msg[1].flags |= I2C_M_RD; |
| 242 | msg[1].len = data_len; |
| 243 | msg[1].buf = buf_recv; |
| 244 | msg_num = 2; |
| 245 | set_bit(I2C_HID_READ_PENDING, &ihid->flags); |
| 246 | } |
| 247 | |
| 248 | if (wait) |
| 249 | set_bit(I2C_HID_RESET_PENDING, &ihid->flags); |
| 250 | |
| 251 | ret = i2c_transfer(client->adapter, msg, msg_num); |
| 252 | |
| 253 | if (data_len > 0) |
| 254 | clear_bit(I2C_HID_READ_PENDING, &ihid->flags); |
| 255 | |
| 256 | if (ret != msg_num) |
| 257 | return ret < 0 ? ret : -EIO; |
| 258 | |
| 259 | ret = 0; |
| 260 | |
Hans de Goede | 402946a | 2017-11-07 13:28:00 +0100 | [diff] [blame] | 261 | if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) { |
| 262 | msleep(100); |
| 263 | } else if (wait) { |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 264 | i2c_hid_dbg(ihid, "%s: waiting...\n", __func__); |
| 265 | if (!wait_event_timeout(ihid->wait, |
| 266 | !test_bit(I2C_HID_RESET_PENDING, &ihid->flags), |
| 267 | msecs_to_jiffies(5000))) |
| 268 | ret = -ENODATA; |
| 269 | i2c_hid_dbg(ihid, "%s: finished.\n", __func__); |
| 270 | } |
| 271 | |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | static int i2c_hid_command(struct i2c_client *client, |
| 276 | const struct i2c_hid_cmd *command, |
| 277 | unsigned char *buf_recv, int data_len) |
| 278 | { |
| 279 | return __i2c_hid_command(client, command, 0, 0, NULL, 0, |
| 280 | buf_recv, data_len); |
| 281 | } |
| 282 | |
| 283 | static int i2c_hid_get_report(struct i2c_client *client, u8 reportType, |
| 284 | u8 reportID, unsigned char *buf_recv, int data_len) |
| 285 | { |
| 286 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 287 | u8 args[3]; |
| 288 | int ret; |
| 289 | int args_len = 0; |
| 290 | u16 readRegister = le16_to_cpu(ihid->hdesc.wDataRegister); |
| 291 | |
| 292 | i2c_hid_dbg(ihid, "%s\n", __func__); |
| 293 | |
| 294 | if (reportID >= 0x0F) { |
| 295 | args[args_len++] = reportID; |
| 296 | reportID = 0x0F; |
| 297 | } |
| 298 | |
| 299 | args[args_len++] = readRegister & 0xFF; |
| 300 | args[args_len++] = readRegister >> 8; |
| 301 | |
| 302 | ret = __i2c_hid_command(client, &hid_get_report_cmd, reportID, |
| 303 | reportType, args, args_len, buf_recv, data_len); |
| 304 | if (ret) { |
| 305 | dev_err(&client->dev, |
| 306 | "failed to retrieve report from device.\n"); |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 307 | return ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 313 | /** |
| 314 | * i2c_hid_set_or_send_report: forward an incoming report to the device |
| 315 | * @client: the i2c_client of the device |
| 316 | * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT |
| 317 | * @reportID: the report ID |
| 318 | * @buf: the actual data to transfer, without the report ID |
| 319 | * @len: size of buf |
| 320 | * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report |
| 321 | */ |
| 322 | static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType, |
| 323 | u8 reportID, unsigned char *buf, size_t data_len, bool use_data) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 324 | { |
| 325 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 326 | u8 *args = ihid->argsbuf; |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 327 | const struct i2c_hid_cmd *hidcmd; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 328 | int ret; |
| 329 | u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister); |
Andrew Duggan | 811adb9 | 2013-06-17 16:15:06 -0700 | [diff] [blame] | 330 | u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister); |
| 331 | u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength); |
Dmitry Torokhov | 3b65428 | 2016-03-14 15:21:04 -0700 | [diff] [blame] | 332 | u16 size; |
| 333 | int args_len; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 334 | int index = 0; |
| 335 | |
| 336 | i2c_hid_dbg(ihid, "%s\n", __func__); |
| 337 | |
Dmitry Torokhov | 3b65428 | 2016-03-14 15:21:04 -0700 | [diff] [blame] | 338 | if (data_len > ihid->bufsize) |
| 339 | return -EINVAL; |
| 340 | |
| 341 | size = 2 /* size */ + |
| 342 | (reportID ? 1 : 0) /* reportID */ + |
| 343 | data_len /* buf */; |
| 344 | args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ + |
| 345 | 2 /* dataRegister */ + |
| 346 | size /* args */; |
| 347 | |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 348 | if (!use_data && maxOutputLength == 0) |
| 349 | return -ENOSYS; |
| 350 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 351 | if (reportID >= 0x0F) { |
| 352 | args[index++] = reportID; |
| 353 | reportID = 0x0F; |
| 354 | } |
| 355 | |
Andrew Duggan | 811adb9 | 2013-06-17 16:15:06 -0700 | [diff] [blame] | 356 | /* |
| 357 | * use the data register for feature reports or if the device does not |
| 358 | * support the output register |
| 359 | */ |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 360 | if (use_data) { |
Andrew Duggan | 811adb9 | 2013-06-17 16:15:06 -0700 | [diff] [blame] | 361 | args[index++] = dataRegister & 0xFF; |
| 362 | args[index++] = dataRegister >> 8; |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 363 | hidcmd = &hid_set_report_cmd; |
Andrew Duggan | 811adb9 | 2013-06-17 16:15:06 -0700 | [diff] [blame] | 364 | } else { |
| 365 | args[index++] = outputRegister & 0xFF; |
| 366 | args[index++] = outputRegister >> 8; |
| 367 | hidcmd = &hid_no_cmd; |
| 368 | } |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 369 | |
| 370 | args[index++] = size & 0xFF; |
| 371 | args[index++] = size >> 8; |
| 372 | |
| 373 | if (reportID) |
| 374 | args[index++] = reportID; |
| 375 | |
| 376 | memcpy(&args[index], buf, data_len); |
| 377 | |
Andrew Duggan | 811adb9 | 2013-06-17 16:15:06 -0700 | [diff] [blame] | 378 | ret = __i2c_hid_command(client, hidcmd, reportID, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 379 | reportType, args, args_len, NULL, 0); |
| 380 | if (ret) { |
| 381 | dev_err(&client->dev, "failed to set a report to device.\n"); |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 382 | return ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | return data_len; |
| 386 | } |
| 387 | |
| 388 | static int i2c_hid_set_power(struct i2c_client *client, int power_state) |
| 389 | { |
| 390 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 391 | int ret; |
| 392 | |
| 393 | i2c_hid_dbg(ihid, "%s\n", __func__); |
| 394 | |
HungNien Chen | 71af01a | 2016-11-10 11:47:13 +0800 | [diff] [blame] | 395 | /* |
| 396 | * Some devices require to send a command to wakeup before power on. |
| 397 | * The call will get a return value (EREMOTEIO) but device will be |
| 398 | * triggered and activated. After that, it goes like a normal device. |
| 399 | */ |
| 400 | if (power_state == I2C_HID_PWR_ON && |
| 401 | ihid->quirks & I2C_HID_QUIRK_SET_PWR_WAKEUP_DEV) { |
| 402 | ret = i2c_hid_command(client, &hid_set_power_cmd, NULL, 0); |
| 403 | |
| 404 | /* Device was already activated */ |
| 405 | if (!ret) |
| 406 | goto set_pwr_exit; |
| 407 | } |
| 408 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 409 | ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state, |
| 410 | 0, NULL, 0, NULL, 0); |
HungNien Chen | 71af01a | 2016-11-10 11:47:13 +0800 | [diff] [blame] | 411 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 412 | if (ret) |
| 413 | dev_err(&client->dev, "failed to change power setting.\n"); |
| 414 | |
HungNien Chen | 71af01a | 2016-11-10 11:47:13 +0800 | [diff] [blame] | 415 | set_pwr_exit: |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | static int i2c_hid_hwreset(struct i2c_client *client) |
| 420 | { |
| 421 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 422 | int ret; |
| 423 | |
| 424 | i2c_hid_dbg(ihid, "%s\n", __func__); |
| 425 | |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 426 | /* |
| 427 | * This prevents sending feature reports while the device is |
| 428 | * being reset. Otherwise we may lose the reset complete |
| 429 | * interrupt. |
| 430 | */ |
| 431 | mutex_lock(&ihid->reset_lock); |
| 432 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 433 | ret = i2c_hid_set_power(client, I2C_HID_PWR_ON); |
| 434 | if (ret) |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 435 | goto out_unlock; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 436 | |
Brendan McGrath | a89af4a | 2017-01-07 08:01:38 +1100 | [diff] [blame] | 437 | /* |
| 438 | * The HID over I2C specification states that if a DEVICE needs time |
| 439 | * after the PWR_ON request, it should utilise CLOCK stretching. |
| 440 | * However, it has been observered that the Windows driver provides a |
| 441 | * 1ms sleep between the PWR_ON and RESET requests and that some devices |
| 442 | * rely on this. |
| 443 | */ |
| 444 | usleep_range(1000, 5000); |
| 445 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 446 | i2c_hid_dbg(ihid, "resetting...\n"); |
| 447 | |
| 448 | ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0); |
| 449 | if (ret) { |
| 450 | dev_err(&client->dev, "failed to reset device.\n"); |
| 451 | i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 452 | } |
| 453 | |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 454 | out_unlock: |
| 455 | mutex_unlock(&ihid->reset_lock); |
| 456 | return ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 457 | } |
| 458 | |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 459 | static void i2c_hid_get_input(struct i2c_hid *ihid) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 460 | { |
Aaron Ma | ac75a04 | 2018-01-08 10:41:40 +0800 | [diff] [blame] | 461 | int ret; |
| 462 | u32 ret_size; |
Seth Forshee | 6d00f37 | 2015-02-20 11:45:11 -0600 | [diff] [blame] | 463 | int size = le16_to_cpu(ihid->hdesc.wMaxInputLength); |
| 464 | |
| 465 | if (size > ihid->bufsize) |
| 466 | size = ihid->bufsize; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 467 | |
| 468 | ret = i2c_master_recv(ihid->client, ihid->inbuf, size); |
| 469 | if (ret != size) { |
| 470 | if (ret < 0) |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 471 | return; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 472 | |
| 473 | dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n", |
| 474 | __func__, ret, size); |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 475 | return; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8; |
| 479 | |
| 480 | if (!ret_size) { |
| 481 | /* host or device initiated RESET completed */ |
| 482 | if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags)) |
| 483 | wake_up(&ihid->wait); |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 484 | return; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Aaron Ma | ac75a04 | 2018-01-08 10:41:40 +0800 | [diff] [blame] | 487 | if ((ret_size > size) || (ret_size <= 2)) { |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 488 | dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n", |
| 489 | __func__, size, ret_size); |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 490 | return; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf); |
| 494 | |
| 495 | if (test_bit(I2C_HID_STARTED, &ihid->flags)) |
| 496 | hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2, |
| 497 | ret_size - 2, 1); |
| 498 | |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 499 | return; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | static irqreturn_t i2c_hid_irq(int irq, void *dev_id) |
| 503 | { |
| 504 | struct i2c_hid *ihid = dev_id; |
| 505 | |
| 506 | if (test_bit(I2C_HID_READ_PENDING, &ihid->flags)) |
| 507 | return IRQ_HANDLED; |
| 508 | |
| 509 | i2c_hid_get_input(ihid); |
| 510 | |
| 511 | return IRQ_HANDLED; |
| 512 | } |
| 513 | |
| 514 | static int i2c_hid_get_report_length(struct hid_report *report) |
| 515 | { |
| 516 | return ((report->size - 1) >> 3) + 1 + |
| 517 | report->device->report_enum[report->type].numbered + 2; |
| 518 | } |
| 519 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 520 | /* |
| 521 | * Traverse the supplied list of reports and find the longest |
| 522 | */ |
| 523 | static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type, |
| 524 | unsigned int *max) |
| 525 | { |
| 526 | struct hid_report *report; |
| 527 | unsigned int size; |
| 528 | |
| 529 | /* We should not rely on wMaxInputLength, as some devices may set it to |
| 530 | * a wrong length. */ |
| 531 | list_for_each_entry(report, &hid->report_enum[type].report_list, list) { |
| 532 | size = i2c_hid_get_report_length(report); |
| 533 | if (*max < size) |
| 534 | *max = size; |
| 535 | } |
| 536 | } |
| 537 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 538 | static void i2c_hid_free_buffers(struct i2c_hid *ihid) |
| 539 | { |
| 540 | kfree(ihid->inbuf); |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 541 | kfree(ihid->rawbuf); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 542 | kfree(ihid->argsbuf); |
| 543 | kfree(ihid->cmdbuf); |
| 544 | ihid->inbuf = NULL; |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 545 | ihid->rawbuf = NULL; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 546 | ihid->cmdbuf = NULL; |
| 547 | ihid->argsbuf = NULL; |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 548 | ihid->bufsize = 0; |
| 549 | } |
| 550 | |
| 551 | static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size) |
| 552 | { |
| 553 | /* the worst case is computed from the set_report command with a |
| 554 | * reportID > 15 and the maximum report length */ |
Adrian Salido | 8320cae | 2017-09-08 10:55:27 -0700 | [diff] [blame] | 555 | int args_len = sizeof(__u8) + /* ReportID */ |
| 556 | sizeof(__u8) + /* optional ReportID byte */ |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 557 | sizeof(__u16) + /* data register */ |
| 558 | sizeof(__u16) + /* size of the report */ |
| 559 | report_size; /* report */ |
| 560 | |
| 561 | ihid->inbuf = kzalloc(report_size, GFP_KERNEL); |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 562 | ihid->rawbuf = kzalloc(report_size, GFP_KERNEL); |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 563 | ihid->argsbuf = kzalloc(args_len, GFP_KERNEL); |
| 564 | ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL); |
| 565 | |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 566 | if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) { |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 567 | i2c_hid_free_buffers(ihid); |
| 568 | return -ENOMEM; |
| 569 | } |
| 570 | |
| 571 | ihid->bufsize = report_size; |
| 572 | |
| 573 | return 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static int i2c_hid_get_raw_report(struct hid_device *hid, |
| 577 | unsigned char report_number, __u8 *buf, size_t count, |
| 578 | unsigned char report_type) |
| 579 | { |
| 580 | struct i2c_client *client = hid->driver_data; |
| 581 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
Benjamin Tissoires | e5b50fe | 2012-12-05 15:02:56 +0100 | [diff] [blame] | 582 | size_t ret_count, ask_count; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 583 | int ret; |
| 584 | |
| 585 | if (report_type == HID_OUTPUT_REPORT) |
| 586 | return -EINVAL; |
| 587 | |
Benjamin Tissoires | e5b50fe | 2012-12-05 15:02:56 +0100 | [diff] [blame] | 588 | /* +2 bytes to include the size of the reply in the query buffer */ |
| 589 | ask_count = min(count + 2, (size_t)ihid->bufsize); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 590 | |
| 591 | ret = i2c_hid_get_report(client, |
| 592 | report_type == HID_FEATURE_REPORT ? 0x03 : 0x01, |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 593 | report_number, ihid->rawbuf, ask_count); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 594 | |
| 595 | if (ret < 0) |
| 596 | return ret; |
| 597 | |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 598 | ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 599 | |
Jiri Kosina | 9afd09a | 2012-12-06 10:59:28 +0100 | [diff] [blame] | 600 | if (ret_count <= 2) |
Benjamin Tissoires | e5b50fe | 2012-12-05 15:02:56 +0100 | [diff] [blame] | 601 | return 0; |
| 602 | |
| 603 | ret_count = min(ret_count, ask_count); |
| 604 | |
| 605 | /* The query buffer contains the size, dropping it in the reply */ |
| 606 | count = min(count, ret_count - 2); |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 607 | memcpy(buf, ihid->rawbuf + 2, count); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 608 | |
| 609 | return count; |
| 610 | } |
| 611 | |
| 612 | static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf, |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 613 | size_t count, unsigned char report_type, bool use_data) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 614 | { |
| 615 | struct i2c_client *client = hid->driver_data; |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 616 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 617 | int report_id = buf[0]; |
Benjamin Tissoires | c284979 | 2013-01-31 17:50:02 +0100 | [diff] [blame] | 618 | int ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 619 | |
| 620 | if (report_type == HID_INPUT_REPORT) |
| 621 | return -EINVAL; |
| 622 | |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 623 | mutex_lock(&ihid->reset_lock); |
| 624 | |
Benjamin Tissoires | c284979 | 2013-01-31 17:50:02 +0100 | [diff] [blame] | 625 | if (report_id) { |
| 626 | buf++; |
| 627 | count--; |
| 628 | } |
| 629 | |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 630 | ret = i2c_hid_set_or_send_report(client, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 631 | report_type == HID_FEATURE_REPORT ? 0x03 : 0x02, |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 632 | report_id, buf, count, use_data); |
Benjamin Tissoires | c284979 | 2013-01-31 17:50:02 +0100 | [diff] [blame] | 633 | |
| 634 | if (report_id && ret >= 0) |
| 635 | ret++; /* add report_id to the number of transfered bytes */ |
| 636 | |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 637 | mutex_unlock(&ihid->reset_lock); |
| 638 | |
Benjamin Tissoires | c284979 | 2013-01-31 17:50:02 +0100 | [diff] [blame] | 639 | return ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 640 | } |
| 641 | |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 642 | static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf, |
| 643 | size_t count) |
Benjamin Tissoires | 545bef6 | 2013-02-25 11:31:50 +0100 | [diff] [blame] | 644 | { |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 645 | return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT, |
| 646 | false); |
| 647 | } |
Benjamin Tissoires | 545bef6 | 2013-02-25 11:31:50 +0100 | [diff] [blame] | 648 | |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 649 | static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum, |
| 650 | __u8 *buf, size_t len, unsigned char rtype, |
| 651 | int reqtype) |
| 652 | { |
Benjamin Tissoires | 545bef6 | 2013-02-25 11:31:50 +0100 | [diff] [blame] | 653 | switch (reqtype) { |
| 654 | case HID_REQ_GET_REPORT: |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 655 | return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype); |
Benjamin Tissoires | 545bef6 | 2013-02-25 11:31:50 +0100 | [diff] [blame] | 656 | case HID_REQ_SET_REPORT: |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 657 | if (buf[0] != reportnum) |
| 658 | return -EINVAL; |
| 659 | return i2c_hid_output_raw_report(hid, buf, len, rtype, true); |
| 660 | default: |
| 661 | return -EIO; |
Benjamin Tissoires | 545bef6 | 2013-02-25 11:31:50 +0100 | [diff] [blame] | 662 | } |
Benjamin Tissoires | 545bef6 | 2013-02-25 11:31:50 +0100 | [diff] [blame] | 663 | } |
| 664 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 665 | static int i2c_hid_parse(struct hid_device *hid) |
| 666 | { |
| 667 | struct i2c_client *client = hid->driver_data; |
| 668 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 669 | struct i2c_hid_desc *hdesc = &ihid->hdesc; |
| 670 | unsigned int rsize; |
| 671 | char *rdesc; |
| 672 | int ret; |
| 673 | int tries = 3; |
| 674 | |
| 675 | i2c_hid_dbg(ihid, "entering %s\n", __func__); |
| 676 | |
| 677 | rsize = le16_to_cpu(hdesc->wReportDescLength); |
| 678 | if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) { |
| 679 | dbg_hid("weird size of report descriptor (%u)\n", rsize); |
| 680 | return -EINVAL; |
| 681 | } |
| 682 | |
| 683 | do { |
| 684 | ret = i2c_hid_hwreset(client); |
| 685 | if (ret) |
| 686 | msleep(1000); |
| 687 | } while (tries-- > 0 && ret); |
| 688 | |
| 689 | if (ret) |
| 690 | return ret; |
| 691 | |
| 692 | rdesc = kzalloc(rsize, GFP_KERNEL); |
| 693 | |
| 694 | if (!rdesc) { |
| 695 | dbg_hid("couldn't allocate rdesc memory\n"); |
| 696 | return -ENOMEM; |
| 697 | } |
| 698 | |
| 699 | i2c_hid_dbg(ihid, "asking HID report descriptor\n"); |
| 700 | |
| 701 | ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize); |
| 702 | if (ret) { |
| 703 | hid_err(hid, "reading report descriptor failed\n"); |
| 704 | kfree(rdesc); |
| 705 | return -EIO; |
| 706 | } |
| 707 | |
| 708 | i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc); |
| 709 | |
| 710 | ret = hid_parse_report(hid, rdesc, rsize); |
| 711 | kfree(rdesc); |
| 712 | if (ret) { |
| 713 | dbg_hid("parsing report descriptor failed\n"); |
| 714 | return ret; |
| 715 | } |
| 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | static int i2c_hid_start(struct hid_device *hid) |
| 721 | { |
| 722 | struct i2c_client *client = hid->driver_data; |
| 723 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 724 | int ret; |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 725 | unsigned int bufsize = HID_MIN_BUFFER_SIZE; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 726 | |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 727 | i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize); |
| 728 | i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize); |
| 729 | i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 730 | |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 731 | if (bufsize > ihid->bufsize) { |
Benjamin Tissoires | 8cd1616 | 2016-12-10 21:58:55 +0100 | [diff] [blame] | 732 | disable_irq(client->irq); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 733 | i2c_hid_free_buffers(ihid); |
| 734 | |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 735 | ret = i2c_hid_alloc_buffers(ihid, bufsize); |
Benjamin Tissoires | 8cd1616 | 2016-12-10 21:58:55 +0100 | [diff] [blame] | 736 | enable_irq(client->irq); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 737 | |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 738 | if (ret) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 739 | return ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 740 | } |
| 741 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | static void i2c_hid_stop(struct hid_device *hid) |
| 746 | { |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 747 | hid->claimed = 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | static int i2c_hid_open(struct hid_device *hid) |
| 751 | { |
| 752 | struct i2c_client *client = hid->driver_data; |
| 753 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
Benjamin Tissoires | 7a7d6d9 | 2012-12-12 18:00:02 +0100 | [diff] [blame] | 754 | int ret = 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 755 | |
Dmitry Torokhov | 85ae911 | 2017-06-06 23:59:35 -0700 | [diff] [blame] | 756 | ret = pm_runtime_get_sync(&client->dev); |
| 757 | if (ret < 0) |
| 758 | return ret; |
| 759 | |
| 760 | set_bit(I2C_HID_STARTED, &ihid->flags); |
| 761 | return 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | static void i2c_hid_close(struct hid_device *hid) |
| 765 | { |
| 766 | struct i2c_client *client = hid->driver_data; |
| 767 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 768 | |
Dmitry Torokhov | 85ae911 | 2017-06-06 23:59:35 -0700 | [diff] [blame] | 769 | clear_bit(I2C_HID_STARTED, &ihid->flags); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 770 | |
Dmitry Torokhov | 85ae911 | 2017-06-06 23:59:35 -0700 | [diff] [blame] | 771 | /* Save some power */ |
| 772 | pm_runtime_put(&client->dev); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | static int i2c_hid_power(struct hid_device *hid, int lvl) |
| 776 | { |
| 777 | struct i2c_client *client = hid->driver_data; |
| 778 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 779 | |
| 780 | i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl); |
| 781 | |
| 782 | switch (lvl) { |
| 783 | case PM_HINT_FULLON: |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 784 | pm_runtime_get_sync(&client->dev); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 785 | break; |
| 786 | case PM_HINT_NORMAL: |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 787 | pm_runtime_put(&client->dev); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 788 | break; |
| 789 | } |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 790 | return 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 791 | } |
| 792 | |
Jason Gerecke | fc2237a | 2017-07-24 09:46:18 -0700 | [diff] [blame] | 793 | struct hid_ll_driver i2c_hid_ll_driver = { |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 794 | .parse = i2c_hid_parse, |
| 795 | .start = i2c_hid_start, |
| 796 | .stop = i2c_hid_stop, |
| 797 | .open = i2c_hid_open, |
| 798 | .close = i2c_hid_close, |
| 799 | .power = i2c_hid_power, |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 800 | .output_report = i2c_hid_output_report, |
| 801 | .raw_request = i2c_hid_raw_request, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 802 | }; |
Jason Gerecke | fc2237a | 2017-07-24 09:46:18 -0700 | [diff] [blame] | 803 | EXPORT_SYMBOL_GPL(i2c_hid_ll_driver); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 804 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 805 | static int i2c_hid_init_irq(struct i2c_client *client) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 806 | { |
| 807 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
Benjamin Tissoires | 00f7fea | 2016-11-21 15:21:27 +0100 | [diff] [blame] | 808 | unsigned long irqflags = 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 809 | int ret; |
| 810 | |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 811 | dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 812 | |
Benjamin Tissoires | 00f7fea | 2016-11-21 15:21:27 +0100 | [diff] [blame] | 813 | if (!irq_get_trigger_type(client->irq)) |
| 814 | irqflags = IRQF_TRIGGER_LOW; |
| 815 | |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 816 | ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq, |
Benjamin Tissoires | 00f7fea | 2016-11-21 15:21:27 +0100 | [diff] [blame] | 817 | irqflags | IRQF_ONESHOT, client->name, ihid); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 818 | if (ret < 0) { |
Benjamin Tissoires | 9972dcc | 2012-12-04 16:27:49 +0100 | [diff] [blame] | 819 | dev_warn(&client->dev, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 820 | "Could not register for %s interrupt, irq = %d," |
| 821 | " ret = %d\n", |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 822 | client->name, client->irq, ret); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 823 | |
| 824 | return ret; |
| 825 | } |
| 826 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 827 | return 0; |
| 828 | } |
| 829 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 830 | static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 831 | { |
| 832 | struct i2c_client *client = ihid->client; |
| 833 | struct i2c_hid_desc *hdesc = &ihid->hdesc; |
| 834 | unsigned int dsize; |
| 835 | int ret; |
| 836 | |
Archana Patni | f58b848 | 2014-05-08 09:26:19 -0400 | [diff] [blame] | 837 | /* i2c hid fetch using a fixed descriptor size (30 bytes) */ |
| 838 | i2c_hid_dbg(ihid, "Fetching the HID descriptor\n"); |
| 839 | ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer, |
| 840 | sizeof(struct i2c_hid_desc)); |
| 841 | if (ret) { |
| 842 | dev_err(&client->dev, "hid_descr_cmd failed\n"); |
| 843 | return -ENODEV; |
| 844 | } |
| 845 | |
| 846 | /* Validate the length of HID descriptor, the 4 first bytes: |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 847 | * bytes 0-1 -> length |
| 848 | * bytes 2-3 -> bcdVersion (has to be 1.00) */ |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 849 | /* check bcdVersion == 1.0 */ |
| 850 | if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) { |
| 851 | dev_err(&client->dev, |
Benjamin Tissoires | 9972dcc | 2012-12-04 16:27:49 +0100 | [diff] [blame] | 852 | "unexpected HID descriptor bcdVersion (0x%04hx)\n", |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 853 | le16_to_cpu(hdesc->bcdVersion)); |
| 854 | return -ENODEV; |
| 855 | } |
| 856 | |
Archana Patni | f58b848 | 2014-05-08 09:26:19 -0400 | [diff] [blame] | 857 | /* Descriptor length should be 30 bytes as per the specification */ |
| 858 | dsize = le16_to_cpu(hdesc->wHIDDescLength); |
| 859 | if (dsize != sizeof(struct i2c_hid_desc)) { |
| 860 | dev_err(&client->dev, "weird size of HID descriptor (%u)\n", |
| 861 | dsize); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 862 | return -ENODEV; |
| 863 | } |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 864 | i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 865 | return 0; |
| 866 | } |
| 867 | |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 868 | #ifdef CONFIG_ACPI |
| 869 | static int i2c_hid_acpi_pdata(struct i2c_client *client, |
| 870 | struct i2c_hid_platform_data *pdata) |
| 871 | { |
Andy Shevchenko | 94116f8 | 2017-06-05 19:40:46 +0300 | [diff] [blame] | 872 | static guid_t i2c_hid_guid = |
| 873 | GUID_INIT(0x3CDFF6F7, 0x4267, 0x4555, |
| 874 | 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE); |
Jiang Liu | ea547d7 | 2013-12-19 20:38:19 +0800 | [diff] [blame] | 875 | union acpi_object *obj; |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 876 | struct acpi_device *adev; |
| 877 | acpi_handle handle; |
| 878 | |
| 879 | handle = ACPI_HANDLE(&client->dev); |
| 880 | if (!handle || acpi_bus_get_device(handle, &adev)) |
| 881 | return -ENODEV; |
| 882 | |
Andy Shevchenko | 94116f8 | 2017-06-05 19:40:46 +0300 | [diff] [blame] | 883 | obj = acpi_evaluate_dsm_typed(handle, &i2c_hid_guid, 1, 1, NULL, |
Jiang Liu | ea547d7 | 2013-12-19 20:38:19 +0800 | [diff] [blame] | 884 | ACPI_TYPE_INTEGER); |
| 885 | if (!obj) { |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 886 | dev_err(&client->dev, "device _DSM execution failed\n"); |
| 887 | return -ENODEV; |
| 888 | } |
| 889 | |
Jiang Liu | ea547d7 | 2013-12-19 20:38:19 +0800 | [diff] [blame] | 890 | pdata->hid_descriptor_address = obj->integer.value; |
| 891 | ACPI_FREE(obj); |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 892 | |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 893 | return 0; |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 894 | } |
| 895 | |
Hans de Goede | f3d3eab | 2017-05-09 10:04:36 +0200 | [diff] [blame] | 896 | static void i2c_hid_acpi_fix_up_power(struct device *dev) |
| 897 | { |
Hans de Goede | f3d3eab | 2017-05-09 10:04:36 +0200 | [diff] [blame] | 898 | struct acpi_device *adev; |
| 899 | |
Andy Shevchenko | 8afede1 | 2018-01-19 16:35:04 +0200 | [diff] [blame] | 900 | adev = ACPI_COMPANION(dev); |
| 901 | if (adev) |
Hans de Goede | f3d3eab | 2017-05-09 10:04:36 +0200 | [diff] [blame] | 902 | acpi_device_fix_up_power(adev); |
| 903 | } |
| 904 | |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 905 | static const struct acpi_device_id i2c_hid_acpi_match[] = { |
| 906 | {"ACPI0C50", 0 }, |
| 907 | {"PNP0C50", 0 }, |
| 908 | { }, |
| 909 | }; |
| 910 | MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match); |
| 911 | #else |
| 912 | static inline int i2c_hid_acpi_pdata(struct i2c_client *client, |
| 913 | struct i2c_hid_platform_data *pdata) |
| 914 | { |
| 915 | return -ENODEV; |
| 916 | } |
Hans de Goede | f3d3eab | 2017-05-09 10:04:36 +0200 | [diff] [blame] | 917 | |
| 918 | static inline void i2c_hid_acpi_fix_up_power(struct device *dev) {} |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 919 | #endif |
| 920 | |
Benjamin Tissoires | 3d7d248 | 2013-06-13 09:50:35 +0200 | [diff] [blame] | 921 | #ifdef CONFIG_OF |
| 922 | static int i2c_hid_of_probe(struct i2c_client *client, |
| 923 | struct i2c_hid_platform_data *pdata) |
| 924 | { |
| 925 | struct device *dev = &client->dev; |
| 926 | u32 val; |
| 927 | int ret; |
| 928 | |
| 929 | ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val); |
| 930 | if (ret) { |
| 931 | dev_err(&client->dev, "HID register address not provided\n"); |
| 932 | return -ENODEV; |
| 933 | } |
| 934 | if (val >> 16) { |
| 935 | dev_err(&client->dev, "Bad HID register address: 0x%08x\n", |
| 936 | val); |
| 937 | return -EINVAL; |
| 938 | } |
| 939 | pdata->hid_descriptor_address = val; |
| 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | static const struct of_device_id i2c_hid_of_match[] = { |
| 945 | { .compatible = "hid-over-i2c" }, |
| 946 | {}, |
| 947 | }; |
| 948 | MODULE_DEVICE_TABLE(of, i2c_hid_of_match); |
| 949 | #else |
| 950 | static inline int i2c_hid_of_probe(struct i2c_client *client, |
| 951 | struct i2c_hid_platform_data *pdata) |
| 952 | { |
| 953 | return -ENODEV; |
| 954 | } |
| 955 | #endif |
| 956 | |
Rajat Jain | 847989e | 2017-10-03 11:19:21 -0700 | [diff] [blame] | 957 | static void i2c_hid_fwnode_probe(struct i2c_client *client, |
| 958 | struct i2c_hid_platform_data *pdata) |
| 959 | { |
| 960 | u32 val; |
| 961 | |
| 962 | if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms", |
| 963 | &val)) |
| 964 | pdata->post_power_delay_ms = val; |
| 965 | } |
| 966 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 967 | static int i2c_hid_probe(struct i2c_client *client, |
| 968 | const struct i2c_device_id *dev_id) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 969 | { |
| 970 | int ret; |
| 971 | struct i2c_hid *ihid; |
| 972 | struct hid_device *hid; |
| 973 | __u16 hidRegister; |
| 974 | struct i2c_hid_platform_data *platform_data = client->dev.platform_data; |
| 975 | |
| 976 | dbg_hid("HID probe called for i2c 0x%02x\n", client->addr); |
| 977 | |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 978 | if (!client->irq) { |
| 979 | dev_err(&client->dev, |
| 980 | "HID over i2c has not been provided an Int IRQ\n"); |
| 981 | return -EINVAL; |
| 982 | } |
| 983 | |
David Arcari | 93d26ae | 2016-10-13 11:30:45 +0200 | [diff] [blame] | 984 | if (client->irq < 0) { |
| 985 | if (client->irq != -EPROBE_DEFER) |
| 986 | dev_err(&client->dev, |
| 987 | "HID over i2c doesn't have a valid IRQ\n"); |
| 988 | return client->irq; |
| 989 | } |
| 990 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 991 | ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL); |
| 992 | if (!ihid) |
| 993 | return -ENOMEM; |
| 994 | |
Benjamin Tissoires | 3d7d248 | 2013-06-13 09:50:35 +0200 | [diff] [blame] | 995 | if (client->dev.of_node) { |
| 996 | ret = i2c_hid_of_probe(client, &ihid->pdata); |
| 997 | if (ret) |
| 998 | goto err; |
| 999 | } else if (!platform_data) { |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 1000 | ret = i2c_hid_acpi_pdata(client, &ihid->pdata); |
| 1001 | if (ret) { |
| 1002 | dev_err(&client->dev, |
| 1003 | "HID register address not provided\n"); |
| 1004 | goto err; |
| 1005 | } |
| 1006 | } else { |
| 1007 | ihid->pdata = *platform_data; |
| 1008 | } |
| 1009 | |
Rajat Jain | 847989e | 2017-10-03 11:19:21 -0700 | [diff] [blame] | 1010 | /* Parse platform agnostic common properties from ACPI / device tree */ |
| 1011 | i2c_hid_fwnode_probe(client, &ihid->pdata); |
| 1012 | |
Brian Norris | 572d3c6 | 2017-02-09 18:03:57 -0800 | [diff] [blame] | 1013 | ihid->pdata.supply = devm_regulator_get(&client->dev, "vdd"); |
| 1014 | if (IS_ERR(ihid->pdata.supply)) { |
| 1015 | ret = PTR_ERR(ihid->pdata.supply); |
| 1016 | if (ret != -EPROBE_DEFER) |
| 1017 | dev_err(&client->dev, "Failed to get regulator: %d\n", |
| 1018 | ret); |
Christophe JAILLET | d3d9adf | 2017-02-19 13:07:59 +0100 | [diff] [blame] | 1019 | goto err; |
Brian Norris | 572d3c6 | 2017-02-09 18:03:57 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | ret = regulator_enable(ihid->pdata.supply); |
| 1023 | if (ret < 0) { |
| 1024 | dev_err(&client->dev, "Failed to enable regulator: %d\n", |
| 1025 | ret); |
| 1026 | goto err; |
| 1027 | } |
| 1028 | if (ihid->pdata.post_power_delay_ms) |
| 1029 | msleep(ihid->pdata.post_power_delay_ms); |
| 1030 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1031 | i2c_set_clientdata(client, ihid); |
| 1032 | |
| 1033 | ihid->client = client; |
| 1034 | |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 1035 | hidRegister = ihid->pdata.hid_descriptor_address; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1036 | ihid->wHIDDescRegister = cpu_to_le16(hidRegister); |
| 1037 | |
| 1038 | init_waitqueue_head(&ihid->wait); |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 1039 | mutex_init(&ihid->reset_lock); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1040 | |
| 1041 | /* we need to allocate the command buffer without knowing the maximum |
| 1042 | * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the |
| 1043 | * real computation later. */ |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 1044 | ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE); |
| 1045 | if (ret < 0) |
Brian Norris | 572d3c6 | 2017-02-09 18:03:57 -0800 | [diff] [blame] | 1046 | goto err_regulator; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1047 | |
Hans de Goede | f3d3eab | 2017-05-09 10:04:36 +0200 | [diff] [blame] | 1048 | i2c_hid_acpi_fix_up_power(&client->dev); |
| 1049 | |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1050 | pm_runtime_get_noresume(&client->dev); |
| 1051 | pm_runtime_set_active(&client->dev); |
| 1052 | pm_runtime_enable(&client->dev); |
Fu, Zhonghui | 982e42d | 2016-05-19 10:46:24 +0800 | [diff] [blame] | 1053 | device_enable_async_suspend(&client->dev); |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1054 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1055 | ret = i2c_hid_fetch_hid_descriptor(ihid); |
| 1056 | if (ret < 0) |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1057 | goto err_pm; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1058 | |
| 1059 | ret = i2c_hid_init_irq(client); |
| 1060 | if (ret < 0) |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1061 | goto err_pm; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1062 | |
| 1063 | hid = hid_allocate_device(); |
| 1064 | if (IS_ERR(hid)) { |
| 1065 | ret = PTR_ERR(hid); |
Benjamin Tissoires | 8a1bbb5 | 2012-12-05 15:02:55 +0100 | [diff] [blame] | 1066 | goto err_irq; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | ihid->hid = hid; |
| 1070 | |
| 1071 | hid->driver_data = client; |
| 1072 | hid->ll_driver = &i2c_hid_ll_driver; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1073 | hid->dev.parent = &client->dev; |
| 1074 | hid->bus = BUS_I2C; |
| 1075 | hid->version = le16_to_cpu(ihid->hdesc.bcdVersion); |
| 1076 | hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID); |
| 1077 | hid->product = le16_to_cpu(ihid->hdesc.wProductID); |
| 1078 | |
Benjamin Tissoires | 9972dcc | 2012-12-04 16:27:49 +0100 | [diff] [blame] | 1079 | snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX", |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1080 | client->name, hid->vendor, hid->product); |
Mika Westerberg | f3984ed | 2015-09-28 15:15:08 +0300 | [diff] [blame] | 1081 | strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys)); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1082 | |
HungNien Chen | 71af01a | 2016-11-10 11:47:13 +0800 | [diff] [blame] | 1083 | ihid->quirks = i2c_hid_lookup_quirk(hid->vendor, hid->product); |
| 1084 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1085 | ret = hid_add_device(hid); |
| 1086 | if (ret) { |
| 1087 | if (ret != -ENODEV) |
| 1088 | hid_err(client, "can't add hid device: %d\n", ret); |
| 1089 | goto err_mem_free; |
| 1090 | } |
| 1091 | |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1092 | pm_runtime_put(&client->dev); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1093 | return 0; |
| 1094 | |
| 1095 | err_mem_free: |
| 1096 | hid_destroy_device(hid); |
| 1097 | |
Benjamin Tissoires | 8a1bbb5 | 2012-12-05 15:02:55 +0100 | [diff] [blame] | 1098 | err_irq: |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 1099 | free_irq(client->irq, ihid); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1100 | |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1101 | err_pm: |
| 1102 | pm_runtime_put_noidle(&client->dev); |
| 1103 | pm_runtime_disable(&client->dev); |
| 1104 | |
Brian Norris | 572d3c6 | 2017-02-09 18:03:57 -0800 | [diff] [blame] | 1105 | err_regulator: |
| 1106 | regulator_disable(ihid->pdata.supply); |
| 1107 | |
Benjamin Tissoires | 8a1bbb5 | 2012-12-05 15:02:55 +0100 | [diff] [blame] | 1108 | err: |
Jiri Kosina | 3c62602 | 2012-11-20 17:09:40 +0100 | [diff] [blame] | 1109 | i2c_hid_free_buffers(ihid); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1110 | kfree(ihid); |
| 1111 | return ret; |
| 1112 | } |
| 1113 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 1114 | static int i2c_hid_remove(struct i2c_client *client) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1115 | { |
| 1116 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 1117 | struct hid_device *hid; |
| 1118 | |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1119 | pm_runtime_get_sync(&client->dev); |
| 1120 | pm_runtime_disable(&client->dev); |
| 1121 | pm_runtime_set_suspended(&client->dev); |
| 1122 | pm_runtime_put_noidle(&client->dev); |
| 1123 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1124 | hid = ihid->hid; |
| 1125 | hid_destroy_device(hid); |
| 1126 | |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 1127 | free_irq(client->irq, ihid); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1128 | |
Benjamin Tissoires | 134ebfd | 2012-12-04 16:27:54 +0100 | [diff] [blame] | 1129 | if (ihid->bufsize) |
| 1130 | i2c_hid_free_buffers(ihid); |
| 1131 | |
Brian Norris | 572d3c6 | 2017-02-09 18:03:57 -0800 | [diff] [blame] | 1132 | regulator_disable(ihid->pdata.supply); |
| 1133 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1134 | kfree(ihid); |
| 1135 | |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
Guohua Zhong | d9f448e3 | 2016-06-21 18:27:45 +0800 | [diff] [blame] | 1139 | static void i2c_hid_shutdown(struct i2c_client *client) |
| 1140 | { |
| 1141 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 1142 | |
| 1143 | i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); |
| 1144 | free_irq(client->irq, ihid); |
| 1145 | } |
| 1146 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1147 | #ifdef CONFIG_PM_SLEEP |
| 1148 | static int i2c_hid_suspend(struct device *dev) |
| 1149 | { |
| 1150 | struct i2c_client *client = to_i2c_client(dev); |
Andrew Duggan | 109571c | 2014-07-11 16:34:18 -0700 | [diff] [blame] | 1151 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 1152 | struct hid_device *hid = ihid->hid; |
Doug Anderson | 01714a6 | 2016-03-08 15:03:23 -0800 | [diff] [blame] | 1153 | int ret; |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 1154 | int wake_status; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1155 | |
Doug Anderson | 01714a6 | 2016-03-08 15:03:23 -0800 | [diff] [blame] | 1156 | if (hid->driver && hid->driver->suspend) { |
| 1157 | /* |
| 1158 | * Wake up the device so that IO issues in |
| 1159 | * HID driver's suspend code can succeed. |
| 1160 | */ |
| 1161 | ret = pm_runtime_resume(dev); |
| 1162 | if (ret < 0) |
| 1163 | return ret; |
Gabriele Mazzotta | af4739c | 2015-07-07 21:58:02 +0200 | [diff] [blame] | 1164 | |
Doug Anderson | 01714a6 | 2016-03-08 15:03:23 -0800 | [diff] [blame] | 1165 | ret = hid->driver->suspend(hid, PMSG_SUSPEND); |
| 1166 | if (ret < 0) |
| 1167 | return ret; |
| 1168 | } |
| 1169 | |
| 1170 | if (!pm_runtime_suspended(dev)) { |
| 1171 | /* Save some power */ |
| 1172 | i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); |
| 1173 | |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 1174 | disable_irq(client->irq); |
Doug Anderson | 01714a6 | 2016-03-08 15:03:23 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 1177 | if (device_may_wakeup(&client->dev)) { |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 1178 | wake_status = enable_irq_wake(client->irq); |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 1179 | if (!wake_status) |
| 1180 | ihid->irq_wake_enabled = true; |
| 1181 | else |
| 1182 | hid_warn(hid, "Failed to enable irq wake: %d\n", |
| 1183 | wake_status); |
Brian Norris | 572d3c6 | 2017-02-09 18:03:57 -0800 | [diff] [blame] | 1184 | } else { |
| 1185 | ret = regulator_disable(ihid->pdata.supply); |
| 1186 | if (ret < 0) |
| 1187 | hid_warn(hid, "Failed to disable supply: %d\n", ret); |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 1188 | } |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1189 | |
Doug Anderson | 01714a6 | 2016-03-08 15:03:23 -0800 | [diff] [blame] | 1190 | return 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | static int i2c_hid_resume(struct device *dev) |
| 1194 | { |
| 1195 | int ret; |
| 1196 | struct i2c_client *client = to_i2c_client(dev); |
Andrew Duggan | 109571c | 2014-07-11 16:34:18 -0700 | [diff] [blame] | 1197 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 1198 | struct hid_device *hid = ihid->hid; |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 1199 | int wake_status; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1200 | |
Brian Norris | 572d3c6 | 2017-02-09 18:03:57 -0800 | [diff] [blame] | 1201 | if (!device_may_wakeup(&client->dev)) { |
| 1202 | ret = regulator_enable(ihid->pdata.supply); |
| 1203 | if (ret < 0) |
| 1204 | hid_warn(hid, "Failed to enable supply: %d\n", ret); |
| 1205 | if (ihid->pdata.post_power_delay_ms) |
| 1206 | msleep(ihid->pdata.post_power_delay_ms); |
| 1207 | } else if (ihid->irq_wake_enabled) { |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 1208 | wake_status = disable_irq_wake(client->irq); |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 1209 | if (!wake_status) |
| 1210 | ihid->irq_wake_enabled = false; |
| 1211 | else |
| 1212 | hid_warn(hid, "Failed to disable irq wake: %d\n", |
| 1213 | wake_status); |
| 1214 | } |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1215 | |
Doug Anderson | 01714a6 | 2016-03-08 15:03:23 -0800 | [diff] [blame] | 1216 | /* We'll resume to full power */ |
| 1217 | pm_runtime_disable(dev); |
| 1218 | pm_runtime_set_active(dev); |
| 1219 | pm_runtime_enable(dev); |
| 1220 | |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 1221 | enable_irq(client->irq); |
Doug Anderson | 01714a6 | 2016-03-08 15:03:23 -0800 | [diff] [blame] | 1222 | ret = i2c_hid_hwreset(client); |
| 1223 | if (ret) |
| 1224 | return ret; |
| 1225 | |
Aaron Ma | 3e83eda | 2018-04-09 15:41:31 +0800 | [diff] [blame] | 1226 | /* RAYDIUM device (2386:3118) need to re-send report descr cmd |
| 1227 | * after resume, after this it will be back normal. |
| 1228 | * otherwise it issues too many incomplete reports. |
| 1229 | */ |
| 1230 | if (ihid->quirks & I2C_HID_QUIRK_RESEND_REPORT_DESCR) { |
| 1231 | ret = i2c_hid_command(client, &hid_report_descr_cmd, NULL, 0); |
Jiri Kosina | b658912 | 2018-04-19 09:25:15 +0200 | [diff] [blame^] | 1232 | if (ret) |
Aaron Ma | 3e83eda | 2018-04-09 15:41:31 +0800 | [diff] [blame] | 1233 | return ret; |
| 1234 | } |
| 1235 | |
Andrew Duggan | 109571c | 2014-07-11 16:34:18 -0700 | [diff] [blame] | 1236 | if (hid->driver && hid->driver->reset_resume) { |
| 1237 | ret = hid->driver->reset_resume(hid); |
| 1238 | return ret; |
| 1239 | } |
| 1240 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1241 | return 0; |
| 1242 | } |
| 1243 | #endif |
| 1244 | |
Rafael J. Wysocki | 721564a | 2014-12-04 01:05:44 +0100 | [diff] [blame] | 1245 | #ifdef CONFIG_PM |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1246 | static int i2c_hid_runtime_suspend(struct device *dev) |
| 1247 | { |
| 1248 | struct i2c_client *client = to_i2c_client(dev); |
| 1249 | |
| 1250 | i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 1251 | disable_irq(client->irq); |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | static int i2c_hid_runtime_resume(struct device *dev) |
| 1256 | { |
| 1257 | struct i2c_client *client = to_i2c_client(dev); |
| 1258 | |
David Arcari | ba18a93 | 2016-10-13 11:30:44 +0200 | [diff] [blame] | 1259 | enable_irq(client->irq); |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1260 | i2c_hid_set_power(client, I2C_HID_PWR_ON); |
| 1261 | return 0; |
| 1262 | } |
| 1263 | #endif |
| 1264 | |
| 1265 | static const struct dev_pm_ops i2c_hid_pm = { |
| 1266 | SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume) |
| 1267 | SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume, |
| 1268 | NULL) |
| 1269 | }; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1270 | |
| 1271 | static const struct i2c_device_id i2c_hid_id_table[] = { |
Benjamin Tissoires | 24ebb37 | 2012-12-04 16:27:42 +0100 | [diff] [blame] | 1272 | { "hid", 0 }, |
Benson Leung | 1dcdde9 | 2016-03-08 11:25:39 -0800 | [diff] [blame] | 1273 | { "hid-over-i2c", 0 }, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1274 | { }, |
| 1275 | }; |
| 1276 | MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table); |
| 1277 | |
| 1278 | |
| 1279 | static struct i2c_driver i2c_hid_driver = { |
| 1280 | .driver = { |
| 1281 | .name = "i2c_hid", |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1282 | .pm = &i2c_hid_pm, |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 1283 | .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match), |
Benjamin Tissoires | 3d7d248 | 2013-06-13 09:50:35 +0200 | [diff] [blame] | 1284 | .of_match_table = of_match_ptr(i2c_hid_of_match), |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1285 | }, |
| 1286 | |
| 1287 | .probe = i2c_hid_probe, |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 1288 | .remove = i2c_hid_remove, |
Guohua Zhong | d9f448e3 | 2016-06-21 18:27:45 +0800 | [diff] [blame] | 1289 | .shutdown = i2c_hid_shutdown, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1290 | .id_table = i2c_hid_id_table, |
| 1291 | }; |
| 1292 | |
| 1293 | module_i2c_driver(i2c_hid_driver); |
| 1294 | |
| 1295 | MODULE_DESCRIPTION("HID over I2C core driver"); |
| 1296 | MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); |
| 1297 | MODULE_LICENSE("GPL"); |