sio.c revision 76869
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 76869 2001-05-20 03:12:55Z 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 */ 68857769Speter {0x3024a341, NULL}, /* PMC2430 - Pace 56 Voice Internal Modem */ 68954944Speter {0x1000eb49, NULL}, /* ROK0010 - Rockwell ? */ 69054944Speter {0x5002734a, NULL}, /* RSS0250 - 5614Jx3(G) Internal Modem */ 69160708Stanimura {0x6202734a, NULL}, /* RSS0262 - 5614Jx3[G] V90+K56Flex Modem */ 69254944Speter {0xc100ad4d, NULL}, /* SMM00C1 - Leopard 56k PnP */ 69356229Speter {0x9012b04e, NULL}, /* SUP1290 - Supra ? */ 69454944Speter {0x1013b04e, NULL}, /* SUP1310 - SupraExpress 336i PnP */ 69554944Speter {0x8013b04e, NULL}, /* SUP1380 - SupraExpress 288i PnP Voice */ 69654944Speter {0x8113b04e, NULL}, /* SUP1381 - SupraExpress 336i PnP Voice */ 69754944Speter {0x5016b04e, NULL}, /* SUP1650 - Supra 336i Sp Intl */ 69860947Stanimura {0x7016b04e, NULL}, /* SUP1670 - Supra 336i V+ Intl */ 69954944Speter {0x7420b04e, NULL}, /* SUP2070 - Supra ? */ 70054944Speter {0x8020b04e, NULL}, /* SUP2080 - Supra ? */ 70154944Speter {0x8420b04e, NULL}, /* SUP2084 - SupraExpress 56i PnP */ 70256229Speter {0x7121b04e, NULL}, /* SUP2171 - SupraExpress 56i Sp? */ 70354944Speter {0x8024b04e, NULL}, /* SUP2480 - Supra ? */ 70454944Speter {0x01007256, NULL}, /* USR0001 - U.S. Robotics Inc., Sportster W */ 70554944Speter {0x02007256, NULL}, /* USR0002 - U.S. Robotics Inc. Sportster 33. */ 70654944Speter {0x04007256, NULL}, /* USR0004 - USR Sportster 14.4k */ 70754944Speter {0x06007256, NULL}, /* USR0006 - USR Sportster 33.6k */ 70856229Speter {0x11007256, NULL}, /* USR0011 - USR ? */ 70954944Speter {0x01017256, NULL}, /* USR0101 - USR ? */ 71054944Speter {0x30207256, NULL}, /* USR2030 - U.S.Robotics Inc. Sportster 560 */ 71154944Speter {0x50207256, NULL}, /* USR2050 - U.S.Robotics Inc. Sportster 33. */ 71254944Speter {0x70207256, NULL}, /* USR2070 - U.S.Robotics Inc. Sportster 560 */ 71354944Speter {0x30307256, NULL}, /* USR3030 - U.S. Robotics 56K FAX INT */ 71454944Speter {0x31307256, NULL}, /* USR3031 - U.S. Robotics 56K FAX INT */ 71558848Speter {0x50307256, NULL}, /* USR3050 - U.S. Robotics 56K FAX INT */ 71654944Speter {0x70307256, NULL}, /* USR3070 - U.S. Robotics 56K Voice INT */ 71756229Speter {0x90307256, NULL}, /* USR3090 - USR ? */ 71864688Speter {0x70917256, NULL}, /* USR9170 - U.S. Robotics 56K FAX INT */ 71954944Speter {0x90917256, NULL}, /* USR9190 - USR 56k Voice INT */ 72054944Speter {0x0300695c, NULL}, /* WCI0003 - Fax/Voice/Modem/Speakphone/Asvd */ 72165469Ssheldonh {0x01a0896a, NULL}, /* ZTIA001 - Zoom Internal V90 Faxmodem */ 72254944Speter {0x61f7896a, NULL}, /* ZTIF761 - Zoom ComStar 33.6 */ 72351078Speter {0} 72451078Speter}; 72551078Speter 72656229Speter 72756229Speter 72851078Speterstatic int 72952471Simpsio_isa_probe(dev) 73052471Simp device_t dev; 73152471Simp{ 73252471Simp /* Check isapnp ids */ 73352471Simp if (ISA_PNP_PROBE(device_get_parent(dev), dev, sio_ids) == ENXIO) 73452471Simp return (ENXIO); 73558885Simp return (sioprobe(dev, 0)); 73652471Simp} 73752471Simp 73852471Simpstatic int 73958885Simpsioprobe(dev, xrid) 74051078Speter device_t dev; 74158885Simp int xrid; 74251078Speter{ 74353344Speter#if 0 74451078Speter static bool_t already_init; 74553344Speter device_t xdev; 74653344Speter#endif 74760471Snyan struct com_s *com; 74851078Speter bool_t failures[10]; 74951078Speter int fn; 75051078Speter device_t idev; 75151078Speter Port_t iobase; 75251078Speter intrmask_t irqmap[4]; 75351078Speter intrmask_t irqs; 75451078Speter u_char mcr_image; 75551078Speter int result; 75654206Speter u_long xirq; 75751088Speter u_int flags = device_get_flags(dev); 75851078Speter int rid; 75951078Speter struct resource *port; 76051078Speter 76158885Simp rid = xrid; 76251078Speter port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 76351078Speter 0, ~0, IO_COMSIZE, RF_ACTIVE); 76451078Speter if (!port) 76557915Simp return (ENXIO); 76651078Speter 76760471Snyan com = device_get_softc(dev); 76860471Snyan com->bst = rman_get_bustag(port); 76960471Snyan com->bsh = rman_get_bushandle(port); 77060471Snyan 77170174Sjhb if (atomic_cmpset_int(&sio_inited, 0, 1)) 77272238Sjhb mtx_init(&sio_lock, driver_name, MTX_SPIN); 77370174Sjhb 77453344Speter#if 0 77553344Speter /* 77653344Speter * XXX this is broken - when we are first called, there are no 77753344Speter * previously configured IO ports. We could hard code 77853344Speter * 0x3f8, 0x2f8, 0x3e8, 0x2e8 etc but that's probably worse. 77953344Speter * This code has been doing nothing since the conversion since 78053344Speter * "count" is zero the first time around. 78153344Speter */ 78251078Speter if (!already_init) { 78351078Speter /* 78451078Speter * Turn off MCR_IENABLE for all likely serial ports. An unused 78551078Speter * port with its MCR_IENABLE gate open will inhibit interrupts 78651078Speter * from any used port that shares the interrupt vector. 78751078Speter * XXX the gate enable is elsewhere for some multiports. 78851078Speter */ 78951078Speter device_t *devs; 79053344Speter int count, i, xioport; 79151078Speter 79251078Speter devclass_get_devices(sio_devclass, &devs, &count); 79351078Speter for (i = 0; i < count; i++) { 79451078Speter xdev = devs[i]; 79554194Speter if (device_is_enabled(xdev) && 79654194Speter bus_get_resource(xdev, SYS_RES_IOPORT, 0, &xioport, 79754194Speter NULL) == 0) 79853344Speter outb(xioport + com_mcr, 0); 79951078Speter } 80051078Speter free(devs, M_TEMP); 80151078Speter already_init = TRUE; 80251078Speter } 80353344Speter#endif 80451078Speter 80551078Speter if (COM_LLCONSOLE(flags)) { 80651078Speter printf("sio%d: reserved for low-level i/o\n", 80751078Speter device_get_unit(dev)); 80856788Sbde bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 80951078Speter return (ENXIO); 81051078Speter } 81151078Speter 81251078Speter /* 81351078Speter * If the device is on a multiport card and has an AST/4 81451078Speter * compatible interrupt control register, initialize this 81551078Speter * register and prepare to leave MCR_IENABLE clear in the mcr. 81651078Speter * Otherwise, prepare to set MCR_IENABLE in the mcr. 81751078Speter * Point idev to the device struct giving the correct id_irq. 81851078Speter * This is the struct for the master device if there is one. 81951078Speter */ 82051078Speter idev = dev; 82151078Speter mcr_image = MCR_IENABLE; 82251078Speter#ifdef COM_MULTIPORT 82357234Sbde if (COM_ISMULTIPORT(flags)) { 82454206Speter Port_t xiobase; 82554206Speter u_long io; 82654206Speter 82751078Speter idev = devclass_get_device(sio_devclass, COM_MPMASTER(flags)); 82851078Speter if (idev == NULL) { 82951078Speter printf("sio%d: master device %d not configured\n", 83051078Speter device_get_unit(dev), COM_MPMASTER(flags)); 83151078Speter idev = dev; 83251078Speter } 83357234Sbde if (!COM_NOTAST4(flags)) { 83457234Sbde if (bus_get_resource(idev, SYS_RES_IOPORT, 0, &io, 83557234Sbde NULL) == 0) { 83657234Sbde xiobase = io; 83757234Sbde if (bus_get_resource(idev, SYS_RES_IRQ, 0, 83857234Sbde NULL, NULL) == 0) 83957234Sbde outb(xiobase + com_scr, 0x80); 84057234Sbde else 84157234Sbde outb(xiobase + com_scr, 0); 84257234Sbde } 84357234Sbde mcr_image = 0; 84451078Speter } 84551078Speter } 84651078Speter#endif /* COM_MULTIPORT */ 84754194Speter if (bus_get_resource(idev, SYS_RES_IRQ, 0, NULL, NULL) != 0) 84851078Speter mcr_image = 0; 84951078Speter 85051078Speter bzero(failures, sizeof failures); 85151078Speter iobase = rman_get_start(port); 85251078Speter 85351078Speter /* 85451078Speter * We don't want to get actual interrupts, just masked ones. 85551078Speter * Interrupts from this line should already be masked in the ICU, 85651078Speter * but mask them in the processor as well in case there are some 85751078Speter * (misconfigured) shared interrupts. 85851078Speter */ 85972200Sbmilekic mtx_lock_spin(&sio_lock); 86051078Speter/* EXTRA DELAY? */ 86151078Speter 86251078Speter /* 86351078Speter * Initialize the speed and the word size and wait long enough to 86451078Speter * drain the maximum of 16 bytes of junk in device output queues. 86551078Speter * The speed is undefined after a master reset and must be set 86651078Speter * before relying on anything related to output. There may be 86751078Speter * junk after a (very fast) soft reboot and (apparently) after 86851078Speter * master reset. 86951078Speter * XXX what about the UART bug avoided by waiting in comparam()? 87051078Speter * We don't want to to wait long enough to drain at 2 bps. 87151078Speter */ 87251078Speter if (iobase == siocniobase) 87351078Speter DELAY((16 + 1) * 1000000 / (comdefaultrate / 10)); 87451078Speter else { 87560471Snyan sio_setreg(com, com_cfcr, CFCR_DLAB | CFCR_8BITS); 87660471Snyan sio_setreg(com, com_dlbl, COMBRD(SIO_TEST_SPEED) & 0xff); 87760471Snyan sio_setreg(com, com_dlbh, (u_int) COMBRD(SIO_TEST_SPEED) >> 8); 87860471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); 87951078Speter DELAY((16 + 1) * 1000000 / (SIO_TEST_SPEED / 10)); 88051078Speter } 88151078Speter 88251078Speter /* 88351078Speter * Enable the interrupt gate and disable device interupts. This 88451078Speter * should leave the device driving the interrupt line low and 88551078Speter * guarantee an edge trigger if an interrupt can be generated. 88651078Speter */ 88751078Speter/* EXTRA DELAY? */ 88860471Snyan sio_setreg(com, com_mcr, mcr_image); 88960471Snyan sio_setreg(com, com_ier, 0); 89051078Speter DELAY(1000); /* XXX */ 89151078Speter irqmap[0] = isa_irq_pending(); 89251078Speter 89351078Speter /* 89451078Speter * Attempt to set loopback mode so that we can send a null byte 89551078Speter * without annoying any external device. 89651078Speter */ 89751078Speter/* EXTRA DELAY? */ 89860471Snyan sio_setreg(com, com_mcr, mcr_image | MCR_LOOPBACK); 89951078Speter 90051078Speter /* 90151078Speter * Attempt to generate an output interrupt. On 8250's, setting 90251078Speter * IER_ETXRDY generates an interrupt independent of the current 90351078Speter * setting and independent of whether the THR is empty. On 16450's, 90451078Speter * setting IER_ETXRDY generates an interrupt independent of the 90551078Speter * current setting. On 16550A's, setting IER_ETXRDY only 90651078Speter * generates an interrupt when IER_ETXRDY is not already set. 90751078Speter */ 90860471Snyan sio_setreg(com, com_ier, IER_ETXRDY); 90951078Speter 91051078Speter /* 91151078Speter * On some 16x50 incompatibles, setting IER_ETXRDY doesn't generate 91251078Speter * an interrupt. They'd better generate one for actually doing 91351078Speter * output. Loopback may be broken on the same incompatibles but 91451078Speter * it's unlikely to do more than allow the null byte out. 91551078Speter */ 91660471Snyan sio_setreg(com, com_data, 0); 91751078Speter DELAY((1 + 2) * 1000000 / (SIO_TEST_SPEED / 10)); 91851078Speter 91951078Speter /* 92051078Speter * Turn off loopback mode so that the interrupt gate works again 92151078Speter * (MCR_IENABLE was hidden). This should leave the device driving 92251078Speter * an interrupt line high. It doesn't matter if the interrupt 92351078Speter * line oscillates while we are not looking at it, since interrupts 92451078Speter * are disabled. 92551078Speter */ 92651078Speter/* EXTRA DELAY? */ 92760471Snyan sio_setreg(com, com_mcr, mcr_image); 92851078Speter 92951078Speter /* 93052471Simp * Some pcmcia cards have the "TXRDY bug", so we check everyone 93151078Speter * for IIR_TXRDY implementation ( Palido 321s, DC-1S... ) 93251078Speter */ 93353370Speter if (COM_NOPROBE(flags)) { 93453370Speter /* Reading IIR register twice */ 93553370Speter for (fn = 0; fn < 2; fn ++) { 93653370Speter DELAY(10000); 93760471Snyan failures[6] = sio_getreg(com, com_iir); 93853370Speter } 93953370Speter /* Check IIR_TXRDY clear ? */ 94053370Speter result = 0; 94153370Speter if (failures[6] & IIR_TXRDY) { 94253370Speter /* Nop, Double check with clearing IER */ 94360471Snyan sio_setreg(com, com_ier, 0); 94460471Snyan if (sio_getreg(com, com_iir) & IIR_NOPEND) { 94553370Speter /* Ok. we're familia this gang */ 94653370Speter SET_FLAG(dev, COM_C_IIR_TXRDYBUG); 94753370Speter } else { 94853370Speter /* Unknown, Just omit this chip.. XXX */ 94953370Speter result = ENXIO; 95053370Speter } 95151078Speter } else { 95253370Speter /* OK. this is well-known guys */ 95353370Speter CLR_FLAG(dev, COM_C_IIR_TXRDYBUG); 95451078Speter } 95560471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); 95672200Sbmilekic mtx_unlock_spin(&sio_lock); 95753344Speter bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 95853344Speter return (iobase == siocniobase ? 0 : result); 95953344Speter } 96053344Speter 96151078Speter /* 96251078Speter * Check that 96351078Speter * o the CFCR, IER and MCR in UART hold the values written to them 96451078Speter * (the values happen to be all distinct - this is good for 96551078Speter * avoiding false positive tests from bus echoes). 96651078Speter * o an output interrupt is generated and its vector is correct. 96751078Speter * o the interrupt goes away when the IIR in the UART is read. 96851078Speter */ 96951078Speter/* EXTRA DELAY? */ 97060471Snyan failures[0] = sio_getreg(com, com_cfcr) - CFCR_8BITS; 97160471Snyan failures[1] = sio_getreg(com, com_ier) - IER_ETXRDY; 97260471Snyan failures[2] = sio_getreg(com, com_mcr) - mcr_image; 97351078Speter DELAY(10000); /* Some internal modems need this time */ 97451078Speter irqmap[1] = isa_irq_pending(); 97560471Snyan failures[4] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_TXRDY; 97651078Speter DELAY(1000); /* XXX */ 97751078Speter irqmap[2] = isa_irq_pending(); 97860471Snyan failures[6] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 97951078Speter 98051078Speter /* 98151078Speter * Turn off all device interrupts and check that they go off properly. 98251078Speter * Leave MCR_IENABLE alone. For ports without a master port, it gates 98351078Speter * the OUT2 output of the UART to 98451078Speter * the ICU input. Closing the gate would give a floating ICU input 98551078Speter * (unless there is another device driving it) and spurious interrupts. 98651078Speter * (On the system that this was first tested on, the input floats high 98751078Speter * and gives a (masked) interrupt as soon as the gate is closed.) 98851078Speter */ 98960471Snyan sio_setreg(com, com_ier, 0); 99060471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); /* dummy to avoid bus echo */ 99160471Snyan failures[7] = sio_getreg(com, com_ier); 99251078Speter DELAY(1000); /* XXX */ 99351078Speter irqmap[3] = isa_irq_pending(); 99460471Snyan failures[9] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 99551078Speter 99672200Sbmilekic mtx_unlock_spin(&sio_lock); 99751078Speter 99851078Speter irqs = irqmap[1] & ~irqmap[0]; 99954194Speter if (bus_get_resource(idev, SYS_RES_IRQ, 0, &xirq, NULL) == 0 && 100054194Speter ((1 << xirq) & irqs) == 0) 100151078Speter printf( 100254206Speter "sio%d: configured irq %ld not in bitmap of probed irqs %#x\n", 100353344Speter device_get_unit(dev), xirq, irqs); 100451078Speter if (bootverbose) 100551078Speter printf("sio%d: irq maps: %#x %#x %#x %#x\n", 100651078Speter device_get_unit(dev), 100751078Speter irqmap[0], irqmap[1], irqmap[2], irqmap[3]); 100851078Speter 100951078Speter result = 0; 101051078Speter for (fn = 0; fn < sizeof failures; ++fn) 101151078Speter if (failures[fn]) { 101260471Snyan sio_setreg(com, com_mcr, 0); 101351078Speter result = ENXIO; 101451078Speter if (bootverbose) { 101551078Speter printf("sio%d: probe failed test(s):", 101651078Speter device_get_unit(dev)); 101751078Speter for (fn = 0; fn < sizeof failures; ++fn) 101851078Speter if (failures[fn]) 101951078Speter printf(" %d", fn); 102051078Speter printf("\n"); 102151078Speter } 102251078Speter break; 102351078Speter } 102451078Speter bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 102551078Speter return (iobase == siocniobase ? 0 : result); 102651078Speter} 102751078Speter 102851078Speter#ifdef COM_ESP 102951078Speterstatic int 103051078Speterespattach(com, esp_port) 103151078Speter struct com_s *com; 103251078Speter Port_t esp_port; 103351078Speter{ 103451078Speter u_char dips; 103551078Speter u_char val; 103651078Speter 103751078Speter /* 103851078Speter * Check the ESP-specific I/O port to see if we're an ESP 103951078Speter * card. If not, return failure immediately. 104051078Speter */ 104151078Speter if ((inb(esp_port) & 0xf3) == 0) { 104251078Speter printf(" port 0x%x is not an ESP board?\n", esp_port); 104351078Speter return (0); 104451078Speter } 104551078Speter 104651078Speter /* 104751078Speter * We've got something that claims to be a Hayes ESP card. 104851078Speter * Let's hope so. 104951078Speter */ 105051078Speter 105151078Speter /* Get the dip-switch configuration */ 105251078Speter outb(esp_port + ESP_CMD1, ESP_GETDIPS); 105351078Speter dips = inb(esp_port + ESP_STATUS1); 105451078Speter 105551078Speter /* 105651078Speter * Bits 0,1 of dips say which COM port we are. 105751078Speter */ 105860471Snyan if (rman_get_start(com->ioportres) == likely_com_ports[dips & 0x03]) 105951078Speter printf(" : ESP"); 106051078Speter else { 106151078Speter printf(" esp_port has com %d\n", dips & 0x03); 106251078Speter return (0); 106351078Speter } 106451078Speter 106551078Speter /* 106651078Speter * Check for ESP version 2.0 or later: bits 4,5,6 = 010. 106751078Speter */ 106851078Speter outb(esp_port + ESP_CMD1, ESP_GETTEST); 106951078Speter val = inb(esp_port + ESP_STATUS1); /* clear reg 1 */ 107051078Speter val = inb(esp_port + ESP_STATUS2); 107151078Speter if ((val & 0x70) < 0x20) { 107251078Speter printf("-old (%o)", val & 0x70); 107351078Speter return (0); 107451078Speter } 107551078Speter 107651078Speter /* 107751078Speter * Check for ability to emulate 16550: bit 7 == 1 107851078Speter */ 107951078Speter if ((dips & 0x80) == 0) { 108051078Speter printf(" slave"); 108151078Speter return (0); 108251078Speter } 108351078Speter 108451078Speter /* 108551078Speter * Okay, we seem to be a Hayes ESP card. Whee. 108651078Speter */ 108751078Speter com->esp = TRUE; 108851078Speter com->esp_port = esp_port; 108951078Speter return (1); 109051078Speter} 109151078Speter#endif /* COM_ESP */ 109251078Speter 109351078Speterstatic int 109452471Simpsio_isa_attach(dev) 109552471Simp device_t dev; 109652471Simp{ 109758885Simp return (sioattach(dev, 0)); 109852471Simp} 109952471Simp 110052471Simpstatic int 110158885Simpsioattach(dev, xrid) 110251078Speter device_t dev; 110358885Simp int xrid; 110451078Speter{ 110551078Speter struct com_s *com; 110651078Speter#ifdef COM_ESP 110751078Speter Port_t *espp; 110851078Speter#endif 110951078Speter Port_t iobase; 111051078Speter int unit; 111153344Speter u_int flags; 111251078Speter int rid; 111351078Speter struct resource *port; 111453344Speter int ret; 111551078Speter 111658885Simp rid = xrid; 111751078Speter port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 111851078Speter 0, ~0, IO_COMSIZE, RF_ACTIVE); 111951078Speter if (!port) 112057915Simp return (ENXIO); 112151078Speter 112251078Speter iobase = rman_get_start(port); 112351078Speter unit = device_get_unit(dev); 112451078Speter com = device_get_softc(dev); 112553344Speter flags = device_get_flags(dev); 112651078Speter 112753344Speter if (unit >= sio_numunits) 112853344Speter sio_numunits = unit + 1; 112951078Speter /* 113051078Speter * sioprobe() has initialized the device registers as follows: 113151078Speter * o cfcr = CFCR_8BITS. 113251078Speter * It is most important that CFCR_DLAB is off, so that the 113351078Speter * data port is not hidden when we enable interrupts. 113451078Speter * o ier = 0. 113551078Speter * Interrupts are only enabled when the line is open. 113651078Speter * o mcr = MCR_IENABLE, or 0 if the port has AST/4 compatible 113751078Speter * interrupt control register or the config specifies no irq. 113851078Speter * Keeping MCR_DTR and MCR_RTS off might stop the external 113951078Speter * device from sending before we are ready. 114051078Speter */ 114151078Speter bzero(com, sizeof *com); 114251078Speter com->unit = unit; 114351078Speter com->ioportres = port; 114460471Snyan com->bst = rman_get_bustag(port); 114560471Snyan com->bsh = rman_get_bushandle(port); 114651078Speter com->cfcr_image = CFCR_8BITS; 114751078Speter com->dtr_wait = 3 * hz; 114851078Speter com->loses_outints = COM_LOSESOUTINTS(flags) != 0; 114957234Sbde com->no_irq = bus_get_resource(dev, SYS_RES_IRQ, 0, NULL, NULL) != 0; 115051078Speter com->tx_fifo_size = 1; 115151078Speter com->obufs[0].l_head = com->obuf1; 115251078Speter com->obufs[1].l_head = com->obuf2; 115351078Speter 115451078Speter com->data_port = iobase + com_data; 115551078Speter com->int_id_port = iobase + com_iir; 115651078Speter com->modem_ctl_port = iobase + com_mcr; 115751078Speter com->mcr_image = inb(com->modem_ctl_port); 115851078Speter com->line_status_port = iobase + com_lsr; 115951078Speter com->modem_status_port = iobase + com_msr; 116051078Speter com->intr_ctl_port = iobase + com_ier; 116151078Speter 116251078Speter /* 116351078Speter * We don't use all the flags from <sys/ttydefaults.h> since they 116451078Speter * are only relevant for logins. It's important to have echo off 116551078Speter * initially so that the line doesn't start blathering before the 116651078Speter * echo flag can be turned off. 116751078Speter */ 116851078Speter com->it_in.c_iflag = 0; 116951078Speter com->it_in.c_oflag = 0; 117051078Speter com->it_in.c_cflag = TTYDEF_CFLAG; 117151078Speter com->it_in.c_lflag = 0; 117251078Speter if (unit == comconsole) { 117351078Speter com->it_in.c_iflag = TTYDEF_IFLAG; 117451078Speter com->it_in.c_oflag = TTYDEF_OFLAG; 117551078Speter com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL; 117651078Speter com->it_in.c_lflag = TTYDEF_LFLAG; 117751078Speter com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL; 117851078Speter com->lt_out.c_ispeed = com->lt_out.c_ospeed = 117951078Speter com->lt_in.c_ispeed = com->lt_in.c_ospeed = 118051078Speter com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate; 118151078Speter } else 118251078Speter com->it_in.c_ispeed = com->it_in.c_ospeed = TTYDEF_SPEED; 118365605Sjhb if (siosetwater(com, com->it_in.c_ispeed) != 0) { 118472200Sbmilekic mtx_unlock_spin(&sio_lock); 118556788Sbde /* 118656788Sbde * Leave i/o resources allocated if this is a `cn'-level 118756788Sbde * console, so that other devices can't snarf them. 118856788Sbde */ 118956788Sbde if (iobase != siocniobase) 119056788Sbde bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 119156788Sbde return (ENOMEM); 119251078Speter } 119372200Sbmilekic mtx_unlock_spin(&sio_lock); 119451078Speter termioschars(&com->it_in); 119551078Speter com->it_out = com->it_in; 119651078Speter 119751078Speter /* attempt to determine UART type */ 119851078Speter printf("sio%d: type", unit); 119951078Speter 120051078Speter 120151078Speter#ifdef COM_MULTIPORT 120251078Speter if (!COM_ISMULTIPORT(flags) && !COM_IIR_TXRDYBUG(flags)) 120351078Speter#else 120451078Speter if (!COM_IIR_TXRDYBUG(flags)) 120551078Speter#endif 120651078Speter { 120751078Speter u_char scr; 120851078Speter u_char scr1; 120951078Speter u_char scr2; 121051078Speter 121160471Snyan scr = sio_getreg(com, com_scr); 121260471Snyan sio_setreg(com, com_scr, 0xa5); 121360471Snyan scr1 = sio_getreg(com, com_scr); 121460471Snyan sio_setreg(com, com_scr, 0x5a); 121560471Snyan scr2 = sio_getreg(com, com_scr); 121660471Snyan sio_setreg(com, com_scr, scr); 121751078Speter if (scr1 != 0xa5 || scr2 != 0x5a) { 121851078Speter printf(" 8250"); 121951078Speter goto determined_type; 122051078Speter } 122151078Speter } 122260471Snyan sio_setreg(com, com_fifo, FIFO_ENABLE | FIFO_RX_HIGH); 122351078Speter DELAY(100); 122451078Speter com->st16650a = 0; 122551078Speter switch (inb(com->int_id_port) & IIR_FIFO_MASK) { 122651078Speter case FIFO_RX_LOW: 122751078Speter printf(" 16450"); 122851078Speter break; 122951078Speter case FIFO_RX_MEDL: 123051078Speter printf(" 16450?"); 123151078Speter break; 123251078Speter case FIFO_RX_MEDH: 123351078Speter printf(" 16550?"); 123451078Speter break; 123551078Speter case FIFO_RX_HIGH: 123651078Speter if (COM_NOFIFO(flags)) { 123751078Speter printf(" 16550A fifo disabled"); 123851078Speter } else { 123951078Speter com->hasfifo = TRUE; 124051078Speter if (COM_ST16650A(flags)) { 124151078Speter com->st16650a = 1; 124251078Speter com->tx_fifo_size = 32; 124351078Speter printf(" ST16650A"); 124451078Speter } else { 124551078Speter com->tx_fifo_size = COM_FIFOSIZE(flags); 124651078Speter printf(" 16550A"); 124751078Speter } 124851078Speter } 124951078Speter#ifdef COM_ESP 125051078Speter for (espp = likely_esp_ports; *espp != 0; espp++) 125151078Speter if (espattach(com, *espp)) { 125251078Speter com->tx_fifo_size = 1024; 125351078Speter break; 125451078Speter } 125551078Speter#endif 125651078Speter if (!com->st16650a) { 125751078Speter if (!com->tx_fifo_size) 125851078Speter com->tx_fifo_size = 16; 125951078Speter else 126051078Speter printf(" lookalike with %d bytes FIFO", 126151078Speter com->tx_fifo_size); 126251078Speter } 126351078Speter 126451078Speter break; 126551078Speter } 126651078Speter 126751078Speter#ifdef COM_ESP 126851078Speter if (com->esp) { 126951078Speter /* 127051078Speter * Set 16550 compatibility mode. 127151078Speter * We don't use the ESP_MODE_SCALE bit to increase the 127251078Speter * fifo trigger levels because we can't handle large 127351078Speter * bursts of input. 127451078Speter * XXX flow control should be set in comparam(), not here. 127551078Speter */ 127651078Speter outb(com->esp_port + ESP_CMD1, ESP_SETMODE); 127751078Speter outb(com->esp_port + ESP_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO); 127851078Speter 127951078Speter /* Set RTS/CTS flow control. */ 128051078Speter outb(com->esp_port + ESP_CMD1, ESP_SETFLOWTYPE); 128151078Speter outb(com->esp_port + ESP_CMD2, ESP_FLOW_RTS); 128251078Speter outb(com->esp_port + ESP_CMD2, ESP_FLOW_CTS); 128351078Speter 128451078Speter /* Set flow-control levels. */ 128551078Speter outb(com->esp_port + ESP_CMD1, ESP_SETRXFLOW); 128651078Speter outb(com->esp_port + ESP_CMD2, HIBYTE(768)); 128751078Speter outb(com->esp_port + ESP_CMD2, LOBYTE(768)); 128851078Speter outb(com->esp_port + ESP_CMD2, HIBYTE(512)); 128951078Speter outb(com->esp_port + ESP_CMD2, LOBYTE(512)); 129051078Speter } 129151078Speter#endif /* COM_ESP */ 129260471Snyan sio_setreg(com, com_fifo, 0); 129351078Speterdetermined_type: ; 129451078Speter 129551078Speter#ifdef COM_MULTIPORT 129651078Speter if (COM_ISMULTIPORT(flags)) { 129753344Speter device_t masterdev; 129853344Speter 129951078Speter com->multiport = TRUE; 130051078Speter printf(" (multiport"); 130151078Speter if (unit == COM_MPMASTER(flags)) 130251078Speter printf(" master"); 130351078Speter printf(")"); 130453344Speter masterdev = devclass_get_device(sio_devclass, 130553344Speter COM_MPMASTER(flags)); 130657234Sbde com->no_irq = (masterdev == NULL || bus_get_resource(masterdev, 130757234Sbde SYS_RES_IRQ, 0, NULL, NULL) != 0); 130851078Speter } 130951078Speter#endif /* COM_MULTIPORT */ 131051078Speter if (unit == comconsole) 131151078Speter printf(", console"); 131253344Speter if (COM_IIR_TXRDYBUG(flags)) 131351078Speter printf(" with a bogus IIR_TXRDY register"); 131451078Speter printf("\n"); 131551078Speter 131667551Sjhb if (sio_fast_ih == NULL) { 131772238Sjhb swi_add(&tty_ithd, "tty:sio", siopoll, NULL, SWI_TTY, 0, 131872238Sjhb &sio_fast_ih); 131972238Sjhb swi_add(&clk_ithd, "tty:sio", siopoll, NULL, SWI_TTY, 0, 132072238Sjhb &sio_slow_ih); 132151078Speter } 132265131Sphk com->devs[0] = make_dev(&sio_cdevsw, unit, 132351078Speter UID_ROOT, GID_WHEEL, 0600, "ttyd%r", unit); 132465131Sphk com->devs[1] = make_dev(&sio_cdevsw, unit | CONTROL_INIT_STATE, 132551078Speter UID_ROOT, GID_WHEEL, 0600, "ttyid%r", unit); 132665131Sphk com->devs[2] = make_dev(&sio_cdevsw, unit | CONTROL_LOCK_STATE, 132751078Speter UID_ROOT, GID_WHEEL, 0600, "ttyld%r", unit); 132865131Sphk com->devs[3] = make_dev(&sio_cdevsw, unit | CALLOUT_MASK, 132951078Speter UID_UUCP, GID_DIALER, 0660, "cuaa%r", unit); 133065131Sphk com->devs[4] = make_dev(&sio_cdevsw, 133165131Sphk unit | CALLOUT_MASK | CONTROL_INIT_STATE, 133251078Speter UID_UUCP, GID_DIALER, 0660, "cuaia%r", unit); 133365131Sphk com->devs[5] = make_dev(&sio_cdevsw, 133465131Sphk unit | CALLOUT_MASK | CONTROL_LOCK_STATE, 133551078Speter UID_UUCP, GID_DIALER, 0660, "cuala%r", unit); 133651078Speter com->flags = flags; 133751078Speter com->pps.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR; 133851078Speter pps_init(&com->pps); 133951078Speter 134051078Speter rid = 0; 134151078Speter com->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0ul, ~0ul, 1, 134253344Speter RF_ACTIVE); 134353344Speter if (com->irqres) { 134453344Speter ret = BUS_SETUP_INTR(device_get_parent(dev), dev, com->irqres, 134565557Sjasone INTR_TYPE_TTY | INTR_FAST, 134654386Simp siointr, com, &com->cookie); 134754194Speter if (ret) { 134854194Speter ret = BUS_SETUP_INTR(device_get_parent(dev), dev, 134954194Speter com->irqres, INTR_TYPE_TTY, 135054386Simp siointr, com, &com->cookie); 135154194Speter if (ret == 0) 135254194Speter device_printf(dev, "unable to activate interrupt in fast mode - using normal mode"); 135354194Speter } 135453344Speter if (ret) 135553344Speter device_printf(dev, "could not activate interrupt\n"); 135653344Speter } 135751078Speter 135851078Speter return (0); 135951078Speter} 136051078Speter 136151078Speterstatic int 136251078Spetersioopen(dev, flag, mode, p) 136351078Speter dev_t dev; 136451078Speter int flag; 136551078Speter int mode; 136651078Speter struct proc *p; 136751078Speter{ 136851078Speter struct com_s *com; 136951078Speter int error; 137051078Speter int mynor; 137151078Speter int s; 137251078Speter struct tty *tp; 137351078Speter int unit; 137451078Speter 137551078Speter mynor = minor(dev); 137651078Speter unit = MINOR_TO_UNIT(mynor); 137753344Speter com = com_addr(unit); 137853344Speter if (com == NULL) 137951078Speter return (ENXIO); 138051078Speter if (com->gone) 138151078Speter return (ENXIO); 138251078Speter if (mynor & CONTROL_MASK) 138351078Speter return (0); 138451078Speter tp = dev->si_tty = com->tp = ttymalloc(com->tp); 138551078Speter s = spltty(); 138651078Speter /* 138751078Speter * We jump to this label after all non-interrupted sleeps to pick 138851078Speter * up any changes of the device state. 138951078Speter */ 139051078Speteropen_top: 139151078Speter while (com->state & CS_DTR_OFF) { 139251078Speter error = tsleep(&com->dtr_wait, TTIPRI | PCATCH, "siodtr", 0); 139351078Speter if (com_addr(unit) == NULL) 139451078Speter return (ENXIO); 139551078Speter if (error != 0 || com->gone) 139651078Speter goto out; 139751078Speter } 139851078Speter if (tp->t_state & TS_ISOPEN) { 139951078Speter /* 140051078Speter * The device is open, so everything has been initialized. 140151078Speter * Handle conflicts. 140251078Speter */ 140351078Speter if (mynor & CALLOUT_MASK) { 140451078Speter if (!com->active_out) { 140551078Speter error = EBUSY; 140651078Speter goto out; 140751078Speter } 140851078Speter } else { 140951078Speter if (com->active_out) { 141051078Speter if (flag & O_NONBLOCK) { 141151078Speter error = EBUSY; 141251078Speter goto out; 141351078Speter } 141451078Speter error = tsleep(&com->active_out, 141551078Speter TTIPRI | PCATCH, "siobi", 0); 141651078Speter if (com_addr(unit) == NULL) 141751078Speter return (ENXIO); 141851078Speter if (error != 0 || com->gone) 141951078Speter goto out; 142051078Speter goto open_top; 142151078Speter } 142251078Speter } 142351078Speter if (tp->t_state & TS_XCLUDE && 142451078Speter suser(p)) { 142551078Speter error = EBUSY; 142651078Speter goto out; 142751078Speter } 142851078Speter } else { 142951078Speter /* 143051078Speter * The device isn't open, so there are no conflicts. 143151078Speter * Initialize it. Initialization is done twice in many 143251078Speter * cases: to preempt sleeping callin opens if we are 143351078Speter * callout, and to complete a callin open after DCD rises. 143451078Speter */ 143551078Speter tp->t_oproc = comstart; 143651078Speter tp->t_param = comparam; 143751654Sphk tp->t_stop = comstop; 143851078Speter tp->t_dev = dev; 143951078Speter tp->t_termios = mynor & CALLOUT_MASK 144051078Speter ? com->it_out : com->it_in; 144151078Speter (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET); 144251078Speter com->poll = com->no_irq; 144351078Speter com->poll_output = com->loses_outints; 144451078Speter ++com->wopeners; 144551078Speter error = comparam(tp, &tp->t_termios); 144651078Speter --com->wopeners; 144751078Speter if (error != 0) 144851078Speter goto out; 144951078Speter /* 145051078Speter * XXX we should goto open_top if comparam() slept. 145151078Speter */ 145251078Speter if (com->hasfifo) { 145351078Speter /* 145451078Speter * (Re)enable and drain fifos. 145551078Speter * 145651078Speter * Certain SMC chips cause problems if the fifos 145751078Speter * are enabled while input is ready. Turn off the 145851078Speter * fifo if necessary to clear the input. We test 145951078Speter * the input ready bit after enabling the fifos 146051078Speter * since we've already enabled them in comparam() 146151078Speter * and to handle races between enabling and fresh 146251078Speter * input. 146351078Speter */ 146451078Speter while (TRUE) { 146560471Snyan sio_setreg(com, com_fifo, 146660471Snyan FIFO_RCV_RST | FIFO_XMT_RST 146760471Snyan | com->fifo_image); 146851078Speter /* 146951078Speter * XXX the delays are for superstitious 147051078Speter * historical reasons. It must be less than 147151078Speter * the character time at the maximum 147251078Speter * supported speed (87 usec at 115200 bps 147351078Speter * 8N1). Otherwise we might loop endlessly 147451078Speter * if data is streaming in. We used to use 147551078Speter * delays of 100. That usually worked 147651078Speter * because DELAY(100) used to usually delay 147751078Speter * for about 85 usec instead of 100. 147851078Speter */ 147951078Speter DELAY(50); 148051078Speter if (!(inb(com->line_status_port) & LSR_RXRDY)) 148151078Speter break; 148260471Snyan sio_setreg(com, com_fifo, 0); 148351078Speter DELAY(50); 148451078Speter (void) inb(com->data_port); 148551078Speter } 148651078Speter } 148751078Speter 148872200Sbmilekic mtx_lock_spin(&sio_lock); 148951078Speter (void) inb(com->line_status_port); 149051078Speter (void) inb(com->data_port); 149151078Speter com->prev_modem_status = com->last_modem_status 149251078Speter = inb(com->modem_status_port); 149351078Speter if (COM_IIR_TXRDYBUG(com->flags)) { 149451078Speter outb(com->intr_ctl_port, IER_ERXRDY | IER_ERLS 149551078Speter | IER_EMSC); 149651078Speter } else { 149751078Speter outb(com->intr_ctl_port, IER_ERXRDY | IER_ETXRDY 149851078Speter | IER_ERLS | IER_EMSC); 149951078Speter } 150072200Sbmilekic mtx_unlock_spin(&sio_lock); 150151078Speter /* 150251078Speter * Handle initial DCD. Callout devices get a fake initial 150351078Speter * DCD (trapdoor DCD). If we are callout, then any sleeping 150451078Speter * callin opens get woken up and resume sleeping on "siobi" 150551078Speter * instead of "siodcd". 150651078Speter */ 150751078Speter /* 150851078Speter * XXX `mynor & CALLOUT_MASK' should be 150951078Speter * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where 151051078Speter * TRAPDOOR_CARRIER is the default initial state for callout 151151078Speter * devices and SOFT_CARRIER is like CLOCAL except it hides 151251078Speter * the true carrier. 151351078Speter */ 151451078Speter if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK) 151551078Speter (*linesw[tp->t_line].l_modem)(tp, 1); 151651078Speter } 151751078Speter /* 151851078Speter * Wait for DCD if necessary. 151951078Speter */ 152051078Speter if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK) 152151078Speter && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) { 152251078Speter ++com->wopeners; 152351078Speter error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "siodcd", 0); 152451078Speter if (com_addr(unit) == NULL) 152551078Speter return (ENXIO); 152651078Speter --com->wopeners; 152751078Speter if (error != 0 || com->gone) 152851078Speter goto out; 152951078Speter goto open_top; 153051078Speter } 153151078Speter error = (*linesw[tp->t_line].l_open)(dev, tp); 153251078Speter disc_optim(tp, &tp->t_termios, com); 153351078Speter if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK) 153451078Speter com->active_out = TRUE; 153551078Speter siosettimeout(); 153651078Speterout: 153751078Speter splx(s); 153851078Speter if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0) 153951078Speter comhardclose(com); 154051078Speter return (error); 154151078Speter} 154251078Speter 154351078Speterstatic int 154451078Spetersioclose(dev, flag, mode, p) 154551078Speter dev_t dev; 154651078Speter int flag; 154751078Speter int mode; 154851078Speter struct proc *p; 154951078Speter{ 155051078Speter struct com_s *com; 155151078Speter int mynor; 155251078Speter int s; 155351078Speter struct tty *tp; 155451078Speter 155551078Speter mynor = minor(dev); 155651078Speter if (mynor & CONTROL_MASK) 155751078Speter return (0); 155851078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 155957915Simp if (com == NULL) 156057915Simp return (ENODEV); 156151078Speter tp = com->tp; 156251078Speter s = spltty(); 156351078Speter (*linesw[tp->t_line].l_close)(tp, flag); 156451078Speter disc_optim(tp, &tp->t_termios, com); 156551654Sphk comstop(tp, FREAD | FWRITE); 156651078Speter comhardclose(com); 156751078Speter ttyclose(tp); 156851078Speter siosettimeout(); 156951078Speter splx(s); 157051078Speter if (com->gone) { 157151078Speter printf("sio%d: gone\n", com->unit); 157251078Speter s = spltty(); 157351078Speter if (com->ibuf != NULL) 157451078Speter free(com->ibuf, M_DEVBUF); 157551078Speter bzero(tp, sizeof *tp); 157651078Speter splx(s); 157751078Speter } 157851078Speter return (0); 157951078Speter} 158051078Speter 158151078Speterstatic void 158251078Spetercomhardclose(com) 158351078Speter struct com_s *com; 158451078Speter{ 158551078Speter int s; 158651078Speter struct tty *tp; 158751078Speter int unit; 158851078Speter 158951078Speter unit = com->unit; 159051078Speter s = spltty(); 159151078Speter com->poll = FALSE; 159251078Speter com->poll_output = FALSE; 159351078Speter com->do_timestamp = FALSE; 159451078Speter com->do_dcd_timestamp = FALSE; 159551078Speter com->pps.ppsparam.mode = 0; 159660471Snyan sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 159751078Speter { 159860471Snyan sio_setreg(com, com_ier, 0); 159951078Speter tp = com->tp; 160051078Speter if (tp->t_cflag & HUPCL 160151078Speter /* 160251078Speter * XXX we will miss any carrier drop between here and the 160351078Speter * next open. Perhaps we should watch DCD even when the 160451078Speter * port is closed; it is not sufficient to check it at 160551078Speter * the next open because it might go up and down while 160651078Speter * we're not watching. 160751078Speter */ 160851078Speter || (!com->active_out 160951078Speter && !(com->prev_modem_status & MSR_DCD) 161051078Speter && !(com->it_in.c_cflag & CLOCAL)) 161151078Speter || !(tp->t_state & TS_ISOPEN)) { 161251078Speter (void)commctl(com, TIOCM_DTR, DMBIC); 161351078Speter if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) { 161451078Speter timeout(siodtrwakeup, com, com->dtr_wait); 161551078Speter com->state |= CS_DTR_OFF; 161651078Speter } 161751078Speter } 161851078Speter } 161951078Speter if (com->hasfifo) { 162051078Speter /* 162151078Speter * Disable fifos so that they are off after controlled 162251078Speter * reboots. Some BIOSes fail to detect 16550s when the 162351078Speter * fifos are enabled. 162451078Speter */ 162560471Snyan sio_setreg(com, com_fifo, 0); 162651078Speter } 162751078Speter com->active_out = FALSE; 162851078Speter wakeup(&com->active_out); 162951078Speter wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */ 163051078Speter splx(s); 163151078Speter} 163251078Speter 163351078Speterstatic int 163451078Spetersioread(dev, uio, flag) 163551078Speter dev_t dev; 163651078Speter struct uio *uio; 163751078Speter int flag; 163851078Speter{ 163951078Speter int mynor; 164051078Speter struct com_s *com; 164151078Speter 164251078Speter mynor = minor(dev); 164351078Speter if (mynor & CONTROL_MASK) 164451078Speter return (ENODEV); 164551078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 164657915Simp if (com == NULL || com->gone) 164751078Speter return (ENODEV); 164851078Speter return ((*linesw[com->tp->t_line].l_read)(com->tp, uio, flag)); 164951078Speter} 165051078Speter 165151078Speterstatic int 165251078Spetersiowrite(dev, uio, flag) 165351078Speter dev_t dev; 165451078Speter struct uio *uio; 165551078Speter int flag; 165651078Speter{ 165751078Speter int mynor; 165851078Speter struct com_s *com; 165951078Speter int unit; 166051078Speter 166151078Speter mynor = minor(dev); 166251078Speter if (mynor & CONTROL_MASK) 166351078Speter return (ENODEV); 166451078Speter 166551078Speter unit = MINOR_TO_UNIT(mynor); 166651078Speter com = com_addr(unit); 166757915Simp if (com == NULL || com->gone) 166851078Speter return (ENODEV); 166951078Speter /* 167051078Speter * (XXX) We disallow virtual consoles if the physical console is 167151078Speter * a serial port. This is in case there is a display attached that 167251078Speter * is not the console. In that situation we don't need/want the X 167351078Speter * server taking over the console. 167451078Speter */ 167551078Speter if (constty != NULL && unit == comconsole) 167651078Speter constty = NULL; 167751078Speter return ((*linesw[com->tp->t_line].l_write)(com->tp, uio, flag)); 167851078Speter} 167951078Speter 168051078Speterstatic void 168151078Spetersiobusycheck(chan) 168251078Speter void *chan; 168351078Speter{ 168451078Speter struct com_s *com; 168551078Speter int s; 168651078Speter 168751078Speter com = (struct com_s *)chan; 168851078Speter 168951078Speter /* 169051078Speter * Clear TS_BUSY if low-level output is complete. 169151078Speter * spl locking is sufficient because siointr1() does not set CS_BUSY. 169251078Speter * If siointr1() clears CS_BUSY after we look at it, then we'll get 169351078Speter * called again. Reading the line status port outside of siointr1() 169451078Speter * is safe because CS_BUSY is clear so there are no output interrupts 169551078Speter * to lose. 169651078Speter */ 169751078Speter s = spltty(); 169851078Speter if (com->state & CS_BUSY) 169951078Speter com->extra_state &= ~CSE_BUSYCHECK; /* False alarm. */ 170051078Speter else if ((inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY)) 170151078Speter == (LSR_TSRE | LSR_TXRDY)) { 170251078Speter com->tp->t_state &= ~TS_BUSY; 170351078Speter ttwwakeup(com->tp); 170451078Speter com->extra_state &= ~CSE_BUSYCHECK; 170551078Speter } else 170651078Speter timeout(siobusycheck, com, hz / 100); 170751078Speter splx(s); 170851078Speter} 170951078Speter 171051078Speterstatic void 171151078Spetersiodtrwakeup(chan) 171251078Speter void *chan; 171351078Speter{ 171451078Speter struct com_s *com; 171551078Speter 171651078Speter com = (struct com_s *)chan; 171751078Speter com->state &= ~CS_DTR_OFF; 171851078Speter wakeup(&com->dtr_wait); 171951078Speter} 172051078Speter 172165557Sjasone/* 172270174Sjhb * Call this function with the sio_lock mutex held. It will return with the 172370174Sjhb * lock still held. 172465557Sjasone */ 172551078Speterstatic void 172651078Spetersioinput(com) 172751078Speter struct com_s *com; 172851078Speter{ 172951078Speter u_char *buf; 173051078Speter int incc; 173151078Speter u_char line_status; 173251078Speter int recv_data; 173351078Speter struct tty *tp; 173451078Speter 173551078Speter buf = com->ibuf; 173651078Speter tp = com->tp; 173751078Speter if (!(tp->t_state & TS_ISOPEN) || !(tp->t_cflag & CREAD)) { 173851078Speter com_events -= (com->iptr - com->ibuf); 173951078Speter com->iptr = com->ibuf; 174051078Speter return; 174151078Speter } 174251078Speter if (tp->t_state & TS_CAN_BYPASS_L_RINT) { 174351078Speter /* 174451078Speter * Avoid the grotesquely inefficient lineswitch routine 174551078Speter * (ttyinput) in "raw" mode. It usually takes about 450 174651078Speter * instructions (that's without canonical processing or echo!). 174751078Speter * slinput is reasonably fast (usually 40 instructions plus 174851078Speter * call overhead). 174951078Speter */ 175051078Speter do { 175165557Sjasone /* 175265557Sjasone * This may look odd, but it is using save-and-enable 175365557Sjasone * semantics instead of the save-and-disable semantics 175465557Sjasone * that are used everywhere else. 175565557Sjasone */ 175672200Sbmilekic mtx_unlock_spin(&sio_lock); 175751078Speter incc = com->iptr - buf; 175851078Speter if (tp->t_rawq.c_cc + incc > tp->t_ihiwat 175951078Speter && (com->state & CS_RTS_IFLOW 176051078Speter || tp->t_iflag & IXOFF) 176151078Speter && !(tp->t_state & TS_TBLOCK)) 176251078Speter ttyblock(tp); 176351078Speter com->delta_error_counts[CE_TTY_BUF_OVERFLOW] 176451078Speter += b_to_q((char *)buf, incc, &tp->t_rawq); 176551078Speter buf += incc; 176651078Speter tk_nin += incc; 176751078Speter tk_rawcc += incc; 176851078Speter tp->t_rawcc += incc; 176951078Speter ttwakeup(tp); 177051078Speter if (tp->t_state & TS_TTSTOP 177151078Speter && (tp->t_iflag & IXANY 177251078Speter || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) { 177351078Speter tp->t_state &= ~TS_TTSTOP; 177451078Speter tp->t_lflag &= ~FLUSHO; 177551078Speter comstart(tp); 177651078Speter } 177772200Sbmilekic mtx_lock_spin(&sio_lock); 177851078Speter } while (buf < com->iptr); 177951078Speter } else { 178051078Speter do { 178165557Sjasone /* 178265557Sjasone * This may look odd, but it is using save-and-enable 178365557Sjasone * semantics instead of the save-and-disable semantics 178465557Sjasone * that are used everywhere else. 178565557Sjasone */ 178672200Sbmilekic mtx_unlock_spin(&sio_lock); 178751078Speter line_status = buf[com->ierroff]; 178851078Speter recv_data = *buf++; 178951078Speter if (line_status 179051078Speter & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) { 179151078Speter if (line_status & LSR_BI) 179251078Speter recv_data |= TTY_BI; 179351078Speter if (line_status & LSR_FE) 179451078Speter recv_data |= TTY_FE; 179551078Speter if (line_status & LSR_OE) 179651078Speter recv_data |= TTY_OE; 179751078Speter if (line_status & LSR_PE) 179851078Speter recv_data |= TTY_PE; 179951078Speter } 180051078Speter (*linesw[tp->t_line].l_rint)(recv_data, tp); 180172200Sbmilekic mtx_lock_spin(&sio_lock); 180251078Speter } while (buf < com->iptr); 180351078Speter } 180451078Speter com_events -= (com->iptr - com->ibuf); 180551078Speter com->iptr = com->ibuf; 180651078Speter 180751078Speter /* 180851078Speter * There is now room for another low-level buffer full of input, 180951078Speter * so enable RTS if it is now disabled and there is room in the 181051078Speter * high-level buffer. 181151078Speter */ 181251078Speter if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & MCR_RTS) && 181351078Speter !(tp->t_state & TS_TBLOCK)) 181451078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 181551078Speter} 181651078Speter 181751078Spetervoid 181851078Spetersiointr(arg) 181951078Speter void *arg; 182051078Speter{ 182170174Sjhb struct com_s *com; 182270174Sjhb 182351078Speter#ifndef COM_MULTIPORT 182470174Sjhb com = (struct com_s *)arg; 182570174Sjhb 182672200Sbmilekic mtx_lock_spin(&sio_lock); 182770174Sjhb siointr1(com); 182872200Sbmilekic mtx_unlock_spin(&sio_lock); 182951078Speter#else /* COM_MULTIPORT */ 183051078Speter bool_t possibly_more_intrs; 183151078Speter int unit; 183251078Speter 183351078Speter /* 183451078Speter * Loop until there is no activity on any port. This is necessary 183551078Speter * to get an interrupt edge more than to avoid another interrupt. 183651078Speter * If the IRQ signal is just an OR of the IRQ signals from several 183751078Speter * devices, then the edge from one may be lost because another is 183851078Speter * on. 183951078Speter */ 184072200Sbmilekic mtx_lock_spin(&sio_lock); 184151078Speter do { 184251078Speter possibly_more_intrs = FALSE; 184353344Speter for (unit = 0; unit < sio_numunits; ++unit) { 184451078Speter com = com_addr(unit); 184551078Speter /* 184651078Speter * XXX COM_LOCK(); 184751078Speter * would it work here, or be counter-productive? 184851078Speter */ 184951078Speter if (com != NULL 185051078Speter && !com->gone 185151078Speter && (inb(com->int_id_port) & IIR_IMASK) 185251078Speter != IIR_NOPEND) { 185351078Speter siointr1(com); 185451078Speter possibly_more_intrs = TRUE; 185551078Speter } 185651078Speter /* XXX COM_UNLOCK(); */ 185751078Speter } 185851078Speter } while (possibly_more_intrs); 185972200Sbmilekic mtx_unlock_spin(&sio_lock); 186051078Speter#endif /* COM_MULTIPORT */ 186151078Speter} 186251078Speter 186351078Speterstatic void 186451078Spetersiointr1(com) 186551078Speter struct com_s *com; 186651078Speter{ 186751078Speter u_char line_status; 186851078Speter u_char modem_status; 186951078Speter u_char *ioptr; 187051078Speter u_char recv_data; 187151078Speter u_char int_ctl; 187251078Speter u_char int_ctl_new; 187351078Speter struct timecounter *tc; 187451078Speter u_int count; 187551078Speter 187651078Speter int_ctl = inb(com->intr_ctl_port); 187751078Speter int_ctl_new = int_ctl; 187851078Speter 187951078Speter while (!com->gone) { 188051078Speter if (com->pps.ppsparam.mode & PPS_CAPTUREBOTH) { 188151078Speter modem_status = inb(com->modem_status_port); 188251078Speter if ((modem_status ^ com->last_modem_status) & MSR_DCD) { 188351078Speter tc = timecounter; 188451078Speter count = tc->tc_get_timecount(tc); 188551078Speter pps_event(&com->pps, tc, count, 188651078Speter (modem_status & MSR_DCD) ? 188751078Speter PPS_CAPTUREASSERT : PPS_CAPTURECLEAR); 188851078Speter } 188951078Speter } 189051078Speter line_status = inb(com->line_status_port); 189151078Speter 189251078Speter /* input event? (check first to help avoid overruns) */ 189351078Speter while (line_status & LSR_RCV_MASK) { 189451078Speter /* break/unnattached error bits or real input? */ 189551078Speter if (!(line_status & LSR_RXRDY)) 189651078Speter recv_data = 0; 189751078Speter else 189851078Speter recv_data = inb(com->data_port); 189961649Sps#if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER) 190061649Sps /* 190161649Sps * Solaris implements a new BREAK which is initiated 190261649Sps * by a character sequence CR ~ ^b which is similar 190361649Sps * to a familiar pattern used on Sun servers by the 190461649Sps * Remote Console. 190561649Sps */ 190661649Sps#define KEY_CRTLB 2 /* ^B */ 190761649Sps#define KEY_CR 13 /* CR '\r' */ 190861649Sps#define KEY_TILDE 126 /* ~ */ 190961649Sps 191061649Sps if (com->unit == comconsole) { 191161649Sps static int brk_state1 = 0, brk_state2 = 0; 191261649Sps if (recv_data == KEY_CR) { 191361649Sps brk_state1 = recv_data; 191461649Sps brk_state2 = 0; 191565557Sjasone } else if (brk_state1 == KEY_CR 191665557Sjasone && (recv_data == KEY_TILDE 191765557Sjasone || recv_data == KEY_CRTLB)) { 191861649Sps if (recv_data == KEY_TILDE) 191961649Sps brk_state2 = recv_data; 192065557Sjasone else if (brk_state2 == KEY_TILDE 192165557Sjasone && recv_data == KEY_CRTLB) { 192261649Sps breakpoint(); 192365557Sjasone brk_state1 = 0; 192465557Sjasone brk_state2 = 0; 192561649Sps goto cont; 192661649Sps } else 192761649Sps brk_state2 = 0; 192861649Sps } else 192961649Sps brk_state1 = 0; 193061649Sps } 193161649Sps#endif 193251078Speter if (line_status & (LSR_BI | LSR_FE | LSR_PE)) { 193351078Speter /* 193451078Speter * Don't store BI if IGNBRK or FE/PE if IGNPAR. 193551078Speter * Otherwise, push the work to a higher level 193651078Speter * (to handle PARMRK) if we're bypassing. 193751078Speter * Otherwise, convert BI/FE and PE+INPCK to 0. 193851078Speter * 193951078Speter * This makes bypassing work right in the 194051078Speter * usual "raw" case (IGNBRK set, and IGNPAR 194151078Speter * and INPCK clear). 194251078Speter * 194351078Speter * Note: BI together with FE/PE means just BI. 194451078Speter */ 194551078Speter if (line_status & LSR_BI) { 194651078Speter#if defined(DDB) && defined(BREAK_TO_DEBUGGER) 194751078Speter if (com->unit == comconsole) { 194851078Speter breakpoint(); 194951078Speter goto cont; 195051078Speter } 195151078Speter#endif 195251078Speter if (com->tp == NULL 195351078Speter || com->tp->t_iflag & IGNBRK) 195451078Speter goto cont; 195551078Speter } else { 195651078Speter if (com->tp == NULL 195751078Speter || com->tp->t_iflag & IGNPAR) 195851078Speter goto cont; 195951078Speter } 196051078Speter if (com->tp->t_state & TS_CAN_BYPASS_L_RINT 196151078Speter && (line_status & (LSR_BI | LSR_FE) 196251078Speter || com->tp->t_iflag & INPCK)) 196351078Speter recv_data = 0; 196451078Speter } 196551078Speter ++com->bytes_in; 196651078Speter if (com->hotchar != 0 && recv_data == com->hotchar) 196772238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 196851078Speter ioptr = com->iptr; 196951078Speter if (ioptr >= com->ibufend) 197051078Speter CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW); 197151078Speter else { 197251078Speter if (com->do_timestamp) 197351078Speter microtime(&com->timestamp); 197451078Speter ++com_events; 197572238Sjhb swi_sched(sio_slow_ih, SWI_DELAY); 197651078Speter#if 0 /* for testing input latency vs efficiency */ 197751078Speterif (com->iptr - com->ibuf == 8) 197872238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 197951078Speter#endif 198051078Speter ioptr[0] = recv_data; 198151078Speter ioptr[com->ierroff] = line_status; 198251078Speter com->iptr = ++ioptr; 198351078Speter if (ioptr == com->ihighwater 198451078Speter && com->state & CS_RTS_IFLOW) 198551078Speter outb(com->modem_ctl_port, 198651078Speter com->mcr_image &= ~MCR_RTS); 198751078Speter if (line_status & LSR_OE) 198851078Speter CE_RECORD(com, CE_OVERRUN); 198951078Speter } 199051078Spetercont: 199151078Speter /* 199251078Speter * "& 0x7F" is to avoid the gcc-1.40 generating a slow 199351078Speter * jump from the top of the loop to here 199451078Speter */ 199551078Speter line_status = inb(com->line_status_port) & 0x7F; 199651078Speter } 199751078Speter 199851078Speter /* modem status change? (always check before doing output) */ 199951078Speter modem_status = inb(com->modem_status_port); 200051078Speter if (modem_status != com->last_modem_status) { 200151078Speter if (com->do_dcd_timestamp 200251078Speter && !(com->last_modem_status & MSR_DCD) 200351078Speter && modem_status & MSR_DCD) 200451078Speter microtime(&com->dcd_timestamp); 200551078Speter 200651078Speter /* 200751078Speter * Schedule high level to handle DCD changes. Note 200851078Speter * that we don't use the delta bits anywhere. Some 200951078Speter * UARTs mess them up, and it's easy to remember the 201051078Speter * previous bits and calculate the delta. 201151078Speter */ 201251078Speter com->last_modem_status = modem_status; 201351078Speter if (!(com->state & CS_CHECKMSR)) { 201451078Speter com_events += LOTS_OF_EVENTS; 201551078Speter com->state |= CS_CHECKMSR; 201672238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 201751078Speter } 201851078Speter 201951078Speter /* handle CTS change immediately for crisp flow ctl */ 202051078Speter if (com->state & CS_CTS_OFLOW) { 202151078Speter if (modem_status & MSR_CTS) 202251078Speter com->state |= CS_ODEVREADY; 202351078Speter else 202451078Speter com->state &= ~CS_ODEVREADY; 202551078Speter } 202651078Speter } 202751078Speter 202851078Speter /* output queued and everything ready? */ 202951078Speter if (line_status & LSR_TXRDY 203051078Speter && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) { 203151078Speter ioptr = com->obufq.l_head; 203251078Speter if (com->tx_fifo_size > 1) { 203351078Speter u_int ocount; 203451078Speter 203551078Speter ocount = com->obufq.l_tail - ioptr; 203651078Speter if (ocount > com->tx_fifo_size) 203751078Speter ocount = com->tx_fifo_size; 203851078Speter com->bytes_out += ocount; 203951078Speter do 204051078Speter outb(com->data_port, *ioptr++); 204151078Speter while (--ocount != 0); 204251078Speter } else { 204351078Speter outb(com->data_port, *ioptr++); 204451078Speter ++com->bytes_out; 204551078Speter } 204651078Speter com->obufq.l_head = ioptr; 204751078Speter if (COM_IIR_TXRDYBUG(com->flags)) { 204851078Speter int_ctl_new = int_ctl | IER_ETXRDY; 204951078Speter } 205051078Speter if (ioptr >= com->obufq.l_tail) { 205151078Speter struct lbq *qp; 205251078Speter 205351078Speter qp = com->obufq.l_next; 205451078Speter qp->l_queued = FALSE; 205551078Speter qp = qp->l_next; 205651078Speter if (qp != NULL) { 205751078Speter com->obufq.l_head = qp->l_head; 205851078Speter com->obufq.l_tail = qp->l_tail; 205951078Speter com->obufq.l_next = qp; 206051078Speter } else { 206151078Speter /* output just completed */ 206253344Speter if (COM_IIR_TXRDYBUG(com->flags)) { 206351078Speter int_ctl_new = int_ctl & ~IER_ETXRDY; 206451078Speter } 206551078Speter com->state &= ~CS_BUSY; 206651078Speter } 206751078Speter if (!(com->state & CS_ODONE)) { 206851078Speter com_events += LOTS_OF_EVENTS; 206951078Speter com->state |= CS_ODONE; 207067551Sjhb /* handle at high level ASAP */ 207172238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 207251078Speter } 207351078Speter } 207453344Speter if (COM_IIR_TXRDYBUG(com->flags) && (int_ctl != int_ctl_new)) { 207551078Speter outb(com->intr_ctl_port, int_ctl_new); 207651078Speter } 207751078Speter } 207851078Speter 207951078Speter /* finished? */ 208051078Speter#ifndef COM_MULTIPORT 208151078Speter if ((inb(com->int_id_port) & IIR_IMASK) == IIR_NOPEND) 208251078Speter#endif /* COM_MULTIPORT */ 208351078Speter return; 208451078Speter } 208551078Speter} 208651078Speter 208751078Speterstatic int 208851078Spetersioioctl(dev, cmd, data, flag, p) 208951078Speter dev_t dev; 209051078Speter u_long cmd; 209151078Speter caddr_t data; 209251078Speter int flag; 209351078Speter struct proc *p; 209451078Speter{ 209551078Speter struct com_s *com; 209651078Speter int error; 209751078Speter int mynor; 209851078Speter int s; 209951078Speter struct tty *tp; 210051078Speter#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 210151078Speter u_long oldcmd; 210251078Speter struct termios term; 210351078Speter#endif 210451078Speter 210551078Speter mynor = minor(dev); 210651078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 210757915Simp if (com == NULL || com->gone) 210851078Speter return (ENODEV); 210951078Speter if (mynor & CONTROL_MASK) { 211051078Speter struct termios *ct; 211151078Speter 211251078Speter switch (mynor & CONTROL_MASK) { 211351078Speter case CONTROL_INIT_STATE: 211451078Speter ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in; 211551078Speter break; 211651078Speter case CONTROL_LOCK_STATE: 211751078Speter ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in; 211851078Speter break; 211951078Speter default: 212051078Speter return (ENODEV); /* /dev/nodev */ 212151078Speter } 212251078Speter switch (cmd) { 212351078Speter case TIOCSETA: 212451078Speter error = suser(p); 212551078Speter if (error != 0) 212651078Speter return (error); 212751078Speter *ct = *(struct termios *)data; 212851078Speter return (0); 212951078Speter case TIOCGETA: 213051078Speter *(struct termios *)data = *ct; 213151078Speter return (0); 213251078Speter case TIOCGETD: 213351078Speter *(int *)data = TTYDISC; 213451078Speter return (0); 213551078Speter case TIOCGWINSZ: 213651078Speter bzero(data, sizeof(struct winsize)); 213751078Speter return (0); 213851078Speter default: 213951078Speter return (ENOTTY); 214051078Speter } 214151078Speter } 214251078Speter tp = com->tp; 214351078Speter#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 214451078Speter term = tp->t_termios; 214551078Speter oldcmd = cmd; 214651078Speter error = ttsetcompat(tp, &cmd, data, &term); 214751078Speter if (error != 0) 214851078Speter return (error); 214951078Speter if (cmd != oldcmd) 215051078Speter data = (caddr_t)&term; 215151078Speter#endif 215251078Speter if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) { 215351078Speter int cc; 215451078Speter struct termios *dt = (struct termios *)data; 215551078Speter struct termios *lt = mynor & CALLOUT_MASK 215651078Speter ? &com->lt_out : &com->lt_in; 215751078Speter 215851078Speter dt->c_iflag = (tp->t_iflag & lt->c_iflag) 215951078Speter | (dt->c_iflag & ~lt->c_iflag); 216051078Speter dt->c_oflag = (tp->t_oflag & lt->c_oflag) 216151078Speter | (dt->c_oflag & ~lt->c_oflag); 216251078Speter dt->c_cflag = (tp->t_cflag & lt->c_cflag) 216351078Speter | (dt->c_cflag & ~lt->c_cflag); 216451078Speter dt->c_lflag = (tp->t_lflag & lt->c_lflag) 216551078Speter | (dt->c_lflag & ~lt->c_lflag); 216651078Speter for (cc = 0; cc < NCCS; ++cc) 216751078Speter if (lt->c_cc[cc] != 0) 216851078Speter dt->c_cc[cc] = tp->t_cc[cc]; 216951078Speter if (lt->c_ispeed != 0) 217051078Speter dt->c_ispeed = tp->t_ispeed; 217151078Speter if (lt->c_ospeed != 0) 217251078Speter dt->c_ospeed = tp->t_ospeed; 217351078Speter } 217451078Speter error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 217551078Speter if (error != ENOIOCTL) 217651078Speter return (error); 217751078Speter s = spltty(); 217851078Speter error = ttioctl(tp, cmd, data, flag); 217951078Speter disc_optim(tp, &tp->t_termios, com); 218051078Speter if (error != ENOIOCTL) { 218151078Speter splx(s); 218251078Speter return (error); 218351078Speter } 218451078Speter switch (cmd) { 218551078Speter case TIOCSBRK: 218660471Snyan sio_setreg(com, com_cfcr, com->cfcr_image |= CFCR_SBREAK); 218751078Speter break; 218851078Speter case TIOCCBRK: 218960471Snyan sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 219051078Speter break; 219151078Speter case TIOCSDTR: 219251078Speter (void)commctl(com, TIOCM_DTR, DMBIS); 219351078Speter break; 219451078Speter case TIOCCDTR: 219551078Speter (void)commctl(com, TIOCM_DTR, DMBIC); 219651078Speter break; 219751078Speter /* 219851078Speter * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The 219951078Speter * changes get undone on the next call to comparam(). 220051078Speter */ 220151078Speter case TIOCMSET: 220251078Speter (void)commctl(com, *(int *)data, DMSET); 220351078Speter break; 220451078Speter case TIOCMBIS: 220551078Speter (void)commctl(com, *(int *)data, DMBIS); 220651078Speter break; 220751078Speter case TIOCMBIC: 220851078Speter (void)commctl(com, *(int *)data, DMBIC); 220951078Speter break; 221051078Speter case TIOCMGET: 221151078Speter *(int *)data = commctl(com, 0, DMGET); 221251078Speter break; 221351078Speter case TIOCMSDTRWAIT: 221451078Speter /* must be root since the wait applies to following logins */ 221551078Speter error = suser(p); 221651078Speter if (error != 0) { 221751078Speter splx(s); 221851078Speter return (error); 221951078Speter } 222051078Speter com->dtr_wait = *(int *)data * hz / 100; 222151078Speter break; 222251078Speter case TIOCMGDTRWAIT: 222351078Speter *(int *)data = com->dtr_wait * 100 / hz; 222451078Speter break; 222551078Speter case TIOCTIMESTAMP: 222651078Speter com->do_timestamp = TRUE; 222751078Speter *(struct timeval *)data = com->timestamp; 222851078Speter break; 222951078Speter case TIOCDCDTIMESTAMP: 223051078Speter com->do_dcd_timestamp = TRUE; 223151078Speter *(struct timeval *)data = com->dcd_timestamp; 223251078Speter break; 223351078Speter default: 223451078Speter splx(s); 223551078Speter error = pps_ioctl(cmd, data, &com->pps); 223651078Speter if (error == ENODEV) 223751078Speter error = ENOTTY; 223851078Speter return (error); 223951078Speter } 224051078Speter splx(s); 224151078Speter return (0); 224251078Speter} 224351078Speter 224465557Sjasone/* software interrupt handler for SWI_TTY */ 224551078Speterstatic void 224667551Sjhbsiopoll(void *dummy) 224751078Speter{ 224851078Speter int unit; 224951078Speter 225051078Speter if (com_events == 0) 225151078Speter return; 225251078Speterrepeat: 225353344Speter for (unit = 0; unit < sio_numunits; ++unit) { 225451078Speter struct com_s *com; 225551078Speter int incc; 225651078Speter struct tty *tp; 225751078Speter 225851078Speter com = com_addr(unit); 225951078Speter if (com == NULL) 226051078Speter continue; 226151078Speter tp = com->tp; 226251078Speter if (tp == NULL || com->gone) { 226351078Speter /* 226451078Speter * Discard any events related to never-opened or 226551078Speter * going-away devices. 226651078Speter */ 226772200Sbmilekic mtx_lock_spin(&sio_lock); 226851078Speter incc = com->iptr - com->ibuf; 226951078Speter com->iptr = com->ibuf; 227051078Speter if (com->state & CS_CHECKMSR) { 227151078Speter incc += LOTS_OF_EVENTS; 227251078Speter com->state &= ~CS_CHECKMSR; 227351078Speter } 227451078Speter com_events -= incc; 227572200Sbmilekic mtx_unlock_spin(&sio_lock); 227651078Speter continue; 227751078Speter } 227851078Speter if (com->iptr != com->ibuf) { 227972200Sbmilekic mtx_lock_spin(&sio_lock); 228051078Speter sioinput(com); 228172200Sbmilekic mtx_unlock_spin(&sio_lock); 228251078Speter } 228351078Speter if (com->state & CS_CHECKMSR) { 228451078Speter u_char delta_modem_status; 228551078Speter 228672200Sbmilekic mtx_lock_spin(&sio_lock); 228751078Speter delta_modem_status = com->last_modem_status 228851078Speter ^ com->prev_modem_status; 228951078Speter com->prev_modem_status = com->last_modem_status; 229051078Speter com_events -= LOTS_OF_EVENTS; 229151078Speter com->state &= ~CS_CHECKMSR; 229272200Sbmilekic mtx_unlock_spin(&sio_lock); 229351078Speter if (delta_modem_status & MSR_DCD) 229451078Speter (*linesw[tp->t_line].l_modem) 229551078Speter (tp, com->prev_modem_status & MSR_DCD); 229651078Speter } 229751078Speter if (com->state & CS_ODONE) { 229872200Sbmilekic mtx_lock_spin(&sio_lock); 229951078Speter com_events -= LOTS_OF_EVENTS; 230051078Speter com->state &= ~CS_ODONE; 230172200Sbmilekic mtx_unlock_spin(&sio_lock); 230251078Speter if (!(com->state & CS_BUSY) 230351078Speter && !(com->extra_state & CSE_BUSYCHECK)) { 230451078Speter timeout(siobusycheck, com, hz / 100); 230551078Speter com->extra_state |= CSE_BUSYCHECK; 230651078Speter } 230751078Speter (*linesw[tp->t_line].l_start)(tp); 230851078Speter } 230951078Speter if (com_events == 0) 231051078Speter break; 231151078Speter } 231251078Speter if (com_events >= LOTS_OF_EVENTS) 231351078Speter goto repeat; 231451078Speter} 231551078Speter 231651078Speterstatic int 231751078Spetercomparam(tp, t) 231851078Speter struct tty *tp; 231951078Speter struct termios *t; 232051078Speter{ 232151078Speter u_int cfcr; 232251078Speter int cflag; 232351078Speter struct com_s *com; 232451078Speter int divisor; 232551078Speter u_char dlbh; 232651078Speter u_char dlbl; 232751078Speter int s; 232851078Speter int unit; 232951078Speter 233051078Speter /* do historical conversions */ 233151078Speter if (t->c_ispeed == 0) 233251078Speter t->c_ispeed = t->c_ospeed; 233351078Speter 233451078Speter /* check requested parameters */ 233551078Speter divisor = ttspeedtab(t->c_ospeed, comspeedtab); 233651078Speter if (divisor < 0 || (divisor > 0 && t->c_ispeed != t->c_ospeed)) 233751078Speter return (EINVAL); 233851078Speter 233951078Speter /* parameters are OK, convert them to the com struct and the device */ 234051078Speter unit = DEV_TO_UNIT(tp->t_dev); 234151078Speter com = com_addr(unit); 234257915Simp if (com == NULL) 234357915Simp return (ENODEV); 234451078Speter s = spltty(); 234551078Speter if (divisor == 0) 234651078Speter (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */ 234751078Speter else 234851078Speter (void)commctl(com, TIOCM_DTR, DMBIS); 234951078Speter cflag = t->c_cflag; 235051078Speter switch (cflag & CSIZE) { 235151078Speter case CS5: 235251078Speter cfcr = CFCR_5BITS; 235351078Speter break; 235451078Speter case CS6: 235551078Speter cfcr = CFCR_6BITS; 235651078Speter break; 235751078Speter case CS7: 235851078Speter cfcr = CFCR_7BITS; 235951078Speter break; 236051078Speter default: 236151078Speter cfcr = CFCR_8BITS; 236251078Speter break; 236351078Speter } 236451078Speter if (cflag & PARENB) { 236551078Speter cfcr |= CFCR_PENAB; 236651078Speter if (!(cflag & PARODD)) 236751078Speter cfcr |= CFCR_PEVEN; 236851078Speter } 236951078Speter if (cflag & CSTOPB) 237051078Speter cfcr |= CFCR_STOPB; 237151078Speter 237251078Speter if (com->hasfifo && divisor != 0) { 237351078Speter /* 237451078Speter * Use a fifo trigger level low enough so that the input 237551078Speter * latency from the fifo is less than about 16 msec and 237651078Speter * the total latency is less than about 30 msec. These 237751078Speter * latencies are reasonable for humans. Serial comms 237851078Speter * protocols shouldn't expect anything better since modem 237951078Speter * latencies are larger. 238051078Speter */ 238151078Speter com->fifo_image = t->c_ospeed <= 4800 238251078Speter ? FIFO_ENABLE : FIFO_ENABLE | FIFO_RX_HIGH; 238351078Speter#ifdef COM_ESP 238451078Speter /* 238551078Speter * The Hayes ESP card needs the fifo DMA mode bit set 238651078Speter * in compatibility mode. If not, it will interrupt 238751078Speter * for each character received. 238851078Speter */ 238951078Speter if (com->esp) 239051078Speter com->fifo_image |= FIFO_DMA_MODE; 239151078Speter#endif 239260471Snyan sio_setreg(com, com_fifo, com->fifo_image); 239351078Speter } 239451078Speter 239565605Sjhb /* 239665605Sjhb * This returns with interrupts disabled so that we can complete 239765605Sjhb * the speed change atomically. Keeping interrupts disabled is 239865605Sjhb * especially important while com_data is hidden. 239965605Sjhb */ 240065605Sjhb (void) siosetwater(com, t->c_ispeed); 240165557Sjasone 240251078Speter if (divisor != 0) { 240360471Snyan sio_setreg(com, com_cfcr, cfcr | CFCR_DLAB); 240451078Speter /* 240551078Speter * Only set the divisor registers if they would change, 240651078Speter * since on some 16550 incompatibles (UMC8669F), setting 240751078Speter * them while input is arriving them loses sync until 240851078Speter * data stops arriving. 240951078Speter */ 241051078Speter dlbl = divisor & 0xFF; 241160471Snyan if (sio_getreg(com, com_dlbl) != dlbl) 241260471Snyan sio_setreg(com, com_dlbl, dlbl); 241351078Speter dlbh = (u_int) divisor >> 8; 241460471Snyan if (sio_getreg(com, com_dlbh) != dlbh) 241560471Snyan sio_setreg(com, com_dlbh, dlbh); 241651078Speter } 241751078Speter 241860471Snyan sio_setreg(com, com_cfcr, com->cfcr_image = cfcr); 241951078Speter 242051078Speter if (!(tp->t_state & TS_TTSTOP)) 242151078Speter com->state |= CS_TTGO; 242251078Speter 242351078Speter if (cflag & CRTS_IFLOW) { 242451078Speter if (com->st16650a) { 242560471Snyan sio_setreg(com, com_cfcr, 0xbf); 242660471Snyan sio_setreg(com, com_fifo, 242760471Snyan sio_getreg(com, com_fifo) | 0x40); 242851078Speter } 242951078Speter com->state |= CS_RTS_IFLOW; 243051078Speter /* 243151078Speter * If CS_RTS_IFLOW just changed from off to on, the change 243251078Speter * needs to be propagated to MCR_RTS. This isn't urgent, 243351078Speter * so do it later by calling comstart() instead of repeating 243451078Speter * a lot of code from comstart() here. 243551078Speter */ 243651078Speter } else if (com->state & CS_RTS_IFLOW) { 243751078Speter com->state &= ~CS_RTS_IFLOW; 243851078Speter /* 243951078Speter * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS 244051078Speter * on here, since comstart() won't do it later. 244151078Speter */ 244251078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 244351078Speter if (com->st16650a) { 244460471Snyan sio_setreg(com, com_cfcr, 0xbf); 244560471Snyan sio_setreg(com, com_fifo, 244660471Snyan sio_getreg(com, com_fifo) & ~0x40); 244751078Speter } 244851078Speter } 244951078Speter 245051078Speter 245151078Speter /* 245251078Speter * Set up state to handle output flow control. 245351078Speter * XXX - worth handling MDMBUF (DCD) flow control at the lowest level? 245451078Speter * Now has 10+ msec latency, while CTS flow has 50- usec latency. 245551078Speter */ 245651078Speter com->state |= CS_ODEVREADY; 245751078Speter com->state &= ~CS_CTS_OFLOW; 245851078Speter if (cflag & CCTS_OFLOW) { 245951078Speter com->state |= CS_CTS_OFLOW; 246051078Speter if (!(com->last_modem_status & MSR_CTS)) 246151078Speter com->state &= ~CS_ODEVREADY; 246251078Speter if (com->st16650a) { 246360471Snyan sio_setreg(com, com_cfcr, 0xbf); 246460471Snyan sio_setreg(com, com_fifo, 246560471Snyan sio_getreg(com, com_fifo) | 0x80); 246651078Speter } 246751078Speter } else { 246851078Speter if (com->st16650a) { 246960471Snyan sio_setreg(com, com_cfcr, 0xbf); 247060471Snyan sio_setreg(com, com_fifo, 247160471Snyan sio_getreg(com, com_fifo) & ~0x80); 247251078Speter } 247351078Speter } 247451078Speter 247560471Snyan sio_setreg(com, com_cfcr, com->cfcr_image); 247651078Speter 247751078Speter /* XXX shouldn't call functions while intrs are disabled. */ 247851078Speter disc_optim(tp, t, com); 247951078Speter /* 248051078Speter * Recover from fiddling with CS_TTGO. We used to call siointr1() 248151078Speter * unconditionally, but that defeated the careful discarding of 248251078Speter * stale input in sioopen(). 248351078Speter */ 248451078Speter if (com->state >= (CS_BUSY | CS_TTGO)) 248551078Speter siointr1(com); 248651078Speter 248772200Sbmilekic mtx_unlock_spin(&sio_lock); 248851078Speter splx(s); 248951078Speter comstart(tp); 249051078Speter if (com->ibufold != NULL) { 249151078Speter free(com->ibufold, M_DEVBUF); 249251078Speter com->ibufold = NULL; 249351078Speter } 249451078Speter return (0); 249551078Speter} 249651078Speter 249765605Sjhb/* 249870174Sjhb * This function must be called with the sio_lock mutex released and will 249970174Sjhb * return with it obtained. 250065605Sjhb */ 250151078Speterstatic int 250265605Sjhbsiosetwater(com, speed) 250351078Speter struct com_s *com; 250451078Speter speed_t speed; 250551078Speter{ 250651078Speter int cp4ticks; 250751078Speter u_char *ibuf; 250851078Speter int ibufsize; 250951078Speter struct tty *tp; 251051078Speter 251151078Speter /* 251251078Speter * Make the buffer size large enough to handle a softtty interrupt 251351078Speter * latency of about 2 ticks without loss of throughput or data 251451078Speter * (about 3 ticks if input flow control is not used or not honoured, 251551078Speter * but a bit less for CS5-CS7 modes). 251651078Speter */ 251751078Speter cp4ticks = speed / 10 / hz * 4; 251851078Speter for (ibufsize = 128; ibufsize < cp4ticks;) 251951078Speter ibufsize <<= 1; 252065605Sjhb if (ibufsize == com->ibufsize) { 252172200Sbmilekic mtx_lock_spin(&sio_lock); 252251078Speter return (0); 252365605Sjhb } 252451078Speter 252551078Speter /* 252651078Speter * Allocate input buffer. The extra factor of 2 in the size is 252751078Speter * to allow for an error byte for each input byte. 252851078Speter */ 252951078Speter ibuf = malloc(2 * ibufsize, M_DEVBUF, M_NOWAIT); 253065605Sjhb if (ibuf == NULL) { 253172200Sbmilekic mtx_lock_spin(&sio_lock); 253251078Speter return (ENOMEM); 253365605Sjhb } 253451078Speter 253551078Speter /* Initialize non-critical variables. */ 253651078Speter com->ibufold = com->ibuf; 253751078Speter com->ibufsize = ibufsize; 253851078Speter tp = com->tp; 253951078Speter if (tp != NULL) { 254051078Speter tp->t_ififosize = 2 * ibufsize; 254151078Speter tp->t_ispeedwat = (speed_t)-1; 254251078Speter tp->t_ospeedwat = (speed_t)-1; 254351078Speter } 254451078Speter 254551078Speter /* 254651078Speter * Read current input buffer, if any. Continue with interrupts 254751078Speter * disabled. 254851078Speter */ 254972200Sbmilekic mtx_lock_spin(&sio_lock); 255051078Speter if (com->iptr != com->ibuf) 255151078Speter sioinput(com); 255251078Speter 255351078Speter /*- 255451078Speter * Initialize critical variables, including input buffer watermarks. 255551078Speter * The external device is asked to stop sending when the buffer 255651078Speter * exactly reaches high water, or when the high level requests it. 255751078Speter * The high level is notified immediately (rather than at a later 255851078Speter * clock tick) when this watermark is reached. 255951078Speter * The buffer size is chosen so the watermark should almost never 256051078Speter * be reached. 256151078Speter * The low watermark is invisibly 0 since the buffer is always 256251078Speter * emptied all at once. 256351078Speter */ 256451078Speter com->iptr = com->ibuf = ibuf; 256551078Speter com->ibufend = ibuf + ibufsize; 256651078Speter com->ierroff = ibufsize; 256751078Speter com->ihighwater = ibuf + 3 * ibufsize / 4; 256851078Speter return (0); 256951078Speter} 257051078Speter 257151078Speterstatic void 257251078Spetercomstart(tp) 257351078Speter struct tty *tp; 257451078Speter{ 257551078Speter struct com_s *com; 257651078Speter int s; 257751078Speter int unit; 257851078Speter 257951078Speter unit = DEV_TO_UNIT(tp->t_dev); 258051078Speter com = com_addr(unit); 258157915Simp if (com == NULL) 258257915Simp return; 258351078Speter s = spltty(); 258472200Sbmilekic mtx_lock_spin(&sio_lock); 258551078Speter if (tp->t_state & TS_TTSTOP) 258651078Speter com->state &= ~CS_TTGO; 258751078Speter else 258851078Speter com->state |= CS_TTGO; 258951078Speter if (tp->t_state & TS_TBLOCK) { 259051078Speter if (com->mcr_image & MCR_RTS && com->state & CS_RTS_IFLOW) 259151078Speter outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS); 259251078Speter } else { 259351078Speter if (!(com->mcr_image & MCR_RTS) && com->iptr < com->ihighwater 259451078Speter && com->state & CS_RTS_IFLOW) 259551078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 259651078Speter } 259772200Sbmilekic mtx_unlock_spin(&sio_lock); 259851078Speter if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) { 259951078Speter ttwwakeup(tp); 260051078Speter splx(s); 260151078Speter return; 260251078Speter } 260351078Speter if (tp->t_outq.c_cc != 0) { 260451078Speter struct lbq *qp; 260551078Speter struct lbq *next; 260651078Speter 260751078Speter if (!com->obufs[0].l_queued) { 260851078Speter com->obufs[0].l_tail 260951078Speter = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1, 261051078Speter sizeof com->obuf1); 261151078Speter com->obufs[0].l_next = NULL; 261251078Speter com->obufs[0].l_queued = TRUE; 261372200Sbmilekic mtx_lock_spin(&sio_lock); 261451078Speter if (com->state & CS_BUSY) { 261551078Speter qp = com->obufq.l_next; 261651078Speter while ((next = qp->l_next) != NULL) 261751078Speter qp = next; 261851078Speter qp->l_next = &com->obufs[0]; 261951078Speter } else { 262051078Speter com->obufq.l_head = com->obufs[0].l_head; 262151078Speter com->obufq.l_tail = com->obufs[0].l_tail; 262251078Speter com->obufq.l_next = &com->obufs[0]; 262351078Speter com->state |= CS_BUSY; 262451078Speter } 262572200Sbmilekic mtx_unlock_spin(&sio_lock); 262651078Speter } 262751078Speter if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) { 262851078Speter com->obufs[1].l_tail 262951078Speter = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2, 263051078Speter sizeof com->obuf2); 263151078Speter com->obufs[1].l_next = NULL; 263251078Speter com->obufs[1].l_queued = TRUE; 263372200Sbmilekic mtx_lock_spin(&sio_lock); 263451078Speter if (com->state & CS_BUSY) { 263551078Speter qp = com->obufq.l_next; 263651078Speter while ((next = qp->l_next) != NULL) 263751078Speter qp = next; 263851078Speter qp->l_next = &com->obufs[1]; 263951078Speter } else { 264051078Speter com->obufq.l_head = com->obufs[1].l_head; 264151078Speter com->obufq.l_tail = com->obufs[1].l_tail; 264251078Speter com->obufq.l_next = &com->obufs[1]; 264351078Speter com->state |= CS_BUSY; 264451078Speter } 264572200Sbmilekic mtx_unlock_spin(&sio_lock); 264651078Speter } 264751078Speter tp->t_state |= TS_BUSY; 264851078Speter } 264972200Sbmilekic mtx_lock_spin(&sio_lock); 265051078Speter if (com->state >= (CS_BUSY | CS_TTGO)) 265151078Speter siointr1(com); /* fake interrupt to start output */ 265272200Sbmilekic mtx_unlock_spin(&sio_lock); 265351078Speter ttwwakeup(tp); 265451078Speter splx(s); 265551078Speter} 265651078Speter 265751078Speterstatic void 265851654Sphkcomstop(tp, rw) 265951078Speter struct tty *tp; 266051078Speter int rw; 266151078Speter{ 266251078Speter struct com_s *com; 266351078Speter 266451078Speter com = com_addr(DEV_TO_UNIT(tp->t_dev)); 266557915Simp if (com == NULL || com->gone) 266651078Speter return; 266772200Sbmilekic mtx_lock_spin(&sio_lock); 266851078Speter if (rw & FWRITE) { 266951078Speter if (com->hasfifo) 267051078Speter#ifdef COM_ESP 267151078Speter /* XXX avoid h/w bug. */ 267251078Speter if (!com->esp) 267351078Speter#endif 267460471Snyan sio_setreg(com, com_fifo, 267560471Snyan FIFO_XMT_RST | com->fifo_image); 267651078Speter com->obufs[0].l_queued = FALSE; 267751078Speter com->obufs[1].l_queued = FALSE; 267851078Speter if (com->state & CS_ODONE) 267951078Speter com_events -= LOTS_OF_EVENTS; 268051078Speter com->state &= ~(CS_ODONE | CS_BUSY); 268151078Speter com->tp->t_state &= ~TS_BUSY; 268251078Speter } 268351078Speter if (rw & FREAD) { 268451078Speter if (com->hasfifo) 268551078Speter#ifdef COM_ESP 268651078Speter /* XXX avoid h/w bug. */ 268751078Speter if (!com->esp) 268851078Speter#endif 268960471Snyan sio_setreg(com, com_fifo, 269060471Snyan FIFO_RCV_RST | com->fifo_image); 269151078Speter com_events -= (com->iptr - com->ibuf); 269251078Speter com->iptr = com->ibuf; 269351078Speter } 269472200Sbmilekic mtx_unlock_spin(&sio_lock); 269551078Speter comstart(tp); 269651078Speter} 269751078Speter 269851078Speterstatic int 269951078Spetercommctl(com, bits, how) 270051078Speter struct com_s *com; 270151078Speter int bits; 270251078Speter int how; 270351078Speter{ 270451078Speter int mcr; 270551078Speter int msr; 270651078Speter 270751078Speter if (how == DMGET) { 270851078Speter bits = TIOCM_LE; /* XXX - always enabled while open */ 270951078Speter mcr = com->mcr_image; 271051078Speter if (mcr & MCR_DTR) 271151078Speter bits |= TIOCM_DTR; 271251078Speter if (mcr & MCR_RTS) 271351078Speter bits |= TIOCM_RTS; 271451078Speter msr = com->prev_modem_status; 271551078Speter if (msr & MSR_CTS) 271651078Speter bits |= TIOCM_CTS; 271751078Speter if (msr & MSR_DCD) 271851078Speter bits |= TIOCM_CD; 271951078Speter if (msr & MSR_DSR) 272051078Speter bits |= TIOCM_DSR; 272151078Speter /* 272251078Speter * XXX - MSR_RI is naturally volatile, and we make MSR_TERI 272351078Speter * more volatile by reading the modem status a lot. Perhaps 272451078Speter * we should latch both bits until the status is read here. 272551078Speter */ 272651078Speter if (msr & (MSR_RI | MSR_TERI)) 272751078Speter bits |= TIOCM_RI; 272851078Speter return (bits); 272951078Speter } 273051078Speter mcr = 0; 273151078Speter if (bits & TIOCM_DTR) 273251078Speter mcr |= MCR_DTR; 273351078Speter if (bits & TIOCM_RTS) 273451078Speter mcr |= MCR_RTS; 273551078Speter if (com->gone) 273651078Speter return(0); 273772200Sbmilekic mtx_lock_spin(&sio_lock); 273851078Speter switch (how) { 273951078Speter case DMSET: 274051078Speter outb(com->modem_ctl_port, 274151078Speter com->mcr_image = mcr | (com->mcr_image & MCR_IENABLE)); 274251078Speter break; 274351078Speter case DMBIS: 274451078Speter outb(com->modem_ctl_port, com->mcr_image |= mcr); 274551078Speter break; 274651078Speter case DMBIC: 274751078Speter outb(com->modem_ctl_port, com->mcr_image &= ~mcr); 274851078Speter break; 274951078Speter } 275072200Sbmilekic mtx_unlock_spin(&sio_lock); 275151078Speter return (0); 275251078Speter} 275351078Speter 275451078Speterstatic void 275551078Spetersiosettimeout() 275651078Speter{ 275751078Speter struct com_s *com; 275851078Speter bool_t someopen; 275951078Speter int unit; 276051078Speter 276151078Speter /* 276251078Speter * Set our timeout period to 1 second if no polled devices are open. 276351078Speter * Otherwise set it to max(1/200, 1/hz). 276451078Speter * Enable timeouts iff some device is open. 276551078Speter */ 276651078Speter untimeout(comwakeup, (void *)NULL, sio_timeout_handle); 276751078Speter sio_timeout = hz; 276851078Speter someopen = FALSE; 276953344Speter for (unit = 0; unit < sio_numunits; ++unit) { 277051078Speter com = com_addr(unit); 277151078Speter if (com != NULL && com->tp != NULL 277251078Speter && com->tp->t_state & TS_ISOPEN && !com->gone) { 277351078Speter someopen = TRUE; 277451078Speter if (com->poll || com->poll_output) { 277551078Speter sio_timeout = hz > 200 ? hz / 200 : 1; 277651078Speter break; 277751078Speter } 277851078Speter } 277951078Speter } 278051078Speter if (someopen) { 278151078Speter sio_timeouts_until_log = hz / sio_timeout; 278251078Speter sio_timeout_handle = timeout(comwakeup, (void *)NULL, 278351078Speter sio_timeout); 278451078Speter } else { 278551078Speter /* Flush error messages, if any. */ 278651078Speter sio_timeouts_until_log = 1; 278751078Speter comwakeup((void *)NULL); 278851078Speter untimeout(comwakeup, (void *)NULL, sio_timeout_handle); 278951078Speter } 279051078Speter} 279151078Speter 279251078Speterstatic void 279351078Spetercomwakeup(chan) 279451078Speter void *chan; 279551078Speter{ 279651078Speter struct com_s *com; 279751078Speter int unit; 279851078Speter 279951078Speter sio_timeout_handle = timeout(comwakeup, (void *)NULL, sio_timeout); 280051078Speter 280151078Speter /* 280251078Speter * Recover from lost output interrupts. 280351078Speter * Poll any lines that don't use interrupts. 280451078Speter */ 280553344Speter for (unit = 0; unit < sio_numunits; ++unit) { 280651078Speter com = com_addr(unit); 280751078Speter if (com != NULL && !com->gone 280851078Speter && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) { 280972200Sbmilekic mtx_lock_spin(&sio_lock); 281051078Speter siointr1(com); 281172200Sbmilekic mtx_unlock_spin(&sio_lock); 281251078Speter } 281351078Speter } 281451078Speter 281551078Speter /* 281651078Speter * Check for and log errors, but not too often. 281751078Speter */ 281851078Speter if (--sio_timeouts_until_log > 0) 281951078Speter return; 282051078Speter sio_timeouts_until_log = hz / sio_timeout; 282153344Speter for (unit = 0; unit < sio_numunits; ++unit) { 282251078Speter int errnum; 282351078Speter 282451078Speter com = com_addr(unit); 282551078Speter if (com == NULL) 282651078Speter continue; 282751078Speter if (com->gone) 282851078Speter continue; 282951078Speter for (errnum = 0; errnum < CE_NTYPES; ++errnum) { 283051078Speter u_int delta; 283151078Speter u_long total; 283251078Speter 283372200Sbmilekic mtx_lock_spin(&sio_lock); 283451078Speter delta = com->delta_error_counts[errnum]; 283551078Speter com->delta_error_counts[errnum] = 0; 283672200Sbmilekic mtx_unlock_spin(&sio_lock); 283751078Speter if (delta == 0) 283851078Speter continue; 283951078Speter total = com->error_counts[errnum] += delta; 284051078Speter log(LOG_ERR, "sio%d: %u more %s%s (total %lu)\n", 284151078Speter unit, delta, error_desc[errnum], 284251078Speter delta == 1 ? "" : "s", total); 284351078Speter } 284451078Speter } 284551078Speter} 284651078Speter 284751078Speterstatic void 284851078Speterdisc_optim(tp, t, com) 284951078Speter struct tty *tp; 285051078Speter struct termios *t; 285151078Speter struct com_s *com; 285251078Speter{ 285351078Speter if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) 285451078Speter && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) 285551078Speter && (!(t->c_iflag & PARMRK) 285651078Speter || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) 285751078Speter && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) 285851078Speter && linesw[tp->t_line].l_rint == ttyinput) 285951078Speter tp->t_state |= TS_CAN_BYPASS_L_RINT; 286051078Speter else 286151078Speter tp->t_state &= ~TS_CAN_BYPASS_L_RINT; 286251078Speter com->hotchar = linesw[tp->t_line].l_hotchar; 286351078Speter} 286451078Speter 286551078Speter/* 286651078Speter * Following are all routines needed for SIO to act as console 286751078Speter */ 286851078Speter#include <sys/cons.h> 286951078Speter 287051078Speterstruct siocnstate { 287151078Speter u_char dlbl; 287251078Speter u_char dlbh; 287351078Speter u_char ier; 287451078Speter u_char cfcr; 287551078Speter u_char mcr; 287651078Speter}; 287751078Speter 287866230Sjhb#ifndef __alpha__ 287951078Speterstatic speed_t siocngetspeed __P((Port_t, struct speedtab *)); 288066230Sjhb#endif 288151078Speterstatic void siocnclose __P((struct siocnstate *sp, Port_t iobase)); 288251078Speterstatic void siocnopen __P((struct siocnstate *sp, Port_t iobase, int speed)); 288351078Speterstatic void siocntxwait __P((Port_t iobase)); 288451078Speter 288566230Sjhb#ifdef __alpha__ 288666230Sjhbint siocnattach __P((int port, int speed)); 288766230Sjhbint siogdbattach __P((int port, int speed)); 288866230Sjhbint siogdbgetc __P((void)); 288966230Sjhbvoid siogdbputc __P((int c)); 289066230Sjhb#else 289151078Speterstatic cn_probe_t siocnprobe; 289251078Speterstatic cn_init_t siocninit; 289366230Sjhb#endif 289451078Speterstatic cn_checkc_t siocncheckc; 289551078Speterstatic cn_getc_t siocngetc; 289651078Speterstatic cn_putc_t siocnputc; 289751078Speter 289851078Speter#ifdef __i386__ 289955823SyokotaCONS_DRIVER(sio, siocnprobe, siocninit, NULL, siocngetc, siocncheckc, 290055823Syokota siocnputc, NULL); 290151078Speter#endif 290251078Speter 290351078Speter/* To get the GDB related variables */ 290451078Speter#if DDB > 0 290551078Speter#include <ddb/ddb.h> 290651078Speter#endif 290751078Speter 290851078Speterstatic void 290951078Spetersiocntxwait(iobase) 291051078Speter Port_t iobase; 291151078Speter{ 291251078Speter int timo; 291351078Speter 291451078Speter /* 291551078Speter * Wait for any pending transmission to finish. Required to avoid 291651078Speter * the UART lockup bug when the speed is changed, and for normal 291751078Speter * transmits. 291851078Speter */ 291951078Speter timo = 100000; 292051078Speter while ((inb(iobase + com_lsr) & (LSR_TSRE | LSR_TXRDY)) 292151078Speter != (LSR_TSRE | LSR_TXRDY) && --timo != 0) 292251078Speter ; 292351078Speter} 292451078Speter 292566230Sjhb#ifndef __alpha__ 292666230Sjhb 292751078Speter/* 292851078Speter * Read the serial port specified and try to figure out what speed 292951078Speter * it's currently running at. We're assuming the serial port has 293051078Speter * been initialized and is basicly idle. This routine is only intended 293151078Speter * to be run at system startup. 293251078Speter * 293351078Speter * If the value read from the serial port doesn't make sense, return 0. 293451078Speter */ 293551078Speter 293651078Speterstatic speed_t 293751078Spetersiocngetspeed(iobase, table) 293851078Speter Port_t iobase; 293951078Speter struct speedtab *table; 294051078Speter{ 294151078Speter int code; 294251078Speter u_char dlbh; 294351078Speter u_char dlbl; 294451078Speter u_char cfcr; 294551078Speter 294651078Speter cfcr = inb(iobase + com_cfcr); 294751078Speter outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 294851078Speter 294951078Speter dlbl = inb(iobase + com_dlbl); 295051078Speter dlbh = inb(iobase + com_dlbh); 295151078Speter 295251078Speter outb(iobase + com_cfcr, cfcr); 295351078Speter 295451078Speter code = dlbh << 8 | dlbl; 295551078Speter 295653344Speter for (; table->sp_speed != -1; table++) 295751078Speter if (table->sp_code == code) 295851078Speter return (table->sp_speed); 295951078Speter 296058885Simp return (0); /* didn't match anything sane */ 296151078Speter} 296251078Speter 296366230Sjhb#endif 296466230Sjhb 296551078Speterstatic void 296651078Spetersiocnopen(sp, iobase, speed) 296751078Speter struct siocnstate *sp; 296851078Speter Port_t iobase; 296951078Speter int speed; 297051078Speter{ 297151078Speter int divisor; 297251078Speter u_char dlbh; 297351078Speter u_char dlbl; 297451078Speter 297551078Speter /* 297651078Speter * Save all the device control registers except the fifo register 297751078Speter * and set our default ones (cs8 -parenb speed=comdefaultrate). 297851078Speter * We can't save the fifo register since it is read-only. 297951078Speter */ 298051078Speter sp->ier = inb(iobase + com_ier); 298151078Speter outb(iobase + com_ier, 0); /* spltty() doesn't stop siointr() */ 298251078Speter siocntxwait(iobase); 298351078Speter sp->cfcr = inb(iobase + com_cfcr); 298451078Speter outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 298551078Speter sp->dlbl = inb(iobase + com_dlbl); 298651078Speter sp->dlbh = inb(iobase + com_dlbh); 298751078Speter /* 298851078Speter * Only set the divisor registers if they would change, since on 298951078Speter * some 16550 incompatibles (Startech), setting them clears the 299051078Speter * data input register. This also reduces the effects of the 299151078Speter * UMC8669F bug. 299251078Speter */ 299351078Speter divisor = ttspeedtab(speed, comspeedtab); 299451078Speter dlbl = divisor & 0xFF; 299551078Speter if (sp->dlbl != dlbl) 299651078Speter outb(iobase + com_dlbl, dlbl); 299751078Speter dlbh = (u_int) divisor >> 8; 299851078Speter if (sp->dlbh != dlbh) 299951078Speter outb(iobase + com_dlbh, dlbh); 300051078Speter outb(iobase + com_cfcr, CFCR_8BITS); 300151078Speter sp->mcr = inb(iobase + com_mcr); 300251078Speter /* 300351078Speter * We don't want interrupts, but must be careful not to "disable" 300451078Speter * them by clearing the MCR_IENABLE bit, since that might cause 300551078Speter * an interrupt by floating the IRQ line. 300651078Speter */ 300751078Speter outb(iobase + com_mcr, (sp->mcr & MCR_IENABLE) | MCR_DTR | MCR_RTS); 300851078Speter} 300951078Speter 301051078Speterstatic void 301151078Spetersiocnclose(sp, iobase) 301251078Speter struct siocnstate *sp; 301351078Speter Port_t iobase; 301451078Speter{ 301551078Speter /* 301651078Speter * Restore the device control registers. 301751078Speter */ 301851078Speter siocntxwait(iobase); 301951078Speter outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 302051078Speter if (sp->dlbl != inb(iobase + com_dlbl)) 302151078Speter outb(iobase + com_dlbl, sp->dlbl); 302251078Speter if (sp->dlbh != inb(iobase + com_dlbh)) 302351078Speter outb(iobase + com_dlbh, sp->dlbh); 302451078Speter outb(iobase + com_cfcr, sp->cfcr); 302551078Speter /* 302651078Speter * XXX damp oscillations of MCR_DTR and MCR_RTS by not restoring them. 302751078Speter */ 302851078Speter outb(iobase + com_mcr, sp->mcr | MCR_DTR | MCR_RTS); 302951078Speter outb(iobase + com_ier, sp->ier); 303051078Speter} 303151078Speter 303266230Sjhb#ifndef __alpha__ 303366230Sjhb 303451078Speterstatic void 303551078Spetersiocnprobe(cp) 303651078Speter struct consdev *cp; 303751078Speter{ 303851078Speter speed_t boot_speed; 303951078Speter u_char cfcr; 304051078Speter int s, unit; 304151078Speter struct siocnstate sp; 304251078Speter 304351078Speter /* 304451078Speter * Find our first enabled console, if any. If it is a high-level 304551078Speter * console device, then initialize it and return successfully. 304651078Speter * If it is a low-level console device, then initialize it and 304751078Speter * return unsuccessfully. It must be initialized in both cases 304851078Speter * for early use by console drivers and debuggers. Initializing 304951078Speter * the hardware is not necessary in all cases, since the i/o 305051078Speter * routines initialize it on the fly, but it is necessary if 305151078Speter * input might arrive while the hardware is switched back to an 305251078Speter * uninitialized state. We can't handle multiple console devices 305351078Speter * yet because our low-level routines don't take a device arg. 305451078Speter * We trust the user to set the console flags properly so that we 305551078Speter * don't need to probe. 305651078Speter */ 305751078Speter cp->cn_pri = CN_DEAD; 305851078Speter 305951078Speter for (unit = 0; unit < 16; unit++) { /* XXX need to know how many */ 306051078Speter int flags; 306151078Speter int disabled; 306251078Speter if (resource_int_value("sio", unit, "disabled", &disabled) == 0) { 306351078Speter if (disabled) 306451078Speter continue; 306551078Speter } 306651078Speter if (resource_int_value("sio", unit, "flags", &flags)) 306751078Speter continue; 306851078Speter if (COM_CONSOLE(flags) || COM_DEBUGGER(flags)) { 306951078Speter int port; 307051078Speter Port_t iobase; 307151078Speter 307251078Speter if (resource_int_value("sio", unit, "port", &port)) 307351078Speter continue; 307451078Speter iobase = port; 307551078Speter s = spltty(); 307651078Speter if (boothowto & RB_SERIAL) { 307751078Speter boot_speed = siocngetspeed(iobase, comspeedtab); 307851078Speter if (boot_speed) 307951078Speter comdefaultrate = boot_speed; 308051078Speter } 308151078Speter 308251078Speter /* 308351078Speter * Initialize the divisor latch. We can't rely on 308451078Speter * siocnopen() to do this the first time, since it 308551078Speter * avoids writing to the latch if the latch appears 308651078Speter * to have the correct value. Also, if we didn't 308751078Speter * just read the speed from the hardware, then we 308851078Speter * need to set the speed in hardware so that 308951078Speter * switching it later is null. 309051078Speter */ 309151078Speter cfcr = inb(iobase + com_cfcr); 309251078Speter outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 309351078Speter outb(iobase + com_dlbl, 309451078Speter COMBRD(comdefaultrate) & 0xff); 309551078Speter outb(iobase + com_dlbh, 309651078Speter (u_int) COMBRD(comdefaultrate) >> 8); 309751078Speter outb(iobase + com_cfcr, cfcr); 309851078Speter 309951078Speter siocnopen(&sp, iobase, comdefaultrate); 310051078Speter 310151078Speter splx(s); 310251078Speter if (COM_CONSOLE(flags) && !COM_LLCONSOLE(flags)) { 310351078Speter cp->cn_dev = makedev(CDEV_MAJOR, unit); 310451078Speter cp->cn_pri = COM_FORCECONSOLE(flags) 310551078Speter || boothowto & RB_SERIAL 310651078Speter ? CN_REMOTE : CN_NORMAL; 310751078Speter siocniobase = iobase; 310851078Speter siocnunit = unit; 310951078Speter } 311051078Speter if (COM_DEBUGGER(flags)) { 311151078Speter printf("sio%d: gdb debugging port\n", unit); 311251078Speter siogdbiobase = iobase; 311351078Speter siogdbunit = unit; 311451078Speter#if DDB > 0 311551078Speter gdbdev = makedev(CDEV_MAJOR, unit); 311651078Speter gdb_getc = siocngetc; 311751078Speter gdb_putc = siocnputc; 311851078Speter#endif 311951078Speter } 312051078Speter } 312151078Speter } 312251078Speter#ifdef __i386__ 312351078Speter#if DDB > 0 312451078Speter /* 312551078Speter * XXX Ugly Compatability. 312651078Speter * If no gdb port has been specified, set it to be the console 312751078Speter * as some configuration files don't specify the gdb port. 312851078Speter */ 312951078Speter if (gdbdev == NODEV && (boothowto & RB_GDB)) { 313051078Speter printf("Warning: no GDB port specified. Defaulting to sio%d.\n", 313151078Speter siocnunit); 313251078Speter printf("Set flag 0x80 on desired GDB port in your\n"); 313351078Speter printf("configuration file (currently sio only).\n"); 313451078Speter siogdbiobase = siocniobase; 313551078Speter siogdbunit = siocnunit; 313651078Speter gdbdev = makedev(CDEV_MAJOR, siocnunit); 313751078Speter gdb_getc = siocngetc; 313851078Speter gdb_putc = siocnputc; 313951078Speter } 314051078Speter#endif 314151078Speter#endif 314251078Speter} 314351078Speter 314466230Sjhbstatic void 314566230Sjhbsiocninit(cp) 314666230Sjhb struct consdev *cp; 314766230Sjhb{ 314866230Sjhb comconsole = DEV_TO_UNIT(cp->cn_dev); 314966230Sjhb} 315066230Sjhb 315166230Sjhb#endif 315266230Sjhb 315351078Speter#ifdef __alpha__ 315451078Speter 315555868SgallatinCONS_DRIVER(sio, NULL, NULL, NULL, siocngetc, siocncheckc, siocnputc, NULL); 315651078Speter 315751078Speterint 315851078Spetersiocnattach(port, speed) 315951078Speter int port; 316051078Speter int speed; 316151078Speter{ 316251078Speter int s; 316351078Speter u_char cfcr; 316451078Speter struct siocnstate sp; 316551078Speter 316651078Speter siocniobase = port; 316751078Speter comdefaultrate = speed; 316851078Speter sio_consdev.cn_pri = CN_NORMAL; 316951078Speter sio_consdev.cn_dev = makedev(CDEV_MAJOR, 0); 317051078Speter 317151078Speter s = spltty(); 317251078Speter 317351078Speter /* 317451078Speter * Initialize the divisor latch. We can't rely on 317551078Speter * siocnopen() to do this the first time, since it 317651078Speter * avoids writing to the latch if the latch appears 317751078Speter * to have the correct value. Also, if we didn't 317851078Speter * just read the speed from the hardware, then we 317951078Speter * need to set the speed in hardware so that 318051078Speter * switching it later is null. 318151078Speter */ 318251078Speter cfcr = inb(siocniobase + com_cfcr); 318351078Speter outb(siocniobase + com_cfcr, CFCR_DLAB | cfcr); 318451078Speter outb(siocniobase + com_dlbl, 318551078Speter COMBRD(comdefaultrate) & 0xff); 318651078Speter outb(siocniobase + com_dlbh, 318751078Speter (u_int) COMBRD(comdefaultrate) >> 8); 318851078Speter outb(siocniobase + com_cfcr, cfcr); 318951078Speter 319051078Speter siocnopen(&sp, siocniobase, comdefaultrate); 319151078Speter splx(s); 319251078Speter 319351078Speter cn_tab = &sio_consdev; 319458885Simp return (0); 319551078Speter} 319651078Speter 319751078Speterint 319851078Spetersiogdbattach(port, speed) 319951078Speter int port; 320051078Speter int speed; 320151078Speter{ 320251078Speter int s; 320351078Speter u_char cfcr; 320451078Speter struct siocnstate sp; 320565714Sjhb int unit = 1; /* XXX !!! */ 320651078Speter 320751078Speter siogdbiobase = port; 320851078Speter gdbdefaultrate = speed; 320951078Speter 321065714Sjhb printf("sio%d: gdb debugging port\n", unit); 321165714Sjhb siogdbunit = unit; 321265714Sjhb#if DDB > 0 321365714Sjhb gdbdev = makedev(CDEV_MAJOR, unit); 321465714Sjhb gdb_getc = siocngetc; 321565714Sjhb gdb_putc = siocnputc; 321665714Sjhb#endif 321765714Sjhb 321851078Speter s = spltty(); 321951078Speter 322051078Speter /* 322151078Speter * Initialize the divisor latch. We can't rely on 322251078Speter * siocnopen() to do this the first time, since it 322351078Speter * avoids writing to the latch if the latch appears 322451078Speter * to have the correct value. Also, if we didn't 322551078Speter * just read the speed from the hardware, then we 322651078Speter * need to set the speed in hardware so that 322751078Speter * switching it later is null. 322851078Speter */ 322951078Speter cfcr = inb(siogdbiobase + com_cfcr); 323051078Speter outb(siogdbiobase + com_cfcr, CFCR_DLAB | cfcr); 323151078Speter outb(siogdbiobase + com_dlbl, 323251078Speter COMBRD(gdbdefaultrate) & 0xff); 323351078Speter outb(siogdbiobase + com_dlbh, 323451078Speter (u_int) COMBRD(gdbdefaultrate) >> 8); 323551078Speter outb(siogdbiobase + com_cfcr, cfcr); 323651078Speter 323751078Speter siocnopen(&sp, siogdbiobase, gdbdefaultrate); 323851078Speter splx(s); 323951078Speter 324058885Simp return (0); 324151078Speter} 324251078Speter 324351078Speter#endif 324451078Speter 324551078Speterstatic int 324651078Spetersiocncheckc(dev) 324751078Speter dev_t dev; 324851078Speter{ 324951078Speter int c; 325051078Speter Port_t iobase; 325151078Speter int s; 325251078Speter struct siocnstate sp; 325351078Speter 325451078Speter if (minor(dev) == siogdbunit) 325551078Speter iobase = siogdbiobase; 325651078Speter else 325751078Speter iobase = siocniobase; 325851078Speter s = spltty(); 325951078Speter siocnopen(&sp, iobase, comdefaultrate); 326051078Speter if (inb(iobase + com_lsr) & LSR_RXRDY) 326151078Speter c = inb(iobase + com_data); 326251078Speter else 326351078Speter c = -1; 326451078Speter siocnclose(&sp, iobase); 326551078Speter splx(s); 326651078Speter return (c); 326751078Speter} 326851078Speter 326951078Speter 327051078Speterint 327151078Spetersiocngetc(dev) 327251078Speter dev_t dev; 327351078Speter{ 327451078Speter int c; 327551078Speter Port_t iobase; 327651078Speter int s; 327751078Speter struct siocnstate sp; 327851078Speter 327951078Speter if (minor(dev) == siogdbunit) 328051078Speter iobase = siogdbiobase; 328151078Speter else 328251078Speter iobase = siocniobase; 328351078Speter s = spltty(); 328451078Speter siocnopen(&sp, iobase, comdefaultrate); 328551078Speter while (!(inb(iobase + com_lsr) & LSR_RXRDY)) 328651078Speter ; 328751078Speter c = inb(iobase + com_data); 328851078Speter siocnclose(&sp, iobase); 328951078Speter splx(s); 329051078Speter return (c); 329151078Speter} 329251078Speter 329351078Spetervoid 329451078Spetersiocnputc(dev, c) 329551078Speter dev_t dev; 329651078Speter int c; 329751078Speter{ 329851078Speter int s; 329951078Speter struct siocnstate sp; 330051078Speter Port_t iobase; 330151078Speter 330251078Speter if (minor(dev) == siogdbunit) 330351078Speter iobase = siogdbiobase; 330451078Speter else 330551078Speter iobase = siocniobase; 330651078Speter s = spltty(); 330751078Speter siocnopen(&sp, iobase, comdefaultrate); 330851078Speter siocntxwait(iobase); 330951078Speter outb(iobase + com_data, c); 331051078Speter siocnclose(&sp, iobase); 331151078Speter splx(s); 331251078Speter} 331351078Speter 331451078Speter#ifdef __alpha__ 331551078Speterint 331651078Spetersiogdbgetc() 331751078Speter{ 331851078Speter int c; 331951078Speter Port_t iobase; 332051078Speter int s; 332151078Speter struct siocnstate sp; 332251078Speter 332351078Speter iobase = siogdbiobase; 332451078Speter s = spltty(); 332551078Speter siocnopen(&sp, iobase, gdbdefaultrate); 332651078Speter while (!(inb(iobase + com_lsr) & LSR_RXRDY)) 332751078Speter ; 332851078Speter c = inb(iobase + com_data); 332951078Speter siocnclose(&sp, iobase); 333051078Speter splx(s); 333151078Speter return (c); 333251078Speter} 333351078Speter 333451078Spetervoid 333551078Spetersiogdbputc(c) 333651078Speter int c; 333751078Speter{ 333851078Speter int s; 333951078Speter struct siocnstate sp; 334051078Speter 334151078Speter s = spltty(); 334251078Speter siocnopen(&sp, siogdbiobase, gdbdefaultrate); 334351078Speter siocntxwait(siogdbiobase); 334451078Speter outb(siogdbiobase + com_data, c); 334551078Speter siocnclose(&sp, siogdbiobase); 334651078Speter splx(s); 334751078Speter} 334851078Speter#endif 334951078Speter 335052471SimpDRIVER_MODULE(sio, isa, sio_isa_driver, sio_devclass, 0, 0); 335152471Simp#if NCARD > 0 335252471SimpDRIVER_MODULE(sio, pccard, sio_pccard_driver, sio_devclass, 0, 0); 335352471Simp#endif 335458885Simp#if NPCI > 0 335558885SimpDRIVER_MODULE(sio, pci, sio_pci_driver, sio_devclass, 0, 0); 335658885Simp#endif 3357