1 /* $NetBSD: ucom.c,v 1.155 2026/11/10 03:43:27 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (lennart (at) augustsson.net) at 9 * Carlstedt Research & Technology. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 /* 33 * This code is very heavily based on the 16550 driver, com.c. 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: ucom.c,v 1.155 2026/11/10 03:43:27 thorpej Exp $"); 38 39 #ifdef _KERNEL_OPT 40 #include "opt_ntp.h" 41 #include "opt_usb.h" 42 #endif 43 44 #include <sys/param.h> 45 #include <sys/callout.h> 46 #include <sys/conf.h> 47 #include <sys/device.h> 48 #include <sys/file.h> 49 #include <sys/ioctl.h> 50 #include <sys/kauth.h> 51 #include <sys/kernel.h> 52 #include <sys/poll.h> 53 #include <sys/proc.h> 54 #include <sys/queue.h> 55 #include <sys/rndsource.h> 56 #include <sys/select.h> 57 #include <sys/sysctl.h> 58 #include <sys/systm.h> 59 #include <sys/timepps.h> 60 #include <sys/tty.h> 61 #include <sys/vnode.h> 62 63 #include <ddb/db_active.h> 64 65 #include <dev/cons.h> 66 67 #include <dev/usb/usb.h> 68 #include <dev/usb/usbdi.h> 69 #include <dev/usb/usbdevs.h> 70 #include <dev/usb/usbhist.h> 71 72 #include <dev/usb/ucomvar.h> 73 74 #include "ucom.h" 75 #include "locators.h" 76 #include "ioconf.h" 77 78 #if NUCOM > 0 79 80 #ifdef USB_DEBUG 81 #ifndef UCOM_DEBUG 82 #define ucomdebug 0 83 #else 84 int ucomdebug = 0; 85 86 SYSCTL_SETUP(sysctl_hw_ucom_setup, "sysctl hw.ucom setup") 87 { 88 int err; 89 const struct sysctlnode *rnode; 90 const struct sysctlnode *cnode; 91 92 err = sysctl_createv(clog, 0, NULL, &rnode, 93 CTLFLAG_PERMANENT, CTLTYPE_NODE, "ucom", 94 SYSCTL_DESCR("ucom global controls"), 95 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL); 96 97 if (err) 98 goto fail; 99 100 /* control debugging printfs */ 101 err = sysctl_createv(clog, 0, &rnode, &cnode, 102 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 103 "debug", SYSCTL_DESCR("Enable debugging output"), 104 NULL, 0, &ucomdebug, sizeof(ucomdebug), CTL_CREATE, CTL_EOL); 105 if (err) 106 goto fail; 107 108 return; 109 fail: 110 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err); 111 } 112 113 #endif /* UCOM_DEBUG */ 114 #endif /* USB_DEBUG */ 115 116 #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(ucomdebug,1,FMT,A,B,C,D) 117 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(ucomdebug,N,FMT,A,B,C,D) 118 #define UCOMHIST_FUNC() USBHIST_FUNC() 119 #define UCOMHIST_CALLED(name) USBHIST_CALLED(ucomdebug) 120 121 #define UCOMCALLUNIT_MASK TTCALLUNIT_MASK 122 #define UCOMUNIT_MASK TTUNIT_MASK 123 #define UCOMDIALOUT_MASK TTDIALOUT_MASK 124 125 #define UCOMCALLUNIT(x) TTCALLUNIT(x) 126 #define UCOMUNIT(x) TTUNIT(x) 127 #define UCOMDIALOUT(x) TTDIALOUT(x) 128 #define ucom_unit tty_unit 129 130 /* 131 * XXX: We can submit multiple input/output buffers to the usb stack 132 * to improve throughput, but the usb stack is too lame to deal with this 133 * in a number of places. 134 */ 135 #define UCOM_IN_BUFFS 1 136 #define UCOM_OUT_BUFFS 1 137 138 struct ucom_buffer { 139 SIMPLEQ_ENTRY(ucom_buffer) ub_link; 140 struct usbd_xfer *ub_xfer; 141 u_char *ub_data; 142 u_int ub_len; 143 u_int ub_index; 144 }; 145 146 struct ucom_softc { 147 device_t sc_dev; /* base device */ 148 149 struct usbd_device * sc_udev; /* USB device */ 150 struct usbd_interface * sc_iface; /* data interface */ 151 152 int sc_bulkin_no; /* bulk in endpoint address */ 153 struct usbd_pipe * sc_bulkin_pipe;/* bulk in pipe */ 154 u_int sc_ibufsize; /* read buffer size */ 155 u_int sc_ibufsizepad; /* read buffer size padded */ 156 struct ucom_buffer sc_ibuff[UCOM_IN_BUFFS]; 157 SIMPLEQ_HEAD(, ucom_buffer) sc_ibuff_empty; 158 SIMPLEQ_HEAD(, ucom_buffer) sc_ibuff_full; 159 160 int sc_bulkout_no; /* bulk out endpoint address */ 161 struct usbd_pipe * sc_bulkout_pipe;/* bulk out pipe */ 162 u_int sc_obufsize; /* write buffer size */ 163 u_int sc_opkthdrlen; /* header length of */ 164 struct ucom_buffer sc_obuff[UCOM_OUT_BUFFS]; 165 SIMPLEQ_HEAD(, ucom_buffer) sc_obuff_free; 166 SIMPLEQ_HEAD(, ucom_buffer) sc_obuff_full; 167 168 void *sc_si; 169 170 const struct ucom_methods *sc_methods; 171 void *sc_parent; 172 int sc_portno; 173 174 struct tty *sc_tty; /* our tty */ 175 u_char sc_lsr; 176 u_char sc_msr; 177 u_char sc_mcr; 178 volatile u_char sc_rx_stopped; 179 u_char sc_rx_unblock; 180 u_char sc_tx_stopped; 181 int sc_swflags; 182 183 enum ucom_state { 184 UCOM_DEAD, 185 UCOM_ATTACHED, 186 UCOM_OPENING, 187 UCOM_OPEN 188 } sc_state; 189 bool sc_closing; /* software is closing */ 190 bool sc_dying; /* hardware is gone */ 191 struct ucom_cnstate *sc_cn; /* console state */ 192 193 struct pps_state sc_pps_state; /* pps state */ 194 195 krndsource_t sc_rndsource; /* random source */ 196 197 kmutex_t sc_lock; 198 kcondvar_t sc_statecv; 199 200 struct timeval sc_hup_time; 201 }; 202 203 dev_type_open(ucomopen); 204 dev_type_cancel(ucomcancel); 205 dev_type_close(ucomclose); 206 dev_type_read(ucomread); 207 dev_type_write(ucomwrite); 208 dev_type_ioctl(ucomioctl); 209 dev_type_stop(ucomstop); 210 dev_type_tty(ucomtty); 211 dev_type_poll(ucompoll); 212 213 const struct cdevsw ucom_cdevsw = { 214 .d_open = ucomopen, 215 .d_cancel = ucomcancel, 216 .d_close = ucomclose, 217 .d_read = ucomread, 218 .d_write = ucomwrite, 219 .d_ioctl = ucomioctl, 220 .d_stop = ucomstop, 221 .d_tty = ucomtty, 222 .d_poll = ucompoll, 223 .d_mmap = nommap, 224 .d_kqfilter = ttykqfilter, 225 .d_discard = nodiscard, 226 .d_cfdriver = &ucom_cd, 227 .d_devtounit = ucom_unit, 228 .d_flag = D_TTY | D_MPSAFE 229 }; 230 231 static void ucom_cleanup(struct ucom_softc *, int); 232 static int ucomparam(struct tty *, struct termios *); 233 static int ucomhwiflow(struct tty *, int); 234 static void ucomstart(struct tty *); 235 static void ucom_shutdown(struct ucom_softc *); 236 static void ucom_dtr(struct ucom_softc *, int); 237 static void ucom_rts(struct ucom_softc *, int); 238 static void ucom_break(struct ucom_softc *, int); 239 static void tiocm_to_ucom(struct ucom_softc *, u_long, int); 240 static int ucom_to_tiocm(struct ucom_softc *); 241 242 static void ucomreadcb(struct usbd_xfer *, void *, usbd_status); 243 static void ucom_submit_write(struct ucom_softc *, struct ucom_buffer *); 244 static void ucom_write_status(struct ucom_softc *, struct ucom_buffer *, 245 usbd_status); 246 247 static void ucomwritecb(struct usbd_xfer *, void *, usbd_status); 248 static void ucom_read_complete(struct ucom_softc *); 249 static int ucomsubmitread(struct ucom_softc *, struct ucom_buffer *); 250 static void ucom_softintr(void *); 251 252 static bool ucom_polling(struct ucom_softc *); 253 static void ucom_mutex_enter(struct ucom_softc *); 254 static void ucom_mutex_exit(struct ucom_softc *); 255 static bool ucom_xfer_done(struct usbd_xfer *); 256 257 static void ucom_cninit(struct ucom_softc *); 258 static void ucom_cnread_complete(struct ucom_softc *); 259 static int ucom_cnput_bytes(struct ucom_softc *); 260 static void ucom_cnput_complete(struct usbd_xfer *, void *, usbd_status); 261 static void ucom_cnput_callout(void *); 262 static void ucom_cnput_softintr(void *); 263 static int ucom_cngetc(dev_t); 264 static void ucom_cnputc(dev_t, int); 265 static void ucom_cnpollc(dev_t, int); 266 static void ucom_cnfree(struct ucom_softc *); 267 static void ucom_cnhalt(dev_t); 268 static void ucom_cncheckmagic(struct ucom_softc *, struct ucom_buffer *); 269 270 /* 271 * Following are all routines needed for COM to act as console 272 */ 273 static struct consdev ucom_cnops = { 274 .cn_getc = ucom_cngetc, 275 .cn_putc = ucom_cnputc, 276 .cn_pollc = ucom_cnpollc, 277 .cn_halt = ucom_cnhalt, 278 .cn_dev = NODEV, 279 .cn_pri = CN_NORMAL 280 }; 281 static int ucom_cnspeed = B115200; 282 283 struct ucom_cnstate { 284 dev_t cn_dev; 285 u_char *cn_buf_in; 286 u_int cn_prod_in; 287 u_int cn_cons_in; 288 289 uint32_t cn_sending; 290 struct usbd_xfer *cn_xfer_out; 291 u_char *cn_buf_out; 292 size_t cn_buf_out_size; 293 u_char *cn_linear_buf_out; 294 u_int cn_prod_out; 295 u_int cn_cons_out; 296 void *cn_si; 297 callout_t cn_callout; 298 struct cnm_state cn_magic_state; 299 bool cn_magic_req; 300 bool cn_polling; 301 }; 302 303 int ucom_match(device_t, cfdata_t, void *); 304 void ucom_attach(device_t, device_t, void *); 305 int ucom_detach(device_t, int); 306 307 CFATTACH_DECL_NEW(ucom, sizeof(struct ucom_softc), ucom_match, ucom_attach, 308 ucom_detach, NULL); 309 310 int 311 ucom_match(device_t parent, cfdata_t match, void *aux) 312 { 313 return 1; 314 } 315 316 /* 317 * Output a chunk of data. 318 * 319 * This is the consumer side of the output ring buffer. 320 */ 321 static void 322 ucom_cnput_complete(struct usbd_xfer *xfer, void *priv, usbd_status status) 323 { 324 struct ucom_softc * const sc = priv; 325 struct ucom_cnstate * const cn = sc->sc_cn; 326 bool polling = ucom_polling(sc); 327 usbd_status err; 328 uint32_t cnt; 329 330 KASSERT(ucom_xfer_done(cn->cn_xfer_out)); 331 332 usbd_get_xfer_status(cn->cn_xfer_out, NULL, NULL, &cnt, &err); 333 334 u_int cnconsout = atomic_load_relaxed(&cn->cn_cons_out); 335 336 switch (err) { 337 case USBD_STALLED: 338 if (!polling) 339 usbd_clear_endpoint_stall_async(sc->sc_bulkout_pipe); 340 341 /* 342 * The device can't keep up so give it a rest and retry 343 * again in 10 ticks. 344 * Do not move cn->cn_cons_out so that this output is 345 * retried later. 346 * XXXNH Maybe a more scientific retry timer calculation. 347 * When DDB is active, ucom_cnputc() loops on 348 * ucom_cnput_bytes(), this will perform the retry. 349 */ 350 if (!db_active) 351 callout_schedule(&cn->cn_callout, 10); 352 return; 353 354 case USBD_NORMAL_COMPLETION: 355 cnconsout += cnt; 356 atomic_store_release(&cn->cn_cons_out, cnconsout); 357 break; 358 359 case USBD_CANCELLED: 360 /* 361 * Device close causes pipe abort, and all in-progress 362 * transfers get USBD_CANCELLED. We will retry the 363 * output on next ucom_cnput_bytes() call. 364 */ 365 break; 366 367 default: 368 /* 369 * Catch unusual errors when DIAGNOSTIC, otherwise 370 * retry on next com_cnput_bytes() call. 371 */ 372 #ifdef DIAGNOSTIC 373 printf("%s: error %d", __func__, err); 374 #endif 375 break; 376 } 377 378 KASSERT(db_active || atomic_load_relaxed(&cn->cn_sending) == 1); 379 380 /* 381 * Under DDB rule, just return, ucom_cnput_bytes() should 382 * poll for completion. 383 */ 384 if (db_active) 385 return; 386 387 (void)ucom_cnput_bytes(sc); 388 } 389 390 static int 391 ucom_cnput_bytes(struct ucom_softc *sc) 392 { 393 struct ucom_cnstate * const cn = sc->sc_cn; 394 u_char *ub_out = usbd_get_buffer(cn->cn_xfer_out); 395 u_char *out = NULL; 396 usbd_status err; 397 u_int prodout = atomic_load_acquire(&cn->cn_prod_out); 398 u_int consout = atomic_load_relaxed(&cn->cn_cons_out); 399 u_int consout_idx = consout % cn->cn_buf_out_size; 400 u_int cnt = prodout - consout; 401 402 if (cnt > sc->sc_obufsize) 403 cnt = sc->sc_obufsize; 404 405 KASSERT(db_active || atomic_load_relaxed(&cn->cn_sending) == 1); 406 407 /* Nothing to do so success. */ 408 if (cnt == 0) { 409 if (!db_active) 410 atomic_store_relaxed(&cn->cn_sending, 0); 411 return 0; 412 } 413 414 u_int cntidx = (consout_idx + cnt) % cn->cn_buf_out_size; 415 if (cntidx > consout_idx || cntidx == 0) { 416 out = cn->cn_buf_out + consout_idx; 417 } else { 418 /* 419 * Producer wrapped around the ring buffer, 420 * data to send is split in two chenks that 421 * we copy to the linear buffer. 422 * - from consout_idx to end of ring buffer 423 * - from start of ring buffer up to cnt total. 424 */ 425 u_int top_size = cn->cn_buf_out_size - consout_idx; 426 427 memcpy(cn->cn_linear_buf_out, 428 cn->cn_buf_out + consout_idx, top_size); 429 430 memcpy(cn->cn_linear_buf_out + top_size, 431 cn->cn_buf_out, cntidx); 432 433 out = cn->cn_linear_buf_out; 434 } 435 436 if (sc->sc_methods->ucom_write != NULL) 437 sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno, 438 ub_out, out, &cnt); 439 else 440 memcpy(ub_out, out, cnt); 441 442 KASSERT(ucom_xfer_done(cn->cn_xfer_out)); 443 444 /* 445 * cn->cn_cons_out is increased after output succeeds, 446 * in callback ucom_cnput_complete(). 447 */ 448 usbd_setup_xfer(cn->cn_xfer_out, sc, ub_out, cnt, 449 0, USBD_NO_TIMEOUT, ucom_cnput_complete); 450 err = usbd_transfer(cn->cn_xfer_out); 451 452 switch (err) { 453 case USBD_NOT_STARTED: 454 case USBD_IN_PROGRESS: 455 /* 456 * Poll for completion if DDB is active. 457 * Otherwise rely on cn->cn_sending to serialize transfers 458 */ 459 while (db_active && ucom_polling(sc) && 460 !ucom_xfer_done(cn->cn_xfer_out)) 461 usbd_dopoll(sc->sc_iface); 462 break; 463 case USBD_CANCELLED: 464 case USBD_NORMAL_COMPLETION: 465 break; 466 default: 467 #ifdef DIAGNOSTIC 468 printf("%s: error %d", __func__, err); 469 #endif 470 break; 471 } 472 473 return cnt; 474 } 475 476 static void 477 ucom_cnput_softintr(void *priv) 478 { 479 struct ucom_softc * const sc = priv; 480 struct ucom_cnstate * const cn = sc->sc_cn; 481 482 /* 483 * atomic_cas_32() returns the original value, 484 * hence we only proceed if cn->cn_sending was 0. 485 * This makes sure ucom_cnput_bytes() does not 486 * reuse cn->cn_xfer_out before it completes the 487 * transfer. 488 */ 489 if (atomic_cas_32(&cn->cn_sending, 0, 1) == 0) 490 (void)ucom_cnput_bytes(sc); 491 } 492 493 static void 494 ucom_cnput_callout(void *priv) 495 { 496 struct ucom_softc * const sc = priv; 497 struct ucom_cnstate * const cn = sc->sc_cn; 498 499 kpreempt_disable(); 500 softint_schedule(cn->cn_si); 501 kpreempt_enable(); 502 } 503 504 static bool 505 ucom_polling(struct ucom_softc *sc) 506 { 507 struct ucom_cnstate * const cn = sc->sc_cn; 508 509 if (cn == NULL) 510 return false; 511 512 return cn->cn_polling; 513 } 514 515 static void 516 ucom_mutex_enter(struct ucom_softc *sc) 517 { 518 if (!ucom_polling(sc)) 519 mutex_enter(&sc->sc_lock); 520 } 521 522 static void 523 ucom_mutex_exit(struct ucom_softc *sc) 524 { 525 if (!ucom_polling(sc)) 526 mutex_exit(&sc->sc_lock); 527 } 528 529 530 static void 531 ucom_cninit(struct ucom_softc *sc) 532 { 533 int major = cdevsw_lookup_major(&ucom_cdevsw); 534 int unit = device_unit(sc->sc_dev); 535 struct tty *tp = sc->sc_tty; 536 struct termios t; 537 struct ucom_buffer *ub; 538 int error = -1; 539 540 if (makedev(major, unit) != ucom_cnops.cn_dev) 541 return; 542 543 /* 544 * TIOCFLAG_SOFTCAR sets CLOCAL in ucomopen(). 545 */ 546 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR); 547 548 memset(&t, 0, sizeof(t)); 549 t.c_ispeed = ucom_cnspeed; 550 t.c_ospeed = ucom_cnspeed; 551 error = ucomparam(tp, &t); 552 if (error) 553 aprint_error_dev(sc->sc_dev, 554 "ucomparam fail, error = %d", error); 555 556 sc->sc_cn = kmem_zalloc(sizeof(*sc->sc_cn), KM_SLEEP); 557 struct ucom_cnstate * const cn = sc->sc_cn; 558 559 cn->cn_dev = ucom_cnops.cn_dev; 560 561 cn->cn_si = softint_establish(SOFTINT_USB, ucom_cnput_softintr, sc); 562 563 callout_init(&cn->cn_callout, CALLOUT_MPSAFE); 564 callout_setfunc(&cn->cn_callout, ucom_cnput_callout, sc); 565 566 cn->cn_buf_in = kmem_alloc(sc->sc_ibufsize, KM_SLEEP); 567 cn->cn_prod_in = 0; 568 cn->cn_cons_in = 0; 569 570 /* 571 * Kernel can easily write to the output buffer faster 572 * that the serial interface can consume. We need a 573 * buffer large enough to avoid overflown abd 574 * sc->sc_obufsize, may be too small for that. 575 */ 576 cn->cn_buf_out_size = MAX(sc->sc_obufsize, PAGE_SIZE); 577 cn->cn_buf_out = kmem_alloc(cn->cn_buf_out_size, KM_SLEEP); 578 cn->cn_prod_out = 0; 579 cn->cn_cons_out = 0; 580 if (usbd_create_xfer(sc->sc_bulkout_pipe, 581 sc->sc_obufsize, 0, 0, &cn->cn_xfer_out) != 0) { 582 aprint_error_dev(sc->sc_dev, "fail to create cn_xfer_out"); 583 goto ucom_cnfail; 584 } 585 586 cn->cn_linear_buf_out = kmem_alloc(sc->sc_obufsize, KM_SLEEP); 587 588 cn_tab = &ucom_cnops; 589 cn_init_magic(&cn->cn_magic_state); 590 cn_set_magic("\047\001"); /* default magic is BREAK */ 591 cn_set_magic_action(&cn->cn_magic_state, voidop); 592 593 aprint_normal_dev(sc->sc_dev, "console\n"); 594 595 mutex_enter(&sc->sc_lock); 596 for (size_t i = 0; i < UCOM_IN_BUFFS; i++) { 597 ub = &sc->sc_ibuff[i]; 598 error = ucomsubmitread(sc, ub); 599 if (error) { 600 mutex_exit(&sc->sc_lock); 601 goto ucom_cnfail; 602 } 603 } 604 mutex_exit(&sc->sc_lock); 605 606 return; 607 608 ucom_cnfail: 609 if (error) 610 ucom_cnfree(sc); 611 } 612 613 void 614 ucom_attach(device_t parent, device_t self, void *aux) 615 { 616 struct ucom_softc *sc = device_private(self); 617 struct ucom_attach_args *ucaa = aux; 618 619 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 620 621 if (ucaa->ucaa_info != NULL) 622 aprint_normal(": %s", ucaa->ucaa_info); 623 aprint_normal("\n"); 624 625 prop_dictionary_set_int32(device_properties(self), "port", 626 ucaa->ucaa_portno); 627 628 sc->sc_dev = self; 629 sc->sc_udev = ucaa->ucaa_device; 630 sc->sc_iface = ucaa->ucaa_iface; 631 sc->sc_bulkout_no = ucaa->ucaa_bulkout; 632 sc->sc_bulkin_no = ucaa->ucaa_bulkin; 633 sc->sc_ibufsize = ucaa->ucaa_ibufsize; 634 sc->sc_ibufsizepad = ucaa->ucaa_ibufsizepad; 635 sc->sc_obufsize = ucaa->ucaa_obufsize; 636 sc->sc_opkthdrlen = ucaa->ucaa_opkthdrlen; 637 sc->sc_methods = ucaa->ucaa_methods; 638 sc->sc_parent = ucaa->ucaa_arg; 639 sc->sc_portno = ucaa->ucaa_portno; 640 641 sc->sc_lsr = 0; 642 sc->sc_msr = 0; 643 sc->sc_mcr = 0; 644 sc->sc_tx_stopped = 0; 645 sc->sc_swflags = 0; 646 sc->sc_closing = false; 647 sc->sc_dying = false; 648 sc->sc_cn = NULL; 649 sc->sc_state = UCOM_DEAD; 650 651 sc->sc_si = softint_establish(SOFTINT_USB, ucom_softintr, sc); 652 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB); 653 cv_init(&sc->sc_statecv, "ucomstate"); 654 655 rnd_attach_source(&sc->sc_rndsource, device_xname(sc->sc_dev), 656 RND_TYPE_TTY, RND_FLAG_DEFAULT); 657 658 SIMPLEQ_INIT(&sc->sc_ibuff_empty); 659 SIMPLEQ_INIT(&sc->sc_ibuff_full); 660 SIMPLEQ_INIT(&sc->sc_obuff_free); 661 SIMPLEQ_INIT(&sc->sc_obuff_full); 662 663 memset(sc->sc_ibuff, 0, sizeof(sc->sc_ibuff)); 664 memset(sc->sc_obuff, 0, sizeof(sc->sc_obuff)); 665 666 DPRINTF("open pipes in=%jd out=%jd", 667 sc->sc_bulkin_no, sc->sc_bulkout_no, 0, 0); 668 669 struct ucom_buffer *ub; 670 usbd_status err; 671 int error; 672 673 /* Open the bulk pipes */ 674 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 675 USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe); 676 if (err) { 677 DPRINTF("open bulk in error (addr %jd), err=%jd", 678 sc->sc_bulkin_no, err, 0, 0); 679 error = EIO; 680 goto fail_0; 681 } 682 /* Allocate input buffers */ 683 for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS]; 684 ub++) { 685 error = usbd_create_xfer(sc->sc_bulkin_pipe, 686 sc->sc_ibufsizepad, 0, 0, 687 &ub->ub_xfer); 688 if (error) 689 goto fail_1; 690 ub->ub_data = usbd_get_buffer(ub->ub_xfer); 691 } 692 693 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no, 694 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe); 695 if (err) { 696 DPRINTF("open bulk out error (addr %jd), err=%jd", 697 sc->sc_bulkout_no, err, 0, 0); 698 error = EIO; 699 goto fail_1; 700 } 701 for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS]; 702 ub++) { 703 error = usbd_create_xfer(sc->sc_bulkout_pipe, 704 sc->sc_obufsize, 0, 0, &ub->ub_xfer); 705 if (error) 706 goto fail_2; 707 ub->ub_data = usbd_get_buffer(ub->ub_xfer); 708 SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link); 709 } 710 711 struct tty *tp = tty_alloc(); 712 tp->t_oproc = ucomstart; 713 tp->t_param = ucomparam; 714 tp->t_hwiflow = ucomhwiflow; 715 tp->t_softc = sc; 716 717 sc->sc_tty = tp; 718 719 DPRINTF("tty_attach %#jx", (uintptr_t)tp, 0, 0, 0); 720 tty_attach(tp); 721 722 if (!pmf_device_register(self, NULL, NULL)) 723 aprint_error_dev(self, "couldn't establish power handler\n"); 724 725 sc->sc_state = UCOM_ATTACHED; 726 727 /* 728 * Attach ucom console 729 */ 730 ucom_cninit(sc); 731 732 return; 733 734 fail_2: 735 for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS]; 736 ub++) { 737 if (ub->ub_xfer) 738 usbd_destroy_xfer(ub->ub_xfer); 739 } 740 usbd_close_pipe(sc->sc_bulkout_pipe); 741 sc->sc_bulkout_pipe = NULL; 742 743 fail_1: 744 for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS]; 745 ub++) { 746 if (ub->ub_xfer) 747 usbd_destroy_xfer(ub->ub_xfer); 748 } 749 usbd_close_pipe(sc->sc_bulkin_pipe); 750 sc->sc_bulkin_pipe = NULL; 751 752 fail_0: 753 aprint_error_dev(self, "attach failed, error=%d\n", error); 754 } 755 756 int 757 ucom_cnattach(int unit, int speed) 758 { 759 int major = cdevsw_lookup_major(&ucom_cdevsw); 760 761 /* 762 * This is called from consinit(), but at that time the 763 * usb devices are not yet attached, hence we must defer 764 * ucom console attachement until ucom_attach(). We set 765 * ucomcn.cn_dev to signal ucom_attach() which ucom 766 * unit should attach the console. 767 */ 768 ucom_cnops.cn_dev = makedev(major, unit); 769 ucom_cnspeed = speed; 770 771 return 0; 772 } 773 774 775 int 776 ucom_detach(device_t self, int flags) 777 { 778 struct ucom_softc *sc = device_private(self); 779 struct tty *tp = sc->sc_tty; 780 int maj, mn; 781 int i; 782 783 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 784 785 DPRINTF("sc=%#jx flags=%jd tp=%#jx", (uintptr_t)sc, flags, 786 (uintptr_t)tp, 0); 787 DPRINTF("... pipe=%jd,%jd", sc->sc_bulkin_no, sc->sc_bulkout_no, 0, 0); 788 789 /* Prevent new opens from hanging. */ 790 mutex_enter(&sc->sc_lock); 791 sc->sc_dying = true; 792 mutex_exit(&sc->sc_lock); 793 794 /* 795 * If console is attached, we still have input pipe used 796 * after device is closed, so that we can catch cnmagic 797 * and run DDB. It is time to abort the pipe now. 798 */ 799 if (sc->sc_cn) 800 usbd_abort_pipe(sc->sc_bulkin_pipe); 801 802 pmf_device_deregister(self); 803 804 /* tty is now off. */ 805 if (tp != NULL) { 806 ttylock(tp); 807 CLR(tp->t_state, TS_CARR_ON); 808 CLR(tp->t_cflag, CLOCAL | MDMBUF); 809 ttyunlock(tp); 810 } 811 812 /* locate the major number */ 813 maj = cdevsw_lookup_major(&ucom_cdevsw); 814 815 /* Nuke the vnodes for any open instances. */ 816 mn = device_unit(self); 817 DPRINTF("maj=%jd mn=%jd", maj, mn, 0, 0); 818 vdevgone(maj, mn, mn, VCHR); 819 vdevgone(maj, mn | UCOMDIALOUT_MASK, mn | UCOMDIALOUT_MASK, VCHR); 820 vdevgone(maj, mn | UCOMCALLUNIT_MASK, mn | UCOMCALLUNIT_MASK, VCHR); 821 822 softint_disestablish(sc->sc_si); 823 824 /* Detach and free the tty. */ 825 if (tp != NULL) { 826 tty_detach(tp); 827 tty_free(tp); 828 sc->sc_tty = NULL; 829 } 830 831 for (i = 0; i < UCOM_IN_BUFFS; i++) { 832 if (sc->sc_ibuff[i].ub_xfer != NULL) 833 usbd_destroy_xfer(sc->sc_ibuff[i].ub_xfer); 834 } 835 836 for (i = 0; i < UCOM_OUT_BUFFS; i++) { 837 if (sc->sc_obuff[i].ub_xfer != NULL) 838 usbd_destroy_xfer(sc->sc_obuff[i].ub_xfer); 839 } 840 841 if (sc->sc_bulkin_pipe != NULL) { 842 usbd_close_pipe(sc->sc_bulkin_pipe); 843 sc->sc_bulkin_pipe = NULL; 844 } 845 846 if (sc->sc_bulkout_pipe != NULL) { 847 usbd_close_pipe(sc->sc_bulkout_pipe); 848 sc->sc_bulkout_pipe = NULL; 849 } 850 851 /* 852 * Detach console after pipes are closed, so that 853 * cn->cn_xfer_out is properly aborted. 854 */ 855 if (sc->sc_cn) { 856 ucom_cnhalt(sc->sc_cn->cn_dev); 857 sc->sc_cn = NULL; 858 } 859 860 /* Detach the random source */ 861 rnd_detach_source(&sc->sc_rndsource); 862 863 mutex_destroy(&sc->sc_lock); 864 cv_destroy(&sc->sc_statecv); 865 866 return 0; 867 } 868 869 void 870 ucom_shutdown(struct ucom_softc *sc) 871 { 872 struct tty *tp = sc->sc_tty; 873 874 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 875 876 /* 877 * Hang up if necessary. Wait a bit, so the other side has time to 878 * notice even if we immediately open the port again. 879 */ 880 if (ISSET(tp->t_cflag, HUPCL)) { 881 ucom_dtr(sc, 0); 882 mutex_enter(&sc->sc_lock); 883 microuptime(&sc->sc_hup_time); 884 sc->sc_hup_time.tv_sec++; 885 mutex_exit(&sc->sc_lock); 886 } 887 } 888 889 /* 890 * ucomopen(dev, flag, mode, l) 891 * 892 * Called when anyone tries to open /dev/ttyU? for an existing 893 * ucom? instance that has completed attach. The attach may have 894 * failed, though, or there may be concurrent detach or close in 895 * progress, so fail if attach failed (no sc_tty) or detach has 896 * begun (sc_dying), or wait if there's a concurrent close in 897 * progress before reopening. 898 */ 899 int 900 ucomopen(dev_t dev, int flag, int mode, struct lwp *l) 901 { 902 const int unit = UCOMUNIT(dev); 903 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit); 904 int error = 0; 905 906 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 907 908 mutex_enter(&sc->sc_lock); 909 if (sc->sc_dying) { 910 DPRINTF("... dying", 0, 0, 0, 0); 911 mutex_exit(&sc->sc_lock); 912 return ENXIO; 913 } 914 915 if (!device_is_active(sc->sc_dev)) { 916 mutex_exit(&sc->sc_lock); 917 return ENXIO; 918 } 919 920 struct tty *tp = sc->sc_tty; 921 if (tp == NULL) { 922 DPRINTF("... not attached", 0, 0, 0, 0); 923 mutex_exit(&sc->sc_lock); 924 return ENXIO; 925 } 926 927 DPRINTF("unit=%jd, tp=%#jx", unit, (uintptr_t)tp, 0, 0); 928 929 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) { 930 mutex_exit(&sc->sc_lock); 931 return EBUSY; 932 } 933 934 /* 935 * If the previous use had set DTR on close, wait up to one 936 * second for the other side to notice we hung up. After 937 * sleeping, the tty may have been revoked, so restart the 938 * whole operation. 939 * 940 * XXX The wchan is not ttclose but maybe should be. 941 */ 942 if (timerisset(&sc->sc_hup_time)) { 943 struct timeval now, delta; 944 int ms, ticks; 945 946 microuptime(&now); 947 if (timercmp(&now, &sc->sc_hup_time, <)) { 948 timersub(&sc->sc_hup_time, &now, &delta); 949 ms = MIN(INT_MAX - 1000, delta.tv_sec*1000); 950 ms += howmany(delta.tv_usec, 1000); 951 ticks = MAX(1, MIN(INT_MAX, mstohz(ms))); 952 (void)cv_timedwait(&sc->sc_statecv, &sc->sc_lock, 953 ticks); 954 mutex_exit(&sc->sc_lock); 955 return ERESTART; 956 } 957 timerclear(&sc->sc_hup_time); 958 } 959 960 /* 961 * Wait while the device is initialized by the 962 * first opener or cleaned up by the last closer. 963 */ 964 enum ucom_state state = sc->sc_state; 965 if (state == UCOM_OPENING || sc->sc_closing) { 966 if (flag & FNONBLOCK) 967 error = EWOULDBLOCK; 968 else 969 error = cv_wait_sig(&sc->sc_statecv, &sc->sc_lock); 970 mutex_exit(&sc->sc_lock); 971 return error ? error : ERESTART; 972 } 973 KASSERTMSG(state == UCOM_OPEN || state == UCOM_ATTACHED, 974 "state is %d", state); 975 976 /* 977 * If this is the first open, then make sure the pipes are 978 * running and perform any initialization needed. 979 */ 980 bool firstopen = (state == UCOM_ATTACHED); 981 if (firstopen) { 982 KASSERT(!ISSET(tp->t_state, TS_ISOPEN)); 983 KASSERT(tp->t_wopen == 0); 984 985 tp->t_dev = dev; 986 sc->sc_state = UCOM_OPENING; 987 mutex_exit(&sc->sc_lock); 988 989 if (sc->sc_methods->ucom_open != NULL) { 990 error = sc->sc_methods->ucom_open(sc->sc_parent, 991 sc->sc_portno); 992 if (error) 993 goto bad; 994 } 995 996 ucom_status_change(sc); 997 998 /* Clear PPS capture state on first open. */ 999 mutex_spin_enter(&timecounter_lock); 1000 memset(&sc->sc_pps_state, 0, sizeof(sc->sc_pps_state)); 1001 sc->sc_pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR; 1002 pps_init(&sc->sc_pps_state); 1003 mutex_spin_exit(&timecounter_lock); 1004 1005 /* 1006 * Initialize the termios status to the defaults. Add in the 1007 * sticky bits from TIOCSFLAGS. 1008 */ 1009 struct termios t; 1010 1011 if (cn_isconsole(dev)) { 1012 t.c_ispeed = ucom_cnspeed; 1013 t.c_ospeed = ucom_cnspeed; 1014 } else { 1015 t.c_ispeed = 0; 1016 t.c_ospeed = TTYDEF_SPEED; 1017 } 1018 t.c_cflag = TTYDEF_CFLAG; 1019 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL)) 1020 SET(t.c_cflag, CLOCAL); 1021 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS)) 1022 SET(t.c_cflag, CRTSCTS); 1023 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF)) 1024 SET(t.c_cflag, MDMBUF); 1025 /* Make sure ucomparam() will do something. */ 1026 tp->t_ospeed = 0; 1027 (void) ucomparam(tp, &t); 1028 tp->t_iflag = TTYDEF_IFLAG; 1029 tp->t_oflag = TTYDEF_OFLAG; 1030 tp->t_lflag = TTYDEF_LFLAG; 1031 ttychars(tp); 1032 ttsetwater(tp); 1033 1034 /* 1035 * Turn on DTR. We must always do this, even if carrier is not 1036 * present, because otherwise we'd have to use TIOCSDTR 1037 * immediately after setting CLOCAL, which applications do not 1038 * expect. We always assert DTR while the device is open 1039 * unless explicitly requested to deassert it. Ditto RTS. 1040 */ 1041 ucom_dtr(sc, 1); 1042 ucom_rts(sc, 1); 1043 1044 mutex_enter(&sc->sc_lock); 1045 sc->sc_rx_unblock = 0; 1046 sc->sc_rx_stopped = 0; 1047 sc->sc_tx_stopped = 0; 1048 1049 /* 1050 * If device is console, input xfers are scheduled 1051 * from attach to detach time. Otherwise from open 1052 * here to close. 1053 */ 1054 if (!cn_isconsole(dev)) { 1055 /* 1056 * Either after ucom was attached, or after 1057 * ucomclose(), this list must be empty, 1058 * otherwise ucomsubmitread() will corrupt 1059 * it by queueing an ucom_buffer that is already 1060 * queued. 1061 */ 1062 KASSERT(SIMPLEQ_EMPTY(&sc->sc_ibuff_empty)); 1063 1064 for (size_t i = 0; i < UCOM_IN_BUFFS; i++) { 1065 struct ucom_buffer *ub = &sc->sc_ibuff[i]; 1066 error = ucomsubmitread(sc, ub); 1067 if (error) { 1068 mutex_exit(&sc->sc_lock); 1069 goto bad; 1070 } 1071 } 1072 } 1073 } 1074 mutex_exit(&sc->sc_lock); 1075 1076 DPRINTF("unit=%jd, tp=%#jx dialout %jd nonblock %jd", unit, 1077 (uintptr_t)tp, !!UCOMDIALOUT(dev), !!ISSET(flag, O_NONBLOCK)); 1078 error = ttyopen(tp, UCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK)); 1079 if (error) 1080 goto bad; 1081 1082 error = (*tp->t_linesw->l_open)(dev, tp); 1083 if (error) 1084 goto bad; 1085 1086 /* 1087 * Success! If this was the first open, notify waiters that 1088 * the tty is open for business. 1089 */ 1090 if (firstopen) { 1091 mutex_enter(&sc->sc_lock); 1092 KASSERT(sc->sc_state == UCOM_OPENING); 1093 sc->sc_state = UCOM_OPEN; 1094 cv_broadcast(&sc->sc_statecv); 1095 mutex_exit(&sc->sc_lock); 1096 } 1097 return 0; 1098 1099 bad: 1100 /* 1101 * Failure! If this was the first open, hang up, abort pipes, 1102 * and notify waiters that we're not opening after all. 1103 */ 1104 if (firstopen) { 1105 ucom_cleanup(sc, flag); 1106 1107 mutex_enter(&sc->sc_lock); 1108 KASSERT(sc->sc_state == UCOM_OPENING); 1109 sc->sc_state = UCOM_ATTACHED; 1110 cv_broadcast(&sc->sc_statecv); 1111 mutex_exit(&sc->sc_lock); 1112 } 1113 return error; 1114 } 1115 1116 /* 1117 * ucomcancel(dev, flag, mode, l) 1118 * 1119 * Called on revoke or last close. Must interrupt any pending I/O 1120 * operations and make them fail promptly; once they have all 1121 * finished (except possibly new opens), ucomclose will be called. 1122 * We set sc_closing to block new opens until ucomclose runs. 1123 */ 1124 int 1125 ucomcancel(dev_t dev, int flag, int mode, struct lwp *l) 1126 { 1127 const int unit = UCOMUNIT(dev); 1128 struct ucom_softc *sc = device_lookup_private(&ucom_cd, unit); 1129 1130 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 1131 1132 DPRINTF("unit=%jd", UCOMUNIT(dev), 0, 0, 0); 1133 1134 /* 1135 * This can run at any time before ucomclose on any device 1136 * node, even if never attached or if attach failed, so we may 1137 * not have a softc or a tty. 1138 */ 1139 if (sc == NULL) 1140 return 0; 1141 struct tty *tp = sc->sc_tty; 1142 if (tp == NULL) 1143 return 0; 1144 1145 /* 1146 * Mark the device closing so opens block until we're done 1147 * closing. Wake them up so they start over at the top -- if 1148 * we're closing because we're detaching, they need to wake up 1149 * and notice it's time to fail. 1150 */ 1151 mutex_enter(&sc->sc_lock); 1152 sc->sc_closing = true; 1153 cv_broadcast(&sc->sc_statecv); 1154 mutex_exit(&sc->sc_lock); 1155 1156 /* 1157 * Cancel any pending tty I/O operations, causing them to wake 1158 * up and fail promptly, and preventing any new ones from 1159 * starting to wait until we have finished closing. 1160 */ 1161 ttycancel(tp); 1162 1163 return 0; 1164 } 1165 1166 /* 1167 * ucomclose(dev, flag, mode, l) 1168 * 1169 * Called after ucomcancel, when all prior operations on the /dev 1170 * node have completed. Only new opens may be in progress at this 1171 * point, but they will block until sc_closing is set to false. 1172 */ 1173 int 1174 ucomclose(dev_t dev, int flag, int mode, struct lwp *l) 1175 { 1176 const int unit = UCOMUNIT(dev); 1177 struct ucom_softc *sc = device_lookup_private(&ucom_cd, unit); 1178 int error = 0; 1179 1180 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 1181 1182 DPRINTF("unit=%jd", UCOMUNIT(dev), 0, 0, 0); 1183 1184 /* 1185 * This can run at any time after ucomcancel on any device 1186 * node, even if never attached or if attach failed, so we may 1187 * not have a softc or a tty. 1188 */ 1189 if (sc == NULL) 1190 return 0; 1191 struct tty *tp = sc->sc_tty; 1192 if (tp == NULL) 1193 return 0; 1194 1195 /* 1196 * Close the tty, causing anyone waiting for it to wake, and 1197 * hang up the phone. 1198 */ 1199 ucom_cleanup(sc, flag); 1200 1201 /* 1202 * ttyclose should have cleared TS_ISOPEN and interrupted all 1203 * pending opens, which should have completed by now. 1204 */ 1205 ttylock(tp); 1206 KASSERT(!ISSET(tp->t_state, TS_ISOPEN)); 1207 KASSERT(tp->t_wopen == 0); 1208 ttyunlock(tp); 1209 1210 /* 1211 * Close any device-specific state. 1212 */ 1213 if (sc->sc_methods->ucom_close != NULL) { 1214 sc->sc_methods->ucom_close(sc->sc_parent, 1215 sc->sc_portno); 1216 } 1217 1218 /* 1219 * We're now closed. Can reopen after this point, so resume 1220 * transfers, mark us no longer closing, and notify anyone 1221 * waiting in open. The state may be OPEN or ATTACHED at this 1222 * point -- OPEN if the device was already open when we closed 1223 * it, ATTACHED if we interrupted it in the process of opening. 1224 */ 1225 mutex_enter(&sc->sc_lock); 1226 KASSERTMSG(sc->sc_state == UCOM_ATTACHED || sc->sc_state == UCOM_OPEN, 1227 "%s sc=%p state=%d", device_xname(sc->sc_dev), sc, sc->sc_state); 1228 KASSERT(sc->sc_closing); 1229 sc->sc_state = UCOM_ATTACHED; 1230 sc->sc_closing = false; 1231 cv_broadcast(&sc->sc_statecv); 1232 mutex_exit(&sc->sc_lock); 1233 1234 return error; 1235 } 1236 1237 int 1238 ucomread(dev_t dev, struct uio *uio, int flag) 1239 { 1240 const int unit = UCOMUNIT(dev); 1241 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit); 1242 struct tty *tp = sc->sc_tty; 1243 1244 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 1245 1246 return (*tp->t_linesw->l_read)(tp, uio, flag); 1247 } 1248 1249 int 1250 ucomwrite(dev_t dev, struct uio *uio, int flag) 1251 { 1252 const int unit = UCOMUNIT(dev); 1253 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit); 1254 struct tty *tp = sc->sc_tty; 1255 1256 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 1257 1258 return (*tp->t_linesw->l_write)(tp, uio, flag); 1259 } 1260 1261 int 1262 ucompoll(dev_t dev, int events, struct lwp *l) 1263 { 1264 const int unit = UCOMUNIT(dev); 1265 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit); 1266 struct tty *tp = sc->sc_tty; 1267 1268 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 1269 1270 return (*tp->t_linesw->l_poll)(tp, events, l); 1271 } 1272 1273 struct tty * 1274 ucomtty(dev_t dev) 1275 { 1276 const int unit = UCOMUNIT(dev); 1277 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit); 1278 1279 return sc->sc_tty; 1280 } 1281 1282 int 1283 ucomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 1284 { 1285 const int unit = UCOMUNIT(dev); 1286 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit); 1287 struct tty *tp = sc->sc_tty; 1288 int error; 1289 1290 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 1291 1292 DPRINTF("cmd=0x%08jx", cmd, 0, 0, 0); 1293 1294 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l); 1295 if (error != EPASSTHROUGH) 1296 return error; 1297 1298 error = ttioctl(tp, cmd, data, flag, l); 1299 if (error != EPASSTHROUGH) 1300 return error; 1301 1302 if (sc->sc_methods->ucom_ioctl != NULL) { 1303 error = sc->sc_methods->ucom_ioctl(sc->sc_parent, 1304 sc->sc_portno, cmd, data, flag, l->l_proc); 1305 if (error != EPASSTHROUGH) 1306 return error; 1307 } 1308 1309 error = 0; 1310 1311 DPRINTF("our cmd=0x%08jx", cmd, 0, 0, 0); 1312 1313 switch (cmd) { 1314 case TIOCSBRK: 1315 ucom_break(sc, 1); 1316 break; 1317 1318 case TIOCCBRK: 1319 ucom_break(sc, 0); 1320 break; 1321 1322 case TIOCSDTR: 1323 ucom_dtr(sc, 1); 1324 break; 1325 1326 case TIOCCDTR: 1327 ucom_dtr(sc, 0); 1328 break; 1329 1330 case TIOCGFLAGS: 1331 mutex_enter(&sc->sc_lock); 1332 *(int *)data = sc->sc_swflags; 1333 mutex_exit(&sc->sc_lock); 1334 break; 1335 1336 case TIOCSFLAGS: 1337 error = kauth_authorize_device_tty(l->l_cred, 1338 KAUTH_DEVICE_TTY_PRIVSET, tp); 1339 if (error) 1340 break; 1341 1342 int swflags = *(int *)data; 1343 /* Force local if device is console */ 1344 if (cn_isconsole(dev)) 1345 swflags |= TIOCFLAG_SOFTCAR; 1346 1347 mutex_enter(&sc->sc_lock); 1348 sc->sc_swflags = swflags; 1349 mutex_exit(&sc->sc_lock); 1350 break; 1351 1352 case TIOCMSET: 1353 case TIOCMBIS: 1354 case TIOCMBIC: 1355 tiocm_to_ucom(sc, cmd, *(int *)data); 1356 break; 1357 1358 case TIOCMGET: 1359 *(int *)data = ucom_to_tiocm(sc); 1360 break; 1361 1362 case PPS_IOC_CREATE: 1363 case PPS_IOC_DESTROY: 1364 case PPS_IOC_GETPARAMS: 1365 case PPS_IOC_SETPARAMS: 1366 case PPS_IOC_GETCAP: 1367 case PPS_IOC_FETCH: 1368 #ifdef PPS_SYNC 1369 case PPS_IOC_KCBIND: 1370 #endif 1371 mutex_spin_enter(&timecounter_lock); 1372 error = pps_ioctl(cmd, data, &sc->sc_pps_state); 1373 mutex_spin_exit(&timecounter_lock); 1374 break; 1375 1376 default: 1377 error = EPASSTHROUGH; 1378 break; 1379 } 1380 1381 return error; 1382 } 1383 1384 static void 1385 tiocm_to_ucom(struct ucom_softc *sc, u_long how, int ttybits) 1386 { 1387 u_char combits; 1388 1389 combits = 0; 1390 if (ISSET(ttybits, TIOCM_DTR)) 1391 SET(combits, UMCR_DTR); 1392 if (ISSET(ttybits, TIOCM_RTS)) 1393 SET(combits, UMCR_RTS); 1394 1395 mutex_enter(&sc->sc_lock); 1396 switch (how) { 1397 case TIOCMBIC: 1398 CLR(sc->sc_mcr, combits); 1399 break; 1400 1401 case TIOCMBIS: 1402 SET(sc->sc_mcr, combits); 1403 break; 1404 1405 case TIOCMSET: 1406 CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS); 1407 SET(sc->sc_mcr, combits); 1408 break; 1409 } 1410 u_char mcr = sc->sc_mcr; 1411 mutex_exit(&sc->sc_lock); 1412 1413 if (how == TIOCMSET || ISSET(combits, UMCR_DTR)) 1414 ucom_dtr(sc, (mcr & UMCR_DTR) != 0); 1415 if (how == TIOCMSET || ISSET(combits, UMCR_RTS)) 1416 ucom_rts(sc, (mcr & UMCR_RTS) != 0); 1417 } 1418 1419 static int 1420 ucom_to_tiocm(struct ucom_softc *sc) 1421 { 1422 u_char combits; 1423 int ttybits = 0; 1424 1425 mutex_enter(&sc->sc_lock); 1426 combits = sc->sc_mcr; 1427 if (ISSET(combits, UMCR_DTR)) 1428 SET(ttybits, TIOCM_DTR); 1429 if (ISSET(combits, UMCR_RTS)) 1430 SET(ttybits, TIOCM_RTS); 1431 1432 combits = sc->sc_msr; 1433 if (ISSET(combits, UMSR_DCD)) 1434 SET(ttybits, TIOCM_CD); 1435 if (ISSET(combits, UMSR_CTS)) 1436 SET(ttybits, TIOCM_CTS); 1437 if (ISSET(combits, UMSR_DSR)) 1438 SET(ttybits, TIOCM_DSR); 1439 if (ISSET(combits, UMSR_RI | UMSR_TERI)) 1440 SET(ttybits, TIOCM_RI); 1441 1442 #if 0 1443 XXX; 1444 if (sc->sc_ier != 0) 1445 SET(ttybits, TIOCM_LE); 1446 #endif 1447 mutex_exit(&sc->sc_lock); 1448 1449 return ttybits; 1450 } 1451 1452 static void 1453 ucom_break(struct ucom_softc *sc, int onoff) 1454 { 1455 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 1456 1457 DPRINTF("onoff=%jd", onoff, 0, 0, 0); 1458 1459 if (sc->sc_methods->ucom_set != NULL) 1460 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno, 1461 UCOM_SET_BREAK, onoff); 1462 } 1463 1464 static void 1465 ucom_dtr(struct ucom_softc *sc, int onoff) 1466 { 1467 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 1468 1469 DPRINTF("onoff=%jd", onoff, 0, 0, 0); 1470 1471 if (sc->sc_methods->ucom_set != NULL) 1472 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno, 1473 UCOM_SET_DTR, onoff); 1474 } 1475 1476 static void 1477 ucom_rts(struct ucom_softc *sc, int onoff) 1478 { 1479 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 1480 1481 DPRINTF("onoff=%jd", onoff, 0, 0, 0); 1482 1483 if (sc->sc_methods->ucom_set != NULL) 1484 sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno, 1485 UCOM_SET_RTS, onoff); 1486 } 1487 1488 void 1489 ucom_status_change(struct ucom_softc *sc) 1490 { 1491 /* We get here from sc->sc_methods->ucom_read */ 1492 if (ucom_polling(sc)) 1493 return; 1494 1495 if (sc->sc_methods->ucom_get_status != NULL) { 1496 struct tty *tp = sc->sc_tty; 1497 u_char msr, lsr; 1498 1499 sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno, 1500 &lsr, &msr); 1501 mutex_enter(&sc->sc_lock); 1502 u_char old_msr = sc->sc_msr; 1503 1504 sc->sc_lsr = lsr; 1505 sc->sc_msr = msr; 1506 mutex_exit(&sc->sc_lock); 1507 1508 if (ISSET((msr ^ old_msr), UMSR_DCD)) { 1509 mutex_spin_enter(&timecounter_lock); 1510 pps_capture(&sc->sc_pps_state); 1511 pps_event(&sc->sc_pps_state, 1512 (sc->sc_msr & UMSR_DCD) ? 1513 PPS_CAPTUREASSERT : 1514 PPS_CAPTURECLEAR); 1515 mutex_spin_exit(&timecounter_lock); 1516 1517 (*tp->t_linesw->l_modem)(tp, ISSET(msr, UMSR_DCD)); 1518 } 1519 } else { 1520 mutex_enter(&sc->sc_lock); 1521 sc->sc_lsr = 0; 1522 /* Assume DCD is present, if we have no chance to check it. */ 1523 sc->sc_msr = UMSR_DCD; 1524 mutex_exit(&sc->sc_lock); 1525 } 1526 } 1527 1528 static int 1529 ucomparam(struct tty *tp, struct termios *t) 1530 { 1531 struct ucom_softc * const sc = tp->t_softc; 1532 int error = 0; 1533 1534 /* XXX should take tty lock around touching tp */ 1535 1536 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 1537 1538 /* Check requested parameters. */ 1539 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) { 1540 error = EINVAL; 1541 goto out; 1542 } 1543 1544 /* 1545 * For the console, always force CLOCAL and !HUPCL, so that the port 1546 * is always active. 1547 */ 1548 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) { 1549 SET(t->c_cflag, CLOCAL); 1550 CLR(t->c_cflag, HUPCL); 1551 } 1552 1553 /* 1554 * If there were no changes, don't do anything. This avoids dropping 1555 * input and improves performance when all we did was frob things like 1556 * VMIN and VTIME. 1557 */ 1558 if (tp->t_ospeed == t->c_ospeed && 1559 tp->t_cflag == t->c_cflag) { 1560 goto out; 1561 } 1562 1563 /* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */ 1564 1565 /* And copy to tty. */ 1566 tp->t_ispeed = 0; 1567 tp->t_ospeed = t->c_ospeed; 1568 tp->t_cflag = t->c_cflag; 1569 1570 if (sc->sc_methods->ucom_param != NULL) { 1571 error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno, 1572 t); 1573 if (error) 1574 goto out; 1575 } 1576 1577 /* XXX worry about CHWFLOW */ 1578 1579 /* 1580 * Update the tty layer's idea of the carrier bit, in case we changed 1581 * CLOCAL or MDMBUF. We don't hang up here; we only do that by 1582 * explicit request. 1583 */ 1584 DPRINTF("l_modem", 0, 0, 0, 0); 1585 (void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, UMSR_DCD)); 1586 1587 #if 0 1588 XXX what if the hardware is not open 1589 if (!ISSET(t->c_cflag, CHWFLOW)) { 1590 if (sc->sc_tx_stopped) { 1591 sc->sc_tx_stopped = 0; 1592 ucomstart(tp); 1593 } 1594 } 1595 #endif 1596 out: 1597 return error; 1598 } 1599 1600 static int 1601 ucomhwiflow(struct tty *tp, int block) 1602 { 1603 struct ucom_softc * const sc = tp->t_softc; 1604 int old; 1605 1606 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 1607 1608 KASSERT(&sc->sc_lock); 1609 KASSERT(ttylocked(tp)); 1610 1611 old = sc->sc_rx_stopped; 1612 sc->sc_rx_stopped = (u_char)block; 1613 1614 if (old && !block) { 1615 sc->sc_rx_unblock = 1; 1616 kpreempt_disable(); 1617 softint_schedule(sc->sc_si); 1618 kpreempt_enable(); 1619 } 1620 1621 return 1; 1622 } 1623 1624 static void 1625 ucomstart(struct tty *tp) 1626 { 1627 struct ucom_softc * const sc = tp->t_softc; 1628 struct ucom_buffer *ub; 1629 u_char *data; 1630 int cnt; 1631 1632 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 1633 1634 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) 1635 goto out; 1636 if (sc->sc_tx_stopped) 1637 goto out; 1638 1639 if (!ttypull(tp)) 1640 goto out; 1641 1642 /* Grab the first contiguous region of buffer space. */ 1643 data = tp->t_outq.c_cf; 1644 cnt = ndqb(&tp->t_outq, 0); 1645 1646 if (cnt == 0) 1647 goto out; 1648 1649 ub = SIMPLEQ_FIRST(&sc->sc_obuff_free); 1650 if (ub == NULL) { 1651 SET(tp->t_state, TS_BUSY); 1652 goto out; 1653 } 1654 1655 SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_free, ub_link); 1656 1657 if (SIMPLEQ_FIRST(&sc->sc_obuff_free) == NULL) 1658 SET(tp->t_state, TS_BUSY); 1659 1660 if (cnt > sc->sc_obufsize) 1661 cnt = sc->sc_obufsize; 1662 1663 if (sc->sc_methods->ucom_write != NULL) 1664 sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno, 1665 ub->ub_data, data, &cnt); 1666 else 1667 memcpy(ub->ub_data, data, cnt); 1668 1669 ub->ub_len = cnt; 1670 ub->ub_index = 0; 1671 1672 SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_full, ub, ub_link); 1673 1674 kpreempt_disable(); 1675 softint_schedule(sc->sc_si); 1676 kpreempt_enable(); 1677 1678 out: 1679 DPRINTF("... done", 0, 0, 0, 0); 1680 } 1681 1682 void 1683 ucomstop(struct tty *tp, int flag) 1684 { 1685 #if 0 1686 struct ucom_softc * const sc = tp->t_softc; 1687 1688 mutex_enter(&sc->sc_lock); 1689 ttylock(tp); 1690 if (ISSET(tp->t_state, TS_BUSY)) { 1691 /* obuff_full -> obuff_free? */ 1692 /* sc->sc_tx_stopped = 1; */ 1693 if (!ISSET(tp->t_state, TS_TTSTOP)) 1694 SET(tp->t_state, TS_FLUSH); 1695 } 1696 ttyunlock(tp); 1697 mutex_exit(&sc->sc_lock); 1698 #endif 1699 } 1700 1701 static void 1702 ucom_write_status(struct ucom_softc *sc, struct ucom_buffer *ub, 1703 usbd_status err) 1704 { 1705 struct tty *tp = sc->sc_tty; 1706 uint32_t cc = ub->ub_len; 1707 bool polling = ucom_polling(sc); 1708 1709 KASSERT(polling || mutex_owned(&sc->sc_lock)); 1710 1711 switch (err) { 1712 case USBD_IN_PROGRESS: 1713 ub->ub_index = ub->ub_len; 1714 break; 1715 case USBD_STALLED: 1716 ub->ub_index = 0; 1717 kpreempt_disable(); 1718 softint_schedule(sc->sc_si); 1719 kpreempt_enable(); 1720 break; 1721 case USBD_NORMAL_COMPLETION: 1722 usbd_get_xfer_status(ub->ub_xfer, NULL, NULL, &cc, NULL); 1723 rnd_add_uint32(&sc->sc_rndsource, cc); 1724 /*FALLTHROUGH*/ 1725 default: 1726 SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_full, ub_link); 1727 SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link); 1728 cc -= sc->sc_opkthdrlen; 1729 1730 ttylock(tp); 1731 CLR(tp->t_state, TS_BUSY); 1732 if (ISSET(tp->t_state, TS_FLUSH)) 1733 CLR(tp->t_state, TS_FLUSH); 1734 else 1735 ndflush(&tp->t_outq, cc); 1736 ttyunlock(tp); 1737 1738 if (err != USBD_CANCELLED && err != USBD_IOERROR && 1739 !sc->sc_closing) { 1740 if ((ub = SIMPLEQ_FIRST(&sc->sc_obuff_full)) != NULL) 1741 ucom_submit_write(sc, ub); 1742 1743 ttylock(tp); 1744 (*tp->t_linesw->l_start)(tp); 1745 ttyunlock(tp); 1746 } 1747 break; 1748 } 1749 } 1750 1751 static void 1752 ucom_submit_write(struct ucom_softc *sc, struct ucom_buffer *ub) 1753 { 1754 1755 KASSERT(mutex_owned(&sc->sc_lock)); 1756 1757 usbd_setup_xfer(ub->ub_xfer, sc, ub->ub_data, ub->ub_len, 1758 0, USBD_NO_TIMEOUT, ucomwritecb); 1759 1760 ucom_write_status(sc, ub, usbd_transfer(ub->ub_xfer)); 1761 } 1762 1763 static void 1764 ucomwritecb(struct usbd_xfer *xfer, void *p, usbd_status status) 1765 { 1766 struct ucom_softc *sc = (struct ucom_softc *)p; 1767 1768 ucom_mutex_enter(sc); 1769 ucom_write_status(sc, SIMPLEQ_FIRST(&sc->sc_obuff_full), status); 1770 ucom_mutex_exit(sc); 1771 1772 } 1773 1774 static void 1775 ucom_softintr(void *arg) 1776 { 1777 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 1778 1779 struct ucom_softc *sc = arg; 1780 struct tty *tp = sc->sc_tty; 1781 struct ucom_buffer *ub; 1782 1783 if (ucom_polling(sc)) 1784 return; 1785 1786 mutex_enter(&sc->sc_lock); 1787 1788 struct ucom_cnstate *cn = sc->sc_cn; 1789 1790 if (cn && cn->cn_magic_req) { 1791 if (!SIMPLEQ_EMPTY(&sc->sc_ibuff_empty)) { 1792 cn->cn_magic_req = false; 1793 console_debugger(); 1794 } else { 1795 /* 1796 * Very unlikely case where ucom_softintr() 1797 * raced with ucomreadcb(). Just retry later. 1798 */ 1799 kpreempt_disable(); 1800 softint_schedule(sc->sc_si); 1801 kpreempt_enable(); 1802 } 1803 } 1804 1805 ttylock(tp); 1806 if (!ISSET(tp->t_state, TS_ISOPEN)) { 1807 ttyunlock(tp); 1808 mutex_exit(&sc->sc_lock); 1809 return; 1810 } 1811 ttyunlock(tp); 1812 1813 ub = SIMPLEQ_FIRST(&sc->sc_obuff_full); 1814 1815 if (ub != NULL && ub->ub_index == 0) 1816 ucom_submit_write(sc, ub); 1817 1818 if (sc->sc_rx_unblock) 1819 ucom_read_complete(sc); 1820 1821 mutex_exit(&sc->sc_lock); 1822 } 1823 1824 static void 1825 ucom_read_complete(struct ucom_softc *sc) 1826 { 1827 int (*rint)(int, struct tty *); 1828 struct ucom_buffer *ub; 1829 struct tty *tp; 1830 1831 KASSERT(mutex_owned(&sc->sc_lock)); 1832 1833 tp = sc->sc_tty; 1834 rint = tp->t_linesw->l_rint; 1835 ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full); 1836 1837 while (ub != NULL && !sc->sc_rx_stopped) { 1838 1839 /* XXX ttyinput takes ttylock */ 1840 while (ub->ub_index < ub->ub_len && !sc->sc_rx_stopped) { 1841 /* Give characters to tty layer. */ 1842 if ((*rint)(ub->ub_data[ub->ub_index], tp) == -1) { 1843 /* Overflow: drop remainder */ 1844 ub->ub_index = ub->ub_len; 1845 } else 1846 ub->ub_index++; 1847 } 1848 1849 if (ub->ub_index == ub->ub_len) { 1850 SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_full, ub_link); 1851 ucomsubmitread(sc, ub); 1852 ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full); 1853 } 1854 } 1855 1856 sc->sc_rx_unblock = (ub != NULL); 1857 } 1858 1859 static int 1860 ucomsubmitread(struct ucom_softc *sc, struct ucom_buffer *ub) 1861 { 1862 usbd_status err; 1863 1864 KASSERT(ucom_polling(sc) || mutex_owned(&sc->sc_lock)); 1865 1866 usbd_setup_xfer(ub->ub_xfer, sc, ub->ub_data, sc->sc_ibufsize, 1867 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ucomreadcb); 1868 1869 if ((err = usbd_transfer(ub->ub_xfer)) != USBD_IN_PROGRESS) { 1870 /* XXX: Recover from this, please! */ 1871 device_printf(sc->sc_dev, "%s: err=%s\n", 1872 __func__, usbd_errstr(err)); 1873 return EIO; 1874 } 1875 1876 SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_empty, ub, ub_link); 1877 1878 return 0; 1879 } 1880 1881 static void 1882 ucom_cnread_complete(struct ucom_softc *sc) 1883 { 1884 struct ucom_buffer *ub; 1885 struct ucom_cnstate * const cn = sc->sc_cn; 1886 u_int prodin = cn->cn_prod_in; 1887 u_int consin = cn->cn_cons_in; 1888 u_int prodin_idx = prodin % sc->sc_ibufsize; 1889 u_int consin_idx = consin % sc->sc_ibufsize; 1890 uint32_t cc, cnt; 1891 u_char *cp; 1892 1893 ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full); 1894 1895 while (ub != NULL) { 1896 KASSERT(ucom_xfer_done(ub->ub_xfer)); 1897 1898 cp = ub->ub_data + ub->ub_index; 1899 cc = ub->ub_len - ub->ub_index; 1900 1901 if (cc == 0) 1902 goto next; 1903 1904 if (consin_idx > prodin_idx) { 1905 if (cc > consin_idx - prodin_idx) 1906 cnt = consin_idx - prodin_idx; 1907 else 1908 cnt = cc; 1909 1910 KASSERT(prodin_idx + cnt <= sc->sc_ibufsize); 1911 1912 memcpy(cn->cn_buf_in + prodin_idx, cp, cnt); 1913 cn->cn_prod_in += cnt; 1914 cp += cnt; 1915 cc -= cnt; 1916 } else { /* consin_idx <= prodin_idx */ 1917 /* 1918 * First chunk, from prodin_idx to end of buffer 1919 */ 1920 if (cc > sc->sc_ibufsize - prodin_idx) 1921 cnt = sc->sc_ibufsize - prodin_idx; 1922 else 1923 cnt = cc; 1924 1925 KASSERT(prodin_idx + cnt <= sc->sc_ibufsize); 1926 1927 memcpy(cn->cn_buf_in + prodin_idx, cp, cnt); 1928 cn->cn_prod_in += cnt; 1929 cp += cnt; 1930 cc -= cnt; 1931 1932 if (cc == 0) 1933 goto next; 1934 1935 /* 1936 * Second chunk, from start of buffer to cosin_idx 1937 */ 1938 if (cc > consin_idx) 1939 cnt = consin_idx; 1940 else 1941 cnt = cc; 1942 1943 KASSERT(cn->cn_prod_in + cnt <= sc->sc_ibufsize); 1944 1945 memcpy(cn->cn_buf_in + prodin_idx, cp, cnt); 1946 cn->cn_prod_in += cnt; 1947 cp += cnt; 1948 cc -= cnt; 1949 } 1950 next: 1951 /* If cc > 0 we discard some input */ 1952 1953 SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_full, ub_link); 1954 ucomsubmitread(sc, ub); 1955 ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full); 1956 } 1957 } 1958 1959 static void 1960 ucomreadcb(struct usbd_xfer *xfer, void *p, usbd_status status) 1961 { 1962 struct ucom_softc *sc = (struct ucom_softc *)p; 1963 struct ucom_buffer *ub; 1964 uint32_t cc; 1965 u_char *cp; 1966 1967 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 1968 1969 ucom_mutex_enter(sc); 1970 1971 /* 1972 * Always remove the ucom_buffer from the sc_ibuff_empty list 1973 * regardless of transfer status. Leaving it on the list on 1974 * USBD_CANCELLED or USBD_IOERROR would cause corruption 1975 * next time the device is opened. 1976 * 1977 * USBD_CANCELLED is a reported when the device is closed 1978 * via ucomclose() -> usbd_abort_pipe() -> usbd_xfer_abort() 1979 */ 1980 ub = SIMPLEQ_FIRST(&sc->sc_ibuff_empty); 1981 SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_empty, ub_link); 1982 KASSERT(xfer == ub->ub_xfer); 1983 1984 if (status == USBD_CANCELLED || status == USBD_IOERROR || 1985 sc->sc_closing) { 1986 DPRINTF("... done (status %jd closing %jd)", 1987 status, sc->sc_closing, 0, 0); 1988 1989 /* 1990 * If console is attached, keep reading after 1991 * close so that we can catch cnmagic. 1992 */ 1993 if (sc->sc_cn) 1994 ucomsubmitread(sc, ub); 1995 1996 ucom_mutex_exit(sc); 1997 1998 return; 1999 } 2000 2001 if (status != USBD_NORMAL_COMPLETION) { 2002 if (status == USBD_STALLED) { 2003 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe); 2004 } else { 2005 device_printf(sc->sc_dev, 2006 "ucomreadcb: wonky status=%s\n", 2007 usbd_errstr(status)); 2008 } 2009 2010 DPRINTF("... done (status %jd)", status, 0, 0, 0); 2011 /* re-adds ub to sc_ibuff_empty */ 2012 ucomsubmitread(sc, ub); 2013 ucom_mutex_exit(sc); 2014 return; 2015 } 2016 2017 usbd_get_xfer_status(xfer, NULL, (void *)&cp, &cc, NULL); 2018 2019 #ifdef UCOM_DEBUG 2020 /* This is triggered by uslsa(4) occasionally. */ 2021 if ((ucomdebug > 0) && (cc == 0)) { 2022 device_printf(sc->sc_dev, "ucomreadcb: zero length xfer!\n"); 2023 } 2024 #endif 2025 2026 KDASSERT(cp == ub->ub_data); 2027 2028 ucom_mutex_exit(sc); 2029 if (sc->sc_methods->ucom_read != NULL) { 2030 sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno, 2031 &cp, &cc); 2032 ub->ub_index = (u_int)(cp - ub->ub_data); 2033 } else 2034 ub->ub_index = 0; 2035 2036 ub->ub_len = cc; 2037 2038 rnd_add_uint32(&sc->sc_rndsource, cc); 2039 2040 ucom_cncheckmagic(sc, ub); 2041 ucom_mutex_enter(sc); 2042 /* 2043 * Quickly discard input if device is not open, 2044 * except when console is polling. 2045 */ 2046 if (sc->sc_state != UCOM_OPEN && !ucom_polling(sc)) { 2047 /* Go around again - we're not quite ready */ 2048 /* re-adds ub to sc_ibuff_empty */ 2049 ucomsubmitread(sc, ub); 2050 ucom_mutex_exit(sc); 2051 DPRINTF("... done (not open)", 0, 0, 0, 0); 2052 return; 2053 } 2054 2055 SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_full, ub, ub_link); 2056 2057 if (ucom_polling(sc)) 2058 ucom_cnread_complete(sc); 2059 else 2060 ucom_read_complete(sc); 2061 2062 ucom_mutex_exit(sc); 2063 2064 DPRINTF("... done", 0, 0, 0, 0); 2065 } 2066 2067 static void 2068 ucom_cleanup(struct ucom_softc *sc, int flag) 2069 { 2070 struct tty *tp = sc->sc_tty; 2071 2072 UCOMHIST_FUNC(); UCOMHIST_CALLED(); 2073 2074 DPRINTF("closing pipes", 0, 0, 0, 0); 2075 2076 /* 2077 * Close the tty and interrupt any pending opens waiting for 2078 * carrier so they restart or give up. This may flush data. 2079 */ 2080 (*tp->t_linesw->l_close)(tp, flag); 2081 ttyclose(tp); 2082 2083 /* 2084 * Interrupt any pending xfers and cause them to fail promptly. 2085 * New xfers will only be submitted under the lock after 2086 * sc_closing is cleared. 2087 * If console is attached, we keep the input pipe running 2088 * so that we can catch cnmagic and run DDB. 2089 */ 2090 if (sc->sc_cn == NULL) 2091 usbd_abort_pipe(sc->sc_bulkin_pipe); 2092 usbd_abort_pipe(sc->sc_bulkout_pipe); 2093 2094 /* 2095 * Hang up the phone and start the timer before we can make a 2096 * call again, if necessary. 2097 */ 2098 ucom_shutdown(sc); 2099 } 2100 2101 #endif /* NUCOM > 0 */ 2102 2103 static bool 2104 ucom_xfer_done(struct usbd_xfer *xfer) 2105 { 2106 usbd_status err; 2107 2108 usbd_get_xfer_status(xfer, NULL, NULL, NULL, &err); 2109 2110 return (err != USBD_NOT_STARTED && err != USBD_IN_PROGRESS); 2111 } 2112 2113 /* 2114 * Kernel console input, used when no user process is involved: 2115 * used by userconf prompt (boot -c), root device prompt (boot -a) 2116 * or DDB. This code path is not used in single user mode or by getty. 2117 * 2118 * ucom console specificities: userconf prompt (boot -c) cannot work 2119 * as USB devices are not yet attached. 2120 */ 2121 static int 2122 ucom_cngetc(dev_t dev) 2123 { 2124 const int unit = UCOMUNIT(dev); 2125 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit); 2126 struct ucom_cnstate * const cn = sc->sc_cn; 2127 int c = -1; /* If -1 we are called again in a loop at splhigh */ 2128 2129 if (cn->cn_cons_in == cn->cn_prod_in) { 2130 struct ucom_buffer *ub; 2131 2132 if ((ub = SIMPLEQ_FIRST(&sc->sc_ibuff_empty)) != NULL) { 2133 if (!ucom_xfer_done(ub->ub_xfer)) 2134 usbd_dopoll(sc->sc_iface); 2135 } else if ((ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full)) != NULL) { 2136 /* 2137 * This may happen if we entered DDB while 2138 * executing ucomreadcb(). We can start an 2139 * input transfer for DDB, but if we get there 2140 * because of a breakpoint, we are not likely 2141 * survive exitting DDB. 2142 */ 2143 SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_full, ub_link); 2144 ucomsubmitread(sc, ub); 2145 } else { 2146 panic("no ibuff anywhere"); 2147 } 2148 } 2149 2150 if (cn->cn_cons_in != cn->cn_prod_in) { 2151 c = cn->cn_buf_in[cn->cn_cons_in % sc->sc_ibufsize]; 2152 cn->cn_cons_in++; 2153 } 2154 2155 return c; 2156 } 2157 2158 /* 2159 * Console kernel output, used when no userland process is 2160 * involved. This is the "green" output on wscons default setup. 2161 * This code path is not used in single user mode, during 2162 * userland boot sequence, or by getty. 2163 * 2164 * When ucom_cnputc() is called from kprintf(), kprintf_lock is held. Take 2165 * advantage of this to serialiase the "producer" side of the ucom console 2166 * ring buffer. 2167 * 2168 * Add to the ring buffer if there is space and trigger a soft interrupt 2169 * handler at IPL_SOFTUSB to initiate USB transfers with the device. 2170 */ 2171 static void 2172 ucom_cnputc(dev_t dev, int out) 2173 { 2174 const int unit = UCOMUNIT(dev); 2175 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit); 2176 struct ucom_cnstate * const cn = sc->sc_cn; 2177 2178 u_int cnprodout = cn->cn_prod_out; 2179 u_int cnconsout = atomic_load_relaxed(&cn->cn_cons_out); 2180 size_t bufsz = cn->cn_buf_out_size; 2181 2182 /* 2183 * Check for cn->cn_buf_out rini buffer overflow. 2184 * New character is discared in that case. 2185 */ 2186 if (cnprodout - cnconsout < bufsz) { 2187 cn->cn_buf_out[cnprodout % bufsz] = (u_char)(out & 0xff); 2188 2189 /* 2190 * ensure the output character write completes before 2191 * incrementing cn->cn_prod_out 2192 */ 2193 atomic_store_release(&cn->cn_prod_out, cnprodout + 1); 2194 } 2195 2196 if (db_active) { 2197 int res; 2198 2199 /* 2200 * When DDB takes over, output is done synchronously 2201 * by ucom_cnput_bytes(). 2202 */ 2203 do { 2204 res = ucom_cnput_bytes(sc); 2205 } while (res != 0); 2206 } else { 2207 /* 2208 * ucom_cnput_bytes() call is deferred to soft 2209 * interrupt context in case current context does not 2210 * allow USB transfer to proceed. 2211 * 2212 * This must be done even if we detected overflow, because 2213 * in that case we still need the output to be flushed. 2214 */ 2215 kpreempt_disable(); 2216 softint_schedule(cn->cn_si); 2217 kpreempt_enable(); 2218 } 2219 } 2220 2221 /* 2222 * Toggle USB polling, where USB interrupt handler (hard and soft) 2223 * are called from ucom_cngetc() through usbd_dopoll(). 2224 * This is called by console driver before calling ucom_cngetc(). 2225 */ 2226 static void 2227 ucom_cnpollc(dev_t dev, int on) 2228 { 2229 const int unit = UCOMUNIT(dev); 2230 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit); 2231 struct ucom_cnstate * const cn = sc->sc_cn; 2232 struct usbd_device *udev = sc->sc_udev; 2233 2234 if (on) 2235 cn->cn_polling = true; 2236 2237 usbd_set_polling(udev, on); 2238 2239 if (on) { 2240 /* 2241 * Poll for regular output completion 2242 */ 2243 while(!db_active && atomic_load_relaxed(&cn->cn_sending) == 1) 2244 usbd_dopoll(sc->sc_iface); 2245 } else { 2246 cn->cn_polling = false; 2247 } 2248 } 2249 2250 static void 2251 ucom_cnfree(struct ucom_softc *sc) 2252 { 2253 struct ucom_cnstate * const cn = sc->sc_cn; 2254 2255 usbd_destroy_xfer(cn->cn_xfer_out); 2256 2257 callout_halt(&cn->cn_callout, NULL); 2258 callout_destroy(&cn->cn_callout); 2259 2260 softint_disestablish(cn->cn_si); 2261 2262 if (cn->cn_buf_in) 2263 kmem_free(cn->cn_buf_in, sc->sc_ibufsize); 2264 2265 if (cn->cn_buf_out) 2266 kmem_free(cn->cn_buf_out, cn->cn_buf_out_size); 2267 2268 if (cn->cn_linear_buf_out) 2269 kmem_free(cn->cn_linear_buf_out, sc->sc_obufsize); 2270 2271 kmem_free(cn, sizeof(*cn)); 2272 2273 sc->sc_cn = NULL; 2274 } 2275 2276 static void 2277 ucom_cnhalt(dev_t dev) 2278 { 2279 const int unit = UCOMUNIT(dev); 2280 struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit); 2281 struct ucom_cnstate * const cn = sc->sc_cn; 2282 2283 if (cn_tab != &ucom_cnops) 2284 return; 2285 2286 /* 2287 * Make sure we do not get any new output, and wait for 2288 * pending ones to complete 2289 */ 2290 cn_tab = NULL; 2291 while (atomic_load_relaxed(&cn->cn_sending) == 1) 2292 kpause("ucomhalt", true, mstohz(1), NULL); 2293 2294 cn_destroy_magic(&cn->cn_magic_state); 2295 2296 ucom_cnfree(sc); 2297 } 2298 2299 /* 2300 * Wrapper around cn_check_magic(); the call to console_debugger() is 2301 * deferred so that it happens in ucom_softintr(). 2302 */ 2303 static void 2304 ucom_cncheckmagic(struct ucom_softc *sc, struct ucom_buffer *ub) 2305 { 2306 struct ucom_cnstate *cn = sc->sc_cn; 2307 struct cnm_state *s = &cn->cn_magic_state; 2308 2309 if (cn == NULL || db_active) 2310 return; 2311 2312 for (size_t i = ub->ub_index; i < ub->ub_len; i++) { 2313 if (cn_check_magic_impl(ub->ub_data[i], s)) { 2314 cn->cn_magic_req = true; 2315 kpreempt_disable(); 2316 softint_schedule(sc->sc_si); 2317 kpreempt_enable(); 2318 break; 2319 } 2320 } 2321 } 2322 2323 int 2324 ucomprint(void *aux, const char *pnp) 2325 { 2326 struct ucom_attach_args *ucaa = aux; 2327 2328 if (pnp) 2329 aprint_normal("ucom at %s", pnp); 2330 if (ucaa->ucaa_portno != UCOM_UNK_PORTNO) 2331 aprint_normal(" portno %d", ucaa->ucaa_portno); 2332 return UNCONF; 2333 } 2334 2335 int 2336 ucomsubmatch(device_t parent, cfdata_t cf, 2337 const int *ldesc, void *aux) 2338 { 2339 struct ucom_attach_args *ucaa = aux; 2340 2341 if (ucaa->ucaa_portno != UCOM_UNK_PORTNO && 2342 cf->cf_loc[UCOMBUSCF_PORTNO] != UCOMBUSCF_PORTNO_DEFAULT && 2343 cf->cf_loc[UCOMBUSCF_PORTNO] != ucaa->ucaa_portno) 2344 return 0; 2345 return config_match(parent, cf, aux); 2346 } 2347