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