sio.c revision 65602
151078Speter/*- 251078Speter * Copyright (c) 1991 The Regents of the University of California. 351078Speter * All rights reserved. 451078Speter * 551078Speter * Redistribution and use in source and binary forms, with or without 651078Speter * modification, are permitted provided that the following conditions 751078Speter * are met: 851078Speter * 1. Redistributions of source code must retain the above copyright 951078Speter * notice, this list of conditions and the following disclaimer. 1051078Speter * 2. Redistributions in binary form must reproduce the above copyright 1151078Speter * notice, this list of conditions and the following disclaimer in the 1251078Speter * documentation and/or other materials provided with the distribution. 1351078Speter * 3. All advertising materials mentioning features or use of this software 1451078Speter * must display the following acknowledgement: 1551078Speter * This product includes software developed by the University of 1651078Speter * California, Berkeley and its contributors. 1751078Speter * 4. Neither the name of the University nor the names of its contributors 1851078Speter * may be used to endorse or promote products derived from this software 1951078Speter * without specific prior written permission. 2051078Speter * 2151078Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2251078Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2351078Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2451078Speter * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2551078Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2651078Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2751078Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2851078Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2951078Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3051078Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3151078Speter * SUCH DAMAGE. 3251078Speter * 3351078Speter * $FreeBSD: head/sys/dev/sio/sio.c 65602 2000-09-08 06:58:58Z grog $ 3451078Speter * from: @(#)com.c 7.5 (Berkeley) 5/16/91 3551078Speter * from: i386/isa sio.c,v 1.234 3651078Speter */ 3751078Speter 3851078Speter#include "opt_comconsole.h" 3951078Speter#include "opt_compat.h" 4051078Speter#include "opt_ddb.h" 4151078Speter#include "opt_sio.h" 4254386Simp#include "card.h" 4358885Simp#include "pci.h" 4451078Speter#include "sio.h" 4551078Speter 4651078Speter/* 4751078Speter * Serial driver, based on 386BSD-0.1 com driver. 4851078Speter * Mostly rewritten to use pseudo-DMA. 4951078Speter * Works for National Semiconductor NS8250-NS16550AF UARTs. 5051078Speter * COM driver, based on HP dca driver. 5151078Speter * 5251078Speter * Changes for PC-Card integration: 5351078Speter * - Added PC-Card driver table and handlers 5451078Speter */ 5551078Speter#include <sys/param.h> 5651078Speter#include <sys/systm.h> 5751078Speter#include <sys/reboot.h> 5851078Speter#include <sys/malloc.h> 5951078Speter#include <sys/tty.h> 6051078Speter#include <sys/proc.h> 6151078Speter#include <sys/module.h> 6251078Speter#include <sys/conf.h> 6351078Speter#include <sys/dkstat.h> 6451078Speter#include <sys/fcntl.h> 6551078Speter#include <sys/interrupt.h> 6651078Speter#include <sys/kernel.h> 6751078Speter#include <sys/syslog.h> 6851078Speter#include <sys/sysctl.h> 6951078Speter#include <sys/bus.h> 7060471Snyan#include <machine/bus_pio.h> 7151078Speter#include <machine/bus.h> 7251078Speter#include <sys/rman.h> 7358377Sphk#include <sys/timetc.h> 7451078Speter#include <sys/timepps.h> 7551078Speter 7651078Speter#include <isa/isareg.h> 7751078Speter#include <isa/isavar.h> 7858885Simp#if NPCI > 0 7958885Simp#include <pci/pcireg.h> 8058885Simp#include <pci/pcivar.h> 8158885Simp#endif 8251078Speter#include <machine/lock.h> 8351078Speter 8451078Speter#include <machine/clock.h> 8551078Speter#include <machine/ipl.h> 8651078Speter#ifndef SMP 8751078Speter#include <machine/lock.h> 8851078Speter#endif 8951078Speter#include <machine/resource.h> 9051078Speter 9151078Speter#include <isa/sioreg.h> 9251078Speter 9351078Speter#ifdef COM_ESP 9451078Speter#include <isa/ic/esp.h> 9551078Speter#endif 9651078Speter#include <isa/ic/ns16550.h> 9751078Speter 9865557Sjasone/* XXX - this is ok because we only do sio fast interrupts on i386 */ 9951078Speter#ifndef __i386__ 10051078Speter#define disable_intr() 10151078Speter#define enable_intr() 10251078Speter#endif 10351078Speter 10451078Speter#define LOTS_OF_EVENTS 64 /* helps separate urgent events from input */ 10551078Speter 10651078Speter#define CALLOUT_MASK 0x80 10751078Speter#define CONTROL_MASK 0x60 10851078Speter#define CONTROL_INIT_STATE 0x20 10951078Speter#define CONTROL_LOCK_STATE 0x40 11051078Speter#define DEV_TO_UNIT(dev) (MINOR_TO_UNIT(minor(dev))) 11151078Speter#define MINOR_MAGIC_MASK (CALLOUT_MASK | CONTROL_MASK) 11251078Speter#define MINOR_TO_UNIT(mynor) ((mynor) & ~MINOR_MAGIC_MASK) 11351078Speter 11451078Speter#ifdef COM_MULTIPORT 11551078Speter/* checks in flags for multiport and which is multiport "master chip" 11651078Speter * for a given card 11751078Speter */ 11851078Speter#define COM_ISMULTIPORT(flags) ((flags) & 0x01) 11951078Speter#define COM_MPMASTER(flags) (((flags) >> 8) & 0x0ff) 12051078Speter#define COM_NOTAST4(flags) ((flags) & 0x04) 12151078Speter#endif /* COM_MULTIPORT */ 12251078Speter 12351078Speter#define COM_CONSOLE(flags) ((flags) & 0x10) 12451078Speter#define COM_FORCECONSOLE(flags) ((flags) & 0x20) 12551078Speter#define COM_LLCONSOLE(flags) ((flags) & 0x40) 12651078Speter#define COM_DEBUGGER(flags) ((flags) & 0x80) 12751078Speter#define COM_LOSESOUTINTS(flags) ((flags) & 0x08) 12851078Speter#define COM_NOFIFO(flags) ((flags) & 0x02) 12951078Speter#define COM_ST16650A(flags) ((flags) & 0x20000) 13051078Speter#define COM_C_NOPROBE (0x40000) 13151078Speter#define COM_NOPROBE(flags) ((flags) & COM_C_NOPROBE) 13251078Speter#define COM_C_IIR_TXRDYBUG (0x80000) 13351078Speter#define COM_IIR_TXRDYBUG(flags) ((flags) & COM_C_IIR_TXRDYBUG) 13451078Speter#define COM_FIFOSIZE(flags) (((flags) & 0xff000000) >> 24) 13551078Speter 13651078Speter#define com_scr 7 /* scratch register for 16450-16550 (R/W) */ 13751078Speter 13860471Snyan#define sio_getreg(com, off) \ 13960471Snyan (bus_space_read_1((com)->bst, (com)->bsh, (off))) 14060471Snyan#define sio_setreg(com, off, value) \ 14160471Snyan (bus_space_write_1((com)->bst, (com)->bsh, (off), (value))) 14260471Snyan 14351078Speter/* 14451078Speter * com state bits. 14551078Speter * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher 14651078Speter * than the other bits so that they can be tested as a group without masking 14751078Speter * off the low bits. 14851078Speter * 14951078Speter * The following com and tty flags correspond closely: 15051078Speter * CS_BUSY = TS_BUSY (maintained by comstart(), siopoll() and 15153344Speter * comstop()) 15251078Speter * CS_TTGO = ~TS_TTSTOP (maintained by comparam() and comstart()) 15351078Speter * CS_CTS_OFLOW = CCTS_OFLOW (maintained by comparam()) 15451078Speter * CS_RTS_IFLOW = CRTS_IFLOW (maintained by comparam()) 15551078Speter * TS_FLUSH is not used. 15651078Speter * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON. 15751078Speter * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state). 15851078Speter */ 15951078Speter#define CS_BUSY 0x80 /* output in progress */ 16051078Speter#define CS_TTGO 0x40 /* output not stopped by XOFF */ 16151078Speter#define CS_ODEVREADY 0x20 /* external device h/w ready (CTS) */ 16251078Speter#define CS_CHECKMSR 1 /* check of MSR scheduled */ 16351078Speter#define CS_CTS_OFLOW 2 /* use CTS output flow control */ 16451078Speter#define CS_DTR_OFF 0x10 /* DTR held off */ 16551078Speter#define CS_ODONE 4 /* output completed */ 16651078Speter#define CS_RTS_IFLOW 8 /* use RTS input flow control */ 16751078Speter#define CSE_BUSYCHECK 1 /* siobusycheck() scheduled */ 16851078Speter 16951078Speterstatic char const * const error_desc[] = { 17051078Speter#define CE_OVERRUN 0 17151078Speter "silo overflow", 17251078Speter#define CE_INTERRUPT_BUF_OVERFLOW 1 17351078Speter "interrupt-level buffer overflow", 17451078Speter#define CE_TTY_BUF_OVERFLOW 2 17551078Speter "tty-level buffer overflow", 17651078Speter}; 17751078Speter 17851078Speter#define CE_NTYPES 3 17951078Speter#define CE_RECORD(com, errnum) (++(com)->delta_error_counts[errnum]) 18051078Speter 18151078Speter/* types. XXX - should be elsewhere */ 18251078Spetertypedef u_int Port_t; /* hardware port */ 18351078Spetertypedef u_char bool_t; /* boolean */ 18451078Speter 18551078Speter/* queue of linear buffers */ 18651078Speterstruct lbq { 18751078Speter u_char *l_head; /* next char to process */ 18851078Speter u_char *l_tail; /* one past the last char to process */ 18951078Speter struct lbq *l_next; /* next in queue */ 19051078Speter bool_t l_queued; /* nonzero if queued */ 19151078Speter}; 19251078Speter 19351078Speter/* com device structure */ 19451078Speterstruct com_s { 19551078Speter u_int flags; /* Copy isa device flags */ 19651078Speter u_char state; /* miscellaneous flag bits */ 19751078Speter bool_t active_out; /* nonzero if the callout device is open */ 19851078Speter u_char cfcr_image; /* copy of value written to CFCR */ 19951078Speter#ifdef COM_ESP 20051078Speter bool_t esp; /* is this unit a hayes esp board? */ 20151078Speter#endif 20251078Speter u_char extra_state; /* more flag bits, separate for order trick */ 20351078Speter u_char fifo_image; /* copy of value written to FIFO */ 20451078Speter bool_t hasfifo; /* nonzero for 16550 UARTs */ 20551078Speter bool_t st16650a; /* Is a Startech 16650A or RTS/CTS compat */ 20651078Speter bool_t loses_outints; /* nonzero if device loses output interrupts */ 20751078Speter u_char mcr_image; /* copy of value written to MCR */ 20851078Speter#ifdef COM_MULTIPORT 20951078Speter bool_t multiport; /* is this unit part of a multiport device? */ 21051078Speter#endif /* COM_MULTIPORT */ 21151078Speter bool_t no_irq; /* nonzero if irq is not attached */ 21251078Speter bool_t gone; /* hardware disappeared */ 21351078Speter bool_t poll; /* nonzero if polling is required */ 21451078Speter bool_t poll_output; /* nonzero if polling for output is required */ 21551078Speter int unit; /* unit number */ 21651078Speter int dtr_wait; /* time to hold DTR down on close (* 1/hz) */ 21751078Speter u_int tx_fifo_size; 21851078Speter u_int wopeners; /* # processes waiting for DCD in open() */ 21951078Speter 22051078Speter /* 22151078Speter * The high level of the driver never reads status registers directly 22251078Speter * because there would be too many side effects to handle conveniently. 22351078Speter * Instead, it reads copies of the registers stored here by the 22451078Speter * interrupt handler. 22551078Speter */ 22651078Speter u_char last_modem_status; /* last MSR read by intr handler */ 22751078Speter u_char prev_modem_status; /* last MSR handled by high level */ 22851078Speter 22951078Speter u_char hotchar; /* ldisc-specific char to be handled ASAP */ 23051078Speter u_char *ibuf; /* start of input buffer */ 23151078Speter u_char *ibufend; /* end of input buffer */ 23251078Speter u_char *ibufold; /* old input buffer, to be freed */ 23351078Speter u_char *ihighwater; /* threshold in input buffer */ 23451078Speter u_char *iptr; /* next free spot in input buffer */ 23551078Speter int ibufsize; /* size of ibuf (not include error bytes) */ 23651078Speter int ierroff; /* offset of error bytes in ibuf */ 23751078Speter 23851078Speter struct lbq obufq; /* head of queue of output buffers */ 23951078Speter struct lbq obufs[2]; /* output buffers */ 24051078Speter 24160471Snyan bus_space_tag_t bst; 24260471Snyan bus_space_handle_t bsh; 24360471Snyan 24451078Speter Port_t data_port; /* i/o ports */ 24551078Speter#ifdef COM_ESP 24651078Speter Port_t esp_port; 24751078Speter#endif 24851078Speter Port_t int_id_port; 24951078Speter Port_t modem_ctl_port; 25051078Speter Port_t line_status_port; 25151078Speter Port_t modem_status_port; 25251078Speter Port_t intr_ctl_port; /* Ports of IIR register */ 25351078Speter 25451078Speter struct tty *tp; /* cross reference */ 25551078Speter 25651078Speter /* Initial state. */ 25751078Speter struct termios it_in; /* should be in struct tty */ 25851078Speter struct termios it_out; 25951078Speter 26051078Speter /* Lock state. */ 26151078Speter struct termios lt_in; /* should be in struct tty */ 26251078Speter struct termios lt_out; 26351078Speter 26451078Speter bool_t do_timestamp; 26551078Speter bool_t do_dcd_timestamp; 26651078Speter struct timeval timestamp; 26751078Speter struct timeval dcd_timestamp; 26851078Speter struct pps_state pps; 26951078Speter 27051078Speter u_long bytes_in; /* statistics */ 27151078Speter u_long bytes_out; 27251078Speter u_int delta_error_counts[CE_NTYPES]; 27351078Speter u_long error_counts[CE_NTYPES]; 27451078Speter 27551078Speter struct resource *irqres; 27651078Speter struct resource *ioportres; 27754386Simp void *cookie; 27865131Sphk dev_t devs[6]; 27951078Speter 28051078Speter /* 28151078Speter * Data area for output buffers. Someday we should build the output 28251078Speter * buffer queue without copying data. 28351078Speter */ 28451078Speter u_char obuf1[256]; 28551078Speter u_char obuf2[256]; 28651078Speter}; 28751078Speter 28851078Speter#ifdef COM_ESP 28951078Speterstatic int espattach __P((struct com_s *com, Port_t esp_port)); 29051078Speter#endif 29158885Simpstatic int sioattach __P((device_t dev, int rid)); 29252471Simpstatic int sio_isa_attach __P((device_t dev)); 29351078Speter 29451078Speterstatic timeout_t siobusycheck; 29551078Speterstatic timeout_t siodtrwakeup; 29651078Speterstatic void comhardclose __P((struct com_s *com)); 29751078Speterstatic void sioinput __P((struct com_s *com)); 29851078Speterstatic void siointr1 __P((struct com_s *com)); 29951078Speterstatic void siointr __P((void *arg)); 30051078Speterstatic int commctl __P((struct com_s *com, int bits, int how)); 30151078Speterstatic int comparam __P((struct tty *tp, struct termios *t)); 30251078Speterstatic swihand_t siopoll; 30358885Simpstatic int sioprobe __P((device_t dev, int xrid)); 30452471Simpstatic int sio_isa_probe __P((device_t dev)); 30551078Speterstatic void siosettimeout __P((void)); 30665596Sjhbstatic int siosetwater __P((struct com_s *com, speed_t speed, int locked)); 30751078Speterstatic void comstart __P((struct tty *tp)); 30851654Sphkstatic void comstop __P((struct tty *tp, int rw)); 30951078Speterstatic timeout_t comwakeup; 31051078Speterstatic void disc_optim __P((struct tty *tp, struct termios *t, 31151078Speter struct com_s *com)); 31251078Speter 31352471Simp#if NCARD > 0 31452471Simpstatic int sio_pccard_attach __P((device_t dev)); 31554386Simpstatic int sio_pccard_detach __P((device_t dev)); 31652471Simpstatic int sio_pccard_probe __P((device_t dev)); 31752471Simp#endif /* NCARD > 0 */ 31851078Speter 31958885Simp#if NPCI > 0 32058885Simpstatic int sio_pci_attach __P((device_t dev)); 32158885Simpstatic void sio_pci_kludge_unit __P((device_t dev)); 32258885Simpstatic int sio_pci_probe __P((device_t dev)); 32358885Simp#endif /* NPCI > 0 */ 32458885Simp 32551078Speterstatic char driver_name[] = "sio"; 32651078Speter 32751078Speter/* table and macro for fast conversion from a unit number to its com struct */ 32851078Speterstatic devclass_t sio_devclass; 32951078Speter#define com_addr(unit) ((struct com_s *) \ 33051078Speter devclass_get_softc(sio_devclass, unit)) 33151078Speter 33252471Simpstatic device_method_t sio_isa_methods[] = { 33351078Speter /* Device interface */ 33452471Simp DEVMETHOD(device_probe, sio_isa_probe), 33552471Simp DEVMETHOD(device_attach, sio_isa_attach), 33651078Speter 33751078Speter { 0, 0 } 33851078Speter}; 33951078Speter 34052471Simpstatic driver_t sio_isa_driver = { 34151078Speter driver_name, 34252471Simp sio_isa_methods, 34351078Speter sizeof(struct com_s), 34451078Speter}; 34551078Speter 34652471Simp#if NCARD > 0 34752471Simpstatic device_method_t sio_pccard_methods[] = { 34852471Simp /* Device interface */ 34952471Simp DEVMETHOD(device_probe, sio_pccard_probe), 35052471Simp DEVMETHOD(device_attach, sio_pccard_attach), 35152471Simp DEVMETHOD(device_detach, sio_pccard_detach), 35252471Simp 35352471Simp { 0, 0 } 35452471Simp}; 35552471Simp 35652471Simpstatic driver_t sio_pccard_driver = { 35752471Simp driver_name, 35852471Simp sio_pccard_methods, 35952471Simp sizeof(struct com_s), 36052471Simp}; 36158885Simp#endif /* NCARD > 0 */ 36252471Simp 36358885Simp#if NPCI > 0 36458885Simpstatic device_method_t sio_pci_methods[] = { 36558885Simp /* Device interface */ 36658885Simp DEVMETHOD(device_probe, sio_pci_probe), 36758885Simp DEVMETHOD(device_attach, sio_pci_attach), 36858885Simp 36958885Simp { 0, 0 } 37058885Simp}; 37158885Simp 37258885Simpstatic driver_t sio_pci_driver = { 37358885Simp driver_name, 37458885Simp sio_pci_methods, 37558885Simp sizeof(struct com_s), 37658885Simp}; 37758885Simp#endif /* NPCI > 0 */ 37858885Simp 37951078Speterstatic d_open_t sioopen; 38051078Speterstatic d_close_t sioclose; 38151078Speterstatic d_read_t sioread; 38251078Speterstatic d_write_t siowrite; 38351078Speterstatic d_ioctl_t sioioctl; 38451078Speter 38551078Speter#define CDEV_MAJOR 28 38651078Speterstatic struct cdevsw sio_cdevsw = { 38751078Speter /* open */ sioopen, 38851078Speter /* close */ sioclose, 38951078Speter /* read */ sioread, 39051078Speter /* write */ siowrite, 39151078Speter /* ioctl */ sioioctl, 39251654Sphk /* poll */ ttypoll, 39351078Speter /* mmap */ nommap, 39451078Speter /* strategy */ nostrategy, 39551078Speter /* name */ driver_name, 39651078Speter /* maj */ CDEV_MAJOR, 39751078Speter /* dump */ nodump, 39851078Speter /* psize */ nopsize, 39951078Speter /* flags */ D_TTY, 40051078Speter /* bmaj */ -1 40151078Speter}; 40251078Speter 40351078Speterint comconsole = -1; 40451078Speterstatic volatile speed_t comdefaultrate = CONSPEED; 40551078Speter#ifdef __alpha__ 40651078Speterstatic volatile speed_t gdbdefaultrate = CONSPEED; 40751078Speter#endif 40851078Speterstatic u_int com_events; /* input chars + weighted output completions */ 40951078Speterstatic Port_t siocniobase; 41051078Speterstatic int siocnunit; 41151078Speterstatic Port_t siogdbiobase; 41251078Speterstatic int siogdbunit = -1; 41351078Speterstatic bool_t sio_registered; 41451078Speterstatic int sio_timeout; 41551078Speterstatic int sio_timeouts_until_log; 41651078Speterstatic struct callout_handle sio_timeout_handle 41751078Speter = CALLOUT_HANDLE_INITIALIZER(&sio_timeout_handle); 41853344Speterstatic int sio_numunits; 41951078Speter 42051078Speterstatic struct speedtab comspeedtab[] = { 42151078Speter { 0, 0 }, 42251078Speter { 50, COMBRD(50) }, 42351078Speter { 75, COMBRD(75) }, 42451078Speter { 110, COMBRD(110) }, 42551078Speter { 134, COMBRD(134) }, 42651078Speter { 150, COMBRD(150) }, 42751078Speter { 200, COMBRD(200) }, 42851078Speter { 300, COMBRD(300) }, 42951078Speter { 600, COMBRD(600) }, 43051078Speter { 1200, COMBRD(1200) }, 43151078Speter { 1800, COMBRD(1800) }, 43251078Speter { 2400, COMBRD(2400) }, 43351078Speter { 4800, COMBRD(4800) }, 43451078Speter { 9600, COMBRD(9600) }, 43551078Speter { 19200, COMBRD(19200) }, 43651078Speter { 38400, COMBRD(38400) }, 43751078Speter { 57600, COMBRD(57600) }, 43851078Speter { 115200, COMBRD(115200) }, 43951078Speter { -1, -1 } 44051078Speter}; 44151078Speter 44251078Speter#ifdef COM_ESP 44351078Speter/* XXX configure this properly. */ 44451078Speterstatic Port_t likely_com_ports[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, }; 44551078Speterstatic Port_t likely_esp_ports[] = { 0x140, 0x180, 0x280, 0 }; 44651078Speter#endif 44751078Speter 44851078Speter/* 44951078Speter * handle sysctl read/write requests for console speed 45051078Speter * 45151078Speter * In addition to setting comdefaultrate for I/O through /dev/console, 45251078Speter * also set the initial and lock values for the /dev/ttyXX device 45351078Speter * if there is one associated with the console. Finally, if the /dev/tty 45451078Speter * device has already been open, change the speed on the open running port 45551078Speter * itself. 45651078Speter */ 45751078Speter 45851078Speterstatic int 45962573Sphksysctl_machdep_comdefaultrate(SYSCTL_HANDLER_ARGS) 46051078Speter{ 46151078Speter int error, s; 46251078Speter speed_t newspeed; 46351078Speter struct com_s *com; 46451078Speter struct tty *tp; 46551078Speter 46651078Speter newspeed = comdefaultrate; 46751078Speter 46851078Speter error = sysctl_handle_opaque(oidp, &newspeed, sizeof newspeed, req); 46951078Speter if (error || !req->newptr) 47051078Speter return (error); 47151078Speter 47251078Speter comdefaultrate = newspeed; 47351078Speter 47451078Speter if (comconsole < 0) /* serial console not selected? */ 47551078Speter return (0); 47651078Speter 47751078Speter com = com_addr(comconsole); 47857915Simp if (com == NULL) 47951078Speter return (ENXIO); 48051078Speter 48151078Speter /* 48251078Speter * set the initial and lock rates for /dev/ttydXX and /dev/cuaXX 48351078Speter * (note, the lock rates really are boolean -- if non-zero, disallow 48451078Speter * speed changes) 48551078Speter */ 48651078Speter com->it_in.c_ispeed = com->it_in.c_ospeed = 48751078Speter com->lt_in.c_ispeed = com->lt_in.c_ospeed = 48851078Speter com->it_out.c_ispeed = com->it_out.c_ospeed = 48951078Speter com->lt_out.c_ispeed = com->lt_out.c_ospeed = comdefaultrate; 49051078Speter 49151078Speter /* 49251078Speter * if we're open, change the running rate too 49351078Speter */ 49451078Speter tp = com->tp; 49551078Speter if (tp && (tp->t_state & TS_ISOPEN)) { 49651078Speter tp->t_termios.c_ispeed = 49751078Speter tp->t_termios.c_ospeed = comdefaultrate; 49851078Speter s = spltty(); 49951078Speter error = comparam(tp, &tp->t_termios); 50051078Speter splx(s); 50151078Speter } 50251078Speter return error; 50351078Speter} 50451078Speter 50551078SpeterSYSCTL_PROC(_machdep, OID_AUTO, conspeed, CTLTYPE_INT | CTLFLAG_RW, 50651078Speter 0, 0, sysctl_machdep_comdefaultrate, "I", ""); 50751078Speter 50853344Speter#define SET_FLAG(dev, bit) device_set_flags(dev, device_get_flags(dev) | (bit)) 50953344Speter#define CLR_FLAG(dev, bit) device_set_flags(dev, device_get_flags(dev) & ~(bit)) 51053344Speter 51151078Speter#if NCARD > 0 51252471Simpstatic int 51352471Simpsio_pccard_probe(dev) 51452471Simp device_t dev; 51551078Speter{ 51654386Simp /* Do not probe IRQ - pccard doesn't turn on the interrupt line */ 51754386Simp /* until bus_setup_intr */ 51853344Speter SET_FLAG(dev, COM_C_NOPROBE); 51953344Speter 52058885Simp return (sioprobe(dev, 0)); 52152471Simp} 52251078Speter 52352471Simpstatic int 52452471Simpsio_pccard_attach(dev) 52552471Simp device_t dev; 52652471Simp{ 52758885Simp return (sioattach(dev, 0)); 52851078Speter} 52951078Speter 53051078Speter/* 53152471Simp * sio_detach - unload the driver and clear the table. 53251078Speter * XXX TODO: 53351078Speter * This is usually called when the card is ejected, but 53451078Speter * can be caused by a modunload of a controller driver. 53551078Speter * The idea is to reset the driver's view of the device 53651078Speter * and ensure that any driver entry points such as 53751078Speter * read and write do not hang. 53851078Speter */ 53953978Simpstatic int 54052471Simpsio_pccard_detach(dev) 54152471Simp device_t dev; 54251078Speter{ 54351078Speter struct com_s *com; 54465131Sphk int i; 54551078Speter 54652471Simp com = (struct com_s *) device_get_softc(dev); 54757915Simp if (com == NULL) { 54852471Simp device_printf(dev, "NULL com in siounload\n"); 54954386Simp return (0); 55051078Speter } 55154386Simp com->gone = 1; 55265131Sphk for (i = 0 ; i < 6; i++) 55365131Sphk destroy_dev(com->devs[i]); 55454386Simp if (com->irqres) { 55554386Simp bus_teardown_intr(dev, com->irqres, com->cookie); 55654386Simp bus_release_resource(dev, SYS_RES_IRQ, 0, com->irqres); 55754386Simp } 55854386Simp if (com->ioportres) 55954386Simp bus_release_resource(dev, SYS_RES_IOPORT, 0, com->ioportres); 56051078Speter if (com->tp && (com->tp->t_state & TS_ISOPEN)) { 56157915Simp device_printf(dev, "still open, forcing close\n"); 56251078Speter com->tp->t_gen++; 56351078Speter ttyclose(com->tp); 56451078Speter ttwakeup(com->tp); 56551078Speter ttwwakeup(com->tp); 56651078Speter } else { 56751078Speter if (com->ibuf != NULL) 56851078Speter free(com->ibuf, M_DEVBUF); 56951078Speter } 57057915Simp device_printf(dev, "unloaded\n"); 57153978Simp return (0); 57251078Speter} 57351078Speter#endif /* NCARD > 0 */ 57451078Speter 57558885Simp#if NPCI > 0 57658885Simpstruct pci_ids { 57758885Simp u_int32_t type; 57858885Simp const char *desc; 57958885Simp int rid; 58058885Simp}; 58151078Speter 58258885Simpstatic struct pci_ids pci_ids[] = { 58358885Simp { 0x100812b9, "3COM PCI FaxModem", 0x10 }, 58458885Simp { 0x048011c1, "ActionTec 56k FAX PCI Modem", 0x14 }, 58558885Simp { 0x00000000, NULL, 0 } 58658885Simp}; 58758885Simp 58858885Simpstatic int 58958885Simpsio_pci_attach(dev) 59058885Simp device_t dev; 59158885Simp{ 59258885Simp u_int32_t type; 59358885Simp struct pci_ids *id; 59458885Simp 59558885Simp type = pci_get_devid(dev); 59658885Simp id = pci_ids; 59758885Simp while (id->type && id->type != type) 59858885Simp id++; 59958885Simp if (id->desc == NULL) 60058885Simp return (ENXIO); 60158885Simp sio_pci_kludge_unit(dev); 60258885Simp return (sioattach(dev, id->rid)); 60358885Simp} 60458885Simp 60558885Simp/* 60658885Simp * Don't cut and paste this to other drivers. It is a horrible kludge 60758885Simp * which will fail to work and also be unnecessary in future versions. 60858885Simp */ 60958885Simpstatic void 61058885Simpsio_pci_kludge_unit(dev) 61158885Simp device_t dev; 61258885Simp{ 61358885Simp devclass_t dc; 61458885Simp int err; 61558885Simp int start; 61658885Simp int unit; 61758885Simp 61858885Simp unit = 0; 61958885Simp start = 0; 62058885Simp while (resource_int_value("sio", unit, "port", &start) == 0 && 62158885Simp start > 0) 62258885Simp unit++; 62358885Simp if (device_get_unit(dev) < unit) { 62458885Simp dc = device_get_devclass(dev); 62558885Simp while (devclass_get_device(dc, unit)) 62658885Simp unit++; 62758885Simp device_printf(dev, "moving to sio%d\n", unit); 62858885Simp err = device_set_unit(dev, unit); /* EVIL DO NOT COPY */ 62958885Simp if (err) 63058885Simp device_printf(dev, "error moving device %d\n", err); 63158885Simp } 63258885Simp} 63358885Simp 63458885Simpstatic int 63558885Simpsio_pci_probe(dev) 63658885Simp device_t dev; 63758885Simp{ 63858885Simp u_int32_t type; 63958885Simp struct pci_ids *id; 64058885Simp 64158885Simp type = pci_get_devid(dev); 64258885Simp id = pci_ids; 64358885Simp while (id->type && id->type != type) 64458885Simp id++; 64558885Simp if (id->desc == NULL) 64658885Simp return (ENXIO); 64758885Simp device_set_desc(dev, id->desc); 64858885Simp return (sioprobe(dev, id->rid)); 64958885Simp} 65058885Simp#endif /* NPCI > 0 */ 65158885Simp 65251078Speterstatic struct isa_pnp_id sio_ids[] = { 65351078Speter {0x0005d041, "Standard PC COM port"}, /* PNP0500 */ 65451078Speter {0x0105d041, "16550A-compatible COM port"}, /* PNP0501 */ 65551078Speter {0x0205d041, "Multiport serial device (non-intelligent 16550)"}, /* PNP0502 */ 65651078Speter {0x1005d041, "Generic IRDA-compatible device"}, /* PNP0510 */ 65751078Speter {0x1105d041, "Generic IRDA-compatible device"}, /* PNP0511 */ 65854944Speter /* Devices that do not have a compatid */ 65960696Stanimura {0x12206804, NULL}, /* ACH2012 - 5634BTS 56K Video Ready Modem */ 66054944Speter {0x7602a904, NULL}, /* AEI0276 - 56K v.90 Fax Modem (LKT) */ 66154944Speter {0x00007905, NULL}, /* AKY0000 - 56K Plug&Play Modem */ 66254944Speter {0x01405407, NULL}, /* AZT4001 - AZT3000 PnP SOUND DEVICE, MODEM */ 66354944Speter {0x56039008, NULL}, /* BDP0356 - Best Data 56x2 */ 66454944Speter {0x36339008, NULL}, /* BDP3336 - Best Data Prods. 336F */ 66554944Speter {0x0014490a, NULL}, /* BRI1400 - Boca 33.6 PnP */ 66654944Speter {0x0015490a, NULL}, /* BRI1500 - Internal Fax Data */ 66754944Speter {0x0034490a, NULL}, /* BRI3400 - Internal ACF Modem */ 66865438Ssheldonh {0x0094490a, NULL}, /* BRI9400 - Boca K56Flex PnP */ 66954944Speter {0x00b4490a, NULL}, /* BRIB400 - Boca 56k PnP */ 67054944Speter {0x0030320d, NULL}, /* CIR3000 - Cirrus Logic V43 */ 67156229Speter {0x0100440e, NULL}, /* CRD0001 - Cardinal MVP288IV ? */ 67260674Stanimura {0x36033610, NULL}, /* DAV0336 - DAVICOM 336PNP MODEM */ 67359888Stanimura {0x0000aa1a, NULL}, /* FUJ0000 - FUJITSU Modem 33600 PNP/I2 */ 67454944Speter {0x1200c31e, NULL}, /* GVC0012 - VF1128HV-R9 (win modem?) */ 67554944Speter {0x0303c31e, NULL}, /* GVC0303 - MaxTech 33.6 PnP D/F/V */ 67655124Speter {0x0505c31e, NULL}, /* GVC0505 - GVC 56k Faxmodem */ 67754944Speter {0x0050c31e, NULL}, /* GVC5000 - some GVC modem */ 67854944Speter {0x3800f91e, NULL}, /* GWY0038 - Telepath with v.90 */ 67954944Speter {0x9062f91e, NULL}, /* GWY6290 - Telepath with x2 Technology */ 68054944Speter {0x0000f435, NULL}, /* MOT0000 - Motorola ModemSURFR 33.6 Intern */ 68154944Speter {0x5015f435, NULL}, /* MOT1550 - Motorola ModemSURFR 56K Modem */ 68254944Speter {0xf015f435, NULL}, /* MOT15F0 - Motorola VoiceSURFR 56K Modem */ 68354944Speter {0x6045f435, NULL}, /* MOT4560 - Motorola ? */ 68454944Speter {0x61e7a338, NULL}, /* NECE761 - 33.6Modem */ 68560674Stanimura {0x0f804f3f, NULL}, /* OZO800f - Zoom 2812 (56k Modem) */ 68654944Speter {0x39804f3f, NULL}, /* OZO8039 - Zoom 56k flex */ 68757769Speter {0x3024a341, NULL}, /* PMC2430 - Pace 56 Voice Internal Modem */ 68854944Speter {0x1000eb49, NULL}, /* ROK0010 - Rockwell ? */ 68954944Speter {0x5002734a, NULL}, /* RSS0250 - 5614Jx3(G) Internal Modem */ 69060708Stanimura {0x6202734a, NULL}, /* RSS0262 - 5614Jx3[G] V90+K56Flex Modem */ 69154944Speter {0xc100ad4d, NULL}, /* SMM00C1 - Leopard 56k PnP */ 69256229Speter {0x9012b04e, NULL}, /* SUP1290 - Supra ? */ 69354944Speter {0x1013b04e, NULL}, /* SUP1310 - SupraExpress 336i PnP */ 69454944Speter {0x8013b04e, NULL}, /* SUP1380 - SupraExpress 288i PnP Voice */ 69554944Speter {0x8113b04e, NULL}, /* SUP1381 - SupraExpress 336i PnP Voice */ 69654944Speter {0x5016b04e, NULL}, /* SUP1650 - Supra 336i Sp Intl */ 69760947Stanimura {0x7016b04e, NULL}, /* SUP1670 - Supra 336i V+ Intl */ 69854944Speter {0x7420b04e, NULL}, /* SUP2070 - Supra ? */ 69954944Speter {0x8020b04e, NULL}, /* SUP2080 - Supra ? */ 70054944Speter {0x8420b04e, NULL}, /* SUP2084 - SupraExpress 56i PnP */ 70156229Speter {0x7121b04e, NULL}, /* SUP2171 - SupraExpress 56i Sp? */ 70254944Speter {0x8024b04e, NULL}, /* SUP2480 - Supra ? */ 70354944Speter {0x01007256, NULL}, /* USR0001 - U.S. Robotics Inc., Sportster W */ 70454944Speter {0x02007256, NULL}, /* USR0002 - U.S. Robotics Inc. Sportster 33. */ 70554944Speter {0x04007256, NULL}, /* USR0004 - USR Sportster 14.4k */ 70654944Speter {0x06007256, NULL}, /* USR0006 - USR Sportster 33.6k */ 70756229Speter {0x11007256, NULL}, /* USR0011 - USR ? */ 70854944Speter {0x01017256, NULL}, /* USR0101 - USR ? */ 70954944Speter {0x30207256, NULL}, /* USR2030 - U.S.Robotics Inc. Sportster 560 */ 71054944Speter {0x50207256, NULL}, /* USR2050 - U.S.Robotics Inc. Sportster 33. */ 71154944Speter {0x70207256, NULL}, /* USR2070 - U.S.Robotics Inc. Sportster 560 */ 71254944Speter {0x30307256, NULL}, /* USR3030 - U.S. Robotics 56K FAX INT */ 71354944Speter {0x31307256, NULL}, /* USR3031 - U.S. Robotics 56K FAX INT */ 71458848Speter {0x50307256, NULL}, /* USR3050 - U.S. Robotics 56K FAX INT */ 71554944Speter {0x70307256, NULL}, /* USR3070 - U.S. Robotics 56K Voice INT */ 71656229Speter {0x90307256, NULL}, /* USR3090 - USR ? */ 71764688Speter {0x70917256, NULL}, /* USR9170 - U.S. Robotics 56K FAX INT */ 71854944Speter {0x90917256, NULL}, /* USR9190 - USR 56k Voice INT */ 71954944Speter {0x0300695c, NULL}, /* WCI0003 - Fax/Voice/Modem/Speakphone/Asvd */ 72065469Ssheldonh {0x01a0896a, NULL}, /* ZTIA001 - Zoom Internal V90 Faxmodem */ 72154944Speter {0x61f7896a, NULL}, /* ZTIF761 - Zoom ComStar 33.6 */ 72251078Speter {0} 72351078Speter}; 72451078Speter 72556229Speter 72656229Speter 72751078Speterstatic int 72852471Simpsio_isa_probe(dev) 72952471Simp device_t dev; 73052471Simp{ 73152471Simp /* Check isapnp ids */ 73252471Simp if (ISA_PNP_PROBE(device_get_parent(dev), dev, sio_ids) == ENXIO) 73352471Simp return (ENXIO); 73458885Simp return (sioprobe(dev, 0)); 73552471Simp} 73652471Simp 73752471Simpstatic int 73858885Simpsioprobe(dev, xrid) 73951078Speter device_t dev; 74058885Simp int xrid; 74151078Speter{ 74253344Speter#if 0 74351078Speter static bool_t already_init; 74453344Speter device_t xdev; 74553344Speter#endif 74660471Snyan struct com_s *com; 74751078Speter bool_t failures[10]; 74851078Speter int fn; 74951078Speter device_t idev; 75051078Speter Port_t iobase; 75151078Speter intrmask_t irqmap[4]; 75251078Speter intrmask_t irqs; 75351078Speter u_char mcr_image; 75451078Speter int result; 75554206Speter u_long xirq; 75651088Speter u_int flags = device_get_flags(dev); 75751078Speter int rid; 75851078Speter struct resource *port; 75965557Sjasone int intrsave; 76051078Speter 76158885Simp rid = xrid; 76251078Speter port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 76351078Speter 0, ~0, IO_COMSIZE, RF_ACTIVE); 76451078Speter if (!port) 76557915Simp return (ENXIO); 76651078Speter 76760471Snyan com = device_get_softc(dev); 76860471Snyan com->bst = rman_get_bustag(port); 76960471Snyan com->bsh = rman_get_bushandle(port); 77060471Snyan 77153344Speter#if 0 77253344Speter /* 77353344Speter * XXX this is broken - when we are first called, there are no 77453344Speter * previously configured IO ports. We could hard code 77553344Speter * 0x3f8, 0x2f8, 0x3e8, 0x2e8 etc but that's probably worse. 77653344Speter * This code has been doing nothing since the conversion since 77753344Speter * "count" is zero the first time around. 77853344Speter */ 77951078Speter if (!already_init) { 78051078Speter /* 78151078Speter * Turn off MCR_IENABLE for all likely serial ports. An unused 78251078Speter * port with its MCR_IENABLE gate open will inhibit interrupts 78351078Speter * from any used port that shares the interrupt vector. 78451078Speter * XXX the gate enable is elsewhere for some multiports. 78551078Speter */ 78651078Speter device_t *devs; 78753344Speter int count, i, xioport; 78851078Speter 78951078Speter devclass_get_devices(sio_devclass, &devs, &count); 79051078Speter for (i = 0; i < count; i++) { 79151078Speter xdev = devs[i]; 79254194Speter if (device_is_enabled(xdev) && 79354194Speter bus_get_resource(xdev, SYS_RES_IOPORT, 0, &xioport, 79454194Speter NULL) == 0) 79553344Speter outb(xioport + com_mcr, 0); 79651078Speter } 79751078Speter free(devs, M_TEMP); 79851078Speter already_init = TRUE; 79951078Speter } 80053344Speter#endif 80151078Speter 80251078Speter if (COM_LLCONSOLE(flags)) { 80351078Speter printf("sio%d: reserved for low-level i/o\n", 80451078Speter device_get_unit(dev)); 80556788Sbde bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 80651078Speter return (ENXIO); 80751078Speter } 80851078Speter 80951078Speter /* 81051078Speter * If the device is on a multiport card and has an AST/4 81151078Speter * compatible interrupt control register, initialize this 81251078Speter * register and prepare to leave MCR_IENABLE clear in the mcr. 81351078Speter * Otherwise, prepare to set MCR_IENABLE in the mcr. 81451078Speter * Point idev to the device struct giving the correct id_irq. 81551078Speter * This is the struct for the master device if there is one. 81651078Speter */ 81751078Speter idev = dev; 81851078Speter mcr_image = MCR_IENABLE; 81951078Speter#ifdef COM_MULTIPORT 82057234Sbde if (COM_ISMULTIPORT(flags)) { 82154206Speter Port_t xiobase; 82254206Speter u_long io; 82354206Speter 82451078Speter idev = devclass_get_device(sio_devclass, COM_MPMASTER(flags)); 82551078Speter if (idev == NULL) { 82651078Speter printf("sio%d: master device %d not configured\n", 82751078Speter device_get_unit(dev), COM_MPMASTER(flags)); 82851078Speter idev = dev; 82951078Speter } 83057234Sbde if (!COM_NOTAST4(flags)) { 83157234Sbde if (bus_get_resource(idev, SYS_RES_IOPORT, 0, &io, 83257234Sbde NULL) == 0) { 83357234Sbde xiobase = io; 83457234Sbde if (bus_get_resource(idev, SYS_RES_IRQ, 0, 83557234Sbde NULL, NULL) == 0) 83657234Sbde outb(xiobase + com_scr, 0x80); 83757234Sbde else 83857234Sbde outb(xiobase + com_scr, 0); 83957234Sbde } 84057234Sbde mcr_image = 0; 84151078Speter } 84251078Speter } 84351078Speter#endif /* COM_MULTIPORT */ 84454194Speter if (bus_get_resource(idev, SYS_RES_IRQ, 0, NULL, NULL) != 0) 84551078Speter mcr_image = 0; 84651078Speter 84751078Speter bzero(failures, sizeof failures); 84851078Speter iobase = rman_get_start(port); 84951078Speter 85051078Speter /* 85151078Speter * We don't want to get actual interrupts, just masked ones. 85251078Speter * Interrupts from this line should already be masked in the ICU, 85351078Speter * but mask them in the processor as well in case there are some 85451078Speter * (misconfigured) shared interrupts. 85551078Speter */ 85665557Sjasone intrsave = save_intr(); 85751078Speter disable_intr(); 85865557Sjasone COM_LOCK(); 85951078Speter/* EXTRA DELAY? */ 86051078Speter 86151078Speter /* 86251078Speter * Initialize the speed and the word size and wait long enough to 86351078Speter * drain the maximum of 16 bytes of junk in device output queues. 86451078Speter * The speed is undefined after a master reset and must be set 86551078Speter * before relying on anything related to output. There may be 86651078Speter * junk after a (very fast) soft reboot and (apparently) after 86751078Speter * master reset. 86851078Speter * XXX what about the UART bug avoided by waiting in comparam()? 86951078Speter * We don't want to to wait long enough to drain at 2 bps. 87051078Speter */ 87151078Speter if (iobase == siocniobase) 87251078Speter DELAY((16 + 1) * 1000000 / (comdefaultrate / 10)); 87351078Speter else { 87460471Snyan sio_setreg(com, com_cfcr, CFCR_DLAB | CFCR_8BITS); 87560471Snyan sio_setreg(com, com_dlbl, COMBRD(SIO_TEST_SPEED) & 0xff); 87660471Snyan sio_setreg(com, com_dlbh, (u_int) COMBRD(SIO_TEST_SPEED) >> 8); 87760471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); 87851078Speter DELAY((16 + 1) * 1000000 / (SIO_TEST_SPEED / 10)); 87951078Speter } 88051078Speter 88151078Speter /* 88251078Speter * Enable the interrupt gate and disable device interupts. This 88351078Speter * should leave the device driving the interrupt line low and 88451078Speter * guarantee an edge trigger if an interrupt can be generated. 88551078Speter */ 88651078Speter/* EXTRA DELAY? */ 88760471Snyan sio_setreg(com, com_mcr, mcr_image); 88860471Snyan sio_setreg(com, com_ier, 0); 88951078Speter DELAY(1000); /* XXX */ 89051078Speter irqmap[0] = isa_irq_pending(); 89151078Speter 89251078Speter /* 89351078Speter * Attempt to set loopback mode so that we can send a null byte 89451078Speter * without annoying any external device. 89551078Speter */ 89651078Speter/* EXTRA DELAY? */ 89760471Snyan sio_setreg(com, com_mcr, mcr_image | MCR_LOOPBACK); 89851078Speter 89951078Speter /* 90051078Speter * Attempt to generate an output interrupt. On 8250's, setting 90151078Speter * IER_ETXRDY generates an interrupt independent of the current 90251078Speter * setting and independent of whether the THR is empty. On 16450's, 90351078Speter * setting IER_ETXRDY generates an interrupt independent of the 90451078Speter * current setting. On 16550A's, setting IER_ETXRDY only 90551078Speter * generates an interrupt when IER_ETXRDY is not already set. 90651078Speter */ 90760471Snyan sio_setreg(com, com_ier, IER_ETXRDY); 90851078Speter 90951078Speter /* 91051078Speter * On some 16x50 incompatibles, setting IER_ETXRDY doesn't generate 91151078Speter * an interrupt. They'd better generate one for actually doing 91251078Speter * output. Loopback may be broken on the same incompatibles but 91351078Speter * it's unlikely to do more than allow the null byte out. 91451078Speter */ 91560471Snyan sio_setreg(com, com_data, 0); 91651078Speter DELAY((1 + 2) * 1000000 / (SIO_TEST_SPEED / 10)); 91751078Speter 91851078Speter /* 91951078Speter * Turn off loopback mode so that the interrupt gate works again 92051078Speter * (MCR_IENABLE was hidden). This should leave the device driving 92151078Speter * an interrupt line high. It doesn't matter if the interrupt 92251078Speter * line oscillates while we are not looking at it, since interrupts 92351078Speter * are disabled. 92451078Speter */ 92551078Speter/* EXTRA DELAY? */ 92660471Snyan sio_setreg(com, com_mcr, mcr_image); 92751078Speter 92851078Speter /* 92952471Simp * Some pcmcia cards have the "TXRDY bug", so we check everyone 93051078Speter * for IIR_TXRDY implementation ( Palido 321s, DC-1S... ) 93151078Speter */ 93253370Speter if (COM_NOPROBE(flags)) { 93353370Speter /* Reading IIR register twice */ 93453370Speter for (fn = 0; fn < 2; fn ++) { 93553370Speter DELAY(10000); 93660471Snyan failures[6] = sio_getreg(com, com_iir); 93753370Speter } 93853370Speter /* Check IIR_TXRDY clear ? */ 93953370Speter result = 0; 94053370Speter if (failures[6] & IIR_TXRDY) { 94153370Speter /* Nop, Double check with clearing IER */ 94260471Snyan sio_setreg(com, com_ier, 0); 94360471Snyan if (sio_getreg(com, com_iir) & IIR_NOPEND) { 94453370Speter /* Ok. we're familia this gang */ 94553370Speter SET_FLAG(dev, COM_C_IIR_TXRDYBUG); 94653370Speter } else { 94753370Speter /* Unknown, Just omit this chip.. XXX */ 94853370Speter result = ENXIO; 94953370Speter } 95051078Speter } else { 95153370Speter /* OK. this is well-known guys */ 95253370Speter CLR_FLAG(dev, COM_C_IIR_TXRDYBUG); 95351078Speter } 95460471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); 95565557Sjasone COM_UNLOCK(); 95665557Sjasone restore_intr(intrsave); 95753344Speter bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 95853344Speter return (iobase == siocniobase ? 0 : result); 95953344Speter } 96053344Speter 96151078Speter /* 96251078Speter * Check that 96351078Speter * o the CFCR, IER and MCR in UART hold the values written to them 96451078Speter * (the values happen to be all distinct - this is good for 96551078Speter * avoiding false positive tests from bus echoes). 96651078Speter * o an output interrupt is generated and its vector is correct. 96751078Speter * o the interrupt goes away when the IIR in the UART is read. 96851078Speter */ 96951078Speter/* EXTRA DELAY? */ 97060471Snyan failures[0] = sio_getreg(com, com_cfcr) - CFCR_8BITS; 97160471Snyan failures[1] = sio_getreg(com, com_ier) - IER_ETXRDY; 97260471Snyan failures[2] = sio_getreg(com, com_mcr) - mcr_image; 97351078Speter DELAY(10000); /* Some internal modems need this time */ 97451078Speter irqmap[1] = isa_irq_pending(); 97560471Snyan failures[4] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_TXRDY; 97651078Speter DELAY(1000); /* XXX */ 97751078Speter irqmap[2] = isa_irq_pending(); 97860471Snyan failures[6] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 97951078Speter 98051078Speter /* 98151078Speter * Turn off all device interrupts and check that they go off properly. 98251078Speter * Leave MCR_IENABLE alone. For ports without a master port, it gates 98351078Speter * the OUT2 output of the UART to 98451078Speter * the ICU input. Closing the gate would give a floating ICU input 98551078Speter * (unless there is another device driving it) and spurious interrupts. 98651078Speter * (On the system that this was first tested on, the input floats high 98751078Speter * and gives a (masked) interrupt as soon as the gate is closed.) 98851078Speter */ 98960471Snyan sio_setreg(com, com_ier, 0); 99060471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); /* dummy to avoid bus echo */ 99160471Snyan failures[7] = sio_getreg(com, com_ier); 99251078Speter DELAY(1000); /* XXX */ 99351078Speter irqmap[3] = isa_irq_pending(); 99460471Snyan failures[9] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 99551078Speter 99665557Sjasone COM_UNLOCK(); 99765557Sjasone restore_intr(intrsave); 99851078Speter 99951078Speter irqs = irqmap[1] & ~irqmap[0]; 100054194Speter if (bus_get_resource(idev, SYS_RES_IRQ, 0, &xirq, NULL) == 0 && 100154194Speter ((1 << xirq) & irqs) == 0) 100251078Speter printf( 100354206Speter "sio%d: configured irq %ld not in bitmap of probed irqs %#x\n", 100453344Speter device_get_unit(dev), xirq, irqs); 100551078Speter if (bootverbose) 100651078Speter printf("sio%d: irq maps: %#x %#x %#x %#x\n", 100751078Speter device_get_unit(dev), 100851078Speter irqmap[0], irqmap[1], irqmap[2], irqmap[3]); 100951078Speter 101051078Speter result = 0; 101151078Speter for (fn = 0; fn < sizeof failures; ++fn) 101251078Speter if (failures[fn]) { 101360471Snyan sio_setreg(com, com_mcr, 0); 101451078Speter result = ENXIO; 101551078Speter if (bootverbose) { 101651078Speter printf("sio%d: probe failed test(s):", 101751078Speter device_get_unit(dev)); 101851078Speter for (fn = 0; fn < sizeof failures; ++fn) 101951078Speter if (failures[fn]) 102051078Speter printf(" %d", fn); 102151078Speter printf("\n"); 102251078Speter } 102351078Speter break; 102451078Speter } 102551078Speter bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 102651078Speter return (iobase == siocniobase ? 0 : result); 102751078Speter} 102851078Speter 102951078Speter#ifdef COM_ESP 103051078Speterstatic int 103151078Speterespattach(com, esp_port) 103251078Speter struct com_s *com; 103351078Speter Port_t esp_port; 103451078Speter{ 103551078Speter u_char dips; 103651078Speter u_char val; 103751078Speter 103851078Speter /* 103951078Speter * Check the ESP-specific I/O port to see if we're an ESP 104051078Speter * card. If not, return failure immediately. 104151078Speter */ 104251078Speter if ((inb(esp_port) & 0xf3) == 0) { 104351078Speter printf(" port 0x%x is not an ESP board?\n", esp_port); 104451078Speter return (0); 104551078Speter } 104651078Speter 104751078Speter /* 104851078Speter * We've got something that claims to be a Hayes ESP card. 104951078Speter * Let's hope so. 105051078Speter */ 105151078Speter 105251078Speter /* Get the dip-switch configuration */ 105351078Speter outb(esp_port + ESP_CMD1, ESP_GETDIPS); 105451078Speter dips = inb(esp_port + ESP_STATUS1); 105551078Speter 105651078Speter /* 105751078Speter * Bits 0,1 of dips say which COM port we are. 105851078Speter */ 105960471Snyan if (rman_get_start(com->ioportres) == likely_com_ports[dips & 0x03]) 106051078Speter printf(" : ESP"); 106151078Speter else { 106251078Speter printf(" esp_port has com %d\n", dips & 0x03); 106351078Speter return (0); 106451078Speter } 106551078Speter 106651078Speter /* 106751078Speter * Check for ESP version 2.0 or later: bits 4,5,6 = 010. 106851078Speter */ 106951078Speter outb(esp_port + ESP_CMD1, ESP_GETTEST); 107051078Speter val = inb(esp_port + ESP_STATUS1); /* clear reg 1 */ 107151078Speter val = inb(esp_port + ESP_STATUS2); 107251078Speter if ((val & 0x70) < 0x20) { 107351078Speter printf("-old (%o)", val & 0x70); 107451078Speter return (0); 107551078Speter } 107651078Speter 107751078Speter /* 107851078Speter * Check for ability to emulate 16550: bit 7 == 1 107951078Speter */ 108051078Speter if ((dips & 0x80) == 0) { 108151078Speter printf(" slave"); 108251078Speter return (0); 108351078Speter } 108451078Speter 108551078Speter /* 108651078Speter * Okay, we seem to be a Hayes ESP card. Whee. 108751078Speter */ 108851078Speter com->esp = TRUE; 108951078Speter com->esp_port = esp_port; 109051078Speter return (1); 109151078Speter} 109251078Speter#endif /* COM_ESP */ 109351078Speter 109451078Speterstatic int 109552471Simpsio_isa_attach(dev) 109652471Simp device_t dev; 109752471Simp{ 109858885Simp return (sioattach(dev, 0)); 109952471Simp} 110052471Simp 110152471Simpstatic int 110258885Simpsioattach(dev, xrid) 110351078Speter device_t dev; 110458885Simp int xrid; 110551078Speter{ 110651078Speter struct com_s *com; 110751078Speter#ifdef COM_ESP 110851078Speter Port_t *espp; 110951078Speter#endif 111051078Speter Port_t iobase; 111151078Speter int unit; 111253344Speter u_int flags; 111351078Speter int rid; 111451078Speter struct resource *port; 111553344Speter int ret; 111651078Speter 111758885Simp rid = xrid; 111851078Speter port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 111951078Speter 0, ~0, IO_COMSIZE, RF_ACTIVE); 112051078Speter if (!port) 112157915Simp return (ENXIO); 112251078Speter 112351078Speter iobase = rman_get_start(port); 112451078Speter unit = device_get_unit(dev); 112551078Speter com = device_get_softc(dev); 112653344Speter flags = device_get_flags(dev); 112751078Speter 112853344Speter if (unit >= sio_numunits) 112953344Speter sio_numunits = unit + 1; 113051078Speter /* 113151078Speter * sioprobe() has initialized the device registers as follows: 113251078Speter * o cfcr = CFCR_8BITS. 113351078Speter * It is most important that CFCR_DLAB is off, so that the 113451078Speter * data port is not hidden when we enable interrupts. 113551078Speter * o ier = 0. 113651078Speter * Interrupts are only enabled when the line is open. 113751078Speter * o mcr = MCR_IENABLE, or 0 if the port has AST/4 compatible 113851078Speter * interrupt control register or the config specifies no irq. 113951078Speter * Keeping MCR_DTR and MCR_RTS off might stop the external 114051078Speter * device from sending before we are ready. 114151078Speter */ 114251078Speter bzero(com, sizeof *com); 114351078Speter com->unit = unit; 114451078Speter com->ioportres = port; 114560471Snyan com->bst = rman_get_bustag(port); 114660471Snyan com->bsh = rman_get_bushandle(port); 114751078Speter com->cfcr_image = CFCR_8BITS; 114851078Speter com->dtr_wait = 3 * hz; 114951078Speter com->loses_outints = COM_LOSESOUTINTS(flags) != 0; 115057234Sbde com->no_irq = bus_get_resource(dev, SYS_RES_IRQ, 0, NULL, NULL) != 0; 115151078Speter com->tx_fifo_size = 1; 115251078Speter com->obufs[0].l_head = com->obuf1; 115351078Speter com->obufs[1].l_head = com->obuf2; 115451078Speter 115551078Speter com->data_port = iobase + com_data; 115651078Speter com->int_id_port = iobase + com_iir; 115751078Speter com->modem_ctl_port = iobase + com_mcr; 115851078Speter com->mcr_image = inb(com->modem_ctl_port); 115951078Speter com->line_status_port = iobase + com_lsr; 116051078Speter com->modem_status_port = iobase + com_msr; 116151078Speter com->intr_ctl_port = iobase + com_ier; 116251078Speter 116351078Speter /* 116451078Speter * We don't use all the flags from <sys/ttydefaults.h> since they 116551078Speter * are only relevant for logins. It's important to have echo off 116651078Speter * initially so that the line doesn't start blathering before the 116751078Speter * echo flag can be turned off. 116851078Speter */ 116951078Speter com->it_in.c_iflag = 0; 117051078Speter com->it_in.c_oflag = 0; 117151078Speter com->it_in.c_cflag = TTYDEF_CFLAG; 117251078Speter com->it_in.c_lflag = 0; 117351078Speter if (unit == comconsole) { 117451078Speter com->it_in.c_iflag = TTYDEF_IFLAG; 117551078Speter com->it_in.c_oflag = TTYDEF_OFLAG; 117651078Speter com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL; 117751078Speter com->it_in.c_lflag = TTYDEF_LFLAG; 117851078Speter com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL; 117951078Speter com->lt_out.c_ispeed = com->lt_out.c_ospeed = 118051078Speter com->lt_in.c_ispeed = com->lt_in.c_ospeed = 118151078Speter com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate; 118251078Speter } else 118351078Speter com->it_in.c_ispeed = com->it_in.c_ospeed = TTYDEF_SPEED; 118465596Sjhb if (siosetwater(com, com->it_in.c_ispeed, 0) != 0) { 118556788Sbde /* 118656788Sbde * Leave i/o resources allocated if this is a `cn'-level 118756788Sbde * console, so that other devices can't snarf them. 118856788Sbde */ 118956788Sbde if (iobase != siocniobase) 119056788Sbde bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 119156788Sbde return (ENOMEM); 119251078Speter } 119351078Speter termioschars(&com->it_in); 119451078Speter com->it_out = com->it_in; 119551078Speter 119651078Speter /* attempt to determine UART type */ 119751078Speter printf("sio%d: type", unit); 119851078Speter 119951078Speter 120051078Speter#ifdef COM_MULTIPORT 120151078Speter if (!COM_ISMULTIPORT(flags) && !COM_IIR_TXRDYBUG(flags)) 120251078Speter#else 120351078Speter if (!COM_IIR_TXRDYBUG(flags)) 120451078Speter#endif 120551078Speter { 120651078Speter u_char scr; 120751078Speter u_char scr1; 120851078Speter u_char scr2; 120951078Speter 121060471Snyan scr = sio_getreg(com, com_scr); 121160471Snyan sio_setreg(com, com_scr, 0xa5); 121260471Snyan scr1 = sio_getreg(com, com_scr); 121360471Snyan sio_setreg(com, com_scr, 0x5a); 121460471Snyan scr2 = sio_getreg(com, com_scr); 121560471Snyan sio_setreg(com, com_scr, scr); 121651078Speter if (scr1 != 0xa5 || scr2 != 0x5a) { 121751078Speter printf(" 8250"); 121851078Speter goto determined_type; 121951078Speter } 122051078Speter } 122160471Snyan sio_setreg(com, com_fifo, FIFO_ENABLE | FIFO_RX_HIGH); 122251078Speter DELAY(100); 122351078Speter com->st16650a = 0; 122451078Speter switch (inb(com->int_id_port) & IIR_FIFO_MASK) { 122551078Speter case FIFO_RX_LOW: 122651078Speter printf(" 16450"); 122751078Speter break; 122851078Speter case FIFO_RX_MEDL: 122951078Speter printf(" 16450?"); 123051078Speter break; 123151078Speter case FIFO_RX_MEDH: 123251078Speter printf(" 16550?"); 123351078Speter break; 123451078Speter case FIFO_RX_HIGH: 123551078Speter if (COM_NOFIFO(flags)) { 123651078Speter printf(" 16550A fifo disabled"); 123751078Speter } else { 123851078Speter com->hasfifo = TRUE; 123951078Speter if (COM_ST16650A(flags)) { 124051078Speter com->st16650a = 1; 124151078Speter com->tx_fifo_size = 32; 124251078Speter printf(" ST16650A"); 124351078Speter } else { 124451078Speter com->tx_fifo_size = COM_FIFOSIZE(flags); 124551078Speter printf(" 16550A"); 124651078Speter } 124751078Speter } 124851078Speter#ifdef COM_ESP 124951078Speter for (espp = likely_esp_ports; *espp != 0; espp++) 125051078Speter if (espattach(com, *espp)) { 125151078Speter com->tx_fifo_size = 1024; 125251078Speter break; 125351078Speter } 125451078Speter#endif 125551078Speter if (!com->st16650a) { 125651078Speter if (!com->tx_fifo_size) 125751078Speter com->tx_fifo_size = 16; 125851078Speter else 125951078Speter printf(" lookalike with %d bytes FIFO", 126051078Speter com->tx_fifo_size); 126151078Speter } 126251078Speter 126351078Speter break; 126451078Speter } 126551078Speter 126651078Speter#ifdef COM_ESP 126751078Speter if (com->esp) { 126851078Speter /* 126951078Speter * Set 16550 compatibility mode. 127051078Speter * We don't use the ESP_MODE_SCALE bit to increase the 127151078Speter * fifo trigger levels because we can't handle large 127251078Speter * bursts of input. 127351078Speter * XXX flow control should be set in comparam(), not here. 127451078Speter */ 127551078Speter outb(com->esp_port + ESP_CMD1, ESP_SETMODE); 127651078Speter outb(com->esp_port + ESP_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO); 127751078Speter 127851078Speter /* Set RTS/CTS flow control. */ 127951078Speter outb(com->esp_port + ESP_CMD1, ESP_SETFLOWTYPE); 128051078Speter outb(com->esp_port + ESP_CMD2, ESP_FLOW_RTS); 128151078Speter outb(com->esp_port + ESP_CMD2, ESP_FLOW_CTS); 128251078Speter 128351078Speter /* Set flow-control levels. */ 128451078Speter outb(com->esp_port + ESP_CMD1, ESP_SETRXFLOW); 128551078Speter outb(com->esp_port + ESP_CMD2, HIBYTE(768)); 128651078Speter outb(com->esp_port + ESP_CMD2, LOBYTE(768)); 128751078Speter outb(com->esp_port + ESP_CMD2, HIBYTE(512)); 128851078Speter outb(com->esp_port + ESP_CMD2, LOBYTE(512)); 128951078Speter } 129051078Speter#endif /* COM_ESP */ 129160471Snyan sio_setreg(com, com_fifo, 0); 129251078Speterdetermined_type: ; 129351078Speter 129451078Speter#ifdef COM_MULTIPORT 129551078Speter if (COM_ISMULTIPORT(flags)) { 129653344Speter device_t masterdev; 129753344Speter 129851078Speter com->multiport = TRUE; 129951078Speter printf(" (multiport"); 130051078Speter if (unit == COM_MPMASTER(flags)) 130151078Speter printf(" master"); 130251078Speter printf(")"); 130353344Speter masterdev = devclass_get_device(sio_devclass, 130453344Speter COM_MPMASTER(flags)); 130557234Sbde com->no_irq = (masterdev == NULL || bus_get_resource(masterdev, 130657234Sbde SYS_RES_IRQ, 0, NULL, NULL) != 0); 130751078Speter } 130851078Speter#endif /* COM_MULTIPORT */ 130951078Speter if (unit == comconsole) 131051078Speter printf(", console"); 131153344Speter if (COM_IIR_TXRDYBUG(flags)) 131251078Speter printf(" with a bogus IIR_TXRDY register"); 131351078Speter printf("\n"); 131451078Speter 131551078Speter if (!sio_registered) { 131651078Speter register_swi(SWI_TTY, siopoll); 131751078Speter sio_registered = TRUE; 131851078Speter } 131965131Sphk com->devs[0] = make_dev(&sio_cdevsw, unit, 132051078Speter UID_ROOT, GID_WHEEL, 0600, "ttyd%r", unit); 132165131Sphk com->devs[1] = make_dev(&sio_cdevsw, unit | CONTROL_INIT_STATE, 132251078Speter UID_ROOT, GID_WHEEL, 0600, "ttyid%r", unit); 132365131Sphk com->devs[2] = make_dev(&sio_cdevsw, unit | CONTROL_LOCK_STATE, 132451078Speter UID_ROOT, GID_WHEEL, 0600, "ttyld%r", unit); 132565131Sphk com->devs[3] = make_dev(&sio_cdevsw, unit | CALLOUT_MASK, 132651078Speter UID_UUCP, GID_DIALER, 0660, "cuaa%r", unit); 132765131Sphk com->devs[4] = make_dev(&sio_cdevsw, 132865131Sphk unit | CALLOUT_MASK | CONTROL_INIT_STATE, 132951078Speter UID_UUCP, GID_DIALER, 0660, "cuaia%r", unit); 133065131Sphk com->devs[5] = make_dev(&sio_cdevsw, 133165131Sphk unit | CALLOUT_MASK | CONTROL_LOCK_STATE, 133251078Speter UID_UUCP, GID_DIALER, 0660, "cuala%r", unit); 133351078Speter com->flags = flags; 133451078Speter com->pps.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR; 133551078Speter pps_init(&com->pps); 133651078Speter 133751078Speter rid = 0; 133851078Speter com->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0ul, ~0ul, 1, 133953344Speter RF_ACTIVE); 134053344Speter if (com->irqres) { 134153344Speter ret = BUS_SETUP_INTR(device_get_parent(dev), dev, com->irqres, 134265557Sjasone INTR_TYPE_TTY | INTR_FAST, 134354386Simp siointr, com, &com->cookie); 134454194Speter if (ret) { 134554194Speter ret = BUS_SETUP_INTR(device_get_parent(dev), dev, 134654194Speter com->irqres, INTR_TYPE_TTY, 134754386Simp siointr, com, &com->cookie); 134854194Speter if (ret == 0) 134954194Speter device_printf(dev, "unable to activate interrupt in fast mode - using normal mode"); 135054194Speter } 135153344Speter if (ret) 135253344Speter device_printf(dev, "could not activate interrupt\n"); 135353344Speter } 135451078Speter 135551078Speter return (0); 135651078Speter} 135751078Speter 135851078Speterstatic int 135951078Spetersioopen(dev, flag, mode, p) 136051078Speter dev_t dev; 136151078Speter int flag; 136251078Speter int mode; 136351078Speter struct proc *p; 136451078Speter{ 136551078Speter struct com_s *com; 136651078Speter int error; 136751078Speter int mynor; 136851078Speter int s; 136951078Speter struct tty *tp; 137051078Speter int unit; 137151078Speter 137251078Speter mynor = minor(dev); 137351078Speter unit = MINOR_TO_UNIT(mynor); 137453344Speter com = com_addr(unit); 137553344Speter if (com == NULL) 137651078Speter return (ENXIO); 137751078Speter if (com->gone) 137851078Speter return (ENXIO); 137951078Speter if (mynor & CONTROL_MASK) 138051078Speter return (0); 138151078Speter tp = dev->si_tty = com->tp = ttymalloc(com->tp); 138251078Speter s = spltty(); 138351078Speter /* 138451078Speter * We jump to this label after all non-interrupted sleeps to pick 138551078Speter * up any changes of the device state. 138651078Speter */ 138751078Speteropen_top: 138851078Speter while (com->state & CS_DTR_OFF) { 138951078Speter error = tsleep(&com->dtr_wait, TTIPRI | PCATCH, "siodtr", 0); 139051078Speter if (com_addr(unit) == NULL) 139151078Speter return (ENXIO); 139251078Speter if (error != 0 || com->gone) 139351078Speter goto out; 139451078Speter } 139551078Speter if (tp->t_state & TS_ISOPEN) { 139651078Speter /* 139751078Speter * The device is open, so everything has been initialized. 139851078Speter * Handle conflicts. 139951078Speter */ 140051078Speter if (mynor & CALLOUT_MASK) { 140151078Speter if (!com->active_out) { 140251078Speter error = EBUSY; 140351078Speter goto out; 140451078Speter } 140551078Speter } else { 140651078Speter if (com->active_out) { 140751078Speter if (flag & O_NONBLOCK) { 140851078Speter error = EBUSY; 140951078Speter goto out; 141051078Speter } 141151078Speter error = tsleep(&com->active_out, 141251078Speter TTIPRI | PCATCH, "siobi", 0); 141351078Speter if (com_addr(unit) == NULL) 141451078Speter return (ENXIO); 141551078Speter if (error != 0 || com->gone) 141651078Speter goto out; 141751078Speter goto open_top; 141851078Speter } 141951078Speter } 142051078Speter if (tp->t_state & TS_XCLUDE && 142151078Speter suser(p)) { 142251078Speter error = EBUSY; 142351078Speter goto out; 142451078Speter } 142551078Speter } else { 142665557Sjasone int intrsave; 142765557Sjasone 142851078Speter /* 142951078Speter * The device isn't open, so there are no conflicts. 143051078Speter * Initialize it. Initialization is done twice in many 143151078Speter * cases: to preempt sleeping callin opens if we are 143251078Speter * callout, and to complete a callin open after DCD rises. 143351078Speter */ 143451078Speter tp->t_oproc = comstart; 143551078Speter tp->t_param = comparam; 143651654Sphk tp->t_stop = comstop; 143751078Speter tp->t_dev = dev; 143851078Speter tp->t_termios = mynor & CALLOUT_MASK 143951078Speter ? com->it_out : com->it_in; 144051078Speter (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET); 144151078Speter com->poll = com->no_irq; 144251078Speter com->poll_output = com->loses_outints; 144351078Speter ++com->wopeners; 144451078Speter error = comparam(tp, &tp->t_termios); 144551078Speter --com->wopeners; 144651078Speter if (error != 0) 144751078Speter goto out; 144851078Speter /* 144951078Speter * XXX we should goto open_top if comparam() slept. 145051078Speter */ 145151078Speter if (com->hasfifo) { 145251078Speter /* 145351078Speter * (Re)enable and drain fifos. 145451078Speter * 145551078Speter * Certain SMC chips cause problems if the fifos 145651078Speter * are enabled while input is ready. Turn off the 145751078Speter * fifo if necessary to clear the input. We test 145851078Speter * the input ready bit after enabling the fifos 145951078Speter * since we've already enabled them in comparam() 146051078Speter * and to handle races between enabling and fresh 146151078Speter * input. 146251078Speter */ 146351078Speter while (TRUE) { 146460471Snyan sio_setreg(com, com_fifo, 146560471Snyan FIFO_RCV_RST | FIFO_XMT_RST 146660471Snyan | com->fifo_image); 146751078Speter /* 146851078Speter * XXX the delays are for superstitious 146951078Speter * historical reasons. It must be less than 147051078Speter * the character time at the maximum 147151078Speter * supported speed (87 usec at 115200 bps 147251078Speter * 8N1). Otherwise we might loop endlessly 147351078Speter * if data is streaming in. We used to use 147451078Speter * delays of 100. That usually worked 147551078Speter * because DELAY(100) used to usually delay 147651078Speter * for about 85 usec instead of 100. 147751078Speter */ 147851078Speter DELAY(50); 147951078Speter if (!(inb(com->line_status_port) & LSR_RXRDY)) 148051078Speter break; 148160471Snyan sio_setreg(com, com_fifo, 0); 148251078Speter DELAY(50); 148351078Speter (void) inb(com->data_port); 148451078Speter } 148551078Speter } 148651078Speter 148765557Sjasone intrsave = save_intr(); 148851078Speter disable_intr(); 148965557Sjasone COM_LOCK(); 149051078Speter (void) inb(com->line_status_port); 149151078Speter (void) inb(com->data_port); 149251078Speter com->prev_modem_status = com->last_modem_status 149351078Speter = inb(com->modem_status_port); 149451078Speter if (COM_IIR_TXRDYBUG(com->flags)) { 149551078Speter outb(com->intr_ctl_port, IER_ERXRDY | IER_ERLS 149651078Speter | IER_EMSC); 149751078Speter } else { 149851078Speter outb(com->intr_ctl_port, IER_ERXRDY | IER_ETXRDY 149951078Speter | IER_ERLS | IER_EMSC); 150051078Speter } 150165557Sjasone COM_UNLOCK(); 150265557Sjasone restore_intr(intrsave); 150351078Speter /* 150451078Speter * Handle initial DCD. Callout devices get a fake initial 150551078Speter * DCD (trapdoor DCD). If we are callout, then any sleeping 150651078Speter * callin opens get woken up and resume sleeping on "siobi" 150751078Speter * instead of "siodcd". 150851078Speter */ 150951078Speter /* 151051078Speter * XXX `mynor & CALLOUT_MASK' should be 151151078Speter * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where 151251078Speter * TRAPDOOR_CARRIER is the default initial state for callout 151351078Speter * devices and SOFT_CARRIER is like CLOCAL except it hides 151451078Speter * the true carrier. 151551078Speter */ 151651078Speter if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK) 151751078Speter (*linesw[tp->t_line].l_modem)(tp, 1); 151851078Speter } 151951078Speter /* 152051078Speter * Wait for DCD if necessary. 152151078Speter */ 152251078Speter if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK) 152351078Speter && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) { 152451078Speter ++com->wopeners; 152551078Speter error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "siodcd", 0); 152651078Speter if (com_addr(unit) == NULL) 152751078Speter return (ENXIO); 152851078Speter --com->wopeners; 152951078Speter if (error != 0 || com->gone) 153051078Speter goto out; 153151078Speter goto open_top; 153251078Speter } 153351078Speter error = (*linesw[tp->t_line].l_open)(dev, tp); 153451078Speter disc_optim(tp, &tp->t_termios, com); 153551078Speter if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK) 153651078Speter com->active_out = TRUE; 153751078Speter siosettimeout(); 153851078Speterout: 153951078Speter splx(s); 154051078Speter if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0) 154151078Speter comhardclose(com); 154251078Speter return (error); 154351078Speter} 154451078Speter 154551078Speterstatic int 154651078Spetersioclose(dev, flag, mode, p) 154751078Speter dev_t dev; 154851078Speter int flag; 154951078Speter int mode; 155051078Speter struct proc *p; 155151078Speter{ 155251078Speter struct com_s *com; 155351078Speter int mynor; 155451078Speter int s; 155551078Speter struct tty *tp; 155651078Speter 155751078Speter mynor = minor(dev); 155851078Speter if (mynor & CONTROL_MASK) 155951078Speter return (0); 156051078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 156157915Simp if (com == NULL) 156257915Simp return (ENODEV); 156351078Speter tp = com->tp; 156451078Speter s = spltty(); 156551078Speter (*linesw[tp->t_line].l_close)(tp, flag); 156651078Speter disc_optim(tp, &tp->t_termios, com); 156751654Sphk comstop(tp, FREAD | FWRITE); 156851078Speter comhardclose(com); 156951078Speter ttyclose(tp); 157051078Speter siosettimeout(); 157151078Speter splx(s); 157251078Speter if (com->gone) { 157351078Speter printf("sio%d: gone\n", com->unit); 157451078Speter s = spltty(); 157551078Speter if (com->ibuf != NULL) 157651078Speter free(com->ibuf, M_DEVBUF); 157751078Speter bzero(tp, sizeof *tp); 157851078Speter splx(s); 157951078Speter } 158051078Speter return (0); 158151078Speter} 158251078Speter 158351078Speterstatic void 158451078Spetercomhardclose(com) 158551078Speter struct com_s *com; 158651078Speter{ 158751078Speter int s; 158851078Speter struct tty *tp; 158951078Speter int unit; 159051078Speter 159151078Speter unit = com->unit; 159251078Speter s = spltty(); 159351078Speter com->poll = FALSE; 159451078Speter com->poll_output = FALSE; 159551078Speter com->do_timestamp = FALSE; 159651078Speter com->do_dcd_timestamp = FALSE; 159751078Speter com->pps.ppsparam.mode = 0; 159860471Snyan sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 159951078Speter { 160060471Snyan sio_setreg(com, com_ier, 0); 160151078Speter tp = com->tp; 160251078Speter if (tp->t_cflag & HUPCL 160351078Speter /* 160451078Speter * XXX we will miss any carrier drop between here and the 160551078Speter * next open. Perhaps we should watch DCD even when the 160651078Speter * port is closed; it is not sufficient to check it at 160751078Speter * the next open because it might go up and down while 160851078Speter * we're not watching. 160951078Speter */ 161051078Speter || (!com->active_out 161151078Speter && !(com->prev_modem_status & MSR_DCD) 161251078Speter && !(com->it_in.c_cflag & CLOCAL)) 161351078Speter || !(tp->t_state & TS_ISOPEN)) { 161451078Speter (void)commctl(com, TIOCM_DTR, DMBIC); 161551078Speter if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) { 161651078Speter timeout(siodtrwakeup, com, com->dtr_wait); 161751078Speter com->state |= CS_DTR_OFF; 161851078Speter } 161951078Speter } 162051078Speter } 162151078Speter if (com->hasfifo) { 162251078Speter /* 162351078Speter * Disable fifos so that they are off after controlled 162451078Speter * reboots. Some BIOSes fail to detect 16550s when the 162551078Speter * fifos are enabled. 162651078Speter */ 162760471Snyan sio_setreg(com, com_fifo, 0); 162851078Speter } 162951078Speter com->active_out = FALSE; 163051078Speter wakeup(&com->active_out); 163151078Speter wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */ 163251078Speter splx(s); 163351078Speter} 163451078Speter 163551078Speterstatic int 163651078Spetersioread(dev, uio, flag) 163751078Speter dev_t dev; 163851078Speter struct uio *uio; 163951078Speter int flag; 164051078Speter{ 164151078Speter int mynor; 164251078Speter struct com_s *com; 164351078Speter 164451078Speter mynor = minor(dev); 164551078Speter if (mynor & CONTROL_MASK) 164651078Speter return (ENODEV); 164751078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 164857915Simp if (com == NULL || com->gone) 164951078Speter return (ENODEV); 165051078Speter return ((*linesw[com->tp->t_line].l_read)(com->tp, uio, flag)); 165151078Speter} 165251078Speter 165351078Speterstatic int 165451078Spetersiowrite(dev, uio, flag) 165551078Speter dev_t dev; 165651078Speter struct uio *uio; 165751078Speter int flag; 165851078Speter{ 165951078Speter int mynor; 166051078Speter struct com_s *com; 166151078Speter int unit; 166251078Speter 166351078Speter mynor = minor(dev); 166451078Speter if (mynor & CONTROL_MASK) 166551078Speter return (ENODEV); 166651078Speter 166751078Speter unit = MINOR_TO_UNIT(mynor); 166851078Speter com = com_addr(unit); 166957915Simp if (com == NULL || com->gone) 167051078Speter return (ENODEV); 167151078Speter /* 167251078Speter * (XXX) We disallow virtual consoles if the physical console is 167351078Speter * a serial port. This is in case there is a display attached that 167451078Speter * is not the console. In that situation we don't need/want the X 167551078Speter * server taking over the console. 167651078Speter */ 167751078Speter if (constty != NULL && unit == comconsole) 167851078Speter constty = NULL; 167951078Speter return ((*linesw[com->tp->t_line].l_write)(com->tp, uio, flag)); 168051078Speter} 168151078Speter 168251078Speterstatic void 168351078Spetersiobusycheck(chan) 168451078Speter void *chan; 168551078Speter{ 168651078Speter struct com_s *com; 168751078Speter int s; 168851078Speter 168951078Speter com = (struct com_s *)chan; 169051078Speter 169151078Speter /* 169251078Speter * Clear TS_BUSY if low-level output is complete. 169351078Speter * spl locking is sufficient because siointr1() does not set CS_BUSY. 169451078Speter * If siointr1() clears CS_BUSY after we look at it, then we'll get 169551078Speter * called again. Reading the line status port outside of siointr1() 169651078Speter * is safe because CS_BUSY is clear so there are no output interrupts 169751078Speter * to lose. 169851078Speter */ 169951078Speter s = spltty(); 170051078Speter if (com->state & CS_BUSY) 170151078Speter com->extra_state &= ~CSE_BUSYCHECK; /* False alarm. */ 170251078Speter else if ((inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY)) 170351078Speter == (LSR_TSRE | LSR_TXRDY)) { 170451078Speter com->tp->t_state &= ~TS_BUSY; 170551078Speter ttwwakeup(com->tp); 170651078Speter com->extra_state &= ~CSE_BUSYCHECK; 170751078Speter } else 170851078Speter timeout(siobusycheck, com, hz / 100); 170951078Speter splx(s); 171051078Speter} 171151078Speter 171251078Speterstatic void 171351078Spetersiodtrwakeup(chan) 171451078Speter void *chan; 171551078Speter{ 171651078Speter struct com_s *com; 171751078Speter 171851078Speter com = (struct com_s *)chan; 171951078Speter com->state &= ~CS_DTR_OFF; 172051078Speter wakeup(&com->dtr_wait); 172151078Speter} 172251078Speter 172365557Sjasone/* 172465557Sjasone * Call this function with COM_LOCK. It will return with the lock still held. 172565557Sjasone */ 172651078Speterstatic void 172751078Spetersioinput(com) 172851078Speter struct com_s *com; 172951078Speter{ 173051078Speter u_char *buf; 173151078Speter int incc; 173251078Speter u_char line_status; 173351078Speter int recv_data; 173451078Speter struct tty *tp; 173565557Sjasone int intrsave; 173651078Speter 173751078Speter buf = com->ibuf; 173851078Speter tp = com->tp; 173951078Speter if (!(tp->t_state & TS_ISOPEN) || !(tp->t_cflag & CREAD)) { 174051078Speter com_events -= (com->iptr - com->ibuf); 174151078Speter com->iptr = com->ibuf; 174251078Speter return; 174351078Speter } 174451078Speter if (tp->t_state & TS_CAN_BYPASS_L_RINT) { 174551078Speter /* 174651078Speter * Avoid the grotesquely inefficient lineswitch routine 174751078Speter * (ttyinput) in "raw" mode. It usually takes about 450 174851078Speter * instructions (that's without canonical processing or echo!). 174951078Speter * slinput is reasonably fast (usually 40 instructions plus 175051078Speter * call overhead). 175151078Speter */ 175251078Speter do { 175365557Sjasone /* 175465557Sjasone * This may look odd, but it is using save-and-enable 175565557Sjasone * semantics instead of the save-and-disable semantics 175665557Sjasone * that are used everywhere else. 175765557Sjasone */ 175865557Sjasone intrsave = save_intr(); 175965557Sjasone COM_UNLOCK(); 176051078Speter enable_intr(); 176151078Speter incc = com->iptr - buf; 176251078Speter if (tp->t_rawq.c_cc + incc > tp->t_ihiwat 176351078Speter && (com->state & CS_RTS_IFLOW 176451078Speter || tp->t_iflag & IXOFF) 176551078Speter && !(tp->t_state & TS_TBLOCK)) 176651078Speter ttyblock(tp); 176751078Speter com->delta_error_counts[CE_TTY_BUF_OVERFLOW] 176851078Speter += b_to_q((char *)buf, incc, &tp->t_rawq); 176951078Speter buf += incc; 177051078Speter tk_nin += incc; 177151078Speter tk_rawcc += incc; 177251078Speter tp->t_rawcc += incc; 177351078Speter ttwakeup(tp); 177451078Speter if (tp->t_state & TS_TTSTOP 177551078Speter && (tp->t_iflag & IXANY 177651078Speter || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) { 177751078Speter tp->t_state &= ~TS_TTSTOP; 177851078Speter tp->t_lflag &= ~FLUSHO; 177951078Speter comstart(tp); 178051078Speter } 178165557Sjasone restore_intr(intrsave); 178265557Sjasone COM_LOCK(); 178351078Speter } while (buf < com->iptr); 178451078Speter } else { 178551078Speter do { 178665557Sjasone /* 178765557Sjasone * This may look odd, but it is using save-and-enable 178865557Sjasone * semantics instead of the save-and-disable semantics 178965557Sjasone * that are used everywhere else. 179065557Sjasone */ 179165557Sjasone intrsave = save_intr(); 179265557Sjasone COM_UNLOCK(); 179351078Speter enable_intr(); 179451078Speter line_status = buf[com->ierroff]; 179551078Speter recv_data = *buf++; 179651078Speter if (line_status 179751078Speter & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) { 179851078Speter if (line_status & LSR_BI) 179951078Speter recv_data |= TTY_BI; 180051078Speter if (line_status & LSR_FE) 180151078Speter recv_data |= TTY_FE; 180251078Speter if (line_status & LSR_OE) 180351078Speter recv_data |= TTY_OE; 180451078Speter if (line_status & LSR_PE) 180551078Speter recv_data |= TTY_PE; 180651078Speter } 180751078Speter (*linesw[tp->t_line].l_rint)(recv_data, tp); 180865557Sjasone restore_intr(intrsave); 180965557Sjasone COM_LOCK(); 181051078Speter } while (buf < com->iptr); 181151078Speter } 181251078Speter com_events -= (com->iptr - com->ibuf); 181351078Speter com->iptr = com->ibuf; 181451078Speter 181551078Speter /* 181651078Speter * There is now room for another low-level buffer full of input, 181751078Speter * so enable RTS if it is now disabled and there is room in the 181851078Speter * high-level buffer. 181951078Speter */ 182051078Speter if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & MCR_RTS) && 182151078Speter !(tp->t_state & TS_TBLOCK)) 182251078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 182351078Speter} 182451078Speter 182551078Spetervoid 182651078Spetersiointr(arg) 182751078Speter void *arg; 182851078Speter{ 182951078Speter#ifndef COM_MULTIPORT 183051078Speter COM_LOCK(); 183151078Speter siointr1((struct com_s *) arg); 183251078Speter COM_UNLOCK(); 183351078Speter#else /* COM_MULTIPORT */ 183451078Speter bool_t possibly_more_intrs; 183551078Speter int unit; 183651078Speter struct com_s *com; 183751078Speter 183851078Speter /* 183951078Speter * Loop until there is no activity on any port. This is necessary 184051078Speter * to get an interrupt edge more than to avoid another interrupt. 184151078Speter * If the IRQ signal is just an OR of the IRQ signals from several 184251078Speter * devices, then the edge from one may be lost because another is 184351078Speter * on. 184451078Speter */ 184551078Speter COM_LOCK(); 184651078Speter do { 184751078Speter possibly_more_intrs = FALSE; 184853344Speter for (unit = 0; unit < sio_numunits; ++unit) { 184951078Speter com = com_addr(unit); 185051078Speter /* 185151078Speter * XXX COM_LOCK(); 185251078Speter * would it work here, or be counter-productive? 185351078Speter */ 185451078Speter if (com != NULL 185551078Speter && !com->gone 185651078Speter && (inb(com->int_id_port) & IIR_IMASK) 185751078Speter != IIR_NOPEND) { 185851078Speter siointr1(com); 185951078Speter possibly_more_intrs = TRUE; 186051078Speter } 186151078Speter /* XXX COM_UNLOCK(); */ 186251078Speter } 186351078Speter } while (possibly_more_intrs); 186451078Speter COM_UNLOCK(); 186551078Speter#endif /* COM_MULTIPORT */ 186651078Speter} 186751078Speter 186851078Speterstatic void 186951078Spetersiointr1(com) 187051078Speter struct com_s *com; 187151078Speter{ 187251078Speter u_char line_status; 187351078Speter u_char modem_status; 187451078Speter u_char *ioptr; 187551078Speter u_char recv_data; 187651078Speter u_char int_ctl; 187751078Speter u_char int_ctl_new; 187851078Speter struct timecounter *tc; 187951078Speter u_int count; 188051078Speter 188151078Speter int_ctl = inb(com->intr_ctl_port); 188251078Speter int_ctl_new = int_ctl; 188351078Speter 188451078Speter while (!com->gone) { 188551078Speter if (com->pps.ppsparam.mode & PPS_CAPTUREBOTH) { 188651078Speter modem_status = inb(com->modem_status_port); 188751078Speter if ((modem_status ^ com->last_modem_status) & MSR_DCD) { 188851078Speter tc = timecounter; 188951078Speter count = tc->tc_get_timecount(tc); 189051078Speter pps_event(&com->pps, tc, count, 189151078Speter (modem_status & MSR_DCD) ? 189251078Speter PPS_CAPTUREASSERT : PPS_CAPTURECLEAR); 189351078Speter } 189451078Speter } 189551078Speter line_status = inb(com->line_status_port); 189651078Speter 189751078Speter /* input event? (check first to help avoid overruns) */ 189851078Speter while (line_status & LSR_RCV_MASK) { 189951078Speter /* break/unnattached error bits or real input? */ 190051078Speter if (!(line_status & LSR_RXRDY)) 190151078Speter recv_data = 0; 190251078Speter else 190351078Speter recv_data = inb(com->data_port); 190461649Sps#if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER) 190561649Sps /* 190661649Sps * Solaris implements a new BREAK which is initiated 190761649Sps * by a character sequence CR ~ ^b which is similar 190861649Sps * to a familiar pattern used on Sun servers by the 190961649Sps * Remote Console. 191061649Sps */ 191161649Sps#define KEY_CRTLB 2 /* ^B */ 191261649Sps#define KEY_CR 13 /* CR '\r' */ 191361649Sps#define KEY_TILDE 126 /* ~ */ 191461649Sps 191561649Sps if (com->unit == comconsole) { 191661649Sps static int brk_state1 = 0, brk_state2 = 0; 191761649Sps if (recv_data == KEY_CR) { 191861649Sps brk_state1 = recv_data; 191961649Sps brk_state2 = 0; 192065557Sjasone } else if (brk_state1 == KEY_CR 192165557Sjasone && (recv_data == KEY_TILDE 192265557Sjasone || recv_data == KEY_CRTLB)) { 192361649Sps if (recv_data == KEY_TILDE) 192461649Sps brk_state2 = recv_data; 192565557Sjasone else if (brk_state2 == KEY_TILDE 192665557Sjasone && recv_data == KEY_CRTLB) { 192761649Sps breakpoint(); 192865557Sjasone brk_state1 = 0; 192965557Sjasone brk_state2 = 0; 193061649Sps goto cont; 193161649Sps } else 193261649Sps brk_state2 = 0; 193361649Sps } else 193461649Sps brk_state1 = 0; 193561649Sps } 193661649Sps#endif 193751078Speter if (line_status & (LSR_BI | LSR_FE | LSR_PE)) { 193851078Speter /* 193951078Speter * Don't store BI if IGNBRK or FE/PE if IGNPAR. 194051078Speter * Otherwise, push the work to a higher level 194151078Speter * (to handle PARMRK) if we're bypassing. 194251078Speter * Otherwise, convert BI/FE and PE+INPCK to 0. 194351078Speter * 194451078Speter * This makes bypassing work right in the 194551078Speter * usual "raw" case (IGNBRK set, and IGNPAR 194651078Speter * and INPCK clear). 194751078Speter * 194851078Speter * Note: BI together with FE/PE means just BI. 194951078Speter */ 195051078Speter if (line_status & LSR_BI) { 195151078Speter#if defined(DDB) && defined(BREAK_TO_DEBUGGER) 195251078Speter if (com->unit == comconsole) { 195351078Speter breakpoint(); 195451078Speter goto cont; 195551078Speter } 195651078Speter#endif 195751078Speter if (com->tp == NULL 195851078Speter || com->tp->t_iflag & IGNBRK) 195951078Speter goto cont; 196051078Speter } else { 196151078Speter if (com->tp == NULL 196251078Speter || com->tp->t_iflag & IGNPAR) 196351078Speter goto cont; 196451078Speter } 196551078Speter if (com->tp->t_state & TS_CAN_BYPASS_L_RINT 196651078Speter && (line_status & (LSR_BI | LSR_FE) 196751078Speter || com->tp->t_iflag & INPCK)) 196851078Speter recv_data = 0; 196951078Speter } 197051078Speter ++com->bytes_in; 197151078Speter if (com->hotchar != 0 && recv_data == com->hotchar) 197251078Speter setsofttty(); 197351078Speter ioptr = com->iptr; 197451078Speter if (ioptr >= com->ibufend) 197551078Speter CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW); 197651078Speter else { 197751078Speter if (com->do_timestamp) 197851078Speter microtime(&com->timestamp); 197951078Speter ++com_events; 198065557Sjasone/* XXX - needs to go away when alpha gets ithreads */ 198165557Sjasone#ifdef __alpha__ 198251078Speter schedsofttty(); 198365602Sgrog#else 198465602Sgrog setsofttty(); 198565557Sjasone#endif 198651078Speter#if 0 /* for testing input latency vs efficiency */ 198751078Speterif (com->iptr - com->ibuf == 8) 198851078Speter setsofttty(); 198951078Speter#endif 199051078Speter ioptr[0] = recv_data; 199151078Speter ioptr[com->ierroff] = line_status; 199251078Speter com->iptr = ++ioptr; 199351078Speter if (ioptr == com->ihighwater 199451078Speter && com->state & CS_RTS_IFLOW) 199551078Speter outb(com->modem_ctl_port, 199651078Speter com->mcr_image &= ~MCR_RTS); 199751078Speter if (line_status & LSR_OE) 199851078Speter CE_RECORD(com, CE_OVERRUN); 199951078Speter } 200051078Spetercont: 200151078Speter /* 200251078Speter * "& 0x7F" is to avoid the gcc-1.40 generating a slow 200351078Speter * jump from the top of the loop to here 200451078Speter */ 200551078Speter line_status = inb(com->line_status_port) & 0x7F; 200651078Speter } 200751078Speter 200851078Speter /* modem status change? (always check before doing output) */ 200951078Speter modem_status = inb(com->modem_status_port); 201051078Speter if (modem_status != com->last_modem_status) { 201151078Speter if (com->do_dcd_timestamp 201251078Speter && !(com->last_modem_status & MSR_DCD) 201351078Speter && modem_status & MSR_DCD) 201451078Speter microtime(&com->dcd_timestamp); 201551078Speter 201651078Speter /* 201751078Speter * Schedule high level to handle DCD changes. Note 201851078Speter * that we don't use the delta bits anywhere. Some 201951078Speter * UARTs mess them up, and it's easy to remember the 202051078Speter * previous bits and calculate the delta. 202151078Speter */ 202251078Speter com->last_modem_status = modem_status; 202351078Speter if (!(com->state & CS_CHECKMSR)) { 202451078Speter com_events += LOTS_OF_EVENTS; 202551078Speter com->state |= CS_CHECKMSR; 202651078Speter setsofttty(); 202751078Speter } 202851078Speter 202951078Speter /* handle CTS change immediately for crisp flow ctl */ 203051078Speter if (com->state & CS_CTS_OFLOW) { 203151078Speter if (modem_status & MSR_CTS) 203251078Speter com->state |= CS_ODEVREADY; 203351078Speter else 203451078Speter com->state &= ~CS_ODEVREADY; 203551078Speter } 203651078Speter } 203751078Speter 203851078Speter /* output queued and everything ready? */ 203951078Speter if (line_status & LSR_TXRDY 204051078Speter && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) { 204151078Speter ioptr = com->obufq.l_head; 204251078Speter if (com->tx_fifo_size > 1) { 204351078Speter u_int ocount; 204451078Speter 204551078Speter ocount = com->obufq.l_tail - ioptr; 204651078Speter if (ocount > com->tx_fifo_size) 204751078Speter ocount = com->tx_fifo_size; 204851078Speter com->bytes_out += ocount; 204951078Speter do 205051078Speter outb(com->data_port, *ioptr++); 205151078Speter while (--ocount != 0); 205251078Speter } else { 205351078Speter outb(com->data_port, *ioptr++); 205451078Speter ++com->bytes_out; 205551078Speter } 205651078Speter com->obufq.l_head = ioptr; 205751078Speter if (COM_IIR_TXRDYBUG(com->flags)) { 205851078Speter int_ctl_new = int_ctl | IER_ETXRDY; 205951078Speter } 206051078Speter if (ioptr >= com->obufq.l_tail) { 206151078Speter struct lbq *qp; 206251078Speter 206351078Speter qp = com->obufq.l_next; 206451078Speter qp->l_queued = FALSE; 206551078Speter qp = qp->l_next; 206651078Speter if (qp != NULL) { 206751078Speter com->obufq.l_head = qp->l_head; 206851078Speter com->obufq.l_tail = qp->l_tail; 206951078Speter com->obufq.l_next = qp; 207051078Speter } else { 207151078Speter /* output just completed */ 207253344Speter if (COM_IIR_TXRDYBUG(com->flags)) { 207351078Speter int_ctl_new = int_ctl & ~IER_ETXRDY; 207451078Speter } 207551078Speter com->state &= ~CS_BUSY; 207651078Speter } 207751078Speter if (!(com->state & CS_ODONE)) { 207851078Speter com_events += LOTS_OF_EVENTS; 207951078Speter com->state |= CS_ODONE; 208051078Speter setsofttty(); /* handle at high level ASAP */ 208151078Speter } 208251078Speter } 208353344Speter if (COM_IIR_TXRDYBUG(com->flags) && (int_ctl != int_ctl_new)) { 208451078Speter outb(com->intr_ctl_port, int_ctl_new); 208551078Speter } 208651078Speter } 208751078Speter 208851078Speter /* finished? */ 208951078Speter#ifndef COM_MULTIPORT 209051078Speter if ((inb(com->int_id_port) & IIR_IMASK) == IIR_NOPEND) 209151078Speter#endif /* COM_MULTIPORT */ 209251078Speter return; 209351078Speter } 209451078Speter} 209551078Speter 209651078Speterstatic int 209751078Spetersioioctl(dev, cmd, data, flag, p) 209851078Speter dev_t dev; 209951078Speter u_long cmd; 210051078Speter caddr_t data; 210151078Speter int flag; 210251078Speter struct proc *p; 210351078Speter{ 210451078Speter struct com_s *com; 210551078Speter int error; 210651078Speter int mynor; 210751078Speter int s; 210851078Speter struct tty *tp; 210951078Speter#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 211051078Speter u_long oldcmd; 211151078Speter struct termios term; 211251078Speter#endif 211351078Speter 211451078Speter mynor = minor(dev); 211551078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 211657915Simp if (com == NULL || com->gone) 211751078Speter return (ENODEV); 211851078Speter if (mynor & CONTROL_MASK) { 211951078Speter struct termios *ct; 212051078Speter 212151078Speter switch (mynor & CONTROL_MASK) { 212251078Speter case CONTROL_INIT_STATE: 212351078Speter ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in; 212451078Speter break; 212551078Speter case CONTROL_LOCK_STATE: 212651078Speter ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in; 212751078Speter break; 212851078Speter default: 212951078Speter return (ENODEV); /* /dev/nodev */ 213051078Speter } 213151078Speter switch (cmd) { 213251078Speter case TIOCSETA: 213351078Speter error = suser(p); 213451078Speter if (error != 0) 213551078Speter return (error); 213651078Speter *ct = *(struct termios *)data; 213751078Speter return (0); 213851078Speter case TIOCGETA: 213951078Speter *(struct termios *)data = *ct; 214051078Speter return (0); 214151078Speter case TIOCGETD: 214251078Speter *(int *)data = TTYDISC; 214351078Speter return (0); 214451078Speter case TIOCGWINSZ: 214551078Speter bzero(data, sizeof(struct winsize)); 214651078Speter return (0); 214751078Speter default: 214851078Speter return (ENOTTY); 214951078Speter } 215051078Speter } 215151078Speter tp = com->tp; 215251078Speter#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 215351078Speter term = tp->t_termios; 215451078Speter oldcmd = cmd; 215551078Speter error = ttsetcompat(tp, &cmd, data, &term); 215651078Speter if (error != 0) 215751078Speter return (error); 215851078Speter if (cmd != oldcmd) 215951078Speter data = (caddr_t)&term; 216051078Speter#endif 216151078Speter if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) { 216251078Speter int cc; 216351078Speter struct termios *dt = (struct termios *)data; 216451078Speter struct termios *lt = mynor & CALLOUT_MASK 216551078Speter ? &com->lt_out : &com->lt_in; 216651078Speter 216751078Speter dt->c_iflag = (tp->t_iflag & lt->c_iflag) 216851078Speter | (dt->c_iflag & ~lt->c_iflag); 216951078Speter dt->c_oflag = (tp->t_oflag & lt->c_oflag) 217051078Speter | (dt->c_oflag & ~lt->c_oflag); 217151078Speter dt->c_cflag = (tp->t_cflag & lt->c_cflag) 217251078Speter | (dt->c_cflag & ~lt->c_cflag); 217351078Speter dt->c_lflag = (tp->t_lflag & lt->c_lflag) 217451078Speter | (dt->c_lflag & ~lt->c_lflag); 217551078Speter for (cc = 0; cc < NCCS; ++cc) 217651078Speter if (lt->c_cc[cc] != 0) 217751078Speter dt->c_cc[cc] = tp->t_cc[cc]; 217851078Speter if (lt->c_ispeed != 0) 217951078Speter dt->c_ispeed = tp->t_ispeed; 218051078Speter if (lt->c_ospeed != 0) 218151078Speter dt->c_ospeed = tp->t_ospeed; 218251078Speter } 218351078Speter error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 218451078Speter if (error != ENOIOCTL) 218551078Speter return (error); 218651078Speter s = spltty(); 218751078Speter error = ttioctl(tp, cmd, data, flag); 218851078Speter disc_optim(tp, &tp->t_termios, com); 218951078Speter if (error != ENOIOCTL) { 219051078Speter splx(s); 219151078Speter return (error); 219251078Speter } 219351078Speter switch (cmd) { 219451078Speter case TIOCSBRK: 219560471Snyan sio_setreg(com, com_cfcr, com->cfcr_image |= CFCR_SBREAK); 219651078Speter break; 219751078Speter case TIOCCBRK: 219860471Snyan sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 219951078Speter break; 220051078Speter case TIOCSDTR: 220151078Speter (void)commctl(com, TIOCM_DTR, DMBIS); 220251078Speter break; 220351078Speter case TIOCCDTR: 220451078Speter (void)commctl(com, TIOCM_DTR, DMBIC); 220551078Speter break; 220651078Speter /* 220751078Speter * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The 220851078Speter * changes get undone on the next call to comparam(). 220951078Speter */ 221051078Speter case TIOCMSET: 221151078Speter (void)commctl(com, *(int *)data, DMSET); 221251078Speter break; 221351078Speter case TIOCMBIS: 221451078Speter (void)commctl(com, *(int *)data, DMBIS); 221551078Speter break; 221651078Speter case TIOCMBIC: 221751078Speter (void)commctl(com, *(int *)data, DMBIC); 221851078Speter break; 221951078Speter case TIOCMGET: 222051078Speter *(int *)data = commctl(com, 0, DMGET); 222151078Speter break; 222251078Speter case TIOCMSDTRWAIT: 222351078Speter /* must be root since the wait applies to following logins */ 222451078Speter error = suser(p); 222551078Speter if (error != 0) { 222651078Speter splx(s); 222751078Speter return (error); 222851078Speter } 222951078Speter com->dtr_wait = *(int *)data * hz / 100; 223051078Speter break; 223151078Speter case TIOCMGDTRWAIT: 223251078Speter *(int *)data = com->dtr_wait * 100 / hz; 223351078Speter break; 223451078Speter case TIOCTIMESTAMP: 223551078Speter com->do_timestamp = TRUE; 223651078Speter *(struct timeval *)data = com->timestamp; 223751078Speter break; 223851078Speter case TIOCDCDTIMESTAMP: 223951078Speter com->do_dcd_timestamp = TRUE; 224051078Speter *(struct timeval *)data = com->dcd_timestamp; 224151078Speter break; 224251078Speter default: 224351078Speter splx(s); 224451078Speter error = pps_ioctl(cmd, data, &com->pps); 224551078Speter if (error == ENODEV) 224651078Speter error = ENOTTY; 224751078Speter return (error); 224851078Speter } 224951078Speter splx(s); 225051078Speter return (0); 225151078Speter} 225251078Speter 225365557Sjasone/* software interrupt handler for SWI_TTY */ 225451078Speterstatic void 225551078Spetersiopoll() 225651078Speter{ 225751078Speter int unit; 225865557Sjasone int intrsave; 225951078Speter 226051078Speter if (com_events == 0) 226151078Speter return; 226251078Speterrepeat: 226353344Speter for (unit = 0; unit < sio_numunits; ++unit) { 226451078Speter struct com_s *com; 226551078Speter int incc; 226651078Speter struct tty *tp; 226751078Speter 226851078Speter com = com_addr(unit); 226951078Speter if (com == NULL) 227051078Speter continue; 227151078Speter tp = com->tp; 227251078Speter if (tp == NULL || com->gone) { 227351078Speter /* 227451078Speter * Discard any events related to never-opened or 227551078Speter * going-away devices. 227651078Speter */ 227765557Sjasone intrsave = save_intr(); 227851078Speter disable_intr(); 227965557Sjasone COM_LOCK(); 228051078Speter incc = com->iptr - com->ibuf; 228151078Speter com->iptr = com->ibuf; 228251078Speter if (com->state & CS_CHECKMSR) { 228351078Speter incc += LOTS_OF_EVENTS; 228451078Speter com->state &= ~CS_CHECKMSR; 228551078Speter } 228651078Speter com_events -= incc; 228765557Sjasone COM_UNLOCK(); 228865557Sjasone restore_intr(intrsave); 228951078Speter continue; 229051078Speter } 229151078Speter if (com->iptr != com->ibuf) { 229265557Sjasone intrsave = save_intr(); 229351078Speter disable_intr(); 229465557Sjasone COM_LOCK(); 229551078Speter sioinput(com); 229665557Sjasone COM_UNLOCK(); 229765557Sjasone restore_intr(intrsave); 229851078Speter } 229951078Speter if (com->state & CS_CHECKMSR) { 230051078Speter u_char delta_modem_status; 230151078Speter 230265557Sjasone intrsave = save_intr(); 230351078Speter disable_intr(); 230465557Sjasone COM_LOCK(); 230551078Speter delta_modem_status = com->last_modem_status 230651078Speter ^ com->prev_modem_status; 230751078Speter com->prev_modem_status = com->last_modem_status; 230851078Speter com_events -= LOTS_OF_EVENTS; 230951078Speter com->state &= ~CS_CHECKMSR; 231065557Sjasone COM_UNLOCK(); 231165557Sjasone restore_intr(intrsave); 231251078Speter if (delta_modem_status & MSR_DCD) 231351078Speter (*linesw[tp->t_line].l_modem) 231451078Speter (tp, com->prev_modem_status & MSR_DCD); 231551078Speter } 231651078Speter if (com->state & CS_ODONE) { 231765557Sjasone intrsave = save_intr(); 231851078Speter disable_intr(); 231965557Sjasone COM_LOCK(); 232051078Speter com_events -= LOTS_OF_EVENTS; 232151078Speter com->state &= ~CS_ODONE; 232265557Sjasone COM_UNLOCK(); 232365557Sjasone restore_intr(intrsave); 232451078Speter if (!(com->state & CS_BUSY) 232551078Speter && !(com->extra_state & CSE_BUSYCHECK)) { 232651078Speter timeout(siobusycheck, com, hz / 100); 232751078Speter com->extra_state |= CSE_BUSYCHECK; 232851078Speter } 232951078Speter (*linesw[tp->t_line].l_start)(tp); 233051078Speter } 233151078Speter if (com_events == 0) 233251078Speter break; 233351078Speter } 233451078Speter if (com_events >= LOTS_OF_EVENTS) 233551078Speter goto repeat; 233651078Speter} 233751078Speter 233851078Speterstatic int 233951078Spetercomparam(tp, t) 234051078Speter struct tty *tp; 234151078Speter struct termios *t; 234251078Speter{ 234351078Speter u_int cfcr; 234451078Speter int cflag; 234551078Speter struct com_s *com; 234651078Speter int divisor; 234751078Speter u_char dlbh; 234851078Speter u_char dlbl; 234951078Speter int s; 235051078Speter int unit; 235165557Sjasone int intrsave; 235251078Speter 235351078Speter /* do historical conversions */ 235451078Speter if (t->c_ispeed == 0) 235551078Speter t->c_ispeed = t->c_ospeed; 235651078Speter 235751078Speter /* check requested parameters */ 235851078Speter divisor = ttspeedtab(t->c_ospeed, comspeedtab); 235951078Speter if (divisor < 0 || (divisor > 0 && t->c_ispeed != t->c_ospeed)) 236051078Speter return (EINVAL); 236151078Speter 236251078Speter /* parameters are OK, convert them to the com struct and the device */ 236351078Speter unit = DEV_TO_UNIT(tp->t_dev); 236451078Speter com = com_addr(unit); 236557915Simp if (com == NULL) 236657915Simp return (ENODEV); 236751078Speter s = spltty(); 236851078Speter if (divisor == 0) 236951078Speter (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */ 237051078Speter else 237151078Speter (void)commctl(com, TIOCM_DTR, DMBIS); 237251078Speter cflag = t->c_cflag; 237351078Speter switch (cflag & CSIZE) { 237451078Speter case CS5: 237551078Speter cfcr = CFCR_5BITS; 237651078Speter break; 237751078Speter case CS6: 237851078Speter cfcr = CFCR_6BITS; 237951078Speter break; 238051078Speter case CS7: 238151078Speter cfcr = CFCR_7BITS; 238251078Speter break; 238351078Speter default: 238451078Speter cfcr = CFCR_8BITS; 238551078Speter break; 238651078Speter } 238751078Speter if (cflag & PARENB) { 238851078Speter cfcr |= CFCR_PENAB; 238951078Speter if (!(cflag & PARODD)) 239051078Speter cfcr |= CFCR_PEVEN; 239151078Speter } 239251078Speter if (cflag & CSTOPB) 239351078Speter cfcr |= CFCR_STOPB; 239451078Speter 239551078Speter if (com->hasfifo && divisor != 0) { 239651078Speter /* 239751078Speter * Use a fifo trigger level low enough so that the input 239851078Speter * latency from the fifo is less than about 16 msec and 239951078Speter * the total latency is less than about 30 msec. These 240051078Speter * latencies are reasonable for humans. Serial comms 240151078Speter * protocols shouldn't expect anything better since modem 240251078Speter * latencies are larger. 240351078Speter */ 240451078Speter com->fifo_image = t->c_ospeed <= 4800 240551078Speter ? FIFO_ENABLE : FIFO_ENABLE | FIFO_RX_HIGH; 240651078Speter#ifdef COM_ESP 240751078Speter /* 240851078Speter * The Hayes ESP card needs the fifo DMA mode bit set 240951078Speter * in compatibility mode. If not, it will interrupt 241051078Speter * for each character received. 241151078Speter */ 241251078Speter if (com->esp) 241351078Speter com->fifo_image |= FIFO_DMA_MODE; 241451078Speter#endif 241560471Snyan sio_setreg(com, com_fifo, com->fifo_image); 241651078Speter } 241751078Speter 241865557Sjasone intrsave = save_intr(); 241965557Sjasone disable_intr(); 242065557Sjasone COM_LOCK(); 242165557Sjasone 242265596Sjhb (void) siosetwater(com, t->c_ispeed, 1); 242351078Speter 242451078Speter if (divisor != 0) { 242560471Snyan sio_setreg(com, com_cfcr, cfcr | CFCR_DLAB); 242651078Speter /* 242751078Speter * Only set the divisor registers if they would change, 242851078Speter * since on some 16550 incompatibles (UMC8669F), setting 242951078Speter * them while input is arriving them loses sync until 243051078Speter * data stops arriving. 243151078Speter */ 243251078Speter dlbl = divisor & 0xFF; 243360471Snyan if (sio_getreg(com, com_dlbl) != dlbl) 243460471Snyan sio_setreg(com, com_dlbl, dlbl); 243551078Speter dlbh = (u_int) divisor >> 8; 243660471Snyan if (sio_getreg(com, com_dlbh) != dlbh) 243760471Snyan sio_setreg(com, com_dlbh, dlbh); 243851078Speter } 243951078Speter 244060471Snyan sio_setreg(com, com_cfcr, com->cfcr_image = cfcr); 244151078Speter 244251078Speter if (!(tp->t_state & TS_TTSTOP)) 244351078Speter com->state |= CS_TTGO; 244451078Speter 244551078Speter if (cflag & CRTS_IFLOW) { 244651078Speter if (com->st16650a) { 244760471Snyan sio_setreg(com, com_cfcr, 0xbf); 244860471Snyan sio_setreg(com, com_fifo, 244960471Snyan sio_getreg(com, com_fifo) | 0x40); 245051078Speter } 245151078Speter com->state |= CS_RTS_IFLOW; 245251078Speter /* 245351078Speter * If CS_RTS_IFLOW just changed from off to on, the change 245451078Speter * needs to be propagated to MCR_RTS. This isn't urgent, 245551078Speter * so do it later by calling comstart() instead of repeating 245651078Speter * a lot of code from comstart() here. 245751078Speter */ 245851078Speter } else if (com->state & CS_RTS_IFLOW) { 245951078Speter com->state &= ~CS_RTS_IFLOW; 246051078Speter /* 246151078Speter * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS 246251078Speter * on here, since comstart() won't do it later. 246351078Speter */ 246451078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 246551078Speter if (com->st16650a) { 246660471Snyan sio_setreg(com, com_cfcr, 0xbf); 246760471Snyan sio_setreg(com, com_fifo, 246860471Snyan sio_getreg(com, com_fifo) & ~0x40); 246951078Speter } 247051078Speter } 247151078Speter 247251078Speter 247351078Speter /* 247451078Speter * Set up state to handle output flow control. 247551078Speter * XXX - worth handling MDMBUF (DCD) flow control at the lowest level? 247651078Speter * Now has 10+ msec latency, while CTS flow has 50- usec latency. 247751078Speter */ 247851078Speter com->state |= CS_ODEVREADY; 247951078Speter com->state &= ~CS_CTS_OFLOW; 248051078Speter if (cflag & CCTS_OFLOW) { 248151078Speter com->state |= CS_CTS_OFLOW; 248251078Speter if (!(com->last_modem_status & MSR_CTS)) 248351078Speter com->state &= ~CS_ODEVREADY; 248451078Speter if (com->st16650a) { 248560471Snyan sio_setreg(com, com_cfcr, 0xbf); 248660471Snyan sio_setreg(com, com_fifo, 248760471Snyan sio_getreg(com, com_fifo) | 0x80); 248851078Speter } 248951078Speter } else { 249051078Speter if (com->st16650a) { 249160471Snyan sio_setreg(com, com_cfcr, 0xbf); 249260471Snyan sio_setreg(com, com_fifo, 249360471Snyan sio_getreg(com, com_fifo) & ~0x80); 249451078Speter } 249551078Speter } 249651078Speter 249760471Snyan sio_setreg(com, com_cfcr, com->cfcr_image); 249851078Speter 249951078Speter /* XXX shouldn't call functions while intrs are disabled. */ 250051078Speter disc_optim(tp, t, com); 250151078Speter /* 250251078Speter * Recover from fiddling with CS_TTGO. We used to call siointr1() 250351078Speter * unconditionally, but that defeated the careful discarding of 250451078Speter * stale input in sioopen(). 250551078Speter */ 250651078Speter if (com->state >= (CS_BUSY | CS_TTGO)) 250751078Speter siointr1(com); 250851078Speter 250965557Sjasone COM_UNLOCK(); 251065557Sjasone restore_intr(intrsave); 251151078Speter splx(s); 251251078Speter comstart(tp); 251351078Speter if (com->ibufold != NULL) { 251451078Speter free(com->ibufold, M_DEVBUF); 251551078Speter com->ibufold = NULL; 251651078Speter } 251751078Speter return (0); 251851078Speter} 251951078Speter 252051078Speterstatic int 252165596Sjhbsiosetwater(com, speed, locked) 252251078Speter struct com_s *com; 252351078Speter speed_t speed; 252465596Sjhb int locked; 252551078Speter{ 252651078Speter int cp4ticks; 252751078Speter u_char *ibuf; 252851078Speter int ibufsize; 252951078Speter struct tty *tp; 253065557Sjasone int intrsave; 253151078Speter 253251078Speter /* 253351078Speter * Make the buffer size large enough to handle a softtty interrupt 253451078Speter * latency of about 2 ticks without loss of throughput or data 253551078Speter * (about 3 ticks if input flow control is not used or not honoured, 253651078Speter * but a bit less for CS5-CS7 modes). 253751078Speter */ 253851078Speter cp4ticks = speed / 10 / hz * 4; 253951078Speter for (ibufsize = 128; ibufsize < cp4ticks;) 254051078Speter ibufsize <<= 1; 254165557Sjasone if (ibufsize == com->ibufsize) 254251078Speter return (0); 254351078Speter 254451078Speter /* 254551078Speter * Allocate input buffer. The extra factor of 2 in the size is 254651078Speter * to allow for an error byte for each input byte. 254751078Speter */ 254851078Speter ibuf = malloc(2 * ibufsize, M_DEVBUF, M_NOWAIT); 254965557Sjasone if (ibuf == NULL) 255051078Speter return (ENOMEM); 255151078Speter 255251078Speter /* Initialize non-critical variables. */ 255351078Speter com->ibufold = com->ibuf; 255451078Speter com->ibufsize = ibufsize; 255551078Speter tp = com->tp; 255651078Speter if (tp != NULL) { 255751078Speter tp->t_ififosize = 2 * ibufsize; 255851078Speter tp->t_ispeedwat = (speed_t)-1; 255951078Speter tp->t_ospeedwat = (speed_t)-1; 256051078Speter } 256151078Speter 256251078Speter /* 256351078Speter * Read current input buffer, if any. Continue with interrupts 256451078Speter * disabled. 256551078Speter */ 256665557Sjasone intrsave = save_intr(); 256751078Speter disable_intr(); 256865596Sjhb if (!locked) 256965596Sjhb COM_LOCK(); 257051078Speter if (com->iptr != com->ibuf) 257151078Speter sioinput(com); 257251078Speter 257351078Speter /*- 257451078Speter * Initialize critical variables, including input buffer watermarks. 257551078Speter * The external device is asked to stop sending when the buffer 257651078Speter * exactly reaches high water, or when the high level requests it. 257751078Speter * The high level is notified immediately (rather than at a later 257851078Speter * clock tick) when this watermark is reached. 257951078Speter * The buffer size is chosen so the watermark should almost never 258051078Speter * be reached. 258151078Speter * The low watermark is invisibly 0 since the buffer is always 258251078Speter * emptied all at once. 258351078Speter */ 258451078Speter com->iptr = com->ibuf = ibuf; 258551078Speter com->ibufend = ibuf + ibufsize; 258651078Speter com->ierroff = ibufsize; 258751078Speter com->ihighwater = ibuf + 3 * ibufsize / 4; 258865596Sjhb if (!locked) 258965596Sjhb COM_UNLOCK(); 259065557Sjasone restore_intr(intrsave); 259151078Speter return (0); 259251078Speter} 259351078Speter 259451078Speterstatic void 259551078Spetercomstart(tp) 259651078Speter struct tty *tp; 259751078Speter{ 259851078Speter struct com_s *com; 259951078Speter int s; 260051078Speter int unit; 260165557Sjasone int intrsave; 260251078Speter 260351078Speter unit = DEV_TO_UNIT(tp->t_dev); 260451078Speter com = com_addr(unit); 260557915Simp if (com == NULL) 260657915Simp return; 260751078Speter s = spltty(); 260865557Sjasone intrsave = save_intr(); 260951078Speter disable_intr(); 261065557Sjasone COM_LOCK(); 261151078Speter if (tp->t_state & TS_TTSTOP) 261251078Speter com->state &= ~CS_TTGO; 261351078Speter else 261451078Speter com->state |= CS_TTGO; 261551078Speter if (tp->t_state & TS_TBLOCK) { 261651078Speter if (com->mcr_image & MCR_RTS && com->state & CS_RTS_IFLOW) 261751078Speter outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS); 261851078Speter } else { 261951078Speter if (!(com->mcr_image & MCR_RTS) && com->iptr < com->ihighwater 262051078Speter && com->state & CS_RTS_IFLOW) 262151078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 262251078Speter } 262365557Sjasone COM_UNLOCK(); 262465557Sjasone restore_intr(intrsave); 262551078Speter if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) { 262651078Speter ttwwakeup(tp); 262751078Speter splx(s); 262851078Speter return; 262951078Speter } 263051078Speter if (tp->t_outq.c_cc != 0) { 263151078Speter struct lbq *qp; 263251078Speter struct lbq *next; 263351078Speter 263451078Speter if (!com->obufs[0].l_queued) { 263551078Speter com->obufs[0].l_tail 263651078Speter = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1, 263751078Speter sizeof com->obuf1); 263851078Speter com->obufs[0].l_next = NULL; 263951078Speter com->obufs[0].l_queued = TRUE; 264065557Sjasone intrsave = save_intr(); 264151078Speter disable_intr(); 264265557Sjasone COM_LOCK(); 264351078Speter if (com->state & CS_BUSY) { 264451078Speter qp = com->obufq.l_next; 264551078Speter while ((next = qp->l_next) != NULL) 264651078Speter qp = next; 264751078Speter qp->l_next = &com->obufs[0]; 264851078Speter } else { 264951078Speter com->obufq.l_head = com->obufs[0].l_head; 265051078Speter com->obufq.l_tail = com->obufs[0].l_tail; 265151078Speter com->obufq.l_next = &com->obufs[0]; 265251078Speter com->state |= CS_BUSY; 265351078Speter } 265465557Sjasone COM_UNLOCK(); 265565557Sjasone restore_intr(intrsave); 265651078Speter } 265751078Speter if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) { 265851078Speter com->obufs[1].l_tail 265951078Speter = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2, 266051078Speter sizeof com->obuf2); 266151078Speter com->obufs[1].l_next = NULL; 266251078Speter com->obufs[1].l_queued = TRUE; 266365557Sjasone intrsave = save_intr(); 266451078Speter disable_intr(); 266565557Sjasone COM_LOCK(); 266651078Speter if (com->state & CS_BUSY) { 266751078Speter qp = com->obufq.l_next; 266851078Speter while ((next = qp->l_next) != NULL) 266951078Speter qp = next; 267051078Speter qp->l_next = &com->obufs[1]; 267151078Speter } else { 267251078Speter com->obufq.l_head = com->obufs[1].l_head; 267351078Speter com->obufq.l_tail = com->obufs[1].l_tail; 267451078Speter com->obufq.l_next = &com->obufs[1]; 267551078Speter com->state |= CS_BUSY; 267651078Speter } 267765557Sjasone COM_UNLOCK(); 267865557Sjasone restore_intr(intrsave); 267951078Speter } 268051078Speter tp->t_state |= TS_BUSY; 268151078Speter } 268265557Sjasone intrsave = save_intr(); 268351078Speter disable_intr(); 268465557Sjasone COM_LOCK(); 268551078Speter if (com->state >= (CS_BUSY | CS_TTGO)) 268651078Speter siointr1(com); /* fake interrupt to start output */ 268765557Sjasone COM_UNLOCK(); 268865557Sjasone restore_intr(intrsave); 268951078Speter ttwwakeup(tp); 269051078Speter splx(s); 269151078Speter} 269251078Speter 269351078Speterstatic void 269451654Sphkcomstop(tp, rw) 269551078Speter struct tty *tp; 269651078Speter int rw; 269751078Speter{ 269851078Speter struct com_s *com; 269965557Sjasone int intrsave; 270051078Speter 270151078Speter com = com_addr(DEV_TO_UNIT(tp->t_dev)); 270257915Simp if (com == NULL || com->gone) 270351078Speter return; 270465557Sjasone intrsave = save_intr(); 270551078Speter disable_intr(); 270665557Sjasone COM_LOCK(); 270751078Speter if (rw & FWRITE) { 270851078Speter if (com->hasfifo) 270951078Speter#ifdef COM_ESP 271051078Speter /* XXX avoid h/w bug. */ 271151078Speter if (!com->esp) 271251078Speter#endif 271360471Snyan sio_setreg(com, com_fifo, 271460471Snyan FIFO_XMT_RST | com->fifo_image); 271551078Speter com->obufs[0].l_queued = FALSE; 271651078Speter com->obufs[1].l_queued = FALSE; 271751078Speter if (com->state & CS_ODONE) 271851078Speter com_events -= LOTS_OF_EVENTS; 271951078Speter com->state &= ~(CS_ODONE | CS_BUSY); 272051078Speter com->tp->t_state &= ~TS_BUSY; 272151078Speter } 272251078Speter if (rw & FREAD) { 272351078Speter if (com->hasfifo) 272451078Speter#ifdef COM_ESP 272551078Speter /* XXX avoid h/w bug. */ 272651078Speter if (!com->esp) 272751078Speter#endif 272860471Snyan sio_setreg(com, com_fifo, 272960471Snyan FIFO_RCV_RST | com->fifo_image); 273051078Speter com_events -= (com->iptr - com->ibuf); 273151078Speter com->iptr = com->ibuf; 273251078Speter } 273365557Sjasone COM_UNLOCK(); 273465557Sjasone restore_intr(intrsave); 273551078Speter comstart(tp); 273651078Speter} 273751078Speter 273851078Speterstatic int 273951078Spetercommctl(com, bits, how) 274051078Speter struct com_s *com; 274151078Speter int bits; 274251078Speter int how; 274351078Speter{ 274451078Speter int mcr; 274551078Speter int msr; 274665557Sjasone int intrsave; 274751078Speter 274851078Speter if (how == DMGET) { 274951078Speter bits = TIOCM_LE; /* XXX - always enabled while open */ 275051078Speter mcr = com->mcr_image; 275151078Speter if (mcr & MCR_DTR) 275251078Speter bits |= TIOCM_DTR; 275351078Speter if (mcr & MCR_RTS) 275451078Speter bits |= TIOCM_RTS; 275551078Speter msr = com->prev_modem_status; 275651078Speter if (msr & MSR_CTS) 275751078Speter bits |= TIOCM_CTS; 275851078Speter if (msr & MSR_DCD) 275951078Speter bits |= TIOCM_CD; 276051078Speter if (msr & MSR_DSR) 276151078Speter bits |= TIOCM_DSR; 276251078Speter /* 276351078Speter * XXX - MSR_RI is naturally volatile, and we make MSR_TERI 276451078Speter * more volatile by reading the modem status a lot. Perhaps 276551078Speter * we should latch both bits until the status is read here. 276651078Speter */ 276751078Speter if (msr & (MSR_RI | MSR_TERI)) 276851078Speter bits |= TIOCM_RI; 276951078Speter return (bits); 277051078Speter } 277151078Speter mcr = 0; 277251078Speter if (bits & TIOCM_DTR) 277351078Speter mcr |= MCR_DTR; 277451078Speter if (bits & TIOCM_RTS) 277551078Speter mcr |= MCR_RTS; 277651078Speter if (com->gone) 277751078Speter return(0); 277865557Sjasone intrsave = save_intr(); 277951078Speter disable_intr(); 278065557Sjasone COM_LOCK(); 278151078Speter switch (how) { 278251078Speter case DMSET: 278351078Speter outb(com->modem_ctl_port, 278451078Speter com->mcr_image = mcr | (com->mcr_image & MCR_IENABLE)); 278551078Speter break; 278651078Speter case DMBIS: 278751078Speter outb(com->modem_ctl_port, com->mcr_image |= mcr); 278851078Speter break; 278951078Speter case DMBIC: 279051078Speter outb(com->modem_ctl_port, com->mcr_image &= ~mcr); 279151078Speter break; 279251078Speter } 279365557Sjasone COM_UNLOCK(); 279465557Sjasone restore_intr(intrsave); 279551078Speter return (0); 279651078Speter} 279751078Speter 279851078Speterstatic void 279951078Spetersiosettimeout() 280051078Speter{ 280151078Speter struct com_s *com; 280251078Speter bool_t someopen; 280351078Speter int unit; 280451078Speter 280551078Speter /* 280651078Speter * Set our timeout period to 1 second if no polled devices are open. 280751078Speter * Otherwise set it to max(1/200, 1/hz). 280851078Speter * Enable timeouts iff some device is open. 280951078Speter */ 281051078Speter untimeout(comwakeup, (void *)NULL, sio_timeout_handle); 281151078Speter sio_timeout = hz; 281251078Speter someopen = FALSE; 281353344Speter for (unit = 0; unit < sio_numunits; ++unit) { 281451078Speter com = com_addr(unit); 281551078Speter if (com != NULL && com->tp != NULL 281651078Speter && com->tp->t_state & TS_ISOPEN && !com->gone) { 281751078Speter someopen = TRUE; 281851078Speter if (com->poll || com->poll_output) { 281951078Speter sio_timeout = hz > 200 ? hz / 200 : 1; 282051078Speter break; 282151078Speter } 282251078Speter } 282351078Speter } 282451078Speter if (someopen) { 282551078Speter sio_timeouts_until_log = hz / sio_timeout; 282651078Speter sio_timeout_handle = timeout(comwakeup, (void *)NULL, 282751078Speter sio_timeout); 282851078Speter } else { 282951078Speter /* Flush error messages, if any. */ 283051078Speter sio_timeouts_until_log = 1; 283151078Speter comwakeup((void *)NULL); 283251078Speter untimeout(comwakeup, (void *)NULL, sio_timeout_handle); 283351078Speter } 283451078Speter} 283551078Speter 283651078Speterstatic void 283751078Spetercomwakeup(chan) 283851078Speter void *chan; 283951078Speter{ 284051078Speter struct com_s *com; 284151078Speter int unit; 284265557Sjasone int intrsave; 284351078Speter 284451078Speter sio_timeout_handle = timeout(comwakeup, (void *)NULL, sio_timeout); 284551078Speter 284651078Speter /* 284751078Speter * Recover from lost output interrupts. 284851078Speter * Poll any lines that don't use interrupts. 284951078Speter */ 285053344Speter for (unit = 0; unit < sio_numunits; ++unit) { 285151078Speter com = com_addr(unit); 285251078Speter if (com != NULL && !com->gone 285351078Speter && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) { 285465557Sjasone intrsave = save_intr(); 285551078Speter disable_intr(); 285665557Sjasone COM_LOCK(); 285751078Speter siointr1(com); 285865557Sjasone COM_UNLOCK(); 285965557Sjasone restore_intr(intrsave); 286051078Speter } 286151078Speter } 286251078Speter 286351078Speter /* 286451078Speter * Check for and log errors, but not too often. 286551078Speter */ 286651078Speter if (--sio_timeouts_until_log > 0) 286751078Speter return; 286851078Speter sio_timeouts_until_log = hz / sio_timeout; 286953344Speter for (unit = 0; unit < sio_numunits; ++unit) { 287051078Speter int errnum; 287151078Speter 287251078Speter com = com_addr(unit); 287351078Speter if (com == NULL) 287451078Speter continue; 287551078Speter if (com->gone) 287651078Speter continue; 287751078Speter for (errnum = 0; errnum < CE_NTYPES; ++errnum) { 287851078Speter u_int delta; 287951078Speter u_long total; 288051078Speter 288165557Sjasone intrsave = save_intr(); 288251078Speter disable_intr(); 288365557Sjasone COM_LOCK(); 288451078Speter delta = com->delta_error_counts[errnum]; 288551078Speter com->delta_error_counts[errnum] = 0; 288665557Sjasone COM_UNLOCK(); 288765557Sjasone restore_intr(intrsave); 288851078Speter if (delta == 0) 288951078Speter continue; 289051078Speter total = com->error_counts[errnum] += delta; 289151078Speter log(LOG_ERR, "sio%d: %u more %s%s (total %lu)\n", 289251078Speter unit, delta, error_desc[errnum], 289351078Speter delta == 1 ? "" : "s", total); 289451078Speter } 289551078Speter } 289651078Speter} 289751078Speter 289851078Speterstatic void 289951078Speterdisc_optim(tp, t, com) 290051078Speter struct tty *tp; 290151078Speter struct termios *t; 290251078Speter struct com_s *com; 290351078Speter{ 290451078Speter if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) 290551078Speter && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) 290651078Speter && (!(t->c_iflag & PARMRK) 290751078Speter || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) 290851078Speter && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) 290951078Speter && linesw[tp->t_line].l_rint == ttyinput) 291051078Speter tp->t_state |= TS_CAN_BYPASS_L_RINT; 291151078Speter else 291251078Speter tp->t_state &= ~TS_CAN_BYPASS_L_RINT; 291351078Speter com->hotchar = linesw[tp->t_line].l_hotchar; 291451078Speter} 291551078Speter 291651078Speter/* 291751078Speter * Following are all routines needed for SIO to act as console 291851078Speter */ 291951078Speter#include <sys/cons.h> 292051078Speter 292151078Speterstruct siocnstate { 292251078Speter u_char dlbl; 292351078Speter u_char dlbh; 292451078Speter u_char ier; 292551078Speter u_char cfcr; 292651078Speter u_char mcr; 292751078Speter}; 292851078Speter 292951078Speterstatic speed_t siocngetspeed __P((Port_t, struct speedtab *)); 293051078Speterstatic void siocnclose __P((struct siocnstate *sp, Port_t iobase)); 293151078Speterstatic void siocnopen __P((struct siocnstate *sp, Port_t iobase, int speed)); 293251078Speterstatic void siocntxwait __P((Port_t iobase)); 293351078Speter 293451078Speterstatic cn_probe_t siocnprobe; 293551078Speterstatic cn_init_t siocninit; 293651078Speterstatic cn_checkc_t siocncheckc; 293751078Speterstatic cn_getc_t siocngetc; 293851078Speterstatic cn_putc_t siocnputc; 293951078Speter 294051078Speter#ifdef __i386__ 294155823SyokotaCONS_DRIVER(sio, siocnprobe, siocninit, NULL, siocngetc, siocncheckc, 294255823Syokota siocnputc, NULL); 294351078Speter#endif 294451078Speter 294551078Speter/* To get the GDB related variables */ 294651078Speter#if DDB > 0 294751078Speter#include <ddb/ddb.h> 294851078Speter#endif 294951078Speter 295051078Speterstatic void 295151078Spetersiocntxwait(iobase) 295251078Speter Port_t iobase; 295351078Speter{ 295451078Speter int timo; 295551078Speter 295651078Speter /* 295751078Speter * Wait for any pending transmission to finish. Required to avoid 295851078Speter * the UART lockup bug when the speed is changed, and for normal 295951078Speter * transmits. 296051078Speter */ 296151078Speter timo = 100000; 296251078Speter while ((inb(iobase + com_lsr) & (LSR_TSRE | LSR_TXRDY)) 296351078Speter != (LSR_TSRE | LSR_TXRDY) && --timo != 0) 296451078Speter ; 296551078Speter} 296651078Speter 296751078Speter/* 296851078Speter * Read the serial port specified and try to figure out what speed 296951078Speter * it's currently running at. We're assuming the serial port has 297051078Speter * been initialized and is basicly idle. This routine is only intended 297151078Speter * to be run at system startup. 297251078Speter * 297351078Speter * If the value read from the serial port doesn't make sense, return 0. 297451078Speter */ 297551078Speter 297651078Speterstatic speed_t 297751078Spetersiocngetspeed(iobase, table) 297851078Speter Port_t iobase; 297951078Speter struct speedtab *table; 298051078Speter{ 298151078Speter int code; 298251078Speter u_char dlbh; 298351078Speter u_char dlbl; 298451078Speter u_char cfcr; 298551078Speter 298651078Speter cfcr = inb(iobase + com_cfcr); 298751078Speter outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 298851078Speter 298951078Speter dlbl = inb(iobase + com_dlbl); 299051078Speter dlbh = inb(iobase + com_dlbh); 299151078Speter 299251078Speter outb(iobase + com_cfcr, cfcr); 299351078Speter 299451078Speter code = dlbh << 8 | dlbl; 299551078Speter 299653344Speter for (; table->sp_speed != -1; table++) 299751078Speter if (table->sp_code == code) 299851078Speter return (table->sp_speed); 299951078Speter 300058885Simp return (0); /* didn't match anything sane */ 300151078Speter} 300251078Speter 300351078Speterstatic void 300451078Spetersiocnopen(sp, iobase, speed) 300551078Speter struct siocnstate *sp; 300651078Speter Port_t iobase; 300751078Speter int speed; 300851078Speter{ 300951078Speter int divisor; 301051078Speter u_char dlbh; 301151078Speter u_char dlbl; 301251078Speter 301351078Speter /* 301451078Speter * Save all the device control registers except the fifo register 301551078Speter * and set our default ones (cs8 -parenb speed=comdefaultrate). 301651078Speter * We can't save the fifo register since it is read-only. 301751078Speter */ 301851078Speter sp->ier = inb(iobase + com_ier); 301951078Speter outb(iobase + com_ier, 0); /* spltty() doesn't stop siointr() */ 302051078Speter siocntxwait(iobase); 302151078Speter sp->cfcr = inb(iobase + com_cfcr); 302251078Speter outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 302351078Speter sp->dlbl = inb(iobase + com_dlbl); 302451078Speter sp->dlbh = inb(iobase + com_dlbh); 302551078Speter /* 302651078Speter * Only set the divisor registers if they would change, since on 302751078Speter * some 16550 incompatibles (Startech), setting them clears the 302851078Speter * data input register. This also reduces the effects of the 302951078Speter * UMC8669F bug. 303051078Speter */ 303151078Speter divisor = ttspeedtab(speed, comspeedtab); 303251078Speter dlbl = divisor & 0xFF; 303351078Speter if (sp->dlbl != dlbl) 303451078Speter outb(iobase + com_dlbl, dlbl); 303551078Speter dlbh = (u_int) divisor >> 8; 303651078Speter if (sp->dlbh != dlbh) 303751078Speter outb(iobase + com_dlbh, dlbh); 303851078Speter outb(iobase + com_cfcr, CFCR_8BITS); 303951078Speter sp->mcr = inb(iobase + com_mcr); 304051078Speter /* 304151078Speter * We don't want interrupts, but must be careful not to "disable" 304251078Speter * them by clearing the MCR_IENABLE bit, since that might cause 304351078Speter * an interrupt by floating the IRQ line. 304451078Speter */ 304551078Speter outb(iobase + com_mcr, (sp->mcr & MCR_IENABLE) | MCR_DTR | MCR_RTS); 304651078Speter} 304751078Speter 304851078Speterstatic void 304951078Spetersiocnclose(sp, iobase) 305051078Speter struct siocnstate *sp; 305151078Speter Port_t iobase; 305251078Speter{ 305351078Speter /* 305451078Speter * Restore the device control registers. 305551078Speter */ 305651078Speter siocntxwait(iobase); 305751078Speter outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 305851078Speter if (sp->dlbl != inb(iobase + com_dlbl)) 305951078Speter outb(iobase + com_dlbl, sp->dlbl); 306051078Speter if (sp->dlbh != inb(iobase + com_dlbh)) 306151078Speter outb(iobase + com_dlbh, sp->dlbh); 306251078Speter outb(iobase + com_cfcr, sp->cfcr); 306351078Speter /* 306451078Speter * XXX damp oscillations of MCR_DTR and MCR_RTS by not restoring them. 306551078Speter */ 306651078Speter outb(iobase + com_mcr, sp->mcr | MCR_DTR | MCR_RTS); 306751078Speter outb(iobase + com_ier, sp->ier); 306851078Speter} 306951078Speter 307051078Speterstatic void 307151078Spetersiocnprobe(cp) 307251078Speter struct consdev *cp; 307351078Speter{ 307451078Speter speed_t boot_speed; 307551078Speter u_char cfcr; 307651078Speter int s, unit; 307751078Speter struct siocnstate sp; 307851078Speter 307951078Speter /* 308051078Speter * Find our first enabled console, if any. If it is a high-level 308151078Speter * console device, then initialize it and return successfully. 308251078Speter * If it is a low-level console device, then initialize it and 308351078Speter * return unsuccessfully. It must be initialized in both cases 308451078Speter * for early use by console drivers and debuggers. Initializing 308551078Speter * the hardware is not necessary in all cases, since the i/o 308651078Speter * routines initialize it on the fly, but it is necessary if 308751078Speter * input might arrive while the hardware is switched back to an 308851078Speter * uninitialized state. We can't handle multiple console devices 308951078Speter * yet because our low-level routines don't take a device arg. 309051078Speter * We trust the user to set the console flags properly so that we 309151078Speter * don't need to probe. 309251078Speter */ 309351078Speter cp->cn_pri = CN_DEAD; 309451078Speter 309551078Speter for (unit = 0; unit < 16; unit++) { /* XXX need to know how many */ 309651078Speter int flags; 309751078Speter int disabled; 309851078Speter if (resource_int_value("sio", unit, "disabled", &disabled) == 0) { 309951078Speter if (disabled) 310051078Speter continue; 310151078Speter } 310251078Speter if (resource_int_value("sio", unit, "flags", &flags)) 310351078Speter continue; 310451078Speter if (COM_CONSOLE(flags) || COM_DEBUGGER(flags)) { 310551078Speter int port; 310651078Speter Port_t iobase; 310751078Speter 310851078Speter if (resource_int_value("sio", unit, "port", &port)) 310951078Speter continue; 311051078Speter iobase = port; 311151078Speter s = spltty(); 311251078Speter if (boothowto & RB_SERIAL) { 311351078Speter boot_speed = siocngetspeed(iobase, comspeedtab); 311451078Speter if (boot_speed) 311551078Speter comdefaultrate = boot_speed; 311651078Speter } 311751078Speter 311851078Speter /* 311951078Speter * Initialize the divisor latch. We can't rely on 312051078Speter * siocnopen() to do this the first time, since it 312151078Speter * avoids writing to the latch if the latch appears 312251078Speter * to have the correct value. Also, if we didn't 312351078Speter * just read the speed from the hardware, then we 312451078Speter * need to set the speed in hardware so that 312551078Speter * switching it later is null. 312651078Speter */ 312751078Speter cfcr = inb(iobase + com_cfcr); 312851078Speter outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 312951078Speter outb(iobase + com_dlbl, 313051078Speter COMBRD(comdefaultrate) & 0xff); 313151078Speter outb(iobase + com_dlbh, 313251078Speter (u_int) COMBRD(comdefaultrate) >> 8); 313351078Speter outb(iobase + com_cfcr, cfcr); 313451078Speter 313551078Speter siocnopen(&sp, iobase, comdefaultrate); 313651078Speter 313751078Speter splx(s); 313851078Speter if (COM_CONSOLE(flags) && !COM_LLCONSOLE(flags)) { 313951078Speter cp->cn_dev = makedev(CDEV_MAJOR, unit); 314051078Speter cp->cn_pri = COM_FORCECONSOLE(flags) 314151078Speter || boothowto & RB_SERIAL 314251078Speter ? CN_REMOTE : CN_NORMAL; 314351078Speter siocniobase = iobase; 314451078Speter siocnunit = unit; 314551078Speter } 314651078Speter if (COM_DEBUGGER(flags)) { 314751078Speter printf("sio%d: gdb debugging port\n", unit); 314851078Speter siogdbiobase = iobase; 314951078Speter siogdbunit = unit; 315051078Speter#if DDB > 0 315151078Speter gdbdev = makedev(CDEV_MAJOR, unit); 315251078Speter gdb_getc = siocngetc; 315351078Speter gdb_putc = siocnputc; 315451078Speter#endif 315551078Speter } 315651078Speter } 315751078Speter } 315851078Speter#ifdef __i386__ 315951078Speter#if DDB > 0 316051078Speter /* 316151078Speter * XXX Ugly Compatability. 316251078Speter * If no gdb port has been specified, set it to be the console 316351078Speter * as some configuration files don't specify the gdb port. 316451078Speter */ 316551078Speter if (gdbdev == NODEV && (boothowto & RB_GDB)) { 316651078Speter printf("Warning: no GDB port specified. Defaulting to sio%d.\n", 316751078Speter siocnunit); 316851078Speter printf("Set flag 0x80 on desired GDB port in your\n"); 316951078Speter printf("configuration file (currently sio only).\n"); 317051078Speter siogdbiobase = siocniobase; 317151078Speter siogdbunit = siocnunit; 317251078Speter gdbdev = makedev(CDEV_MAJOR, siocnunit); 317351078Speter gdb_getc = siocngetc; 317451078Speter gdb_putc = siocnputc; 317551078Speter } 317651078Speter#endif 317751078Speter#endif 317851078Speter} 317951078Speter 318051078Speter#ifdef __alpha__ 318151078Speter 318255868SgallatinCONS_DRIVER(sio, NULL, NULL, NULL, siocngetc, siocncheckc, siocnputc, NULL); 318351078Speter 318451078Speterint 318551078Spetersiocnattach(port, speed) 318651078Speter int port; 318751078Speter int speed; 318851078Speter{ 318951078Speter int s; 319051078Speter u_char cfcr; 319151078Speter struct siocnstate sp; 319251078Speter 319351078Speter siocniobase = port; 319451078Speter comdefaultrate = speed; 319551078Speter sio_consdev.cn_pri = CN_NORMAL; 319651078Speter sio_consdev.cn_dev = makedev(CDEV_MAJOR, 0); 319751078Speter 319851078Speter s = spltty(); 319951078Speter 320051078Speter /* 320151078Speter * Initialize the divisor latch. We can't rely on 320251078Speter * siocnopen() to do this the first time, since it 320351078Speter * avoids writing to the latch if the latch appears 320451078Speter * to have the correct value. Also, if we didn't 320551078Speter * just read the speed from the hardware, then we 320651078Speter * need to set the speed in hardware so that 320751078Speter * switching it later is null. 320851078Speter */ 320951078Speter cfcr = inb(siocniobase + com_cfcr); 321051078Speter outb(siocniobase + com_cfcr, CFCR_DLAB | cfcr); 321151078Speter outb(siocniobase + com_dlbl, 321251078Speter COMBRD(comdefaultrate) & 0xff); 321351078Speter outb(siocniobase + com_dlbh, 321451078Speter (u_int) COMBRD(comdefaultrate) >> 8); 321551078Speter outb(siocniobase + com_cfcr, cfcr); 321651078Speter 321751078Speter siocnopen(&sp, siocniobase, comdefaultrate); 321851078Speter splx(s); 321951078Speter 322051078Speter cn_tab = &sio_consdev; 322158885Simp return (0); 322251078Speter} 322351078Speter 322451078Speterint 322551078Spetersiogdbattach(port, speed) 322651078Speter int port; 322751078Speter int speed; 322851078Speter{ 322951078Speter int s; 323051078Speter u_char cfcr; 323151078Speter struct siocnstate sp; 323251078Speter 323351078Speter siogdbiobase = port; 323451078Speter gdbdefaultrate = speed; 323551078Speter 323651078Speter s = spltty(); 323751078Speter 323851078Speter /* 323951078Speter * Initialize the divisor latch. We can't rely on 324051078Speter * siocnopen() to do this the first time, since it 324151078Speter * avoids writing to the latch if the latch appears 324251078Speter * to have the correct value. Also, if we didn't 324351078Speter * just read the speed from the hardware, then we 324451078Speter * need to set the speed in hardware so that 324551078Speter * switching it later is null. 324651078Speter */ 324751078Speter cfcr = inb(siogdbiobase + com_cfcr); 324851078Speter outb(siogdbiobase + com_cfcr, CFCR_DLAB | cfcr); 324951078Speter outb(siogdbiobase + com_dlbl, 325051078Speter COMBRD(gdbdefaultrate) & 0xff); 325151078Speter outb(siogdbiobase + com_dlbh, 325251078Speter (u_int) COMBRD(gdbdefaultrate) >> 8); 325351078Speter outb(siogdbiobase + com_cfcr, cfcr); 325451078Speter 325551078Speter siocnopen(&sp, siogdbiobase, gdbdefaultrate); 325651078Speter splx(s); 325751078Speter 325858885Simp return (0); 325951078Speter} 326051078Speter 326151078Speter#endif 326251078Speter 326351078Speterstatic void 326451078Spetersiocninit(cp) 326551078Speter struct consdev *cp; 326651078Speter{ 326751078Speter comconsole = DEV_TO_UNIT(cp->cn_dev); 326851078Speter} 326951078Speter 327051078Speterstatic int 327151078Spetersiocncheckc(dev) 327251078Speter dev_t dev; 327351078Speter{ 327451078Speter int c; 327551078Speter Port_t iobase; 327651078Speter int s; 327751078Speter struct siocnstate sp; 327851078Speter 327951078Speter if (minor(dev) == siogdbunit) 328051078Speter iobase = siogdbiobase; 328151078Speter else 328251078Speter iobase = siocniobase; 328351078Speter s = spltty(); 328451078Speter siocnopen(&sp, iobase, comdefaultrate); 328551078Speter if (inb(iobase + com_lsr) & LSR_RXRDY) 328651078Speter c = inb(iobase + com_data); 328751078Speter else 328851078Speter c = -1; 328951078Speter siocnclose(&sp, iobase); 329051078Speter splx(s); 329151078Speter return (c); 329251078Speter} 329351078Speter 329451078Speter 329551078Speterint 329651078Spetersiocngetc(dev) 329751078Speter dev_t dev; 329851078Speter{ 329951078Speter int c; 330051078Speter Port_t iobase; 330151078Speter int s; 330251078Speter struct siocnstate sp; 330351078Speter 330451078Speter if (minor(dev) == siogdbunit) 330551078Speter iobase = siogdbiobase; 330651078Speter else 330751078Speter iobase = siocniobase; 330851078Speter s = spltty(); 330951078Speter siocnopen(&sp, iobase, comdefaultrate); 331051078Speter while (!(inb(iobase + com_lsr) & LSR_RXRDY)) 331151078Speter ; 331251078Speter c = inb(iobase + com_data); 331351078Speter siocnclose(&sp, iobase); 331451078Speter splx(s); 331551078Speter return (c); 331651078Speter} 331751078Speter 331851078Spetervoid 331951078Spetersiocnputc(dev, c) 332051078Speter dev_t dev; 332151078Speter int c; 332251078Speter{ 332351078Speter int s; 332451078Speter struct siocnstate sp; 332551078Speter Port_t iobase; 332651078Speter 332751078Speter if (minor(dev) == siogdbunit) 332851078Speter iobase = siogdbiobase; 332951078Speter else 333051078Speter iobase = siocniobase; 333151078Speter s = spltty(); 333251078Speter siocnopen(&sp, iobase, comdefaultrate); 333351078Speter siocntxwait(iobase); 333451078Speter outb(iobase + com_data, c); 333551078Speter siocnclose(&sp, iobase); 333651078Speter splx(s); 333751078Speter} 333851078Speter 333951078Speter#ifdef __alpha__ 334051078Speterint 334151078Spetersiogdbgetc() 334251078Speter{ 334351078Speter int c; 334451078Speter Port_t iobase; 334551078Speter int s; 334651078Speter struct siocnstate sp; 334751078Speter 334851078Speter iobase = siogdbiobase; 334951078Speter s = spltty(); 335051078Speter siocnopen(&sp, iobase, gdbdefaultrate); 335151078Speter while (!(inb(iobase + com_lsr) & LSR_RXRDY)) 335251078Speter ; 335351078Speter c = inb(iobase + com_data); 335451078Speter siocnclose(&sp, iobase); 335551078Speter splx(s); 335651078Speter return (c); 335751078Speter} 335851078Speter 335951078Spetervoid 336051078Spetersiogdbputc(c) 336151078Speter int c; 336251078Speter{ 336351078Speter int s; 336451078Speter struct siocnstate sp; 336551078Speter 336651078Speter s = spltty(); 336751078Speter siocnopen(&sp, siogdbiobase, gdbdefaultrate); 336851078Speter siocntxwait(siogdbiobase); 336951078Speter outb(siogdbiobase + com_data, c); 337051078Speter siocnclose(&sp, siogdbiobase); 337151078Speter splx(s); 337251078Speter} 337351078Speter#endif 337451078Speter 337552471SimpDRIVER_MODULE(sio, isa, sio_isa_driver, sio_devclass, 0, 0); 337652471Simp#if NCARD > 0 337752471SimpDRIVER_MODULE(sio, pccard, sio_pccard_driver, sio_devclass, 0, 0); 337852471Simp#endif 337958885Simp#if NPCI > 0 338058885SimpDRIVER_MODULE(sio, pci, sio_pci_driver, sio_devclass, 0, 0); 338158885Simp#endif 3382