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