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