1 #include <sys/cdefs.h> 2 /*- 3 * SPDX-License-Identifier: BSD-2-Clause 4 * 5 * Copyright (c) 2004 Dag-Erling Coïdan Smørgrav 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer 13 * in this position and unchanged. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to 34 * RS232 bridges. 35 */ 36 37 #include <sys/stdint.h> 38 #include <sys/stddef.h> 39 #include <sys/param.h> 40 #include <sys/queue.h> 41 #include <sys/types.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/bus.h> 45 #include <sys/module.h> 46 #include <sys/lock.h> 47 #include <sys/mutex.h> 48 #include <sys/condvar.h> 49 #include <sys/sysctl.h> 50 #include <sys/sx.h> 51 #include <sys/unistd.h> 52 #include <sys/callout.h> 53 #include <sys/malloc.h> 54 #include <sys/priv.h> 55 56 #include <dev/hid/hid.h> 57 58 #include <dev/usb/usb.h> 59 #include <dev/usb/usbdi.h> 60 #include <dev/usb/usbdi_util.h> 61 #include <dev/usb/usbhid.h> 62 #include "usbdevs.h" 63 64 #define USB_DEBUG_VAR usb_debug 65 #include <dev/usb/usb_debug.h> 66 #include <dev/usb/usb_process.h> 67 68 #include <dev/usb/serial/usb_serial.h> 69 70 #define UCYCOM_MAX_IOLEN (1024 + 2) /* bytes */ 71 72 #define UCYCOM_IFACE_INDEX 0 73 74 enum { 75 UCYCOM_CTRL_RD, 76 UCYCOM_INTR_RD, 77 UCYCOM_N_TRANSFER, 78 }; 79 80 struct ucycom_softc { 81 struct ucom_super_softc sc_super_ucom; 82 struct ucom_softc sc_ucom; 83 84 struct usb_device *sc_udev; 85 struct usb_xfer *sc_xfer[UCYCOM_N_TRANSFER]; 86 struct mtx sc_mtx; 87 88 uint32_t sc_model; 89 #define MODEL_CY7C63743 0x63743 90 #define MODEL_CY7C64013 0x64013 91 92 uint16_t sc_flen; /* feature report length */ 93 uint16_t sc_ilen; /* input report length */ 94 uint16_t sc_olen; /* output report length */ 95 96 uint8_t sc_fid; /* feature report id */ 97 uint8_t sc_iid; /* input report id */ 98 uint8_t sc_oid; /* output report id */ 99 uint8_t sc_cfg; 100 #define UCYCOM_CFG_RESET 0x80 101 #define UCYCOM_CFG_PARODD 0x20 102 #define UCYCOM_CFG_PAREN 0x10 103 #define UCYCOM_CFG_STOPB 0x08 104 #define UCYCOM_CFG_DATAB 0x03 105 uint8_t sc_ist; /* status flags from last input */ 106 uint8_t sc_iface_no; 107 uint8_t sc_temp_cfg[32]; 108 }; 109 110 /* prototypes */ 111 112 static device_probe_t ucycom_probe; 113 static device_attach_t ucycom_attach; 114 static device_detach_t ucycom_detach; 115 static void ucycom_free_softc(struct ucycom_softc *); 116 117 static usb_callback_t ucycom_ctrl_write_callback; 118 static usb_callback_t ucycom_intr_read_callback; 119 120 static void ucycom_free(struct ucom_softc *); 121 static void ucycom_cfg_open(struct ucom_softc *); 122 static void ucycom_start_read(struct ucom_softc *); 123 static void ucycom_stop_read(struct ucom_softc *); 124 static void ucycom_start_write(struct ucom_softc *); 125 static void ucycom_stop_write(struct ucom_softc *); 126 static void ucycom_cfg_write(struct ucycom_softc *, uint32_t, uint8_t); 127 static int ucycom_pre_param(struct ucom_softc *, struct termios *); 128 static void ucycom_cfg_param(struct ucom_softc *, struct termios *); 129 static void ucycom_poll(struct ucom_softc *ucom); 130 131 static const struct usb_config ucycom_config[UCYCOM_N_TRANSFER] = { 132 [UCYCOM_CTRL_RD] = { 133 .type = UE_CONTROL, 134 .endpoint = 0x00, /* Control pipe */ 135 .direction = UE_DIR_ANY, 136 .bufsize = (sizeof(struct usb_device_request) + UCYCOM_MAX_IOLEN), 137 .callback = &ucycom_ctrl_write_callback, 138 .timeout = 1000, /* 1 second */ 139 }, 140 141 [UCYCOM_INTR_RD] = { 142 .type = UE_INTERRUPT, 143 .endpoint = UE_ADDR_ANY, 144 .direction = UE_DIR_IN, 145 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 146 .bufsize = UCYCOM_MAX_IOLEN, 147 .callback = &ucycom_intr_read_callback, 148 }, 149 }; 150 151 static const struct ucom_callback ucycom_callback = { 152 .ucom_cfg_param = &ucycom_cfg_param, 153 .ucom_cfg_open = &ucycom_cfg_open, 154 .ucom_pre_param = &ucycom_pre_param, 155 .ucom_start_read = &ucycom_start_read, 156 .ucom_stop_read = &ucycom_stop_read, 157 .ucom_start_write = &ucycom_start_write, 158 .ucom_stop_write = &ucycom_stop_write, 159 .ucom_poll = &ucycom_poll, 160 .ucom_free = &ucycom_free, 161 }; 162 163 static device_method_t ucycom_methods[] = { 164 DEVMETHOD(device_probe, ucycom_probe), 165 DEVMETHOD(device_attach, ucycom_attach), 166 DEVMETHOD(device_detach, ucycom_detach), 167 DEVMETHOD_END 168 }; 169 170 static driver_t ucycom_driver = { 171 .name = "ucycom", 172 .methods = ucycom_methods, 173 .size = sizeof(struct ucycom_softc), 174 }; 175 176 /* 177 * Supported devices 178 */ 179 static const STRUCT_USB_HOST_ID ucycom_devs[] = { 180 {USB_VPI(USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE, MODEL_CY7C64013)}, 181 }; 182 183 DRIVER_MODULE(ucycom, uhub, ucycom_driver, NULL, NULL); 184 MODULE_DEPEND(ucycom, ucom, 1, 1, 1); 185 MODULE_DEPEND(ucycom, usb, 1, 1, 1); 186 MODULE_DEPEND(ucycom, hid, 1, 1, 1); 187 MODULE_VERSION(ucycom, 1); 188 USB_PNP_HOST_INFO(ucycom_devs); 189 190 #define UCYCOM_DEFAULT_RATE 4800 191 #define UCYCOM_DEFAULT_CFG 0x03 /* N-8-1 */ 192 193 static int 194 ucycom_probe(device_t dev) 195 { 196 struct usb_attach_arg *uaa = device_get_ivars(dev); 197 198 if (uaa->usb_mode != USB_MODE_HOST) { 199 return (ENXIO); 200 } 201 if (uaa->info.bConfigIndex != 0) { 202 return (ENXIO); 203 } 204 if (uaa->info.bIfaceIndex != UCYCOM_IFACE_INDEX) { 205 return (ENXIO); 206 } 207 return (usbd_lookup_id_by_uaa(ucycom_devs, sizeof(ucycom_devs), uaa)); 208 } 209 210 static int 211 ucycom_attach(device_t dev) 212 { 213 struct usb_attach_arg *uaa = device_get_ivars(dev); 214 struct ucycom_softc *sc = device_get_softc(dev); 215 void *urd_ptr = NULL; 216 int32_t error; 217 uint16_t urd_len; 218 uint8_t iface_index; 219 220 sc->sc_udev = uaa->device; 221 222 device_set_usb_desc(dev); 223 mtx_init(&sc->sc_mtx, "ucycom", NULL, MTX_DEF); 224 ucom_ref(&sc->sc_super_ucom); 225 226 DPRINTF("\n"); 227 228 /* get chip model */ 229 sc->sc_model = USB_GET_DRIVER_INFO(uaa); 230 if (sc->sc_model == 0) { 231 device_printf(dev, "unsupported device\n"); 232 goto detach; 233 } 234 device_printf(dev, "Cypress CY7C%X USB to RS232 bridge\n", sc->sc_model); 235 236 /* get report descriptor */ 237 238 error = usbd_req_get_hid_desc(uaa->device, NULL, 239 &urd_ptr, &urd_len, M_USBDEV, 240 UCYCOM_IFACE_INDEX); 241 242 if (error) { 243 device_printf(dev, "failed to get report " 244 "descriptor: %s\n", 245 usbd_errstr(error)); 246 goto detach; 247 } 248 /* get report sizes */ 249 250 sc->sc_flen = hid_report_size_max(urd_ptr, urd_len, hid_feature, &sc->sc_fid); 251 sc->sc_ilen = hid_report_size_max(urd_ptr, urd_len, hid_input, &sc->sc_iid); 252 sc->sc_olen = hid_report_size_max(urd_ptr, urd_len, hid_output, &sc->sc_oid); 253 254 if ((sc->sc_ilen > UCYCOM_MAX_IOLEN) || (sc->sc_ilen < 1) || 255 (sc->sc_olen > UCYCOM_MAX_IOLEN) || (sc->sc_olen < 2) || 256 (sc->sc_flen > UCYCOM_MAX_IOLEN) || (sc->sc_flen < 5)) { 257 device_printf(dev, "invalid report size i=%d, o=%d, f=%d, max=%d\n", 258 sc->sc_ilen, sc->sc_olen, sc->sc_flen, 259 UCYCOM_MAX_IOLEN); 260 goto detach; 261 } 262 sc->sc_iface_no = uaa->info.bIfaceNum; 263 264 iface_index = UCYCOM_IFACE_INDEX; 265 error = usbd_transfer_setup(uaa->device, &iface_index, 266 sc->sc_xfer, ucycom_config, UCYCOM_N_TRANSFER, 267 sc, &sc->sc_mtx); 268 if (error) { 269 device_printf(dev, "allocating USB " 270 "transfers failed\n"); 271 goto detach; 272 } 273 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 274 &ucycom_callback, &sc->sc_mtx); 275 if (error) { 276 goto detach; 277 } 278 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev); 279 280 if (urd_ptr) { 281 free(urd_ptr, M_USBDEV); 282 } 283 284 return (0); /* success */ 285 286 detach: 287 if (urd_ptr) { 288 free(urd_ptr, M_USBDEV); 289 } 290 ucycom_detach(dev); 291 return (ENXIO); 292 } 293 294 static int 295 ucycom_detach(device_t dev) 296 { 297 struct ucycom_softc *sc = device_get_softc(dev); 298 299 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom); 300 usbd_transfer_unsetup(sc->sc_xfer, UCYCOM_N_TRANSFER); 301 302 device_claim_softc(dev); 303 304 ucycom_free_softc(sc); 305 306 return (0); 307 } 308 309 UCOM_UNLOAD_DRAIN(ucycom); 310 311 static void 312 ucycom_free_softc(struct ucycom_softc *sc) 313 { 314 if (ucom_unref(&sc->sc_super_ucom)) { 315 mtx_destroy(&sc->sc_mtx); 316 device_free_softc(sc); 317 } 318 } 319 320 static void 321 ucycom_free(struct ucom_softc *ucom) 322 { 323 ucycom_free_softc(ucom->sc_parent); 324 } 325 326 static void 327 ucycom_cfg_open(struct ucom_softc *ucom) 328 { 329 struct ucycom_softc *sc = ucom->sc_parent; 330 331 /* set default configuration */ 332 ucycom_cfg_write(sc, UCYCOM_DEFAULT_RATE, UCYCOM_DEFAULT_CFG); 333 } 334 335 static void 336 ucycom_start_read(struct ucom_softc *ucom) 337 { 338 struct ucycom_softc *sc = ucom->sc_parent; 339 340 usbd_transfer_start(sc->sc_xfer[UCYCOM_INTR_RD]); 341 } 342 343 static void 344 ucycom_stop_read(struct ucom_softc *ucom) 345 { 346 struct ucycom_softc *sc = ucom->sc_parent; 347 348 usbd_transfer_stop(sc->sc_xfer[UCYCOM_INTR_RD]); 349 } 350 351 static void 352 ucycom_start_write(struct ucom_softc *ucom) 353 { 354 struct ucycom_softc *sc = ucom->sc_parent; 355 356 usbd_transfer_start(sc->sc_xfer[UCYCOM_CTRL_RD]); 357 } 358 359 static void 360 ucycom_stop_write(struct ucom_softc *ucom) 361 { 362 struct ucycom_softc *sc = ucom->sc_parent; 363 364 usbd_transfer_stop(sc->sc_xfer[UCYCOM_CTRL_RD]); 365 } 366 367 static void 368 ucycom_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error) 369 { 370 struct ucycom_softc *sc = usbd_xfer_softc(xfer); 371 struct usb_device_request req; 372 struct usb_page_cache *pc0, *pc1; 373 uint8_t data[2]; 374 uint8_t offset; 375 uint32_t actlen; 376 377 pc0 = usbd_xfer_get_frame(xfer, 0); 378 pc1 = usbd_xfer_get_frame(xfer, 1); 379 380 switch (USB_GET_STATE(xfer)) { 381 case USB_ST_TRANSFERRED: 382 tr_transferred: 383 case USB_ST_SETUP: 384 385 switch (sc->sc_model) { 386 case MODEL_CY7C63743: 387 offset = 1; 388 break; 389 case MODEL_CY7C64013: 390 offset = 2; 391 break; 392 default: 393 offset = 0; 394 break; 395 } 396 397 if (ucom_get_data(&sc->sc_ucom, pc1, offset, 398 sc->sc_olen - offset, &actlen)) { 399 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 400 req.bRequest = UR_SET_REPORT; 401 USETW2(req.wValue, UHID_OUTPUT_REPORT, sc->sc_oid); 402 req.wIndex[0] = sc->sc_iface_no; 403 req.wIndex[1] = 0; 404 USETW(req.wLength, sc->sc_olen); 405 406 switch (sc->sc_model) { 407 case MODEL_CY7C63743: 408 data[0] = actlen; 409 break; 410 case MODEL_CY7C64013: 411 data[0] = 0; 412 data[1] = actlen; 413 break; 414 default: 415 break; 416 } 417 418 usbd_copy_in(pc0, 0, &req, sizeof(req)); 419 usbd_copy_in(pc1, 0, data, offset); 420 421 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 422 usbd_xfer_set_frame_len(xfer, 1, sc->sc_olen); 423 usbd_xfer_set_frames(xfer, sc->sc_olen ? 2 : 1); 424 usbd_transfer_submit(xfer); 425 } 426 return; 427 428 default: /* Error */ 429 if (error == USB_ERR_CANCELLED) { 430 return; 431 } 432 DPRINTF("error=%s\n", 433 usbd_errstr(error)); 434 goto tr_transferred; 435 } 436 } 437 438 static void 439 ucycom_cfg_write(struct ucycom_softc *sc, uint32_t baud, uint8_t cfg) 440 { 441 struct usb_device_request req; 442 uint16_t len; 443 usb_error_t err; 444 445 len = sc->sc_flen; 446 if (len > sizeof(sc->sc_temp_cfg)) { 447 len = sizeof(sc->sc_temp_cfg); 448 } 449 sc->sc_cfg = cfg; 450 451 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 452 req.bRequest = UR_SET_REPORT; 453 USETW2(req.wValue, UHID_FEATURE_REPORT, sc->sc_fid); 454 req.wIndex[0] = sc->sc_iface_no; 455 req.wIndex[1] = 0; 456 USETW(req.wLength, len); 457 458 sc->sc_temp_cfg[0] = (baud & 0xff); 459 sc->sc_temp_cfg[1] = (baud >> 8) & 0xff; 460 sc->sc_temp_cfg[2] = (baud >> 16) & 0xff; 461 sc->sc_temp_cfg[3] = (baud >> 24) & 0xff; 462 sc->sc_temp_cfg[4] = cfg; 463 464 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 465 &req, sc->sc_temp_cfg, 0, 1000); 466 if (err) { 467 DPRINTFN(0, "device request failed, err=%s " 468 "(ignored)\n", usbd_errstr(err)); 469 } 470 } 471 472 static int 473 ucycom_pre_param(struct ucom_softc *ucom, struct termios *t) 474 { 475 switch (t->c_ospeed) { 476 case 600: 477 case 1200: 478 case 2400: 479 case 4800: 480 case 9600: 481 case 19200: 482 case 38400: 483 case 57600: 484 #if 0 485 /* 486 * Stock chips only support standard baud rates in the 600 - 57600 487 * range, but higher rates can be achieved using custom firmware. 488 */ 489 case 115200: 490 case 153600: 491 case 192000: 492 #endif 493 break; 494 default: 495 return (EINVAL); 496 } 497 return (0); 498 } 499 500 static void 501 ucycom_cfg_param(struct ucom_softc *ucom, struct termios *t) 502 { 503 struct ucycom_softc *sc = ucom->sc_parent; 504 uint8_t cfg; 505 506 DPRINTF("\n"); 507 508 if (t->c_cflag & CIGNORE) { 509 cfg = sc->sc_cfg; 510 } else { 511 cfg = 0; 512 switch (t->c_cflag & CSIZE) { 513 default: 514 case CS8: 515 ++cfg; 516 case CS7: 517 ++cfg; 518 case CS6: 519 ++cfg; 520 case CS5: 521 break; 522 } 523 524 if (t->c_cflag & CSTOPB) 525 cfg |= UCYCOM_CFG_STOPB; 526 if (t->c_cflag & PARENB) 527 cfg |= UCYCOM_CFG_PAREN; 528 if (t->c_cflag & PARODD) 529 cfg |= UCYCOM_CFG_PARODD; 530 } 531 532 ucycom_cfg_write(sc, t->c_ospeed, cfg); 533 } 534 535 static void 536 ucycom_intr_read_callback(struct usb_xfer *xfer, usb_error_t error) 537 { 538 struct ucycom_softc *sc = usbd_xfer_softc(xfer); 539 struct usb_page_cache *pc; 540 uint8_t buf[2]; 541 uint32_t offset; 542 int len; 543 int actlen; 544 545 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 546 pc = usbd_xfer_get_frame(xfer, 0); 547 548 switch (USB_GET_STATE(xfer)) { 549 case USB_ST_TRANSFERRED: 550 switch (sc->sc_model) { 551 case MODEL_CY7C63743: 552 if (actlen < 1) { 553 goto tr_setup; 554 } 555 usbd_copy_out(pc, 0, buf, 1); 556 557 sc->sc_ist = buf[0] & ~0x07; 558 len = buf[0] & 0x07; 559 560 actlen--; 561 offset = 1; 562 563 break; 564 565 case MODEL_CY7C64013: 566 if (actlen < 2) { 567 goto tr_setup; 568 } 569 usbd_copy_out(pc, 0, buf, 2); 570 571 sc->sc_ist = buf[0] & ~0x07; 572 len = buf[1]; 573 574 actlen -= 2; 575 offset = 2; 576 577 break; 578 579 default: 580 DPRINTFN(0, "unsupported model number\n"); 581 goto tr_setup; 582 } 583 584 if (len > actlen) 585 len = actlen; 586 if (len) 587 ucom_put_data(&sc->sc_ucom, pc, offset, len); 588 /* FALLTHROUGH */ 589 case USB_ST_SETUP: 590 tr_setup: 591 usbd_xfer_set_frame_len(xfer, 0, sc->sc_ilen); 592 usbd_transfer_submit(xfer); 593 return; 594 595 default: /* Error */ 596 if (error != USB_ERR_CANCELLED) { 597 /* try to clear stall first */ 598 usbd_xfer_set_stall(xfer); 599 goto tr_setup; 600 } 601 return; 602 } 603 } 604 605 static void 606 ucycom_poll(struct ucom_softc *ucom) 607 { 608 struct ucycom_softc *sc = ucom->sc_parent; 609 usbd_transfer_poll(sc->sc_xfer, UCYCOM_N_TRANSFER); 610 } 611