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