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