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