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_version = D_VERSION, 71 .d_open = uart_tty_open, 72 .d_close = uart_tty_close, 73 .d_ioctl = uart_tty_ioctl, 74 .d_name = uart_driver_name, 75 .d_flags = D_TTY | D_NEEDGIANT, 76 }; 77 78 static struct uart_devinfo uart_console; 79 80 static void 81 uart_cnprobe(struct consdev *cp) 82 { 83 84 cp->cn_pri = CN_DEAD; 85 86 KASSERT(uart_console.cookie == NULL, ("foo")); 87 88 if (uart_cpu_getdev(UART_DEV_CONSOLE, &uart_console)) 89 return; 90 91 if (uart_probe(&uart_console)) 92 return; 93 94 cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL; 95 cp->cn_arg = &uart_console; 96 } 97 98 static void 99 uart_cninit(struct consdev *cp) 100 { 101 struct uart_devinfo *di; 102 103 /* 104 * Yedi trick: we need to be able to define cn_dev before we go 105 * single- or multi-user. The problem is that we don't know at 106 * this time what the device will be. Hence, we need to link from 107 * the uart_devinfo to the consdev that corresponds to it so that 108 * we can define cn_dev in uart_bus_attach() when we find the 109 * device during bus enumeration. That's when we'll know what the 110 * the unit number will be. 111 */ 112 di = cp->cn_arg; 113 KASSERT(di->cookie == NULL, ("foo")); 114 di->cookie = cp; 115 di->type = UART_DEV_CONSOLE; 116 uart_add_sysdev(di); 117 uart_init(di); 118 } 119 120 static void 121 uart_cnterm(struct consdev *cp) 122 { 123 124 uart_term(cp->cn_arg); 125 } 126 127 static void 128 uart_cnputc(struct consdev *cp, int c) 129 { 130 131 uart_putc(cp->cn_arg, c); 132 } 133 134 static int 135 uart_cncheckc(struct consdev *cp) 136 { 137 138 return (uart_poll(cp->cn_arg)); 139 } 140 141 static int 142 uart_cngetc(struct consdev *cp) 143 { 144 145 return (uart_getc(cp->cn_arg)); 146 } 147 148 static void 149 uart_tty_oproc(struct tty *tp) 150 { 151 struct uart_softc *sc; 152 153 KASSERT(tp->t_dev != NULL, ("foo")); 154 sc = tp->t_dev->si_drv1; 155 if (sc == NULL || sc->sc_leaving) 156 return; 157 158 /* 159 * Handle input flow control. Note that if we have hardware support, 160 * we don't do anything here. We continue to receive until our buffer 161 * is full. At that time we cannot empty the UART itself and it will 162 * de-assert RTS for us. In that situation we're completely stuffed. 163 * Without hardware support, we need to toggle RTS ourselves. 164 */ 165 if ((tp->t_cflag & CRTS_IFLOW) && !sc->sc_hwiflow) { 166 if ((tp->t_state & TS_TBLOCK) && 167 (sc->sc_hwsig & SER_RTS)) 168 UART_SETSIG(sc, SER_DRTS); 169 else if (!(tp->t_state & TS_TBLOCK) && 170 !(sc->sc_hwsig & SER_RTS)) 171 UART_SETSIG(sc, SER_DRTS|SER_RTS); 172 } 173 174 if (tp->t_state & TS_TTSTOP) 175 return; 176 177 if ((tp->t_state & TS_BUSY) || sc->sc_txbusy) 178 return; 179 180 if (tp->t_outq.c_cc == 0) { 181 ttwwakeup(tp); 182 return; 183 } 184 185 sc->sc_txdatasz = q_to_b(&tp->t_outq, sc->sc_txbuf, sc->sc_txfifosz); 186 tp->t_state |= TS_BUSY; 187 UART_TRANSMIT(sc); 188 ttwwakeup(tp); 189 } 190 191 static int 192 uart_tty_param(struct tty *tp, struct termios *t) 193 { 194 struct uart_softc *sc; 195 int databits, parity, stopbits; 196 197 KASSERT(tp->t_dev != NULL, ("foo")); 198 sc = tp->t_dev->si_drv1; 199 if (sc == NULL || sc->sc_leaving) 200 return (ENODEV); 201 if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0) 202 return (EINVAL); 203 /* Fixate certain parameters for system devices. */ 204 if (sc->sc_sysdev != NULL) { 205 t->c_ispeed = t->c_ospeed = sc->sc_sysdev->baudrate; 206 t->c_cflag |= CLOCAL; 207 t->c_cflag &= ~HUPCL; 208 } 209 if (t->c_ospeed == 0) { 210 UART_SETSIG(sc, SER_DDTR | SER_DRTS); 211 return (0); 212 } 213 switch (t->c_cflag & CSIZE) { 214 case CS5: databits = 5; break; 215 case CS6: databits = 6; break; 216 case CS7: databits = 7; break; 217 default: databits = 8; break; 218 } 219 stopbits = (t->c_cflag & CSTOPB) ? 2 : 1; 220 if (t->c_cflag & PARENB) 221 parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD 222 : UART_PARITY_EVEN; 223 else 224 parity = UART_PARITY_NONE; 225 if (UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity) != 0) 226 return (EINVAL); 227 UART_SETSIG(sc, SER_DDTR | SER_DTR); 228 /* Set input flow control state. */ 229 if (!sc->sc_hwiflow) { 230 if ((t->c_cflag & CRTS_IFLOW) && (tp->t_state & TS_TBLOCK)) 231 UART_SETSIG(sc, SER_DRTS); 232 else 233 UART_SETSIG(sc, SER_DRTS | SER_RTS); 234 } else 235 UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW)); 236 /* Set output flow control state. */ 237 if (sc->sc_hwoflow) 238 UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW)); 239 ttsetwater(tp); 240 return (0); 241 } 242 243 static int 244 uart_tty_modem(struct tty *tp, int biton, int bitoff) 245 { 246 struct uart_softc *sc; 247 248 sc = tp->t_dev->si_drv1; 249 if (biton != 0 || bitoff != 0) 250 UART_SETSIG(sc, SER_DELTA(bitoff|biton) | biton); 251 return (sc->sc_hwsig); 252 } 253 254 static void 255 uart_tty_break(struct tty *tp, int state) 256 { 257 struct uart_softc *sc; 258 259 sc = tp->t_dev->si_drv1; 260 UART_IOCTL(sc, UART_IOCTL_BREAK, state); 261 } 262 263 static void 264 uart_tty_stop(struct tty *tp, int rw) 265 { 266 struct uart_softc *sc; 267 268 KASSERT(tp->t_dev != NULL, ("foo")); 269 sc = tp->t_dev->si_drv1; 270 if (sc == NULL || sc->sc_leaving) 271 return; 272 if (rw & FWRITE) { 273 if (sc->sc_txbusy) { 274 sc->sc_txbusy = 0; 275 UART_FLUSH(sc, UART_FLUSH_TRANSMITTER); 276 } 277 tp->t_state &= ~TS_BUSY; 278 } 279 if (rw & FREAD) { 280 UART_FLUSH(sc, UART_FLUSH_RECEIVER); 281 sc->sc_rxget = sc->sc_rxput = 0; 282 } 283 } 284 285 void 286 uart_tty_intr(void *arg) 287 { 288 struct uart_softc *sc = arg; 289 struct tty *tp; 290 int c, pend, sig, xc; 291 292 if (sc->sc_leaving) 293 return; 294 295 pend = atomic_readandclear_32(&sc->sc_ttypend); 296 if (!(pend & UART_IPEND_MASK)) 297 return; 298 299 tp = sc->sc_u.u_tty.tp; 300 301 if (pend & UART_IPEND_RXREADY) { 302 while (!uart_rx_empty(sc) && !(tp->t_state & TS_TBLOCK)) { 303 xc = uart_rx_get(sc); 304 c = xc & 0xff; 305 if (xc & UART_STAT_FRAMERR) 306 c |= TTY_FE; 307 if (xc & UART_STAT_PARERR) 308 c |= TTY_PE; 309 ttyld_rint(tp, c); 310 } 311 } 312 313 if (pend & UART_IPEND_BREAK) { 314 if (tp != NULL && !(tp->t_iflag & IGNBRK)) 315 ttyld_rint(tp, 0); 316 } 317 318 if (pend & UART_IPEND_SIGCHG) { 319 sig = pend & UART_IPEND_SIGMASK; 320 if (sig & SER_DDCD) 321 ttyld_modem(tp, sig & SER_DCD); 322 if ((sig & SER_DCTS) && (tp->t_cflag & CCTS_OFLOW) && 323 !sc->sc_hwoflow) { 324 if (sig & SER_CTS) { 325 tp->t_state &= ~TS_TTSTOP; 326 ttyld_start(tp); 327 } else 328 tp->t_state |= TS_TTSTOP; 329 } 330 } 331 332 if (pend & UART_IPEND_TXIDLE) { 333 tp->t_state &= ~TS_BUSY; 334 ttyld_start(tp); 335 } 336 } 337 338 int 339 uart_tty_attach(struct uart_softc *sc) 340 { 341 struct tty *tp; 342 343 tp = ttymalloc(NULL); 344 sc->sc_u.u_tty.tp = tp; 345 346 sc->sc_u.u_tty.si[0] = make_dev(&uart_cdevsw, 347 device_get_unit(sc->sc_dev), UID_ROOT, GID_WHEEL, 0600, "ttyu%r", 348 device_get_unit(sc->sc_dev)); 349 sc->sc_u.u_tty.si[0]->si_drv1 = sc; 350 sc->sc_u.u_tty.si[0]->si_tty = tp; 351 sc->sc_u.u_tty.si[1] = make_dev(&uart_cdevsw, 352 device_get_unit(sc->sc_dev) | UART_MINOR_CALLOUT, UID_UUCP, 353 GID_DIALER, 0660, "uart%r", device_get_unit(sc->sc_dev)); 354 sc->sc_u.u_tty.si[1]->si_drv1 = sc; 355 sc->sc_u.u_tty.si[1]->si_tty = tp; 356 357 tp->t_oproc = uart_tty_oproc; 358 tp->t_param = uart_tty_param; 359 tp->t_stop = uart_tty_stop; 360 tp->t_modem = uart_tty_modem; 361 tp->t_break = uart_tty_break; 362 363 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) { 364 sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name, 365 "ttyu%r", device_get_unit(sc->sc_dev)); 366 } 367 368 swi_add(&tty_ithd, uart_driver_name, uart_tty_intr, sc, SWI_TTY, 369 INTR_TYPE_TTY, &sc->sc_softih); 370 371 return (0); 372 } 373 374 int uart_tty_detach(struct uart_softc *sc) 375 { 376 377 ithread_remove_handler(sc->sc_softih); 378 destroy_dev(sc->sc_u.u_tty.si[0]); 379 destroy_dev(sc->sc_u.u_tty.si[1]); 380 /* ttyfree(sc->sc_u.u_tty.tp); */ 381 382 return (0); 383 } 384 385 static int 386 uart_tty_open(struct cdev *dev, int flags, int mode, struct thread *td) 387 { 388 struct uart_softc *sc; 389 struct tty *tp; 390 int error; 391 392 loop: 393 sc = dev->si_drv1; 394 if (sc == NULL || sc->sc_leaving) 395 return (ENODEV); 396 397 tp = dev->si_tty; 398 399 if (sc->sc_opened) { 400 KASSERT(tp->t_state & TS_ISOPEN, ("foo")); 401 /* 402 * The device is open, so everything has been initialized. 403 * Handle conflicts. 404 */ 405 if (minor(dev) & UART_MINOR_CALLOUT) { 406 if (!sc->sc_callout) 407 return (EBUSY); 408 } else { 409 if (sc->sc_callout) { 410 if (flags & O_NONBLOCK) 411 return (EBUSY); 412 error = tsleep(sc, TTIPRI|PCATCH, "uartbi", 0); 413 if (error) 414 return (error); 415 goto loop; 416 } 417 } 418 if (tp->t_state & TS_XCLUDE && suser(td) != 0) 419 return (EBUSY); 420 } else { 421 KASSERT(!(tp->t_state & TS_ISOPEN), ("foo")); 422 /* 423 * The device isn't open, so there are no conflicts. 424 * Initialize it. Initialization is done twice in many 425 * cases: to preempt sleeping callin opens if we are 426 * callout, and to complete a callin open after DCD rises. 427 */ 428 sc->sc_callout = (minor(dev) & UART_MINOR_CALLOUT) ? 1 : 0; 429 tp->t_dev = dev; 430 431 tp->t_cflag = TTYDEF_CFLAG; 432 tp->t_iflag = TTYDEF_IFLAG; 433 tp->t_lflag = TTYDEF_LFLAG; 434 tp->t_oflag = TTYDEF_OFLAG; 435 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 436 ttychars(tp); 437 error = uart_tty_param(tp, &tp->t_termios); 438 if (error) 439 return (error); 440 /* 441 * Handle initial DCD. 442 */ 443 if ((sc->sc_hwsig & SER_DCD) || sc->sc_callout) 444 ttyld_modem(tp, 1); 445 } 446 /* 447 * Wait for DCD if necessary. 448 */ 449 if (!(tp->t_state & TS_CARR_ON) && !sc->sc_callout && 450 !(tp->t_cflag & CLOCAL) && !(flags & O_NONBLOCK)) { 451 error = tsleep(TSA_CARR_ON(tp), TTIPRI|PCATCH, "uartdcd", 0); 452 if (error) 453 return (error); 454 goto loop; 455 } 456 error = tty_open(dev, tp); 457 if (error) 458 return (error); 459 error = ttyld_open(tp, dev); 460 if (error) 461 return (error); 462 463 KASSERT(tp->t_state & TS_ISOPEN, ("foo")); 464 sc->sc_opened = 1; 465 return (0); 466 } 467 468 static int 469 uart_tty_close(struct cdev *dev, int flags, int mode, struct thread *td) 470 { 471 struct uart_softc *sc; 472 struct tty *tp; 473 474 sc = dev->si_drv1; 475 if (sc == NULL || sc->sc_leaving) 476 return (ENODEV); 477 tp = dev->si_tty; 478 if (!sc->sc_opened) { 479 KASSERT(!(tp->t_state & TS_ISOPEN), ("foo")); 480 return (0); 481 } 482 KASSERT(tp->t_state & TS_ISOPEN, ("foo")); 483 484 if (sc->sc_hwiflow) 485 UART_IOCTL(sc, UART_IOCTL_IFLOW, 0); 486 if (sc->sc_hwoflow) 487 UART_IOCTL(sc, UART_IOCTL_OFLOW, 0); 488 if (sc->sc_sysdev == NULL) 489 UART_SETSIG(sc, SER_DDTR | SER_DRTS); 490 491 /* Disable pulse capturing. */ 492 sc->sc_pps.ppsparam.mode = 0; 493 494 ttyld_close(tp, flags); 495 tty_close(tp); 496 wakeup(sc); 497 wakeup(TSA_CARR_ON(tp)); 498 KASSERT(!(tp->t_state & TS_ISOPEN), ("foo")); 499 sc->sc_opened = 0; 500 return (0); 501 } 502 503 static int 504 uart_tty_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, 505 struct thread *td) 506 { 507 struct uart_softc *sc; 508 struct tty *tp; 509 int error; 510 511 sc = dev->si_drv1; 512 if (sc == NULL || sc->sc_leaving) 513 return (ENODEV); 514 515 tp = dev->si_tty; 516 error = ttyioctl(dev, cmd, data, flags, td); 517 if (error != ENOTTY) 518 return (error); 519 520 error = pps_ioctl(cmd, data, &sc->sc_pps); 521 if (error == ENODEV) 522 error = ENOTTY; 523 return (error); 524 } 525