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