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