1 /* 2 * hid-cp2112.c - Silicon Labs HID USB to SMBus master bridge 3 * Copyright (c) 2013,2014 Uplogix, Inc. 4 * David Barksdale <dbarksdale@uplogix.com> 5 * 6 * This program is free software; you can redistribute it and/or modify it 7 * under the terms and conditions of the GNU General Public License, 8 * version 2, as published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 * more details. 14 */ 15 16 /* 17 * The Silicon Labs CP2112 chip is a USB HID device which provides an 18 * SMBus controller for talking to slave devices and 8 GPIO pins. The 19 * host communicates with the CP2112 via raw HID reports. 20 * 21 * Data Sheet: 22 * http://www.silabs.com/Support%20Documents/TechnicalDocs/CP2112.pdf 23 * Programming Interface Specification: 24 * http://www.silabs.com/Support%20Documents/TechnicalDocs/AN495.pdf 25 */ 26 27 #include <linux/gpio.h> 28 #include <linux/gpio/driver.h> 29 #include <linux/hid.h> 30 #include <linux/i2c.h> 31 #include <linux/module.h> 32 #include <linux/nls.h> 33 #include <linux/usb/ch9.h> 34 #include "hid-ids.h" 35 36 #define CP2112_REPORT_MAX_LENGTH 64 37 #define CP2112_GPIO_CONFIG_LENGTH 5 38 #define CP2112_GPIO_GET_LENGTH 2 39 #define CP2112_GPIO_SET_LENGTH 3 40 41 enum { 42 CP2112_GPIO_CONFIG = 0x02, 43 CP2112_GPIO_GET = 0x03, 44 CP2112_GPIO_SET = 0x04, 45 CP2112_GET_VERSION_INFO = 0x05, 46 CP2112_SMBUS_CONFIG = 0x06, 47 CP2112_DATA_READ_REQUEST = 0x10, 48 CP2112_DATA_WRITE_READ_REQUEST = 0x11, 49 CP2112_DATA_READ_FORCE_SEND = 0x12, 50 CP2112_DATA_READ_RESPONSE = 0x13, 51 CP2112_DATA_WRITE_REQUEST = 0x14, 52 CP2112_TRANSFER_STATUS_REQUEST = 0x15, 53 CP2112_TRANSFER_STATUS_RESPONSE = 0x16, 54 CP2112_CANCEL_TRANSFER = 0x17, 55 CP2112_LOCK_BYTE = 0x20, 56 CP2112_USB_CONFIG = 0x21, 57 CP2112_MANUFACTURER_STRING = 0x22, 58 CP2112_PRODUCT_STRING = 0x23, 59 CP2112_SERIAL_STRING = 0x24, 60 }; 61 62 enum { 63 STATUS0_IDLE = 0x00, 64 STATUS0_BUSY = 0x01, 65 STATUS0_COMPLETE = 0x02, 66 STATUS0_ERROR = 0x03, 67 }; 68 69 enum { 70 STATUS1_TIMEOUT_NACK = 0x00, 71 STATUS1_TIMEOUT_BUS = 0x01, 72 STATUS1_ARBITRATION_LOST = 0x02, 73 STATUS1_READ_INCOMPLETE = 0x03, 74 STATUS1_WRITE_INCOMPLETE = 0x04, 75 STATUS1_SUCCESS = 0x05, 76 }; 77 78 struct cp2112_smbus_config_report { 79 u8 report; /* CP2112_SMBUS_CONFIG */ 80 __be32 clock_speed; /* Hz */ 81 u8 device_address; /* Stored in the upper 7 bits */ 82 u8 auto_send_read; /* 1 = enabled, 0 = disabled */ 83 __be16 write_timeout; /* ms, 0 = no timeout */ 84 __be16 read_timeout; /* ms, 0 = no timeout */ 85 u8 scl_low_timeout; /* 1 = enabled, 0 = disabled */ 86 __be16 retry_time; /* # of retries, 0 = no limit */ 87 } __packed; 88 89 struct cp2112_usb_config_report { 90 u8 report; /* CP2112_USB_CONFIG */ 91 __le16 vid; /* Vendor ID */ 92 __le16 pid; /* Product ID */ 93 u8 max_power; /* Power requested in 2mA units */ 94 u8 power_mode; /* 0x00 = bus powered 95 0x01 = self powered & regulator off 96 0x02 = self powered & regulator on */ 97 u8 release_major; 98 u8 release_minor; 99 u8 mask; /* What fields to program */ 100 } __packed; 101 102 struct cp2112_read_req_report { 103 u8 report; /* CP2112_DATA_READ_REQUEST */ 104 u8 slave_address; 105 __be16 length; 106 } __packed; 107 108 struct cp2112_write_read_req_report { 109 u8 report; /* CP2112_DATA_WRITE_READ_REQUEST */ 110 u8 slave_address; 111 __be16 length; 112 u8 target_address_length; 113 u8 target_address[16]; 114 } __packed; 115 116 struct cp2112_write_req_report { 117 u8 report; /* CP2112_DATA_WRITE_REQUEST */ 118 u8 slave_address; 119 u8 length; 120 u8 data[61]; 121 } __packed; 122 123 struct cp2112_force_read_report { 124 u8 report; /* CP2112_DATA_READ_FORCE_SEND */ 125 __be16 length; 126 } __packed; 127 128 struct cp2112_xfer_status_report { 129 u8 report; /* CP2112_TRANSFER_STATUS_RESPONSE */ 130 u8 status0; /* STATUS0_* */ 131 u8 status1; /* STATUS1_* */ 132 __be16 retries; 133 __be16 length; 134 } __packed; 135 136 struct cp2112_string_report { 137 u8 dummy; /* force .string to be aligned */ 138 u8 report; /* CP2112_*_STRING */ 139 u8 length; /* length in bytes of everyting after .report */ 140 u8 type; /* USB_DT_STRING */ 141 wchar_t string[30]; /* UTF16_LITTLE_ENDIAN string */ 142 } __packed; 143 144 /* Number of times to request transfer status before giving up waiting for a 145 transfer to complete. This may need to be changed if SMBUS clock, retries, 146 or read/write/scl_low timeout settings are changed. */ 147 static const int XFER_STATUS_RETRIES = 10; 148 149 /* Time in ms to wait for a CP2112_DATA_READ_RESPONSE or 150 CP2112_TRANSFER_STATUS_RESPONSE. */ 151 static const int RESPONSE_TIMEOUT = 50; 152 153 static const struct hid_device_id cp2112_devices[] = { 154 { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_CP2112) }, 155 { } 156 }; 157 MODULE_DEVICE_TABLE(hid, cp2112_devices); 158 159 struct cp2112_device { 160 struct i2c_adapter adap; 161 struct hid_device *hdev; 162 wait_queue_head_t wait; 163 u8 read_data[61]; 164 u8 read_length; 165 u8 hwversion; 166 int xfer_status; 167 atomic_t read_avail; 168 atomic_t xfer_avail; 169 struct gpio_chip gc; 170 u8 *in_out_buffer; 171 struct mutex lock; 172 173 struct gpio_desc *desc[8]; 174 bool gpio_poll; 175 struct delayed_work gpio_poll_worker; 176 unsigned long irq_mask; 177 u8 gpio_prev_state; 178 }; 179 180 static int gpio_push_pull = 0xFF; 181 module_param(gpio_push_pull, int, S_IRUGO | S_IWUSR); 182 MODULE_PARM_DESC(gpio_push_pull, "GPIO push-pull configuration bitmask"); 183 184 static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset) 185 { 186 struct cp2112_device *dev = gpiochip_get_data(chip); 187 struct hid_device *hdev = dev->hdev; 188 u8 *buf = dev->in_out_buffer; 189 int ret; 190 191 mutex_lock(&dev->lock); 192 193 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, 194 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT, 195 HID_REQ_GET_REPORT); 196 if (ret != CP2112_GPIO_CONFIG_LENGTH) { 197 hid_err(hdev, "error requesting GPIO config: %d\n", ret); 198 goto exit; 199 } 200 201 buf[1] &= ~(1 << offset); 202 buf[2] = gpio_push_pull; 203 204 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, 205 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT, 206 HID_REQ_SET_REPORT); 207 if (ret < 0) { 208 hid_err(hdev, "error setting GPIO config: %d\n", ret); 209 goto exit; 210 } 211 212 ret = 0; 213 214 exit: 215 mutex_unlock(&dev->lock); 216 return ret < 0 ? ret : -EIO; 217 } 218 219 static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value) 220 { 221 struct cp2112_device *dev = gpiochip_get_data(chip); 222 struct hid_device *hdev = dev->hdev; 223 u8 *buf = dev->in_out_buffer; 224 int ret; 225 226 mutex_lock(&dev->lock); 227 228 buf[0] = CP2112_GPIO_SET; 229 buf[1] = value ? 0xff : 0; 230 buf[2] = 1 << offset; 231 232 ret = hid_hw_raw_request(hdev, CP2112_GPIO_SET, buf, 233 CP2112_GPIO_SET_LENGTH, HID_FEATURE_REPORT, 234 HID_REQ_SET_REPORT); 235 if (ret < 0) 236 hid_err(hdev, "error setting GPIO values: %d\n", ret); 237 238 mutex_unlock(&dev->lock); 239 } 240 241 static int cp2112_gpio_get_all(struct gpio_chip *chip) 242 { 243 struct cp2112_device *dev = gpiochip_get_data(chip); 244 struct hid_device *hdev = dev->hdev; 245 u8 *buf = dev->in_out_buffer; 246 int ret; 247 248 mutex_lock(&dev->lock); 249 250 ret = hid_hw_raw_request(hdev, CP2112_GPIO_GET, buf, 251 CP2112_GPIO_GET_LENGTH, HID_FEATURE_REPORT, 252 HID_REQ_GET_REPORT); 253 if (ret != CP2112_GPIO_GET_LENGTH) { 254 hid_err(hdev, "error requesting GPIO values: %d\n", ret); 255 ret = ret < 0 ? ret : -EIO; 256 goto exit; 257 } 258 259 ret = buf[1]; 260 261 exit: 262 mutex_unlock(&dev->lock); 263 264 return ret; 265 } 266 267 static int cp2112_gpio_get(struct gpio_chip *chip, unsigned int offset) 268 { 269 int ret; 270 271 ret = cp2112_gpio_get_all(chip); 272 if (ret < 0) 273 return ret; 274 275 return (ret >> offset) & 1; 276 } 277 278 static int cp2112_gpio_direction_output(struct gpio_chip *chip, 279 unsigned offset, int value) 280 { 281 struct cp2112_device *dev = gpiochip_get_data(chip); 282 struct hid_device *hdev = dev->hdev; 283 u8 *buf = dev->in_out_buffer; 284 int ret; 285 286 mutex_lock(&dev->lock); 287 288 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, 289 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT, 290 HID_REQ_GET_REPORT); 291 if (ret != CP2112_GPIO_CONFIG_LENGTH) { 292 hid_err(hdev, "error requesting GPIO config: %d\n", ret); 293 goto fail; 294 } 295 296 buf[1] |= 1 << offset; 297 buf[2] = gpio_push_pull; 298 299 ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, 300 CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT, 301 HID_REQ_SET_REPORT); 302 if (ret < 0) { 303 hid_err(hdev, "error setting GPIO config: %d\n", ret); 304 goto fail; 305 } 306 307 mutex_unlock(&dev->lock); 308 309 /* 310 * Set gpio value when output direction is already set, 311 * as specified in AN495, Rev. 0.2, cpt. 4.4 312 */ 313 cp2112_gpio_set(chip, offset, value); 314 315 return 0; 316 317 fail: 318 mutex_unlock(&dev->lock); 319 return ret < 0 ? ret : -EIO; 320 } 321 322 static int cp2112_hid_get(struct hid_device *hdev, unsigned char report_number, 323 u8 *data, size_t count, unsigned char report_type) 324 { 325 u8 *buf; 326 int ret; 327 328 buf = kmalloc(count, GFP_KERNEL); 329 if (!buf) 330 return -ENOMEM; 331 332 ret = hid_hw_raw_request(hdev, report_number, buf, count, 333 report_type, HID_REQ_GET_REPORT); 334 memcpy(data, buf, count); 335 kfree(buf); 336 return ret; 337 } 338 339 static int cp2112_hid_output(struct hid_device *hdev, u8 *data, size_t count, 340 unsigned char report_type) 341 { 342 u8 *buf; 343 int ret; 344 345 buf = kmemdup(data, count, GFP_KERNEL); 346 if (!buf) 347 return -ENOMEM; 348 349 if (report_type == HID_OUTPUT_REPORT) 350 ret = hid_hw_output_report(hdev, buf, count); 351 else 352 ret = hid_hw_raw_request(hdev, buf[0], buf, count, report_type, 353 HID_REQ_SET_REPORT); 354 355 kfree(buf); 356 return ret; 357 } 358 359 static int cp2112_wait(struct cp2112_device *dev, atomic_t *avail) 360 { 361 int ret = 0; 362 363 /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a 364 * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to 365 * come in cp2112_raw_event or timeout. There will only be one of these 366 * in flight at any one time. The timeout is extremely large and is a 367 * last resort if the CP2112 has died. If we do timeout we don't expect 368 * to receive the response which would cause data races, it's not like 369 * we can do anything about it anyway. 370 */ 371 ret = wait_event_interruptible_timeout(dev->wait, 372 atomic_read(avail), msecs_to_jiffies(RESPONSE_TIMEOUT)); 373 if (-ERESTARTSYS == ret) 374 return ret; 375 if (!ret) 376 return -ETIMEDOUT; 377 378 atomic_set(avail, 0); 379 return 0; 380 } 381 382 static int cp2112_xfer_status(struct cp2112_device *dev) 383 { 384 struct hid_device *hdev = dev->hdev; 385 u8 buf[2]; 386 int ret; 387 388 buf[0] = CP2112_TRANSFER_STATUS_REQUEST; 389 buf[1] = 0x01; 390 atomic_set(&dev->xfer_avail, 0); 391 392 ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT); 393 if (ret < 0) { 394 hid_warn(hdev, "Error requesting status: %d\n", ret); 395 return ret; 396 } 397 398 ret = cp2112_wait(dev, &dev->xfer_avail); 399 if (ret) 400 return ret; 401 402 return dev->xfer_status; 403 } 404 405 static int cp2112_read(struct cp2112_device *dev, u8 *data, size_t size) 406 { 407 struct hid_device *hdev = dev->hdev; 408 struct cp2112_force_read_report report; 409 int ret; 410 411 if (size > sizeof(dev->read_data)) 412 size = sizeof(dev->read_data); 413 report.report = CP2112_DATA_READ_FORCE_SEND; 414 report.length = cpu_to_be16(size); 415 416 atomic_set(&dev->read_avail, 0); 417 418 ret = cp2112_hid_output(hdev, &report.report, sizeof(report), 419 HID_OUTPUT_REPORT); 420 if (ret < 0) { 421 hid_warn(hdev, "Error requesting data: %d\n", ret); 422 return ret; 423 } 424 425 ret = cp2112_wait(dev, &dev->read_avail); 426 if (ret) 427 return ret; 428 429 hid_dbg(hdev, "read %d of %zd bytes requested\n", 430 dev->read_length, size); 431 432 if (size > dev->read_length) 433 size = dev->read_length; 434 435 memcpy(data, dev->read_data, size); 436 return dev->read_length; 437 } 438 439 static int cp2112_read_req(void *buf, u8 slave_address, u16 length) 440 { 441 struct cp2112_read_req_report *report = buf; 442 443 if (length < 1 || length > 512) 444 return -EINVAL; 445 446 report->report = CP2112_DATA_READ_REQUEST; 447 report->slave_address = slave_address << 1; 448 report->length = cpu_to_be16(length); 449 return sizeof(*report); 450 } 451 452 static int cp2112_write_read_req(void *buf, u8 slave_address, u16 length, 453 u8 command, u8 *data, u8 data_length) 454 { 455 struct cp2112_write_read_req_report *report = buf; 456 457 if (length < 1 || length > 512 458 || data_length > sizeof(report->target_address) - 1) 459 return -EINVAL; 460 461 report->report = CP2112_DATA_WRITE_READ_REQUEST; 462 report->slave_address = slave_address << 1; 463 report->length = cpu_to_be16(length); 464 report->target_address_length = data_length + 1; 465 report->target_address[0] = command; 466 memcpy(&report->target_address[1], data, data_length); 467 return data_length + 6; 468 } 469 470 static int cp2112_write_req(void *buf, u8 slave_address, u8 command, u8 *data, 471 u8 data_length) 472 { 473 struct cp2112_write_req_report *report = buf; 474 475 if (data_length > sizeof(report->data) - 1) 476 return -EINVAL; 477 478 report->report = CP2112_DATA_WRITE_REQUEST; 479 report->slave_address = slave_address << 1; 480 report->length = data_length + 1; 481 report->data[0] = command; 482 memcpy(&report->data[1], data, data_length); 483 return data_length + 4; 484 } 485 486 static int cp2112_i2c_write_req(void *buf, u8 slave_address, u8 *data, 487 u8 data_length) 488 { 489 struct cp2112_write_req_report *report = buf; 490 491 if (data_length > sizeof(report->data)) 492 return -EINVAL; 493 494 report->report = CP2112_DATA_WRITE_REQUEST; 495 report->slave_address = slave_address << 1; 496 report->length = data_length; 497 memcpy(report->data, data, data_length); 498 return data_length + 3; 499 } 500 501 static int cp2112_i2c_write_read_req(void *buf, u8 slave_address, 502 u8 *addr, int addr_length, 503 int read_length) 504 { 505 struct cp2112_write_read_req_report *report = buf; 506 507 if (read_length < 1 || read_length > 512 || 508 addr_length > sizeof(report->target_address)) 509 return -EINVAL; 510 511 report->report = CP2112_DATA_WRITE_READ_REQUEST; 512 report->slave_address = slave_address << 1; 513 report->length = cpu_to_be16(read_length); 514 report->target_address_length = addr_length; 515 memcpy(report->target_address, addr, addr_length); 516 return addr_length + 5; 517 } 518 519 static int cp2112_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, 520 int num) 521 { 522 struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data; 523 struct hid_device *hdev = dev->hdev; 524 u8 buf[64]; 525 ssize_t count; 526 ssize_t read_length = 0; 527 u8 *read_buf = NULL; 528 unsigned int retries; 529 int ret; 530 531 hid_dbg(hdev, "I2C %d messages\n", num); 532 533 if (num == 1) { 534 if (msgs->flags & I2C_M_RD) { 535 hid_dbg(hdev, "I2C read %#04x len %d\n", 536 msgs->addr, msgs->len); 537 read_length = msgs->len; 538 read_buf = msgs->buf; 539 count = cp2112_read_req(buf, msgs->addr, msgs->len); 540 } else { 541 hid_dbg(hdev, "I2C write %#04x len %d\n", 542 msgs->addr, msgs->len); 543 count = cp2112_i2c_write_req(buf, msgs->addr, 544 msgs->buf, msgs->len); 545 } 546 if (count < 0) 547 return count; 548 } else if (dev->hwversion > 1 && /* no repeated start in rev 1 */ 549 num == 2 && 550 msgs[0].addr == msgs[1].addr && 551 !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) { 552 hid_dbg(hdev, "I2C write-read %#04x wlen %d rlen %d\n", 553 msgs[0].addr, msgs[0].len, msgs[1].len); 554 read_length = msgs[1].len; 555 read_buf = msgs[1].buf; 556 count = cp2112_i2c_write_read_req(buf, msgs[0].addr, 557 msgs[0].buf, msgs[0].len, msgs[1].len); 558 if (count < 0) 559 return count; 560 } else { 561 hid_err(hdev, 562 "Multi-message I2C transactions not supported\n"); 563 return -EOPNOTSUPP; 564 } 565 566 ret = hid_hw_power(hdev, PM_HINT_FULLON); 567 if (ret < 0) { 568 hid_err(hdev, "power management error: %d\n", ret); 569 return ret; 570 } 571 572 ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT); 573 if (ret < 0) { 574 hid_warn(hdev, "Error starting transaction: %d\n", ret); 575 goto power_normal; 576 } 577 578 for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) { 579 ret = cp2112_xfer_status(dev); 580 if (-EBUSY == ret) 581 continue; 582 if (ret < 0) 583 goto power_normal; 584 break; 585 } 586 587 if (XFER_STATUS_RETRIES <= retries) { 588 hid_warn(hdev, "Transfer timed out, cancelling.\n"); 589 buf[0] = CP2112_CANCEL_TRANSFER; 590 buf[1] = 0x01; 591 592 ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT); 593 if (ret < 0) 594 hid_warn(hdev, "Error cancelling transaction: %d\n", 595 ret); 596 597 ret = -ETIMEDOUT; 598 goto power_normal; 599 } 600 601 for (count = 0; count < read_length;) { 602 ret = cp2112_read(dev, read_buf + count, read_length - count); 603 if (ret < 0) 604 goto power_normal; 605 if (ret == 0) { 606 hid_err(hdev, "read returned 0\n"); 607 ret = -EIO; 608 goto power_normal; 609 } 610 count += ret; 611 if (count > read_length) { 612 /* 613 * The hardware returned too much data. 614 * This is mostly harmless because cp2112_read() 615 * has a limit check so didn't overrun our 616 * buffer. Nevertheless, we return an error 617 * because something is seriously wrong and 618 * it shouldn't go unnoticed. 619 */ 620 hid_err(hdev, "long read: %d > %zd\n", 621 ret, read_length - count + ret); 622 ret = -EIO; 623 goto power_normal; 624 } 625 } 626 627 /* return the number of transferred messages */ 628 ret = num; 629 630 power_normal: 631 hid_hw_power(hdev, PM_HINT_NORMAL); 632 hid_dbg(hdev, "I2C transfer finished: %d\n", ret); 633 return ret; 634 } 635 636 static int cp2112_xfer(struct i2c_adapter *adap, u16 addr, 637 unsigned short flags, char read_write, u8 command, 638 int size, union i2c_smbus_data *data) 639 { 640 struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data; 641 struct hid_device *hdev = dev->hdev; 642 u8 buf[64]; 643 __le16 word; 644 ssize_t count; 645 size_t read_length = 0; 646 unsigned int retries; 647 int ret; 648 649 hid_dbg(hdev, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n", 650 read_write == I2C_SMBUS_WRITE ? "write" : "read", 651 addr, flags, command, size); 652 653 switch (size) { 654 case I2C_SMBUS_BYTE: 655 read_length = 1; 656 657 if (I2C_SMBUS_READ == read_write) 658 count = cp2112_read_req(buf, addr, read_length); 659 else 660 count = cp2112_write_req(buf, addr, command, NULL, 661 0); 662 break; 663 case I2C_SMBUS_BYTE_DATA: 664 read_length = 1; 665 666 if (I2C_SMBUS_READ == read_write) 667 count = cp2112_write_read_req(buf, addr, read_length, 668 command, NULL, 0); 669 else 670 count = cp2112_write_req(buf, addr, command, 671 &data->byte, 1); 672 break; 673 case I2C_SMBUS_WORD_DATA: 674 read_length = 2; 675 word = cpu_to_le16(data->word); 676 677 if (I2C_SMBUS_READ == read_write) 678 count = cp2112_write_read_req(buf, addr, read_length, 679 command, NULL, 0); 680 else 681 count = cp2112_write_req(buf, addr, command, 682 (u8 *)&word, 2); 683 break; 684 case I2C_SMBUS_PROC_CALL: 685 size = I2C_SMBUS_WORD_DATA; 686 read_write = I2C_SMBUS_READ; 687 read_length = 2; 688 word = cpu_to_le16(data->word); 689 690 count = cp2112_write_read_req(buf, addr, read_length, command, 691 (u8 *)&word, 2); 692 break; 693 case I2C_SMBUS_I2C_BLOCK_DATA: 694 size = I2C_SMBUS_BLOCK_DATA; 695 /* fallthrough */ 696 case I2C_SMBUS_BLOCK_DATA: 697 if (I2C_SMBUS_READ == read_write) { 698 count = cp2112_write_read_req(buf, addr, 699 I2C_SMBUS_BLOCK_MAX, 700 command, NULL, 0); 701 } else { 702 count = cp2112_write_req(buf, addr, command, 703 data->block, 704 data->block[0] + 1); 705 } 706 break; 707 case I2C_SMBUS_BLOCK_PROC_CALL: 708 size = I2C_SMBUS_BLOCK_DATA; 709 read_write = I2C_SMBUS_READ; 710 711 count = cp2112_write_read_req(buf, addr, I2C_SMBUS_BLOCK_MAX, 712 command, data->block, 713 data->block[0] + 1); 714 break; 715 default: 716 hid_warn(hdev, "Unsupported transaction %d\n", size); 717 return -EOPNOTSUPP; 718 } 719 720 if (count < 0) 721 return count; 722 723 ret = hid_hw_power(hdev, PM_HINT_FULLON); 724 if (ret < 0) { 725 hid_err(hdev, "power management error: %d\n", ret); 726 return ret; 727 } 728 729 ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT); 730 if (ret < 0) { 731 hid_warn(hdev, "Error starting transaction: %d\n", ret); 732 goto power_normal; 733 } 734 735 for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) { 736 ret = cp2112_xfer_status(dev); 737 if (-EBUSY == ret) 738 continue; 739 if (ret < 0) 740 goto power_normal; 741 break; 742 } 743 744 if (XFER_STATUS_RETRIES <= retries) { 745 hid_warn(hdev, "Transfer timed out, cancelling.\n"); 746 buf[0] = CP2112_CANCEL_TRANSFER; 747 buf[1] = 0x01; 748 749 ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT); 750 if (ret < 0) 751 hid_warn(hdev, "Error cancelling transaction: %d\n", 752 ret); 753 754 ret = -ETIMEDOUT; 755 goto power_normal; 756 } 757 758 if (I2C_SMBUS_WRITE == read_write) { 759 ret = 0; 760 goto power_normal; 761 } 762 763 if (I2C_SMBUS_BLOCK_DATA == size) 764 read_length = ret; 765 766 ret = cp2112_read(dev, buf, read_length); 767 if (ret < 0) 768 goto power_normal; 769 if (ret != read_length) { 770 hid_warn(hdev, "short read: %d < %zd\n", ret, read_length); 771 ret = -EIO; 772 goto power_normal; 773 } 774 775 switch (size) { 776 case I2C_SMBUS_BYTE: 777 case I2C_SMBUS_BYTE_DATA: 778 data->byte = buf[0]; 779 break; 780 case I2C_SMBUS_WORD_DATA: 781 data->word = le16_to_cpup((__le16 *)buf); 782 break; 783 case I2C_SMBUS_BLOCK_DATA: 784 if (read_length > I2C_SMBUS_BLOCK_MAX) { 785 ret = -EPROTO; 786 goto power_normal; 787 } 788 789 memcpy(data->block, buf, read_length); 790 break; 791 } 792 793 ret = 0; 794 power_normal: 795 hid_hw_power(hdev, PM_HINT_NORMAL); 796 hid_dbg(hdev, "transfer finished: %d\n", ret); 797 return ret; 798 } 799 800 static u32 cp2112_functionality(struct i2c_adapter *adap) 801 { 802 return I2C_FUNC_I2C | 803 I2C_FUNC_SMBUS_BYTE | 804 I2C_FUNC_SMBUS_BYTE_DATA | 805 I2C_FUNC_SMBUS_WORD_DATA | 806 I2C_FUNC_SMBUS_BLOCK_DATA | 807 I2C_FUNC_SMBUS_I2C_BLOCK | 808 I2C_FUNC_SMBUS_PROC_CALL | 809 I2C_FUNC_SMBUS_BLOCK_PROC_CALL; 810 } 811 812 static const struct i2c_algorithm smbus_algorithm = { 813 .master_xfer = cp2112_i2c_xfer, 814 .smbus_xfer = cp2112_xfer, 815 .functionality = cp2112_functionality, 816 }; 817 818 static int cp2112_get_usb_config(struct hid_device *hdev, 819 struct cp2112_usb_config_report *cfg) 820 { 821 int ret; 822 823 ret = cp2112_hid_get(hdev, CP2112_USB_CONFIG, (u8 *)cfg, sizeof(*cfg), 824 HID_FEATURE_REPORT); 825 if (ret != sizeof(*cfg)) { 826 hid_err(hdev, "error reading usb config: %d\n", ret); 827 if (ret < 0) 828 return ret; 829 return -EIO; 830 } 831 832 return 0; 833 } 834 835 static int cp2112_set_usb_config(struct hid_device *hdev, 836 struct cp2112_usb_config_report *cfg) 837 { 838 int ret; 839 840 BUG_ON(cfg->report != CP2112_USB_CONFIG); 841 842 ret = cp2112_hid_output(hdev, (u8 *)cfg, sizeof(*cfg), 843 HID_FEATURE_REPORT); 844 if (ret != sizeof(*cfg)) { 845 hid_err(hdev, "error writing usb config: %d\n", ret); 846 if (ret < 0) 847 return ret; 848 return -EIO; 849 } 850 851 return 0; 852 } 853 854 static void chmod_sysfs_attrs(struct hid_device *hdev); 855 856 #define CP2112_CONFIG_ATTR(name, store, format, ...) \ 857 static ssize_t name##_store(struct device *kdev, \ 858 struct device_attribute *attr, const char *buf, \ 859 size_t count) \ 860 { \ 861 struct hid_device *hdev = to_hid_device(kdev); \ 862 struct cp2112_usb_config_report cfg; \ 863 int ret = cp2112_get_usb_config(hdev, &cfg); \ 864 if (ret) \ 865 return ret; \ 866 store; \ 867 ret = cp2112_set_usb_config(hdev, &cfg); \ 868 if (ret) \ 869 return ret; \ 870 chmod_sysfs_attrs(hdev); \ 871 return count; \ 872 } \ 873 static ssize_t name##_show(struct device *kdev, \ 874 struct device_attribute *attr, char *buf) \ 875 { \ 876 struct hid_device *hdev = to_hid_device(kdev); \ 877 struct cp2112_usb_config_report cfg; \ 878 int ret = cp2112_get_usb_config(hdev, &cfg); \ 879 if (ret) \ 880 return ret; \ 881 return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \ 882 } \ 883 static DEVICE_ATTR_RW(name); 884 885 CP2112_CONFIG_ATTR(vendor_id, ({ 886 u16 vid; 887 888 if (sscanf(buf, "%hi", &vid) != 1) 889 return -EINVAL; 890 891 cfg.vid = cpu_to_le16(vid); 892 cfg.mask = 0x01; 893 }), "0x%04x\n", le16_to_cpu(cfg.vid)); 894 895 CP2112_CONFIG_ATTR(product_id, ({ 896 u16 pid; 897 898 if (sscanf(buf, "%hi", &pid) != 1) 899 return -EINVAL; 900 901 cfg.pid = cpu_to_le16(pid); 902 cfg.mask = 0x02; 903 }), "0x%04x\n", le16_to_cpu(cfg.pid)); 904 905 CP2112_CONFIG_ATTR(max_power, ({ 906 int mA; 907 908 if (sscanf(buf, "%i", &mA) != 1) 909 return -EINVAL; 910 911 cfg.max_power = (mA + 1) / 2; 912 cfg.mask = 0x04; 913 }), "%u mA\n", cfg.max_power * 2); 914 915 CP2112_CONFIG_ATTR(power_mode, ({ 916 if (sscanf(buf, "%hhi", &cfg.power_mode) != 1) 917 return -EINVAL; 918 919 cfg.mask = 0x08; 920 }), "%u\n", cfg.power_mode); 921 922 CP2112_CONFIG_ATTR(release_version, ({ 923 if (sscanf(buf, "%hhi.%hhi", &cfg.release_major, &cfg.release_minor) 924 != 2) 925 return -EINVAL; 926 927 cfg.mask = 0x10; 928 }), "%u.%u\n", cfg.release_major, cfg.release_minor); 929 930 #undef CP2112_CONFIG_ATTR 931 932 struct cp2112_pstring_attribute { 933 struct device_attribute attr; 934 unsigned char report; 935 }; 936 937 static ssize_t pstr_store(struct device *kdev, 938 struct device_attribute *kattr, const char *buf, 939 size_t count) 940 { 941 struct hid_device *hdev = to_hid_device(kdev); 942 struct cp2112_pstring_attribute *attr = 943 container_of(kattr, struct cp2112_pstring_attribute, attr); 944 struct cp2112_string_report report; 945 int ret; 946 947 memset(&report, 0, sizeof(report)); 948 949 ret = utf8s_to_utf16s(buf, count, UTF16_LITTLE_ENDIAN, 950 report.string, ARRAY_SIZE(report.string)); 951 report.report = attr->report; 952 report.length = ret * sizeof(report.string[0]) + 2; 953 report.type = USB_DT_STRING; 954 955 ret = cp2112_hid_output(hdev, &report.report, report.length + 1, 956 HID_FEATURE_REPORT); 957 if (ret != report.length + 1) { 958 hid_err(hdev, "error writing %s string: %d\n", kattr->attr.name, 959 ret); 960 if (ret < 0) 961 return ret; 962 return -EIO; 963 } 964 965 chmod_sysfs_attrs(hdev); 966 return count; 967 } 968 969 static ssize_t pstr_show(struct device *kdev, 970 struct device_attribute *kattr, char *buf) 971 { 972 struct hid_device *hdev = to_hid_device(kdev); 973 struct cp2112_pstring_attribute *attr = 974 container_of(kattr, struct cp2112_pstring_attribute, attr); 975 struct cp2112_string_report report; 976 u8 length; 977 int ret; 978 979 ret = cp2112_hid_get(hdev, attr->report, &report.report, 980 sizeof(report) - 1, HID_FEATURE_REPORT); 981 if (ret < 3) { 982 hid_err(hdev, "error reading %s string: %d\n", kattr->attr.name, 983 ret); 984 if (ret < 0) 985 return ret; 986 return -EIO; 987 } 988 989 if (report.length < 2) { 990 hid_err(hdev, "invalid %s string length: %d\n", 991 kattr->attr.name, report.length); 992 return -EIO; 993 } 994 995 length = report.length > ret - 1 ? ret - 1 : report.length; 996 length = (length - 2) / sizeof(report.string[0]); 997 ret = utf16s_to_utf8s(report.string, length, UTF16_LITTLE_ENDIAN, buf, 998 PAGE_SIZE - 1); 999 buf[ret++] = '\n'; 1000 return ret; 1001 } 1002 1003 #define CP2112_PSTR_ATTR(name, _report) \ 1004 static struct cp2112_pstring_attribute dev_attr_##name = { \ 1005 .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \ 1006 .report = _report, \ 1007 }; 1008 1009 CP2112_PSTR_ATTR(manufacturer, CP2112_MANUFACTURER_STRING); 1010 CP2112_PSTR_ATTR(product, CP2112_PRODUCT_STRING); 1011 CP2112_PSTR_ATTR(serial, CP2112_SERIAL_STRING); 1012 1013 #undef CP2112_PSTR_ATTR 1014 1015 static const struct attribute_group cp2112_attr_group = { 1016 .attrs = (struct attribute *[]){ 1017 &dev_attr_vendor_id.attr, 1018 &dev_attr_product_id.attr, 1019 &dev_attr_max_power.attr, 1020 &dev_attr_power_mode.attr, 1021 &dev_attr_release_version.attr, 1022 &dev_attr_manufacturer.attr.attr, 1023 &dev_attr_product.attr.attr, 1024 &dev_attr_serial.attr.attr, 1025 NULL 1026 } 1027 }; 1028 1029 /* Chmoding our sysfs attributes is simply a way to expose which fields in the 1030 * PROM have already been programmed. We do not depend on this preventing 1031 * writing to these attributes since the CP2112 will simply ignore writes to 1032 * already-programmed fields. This is why there is no sense in fixing this 1033 * racy behaviour. 1034 */ 1035 static void chmod_sysfs_attrs(struct hid_device *hdev) 1036 { 1037 struct attribute **attr; 1038 u8 buf[2]; 1039 int ret; 1040 1041 ret = cp2112_hid_get(hdev, CP2112_LOCK_BYTE, buf, sizeof(buf), 1042 HID_FEATURE_REPORT); 1043 if (ret != sizeof(buf)) { 1044 hid_err(hdev, "error reading lock byte: %d\n", ret); 1045 return; 1046 } 1047 1048 for (attr = cp2112_attr_group.attrs; *attr; ++attr) { 1049 umode_t mode = (buf[1] & 1) ? S_IWUSR | S_IRUGO : S_IRUGO; 1050 ret = sysfs_chmod_file(&hdev->dev.kobj, *attr, mode); 1051 if (ret < 0) 1052 hid_err(hdev, "error chmoding sysfs file %s\n", 1053 (*attr)->name); 1054 buf[1] >>= 1; 1055 } 1056 } 1057 1058 static void cp2112_gpio_irq_ack(struct irq_data *d) 1059 { 1060 } 1061 1062 static void cp2112_gpio_irq_mask(struct irq_data *d) 1063 { 1064 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1065 struct cp2112_device *dev = gpiochip_get_data(gc); 1066 1067 __clear_bit(d->hwirq, &dev->irq_mask); 1068 } 1069 1070 static void cp2112_gpio_irq_unmask(struct irq_data *d) 1071 { 1072 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1073 struct cp2112_device *dev = gpiochip_get_data(gc); 1074 1075 __set_bit(d->hwirq, &dev->irq_mask); 1076 } 1077 1078 static void cp2112_gpio_poll_callback(struct work_struct *work) 1079 { 1080 struct cp2112_device *dev = container_of(work, struct cp2112_device, 1081 gpio_poll_worker.work); 1082 struct irq_data *d; 1083 u8 gpio_mask; 1084 u8 virqs = (u8)dev->irq_mask; 1085 u32 irq_type; 1086 int irq, virq, ret; 1087 1088 ret = cp2112_gpio_get_all(&dev->gc); 1089 if (ret == -ENODEV) /* the hardware has been disconnected */ 1090 return; 1091 if (ret < 0) 1092 goto exit; 1093 1094 gpio_mask = ret; 1095 1096 while (virqs) { 1097 virq = ffs(virqs) - 1; 1098 virqs &= ~BIT(virq); 1099 1100 if (!dev->gc.to_irq) 1101 break; 1102 1103 irq = dev->gc.to_irq(&dev->gc, virq); 1104 1105 d = irq_get_irq_data(irq); 1106 if (!d) 1107 continue; 1108 1109 irq_type = irqd_get_trigger_type(d); 1110 1111 if (gpio_mask & BIT(virq)) { 1112 /* Level High */ 1113 1114 if (irq_type & IRQ_TYPE_LEVEL_HIGH) 1115 handle_nested_irq(irq); 1116 1117 if ((irq_type & IRQ_TYPE_EDGE_RISING) && 1118 !(dev->gpio_prev_state & BIT(virq))) 1119 handle_nested_irq(irq); 1120 } else { 1121 /* Level Low */ 1122 1123 if (irq_type & IRQ_TYPE_LEVEL_LOW) 1124 handle_nested_irq(irq); 1125 1126 if ((irq_type & IRQ_TYPE_EDGE_FALLING) && 1127 (dev->gpio_prev_state & BIT(virq))) 1128 handle_nested_irq(irq); 1129 } 1130 } 1131 1132 dev->gpio_prev_state = gpio_mask; 1133 1134 exit: 1135 if (dev->gpio_poll) 1136 schedule_delayed_work(&dev->gpio_poll_worker, 10); 1137 } 1138 1139 1140 static unsigned int cp2112_gpio_irq_startup(struct irq_data *d) 1141 { 1142 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1143 struct cp2112_device *dev = gpiochip_get_data(gc); 1144 1145 INIT_DELAYED_WORK(&dev->gpio_poll_worker, cp2112_gpio_poll_callback); 1146 1147 cp2112_gpio_direction_input(gc, d->hwirq); 1148 1149 if (!dev->gpio_poll) { 1150 dev->gpio_poll = true; 1151 schedule_delayed_work(&dev->gpio_poll_worker, 0); 1152 } 1153 1154 cp2112_gpio_irq_unmask(d); 1155 return 0; 1156 } 1157 1158 static void cp2112_gpio_irq_shutdown(struct irq_data *d) 1159 { 1160 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1161 struct cp2112_device *dev = gpiochip_get_data(gc); 1162 1163 cancel_delayed_work_sync(&dev->gpio_poll_worker); 1164 } 1165 1166 static int cp2112_gpio_irq_type(struct irq_data *d, unsigned int type) 1167 { 1168 return 0; 1169 } 1170 1171 static struct irq_chip cp2112_gpio_irqchip = { 1172 .name = "cp2112-gpio", 1173 .irq_startup = cp2112_gpio_irq_startup, 1174 .irq_shutdown = cp2112_gpio_irq_shutdown, 1175 .irq_ack = cp2112_gpio_irq_ack, 1176 .irq_mask = cp2112_gpio_irq_mask, 1177 .irq_unmask = cp2112_gpio_irq_unmask, 1178 .irq_set_type = cp2112_gpio_irq_type, 1179 }; 1180 1181 static int __maybe_unused cp2112_allocate_irq(struct cp2112_device *dev, 1182 int pin) 1183 { 1184 int ret; 1185 1186 if (dev->desc[pin]) 1187 return -EINVAL; 1188 1189 dev->desc[pin] = gpiochip_request_own_desc(&dev->gc, pin, 1190 "HID/I2C:Event"); 1191 if (IS_ERR(dev->desc[pin])) { 1192 dev_err(dev->gc.parent, "Failed to request GPIO\n"); 1193 return PTR_ERR(dev->desc[pin]); 1194 } 1195 1196 ret = gpiochip_lock_as_irq(&dev->gc, pin); 1197 if (ret) { 1198 dev_err(dev->gc.parent, "Failed to lock GPIO as interrupt\n"); 1199 goto err_desc; 1200 } 1201 1202 ret = gpiod_to_irq(dev->desc[pin]); 1203 if (ret < 0) { 1204 dev_err(dev->gc.parent, "Failed to translate GPIO to IRQ\n"); 1205 goto err_lock; 1206 } 1207 1208 return ret; 1209 1210 err_lock: 1211 gpiochip_unlock_as_irq(&dev->gc, pin); 1212 err_desc: 1213 gpiochip_free_own_desc(dev->desc[pin]); 1214 dev->desc[pin] = NULL; 1215 return ret; 1216 } 1217 1218 static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id) 1219 { 1220 struct cp2112_device *dev; 1221 u8 buf[3]; 1222 struct cp2112_smbus_config_report config; 1223 int ret; 1224 1225 dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL); 1226 if (!dev) 1227 return -ENOMEM; 1228 1229 dev->in_out_buffer = devm_kzalloc(&hdev->dev, CP2112_REPORT_MAX_LENGTH, 1230 GFP_KERNEL); 1231 if (!dev->in_out_buffer) 1232 return -ENOMEM; 1233 1234 mutex_init(&dev->lock); 1235 1236 ret = hid_parse(hdev); 1237 if (ret) { 1238 hid_err(hdev, "parse failed\n"); 1239 return ret; 1240 } 1241 1242 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); 1243 if (ret) { 1244 hid_err(hdev, "hw start failed\n"); 1245 return ret; 1246 } 1247 1248 ret = hid_hw_open(hdev); 1249 if (ret) { 1250 hid_err(hdev, "hw open failed\n"); 1251 goto err_hid_stop; 1252 } 1253 1254 ret = hid_hw_power(hdev, PM_HINT_FULLON); 1255 if (ret < 0) { 1256 hid_err(hdev, "power management error: %d\n", ret); 1257 goto err_hid_close; 1258 } 1259 1260 ret = cp2112_hid_get(hdev, CP2112_GET_VERSION_INFO, buf, sizeof(buf), 1261 HID_FEATURE_REPORT); 1262 if (ret != sizeof(buf)) { 1263 hid_err(hdev, "error requesting version\n"); 1264 if (ret >= 0) 1265 ret = -EIO; 1266 goto err_power_normal; 1267 } 1268 1269 hid_info(hdev, "Part Number: 0x%02X Device Version: 0x%02X\n", 1270 buf[1], buf[2]); 1271 1272 ret = cp2112_hid_get(hdev, CP2112_SMBUS_CONFIG, (u8 *)&config, 1273 sizeof(config), HID_FEATURE_REPORT); 1274 if (ret != sizeof(config)) { 1275 hid_err(hdev, "error requesting SMBus config\n"); 1276 if (ret >= 0) 1277 ret = -EIO; 1278 goto err_power_normal; 1279 } 1280 1281 config.retry_time = cpu_to_be16(1); 1282 1283 ret = cp2112_hid_output(hdev, (u8 *)&config, sizeof(config), 1284 HID_FEATURE_REPORT); 1285 if (ret != sizeof(config)) { 1286 hid_err(hdev, "error setting SMBus config\n"); 1287 if (ret >= 0) 1288 ret = -EIO; 1289 goto err_power_normal; 1290 } 1291 1292 hid_set_drvdata(hdev, (void *)dev); 1293 dev->hdev = hdev; 1294 dev->adap.owner = THIS_MODULE; 1295 dev->adap.class = I2C_CLASS_HWMON; 1296 dev->adap.algo = &smbus_algorithm; 1297 dev->adap.algo_data = dev; 1298 dev->adap.dev.parent = &hdev->dev; 1299 snprintf(dev->adap.name, sizeof(dev->adap.name), 1300 "CP2112 SMBus Bridge on hiddev%d", hdev->minor); 1301 dev->hwversion = buf[2]; 1302 init_waitqueue_head(&dev->wait); 1303 1304 hid_device_io_start(hdev); 1305 ret = i2c_add_adapter(&dev->adap); 1306 hid_device_io_stop(hdev); 1307 1308 if (ret) { 1309 hid_err(hdev, "error registering i2c adapter\n"); 1310 goto err_power_normal; 1311 } 1312 1313 hid_dbg(hdev, "adapter registered\n"); 1314 1315 dev->gc.label = "cp2112_gpio"; 1316 dev->gc.direction_input = cp2112_gpio_direction_input; 1317 dev->gc.direction_output = cp2112_gpio_direction_output; 1318 dev->gc.set = cp2112_gpio_set; 1319 dev->gc.get = cp2112_gpio_get; 1320 dev->gc.base = -1; 1321 dev->gc.ngpio = 8; 1322 dev->gc.can_sleep = 1; 1323 dev->gc.parent = &hdev->dev; 1324 1325 ret = gpiochip_add_data(&dev->gc, dev); 1326 if (ret < 0) { 1327 hid_err(hdev, "error registering gpio chip\n"); 1328 goto err_free_i2c; 1329 } 1330 1331 ret = sysfs_create_group(&hdev->dev.kobj, &cp2112_attr_group); 1332 if (ret < 0) { 1333 hid_err(hdev, "error creating sysfs attrs\n"); 1334 goto err_gpiochip_remove; 1335 } 1336 1337 chmod_sysfs_attrs(hdev); 1338 hid_hw_power(hdev, PM_HINT_NORMAL); 1339 1340 ret = gpiochip_irqchip_add(&dev->gc, &cp2112_gpio_irqchip, 0, 1341 handle_simple_irq, IRQ_TYPE_NONE); 1342 if (ret) { 1343 dev_err(dev->gc.parent, "failed to add IRQ chip\n"); 1344 goto err_sysfs_remove; 1345 } 1346 1347 return ret; 1348 1349 err_sysfs_remove: 1350 sysfs_remove_group(&hdev->dev.kobj, &cp2112_attr_group); 1351 err_gpiochip_remove: 1352 gpiochip_remove(&dev->gc); 1353 err_free_i2c: 1354 i2c_del_adapter(&dev->adap); 1355 err_power_normal: 1356 hid_hw_power(hdev, PM_HINT_NORMAL); 1357 err_hid_close: 1358 hid_hw_close(hdev); 1359 err_hid_stop: 1360 hid_hw_stop(hdev); 1361 return ret; 1362 } 1363 1364 static void cp2112_remove(struct hid_device *hdev) 1365 { 1366 struct cp2112_device *dev = hid_get_drvdata(hdev); 1367 int i; 1368 1369 sysfs_remove_group(&hdev->dev.kobj, &cp2112_attr_group); 1370 i2c_del_adapter(&dev->adap); 1371 1372 if (dev->gpio_poll) { 1373 dev->gpio_poll = false; 1374 cancel_delayed_work_sync(&dev->gpio_poll_worker); 1375 } 1376 1377 for (i = 0; i < ARRAY_SIZE(dev->desc); i++) { 1378 gpiochip_unlock_as_irq(&dev->gc, i); 1379 gpiochip_free_own_desc(dev->desc[i]); 1380 } 1381 1382 gpiochip_remove(&dev->gc); 1383 /* i2c_del_adapter has finished removing all i2c devices from our 1384 * adapter. Well behaved devices should no longer call our cp2112_xfer 1385 * and should have waited for any pending calls to finish. It has also 1386 * waited for device_unregister(&adap->dev) to complete. Therefore we 1387 * can safely free our struct cp2112_device. 1388 */ 1389 hid_hw_close(hdev); 1390 hid_hw_stop(hdev); 1391 } 1392 1393 static int cp2112_raw_event(struct hid_device *hdev, struct hid_report *report, 1394 u8 *data, int size) 1395 { 1396 struct cp2112_device *dev = hid_get_drvdata(hdev); 1397 struct cp2112_xfer_status_report *xfer = (void *)data; 1398 1399 switch (data[0]) { 1400 case CP2112_TRANSFER_STATUS_RESPONSE: 1401 hid_dbg(hdev, "xfer status: %02x %02x %04x %04x\n", 1402 xfer->status0, xfer->status1, 1403 be16_to_cpu(xfer->retries), be16_to_cpu(xfer->length)); 1404 1405 switch (xfer->status0) { 1406 case STATUS0_IDLE: 1407 dev->xfer_status = -EAGAIN; 1408 break; 1409 case STATUS0_BUSY: 1410 dev->xfer_status = -EBUSY; 1411 break; 1412 case STATUS0_COMPLETE: 1413 dev->xfer_status = be16_to_cpu(xfer->length); 1414 break; 1415 case STATUS0_ERROR: 1416 switch (xfer->status1) { 1417 case STATUS1_TIMEOUT_NACK: 1418 case STATUS1_TIMEOUT_BUS: 1419 dev->xfer_status = -ETIMEDOUT; 1420 break; 1421 default: 1422 dev->xfer_status = -EIO; 1423 break; 1424 } 1425 break; 1426 default: 1427 dev->xfer_status = -EINVAL; 1428 break; 1429 } 1430 1431 atomic_set(&dev->xfer_avail, 1); 1432 break; 1433 case CP2112_DATA_READ_RESPONSE: 1434 hid_dbg(hdev, "read response: %02x %02x\n", data[1], data[2]); 1435 1436 dev->read_length = data[2]; 1437 if (dev->read_length > sizeof(dev->read_data)) 1438 dev->read_length = sizeof(dev->read_data); 1439 1440 memcpy(dev->read_data, &data[3], dev->read_length); 1441 atomic_set(&dev->read_avail, 1); 1442 break; 1443 default: 1444 hid_err(hdev, "unknown report\n"); 1445 1446 return 0; 1447 } 1448 1449 wake_up_interruptible(&dev->wait); 1450 return 1; 1451 } 1452 1453 static struct hid_driver cp2112_driver = { 1454 .name = "cp2112", 1455 .id_table = cp2112_devices, 1456 .probe = cp2112_probe, 1457 .remove = cp2112_remove, 1458 .raw_event = cp2112_raw_event, 1459 }; 1460 1461 module_hid_driver(cp2112_driver); 1462 MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge"); 1463 MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>"); 1464 MODULE_LICENSE("GPL"); 1465 1466