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