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