sio.c revision 119419
1/*- 2 * Copyright (c) 1991 The Regents of the University of California. 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 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * from: @(#)com.c 7.5 (Berkeley) 5/16/91 34 * from: i386/isa sio.c,v 1.234 35 */ 36 37#include <sys/cdefs.h> 38__FBSDID("$FreeBSD: head/sys/dev/sio/sio.c 119419 2003-08-24 18:03:45Z obrien $"); 39 40#include "opt_comconsole.h" 41#include "opt_compat.h" 42#include "opt_ddb.h" 43#include "opt_sio.h" 44 45/* 46 * Serial driver, based on 386BSD-0.1 com driver. 47 * Mostly rewritten to use pseudo-DMA. 48 * Works for National Semiconductor NS8250-NS16550AF UARTs. 49 * COM driver, based on HP dca driver. 50 * 51 * Changes for PC-Card integration: 52 * - Added PC-Card driver table and handlers 53 */ 54#include <sys/param.h> 55#include <sys/systm.h> 56#include <sys/bus.h> 57#include <sys/conf.h> 58#include <sys/fcntl.h> 59#include <sys/interrupt.h> 60#include <sys/kernel.h> 61#include <sys/limits.h> 62#include <sys/lock.h> 63#include <sys/malloc.h> 64#include <sys/module.h> 65#include <sys/mutex.h> 66#include <sys/proc.h> 67#include <sys/reboot.h> 68#include <sys/sysctl.h> 69#include <sys/syslog.h> 70#include <sys/tty.h> 71#include <machine/bus_pio.h> 72#include <machine/bus.h> 73#include <sys/rman.h> 74#include <sys/timepps.h> 75#include <sys/uio.h> 76 77#include <isa/isavar.h> 78 79#include <machine/resource.h> 80 81#include <dev/sio/sioreg.h> 82#include <dev/sio/siovar.h> 83 84#ifdef COM_ESP 85#include <dev/ic/esp.h> 86#endif 87#include <dev/ic/ns16550.h> 88 89#define LOTS_OF_EVENTS 64 /* helps separate urgent events from input */ 90 91#define CALLOUT_MASK 0x80 92#define CONTROL_MASK 0x60 93#define CONTROL_INIT_STATE 0x20 94#define CONTROL_LOCK_STATE 0x40 95#define DEV_TO_UNIT(dev) (MINOR_TO_UNIT(minor(dev))) 96#define MINOR_TO_UNIT(mynor) ((((mynor) & ~0xffffU) >> (8 + 3)) \ 97 | ((mynor) & 0x1f)) 98#define UNIT_TO_MINOR(unit) ((((unit) & ~0x1fU) << (8 + 3)) \ 99 | ((unit) & 0x1f)) 100 101#ifdef COM_MULTIPORT 102/* checks in flags for multiport and which is multiport "master chip" 103 * for a given card 104 */ 105#define COM_ISMULTIPORT(flags) ((flags) & 0x01) 106#define COM_MPMASTER(flags) (((flags) >> 8) & 0x0ff) 107#define COM_NOTAST4(flags) ((flags) & 0x04) 108#else 109#define COM_ISMULTIPORT(flags) (0) 110#endif /* COM_MULTIPORT */ 111 112#define COM_CONSOLE(flags) ((flags) & 0x10) 113#define COM_FORCECONSOLE(flags) ((flags) & 0x20) 114#define COM_LLCONSOLE(flags) ((flags) & 0x40) 115#define COM_DEBUGGER(flags) ((flags) & 0x80) 116#define COM_LOSESOUTINTS(flags) ((flags) & 0x08) 117#define COM_NOFIFO(flags) ((flags) & 0x02) 118#define COM_PPSCTS(flags) ((flags) & 0x10000) 119#define COM_ST16650A(flags) ((flags) & 0x20000) 120#define COM_C_NOPROBE (0x40000) 121#define COM_NOPROBE(flags) ((flags) & COM_C_NOPROBE) 122#define COM_C_IIR_TXRDYBUG (0x80000) 123#define COM_IIR_TXRDYBUG(flags) ((flags) & COM_C_IIR_TXRDYBUG) 124#define COM_NOSCR(flags) ((flags) & 0x100000) 125#define COM_TI16754(flags) ((flags) & 0x200000) 126#define COM_FIFOSIZE(flags) (((flags) & 0xff000000) >> 24) 127 128#define sio_getreg(com, off) \ 129 (bus_space_read_1((com)->bst, (com)->bsh, (off))) 130#define sio_setreg(com, off, value) \ 131 (bus_space_write_1((com)->bst, (com)->bsh, (off), (value))) 132 133/* 134 * com state bits. 135 * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher 136 * than the other bits so that they can be tested as a group without masking 137 * off the low bits. 138 * 139 * The following com and tty flags correspond closely: 140 * CS_BUSY = TS_BUSY (maintained by comstart(), siopoll() and 141 * comstop()) 142 * CS_TTGO = ~TS_TTSTOP (maintained by comparam() and comstart()) 143 * CS_CTS_OFLOW = CCTS_OFLOW (maintained by comparam()) 144 * CS_RTS_IFLOW = CRTS_IFLOW (maintained by comparam()) 145 * TS_FLUSH is not used. 146 * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON. 147 * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state). 148 */ 149#define CS_BUSY 0x80 /* output in progress */ 150#define CS_TTGO 0x40 /* output not stopped by XOFF */ 151#define CS_ODEVREADY 0x20 /* external device h/w ready (CTS) */ 152#define CS_CHECKMSR 1 /* check of MSR scheduled */ 153#define CS_CTS_OFLOW 2 /* use CTS output flow control */ 154#define CS_DTR_OFF 0x10 /* DTR held off */ 155#define CS_ODONE 4 /* output completed */ 156#define CS_RTS_IFLOW 8 /* use RTS input flow control */ 157#define CSE_BUSYCHECK 1 /* siobusycheck() scheduled */ 158 159static char const * const error_desc[] = { 160#define CE_OVERRUN 0 161 "silo overflow", 162#define CE_INTERRUPT_BUF_OVERFLOW 1 163 "interrupt-level buffer overflow", 164#define CE_TTY_BUF_OVERFLOW 2 165 "tty-level buffer overflow", 166}; 167 168#define CE_NTYPES 3 169#define CE_RECORD(com, errnum) (++(com)->delta_error_counts[errnum]) 170 171/* types. XXX - should be elsewhere */ 172typedef u_int Port_t; /* hardware port */ 173typedef u_char bool_t; /* boolean */ 174 175/* queue of linear buffers */ 176struct lbq { 177 u_char *l_head; /* next char to process */ 178 u_char *l_tail; /* one past the last char to process */ 179 struct lbq *l_next; /* next in queue */ 180 bool_t l_queued; /* nonzero if queued */ 181}; 182 183/* com device structure */ 184struct com_s { 185 u_int flags; /* Copy isa device flags */ 186 u_char state; /* miscellaneous flag bits */ 187 bool_t active_out; /* nonzero if the callout device is open */ 188 u_char cfcr_image; /* copy of value written to CFCR */ 189#ifdef COM_ESP 190 bool_t esp; /* is this unit a hayes esp board? */ 191#endif 192 u_char extra_state; /* more flag bits, separate for order trick */ 193 u_char fifo_image; /* copy of value written to FIFO */ 194 bool_t hasfifo; /* nonzero for 16550 UARTs */ 195 bool_t st16650a; /* Is a Startech 16650A or RTS/CTS compat */ 196 bool_t loses_outints; /* nonzero if device loses output interrupts */ 197 u_char mcr_image; /* copy of value written to MCR */ 198#ifdef COM_MULTIPORT 199 bool_t multiport; /* is this unit part of a multiport device? */ 200#endif /* COM_MULTIPORT */ 201 bool_t no_irq; /* nonzero if irq is not attached */ 202 bool_t gone; /* hardware disappeared */ 203 bool_t poll; /* nonzero if polling is required */ 204 bool_t poll_output; /* nonzero if polling for output is required */ 205 int unit; /* unit number */ 206 int dtr_wait; /* time to hold DTR down on close (* 1/hz) */ 207 u_int tx_fifo_size; 208 u_int wopeners; /* # processes waiting for DCD in open() */ 209 210 /* 211 * The high level of the driver never reads status registers directly 212 * because there would be too many side effects to handle conveniently. 213 * Instead, it reads copies of the registers stored here by the 214 * interrupt handler. 215 */ 216 u_char last_modem_status; /* last MSR read by intr handler */ 217 u_char prev_modem_status; /* last MSR handled by high level */ 218 219 u_char hotchar; /* ldisc-specific char to be handled ASAP */ 220 u_char *ibuf; /* start of input buffer */ 221 u_char *ibufend; /* end of input buffer */ 222 u_char *ibufold; /* old input buffer, to be freed */ 223 u_char *ihighwater; /* threshold in input buffer */ 224 u_char *iptr; /* next free spot in input buffer */ 225 int ibufsize; /* size of ibuf (not include error bytes) */ 226 int ierroff; /* offset of error bytes in ibuf */ 227 228 struct lbq obufq; /* head of queue of output buffers */ 229 struct lbq obufs[2]; /* output buffers */ 230 231 bus_space_tag_t bst; 232 bus_space_handle_t bsh; 233 234 Port_t data_port; /* i/o ports */ 235#ifdef COM_ESP 236 Port_t esp_port; 237#endif 238 Port_t int_id_port; 239 Port_t modem_ctl_port; 240 Port_t line_status_port; 241 Port_t modem_status_port; 242 Port_t intr_ctl_port; /* Ports of IIR register */ 243 244 struct tty *tp; /* cross reference */ 245 246 /* Initial state. */ 247 struct termios it_in; /* should be in struct tty */ 248 struct termios it_out; 249 250 /* Lock state. */ 251 struct termios lt_in; /* should be in struct tty */ 252 struct termios lt_out; 253 254 bool_t do_timestamp; 255 bool_t do_dcd_timestamp; 256 struct timeval timestamp; 257 struct timeval dcd_timestamp; 258 struct pps_state pps; 259 int pps_bit; 260 261 u_long bytes_in; /* statistics */ 262 u_long bytes_out; 263 u_int delta_error_counts[CE_NTYPES]; 264 u_long error_counts[CE_NTYPES]; 265 266 u_long rclk; 267 268 struct resource *irqres; 269 struct resource *ioportres; 270 int ioportrid; 271 void *cookie; 272 dev_t devs[6]; 273 274 /* 275 * Data area for output buffers. Someday we should build the output 276 * buffer queue without copying data. 277 */ 278 u_char obuf1[256]; 279 u_char obuf2[256]; 280}; 281 282#ifdef COM_ESP 283static int espattach(struct com_s *com, Port_t esp_port); 284#endif 285 286static timeout_t siobusycheck; 287static u_int siodivisor(u_long rclk, speed_t speed); 288static timeout_t siodtrwakeup; 289static void comhardclose(struct com_s *com); 290static void sioinput(struct com_s *com); 291static void siointr1(struct com_s *com); 292static void siointr(void *arg); 293static int commctl(struct com_s *com, int bits, int how); 294static int comparam(struct tty *tp, struct termios *t); 295static void siopoll(void *); 296static void siosettimeout(void); 297static int siosetwater(struct com_s *com, speed_t speed); 298static void comstart(struct tty *tp); 299static void comstop(struct tty *tp, int rw); 300static timeout_t comwakeup; 301static void disc_optim(struct tty *tp, struct termios *t, 302 struct com_s *com); 303 304char sio_driver_name[] = "sio"; 305static struct mtx sio_lock; 306static int sio_inited; 307 308/* table and macro for fast conversion from a unit number to its com struct */ 309devclass_t sio_devclass; 310#define com_addr(unit) ((struct com_s *) \ 311 devclass_get_softc(sio_devclass, unit)) /* XXX */ 312 313static d_open_t sioopen; 314static d_close_t sioclose; 315static d_read_t sioread; 316static d_write_t siowrite; 317static d_ioctl_t sioioctl; 318 319#define CDEV_MAJOR 28 320static struct cdevsw sio_cdevsw = { 321 .d_open = sioopen, 322 .d_close = sioclose, 323 .d_read = sioread, 324 .d_write = siowrite, 325 .d_ioctl = sioioctl, 326 .d_poll = ttypoll, 327 .d_name = sio_driver_name, 328 .d_maj = CDEV_MAJOR, 329 .d_flags = D_TTY, 330 .d_kqfilter = ttykqfilter, 331}; 332 333int comconsole = -1; 334static volatile speed_t comdefaultrate = CONSPEED; 335static u_long comdefaultrclk = DEFAULT_RCLK; 336SYSCTL_ULONG(_machdep, OID_AUTO, conrclk, CTLFLAG_RW, &comdefaultrclk, 0, ""); 337static speed_t gdbdefaultrate = GDBSPEED; 338SYSCTL_UINT(_machdep, OID_AUTO, gdbspeed, CTLFLAG_RW, 339 &gdbdefaultrate, GDBSPEED, ""); 340static u_int com_events; /* input chars + weighted output completions */ 341static Port_t siocniobase; 342static int siocnunit = -1; 343static Port_t siogdbiobase; 344static int siogdbunit = -1; 345static void *sio_slow_ih; 346static void *sio_fast_ih; 347static int sio_timeout; 348static int sio_timeouts_until_log; 349static struct callout_handle sio_timeout_handle 350 = CALLOUT_HANDLE_INITIALIZER(&sio_timeout_handle); 351static int sio_numunits; 352 353#ifdef COM_ESP 354/* XXX configure this properly. */ 355/* XXX quite broken for new-bus. */ 356static Port_t likely_com_ports[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, }; 357static Port_t likely_esp_ports[] = { 0x140, 0x180, 0x280, 0 }; 358#endif 359 360/* 361 * handle sysctl read/write requests for console speed 362 * 363 * In addition to setting comdefaultrate for I/O through /dev/console, 364 * also set the initial and lock values for the /dev/ttyXX device 365 * if there is one associated with the console. Finally, if the /dev/tty 366 * device has already been open, change the speed on the open running port 367 * itself. 368 */ 369 370static int 371sysctl_machdep_comdefaultrate(SYSCTL_HANDLER_ARGS) 372{ 373 int error, s; 374 speed_t newspeed; 375 struct com_s *com; 376 struct tty *tp; 377 378 newspeed = comdefaultrate; 379 380 error = sysctl_handle_opaque(oidp, &newspeed, sizeof newspeed, req); 381 if (error || !req->newptr) 382 return (error); 383 384 comdefaultrate = newspeed; 385 386 if (comconsole < 0) /* serial console not selected? */ 387 return (0); 388 389 com = com_addr(comconsole); 390 if (com == NULL) 391 return (ENXIO); 392 393 /* 394 * set the initial and lock rates for /dev/ttydXX and /dev/cuaXX 395 * (note, the lock rates really are boolean -- if non-zero, disallow 396 * speed changes) 397 */ 398 com->it_in.c_ispeed = com->it_in.c_ospeed = 399 com->lt_in.c_ispeed = com->lt_in.c_ospeed = 400 com->it_out.c_ispeed = com->it_out.c_ospeed = 401 com->lt_out.c_ispeed = com->lt_out.c_ospeed = comdefaultrate; 402 403 /* 404 * if we're open, change the running rate too 405 */ 406 tp = com->tp; 407 if (tp && (tp->t_state & TS_ISOPEN)) { 408 tp->t_termios.c_ispeed = 409 tp->t_termios.c_ospeed = comdefaultrate; 410 s = spltty(); 411 error = comparam(tp, &tp->t_termios); 412 splx(s); 413 } 414 return error; 415} 416 417SYSCTL_PROC(_machdep, OID_AUTO, conspeed, CTLTYPE_INT | CTLFLAG_RW, 418 0, 0, sysctl_machdep_comdefaultrate, "I", ""); 419/* TUNABLE_INT("machdep.conspeed", &comdefaultrate); */ 420 421#define SET_FLAG(dev, bit) device_set_flags(dev, device_get_flags(dev) | (bit)) 422#define CLR_FLAG(dev, bit) device_set_flags(dev, device_get_flags(dev) & ~(bit)) 423 424/* 425 * Unload the driver and clear the table. 426 * XXX this is mostly wrong. 427 * XXX TODO: 428 * This is usually called when the card is ejected, but 429 * can be caused by a kldunload of a controller driver. 430 * The idea is to reset the driver's view of the device 431 * and ensure that any driver entry points such as 432 * read and write do not hang. 433 */ 434int 435siodetach(dev) 436 device_t dev; 437{ 438 struct com_s *com; 439 int i; 440 441 com = (struct com_s *) device_get_softc(dev); 442 if (com == NULL) { 443 device_printf(dev, "NULL com in siounload\n"); 444 return (0); 445 } 446 com->gone = 1; 447 for (i = 0 ; i < 6; i++) 448 destroy_dev(com->devs[i]); 449 if (com->irqres) { 450 bus_teardown_intr(dev, com->irqres, com->cookie); 451 bus_release_resource(dev, SYS_RES_IRQ, 0, com->irqres); 452 } 453 if (com->ioportres) 454 bus_release_resource(dev, SYS_RES_IOPORT, com->ioportrid, 455 com->ioportres); 456 if (com->tp && (com->tp->t_state & TS_ISOPEN)) { 457 device_printf(dev, "still open, forcing close\n"); 458 (*linesw[com->tp->t_line].l_close)(com->tp, 0); 459 com->tp->t_gen++; 460 ttyclose(com->tp); 461 ttwakeup(com->tp); 462 ttwwakeup(com->tp); 463 } else { 464 if (com->ibuf != NULL) 465 free(com->ibuf, M_DEVBUF); 466 device_set_softc(dev, NULL); 467 free(com, M_DEVBUF); 468 } 469 return (0); 470} 471 472int 473sioprobe(dev, xrid, rclk, noprobe) 474 device_t dev; 475 int xrid; 476 u_long rclk; 477 int noprobe; 478{ 479#if 0 480 static bool_t already_init; 481 device_t xdev; 482#endif 483 struct com_s *com; 484 u_int divisor; 485 bool_t failures[10]; 486 int fn; 487 device_t idev; 488 Port_t iobase; 489 intrmask_t irqmap[4]; 490 intrmask_t irqs; 491 u_char mcr_image; 492 int result; 493 u_long xirq; 494 u_int flags = device_get_flags(dev); 495 int rid; 496 struct resource *port; 497 498 rid = xrid; 499 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 500 0, ~0, IO_COMSIZE, RF_ACTIVE); 501 if (!port) 502 return (ENXIO); 503 504 com = malloc(sizeof(*com), M_DEVBUF, M_NOWAIT | M_ZERO); 505 if (com == NULL) 506 return (ENOMEM); 507 device_set_softc(dev, com); 508 com->bst = rman_get_bustag(port); 509 com->bsh = rman_get_bushandle(port); 510 if (rclk == 0) 511 rclk = DEFAULT_RCLK; 512 com->rclk = rclk; 513 514 while (sio_inited != 2) 515 if (atomic_cmpset_int(&sio_inited, 0, 1)) { 516 mtx_init(&sio_lock, sio_driver_name, NULL, 517 (comconsole != -1) ? 518 MTX_SPIN | MTX_QUIET : MTX_SPIN); 519 atomic_store_rel_int(&sio_inited, 2); 520 } 521 522#if 0 523 /* 524 * XXX this is broken - when we are first called, there are no 525 * previously configured IO ports. We could hard code 526 * 0x3f8, 0x2f8, 0x3e8, 0x2e8 etc but that's probably worse. 527 * This code has been doing nothing since the conversion since 528 * "count" is zero the first time around. 529 */ 530 if (!already_init) { 531 /* 532 * Turn off MCR_IENABLE for all likely serial ports. An unused 533 * port with its MCR_IENABLE gate open will inhibit interrupts 534 * from any used port that shares the interrupt vector. 535 * XXX the gate enable is elsewhere for some multiports. 536 */ 537 device_t *devs; 538 int count, i, xioport; 539 540 devclass_get_devices(sio_devclass, &devs, &count); 541 for (i = 0; i < count; i++) { 542 xdev = devs[i]; 543 if (device_is_enabled(xdev) && 544 bus_get_resource(xdev, SYS_RES_IOPORT, 0, &xioport, 545 NULL) == 0) 546 outb(xioport + com_mcr, 0); 547 } 548 free(devs, M_TEMP); 549 already_init = TRUE; 550 } 551#endif 552 553 if (COM_LLCONSOLE(flags)) { 554 printf("sio%d: reserved for low-level i/o\n", 555 device_get_unit(dev)); 556 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 557 device_set_softc(dev, NULL); 558 free(com, M_DEVBUF); 559 return (ENXIO); 560 } 561 562 /* 563 * If the device is on a multiport card and has an AST/4 564 * compatible interrupt control register, initialize this 565 * register and prepare to leave MCR_IENABLE clear in the mcr. 566 * Otherwise, prepare to set MCR_IENABLE in the mcr. 567 * Point idev to the device struct giving the correct id_irq. 568 * This is the struct for the master device if there is one. 569 */ 570 idev = dev; 571 mcr_image = MCR_IENABLE; 572#ifdef COM_MULTIPORT 573 if (COM_ISMULTIPORT(flags)) { 574 Port_t xiobase; 575 u_long io; 576 577 idev = devclass_get_device(sio_devclass, COM_MPMASTER(flags)); 578 if (idev == NULL) { 579 printf("sio%d: master device %d not configured\n", 580 device_get_unit(dev), COM_MPMASTER(flags)); 581 idev = dev; 582 } 583 if (!COM_NOTAST4(flags)) { 584 if (bus_get_resource(idev, SYS_RES_IOPORT, 0, &io, 585 NULL) == 0) { 586 xiobase = io; 587 if (bus_get_resource(idev, SYS_RES_IRQ, 0, 588 NULL, NULL) == 0) 589 outb(xiobase + com_scr, 0x80); 590 else 591 outb(xiobase + com_scr, 0); 592 } 593 mcr_image = 0; 594 } 595 } 596#endif /* COM_MULTIPORT */ 597 if (bus_get_resource(idev, SYS_RES_IRQ, 0, NULL, NULL) != 0) 598 mcr_image = 0; 599 600 bzero(failures, sizeof failures); 601 iobase = rman_get_start(port); 602 603 /* 604 * We don't want to get actual interrupts, just masked ones. 605 * Interrupts from this line should already be masked in the ICU, 606 * but mask them in the processor as well in case there are some 607 * (misconfigured) shared interrupts. 608 */ 609 mtx_lock_spin(&sio_lock); 610/* EXTRA DELAY? */ 611 612 /* 613 * For the TI16754 chips, set prescaler to 1 (4 is often the 614 * default after-reset value) as otherwise it's impossible to 615 * get highest baudrates. 616 */ 617 if (COM_TI16754(flags)) { 618 u_char cfcr, efr; 619 620 cfcr = sio_getreg(com, com_cfcr); 621 sio_setreg(com, com_cfcr, CFCR_EFR_ENABLE); 622 efr = sio_getreg(com, com_efr); 623 /* Unlock extended features to turn off prescaler. */ 624 sio_setreg(com, com_efr, efr | EFR_EFE); 625 /* Disable EFR. */ 626 sio_setreg(com, com_cfcr, (cfcr != CFCR_EFR_ENABLE) ? cfcr : 0); 627 /* Turn off prescaler. */ 628 sio_setreg(com, com_mcr, 629 sio_getreg(com, com_mcr) & ~MCR_PRESCALE); 630 sio_setreg(com, com_cfcr, CFCR_EFR_ENABLE); 631 sio_setreg(com, com_efr, efr); 632 sio_setreg(com, com_cfcr, cfcr); 633 } 634 635 /* 636 * Initialize the speed and the word size and wait long enough to 637 * drain the maximum of 16 bytes of junk in device output queues. 638 * The speed is undefined after a master reset and must be set 639 * before relying on anything related to output. There may be 640 * junk after a (very fast) soft reboot and (apparently) after 641 * master reset. 642 * XXX what about the UART bug avoided by waiting in comparam()? 643 * We don't want to to wait long enough to drain at 2 bps. 644 */ 645 if (iobase == siocniobase) 646 DELAY((16 + 1) * 1000000 / (comdefaultrate / 10)); 647 else { 648 sio_setreg(com, com_cfcr, CFCR_DLAB | CFCR_8BITS); 649 divisor = siodivisor(rclk, SIO_TEST_SPEED); 650 sio_setreg(com, com_dlbl, divisor & 0xff); 651 sio_setreg(com, com_dlbh, divisor >> 8); 652 sio_setreg(com, com_cfcr, CFCR_8BITS); 653 DELAY((16 + 1) * 1000000 / (SIO_TEST_SPEED / 10)); 654 } 655 656 /* 657 * Enable the interrupt gate and disable device interupts. This 658 * should leave the device driving the interrupt line low and 659 * guarantee an edge trigger if an interrupt can be generated. 660 */ 661/* EXTRA DELAY? */ 662 sio_setreg(com, com_mcr, mcr_image); 663 sio_setreg(com, com_ier, 0); 664 DELAY(1000); /* XXX */ 665 irqmap[0] = isa_irq_pending(); 666 667 /* 668 * Attempt to set loopback mode so that we can send a null byte 669 * without annoying any external device. 670 */ 671/* EXTRA DELAY? */ 672 sio_setreg(com, com_mcr, mcr_image | MCR_LOOPBACK); 673 674 /* 675 * Attempt to generate an output interrupt. On 8250's, setting 676 * IER_ETXRDY generates an interrupt independent of the current 677 * setting and independent of whether the THR is empty. On 16450's, 678 * setting IER_ETXRDY generates an interrupt independent of the 679 * current setting. On 16550A's, setting IER_ETXRDY only 680 * generates an interrupt when IER_ETXRDY is not already set. 681 */ 682 sio_setreg(com, com_ier, IER_ETXRDY); 683 684 /* 685 * On some 16x50 incompatibles, setting IER_ETXRDY doesn't generate 686 * an interrupt. They'd better generate one for actually doing 687 * output. Loopback may be broken on the same incompatibles but 688 * it's unlikely to do more than allow the null byte out. 689 */ 690 sio_setreg(com, com_data, 0); 691 DELAY((1 + 2) * 1000000 / (SIO_TEST_SPEED / 10)); 692 693 /* 694 * Turn off loopback mode so that the interrupt gate works again 695 * (MCR_IENABLE was hidden). This should leave the device driving 696 * an interrupt line high. It doesn't matter if the interrupt 697 * line oscillates while we are not looking at it, since interrupts 698 * are disabled. 699 */ 700/* EXTRA DELAY? */ 701 sio_setreg(com, com_mcr, mcr_image); 702 703 /* 704 * It seems my Xircom CBEM56G Cardbus modem wants to be reset 705 * to 8 bits *again*, or else probe test 0 will fail. 706 * gwk@sgi.com, 4/19/2001 707 */ 708 sio_setreg(com, com_cfcr, CFCR_8BITS); 709 710 /* 711 * Some pcmcia cards have the "TXRDY bug", so we check everyone 712 * for IIR_TXRDY implementation ( Palido 321s, DC-1S... ) 713 */ 714 if (noprobe) { 715 /* Reading IIR register twice */ 716 for (fn = 0; fn < 2; fn ++) { 717 DELAY(10000); 718 failures[6] = sio_getreg(com, com_iir); 719 } 720 /* Check IIR_TXRDY clear ? */ 721 result = 0; 722 if (failures[6] & IIR_TXRDY) { 723 /* No, Double check with clearing IER */ 724 sio_setreg(com, com_ier, 0); 725 if (sio_getreg(com, com_iir) & IIR_NOPEND) { 726 /* Ok. We discovered TXRDY bug! */ 727 SET_FLAG(dev, COM_C_IIR_TXRDYBUG); 728 } else { 729 /* Unknown, Just omit this chip.. XXX */ 730 result = ENXIO; 731 sio_setreg(com, com_mcr, 0); 732 } 733 } else { 734 /* OK. this is well-known guys */ 735 CLR_FLAG(dev, COM_C_IIR_TXRDYBUG); 736 } 737 sio_setreg(com, com_ier, 0); 738 sio_setreg(com, com_cfcr, CFCR_8BITS); 739 mtx_unlock_spin(&sio_lock); 740 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 741 if (iobase == siocniobase) 742 result = 0; 743 if (result != 0) { 744 device_set_softc(dev, NULL); 745 free(com, M_DEVBUF); 746 } 747 return (result); 748 } 749 750 /* 751 * Check that 752 * o the CFCR, IER and MCR in UART hold the values written to them 753 * (the values happen to be all distinct - this is good for 754 * avoiding false positive tests from bus echoes). 755 * o an output interrupt is generated and its vector is correct. 756 * o the interrupt goes away when the IIR in the UART is read. 757 */ 758/* EXTRA DELAY? */ 759 failures[0] = sio_getreg(com, com_cfcr) - CFCR_8BITS; 760 failures[1] = sio_getreg(com, com_ier) - IER_ETXRDY; 761 failures[2] = sio_getreg(com, com_mcr) - mcr_image; 762 DELAY(10000); /* Some internal modems need this time */ 763 irqmap[1] = isa_irq_pending(); 764 failures[4] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_TXRDY; 765 DELAY(1000); /* XXX */ 766 irqmap[2] = isa_irq_pending(); 767 failures[6] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 768 769 /* 770 * Turn off all device interrupts and check that they go off properly. 771 * Leave MCR_IENABLE alone. For ports without a master port, it gates 772 * the OUT2 output of the UART to 773 * the ICU input. Closing the gate would give a floating ICU input 774 * (unless there is another device driving it) and spurious interrupts. 775 * (On the system that this was first tested on, the input floats high 776 * and gives a (masked) interrupt as soon as the gate is closed.) 777 */ 778 sio_setreg(com, com_ier, 0); 779 sio_setreg(com, com_cfcr, CFCR_8BITS); /* dummy to avoid bus echo */ 780 failures[7] = sio_getreg(com, com_ier); 781 DELAY(1000); /* XXX */ 782 irqmap[3] = isa_irq_pending(); 783 failures[9] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 784 785 mtx_unlock_spin(&sio_lock); 786 787 irqs = irqmap[1] & ~irqmap[0]; 788 if (bus_get_resource(idev, SYS_RES_IRQ, 0, &xirq, NULL) == 0 && 789 ((1 << xirq) & irqs) == 0) { 790 printf( 791 "sio%d: configured irq %ld not in bitmap of probed irqs %#x\n", 792 device_get_unit(dev), xirq, irqs); 793 printf( 794 "sio%d: port may not be enabled\n", 795 device_get_unit(dev)); 796 } 797 if (bootverbose) 798 printf("sio%d: irq maps: %#x %#x %#x %#x\n", 799 device_get_unit(dev), 800 irqmap[0], irqmap[1], irqmap[2], irqmap[3]); 801 802 result = 0; 803 for (fn = 0; fn < sizeof failures; ++fn) 804 if (failures[fn]) { 805 sio_setreg(com, com_mcr, 0); 806 result = ENXIO; 807 if (bootverbose) { 808 printf("sio%d: probe failed test(s):", 809 device_get_unit(dev)); 810 for (fn = 0; fn < sizeof failures; ++fn) 811 if (failures[fn]) 812 printf(" %d", fn); 813 printf("\n"); 814 } 815 break; 816 } 817 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 818 if (iobase == siocniobase) 819 result = 0; 820 if (result != 0) { 821 device_set_softc(dev, NULL); 822 free(com, M_DEVBUF); 823 } 824 return (result); 825} 826 827#ifdef COM_ESP 828static int 829espattach(com, esp_port) 830 struct com_s *com; 831 Port_t esp_port; 832{ 833 u_char dips; 834 u_char val; 835 836 /* 837 * Check the ESP-specific I/O port to see if we're an ESP 838 * card. If not, return failure immediately. 839 */ 840 if ((inb(esp_port) & 0xf3) == 0) { 841 printf(" port 0x%x is not an ESP board?\n", esp_port); 842 return (0); 843 } 844 845 /* 846 * We've got something that claims to be a Hayes ESP card. 847 * Let's hope so. 848 */ 849 850 /* Get the dip-switch configuration */ 851 outb(esp_port + ESP_CMD1, ESP_GETDIPS); 852 dips = inb(esp_port + ESP_STATUS1); 853 854 /* 855 * Bits 0,1 of dips say which COM port we are. 856 */ 857 if (rman_get_start(com->ioportres) == likely_com_ports[dips & 0x03]) 858 printf(" : ESP"); 859 else { 860 printf(" esp_port has com %d\n", dips & 0x03); 861 return (0); 862 } 863 864 /* 865 * Check for ESP version 2.0 or later: bits 4,5,6 = 010. 866 */ 867 outb(esp_port + ESP_CMD1, ESP_GETTEST); 868 val = inb(esp_port + ESP_STATUS1); /* clear reg 1 */ 869 val = inb(esp_port + ESP_STATUS2); 870 if ((val & 0x70) < 0x20) { 871 printf("-old (%o)", val & 0x70); 872 return (0); 873 } 874 875 /* 876 * Check for ability to emulate 16550: bit 7 == 1 877 */ 878 if ((dips & 0x80) == 0) { 879 printf(" slave"); 880 return (0); 881 } 882 883 /* 884 * Okay, we seem to be a Hayes ESP card. Whee. 885 */ 886 com->esp = TRUE; 887 com->esp_port = esp_port; 888 return (1); 889} 890#endif /* COM_ESP */ 891 892int 893sioattach(dev, xrid, rclk) 894 device_t dev; 895 int xrid; 896 u_long rclk; 897{ 898 struct com_s *com; 899#ifdef COM_ESP 900 Port_t *espp; 901#endif 902 Port_t iobase; 903 int minorbase; 904 int unit; 905 u_int flags; 906 int rid; 907 struct resource *port; 908 int ret; 909 910 rid = xrid; 911 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 912 0, ~0, IO_COMSIZE, RF_ACTIVE); 913 if (!port) 914 return (ENXIO); 915 916 iobase = rman_get_start(port); 917 unit = device_get_unit(dev); 918 com = device_get_softc(dev); 919 flags = device_get_flags(dev); 920 921 if (unit >= sio_numunits) 922 sio_numunits = unit + 1; 923 /* 924 * sioprobe() has initialized the device registers as follows: 925 * o cfcr = CFCR_8BITS. 926 * It is most important that CFCR_DLAB is off, so that the 927 * data port is not hidden when we enable interrupts. 928 * o ier = 0. 929 * Interrupts are only enabled when the line is open. 930 * o mcr = MCR_IENABLE, or 0 if the port has AST/4 compatible 931 * interrupt control register or the config specifies no irq. 932 * Keeping MCR_DTR and MCR_RTS off might stop the external 933 * device from sending before we are ready. 934 */ 935 bzero(com, sizeof *com); 936 com->unit = unit; 937 com->ioportres = port; 938 com->ioportrid = rid; 939 com->bst = rman_get_bustag(port); 940 com->bsh = rman_get_bushandle(port); 941 com->cfcr_image = CFCR_8BITS; 942 com->dtr_wait = 3 * hz; 943 com->loses_outints = COM_LOSESOUTINTS(flags) != 0; 944 com->no_irq = bus_get_resource(dev, SYS_RES_IRQ, 0, NULL, NULL) != 0; 945 com->tx_fifo_size = 1; 946 com->obufs[0].l_head = com->obuf1; 947 com->obufs[1].l_head = com->obuf2; 948 949 com->data_port = iobase + com_data; 950 com->int_id_port = iobase + com_iir; 951 com->modem_ctl_port = iobase + com_mcr; 952 com->mcr_image = inb(com->modem_ctl_port); 953 com->line_status_port = iobase + com_lsr; 954 com->modem_status_port = iobase + com_msr; 955 com->intr_ctl_port = iobase + com_ier; 956 957 if (rclk == 0) 958 rclk = DEFAULT_RCLK; 959 com->rclk = rclk; 960 961 /* 962 * We don't use all the flags from <sys/ttydefaults.h> since they 963 * are only relevant for logins. It's important to have echo off 964 * initially so that the line doesn't start blathering before the 965 * echo flag can be turned off. 966 */ 967 com->it_in.c_iflag = 0; 968 com->it_in.c_oflag = 0; 969 com->it_in.c_cflag = TTYDEF_CFLAG; 970 com->it_in.c_lflag = 0; 971 if (unit == comconsole) { 972 com->it_in.c_iflag = TTYDEF_IFLAG; 973 com->it_in.c_oflag = TTYDEF_OFLAG; 974 com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL; 975 com->it_in.c_lflag = TTYDEF_LFLAG; 976 com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL; 977 com->lt_out.c_ispeed = com->lt_out.c_ospeed = 978 com->lt_in.c_ispeed = com->lt_in.c_ospeed = 979 com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate; 980 } else 981 com->it_in.c_ispeed = com->it_in.c_ospeed = TTYDEF_SPEED; 982 if (siosetwater(com, com->it_in.c_ispeed) != 0) { 983 mtx_unlock_spin(&sio_lock); 984 /* 985 * Leave i/o resources allocated if this is a `cn'-level 986 * console, so that other devices can't snarf them. 987 */ 988 if (iobase != siocniobase) 989 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 990 return (ENOMEM); 991 } 992 mtx_unlock_spin(&sio_lock); 993 termioschars(&com->it_in); 994 com->it_out = com->it_in; 995 996 /* attempt to determine UART type */ 997 printf("sio%d: type", unit); 998 999 1000 if (!COM_ISMULTIPORT(flags) && 1001 !COM_IIR_TXRDYBUG(flags) && !COM_NOSCR(flags)) { 1002 u_char scr; 1003 u_char scr1; 1004 u_char scr2; 1005 1006 scr = sio_getreg(com, com_scr); 1007 sio_setreg(com, com_scr, 0xa5); 1008 scr1 = sio_getreg(com, com_scr); 1009 sio_setreg(com, com_scr, 0x5a); 1010 scr2 = sio_getreg(com, com_scr); 1011 sio_setreg(com, com_scr, scr); 1012 if (scr1 != 0xa5 || scr2 != 0x5a) { 1013 printf(" 8250 or not responding"); 1014 goto determined_type; 1015 } 1016 } 1017 sio_setreg(com, com_fifo, FIFO_ENABLE | FIFO_RX_HIGH); 1018 DELAY(100); 1019 com->st16650a = 0; 1020 switch (inb(com->int_id_port) & IIR_FIFO_MASK) { 1021 case FIFO_RX_LOW: 1022 printf(" 16450"); 1023 break; 1024 case FIFO_RX_MEDL: 1025 printf(" 16450?"); 1026 break; 1027 case FIFO_RX_MEDH: 1028 printf(" 16550?"); 1029 break; 1030 case FIFO_RX_HIGH: 1031 if (COM_NOFIFO(flags)) { 1032 printf(" 16550A fifo disabled"); 1033 } else { 1034 com->hasfifo = TRUE; 1035 if (COM_ST16650A(flags)) { 1036 com->st16650a = 1; 1037 com->tx_fifo_size = 32; 1038 printf(" ST16650A"); 1039 } else if (COM_TI16754(flags)) { 1040 com->tx_fifo_size = 64; 1041 printf(" TI16754"); 1042 } else { 1043 com->tx_fifo_size = COM_FIFOSIZE(flags); 1044 printf(" 16550A"); 1045 } 1046 } 1047#ifdef COM_ESP 1048 for (espp = likely_esp_ports; *espp != 0; espp++) 1049 if (espattach(com, *espp)) { 1050 com->tx_fifo_size = 1024; 1051 break; 1052 } 1053#endif 1054 if (!com->st16650a && !COM_TI16754(flags)) { 1055 if (!com->tx_fifo_size) 1056 com->tx_fifo_size = 16; 1057 else 1058 printf(" lookalike with %d bytes FIFO", 1059 com->tx_fifo_size); 1060 } 1061 1062 break; 1063 } 1064 1065#ifdef COM_ESP 1066 if (com->esp) { 1067 /* 1068 * Set 16550 compatibility mode. 1069 * We don't use the ESP_MODE_SCALE bit to increase the 1070 * fifo trigger levels because we can't handle large 1071 * bursts of input. 1072 * XXX flow control should be set in comparam(), not here. 1073 */ 1074 outb(com->esp_port + ESP_CMD1, ESP_SETMODE); 1075 outb(com->esp_port + ESP_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO); 1076 1077 /* Set RTS/CTS flow control. */ 1078 outb(com->esp_port + ESP_CMD1, ESP_SETFLOWTYPE); 1079 outb(com->esp_port + ESP_CMD2, ESP_FLOW_RTS); 1080 outb(com->esp_port + ESP_CMD2, ESP_FLOW_CTS); 1081 1082 /* Set flow-control levels. */ 1083 outb(com->esp_port + ESP_CMD1, ESP_SETRXFLOW); 1084 outb(com->esp_port + ESP_CMD2, HIBYTE(768)); 1085 outb(com->esp_port + ESP_CMD2, LOBYTE(768)); 1086 outb(com->esp_port + ESP_CMD2, HIBYTE(512)); 1087 outb(com->esp_port + ESP_CMD2, LOBYTE(512)); 1088 } 1089#endif /* COM_ESP */ 1090 sio_setreg(com, com_fifo, 0); 1091determined_type: ; 1092 1093#ifdef COM_MULTIPORT 1094 if (COM_ISMULTIPORT(flags)) { 1095 device_t masterdev; 1096 1097 com->multiport = TRUE; 1098 printf(" (multiport"); 1099 if (unit == COM_MPMASTER(flags)) 1100 printf(" master"); 1101 printf(")"); 1102 masterdev = devclass_get_device(sio_devclass, 1103 COM_MPMASTER(flags)); 1104 com->no_irq = (masterdev == NULL || bus_get_resource(masterdev, 1105 SYS_RES_IRQ, 0, NULL, NULL) != 0); 1106 } 1107#endif /* COM_MULTIPORT */ 1108 if (unit == comconsole) 1109 printf(", console"); 1110 if (COM_IIR_TXRDYBUG(flags)) 1111 printf(" with a bogus IIR_TXRDY register"); 1112 printf("\n"); 1113 1114 if (sio_fast_ih == NULL) { 1115 swi_add(&tty_ithd, "tty:sio", siopoll, NULL, SWI_TTY, 0, 1116 &sio_fast_ih); 1117 swi_add(&clk_ithd, "tty:sio", siopoll, NULL, SWI_TTY, 0, 1118 &sio_slow_ih); 1119 } 1120 minorbase = UNIT_TO_MINOR(unit); 1121 com->devs[0] = make_dev(&sio_cdevsw, minorbase, 1122 UID_ROOT, GID_WHEEL, 0600, "ttyd%r", unit); 1123 com->devs[1] = make_dev(&sio_cdevsw, minorbase | CONTROL_INIT_STATE, 1124 UID_ROOT, GID_WHEEL, 0600, "ttyid%r", unit); 1125 com->devs[2] = make_dev(&sio_cdevsw, minorbase | CONTROL_LOCK_STATE, 1126 UID_ROOT, GID_WHEEL, 0600, "ttyld%r", unit); 1127 com->devs[3] = make_dev(&sio_cdevsw, minorbase | CALLOUT_MASK, 1128 UID_UUCP, GID_DIALER, 0660, "cuaa%r", unit); 1129 com->devs[4] = make_dev(&sio_cdevsw, 1130 minorbase | CALLOUT_MASK | CONTROL_INIT_STATE, 1131 UID_UUCP, GID_DIALER, 0660, "cuaia%r", unit); 1132 com->devs[5] = make_dev(&sio_cdevsw, 1133 minorbase | CALLOUT_MASK | CONTROL_LOCK_STATE, 1134 UID_UUCP, GID_DIALER, 0660, "cuala%r", unit); 1135 for (rid = 0; rid < 6; rid++) 1136 com->devs[rid]->si_drv1 = com; 1137 com->flags = flags; 1138 com->pps.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR; 1139 1140 if (COM_PPSCTS(flags)) 1141 com->pps_bit = MSR_CTS; 1142 else 1143 com->pps_bit = MSR_DCD; 1144 pps_init(&com->pps); 1145 1146 rid = 0; 1147 com->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0ul, ~0ul, 1, 1148 RF_ACTIVE); 1149 if (com->irqres) { 1150 ret = BUS_SETUP_INTR(device_get_parent(dev), dev, com->irqres, 1151 INTR_TYPE_TTY | INTR_FAST, 1152 siointr, com, &com->cookie); 1153 if (ret) { 1154 ret = BUS_SETUP_INTR(device_get_parent(dev), dev, 1155 com->irqres, INTR_TYPE_TTY, 1156 siointr, com, &com->cookie); 1157 if (ret == 0) 1158 device_printf(dev, "unable to activate interrupt in fast mode - using normal mode\n"); 1159 } 1160 if (ret) 1161 device_printf(dev, "could not activate interrupt\n"); 1162#if defined(DDB) && (defined(BREAK_TO_DEBUGGER) || \ 1163 defined(ALT_BREAK_TO_DEBUGGER)) 1164 /* 1165 * Enable interrupts for early break-to-debugger support 1166 * on the console. 1167 */ 1168 if (ret == 0 && unit == comconsole) 1169 outb(siocniobase + com_ier, IER_ERXRDY | IER_ERLS | 1170 IER_EMSC); 1171#endif 1172 } 1173 1174 return (0); 1175} 1176 1177static int 1178sioopen(dev, flag, mode, td) 1179 dev_t dev; 1180 int flag; 1181 int mode; 1182 struct thread *td; 1183{ 1184 struct com_s *com; 1185 int error; 1186 int mynor; 1187 int s; 1188 struct tty *tp; 1189 int unit; 1190 1191 mynor = minor(dev); 1192 unit = MINOR_TO_UNIT(mynor); 1193 com = com_addr(unit); 1194 if (com == NULL) 1195 return (ENXIO); 1196 if (com->gone) 1197 return (ENXIO); 1198 if (mynor & CONTROL_MASK) 1199 return (0); 1200 tp = dev->si_tty = com->tp = ttymalloc(com->tp); 1201 s = spltty(); 1202 /* 1203 * We jump to this label after all non-interrupted sleeps to pick 1204 * up any changes of the device state. 1205 */ 1206open_top: 1207 while (com->state & CS_DTR_OFF) { 1208 error = tsleep(&com->dtr_wait, TTIPRI | PCATCH, "siodtr", 0); 1209 if (com_addr(unit) == NULL) 1210 return (ENXIO); 1211 if (error != 0 || com->gone) 1212 goto out; 1213 } 1214 if (tp->t_state & TS_ISOPEN) { 1215 /* 1216 * The device is open, so everything has been initialized. 1217 * Handle conflicts. 1218 */ 1219 if (mynor & CALLOUT_MASK) { 1220 if (!com->active_out) { 1221 error = EBUSY; 1222 goto out; 1223 } 1224 } else { 1225 if (com->active_out) { 1226 if (flag & O_NONBLOCK) { 1227 error = EBUSY; 1228 goto out; 1229 } 1230 error = tsleep(&com->active_out, 1231 TTIPRI | PCATCH, "siobi", 0); 1232 if (com_addr(unit) == NULL) 1233 return (ENXIO); 1234 if (error != 0 || com->gone) 1235 goto out; 1236 goto open_top; 1237 } 1238 } 1239 if (tp->t_state & TS_XCLUDE && 1240 suser(td)) { 1241 error = EBUSY; 1242 goto out; 1243 } 1244 } else { 1245 /* 1246 * The device isn't open, so there are no conflicts. 1247 * Initialize it. Initialization is done twice in many 1248 * cases: to preempt sleeping callin opens if we are 1249 * callout, and to complete a callin open after DCD rises. 1250 */ 1251 tp->t_oproc = comstart; 1252 tp->t_param = comparam; 1253 tp->t_stop = comstop; 1254 tp->t_dev = dev; 1255 tp->t_termios = mynor & CALLOUT_MASK 1256 ? com->it_out : com->it_in; 1257 (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET); 1258 com->poll = com->no_irq; 1259 com->poll_output = com->loses_outints; 1260 ++com->wopeners; 1261 error = comparam(tp, &tp->t_termios); 1262 --com->wopeners; 1263 if (error != 0) 1264 goto out; 1265 /* 1266 * XXX we should goto open_top if comparam() slept. 1267 */ 1268 if (com->hasfifo) { 1269 int i; 1270 /* 1271 * (Re)enable and drain fifos. 1272 * 1273 * Certain SMC chips cause problems if the fifos 1274 * are enabled while input is ready. Turn off the 1275 * fifo if necessary to clear the input. We test 1276 * the input ready bit after enabling the fifos 1277 * since we've already enabled them in comparam() 1278 * and to handle races between enabling and fresh 1279 * input. 1280 */ 1281 for (i = 0; i < 500; i++) { 1282 sio_setreg(com, com_fifo, 1283 FIFO_RCV_RST | FIFO_XMT_RST 1284 | com->fifo_image); 1285 /* 1286 * XXX the delays are for superstitious 1287 * historical reasons. It must be less than 1288 * the character time at the maximum 1289 * supported speed (87 usec at 115200 bps 1290 * 8N1). Otherwise we might loop endlessly 1291 * if data is streaming in. We used to use 1292 * delays of 100. That usually worked 1293 * because DELAY(100) used to usually delay 1294 * for about 85 usec instead of 100. 1295 */ 1296 DELAY(50); 1297 if (!(inb(com->line_status_port) & LSR_RXRDY)) 1298 break; 1299 sio_setreg(com, com_fifo, 0); 1300 DELAY(50); 1301 (void) inb(com->data_port); 1302 } 1303 if (i == 500) { 1304 error = EIO; 1305 goto out; 1306 } 1307 } 1308 1309 mtx_lock_spin(&sio_lock); 1310 (void) inb(com->line_status_port); 1311 (void) inb(com->data_port); 1312 com->prev_modem_status = com->last_modem_status 1313 = inb(com->modem_status_port); 1314 if (COM_IIR_TXRDYBUG(com->flags)) { 1315 outb(com->intr_ctl_port, IER_ERXRDY | IER_ERLS 1316 | IER_EMSC); 1317 } else { 1318 outb(com->intr_ctl_port, IER_ERXRDY | IER_ETXRDY 1319 | IER_ERLS | IER_EMSC); 1320 } 1321 mtx_unlock_spin(&sio_lock); 1322 /* 1323 * Handle initial DCD. Callout devices get a fake initial 1324 * DCD (trapdoor DCD). If we are callout, then any sleeping 1325 * callin opens get woken up and resume sleeping on "siobi" 1326 * instead of "siodcd". 1327 */ 1328 /* 1329 * XXX `mynor & CALLOUT_MASK' should be 1330 * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where 1331 * TRAPDOOR_CARRIER is the default initial state for callout 1332 * devices and SOFT_CARRIER is like CLOCAL except it hides 1333 * the true carrier. 1334 */ 1335 if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK) 1336 (*linesw[tp->t_line].l_modem)(tp, 1); 1337 } 1338 /* 1339 * Wait for DCD if necessary. 1340 */ 1341 if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK) 1342 && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) { 1343 ++com->wopeners; 1344 error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "siodcd", 0); 1345 if (com_addr(unit) == NULL) 1346 return (ENXIO); 1347 --com->wopeners; 1348 if (error != 0 || com->gone) 1349 goto out; 1350 goto open_top; 1351 } 1352 error = (*linesw[tp->t_line].l_open)(dev, tp); 1353 disc_optim(tp, &tp->t_termios, com); 1354 if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK) 1355 com->active_out = TRUE; 1356 siosettimeout(); 1357out: 1358 splx(s); 1359 if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0) 1360 comhardclose(com); 1361 return (error); 1362} 1363 1364static int 1365sioclose(dev, flag, mode, td) 1366 dev_t dev; 1367 int flag; 1368 int mode; 1369 struct thread *td; 1370{ 1371 struct com_s *com; 1372 int mynor; 1373 int s; 1374 struct tty *tp; 1375 1376 mynor = minor(dev); 1377 if (mynor & CONTROL_MASK) 1378 return (0); 1379 com = com_addr(MINOR_TO_UNIT(mynor)); 1380 if (com == NULL) 1381 return (ENODEV); 1382 tp = com->tp; 1383 s = spltty(); 1384 (*linesw[tp->t_line].l_close)(tp, flag); 1385 disc_optim(tp, &tp->t_termios, com); 1386 comstop(tp, FREAD | FWRITE); 1387 comhardclose(com); 1388 ttyclose(tp); 1389 siosettimeout(); 1390 splx(s); 1391 if (com->gone) { 1392 printf("sio%d: gone\n", com->unit); 1393 s = spltty(); 1394 if (com->ibuf != NULL) 1395 free(com->ibuf, M_DEVBUF); 1396 bzero(tp, sizeof *tp); 1397 splx(s); 1398 } 1399 return (0); 1400} 1401 1402static void 1403comhardclose(com) 1404 struct com_s *com; 1405{ 1406 int s; 1407 struct tty *tp; 1408 1409 s = spltty(); 1410 com->poll = FALSE; 1411 com->poll_output = FALSE; 1412 com->do_timestamp = FALSE; 1413 com->do_dcd_timestamp = FALSE; 1414 com->pps.ppsparam.mode = 0; 1415 sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 1416 tp = com->tp; 1417 1418#if defined(DDB) && (defined(BREAK_TO_DEBUGGER) || \ 1419 defined(ALT_BREAK_TO_DEBUGGER)) 1420 /* 1421 * Leave interrupts enabled and don't clear DTR if this is the 1422 * console. This allows us to detect break-to-debugger events 1423 * while the console device is closed. 1424 */ 1425 if (com->unit != comconsole) 1426#endif 1427 { 1428 sio_setreg(com, com_ier, 0); 1429 if (tp->t_cflag & HUPCL 1430 /* 1431 * XXX we will miss any carrier drop between here and the 1432 * next open. Perhaps we should watch DCD even when the 1433 * port is closed; it is not sufficient to check it at 1434 * the next open because it might go up and down while 1435 * we're not watching. 1436 */ 1437 || (!com->active_out 1438 && !(com->prev_modem_status & MSR_DCD) 1439 && !(com->it_in.c_cflag & CLOCAL)) 1440 || !(tp->t_state & TS_ISOPEN)) { 1441 (void)commctl(com, TIOCM_DTR, DMBIC); 1442 if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) { 1443 timeout(siodtrwakeup, com, com->dtr_wait); 1444 com->state |= CS_DTR_OFF; 1445 } 1446 } 1447 } 1448 if (com->hasfifo) { 1449 /* 1450 * Disable fifos so that they are off after controlled 1451 * reboots. Some BIOSes fail to detect 16550s when the 1452 * fifos are enabled. 1453 */ 1454 sio_setreg(com, com_fifo, 0); 1455 } 1456 com->active_out = FALSE; 1457 wakeup(&com->active_out); 1458 wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */ 1459 splx(s); 1460} 1461 1462static int 1463sioread(dev, uio, flag) 1464 dev_t dev; 1465 struct uio *uio; 1466 int flag; 1467{ 1468 int mynor; 1469 struct com_s *com; 1470 1471 mynor = minor(dev); 1472 if (mynor & CONTROL_MASK) 1473 return (ENODEV); 1474 com = com_addr(MINOR_TO_UNIT(mynor)); 1475 if (com == NULL || com->gone) 1476 return (ENODEV); 1477 return ((*linesw[com->tp->t_line].l_read)(com->tp, uio, flag)); 1478} 1479 1480static int 1481siowrite(dev, uio, flag) 1482 dev_t dev; 1483 struct uio *uio; 1484 int flag; 1485{ 1486 int mynor; 1487 struct com_s *com; 1488 int unit; 1489 1490 mynor = minor(dev); 1491 if (mynor & CONTROL_MASK) 1492 return (ENODEV); 1493 1494 unit = MINOR_TO_UNIT(mynor); 1495 com = com_addr(unit); 1496 if (com == NULL || com->gone) 1497 return (ENODEV); 1498 /* 1499 * (XXX) We disallow virtual consoles if the physical console is 1500 * a serial port. This is in case there is a display attached that 1501 * is not the console. In that situation we don't need/want the X 1502 * server taking over the console. 1503 */ 1504 if (constty != NULL && unit == comconsole) 1505 constty = NULL; 1506 return ((*linesw[com->tp->t_line].l_write)(com->tp, uio, flag)); 1507} 1508 1509static void 1510siobusycheck(chan) 1511 void *chan; 1512{ 1513 struct com_s *com; 1514 int s; 1515 1516 com = (struct com_s *)chan; 1517 1518 /* 1519 * Clear TS_BUSY if low-level output is complete. 1520 * spl locking is sufficient because siointr1() does not set CS_BUSY. 1521 * If siointr1() clears CS_BUSY after we look at it, then we'll get 1522 * called again. Reading the line status port outside of siointr1() 1523 * is safe because CS_BUSY is clear so there are no output interrupts 1524 * to lose. 1525 */ 1526 s = spltty(); 1527 if (com->state & CS_BUSY) 1528 com->extra_state &= ~CSE_BUSYCHECK; /* False alarm. */ 1529 else if ((inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY)) 1530 == (LSR_TSRE | LSR_TXRDY)) { 1531 com->tp->t_state &= ~TS_BUSY; 1532 ttwwakeup(com->tp); 1533 com->extra_state &= ~CSE_BUSYCHECK; 1534 } else 1535 timeout(siobusycheck, com, hz / 100); 1536 splx(s); 1537} 1538 1539static u_int 1540siodivisor(rclk, speed) 1541 u_long rclk; 1542 speed_t speed; 1543{ 1544 long actual_speed; 1545 u_int divisor; 1546 int error; 1547 1548 if (speed == 0) 1549 return (0); 1550#if UINT_MAX > (ULONG_MAX - 1) / 8 1551 if (speed > (ULONG_MAX - 1) / 8) 1552 return (0); 1553#endif 1554 divisor = (rclk / (8UL * speed) + 1) / 2; 1555 if (divisor == 0 || divisor >= 65536) 1556 return (0); 1557 actual_speed = rclk / (16UL * divisor); 1558 1559 /* 10 times error in percent: */ 1560 error = ((actual_speed - (long)speed) * 2000 / (long)speed + 1) / 2; 1561 1562 /* 3.0% maximum error tolerance: */ 1563 if (error < -30 || error > 30) 1564 return (0); 1565 1566 return (divisor); 1567} 1568 1569static void 1570siodtrwakeup(chan) 1571 void *chan; 1572{ 1573 struct com_s *com; 1574 1575 com = (struct com_s *)chan; 1576 com->state &= ~CS_DTR_OFF; 1577 wakeup(&com->dtr_wait); 1578} 1579 1580/* 1581 * Call this function with the sio_lock mutex held. It will return with the 1582 * lock still held. 1583 */ 1584static void 1585sioinput(com) 1586 struct com_s *com; 1587{ 1588 u_char *buf; 1589 int incc; 1590 u_char line_status; 1591 int recv_data; 1592 struct tty *tp; 1593 1594 buf = com->ibuf; 1595 tp = com->tp; 1596 if (!(tp->t_state & TS_ISOPEN) || !(tp->t_cflag & CREAD)) { 1597 com_events -= (com->iptr - com->ibuf); 1598 com->iptr = com->ibuf; 1599 return; 1600 } 1601 if (tp->t_state & TS_CAN_BYPASS_L_RINT) { 1602 /* 1603 * Avoid the grotesquely inefficient lineswitch routine 1604 * (ttyinput) in "raw" mode. It usually takes about 450 1605 * instructions (that's without canonical processing or echo!). 1606 * slinput is reasonably fast (usually 40 instructions plus 1607 * call overhead). 1608 */ 1609 do { 1610 /* 1611 * This may look odd, but it is using save-and-enable 1612 * semantics instead of the save-and-disable semantics 1613 * that are used everywhere else. 1614 */ 1615 mtx_unlock_spin(&sio_lock); 1616 incc = com->iptr - buf; 1617 if (tp->t_rawq.c_cc + incc > tp->t_ihiwat 1618 && (com->state & CS_RTS_IFLOW 1619 || tp->t_iflag & IXOFF) 1620 && !(tp->t_state & TS_TBLOCK)) 1621 ttyblock(tp); 1622 com->delta_error_counts[CE_TTY_BUF_OVERFLOW] 1623 += b_to_q((char *)buf, incc, &tp->t_rawq); 1624 buf += incc; 1625 tk_nin += incc; 1626 tk_rawcc += incc; 1627 tp->t_rawcc += incc; 1628 ttwakeup(tp); 1629 if (tp->t_state & TS_TTSTOP 1630 && (tp->t_iflag & IXANY 1631 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) { 1632 tp->t_state &= ~TS_TTSTOP; 1633 tp->t_lflag &= ~FLUSHO; 1634 comstart(tp); 1635 } 1636 mtx_lock_spin(&sio_lock); 1637 } while (buf < com->iptr); 1638 } else { 1639 do { 1640 /* 1641 * This may look odd, but it is using save-and-enable 1642 * semantics instead of the save-and-disable semantics 1643 * that are used everywhere else. 1644 */ 1645 mtx_unlock_spin(&sio_lock); 1646 line_status = buf[com->ierroff]; 1647 recv_data = *buf++; 1648 if (line_status 1649 & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) { 1650 if (line_status & LSR_BI) 1651 recv_data |= TTY_BI; 1652 if (line_status & LSR_FE) 1653 recv_data |= TTY_FE; 1654 if (line_status & LSR_OE) 1655 recv_data |= TTY_OE; 1656 if (line_status & LSR_PE) 1657 recv_data |= TTY_PE; 1658 } 1659 (*linesw[tp->t_line].l_rint)(recv_data, tp); 1660 mtx_lock_spin(&sio_lock); 1661 } while (buf < com->iptr); 1662 } 1663 com_events -= (com->iptr - com->ibuf); 1664 com->iptr = com->ibuf; 1665 1666 /* 1667 * There is now room for another low-level buffer full of input, 1668 * so enable RTS if it is now disabled and there is room in the 1669 * high-level buffer. 1670 */ 1671 if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & MCR_RTS) && 1672 !(tp->t_state & TS_TBLOCK)) 1673 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 1674} 1675 1676static void 1677siointr(arg) 1678 void *arg; 1679{ 1680 struct com_s *com; 1681 1682#ifndef COM_MULTIPORT 1683 com = (struct com_s *)arg; 1684 1685 mtx_lock_spin(&sio_lock); 1686 siointr1(com); 1687 mtx_unlock_spin(&sio_lock); 1688#else /* COM_MULTIPORT */ 1689 bool_t possibly_more_intrs; 1690 int unit; 1691 1692 /* 1693 * Loop until there is no activity on any port. This is necessary 1694 * to get an interrupt edge more than to avoid another interrupt. 1695 * If the IRQ signal is just an OR of the IRQ signals from several 1696 * devices, then the edge from one may be lost because another is 1697 * on. 1698 */ 1699 mtx_lock_spin(&sio_lock); 1700 do { 1701 possibly_more_intrs = FALSE; 1702 for (unit = 0; unit < sio_numunits; ++unit) { 1703 com = com_addr(unit); 1704 /* 1705 * XXX COM_LOCK(); 1706 * would it work here, or be counter-productive? 1707 */ 1708 if (com != NULL 1709 && !com->gone 1710 && (inb(com->int_id_port) & IIR_IMASK) 1711 != IIR_NOPEND) { 1712 siointr1(com); 1713 possibly_more_intrs = TRUE; 1714 } 1715 /* XXX COM_UNLOCK(); */ 1716 } 1717 } while (possibly_more_intrs); 1718 mtx_unlock_spin(&sio_lock); 1719#endif /* COM_MULTIPORT */ 1720} 1721 1722static struct timespec siots[8192]; 1723static int siotso; 1724static int volatile siotsunit = -1; 1725 1726static int 1727sysctl_siots(SYSCTL_HANDLER_ARGS) 1728{ 1729 char buf[128]; 1730 long long delta; 1731 size_t len; 1732 int error, i; 1733 1734 for (i = 1; i < siotso; i++) { 1735 delta = (long long)(siots[i].tv_sec - siots[i - 1].tv_sec) * 1736 1000000000 + 1737 (siots[i].tv_nsec - siots[i - 1].tv_nsec); 1738 len = sprintf(buf, "%lld\n", delta); 1739 if (delta >= 110000) 1740 len += sprintf(buf + len - 1, ": *** %ld.%09ld\n", 1741 (long)siots[i].tv_sec, siots[i].tv_nsec); 1742 if (i == siotso - 1) 1743 buf[len - 1] = '\0'; 1744 error = SYSCTL_OUT(req, buf, len); 1745 if (error != 0) 1746 return (error); 1747 uio_yield(); 1748 } 1749 return (0); 1750} 1751 1752SYSCTL_PROC(_machdep, OID_AUTO, siots, CTLTYPE_STRING | CTLFLAG_RD, 1753 0, 0, sysctl_siots, "A", "sio timestamps"); 1754 1755static void 1756siointr1(com) 1757 struct com_s *com; 1758{ 1759 u_char line_status; 1760 u_char modem_status; 1761 u_char *ioptr; 1762 u_char recv_data; 1763 u_char int_ctl; 1764 u_char int_ctl_new; 1765 1766 int_ctl = inb(com->intr_ctl_port); 1767 int_ctl_new = int_ctl; 1768 1769 while (!com->gone) { 1770 if (com->pps.ppsparam.mode & PPS_CAPTUREBOTH) { 1771 modem_status = inb(com->modem_status_port); 1772 if ((modem_status ^ com->last_modem_status) & 1773 com->pps_bit) { 1774 pps_capture(&com->pps); 1775 pps_event(&com->pps, 1776 (modem_status & com->pps_bit) ? 1777 PPS_CAPTUREASSERT : PPS_CAPTURECLEAR); 1778 } 1779 } 1780 line_status = inb(com->line_status_port); 1781 1782 /* input event? (check first to help avoid overruns) */ 1783 while (line_status & LSR_RCV_MASK) { 1784 /* break/unnattached error bits or real input? */ 1785 if (!(line_status & LSR_RXRDY)) 1786 recv_data = 0; 1787 else 1788 recv_data = inb(com->data_port); 1789#if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER) 1790 /* 1791 * Solaris implements a new BREAK which is initiated 1792 * by a character sequence CR ~ ^b which is similar 1793 * to a familiar pattern used on Sun servers by the 1794 * Remote Console. 1795 */ 1796#define KEY_CRTLB 2 /* ^B */ 1797#define KEY_CR 13 /* CR '\r' */ 1798#define KEY_TILDE 126 /* ~ */ 1799 1800 if (com->unit == comconsole) { 1801 static int brk_state1 = 0, brk_state2 = 0; 1802 if (recv_data == KEY_CR) { 1803 brk_state1 = recv_data; 1804 brk_state2 = 0; 1805 } else if (brk_state1 == KEY_CR 1806 && (recv_data == KEY_TILDE 1807 || recv_data == KEY_CRTLB)) { 1808 if (recv_data == KEY_TILDE) 1809 brk_state2 = recv_data; 1810 else if (brk_state2 == KEY_TILDE 1811 && recv_data == KEY_CRTLB) { 1812 breakpoint(); 1813 brk_state1 = 0; 1814 brk_state2 = 0; 1815 goto cont; 1816 } else 1817 brk_state2 = 0; 1818 } else 1819 brk_state1 = 0; 1820 } 1821#endif 1822 if (line_status & (LSR_BI | LSR_FE | LSR_PE)) { 1823 /* 1824 * Don't store BI if IGNBRK or FE/PE if IGNPAR. 1825 * Otherwise, push the work to a higher level 1826 * (to handle PARMRK) if we're bypassing. 1827 * Otherwise, convert BI/FE and PE+INPCK to 0. 1828 * 1829 * This makes bypassing work right in the 1830 * usual "raw" case (IGNBRK set, and IGNPAR 1831 * and INPCK clear). 1832 * 1833 * Note: BI together with FE/PE means just BI. 1834 */ 1835 if (line_status & LSR_BI) { 1836#if defined(DDB) && defined(BREAK_TO_DEBUGGER) 1837 if (com->unit == comconsole) { 1838 breakpoint(); 1839 goto cont; 1840 } 1841#endif 1842 if (com->tp == NULL 1843 || com->tp->t_iflag & IGNBRK) 1844 goto cont; 1845 } else { 1846 if (com->tp == NULL 1847 || com->tp->t_iflag & IGNPAR) 1848 goto cont; 1849 } 1850 if (com->tp->t_state & TS_CAN_BYPASS_L_RINT 1851 && (line_status & (LSR_BI | LSR_FE) 1852 || com->tp->t_iflag & INPCK)) 1853 recv_data = 0; 1854 } 1855 ++com->bytes_in; 1856 if (com->hotchar != 0 && recv_data == com->hotchar) 1857 swi_sched(sio_fast_ih, 0); 1858 ioptr = com->iptr; 1859 if (ioptr >= com->ibufend) 1860 CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW); 1861 else { 1862 if (com->do_timestamp) 1863 microtime(&com->timestamp); 1864 ++com_events; 1865 swi_sched(sio_slow_ih, SWI_DELAY); 1866#if 0 /* for testing input latency vs efficiency */ 1867if (com->iptr - com->ibuf == 8) 1868 swi_sched(sio_fast_ih, 0); 1869#endif 1870 ioptr[0] = recv_data; 1871 ioptr[com->ierroff] = line_status; 1872 com->iptr = ++ioptr; 1873 if (ioptr == com->ihighwater 1874 && com->state & CS_RTS_IFLOW) 1875 outb(com->modem_ctl_port, 1876 com->mcr_image &= ~MCR_RTS); 1877 if (line_status & LSR_OE) 1878 CE_RECORD(com, CE_OVERRUN); 1879 } 1880cont: 1881 /* 1882 * "& 0x7F" is to avoid the gcc-1.40 generating a slow 1883 * jump from the top of the loop to here 1884 */ 1885 line_status = inb(com->line_status_port) & 0x7F; 1886 } 1887 1888 /* modem status change? (always check before doing output) */ 1889 modem_status = inb(com->modem_status_port); 1890 if (modem_status != com->last_modem_status) { 1891 if (com->do_dcd_timestamp 1892 && !(com->last_modem_status & MSR_DCD) 1893 && modem_status & MSR_DCD) 1894 microtime(&com->dcd_timestamp); 1895 1896 /* 1897 * Schedule high level to handle DCD changes. Note 1898 * that we don't use the delta bits anywhere. Some 1899 * UARTs mess them up, and it's easy to remember the 1900 * previous bits and calculate the delta. 1901 */ 1902 com->last_modem_status = modem_status; 1903 if (!(com->state & CS_CHECKMSR)) { 1904 com_events += LOTS_OF_EVENTS; 1905 com->state |= CS_CHECKMSR; 1906 swi_sched(sio_fast_ih, 0); 1907 } 1908 1909 /* handle CTS change immediately for crisp flow ctl */ 1910 if (com->state & CS_CTS_OFLOW) { 1911 if (modem_status & MSR_CTS) 1912 com->state |= CS_ODEVREADY; 1913 else 1914 com->state &= ~CS_ODEVREADY; 1915 } 1916 } 1917 1918 /* output queued and everything ready? */ 1919 if (line_status & LSR_TXRDY 1920 && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) { 1921 ioptr = com->obufq.l_head; 1922 if (com->tx_fifo_size > 1 && com->unit != siotsunit) { 1923 u_int ocount; 1924 1925 ocount = com->obufq.l_tail - ioptr; 1926 if (ocount > com->tx_fifo_size) 1927 ocount = com->tx_fifo_size; 1928 com->bytes_out += ocount; 1929 do 1930 outb(com->data_port, *ioptr++); 1931 while (--ocount != 0); 1932 } else { 1933 outb(com->data_port, *ioptr++); 1934 ++com->bytes_out; 1935 if (com->unit == siotsunit) { 1936 nanouptime(&siots[siotso]); 1937 siotso = (siotso + 1) % 1938 (sizeof siots / sizeof siots[0]); 1939 } 1940 } 1941 com->obufq.l_head = ioptr; 1942 if (COM_IIR_TXRDYBUG(com->flags)) { 1943 int_ctl_new = int_ctl | IER_ETXRDY; 1944 } 1945 if (ioptr >= com->obufq.l_tail) { 1946 struct lbq *qp; 1947 1948 qp = com->obufq.l_next; 1949 qp->l_queued = FALSE; 1950 qp = qp->l_next; 1951 if (qp != NULL) { 1952 com->obufq.l_head = qp->l_head; 1953 com->obufq.l_tail = qp->l_tail; 1954 com->obufq.l_next = qp; 1955 } else { 1956 /* output just completed */ 1957 if (COM_IIR_TXRDYBUG(com->flags)) { 1958 int_ctl_new = int_ctl & ~IER_ETXRDY; 1959 } 1960 com->state &= ~CS_BUSY; 1961 } 1962 if (!(com->state & CS_ODONE)) { 1963 com_events += LOTS_OF_EVENTS; 1964 com->state |= CS_ODONE; 1965 /* handle at high level ASAP */ 1966 swi_sched(sio_fast_ih, 0); 1967 } 1968 } 1969 if (COM_IIR_TXRDYBUG(com->flags) && (int_ctl != int_ctl_new)) { 1970 outb(com->intr_ctl_port, int_ctl_new); 1971 } 1972 } 1973 1974 /* finished? */ 1975#ifndef COM_MULTIPORT 1976 if ((inb(com->int_id_port) & IIR_IMASK) == IIR_NOPEND) 1977#endif /* COM_MULTIPORT */ 1978 return; 1979 } 1980} 1981 1982static int 1983sioioctl(dev, cmd, data, flag, td) 1984 dev_t dev; 1985 u_long cmd; 1986 caddr_t data; 1987 int flag; 1988 struct thread *td; 1989{ 1990 struct com_s *com; 1991 int error; 1992 int mynor; 1993 int s; 1994 struct tty *tp; 1995#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 1996 u_long oldcmd; 1997 struct termios term; 1998#endif 1999 2000 mynor = minor(dev); 2001 com = com_addr(MINOR_TO_UNIT(mynor)); 2002 if (com == NULL || com->gone) 2003 return (ENODEV); 2004 if (mynor & CONTROL_MASK) { 2005 struct termios *ct; 2006 2007 switch (mynor & CONTROL_MASK) { 2008 case CONTROL_INIT_STATE: 2009 ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in; 2010 break; 2011 case CONTROL_LOCK_STATE: 2012 ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in; 2013 break; 2014 default: 2015 return (ENODEV); /* /dev/nodev */ 2016 } 2017 switch (cmd) { 2018 case TIOCSETA: 2019 error = suser(td); 2020 if (error != 0) 2021 return (error); 2022 *ct = *(struct termios *)data; 2023 return (0); 2024 case TIOCGETA: 2025 *(struct termios *)data = *ct; 2026 return (0); 2027 case TIOCGETD: 2028 *(int *)data = TTYDISC; 2029 return (0); 2030 case TIOCGWINSZ: 2031 bzero(data, sizeof(struct winsize)); 2032 return (0); 2033 default: 2034 return (ENOTTY); 2035 } 2036 } 2037 tp = com->tp; 2038#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 2039 term = tp->t_termios; 2040 oldcmd = cmd; 2041 error = ttsetcompat(tp, &cmd, data, &term); 2042 if (error != 0) 2043 return (error); 2044 if (cmd != oldcmd) 2045 data = (caddr_t)&term; 2046#endif 2047 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) { 2048 int cc; 2049 struct termios *dt = (struct termios *)data; 2050 struct termios *lt = mynor & CALLOUT_MASK 2051 ? &com->lt_out : &com->lt_in; 2052 2053 dt->c_iflag = (tp->t_iflag & lt->c_iflag) 2054 | (dt->c_iflag & ~lt->c_iflag); 2055 dt->c_oflag = (tp->t_oflag & lt->c_oflag) 2056 | (dt->c_oflag & ~lt->c_oflag); 2057 dt->c_cflag = (tp->t_cflag & lt->c_cflag) 2058 | (dt->c_cflag & ~lt->c_cflag); 2059 dt->c_lflag = (tp->t_lflag & lt->c_lflag) 2060 | (dt->c_lflag & ~lt->c_lflag); 2061 for (cc = 0; cc < NCCS; ++cc) 2062 if (lt->c_cc[cc] != 0) 2063 dt->c_cc[cc] = tp->t_cc[cc]; 2064 if (lt->c_ispeed != 0) 2065 dt->c_ispeed = tp->t_ispeed; 2066 if (lt->c_ospeed != 0) 2067 dt->c_ospeed = tp->t_ospeed; 2068 } 2069 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td); 2070 if (error != ENOIOCTL) 2071 return (error); 2072 s = spltty(); 2073 error = ttioctl(tp, cmd, data, flag); 2074 disc_optim(tp, &tp->t_termios, com); 2075 if (error != ENOIOCTL) { 2076 splx(s); 2077 return (error); 2078 } 2079 switch (cmd) { 2080 case TIOCSBRK: 2081 sio_setreg(com, com_cfcr, com->cfcr_image |= CFCR_SBREAK); 2082 break; 2083 case TIOCCBRK: 2084 sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 2085 break; 2086 case TIOCSDTR: 2087 (void)commctl(com, TIOCM_DTR, DMBIS); 2088 break; 2089 case TIOCCDTR: 2090 (void)commctl(com, TIOCM_DTR, DMBIC); 2091 break; 2092 /* 2093 * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The 2094 * changes get undone on the next call to comparam(). 2095 */ 2096 case TIOCMSET: 2097 (void)commctl(com, *(int *)data, DMSET); 2098 break; 2099 case TIOCMBIS: 2100 (void)commctl(com, *(int *)data, DMBIS); 2101 break; 2102 case TIOCMBIC: 2103 (void)commctl(com, *(int *)data, DMBIC); 2104 break; 2105 case TIOCMGET: 2106 *(int *)data = commctl(com, 0, DMGET); 2107 break; 2108 case TIOCMSDTRWAIT: 2109 /* must be root since the wait applies to following logins */ 2110 error = suser(td); 2111 if (error != 0) { 2112 splx(s); 2113 return (error); 2114 } 2115 com->dtr_wait = *(int *)data * hz / 100; 2116 break; 2117 case TIOCMGDTRWAIT: 2118 *(int *)data = com->dtr_wait * 100 / hz; 2119 break; 2120 case TIOCTIMESTAMP: 2121 com->do_timestamp = TRUE; 2122 *(struct timeval *)data = com->timestamp; 2123 break; 2124 case TIOCDCDTIMESTAMP: 2125 com->do_dcd_timestamp = TRUE; 2126 *(struct timeval *)data = com->dcd_timestamp; 2127 break; 2128 default: 2129 splx(s); 2130 error = pps_ioctl(cmd, data, &com->pps); 2131 if (error == ENODEV) 2132 error = ENOTTY; 2133 return (error); 2134 } 2135 splx(s); 2136 return (0); 2137} 2138 2139/* software interrupt handler for SWI_TTY */ 2140static void 2141siopoll(void *dummy) 2142{ 2143 int unit; 2144 2145 if (com_events == 0) 2146 return; 2147repeat: 2148 for (unit = 0; unit < sio_numunits; ++unit) { 2149 struct com_s *com; 2150 int incc; 2151 struct tty *tp; 2152 2153 com = com_addr(unit); 2154 if (com == NULL) 2155 continue; 2156 tp = com->tp; 2157 if (tp == NULL || com->gone) { 2158 /* 2159 * Discard any events related to never-opened or 2160 * going-away devices. 2161 */ 2162 mtx_lock_spin(&sio_lock); 2163 incc = com->iptr - com->ibuf; 2164 com->iptr = com->ibuf; 2165 if (com->state & CS_CHECKMSR) { 2166 incc += LOTS_OF_EVENTS; 2167 com->state &= ~CS_CHECKMSR; 2168 } 2169 com_events -= incc; 2170 mtx_unlock_spin(&sio_lock); 2171 continue; 2172 } 2173 if (com->iptr != com->ibuf) { 2174 mtx_lock_spin(&sio_lock); 2175 sioinput(com); 2176 mtx_unlock_spin(&sio_lock); 2177 } 2178 if (com->state & CS_CHECKMSR) { 2179 u_char delta_modem_status; 2180 2181 mtx_lock_spin(&sio_lock); 2182 delta_modem_status = com->last_modem_status 2183 ^ com->prev_modem_status; 2184 com->prev_modem_status = com->last_modem_status; 2185 com_events -= LOTS_OF_EVENTS; 2186 com->state &= ~CS_CHECKMSR; 2187 mtx_unlock_spin(&sio_lock); 2188 if (delta_modem_status & MSR_DCD) 2189 (*linesw[tp->t_line].l_modem) 2190 (tp, com->prev_modem_status & MSR_DCD); 2191 } 2192 if (com->state & CS_ODONE) { 2193 mtx_lock_spin(&sio_lock); 2194 com_events -= LOTS_OF_EVENTS; 2195 com->state &= ~CS_ODONE; 2196 mtx_unlock_spin(&sio_lock); 2197 if (!(com->state & CS_BUSY) 2198 && !(com->extra_state & CSE_BUSYCHECK)) { 2199 timeout(siobusycheck, com, hz / 100); 2200 com->extra_state |= CSE_BUSYCHECK; 2201 } 2202 (*linesw[tp->t_line].l_start)(tp); 2203 } 2204 if (com_events == 0) 2205 break; 2206 } 2207 if (com_events >= LOTS_OF_EVENTS) 2208 goto repeat; 2209} 2210 2211static int 2212comparam(tp, t) 2213 struct tty *tp; 2214 struct termios *t; 2215{ 2216 u_int cfcr; 2217 int cflag; 2218 struct com_s *com; 2219 u_int divisor; 2220 u_char dlbh; 2221 u_char dlbl; 2222 int s; 2223 int unit; 2224 2225 unit = DEV_TO_UNIT(tp->t_dev); 2226 com = com_addr(unit); 2227 if (com == NULL) 2228 return (ENODEV); 2229 2230 /* do historical conversions */ 2231 if (t->c_ispeed == 0) 2232 t->c_ispeed = t->c_ospeed; 2233 2234 /* check requested parameters */ 2235 if (t->c_ospeed == 0) 2236 divisor = 0; 2237 else { 2238 if (t->c_ispeed != t->c_ospeed) 2239 return (EINVAL); 2240 divisor = siodivisor(com->rclk, t->c_ispeed); 2241 if (divisor == 0) 2242 return (EINVAL); 2243 } 2244 2245 /* parameters are OK, convert them to the com struct and the device */ 2246 s = spltty(); 2247 if (divisor == 0) 2248 (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */ 2249 else 2250 (void)commctl(com, TIOCM_DTR, DMBIS); 2251 cflag = t->c_cflag; 2252 switch (cflag & CSIZE) { 2253 case CS5: 2254 cfcr = CFCR_5BITS; 2255 break; 2256 case CS6: 2257 cfcr = CFCR_6BITS; 2258 break; 2259 case CS7: 2260 cfcr = CFCR_7BITS; 2261 break; 2262 default: 2263 cfcr = CFCR_8BITS; 2264 break; 2265 } 2266 if (cflag & PARENB) { 2267 cfcr |= CFCR_PENAB; 2268 if (!(cflag & PARODD)) 2269 cfcr |= CFCR_PEVEN; 2270 } 2271 if (cflag & CSTOPB) 2272 cfcr |= CFCR_STOPB; 2273 2274 if (com->hasfifo && divisor != 0) { 2275 /* 2276 * Use a fifo trigger level low enough so that the input 2277 * latency from the fifo is less than about 16 msec and 2278 * the total latency is less than about 30 msec. These 2279 * latencies are reasonable for humans. Serial comms 2280 * protocols shouldn't expect anything better since modem 2281 * latencies are larger. 2282 * 2283 * The fifo trigger level cannot be set at RX_HIGH for high 2284 * speed connections without further work on reducing 2285 * interrupt disablement times in other parts of the system, 2286 * without producing silo overflow errors. 2287 */ 2288 com->fifo_image = com->unit == siotsunit ? 0 2289 : t->c_ospeed <= 4800 2290 ? FIFO_ENABLE : FIFO_ENABLE | FIFO_RX_MEDH; 2291#ifdef COM_ESP 2292 /* 2293 * The Hayes ESP card needs the fifo DMA mode bit set 2294 * in compatibility mode. If not, it will interrupt 2295 * for each character received. 2296 */ 2297 if (com->esp) 2298 com->fifo_image |= FIFO_DMA_MODE; 2299#endif 2300 sio_setreg(com, com_fifo, com->fifo_image); 2301 } 2302 2303 /* 2304 * This returns with interrupts disabled so that we can complete 2305 * the speed change atomically. Keeping interrupts disabled is 2306 * especially important while com_data is hidden. 2307 */ 2308 (void) siosetwater(com, t->c_ispeed); 2309 2310 if (divisor != 0) { 2311 sio_setreg(com, com_cfcr, cfcr | CFCR_DLAB); 2312 /* 2313 * Only set the divisor registers if they would change, 2314 * since on some 16550 incompatibles (UMC8669F), setting 2315 * them while input is arriving them loses sync until 2316 * data stops arriving. 2317 */ 2318 dlbl = divisor & 0xFF; 2319 if (sio_getreg(com, com_dlbl) != dlbl) 2320 sio_setreg(com, com_dlbl, dlbl); 2321 dlbh = divisor >> 8; 2322 if (sio_getreg(com, com_dlbh) != dlbh) 2323 sio_setreg(com, com_dlbh, dlbh); 2324 } 2325 2326 sio_setreg(com, com_cfcr, com->cfcr_image = cfcr); 2327 2328 if (!(tp->t_state & TS_TTSTOP)) 2329 com->state |= CS_TTGO; 2330 2331 if (cflag & CRTS_IFLOW) { 2332 if (com->st16650a) { 2333 sio_setreg(com, com_cfcr, 0xbf); 2334 sio_setreg(com, com_fifo, 2335 sio_getreg(com, com_fifo) | 0x40); 2336 } 2337 com->state |= CS_RTS_IFLOW; 2338 /* 2339 * If CS_RTS_IFLOW just changed from off to on, the change 2340 * needs to be propagated to MCR_RTS. This isn't urgent, 2341 * so do it later by calling comstart() instead of repeating 2342 * a lot of code from comstart() here. 2343 */ 2344 } else if (com->state & CS_RTS_IFLOW) { 2345 com->state &= ~CS_RTS_IFLOW; 2346 /* 2347 * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS 2348 * on here, since comstart() won't do it later. 2349 */ 2350 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 2351 if (com->st16650a) { 2352 sio_setreg(com, com_cfcr, 0xbf); 2353 sio_setreg(com, com_fifo, 2354 sio_getreg(com, com_fifo) & ~0x40); 2355 } 2356 } 2357 2358 2359 /* 2360 * Set up state to handle output flow control. 2361 * XXX - worth handling MDMBUF (DCD) flow control at the lowest level? 2362 * Now has 10+ msec latency, while CTS flow has 50- usec latency. 2363 */ 2364 com->state |= CS_ODEVREADY; 2365 com->state &= ~CS_CTS_OFLOW; 2366 if (cflag & CCTS_OFLOW) { 2367 com->state |= CS_CTS_OFLOW; 2368 if (!(com->last_modem_status & MSR_CTS)) 2369 com->state &= ~CS_ODEVREADY; 2370 if (com->st16650a) { 2371 sio_setreg(com, com_cfcr, 0xbf); 2372 sio_setreg(com, com_fifo, 2373 sio_getreg(com, com_fifo) | 0x80); 2374 } 2375 } else { 2376 if (com->st16650a) { 2377 sio_setreg(com, com_cfcr, 0xbf); 2378 sio_setreg(com, com_fifo, 2379 sio_getreg(com, com_fifo) & ~0x80); 2380 } 2381 } 2382 2383 sio_setreg(com, com_cfcr, com->cfcr_image); 2384 2385 /* XXX shouldn't call functions while intrs are disabled. */ 2386 disc_optim(tp, t, com); 2387 /* 2388 * Recover from fiddling with CS_TTGO. We used to call siointr1() 2389 * unconditionally, but that defeated the careful discarding of 2390 * stale input in sioopen(). 2391 */ 2392 if (com->state >= (CS_BUSY | CS_TTGO)) 2393 siointr1(com); 2394 2395 mtx_unlock_spin(&sio_lock); 2396 splx(s); 2397 comstart(tp); 2398 if (com->ibufold != NULL) { 2399 free(com->ibufold, M_DEVBUF); 2400 com->ibufold = NULL; 2401 } 2402 return (0); 2403} 2404 2405/* 2406 * This function must be called with the sio_lock mutex released and will 2407 * return with it obtained. 2408 */ 2409static int 2410siosetwater(com, speed) 2411 struct com_s *com; 2412 speed_t speed; 2413{ 2414 int cp4ticks; 2415 u_char *ibuf; 2416 int ibufsize; 2417 struct tty *tp; 2418 2419 /* 2420 * Make the buffer size large enough to handle a softtty interrupt 2421 * latency of about 2 ticks without loss of throughput or data 2422 * (about 3 ticks if input flow control is not used or not honoured, 2423 * but a bit less for CS5-CS7 modes). 2424 */ 2425 cp4ticks = speed / 10 / hz * 4; 2426 for (ibufsize = 128; ibufsize < cp4ticks;) 2427 ibufsize <<= 1; 2428 if (ibufsize == com->ibufsize) { 2429 mtx_lock_spin(&sio_lock); 2430 return (0); 2431 } 2432 2433 /* 2434 * Allocate input buffer. The extra factor of 2 in the size is 2435 * to allow for an error byte for each input byte. 2436 */ 2437 ibuf = malloc(2 * ibufsize, M_DEVBUF, M_NOWAIT); 2438 if (ibuf == NULL) { 2439 mtx_lock_spin(&sio_lock); 2440 return (ENOMEM); 2441 } 2442 2443 /* Initialize non-critical variables. */ 2444 com->ibufold = com->ibuf; 2445 com->ibufsize = ibufsize; 2446 tp = com->tp; 2447 if (tp != NULL) { 2448 tp->t_ififosize = 2 * ibufsize; 2449 tp->t_ispeedwat = (speed_t)-1; 2450 tp->t_ospeedwat = (speed_t)-1; 2451 } 2452 2453 /* 2454 * Read current input buffer, if any. Continue with interrupts 2455 * disabled. 2456 */ 2457 mtx_lock_spin(&sio_lock); 2458 if (com->iptr != com->ibuf) 2459 sioinput(com); 2460 2461 /*- 2462 * Initialize critical variables, including input buffer watermarks. 2463 * The external device is asked to stop sending when the buffer 2464 * exactly reaches high water, or when the high level requests it. 2465 * The high level is notified immediately (rather than at a later 2466 * clock tick) when this watermark is reached. 2467 * The buffer size is chosen so the watermark should almost never 2468 * be reached. 2469 * The low watermark is invisibly 0 since the buffer is always 2470 * emptied all at once. 2471 */ 2472 com->iptr = com->ibuf = ibuf; 2473 com->ibufend = ibuf + ibufsize; 2474 com->ierroff = ibufsize; 2475 com->ihighwater = ibuf + 3 * ibufsize / 4; 2476 return (0); 2477} 2478 2479static void 2480comstart(tp) 2481 struct tty *tp; 2482{ 2483 struct com_s *com; 2484 int s; 2485 int unit; 2486 2487 unit = DEV_TO_UNIT(tp->t_dev); 2488 com = com_addr(unit); 2489 if (com == NULL) 2490 return; 2491 s = spltty(); 2492 mtx_lock_spin(&sio_lock); 2493 if (tp->t_state & TS_TTSTOP) 2494 com->state &= ~CS_TTGO; 2495 else 2496 com->state |= CS_TTGO; 2497 if (tp->t_state & TS_TBLOCK) { 2498 if (com->mcr_image & MCR_RTS && com->state & CS_RTS_IFLOW) 2499 outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS); 2500 } else { 2501 if (!(com->mcr_image & MCR_RTS) && com->iptr < com->ihighwater 2502 && com->state & CS_RTS_IFLOW) 2503 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 2504 } 2505 mtx_unlock_spin(&sio_lock); 2506 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) { 2507 ttwwakeup(tp); 2508 splx(s); 2509 return; 2510 } 2511 if (tp->t_outq.c_cc != 0) { 2512 struct lbq *qp; 2513 struct lbq *next; 2514 2515 if (!com->obufs[0].l_queued) { 2516 com->obufs[0].l_tail 2517 = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1, 2518 sizeof com->obuf1); 2519 com->obufs[0].l_next = NULL; 2520 com->obufs[0].l_queued = TRUE; 2521 mtx_lock_spin(&sio_lock); 2522 if (com->state & CS_BUSY) { 2523 qp = com->obufq.l_next; 2524 while ((next = qp->l_next) != NULL) 2525 qp = next; 2526 qp->l_next = &com->obufs[0]; 2527 } else { 2528 com->obufq.l_head = com->obufs[0].l_head; 2529 com->obufq.l_tail = com->obufs[0].l_tail; 2530 com->obufq.l_next = &com->obufs[0]; 2531 com->state |= CS_BUSY; 2532 } 2533 mtx_unlock_spin(&sio_lock); 2534 } 2535 if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) { 2536 com->obufs[1].l_tail 2537 = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2, 2538 sizeof com->obuf2); 2539 com->obufs[1].l_next = NULL; 2540 com->obufs[1].l_queued = TRUE; 2541 mtx_lock_spin(&sio_lock); 2542 if (com->state & CS_BUSY) { 2543 qp = com->obufq.l_next; 2544 while ((next = qp->l_next) != NULL) 2545 qp = next; 2546 qp->l_next = &com->obufs[1]; 2547 } else { 2548 com->obufq.l_head = com->obufs[1].l_head; 2549 com->obufq.l_tail = com->obufs[1].l_tail; 2550 com->obufq.l_next = &com->obufs[1]; 2551 com->state |= CS_BUSY; 2552 } 2553 mtx_unlock_spin(&sio_lock); 2554 } 2555 tp->t_state |= TS_BUSY; 2556 } 2557 mtx_lock_spin(&sio_lock); 2558 if (com->state >= (CS_BUSY | CS_TTGO)) 2559 siointr1(com); /* fake interrupt to start output */ 2560 mtx_unlock_spin(&sio_lock); 2561 ttwwakeup(tp); 2562 splx(s); 2563} 2564 2565static void 2566comstop(tp, rw) 2567 struct tty *tp; 2568 int rw; 2569{ 2570 struct com_s *com; 2571 2572 com = com_addr(DEV_TO_UNIT(tp->t_dev)); 2573 if (com == NULL || com->gone) 2574 return; 2575 mtx_lock_spin(&sio_lock); 2576 if (rw & FWRITE) { 2577 if (com->hasfifo) 2578#ifdef COM_ESP 2579 /* XXX avoid h/w bug. */ 2580 if (!com->esp) 2581#endif 2582 sio_setreg(com, com_fifo, 2583 FIFO_XMT_RST | com->fifo_image); 2584 com->obufs[0].l_queued = FALSE; 2585 com->obufs[1].l_queued = FALSE; 2586 if (com->state & CS_ODONE) 2587 com_events -= LOTS_OF_EVENTS; 2588 com->state &= ~(CS_ODONE | CS_BUSY); 2589 com->tp->t_state &= ~TS_BUSY; 2590 } 2591 if (rw & FREAD) { 2592 if (com->hasfifo) 2593#ifdef COM_ESP 2594 /* XXX avoid h/w bug. */ 2595 if (!com->esp) 2596#endif 2597 sio_setreg(com, com_fifo, 2598 FIFO_RCV_RST | com->fifo_image); 2599 com_events -= (com->iptr - com->ibuf); 2600 com->iptr = com->ibuf; 2601 } 2602 mtx_unlock_spin(&sio_lock); 2603 comstart(tp); 2604} 2605 2606static int 2607commctl(com, bits, how) 2608 struct com_s *com; 2609 int bits; 2610 int how; 2611{ 2612 int mcr; 2613 int msr; 2614 2615 if (how == DMGET) { 2616 bits = TIOCM_LE; /* XXX - always enabled while open */ 2617 mcr = com->mcr_image; 2618 if (mcr & MCR_DTR) 2619 bits |= TIOCM_DTR; 2620 if (mcr & MCR_RTS) 2621 bits |= TIOCM_RTS; 2622 msr = com->prev_modem_status; 2623 if (msr & MSR_CTS) 2624 bits |= TIOCM_CTS; 2625 if (msr & MSR_DCD) 2626 bits |= TIOCM_CD; 2627 if (msr & MSR_DSR) 2628 bits |= TIOCM_DSR; 2629 /* 2630 * XXX - MSR_RI is naturally volatile, and we make MSR_TERI 2631 * more volatile by reading the modem status a lot. Perhaps 2632 * we should latch both bits until the status is read here. 2633 */ 2634 if (msr & (MSR_RI | MSR_TERI)) 2635 bits |= TIOCM_RI; 2636 return (bits); 2637 } 2638 mcr = 0; 2639 if (bits & TIOCM_DTR) 2640 mcr |= MCR_DTR; 2641 if (bits & TIOCM_RTS) 2642 mcr |= MCR_RTS; 2643 if (com->gone) 2644 return(0); 2645 mtx_lock_spin(&sio_lock); 2646 switch (how) { 2647 case DMSET: 2648 outb(com->modem_ctl_port, 2649 com->mcr_image = mcr | (com->mcr_image & MCR_IENABLE)); 2650 break; 2651 case DMBIS: 2652 outb(com->modem_ctl_port, com->mcr_image |= mcr); 2653 break; 2654 case DMBIC: 2655 outb(com->modem_ctl_port, com->mcr_image &= ~mcr); 2656 break; 2657 } 2658 mtx_unlock_spin(&sio_lock); 2659 return (0); 2660} 2661 2662static void 2663siosettimeout() 2664{ 2665 struct com_s *com; 2666 bool_t someopen; 2667 int unit; 2668 2669 /* 2670 * Set our timeout period to 1 second if no polled devices are open. 2671 * Otherwise set it to max(1/200, 1/hz). 2672 * Enable timeouts iff some device is open. 2673 */ 2674 untimeout(comwakeup, (void *)NULL, sio_timeout_handle); 2675 sio_timeout = hz; 2676 someopen = FALSE; 2677 for (unit = 0; unit < sio_numunits; ++unit) { 2678 com = com_addr(unit); 2679 if (com != NULL && com->tp != NULL 2680 && com->tp->t_state & TS_ISOPEN && !com->gone) { 2681 someopen = TRUE; 2682 if (com->poll || com->poll_output) { 2683 sio_timeout = hz > 200 ? hz / 200 : 1; 2684 break; 2685 } 2686 } 2687 } 2688 if (someopen) { 2689 sio_timeouts_until_log = hz / sio_timeout; 2690 sio_timeout_handle = timeout(comwakeup, (void *)NULL, 2691 sio_timeout); 2692 } else { 2693 /* Flush error messages, if any. */ 2694 sio_timeouts_until_log = 1; 2695 comwakeup((void *)NULL); 2696 untimeout(comwakeup, (void *)NULL, sio_timeout_handle); 2697 } 2698} 2699 2700static void 2701comwakeup(chan) 2702 void *chan; 2703{ 2704 struct com_s *com; 2705 int unit; 2706 2707 sio_timeout_handle = timeout(comwakeup, (void *)NULL, sio_timeout); 2708 2709 /* 2710 * Recover from lost output interrupts. 2711 * Poll any lines that don't use interrupts. 2712 */ 2713 for (unit = 0; unit < sio_numunits; ++unit) { 2714 com = com_addr(unit); 2715 if (com != NULL && !com->gone 2716 && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) { 2717 mtx_lock_spin(&sio_lock); 2718 siointr1(com); 2719 mtx_unlock_spin(&sio_lock); 2720 } 2721 } 2722 2723 /* 2724 * Check for and log errors, but not too often. 2725 */ 2726 if (--sio_timeouts_until_log > 0) 2727 return; 2728 sio_timeouts_until_log = hz / sio_timeout; 2729 for (unit = 0; unit < sio_numunits; ++unit) { 2730 int errnum; 2731 2732 com = com_addr(unit); 2733 if (com == NULL) 2734 continue; 2735 if (com->gone) 2736 continue; 2737 for (errnum = 0; errnum < CE_NTYPES; ++errnum) { 2738 u_int delta; 2739 u_long total; 2740 2741 mtx_lock_spin(&sio_lock); 2742 delta = com->delta_error_counts[errnum]; 2743 com->delta_error_counts[errnum] = 0; 2744 mtx_unlock_spin(&sio_lock); 2745 if (delta == 0) 2746 continue; 2747 total = com->error_counts[errnum] += delta; 2748 log(LOG_ERR, "sio%d: %u more %s%s (total %lu)\n", 2749 unit, delta, error_desc[errnum], 2750 delta == 1 ? "" : "s", total); 2751 } 2752 } 2753} 2754 2755static void 2756disc_optim(tp, t, com) 2757 struct tty *tp; 2758 struct termios *t; 2759 struct com_s *com; 2760{ 2761 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) 2762 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) 2763 && (!(t->c_iflag & PARMRK) 2764 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) 2765 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) 2766 && linesw[tp->t_line].l_rint == ttyinput) 2767 tp->t_state |= TS_CAN_BYPASS_L_RINT; 2768 else 2769 tp->t_state &= ~TS_CAN_BYPASS_L_RINT; 2770 com->hotchar = linesw[tp->t_line].l_hotchar; 2771} 2772 2773/* 2774 * Following are all routines needed for SIO to act as console 2775 */ 2776#include <sys/cons.h> 2777 2778struct siocnstate { 2779 u_char dlbl; 2780 u_char dlbh; 2781 u_char ier; 2782 u_char cfcr; 2783 u_char mcr; 2784}; 2785 2786#ifndef __alpha__ 2787static speed_t siocngetspeed(Port_t, u_long rclk); 2788#endif 2789static void siocnclose(struct siocnstate *sp, Port_t iobase); 2790static void siocnopen(struct siocnstate *sp, Port_t iobase, int speed); 2791static void siocntxwait(Port_t iobase); 2792 2793#ifdef __alpha__ 2794int siocnattach(int port, int speed); 2795int siogdbattach(int port, int speed); 2796int siogdbgetc(void); 2797void siogdbputc(int c); 2798#else 2799static cn_probe_t siocnprobe; 2800static cn_init_t siocninit; 2801static cn_term_t siocnterm; 2802#endif 2803static cn_checkc_t siocncheckc; 2804static cn_getc_t siocngetc; 2805static cn_putc_t siocnputc; 2806 2807#ifndef __alpha__ 2808CONS_DRIVER(sio, siocnprobe, siocninit, siocnterm, siocngetc, siocncheckc, 2809 siocnputc, NULL); 2810#endif 2811 2812/* To get the GDB related variables */ 2813#if DDB > 0 2814#include <ddb/ddb.h> 2815static struct consdev gdbconsdev; 2816 2817#endif 2818 2819static void 2820siocntxwait(iobase) 2821 Port_t iobase; 2822{ 2823 int timo; 2824 2825 /* 2826 * Wait for any pending transmission to finish. Required to avoid 2827 * the UART lockup bug when the speed is changed, and for normal 2828 * transmits. 2829 */ 2830 timo = 100000; 2831 while ((inb(iobase + com_lsr) & (LSR_TSRE | LSR_TXRDY)) 2832 != (LSR_TSRE | LSR_TXRDY) && --timo != 0) 2833 ; 2834} 2835 2836#ifndef __alpha__ 2837 2838/* 2839 * Read the serial port specified and try to figure out what speed 2840 * it's currently running at. We're assuming the serial port has 2841 * been initialized and is basicly idle. This routine is only intended 2842 * to be run at system startup. 2843 * 2844 * If the value read from the serial port doesn't make sense, return 0. 2845 */ 2846 2847static speed_t 2848siocngetspeed(iobase, rclk) 2849 Port_t iobase; 2850 u_long rclk; 2851{ 2852 u_int divisor; 2853 u_char dlbh; 2854 u_char dlbl; 2855 u_char cfcr; 2856 2857 cfcr = inb(iobase + com_cfcr); 2858 outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 2859 2860 dlbl = inb(iobase + com_dlbl); 2861 dlbh = inb(iobase + com_dlbh); 2862 2863 outb(iobase + com_cfcr, cfcr); 2864 2865 divisor = dlbh << 8 | dlbl; 2866 2867 /* XXX there should be more sanity checking. */ 2868 if (divisor == 0) 2869 return (CONSPEED); 2870 return (rclk / (16UL * divisor)); 2871} 2872 2873#endif 2874 2875static void 2876siocnopen(sp, iobase, speed) 2877 struct siocnstate *sp; 2878 Port_t iobase; 2879 int speed; 2880{ 2881 u_int divisor; 2882 u_char dlbh; 2883 u_char dlbl; 2884 2885 /* 2886 * Save all the device control registers except the fifo register 2887 * and set our default ones (cs8 -parenb speed=comdefaultrate). 2888 * We can't save the fifo register since it is read-only. 2889 */ 2890 sp->ier = inb(iobase + com_ier); 2891 outb(iobase + com_ier, 0); /* spltty() doesn't stop siointr() */ 2892 siocntxwait(iobase); 2893 sp->cfcr = inb(iobase + com_cfcr); 2894 outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 2895 sp->dlbl = inb(iobase + com_dlbl); 2896 sp->dlbh = inb(iobase + com_dlbh); 2897 /* 2898 * Only set the divisor registers if they would change, since on 2899 * some 16550 incompatibles (Startech), setting them clears the 2900 * data input register. This also reduces the effects of the 2901 * UMC8669F bug. 2902 */ 2903 divisor = siodivisor(comdefaultrclk, speed); 2904 dlbl = divisor & 0xFF; 2905 if (sp->dlbl != dlbl) 2906 outb(iobase + com_dlbl, dlbl); 2907 dlbh = divisor >> 8; 2908 if (sp->dlbh != dlbh) 2909 outb(iobase + com_dlbh, dlbh); 2910 outb(iobase + com_cfcr, CFCR_8BITS); 2911 sp->mcr = inb(iobase + com_mcr); 2912 /* 2913 * We don't want interrupts, but must be careful not to "disable" 2914 * them by clearing the MCR_IENABLE bit, since that might cause 2915 * an interrupt by floating the IRQ line. 2916 */ 2917 outb(iobase + com_mcr, (sp->mcr & MCR_IENABLE) | MCR_DTR | MCR_RTS); 2918} 2919 2920static void 2921siocnclose(sp, iobase) 2922 struct siocnstate *sp; 2923 Port_t iobase; 2924{ 2925 /* 2926 * Restore the device control registers. 2927 */ 2928 siocntxwait(iobase); 2929 outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 2930 if (sp->dlbl != inb(iobase + com_dlbl)) 2931 outb(iobase + com_dlbl, sp->dlbl); 2932 if (sp->dlbh != inb(iobase + com_dlbh)) 2933 outb(iobase + com_dlbh, sp->dlbh); 2934 outb(iobase + com_cfcr, sp->cfcr); 2935 /* 2936 * XXX damp oscillations of MCR_DTR and MCR_RTS by not restoring them. 2937 */ 2938 outb(iobase + com_mcr, sp->mcr | MCR_DTR | MCR_RTS); 2939 outb(iobase + com_ier, sp->ier); 2940} 2941 2942#ifndef __alpha__ 2943 2944static void 2945siocnprobe(cp) 2946 struct consdev *cp; 2947{ 2948 speed_t boot_speed; 2949 u_char cfcr; 2950 u_int divisor; 2951 int s, unit; 2952 struct siocnstate sp; 2953 2954 /* 2955 * Find our first enabled console, if any. If it is a high-level 2956 * console device, then initialize it and return successfully. 2957 * If it is a low-level console device, then initialize it and 2958 * return unsuccessfully. It must be initialized in both cases 2959 * for early use by console drivers and debuggers. Initializing 2960 * the hardware is not necessary in all cases, since the i/o 2961 * routines initialize it on the fly, but it is necessary if 2962 * input might arrive while the hardware is switched back to an 2963 * uninitialized state. We can't handle multiple console devices 2964 * yet because our low-level routines don't take a device arg. 2965 * We trust the user to set the console flags properly so that we 2966 * don't need to probe. 2967 */ 2968 cp->cn_pri = CN_DEAD; 2969 2970 for (unit = 0; unit < 16; unit++) { /* XXX need to know how many */ 2971 int flags; 2972 2973 if (resource_disabled("sio", unit)) 2974 continue; 2975 if (resource_int_value("sio", unit, "flags", &flags)) 2976 continue; 2977 if (COM_CONSOLE(flags) || COM_DEBUGGER(flags)) { 2978 int port; 2979 Port_t iobase; 2980 2981 if (resource_int_value("sio", unit, "port", &port)) 2982 continue; 2983 iobase = port; 2984 s = spltty(); 2985 if (boothowto & RB_SERIAL) { 2986 boot_speed = 2987 siocngetspeed(iobase, comdefaultrclk); 2988 if (boot_speed) 2989 comdefaultrate = boot_speed; 2990 } 2991 2992 /* 2993 * Initialize the divisor latch. We can't rely on 2994 * siocnopen() to do this the first time, since it 2995 * avoids writing to the latch if the latch appears 2996 * to have the correct value. Also, if we didn't 2997 * just read the speed from the hardware, then we 2998 * need to set the speed in hardware so that 2999 * switching it later is null. 3000 */ 3001 cfcr = inb(iobase + com_cfcr); 3002 outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 3003 divisor = siodivisor(comdefaultrclk, comdefaultrate); 3004 outb(iobase + com_dlbl, divisor & 0xff); 3005 outb(iobase + com_dlbh, divisor >> 8); 3006 outb(iobase + com_cfcr, cfcr); 3007 3008 siocnopen(&sp, iobase, comdefaultrate); 3009 3010 splx(s); 3011 if (COM_CONSOLE(flags) && !COM_LLCONSOLE(flags)) { 3012 cp->cn_dev = makedev(CDEV_MAJOR, unit); 3013 cp->cn_pri = COM_FORCECONSOLE(flags) 3014 || boothowto & RB_SERIAL 3015 ? CN_REMOTE : CN_NORMAL; 3016 siocniobase = iobase; 3017 siocnunit = unit; 3018 } 3019 if (COM_DEBUGGER(flags)) { 3020 printf("sio%d: gdb debugging port\n", unit); 3021 siogdbiobase = iobase; 3022 siogdbunit = unit; 3023#if DDB > 0 3024 gdbconsdev.cn_dev = makedev(CDEV_MAJOR, unit); 3025 gdb_arg = &gdbconsdev; 3026 gdb_getc = siocngetc; 3027 gdb_putc = siocnputc; 3028#endif 3029 } 3030 } 3031 } 3032#ifdef __i386__ 3033#if DDB > 0 3034 /* 3035 * XXX Ugly Compatability. 3036 * If no gdb port has been specified, set it to be the console 3037 * as some configuration files don't specify the gdb port. 3038 */ 3039 if (gdb_arg == NULL && (boothowto & RB_GDB)) { 3040 printf("Warning: no GDB port specified. Defaulting to sio%d.\n", 3041 siocnunit); 3042 printf("Set flag 0x80 on desired GDB port in your\n"); 3043 printf("configuration file (currently sio only).\n"); 3044 siogdbiobase = siocniobase; 3045 siogdbunit = siocnunit; 3046 gdbconsdev.cn_dev = makedev(CDEV_MAJOR, siocnunit); 3047 gdb_arg = &gdbconsdev; 3048 gdb_getc = siocngetc; 3049 gdb_putc = siocnputc; 3050 } 3051#endif 3052#endif 3053} 3054 3055static void 3056siocninit(cp) 3057 struct consdev *cp; 3058{ 3059 comconsole = DEV_TO_UNIT(cp->cn_dev); 3060} 3061 3062static void 3063siocnterm(cp) 3064 struct consdev *cp; 3065{ 3066 comconsole = -1; 3067} 3068 3069#endif 3070 3071#ifdef __alpha__ 3072 3073CONS_DRIVER(sio, NULL, NULL, NULL, siocngetc, siocncheckc, siocnputc, NULL); 3074 3075int 3076siocnattach(port, speed) 3077 int port; 3078 int speed; 3079{ 3080 int s; 3081 u_char cfcr; 3082 u_int divisor; 3083 struct siocnstate sp; 3084 int unit = 0; /* XXX random value! */ 3085 3086 siocniobase = port; 3087 siocnunit = unit; 3088 comdefaultrate = speed; 3089 sio_consdev.cn_pri = CN_NORMAL; 3090 sio_consdev.cn_dev = makedev(CDEV_MAJOR, unit); 3091 3092 s = spltty(); 3093 3094 /* 3095 * Initialize the divisor latch. We can't rely on 3096 * siocnopen() to do this the first time, since it 3097 * avoids writing to the latch if the latch appears 3098 * to have the correct value. Also, if we didn't 3099 * just read the speed from the hardware, then we 3100 * need to set the speed in hardware so that 3101 * switching it later is null. 3102 */ 3103 cfcr = inb(siocniobase + com_cfcr); 3104 outb(siocniobase + com_cfcr, CFCR_DLAB | cfcr); 3105 divisor = siodivisor(comdefaultrclk, comdefaultrate); 3106 outb(siocniobase + com_dlbl, divisor & 0xff); 3107 outb(siocniobase + com_dlbh, divisor >> 8); 3108 outb(siocniobase + com_cfcr, cfcr); 3109 3110 siocnopen(&sp, siocniobase, comdefaultrate); 3111 splx(s); 3112 3113 cnadd(&sio_consdev); 3114 return (0); 3115} 3116 3117int 3118siogdbattach(port, speed) 3119 int port; 3120 int speed; 3121{ 3122 int s; 3123 u_char cfcr; 3124 u_int divisor; 3125 struct siocnstate sp; 3126 int unit = 1; /* XXX random value! */ 3127 3128 siogdbiobase = port; 3129 gdbdefaultrate = speed; 3130 3131 printf("sio%d: gdb debugging port\n", unit); 3132 siogdbunit = unit; 3133#if DDB > 0 3134 gdbconsdev.cn_dev = makedev(CDEV_MAJOR, unit); 3135 gdb_arg = &gdbconsdev; 3136 gdb_getc = siocngetc; 3137 gdb_putc = siocnputc; 3138#endif 3139 3140 s = spltty(); 3141 3142 /* 3143 * Initialize the divisor latch. We can't rely on 3144 * siocnopen() to do this the first time, since it 3145 * avoids writing to the latch if the latch appears 3146 * to have the correct value. Also, if we didn't 3147 * just read the speed from the hardware, then we 3148 * need to set the speed in hardware so that 3149 * switching it later is null. 3150 */ 3151 cfcr = inb(siogdbiobase + com_cfcr); 3152 outb(siogdbiobase + com_cfcr, CFCR_DLAB | cfcr); 3153 divisor = siodivisor(comdefaultrclk, gdbdefaultrate); 3154 outb(siogdbiobase + com_dlbl, divisor & 0xff); 3155 outb(siogdbiobase + com_dlbh, divisor >> 8); 3156 outb(siogdbiobase + com_cfcr, cfcr); 3157 3158 siocnopen(&sp, siogdbiobase, gdbdefaultrate); 3159 splx(s); 3160 3161 return (0); 3162} 3163 3164#endif 3165 3166static int 3167siocncheckc(struct consdev *cd) 3168{ 3169 int c; 3170 dev_t dev; 3171 Port_t iobase; 3172 int s; 3173 struct siocnstate sp; 3174 speed_t speed; 3175 3176 dev = cd->cn_dev; 3177 if (minor(dev) == siocnunit) { 3178 iobase = siocniobase; 3179 speed = comdefaultrate; 3180 } else { 3181 iobase = siogdbiobase; 3182 speed = gdbdefaultrate; 3183 } 3184 s = spltty(); 3185 siocnopen(&sp, iobase, speed); 3186 if (inb(iobase + com_lsr) & LSR_RXRDY) 3187 c = inb(iobase + com_data); 3188 else 3189 c = -1; 3190 siocnclose(&sp, iobase); 3191 splx(s); 3192 return (c); 3193} 3194 3195 3196static int 3197siocngetc(struct consdev *cd) 3198{ 3199 int c; 3200 dev_t dev; 3201 Port_t iobase; 3202 int s; 3203 struct siocnstate sp; 3204 speed_t speed; 3205 3206 dev = cd->cn_dev; 3207 if (minor(dev) == siocnunit) { 3208 iobase = siocniobase; 3209 speed = comdefaultrate; 3210 } else { 3211 iobase = siogdbiobase; 3212 speed = gdbdefaultrate; 3213 } 3214 s = spltty(); 3215 siocnopen(&sp, iobase, speed); 3216 while (!(inb(iobase + com_lsr) & LSR_RXRDY)) 3217 ; 3218 c = inb(iobase + com_data); 3219 siocnclose(&sp, iobase); 3220 splx(s); 3221 return (c); 3222} 3223 3224static void 3225siocnputc(struct consdev *cd, int c) 3226{ 3227 int need_unlock; 3228 int s; 3229 dev_t dev; 3230 struct siocnstate sp; 3231 Port_t iobase; 3232 speed_t speed; 3233 3234 dev = cd->cn_dev; 3235 if (minor(dev) == siocnunit) { 3236 iobase = siocniobase; 3237 speed = comdefaultrate; 3238 } else { 3239 iobase = siogdbiobase; 3240 speed = gdbdefaultrate; 3241 } 3242 s = spltty(); 3243 need_unlock = 0; 3244 if (sio_inited == 2 && !mtx_owned(&sio_lock)) { 3245 mtx_lock_spin(&sio_lock); 3246 need_unlock = 1; 3247 } 3248 siocnopen(&sp, iobase, speed); 3249 siocntxwait(iobase); 3250 outb(iobase + com_data, c); 3251 siocnclose(&sp, iobase); 3252 if (need_unlock) 3253 mtx_unlock_spin(&sio_lock); 3254 splx(s); 3255} 3256 3257#ifdef __alpha__ 3258int 3259siogdbgetc() 3260{ 3261 int c; 3262 Port_t iobase; 3263 speed_t speed; 3264 int s; 3265 struct siocnstate sp; 3266 3267 if (siogdbunit == siocnunit) { 3268 iobase = siocniobase; 3269 speed = comdefaultrate; 3270 } else { 3271 iobase = siogdbiobase; 3272 speed = gdbdefaultrate; 3273 } 3274 3275 s = spltty(); 3276 siocnopen(&sp, iobase, speed); 3277 while (!(inb(iobase + com_lsr) & LSR_RXRDY)) 3278 ; 3279 c = inb(iobase + com_data); 3280 siocnclose(&sp, iobase); 3281 splx(s); 3282 return (c); 3283} 3284 3285void 3286siogdbputc(c) 3287 int c; 3288{ 3289 Port_t iobase; 3290 speed_t speed; 3291 int s; 3292 struct siocnstate sp; 3293 3294 if (siogdbunit == siocnunit) { 3295 iobase = siocniobase; 3296 speed = comdefaultrate; 3297 } else { 3298 iobase = siogdbiobase; 3299 speed = gdbdefaultrate; 3300 } 3301 3302 s = spltty(); 3303 siocnopen(&sp, iobase, speed); 3304 siocntxwait(siogdbiobase); 3305 outb(siogdbiobase + com_data, c); 3306 siocnclose(&sp, siogdbiobase); 3307 splx(s); 3308} 3309#endif 3310