1 /* 2 * Copyright (c) 2003 Marcel Moolenaar 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/bus.h> 33 #include <sys/conf.h> 34 #include <sys/cons.h> 35 #include <sys/fcntl.h> 36 #include <sys/interrupt.h> 37 #include <sys/kernel.h> 38 #include <sys/malloc.h> 39 #include <sys/reboot.h> 40 #include <machine/bus.h> 41 #include <sys/rman.h> 42 #include <sys/termios.h> 43 #include <sys/tty.h> 44 #include <machine/resource.h> 45 #include <machine/stdarg.h> 46 47 #include <dev/uart/uart.h> 48 #include <dev/uart/uart_bus.h> 49 #include <dev/uart/uart_cpu.h> 50 51 #include "uart_if.h" 52 53 #define UART_MINOR_CALLOUT 0x10000 54 55 static cn_probe_t uart_cnprobe; 56 static cn_init_t uart_cninit; 57 static cn_term_t uart_cnterm; 58 static cn_getc_t uart_cngetc; 59 static cn_checkc_t uart_cncheckc; 60 static cn_putc_t uart_cnputc; 61 62 CONS_DRIVER(uart, uart_cnprobe, uart_cninit, uart_cnterm, uart_cngetc, 63 uart_cncheckc, uart_cnputc, NULL); 64 65 static d_open_t uart_tty_open; 66 static d_close_t uart_tty_close; 67 static d_ioctl_t uart_tty_ioctl; 68 69 static struct cdevsw uart_cdevsw = { 70 .d_open = uart_tty_open, 71 .d_close = uart_tty_close, 72 .d_read = ttyread, 73 .d_write = ttywrite, 74 .d_ioctl = uart_tty_ioctl, 75 .d_poll = ttypoll, 76 .d_name = uart_driver_name, 77 .d_flags = D_TTY, 78 .d_kqfilter = ttykqfilter, 79 }; 80 81 static struct uart_devinfo uart_console; 82 83 static void 84 uart_cnprobe(struct consdev *cp) 85 { 86 87 cp->cn_pri = CN_DEAD; 88 89 KASSERT(uart_console.cookie == NULL, ("foo")); 90 91 if (uart_cpu_getdev(UART_DEV_CONSOLE, &uart_console)) 92 return; 93 94 if (uart_probe(&uart_console)) 95 return; 96 97 cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL; 98 cp->cn_arg = &uart_console; 99 } 100 101 static void 102 uart_cninit(struct consdev *cp) 103 { 104 struct uart_devinfo *di; 105 106 /* 107 * Yedi trick: we need to be able to define cn_dev before we go 108 * single- or multi-user. The problem is that we don't know at 109 * this time what the device will be. Hence, we need to link from 110 * the uart_devinfo to the consdev that corresponds to it so that 111 * we can define cn_dev in uart_bus_attach() when we find the 112 * device during bus enumeration. That's when we'll know what the 113 * the unit number will be. 114 */ 115 di = cp->cn_arg; 116 KASSERT(di->cookie == NULL, ("foo")); 117 di->cookie = cp; 118 di->type = UART_DEV_CONSOLE; 119 uart_add_sysdev(di); 120 uart_init(di); 121 } 122 123 static void 124 uart_cnterm(struct consdev *cp) 125 { 126 127 uart_term(cp->cn_arg); 128 } 129 130 static void 131 uart_cnputc(struct consdev *cp, int c) 132 { 133 134 uart_putc(cp->cn_arg, c); 135 } 136 137 static int 138 uart_cncheckc(struct consdev *cp) 139 { 140 141 return (uart_poll(cp->cn_arg)); 142 } 143 144 static int 145 uart_cngetc(struct consdev *cp) 146 { 147 148 return (uart_getc(cp->cn_arg)); 149 } 150 151 static void 152 uart_tty_oproc(struct tty *tp) 153 { 154 struct uart_softc *sc; 155 156 KASSERT(tp->t_dev != NULL, ("foo")); 157 sc = tp->t_dev->si_drv1; 158 if (sc == NULL || sc->sc_leaving) 159 return; 160 161 /* 162 * Handle input flow control. Note that if we have hardware support, 163 * we don't do anything here. We continue to receive until our buffer 164 * is full. At that time we cannot empty the UART itself and it will 165 * de-assert RTS for us. In that situation we're completely stuffed. 166 * Without hardware support, we need to toggle RTS ourselves. 167 */ 168 if ((tp->t_cflag & CRTS_IFLOW) && !sc->sc_hwiflow) { 169 if ((tp->t_state & TS_TBLOCK) && 170 (sc->sc_hwsig & UART_SIG_RTS)) 171 UART_SETSIG(sc, UART_SIG_DRTS); 172 else if (!(tp->t_state & TS_TBLOCK) && 173 !(sc->sc_hwsig & UART_SIG_RTS)) 174 UART_SETSIG(sc, UART_SIG_DRTS|UART_SIG_RTS); 175 } 176 177 if (tp->t_state & TS_TTSTOP) 178 return; 179 180 if ((tp->t_state & TS_BUSY) || sc->sc_txbusy) 181 return; 182 183 if (tp->t_outq.c_cc == 0) { 184 ttwwakeup(tp); 185 return; 186 } 187 188 sc->sc_txdatasz = q_to_b(&tp->t_outq, sc->sc_txbuf, sc->sc_txfifosz); 189 tp->t_state |= TS_BUSY; 190 UART_TRANSMIT(sc); 191 ttwwakeup(tp); 192 } 193 194 static int 195 uart_tty_param(struct tty *tp, struct termios *t) 196 { 197 struct uart_softc *sc; 198 int databits, parity, stopbits; 199 200 KASSERT(tp->t_dev != NULL, ("foo")); 201 sc = tp->t_dev->si_drv1; 202 if (sc == NULL || sc->sc_leaving) 203 return (ENODEV); 204 if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0) 205 return (EINVAL); 206 /* Fixate certain parameters for system devices. */ 207 if (sc->sc_sysdev != NULL) { 208 t->c_ispeed = t->c_ospeed = sc->sc_sysdev->baudrate; 209 t->c_cflag |= CLOCAL; 210 t->c_cflag &= ~HUPCL; 211 } 212 if (t->c_ospeed == 0) { 213 UART_SETSIG(sc, UART_SIG_DDTR | UART_SIG_DRTS); 214 return (0); 215 } 216 switch (t->c_cflag & CSIZE) { 217 case CS5: databits = 5; break; 218 case CS6: databits = 6; break; 219 case CS7: databits = 7; break; 220 default: databits = 8; break; 221 } 222 stopbits = (t->c_cflag & CSTOPB) ? 2 : 1; 223 if (t->c_cflag & PARENB) 224 parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD 225 : UART_PARITY_EVEN; 226 else 227 parity = UART_PARITY_NONE; 228 UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity); 229 UART_SETSIG(sc, UART_SIG_DDTR | UART_SIG_DTR); 230 /* Set input flow control state. */ 231 if (!sc->sc_hwiflow) { 232 if ((t->c_cflag & CRTS_IFLOW) && (tp->t_state & TS_TBLOCK)) 233 UART_SETSIG(sc, UART_SIG_DRTS); 234 else 235 UART_SETSIG(sc, UART_SIG_DRTS | UART_SIG_RTS); 236 } else 237 UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW)); 238 /* Set output flow control state. */ 239 if (sc->sc_hwoflow) 240 UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW)); 241 ttsetwater(tp); 242 return (0); 243 } 244 245 static void 246 uart_tty_stop(struct tty *tp, int rw) 247 { 248 struct uart_softc *sc; 249 250 KASSERT(tp->t_dev != NULL, ("foo")); 251 sc = tp->t_dev->si_drv1; 252 if (sc == NULL || sc->sc_leaving) 253 return; 254 if (rw & FWRITE) { 255 if (sc->sc_txbusy) { 256 sc->sc_txbusy = 0; 257 UART_FLUSH(sc, UART_FLUSH_TRANSMITTER); 258 } 259 tp->t_state &= ~TS_BUSY; 260 } 261 if (rw & FREAD) { 262 UART_FLUSH(sc, UART_FLUSH_RECEIVER); 263 sc->sc_rxget = sc->sc_rxput = 0; 264 } 265 } 266 267 void 268 uart_tty_intr(void *arg) 269 { 270 struct uart_softc *sc = arg; 271 struct tty *tp; 272 int c, pend, sig, xc; 273 274 if (sc->sc_leaving) 275 return; 276 277 pend = atomic_readandclear_32(&sc->sc_ttypend); 278 if (!(pend & UART_IPEND_MASK)) 279 return; 280 281 tp = sc->sc_u.u_tty.tp; 282 283 if (pend & UART_IPEND_RXREADY) { 284 while (!uart_rx_empty(sc) && !(tp->t_state & TS_TBLOCK)) { 285 xc = uart_rx_get(sc); 286 c = xc & 0xff; 287 if (xc & UART_STAT_FRAMERR) 288 c |= TTY_FE; 289 if (xc & UART_STAT_PARERR) 290 c |= TTY_PE; 291 (*linesw[tp->t_line].l_rint)(c, tp); 292 } 293 } 294 295 if (pend & UART_IPEND_BREAK) { 296 if (tp != NULL && !(tp->t_iflag & IGNBRK)) 297 (*linesw[tp->t_line].l_rint)(0, tp); 298 } 299 300 if (pend & UART_IPEND_SIGCHG) { 301 sig = pend & UART_IPEND_SIGMASK; 302 if (sig & UART_SIG_DDCD) 303 (*linesw[tp->t_line].l_modem)(tp, sig & UART_SIG_DCD); 304 if ((sig & UART_SIG_DCTS) && (tp->t_cflag & CCTS_OFLOW) && 305 !sc->sc_hwoflow) { 306 if (sig & UART_SIG_CTS) { 307 tp->t_state &= ~TS_TTSTOP; 308 (*linesw[tp->t_line].l_start)(tp); 309 } else 310 tp->t_state |= TS_TTSTOP; 311 } 312 } 313 314 if (pend & UART_IPEND_TXIDLE) { 315 tp->t_state &= ~TS_BUSY; 316 (*linesw[tp->t_line].l_start)(tp); 317 } 318 } 319 320 int 321 uart_tty_attach(struct uart_softc *sc) 322 { 323 struct tty *tp; 324 325 tp = ttymalloc(NULL); 326 sc->sc_u.u_tty.tp = tp; 327 328 sc->sc_u.u_tty.si[0] = make_dev(&uart_cdevsw, 329 device_get_unit(sc->sc_dev), UID_ROOT, GID_WHEEL, 0600, "ttyu%r", 330 device_get_unit(sc->sc_dev)); 331 sc->sc_u.u_tty.si[0]->si_drv1 = sc; 332 sc->sc_u.u_tty.si[0]->si_tty = tp; 333 sc->sc_u.u_tty.si[1] = make_dev(&uart_cdevsw, 334 device_get_unit(sc->sc_dev) | UART_MINOR_CALLOUT, UID_UUCP, 335 GID_DIALER, 0660, "uart%r", device_get_unit(sc->sc_dev)); 336 sc->sc_u.u_tty.si[1]->si_drv1 = sc; 337 sc->sc_u.u_tty.si[1]->si_tty = tp; 338 339 tp->t_oproc = uart_tty_oproc; 340 tp->t_param = uart_tty_param; 341 tp->t_stop = uart_tty_stop; 342 343 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) { 344 sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name, 345 "ttyu%r", device_get_unit(sc->sc_dev)); 346 } 347 348 swi_add(&tty_ithd, uart_driver_name, uart_tty_intr, sc, SWI_TTY, 349 INTR_TYPE_TTY, &sc->sc_softih); 350 351 return (0); 352 } 353 354 int uart_tty_detach(struct uart_softc *sc) 355 { 356 357 ithread_remove_handler(sc->sc_softih); 358 destroy_dev(sc->sc_u.u_tty.si[0]); 359 destroy_dev(sc->sc_u.u_tty.si[1]); 360 /* ttyfree(sc->sc_u.u_tty.tp); */ 361 362 return (0); 363 } 364 365 static int 366 uart_tty_open(dev_t dev, int flags, int mode, struct thread *td) 367 { 368 struct uart_softc *sc; 369 struct tty *tp; 370 int error; 371 372 sc = dev->si_drv1; 373 if (sc == NULL || sc->sc_leaving) 374 return (ENODEV); 375 376 tp = dev->si_tty; 377 378 loop: 379 if (sc->sc_opened) { 380 KASSERT(tp->t_state & TS_ISOPEN, ("foo")); 381 /* 382 * The device is open, so everything has been initialized. 383 * Handle conflicts. 384 */ 385 if (minor(dev) & UART_MINOR_CALLOUT) { 386 if (!sc->sc_callout) 387 return (EBUSY); 388 } else { 389 if (sc->sc_callout) { 390 if (flags & O_NONBLOCK) 391 return (EBUSY); 392 error = tsleep(sc, TTIPRI|PCATCH, "uartbi", 0); 393 if (error) 394 return (error); 395 sc = dev->si_drv1; 396 if (sc == NULL || sc->sc_leaving) 397 return (ENODEV); 398 goto loop; 399 } 400 } 401 if (tp->t_state & TS_XCLUDE && suser(td) != 0) 402 return (EBUSY); 403 } else { 404 KASSERT(!(tp->t_state & TS_ISOPEN), ("foo")); 405 /* 406 * The device isn't open, so there are no conflicts. 407 * Initialize it. Initialization is done twice in many 408 * cases: to preempt sleeping callin opens if we are 409 * callout, and to complete a callin open after DCD rises. 410 */ 411 sc->sc_callout = (minor(dev) & UART_MINOR_CALLOUT) ? 1 : 0; 412 tp->t_dev = dev; 413 414 tp->t_cflag = TTYDEF_CFLAG; 415 tp->t_iflag = TTYDEF_IFLAG; 416 tp->t_lflag = TTYDEF_LFLAG; 417 tp->t_oflag = TTYDEF_OFLAG; 418 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 419 ttychars(tp); 420 error = uart_tty_param(tp, &tp->t_termios); 421 if (error) 422 return (error); 423 /* 424 * Handle initial DCD. 425 */ 426 if ((sc->sc_hwsig & UART_SIG_DCD) || sc->sc_callout) 427 (*linesw[tp->t_line].l_modem)(tp, 1); 428 } 429 /* 430 * Wait for DCD if necessary. 431 */ 432 if (!(tp->t_state & TS_CARR_ON) && !sc->sc_callout && 433 !(tp->t_cflag & CLOCAL) && !(flags & O_NONBLOCK)) { 434 error = tsleep(TSA_CARR_ON(tp), TTIPRI|PCATCH, "uartdcd", 0); 435 if (error) 436 return (error); 437 sc = dev->si_drv1; 438 if (sc == NULL || sc->sc_leaving) 439 return (ENODEV); 440 goto loop; 441 } 442 error = ttyopen(dev, tp); 443 if (error) 444 return (error); 445 error = (*linesw[tp->t_line].l_open)(dev, tp); 446 if (error) 447 return (error); 448 449 KASSERT(tp->t_state & TS_ISOPEN, ("foo")); 450 sc->sc_opened = 1; 451 return (0); 452 } 453 454 static int 455 uart_tty_close(dev_t dev, int flags, int mode, struct thread *td) 456 { 457 struct uart_softc *sc; 458 struct tty *tp; 459 460 sc = dev->si_drv1; 461 if (sc == NULL || sc->sc_leaving) 462 return (ENODEV); 463 tp = dev->si_tty; 464 if (!sc->sc_opened) { 465 KASSERT(!(tp->t_state & TS_ISOPEN), ("foo")); 466 return (0); 467 } 468 KASSERT(tp->t_state & TS_ISOPEN, ("foo")); 469 470 if (sc->sc_hwiflow) 471 UART_IOCTL(sc, UART_IOCTL_IFLOW, 0); 472 if (sc->sc_hwoflow) 473 UART_IOCTL(sc, UART_IOCTL_OFLOW, 0); 474 if (sc->sc_sysdev == NULL) 475 UART_SETSIG(sc, UART_SIG_DDTR | UART_SIG_DRTS); 476 477 /* Disable pulse capturing. */ 478 sc->sc_pps.ppsparam.mode = 0; 479 480 (*linesw[tp->t_line].l_close)(tp, flags); 481 ttyclose(tp); 482 wakeup(sc); 483 wakeup(TSA_CARR_ON(tp)); 484 KASSERT(!(tp->t_state & TS_ISOPEN), ("foo")); 485 sc->sc_opened = 0; 486 return (0); 487 } 488 489 static int 490 uart_tty_ioctl(dev_t dev, u_long cmd, caddr_t data, int flags, 491 struct thread *td) 492 { 493 struct uart_softc *sc; 494 struct tty *tp; 495 int bits, error, sig; 496 497 sc = dev->si_drv1; 498 if (sc == NULL || sc->sc_leaving) 499 return (ENODEV); 500 501 tp = dev->si_tty; 502 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flags, td); 503 if (error != ENOIOCTL) 504 return (error); 505 error = ttioctl(tp, cmd, data, flags); 506 if (error != ENOIOCTL) 507 return (error); 508 509 error = 0; 510 switch (cmd) { 511 case TIOCSBRK: 512 UART_IOCTL(sc, UART_IOCTL_BREAK, 1); 513 break; 514 case TIOCCBRK: 515 UART_IOCTL(sc, UART_IOCTL_BREAK, 0); 516 break; 517 case TIOCSDTR: 518 UART_SETSIG(sc, UART_SIG_DDTR | UART_SIG_DTR); 519 break; 520 case TIOCCDTR: 521 UART_SETSIG(sc, UART_SIG_DDTR); 522 break; 523 case TIOCMSET: 524 bits = *(int*)data; 525 sig = UART_SIG_DDTR | UART_SIG_DRTS; 526 if (bits & TIOCM_DTR) 527 sig |= UART_SIG_DTR; 528 if (bits & TIOCM_RTS) 529 sig |= UART_SIG_RTS; 530 UART_SETSIG(sc, sig); 531 break; 532 case TIOCMBIS: 533 bits = *(int*)data; 534 sig = 0; 535 if (bits & TIOCM_DTR) 536 sig |= UART_SIG_DDTR | UART_SIG_DTR; 537 if (bits & TIOCM_RTS) 538 sig |= UART_SIG_DRTS | UART_SIG_RTS; 539 UART_SETSIG(sc, sig); 540 break; 541 case TIOCMBIC: 542 bits = *(int*)data; 543 sig = 0; 544 if (bits & TIOCM_DTR) 545 sig |= UART_SIG_DDTR; 546 if (bits & TIOCM_RTS) 547 sig |= UART_SIG_DRTS; 548 UART_SETSIG(sc, sig); 549 break; 550 case TIOCMGET: 551 sig = sc->sc_hwsig; 552 bits = TIOCM_LE; 553 if (sig & UART_SIG_DTR) 554 bits |= TIOCM_DTR; 555 if (sig & UART_SIG_RTS) 556 bits |= TIOCM_RTS; 557 if (sig & UART_SIG_DSR) 558 bits |= TIOCM_DSR; 559 if (sig & UART_SIG_CTS) 560 bits |= TIOCM_CTS; 561 if (sig & UART_SIG_DCD) 562 bits |= TIOCM_CD; 563 if (sig & (UART_SIG_DRI | UART_SIG_RI)) 564 bits |= TIOCM_RI; 565 *(int*)data = bits; 566 break; 567 default: 568 error = pps_ioctl(cmd, data, &sc->sc_pps); 569 if (error == ENODEV) 570 error = ENOTTY; 571 break; 572 } 573 return (error); 574 } 575