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 180 /* 181 * Supported devices 182 */ 183 static const struct usb_device_id ucycom_devs[] = { 184 {USB_VPI(USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE, MODEL_CY7C64013)}, 185 }; 186 187 #define UCYCOM_DEFAULT_RATE 4800 188 #define UCYCOM_DEFAULT_CFG 0x03 /* N-8-1 */ 189 190 static int 191 ucycom_probe(device_t dev) 192 { 193 struct usb_attach_arg *uaa = device_get_ivars(dev); 194 195 if (uaa->usb_mode != USB_MODE_HOST) { 196 return (ENXIO); 197 } 198 if (uaa->info.bConfigIndex != 0) { 199 return (ENXIO); 200 } 201 if (uaa->info.bIfaceIndex != UCYCOM_IFACE_INDEX) { 202 return (ENXIO); 203 } 204 return (usbd_lookup_id_by_uaa(ucycom_devs, sizeof(ucycom_devs), uaa)); 205 } 206 207 static int 208 ucycom_attach(device_t dev) 209 { 210 struct usb_attach_arg *uaa = device_get_ivars(dev); 211 struct ucycom_softc *sc = device_get_softc(dev); 212 void *urd_ptr = NULL; 213 int32_t error; 214 uint16_t urd_len; 215 uint8_t iface_index; 216 217 sc->sc_udev = uaa->device; 218 219 device_set_usb_desc(dev); 220 mtx_init(&sc->sc_mtx, "ucycom", NULL, MTX_DEF); 221 222 snprintf(sc->sc_name, sizeof(sc->sc_name), 223 "%s", device_get_nameunit(dev)); 224 225 DPRINTF("\n"); 226 227 /* get chip model */ 228 sc->sc_model = USB_GET_DRIVER_INFO(uaa); 229 if (sc->sc_model == 0) { 230 device_printf(dev, "unsupported device\n"); 231 goto detach; 232 } 233 device_printf(dev, "Cypress CY7C%X USB to RS232 bridge\n", sc->sc_model); 234 235 /* get report descriptor */ 236 237 error = usbd_req_get_hid_desc(uaa->device, NULL, 238 &urd_ptr, &urd_len, M_USBDEV, 239 UCYCOM_IFACE_INDEX); 240 241 if (error) { 242 device_printf(dev, "failed to get report " 243 "descriptor: %s\n", 244 usbd_errstr(error)); 245 goto detach; 246 } 247 /* get report sizes */ 248 249 sc->sc_flen = hid_report_size(urd_ptr, urd_len, hid_feature, &sc->sc_fid); 250 sc->sc_ilen = hid_report_size(urd_ptr, urd_len, hid_input, &sc->sc_iid); 251 sc->sc_olen = hid_report_size(urd_ptr, urd_len, hid_output, &sc->sc_oid); 252 253 if ((sc->sc_ilen > UCYCOM_MAX_IOLEN) || (sc->sc_ilen < 1) || 254 (sc->sc_olen > UCYCOM_MAX_IOLEN) || (sc->sc_olen < 2) || 255 (sc->sc_flen > UCYCOM_MAX_IOLEN) || (sc->sc_flen < 5)) { 256 device_printf(dev, "invalid report size i=%d, o=%d, f=%d, max=%d\n", 257 sc->sc_ilen, sc->sc_olen, sc->sc_flen, 258 UCYCOM_MAX_IOLEN); 259 goto detach; 260 } 261 sc->sc_iface_no = uaa->info.bIfaceNum; 262 263 iface_index = UCYCOM_IFACE_INDEX; 264 error = usbd_transfer_setup(uaa->device, &iface_index, 265 sc->sc_xfer, ucycom_config, UCYCOM_N_TRANSFER, 266 sc, &sc->sc_mtx); 267 if (error) { 268 device_printf(dev, "allocating USB " 269 "transfers failed\n"); 270 goto detach; 271 } 272 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 273 &ucycom_callback, &sc->sc_mtx); 274 275 if (error) { 276 goto detach; 277 } 278 if (urd_ptr) { 279 free(urd_ptr, M_USBDEV); 280 } 281 return (0); /* success */ 282 283 detach: 284 if (urd_ptr) { 285 free(urd_ptr, M_USBDEV); 286 } 287 ucycom_detach(dev); 288 return (ENXIO); 289 } 290 291 static int 292 ucycom_detach(device_t dev) 293 { 294 struct ucycom_softc *sc = device_get_softc(dev); 295 296 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1); 297 usbd_transfer_unsetup(sc->sc_xfer, UCYCOM_N_TRANSFER); 298 mtx_destroy(&sc->sc_mtx); 299 300 return (0); 301 } 302 303 static void 304 ucycom_cfg_open(struct ucom_softc *ucom) 305 { 306 struct ucycom_softc *sc = ucom->sc_parent; 307 308 /* set default configuration */ 309 ucycom_cfg_write(sc, UCYCOM_DEFAULT_RATE, UCYCOM_DEFAULT_CFG); 310 } 311 312 static void 313 ucycom_start_read(struct ucom_softc *ucom) 314 { 315 struct ucycom_softc *sc = ucom->sc_parent; 316 317 usbd_transfer_start(sc->sc_xfer[UCYCOM_INTR_RD]); 318 } 319 320 static void 321 ucycom_stop_read(struct ucom_softc *ucom) 322 { 323 struct ucycom_softc *sc = ucom->sc_parent; 324 325 usbd_transfer_stop(sc->sc_xfer[UCYCOM_INTR_RD]); 326 } 327 328 static void 329 ucycom_start_write(struct ucom_softc *ucom) 330 { 331 struct ucycom_softc *sc = ucom->sc_parent; 332 333 usbd_transfer_start(sc->sc_xfer[UCYCOM_CTRL_RD]); 334 } 335 336 static void 337 ucycom_stop_write(struct ucom_softc *ucom) 338 { 339 struct ucycom_softc *sc = ucom->sc_parent; 340 341 usbd_transfer_stop(sc->sc_xfer[UCYCOM_CTRL_RD]); 342 } 343 344 static void 345 ucycom_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error) 346 { 347 struct ucycom_softc *sc = usbd_xfer_softc(xfer); 348 struct usb_device_request req; 349 struct usb_page_cache *pc0, *pc1; 350 uint8_t data[2]; 351 uint8_t offset; 352 uint32_t actlen; 353 354 pc0 = usbd_xfer_get_frame(xfer, 0); 355 pc1 = usbd_xfer_get_frame(xfer, 1); 356 357 switch (USB_GET_STATE(xfer)) { 358 case USB_ST_TRANSFERRED: 359 tr_transferred: 360 case USB_ST_SETUP: 361 362 switch (sc->sc_model) { 363 case MODEL_CY7C63743: 364 offset = 1; 365 break; 366 case MODEL_CY7C64013: 367 offset = 2; 368 break; 369 default: 370 offset = 0; 371 break; 372 } 373 374 if (ucom_get_data(&sc->sc_ucom, pc1, offset, 375 sc->sc_olen - offset, &actlen)) { 376 377 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 378 req.bRequest = UR_SET_REPORT; 379 USETW2(req.wValue, UHID_OUTPUT_REPORT, sc->sc_oid); 380 req.wIndex[0] = sc->sc_iface_no; 381 req.wIndex[1] = 0; 382 USETW(req.wLength, sc->sc_olen); 383 384 switch (sc->sc_model) { 385 case MODEL_CY7C63743: 386 data[0] = actlen; 387 break; 388 case MODEL_CY7C64013: 389 data[0] = 0; 390 data[1] = actlen; 391 break; 392 default: 393 break; 394 } 395 396 usbd_copy_in(pc0, 0, &req, sizeof(req)); 397 usbd_copy_in(pc1, 0, data, offset); 398 399 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 400 usbd_xfer_set_frame_len(xfer, 1, sc->sc_olen); 401 usbd_xfer_set_frames(xfer, sc->sc_olen ? 2 : 1); 402 usbd_transfer_submit(xfer); 403 } 404 return; 405 406 default: /* Error */ 407 if (error == USB_ERR_CANCELLED) { 408 return; 409 } 410 DPRINTF("error=%s\n", 411 usbd_errstr(error)); 412 goto tr_transferred; 413 } 414 } 415 416 static void 417 ucycom_cfg_write(struct ucycom_softc *sc, uint32_t baud, uint8_t cfg) 418 { 419 struct usb_device_request req; 420 uint16_t len; 421 usb_error_t err; 422 423 len = sc->sc_flen; 424 if (len > sizeof(sc->sc_temp_cfg)) { 425 len = sizeof(sc->sc_temp_cfg); 426 } 427 sc->sc_cfg = cfg; 428 429 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 430 req.bRequest = UR_SET_REPORT; 431 USETW2(req.wValue, UHID_FEATURE_REPORT, sc->sc_fid); 432 req.wIndex[0] = sc->sc_iface_no; 433 req.wIndex[1] = 0; 434 USETW(req.wLength, len); 435 436 sc->sc_temp_cfg[0] = (baud & 0xff); 437 sc->sc_temp_cfg[1] = (baud >> 8) & 0xff; 438 sc->sc_temp_cfg[2] = (baud >> 16) & 0xff; 439 sc->sc_temp_cfg[3] = (baud >> 24) & 0xff; 440 sc->sc_temp_cfg[4] = cfg; 441 442 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 443 &req, sc->sc_temp_cfg, 0, 1000); 444 if (err) { 445 DPRINTFN(0, "device request failed, err=%s " 446 "(ignored)\n", usbd_errstr(err)); 447 } 448 } 449 450 static int 451 ucycom_pre_param(struct ucom_softc *ucom, struct termios *t) 452 { 453 switch (t->c_ospeed) { 454 case 600: 455 case 1200: 456 case 2400: 457 case 4800: 458 case 9600: 459 case 19200: 460 case 38400: 461 case 57600: 462 #if 0 463 /* 464 * Stock chips only support standard baud rates in the 600 - 57600 465 * range, but higher rates can be achieved using custom firmware. 466 */ 467 case 115200: 468 case 153600: 469 case 192000: 470 #endif 471 break; 472 default: 473 return (EINVAL); 474 } 475 return (0); 476 } 477 478 static void 479 ucycom_cfg_param(struct ucom_softc *ucom, struct termios *t) 480 { 481 struct ucycom_softc *sc = ucom->sc_parent; 482 uint8_t cfg; 483 484 DPRINTF("\n"); 485 486 if (t->c_cflag & CIGNORE) { 487 cfg = sc->sc_cfg; 488 } else { 489 cfg = 0; 490 switch (t->c_cflag & CSIZE) { 491 default: 492 case CS8: 493 ++cfg; 494 case CS7: 495 ++cfg; 496 case CS6: 497 ++cfg; 498 case CS5: 499 break; 500 } 501 502 if (t->c_cflag & CSTOPB) 503 cfg |= UCYCOM_CFG_STOPB; 504 if (t->c_cflag & PARENB) 505 cfg |= UCYCOM_CFG_PAREN; 506 if (t->c_cflag & PARODD) 507 cfg |= UCYCOM_CFG_PARODD; 508 } 509 510 ucycom_cfg_write(sc, t->c_ospeed, cfg); 511 } 512 513 static void 514 ucycom_intr_read_callback(struct usb_xfer *xfer, usb_error_t error) 515 { 516 struct ucycom_softc *sc = usbd_xfer_softc(xfer); 517 struct usb_page_cache *pc; 518 uint8_t buf[2]; 519 uint32_t offset; 520 uint32_t len; 521 int actlen; 522 523 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 524 pc = usbd_xfer_get_frame(xfer, 0); 525 526 switch (USB_GET_STATE(xfer)) { 527 case USB_ST_TRANSFERRED: 528 switch (sc->sc_model) { 529 case MODEL_CY7C63743: 530 if (actlen < 1) { 531 goto tr_setup; 532 } 533 usbd_copy_out(pc, 0, buf, 1); 534 535 sc->sc_ist = buf[0] & ~0x07; 536 len = buf[0] & 0x07; 537 538 actlen--; 539 offset = 1; 540 541 break; 542 543 case MODEL_CY7C64013: 544 if (actlen < 2) { 545 goto tr_setup; 546 } 547 usbd_copy_out(pc, 0, buf, 2); 548 549 sc->sc_ist = buf[0] & ~0x07; 550 len = buf[1]; 551 552 actlen -= 2; 553 offset = 2; 554 555 break; 556 557 default: 558 DPRINTFN(0, "unsupported model number\n"); 559 goto tr_setup; 560 } 561 562 if (len > actlen) 563 len = actlen; 564 if (len) 565 ucom_put_data(&sc->sc_ucom, pc, offset, len); 566 /* FALLTHROUGH */ 567 case USB_ST_SETUP: 568 tr_setup: 569 usbd_xfer_set_frame_len(xfer, 0, sc->sc_ilen); 570 usbd_transfer_submit(xfer); 571 return; 572 573 default: /* Error */ 574 if (error != USB_ERR_CANCELLED) { 575 /* try to clear stall first */ 576 usbd_xfer_set_stall(xfer); 577 goto tr_setup; 578 } 579 return; 580 581 } 582 } 583 584 static void 585 ucycom_poll(struct ucom_softc *ucom) 586 { 587 struct ucycom_softc *sc = ucom->sc_parent; 588 usbd_transfer_poll(sc->sc_xfer, UCYCOM_N_TRANSFER); 589 } 590