1 /* $OpenBSD: ucycom.c,v 1.14 2008/06/26 05:42:18 ray Exp $ */ 2 /* $NetBSD: ucycom.c,v 1.3 2005/08/05 07:27:47 skrll Exp $ */ 3 4 /* 5 * Copyright (c) 2005 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Nick Hudson 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 /* 33 * This code is based on the ucom driver. 34 */ 35 36 /* 37 * Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to 38 * RS232 bridges. 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/conf.h> 44 #include <sys/kernel.h> 45 #include <sys/malloc.h> 46 #include <sys/device.h> 47 #include <sys/sysctl.h> 48 #include <sys/tty.h> 49 #include <sys/file.h> 50 #include <sys/vnode.h> 51 52 #include <dev/usb/usb.h> 53 #include <dev/usb/usbhid.h> 54 55 #include <dev/usb/usbdi.h> 56 #include <dev/usb/usbdi_util.h> 57 #include <dev/usb/usbdevs.h> 58 #include <dev/usb/uhidev.h> 59 #include <dev/usb/hid.h> 60 61 #include <dev/usb/ucomvar.h> 62 63 #ifdef UCYCOM_DEBUG 64 #define DPRINTF(x) if (ucycomdebug) printf x 65 #define DPRINTFN(n, x) if (ucycomdebug > (n)) printf x 66 int ucycomdebug = 200; 67 #else 68 #define DPRINTF(x) 69 #define DPRINTFN(n,x) 70 #endif 71 72 /* Configuration Byte */ 73 #define UCYCOM_RESET 0x80 74 #define UCYCOM_PARITY_TYPE_MASK 0x20 75 #define UCYCOM_PARITY_ODD 0x20 76 #define UCYCOM_PARITY_EVEN 0x00 77 #define UCYCOM_PARITY_MASK 0x10 78 #define UCYCOM_PARITY_ON 0x10 79 #define UCYCOM_PARITY_OFF 0x00 80 #define UCYCOM_STOP_MASK 0x08 81 #define UCYCOM_STOP_BITS_2 0x08 82 #define UCYCOM_STOP_BITS_1 0x00 83 #define UCYCOM_DATA_MASK 0x03 84 #define UCYCOM_DATA_BITS_8 0x03 85 #define UCYCOM_DATA_BITS_7 0x02 86 #define UCYCOM_DATA_BITS_6 0x01 87 #define UCYCOM_DATA_BITS_5 0x00 88 89 /* Modem (Input) status byte */ 90 #define UCYCOM_RI 0x80 91 #define UCYCOM_DCD 0x40 92 #define UCYCOM_DSR 0x20 93 #define UCYCOM_CTS 0x10 94 #define UCYCOM_ERROR 0x08 95 #define UCYCOM_LMASK 0x07 96 97 /* Modem (Output) control byte */ 98 #define UCYCOM_DTR 0x20 99 #define UCYCOM_RTS 0x10 100 #define UCYCOM_ORESET 0x08 101 102 struct ucycom_softc { 103 struct uhidev sc_hdev; 104 usbd_device_handle sc_udev; 105 106 /* uhidev parameters */ 107 size_t sc_flen; /* feature report length */ 108 size_t sc_ilen; /* input report length */ 109 size_t sc_olen; /* output report length */ 110 111 uint8_t *sc_obuf; 112 113 uint8_t *sc_ibuf; 114 uint32_t sc_icnt; 115 116 /* settings */ 117 uint32_t sc_baud; 118 uint8_t sc_cfg; /* Data format */ 119 uint8_t sc_mcr; /* Modem control */ 120 uint8_t sc_msr; /* Modem status */ 121 uint8_t sc_newmsr; /* from HID intr */ 122 int sc_swflags; 123 124 struct device *sc_subdev; 125 126 /* flags */ 127 u_char sc_dying; 128 }; 129 130 /* Callback routines */ 131 void ucycom_set(void *, int, int, int); 132 int ucycom_param(void *, int, struct termios *); 133 void ucycom_get_status(void *, int, u_char *, u_char *); 134 int ucycom_open(void *, int); 135 void ucycom_close(void *, int); 136 void ucycom_write(void *, int, u_char *, u_char *, u_int32_t *); 137 void ucycom_read(void *, int, u_char **, u_int32_t *); 138 139 struct ucom_methods ucycom_methods = { 140 NULL, /* ucycom_get_status, */ 141 ucycom_set, 142 ucycom_param, 143 NULL, 144 ucycom_open, 145 ucycom_close, 146 ucycom_read, 147 ucycom_write, 148 }; 149 150 void ucycom_intr(struct uhidev *, void *, u_int); 151 152 void ucycom_get_cfg(struct ucycom_softc *); 153 154 const struct usb_devno ucycom_devs[] = { 155 { USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_USBRS232 }, 156 { USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EMUSB }, 157 { USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EMLT20 }, 158 }; 159 #define ucycom_lookup(v, p) usb_lookup(ucycom_devs, v, p) 160 161 int ucycom_match(struct device *, void *, void *); 162 void ucycom_attach(struct device *, struct device *, void *); 163 int ucycom_detach(struct device *, int); 164 int ucycom_activate(struct device *, enum devact); 165 166 struct cfdriver ucycom_cd = { 167 NULL, "ucycom", DV_DULL 168 }; 169 170 const struct cfattach ucycom_ca = { 171 sizeof(struct ucycom_softc), 172 ucycom_match, 173 ucycom_attach, 174 ucycom_detach, 175 ucycom_activate, 176 }; 177 178 int 179 ucycom_match(struct device *parent, void *match, void *aux) 180 { 181 struct uhidev_attach_arg *uha = aux; 182 183 DPRINTF(("ucycom match\n")); 184 return (ucycom_lookup(uha->uaa->vendor, uha->uaa->product) != NULL ? 185 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 186 } 187 188 void 189 ucycom_attach(struct device *parent, struct device *self, void *aux) 190 { 191 struct ucycom_softc *sc = (struct ucycom_softc *)self; 192 struct usb_attach_arg *uaa = aux; 193 struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa; 194 usbd_device_handle dev = uha->parent->sc_udev; 195 struct ucom_attach_args uca; 196 int size, repid, err; 197 void *desc; 198 199 sc->sc_hdev.sc_intr = ucycom_intr; 200 sc->sc_hdev.sc_parent = uha->parent; 201 sc->sc_hdev.sc_report_id = uha->reportid; 202 203 uhidev_get_report_desc(uha->parent, &desc, &size); 204 repid = uha->reportid; 205 sc->sc_ilen = hid_report_size(desc, size, hid_input, repid); 206 sc->sc_olen = hid_report_size(desc, size, hid_output, repid); 207 sc->sc_flen = hid_report_size(desc, size, hid_feature, repid); 208 209 DPRINTF(("ucycom_open: olen %d ilen %d flen %d\n", sc->sc_ilen, 210 sc->sc_olen, sc->sc_flen)); 211 212 printf("\n"); 213 214 sc->sc_udev = dev; 215 216 sc->sc_msr = sc->sc_mcr = 0; 217 218 err = uhidev_open(&sc->sc_hdev); 219 if (err) { 220 DPRINTF(("ucycom_open: uhidev_open %d\n", err)); 221 return; 222 } 223 224 DPRINTF(("ucycom attach: sc %p opipe %p ipipe %p report_id %d\n", 225 sc, sc->sc_hdev.sc_parent->sc_opipe, sc->sc_hdev.sc_parent->sc_ipipe, 226 uha->reportid)); 227 228 /* bulkin, bulkout set above */ 229 bzero(&uca, sizeof uca); 230 uca.bulkin = uca.bulkout = -1; 231 uca.uhidev = sc->sc_hdev.sc_parent; 232 uca.ibufsize = sc->sc_ilen - 1; 233 uca.obufsize = sc->sc_olen - 1; 234 uca.ibufsizepad = 1; 235 uca.opkthdrlen = 0; 236 uca.device = uaa->device; 237 uca.iface = uaa->iface; 238 uca.methods = &ucycom_methods; 239 uca.arg = sc; 240 uca.info = NULL; 241 242 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 243 &sc->sc_hdev.sc_dev); 244 245 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch); 246 DPRINTF(("ucycom_attach: complete %p\n", sc->sc_subdev)); 247 } 248 249 void 250 ucycom_get_status(void *addr, int portno, u_char *lsr, u_char *msr) 251 { 252 struct ucycom_softc *sc = addr; 253 254 DPRINTF(("ucycom_get_status:\n")); 255 256 #if 0 257 if (lsr != NULL) 258 *lsr = sc->sc_lsr; 259 #endif 260 if (msr != NULL) 261 *msr = sc->sc_msr; 262 } 263 264 int 265 ucycom_open(void *addr, int portno) 266 { 267 struct ucycom_softc *sc = addr; 268 struct termios t; 269 int err; 270 271 DPRINTF(("ucycom_open: complete\n")); 272 273 if (sc->sc_dying) 274 return (EIO); 275 276 /* Allocate an output report buffer */ 277 sc->sc_obuf = malloc(sc->sc_olen, M_USBDEV, M_WAITOK | M_ZERO); 278 279 /* Allocate an input report buffer */ 280 sc->sc_ibuf = malloc(sc->sc_ilen, M_USBDEV, M_WAITOK); 281 282 DPRINTF(("ucycom_open: sc->sc_ibuf=%p sc->sc_obuf=%p \n", 283 sc->sc_ibuf, sc->sc_obuf)); 284 285 t.c_ospeed = 9600; 286 t.c_cflag = CSTOPB | CS8; 287 (void)ucycom_param(sc, portno, &t); 288 289 sc->sc_mcr = UCYCOM_DTR | UCYCOM_RTS; 290 sc->sc_obuf[0] = sc->sc_mcr; 291 err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen); 292 if (err) { 293 DPRINTF(("ucycom_open: set RTS err=%d\n", err)); 294 return (EIO); 295 } 296 297 return (0); 298 } 299 300 void 301 ucycom_close(void *addr, int portno) 302 { 303 struct ucycom_softc *sc = addr; 304 int s; 305 306 if (sc->sc_dying) 307 return; 308 309 s = splusb(); 310 if (sc->sc_obuf != NULL) { 311 free(sc->sc_obuf, M_USBDEV); 312 sc->sc_obuf = NULL; 313 } 314 if (sc->sc_ibuf != NULL) { 315 free(sc->sc_ibuf, M_USBDEV); 316 sc->sc_ibuf = NULL; 317 } 318 splx(s); 319 } 320 321 void 322 ucycom_read(void *addr, int portno, u_char **ptr, u_int32_t *count) 323 { 324 struct ucycom_softc *sc = addr; 325 326 if (sc->sc_newmsr ^ sc->sc_msr) { 327 DPRINTF(("ucycom_read: msr %d new %d\n", 328 sc->sc_msr, sc->sc_newmsr)); 329 sc->sc_msr = sc->sc_newmsr; 330 ucom_status_change((struct ucom_softc *)sc->sc_subdev); 331 } 332 333 DPRINTF(("ucycom_read: buf %p chars %d\n", sc->sc_ibuf, sc->sc_icnt)); 334 *ptr = sc->sc_ibuf; 335 *count = sc->sc_icnt; 336 } 337 338 void 339 ucycom_write(void *addr, int portno, u_char *to, u_char *data, u_int32_t *cnt) 340 { 341 struct ucycom_softc *sc = addr; 342 u_int32_t len; 343 #ifdef UCYCOM_DEBUG 344 u_int32_t want = *cnt; 345 #endif 346 347 /* 348 * The 8 byte output report uses byte 0 for control and byte 349 * count. 350 * 351 * The 32 byte output report uses byte 0 for control. Byte 1 352 * is used for byte count. 353 */ 354 len = sc->sc_olen; 355 memset(to, 0, len); 356 switch (sc->sc_olen) { 357 case 8: 358 to[0] = *cnt | sc->sc_mcr; 359 memcpy(&to[1], data, *cnt); 360 DPRINTF(("ucycomstart(8): to[0] = %d | %d = %d\n", 361 *cnt, sc->sc_mcr, to[0])); 362 break; 363 364 case 32: 365 to[0] = sc->sc_mcr; 366 to[1] = *cnt; 367 memcpy(&to[2], data, *cnt); 368 DPRINTF(("ucycomstart(32): to[0] = %d\nto[1] = %d\n", 369 to[0], to[1])); 370 break; 371 } 372 373 #ifdef UCYCOM_DEBUG 374 if (ucycomdebug > 5) { 375 int i; 376 377 if (len != 0) { 378 DPRINTF(("ucycomstart: to[0..%d) =", len-1)); 379 for (i = 0; i < len; i++) 380 DPRINTF((" %02x", to[i])); 381 DPRINTF(("\n")); 382 } 383 } 384 #endif 385 *cnt = len; 386 387 #if 0 388 ucycom_get_cfg(sc); 389 #endif 390 DPRINTFN(4,("ucycomstart: req %d chars did %d chars\n", want, len)); 391 } 392 393 int 394 ucycom_param(void *addr, int portno, struct termios *t) 395 { 396 struct ucycom_softc *sc = addr; 397 uint8_t report[5]; 398 uint32_t baud = 0; 399 uint8_t cfg; 400 int err; 401 402 if (sc->sc_dying) 403 return (EIO); 404 405 switch (t->c_ospeed) { 406 case 600: 407 case 1200: 408 case 2400: 409 case 4800: 410 case 9600: 411 case 19200: 412 case 38400: 413 case 57600: 414 #if 0 415 /* 416 * Stock chips only support standard baud rates in the 600 - 57600 417 * range, but higher rates can be achieved using custom firmware. 418 */ 419 case 115200: 420 case 153600: 421 case 192000: 422 #endif 423 baud = t->c_ospeed; 424 break; 425 default: 426 return (EINVAL); 427 } 428 429 if (t->c_cflag & CIGNORE) { 430 cfg = sc->sc_cfg; 431 } else { 432 cfg = 0; 433 switch (t->c_cflag & CSIZE) { 434 case CS8: 435 cfg |= UCYCOM_DATA_BITS_8; 436 break; 437 case CS7: 438 cfg |= UCYCOM_DATA_BITS_7; 439 break; 440 case CS6: 441 cfg |= UCYCOM_DATA_BITS_6; 442 break; 443 case CS5: 444 cfg |= UCYCOM_DATA_BITS_5; 445 break; 446 default: 447 return (EINVAL); 448 } 449 cfg |= ISSET(t->c_cflag, CSTOPB) ? 450 UCYCOM_STOP_BITS_2 : UCYCOM_STOP_BITS_1; 451 cfg |= ISSET(t->c_cflag, PARENB) ? 452 UCYCOM_PARITY_ON : UCYCOM_PARITY_OFF; 453 cfg |= ISSET(t->c_cflag, PARODD) ? 454 UCYCOM_PARITY_ODD : UCYCOM_PARITY_EVEN; 455 } 456 457 DPRINTF(("ucycom_param: setting %d baud, %d-%c-%d (%d)\n", baud, 458 5 + (cfg & UCYCOM_DATA_MASK), 459 (cfg & UCYCOM_PARITY_MASK) ? 460 ((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N', 461 (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg)); 462 463 report[0] = baud & 0xff; 464 report[1] = (baud >> 8) & 0xff; 465 report[2] = (baud >> 16) & 0xff; 466 report[3] = (baud >> 24) & 0xff; 467 report[4] = cfg; 468 err = uhidev_set_report(&sc->sc_hdev, UHID_FEATURE_REPORT, 469 report, sc->sc_flen); 470 if (err != 0) { 471 DPRINTF(("ucycom_param: uhidev_set_report %d %s\n", 472 err, usbd_errstr(err))); 473 return EIO; 474 } 475 sc->sc_baud = baud; 476 return (err); 477 } 478 479 void 480 ucycom_intr(struct uhidev *addr, void *ibuf, u_int len) 481 { 482 extern void ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status); 483 struct ucycom_softc *sc = (struct ucycom_softc *)addr; 484 uint8_t *cp = ibuf; 485 int n, st, s; 486 487 /* not accepting data anymore.. */ 488 if (sc->sc_ibuf == NULL) 489 return; 490 491 /* We understand 8 byte and 32 byte input records */ 492 switch (len) { 493 case 8: 494 n = cp[0] & UCYCOM_LMASK; 495 st = cp[0] & ~UCYCOM_LMASK; 496 cp++; 497 break; 498 499 case 32: 500 st = cp[0]; 501 n = cp[1]; 502 cp += 2; 503 break; 504 505 default: 506 DPRINTFN(3,("ucycom_intr: Unknown input report length\n")); 507 return; 508 } 509 510 #ifdef UCYCOM_DEBUG 511 if (ucycomdebug > 5) { 512 u_int32_t i; 513 514 if (n != 0) { 515 DPRINTF(("ucycom_intr: ibuf[0..%d) =", n)); 516 for (i = 0; i < n; i++) 517 DPRINTF((" %02x", cp[i])); 518 DPRINTF(("\n")); 519 } 520 } 521 #endif 522 523 if (n > 0 || st != sc->sc_msr) { 524 s = spltty(); 525 sc->sc_newmsr = st; 526 bcopy(cp, sc->sc_ibuf, n); 527 sc->sc_icnt = n; 528 ucomreadcb(addr->sc_parent->sc_ixfer, sc->sc_subdev, 529 USBD_NORMAL_COMPLETION); 530 splx(s); 531 } 532 } 533 534 void 535 ucycom_set(void *addr, int portno, int reg, int onoff) 536 { 537 struct ucycom_softc *sc = addr; 538 int err; 539 540 switch (reg) { 541 case UCOM_SET_DTR: 542 if (onoff) 543 SET(sc->sc_mcr, UCYCOM_DTR); 544 else 545 CLR(sc->sc_mcr, UCYCOM_DTR); 546 break; 547 case UCOM_SET_RTS: 548 if (onoff) 549 SET(sc->sc_mcr, UCYCOM_RTS); 550 else 551 CLR(sc->sc_mcr, UCYCOM_RTS); 552 break; 553 case UCOM_SET_BREAK: 554 break; 555 } 556 557 memset(sc->sc_obuf, 0, sc->sc_olen); 558 sc->sc_obuf[0] = sc->sc_mcr; 559 560 err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen); 561 if (err) 562 DPRINTF(("ucycom_set_status: err=%d\n", err)); 563 } 564 565 void 566 ucycom_get_cfg(struct ucycom_softc *sc) 567 { 568 int err, cfg, baud; 569 uint8_t report[5]; 570 571 err = uhidev_get_report(&sc->sc_hdev, UHID_FEATURE_REPORT, 572 report, sc->sc_flen); 573 cfg = report[4]; 574 baud = (report[3] << 24) + (report[2] << 16) + (report[1] << 8) + report[0]; 575 DPRINTF(("ucycom_configure: device reports %d baud, %d-%c-%d (%d)\n", baud, 576 5 + (cfg & UCYCOM_DATA_MASK), 577 (cfg & UCYCOM_PARITY_MASK) ? 578 ((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N', 579 (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg)); 580 } 581 582 int 583 ucycom_detach(struct device *self, int flags) 584 { 585 struct ucycom_softc *sc = (struct ucycom_softc *)self; 586 587 DPRINTF(("ucycom_detach: sc=%p flags=%d\n", sc, flags)); 588 sc->sc_dying = 1; 589 if (sc->sc_subdev != NULL) { 590 config_detach(sc->sc_subdev, flags); 591 sc->sc_subdev = NULL; 592 } 593 return (0); 594 } 595 596 int 597 ucycom_activate(struct device *self, enum devact act) 598 { 599 struct ucycom_softc *sc = (struct ucycom_softc *)self; 600 int rv = 0; 601 602 DPRINTFN(5,("ucycom_activate: %d\n", act)); 603 604 switch (act) { 605 case DVACT_ACTIVATE: 606 break; 607 608 case DVACT_DEACTIVATE: 609 if (sc->sc_subdev != NULL) 610 rv = config_deactivate(sc->sc_subdev); 611 sc->sc_dying = 1; 612 break; 613 } 614 return (rv); 615 } 616