1 #include <sys/cdefs.h> 2 __FBSDID("$FreeBSD$"); 3 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/linker_set.h> 46 #include <sys/module.h> 47 #include <sys/lock.h> 48 #include <sys/mutex.h> 49 #include <sys/condvar.h> 50 #include <sys/sysctl.h> 51 #include <sys/sx.h> 52 #include <sys/unistd.h> 53 #include <sys/callout.h> 54 #include <sys/malloc.h> 55 #include <sys/priv.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 #include <dev/usb/usb_process.h> 66 67 #include <dev/usb/serial/usb_serial.h> 68 69 #define UCYCOM_MAX_IOLEN (1024 + 2) /* bytes */ 70 71 #define UCYCOM_IFACE_INDEX 0 72 73 enum { 74 UCYCOM_CTRL_RD, 75 UCYCOM_INTR_RD, 76 UCYCOM_N_TRANSFER, 77 }; 78 79 struct ucycom_softc { 80 struct ucom_super_softc sc_super_ucom; 81 struct ucom_softc sc_ucom; 82 83 struct usb_device *sc_udev; 84 struct usb_xfer *sc_xfer[UCYCOM_N_TRANSFER]; 85 struct mtx sc_mtx; 86 87 uint32_t sc_model; 88 #define MODEL_CY7C63743 0x63743 89 #define MODEL_CY7C64013 0x64013 90 91 uint16_t sc_flen; /* feature report length */ 92 uint16_t sc_ilen; /* input report length */ 93 uint16_t sc_olen; /* output report length */ 94 95 uint8_t sc_fid; /* feature report id */ 96 uint8_t sc_iid; /* input report id */ 97 uint8_t sc_oid; /* output report id */ 98 uint8_t sc_cfg; 99 #define UCYCOM_CFG_RESET 0x80 100 #define UCYCOM_CFG_PARODD 0x20 101 #define UCYCOM_CFG_PAREN 0x10 102 #define UCYCOM_CFG_STOPB 0x08 103 #define UCYCOM_CFG_DATAB 0x03 104 uint8_t sc_ist; /* status flags from last input */ 105 uint8_t sc_name[16]; 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 116 static usb_callback_t ucycom_ctrl_write_callback; 117 static usb_callback_t ucycom_intr_read_callback; 118 119 static void ucycom_cfg_open(struct ucom_softc *); 120 static void ucycom_start_read(struct ucom_softc *); 121 static void ucycom_stop_read(struct ucom_softc *); 122 static void ucycom_start_write(struct ucom_softc *); 123 static void ucycom_stop_write(struct ucom_softc *); 124 static void ucycom_cfg_write(struct ucycom_softc *, uint32_t, uint8_t); 125 static int ucycom_pre_param(struct ucom_softc *, struct termios *); 126 static void ucycom_cfg_param(struct ucom_softc *, struct termios *); 127 static void ucycom_poll(struct ucom_softc *ucom); 128 129 static const struct usb_config ucycom_config[UCYCOM_N_TRANSFER] = { 130 131 [UCYCOM_CTRL_RD] = { 132 .type = UE_CONTROL, 133 .endpoint = 0x00, /* Control pipe */ 134 .direction = UE_DIR_ANY, 135 .bufsize = (sizeof(struct usb_device_request) + UCYCOM_MAX_IOLEN), 136 .callback = &ucycom_ctrl_write_callback, 137 .timeout = 1000, /* 1 second */ 138 }, 139 140 [UCYCOM_INTR_RD] = { 141 .type = UE_INTERRUPT, 142 .endpoint = UE_ADDR_ANY, 143 .direction = UE_DIR_IN, 144 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 145 .bufsize = UCYCOM_MAX_IOLEN, 146 .callback = &ucycom_intr_read_callback, 147 }, 148 }; 149 150 static const struct ucom_callback ucycom_callback = { 151 .ucom_cfg_param = &ucycom_cfg_param, 152 .ucom_cfg_open = &ucycom_cfg_open, 153 .ucom_pre_param = &ucycom_pre_param, 154 .ucom_start_read = &ucycom_start_read, 155 .ucom_stop_read = &ucycom_stop_read, 156 .ucom_start_write = &ucycom_start_write, 157 .ucom_stop_write = &ucycom_stop_write, 158 .ucom_poll = &ucycom_poll, 159 }; 160 161 static device_method_t ucycom_methods[] = { 162 DEVMETHOD(device_probe, ucycom_probe), 163 DEVMETHOD(device_attach, ucycom_attach), 164 DEVMETHOD(device_detach, ucycom_detach), 165 {0, 0} 166 }; 167 168 static devclass_t ucycom_devclass; 169 170 static driver_t ucycom_driver = { 171 .name = "ucycom", 172 .methods = ucycom_methods, 173 .size = sizeof(struct ucycom_softc), 174 }; 175 176 DRIVER_MODULE(ucycom, uhub, ucycom_driver, ucycom_devclass, NULL, 0); 177 MODULE_DEPEND(ucycom, ucom, 1, 1, 1); 178 MODULE_DEPEND(ucycom, usb, 1, 1, 1); 179 MODULE_VERSION(ucycom, 1); 180 181 /* 182 * Supported devices 183 */ 184 static const struct usb_device_id ucycom_devs[] = { 185 {USB_VPI(USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE, MODEL_CY7C64013)}, 186 }; 187 188 #define UCYCOM_DEFAULT_RATE 4800 189 #define UCYCOM_DEFAULT_CFG 0x03 /* N-8-1 */ 190 191 static int 192 ucycom_probe(device_t dev) 193 { 194 struct usb_attach_arg *uaa = device_get_ivars(dev); 195 196 if (uaa->usb_mode != USB_MODE_HOST) { 197 return (ENXIO); 198 } 199 if (uaa->info.bConfigIndex != 0) { 200 return (ENXIO); 201 } 202 if (uaa->info.bIfaceIndex != UCYCOM_IFACE_INDEX) { 203 return (ENXIO); 204 } 205 return (usbd_lookup_id_by_uaa(ucycom_devs, sizeof(ucycom_devs), uaa)); 206 } 207 208 static int 209 ucycom_attach(device_t dev) 210 { 211 struct usb_attach_arg *uaa = device_get_ivars(dev); 212 struct ucycom_softc *sc = device_get_softc(dev); 213 void *urd_ptr = NULL; 214 int32_t error; 215 uint16_t urd_len; 216 uint8_t iface_index; 217 218 sc->sc_udev = uaa->device; 219 220 device_set_usb_desc(dev); 221 mtx_init(&sc->sc_mtx, "ucycom", NULL, MTX_DEF); 222 223 snprintf(sc->sc_name, sizeof(sc->sc_name), 224 "%s", device_get_nameunit(dev)); 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(urd_ptr, urd_len, hid_feature, &sc->sc_fid); 251 sc->sc_ilen = hid_report_size(urd_ptr, urd_len, hid_input, &sc->sc_iid); 252 sc->sc_olen = hid_report_size(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 mtx_destroy(&sc->sc_mtx); 302 303 return (0); 304 } 305 306 static void 307 ucycom_cfg_open(struct ucom_softc *ucom) 308 { 309 struct ucycom_softc *sc = ucom->sc_parent; 310 311 /* set default configuration */ 312 ucycom_cfg_write(sc, UCYCOM_DEFAULT_RATE, UCYCOM_DEFAULT_CFG); 313 } 314 315 static void 316 ucycom_start_read(struct ucom_softc *ucom) 317 { 318 struct ucycom_softc *sc = ucom->sc_parent; 319 320 usbd_transfer_start(sc->sc_xfer[UCYCOM_INTR_RD]); 321 } 322 323 static void 324 ucycom_stop_read(struct ucom_softc *ucom) 325 { 326 struct ucycom_softc *sc = ucom->sc_parent; 327 328 usbd_transfer_stop(sc->sc_xfer[UCYCOM_INTR_RD]); 329 } 330 331 static void 332 ucycom_start_write(struct ucom_softc *ucom) 333 { 334 struct ucycom_softc *sc = ucom->sc_parent; 335 336 usbd_transfer_start(sc->sc_xfer[UCYCOM_CTRL_RD]); 337 } 338 339 static void 340 ucycom_stop_write(struct ucom_softc *ucom) 341 { 342 struct ucycom_softc *sc = ucom->sc_parent; 343 344 usbd_transfer_stop(sc->sc_xfer[UCYCOM_CTRL_RD]); 345 } 346 347 static void 348 ucycom_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error) 349 { 350 struct ucycom_softc *sc = usbd_xfer_softc(xfer); 351 struct usb_device_request req; 352 struct usb_page_cache *pc0, *pc1; 353 uint8_t data[2]; 354 uint8_t offset; 355 uint32_t actlen; 356 357 pc0 = usbd_xfer_get_frame(xfer, 0); 358 pc1 = usbd_xfer_get_frame(xfer, 1); 359 360 switch (USB_GET_STATE(xfer)) { 361 case USB_ST_TRANSFERRED: 362 tr_transferred: 363 case USB_ST_SETUP: 364 365 switch (sc->sc_model) { 366 case MODEL_CY7C63743: 367 offset = 1; 368 break; 369 case MODEL_CY7C64013: 370 offset = 2; 371 break; 372 default: 373 offset = 0; 374 break; 375 } 376 377 if (ucom_get_data(&sc->sc_ucom, pc1, offset, 378 sc->sc_olen - offset, &actlen)) { 379 380 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 381 req.bRequest = UR_SET_REPORT; 382 USETW2(req.wValue, UHID_OUTPUT_REPORT, sc->sc_oid); 383 req.wIndex[0] = sc->sc_iface_no; 384 req.wIndex[1] = 0; 385 USETW(req.wLength, sc->sc_olen); 386 387 switch (sc->sc_model) { 388 case MODEL_CY7C63743: 389 data[0] = actlen; 390 break; 391 case MODEL_CY7C64013: 392 data[0] = 0; 393 data[1] = actlen; 394 break; 395 default: 396 break; 397 } 398 399 usbd_copy_in(pc0, 0, &req, sizeof(req)); 400 usbd_copy_in(pc1, 0, data, offset); 401 402 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 403 usbd_xfer_set_frame_len(xfer, 1, sc->sc_olen); 404 usbd_xfer_set_frames(xfer, sc->sc_olen ? 2 : 1); 405 usbd_transfer_submit(xfer); 406 } 407 return; 408 409 default: /* Error */ 410 if (error == USB_ERR_CANCELLED) { 411 return; 412 } 413 DPRINTF("error=%s\n", 414 usbd_errstr(error)); 415 goto tr_transferred; 416 } 417 } 418 419 static void 420 ucycom_cfg_write(struct ucycom_softc *sc, uint32_t baud, uint8_t cfg) 421 { 422 struct usb_device_request req; 423 uint16_t len; 424 usb_error_t err; 425 426 len = sc->sc_flen; 427 if (len > sizeof(sc->sc_temp_cfg)) { 428 len = sizeof(sc->sc_temp_cfg); 429 } 430 sc->sc_cfg = cfg; 431 432 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 433 req.bRequest = UR_SET_REPORT; 434 USETW2(req.wValue, UHID_FEATURE_REPORT, sc->sc_fid); 435 req.wIndex[0] = sc->sc_iface_no; 436 req.wIndex[1] = 0; 437 USETW(req.wLength, len); 438 439 sc->sc_temp_cfg[0] = (baud & 0xff); 440 sc->sc_temp_cfg[1] = (baud >> 8) & 0xff; 441 sc->sc_temp_cfg[2] = (baud >> 16) & 0xff; 442 sc->sc_temp_cfg[3] = (baud >> 24) & 0xff; 443 sc->sc_temp_cfg[4] = cfg; 444 445 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 446 &req, sc->sc_temp_cfg, 0, 1000); 447 if (err) { 448 DPRINTFN(0, "device request failed, err=%s " 449 "(ignored)\n", usbd_errstr(err)); 450 } 451 } 452 453 static int 454 ucycom_pre_param(struct ucom_softc *ucom, struct termios *t) 455 { 456 switch (t->c_ospeed) { 457 case 600: 458 case 1200: 459 case 2400: 460 case 4800: 461 case 9600: 462 case 19200: 463 case 38400: 464 case 57600: 465 #if 0 466 /* 467 * Stock chips only support standard baud rates in the 600 - 57600 468 * range, but higher rates can be achieved using custom firmware. 469 */ 470 case 115200: 471 case 153600: 472 case 192000: 473 #endif 474 break; 475 default: 476 return (EINVAL); 477 } 478 return (0); 479 } 480 481 static void 482 ucycom_cfg_param(struct ucom_softc *ucom, struct termios *t) 483 { 484 struct ucycom_softc *sc = ucom->sc_parent; 485 uint8_t cfg; 486 487 DPRINTF("\n"); 488 489 if (t->c_cflag & CIGNORE) { 490 cfg = sc->sc_cfg; 491 } else { 492 cfg = 0; 493 switch (t->c_cflag & CSIZE) { 494 default: 495 case CS8: 496 ++cfg; 497 case CS7: 498 ++cfg; 499 case CS6: 500 ++cfg; 501 case CS5: 502 break; 503 } 504 505 if (t->c_cflag & CSTOPB) 506 cfg |= UCYCOM_CFG_STOPB; 507 if (t->c_cflag & PARENB) 508 cfg |= UCYCOM_CFG_PAREN; 509 if (t->c_cflag & PARODD) 510 cfg |= UCYCOM_CFG_PARODD; 511 } 512 513 ucycom_cfg_write(sc, t->c_ospeed, cfg); 514 } 515 516 static void 517 ucycom_intr_read_callback(struct usb_xfer *xfer, usb_error_t error) 518 { 519 struct ucycom_softc *sc = usbd_xfer_softc(xfer); 520 struct usb_page_cache *pc; 521 uint8_t buf[2]; 522 uint32_t offset; 523 uint32_t len; 524 int actlen; 525 526 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 527 pc = usbd_xfer_get_frame(xfer, 0); 528 529 switch (USB_GET_STATE(xfer)) { 530 case USB_ST_TRANSFERRED: 531 switch (sc->sc_model) { 532 case MODEL_CY7C63743: 533 if (actlen < 1) { 534 goto tr_setup; 535 } 536 usbd_copy_out(pc, 0, buf, 1); 537 538 sc->sc_ist = buf[0] & ~0x07; 539 len = buf[0] & 0x07; 540 541 actlen--; 542 offset = 1; 543 544 break; 545 546 case MODEL_CY7C64013: 547 if (actlen < 2) { 548 goto tr_setup; 549 } 550 usbd_copy_out(pc, 0, buf, 2); 551 552 sc->sc_ist = buf[0] & ~0x07; 553 len = buf[1]; 554 555 actlen -= 2; 556 offset = 2; 557 558 break; 559 560 default: 561 DPRINTFN(0, "unsupported model number\n"); 562 goto tr_setup; 563 } 564 565 if (len > actlen) 566 len = actlen; 567 if (len) 568 ucom_put_data(&sc->sc_ucom, pc, offset, len); 569 /* FALLTHROUGH */ 570 case USB_ST_SETUP: 571 tr_setup: 572 usbd_xfer_set_frame_len(xfer, 0, sc->sc_ilen); 573 usbd_transfer_submit(xfer); 574 return; 575 576 default: /* Error */ 577 if (error != USB_ERR_CANCELLED) { 578 /* try to clear stall first */ 579 usbd_xfer_set_stall(xfer); 580 goto tr_setup; 581 } 582 return; 583 584 } 585 } 586 587 static void 588 ucycom_poll(struct ucom_softc *ucom) 589 { 590 struct ucycom_softc *sc = ucom->sc_parent; 591 usbd_transfer_poll(sc->sc_xfer, UCYCOM_N_TRANSFER); 592 } 593