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