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