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 | fa73864 | 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); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 286 | |
Benjamin Tissoires | 5318251 | 2014-02-10 12:58:59 -0500 | [diff] [blame] | 287 | /* hid_hw_* already checked that data_len < HID_MAX_BUFFER_SIZE */ |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 288 | u16 size = 2 /* size */ + |
| 289 | (reportID ? 1 : 0) /* reportID */ + |
| 290 | data_len /* buf */; |
| 291 | int args_len = (reportID >= 0x0F ? 1 : 0) /* optional third byte */ + |
| 292 | 2 /* dataRegister */ + |
| 293 | size /* args */; |
| 294 | int index = 0; |
| 295 | |
| 296 | i2c_hid_dbg(ihid, "%s\n", __func__); |
| 297 | |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 298 | if (!use_data && maxOutputLength == 0) |
| 299 | return -ENOSYS; |
| 300 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 301 | if (reportID >= 0x0F) { |
| 302 | args[index++] = reportID; |
| 303 | reportID = 0x0F; |
| 304 | } |
| 305 | |
Andrew Duggan | 811adb9 | 2013-06-17 16:15:06 -0700 | [diff] [blame] | 306 | /* |
| 307 | * use the data register for feature reports or if the device does not |
| 308 | * support the output register |
| 309 | */ |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 310 | if (use_data) { |
Andrew Duggan | 811adb9 | 2013-06-17 16:15:06 -0700 | [diff] [blame] | 311 | args[index++] = dataRegister & 0xFF; |
| 312 | args[index++] = dataRegister >> 8; |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 313 | hidcmd = &hid_set_report_cmd; |
Andrew Duggan | 811adb9 | 2013-06-17 16:15:06 -0700 | [diff] [blame] | 314 | } else { |
| 315 | args[index++] = outputRegister & 0xFF; |
| 316 | args[index++] = outputRegister >> 8; |
| 317 | hidcmd = &hid_no_cmd; |
| 318 | } |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 319 | |
| 320 | args[index++] = size & 0xFF; |
| 321 | args[index++] = size >> 8; |
| 322 | |
| 323 | if (reportID) |
| 324 | args[index++] = reportID; |
| 325 | |
| 326 | memcpy(&args[index], buf, data_len); |
| 327 | |
Andrew Duggan | 811adb9 | 2013-06-17 16:15:06 -0700 | [diff] [blame] | 328 | ret = __i2c_hid_command(client, hidcmd, reportID, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 329 | reportType, args, args_len, NULL, 0); |
| 330 | if (ret) { |
| 331 | dev_err(&client->dev, "failed to set a report to device.\n"); |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 332 | return ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | return data_len; |
| 336 | } |
| 337 | |
| 338 | static int i2c_hid_set_power(struct i2c_client *client, int power_state) |
| 339 | { |
| 340 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 341 | int ret; |
| 342 | |
| 343 | i2c_hid_dbg(ihid, "%s\n", __func__); |
| 344 | |
| 345 | ret = __i2c_hid_command(client, &hid_set_power_cmd, power_state, |
| 346 | 0, NULL, 0, NULL, 0); |
| 347 | if (ret) |
| 348 | dev_err(&client->dev, "failed to change power setting.\n"); |
| 349 | |
| 350 | return ret; |
| 351 | } |
| 352 | |
| 353 | static int i2c_hid_hwreset(struct i2c_client *client) |
| 354 | { |
| 355 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 356 | int ret; |
| 357 | |
| 358 | i2c_hid_dbg(ihid, "%s\n", __func__); |
| 359 | |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 360 | /* |
| 361 | * This prevents sending feature reports while the device is |
| 362 | * being reset. Otherwise we may lose the reset complete |
| 363 | * interrupt. |
| 364 | */ |
| 365 | mutex_lock(&ihid->reset_lock); |
| 366 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 367 | ret = i2c_hid_set_power(client, I2C_HID_PWR_ON); |
| 368 | if (ret) |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 369 | goto out_unlock; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 370 | |
| 371 | i2c_hid_dbg(ihid, "resetting...\n"); |
| 372 | |
| 373 | ret = i2c_hid_command(client, &hid_reset_cmd, NULL, 0); |
| 374 | if (ret) { |
| 375 | dev_err(&client->dev, "failed to reset device.\n"); |
| 376 | i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 377 | } |
| 378 | |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 379 | out_unlock: |
| 380 | mutex_unlock(&ihid->reset_lock); |
| 381 | return ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 384 | static void i2c_hid_get_input(struct i2c_hid *ihid) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 385 | { |
| 386 | int ret, ret_size; |
Seth Forshee | 6d00f37 | 2015-02-20 11:45:11 -0600 | [diff] [blame] | 387 | int size = le16_to_cpu(ihid->hdesc.wMaxInputLength); |
| 388 | |
| 389 | if (size > ihid->bufsize) |
| 390 | size = ihid->bufsize; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 391 | |
| 392 | ret = i2c_master_recv(ihid->client, ihid->inbuf, size); |
| 393 | if (ret != size) { |
| 394 | if (ret < 0) |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 395 | return; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 396 | |
| 397 | dev_err(&ihid->client->dev, "%s: got %d data instead of %d\n", |
| 398 | __func__, ret, size); |
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 | |
| 402 | ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8; |
| 403 | |
| 404 | if (!ret_size) { |
| 405 | /* host or device initiated RESET completed */ |
| 406 | if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags)) |
| 407 | wake_up(&ihid->wait); |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 408 | return; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | if (ret_size > size) { |
| 412 | dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n", |
| 413 | __func__, size, ret_size); |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 414 | return; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | i2c_hid_dbg(ihid, "input: %*ph\n", ret_size, ihid->inbuf); |
| 418 | |
| 419 | if (test_bit(I2C_HID_STARTED, &ihid->flags)) |
| 420 | hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2, |
| 421 | ret_size - 2, 1); |
| 422 | |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 423 | return; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | static irqreturn_t i2c_hid_irq(int irq, void *dev_id) |
| 427 | { |
| 428 | struct i2c_hid *ihid = dev_id; |
| 429 | |
| 430 | if (test_bit(I2C_HID_READ_PENDING, &ihid->flags)) |
| 431 | return IRQ_HANDLED; |
| 432 | |
| 433 | i2c_hid_get_input(ihid); |
| 434 | |
| 435 | return IRQ_HANDLED; |
| 436 | } |
| 437 | |
| 438 | static int i2c_hid_get_report_length(struct hid_report *report) |
| 439 | { |
| 440 | return ((report->size - 1) >> 3) + 1 + |
| 441 | report->device->report_enum[report->type].numbered + 2; |
| 442 | } |
| 443 | |
| 444 | static void i2c_hid_init_report(struct hid_report *report, u8 *buffer, |
| 445 | size_t bufsize) |
| 446 | { |
| 447 | struct hid_device *hid = report->device; |
| 448 | struct i2c_client *client = hid->driver_data; |
| 449 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 450 | unsigned int size, ret_size; |
| 451 | |
| 452 | size = i2c_hid_get_report_length(report); |
Benjamin Tissoires | c737bcf | 2012-12-04 16:27:50 +0100 | [diff] [blame] | 453 | if (i2c_hid_get_report(client, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 454 | report->type == HID_FEATURE_REPORT ? 0x03 : 0x01, |
Benjamin Tissoires | c737bcf | 2012-12-04 16:27:50 +0100 | [diff] [blame] | 455 | report->id, buffer, size)) |
| 456 | return; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 457 | |
Jean-Baptiste Maneyrol | 08bb7be | 2014-11-16 22:45:43 +0800 | [diff] [blame] | 458 | i2c_hid_dbg(ihid, "report (len=%d): %*ph\n", size, size, buffer); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 459 | |
| 460 | ret_size = buffer[0] | (buffer[1] << 8); |
| 461 | |
| 462 | if (ret_size != size) { |
| 463 | dev_err(&client->dev, "error in %s size:%d / ret_size:%d\n", |
| 464 | __func__, size, ret_size); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | /* hid->driver_lock is held as we are in probe function, |
| 469 | * we just need to setup the input fields, so using |
| 470 | * hid_report_raw_event is safe. */ |
| 471 | hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1); |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * Initialize all reports |
| 476 | */ |
| 477 | static void i2c_hid_init_reports(struct hid_device *hid) |
| 478 | { |
| 479 | struct hid_report *report; |
| 480 | struct i2c_client *client = hid->driver_data; |
| 481 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 482 | u8 *inbuf = kzalloc(ihid->bufsize, GFP_KERNEL); |
| 483 | |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 484 | if (!inbuf) { |
| 485 | dev_err(&client->dev, "can not retrieve initial reports\n"); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 486 | return; |
Benjamin Tissoires | 317b204 | 2012-12-04 16:27:48 +0100 | [diff] [blame] | 487 | } |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 488 | |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 489 | /* |
| 490 | * The device must be powered on while we fetch initial reports |
| 491 | * from it. |
| 492 | */ |
| 493 | pm_runtime_get_sync(&client->dev); |
| 494 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 495 | list_for_each_entry(report, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 496 | &hid->report_enum[HID_FEATURE_REPORT].report_list, list) |
| 497 | i2c_hid_init_report(report, inbuf, ihid->bufsize); |
| 498 | |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 499 | pm_runtime_put(&client->dev); |
| 500 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 501 | kfree(inbuf); |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | * Traverse the supplied list of reports and find the longest |
| 506 | */ |
| 507 | static void i2c_hid_find_max_report(struct hid_device *hid, unsigned int type, |
| 508 | unsigned int *max) |
| 509 | { |
| 510 | struct hid_report *report; |
| 511 | unsigned int size; |
| 512 | |
| 513 | /* We should not rely on wMaxInputLength, as some devices may set it to |
| 514 | * a wrong length. */ |
| 515 | list_for_each_entry(report, &hid->report_enum[type].report_list, list) { |
| 516 | size = i2c_hid_get_report_length(report); |
| 517 | if (*max < size) |
| 518 | *max = size; |
| 519 | } |
| 520 | } |
| 521 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 522 | static void i2c_hid_free_buffers(struct i2c_hid *ihid) |
| 523 | { |
| 524 | kfree(ihid->inbuf); |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 525 | kfree(ihid->rawbuf); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 526 | kfree(ihid->argsbuf); |
| 527 | kfree(ihid->cmdbuf); |
| 528 | ihid->inbuf = NULL; |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 529 | ihid->rawbuf = NULL; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 530 | ihid->cmdbuf = NULL; |
| 531 | ihid->argsbuf = NULL; |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 532 | ihid->bufsize = 0; |
| 533 | } |
| 534 | |
| 535 | static int i2c_hid_alloc_buffers(struct i2c_hid *ihid, size_t report_size) |
| 536 | { |
| 537 | /* the worst case is computed from the set_report command with a |
| 538 | * reportID > 15 and the maximum report length */ |
| 539 | int args_len = sizeof(__u8) + /* optional ReportID byte */ |
| 540 | sizeof(__u16) + /* data register */ |
| 541 | sizeof(__u16) + /* size of the report */ |
| 542 | report_size; /* report */ |
| 543 | |
| 544 | ihid->inbuf = kzalloc(report_size, GFP_KERNEL); |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 545 | ihid->rawbuf = kzalloc(report_size, GFP_KERNEL); |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 546 | ihid->argsbuf = kzalloc(args_len, GFP_KERNEL); |
| 547 | ihid->cmdbuf = kzalloc(sizeof(union command) + args_len, GFP_KERNEL); |
| 548 | |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 549 | if (!ihid->inbuf || !ihid->rawbuf || !ihid->argsbuf || !ihid->cmdbuf) { |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 550 | i2c_hid_free_buffers(ihid); |
| 551 | return -ENOMEM; |
| 552 | } |
| 553 | |
| 554 | ihid->bufsize = report_size; |
| 555 | |
| 556 | return 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | static int i2c_hid_get_raw_report(struct hid_device *hid, |
| 560 | unsigned char report_number, __u8 *buf, size_t count, |
| 561 | unsigned char report_type) |
| 562 | { |
| 563 | struct i2c_client *client = hid->driver_data; |
| 564 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
Benjamin Tissoires | e5b50fe | 2012-12-05 15:02:56 +0100 | [diff] [blame] | 565 | size_t ret_count, ask_count; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 566 | int ret; |
| 567 | |
| 568 | if (report_type == HID_OUTPUT_REPORT) |
| 569 | return -EINVAL; |
| 570 | |
Benjamin Tissoires | e5b50fe | 2012-12-05 15:02:56 +0100 | [diff] [blame] | 571 | /* +2 bytes to include the size of the reply in the query buffer */ |
| 572 | ask_count = min(count + 2, (size_t)ihid->bufsize); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 573 | |
| 574 | ret = i2c_hid_get_report(client, |
| 575 | report_type == HID_FEATURE_REPORT ? 0x03 : 0x01, |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 576 | report_number, ihid->rawbuf, ask_count); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 577 | |
| 578 | if (ret < 0) |
| 579 | return ret; |
| 580 | |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 581 | ret_count = ihid->rawbuf[0] | (ihid->rawbuf[1] << 8); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 582 | |
Jiri Kosina | 9afd09a | 2012-12-06 10:59:28 +0100 | [diff] [blame] | 583 | if (ret_count <= 2) |
Benjamin Tissoires | e5b50fe | 2012-12-05 15:02:56 +0100 | [diff] [blame] | 584 | return 0; |
| 585 | |
| 586 | ret_count = min(ret_count, ask_count); |
| 587 | |
| 588 | /* The query buffer contains the size, dropping it in the reply */ |
| 589 | count = min(count, ret_count - 2); |
Jean-Baptiste Maneyrol | 6296f4a | 2014-11-20 00:46:37 +0800 | [diff] [blame] | 590 | memcpy(buf, ihid->rawbuf + 2, count); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 591 | |
| 592 | return count; |
| 593 | } |
| 594 | |
| 595 | 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] | 596 | size_t count, unsigned char report_type, bool use_data) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 597 | { |
| 598 | struct i2c_client *client = hid->driver_data; |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 599 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 600 | int report_id = buf[0]; |
Benjamin Tissoires | c284979 | 2013-01-31 17:50:02 +0100 | [diff] [blame] | 601 | int ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 602 | |
| 603 | if (report_type == HID_INPUT_REPORT) |
| 604 | return -EINVAL; |
| 605 | |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 606 | mutex_lock(&ihid->reset_lock); |
| 607 | |
Benjamin Tissoires | c284979 | 2013-01-31 17:50:02 +0100 | [diff] [blame] | 608 | if (report_id) { |
| 609 | buf++; |
| 610 | count--; |
| 611 | } |
| 612 | |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 613 | ret = i2c_hid_set_or_send_report(client, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 614 | report_type == HID_FEATURE_REPORT ? 0x03 : 0x02, |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 615 | report_id, buf, count, use_data); |
Benjamin Tissoires | c284979 | 2013-01-31 17:50:02 +0100 | [diff] [blame] | 616 | |
| 617 | if (report_id && ret >= 0) |
| 618 | ret++; /* add report_id to the number of transfered bytes */ |
| 619 | |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 620 | mutex_unlock(&ihid->reset_lock); |
| 621 | |
Benjamin Tissoires | c284979 | 2013-01-31 17:50:02 +0100 | [diff] [blame] | 622 | return ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 623 | } |
| 624 | |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 625 | static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf, |
| 626 | size_t count) |
Benjamin Tissoires | 545bef6 | 2013-02-25 11:31:50 +0100 | [diff] [blame] | 627 | { |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 628 | return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT, |
| 629 | false); |
| 630 | } |
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 | static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum, |
| 633 | __u8 *buf, size_t len, unsigned char rtype, |
| 634 | int reqtype) |
| 635 | { |
Benjamin Tissoires | 545bef6 | 2013-02-25 11:31:50 +0100 | [diff] [blame] | 636 | switch (reqtype) { |
| 637 | case HID_REQ_GET_REPORT: |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 638 | return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype); |
Benjamin Tissoires | 545bef6 | 2013-02-25 11:31:50 +0100 | [diff] [blame] | 639 | case HID_REQ_SET_REPORT: |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 640 | if (buf[0] != reportnum) |
| 641 | return -EINVAL; |
| 642 | return i2c_hid_output_raw_report(hid, buf, len, rtype, true); |
| 643 | default: |
| 644 | return -EIO; |
Benjamin Tissoires | 545bef6 | 2013-02-25 11:31:50 +0100 | [diff] [blame] | 645 | } |
Benjamin Tissoires | 545bef6 | 2013-02-25 11:31:50 +0100 | [diff] [blame] | 646 | } |
| 647 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 648 | static int i2c_hid_parse(struct hid_device *hid) |
| 649 | { |
| 650 | struct i2c_client *client = hid->driver_data; |
| 651 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 652 | struct i2c_hid_desc *hdesc = &ihid->hdesc; |
| 653 | unsigned int rsize; |
| 654 | char *rdesc; |
| 655 | int ret; |
| 656 | int tries = 3; |
| 657 | |
| 658 | i2c_hid_dbg(ihid, "entering %s\n", __func__); |
| 659 | |
| 660 | rsize = le16_to_cpu(hdesc->wReportDescLength); |
| 661 | if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) { |
| 662 | dbg_hid("weird size of report descriptor (%u)\n", rsize); |
| 663 | return -EINVAL; |
| 664 | } |
| 665 | |
| 666 | do { |
| 667 | ret = i2c_hid_hwreset(client); |
| 668 | if (ret) |
| 669 | msleep(1000); |
| 670 | } while (tries-- > 0 && ret); |
| 671 | |
| 672 | if (ret) |
| 673 | return ret; |
| 674 | |
| 675 | rdesc = kzalloc(rsize, GFP_KERNEL); |
| 676 | |
| 677 | if (!rdesc) { |
| 678 | dbg_hid("couldn't allocate rdesc memory\n"); |
| 679 | return -ENOMEM; |
| 680 | } |
| 681 | |
| 682 | i2c_hid_dbg(ihid, "asking HID report descriptor\n"); |
| 683 | |
| 684 | ret = i2c_hid_command(client, &hid_report_descr_cmd, rdesc, rsize); |
| 685 | if (ret) { |
| 686 | hid_err(hid, "reading report descriptor failed\n"); |
| 687 | kfree(rdesc); |
| 688 | return -EIO; |
| 689 | } |
| 690 | |
| 691 | i2c_hid_dbg(ihid, "Report Descriptor: %*ph\n", rsize, rdesc); |
| 692 | |
| 693 | ret = hid_parse_report(hid, rdesc, rsize); |
| 694 | kfree(rdesc); |
| 695 | if (ret) { |
| 696 | dbg_hid("parsing report descriptor failed\n"); |
| 697 | return ret; |
| 698 | } |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | static int i2c_hid_start(struct hid_device *hid) |
| 704 | { |
| 705 | struct i2c_client *client = hid->driver_data; |
| 706 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 707 | int ret; |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 708 | unsigned int bufsize = HID_MIN_BUFFER_SIZE; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 709 | |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 710 | i2c_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize); |
| 711 | i2c_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize); |
| 712 | i2c_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize); |
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 | if (bufsize > ihid->bufsize) { |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 715 | i2c_hid_free_buffers(ihid); |
| 716 | |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 717 | ret = i2c_hid_alloc_buffers(ihid, bufsize); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 718 | |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 719 | if (ret) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 720 | return ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS)) |
| 724 | i2c_hid_init_reports(hid); |
| 725 | |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | static void i2c_hid_stop(struct hid_device *hid) |
| 730 | { |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 731 | hid->claimed = 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | static int i2c_hid_open(struct hid_device *hid) |
| 735 | { |
| 736 | struct i2c_client *client = hid->driver_data; |
| 737 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
Benjamin Tissoires | 7a7d6d9 | 2012-12-12 18:00:02 +0100 | [diff] [blame] | 738 | int ret = 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 739 | |
Benjamin Tissoires | 7a7d6d9 | 2012-12-12 18:00:02 +0100 | [diff] [blame] | 740 | mutex_lock(&i2c_hid_open_mut); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 741 | if (!hid->open++) { |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 742 | ret = pm_runtime_get_sync(&client->dev); |
| 743 | if (ret < 0) { |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 744 | hid->open--; |
Benjamin Tissoires | 7a7d6d9 | 2012-12-12 18:00:02 +0100 | [diff] [blame] | 745 | goto done; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 746 | } |
| 747 | set_bit(I2C_HID_STARTED, &ihid->flags); |
| 748 | } |
Benjamin Tissoires | 7a7d6d9 | 2012-12-12 18:00:02 +0100 | [diff] [blame] | 749 | done: |
| 750 | mutex_unlock(&i2c_hid_open_mut); |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 751 | return ret < 0 ? ret : 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | static void i2c_hid_close(struct hid_device *hid) |
| 755 | { |
| 756 | struct i2c_client *client = hid->driver_data; |
| 757 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 758 | |
| 759 | /* protecting hid->open to make sure we don't restart |
| 760 | * data acquistion due to a resumption we no longer |
| 761 | * care about |
| 762 | */ |
Benjamin Tissoires | 7a7d6d9 | 2012-12-12 18:00:02 +0100 | [diff] [blame] | 763 | mutex_lock(&i2c_hid_open_mut); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 764 | if (!--hid->open) { |
| 765 | clear_bit(I2C_HID_STARTED, &ihid->flags); |
| 766 | |
| 767 | /* Save some power */ |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 768 | pm_runtime_put(&client->dev); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 769 | } |
Benjamin Tissoires | 7a7d6d9 | 2012-12-12 18:00:02 +0100 | [diff] [blame] | 770 | mutex_unlock(&i2c_hid_open_mut); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | static int i2c_hid_power(struct hid_device *hid, int lvl) |
| 774 | { |
| 775 | struct i2c_client *client = hid->driver_data; |
| 776 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 777 | |
| 778 | i2c_hid_dbg(ihid, "%s lvl:%d\n", __func__, lvl); |
| 779 | |
| 780 | switch (lvl) { |
| 781 | case PM_HINT_FULLON: |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 782 | pm_runtime_get_sync(&client->dev); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 783 | break; |
| 784 | case PM_HINT_NORMAL: |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 785 | pm_runtime_put(&client->dev); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 786 | break; |
| 787 | } |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 788 | return 0; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 789 | } |
| 790 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 791 | static struct hid_ll_driver i2c_hid_ll_driver = { |
| 792 | .parse = i2c_hid_parse, |
| 793 | .start = i2c_hid_start, |
| 794 | .stop = i2c_hid_stop, |
| 795 | .open = i2c_hid_open, |
| 796 | .close = i2c_hid_close, |
| 797 | .power = i2c_hid_power, |
Benjamin Tissoires | 9b5a9ae | 2014-02-10 12:58:49 -0500 | [diff] [blame] | 798 | .output_report = i2c_hid_output_report, |
| 799 | .raw_request = i2c_hid_raw_request, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 800 | }; |
| 801 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 802 | static int i2c_hid_init_irq(struct i2c_client *client) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 803 | { |
| 804 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 805 | int ret; |
| 806 | |
Mika Westerberg | a485923 | 2015-02-23 15:52:45 +0200 | [diff] [blame] | 807 | dev_dbg(&client->dev, "Requesting IRQ: %d\n", ihid->irq); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 808 | |
Mika Westerberg | a485923 | 2015-02-23 15:52:45 +0200 | [diff] [blame] | 809 | ret = request_threaded_irq(ihid->irq, NULL, i2c_hid_irq, |
Mika Westerberg | f2de746 | 2015-01-26 16:29:32 +0200 | [diff] [blame] | 810 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 811 | client->name, ihid); |
| 812 | if (ret < 0) { |
Benjamin Tissoires | 9972dcc | 2012-12-04 16:27:49 +0100 | [diff] [blame] | 813 | dev_warn(&client->dev, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 814 | "Could not register for %s interrupt, irq = %d," |
| 815 | " ret = %d\n", |
Mika Westerberg | a485923 | 2015-02-23 15:52:45 +0200 | [diff] [blame] | 816 | client->name, ihid->irq, ret); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 817 | |
| 818 | return ret; |
| 819 | } |
| 820 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 821 | return 0; |
| 822 | } |
| 823 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 824 | static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 825 | { |
| 826 | struct i2c_client *client = ihid->client; |
| 827 | struct i2c_hid_desc *hdesc = &ihid->hdesc; |
| 828 | unsigned int dsize; |
| 829 | int ret; |
| 830 | |
Archana Patni | f58b848 | 2014-05-08 09:26:19 -0400 | [diff] [blame] | 831 | /* i2c hid fetch using a fixed descriptor size (30 bytes) */ |
| 832 | i2c_hid_dbg(ihid, "Fetching the HID descriptor\n"); |
| 833 | ret = i2c_hid_command(client, &hid_descr_cmd, ihid->hdesc_buffer, |
| 834 | sizeof(struct i2c_hid_desc)); |
| 835 | if (ret) { |
| 836 | dev_err(&client->dev, "hid_descr_cmd failed\n"); |
| 837 | return -ENODEV; |
| 838 | } |
| 839 | |
| 840 | /* Validate the length of HID descriptor, the 4 first bytes: |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 841 | * bytes 0-1 -> length |
| 842 | * bytes 2-3 -> bcdVersion (has to be 1.00) */ |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 843 | /* check bcdVersion == 1.0 */ |
| 844 | if (le16_to_cpu(hdesc->bcdVersion) != 0x0100) { |
| 845 | dev_err(&client->dev, |
Benjamin Tissoires | 9972dcc | 2012-12-04 16:27:49 +0100 | [diff] [blame] | 846 | "unexpected HID descriptor bcdVersion (0x%04hx)\n", |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 847 | le16_to_cpu(hdesc->bcdVersion)); |
| 848 | return -ENODEV; |
| 849 | } |
| 850 | |
Archana Patni | f58b848 | 2014-05-08 09:26:19 -0400 | [diff] [blame] | 851 | /* Descriptor length should be 30 bytes as per the specification */ |
| 852 | dsize = le16_to_cpu(hdesc->wHIDDescLength); |
| 853 | if (dsize != sizeof(struct i2c_hid_desc)) { |
| 854 | dev_err(&client->dev, "weird size of HID descriptor (%u)\n", |
| 855 | dsize); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 856 | return -ENODEV; |
| 857 | } |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 858 | i2c_hid_dbg(ihid, "HID Descriptor: %*ph\n", dsize, ihid->hdesc_buffer); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 859 | return 0; |
| 860 | } |
| 861 | |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 862 | #ifdef CONFIG_ACPI |
Mika Westerberg | a485923 | 2015-02-23 15:52:45 +0200 | [diff] [blame] | 863 | |
| 864 | /* Default GPIO mapping */ |
| 865 | static const struct acpi_gpio_params i2c_hid_irq_gpio = { 0, 0, true }; |
| 866 | static const struct acpi_gpio_mapping i2c_hid_acpi_gpios[] = { |
| 867 | { "gpios", &i2c_hid_irq_gpio, 1 }, |
| 868 | { }, |
| 869 | }; |
| 870 | |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 871 | static int i2c_hid_acpi_pdata(struct i2c_client *client, |
| 872 | struct i2c_hid_platform_data *pdata) |
| 873 | { |
| 874 | static u8 i2c_hid_guid[] = { |
| 875 | 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45, |
| 876 | 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE, |
| 877 | }; |
Jiang Liu | ea547d7 | 2013-12-19 20:38:19 +0800 | [diff] [blame] | 878 | union acpi_object *obj; |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 879 | struct acpi_device *adev; |
| 880 | acpi_handle handle; |
Mika Westerberg | a7d2bf2 | 2015-04-14 17:35:12 +0300 | [diff] [blame] | 881 | int ret; |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 882 | |
| 883 | handle = ACPI_HANDLE(&client->dev); |
| 884 | if (!handle || acpi_bus_get_device(handle, &adev)) |
| 885 | return -ENODEV; |
| 886 | |
Jiang Liu | ea547d7 | 2013-12-19 20:38:19 +0800 | [diff] [blame] | 887 | obj = acpi_evaluate_dsm_typed(handle, i2c_hid_guid, 1, 1, NULL, |
| 888 | ACPI_TYPE_INTEGER); |
| 889 | if (!obj) { |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 890 | dev_err(&client->dev, "device _DSM execution failed\n"); |
| 891 | return -ENODEV; |
| 892 | } |
| 893 | |
Jiang Liu | ea547d7 | 2013-12-19 20:38:19 +0800 | [diff] [blame] | 894 | pdata->hid_descriptor_address = obj->integer.value; |
| 895 | ACPI_FREE(obj); |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 896 | |
Mika Westerberg | a7d2bf2 | 2015-04-14 17:35:12 +0300 | [diff] [blame] | 897 | /* GPIOs are optional */ |
| 898 | ret = acpi_dev_add_driver_gpios(adev, i2c_hid_acpi_gpios); |
| 899 | return ret < 0 && ret != -ENXIO ? ret : 0; |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | static const struct acpi_device_id i2c_hid_acpi_match[] = { |
| 903 | {"ACPI0C50", 0 }, |
| 904 | {"PNP0C50", 0 }, |
| 905 | { }, |
| 906 | }; |
| 907 | MODULE_DEVICE_TABLE(acpi, i2c_hid_acpi_match); |
| 908 | #else |
| 909 | static inline int i2c_hid_acpi_pdata(struct i2c_client *client, |
| 910 | struct i2c_hid_platform_data *pdata) |
| 911 | { |
| 912 | return -ENODEV; |
| 913 | } |
| 914 | #endif |
| 915 | |
Benjamin Tissoires | 3d7d248 | 2013-06-13 09:50:35 +0200 | [diff] [blame] | 916 | #ifdef CONFIG_OF |
| 917 | static int i2c_hid_of_probe(struct i2c_client *client, |
| 918 | struct i2c_hid_platform_data *pdata) |
| 919 | { |
| 920 | struct device *dev = &client->dev; |
| 921 | u32 val; |
| 922 | int ret; |
| 923 | |
| 924 | ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val); |
| 925 | if (ret) { |
| 926 | dev_err(&client->dev, "HID register address not provided\n"); |
| 927 | return -ENODEV; |
| 928 | } |
| 929 | if (val >> 16) { |
| 930 | dev_err(&client->dev, "Bad HID register address: 0x%08x\n", |
| 931 | val); |
| 932 | return -EINVAL; |
| 933 | } |
| 934 | pdata->hid_descriptor_address = val; |
| 935 | |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | static const struct of_device_id i2c_hid_of_match[] = { |
| 940 | { .compatible = "hid-over-i2c" }, |
| 941 | {}, |
| 942 | }; |
| 943 | MODULE_DEVICE_TABLE(of, i2c_hid_of_match); |
| 944 | #else |
| 945 | static inline int i2c_hid_of_probe(struct i2c_client *client, |
| 946 | struct i2c_hid_platform_data *pdata) |
| 947 | { |
| 948 | return -ENODEV; |
| 949 | } |
| 950 | #endif |
| 951 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 952 | static int i2c_hid_probe(struct i2c_client *client, |
| 953 | const struct i2c_device_id *dev_id) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 954 | { |
| 955 | int ret; |
| 956 | struct i2c_hid *ihid; |
| 957 | struct hid_device *hid; |
| 958 | __u16 hidRegister; |
| 959 | struct i2c_hid_platform_data *platform_data = client->dev.platform_data; |
| 960 | |
| 961 | dbg_hid("HID probe called for i2c 0x%02x\n", client->addr); |
| 962 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 963 | ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL); |
| 964 | if (!ihid) |
| 965 | return -ENOMEM; |
| 966 | |
Benjamin Tissoires | 3d7d248 | 2013-06-13 09:50:35 +0200 | [diff] [blame] | 967 | if (client->dev.of_node) { |
| 968 | ret = i2c_hid_of_probe(client, &ihid->pdata); |
| 969 | if (ret) |
| 970 | goto err; |
| 971 | } else if (!platform_data) { |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 972 | ret = i2c_hid_acpi_pdata(client, &ihid->pdata); |
| 973 | if (ret) { |
| 974 | dev_err(&client->dev, |
| 975 | "HID register address not provided\n"); |
| 976 | goto err; |
| 977 | } |
| 978 | } else { |
| 979 | ihid->pdata = *platform_data; |
| 980 | } |
| 981 | |
Mika Westerberg | a485923 | 2015-02-23 15:52:45 +0200 | [diff] [blame] | 982 | if (client->irq > 0) { |
| 983 | ihid->irq = client->irq; |
| 984 | } else if (ACPI_COMPANION(&client->dev)) { |
| 985 | ihid->desc = gpiod_get(&client->dev, NULL, GPIOD_IN); |
| 986 | if (IS_ERR(ihid->desc)) { |
| 987 | dev_err(&client->dev, "Failed to get GPIO interrupt\n"); |
| 988 | return PTR_ERR(ihid->desc); |
| 989 | } |
| 990 | |
| 991 | ihid->irq = gpiod_to_irq(ihid->desc); |
| 992 | if (ihid->irq < 0) { |
| 993 | gpiod_put(ihid->desc); |
| 994 | dev_err(&client->dev, "Failed to convert GPIO to IRQ\n"); |
| 995 | return ihid->irq; |
| 996 | } |
| 997 | } |
| 998 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 999 | i2c_set_clientdata(client, ihid); |
| 1000 | |
| 1001 | ihid->client = client; |
| 1002 | |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 1003 | hidRegister = ihid->pdata.hid_descriptor_address; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1004 | ihid->wHIDDescRegister = cpu_to_le16(hidRegister); |
| 1005 | |
| 1006 | init_waitqueue_head(&ihid->wait); |
Mika Westerberg | 9a32740 | 2015-12-21 15:26:31 +0200 | [diff] [blame] | 1007 | mutex_init(&ihid->reset_lock); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1008 | |
| 1009 | /* we need to allocate the command buffer without knowing the maximum |
| 1010 | * size of the reports. Let's use HID_MIN_BUFFER_SIZE, then we do the |
| 1011 | * real computation later. */ |
Benjamin Tissoires | 29b4578 | 2012-12-05 15:02:54 +0100 | [diff] [blame] | 1012 | ret = i2c_hid_alloc_buffers(ihid, HID_MIN_BUFFER_SIZE); |
| 1013 | if (ret < 0) |
| 1014 | goto err; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1015 | |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1016 | pm_runtime_get_noresume(&client->dev); |
| 1017 | pm_runtime_set_active(&client->dev); |
| 1018 | pm_runtime_enable(&client->dev); |
| 1019 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1020 | ret = i2c_hid_fetch_hid_descriptor(ihid); |
| 1021 | if (ret < 0) |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1022 | goto err_pm; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1023 | |
| 1024 | ret = i2c_hid_init_irq(client); |
| 1025 | if (ret < 0) |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1026 | goto err_pm; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1027 | |
| 1028 | hid = hid_allocate_device(); |
| 1029 | if (IS_ERR(hid)) { |
| 1030 | ret = PTR_ERR(hid); |
Benjamin Tissoires | 8a1bbb5 | 2012-12-05 15:02:55 +0100 | [diff] [blame] | 1031 | goto err_irq; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | ihid->hid = hid; |
| 1035 | |
| 1036 | hid->driver_data = client; |
| 1037 | hid->ll_driver = &i2c_hid_ll_driver; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1038 | hid->dev.parent = &client->dev; |
| 1039 | hid->bus = BUS_I2C; |
| 1040 | hid->version = le16_to_cpu(ihid->hdesc.bcdVersion); |
| 1041 | hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID); |
| 1042 | hid->product = le16_to_cpu(ihid->hdesc.wProductID); |
| 1043 | |
Benjamin Tissoires | 9972dcc | 2012-12-04 16:27:49 +0100 | [diff] [blame] | 1044 | snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX", |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1045 | client->name, hid->vendor, hid->product); |
Mika Westerberg | f3984ed | 2015-09-28 15:15:08 +0300 | [diff] [blame] | 1046 | strlcpy(hid->phys, dev_name(&client->dev), sizeof(hid->phys)); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1047 | |
| 1048 | ret = hid_add_device(hid); |
| 1049 | if (ret) { |
| 1050 | if (ret != -ENODEV) |
| 1051 | hid_err(client, "can't add hid device: %d\n", ret); |
| 1052 | goto err_mem_free; |
| 1053 | } |
| 1054 | |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1055 | pm_runtime_put(&client->dev); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1056 | return 0; |
| 1057 | |
| 1058 | err_mem_free: |
| 1059 | hid_destroy_device(hid); |
| 1060 | |
Benjamin Tissoires | 8a1bbb5 | 2012-12-05 15:02:55 +0100 | [diff] [blame] | 1061 | err_irq: |
Mika Westerberg | a485923 | 2015-02-23 15:52:45 +0200 | [diff] [blame] | 1062 | free_irq(ihid->irq, ihid); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1063 | |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1064 | err_pm: |
| 1065 | pm_runtime_put_noidle(&client->dev); |
| 1066 | pm_runtime_disable(&client->dev); |
| 1067 | |
Benjamin Tissoires | 8a1bbb5 | 2012-12-05 15:02:55 +0100 | [diff] [blame] | 1068 | err: |
Mika Westerberg | a485923 | 2015-02-23 15:52:45 +0200 | [diff] [blame] | 1069 | if (ihid->desc) |
| 1070 | gpiod_put(ihid->desc); |
| 1071 | |
Jiri Kosina | 3c62602 | 2012-11-20 17:09:40 +0100 | [diff] [blame] | 1072 | i2c_hid_free_buffers(ihid); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1073 | kfree(ihid); |
| 1074 | return ret; |
| 1075 | } |
| 1076 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 1077 | static int i2c_hid_remove(struct i2c_client *client) |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1078 | { |
| 1079 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 1080 | struct hid_device *hid; |
| 1081 | |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1082 | pm_runtime_get_sync(&client->dev); |
| 1083 | pm_runtime_disable(&client->dev); |
| 1084 | pm_runtime_set_suspended(&client->dev); |
| 1085 | pm_runtime_put_noidle(&client->dev); |
| 1086 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1087 | hid = ihid->hid; |
| 1088 | hid_destroy_device(hid); |
| 1089 | |
Mika Westerberg | a485923 | 2015-02-23 15:52:45 +0200 | [diff] [blame] | 1090 | free_irq(ihid->irq, ihid); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1091 | |
Benjamin Tissoires | 134ebfd | 2012-12-04 16:27:54 +0100 | [diff] [blame] | 1092 | if (ihid->bufsize) |
| 1093 | i2c_hid_free_buffers(ihid); |
| 1094 | |
Mika Westerberg | a485923 | 2015-02-23 15:52:45 +0200 | [diff] [blame] | 1095 | if (ihid->desc) |
| 1096 | gpiod_put(ihid->desc); |
| 1097 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1098 | kfree(ihid); |
| 1099 | |
Mika Westerberg | a485923 | 2015-02-23 15:52:45 +0200 | [diff] [blame] | 1100 | acpi_dev_remove_driver_gpios(ACPI_COMPANION(&client->dev)); |
| 1101 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | #ifdef CONFIG_PM_SLEEP |
| 1106 | static int i2c_hid_suspend(struct device *dev) |
| 1107 | { |
| 1108 | struct i2c_client *client = to_i2c_client(dev); |
Andrew Duggan | 109571c | 2014-07-11 16:34:18 -0700 | [diff] [blame] | 1109 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 1110 | struct hid_device *hid = ihid->hid; |
| 1111 | int ret = 0; |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 1112 | int wake_status; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1113 | |
Gabriele Mazzotta | af4739c | 2015-07-07 21:58:02 +0200 | [diff] [blame] | 1114 | if (hid->driver && hid->driver->suspend) |
| 1115 | ret = hid->driver->suspend(hid, PMSG_SUSPEND); |
| 1116 | |
Mika Westerberg | a485923 | 2015-02-23 15:52:45 +0200 | [diff] [blame] | 1117 | disable_irq(ihid->irq); |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 1118 | if (device_may_wakeup(&client->dev)) { |
| 1119 | wake_status = enable_irq_wake(ihid->irq); |
| 1120 | if (!wake_status) |
| 1121 | ihid->irq_wake_enabled = true; |
| 1122 | else |
| 1123 | hid_warn(hid, "Failed to enable irq wake: %d\n", |
| 1124 | wake_status); |
| 1125 | } |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1126 | |
| 1127 | /* Save some power */ |
| 1128 | i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); |
| 1129 | |
Andrew Duggan | 109571c | 2014-07-11 16:34:18 -0700 | [diff] [blame] | 1130 | return ret; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | static int i2c_hid_resume(struct device *dev) |
| 1134 | { |
| 1135 | int ret; |
| 1136 | struct i2c_client *client = to_i2c_client(dev); |
Andrew Duggan | 109571c | 2014-07-11 16:34:18 -0700 | [diff] [blame] | 1137 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
| 1138 | struct hid_device *hid = ihid->hid; |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 1139 | int wake_status; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1140 | |
Mika Westerberg | a485923 | 2015-02-23 15:52:45 +0200 | [diff] [blame] | 1141 | enable_irq(ihid->irq); |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1142 | ret = i2c_hid_hwreset(client); |
| 1143 | if (ret) |
| 1144 | return ret; |
| 1145 | |
Andrew Duggan | d1c4803 | 2015-07-30 14:49:00 -0700 | [diff] [blame] | 1146 | if (device_may_wakeup(&client->dev) && ihid->irq_wake_enabled) { |
| 1147 | wake_status = disable_irq_wake(ihid->irq); |
| 1148 | if (!wake_status) |
| 1149 | ihid->irq_wake_enabled = false; |
| 1150 | else |
| 1151 | hid_warn(hid, "Failed to disable irq wake: %d\n", |
| 1152 | wake_status); |
| 1153 | } |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1154 | |
Andrew Duggan | 109571c | 2014-07-11 16:34:18 -0700 | [diff] [blame] | 1155 | if (hid->driver && hid->driver->reset_resume) { |
| 1156 | ret = hid->driver->reset_resume(hid); |
| 1157 | return ret; |
| 1158 | } |
| 1159 | |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1160 | return 0; |
| 1161 | } |
| 1162 | #endif |
| 1163 | |
Rafael J. Wysocki | 721564a | 2014-12-04 01:05:44 +0100 | [diff] [blame] | 1164 | #ifdef CONFIG_PM |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1165 | static int i2c_hid_runtime_suspend(struct device *dev) |
| 1166 | { |
| 1167 | struct i2c_client *client = to_i2c_client(dev); |
Mika Westerberg | a485923 | 2015-02-23 15:52:45 +0200 | [diff] [blame] | 1168 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1169 | |
| 1170 | i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); |
Mika Westerberg | a485923 | 2015-02-23 15:52:45 +0200 | [diff] [blame] | 1171 | disable_irq(ihid->irq); |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1172 | return 0; |
| 1173 | } |
| 1174 | |
| 1175 | static int i2c_hid_runtime_resume(struct device *dev) |
| 1176 | { |
| 1177 | struct i2c_client *client = to_i2c_client(dev); |
Mika Westerberg | a485923 | 2015-02-23 15:52:45 +0200 | [diff] [blame] | 1178 | struct i2c_hid *ihid = i2c_get_clientdata(client); |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1179 | |
Mika Westerberg | a485923 | 2015-02-23 15:52:45 +0200 | [diff] [blame] | 1180 | enable_irq(ihid->irq); |
Mika Westerberg | 34f439e | 2014-01-29 11:24:36 +0200 | [diff] [blame] | 1181 | i2c_hid_set_power(client, I2C_HID_PWR_ON); |
| 1182 | return 0; |
| 1183 | } |
| 1184 | #endif |
| 1185 | |
| 1186 | static const struct dev_pm_ops i2c_hid_pm = { |
| 1187 | SET_SYSTEM_SLEEP_PM_OPS(i2c_hid_suspend, i2c_hid_resume) |
| 1188 | SET_RUNTIME_PM_OPS(i2c_hid_runtime_suspend, i2c_hid_runtime_resume, |
| 1189 | NULL) |
| 1190 | }; |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1191 | |
| 1192 | static const struct i2c_device_id i2c_hid_id_table[] = { |
Benjamin Tissoires | 24ebb37 | 2012-12-04 16:27:42 +0100 | [diff] [blame] | 1193 | { "hid", 0 }, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1194 | { }, |
| 1195 | }; |
| 1196 | MODULE_DEVICE_TABLE(i2c, i2c_hid_id_table); |
| 1197 | |
| 1198 | |
| 1199 | static struct i2c_driver i2c_hid_driver = { |
| 1200 | .driver = { |
| 1201 | .name = "i2c_hid", |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1202 | .pm = &i2c_hid_pm, |
Mika Westerberg | 92241e6 | 2013-01-09 16:43:08 +0200 | [diff] [blame] | 1203 | .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match), |
Benjamin Tissoires | 3d7d248 | 2013-06-13 09:50:35 +0200 | [diff] [blame] | 1204 | .of_match_table = of_match_ptr(i2c_hid_of_match), |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1205 | }, |
| 1206 | |
| 1207 | .probe = i2c_hid_probe, |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 1208 | .remove = i2c_hid_remove, |
Benjamin Tissoires | 4a200c3 | 2012-11-12 15:42:59 +0100 | [diff] [blame] | 1209 | |
| 1210 | .id_table = i2c_hid_id_table, |
| 1211 | }; |
| 1212 | |
| 1213 | module_i2c_driver(i2c_hid_driver); |
| 1214 | |
| 1215 | MODULE_DESCRIPTION("HID over I2C core driver"); |
| 1216 | MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); |
| 1217 | MODULE_LICENSE("GPL"); |