1 /* $OpenBSD: ucom.c,v 1.9 2001/10/31 04:24:44 nate Exp $ */ 2 /* $NetBSD: ucom.c,v 1.39 2001/08/16 22:31:24 augustss Exp $ */ 3 4 /* 5 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Lennart Augustsson (lennart@augustsson.net) at 10 * Carlstedt Research & Technology. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the NetBSD 23 * Foundation, Inc. and its contributors. 24 * 4. Neither the name of The NetBSD Foundation nor the names of its 25 * contributors may be used to endorse or promote products derived 26 * from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGE. 39 */ 40 /* 41 * This code is very heavily based on the 16550 driver, com.c. 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/ioctl.h> 48 #include <sys/conf.h> 49 #include <sys/tty.h> 50 #include <sys/file.h> 51 #include <sys/select.h> 52 #include <sys/proc.h> 53 #include <sys/vnode.h> 54 #include <sys/device.h> 55 #include <sys/poll.h> 56 #if defined(__NetBSD__) 57 #include "rnd.h" 58 #if NRND > 0 59 #include <sys/rnd.h> 60 #endif 61 #endif 62 63 #include <dev/usb/usb.h> 64 65 #include <dev/usb/usbdi.h> 66 #include <dev/usb/usbdi_util.h> 67 #include <dev/usb/usbdevs.h> 68 #include <dev/usb/usb_quirks.h> 69 70 #include <dev/usb/ucomvar.h> 71 72 #include "ucom.h" 73 74 #if NUCOM > 0 75 76 #ifdef UCOM_DEBUG 77 #define DPRINTFN(n, x) if (ucomdebug > (n)) logprintf x 78 int ucomdebug = 0; 79 #else 80 #define DPRINTFN(n, x) 81 #endif 82 #define DPRINTF(x) DPRINTFN(0, x) 83 84 #if defined(__NetBSD__) 85 #define UCOMUNIT_MASK 0x3ffff 86 #define UCOMDIALOUT_MASK 0x80000 87 #define UCOMCALLUNIT_MASK 0x40000 88 #endif 89 90 #if defined(__OpenBSD__) 91 #define UCOMUNIT_MASK 0x3f 92 #define UCOMDIALOUT_MASK 0x80 93 #define UCOMCALLUNIT_MASK 0x40 94 #endif 95 96 #define UCOMUNIT(x) (minor(x) & UCOMUNIT_MASK) 97 #define UCOMDIALOUT(x) (minor(x) & UCOMDIALOUT_MASK) 98 #define UCOMCALLUNIT(x) (minor(x) & UCOMCALLUNIT_MASK) 99 100 struct ucom_softc { 101 USBBASEDEVICE sc_dev; /* base device */ 102 103 usbd_device_handle sc_udev; /* USB device */ 104 105 usbd_interface_handle sc_iface; /* data interface */ 106 107 int sc_bulkin_no; /* bulk in endpoint address */ 108 usbd_pipe_handle sc_bulkin_pipe; /* bulk in pipe */ 109 usbd_xfer_handle sc_ixfer; /* read request */ 110 u_char *sc_ibuf; /* read buffer */ 111 u_int sc_ibufsize; /* read buffer size */ 112 u_int sc_ibufsizepad; /* read buffer size padded */ 113 114 int sc_bulkout_no; /* bulk out endpoint address */ 115 usbd_pipe_handle sc_bulkout_pipe;/* bulk out pipe */ 116 usbd_xfer_handle sc_oxfer; /* write request */ 117 u_char *sc_obuf; /* write buffer */ 118 u_int sc_obufsize; /* write buffer size */ 119 u_int sc_opkthdrlen; /* header length of 120 * output packet */ 121 122 struct ucom_methods *sc_methods; 123 void *sc_parent; 124 int sc_portno; 125 126 struct tty *sc_tty; /* our tty */ 127 u_char sc_lsr; 128 u_char sc_msr; 129 u_char sc_mcr; 130 u_char sc_tx_stopped; 131 int sc_swflags; 132 133 u_char sc_opening; /* lock during open */ 134 int sc_refcnt; 135 u_char sc_dying; /* disconnecting */ 136 137 #if defined(__NetBSD__) && NRND > 0 138 rndsource_element_t sc_rndsource; /* random source */ 139 #endif 140 }; 141 142 cdev_decl(ucom); 143 144 Static void ucom_cleanup(struct ucom_softc *); 145 Static void ucom_hwiflow(struct ucom_softc *); 146 Static int ucomparam(struct tty *, struct termios *); 147 Static void ucomstart(struct tty *); 148 Static void ucom_shutdown(struct ucom_softc *); 149 Static int ucom_do_ioctl(struct ucom_softc *, u_long, caddr_t, 150 int, struct proc *); 151 Static void ucom_dtr(struct ucom_softc *, int); 152 Static void ucom_rts(struct ucom_softc *, int); 153 Static void ucom_break(struct ucom_softc *, int); 154 Static usbd_status ucomstartread(struct ucom_softc *); 155 Static void ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status); 156 Static void ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status); 157 Static void tiocm_to_ucom(struct ucom_softc *, u_long, int); 158 Static int ucom_to_tiocm(struct ucom_softc *); 159 160 USB_DECLARE_DRIVER(ucom); 161 162 USB_MATCH(ucom) 163 { 164 return (1); 165 } 166 167 USB_ATTACH(ucom) 168 { 169 struct ucom_softc *sc = (struct ucom_softc *)self; 170 struct ucom_attach_args *uca = aux; 171 struct tty *tp; 172 173 if (uca->portno != UCOM_UNK_PORTNO) 174 printf(": portno %d", uca->portno); 175 if (uca->info != NULL) 176 printf(", %s", uca->info); 177 printf("\n"); 178 179 sc->sc_udev = uca->device; 180 sc->sc_iface = uca->iface; 181 sc->sc_bulkout_no = uca->bulkout; 182 sc->sc_bulkin_no = uca->bulkin; 183 sc->sc_ibufsize = uca->ibufsize; 184 sc->sc_ibufsizepad = uca->ibufsizepad; 185 sc->sc_obufsize = uca->obufsize; 186 sc->sc_opkthdrlen = uca->opkthdrlen; 187 sc->sc_methods = uca->methods; 188 sc->sc_parent = uca->arg; 189 sc->sc_portno = uca->portno; 190 191 tp = ttymalloc(); 192 tp->t_oproc = ucomstart; 193 tp->t_param = ucomparam; 194 sc->sc_tty = tp; 195 196 DPRINTF(("ucom_attach: tty_attach %p\n", tp)); 197 tty_attach(tp); 198 199 #if defined(__NetBSD__) && NRND > 0 200 rnd_attach_source(&sc->sc_rndsource, USBDEVNAME(sc->sc_dev), 201 RND_TYPE_TTY, 0); 202 #endif 203 204 USB_ATTACH_SUCCESS_RETURN; 205 } 206 207 USB_DETACH(ucom) 208 { 209 struct ucom_softc *sc = (struct ucom_softc *)self; 210 struct tty *tp = sc->sc_tty; 211 int maj, mn; 212 int s; 213 214 DPRINTF(("ucom_detach: sc=%p flags=%d tp=%p, pipe=%d,%d\n", 215 sc, flags, tp, sc->sc_bulkin_no, sc->sc_bulkout_no)); 216 217 sc->sc_dying = 1; 218 219 if (sc->sc_bulkin_pipe != NULL) 220 usbd_abort_pipe(sc->sc_bulkin_pipe); 221 if (sc->sc_bulkout_pipe != NULL) 222 usbd_abort_pipe(sc->sc_bulkout_pipe); 223 224 s = splusb(); 225 if (--sc->sc_refcnt >= 0) { 226 /* Wake up anyone waiting */ 227 if (tp != NULL) { 228 CLR(tp->t_state, TS_CARR_ON); 229 CLR(tp->t_cflag, CLOCAL | MDMBUF); 230 ttyflush(tp, FREAD|FWRITE); 231 } 232 /* Wait for processes to go away. */ 233 usb_detach_wait(USBDEV(sc->sc_dev)); 234 } 235 splx(s); 236 237 /* locate the major number */ 238 for (maj = 0; maj < nchrdev; maj++) 239 if (cdevsw[maj].d_open == ucomopen) 240 break; 241 242 /* Nuke the vnodes for any open instances. */ 243 mn = self->dv_unit; 244 DPRINTF(("ucom_detach: maj=%d mn=%d\n", maj, mn)); 245 vdevgone(maj, mn, mn, VCHR); 246 vdevgone(maj, mn | UCOMDIALOUT_MASK, mn | UCOMDIALOUT_MASK, VCHR); 247 vdevgone(maj, mn | UCOMCALLUNIT_MASK, mn | UCOMCALLUNIT_MASK, VCHR); 248 249 /* Detach and free the tty. */ 250 if (tp != NULL) { 251 tty_detach(tp); 252 ttyfree(tp); 253 sc->sc_tty = NULL; 254 } 255 256 /* Detach the random source */ 257 #if defined(__NetBSD__) && NRND > 0 258 rnd_detach_source(&sc->sc_rndsource); 259 #endif 260 261 return (0); 262 } 263 264 int 265 ucom_activate(device_ptr_t self, enum devact act) 266 { 267 struct ucom_softc *sc = (struct ucom_softc *)self; 268 269 DPRINTFN(5,("ucom_activate: %d\n", act)); 270 271 switch (act) { 272 case DVACT_ACTIVATE: 273 return (EOPNOTSUPP); 274 break; 275 276 case DVACT_DEACTIVATE: 277 sc->sc_dying = 1; 278 break; 279 } 280 return (0); 281 } 282 283 void 284 ucom_shutdown(struct ucom_softc *sc) 285 { 286 struct tty *tp = sc->sc_tty; 287 288 DPRINTF(("ucom_shutdown\n")); 289 /* 290 * Hang up if necessary. Wait a bit, so the other side has time to 291 * notice even if we immediately open the port again. 292 */ 293 if (ISSET(tp->t_cflag, HUPCL)) { 294 ucom_dtr(sc, 0); 295 (void)tsleep(sc, TTIPRI, ttclos, hz); 296 } 297 } 298 299 int 300 ucomopen(dev_t dev, int flag, int mode, struct proc *p) 301 { 302 int unit = UCOMUNIT(dev); 303 usbd_status err; 304 struct ucom_softc *sc; 305 struct tty *tp; 306 int s; 307 int error; 308 309 if (unit >= ucom_cd.cd_ndevs) 310 return (ENXIO); 311 sc = ucom_cd.cd_devs[unit]; 312 if (sc == NULL) 313 return (ENXIO); 314 315 if (sc->sc_dying) 316 return (EIO); 317 318 if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0) 319 return (ENXIO); 320 321 tp = sc->sc_tty; 322 323 DPRINTF(("ucomopen: unit=%d, tp=%p\n", unit, tp)); 324 325 if (ISSET(tp->t_state, TS_ISOPEN) && 326 ISSET(tp->t_state, TS_XCLUDE) && 327 p->p_ucred->cr_uid != 0) 328 return (EBUSY); 329 330 s = spltty(); 331 332 /* 333 * Do the following iff this is a first open. 334 */ 335 while (sc->sc_opening) 336 tsleep(&sc->sc_opening, PRIBIO, "ucomop", 0); 337 338 if (sc->sc_dying) { 339 splx(s); 340 return (EIO); 341 } 342 sc->sc_opening = 1; 343 344 #if defined(__NetBSD__) 345 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 346 #else 347 if (!ISSET(tp->t_state, TS_ISOPEN)) { 348 #endif 349 struct termios t; 350 351 tp->t_dev = dev; 352 353 if (sc->sc_methods->ucom_open != NULL) { 354 error = sc->sc_methods->ucom_open(sc->sc_parent, 355 sc->sc_portno); 356 if (error) { 357 ucom_cleanup(sc); 358 sc->sc_opening = 0; 359 wakeup(&sc->sc_opening); 360 splx(s); 361 return (error); 362 } 363 } 364 365 ucom_status_change(sc); 366 367 /* 368 * Initialize the termios status to the defaults. Add in the 369 * sticky bits from TIOCSFLAGS. 370 */ 371 t.c_ispeed = 0; 372 t.c_ospeed = TTYDEF_SPEED; 373 t.c_cflag = TTYDEF_CFLAG; 374 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL)) 375 SET(t.c_cflag, CLOCAL); 376 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS)) 377 SET(t.c_cflag, CRTSCTS); 378 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF)) 379 SET(t.c_cflag, MDMBUF); 380 /* Make sure ucomparam() will do something. */ 381 tp->t_ospeed = 0; 382 (void) ucomparam(tp, &t); 383 tp->t_iflag = TTYDEF_IFLAG; 384 tp->t_oflag = TTYDEF_OFLAG; 385 tp->t_lflag = TTYDEF_LFLAG; 386 ttychars(tp); 387 ttsetwater(tp); 388 389 /* 390 * Turn on DTR. We must always do this, even if carrier is not 391 * present, because otherwise we'd have to use TIOCSDTR 392 * immediately after setting CLOCAL, which applications do not 393 * expect. We always assert DTR while the device is open 394 * unless explicitly requested to deassert it. 395 */ 396 ucom_dtr(sc, 1); 397 398 /* XXX CLR(sc->sc_rx_flags, RX_ANY_BLOCK);*/ 399 ucom_hwiflow(sc); 400 401 DPRINTF(("ucomopen: open pipes in=%d out=%d\n", 402 sc->sc_bulkin_no, sc->sc_bulkout_no)); 403 404 /* Open the bulk pipes */ 405 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0, 406 &sc->sc_bulkin_pipe); 407 if (err) { 408 DPRINTF(("%s: open bulk out error (addr %d), err=%s\n", 409 USBDEVNAME(sc->sc_dev), sc->sc_bulkin_no, 410 usbd_errstr(err))); 411 error = EIO; 412 goto fail_0; 413 } 414 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no, 415 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe); 416 if (err) { 417 DPRINTF(("%s: open bulk in error (addr %d), err=%s\n", 418 USBDEVNAME(sc->sc_dev), sc->sc_bulkout_no, 419 usbd_errstr(err))); 420 error = EIO; 421 goto fail_1; 422 } 423 424 /* Allocate a request and an input buffer and start reading. */ 425 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev); 426 if (sc->sc_ixfer == NULL) { 427 error = ENOMEM; 428 goto fail_2; 429 } 430 431 sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer, 432 sc->sc_ibufsizepad); 433 if (sc->sc_ibuf == NULL) { 434 error = ENOMEM; 435 goto fail_3; 436 } 437 438 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev); 439 if (sc->sc_oxfer == NULL) { 440 error = ENOMEM; 441 goto fail_3; 442 } 443 444 sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer, 445 sc->sc_obufsize + 446 sc->sc_opkthdrlen); 447 if (sc->sc_obuf == NULL) { 448 error = ENOMEM; 449 goto fail_4; 450 } 451 452 ucomstartread(sc); 453 } 454 sc->sc_opening = 0; 455 wakeup(&sc->sc_opening); 456 splx(s); 457 458 #if defined(__NetBSD__) 459 error = ttyopen(tp, UCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK)); 460 #else 461 error = ttyopen(UCOMDIALOUT(dev), tp); 462 #endif 463 if (error) 464 goto bad; 465 466 error = (*linesw[tp->t_line].l_open)(dev, tp); 467 if (error) 468 goto bad; 469 470 return (0); 471 472 fail_4: 473 usbd_free_xfer(sc->sc_oxfer); 474 sc->sc_oxfer = NULL; 475 fail_3: 476 usbd_free_xfer(sc->sc_ixfer); 477 sc->sc_ixfer = NULL; 478 fail_2: 479 usbd_close_pipe(sc->sc_bulkout_pipe); 480 sc->sc_bulkout_pipe = NULL; 481 fail_1: 482 usbd_close_pipe(sc->sc_bulkin_pipe); 483 sc->sc_bulkin_pipe = NULL; 484 fail_0: 485 sc->sc_opening = 0; 486 wakeup(&sc->sc_opening); 487 splx(s); 488 return (error); 489 490 bad: 491 #if defined(__NetBSD__) 492 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 493 #else 494 if (!ISSET(tp->t_state, TS_ISOPEN)) { 495 #endif 496 /* 497 * We failed to open the device, and nobody else had it opened. 498 * Clean up the state as appropriate. 499 */ 500 ucom_cleanup(sc); 501 } 502 503 return (error); 504 } 505 506 int 507 ucomclose(dev_t dev, int flag, int mode, struct proc *p) 508 { 509 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)]; 510 struct tty *tp = sc->sc_tty; 511 512 DPRINTF(("ucomclose: unit=%d\n", UCOMUNIT(dev))); 513 if (!ISSET(tp->t_state, TS_ISOPEN)) 514 return (0); 515 516 sc->sc_refcnt++; 517 518 (*linesw[tp->t_line].l_close)(tp, flag); 519 ttyclose(tp); 520 521 #if defined(__NetBSD__) 522 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 523 #else 524 if (!ISSET(tp->t_state, TS_ISOPEN)) { 525 #endif 526 /* 527 * Although we got a last close, the device may still be in 528 * use; e.g. if this was the dialout node, and there are still 529 * processes waiting for carrier on the non-dialout node. 530 */ 531 ucom_cleanup(sc); 532 } 533 534 if (sc->sc_methods->ucom_close != NULL) 535 sc->sc_methods->ucom_close(sc->sc_parent, sc->sc_portno); 536 537 if (--sc->sc_refcnt < 0) 538 usb_detach_wakeup(USBDEV(sc->sc_dev)); 539 540 return (0); 541 } 542 543 int 544 ucomread(dev_t dev, struct uio *uio, int flag) 545 { 546 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)]; 547 struct tty *tp = sc->sc_tty; 548 int error; 549 550 if (sc->sc_dying) 551 return (EIO); 552 553 sc->sc_refcnt++; 554 error = ((*linesw[tp->t_line].l_read)(tp, uio, flag)); 555 if (--sc->sc_refcnt < 0) 556 usb_detach_wakeup(USBDEV(sc->sc_dev)); 557 return (error); 558 } 559 560 int 561 ucomwrite(dev_t dev, struct uio *uio, int flag) 562 { 563 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)]; 564 struct tty *tp = sc->sc_tty; 565 int error; 566 567 if (sc->sc_dying) 568 return (EIO); 569 570 sc->sc_refcnt++; 571 error = ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 572 if (--sc->sc_refcnt < 0) 573 usb_detach_wakeup(USBDEV(sc->sc_dev)); 574 return (error); 575 } 576 577 #if 0 578 int 579 ucompoll(dev, events, p) 580 dev_t dev; 581 int events; 582 struct proc *p; 583 { 584 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)]; 585 struct tty *tp = sc->sc_tty; 586 int error; 587 588 if (sc->sc_dying) 589 return (EIO); 590 591 sc->sc_refcnt++; 592 error = ((*linesw[tp->t_line].l_poll)(tp, events, p)); 593 if (--sc->sc_refcnt < 0) 594 usb_detach_wakeup(USBDEV(sc->sc_dev)); 595 return (error); 596 } 597 #endif 598 599 struct tty * 600 ucomtty(dev_t dev) 601 { 602 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)]; 603 struct tty *tp = sc->sc_tty; 604 605 return (tp); 606 } 607 608 int 609 ucomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 610 { 611 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(dev)]; 612 int error; 613 614 sc->sc_refcnt++; 615 error = ucom_do_ioctl(sc, cmd, data, flag, p); 616 if (--sc->sc_refcnt < 0) 617 usb_detach_wakeup(USBDEV(sc->sc_dev)); 618 return (error); 619 } 620 621 Static int 622 ucom_do_ioctl(struct ucom_softc *sc, u_long cmd, caddr_t data, 623 int flag, struct proc *p) 624 { 625 struct tty *tp = sc->sc_tty; 626 int error; 627 int s; 628 629 if (sc->sc_dying) 630 return (EIO); 631 632 DPRINTF(("ucomioctl: cmd=0x%08lx\n", cmd)); 633 634 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 635 if (error >= 0) 636 return (error); 637 638 error = ttioctl(tp, cmd, data, flag, p); 639 if (error >= 0) 640 return (error); 641 642 if (sc->sc_methods->ucom_ioctl != NULL) { 643 error = sc->sc_methods->ucom_ioctl(sc->sc_parent, 644 sc->sc_portno, cmd, data, flag, p); 645 if (error >= 0) 646 return (error); 647 } 648 649 error = 0; 650 651 DPRINTF(("ucomioctl: our cmd=0x%08lx\n", cmd)); 652 s = spltty(); 653 654 switch (cmd) { 655 case TIOCSBRK: 656 ucom_break(sc, 1); 657 break; 658 659 case TIOCCBRK: 660 ucom_break(sc, 0); 661 break; 662 663 case TIOCSDTR: 664 ucom_dtr(sc, 1); 665 break; 666 667 case TIOCCDTR: 668 ucom_dtr(sc, 0); 669 break; 670 671 case TIOCGFLAGS: 672 *(int *)data = sc->sc_swflags; 673 break; 674 675 case TIOCSFLAGS: 676 error = suser(p->p_ucred, &p->p_acflag); 677 if (error) 678 break; 679 sc->sc_swflags = *(int *)data; 680 break; 681 682 case TIOCMSET: 683 case TIOCMBIS: 684 case TIOCMBIC: 685 tiocm_to_ucom(sc, cmd, *(int *)data); 686 break; 687 688 case TIOCMGET: 689 *(int *)data = ucom_to_tiocm(sc); 690 break; 691 692 default: 693 error = ENOTTY; 694 break; 695 } 696 697 splx(s); 698 699 return (error); 700 } 701 702 Static void 703 tiocm_to_ucom(struct ucom_softc *sc, u_long how, int ttybits) 704 { 705 u_char combits; 706 707 combits = 0; 708 if (ISSET(ttybits, TIOCM_DTR)) 709 SET(combits, UMCR_DTR); 710 if (ISSET(ttybits, TIOCM_RTS)) 711 SET(combits, UMCR_RTS); 712 713 switch (how) { 714 case TIOCMBIC: 715 CLR(sc->sc_mcr, combits); 716 break; 717 718 case TIOCMBIS: 719 SET(sc->sc_mcr, combits); 720 break; 721 722 case TIOCMSET: 723 CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS); 724 SET(sc->sc_mcr, combits); 725 break; 726 } 727 728 if (how == TIOCMSET || ISSET(combits, UMCR_DTR)) 729 ucom_dtr(sc, (sc->sc_mcr & UMCR_DTR) != 0); 730 if (how == TIOCMSET || ISSET(combits, UMCR_RTS)) 731 ucom_rts(sc, (sc->sc_mcr & UMCR_RTS) != 0); 732 } 733 734 Static int 735 ucom_to_tiocm(struct ucom_softc *sc) 736 { 737 u_char combits; 738 int ttybits = 0; 739 740 combits = sc->sc_mcr; 741 if (ISSET(combits, UMCR_DTR)) 742 SET(ttybits, TIOCM_DTR); 743 if (ISSET(combits, UMCR_RTS)) 744 SET(ttybits, TIOCM_RTS); 745 746 combits = sc->sc_msr; 747 if (ISSET(combits, UMSR_DCD)) 748 SET(ttybits, TIOCM_CD); 749 if (ISSET(combits, UMSR_CTS)) 750 SET(ttybits, TIOCM_CTS); 751 if (ISSET(combits, UMSR_DSR)) 752 SET(ttybits, TIOCM_DSR); 753 if (ISSET(combits, UMSR_RI | UMSR_TERI)) 754 SET(ttybits, TIOCM_RI); 755 756 #if 0 757 XXX; 758 if (sc->sc_ier != 0) 759 SET(ttybits, TIOCM_LE); 760 #endif 761 762 return (ttybits); 763 } 764 765 Static void 766 ucom_break(sc, onoff) 767 struct ucom_softc *sc; 768 int onoff; 769 { 770 DPRINTF(("ucom_break: onoff=%d\n", onoff)); 771 772 if (sc->sc_methods->ucom_set != NULL) 773 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno, 774 UCOM_SET_BREAK, onoff); 775 } 776 777 Static void 778 ucom_dtr(struct ucom_softc *sc, int onoff) 779 { 780 DPRINTF(("ucom_dtr: onoff=%d\n", onoff)); 781 782 if (sc->sc_methods->ucom_set != NULL) 783 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno, 784 UCOM_SET_DTR, onoff); 785 } 786 787 Static void 788 ucom_rts(struct ucom_softc *sc, int onoff) 789 { 790 DPRINTF(("ucom_rts: onoff=%d\n", onoff)); 791 792 if (sc->sc_methods->ucom_set != NULL) 793 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno, 794 UCOM_SET_RTS, onoff); 795 } 796 797 void 798 ucom_status_change(struct ucom_softc *sc) 799 { 800 struct tty *tp = sc->sc_tty; 801 u_char old_msr; 802 803 if (sc->sc_methods->ucom_get_status != NULL) { 804 old_msr = sc->sc_msr; 805 sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno, 806 &sc->sc_lsr, &sc->sc_msr); 807 if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD)) 808 (*linesw[tp->t_line].l_modem)(tp, 809 ISSET(sc->sc_msr, UMSR_DCD)); 810 } else { 811 sc->sc_lsr = 0; 812 sc->sc_msr = 0; 813 } 814 } 815 816 Static int 817 ucomparam(struct tty *tp, struct termios *t) 818 { 819 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)]; 820 int error; 821 822 if (sc->sc_dying) 823 return (EIO); 824 825 /* Check requested parameters. */ 826 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) 827 return (EINVAL); 828 829 /* 830 * For the console, always force CLOCAL and !HUPCL, so that the port 831 * is always active. 832 */ 833 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) { 834 SET(t->c_cflag, CLOCAL); 835 CLR(t->c_cflag, HUPCL); 836 } 837 838 /* 839 * If there were no changes, don't do anything. This avoids dropping 840 * input and improves performance when all we did was frob things like 841 * VMIN and VTIME. 842 */ 843 if (tp->t_ospeed == t->c_ospeed && 844 tp->t_cflag == t->c_cflag) 845 return (0); 846 847 /* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */ 848 849 /* And copy to tty. */ 850 tp->t_ispeed = 0; 851 tp->t_ospeed = t->c_ospeed; 852 tp->t_cflag = t->c_cflag; 853 854 if (sc->sc_methods->ucom_param != NULL) { 855 error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno, 856 t); 857 if (error) 858 return (error); 859 } 860 861 /* XXX worry about CHWFLOW */ 862 863 /* 864 * Update the tty layer's idea of the carrier bit, in case we changed 865 * CLOCAL or MDMBUF. We don't hang up here; we only do that by 866 * explicit request. 867 */ 868 DPRINTF(("ucomparam: l_modem\n")); 869 (void) (*linesw[tp->t_line].l_modem)(tp, 1 /* XXX carrier */ ); 870 871 #if 0 872 XXX what if the hardware is not open 873 if (!ISSET(t->c_cflag, CHWFLOW)) { 874 if (sc->sc_tx_stopped) { 875 sc->sc_tx_stopped = 0; 876 ucomstart(tp); 877 } 878 } 879 #endif 880 881 return (0); 882 } 883 884 /* 885 * (un)block input via hw flowcontrol 886 */ 887 Static void 888 ucom_hwiflow(struct ucom_softc *sc) 889 { 890 DPRINTF(("ucom_hwiflow:\n")); 891 #if 0 892 XXX 893 bus_space_tag_t iot = sc->sc_iot; 894 bus_space_handle_t ioh = sc->sc_ioh; 895 896 if (sc->sc_mcr_rts == 0) 897 return; 898 899 if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) { 900 CLR(sc->sc_mcr, sc->sc_mcr_rts); 901 CLR(sc->sc_mcr_active, sc->sc_mcr_rts); 902 } else { 903 SET(sc->sc_mcr, sc->sc_mcr_rts); 904 SET(sc->sc_mcr_active, sc->sc_mcr_rts); 905 } 906 bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active); 907 #endif 908 } 909 910 Static void 911 ucomstart(struct tty *tp) 912 { 913 struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)]; 914 usbd_status err; 915 int s; 916 u_char *data; 917 int cnt; 918 919 if (sc->sc_dying) 920 return; 921 922 s = spltty(); 923 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) { 924 DPRINTFN(4,("ucomstart: no go, state=0x%x\n", tp->t_state)); 925 goto out; 926 } 927 if (sc->sc_tx_stopped) 928 goto out; 929 930 if (tp->t_outq.c_cc <= tp->t_lowat) { 931 if (ISSET(tp->t_state, TS_ASLEEP)) { 932 CLR(tp->t_state, TS_ASLEEP); 933 wakeup(&tp->t_outq); 934 } 935 selwakeup(&tp->t_wsel); 936 if (tp->t_outq.c_cc == 0) 937 goto out; 938 } 939 940 /* Grab the first contiguous region of buffer space. */ 941 data = tp->t_outq.c_cf; 942 cnt = ndqb(&tp->t_outq, 0); 943 944 if (cnt == 0) { 945 DPRINTF(("ucomstart: cnt==0\n")); 946 goto out; 947 } 948 949 SET(tp->t_state, TS_BUSY); 950 951 if (cnt > sc->sc_obufsize) { 952 DPRINTF(("ucomstart: big buffer %d chars\n", cnt)); 953 cnt = sc->sc_obufsize; 954 } 955 if (sc->sc_methods->ucom_write != NULL) 956 sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno, 957 sc->sc_obuf, data, &cnt); 958 else 959 memcpy(sc->sc_obuf, data, cnt); 960 961 DPRINTFN(4,("ucomstart: %d chars\n", cnt)); 962 usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe, 963 (usbd_private_handle)sc, sc->sc_obuf, cnt, 964 USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb); 965 /* What can we do on error? */ 966 err = usbd_transfer(sc->sc_oxfer); 967 #ifdef DIAGNOSTIC 968 if (err != USBD_IN_PROGRESS) 969 printf("ucomstart: err=%s\n", usbd_errstr(err)); 970 #endif 971 972 out: 973 splx(s); 974 } 975 976 #if defined(__NetBSD__) 977 void 978 #else 979 int 980 #endif 981 ucomstop(struct tty *tp, int flag) 982 { 983 DPRINTF(("ucomstop: flag=%d\n", flag)); 984 #if 0 985 /*struct ucom_softc *sc = ucom_cd.cd_devs[UCOMUNIT(tp->t_dev)];*/ 986 int s; 987 988 s = spltty(); 989 if (ISSET(tp->t_state, TS_BUSY)) { 990 DPRINTF(("ucomstop: XXX\n")); 991 /* sc->sc_tx_stopped = 1; */ 992 if (!ISSET(tp->t_state, TS_TTSTOP)) 993 SET(tp->t_state, TS_FLUSH); 994 } 995 splx(s); 996 #endif 997 #if !defined(__NetBSD__) 998 return (0); 999 #endif 1000 } 1001 1002 Static void 1003 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status) 1004 { 1005 struct ucom_softc *sc = (struct ucom_softc *)p; 1006 struct tty *tp = sc->sc_tty; 1007 u_int32_t cc; 1008 int s; 1009 1010 DPRINTFN(5,("ucomwritecb: status=%d\n", status)); 1011 1012 if (status == USBD_CANCELLED || sc->sc_dying) 1013 goto error; 1014 1015 if (status) { 1016 DPRINTF(("ucomwritecb: status=%d\n", status)); 1017 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe); 1018 /* XXX we should restart after some delay. */ 1019 goto error; 1020 } 1021 1022 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL); 1023 #if defined(__NetBSD__) && NRND > 0 1024 rnd_add_uint32(&sc->sc_rndsource, cc); 1025 #endif 1026 DPRINTFN(5,("ucomwritecb: cc=%d\n", cc)); 1027 /* convert from USB bytes to tty bytes */ 1028 cc -= sc->sc_opkthdrlen; 1029 1030 s = spltty(); 1031 CLR(tp->t_state, TS_BUSY); 1032 if (ISSET(tp->t_state, TS_FLUSH)) 1033 CLR(tp->t_state, TS_FLUSH); 1034 else 1035 ndflush(&tp->t_outq, cc); 1036 (*linesw[tp->t_line].l_start)(tp); 1037 splx(s); 1038 return; 1039 1040 error: 1041 s = spltty(); 1042 CLR(tp->t_state, TS_BUSY); 1043 splx(s); 1044 } 1045 1046 Static usbd_status 1047 ucomstartread(struct ucom_softc *sc) 1048 { 1049 usbd_status err; 1050 1051 DPRINTFN(5,("ucomstartread: start\n")); 1052 usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe, 1053 (usbd_private_handle)sc, 1054 sc->sc_ibuf, sc->sc_ibufsize, 1055 USBD_SHORT_XFER_OK | USBD_NO_COPY, 1056 USBD_NO_TIMEOUT, ucomreadcb); 1057 err = usbd_transfer(sc->sc_ixfer); 1058 if (err != USBD_IN_PROGRESS) { 1059 DPRINTF(("ucomstartread: err=%s\n", usbd_errstr(err))); 1060 return (err); 1061 } 1062 return (USBD_NORMAL_COMPLETION); 1063 } 1064 1065 Static void 1066 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status) 1067 { 1068 struct ucom_softc *sc = (struct ucom_softc *)p; 1069 struct tty *tp = sc->sc_tty; 1070 int (*rint)(int c, struct tty *tp) = linesw[tp->t_line].l_rint; 1071 usbd_status err; 1072 u_int32_t cc; 1073 u_char *cp; 1074 int s; 1075 1076 DPRINTFN(5,("ucomreadcb: status=%d\n", status)); 1077 1078 if (status == USBD_CANCELLED || status == USBD_IOERROR || 1079 sc->sc_dying) { 1080 DPRINTF(("ucomreadcb: dying\n")); 1081 /* Send something to wake upper layer */ 1082 s = spltty(); 1083 (*rint)('\n', tp); 1084 ttwakeup(tp); 1085 splx(s); 1086 return; 1087 } 1088 1089 if (status) { 1090 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe); 1091 /* XXX we should restart after some delay. */ 1092 return; 1093 } 1094 1095 usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL); 1096 #if defined(__NetBSD__) && NRND > 0 1097 rnd_add_uint32(&sc->sc_rndsource, cc); 1098 #endif 1099 DPRINTFN(5,("ucomreadcb: got %d chars, tp=%p\n", cc, tp)); 1100 if (sc->sc_methods->ucom_read != NULL) 1101 sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno, 1102 &cp, &cc); 1103 1104 s = spltty(); 1105 /* Give characters to tty layer. */ 1106 while (cc-- > 0) { 1107 DPRINTFN(7,("ucomreadcb: char=0x%02x\n", *cp)); 1108 if ((*rint)(*cp++, tp) == -1) { 1109 /* XXX what should we do? */ 1110 printf("%s: lost %d chars\n", USBDEVNAME(sc->sc_dev), 1111 cc); 1112 break; 1113 } 1114 } 1115 splx(s); 1116 1117 err = ucomstartread(sc); 1118 if (err) { 1119 printf("%s: read start failed\n", USBDEVNAME(sc->sc_dev)); 1120 /* XXX what should we dow now? */ 1121 } 1122 } 1123 1124 Static void 1125 ucom_cleanup(struct ucom_softc *sc) 1126 { 1127 DPRINTF(("ucom_cleanup: closing pipes\n")); 1128 1129 ucom_shutdown(sc); 1130 if (sc->sc_bulkin_pipe != NULL) { 1131 usbd_abort_pipe(sc->sc_bulkin_pipe); 1132 usbd_close_pipe(sc->sc_bulkin_pipe); 1133 sc->sc_bulkin_pipe = NULL; 1134 } 1135 if (sc->sc_bulkout_pipe != NULL) { 1136 usbd_abort_pipe(sc->sc_bulkout_pipe); 1137 usbd_close_pipe(sc->sc_bulkout_pipe); 1138 sc->sc_bulkout_pipe = NULL; 1139 } 1140 if (sc->sc_ixfer != NULL) { 1141 usbd_free_xfer(sc->sc_ixfer); 1142 sc->sc_ixfer = NULL; 1143 } 1144 if (sc->sc_oxfer != NULL) { 1145 usbd_free_xfer(sc->sc_oxfer); 1146 sc->sc_oxfer = NULL; 1147 } 1148 } 1149 1150 #endif /* NUCOM > 0 */ 1151 1152 int 1153 ucomprint(void *aux, const char *pnp) 1154 { 1155 struct ucom_attach_args *uca = aux; 1156 1157 if (pnp) 1158 printf("ucom at %s", pnp); 1159 if (uca->portno != UCOM_UNK_PORTNO) 1160 printf(" portno %d", uca->portno); 1161 return (UNCONF); 1162 } 1163 1164 int 1165 #if defined(__OpenBSD__) 1166 ucomsubmatch(struct device *parent, void *match, void *aux) 1167 #else 1168 ucomsubmatch(struct device *parent, struct cfdata *cf, void *aux) 1169 #endif 1170 { 1171 struct ucom_attach_args *uca = aux; 1172 #if defined(__OpenBSD__) 1173 struct cfdata *cf = match; 1174 #endif 1175 1176 if (uca->portno != UCOM_UNK_PORTNO && 1177 cf->ucomcf_portno != UCOM_UNK_PORTNO && 1178 cf->ucomcf_portno != uca->portno) 1179 return (0); 1180 return ((*cf->cf_attach->ca_match)(parent, cf, aux)); 1181 } 1182