1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) Andriy Gapon 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions, and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 */ 28 29 /* 30 * Hardware information links: 31 * - CP2112 Datasheet 32 * https://www.silabs.com/documents/public/data-sheets/cp2112-datasheet.pdf 33 * - AN495: CP2112 Interface Specification 34 * https://www.silabs.com/documents/public/application-notes/an495-cp2112-interface-specification.pdf 35 * - CP2112 Errata 36 * https://www.silabs.com/documents/public/errata/cp2112-errata.pdf 37 */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/condvar.h> 42 #include <sys/bus.h> 43 #include <sys/gpio.h> 44 #include <sys/kernel.h> 45 #include <sys/lock.h> 46 #include <sys/module.h> 47 #include <sys/mutex.h> 48 #include <sys/sdt.h> 49 #include <sys/sx.h> 50 51 #include <dev/gpio/gpiobusvar.h> 52 53 #include <dev/iicbus/iiconf.h> 54 #include <dev/iicbus/iicbus.h> 55 #include "iicbus_if.h" 56 57 #include <dev/usb/usb.h> 58 #include <dev/usb/usbdi.h> 59 #include <dev/usb/usbdi_util.h> 60 #include <dev/usb/usbhid.h> 61 #include "usbdevs.h" 62 63 #define USB_DEBUG_VAR usb_debug 64 #include <dev/usb/usb_debug.h> 65 66 #define SIZEOF_FIELD(_s, _f) sizeof(((struct _s *)NULL)->_f) 67 68 #define CP2112GPIO_LOCK(sc) sx_xlock(&sc->gpio_lock) 69 #define CP2112GPIO_UNLOCK(sc) sx_xunlock(&sc->gpio_lock) 70 #define CP2112GPIO_LOCKED(sc) sx_assert(&sc->gpio_lock, SX_XLOCKED) 71 72 #define CP2112_PART_NUM 0x0c 73 #define CP2112_GPIO_COUNT 8 74 #define CP2112_REPORT_SIZE 64 75 76 #define CP2112_REQ_RESET 0x1 77 #define CP2112_REQ_GPIO_CFG 0x2 78 #define CP2112_REQ_GPIO_GET 0x3 79 #define CP2112_REQ_GPIO_SET 0x4 80 #define CP2112_REQ_VERSION 0x5 81 #define CP2112_REQ_SMB_CFG 0x6 82 83 #define CP2112_REQ_SMB_READ 0x10 84 #define CP2112_REQ_SMB_WRITE_READ 0x11 85 #define CP2112_REQ_SMB_READ_FORCE_SEND 0x12 86 #define CP2112_REQ_SMB_READ_RESPONSE 0x13 87 #define CP2112_REQ_SMB_WRITE 0x14 88 #define CP2112_REQ_SMB_XFER_STATUS_REQ 0x15 89 #define CP2112_REQ_SMB_XFER_STATUS_RESP 0x16 90 #define CP2112_REQ_SMB_CANCEL 0x17 91 92 #define CP2112_REQ_LOCK 0x20 93 #define CP2112_REQ_USB_CFG 0x21 94 95 #define CP2112_IIC_MAX_READ_LEN 512 96 #define CP2112_IIC_REPSTART_VER 2 /* Erratum CP2112_E10. */ 97 98 #define CP2112_GPIO_SPEC_CLK7 1 /* Pin 7 is clock output. */ 99 #define CP2112_GPIO_SPEC_TX0 2 /* Pin 0 pulses on USB TX. */ 100 #define CP2112_GPIO_SPEC_RX1 4 /* Pin 1 pulses on USB RX. */ 101 102 #define CP2112_IIC_STATUS0_IDLE 0 103 #define CP2112_IIC_STATUS0_BUSY 1 104 #define CP2112_IIC_STATUS0_CMP 2 105 #define CP2112_IIC_STATUS0_ERROR 3 106 107 #define CP2112_IIC_STATUS1_TIMEOUT_NACK 0 108 #define CP2112_IIC_STATUS1_TIMEOUT_BUS 1 109 #define CP2112_IIC_STATUS1_ARB_LOST 2 110 111 /* CP2112_REQ_VERSION */ 112 struct version_request { 113 uint8_t id; 114 uint8_t part_num; 115 uint8_t version; 116 } __packed; 117 118 /* CP2112_REQ_GPIO_GET */ 119 struct gpio_get_req { 120 uint8_t id; 121 uint8_t state; 122 } __packed; 123 124 /* CP2112_REQ_GPIO_SET */ 125 struct gpio_set_req { 126 uint8_t id; 127 uint8_t state; 128 uint8_t mask; 129 } __packed; 130 131 /* CP2112_REQ_GPIO_CFG */ 132 struct gpio_config_req { 133 uint8_t id; 134 uint8_t output; 135 uint8_t pushpull; 136 uint8_t special; 137 uint8_t divider; 138 } __packed; 139 140 /* CP2112_REQ_SMB_XFER_STATUS_REQ */ 141 struct i2c_xfer_status_req { 142 uint8_t id; 143 uint8_t request; 144 } __packed; 145 146 /* CP2112_REQ_SMB_XFER_STATUS_RESP */ 147 struct i2c_xfer_status_resp { 148 uint8_t id; 149 uint8_t status0; 150 uint8_t status1; 151 uint16_t status2; 152 uint16_t status3; 153 } __packed; 154 155 /* CP2112_REQ_SMB_READ_FORCE_SEND */ 156 struct i2c_data_read_force_send_req { 157 uint8_t id; 158 uint16_t len; 159 } __packed; 160 161 /* CP2112_REQ_SMB_READ_RESPONSE */ 162 struct i2c_data_read_resp { 163 uint8_t id; 164 uint8_t status; 165 uint8_t len; 166 uint8_t data[61]; 167 } __packed; 168 169 /* CP2112_REQ_SMB_READ */ 170 struct i2c_write_read_req { 171 uint8_t id; 172 uint8_t slave; 173 uint16_t rlen; 174 uint8_t wlen; 175 uint8_t wdata[16]; 176 } __packed; 177 178 /* CP2112_REQ_SMB_WRITE */ 179 struct i2c_read_req { 180 uint8_t id; 181 uint8_t slave; 182 uint16_t len; 183 } __packed; 184 185 /* CP2112_REQ_SMB_WRITE_READ */ 186 struct i2c_write_req { 187 uint8_t id; 188 uint8_t slave; 189 uint8_t len; 190 uint8_t data[61]; 191 } __packed; 192 193 /* CP2112_REQ_SMB_CFG */ 194 struct i2c_cfg_req { 195 uint8_t id; 196 uint32_t speed; /* Hz */ 197 uint8_t slave_addr; /* ACK only */ 198 uint8_t auto_send_read; /* boolean */ 199 uint16_t write_timeout; /* 0-1000 ms, 0 ~ no timeout */ 200 uint16_t read_timeout; /* 0-1000 ms, 0 ~ no timeout */ 201 uint8_t scl_low_timeout;/* boolean */ 202 uint16_t retry_count; /* 1-1000, 0 ~ forever */ 203 } __packed; 204 205 enum cp2112_out_mode { 206 OUT_OD, 207 OUT_PP, 208 OUT_KEEP 209 }; 210 211 enum { 212 CP2112_INTR_OUT = 0, 213 CP2112_INTR_IN, 214 CP2112_N_TRANSFER, 215 }; 216 217 struct cp2112_softc { 218 device_t sc_gpio_dev; 219 device_t sc_iic_dev; 220 struct usb_device *sc_udev; 221 uint8_t sc_iface_index; 222 uint8_t sc_version; 223 }; 224 225 struct cp2112gpio_softc { 226 struct sx gpio_lock; 227 device_t busdev; 228 int gpio_caps; 229 struct gpio_pin pins[CP2112_GPIO_COUNT]; 230 }; 231 232 struct cp2112iic_softc { 233 device_t dev; 234 device_t iicbus_dev; 235 struct usb_xfer *xfers[CP2112_N_TRANSFER]; 236 u_char own_addr; 237 struct { 238 struct mtx lock; 239 struct cv cv; 240 struct { 241 uint8_t *data; 242 int len; 243 int done; 244 int error; 245 } in; 246 struct { 247 const uint8_t *data; 248 int len; 249 int done; 250 int error; 251 } out; 252 } io; 253 }; 254 255 static int cp2112gpio_detach(device_t dev); 256 static int cp2112iic_detach(device_t dev); 257 258 static const STRUCT_USB_HOST_ID cp2112_devs[] = { 259 { USB_VP(USB_VENDOR_SILABS, USB_PRODUCT_SILABS_CP2112) }, 260 { USB_VP(0x1009, USB_PRODUCT_SILABS_CP2112) }, /* XXX */ 261 }; 262 263 static int 264 cp2112_get_report(device_t dev, uint8_t id, void *data, uint16_t len) 265 { 266 struct cp2112_softc *sc; 267 int err; 268 269 sc = device_get_softc(dev); 270 err = usbd_req_get_report(sc->sc_udev, NULL, data, 271 len, sc->sc_iface_index, UHID_FEATURE_REPORT, id); 272 return (err); 273 } 274 275 static int 276 cp2112_set_report(device_t dev, uint8_t id, void *data, uint16_t len) 277 { 278 struct cp2112_softc *sc; 279 int err; 280 281 sc = device_get_softc(dev); 282 *(uint8_t *)data = id; 283 err = usbd_req_set_report(sc->sc_udev, NULL, data, 284 len, sc->sc_iface_index, UHID_FEATURE_REPORT, id); 285 return (err); 286 } 287 288 static int 289 cp2112_probe(device_t dev) 290 { 291 struct usb_attach_arg *uaa; 292 293 uaa = device_get_ivars(dev); 294 if (uaa->usb_mode != USB_MODE_HOST) 295 return (ENXIO); 296 if (uaa->info.bInterfaceClass != UICLASS_HID) 297 return (ENXIO); 298 299 if (usbd_lookup_id_by_uaa(cp2112_devs, sizeof(cp2112_devs), uaa) == 0) 300 return (BUS_PROBE_DEFAULT); 301 return (ENXIO); 302 } 303 304 static int 305 cp2112_attach(device_t dev) 306 { 307 struct version_request vdata; 308 struct usb_attach_arg *uaa; 309 struct cp2112_softc *sc; 310 int err; 311 312 uaa = device_get_ivars(dev); 313 sc = device_get_softc(dev); 314 315 device_set_usb_desc(dev); 316 317 sc->sc_udev = uaa->device; 318 sc->sc_iface_index = uaa->info.bIfaceIndex; 319 320 err = cp2112_get_report(dev, CP2112_REQ_VERSION, &vdata, sizeof(vdata)); 321 if (err != 0) 322 goto detach; 323 device_printf(dev, "part number 0x%02x, version 0x%02x\n", 324 vdata.part_num, vdata.version); 325 if (vdata.part_num != CP2112_PART_NUM) { 326 device_printf(dev, "unsupported part number\n"); 327 goto detach; 328 } 329 sc->sc_version = vdata.version; 330 sc->sc_gpio_dev = device_add_child(dev, "gpio", DEVICE_UNIT_ANY); 331 if (sc->sc_gpio_dev != NULL) { 332 err = device_probe_and_attach(sc->sc_gpio_dev); 333 if (err != 0) { 334 device_printf(dev, "failed to attach gpio child\n"); 335 } 336 } else { 337 device_printf(dev, "failed to create gpio child\n"); 338 } 339 340 sc->sc_iic_dev = device_add_child(dev, "iichb", DEVICE_UNIT_ANY); 341 if (sc->sc_iic_dev != NULL) { 342 err = device_probe_and_attach(sc->sc_iic_dev); 343 if (err != 0) { 344 device_printf(dev, "failed to attach iic child\n"); 345 } 346 } else { 347 device_printf(dev, "failed to create iic child\n"); 348 } 349 350 return (0); 351 352 detach: 353 bus_generic_detach(dev); 354 return (ENXIO); 355 } 356 357 static int 358 cp2112_gpio_read_pin(device_t dev, uint32_t pin_num, bool *on) 359 { 360 struct gpio_get_req data; 361 struct cp2112gpio_softc *sc __diagused; 362 int err; 363 364 sc = device_get_softc(dev); 365 CP2112GPIO_LOCKED(sc); 366 367 err = cp2112_get_report(device_get_parent(dev), 368 CP2112_REQ_GPIO_GET, &data, sizeof(data)); 369 if (err != 0) 370 return (err); 371 *on = (data.state & ((uint8_t)1 << pin_num)) != 0; 372 return (0); 373 374 } 375 376 static int 377 cp2112_gpio_write_pin(device_t dev, uint32_t pin_num, bool on) 378 { 379 struct gpio_set_req data; 380 struct cp2112gpio_softc *sc __diagused; 381 int err; 382 bool actual; 383 384 sc = device_get_softc(dev); 385 CP2112GPIO_LOCKED(sc); 386 387 data.state = (uint8_t)on << pin_num; 388 data.mask = (uint8_t)1 << pin_num; 389 err = cp2112_set_report(device_get_parent(dev), 390 CP2112_REQ_GPIO_SET, &data, sizeof(data)); 391 if (err != 0) 392 return (err); 393 err = cp2112_gpio_read_pin(dev, pin_num, &actual); 394 if (err != 0) 395 return (err); 396 if (actual != on) 397 return (EIO); 398 return (0); 399 } 400 401 static int 402 cp2112_gpio_configure_write_pin(device_t dev, uint32_t pin_num, 403 bool output, enum cp2112_out_mode *mode) 404 { 405 struct gpio_config_req data; 406 struct cp2112gpio_softc *sc __diagused; 407 int err; 408 uint8_t mask; 409 410 sc = device_get_softc(dev); 411 CP2112GPIO_LOCKED(sc); 412 413 err = cp2112_get_report(device_get_parent(dev), 414 CP2112_REQ_GPIO_CFG, &data, sizeof(data)); 415 if (err != 0) 416 return (err); 417 418 mask = (uint8_t)1 << pin_num; 419 if (output) { 420 data.output |= mask; 421 switch (*mode) { 422 case OUT_PP: 423 data.pushpull |= mask; 424 break; 425 case OUT_OD: 426 data.pushpull &= ~mask; 427 break; 428 default: 429 break; 430 } 431 } else { 432 data.output &= ~mask; 433 } 434 435 err = cp2112_set_report(device_get_parent(dev), 436 CP2112_REQ_GPIO_CFG, &data, sizeof(data)); 437 if (err != 0) 438 return (err); 439 440 /* Read back and verify. */ 441 err = cp2112_get_report(device_get_parent(dev), 442 CP2112_REQ_GPIO_CFG, &data, sizeof(data)); 443 if (err != 0) 444 return (err); 445 446 if (((data.output & mask) != 0) != output) 447 return (EIO); 448 if (output) { 449 switch (*mode) { 450 case OUT_PP: 451 if ((data.pushpull & mask) == 0) 452 return (EIO); 453 break; 454 case OUT_OD: 455 if ((data.pushpull & mask) != 0) 456 return (EIO); 457 break; 458 default: 459 *mode = (data.pushpull & mask) != 0 ? 460 OUT_PP : OUT_OD; 461 break; 462 } 463 } 464 return (0); 465 } 466 467 static device_t 468 cp2112_gpio_get_bus(device_t dev) 469 { 470 struct cp2112gpio_softc *sc; 471 472 sc = device_get_softc(dev); 473 return (sc->busdev); 474 } 475 476 static int 477 cp2112_gpio_pin_max(device_t dev, int *maxpin) 478 { 479 480 *maxpin = CP2112_GPIO_COUNT - 1; 481 return (0); 482 } 483 484 static int 485 cp2112_gpio_pin_set(device_t dev, uint32_t pin_num, uint32_t pin_value) 486 { 487 struct cp2112gpio_softc *sc; 488 int err; 489 490 if (pin_num >= CP2112_GPIO_COUNT) 491 return (EINVAL); 492 493 sc = device_get_softc(dev); 494 CP2112GPIO_LOCK(sc); 495 err = cp2112_gpio_write_pin(dev, pin_num, pin_value != 0); 496 CP2112GPIO_UNLOCK(sc); 497 498 return (err); 499 } 500 501 static int 502 cp2112_gpio_pin_get(device_t dev, uint32_t pin_num, uint32_t *pin_value) 503 { 504 struct cp2112gpio_softc *sc; 505 int err; 506 bool on; 507 508 if (pin_num >= CP2112_GPIO_COUNT) 509 return (EINVAL); 510 511 sc = device_get_softc(dev); 512 CP2112GPIO_LOCK(sc); 513 err = cp2112_gpio_read_pin(dev, pin_num, &on); 514 CP2112GPIO_UNLOCK(sc); 515 516 if (err == 0) 517 *pin_value = on; 518 return (err); 519 } 520 521 static int 522 cp2112_gpio_pin_toggle(device_t dev, uint32_t pin_num) 523 { 524 struct cp2112gpio_softc *sc; 525 int err; 526 bool on; 527 528 if (pin_num >= CP2112_GPIO_COUNT) 529 return (EINVAL); 530 531 sc = device_get_softc(dev); 532 CP2112GPIO_LOCK(sc); 533 err = cp2112_gpio_read_pin(dev, pin_num, &on); 534 if (err == 0) 535 err = cp2112_gpio_write_pin(dev, pin_num, !on); 536 CP2112GPIO_UNLOCK(sc); 537 538 return (err); 539 } 540 541 static int 542 cp2112_gpio_pin_getcaps(device_t dev, uint32_t pin_num, uint32_t *caps) 543 { 544 struct cp2112gpio_softc *sc; 545 546 if (pin_num >= CP2112_GPIO_COUNT) 547 return (EINVAL); 548 549 sc = device_get_softc(dev); 550 CP2112GPIO_LOCK(sc); 551 *caps = sc->gpio_caps; 552 CP2112GPIO_UNLOCK(sc); 553 554 return (0); 555 } 556 557 static int 558 cp2112_gpio_pin_getflags(device_t dev, uint32_t pin_num, uint32_t *flags) 559 { 560 struct cp2112gpio_softc *sc; 561 562 if (pin_num >= CP2112_GPIO_COUNT) 563 return (EINVAL); 564 565 sc = device_get_softc(dev); 566 CP2112GPIO_LOCK(sc); 567 *flags = sc->pins[pin_num].gp_flags; 568 CP2112GPIO_UNLOCK(sc); 569 570 return (0); 571 } 572 573 static int 574 cp2112_gpio_pin_getname(device_t dev, uint32_t pin_num, char *name) 575 { 576 struct cp2112gpio_softc *sc; 577 578 if (pin_num >= CP2112_GPIO_COUNT) 579 return (EINVAL); 580 581 sc = device_get_softc(dev); 582 CP2112GPIO_LOCK(sc); 583 memcpy(name, sc->pins[pin_num].gp_name, GPIOMAXNAME); 584 CP2112GPIO_UNLOCK(sc); 585 586 return (0); 587 } 588 589 static int 590 cp2112_gpio_pin_setflags(device_t dev, uint32_t pin_num, uint32_t flags) 591 { 592 struct cp2112gpio_softc *sc; 593 struct gpio_pin *pin; 594 enum cp2112_out_mode out_mode; 595 int err; 596 597 if (pin_num >= CP2112_GPIO_COUNT) 598 return (EINVAL); 599 600 sc = device_get_softc(dev); 601 if ((flags & sc->gpio_caps) != flags) 602 return (EINVAL); 603 604 if ((flags & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) == 0) 605 return (EINVAL); 606 if ((flags & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) == 607 (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) { 608 return (EINVAL); 609 } 610 if ((flags & GPIO_PIN_INPUT) != 0) { 611 if ((flags & (GPIO_PIN_OPENDRAIN | GPIO_PIN_PUSHPULL)) != 0) 612 return (EINVAL); 613 } else { 614 if ((flags & (GPIO_PIN_OPENDRAIN | GPIO_PIN_PUSHPULL)) == 615 (GPIO_PIN_OPENDRAIN | GPIO_PIN_PUSHPULL)) 616 return (EINVAL); 617 } 618 619 /* 620 * If neither push-pull or open-drain is explicitly requested, then 621 * preserve the current state. 622 */ 623 out_mode = OUT_KEEP; 624 if ((flags & GPIO_PIN_OUTPUT) != 0) { 625 if ((flags & GPIO_PIN_OPENDRAIN) != 0) 626 out_mode = OUT_OD; 627 if ((flags & GPIO_PIN_PUSHPULL) != 0) 628 out_mode = OUT_PP; 629 } 630 631 CP2112GPIO_LOCK(sc); 632 pin = &sc->pins[pin_num]; 633 err = cp2112_gpio_configure_write_pin(dev, pin_num, 634 (flags & GPIO_PIN_OUTPUT) != 0, &out_mode); 635 if (err == 0) { 636 /* 637 * If neither open-drain or push-pull was requested, then see 638 * what hardware actually had. Otherwise, it has been 639 * reconfigured as requested. 640 */ 641 if ((flags & GPIO_PIN_OUTPUT) != 0 && 642 (flags & (GPIO_PIN_OPENDRAIN | GPIO_PIN_PUSHPULL)) == 0) { 643 KASSERT(out_mode != OUT_KEEP, 644 ("impossible current output mode")); 645 if (out_mode == OUT_OD) 646 flags |= GPIO_PIN_OPENDRAIN; 647 else 648 flags |= GPIO_PIN_PUSHPULL; 649 } 650 pin->gp_flags = flags; 651 } 652 CP2112GPIO_UNLOCK(sc); 653 654 return (err); 655 } 656 657 static int 658 cp2112gpio_probe(device_t dev) 659 { 660 device_set_desc(dev, "CP2112 GPIO interface"); 661 return (BUS_PROBE_SPECIFIC); 662 } 663 664 static int 665 cp2112gpio_attach(device_t dev) 666 { 667 struct gpio_config_req data; 668 struct cp2112gpio_softc *sc; 669 device_t cp2112; 670 int err; 671 int i; 672 uint8_t mask; 673 674 cp2112 = device_get_parent(dev); 675 sc = device_get_softc(dev); 676 sx_init(&sc->gpio_lock, "cp2112 lock"); 677 678 sc->gpio_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN | 679 GPIO_PIN_PUSHPULL; 680 681 err = cp2112_get_report(cp2112, CP2112_REQ_GPIO_CFG, 682 &data, sizeof(data)); 683 if (err != 0) 684 goto detach; 685 686 for (i = 0; i < CP2112_GPIO_COUNT; i++) { 687 struct gpio_pin *pin; 688 689 mask = (uint8_t)1 << i; 690 pin = &sc->pins[i]; 691 pin->gp_flags = 0; 692 693 snprintf(pin->gp_name, GPIOMAXNAME, "GPIO%u", i); 694 pin->gp_name[GPIOMAXNAME - 1] = '\0'; 695 696 if ((i == 0 && (data.special & CP2112_GPIO_SPEC_TX0) != 0) || 697 (i == 1 && (data.special & CP2112_GPIO_SPEC_RX1) != 0) || 698 (i == 7 && (data.special & CP2112_GPIO_SPEC_CLK7) != 0)) { 699 /* Special mode means that a pin is not for GPIO. */ 700 } else if ((data.output & mask) != 0) { 701 pin->gp_flags |= GPIO_PIN_OUTPUT; 702 if ((data.pushpull & mask) != 0) 703 pin->gp_flags |= GPIO_PIN_PUSHPULL; 704 else 705 pin->gp_flags |= GPIO_PIN_OPENDRAIN; 706 } else { 707 pin->gp_flags |= GPIO_PIN_INPUT; 708 } 709 } 710 711 sc->busdev = gpiobus_add_bus(dev); 712 if (sc->busdev == NULL) { 713 device_printf(dev, "gpiobus_add_bus failed\n"); 714 goto detach; 715 } 716 bus_attach_children(dev); 717 return (0); 718 719 detach: 720 cp2112gpio_detach(dev); 721 return (ENXIO); 722 } 723 724 static int 725 cp2112gpio_detach(device_t dev) 726 { 727 struct cp2112gpio_softc *sc; 728 729 sc = device_get_softc(dev); 730 if (sc->busdev != NULL) 731 gpiobus_detach_bus(dev); 732 sx_destroy(&sc->gpio_lock); 733 return (0); 734 } 735 736 static void 737 cp2112iic_intr_write_callback(struct usb_xfer *xfer, usb_error_t error) 738 { 739 struct cp2112iic_softc *sc; 740 struct usb_page_cache *pc; 741 742 sc = usbd_xfer_softc(xfer); 743 744 mtx_assert(&sc->io.lock, MA_OWNED); 745 746 switch (USB_GET_STATE(xfer)) { 747 case USB_ST_SETUP: 748 pc = usbd_xfer_get_frame(xfer, 0); 749 usbd_copy_in(pc, 0, sc->io.out.data, sc->io.out.len); 750 usbd_xfer_set_frame_len(xfer, 0, sc->io.out.len); 751 usbd_xfer_set_frames(xfer, 1); 752 usbd_transfer_submit(xfer); 753 break; 754 case USB_ST_TRANSFERRED: 755 sc->io.out.error = 0; 756 sc->io.out.done = 1; 757 cv_signal(&sc->io.cv); 758 break; 759 default: /* Error */ 760 device_printf(sc->dev, "write intr state %d error %d\n", 761 USB_GET_STATE(xfer), error); 762 sc->io.out.error = IIC_EBUSERR; 763 cv_signal(&sc->io.cv); 764 if (error != USB_ERR_CANCELLED) { 765 /* try to clear stall first */ 766 usbd_xfer_set_stall(xfer); 767 } 768 break; 769 } 770 } 771 772 static void 773 cp2112iic_intr_read_callback(struct usb_xfer *xfer, usb_error_t error) 774 { 775 struct cp2112iic_softc *sc = usbd_xfer_softc(xfer); 776 struct usb_page_cache *pc; 777 int act_len, len; 778 779 mtx_assert(&sc->io.lock, MA_OWNED); 780 usbd_xfer_status(xfer, &act_len, NULL, NULL, NULL); 781 782 switch (USB_GET_STATE(xfer)) { 783 case USB_ST_TRANSFERRED: 784 if (sc->io.in.done) { 785 device_printf(sc->dev, 786 "interrupt while previous is pending, ignored\n"); 787 } else if (sc->io.in.len == 0) { 788 uint8_t buf[8]; 789 790 /* 791 * There is a spurious Transfer Status Response and 792 * zero-length Read Response during hardware 793 * configuration. Possibly they carry some information 794 * about the initial bus state. 795 */ 796 if (device_is_attached(sc->dev)) { 797 device_printf(sc->dev, 798 "unsolicited interrupt, ignored\n"); 799 if (bootverbose) { 800 pc = usbd_xfer_get_frame(xfer, 0); 801 len = MIN(sizeof(buf), act_len); 802 usbd_copy_out(pc, 0, buf, len); 803 device_printf(sc->dev, "data: %*D\n", 804 len, buf, " "); 805 } 806 } else { 807 pc = usbd_xfer_get_frame(xfer, 0); 808 len = MIN(sizeof(buf), act_len); 809 usbd_copy_out(pc, 0, buf, len); 810 if (buf[0] == CP2112_REQ_SMB_XFER_STATUS_RESP) { 811 device_printf(sc->dev, 812 "initial bus status0 = 0x%02x, " 813 "status1 = 0x%02x\n", 814 buf[1], buf[2]); 815 } 816 } 817 } else if (act_len == CP2112_REPORT_SIZE) { 818 pc = usbd_xfer_get_frame(xfer, 0); 819 usbd_copy_out(pc, 0, sc->io.in.data, sc->io.in.len); 820 sc->io.in.error = 0; 821 sc->io.in.done = 1; 822 } else { 823 device_printf(sc->dev, 824 "unexpected input report length %u\n", act_len); 825 sc->io.in.error = IIC_EBUSERR; 826 sc->io.in.done = 1; 827 } 828 cv_signal(&sc->io.cv); 829 case USB_ST_SETUP: 830 tr_setup: 831 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 832 usbd_transfer_submit(xfer); 833 break; 834 835 default: /* Error */ 836 device_printf(sc->dev, "read intr state %d error %d\n", 837 USB_GET_STATE(xfer), error); 838 839 sc->io.in.error = IIC_EBUSERR; 840 sc->io.in.done = 1; 841 cv_signal(&sc->io.cv); 842 if (error != USB_ERR_CANCELLED) { 843 /* try to clear stall first */ 844 usbd_xfer_set_stall(xfer); 845 goto tr_setup; 846 } 847 break; 848 } 849 } 850 851 static const struct usb_config cp2112iic_config[CP2112_N_TRANSFER] = { 852 [CP2112_INTR_OUT] = { 853 .type = UE_INTERRUPT, 854 .endpoint = UE_ADDR_ANY, 855 .direction = UE_DIR_OUT, 856 .flags = { .pipe_bof = 1, .no_pipe_ok = 1, }, 857 .bufsize = 0, /* use wMaxPacketSize */ 858 .callback = &cp2112iic_intr_write_callback, 859 }, 860 [CP2112_INTR_IN] = { 861 .type = UE_INTERRUPT, 862 .endpoint = UE_ADDR_ANY, 863 .direction = UE_DIR_IN, 864 .flags = { .pipe_bof = 1, .short_xfer_ok = 1, }, 865 .bufsize = 0, /* use wMaxPacketSize */ 866 .callback = &cp2112iic_intr_read_callback, 867 }, 868 }; 869 870 static int 871 cp2112iic_send_req(struct cp2112iic_softc *sc, const void *data, 872 uint16_t len) 873 { 874 int err; 875 876 mtx_assert(&sc->io.lock, MA_OWNED); 877 KASSERT(sc->io.out.done == 0, ("%s: conflicting request", __func__)); 878 879 sc->io.out.data = data; 880 sc->io.out.len = len; 881 882 DTRACE_PROBE1(send__req, uint8_t, *(const uint8_t *)data); 883 884 usbd_transfer_start(sc->xfers[CP2112_INTR_OUT]); 885 886 while (!sc->io.out.done) 887 cv_wait(&sc->io.cv, &sc->io.lock); 888 889 usbd_transfer_stop(sc->xfers[CP2112_INTR_OUT]); 890 891 sc->io.out.done = 0; 892 sc->io.out.data = NULL; 893 sc->io.out.len = 0; 894 err = sc->io.out.error; 895 if (err != 0) { 896 device_printf(sc->dev, "output report 0x%02x failed: %d\n", 897 *(const uint8_t*)data, err); 898 } 899 return (err); 900 } 901 902 static int 903 cp2112iic_req_resp(struct cp2112iic_softc *sc, const void *req_data, 904 uint16_t req_len, void *resp_data, uint16_t resp_len) 905 { 906 int err; 907 908 mtx_assert(&sc->io.lock, MA_OWNED); 909 910 /* 911 * Prepare to receive a response interrupt even before the 912 * request transfer is confirmed (USB_ST_TRANSFERED). 913 */ 914 KASSERT(sc->io.in.done == 0, ("%s: conflicting request", __func__)); 915 sc->io.in.len = resp_len; 916 sc->io.in.data = resp_data; 917 918 err = cp2112iic_send_req(sc, req_data, req_len); 919 if (err != 0) { 920 sc->io.in.len = 0; 921 sc->io.in.data = NULL; 922 return (err); 923 } 924 925 while (!sc->io.in.done) 926 cv_wait(&sc->io.cv, &sc->io.lock); 927 928 err = sc->io.in.error; 929 sc->io.in.done = 0; 930 sc->io.in.error = 0; 931 sc->io.in.len = 0; 932 sc->io.in.data = NULL; 933 return (err); 934 } 935 936 static int 937 cp2112iic_check_req_status(struct cp2112iic_softc *sc) 938 { 939 struct i2c_xfer_status_req xfer_status_req; 940 struct i2c_xfer_status_resp xfer_status_resp; 941 int err; 942 943 mtx_assert(&sc->io.lock, MA_OWNED); 944 945 do { 946 xfer_status_req.id = CP2112_REQ_SMB_XFER_STATUS_REQ; 947 xfer_status_req.request = 1; 948 err = cp2112iic_req_resp(sc, 949 &xfer_status_req, sizeof(xfer_status_req), 950 &xfer_status_resp, sizeof(xfer_status_resp)); 951 952 if (xfer_status_resp.id != CP2112_REQ_SMB_XFER_STATUS_RESP) { 953 device_printf(sc->dev, 954 "unexpected response 0x%02x to status request\n", 955 xfer_status_resp.id); 956 err = IIC_EBUSERR; 957 goto out; 958 } 959 960 DTRACE_PROBE4(xfer__status, uint8_t, xfer_status_resp.status0, 961 uint8_t, xfer_status_resp.status1, 962 uint16_t, be16toh(xfer_status_resp.status2), 963 uint16_t, be16toh(xfer_status_resp.status3)); 964 965 switch (xfer_status_resp.status0) { 966 case CP2112_IIC_STATUS0_IDLE: 967 err = IIC_ESTATUS; 968 break; 969 case CP2112_IIC_STATUS0_BUSY: 970 err = ERESTART; /* non-I2C, special handling */ 971 break; 972 case CP2112_IIC_STATUS0_CMP: 973 err = IIC_NOERR; 974 break; 975 case CP2112_IIC_STATUS0_ERROR: 976 switch (xfer_status_resp.status1) { 977 case CP2112_IIC_STATUS1_TIMEOUT_NACK: 978 err = IIC_ENOACK; 979 break; 980 case CP2112_IIC_STATUS1_TIMEOUT_BUS: 981 err = IIC_ETIMEOUT; 982 break; 983 case CP2112_IIC_STATUS1_ARB_LOST: 984 err = IIC_EBUSBSY; 985 break; 986 default: 987 device_printf(sc->dev, 988 "i2c error, status = 0x%02x\n", 989 xfer_status_resp.status1); 990 err = IIC_ESTATUS; 991 break; 992 } 993 break; 994 default: 995 device_printf(sc->dev, 996 "unknown i2c xfer status0 0x%02x\n", 997 xfer_status_resp.status0); 998 err = IIC_EBUSERR; 999 break; 1000 } 1001 1002 } while (err == ERESTART); 1003 out: 1004 return (err); 1005 } 1006 1007 static int 1008 cp2112iic_read_data(struct cp2112iic_softc *sc, void *data, uint16_t in_len, 1009 uint16_t *out_len) 1010 { 1011 struct i2c_data_read_force_send_req data_read_force_send; 1012 struct i2c_data_read_resp data_read_resp; 1013 int err; 1014 1015 mtx_assert(&sc->io.lock, MA_OWNED); 1016 1017 /* 1018 * Prepare to receive a response interrupt even before the request 1019 * transfer is confirmed (USB_ST_TRANSFERED). 1020 */ 1021 1022 if (in_len > sizeof(data_read_resp.data)) 1023 in_len = sizeof(data_read_resp.data); 1024 data_read_force_send.id = CP2112_REQ_SMB_READ_FORCE_SEND; 1025 data_read_force_send.len = htobe16(in_len); 1026 err = cp2112iic_req_resp(sc, 1027 &data_read_force_send, sizeof(data_read_force_send), 1028 &data_read_resp, sizeof(data_read_resp)); 1029 if (err != 0) 1030 goto out; 1031 1032 if (data_read_resp.id != CP2112_REQ_SMB_READ_RESPONSE) { 1033 device_printf(sc->dev, 1034 "unexpected response 0x%02x to data read request\n", 1035 data_read_resp.id); 1036 err = IIC_EBUSERR; 1037 goto out; 1038 } 1039 1040 DTRACE_PROBE2(read__response, uint8_t, data_read_resp.status, 1041 uint8_t, data_read_resp.len); 1042 1043 /* 1044 * We expect either the request completed status or, more typical for 1045 * this driver, the bus idle status because of the preceding 1046 * Force Read Status command (which is not an I2C request). 1047 */ 1048 if (data_read_resp.status != CP2112_IIC_STATUS0_CMP && 1049 data_read_resp.status != CP2112_IIC_STATUS0_IDLE) { 1050 err = IIC_EBUSERR; 1051 goto out; 1052 } 1053 if (data_read_resp.len > in_len) { 1054 device_printf(sc->dev, "device returns more data than asked\n"); 1055 err = IIC_EOVERFLOW; 1056 goto out; 1057 } 1058 1059 *out_len = data_read_resp.len; 1060 if (*out_len > 0) 1061 memcpy(data, data_read_resp.data, *out_len); 1062 out: 1063 return (err); 1064 } 1065 1066 static int 1067 cp2112iic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs) 1068 { 1069 struct cp2112iic_softc *sc = device_get_softc(dev); 1070 struct cp2112_softc *psc = device_get_softc(device_get_parent(dev)); 1071 const char *reason = NULL; 1072 uint32_t i; 1073 uint16_t read_off, to_read; 1074 int err; 1075 1076 /* 1077 * The hardware interface imposes limits on allowed I2C messages. 1078 * It is not possible to explicitly send a start or stop. 1079 * It is not possible to do a zero length transfer. 1080 * For this reason it's impossible to send a message with no data 1081 * at all (like an SMBus quick message). 1082 * Each read or write transfer beginning with the start condition 1083 * and ends with the stop condition. The only exception is that 1084 * it is possible to have a write transfer followed by a read 1085 * transfer to the same slave with the repeated start condition 1086 * between them. 1087 */ 1088 for (i = 0; i < nmsgs; i++) { 1089 if (i == 0 && (msgs[i].flags & IIC_M_NOSTART) != 0) { 1090 reason = "first message without start"; 1091 break; 1092 } 1093 if (i == nmsgs - 1 && (msgs[i].flags & IIC_M_NOSTOP) != 0) { 1094 reason = "last message without stop"; 1095 break; 1096 } 1097 if (msgs[i].len == 0) { 1098 reason = "message with no data"; 1099 break; 1100 } 1101 if ((msgs[i].flags & IIC_M_RD) != 0 && 1102 msgs[i].len > CP2112_IIC_MAX_READ_LEN) { 1103 reason = "too long read"; 1104 break; 1105 } 1106 if ((msgs[i].flags & IIC_M_RD) == 0 && 1107 msgs[i].len > SIZEOF_FIELD(i2c_write_req, data)) { 1108 reason = "too long write"; 1109 break; 1110 } 1111 if ((msgs[i].flags & IIC_M_NOSTART) != 0) { 1112 reason = "message without start or repeated start"; 1113 break; 1114 } 1115 if ((msgs[i].flags & IIC_M_NOSTOP) != 0 && 1116 (msgs[i].flags & IIC_M_RD) != 0) { 1117 reason = "read without stop"; 1118 break; 1119 } 1120 if ((msgs[i].flags & IIC_M_NOSTOP) != 0 && 1121 psc->sc_version < CP2112_IIC_REPSTART_VER) { 1122 reason = "write without stop"; 1123 break; 1124 } 1125 if ((msgs[i].flags & IIC_M_NOSTOP) != 0 && 1126 msgs[i].len > SIZEOF_FIELD(i2c_write_read_req, wdata)) { 1127 reason = "too long write without stop"; 1128 break; 1129 } 1130 if (i > 0) { 1131 if ((msgs[i - 1].flags & IIC_M_NOSTOP) != 0 && 1132 msgs[i].slave != msgs[i - 1].slave) { 1133 reason = "change of slave without stop"; 1134 break; 1135 } 1136 if ((msgs[i - 1].flags & IIC_M_NOSTOP) != 0 && 1137 (msgs[i].flags & IIC_M_RD) == 0) { 1138 reason = "write after repeated start"; 1139 break; 1140 } 1141 } 1142 } 1143 if (reason != NULL) { 1144 if (bootverbose) 1145 device_printf(dev, "unsupported i2c message: %s\n", 1146 reason); 1147 return (IIC_ENOTSUPP); 1148 } 1149 1150 mtx_lock(&sc->io.lock); 1151 1152 for (i = 0; i < nmsgs; i++) { 1153 if (i + 1 < nmsgs && (msgs[i].flags & IIC_M_NOSTOP) != 0) { 1154 /* 1155 * Combine <write><repeated start><read> into a single 1156 * CP2112 operation. 1157 */ 1158 struct i2c_write_read_req req; 1159 1160 KASSERT((msgs[i].flags & IIC_M_RD) == 0, 1161 ("read without stop")); 1162 KASSERT((msgs[i + 1].flags & IIC_M_RD) != 0, 1163 ("write after write without stop")); 1164 req.id = CP2112_REQ_SMB_WRITE_READ; 1165 req.slave = msgs[i].slave & ~LSB; 1166 to_read = msgs[i + 1].len; 1167 req.rlen = htobe16(to_read); 1168 req.wlen = msgs[i].len; 1169 memcpy(req.wdata, msgs[i].buf, msgs[i].len); 1170 err = cp2112iic_send_req(sc, &req, msgs[i].len + 5); 1171 1172 /* 1173 * The next message is already handled. 1174 * Also needed for read data to go into the right msg. 1175 */ 1176 i++; 1177 } else if ((msgs[i].flags & IIC_M_RD) != 0) { 1178 struct i2c_read_req req; 1179 1180 req.id = CP2112_REQ_SMB_READ; 1181 req.slave = msgs[i].slave & ~LSB; 1182 to_read = msgs[i].len; 1183 req.len = htobe16(to_read); 1184 err = cp2112iic_send_req(sc, &req, sizeof(req)); 1185 } else { 1186 struct i2c_write_req req; 1187 1188 req.id = CP2112_REQ_SMB_WRITE; 1189 req.slave = msgs[i].slave & ~LSB; 1190 req.len = msgs[i].len; 1191 memcpy(req.data, msgs[i].buf, msgs[i].len); 1192 to_read = 0; 1193 err = cp2112iic_send_req(sc, &req, msgs[i].len + 3); 1194 } 1195 if (err != 0) 1196 break; 1197 1198 err = cp2112iic_check_req_status(sc); 1199 if (err != 0) 1200 break; 1201 1202 read_off = 0; 1203 while (to_read > 0) { 1204 uint16_t act_read; 1205 1206 err = cp2112iic_read_data(sc, msgs[i].buf + read_off, 1207 to_read, &act_read); 1208 if (err != 0) 1209 break; 1210 KASSERT(act_read <= to_read, ("cp2112iic_read_data " 1211 "returned more data than asked")); 1212 read_off += act_read; 1213 to_read -= act_read; 1214 } 1215 if (err != 0) 1216 break; 1217 } 1218 1219 mtx_unlock(&sc->io.lock); 1220 return (err); 1221 } 1222 1223 static int 1224 cp2112iic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr) 1225 { 1226 struct i2c_cfg_req i2c_cfg; 1227 struct cp2112iic_softc *sc; 1228 device_t cp2112; 1229 u_int busfreq; 1230 int err; 1231 1232 sc = device_get_softc(dev); 1233 cp2112 = device_get_parent(dev); 1234 if (sc->iicbus_dev == NULL) 1235 busfreq = 100000; 1236 else 1237 busfreq = IICBUS_GET_FREQUENCY(sc->iicbus_dev, speed); 1238 1239 err = cp2112_get_report(cp2112, CP2112_REQ_SMB_CFG, 1240 &i2c_cfg, sizeof(i2c_cfg)); 1241 if (err != 0) { 1242 device_printf(dev, "failed to get CP2112_REQ_SMB_CFG report\n"); 1243 return (err); 1244 } 1245 1246 if (oldaddr != NULL) 1247 *oldaddr = i2c_cfg.slave_addr; 1248 /* 1249 * For simplicity we do not enable Auto Send Read 1250 * because of erratum CP2112_E101 (fixed in version 3). 1251 * 1252 * TODO: set I2C parameters based on configuration preferences: 1253 * - read and write timeouts (no timeout by default), 1254 * - SCL low timeout (disabled by default), 1255 * etc. 1256 * 1257 * TODO: should the device reset request (0x01) be sent? 1258 * If the device disconnects as a result, then no. 1259 */ 1260 i2c_cfg.speed = htobe32(busfreq); 1261 if (addr != 0) 1262 i2c_cfg.slave_addr = addr; 1263 i2c_cfg.auto_send_read = 0; 1264 i2c_cfg.retry_count = htobe16(1); 1265 i2c_cfg.scl_low_timeout = 0; 1266 if (bootverbose) { 1267 device_printf(dev, "speed %d Hz\n", be32toh(i2c_cfg.speed)); 1268 device_printf(dev, "slave addr 0x%02x\n", i2c_cfg.slave_addr); 1269 device_printf(dev, "auto send read %s\n", 1270 i2c_cfg.auto_send_read ? "on" : "off"); 1271 device_printf(dev, "write timeout %d ms (0 - disabled)\n", 1272 be16toh(i2c_cfg.write_timeout)); 1273 device_printf(dev, "read timeout %d ms (0 - disabled)\n", 1274 be16toh(i2c_cfg.read_timeout)); 1275 device_printf(dev, "scl low timeout %s\n", 1276 i2c_cfg.scl_low_timeout ? "on" : "off"); 1277 device_printf(dev, "retry count %d (0 - no limit)\n", 1278 be16toh(i2c_cfg.retry_count)); 1279 } 1280 err = cp2112_set_report(cp2112, CP2112_REQ_SMB_CFG, 1281 &i2c_cfg, sizeof(i2c_cfg)); 1282 if (err != 0) { 1283 device_printf(dev, "failed to set CP2112_REQ_SMB_CFG report\n"); 1284 return (err); 1285 } 1286 return (0); 1287 } 1288 1289 static int 1290 cp2112iic_probe(device_t dev) 1291 { 1292 device_set_desc(dev, "CP2112 I2C interface"); 1293 return (BUS_PROBE_SPECIFIC); 1294 } 1295 1296 static int 1297 cp2112iic_attach(device_t dev) 1298 { 1299 struct cp2112iic_softc *sc; 1300 struct cp2112_softc *psc; 1301 device_t cp2112; 1302 int err; 1303 1304 sc = device_get_softc(dev); 1305 sc->dev = dev; 1306 cp2112 = device_get_parent(dev); 1307 psc = device_get_softc(cp2112); 1308 1309 mtx_init(&sc->io.lock, "cp2112iic lock", NULL, MTX_DEF | MTX_RECURSE); 1310 cv_init(&sc->io.cv, "cp2112iic cv"); 1311 1312 err = usbd_transfer_setup(psc->sc_udev, 1313 &psc->sc_iface_index, sc->xfers, cp2112iic_config, 1314 nitems(cp2112iic_config), sc, &sc->io.lock); 1315 if (err != 0) { 1316 device_printf(dev, "usbd_transfer_setup failed %d\n", err); 1317 goto detach; 1318 } 1319 1320 /* Prepare to receive interrupts. */ 1321 mtx_lock(&sc->io.lock); 1322 usbd_transfer_start(sc->xfers[CP2112_INTR_IN]); 1323 mtx_unlock(&sc->io.lock); 1324 1325 sc->iicbus_dev = device_add_child(dev, "iicbus", DEVICE_UNIT_ANY); 1326 if (sc->iicbus_dev == NULL) { 1327 device_printf(dev, "iicbus creation failed\n"); 1328 err = ENXIO; 1329 goto detach; 1330 } 1331 bus_attach_children(dev); 1332 return (0); 1333 1334 detach: 1335 cp2112iic_detach(dev); 1336 return (err); 1337 } 1338 1339 static int 1340 cp2112iic_detach(device_t dev) 1341 { 1342 struct cp2112iic_softc *sc; 1343 int err; 1344 1345 sc = device_get_softc(dev); 1346 err = bus_generic_detach(dev); 1347 if (err != 0) 1348 return (err); 1349 1350 mtx_lock(&sc->io.lock); 1351 usbd_transfer_stop(sc->xfers[CP2112_INTR_IN]); 1352 mtx_unlock(&sc->io.lock); 1353 usbd_transfer_unsetup(sc->xfers, nitems(cp2112iic_config)); 1354 1355 cv_destroy(&sc->io.cv); 1356 mtx_destroy(&sc->io.lock); 1357 1358 return (0); 1359 } 1360 1361 static device_method_t cp2112hid_methods[] = { 1362 DEVMETHOD(device_probe, cp2112_probe), 1363 DEVMETHOD(device_attach, cp2112_attach), 1364 DEVMETHOD(device_detach, bus_generic_detach), 1365 1366 DEVMETHOD_END 1367 }; 1368 1369 static driver_t cp2112hid_driver = { 1370 .name = "cp2112hid", 1371 .methods = cp2112hid_methods, 1372 .size = sizeof(struct cp2112_softc), 1373 }; 1374 1375 DRIVER_MODULE(cp2112hid, uhub, cp2112hid_driver, NULL, NULL); 1376 MODULE_DEPEND(cp2112hid, usb, 1, 1, 1); 1377 MODULE_VERSION(cp2112hid, 1); 1378 USB_PNP_HOST_INFO(cp2112_devs); 1379 1380 static device_method_t cp2112gpio_methods[] = { 1381 /* Device */ 1382 DEVMETHOD(device_probe, cp2112gpio_probe), 1383 DEVMETHOD(device_attach, cp2112gpio_attach), 1384 DEVMETHOD(device_detach, cp2112gpio_detach), 1385 1386 /* GPIO */ 1387 DEVMETHOD(gpio_get_bus, cp2112_gpio_get_bus), 1388 DEVMETHOD(gpio_pin_max, cp2112_gpio_pin_max), 1389 DEVMETHOD(gpio_pin_get, cp2112_gpio_pin_get), 1390 DEVMETHOD(gpio_pin_set, cp2112_gpio_pin_set), 1391 DEVMETHOD(gpio_pin_toggle, cp2112_gpio_pin_toggle), 1392 DEVMETHOD(gpio_pin_getname, cp2112_gpio_pin_getname), 1393 DEVMETHOD(gpio_pin_getcaps, cp2112_gpio_pin_getcaps), 1394 DEVMETHOD(gpio_pin_getflags, cp2112_gpio_pin_getflags), 1395 DEVMETHOD(gpio_pin_setflags, cp2112_gpio_pin_setflags), 1396 1397 DEVMETHOD_END 1398 }; 1399 1400 static driver_t cp2112gpio_driver = { 1401 .name = "gpio", 1402 .methods = cp2112gpio_methods, 1403 .size = sizeof(struct cp2112gpio_softc), 1404 }; 1405 1406 DRIVER_MODULE(cp2112gpio, cp2112hid, cp2112gpio_driver, NULL, NULL); 1407 MODULE_DEPEND(cp2112gpio, cp2112hid, 1, 1, 1); 1408 MODULE_DEPEND(cp2112gpio, gpiobus, 1, 1, 1); 1409 MODULE_VERSION(cp2112gpio, 1); 1410 1411 static device_method_t cp2112iic_methods[] = { 1412 /* Device interface */ 1413 DEVMETHOD(device_probe, cp2112iic_probe), 1414 DEVMETHOD(device_attach, cp2112iic_attach), 1415 DEVMETHOD(device_detach, cp2112iic_detach), 1416 1417 /* I2C methods */ 1418 DEVMETHOD(iicbus_transfer, cp2112iic_transfer), 1419 DEVMETHOD(iicbus_reset, cp2112iic_reset), 1420 DEVMETHOD(iicbus_callback, iicbus_null_callback), 1421 1422 DEVMETHOD_END 1423 }; 1424 1425 static driver_t cp2112iic_driver = { 1426 "iichb", 1427 cp2112iic_methods, 1428 sizeof(struct cp2112iic_softc) 1429 }; 1430 1431 DRIVER_MODULE(cp2112iic, cp2112hid, cp2112iic_driver, NULL, NULL); 1432 MODULE_DEPEND(cp2112iic, cp2112hid, 1, 1, 1); 1433 MODULE_DEPEND(cp2112iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); 1434 MODULE_VERSION(cp2112iic, 1); 1435