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