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