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