sio.c revision 77222
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 77222 2001-05-26 11:17:57Z sanpei $ 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 4551078Speter/* 4651078Speter * Serial driver, based on 386BSD-0.1 com driver. 4751078Speter * Mostly rewritten to use pseudo-DMA. 4851078Speter * Works for National Semiconductor NS8250-NS16550AF UARTs. 4951078Speter * COM driver, based on HP dca driver. 5051078Speter * 5151078Speter * Changes for PC-Card integration: 5251078Speter * - Added PC-Card driver table and handlers 5351078Speter */ 5451078Speter#include <sys/param.h> 5576166Smarkm#include <sys/systm.h> 5665822Sjhb#include <sys/bus.h> 5776166Smarkm#include <sys/bus.h> 5851078Speter#include <sys/conf.h> 5951078Speter#include <sys/dkstat.h> 6051078Speter#include <sys/fcntl.h> 6151078Speter#include <sys/interrupt.h> 6251078Speter#include <sys/kernel.h> 6376166Smarkm#include <sys/lock.h> 6476166Smarkm#include <sys/malloc.h> 6576166Smarkm#include <sys/module.h> 6676166Smarkm#include <sys/mutex.h> 6776166Smarkm#include <sys/proc.h> 6876166Smarkm#include <sys/reboot.h> 6976166Smarkm#include <sys/sysctl.h> 7051078Speter#include <sys/syslog.h> 7176166Smarkm#include <sys/tty.h> 7260471Snyan#include <machine/bus_pio.h> 7351078Speter#include <machine/bus.h> 7451078Speter#include <sys/rman.h> 7558377Sphk#include <sys/timetc.h> 7651078Speter#include <sys/timepps.h> 7751078Speter 7851078Speter#include <isa/isareg.h> 7951078Speter#include <isa/isavar.h> 8058885Simp#if NPCI > 0 8158885Simp#include <pci/pcireg.h> 8258885Simp#include <pci/pcivar.h> 8358885Simp#endif 8451078Speter 8551078Speter#include <machine/clock.h> 8651078Speter#include <machine/resource.h> 8751078Speter 8851078Speter#include <isa/sioreg.h> 8951078Speter 9051078Speter#ifdef COM_ESP 9151078Speter#include <isa/ic/esp.h> 9251078Speter#endif 9351078Speter#include <isa/ic/ns16550.h> 9451078Speter 9551078Speter#define LOTS_OF_EVENTS 64 /* helps separate urgent events from input */ 9651078Speter 9751078Speter#define CALLOUT_MASK 0x80 9851078Speter#define CONTROL_MASK 0x60 9951078Speter#define CONTROL_INIT_STATE 0x20 10051078Speter#define CONTROL_LOCK_STATE 0x40 10151078Speter#define DEV_TO_UNIT(dev) (MINOR_TO_UNIT(minor(dev))) 10251078Speter#define MINOR_MAGIC_MASK (CALLOUT_MASK | CONTROL_MASK) 10351078Speter#define MINOR_TO_UNIT(mynor) ((mynor) & ~MINOR_MAGIC_MASK) 10451078Speter 10551078Speter#ifdef COM_MULTIPORT 10651078Speter/* checks in flags for multiport and which is multiport "master chip" 10751078Speter * for a given card 10851078Speter */ 10951078Speter#define COM_ISMULTIPORT(flags) ((flags) & 0x01) 11051078Speter#define COM_MPMASTER(flags) (((flags) >> 8) & 0x0ff) 11151078Speter#define COM_NOTAST4(flags) ((flags) & 0x04) 11251078Speter#endif /* COM_MULTIPORT */ 11351078Speter 11451078Speter#define COM_CONSOLE(flags) ((flags) & 0x10) 11551078Speter#define COM_FORCECONSOLE(flags) ((flags) & 0x20) 11651078Speter#define COM_LLCONSOLE(flags) ((flags) & 0x40) 11751078Speter#define COM_DEBUGGER(flags) ((flags) & 0x80) 11851078Speter#define COM_LOSESOUTINTS(flags) ((flags) & 0x08) 11951078Speter#define COM_NOFIFO(flags) ((flags) & 0x02) 12051078Speter#define COM_ST16650A(flags) ((flags) & 0x20000) 12151078Speter#define COM_C_NOPROBE (0x40000) 12251078Speter#define COM_NOPROBE(flags) ((flags) & COM_C_NOPROBE) 12351078Speter#define COM_C_IIR_TXRDYBUG (0x80000) 12451078Speter#define COM_IIR_TXRDYBUG(flags) ((flags) & COM_C_IIR_TXRDYBUG) 12551078Speter#define COM_FIFOSIZE(flags) (((flags) & 0xff000000) >> 24) 12651078Speter 12751078Speter#define com_scr 7 /* scratch register for 16450-16550 (R/W) */ 12851078Speter 12960471Snyan#define sio_getreg(com, off) \ 13060471Snyan (bus_space_read_1((com)->bst, (com)->bsh, (off))) 13160471Snyan#define sio_setreg(com, off, value) \ 13260471Snyan (bus_space_write_1((com)->bst, (com)->bsh, (off), (value))) 13360471Snyan 13451078Speter/* 13551078Speter * com state bits. 13651078Speter * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher 13751078Speter * than the other bits so that they can be tested as a group without masking 13851078Speter * off the low bits. 13951078Speter * 14051078Speter * The following com and tty flags correspond closely: 14151078Speter * CS_BUSY = TS_BUSY (maintained by comstart(), siopoll() and 14253344Speter * comstop()) 14351078Speter * CS_TTGO = ~TS_TTSTOP (maintained by comparam() and comstart()) 14451078Speter * CS_CTS_OFLOW = CCTS_OFLOW (maintained by comparam()) 14551078Speter * CS_RTS_IFLOW = CRTS_IFLOW (maintained by comparam()) 14651078Speter * TS_FLUSH is not used. 14751078Speter * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON. 14851078Speter * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state). 14951078Speter */ 15051078Speter#define CS_BUSY 0x80 /* output in progress */ 15151078Speter#define CS_TTGO 0x40 /* output not stopped by XOFF */ 15251078Speter#define CS_ODEVREADY 0x20 /* external device h/w ready (CTS) */ 15351078Speter#define CS_CHECKMSR 1 /* check of MSR scheduled */ 15451078Speter#define CS_CTS_OFLOW 2 /* use CTS output flow control */ 15551078Speter#define CS_DTR_OFF 0x10 /* DTR held off */ 15651078Speter#define CS_ODONE 4 /* output completed */ 15751078Speter#define CS_RTS_IFLOW 8 /* use RTS input flow control */ 15851078Speter#define CSE_BUSYCHECK 1 /* siobusycheck() scheduled */ 15951078Speter 16051078Speterstatic char const * const error_desc[] = { 16151078Speter#define CE_OVERRUN 0 16251078Speter "silo overflow", 16351078Speter#define CE_INTERRUPT_BUF_OVERFLOW 1 16451078Speter "interrupt-level buffer overflow", 16551078Speter#define CE_TTY_BUF_OVERFLOW 2 16651078Speter "tty-level buffer overflow", 16751078Speter}; 16851078Speter 16951078Speter#define CE_NTYPES 3 17051078Speter#define CE_RECORD(com, errnum) (++(com)->delta_error_counts[errnum]) 17151078Speter 17251078Speter/* types. XXX - should be elsewhere */ 17351078Spetertypedef u_int Port_t; /* hardware port */ 17451078Spetertypedef u_char bool_t; /* boolean */ 17551078Speter 17651078Speter/* queue of linear buffers */ 17751078Speterstruct lbq { 17851078Speter u_char *l_head; /* next char to process */ 17951078Speter u_char *l_tail; /* one past the last char to process */ 18051078Speter struct lbq *l_next; /* next in queue */ 18151078Speter bool_t l_queued; /* nonzero if queued */ 18251078Speter}; 18351078Speter 18451078Speter/* com device structure */ 18551078Speterstruct com_s { 18651078Speter u_int flags; /* Copy isa device flags */ 18751078Speter u_char state; /* miscellaneous flag bits */ 18851078Speter bool_t active_out; /* nonzero if the callout device is open */ 18951078Speter u_char cfcr_image; /* copy of value written to CFCR */ 19051078Speter#ifdef COM_ESP 19151078Speter bool_t esp; /* is this unit a hayes esp board? */ 19251078Speter#endif 19351078Speter u_char extra_state; /* more flag bits, separate for order trick */ 19451078Speter u_char fifo_image; /* copy of value written to FIFO */ 19551078Speter bool_t hasfifo; /* nonzero for 16550 UARTs */ 19651078Speter bool_t st16650a; /* Is a Startech 16650A or RTS/CTS compat */ 19751078Speter bool_t loses_outints; /* nonzero if device loses output interrupts */ 19851078Speter u_char mcr_image; /* copy of value written to MCR */ 19951078Speter#ifdef COM_MULTIPORT 20051078Speter bool_t multiport; /* is this unit part of a multiport device? */ 20151078Speter#endif /* COM_MULTIPORT */ 20251078Speter bool_t no_irq; /* nonzero if irq is not attached */ 20351078Speter bool_t gone; /* hardware disappeared */ 20451078Speter bool_t poll; /* nonzero if polling is required */ 20551078Speter bool_t poll_output; /* nonzero if polling for output is required */ 20651078Speter int unit; /* unit number */ 20751078Speter int dtr_wait; /* time to hold DTR down on close (* 1/hz) */ 20851078Speter u_int tx_fifo_size; 20951078Speter u_int wopeners; /* # processes waiting for DCD in open() */ 21051078Speter 21151078Speter /* 21251078Speter * The high level of the driver never reads status registers directly 21351078Speter * because there would be too many side effects to handle conveniently. 21451078Speter * Instead, it reads copies of the registers stored here by the 21551078Speter * interrupt handler. 21651078Speter */ 21751078Speter u_char last_modem_status; /* last MSR read by intr handler */ 21851078Speter u_char prev_modem_status; /* last MSR handled by high level */ 21951078Speter 22051078Speter u_char hotchar; /* ldisc-specific char to be handled ASAP */ 22151078Speter u_char *ibuf; /* start of input buffer */ 22251078Speter u_char *ibufend; /* end of input buffer */ 22351078Speter u_char *ibufold; /* old input buffer, to be freed */ 22451078Speter u_char *ihighwater; /* threshold in input buffer */ 22551078Speter u_char *iptr; /* next free spot in input buffer */ 22651078Speter int ibufsize; /* size of ibuf (not include error bytes) */ 22751078Speter int ierroff; /* offset of error bytes in ibuf */ 22851078Speter 22951078Speter struct lbq obufq; /* head of queue of output buffers */ 23051078Speter struct lbq obufs[2]; /* output buffers */ 23151078Speter 23260471Snyan bus_space_tag_t bst; 23360471Snyan bus_space_handle_t bsh; 23460471Snyan 23551078Speter Port_t data_port; /* i/o ports */ 23651078Speter#ifdef COM_ESP 23751078Speter Port_t esp_port; 23851078Speter#endif 23951078Speter Port_t int_id_port; 24051078Speter Port_t modem_ctl_port; 24151078Speter Port_t line_status_port; 24251078Speter Port_t modem_status_port; 24351078Speter Port_t intr_ctl_port; /* Ports of IIR register */ 24451078Speter 24551078Speter struct tty *tp; /* cross reference */ 24651078Speter 24751078Speter /* Initial state. */ 24851078Speter struct termios it_in; /* should be in struct tty */ 24951078Speter struct termios it_out; 25051078Speter 25151078Speter /* Lock state. */ 25251078Speter struct termios lt_in; /* should be in struct tty */ 25351078Speter struct termios lt_out; 25451078Speter 25551078Speter bool_t do_timestamp; 25651078Speter bool_t do_dcd_timestamp; 25751078Speter struct timeval timestamp; 25851078Speter struct timeval dcd_timestamp; 25951078Speter struct pps_state pps; 26051078Speter 26151078Speter u_long bytes_in; /* statistics */ 26251078Speter u_long bytes_out; 26351078Speter u_int delta_error_counts[CE_NTYPES]; 26451078Speter u_long error_counts[CE_NTYPES]; 26551078Speter 26651078Speter struct resource *irqres; 26751078Speter struct resource *ioportres; 26854386Simp void *cookie; 26965131Sphk dev_t devs[6]; 27051078Speter 27151078Speter /* 27251078Speter * Data area for output buffers. Someday we should build the output 27351078Speter * buffer queue without copying data. 27451078Speter */ 27551078Speter u_char obuf1[256]; 27651078Speter u_char obuf2[256]; 27751078Speter}; 27851078Speter 27951078Speter#ifdef COM_ESP 28051078Speterstatic int espattach __P((struct com_s *com, Port_t esp_port)); 28151078Speter#endif 28258885Simpstatic int sioattach __P((device_t dev, int rid)); 28352471Simpstatic int sio_isa_attach __P((device_t dev)); 28451078Speter 28551078Speterstatic timeout_t siobusycheck; 28651078Speterstatic timeout_t siodtrwakeup; 28751078Speterstatic void comhardclose __P((struct com_s *com)); 28851078Speterstatic void sioinput __P((struct com_s *com)); 28951078Speterstatic void siointr1 __P((struct com_s *com)); 29051078Speterstatic void siointr __P((void *arg)); 29151078Speterstatic int commctl __P((struct com_s *com, int bits, int how)); 29251078Speterstatic int comparam __P((struct tty *tp, struct termios *t)); 29367551Sjhbstatic void siopoll __P((void *)); 29458885Simpstatic int sioprobe __P((device_t dev, int xrid)); 29552471Simpstatic int sio_isa_probe __P((device_t dev)); 29651078Speterstatic void siosettimeout __P((void)); 29765605Sjhbstatic int siosetwater __P((struct com_s *com, speed_t speed)); 29851078Speterstatic void comstart __P((struct tty *tp)); 29951654Sphkstatic void comstop __P((struct tty *tp, int rw)); 30051078Speterstatic timeout_t comwakeup; 30151078Speterstatic void disc_optim __P((struct tty *tp, struct termios *t, 30251078Speter struct com_s *com)); 30351078Speter 30452471Simp#if NCARD > 0 30552471Simpstatic int sio_pccard_attach __P((device_t dev)); 30654386Simpstatic int sio_pccard_detach __P((device_t dev)); 30752471Simpstatic int sio_pccard_probe __P((device_t dev)); 30852471Simp#endif /* NCARD > 0 */ 30951078Speter 31058885Simp#if NPCI > 0 31158885Simpstatic int sio_pci_attach __P((device_t dev)); 31258885Simpstatic void sio_pci_kludge_unit __P((device_t dev)); 31358885Simpstatic int sio_pci_probe __P((device_t dev)); 31458885Simp#endif /* NPCI > 0 */ 31558885Simp 31670174Sjhbstatic char driver_name[] = "sio"; 31770174Sjhbstatic struct mtx sio_lock; 31870174Sjhbstatic int sio_inited; 31951078Speter 32051078Speter/* table and macro for fast conversion from a unit number to its com struct */ 32151078Speterstatic devclass_t sio_devclass; 32251078Speter#define com_addr(unit) ((struct com_s *) \ 32351078Speter devclass_get_softc(sio_devclass, unit)) 32451078Speter 32552471Simpstatic device_method_t sio_isa_methods[] = { 32651078Speter /* Device interface */ 32752471Simp DEVMETHOD(device_probe, sio_isa_probe), 32852471Simp DEVMETHOD(device_attach, sio_isa_attach), 32951078Speter 33051078Speter { 0, 0 } 33151078Speter}; 33251078Speter 33352471Simpstatic driver_t sio_isa_driver = { 33451078Speter driver_name, 33552471Simp sio_isa_methods, 33651078Speter sizeof(struct com_s), 33751078Speter}; 33851078Speter 33952471Simp#if NCARD > 0 34052471Simpstatic device_method_t sio_pccard_methods[] = { 34152471Simp /* Device interface */ 34252471Simp DEVMETHOD(device_probe, sio_pccard_probe), 34352471Simp DEVMETHOD(device_attach, sio_pccard_attach), 34452471Simp DEVMETHOD(device_detach, sio_pccard_detach), 34552471Simp 34652471Simp { 0, 0 } 34752471Simp}; 34852471Simp 34952471Simpstatic driver_t sio_pccard_driver = { 35052471Simp driver_name, 35152471Simp sio_pccard_methods, 35252471Simp sizeof(struct com_s), 35352471Simp}; 35458885Simp#endif /* NCARD > 0 */ 35552471Simp 35658885Simp#if NPCI > 0 35758885Simpstatic device_method_t sio_pci_methods[] = { 35858885Simp /* Device interface */ 35958885Simp DEVMETHOD(device_probe, sio_pci_probe), 36058885Simp DEVMETHOD(device_attach, sio_pci_attach), 36158885Simp 36258885Simp { 0, 0 } 36358885Simp}; 36458885Simp 36558885Simpstatic driver_t sio_pci_driver = { 36658885Simp driver_name, 36758885Simp sio_pci_methods, 36858885Simp sizeof(struct com_s), 36958885Simp}; 37058885Simp#endif /* NPCI > 0 */ 37158885Simp 37251078Speterstatic d_open_t sioopen; 37351078Speterstatic d_close_t sioclose; 37451078Speterstatic d_read_t sioread; 37551078Speterstatic d_write_t siowrite; 37651078Speterstatic d_ioctl_t sioioctl; 37751078Speter 37851078Speter#define CDEV_MAJOR 28 37951078Speterstatic struct cdevsw sio_cdevsw = { 38051078Speter /* open */ sioopen, 38151078Speter /* close */ sioclose, 38251078Speter /* read */ sioread, 38351078Speter /* write */ siowrite, 38451078Speter /* ioctl */ sioioctl, 38551654Sphk /* poll */ ttypoll, 38651078Speter /* mmap */ nommap, 38751078Speter /* strategy */ nostrategy, 38851078Speter /* name */ driver_name, 38951078Speter /* maj */ CDEV_MAJOR, 39051078Speter /* dump */ nodump, 39151078Speter /* psize */ nopsize, 39272521Sjlemon /* flags */ D_TTY | D_KQFILTER, 39372521Sjlemon /* kqfilter */ ttykqfilter, 39451078Speter}; 39551078Speter 39651078Speterint comconsole = -1; 39751078Speterstatic volatile speed_t comdefaultrate = CONSPEED; 39851078Speter#ifdef __alpha__ 39951078Speterstatic volatile speed_t gdbdefaultrate = CONSPEED; 40051078Speter#endif 40151078Speterstatic u_int com_events; /* input chars + weighted output completions */ 40251078Speterstatic Port_t siocniobase; 40366230Sjhb#ifndef __alpha__ 40451078Speterstatic int siocnunit; 40566230Sjhb#endif 40651078Speterstatic Port_t siogdbiobase; 40751078Speterstatic int siogdbunit = -1; 40872238Sjhbstatic void *sio_slow_ih; 40972238Sjhbstatic void *sio_fast_ih; 41051078Speterstatic int sio_timeout; 41151078Speterstatic int sio_timeouts_until_log; 41251078Speterstatic struct callout_handle sio_timeout_handle 41351078Speter = CALLOUT_HANDLE_INITIALIZER(&sio_timeout_handle); 41453344Speterstatic int sio_numunits; 41551078Speter 41651078Speterstatic struct speedtab comspeedtab[] = { 41751078Speter { 0, 0 }, 41851078Speter { 50, COMBRD(50) }, 41951078Speter { 75, COMBRD(75) }, 42051078Speter { 110, COMBRD(110) }, 42151078Speter { 134, COMBRD(134) }, 42251078Speter { 150, COMBRD(150) }, 42351078Speter { 200, COMBRD(200) }, 42451078Speter { 300, COMBRD(300) }, 42551078Speter { 600, COMBRD(600) }, 42651078Speter { 1200, COMBRD(1200) }, 42751078Speter { 1800, COMBRD(1800) }, 42851078Speter { 2400, COMBRD(2400) }, 42951078Speter { 4800, COMBRD(4800) }, 43051078Speter { 9600, COMBRD(9600) }, 43151078Speter { 19200, COMBRD(19200) }, 43251078Speter { 38400, COMBRD(38400) }, 43351078Speter { 57600, COMBRD(57600) }, 43451078Speter { 115200, COMBRD(115200) }, 43551078Speter { -1, -1 } 43651078Speter}; 43751078Speter 43851078Speter#ifdef COM_ESP 43951078Speter/* XXX configure this properly. */ 44051078Speterstatic Port_t likely_com_ports[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, }; 44151078Speterstatic Port_t likely_esp_ports[] = { 0x140, 0x180, 0x280, 0 }; 44251078Speter#endif 44351078Speter 44451078Speter/* 44551078Speter * handle sysctl read/write requests for console speed 44651078Speter * 44751078Speter * In addition to setting comdefaultrate for I/O through /dev/console, 44851078Speter * also set the initial and lock values for the /dev/ttyXX device 44951078Speter * if there is one associated with the console. Finally, if the /dev/tty 45051078Speter * device has already been open, change the speed on the open running port 45151078Speter * itself. 45251078Speter */ 45351078Speter 45451078Speterstatic int 45562573Sphksysctl_machdep_comdefaultrate(SYSCTL_HANDLER_ARGS) 45651078Speter{ 45751078Speter int error, s; 45851078Speter speed_t newspeed; 45951078Speter struct com_s *com; 46051078Speter struct tty *tp; 46151078Speter 46251078Speter newspeed = comdefaultrate; 46351078Speter 46451078Speter error = sysctl_handle_opaque(oidp, &newspeed, sizeof newspeed, req); 46551078Speter if (error || !req->newptr) 46651078Speter return (error); 46751078Speter 46851078Speter comdefaultrate = newspeed; 46951078Speter 47051078Speter if (comconsole < 0) /* serial console not selected? */ 47151078Speter return (0); 47251078Speter 47351078Speter com = com_addr(comconsole); 47457915Simp if (com == NULL) 47551078Speter return (ENXIO); 47651078Speter 47751078Speter /* 47851078Speter * set the initial and lock rates for /dev/ttydXX and /dev/cuaXX 47951078Speter * (note, the lock rates really are boolean -- if non-zero, disallow 48051078Speter * speed changes) 48151078Speter */ 48251078Speter com->it_in.c_ispeed = com->it_in.c_ospeed = 48351078Speter com->lt_in.c_ispeed = com->lt_in.c_ospeed = 48451078Speter com->it_out.c_ispeed = com->it_out.c_ospeed = 48551078Speter com->lt_out.c_ispeed = com->lt_out.c_ospeed = comdefaultrate; 48651078Speter 48751078Speter /* 48851078Speter * if we're open, change the running rate too 48951078Speter */ 49051078Speter tp = com->tp; 49151078Speter if (tp && (tp->t_state & TS_ISOPEN)) { 49251078Speter tp->t_termios.c_ispeed = 49351078Speter tp->t_termios.c_ospeed = comdefaultrate; 49451078Speter s = spltty(); 49551078Speter error = comparam(tp, &tp->t_termios); 49651078Speter splx(s); 49751078Speter } 49851078Speter return error; 49951078Speter} 50051078Speter 50151078SpeterSYSCTL_PROC(_machdep, OID_AUTO, conspeed, CTLTYPE_INT | CTLFLAG_RW, 50251078Speter 0, 0, sysctl_machdep_comdefaultrate, "I", ""); 50351078Speter 50453344Speter#define SET_FLAG(dev, bit) device_set_flags(dev, device_get_flags(dev) | (bit)) 50553344Speter#define CLR_FLAG(dev, bit) device_set_flags(dev, device_get_flags(dev) & ~(bit)) 50653344Speter 50751078Speter#if NCARD > 0 50852471Simpstatic int 50952471Simpsio_pccard_probe(dev) 51052471Simp device_t dev; 51151078Speter{ 51254386Simp /* Do not probe IRQ - pccard doesn't turn on the interrupt line */ 51354386Simp /* until bus_setup_intr */ 51453344Speter SET_FLAG(dev, COM_C_NOPROBE); 51553344Speter 51658885Simp return (sioprobe(dev, 0)); 51752471Simp} 51851078Speter 51952471Simpstatic int 52052471Simpsio_pccard_attach(dev) 52152471Simp device_t dev; 52252471Simp{ 52358885Simp return (sioattach(dev, 0)); 52451078Speter} 52551078Speter 52651078Speter/* 52752471Simp * sio_detach - unload the driver and clear the table. 52851078Speter * XXX TODO: 52951078Speter * This is usually called when the card is ejected, but 53051078Speter * can be caused by a modunload of a controller driver. 53151078Speter * The idea is to reset the driver's view of the device 53251078Speter * and ensure that any driver entry points such as 53351078Speter * read and write do not hang. 53451078Speter */ 53553978Simpstatic int 53652471Simpsio_pccard_detach(dev) 53752471Simp device_t dev; 53851078Speter{ 53951078Speter struct com_s *com; 54065131Sphk int i; 54151078Speter 54252471Simp com = (struct com_s *) device_get_softc(dev); 54357915Simp if (com == NULL) { 54452471Simp device_printf(dev, "NULL com in siounload\n"); 54554386Simp return (0); 54651078Speter } 54754386Simp com->gone = 1; 54865131Sphk for (i = 0 ; i < 6; i++) 54965131Sphk destroy_dev(com->devs[i]); 55054386Simp if (com->irqres) { 55154386Simp bus_teardown_intr(dev, com->irqres, com->cookie); 55254386Simp bus_release_resource(dev, SYS_RES_IRQ, 0, com->irqres); 55354386Simp } 55454386Simp if (com->ioportres) 55554386Simp bus_release_resource(dev, SYS_RES_IOPORT, 0, com->ioportres); 55651078Speter if (com->tp && (com->tp->t_state & TS_ISOPEN)) { 55757915Simp device_printf(dev, "still open, forcing close\n"); 55851078Speter com->tp->t_gen++; 55951078Speter ttyclose(com->tp); 56051078Speter ttwakeup(com->tp); 56151078Speter ttwwakeup(com->tp); 56251078Speter } else { 56351078Speter if (com->ibuf != NULL) 56451078Speter free(com->ibuf, M_DEVBUF); 56551078Speter } 56657915Simp device_printf(dev, "unloaded\n"); 56753978Simp return (0); 56851078Speter} 56951078Speter#endif /* NCARD > 0 */ 57051078Speter 57158885Simp#if NPCI > 0 57258885Simpstruct pci_ids { 57358885Simp u_int32_t type; 57458885Simp const char *desc; 57558885Simp int rid; 57658885Simp}; 57751078Speter 57858885Simpstatic struct pci_ids pci_ids[] = { 57958885Simp { 0x100812b9, "3COM PCI FaxModem", 0x10 }, 58058885Simp { 0x048011c1, "ActionTec 56k FAX PCI Modem", 0x14 }, 58176869Ssanpei { 0x0000151f, "SmartLink 5634PCV SurfRider", 0x10 }, 58258885Simp { 0x00000000, NULL, 0 } 58358885Simp}; 58458885Simp 58558885Simpstatic int 58658885Simpsio_pci_attach(dev) 58758885Simp device_t dev; 58858885Simp{ 58958885Simp u_int32_t type; 59058885Simp struct pci_ids *id; 59158885Simp 59258885Simp type = pci_get_devid(dev); 59358885Simp id = pci_ids; 59458885Simp while (id->type && id->type != type) 59558885Simp id++; 59658885Simp if (id->desc == NULL) 59758885Simp return (ENXIO); 59858885Simp sio_pci_kludge_unit(dev); 59958885Simp return (sioattach(dev, id->rid)); 60058885Simp} 60158885Simp 60258885Simp/* 60358885Simp * Don't cut and paste this to other drivers. It is a horrible kludge 60458885Simp * which will fail to work and also be unnecessary in future versions. 60558885Simp */ 60658885Simpstatic void 60758885Simpsio_pci_kludge_unit(dev) 60858885Simp device_t dev; 60958885Simp{ 61058885Simp devclass_t dc; 61158885Simp int err; 61258885Simp int start; 61358885Simp int unit; 61458885Simp 61558885Simp unit = 0; 61658885Simp start = 0; 61758885Simp while (resource_int_value("sio", unit, "port", &start) == 0 && 61858885Simp start > 0) 61958885Simp unit++; 62058885Simp if (device_get_unit(dev) < unit) { 62158885Simp dc = device_get_devclass(dev); 62258885Simp while (devclass_get_device(dc, unit)) 62358885Simp unit++; 62458885Simp device_printf(dev, "moving to sio%d\n", unit); 62558885Simp err = device_set_unit(dev, unit); /* EVIL DO NOT COPY */ 62658885Simp if (err) 62758885Simp device_printf(dev, "error moving device %d\n", err); 62858885Simp } 62958885Simp} 63058885Simp 63158885Simpstatic int 63258885Simpsio_pci_probe(dev) 63358885Simp device_t dev; 63458885Simp{ 63558885Simp u_int32_t type; 63658885Simp struct pci_ids *id; 63758885Simp 63858885Simp type = pci_get_devid(dev); 63958885Simp id = pci_ids; 64058885Simp while (id->type && id->type != type) 64158885Simp id++; 64258885Simp if (id->desc == NULL) 64358885Simp return (ENXIO); 64458885Simp device_set_desc(dev, id->desc); 64558885Simp return (sioprobe(dev, id->rid)); 64658885Simp} 64758885Simp#endif /* NPCI > 0 */ 64858885Simp 64951078Speterstatic struct isa_pnp_id sio_ids[] = { 65051078Speter {0x0005d041, "Standard PC COM port"}, /* PNP0500 */ 65151078Speter {0x0105d041, "16550A-compatible COM port"}, /* PNP0501 */ 65251078Speter {0x0205d041, "Multiport serial device (non-intelligent 16550)"}, /* PNP0502 */ 65351078Speter {0x1005d041, "Generic IRDA-compatible device"}, /* PNP0510 */ 65451078Speter {0x1105d041, "Generic IRDA-compatible device"}, /* PNP0511 */ 65554944Speter /* Devices that do not have a compatid */ 65660696Stanimura {0x12206804, NULL}, /* ACH2012 - 5634BTS 56K Video Ready Modem */ 65754944Speter {0x7602a904, NULL}, /* AEI0276 - 56K v.90 Fax Modem (LKT) */ 65854944Speter {0x00007905, NULL}, /* AKY0000 - 56K Plug&Play Modem */ 65954944Speter {0x01405407, NULL}, /* AZT4001 - AZT3000 PnP SOUND DEVICE, MODEM */ 66054944Speter {0x56039008, NULL}, /* BDP0356 - Best Data 56x2 */ 66154944Speter {0x36339008, NULL}, /* BDP3336 - Best Data Prods. 336F */ 66254944Speter {0x0014490a, NULL}, /* BRI1400 - Boca 33.6 PnP */ 66354944Speter {0x0015490a, NULL}, /* BRI1500 - Internal Fax Data */ 66454944Speter {0x0034490a, NULL}, /* BRI3400 - Internal ACF Modem */ 66565438Ssheldonh {0x0094490a, NULL}, /* BRI9400 - Boca K56Flex PnP */ 66654944Speter {0x00b4490a, NULL}, /* BRIB400 - Boca 56k PnP */ 66754944Speter {0x0030320d, NULL}, /* CIR3000 - Cirrus Logic V43 */ 66856229Speter {0x0100440e, NULL}, /* CRD0001 - Cardinal MVP288IV ? */ 66960674Stanimura {0x36033610, NULL}, /* DAV0336 - DAVICOM 336PNP MODEM */ 67059888Stanimura {0x0000aa1a, NULL}, /* FUJ0000 - FUJITSU Modem 33600 PNP/I2 */ 67154944Speter {0x1200c31e, NULL}, /* GVC0012 - VF1128HV-R9 (win modem?) */ 67254944Speter {0x0303c31e, NULL}, /* GVC0303 - MaxTech 33.6 PnP D/F/V */ 67355124Speter {0x0505c31e, NULL}, /* GVC0505 - GVC 56k Faxmodem */ 67473961Ssanpei {0x0116c31e, NULL}, /* GVC1601 - Rockwell V.34 Plug & Play Modem */ 67554944Speter {0x0050c31e, NULL}, /* GVC5000 - some GVC modem */ 67654944Speter {0x3800f91e, NULL}, /* GWY0038 - Telepath with v.90 */ 67754944Speter {0x9062f91e, NULL}, /* GWY6290 - Telepath with x2 Technology */ 67873960Ssanpei {0x8100e425, NULL}, /* IOD0081 - I-O DATA DEVICE,INC. IFML-560 */ 67966920Stanimura {0x21002534, NULL}, /* MAE0021 - Jetstream Int V.90 56k Voice Series 2*/ 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 */ 68570356Stanimura {0x08804f3f, NULL}, /* OZO8008 - Zoom (33.6k Modem) */ 68660674Stanimura {0x0f804f3f, NULL}, /* OZO800f - Zoom 2812 (56k Modem) */ 68754944Speter {0x39804f3f, NULL}, /* OZO8039 - Zoom 56k flex */ 68877222Ssanpei {0x00914f3f, NULL}, /* OZO9100 - Zoom 2919 (K56 Faxmodem) */ 68957769Speter {0x3024a341, NULL}, /* PMC2430 - Pace 56 Voice Internal Modem */ 69054944Speter {0x1000eb49, NULL}, /* ROK0010 - Rockwell ? */ 69154944Speter {0x5002734a, NULL}, /* RSS0250 - 5614Jx3(G) Internal Modem */ 69260708Stanimura {0x6202734a, NULL}, /* RSS0262 - 5614Jx3[G] V90+K56Flex Modem */ 69354944Speter {0xc100ad4d, NULL}, /* SMM00C1 - Leopard 56k PnP */ 69456229Speter {0x9012b04e, NULL}, /* SUP1290 - Supra ? */ 69554944Speter {0x1013b04e, NULL}, /* SUP1310 - SupraExpress 336i PnP */ 69654944Speter {0x8013b04e, NULL}, /* SUP1380 - SupraExpress 288i PnP Voice */ 69754944Speter {0x8113b04e, NULL}, /* SUP1381 - SupraExpress 336i PnP Voice */ 69854944Speter {0x5016b04e, NULL}, /* SUP1650 - Supra 336i Sp Intl */ 69960947Stanimura {0x7016b04e, NULL}, /* SUP1670 - Supra 336i V+ Intl */ 70054944Speter {0x7420b04e, NULL}, /* SUP2070 - Supra ? */ 70154944Speter {0x8020b04e, NULL}, /* SUP2080 - Supra ? */ 70254944Speter {0x8420b04e, NULL}, /* SUP2084 - SupraExpress 56i PnP */ 70356229Speter {0x7121b04e, NULL}, /* SUP2171 - SupraExpress 56i Sp? */ 70454944Speter {0x8024b04e, NULL}, /* SUP2480 - Supra ? */ 70554944Speter {0x01007256, NULL}, /* USR0001 - U.S. Robotics Inc., Sportster W */ 70654944Speter {0x02007256, NULL}, /* USR0002 - U.S. Robotics Inc. Sportster 33. */ 70754944Speter {0x04007256, NULL}, /* USR0004 - USR Sportster 14.4k */ 70854944Speter {0x06007256, NULL}, /* USR0006 - USR Sportster 33.6k */ 70956229Speter {0x11007256, NULL}, /* USR0011 - USR ? */ 71054944Speter {0x01017256, NULL}, /* USR0101 - USR ? */ 71154944Speter {0x30207256, NULL}, /* USR2030 - U.S.Robotics Inc. Sportster 560 */ 71254944Speter {0x50207256, NULL}, /* USR2050 - U.S.Robotics Inc. Sportster 33. */ 71354944Speter {0x70207256, NULL}, /* USR2070 - U.S.Robotics Inc. Sportster 560 */ 71454944Speter {0x30307256, NULL}, /* USR3030 - U.S. Robotics 56K FAX INT */ 71554944Speter {0x31307256, NULL}, /* USR3031 - U.S. Robotics 56K FAX INT */ 71658848Speter {0x50307256, NULL}, /* USR3050 - U.S. Robotics 56K FAX INT */ 71754944Speter {0x70307256, NULL}, /* USR3070 - U.S. Robotics 56K Voice INT */ 71856229Speter {0x90307256, NULL}, /* USR3090 - USR ? */ 71964688Speter {0x70917256, NULL}, /* USR9170 - U.S. Robotics 56K FAX INT */ 72054944Speter {0x90917256, NULL}, /* USR9190 - USR 56k Voice INT */ 72154944Speter {0x0300695c, NULL}, /* WCI0003 - Fax/Voice/Modem/Speakphone/Asvd */ 72265469Ssheldonh {0x01a0896a, NULL}, /* ZTIA001 - Zoom Internal V90 Faxmodem */ 72354944Speter {0x61f7896a, NULL}, /* ZTIF761 - Zoom ComStar 33.6 */ 72451078Speter {0} 72551078Speter}; 72651078Speter 72756229Speter 72856229Speter 72951078Speterstatic int 73052471Simpsio_isa_probe(dev) 73152471Simp device_t dev; 73252471Simp{ 73352471Simp /* Check isapnp ids */ 73452471Simp if (ISA_PNP_PROBE(device_get_parent(dev), dev, sio_ids) == ENXIO) 73552471Simp return (ENXIO); 73658885Simp return (sioprobe(dev, 0)); 73752471Simp} 73852471Simp 73952471Simpstatic int 74058885Simpsioprobe(dev, xrid) 74151078Speter device_t dev; 74258885Simp int xrid; 74351078Speter{ 74453344Speter#if 0 74551078Speter static bool_t already_init; 74653344Speter device_t xdev; 74753344Speter#endif 74860471Snyan struct com_s *com; 74951078Speter bool_t failures[10]; 75051078Speter int fn; 75151078Speter device_t idev; 75251078Speter Port_t iobase; 75351078Speter intrmask_t irqmap[4]; 75451078Speter intrmask_t irqs; 75551078Speter u_char mcr_image; 75651078Speter int result; 75754206Speter u_long xirq; 75851088Speter u_int flags = device_get_flags(dev); 75951078Speter int rid; 76051078Speter struct resource *port; 76151078Speter 76258885Simp rid = xrid; 76351078Speter port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 76451078Speter 0, ~0, IO_COMSIZE, RF_ACTIVE); 76551078Speter if (!port) 76657915Simp return (ENXIO); 76751078Speter 76860471Snyan com = device_get_softc(dev); 76960471Snyan com->bst = rman_get_bustag(port); 77060471Snyan com->bsh = rman_get_bushandle(port); 77160471Snyan 77270174Sjhb if (atomic_cmpset_int(&sio_inited, 0, 1)) 77372238Sjhb mtx_init(&sio_lock, driver_name, MTX_SPIN); 77470174Sjhb 77553344Speter#if 0 77653344Speter /* 77753344Speter * XXX this is broken - when we are first called, there are no 77853344Speter * previously configured IO ports. We could hard code 77953344Speter * 0x3f8, 0x2f8, 0x3e8, 0x2e8 etc but that's probably worse. 78053344Speter * This code has been doing nothing since the conversion since 78153344Speter * "count" is zero the first time around. 78253344Speter */ 78351078Speter if (!already_init) { 78451078Speter /* 78551078Speter * Turn off MCR_IENABLE for all likely serial ports. An unused 78651078Speter * port with its MCR_IENABLE gate open will inhibit interrupts 78751078Speter * from any used port that shares the interrupt vector. 78851078Speter * XXX the gate enable is elsewhere for some multiports. 78951078Speter */ 79051078Speter device_t *devs; 79153344Speter int count, i, xioport; 79251078Speter 79351078Speter devclass_get_devices(sio_devclass, &devs, &count); 79451078Speter for (i = 0; i < count; i++) { 79551078Speter xdev = devs[i]; 79654194Speter if (device_is_enabled(xdev) && 79754194Speter bus_get_resource(xdev, SYS_RES_IOPORT, 0, &xioport, 79854194Speter NULL) == 0) 79953344Speter outb(xioport + com_mcr, 0); 80051078Speter } 80151078Speter free(devs, M_TEMP); 80251078Speter already_init = TRUE; 80351078Speter } 80453344Speter#endif 80551078Speter 80651078Speter if (COM_LLCONSOLE(flags)) { 80751078Speter printf("sio%d: reserved for low-level i/o\n", 80851078Speter device_get_unit(dev)); 80956788Sbde bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 81051078Speter return (ENXIO); 81151078Speter } 81251078Speter 81351078Speter /* 81451078Speter * If the device is on a multiport card and has an AST/4 81551078Speter * compatible interrupt control register, initialize this 81651078Speter * register and prepare to leave MCR_IENABLE clear in the mcr. 81751078Speter * Otherwise, prepare to set MCR_IENABLE in the mcr. 81851078Speter * Point idev to the device struct giving the correct id_irq. 81951078Speter * This is the struct for the master device if there is one. 82051078Speter */ 82151078Speter idev = dev; 82251078Speter mcr_image = MCR_IENABLE; 82351078Speter#ifdef COM_MULTIPORT 82457234Sbde if (COM_ISMULTIPORT(flags)) { 82554206Speter Port_t xiobase; 82654206Speter u_long io; 82754206Speter 82851078Speter idev = devclass_get_device(sio_devclass, COM_MPMASTER(flags)); 82951078Speter if (idev == NULL) { 83051078Speter printf("sio%d: master device %d not configured\n", 83151078Speter device_get_unit(dev), COM_MPMASTER(flags)); 83251078Speter idev = dev; 83351078Speter } 83457234Sbde if (!COM_NOTAST4(flags)) { 83557234Sbde if (bus_get_resource(idev, SYS_RES_IOPORT, 0, &io, 83657234Sbde NULL) == 0) { 83757234Sbde xiobase = io; 83857234Sbde if (bus_get_resource(idev, SYS_RES_IRQ, 0, 83957234Sbde NULL, NULL) == 0) 84057234Sbde outb(xiobase + com_scr, 0x80); 84157234Sbde else 84257234Sbde outb(xiobase + com_scr, 0); 84357234Sbde } 84457234Sbde mcr_image = 0; 84551078Speter } 84651078Speter } 84751078Speter#endif /* COM_MULTIPORT */ 84854194Speter if (bus_get_resource(idev, SYS_RES_IRQ, 0, NULL, NULL) != 0) 84951078Speter mcr_image = 0; 85051078Speter 85151078Speter bzero(failures, sizeof failures); 85251078Speter iobase = rman_get_start(port); 85351078Speter 85451078Speter /* 85551078Speter * We don't want to get actual interrupts, just masked ones. 85651078Speter * Interrupts from this line should already be masked in the ICU, 85751078Speter * but mask them in the processor as well in case there are some 85851078Speter * (misconfigured) shared interrupts. 85951078Speter */ 86072200Sbmilekic mtx_lock_spin(&sio_lock); 86151078Speter/* EXTRA DELAY? */ 86251078Speter 86351078Speter /* 86451078Speter * Initialize the speed and the word size and wait long enough to 86551078Speter * drain the maximum of 16 bytes of junk in device output queues. 86651078Speter * The speed is undefined after a master reset and must be set 86751078Speter * before relying on anything related to output. There may be 86851078Speter * junk after a (very fast) soft reboot and (apparently) after 86951078Speter * master reset. 87051078Speter * XXX what about the UART bug avoided by waiting in comparam()? 87151078Speter * We don't want to to wait long enough to drain at 2 bps. 87251078Speter */ 87351078Speter if (iobase == siocniobase) 87451078Speter DELAY((16 + 1) * 1000000 / (comdefaultrate / 10)); 87551078Speter else { 87660471Snyan sio_setreg(com, com_cfcr, CFCR_DLAB | CFCR_8BITS); 87760471Snyan sio_setreg(com, com_dlbl, COMBRD(SIO_TEST_SPEED) & 0xff); 87860471Snyan sio_setreg(com, com_dlbh, (u_int) COMBRD(SIO_TEST_SPEED) >> 8); 87960471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); 88051078Speter DELAY((16 + 1) * 1000000 / (SIO_TEST_SPEED / 10)); 88151078Speter } 88251078Speter 88351078Speter /* 88451078Speter * Enable the interrupt gate and disable device interupts. This 88551078Speter * should leave the device driving the interrupt line low and 88651078Speter * guarantee an edge trigger if an interrupt can be generated. 88751078Speter */ 88851078Speter/* EXTRA DELAY? */ 88960471Snyan sio_setreg(com, com_mcr, mcr_image); 89060471Snyan sio_setreg(com, com_ier, 0); 89151078Speter DELAY(1000); /* XXX */ 89251078Speter irqmap[0] = isa_irq_pending(); 89351078Speter 89451078Speter /* 89551078Speter * Attempt to set loopback mode so that we can send a null byte 89651078Speter * without annoying any external device. 89751078Speter */ 89851078Speter/* EXTRA DELAY? */ 89960471Snyan sio_setreg(com, com_mcr, mcr_image | MCR_LOOPBACK); 90051078Speter 90151078Speter /* 90251078Speter * Attempt to generate an output interrupt. On 8250's, setting 90351078Speter * IER_ETXRDY generates an interrupt independent of the current 90451078Speter * setting and independent of whether the THR is empty. On 16450's, 90551078Speter * setting IER_ETXRDY generates an interrupt independent of the 90651078Speter * current setting. On 16550A's, setting IER_ETXRDY only 90751078Speter * generates an interrupt when IER_ETXRDY is not already set. 90851078Speter */ 90960471Snyan sio_setreg(com, com_ier, IER_ETXRDY); 91051078Speter 91151078Speter /* 91251078Speter * On some 16x50 incompatibles, setting IER_ETXRDY doesn't generate 91351078Speter * an interrupt. They'd better generate one for actually doing 91451078Speter * output. Loopback may be broken on the same incompatibles but 91551078Speter * it's unlikely to do more than allow the null byte out. 91651078Speter */ 91760471Snyan sio_setreg(com, com_data, 0); 91851078Speter DELAY((1 + 2) * 1000000 / (SIO_TEST_SPEED / 10)); 91951078Speter 92051078Speter /* 92151078Speter * Turn off loopback mode so that the interrupt gate works again 92251078Speter * (MCR_IENABLE was hidden). This should leave the device driving 92351078Speter * an interrupt line high. It doesn't matter if the interrupt 92451078Speter * line oscillates while we are not looking at it, since interrupts 92551078Speter * are disabled. 92651078Speter */ 92751078Speter/* EXTRA DELAY? */ 92860471Snyan sio_setreg(com, com_mcr, mcr_image); 92951078Speter 93051078Speter /* 93152471Simp * Some pcmcia cards have the "TXRDY bug", so we check everyone 93251078Speter * for IIR_TXRDY implementation ( Palido 321s, DC-1S... ) 93351078Speter */ 93453370Speter if (COM_NOPROBE(flags)) { 93553370Speter /* Reading IIR register twice */ 93653370Speter for (fn = 0; fn < 2; fn ++) { 93753370Speter DELAY(10000); 93860471Snyan failures[6] = sio_getreg(com, com_iir); 93953370Speter } 94053370Speter /* Check IIR_TXRDY clear ? */ 94153370Speter result = 0; 94253370Speter if (failures[6] & IIR_TXRDY) { 94353370Speter /* Nop, Double check with clearing IER */ 94460471Snyan sio_setreg(com, com_ier, 0); 94560471Snyan if (sio_getreg(com, com_iir) & IIR_NOPEND) { 94653370Speter /* Ok. we're familia this gang */ 94753370Speter SET_FLAG(dev, COM_C_IIR_TXRDYBUG); 94853370Speter } else { 94953370Speter /* Unknown, Just omit this chip.. XXX */ 95053370Speter result = ENXIO; 95153370Speter } 95251078Speter } else { 95353370Speter /* OK. this is well-known guys */ 95453370Speter CLR_FLAG(dev, COM_C_IIR_TXRDYBUG); 95551078Speter } 95660471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); 95772200Sbmilekic mtx_unlock_spin(&sio_lock); 95853344Speter bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 95953344Speter return (iobase == siocniobase ? 0 : result); 96053344Speter } 96153344Speter 96251078Speter /* 96351078Speter * Check that 96451078Speter * o the CFCR, IER and MCR in UART hold the values written to them 96551078Speter * (the values happen to be all distinct - this is good for 96651078Speter * avoiding false positive tests from bus echoes). 96751078Speter * o an output interrupt is generated and its vector is correct. 96851078Speter * o the interrupt goes away when the IIR in the UART is read. 96951078Speter */ 97051078Speter/* EXTRA DELAY? */ 97160471Snyan failures[0] = sio_getreg(com, com_cfcr) - CFCR_8BITS; 97260471Snyan failures[1] = sio_getreg(com, com_ier) - IER_ETXRDY; 97360471Snyan failures[2] = sio_getreg(com, com_mcr) - mcr_image; 97451078Speter DELAY(10000); /* Some internal modems need this time */ 97551078Speter irqmap[1] = isa_irq_pending(); 97660471Snyan failures[4] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_TXRDY; 97751078Speter DELAY(1000); /* XXX */ 97851078Speter irqmap[2] = isa_irq_pending(); 97960471Snyan failures[6] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 98051078Speter 98151078Speter /* 98251078Speter * Turn off all device interrupts and check that they go off properly. 98351078Speter * Leave MCR_IENABLE alone. For ports without a master port, it gates 98451078Speter * the OUT2 output of the UART to 98551078Speter * the ICU input. Closing the gate would give a floating ICU input 98651078Speter * (unless there is another device driving it) and spurious interrupts. 98751078Speter * (On the system that this was first tested on, the input floats high 98851078Speter * and gives a (masked) interrupt as soon as the gate is closed.) 98951078Speter */ 99060471Snyan sio_setreg(com, com_ier, 0); 99160471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); /* dummy to avoid bus echo */ 99260471Snyan failures[7] = sio_getreg(com, com_ier); 99351078Speter DELAY(1000); /* XXX */ 99451078Speter irqmap[3] = isa_irq_pending(); 99560471Snyan failures[9] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 99651078Speter 99772200Sbmilekic mtx_unlock_spin(&sio_lock); 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; 118465605Sjhb if (siosetwater(com, com->it_in.c_ispeed) != 0) { 118572200Sbmilekic mtx_unlock_spin(&sio_lock); 118656788Sbde /* 118756788Sbde * Leave i/o resources allocated if this is a `cn'-level 118856788Sbde * console, so that other devices can't snarf them. 118956788Sbde */ 119056788Sbde if (iobase != siocniobase) 119156788Sbde bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 119256788Sbde return (ENOMEM); 119351078Speter } 119472200Sbmilekic mtx_unlock_spin(&sio_lock); 119551078Speter termioschars(&com->it_in); 119651078Speter com->it_out = com->it_in; 119751078Speter 119851078Speter /* attempt to determine UART type */ 119951078Speter printf("sio%d: type", unit); 120051078Speter 120151078Speter 120251078Speter#ifdef COM_MULTIPORT 120351078Speter if (!COM_ISMULTIPORT(flags) && !COM_IIR_TXRDYBUG(flags)) 120451078Speter#else 120551078Speter if (!COM_IIR_TXRDYBUG(flags)) 120651078Speter#endif 120751078Speter { 120851078Speter u_char scr; 120951078Speter u_char scr1; 121051078Speter u_char scr2; 121151078Speter 121260471Snyan scr = sio_getreg(com, com_scr); 121360471Snyan sio_setreg(com, com_scr, 0xa5); 121460471Snyan scr1 = sio_getreg(com, com_scr); 121560471Snyan sio_setreg(com, com_scr, 0x5a); 121660471Snyan scr2 = sio_getreg(com, com_scr); 121760471Snyan sio_setreg(com, com_scr, scr); 121851078Speter if (scr1 != 0xa5 || scr2 != 0x5a) { 121951078Speter printf(" 8250"); 122051078Speter goto determined_type; 122151078Speter } 122251078Speter } 122360471Snyan sio_setreg(com, com_fifo, FIFO_ENABLE | FIFO_RX_HIGH); 122451078Speter DELAY(100); 122551078Speter com->st16650a = 0; 122651078Speter switch (inb(com->int_id_port) & IIR_FIFO_MASK) { 122751078Speter case FIFO_RX_LOW: 122851078Speter printf(" 16450"); 122951078Speter break; 123051078Speter case FIFO_RX_MEDL: 123151078Speter printf(" 16450?"); 123251078Speter break; 123351078Speter case FIFO_RX_MEDH: 123451078Speter printf(" 16550?"); 123551078Speter break; 123651078Speter case FIFO_RX_HIGH: 123751078Speter if (COM_NOFIFO(flags)) { 123851078Speter printf(" 16550A fifo disabled"); 123951078Speter } else { 124051078Speter com->hasfifo = TRUE; 124151078Speter if (COM_ST16650A(flags)) { 124251078Speter com->st16650a = 1; 124351078Speter com->tx_fifo_size = 32; 124451078Speter printf(" ST16650A"); 124551078Speter } else { 124651078Speter com->tx_fifo_size = COM_FIFOSIZE(flags); 124751078Speter printf(" 16550A"); 124851078Speter } 124951078Speter } 125051078Speter#ifdef COM_ESP 125151078Speter for (espp = likely_esp_ports; *espp != 0; espp++) 125251078Speter if (espattach(com, *espp)) { 125351078Speter com->tx_fifo_size = 1024; 125451078Speter break; 125551078Speter } 125651078Speter#endif 125751078Speter if (!com->st16650a) { 125851078Speter if (!com->tx_fifo_size) 125951078Speter com->tx_fifo_size = 16; 126051078Speter else 126151078Speter printf(" lookalike with %d bytes FIFO", 126251078Speter com->tx_fifo_size); 126351078Speter } 126451078Speter 126551078Speter break; 126651078Speter } 126751078Speter 126851078Speter#ifdef COM_ESP 126951078Speter if (com->esp) { 127051078Speter /* 127151078Speter * Set 16550 compatibility mode. 127251078Speter * We don't use the ESP_MODE_SCALE bit to increase the 127351078Speter * fifo trigger levels because we can't handle large 127451078Speter * bursts of input. 127551078Speter * XXX flow control should be set in comparam(), not here. 127651078Speter */ 127751078Speter outb(com->esp_port + ESP_CMD1, ESP_SETMODE); 127851078Speter outb(com->esp_port + ESP_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO); 127951078Speter 128051078Speter /* Set RTS/CTS flow control. */ 128151078Speter outb(com->esp_port + ESP_CMD1, ESP_SETFLOWTYPE); 128251078Speter outb(com->esp_port + ESP_CMD2, ESP_FLOW_RTS); 128351078Speter outb(com->esp_port + ESP_CMD2, ESP_FLOW_CTS); 128451078Speter 128551078Speter /* Set flow-control levels. */ 128651078Speter outb(com->esp_port + ESP_CMD1, ESP_SETRXFLOW); 128751078Speter outb(com->esp_port + ESP_CMD2, HIBYTE(768)); 128851078Speter outb(com->esp_port + ESP_CMD2, LOBYTE(768)); 128951078Speter outb(com->esp_port + ESP_CMD2, HIBYTE(512)); 129051078Speter outb(com->esp_port + ESP_CMD2, LOBYTE(512)); 129151078Speter } 129251078Speter#endif /* COM_ESP */ 129360471Snyan sio_setreg(com, com_fifo, 0); 129451078Speterdetermined_type: ; 129551078Speter 129651078Speter#ifdef COM_MULTIPORT 129751078Speter if (COM_ISMULTIPORT(flags)) { 129853344Speter device_t masterdev; 129953344Speter 130051078Speter com->multiport = TRUE; 130151078Speter printf(" (multiport"); 130251078Speter if (unit == COM_MPMASTER(flags)) 130351078Speter printf(" master"); 130451078Speter printf(")"); 130553344Speter masterdev = devclass_get_device(sio_devclass, 130653344Speter COM_MPMASTER(flags)); 130757234Sbde com->no_irq = (masterdev == NULL || bus_get_resource(masterdev, 130857234Sbde SYS_RES_IRQ, 0, NULL, NULL) != 0); 130951078Speter } 131051078Speter#endif /* COM_MULTIPORT */ 131151078Speter if (unit == comconsole) 131251078Speter printf(", console"); 131353344Speter if (COM_IIR_TXRDYBUG(flags)) 131451078Speter printf(" with a bogus IIR_TXRDY register"); 131551078Speter printf("\n"); 131651078Speter 131767551Sjhb if (sio_fast_ih == NULL) { 131872238Sjhb swi_add(&tty_ithd, "tty:sio", siopoll, NULL, SWI_TTY, 0, 131972238Sjhb &sio_fast_ih); 132072238Sjhb swi_add(&clk_ithd, "tty:sio", siopoll, NULL, SWI_TTY, 0, 132172238Sjhb &sio_slow_ih); 132251078Speter } 132365131Sphk com->devs[0] = make_dev(&sio_cdevsw, unit, 132451078Speter UID_ROOT, GID_WHEEL, 0600, "ttyd%r", unit); 132565131Sphk com->devs[1] = make_dev(&sio_cdevsw, unit | CONTROL_INIT_STATE, 132651078Speter UID_ROOT, GID_WHEEL, 0600, "ttyid%r", unit); 132765131Sphk com->devs[2] = make_dev(&sio_cdevsw, unit | CONTROL_LOCK_STATE, 132851078Speter UID_ROOT, GID_WHEEL, 0600, "ttyld%r", unit); 132965131Sphk com->devs[3] = make_dev(&sio_cdevsw, unit | CALLOUT_MASK, 133051078Speter UID_UUCP, GID_DIALER, 0660, "cuaa%r", unit); 133165131Sphk com->devs[4] = make_dev(&sio_cdevsw, 133265131Sphk unit | CALLOUT_MASK | CONTROL_INIT_STATE, 133351078Speter UID_UUCP, GID_DIALER, 0660, "cuaia%r", unit); 133465131Sphk com->devs[5] = make_dev(&sio_cdevsw, 133565131Sphk unit | CALLOUT_MASK | CONTROL_LOCK_STATE, 133651078Speter UID_UUCP, GID_DIALER, 0660, "cuala%r", unit); 133751078Speter com->flags = flags; 133851078Speter com->pps.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR; 133951078Speter pps_init(&com->pps); 134051078Speter 134151078Speter rid = 0; 134251078Speter com->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0ul, ~0ul, 1, 134353344Speter RF_ACTIVE); 134453344Speter if (com->irqres) { 134553344Speter ret = BUS_SETUP_INTR(device_get_parent(dev), dev, com->irqres, 134665557Sjasone INTR_TYPE_TTY | INTR_FAST, 134754386Simp siointr, com, &com->cookie); 134854194Speter if (ret) { 134954194Speter ret = BUS_SETUP_INTR(device_get_parent(dev), dev, 135054194Speter com->irqres, INTR_TYPE_TTY, 135154386Simp siointr, com, &com->cookie); 135254194Speter if (ret == 0) 135354194Speter device_printf(dev, "unable to activate interrupt in fast mode - using normal mode"); 135454194Speter } 135553344Speter if (ret) 135653344Speter device_printf(dev, "could not activate interrupt\n"); 135753344Speter } 135851078Speter 135951078Speter return (0); 136051078Speter} 136151078Speter 136251078Speterstatic int 136351078Spetersioopen(dev, flag, mode, p) 136451078Speter dev_t dev; 136551078Speter int flag; 136651078Speter int mode; 136751078Speter struct proc *p; 136851078Speter{ 136951078Speter struct com_s *com; 137051078Speter int error; 137151078Speter int mynor; 137251078Speter int s; 137351078Speter struct tty *tp; 137451078Speter int unit; 137551078Speter 137651078Speter mynor = minor(dev); 137751078Speter unit = MINOR_TO_UNIT(mynor); 137853344Speter com = com_addr(unit); 137953344Speter if (com == NULL) 138051078Speter return (ENXIO); 138151078Speter if (com->gone) 138251078Speter return (ENXIO); 138351078Speter if (mynor & CONTROL_MASK) 138451078Speter return (0); 138551078Speter tp = dev->si_tty = com->tp = ttymalloc(com->tp); 138651078Speter s = spltty(); 138751078Speter /* 138851078Speter * We jump to this label after all non-interrupted sleeps to pick 138951078Speter * up any changes of the device state. 139051078Speter */ 139151078Speteropen_top: 139251078Speter while (com->state & CS_DTR_OFF) { 139351078Speter error = tsleep(&com->dtr_wait, TTIPRI | PCATCH, "siodtr", 0); 139451078Speter if (com_addr(unit) == NULL) 139551078Speter return (ENXIO); 139651078Speter if (error != 0 || com->gone) 139751078Speter goto out; 139851078Speter } 139951078Speter if (tp->t_state & TS_ISOPEN) { 140051078Speter /* 140151078Speter * The device is open, so everything has been initialized. 140251078Speter * Handle conflicts. 140351078Speter */ 140451078Speter if (mynor & CALLOUT_MASK) { 140551078Speter if (!com->active_out) { 140651078Speter error = EBUSY; 140751078Speter goto out; 140851078Speter } 140951078Speter } else { 141051078Speter if (com->active_out) { 141151078Speter if (flag & O_NONBLOCK) { 141251078Speter error = EBUSY; 141351078Speter goto out; 141451078Speter } 141551078Speter error = tsleep(&com->active_out, 141651078Speter TTIPRI | PCATCH, "siobi", 0); 141751078Speter if (com_addr(unit) == NULL) 141851078Speter return (ENXIO); 141951078Speter if (error != 0 || com->gone) 142051078Speter goto out; 142151078Speter goto open_top; 142251078Speter } 142351078Speter } 142451078Speter if (tp->t_state & TS_XCLUDE && 142551078Speter suser(p)) { 142651078Speter error = EBUSY; 142751078Speter goto out; 142851078Speter } 142951078Speter } else { 143051078Speter /* 143151078Speter * The device isn't open, so there are no conflicts. 143251078Speter * Initialize it. Initialization is done twice in many 143351078Speter * cases: to preempt sleeping callin opens if we are 143451078Speter * callout, and to complete a callin open after DCD rises. 143551078Speter */ 143651078Speter tp->t_oproc = comstart; 143751078Speter tp->t_param = comparam; 143851654Sphk tp->t_stop = comstop; 143951078Speter tp->t_dev = dev; 144051078Speter tp->t_termios = mynor & CALLOUT_MASK 144151078Speter ? com->it_out : com->it_in; 144251078Speter (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET); 144351078Speter com->poll = com->no_irq; 144451078Speter com->poll_output = com->loses_outints; 144551078Speter ++com->wopeners; 144651078Speter error = comparam(tp, &tp->t_termios); 144751078Speter --com->wopeners; 144851078Speter if (error != 0) 144951078Speter goto out; 145051078Speter /* 145151078Speter * XXX we should goto open_top if comparam() slept. 145251078Speter */ 145351078Speter if (com->hasfifo) { 145451078Speter /* 145551078Speter * (Re)enable and drain fifos. 145651078Speter * 145751078Speter * Certain SMC chips cause problems if the fifos 145851078Speter * are enabled while input is ready. Turn off the 145951078Speter * fifo if necessary to clear the input. We test 146051078Speter * the input ready bit after enabling the fifos 146151078Speter * since we've already enabled them in comparam() 146251078Speter * and to handle races between enabling and fresh 146351078Speter * input. 146451078Speter */ 146551078Speter while (TRUE) { 146660471Snyan sio_setreg(com, com_fifo, 146760471Snyan FIFO_RCV_RST | FIFO_XMT_RST 146860471Snyan | com->fifo_image); 146951078Speter /* 147051078Speter * XXX the delays are for superstitious 147151078Speter * historical reasons. It must be less than 147251078Speter * the character time at the maximum 147351078Speter * supported speed (87 usec at 115200 bps 147451078Speter * 8N1). Otherwise we might loop endlessly 147551078Speter * if data is streaming in. We used to use 147651078Speter * delays of 100. That usually worked 147751078Speter * because DELAY(100) used to usually delay 147851078Speter * for about 85 usec instead of 100. 147951078Speter */ 148051078Speter DELAY(50); 148151078Speter if (!(inb(com->line_status_port) & LSR_RXRDY)) 148251078Speter break; 148360471Snyan sio_setreg(com, com_fifo, 0); 148451078Speter DELAY(50); 148551078Speter (void) inb(com->data_port); 148651078Speter } 148751078Speter } 148851078Speter 148972200Sbmilekic mtx_lock_spin(&sio_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 } 150172200Sbmilekic mtx_unlock_spin(&sio_lock); 150251078Speter /* 150351078Speter * Handle initial DCD. Callout devices get a fake initial 150451078Speter * DCD (trapdoor DCD). If we are callout, then any sleeping 150551078Speter * callin opens get woken up and resume sleeping on "siobi" 150651078Speter * instead of "siodcd". 150751078Speter */ 150851078Speter /* 150951078Speter * XXX `mynor & CALLOUT_MASK' should be 151051078Speter * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where 151151078Speter * TRAPDOOR_CARRIER is the default initial state for callout 151251078Speter * devices and SOFT_CARRIER is like CLOCAL except it hides 151351078Speter * the true carrier. 151451078Speter */ 151551078Speter if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK) 151651078Speter (*linesw[tp->t_line].l_modem)(tp, 1); 151751078Speter } 151851078Speter /* 151951078Speter * Wait for DCD if necessary. 152051078Speter */ 152151078Speter if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK) 152251078Speter && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) { 152351078Speter ++com->wopeners; 152451078Speter error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "siodcd", 0); 152551078Speter if (com_addr(unit) == NULL) 152651078Speter return (ENXIO); 152751078Speter --com->wopeners; 152851078Speter if (error != 0 || com->gone) 152951078Speter goto out; 153051078Speter goto open_top; 153151078Speter } 153251078Speter error = (*linesw[tp->t_line].l_open)(dev, tp); 153351078Speter disc_optim(tp, &tp->t_termios, com); 153451078Speter if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK) 153551078Speter com->active_out = TRUE; 153651078Speter siosettimeout(); 153751078Speterout: 153851078Speter splx(s); 153951078Speter if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0) 154051078Speter comhardclose(com); 154151078Speter return (error); 154251078Speter} 154351078Speter 154451078Speterstatic int 154551078Spetersioclose(dev, flag, mode, p) 154651078Speter dev_t dev; 154751078Speter int flag; 154851078Speter int mode; 154951078Speter struct proc *p; 155051078Speter{ 155151078Speter struct com_s *com; 155251078Speter int mynor; 155351078Speter int s; 155451078Speter struct tty *tp; 155551078Speter 155651078Speter mynor = minor(dev); 155751078Speter if (mynor & CONTROL_MASK) 155851078Speter return (0); 155951078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 156057915Simp if (com == NULL) 156157915Simp return (ENODEV); 156251078Speter tp = com->tp; 156351078Speter s = spltty(); 156451078Speter (*linesw[tp->t_line].l_close)(tp, flag); 156551078Speter disc_optim(tp, &tp->t_termios, com); 156651654Sphk comstop(tp, FREAD | FWRITE); 156751078Speter comhardclose(com); 156851078Speter ttyclose(tp); 156951078Speter siosettimeout(); 157051078Speter splx(s); 157151078Speter if (com->gone) { 157251078Speter printf("sio%d: gone\n", com->unit); 157351078Speter s = spltty(); 157451078Speter if (com->ibuf != NULL) 157551078Speter free(com->ibuf, M_DEVBUF); 157651078Speter bzero(tp, sizeof *tp); 157751078Speter splx(s); 157851078Speter } 157951078Speter return (0); 158051078Speter} 158151078Speter 158251078Speterstatic void 158351078Spetercomhardclose(com) 158451078Speter struct com_s *com; 158551078Speter{ 158651078Speter int s; 158751078Speter struct tty *tp; 158851078Speter int unit; 158951078Speter 159051078Speter unit = com->unit; 159151078Speter s = spltty(); 159251078Speter com->poll = FALSE; 159351078Speter com->poll_output = FALSE; 159451078Speter com->do_timestamp = FALSE; 159551078Speter com->do_dcd_timestamp = FALSE; 159651078Speter com->pps.ppsparam.mode = 0; 159760471Snyan sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 159851078Speter { 159960471Snyan sio_setreg(com, com_ier, 0); 160051078Speter tp = com->tp; 160151078Speter if (tp->t_cflag & HUPCL 160251078Speter /* 160351078Speter * XXX we will miss any carrier drop between here and the 160451078Speter * next open. Perhaps we should watch DCD even when the 160551078Speter * port is closed; it is not sufficient to check it at 160651078Speter * the next open because it might go up and down while 160751078Speter * we're not watching. 160851078Speter */ 160951078Speter || (!com->active_out 161051078Speter && !(com->prev_modem_status & MSR_DCD) 161151078Speter && !(com->it_in.c_cflag & CLOCAL)) 161251078Speter || !(tp->t_state & TS_ISOPEN)) { 161351078Speter (void)commctl(com, TIOCM_DTR, DMBIC); 161451078Speter if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) { 161551078Speter timeout(siodtrwakeup, com, com->dtr_wait); 161651078Speter com->state |= CS_DTR_OFF; 161751078Speter } 161851078Speter } 161951078Speter } 162051078Speter if (com->hasfifo) { 162151078Speter /* 162251078Speter * Disable fifos so that they are off after controlled 162351078Speter * reboots. Some BIOSes fail to detect 16550s when the 162451078Speter * fifos are enabled. 162551078Speter */ 162660471Snyan sio_setreg(com, com_fifo, 0); 162751078Speter } 162851078Speter com->active_out = FALSE; 162951078Speter wakeup(&com->active_out); 163051078Speter wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */ 163151078Speter splx(s); 163251078Speter} 163351078Speter 163451078Speterstatic int 163551078Spetersioread(dev, uio, flag) 163651078Speter dev_t dev; 163751078Speter struct uio *uio; 163851078Speter int flag; 163951078Speter{ 164051078Speter int mynor; 164151078Speter struct com_s *com; 164251078Speter 164351078Speter mynor = minor(dev); 164451078Speter if (mynor & CONTROL_MASK) 164551078Speter return (ENODEV); 164651078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 164757915Simp if (com == NULL || com->gone) 164851078Speter return (ENODEV); 164951078Speter return ((*linesw[com->tp->t_line].l_read)(com->tp, uio, flag)); 165051078Speter} 165151078Speter 165251078Speterstatic int 165351078Spetersiowrite(dev, uio, flag) 165451078Speter dev_t dev; 165551078Speter struct uio *uio; 165651078Speter int flag; 165751078Speter{ 165851078Speter int mynor; 165951078Speter struct com_s *com; 166051078Speter int unit; 166151078Speter 166251078Speter mynor = minor(dev); 166351078Speter if (mynor & CONTROL_MASK) 166451078Speter return (ENODEV); 166551078Speter 166651078Speter unit = MINOR_TO_UNIT(mynor); 166751078Speter com = com_addr(unit); 166857915Simp if (com == NULL || com->gone) 166951078Speter return (ENODEV); 167051078Speter /* 167151078Speter * (XXX) We disallow virtual consoles if the physical console is 167251078Speter * a serial port. This is in case there is a display attached that 167351078Speter * is not the console. In that situation we don't need/want the X 167451078Speter * server taking over the console. 167551078Speter */ 167651078Speter if (constty != NULL && unit == comconsole) 167751078Speter constty = NULL; 167851078Speter return ((*linesw[com->tp->t_line].l_write)(com->tp, uio, flag)); 167951078Speter} 168051078Speter 168151078Speterstatic void 168251078Spetersiobusycheck(chan) 168351078Speter void *chan; 168451078Speter{ 168551078Speter struct com_s *com; 168651078Speter int s; 168751078Speter 168851078Speter com = (struct com_s *)chan; 168951078Speter 169051078Speter /* 169151078Speter * Clear TS_BUSY if low-level output is complete. 169251078Speter * spl locking is sufficient because siointr1() does not set CS_BUSY. 169351078Speter * If siointr1() clears CS_BUSY after we look at it, then we'll get 169451078Speter * called again. Reading the line status port outside of siointr1() 169551078Speter * is safe because CS_BUSY is clear so there are no output interrupts 169651078Speter * to lose. 169751078Speter */ 169851078Speter s = spltty(); 169951078Speter if (com->state & CS_BUSY) 170051078Speter com->extra_state &= ~CSE_BUSYCHECK; /* False alarm. */ 170151078Speter else if ((inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY)) 170251078Speter == (LSR_TSRE | LSR_TXRDY)) { 170351078Speter com->tp->t_state &= ~TS_BUSY; 170451078Speter ttwwakeup(com->tp); 170551078Speter com->extra_state &= ~CSE_BUSYCHECK; 170651078Speter } else 170751078Speter timeout(siobusycheck, com, hz / 100); 170851078Speter splx(s); 170951078Speter} 171051078Speter 171151078Speterstatic void 171251078Spetersiodtrwakeup(chan) 171351078Speter void *chan; 171451078Speter{ 171551078Speter struct com_s *com; 171651078Speter 171751078Speter com = (struct com_s *)chan; 171851078Speter com->state &= ~CS_DTR_OFF; 171951078Speter wakeup(&com->dtr_wait); 172051078Speter} 172151078Speter 172265557Sjasone/* 172370174Sjhb * Call this function with the sio_lock mutex held. It will return with the 172470174Sjhb * 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; 173551078Speter 173651078Speter buf = com->ibuf; 173751078Speter tp = com->tp; 173851078Speter if (!(tp->t_state & TS_ISOPEN) || !(tp->t_cflag & CREAD)) { 173951078Speter com_events -= (com->iptr - com->ibuf); 174051078Speter com->iptr = com->ibuf; 174151078Speter return; 174251078Speter } 174351078Speter if (tp->t_state & TS_CAN_BYPASS_L_RINT) { 174451078Speter /* 174551078Speter * Avoid the grotesquely inefficient lineswitch routine 174651078Speter * (ttyinput) in "raw" mode. It usually takes about 450 174751078Speter * instructions (that's without canonical processing or echo!). 174851078Speter * slinput is reasonably fast (usually 40 instructions plus 174951078Speter * call overhead). 175051078Speter */ 175151078Speter do { 175265557Sjasone /* 175365557Sjasone * This may look odd, but it is using save-and-enable 175465557Sjasone * semantics instead of the save-and-disable semantics 175565557Sjasone * that are used everywhere else. 175665557Sjasone */ 175772200Sbmilekic mtx_unlock_spin(&sio_lock); 175851078Speter incc = com->iptr - buf; 175951078Speter if (tp->t_rawq.c_cc + incc > tp->t_ihiwat 176051078Speter && (com->state & CS_RTS_IFLOW 176151078Speter || tp->t_iflag & IXOFF) 176251078Speter && !(tp->t_state & TS_TBLOCK)) 176351078Speter ttyblock(tp); 176451078Speter com->delta_error_counts[CE_TTY_BUF_OVERFLOW] 176551078Speter += b_to_q((char *)buf, incc, &tp->t_rawq); 176651078Speter buf += incc; 176751078Speter tk_nin += incc; 176851078Speter tk_rawcc += incc; 176951078Speter tp->t_rawcc += incc; 177051078Speter ttwakeup(tp); 177151078Speter if (tp->t_state & TS_TTSTOP 177251078Speter && (tp->t_iflag & IXANY 177351078Speter || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) { 177451078Speter tp->t_state &= ~TS_TTSTOP; 177551078Speter tp->t_lflag &= ~FLUSHO; 177651078Speter comstart(tp); 177751078Speter } 177872200Sbmilekic mtx_lock_spin(&sio_lock); 177951078Speter } while (buf < com->iptr); 178051078Speter } else { 178151078Speter do { 178265557Sjasone /* 178365557Sjasone * This may look odd, but it is using save-and-enable 178465557Sjasone * semantics instead of the save-and-disable semantics 178565557Sjasone * that are used everywhere else. 178665557Sjasone */ 178772200Sbmilekic mtx_unlock_spin(&sio_lock); 178851078Speter line_status = buf[com->ierroff]; 178951078Speter recv_data = *buf++; 179051078Speter if (line_status 179151078Speter & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) { 179251078Speter if (line_status & LSR_BI) 179351078Speter recv_data |= TTY_BI; 179451078Speter if (line_status & LSR_FE) 179551078Speter recv_data |= TTY_FE; 179651078Speter if (line_status & LSR_OE) 179751078Speter recv_data |= TTY_OE; 179851078Speter if (line_status & LSR_PE) 179951078Speter recv_data |= TTY_PE; 180051078Speter } 180151078Speter (*linesw[tp->t_line].l_rint)(recv_data, tp); 180272200Sbmilekic mtx_lock_spin(&sio_lock); 180351078Speter } while (buf < com->iptr); 180451078Speter } 180551078Speter com_events -= (com->iptr - com->ibuf); 180651078Speter com->iptr = com->ibuf; 180751078Speter 180851078Speter /* 180951078Speter * There is now room for another low-level buffer full of input, 181051078Speter * so enable RTS if it is now disabled and there is room in the 181151078Speter * high-level buffer. 181251078Speter */ 181351078Speter if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & MCR_RTS) && 181451078Speter !(tp->t_state & TS_TBLOCK)) 181551078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 181651078Speter} 181751078Speter 181851078Spetervoid 181951078Spetersiointr(arg) 182051078Speter void *arg; 182151078Speter{ 182270174Sjhb struct com_s *com; 182370174Sjhb 182451078Speter#ifndef COM_MULTIPORT 182570174Sjhb com = (struct com_s *)arg; 182670174Sjhb 182772200Sbmilekic mtx_lock_spin(&sio_lock); 182870174Sjhb siointr1(com); 182972200Sbmilekic mtx_unlock_spin(&sio_lock); 183051078Speter#else /* COM_MULTIPORT */ 183151078Speter bool_t possibly_more_intrs; 183251078Speter int unit; 183351078Speter 183451078Speter /* 183551078Speter * Loop until there is no activity on any port. This is necessary 183651078Speter * to get an interrupt edge more than to avoid another interrupt. 183751078Speter * If the IRQ signal is just an OR of the IRQ signals from several 183851078Speter * devices, then the edge from one may be lost because another is 183951078Speter * on. 184051078Speter */ 184172200Sbmilekic mtx_lock_spin(&sio_lock); 184251078Speter do { 184351078Speter possibly_more_intrs = FALSE; 184453344Speter for (unit = 0; unit < sio_numunits; ++unit) { 184551078Speter com = com_addr(unit); 184651078Speter /* 184751078Speter * XXX COM_LOCK(); 184851078Speter * would it work here, or be counter-productive? 184951078Speter */ 185051078Speter if (com != NULL 185151078Speter && !com->gone 185251078Speter && (inb(com->int_id_port) & IIR_IMASK) 185351078Speter != IIR_NOPEND) { 185451078Speter siointr1(com); 185551078Speter possibly_more_intrs = TRUE; 185651078Speter } 185751078Speter /* XXX COM_UNLOCK(); */ 185851078Speter } 185951078Speter } while (possibly_more_intrs); 186072200Sbmilekic mtx_unlock_spin(&sio_lock); 186151078Speter#endif /* COM_MULTIPORT */ 186251078Speter} 186351078Speter 186451078Speterstatic void 186551078Spetersiointr1(com) 186651078Speter struct com_s *com; 186751078Speter{ 186851078Speter u_char line_status; 186951078Speter u_char modem_status; 187051078Speter u_char *ioptr; 187151078Speter u_char recv_data; 187251078Speter u_char int_ctl; 187351078Speter u_char int_ctl_new; 187451078Speter struct timecounter *tc; 187551078Speter u_int count; 187651078Speter 187751078Speter int_ctl = inb(com->intr_ctl_port); 187851078Speter int_ctl_new = int_ctl; 187951078Speter 188051078Speter while (!com->gone) { 188151078Speter if (com->pps.ppsparam.mode & PPS_CAPTUREBOTH) { 188251078Speter modem_status = inb(com->modem_status_port); 188351078Speter if ((modem_status ^ com->last_modem_status) & MSR_DCD) { 188451078Speter tc = timecounter; 188551078Speter count = tc->tc_get_timecount(tc); 188651078Speter pps_event(&com->pps, tc, count, 188751078Speter (modem_status & MSR_DCD) ? 188851078Speter PPS_CAPTUREASSERT : PPS_CAPTURECLEAR); 188951078Speter } 189051078Speter } 189151078Speter line_status = inb(com->line_status_port); 189251078Speter 189351078Speter /* input event? (check first to help avoid overruns) */ 189451078Speter while (line_status & LSR_RCV_MASK) { 189551078Speter /* break/unnattached error bits or real input? */ 189651078Speter if (!(line_status & LSR_RXRDY)) 189751078Speter recv_data = 0; 189851078Speter else 189951078Speter recv_data = inb(com->data_port); 190061649Sps#if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER) 190161649Sps /* 190261649Sps * Solaris implements a new BREAK which is initiated 190361649Sps * by a character sequence CR ~ ^b which is similar 190461649Sps * to a familiar pattern used on Sun servers by the 190561649Sps * Remote Console. 190661649Sps */ 190761649Sps#define KEY_CRTLB 2 /* ^B */ 190861649Sps#define KEY_CR 13 /* CR '\r' */ 190961649Sps#define KEY_TILDE 126 /* ~ */ 191061649Sps 191161649Sps if (com->unit == comconsole) { 191261649Sps static int brk_state1 = 0, brk_state2 = 0; 191361649Sps if (recv_data == KEY_CR) { 191461649Sps brk_state1 = recv_data; 191561649Sps brk_state2 = 0; 191665557Sjasone } else if (brk_state1 == KEY_CR 191765557Sjasone && (recv_data == KEY_TILDE 191865557Sjasone || recv_data == KEY_CRTLB)) { 191961649Sps if (recv_data == KEY_TILDE) 192061649Sps brk_state2 = recv_data; 192165557Sjasone else if (brk_state2 == KEY_TILDE 192265557Sjasone && recv_data == KEY_CRTLB) { 192361649Sps breakpoint(); 192465557Sjasone brk_state1 = 0; 192565557Sjasone brk_state2 = 0; 192661649Sps goto cont; 192761649Sps } else 192861649Sps brk_state2 = 0; 192961649Sps } else 193061649Sps brk_state1 = 0; 193161649Sps } 193261649Sps#endif 193351078Speter if (line_status & (LSR_BI | LSR_FE | LSR_PE)) { 193451078Speter /* 193551078Speter * Don't store BI if IGNBRK or FE/PE if IGNPAR. 193651078Speter * Otherwise, push the work to a higher level 193751078Speter * (to handle PARMRK) if we're bypassing. 193851078Speter * Otherwise, convert BI/FE and PE+INPCK to 0. 193951078Speter * 194051078Speter * This makes bypassing work right in the 194151078Speter * usual "raw" case (IGNBRK set, and IGNPAR 194251078Speter * and INPCK clear). 194351078Speter * 194451078Speter * Note: BI together with FE/PE means just BI. 194551078Speter */ 194651078Speter if (line_status & LSR_BI) { 194751078Speter#if defined(DDB) && defined(BREAK_TO_DEBUGGER) 194851078Speter if (com->unit == comconsole) { 194951078Speter breakpoint(); 195051078Speter goto cont; 195151078Speter } 195251078Speter#endif 195351078Speter if (com->tp == NULL 195451078Speter || com->tp->t_iflag & IGNBRK) 195551078Speter goto cont; 195651078Speter } else { 195751078Speter if (com->tp == NULL 195851078Speter || com->tp->t_iflag & IGNPAR) 195951078Speter goto cont; 196051078Speter } 196151078Speter if (com->tp->t_state & TS_CAN_BYPASS_L_RINT 196251078Speter && (line_status & (LSR_BI | LSR_FE) 196351078Speter || com->tp->t_iflag & INPCK)) 196451078Speter recv_data = 0; 196551078Speter } 196651078Speter ++com->bytes_in; 196751078Speter if (com->hotchar != 0 && recv_data == com->hotchar) 196872238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 196951078Speter ioptr = com->iptr; 197051078Speter if (ioptr >= com->ibufend) 197151078Speter CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW); 197251078Speter else { 197351078Speter if (com->do_timestamp) 197451078Speter microtime(&com->timestamp); 197551078Speter ++com_events; 197672238Sjhb swi_sched(sio_slow_ih, SWI_DELAY); 197751078Speter#if 0 /* for testing input latency vs efficiency */ 197851078Speterif (com->iptr - com->ibuf == 8) 197972238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 198051078Speter#endif 198151078Speter ioptr[0] = recv_data; 198251078Speter ioptr[com->ierroff] = line_status; 198351078Speter com->iptr = ++ioptr; 198451078Speter if (ioptr == com->ihighwater 198551078Speter && com->state & CS_RTS_IFLOW) 198651078Speter outb(com->modem_ctl_port, 198751078Speter com->mcr_image &= ~MCR_RTS); 198851078Speter if (line_status & LSR_OE) 198951078Speter CE_RECORD(com, CE_OVERRUN); 199051078Speter } 199151078Spetercont: 199251078Speter /* 199351078Speter * "& 0x7F" is to avoid the gcc-1.40 generating a slow 199451078Speter * jump from the top of the loop to here 199551078Speter */ 199651078Speter line_status = inb(com->line_status_port) & 0x7F; 199751078Speter } 199851078Speter 199951078Speter /* modem status change? (always check before doing output) */ 200051078Speter modem_status = inb(com->modem_status_port); 200151078Speter if (modem_status != com->last_modem_status) { 200251078Speter if (com->do_dcd_timestamp 200351078Speter && !(com->last_modem_status & MSR_DCD) 200451078Speter && modem_status & MSR_DCD) 200551078Speter microtime(&com->dcd_timestamp); 200651078Speter 200751078Speter /* 200851078Speter * Schedule high level to handle DCD changes. Note 200951078Speter * that we don't use the delta bits anywhere. Some 201051078Speter * UARTs mess them up, and it's easy to remember the 201151078Speter * previous bits and calculate the delta. 201251078Speter */ 201351078Speter com->last_modem_status = modem_status; 201451078Speter if (!(com->state & CS_CHECKMSR)) { 201551078Speter com_events += LOTS_OF_EVENTS; 201651078Speter com->state |= CS_CHECKMSR; 201772238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 201851078Speter } 201951078Speter 202051078Speter /* handle CTS change immediately for crisp flow ctl */ 202151078Speter if (com->state & CS_CTS_OFLOW) { 202251078Speter if (modem_status & MSR_CTS) 202351078Speter com->state |= CS_ODEVREADY; 202451078Speter else 202551078Speter com->state &= ~CS_ODEVREADY; 202651078Speter } 202751078Speter } 202851078Speter 202951078Speter /* output queued and everything ready? */ 203051078Speter if (line_status & LSR_TXRDY 203151078Speter && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) { 203251078Speter ioptr = com->obufq.l_head; 203351078Speter if (com->tx_fifo_size > 1) { 203451078Speter u_int ocount; 203551078Speter 203651078Speter ocount = com->obufq.l_tail - ioptr; 203751078Speter if (ocount > com->tx_fifo_size) 203851078Speter ocount = com->tx_fifo_size; 203951078Speter com->bytes_out += ocount; 204051078Speter do 204151078Speter outb(com->data_port, *ioptr++); 204251078Speter while (--ocount != 0); 204351078Speter } else { 204451078Speter outb(com->data_port, *ioptr++); 204551078Speter ++com->bytes_out; 204651078Speter } 204751078Speter com->obufq.l_head = ioptr; 204851078Speter if (COM_IIR_TXRDYBUG(com->flags)) { 204951078Speter int_ctl_new = int_ctl | IER_ETXRDY; 205051078Speter } 205151078Speter if (ioptr >= com->obufq.l_tail) { 205251078Speter struct lbq *qp; 205351078Speter 205451078Speter qp = com->obufq.l_next; 205551078Speter qp->l_queued = FALSE; 205651078Speter qp = qp->l_next; 205751078Speter if (qp != NULL) { 205851078Speter com->obufq.l_head = qp->l_head; 205951078Speter com->obufq.l_tail = qp->l_tail; 206051078Speter com->obufq.l_next = qp; 206151078Speter } else { 206251078Speter /* output just completed */ 206353344Speter if (COM_IIR_TXRDYBUG(com->flags)) { 206451078Speter int_ctl_new = int_ctl & ~IER_ETXRDY; 206551078Speter } 206651078Speter com->state &= ~CS_BUSY; 206751078Speter } 206851078Speter if (!(com->state & CS_ODONE)) { 206951078Speter com_events += LOTS_OF_EVENTS; 207051078Speter com->state |= CS_ODONE; 207167551Sjhb /* handle at high level ASAP */ 207272238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 207351078Speter } 207451078Speter } 207553344Speter if (COM_IIR_TXRDYBUG(com->flags) && (int_ctl != int_ctl_new)) { 207651078Speter outb(com->intr_ctl_port, int_ctl_new); 207751078Speter } 207851078Speter } 207951078Speter 208051078Speter /* finished? */ 208151078Speter#ifndef COM_MULTIPORT 208251078Speter if ((inb(com->int_id_port) & IIR_IMASK) == IIR_NOPEND) 208351078Speter#endif /* COM_MULTIPORT */ 208451078Speter return; 208551078Speter } 208651078Speter} 208751078Speter 208851078Speterstatic int 208951078Spetersioioctl(dev, cmd, data, flag, p) 209051078Speter dev_t dev; 209151078Speter u_long cmd; 209251078Speter caddr_t data; 209351078Speter int flag; 209451078Speter struct proc *p; 209551078Speter{ 209651078Speter struct com_s *com; 209751078Speter int error; 209851078Speter int mynor; 209951078Speter int s; 210051078Speter struct tty *tp; 210151078Speter#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 210251078Speter u_long oldcmd; 210351078Speter struct termios term; 210451078Speter#endif 210551078Speter 210651078Speter mynor = minor(dev); 210751078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 210857915Simp if (com == NULL || com->gone) 210951078Speter return (ENODEV); 211051078Speter if (mynor & CONTROL_MASK) { 211151078Speter struct termios *ct; 211251078Speter 211351078Speter switch (mynor & CONTROL_MASK) { 211451078Speter case CONTROL_INIT_STATE: 211551078Speter ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in; 211651078Speter break; 211751078Speter case CONTROL_LOCK_STATE: 211851078Speter ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in; 211951078Speter break; 212051078Speter default: 212151078Speter return (ENODEV); /* /dev/nodev */ 212251078Speter } 212351078Speter switch (cmd) { 212451078Speter case TIOCSETA: 212551078Speter error = suser(p); 212651078Speter if (error != 0) 212751078Speter return (error); 212851078Speter *ct = *(struct termios *)data; 212951078Speter return (0); 213051078Speter case TIOCGETA: 213151078Speter *(struct termios *)data = *ct; 213251078Speter return (0); 213351078Speter case TIOCGETD: 213451078Speter *(int *)data = TTYDISC; 213551078Speter return (0); 213651078Speter case TIOCGWINSZ: 213751078Speter bzero(data, sizeof(struct winsize)); 213851078Speter return (0); 213951078Speter default: 214051078Speter return (ENOTTY); 214151078Speter } 214251078Speter } 214351078Speter tp = com->tp; 214451078Speter#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 214551078Speter term = tp->t_termios; 214651078Speter oldcmd = cmd; 214751078Speter error = ttsetcompat(tp, &cmd, data, &term); 214851078Speter if (error != 0) 214951078Speter return (error); 215051078Speter if (cmd != oldcmd) 215151078Speter data = (caddr_t)&term; 215251078Speter#endif 215351078Speter if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) { 215451078Speter int cc; 215551078Speter struct termios *dt = (struct termios *)data; 215651078Speter struct termios *lt = mynor & CALLOUT_MASK 215751078Speter ? &com->lt_out : &com->lt_in; 215851078Speter 215951078Speter dt->c_iflag = (tp->t_iflag & lt->c_iflag) 216051078Speter | (dt->c_iflag & ~lt->c_iflag); 216151078Speter dt->c_oflag = (tp->t_oflag & lt->c_oflag) 216251078Speter | (dt->c_oflag & ~lt->c_oflag); 216351078Speter dt->c_cflag = (tp->t_cflag & lt->c_cflag) 216451078Speter | (dt->c_cflag & ~lt->c_cflag); 216551078Speter dt->c_lflag = (tp->t_lflag & lt->c_lflag) 216651078Speter | (dt->c_lflag & ~lt->c_lflag); 216751078Speter for (cc = 0; cc < NCCS; ++cc) 216851078Speter if (lt->c_cc[cc] != 0) 216951078Speter dt->c_cc[cc] = tp->t_cc[cc]; 217051078Speter if (lt->c_ispeed != 0) 217151078Speter dt->c_ispeed = tp->t_ispeed; 217251078Speter if (lt->c_ospeed != 0) 217351078Speter dt->c_ospeed = tp->t_ospeed; 217451078Speter } 217551078Speter error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 217651078Speter if (error != ENOIOCTL) 217751078Speter return (error); 217851078Speter s = spltty(); 217951078Speter error = ttioctl(tp, cmd, data, flag); 218051078Speter disc_optim(tp, &tp->t_termios, com); 218151078Speter if (error != ENOIOCTL) { 218251078Speter splx(s); 218351078Speter return (error); 218451078Speter } 218551078Speter switch (cmd) { 218651078Speter case TIOCSBRK: 218760471Snyan sio_setreg(com, com_cfcr, com->cfcr_image |= CFCR_SBREAK); 218851078Speter break; 218951078Speter case TIOCCBRK: 219060471Snyan sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 219151078Speter break; 219251078Speter case TIOCSDTR: 219351078Speter (void)commctl(com, TIOCM_DTR, DMBIS); 219451078Speter break; 219551078Speter case TIOCCDTR: 219651078Speter (void)commctl(com, TIOCM_DTR, DMBIC); 219751078Speter break; 219851078Speter /* 219951078Speter * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The 220051078Speter * changes get undone on the next call to comparam(). 220151078Speter */ 220251078Speter case TIOCMSET: 220351078Speter (void)commctl(com, *(int *)data, DMSET); 220451078Speter break; 220551078Speter case TIOCMBIS: 220651078Speter (void)commctl(com, *(int *)data, DMBIS); 220751078Speter break; 220851078Speter case TIOCMBIC: 220951078Speter (void)commctl(com, *(int *)data, DMBIC); 221051078Speter break; 221151078Speter case TIOCMGET: 221251078Speter *(int *)data = commctl(com, 0, DMGET); 221351078Speter break; 221451078Speter case TIOCMSDTRWAIT: 221551078Speter /* must be root since the wait applies to following logins */ 221651078Speter error = suser(p); 221751078Speter if (error != 0) { 221851078Speter splx(s); 221951078Speter return (error); 222051078Speter } 222151078Speter com->dtr_wait = *(int *)data * hz / 100; 222251078Speter break; 222351078Speter case TIOCMGDTRWAIT: 222451078Speter *(int *)data = com->dtr_wait * 100 / hz; 222551078Speter break; 222651078Speter case TIOCTIMESTAMP: 222751078Speter com->do_timestamp = TRUE; 222851078Speter *(struct timeval *)data = com->timestamp; 222951078Speter break; 223051078Speter case TIOCDCDTIMESTAMP: 223151078Speter com->do_dcd_timestamp = TRUE; 223251078Speter *(struct timeval *)data = com->dcd_timestamp; 223351078Speter break; 223451078Speter default: 223551078Speter splx(s); 223651078Speter error = pps_ioctl(cmd, data, &com->pps); 223751078Speter if (error == ENODEV) 223851078Speter error = ENOTTY; 223951078Speter return (error); 224051078Speter } 224151078Speter splx(s); 224251078Speter return (0); 224351078Speter} 224451078Speter 224565557Sjasone/* software interrupt handler for SWI_TTY */ 224651078Speterstatic void 224767551Sjhbsiopoll(void *dummy) 224851078Speter{ 224951078Speter int unit; 225051078Speter 225151078Speter if (com_events == 0) 225251078Speter return; 225351078Speterrepeat: 225453344Speter for (unit = 0; unit < sio_numunits; ++unit) { 225551078Speter struct com_s *com; 225651078Speter int incc; 225751078Speter struct tty *tp; 225851078Speter 225951078Speter com = com_addr(unit); 226051078Speter if (com == NULL) 226151078Speter continue; 226251078Speter tp = com->tp; 226351078Speter if (tp == NULL || com->gone) { 226451078Speter /* 226551078Speter * Discard any events related to never-opened or 226651078Speter * going-away devices. 226751078Speter */ 226872200Sbmilekic mtx_lock_spin(&sio_lock); 226951078Speter incc = com->iptr - com->ibuf; 227051078Speter com->iptr = com->ibuf; 227151078Speter if (com->state & CS_CHECKMSR) { 227251078Speter incc += LOTS_OF_EVENTS; 227351078Speter com->state &= ~CS_CHECKMSR; 227451078Speter } 227551078Speter com_events -= incc; 227672200Sbmilekic mtx_unlock_spin(&sio_lock); 227751078Speter continue; 227851078Speter } 227951078Speter if (com->iptr != com->ibuf) { 228072200Sbmilekic mtx_lock_spin(&sio_lock); 228151078Speter sioinput(com); 228272200Sbmilekic mtx_unlock_spin(&sio_lock); 228351078Speter } 228451078Speter if (com->state & CS_CHECKMSR) { 228551078Speter u_char delta_modem_status; 228651078Speter 228772200Sbmilekic mtx_lock_spin(&sio_lock); 228851078Speter delta_modem_status = com->last_modem_status 228951078Speter ^ com->prev_modem_status; 229051078Speter com->prev_modem_status = com->last_modem_status; 229151078Speter com_events -= LOTS_OF_EVENTS; 229251078Speter com->state &= ~CS_CHECKMSR; 229372200Sbmilekic mtx_unlock_spin(&sio_lock); 229451078Speter if (delta_modem_status & MSR_DCD) 229551078Speter (*linesw[tp->t_line].l_modem) 229651078Speter (tp, com->prev_modem_status & MSR_DCD); 229751078Speter } 229851078Speter if (com->state & CS_ODONE) { 229972200Sbmilekic mtx_lock_spin(&sio_lock); 230051078Speter com_events -= LOTS_OF_EVENTS; 230151078Speter com->state &= ~CS_ODONE; 230272200Sbmilekic mtx_unlock_spin(&sio_lock); 230351078Speter if (!(com->state & CS_BUSY) 230451078Speter && !(com->extra_state & CSE_BUSYCHECK)) { 230551078Speter timeout(siobusycheck, com, hz / 100); 230651078Speter com->extra_state |= CSE_BUSYCHECK; 230751078Speter } 230851078Speter (*linesw[tp->t_line].l_start)(tp); 230951078Speter } 231051078Speter if (com_events == 0) 231151078Speter break; 231251078Speter } 231351078Speter if (com_events >= LOTS_OF_EVENTS) 231451078Speter goto repeat; 231551078Speter} 231651078Speter 231751078Speterstatic int 231851078Spetercomparam(tp, t) 231951078Speter struct tty *tp; 232051078Speter struct termios *t; 232151078Speter{ 232251078Speter u_int cfcr; 232351078Speter int cflag; 232451078Speter struct com_s *com; 232551078Speter int divisor; 232651078Speter u_char dlbh; 232751078Speter u_char dlbl; 232851078Speter int s; 232951078Speter int unit; 233051078Speter 233151078Speter /* do historical conversions */ 233251078Speter if (t->c_ispeed == 0) 233351078Speter t->c_ispeed = t->c_ospeed; 233451078Speter 233551078Speter /* check requested parameters */ 233651078Speter divisor = ttspeedtab(t->c_ospeed, comspeedtab); 233751078Speter if (divisor < 0 || (divisor > 0 && t->c_ispeed != t->c_ospeed)) 233851078Speter return (EINVAL); 233951078Speter 234051078Speter /* parameters are OK, convert them to the com struct and the device */ 234151078Speter unit = DEV_TO_UNIT(tp->t_dev); 234251078Speter com = com_addr(unit); 234357915Simp if (com == NULL) 234457915Simp return (ENODEV); 234551078Speter s = spltty(); 234651078Speter if (divisor == 0) 234751078Speter (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */ 234851078Speter else 234951078Speter (void)commctl(com, TIOCM_DTR, DMBIS); 235051078Speter cflag = t->c_cflag; 235151078Speter switch (cflag & CSIZE) { 235251078Speter case CS5: 235351078Speter cfcr = CFCR_5BITS; 235451078Speter break; 235551078Speter case CS6: 235651078Speter cfcr = CFCR_6BITS; 235751078Speter break; 235851078Speter case CS7: 235951078Speter cfcr = CFCR_7BITS; 236051078Speter break; 236151078Speter default: 236251078Speter cfcr = CFCR_8BITS; 236351078Speter break; 236451078Speter } 236551078Speter if (cflag & PARENB) { 236651078Speter cfcr |= CFCR_PENAB; 236751078Speter if (!(cflag & PARODD)) 236851078Speter cfcr |= CFCR_PEVEN; 236951078Speter } 237051078Speter if (cflag & CSTOPB) 237151078Speter cfcr |= CFCR_STOPB; 237251078Speter 237351078Speter if (com->hasfifo && divisor != 0) { 237451078Speter /* 237551078Speter * Use a fifo trigger level low enough so that the input 237651078Speter * latency from the fifo is less than about 16 msec and 237751078Speter * the total latency is less than about 30 msec. These 237851078Speter * latencies are reasonable for humans. Serial comms 237951078Speter * protocols shouldn't expect anything better since modem 238051078Speter * latencies are larger. 238151078Speter */ 238251078Speter com->fifo_image = t->c_ospeed <= 4800 238351078Speter ? FIFO_ENABLE : FIFO_ENABLE | FIFO_RX_HIGH; 238451078Speter#ifdef COM_ESP 238551078Speter /* 238651078Speter * The Hayes ESP card needs the fifo DMA mode bit set 238751078Speter * in compatibility mode. If not, it will interrupt 238851078Speter * for each character received. 238951078Speter */ 239051078Speter if (com->esp) 239151078Speter com->fifo_image |= FIFO_DMA_MODE; 239251078Speter#endif 239360471Snyan sio_setreg(com, com_fifo, com->fifo_image); 239451078Speter } 239551078Speter 239665605Sjhb /* 239765605Sjhb * This returns with interrupts disabled so that we can complete 239865605Sjhb * the speed change atomically. Keeping interrupts disabled is 239965605Sjhb * especially important while com_data is hidden. 240065605Sjhb */ 240165605Sjhb (void) siosetwater(com, t->c_ispeed); 240265557Sjasone 240351078Speter if (divisor != 0) { 240460471Snyan sio_setreg(com, com_cfcr, cfcr | CFCR_DLAB); 240551078Speter /* 240651078Speter * Only set the divisor registers if they would change, 240751078Speter * since on some 16550 incompatibles (UMC8669F), setting 240851078Speter * them while input is arriving them loses sync until 240951078Speter * data stops arriving. 241051078Speter */ 241151078Speter dlbl = divisor & 0xFF; 241260471Snyan if (sio_getreg(com, com_dlbl) != dlbl) 241360471Snyan sio_setreg(com, com_dlbl, dlbl); 241451078Speter dlbh = (u_int) divisor >> 8; 241560471Snyan if (sio_getreg(com, com_dlbh) != dlbh) 241660471Snyan sio_setreg(com, com_dlbh, dlbh); 241751078Speter } 241851078Speter 241960471Snyan sio_setreg(com, com_cfcr, com->cfcr_image = cfcr); 242051078Speter 242151078Speter if (!(tp->t_state & TS_TTSTOP)) 242251078Speter com->state |= CS_TTGO; 242351078Speter 242451078Speter if (cflag & CRTS_IFLOW) { 242551078Speter if (com->st16650a) { 242660471Snyan sio_setreg(com, com_cfcr, 0xbf); 242760471Snyan sio_setreg(com, com_fifo, 242860471Snyan sio_getreg(com, com_fifo) | 0x40); 242951078Speter } 243051078Speter com->state |= CS_RTS_IFLOW; 243151078Speter /* 243251078Speter * If CS_RTS_IFLOW just changed from off to on, the change 243351078Speter * needs to be propagated to MCR_RTS. This isn't urgent, 243451078Speter * so do it later by calling comstart() instead of repeating 243551078Speter * a lot of code from comstart() here. 243651078Speter */ 243751078Speter } else if (com->state & CS_RTS_IFLOW) { 243851078Speter com->state &= ~CS_RTS_IFLOW; 243951078Speter /* 244051078Speter * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS 244151078Speter * on here, since comstart() won't do it later. 244251078Speter */ 244351078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 244451078Speter if (com->st16650a) { 244560471Snyan sio_setreg(com, com_cfcr, 0xbf); 244660471Snyan sio_setreg(com, com_fifo, 244760471Snyan sio_getreg(com, com_fifo) & ~0x40); 244851078Speter } 244951078Speter } 245051078Speter 245151078Speter 245251078Speter /* 245351078Speter * Set up state to handle output flow control. 245451078Speter * XXX - worth handling MDMBUF (DCD) flow control at the lowest level? 245551078Speter * Now has 10+ msec latency, while CTS flow has 50- usec latency. 245651078Speter */ 245751078Speter com->state |= CS_ODEVREADY; 245851078Speter com->state &= ~CS_CTS_OFLOW; 245951078Speter if (cflag & CCTS_OFLOW) { 246051078Speter com->state |= CS_CTS_OFLOW; 246151078Speter if (!(com->last_modem_status & MSR_CTS)) 246251078Speter com->state &= ~CS_ODEVREADY; 246351078Speter if (com->st16650a) { 246460471Snyan sio_setreg(com, com_cfcr, 0xbf); 246560471Snyan sio_setreg(com, com_fifo, 246660471Snyan sio_getreg(com, com_fifo) | 0x80); 246751078Speter } 246851078Speter } else { 246951078Speter if (com->st16650a) { 247060471Snyan sio_setreg(com, com_cfcr, 0xbf); 247160471Snyan sio_setreg(com, com_fifo, 247260471Snyan sio_getreg(com, com_fifo) & ~0x80); 247351078Speter } 247451078Speter } 247551078Speter 247660471Snyan sio_setreg(com, com_cfcr, com->cfcr_image); 247751078Speter 247851078Speter /* XXX shouldn't call functions while intrs are disabled. */ 247951078Speter disc_optim(tp, t, com); 248051078Speter /* 248151078Speter * Recover from fiddling with CS_TTGO. We used to call siointr1() 248251078Speter * unconditionally, but that defeated the careful discarding of 248351078Speter * stale input in sioopen(). 248451078Speter */ 248551078Speter if (com->state >= (CS_BUSY | CS_TTGO)) 248651078Speter siointr1(com); 248751078Speter 248872200Sbmilekic mtx_unlock_spin(&sio_lock); 248951078Speter splx(s); 249051078Speter comstart(tp); 249151078Speter if (com->ibufold != NULL) { 249251078Speter free(com->ibufold, M_DEVBUF); 249351078Speter com->ibufold = NULL; 249451078Speter } 249551078Speter return (0); 249651078Speter} 249751078Speter 249865605Sjhb/* 249970174Sjhb * This function must be called with the sio_lock mutex released and will 250070174Sjhb * return with it obtained. 250165605Sjhb */ 250251078Speterstatic int 250365605Sjhbsiosetwater(com, speed) 250451078Speter struct com_s *com; 250551078Speter speed_t speed; 250651078Speter{ 250751078Speter int cp4ticks; 250851078Speter u_char *ibuf; 250951078Speter int ibufsize; 251051078Speter struct tty *tp; 251151078Speter 251251078Speter /* 251351078Speter * Make the buffer size large enough to handle a softtty interrupt 251451078Speter * latency of about 2 ticks without loss of throughput or data 251551078Speter * (about 3 ticks if input flow control is not used or not honoured, 251651078Speter * but a bit less for CS5-CS7 modes). 251751078Speter */ 251851078Speter cp4ticks = speed / 10 / hz * 4; 251951078Speter for (ibufsize = 128; ibufsize < cp4ticks;) 252051078Speter ibufsize <<= 1; 252165605Sjhb if (ibufsize == com->ibufsize) { 252272200Sbmilekic mtx_lock_spin(&sio_lock); 252351078Speter return (0); 252465605Sjhb } 252551078Speter 252651078Speter /* 252751078Speter * Allocate input buffer. The extra factor of 2 in the size is 252851078Speter * to allow for an error byte for each input byte. 252951078Speter */ 253051078Speter ibuf = malloc(2 * ibufsize, M_DEVBUF, M_NOWAIT); 253165605Sjhb if (ibuf == NULL) { 253272200Sbmilekic mtx_lock_spin(&sio_lock); 253351078Speter return (ENOMEM); 253465605Sjhb } 253551078Speter 253651078Speter /* Initialize non-critical variables. */ 253751078Speter com->ibufold = com->ibuf; 253851078Speter com->ibufsize = ibufsize; 253951078Speter tp = com->tp; 254051078Speter if (tp != NULL) { 254151078Speter tp->t_ififosize = 2 * ibufsize; 254251078Speter tp->t_ispeedwat = (speed_t)-1; 254351078Speter tp->t_ospeedwat = (speed_t)-1; 254451078Speter } 254551078Speter 254651078Speter /* 254751078Speter * Read current input buffer, if any. Continue with interrupts 254851078Speter * disabled. 254951078Speter */ 255072200Sbmilekic mtx_lock_spin(&sio_lock); 255151078Speter if (com->iptr != com->ibuf) 255251078Speter sioinput(com); 255351078Speter 255451078Speter /*- 255551078Speter * Initialize critical variables, including input buffer watermarks. 255651078Speter * The external device is asked to stop sending when the buffer 255751078Speter * exactly reaches high water, or when the high level requests it. 255851078Speter * The high level is notified immediately (rather than at a later 255951078Speter * clock tick) when this watermark is reached. 256051078Speter * The buffer size is chosen so the watermark should almost never 256151078Speter * be reached. 256251078Speter * The low watermark is invisibly 0 since the buffer is always 256351078Speter * emptied all at once. 256451078Speter */ 256551078Speter com->iptr = com->ibuf = ibuf; 256651078Speter com->ibufend = ibuf + ibufsize; 256751078Speter com->ierroff = ibufsize; 256851078Speter com->ihighwater = ibuf + 3 * ibufsize / 4; 256951078Speter return (0); 257051078Speter} 257151078Speter 257251078Speterstatic void 257351078Spetercomstart(tp) 257451078Speter struct tty *tp; 257551078Speter{ 257651078Speter struct com_s *com; 257751078Speter int s; 257851078Speter int unit; 257951078Speter 258051078Speter unit = DEV_TO_UNIT(tp->t_dev); 258151078Speter com = com_addr(unit); 258257915Simp if (com == NULL) 258357915Simp return; 258451078Speter s = spltty(); 258572200Sbmilekic mtx_lock_spin(&sio_lock); 258651078Speter if (tp->t_state & TS_TTSTOP) 258751078Speter com->state &= ~CS_TTGO; 258851078Speter else 258951078Speter com->state |= CS_TTGO; 259051078Speter if (tp->t_state & TS_TBLOCK) { 259151078Speter if (com->mcr_image & MCR_RTS && com->state & CS_RTS_IFLOW) 259251078Speter outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS); 259351078Speter } else { 259451078Speter if (!(com->mcr_image & MCR_RTS) && com->iptr < com->ihighwater 259551078Speter && com->state & CS_RTS_IFLOW) 259651078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 259751078Speter } 259872200Sbmilekic mtx_unlock_spin(&sio_lock); 259951078Speter if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) { 260051078Speter ttwwakeup(tp); 260151078Speter splx(s); 260251078Speter return; 260351078Speter } 260451078Speter if (tp->t_outq.c_cc != 0) { 260551078Speter struct lbq *qp; 260651078Speter struct lbq *next; 260751078Speter 260851078Speter if (!com->obufs[0].l_queued) { 260951078Speter com->obufs[0].l_tail 261051078Speter = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1, 261151078Speter sizeof com->obuf1); 261251078Speter com->obufs[0].l_next = NULL; 261351078Speter com->obufs[0].l_queued = TRUE; 261472200Sbmilekic mtx_lock_spin(&sio_lock); 261551078Speter if (com->state & CS_BUSY) { 261651078Speter qp = com->obufq.l_next; 261751078Speter while ((next = qp->l_next) != NULL) 261851078Speter qp = next; 261951078Speter qp->l_next = &com->obufs[0]; 262051078Speter } else { 262151078Speter com->obufq.l_head = com->obufs[0].l_head; 262251078Speter com->obufq.l_tail = com->obufs[0].l_tail; 262351078Speter com->obufq.l_next = &com->obufs[0]; 262451078Speter com->state |= CS_BUSY; 262551078Speter } 262672200Sbmilekic mtx_unlock_spin(&sio_lock); 262751078Speter } 262851078Speter if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) { 262951078Speter com->obufs[1].l_tail 263051078Speter = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2, 263151078Speter sizeof com->obuf2); 263251078Speter com->obufs[1].l_next = NULL; 263351078Speter com->obufs[1].l_queued = TRUE; 263472200Sbmilekic mtx_lock_spin(&sio_lock); 263551078Speter if (com->state & CS_BUSY) { 263651078Speter qp = com->obufq.l_next; 263751078Speter while ((next = qp->l_next) != NULL) 263851078Speter qp = next; 263951078Speter qp->l_next = &com->obufs[1]; 264051078Speter } else { 264151078Speter com->obufq.l_head = com->obufs[1].l_head; 264251078Speter com->obufq.l_tail = com->obufs[1].l_tail; 264351078Speter com->obufq.l_next = &com->obufs[1]; 264451078Speter com->state |= CS_BUSY; 264551078Speter } 264672200Sbmilekic mtx_unlock_spin(&sio_lock); 264751078Speter } 264851078Speter tp->t_state |= TS_BUSY; 264951078Speter } 265072200Sbmilekic mtx_lock_spin(&sio_lock); 265151078Speter if (com->state >= (CS_BUSY | CS_TTGO)) 265251078Speter siointr1(com); /* fake interrupt to start output */ 265372200Sbmilekic mtx_unlock_spin(&sio_lock); 265451078Speter ttwwakeup(tp); 265551078Speter splx(s); 265651078Speter} 265751078Speter 265851078Speterstatic void 265951654Sphkcomstop(tp, rw) 266051078Speter struct tty *tp; 266151078Speter int rw; 266251078Speter{ 266351078Speter struct com_s *com; 266451078Speter 266551078Speter com = com_addr(DEV_TO_UNIT(tp->t_dev)); 266657915Simp if (com == NULL || com->gone) 266751078Speter return; 266872200Sbmilekic mtx_lock_spin(&sio_lock); 266951078Speter if (rw & FWRITE) { 267051078Speter if (com->hasfifo) 267151078Speter#ifdef COM_ESP 267251078Speter /* XXX avoid h/w bug. */ 267351078Speter if (!com->esp) 267451078Speter#endif 267560471Snyan sio_setreg(com, com_fifo, 267660471Snyan FIFO_XMT_RST | com->fifo_image); 267751078Speter com->obufs[0].l_queued = FALSE; 267851078Speter com->obufs[1].l_queued = FALSE; 267951078Speter if (com->state & CS_ODONE) 268051078Speter com_events -= LOTS_OF_EVENTS; 268151078Speter com->state &= ~(CS_ODONE | CS_BUSY); 268251078Speter com->tp->t_state &= ~TS_BUSY; 268351078Speter } 268451078Speter if (rw & FREAD) { 268551078Speter if (com->hasfifo) 268651078Speter#ifdef COM_ESP 268751078Speter /* XXX avoid h/w bug. */ 268851078Speter if (!com->esp) 268951078Speter#endif 269060471Snyan sio_setreg(com, com_fifo, 269160471Snyan FIFO_RCV_RST | com->fifo_image); 269251078Speter com_events -= (com->iptr - com->ibuf); 269351078Speter com->iptr = com->ibuf; 269451078Speter } 269572200Sbmilekic mtx_unlock_spin(&sio_lock); 269651078Speter comstart(tp); 269751078Speter} 269851078Speter 269951078Speterstatic int 270051078Spetercommctl(com, bits, how) 270151078Speter struct com_s *com; 270251078Speter int bits; 270351078Speter int how; 270451078Speter{ 270551078Speter int mcr; 270651078Speter int msr; 270751078Speter 270851078Speter if (how == DMGET) { 270951078Speter bits = TIOCM_LE; /* XXX - always enabled while open */ 271051078Speter mcr = com->mcr_image; 271151078Speter if (mcr & MCR_DTR) 271251078Speter bits |= TIOCM_DTR; 271351078Speter if (mcr & MCR_RTS) 271451078Speter bits |= TIOCM_RTS; 271551078Speter msr = com->prev_modem_status; 271651078Speter if (msr & MSR_CTS) 271751078Speter bits |= TIOCM_CTS; 271851078Speter if (msr & MSR_DCD) 271951078Speter bits |= TIOCM_CD; 272051078Speter if (msr & MSR_DSR) 272151078Speter bits |= TIOCM_DSR; 272251078Speter /* 272351078Speter * XXX - MSR_RI is naturally volatile, and we make MSR_TERI 272451078Speter * more volatile by reading the modem status a lot. Perhaps 272551078Speter * we should latch both bits until the status is read here. 272651078Speter */ 272751078Speter if (msr & (MSR_RI | MSR_TERI)) 272851078Speter bits |= TIOCM_RI; 272951078Speter return (bits); 273051078Speter } 273151078Speter mcr = 0; 273251078Speter if (bits & TIOCM_DTR) 273351078Speter mcr |= MCR_DTR; 273451078Speter if (bits & TIOCM_RTS) 273551078Speter mcr |= MCR_RTS; 273651078Speter if (com->gone) 273751078Speter return(0); 273872200Sbmilekic mtx_lock_spin(&sio_lock); 273951078Speter switch (how) { 274051078Speter case DMSET: 274151078Speter outb(com->modem_ctl_port, 274251078Speter com->mcr_image = mcr | (com->mcr_image & MCR_IENABLE)); 274351078Speter break; 274451078Speter case DMBIS: 274551078Speter outb(com->modem_ctl_port, com->mcr_image |= mcr); 274651078Speter break; 274751078Speter case DMBIC: 274851078Speter outb(com->modem_ctl_port, com->mcr_image &= ~mcr); 274951078Speter break; 275051078Speter } 275172200Sbmilekic mtx_unlock_spin(&sio_lock); 275251078Speter return (0); 275351078Speter} 275451078Speter 275551078Speterstatic void 275651078Spetersiosettimeout() 275751078Speter{ 275851078Speter struct com_s *com; 275951078Speter bool_t someopen; 276051078Speter int unit; 276151078Speter 276251078Speter /* 276351078Speter * Set our timeout period to 1 second if no polled devices are open. 276451078Speter * Otherwise set it to max(1/200, 1/hz). 276551078Speter * Enable timeouts iff some device is open. 276651078Speter */ 276751078Speter untimeout(comwakeup, (void *)NULL, sio_timeout_handle); 276851078Speter sio_timeout = hz; 276951078Speter someopen = FALSE; 277053344Speter for (unit = 0; unit < sio_numunits; ++unit) { 277151078Speter com = com_addr(unit); 277251078Speter if (com != NULL && com->tp != NULL 277351078Speter && com->tp->t_state & TS_ISOPEN && !com->gone) { 277451078Speter someopen = TRUE; 277551078Speter if (com->poll || com->poll_output) { 277651078Speter sio_timeout = hz > 200 ? hz / 200 : 1; 277751078Speter break; 277851078Speter } 277951078Speter } 278051078Speter } 278151078Speter if (someopen) { 278251078Speter sio_timeouts_until_log = hz / sio_timeout; 278351078Speter sio_timeout_handle = timeout(comwakeup, (void *)NULL, 278451078Speter sio_timeout); 278551078Speter } else { 278651078Speter /* Flush error messages, if any. */ 278751078Speter sio_timeouts_until_log = 1; 278851078Speter comwakeup((void *)NULL); 278951078Speter untimeout(comwakeup, (void *)NULL, sio_timeout_handle); 279051078Speter } 279151078Speter} 279251078Speter 279351078Speterstatic void 279451078Spetercomwakeup(chan) 279551078Speter void *chan; 279651078Speter{ 279751078Speter struct com_s *com; 279851078Speter int unit; 279951078Speter 280051078Speter sio_timeout_handle = timeout(comwakeup, (void *)NULL, sio_timeout); 280151078Speter 280251078Speter /* 280351078Speter * Recover from lost output interrupts. 280451078Speter * Poll any lines that don't use interrupts. 280551078Speter */ 280653344Speter for (unit = 0; unit < sio_numunits; ++unit) { 280751078Speter com = com_addr(unit); 280851078Speter if (com != NULL && !com->gone 280951078Speter && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) { 281072200Sbmilekic mtx_lock_spin(&sio_lock); 281151078Speter siointr1(com); 281272200Sbmilekic mtx_unlock_spin(&sio_lock); 281351078Speter } 281451078Speter } 281551078Speter 281651078Speter /* 281751078Speter * Check for and log errors, but not too often. 281851078Speter */ 281951078Speter if (--sio_timeouts_until_log > 0) 282051078Speter return; 282151078Speter sio_timeouts_until_log = hz / sio_timeout; 282253344Speter for (unit = 0; unit < sio_numunits; ++unit) { 282351078Speter int errnum; 282451078Speter 282551078Speter com = com_addr(unit); 282651078Speter if (com == NULL) 282751078Speter continue; 282851078Speter if (com->gone) 282951078Speter continue; 283051078Speter for (errnum = 0; errnum < CE_NTYPES; ++errnum) { 283151078Speter u_int delta; 283251078Speter u_long total; 283351078Speter 283472200Sbmilekic mtx_lock_spin(&sio_lock); 283551078Speter delta = com->delta_error_counts[errnum]; 283651078Speter com->delta_error_counts[errnum] = 0; 283772200Sbmilekic mtx_unlock_spin(&sio_lock); 283851078Speter if (delta == 0) 283951078Speter continue; 284051078Speter total = com->error_counts[errnum] += delta; 284151078Speter log(LOG_ERR, "sio%d: %u more %s%s (total %lu)\n", 284251078Speter unit, delta, error_desc[errnum], 284351078Speter delta == 1 ? "" : "s", total); 284451078Speter } 284551078Speter } 284651078Speter} 284751078Speter 284851078Speterstatic void 284951078Speterdisc_optim(tp, t, com) 285051078Speter struct tty *tp; 285151078Speter struct termios *t; 285251078Speter struct com_s *com; 285351078Speter{ 285451078Speter if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) 285551078Speter && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) 285651078Speter && (!(t->c_iflag & PARMRK) 285751078Speter || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) 285851078Speter && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) 285951078Speter && linesw[tp->t_line].l_rint == ttyinput) 286051078Speter tp->t_state |= TS_CAN_BYPASS_L_RINT; 286151078Speter else 286251078Speter tp->t_state &= ~TS_CAN_BYPASS_L_RINT; 286351078Speter com->hotchar = linesw[tp->t_line].l_hotchar; 286451078Speter} 286551078Speter 286651078Speter/* 286751078Speter * Following are all routines needed for SIO to act as console 286851078Speter */ 286951078Speter#include <sys/cons.h> 287051078Speter 287151078Speterstruct siocnstate { 287251078Speter u_char dlbl; 287351078Speter u_char dlbh; 287451078Speter u_char ier; 287551078Speter u_char cfcr; 287651078Speter u_char mcr; 287751078Speter}; 287851078Speter 287966230Sjhb#ifndef __alpha__ 288051078Speterstatic speed_t siocngetspeed __P((Port_t, struct speedtab *)); 288166230Sjhb#endif 288251078Speterstatic void siocnclose __P((struct siocnstate *sp, Port_t iobase)); 288351078Speterstatic void siocnopen __P((struct siocnstate *sp, Port_t iobase, int speed)); 288451078Speterstatic void siocntxwait __P((Port_t iobase)); 288551078Speter 288666230Sjhb#ifdef __alpha__ 288766230Sjhbint siocnattach __P((int port, int speed)); 288866230Sjhbint siogdbattach __P((int port, int speed)); 288966230Sjhbint siogdbgetc __P((void)); 289066230Sjhbvoid siogdbputc __P((int c)); 289166230Sjhb#else 289251078Speterstatic cn_probe_t siocnprobe; 289351078Speterstatic cn_init_t siocninit; 289466230Sjhb#endif 289551078Speterstatic cn_checkc_t siocncheckc; 289651078Speterstatic cn_getc_t siocngetc; 289751078Speterstatic cn_putc_t siocnputc; 289851078Speter 289951078Speter#ifdef __i386__ 290055823SyokotaCONS_DRIVER(sio, siocnprobe, siocninit, NULL, siocngetc, siocncheckc, 290155823Syokota siocnputc, NULL); 290251078Speter#endif 290351078Speter 290451078Speter/* To get the GDB related variables */ 290551078Speter#if DDB > 0 290651078Speter#include <ddb/ddb.h> 290751078Speter#endif 290851078Speter 290951078Speterstatic void 291051078Spetersiocntxwait(iobase) 291151078Speter Port_t iobase; 291251078Speter{ 291351078Speter int timo; 291451078Speter 291551078Speter /* 291651078Speter * Wait for any pending transmission to finish. Required to avoid 291751078Speter * the UART lockup bug when the speed is changed, and for normal 291851078Speter * transmits. 291951078Speter */ 292051078Speter timo = 100000; 292151078Speter while ((inb(iobase + com_lsr) & (LSR_TSRE | LSR_TXRDY)) 292251078Speter != (LSR_TSRE | LSR_TXRDY) && --timo != 0) 292351078Speter ; 292451078Speter} 292551078Speter 292666230Sjhb#ifndef __alpha__ 292766230Sjhb 292851078Speter/* 292951078Speter * Read the serial port specified and try to figure out what speed 293051078Speter * it's currently running at. We're assuming the serial port has 293151078Speter * been initialized and is basicly idle. This routine is only intended 293251078Speter * to be run at system startup. 293351078Speter * 293451078Speter * If the value read from the serial port doesn't make sense, return 0. 293551078Speter */ 293651078Speter 293751078Speterstatic speed_t 293851078Spetersiocngetspeed(iobase, table) 293951078Speter Port_t iobase; 294051078Speter struct speedtab *table; 294151078Speter{ 294251078Speter int code; 294351078Speter u_char dlbh; 294451078Speter u_char dlbl; 294551078Speter u_char cfcr; 294651078Speter 294751078Speter cfcr = inb(iobase + com_cfcr); 294851078Speter outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 294951078Speter 295051078Speter dlbl = inb(iobase + com_dlbl); 295151078Speter dlbh = inb(iobase + com_dlbh); 295251078Speter 295351078Speter outb(iobase + com_cfcr, cfcr); 295451078Speter 295551078Speter code = dlbh << 8 | dlbl; 295651078Speter 295753344Speter for (; table->sp_speed != -1; table++) 295851078Speter if (table->sp_code == code) 295951078Speter return (table->sp_speed); 296051078Speter 296158885Simp return (0); /* didn't match anything sane */ 296251078Speter} 296351078Speter 296466230Sjhb#endif 296566230Sjhb 296651078Speterstatic void 296751078Spetersiocnopen(sp, iobase, speed) 296851078Speter struct siocnstate *sp; 296951078Speter Port_t iobase; 297051078Speter int speed; 297151078Speter{ 297251078Speter int divisor; 297351078Speter u_char dlbh; 297451078Speter u_char dlbl; 297551078Speter 297651078Speter /* 297751078Speter * Save all the device control registers except the fifo register 297851078Speter * and set our default ones (cs8 -parenb speed=comdefaultrate). 297951078Speter * We can't save the fifo register since it is read-only. 298051078Speter */ 298151078Speter sp->ier = inb(iobase + com_ier); 298251078Speter outb(iobase + com_ier, 0); /* spltty() doesn't stop siointr() */ 298351078Speter siocntxwait(iobase); 298451078Speter sp->cfcr = inb(iobase + com_cfcr); 298551078Speter outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 298651078Speter sp->dlbl = inb(iobase + com_dlbl); 298751078Speter sp->dlbh = inb(iobase + com_dlbh); 298851078Speter /* 298951078Speter * Only set the divisor registers if they would change, since on 299051078Speter * some 16550 incompatibles (Startech), setting them clears the 299151078Speter * data input register. This also reduces the effects of the 299251078Speter * UMC8669F bug. 299351078Speter */ 299451078Speter divisor = ttspeedtab(speed, comspeedtab); 299551078Speter dlbl = divisor & 0xFF; 299651078Speter if (sp->dlbl != dlbl) 299751078Speter outb(iobase + com_dlbl, dlbl); 299851078Speter dlbh = (u_int) divisor >> 8; 299951078Speter if (sp->dlbh != dlbh) 300051078Speter outb(iobase + com_dlbh, dlbh); 300151078Speter outb(iobase + com_cfcr, CFCR_8BITS); 300251078Speter sp->mcr = inb(iobase + com_mcr); 300351078Speter /* 300451078Speter * We don't want interrupts, but must be careful not to "disable" 300551078Speter * them by clearing the MCR_IENABLE bit, since that might cause 300651078Speter * an interrupt by floating the IRQ line. 300751078Speter */ 300851078Speter outb(iobase + com_mcr, (sp->mcr & MCR_IENABLE) | MCR_DTR | MCR_RTS); 300951078Speter} 301051078Speter 301151078Speterstatic void 301251078Spetersiocnclose(sp, iobase) 301351078Speter struct siocnstate *sp; 301451078Speter Port_t iobase; 301551078Speter{ 301651078Speter /* 301751078Speter * Restore the device control registers. 301851078Speter */ 301951078Speter siocntxwait(iobase); 302051078Speter outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 302151078Speter if (sp->dlbl != inb(iobase + com_dlbl)) 302251078Speter outb(iobase + com_dlbl, sp->dlbl); 302351078Speter if (sp->dlbh != inb(iobase + com_dlbh)) 302451078Speter outb(iobase + com_dlbh, sp->dlbh); 302551078Speter outb(iobase + com_cfcr, sp->cfcr); 302651078Speter /* 302751078Speter * XXX damp oscillations of MCR_DTR and MCR_RTS by not restoring them. 302851078Speter */ 302951078Speter outb(iobase + com_mcr, sp->mcr | MCR_DTR | MCR_RTS); 303051078Speter outb(iobase + com_ier, sp->ier); 303151078Speter} 303251078Speter 303366230Sjhb#ifndef __alpha__ 303466230Sjhb 303551078Speterstatic void 303651078Spetersiocnprobe(cp) 303751078Speter struct consdev *cp; 303851078Speter{ 303951078Speter speed_t boot_speed; 304051078Speter u_char cfcr; 304151078Speter int s, unit; 304251078Speter struct siocnstate sp; 304351078Speter 304451078Speter /* 304551078Speter * Find our first enabled console, if any. If it is a high-level 304651078Speter * console device, then initialize it and return successfully. 304751078Speter * If it is a low-level console device, then initialize it and 304851078Speter * return unsuccessfully. It must be initialized in both cases 304951078Speter * for early use by console drivers and debuggers. Initializing 305051078Speter * the hardware is not necessary in all cases, since the i/o 305151078Speter * routines initialize it on the fly, but it is necessary if 305251078Speter * input might arrive while the hardware is switched back to an 305351078Speter * uninitialized state. We can't handle multiple console devices 305451078Speter * yet because our low-level routines don't take a device arg. 305551078Speter * We trust the user to set the console flags properly so that we 305651078Speter * don't need to probe. 305751078Speter */ 305851078Speter cp->cn_pri = CN_DEAD; 305951078Speter 306051078Speter for (unit = 0; unit < 16; unit++) { /* XXX need to know how many */ 306151078Speter int flags; 306251078Speter int disabled; 306351078Speter if (resource_int_value("sio", unit, "disabled", &disabled) == 0) { 306451078Speter if (disabled) 306551078Speter continue; 306651078Speter } 306751078Speter if (resource_int_value("sio", unit, "flags", &flags)) 306851078Speter continue; 306951078Speter if (COM_CONSOLE(flags) || COM_DEBUGGER(flags)) { 307051078Speter int port; 307151078Speter Port_t iobase; 307251078Speter 307351078Speter if (resource_int_value("sio", unit, "port", &port)) 307451078Speter continue; 307551078Speter iobase = port; 307651078Speter s = spltty(); 307751078Speter if (boothowto & RB_SERIAL) { 307851078Speter boot_speed = siocngetspeed(iobase, comspeedtab); 307951078Speter if (boot_speed) 308051078Speter comdefaultrate = boot_speed; 308151078Speter } 308251078Speter 308351078Speter /* 308451078Speter * Initialize the divisor latch. We can't rely on 308551078Speter * siocnopen() to do this the first time, since it 308651078Speter * avoids writing to the latch if the latch appears 308751078Speter * to have the correct value. Also, if we didn't 308851078Speter * just read the speed from the hardware, then we 308951078Speter * need to set the speed in hardware so that 309051078Speter * switching it later is null. 309151078Speter */ 309251078Speter cfcr = inb(iobase + com_cfcr); 309351078Speter outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 309451078Speter outb(iobase + com_dlbl, 309551078Speter COMBRD(comdefaultrate) & 0xff); 309651078Speter outb(iobase + com_dlbh, 309751078Speter (u_int) COMBRD(comdefaultrate) >> 8); 309851078Speter outb(iobase + com_cfcr, cfcr); 309951078Speter 310051078Speter siocnopen(&sp, iobase, comdefaultrate); 310151078Speter 310251078Speter splx(s); 310351078Speter if (COM_CONSOLE(flags) && !COM_LLCONSOLE(flags)) { 310451078Speter cp->cn_dev = makedev(CDEV_MAJOR, unit); 310551078Speter cp->cn_pri = COM_FORCECONSOLE(flags) 310651078Speter || boothowto & RB_SERIAL 310751078Speter ? CN_REMOTE : CN_NORMAL; 310851078Speter siocniobase = iobase; 310951078Speter siocnunit = unit; 311051078Speter } 311151078Speter if (COM_DEBUGGER(flags)) { 311251078Speter printf("sio%d: gdb debugging port\n", unit); 311351078Speter siogdbiobase = iobase; 311451078Speter siogdbunit = unit; 311551078Speter#if DDB > 0 311651078Speter gdbdev = makedev(CDEV_MAJOR, unit); 311751078Speter gdb_getc = siocngetc; 311851078Speter gdb_putc = siocnputc; 311951078Speter#endif 312051078Speter } 312151078Speter } 312251078Speter } 312351078Speter#ifdef __i386__ 312451078Speter#if DDB > 0 312551078Speter /* 312651078Speter * XXX Ugly Compatability. 312751078Speter * If no gdb port has been specified, set it to be the console 312851078Speter * as some configuration files don't specify the gdb port. 312951078Speter */ 313051078Speter if (gdbdev == NODEV && (boothowto & RB_GDB)) { 313151078Speter printf("Warning: no GDB port specified. Defaulting to sio%d.\n", 313251078Speter siocnunit); 313351078Speter printf("Set flag 0x80 on desired GDB port in your\n"); 313451078Speter printf("configuration file (currently sio only).\n"); 313551078Speter siogdbiobase = siocniobase; 313651078Speter siogdbunit = siocnunit; 313751078Speter gdbdev = makedev(CDEV_MAJOR, siocnunit); 313851078Speter gdb_getc = siocngetc; 313951078Speter gdb_putc = siocnputc; 314051078Speter } 314151078Speter#endif 314251078Speter#endif 314351078Speter} 314451078Speter 314566230Sjhbstatic void 314666230Sjhbsiocninit(cp) 314766230Sjhb struct consdev *cp; 314866230Sjhb{ 314966230Sjhb comconsole = DEV_TO_UNIT(cp->cn_dev); 315066230Sjhb} 315166230Sjhb 315266230Sjhb#endif 315366230Sjhb 315451078Speter#ifdef __alpha__ 315551078Speter 315655868SgallatinCONS_DRIVER(sio, NULL, NULL, NULL, siocngetc, siocncheckc, siocnputc, NULL); 315751078Speter 315851078Speterint 315951078Spetersiocnattach(port, speed) 316051078Speter int port; 316151078Speter int speed; 316251078Speter{ 316351078Speter int s; 316451078Speter u_char cfcr; 316551078Speter struct siocnstate sp; 316651078Speter 316751078Speter siocniobase = port; 316851078Speter comdefaultrate = speed; 316951078Speter sio_consdev.cn_pri = CN_NORMAL; 317051078Speter sio_consdev.cn_dev = makedev(CDEV_MAJOR, 0); 317151078Speter 317251078Speter s = spltty(); 317351078Speter 317451078Speter /* 317551078Speter * Initialize the divisor latch. We can't rely on 317651078Speter * siocnopen() to do this the first time, since it 317751078Speter * avoids writing to the latch if the latch appears 317851078Speter * to have the correct value. Also, if we didn't 317951078Speter * just read the speed from the hardware, then we 318051078Speter * need to set the speed in hardware so that 318151078Speter * switching it later is null. 318251078Speter */ 318351078Speter cfcr = inb(siocniobase + com_cfcr); 318451078Speter outb(siocniobase + com_cfcr, CFCR_DLAB | cfcr); 318551078Speter outb(siocniobase + com_dlbl, 318651078Speter COMBRD(comdefaultrate) & 0xff); 318751078Speter outb(siocniobase + com_dlbh, 318851078Speter (u_int) COMBRD(comdefaultrate) >> 8); 318951078Speter outb(siocniobase + com_cfcr, cfcr); 319051078Speter 319151078Speter siocnopen(&sp, siocniobase, comdefaultrate); 319251078Speter splx(s); 319351078Speter 319451078Speter cn_tab = &sio_consdev; 319558885Simp return (0); 319651078Speter} 319751078Speter 319851078Speterint 319951078Spetersiogdbattach(port, speed) 320051078Speter int port; 320151078Speter int speed; 320251078Speter{ 320351078Speter int s; 320451078Speter u_char cfcr; 320551078Speter struct siocnstate sp; 320665714Sjhb int unit = 1; /* XXX !!! */ 320751078Speter 320851078Speter siogdbiobase = port; 320951078Speter gdbdefaultrate = speed; 321051078Speter 321165714Sjhb printf("sio%d: gdb debugging port\n", unit); 321265714Sjhb siogdbunit = unit; 321365714Sjhb#if DDB > 0 321465714Sjhb gdbdev = makedev(CDEV_MAJOR, unit); 321565714Sjhb gdb_getc = siocngetc; 321665714Sjhb gdb_putc = siocnputc; 321765714Sjhb#endif 321865714Sjhb 321951078Speter s = spltty(); 322051078Speter 322151078Speter /* 322251078Speter * Initialize the divisor latch. We can't rely on 322351078Speter * siocnopen() to do this the first time, since it 322451078Speter * avoids writing to the latch if the latch appears 322551078Speter * to have the correct value. Also, if we didn't 322651078Speter * just read the speed from the hardware, then we 322751078Speter * need to set the speed in hardware so that 322851078Speter * switching it later is null. 322951078Speter */ 323051078Speter cfcr = inb(siogdbiobase + com_cfcr); 323151078Speter outb(siogdbiobase + com_cfcr, CFCR_DLAB | cfcr); 323251078Speter outb(siogdbiobase + com_dlbl, 323351078Speter COMBRD(gdbdefaultrate) & 0xff); 323451078Speter outb(siogdbiobase + com_dlbh, 323551078Speter (u_int) COMBRD(gdbdefaultrate) >> 8); 323651078Speter outb(siogdbiobase + com_cfcr, cfcr); 323751078Speter 323851078Speter siocnopen(&sp, siogdbiobase, gdbdefaultrate); 323951078Speter splx(s); 324051078Speter 324158885Simp return (0); 324251078Speter} 324351078Speter 324451078Speter#endif 324551078Speter 324651078Speterstatic int 324751078Spetersiocncheckc(dev) 324851078Speter dev_t dev; 324951078Speter{ 325051078Speter int c; 325151078Speter Port_t iobase; 325251078Speter int s; 325351078Speter struct siocnstate sp; 325451078Speter 325551078Speter if (minor(dev) == siogdbunit) 325651078Speter iobase = siogdbiobase; 325751078Speter else 325851078Speter iobase = siocniobase; 325951078Speter s = spltty(); 326051078Speter siocnopen(&sp, iobase, comdefaultrate); 326151078Speter if (inb(iobase + com_lsr) & LSR_RXRDY) 326251078Speter c = inb(iobase + com_data); 326351078Speter else 326451078Speter c = -1; 326551078Speter siocnclose(&sp, iobase); 326651078Speter splx(s); 326751078Speter return (c); 326851078Speter} 326951078Speter 327051078Speter 327151078Speterint 327251078Spetersiocngetc(dev) 327351078Speter dev_t dev; 327451078Speter{ 327551078Speter int c; 327651078Speter Port_t iobase; 327751078Speter int s; 327851078Speter struct siocnstate sp; 327951078Speter 328051078Speter if (minor(dev) == siogdbunit) 328151078Speter iobase = siogdbiobase; 328251078Speter else 328351078Speter iobase = siocniobase; 328451078Speter s = spltty(); 328551078Speter siocnopen(&sp, iobase, comdefaultrate); 328651078Speter while (!(inb(iobase + com_lsr) & LSR_RXRDY)) 328751078Speter ; 328851078Speter c = inb(iobase + com_data); 328951078Speter siocnclose(&sp, iobase); 329051078Speter splx(s); 329151078Speter return (c); 329251078Speter} 329351078Speter 329451078Spetervoid 329551078Spetersiocnputc(dev, c) 329651078Speter dev_t dev; 329751078Speter int c; 329851078Speter{ 329951078Speter int s; 330051078Speter struct siocnstate sp; 330151078Speter Port_t iobase; 330251078Speter 330351078Speter if (minor(dev) == siogdbunit) 330451078Speter iobase = siogdbiobase; 330551078Speter else 330651078Speter iobase = siocniobase; 330751078Speter s = spltty(); 330851078Speter siocnopen(&sp, iobase, comdefaultrate); 330951078Speter siocntxwait(iobase); 331051078Speter outb(iobase + com_data, c); 331151078Speter siocnclose(&sp, iobase); 331251078Speter splx(s); 331351078Speter} 331451078Speter 331551078Speter#ifdef __alpha__ 331651078Speterint 331751078Spetersiogdbgetc() 331851078Speter{ 331951078Speter int c; 332051078Speter Port_t iobase; 332151078Speter int s; 332251078Speter struct siocnstate sp; 332351078Speter 332451078Speter iobase = siogdbiobase; 332551078Speter s = spltty(); 332651078Speter siocnopen(&sp, iobase, gdbdefaultrate); 332751078Speter while (!(inb(iobase + com_lsr) & LSR_RXRDY)) 332851078Speter ; 332951078Speter c = inb(iobase + com_data); 333051078Speter siocnclose(&sp, iobase); 333151078Speter splx(s); 333251078Speter return (c); 333351078Speter} 333451078Speter 333551078Spetervoid 333651078Spetersiogdbputc(c) 333751078Speter int c; 333851078Speter{ 333951078Speter int s; 334051078Speter struct siocnstate sp; 334151078Speter 334251078Speter s = spltty(); 334351078Speter siocnopen(&sp, siogdbiobase, gdbdefaultrate); 334451078Speter siocntxwait(siogdbiobase); 334551078Speter outb(siogdbiobase + com_data, c); 334651078Speter siocnclose(&sp, siogdbiobase); 334751078Speter splx(s); 334851078Speter} 334951078Speter#endif 335051078Speter 335152471SimpDRIVER_MODULE(sio, isa, sio_isa_driver, sio_devclass, 0, 0); 335252471Simp#if NCARD > 0 335352471SimpDRIVER_MODULE(sio, pccard, sio_pccard_driver, sio_devclass, 0, 0); 335452471Simp#endif 335558885Simp#if NPCI > 0 335658885SimpDRIVER_MODULE(sio, pci, sio_pci_driver, sio_devclass, 0, 0); 335758885Simp#endif 3358