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