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