com.c revision 1.12.2.3 1 1.1 cgd /*-
2 1.12.2.2 mycroft * Copyright (c) 1993 Charles Hannum
3 1.1 cgd * Copyright (c) 1991 The Regents of the University of California.
4 1.1 cgd * All rights reserved.
5 1.1 cgd *
6 1.1 cgd * Redistribution and use in source and binary forms, with or without
7 1.1 cgd * modification, are permitted provided that the following conditions
8 1.1 cgd * are met:
9 1.1 cgd * 1. Redistributions of source code must retain the above copyright
10 1.1 cgd * notice, this list of conditions and the following disclaimer.
11 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer in the
13 1.1 cgd * documentation and/or other materials provided with the distribution.
14 1.1 cgd * 3. All advertising materials mentioning features or use of this software
15 1.1 cgd * must display the following acknowledgement:
16 1.1 cgd * This product includes software developed by the University of
17 1.1 cgd * California, Berkeley and its contributors.
18 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd *
34 1.7 cgd * from: @(#)com.c 7.5 (Berkeley) 5/16/91
35 1.12.2.3 mycroft * $Id: com.c,v 1.12.2.3 1993/09/30 20:18:59 mycroft Exp $
36 1.1 cgd */
37 1.1 cgd
38 1.1 cgd /*
39 1.12.2.2 mycroft * COM driver, based originally on HP dca driver
40 1.1 cgd * uses National Semiconductor NS16450/NS16550AF UART
41 1.1 cgd */
42 1.1 cgd #include "param.h"
43 1.1 cgd #include "systm.h"
44 1.1 cgd #include "ioctl.h"
45 1.7 cgd #include "select.h"
46 1.1 cgd #include "tty.h"
47 1.1 cgd #include "proc.h"
48 1.1 cgd #include "user.h"
49 1.1 cgd #include "conf.h"
50 1.1 cgd #include "file.h"
51 1.1 cgd #include "uio.h"
52 1.1 cgd #include "kernel.h"
53 1.1 cgd #include "syslog.h"
54 1.6 glass #include "types.h"
55 1.12.2.2 mycroft #include "sys/device.h"
56 1.1 cgd
57 1.12.2.2 mycroft #include "machine/cpu.h"
58 1.12.2.3 mycroft
59 1.12.2.2 mycroft #include "i386/isa/isavar.h"
60 1.12.2.3 mycroft #include "i386/isa/isa.h"
61 1.12.2.3 mycroft #include "i386/isa/icu.h"
62 1.1 cgd #include "i386/isa/comreg.h"
63 1.1 cgd #include "i386/isa/ic/ns16550.h"
64 1.1 cgd
65 1.12.2.2 mycroft struct com_softc {
66 1.12.2.2 mycroft struct device sc_dev;
67 1.12.2.2 mycroft struct isadev sc_id;
68 1.12.2.2 mycroft struct intrhand sc_ih;
69 1.12.2.2 mycroft
70 1.12.2.2 mycroft struct ringbuf {
71 1.12.2.2 mycroft int rb_count, rb_first, rb_last, rb_size;
72 1.12.2.2 mycroft char *rb_data;
73 1.12.2.2 mycroft } sc_q;
74 1.12.2.2 mycroft u_short sc_iobase;
75 1.12.2.2 mycroft u_char sc_flags;
76 1.12.2.2 mycroft #define COM_SOFTCAR 0x01
77 1.12.2.2 mycroft #define COM_FIFO 0x02
78 1.1 cgd };
79 1.12.2.2 mycroft /* XXXX should be in com_softc, but not ready for that yet */
80 1.12.2.2 mycroft #include "com.h"
81 1.12.2.2 mycroft struct tty *com_tty[NCOM];
82 1.1 cgd
83 1.1 cgd int comdefaultrate = TTYDEF_SPEED;
84 1.1 cgd
85 1.12.2.2 mycroft static int comprobe __P((struct device *, struct cfdata *, void *));
86 1.12.2.2 mycroft static void comforceintr __P((void *));
87 1.12.2.2 mycroft static void comattach __P((struct device *, struct device *, void *));
88 1.12.2.2 mycroft static int comintr __P((void *));
89 1.1 cgd
90 1.12.2.2 mycroft struct cfdriver comcd =
91 1.12.2.2 mycroft { NULL, "com", comprobe, comattach, sizeof (struct com_softc) };
92 1.1 cgd
93 1.12.2.2 mycroft int comparam __P((struct tty *, struct termios *));
94 1.12.2.2 mycroft void comstart __P((struct tty *));
95 1.12.2.2 mycroft
96 1.12.2.2 mycroft int comconsole = -1;
97 1.12.2.2 mycroft int comconsinit;
98 1.12.2.2 mycroft int commajor;
99 1.12.2.2 mycroft extern struct tty *constty;
100 1.1 cgd
101 1.12.2.2 mycroft #define COMUNIT(x) (minor(x))
102 1.1 cgd
103 1.12.2.2 mycroft #define bis(c, b) do { const register u_short com_ad = (c); \
104 1.12.2.2 mycroft outb(com_ad, inb(com_ad) | (b)); } while(0)
105 1.12.2.2 mycroft #define bic(c, b) do { const register u_short com_ad = (c); \
106 1.12.2.2 mycroft outb(com_ad, inb(com_ad) &~ (b)); } while(0)
107 1.12.2.2 mycroft
108 1.12.2.2 mycroft static int
109 1.12.2.2 mycroft comspeed(speed)
110 1.12.2.2 mycroft int speed;
111 1.12.2.2 mycroft {
112 1.12.2.2 mycroft #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
113 1.12.2.2 mycroft
114 1.12.2.2 mycroft int x, err;
115 1.12.2.2 mycroft
116 1.12.2.2 mycroft if (speed == 0)
117 1.12.2.2 mycroft return 0;
118 1.12.2.2 mycroft if (speed < 0)
119 1.12.2.2 mycroft return -1;
120 1.12.2.2 mycroft x = divrnd((COM_FREQ / 16), speed);
121 1.12.2.2 mycroft if (x <= 0)
122 1.12.2.2 mycroft return -1;
123 1.12.2.2 mycroft err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
124 1.12.2.2 mycroft if (err < 0)
125 1.12.2.2 mycroft err = -err;
126 1.12.2.2 mycroft if (err > COM_TOLERANCE)
127 1.12.2.2 mycroft return -1;
128 1.12.2.2 mycroft return x;
129 1.12.2.2 mycroft
130 1.12.2.2 mycroft #undef divrnd(n, q)
131 1.12.2.2 mycroft }
132 1.12.2.2 mycroft
133 1.12.2.2 mycroft static int
134 1.12.2.2 mycroft _comprobe(iobase)
135 1.12.2.2 mycroft u_short iobase;
136 1.1 cgd {
137 1.12.2.2 mycroft
138 1.1 cgd /* force access to id reg */
139 1.12.2.2 mycroft outb(iobase + com_cfcr, 0);
140 1.12.2.2 mycroft outb(iobase + com_iir, 0);
141 1.12.2.2 mycroft if (inb(iobase + com_iir) & 0x38)
142 1.12.2.2 mycroft return 0;
143 1.12.2.2 mycroft outb(iobase + com_ier, 0);
144 1.12.2.2 mycroft return 1;
145 1.12.2.2 mycroft }
146 1.12.2.2 mycroft
147 1.12.2.2 mycroft static int
148 1.12.2.2 mycroft comprobe(parent, cf, aux)
149 1.12.2.2 mycroft struct device *parent;
150 1.12.2.2 mycroft struct cfdata *cf;
151 1.12.2.2 mycroft void *aux;
152 1.12.2.2 mycroft {
153 1.12.2.2 mycroft struct isa_attach_args *ia = aux;
154 1.12.2.2 mycroft u_short iobase = ia->ia_iobase;
155 1.12.2.2 mycroft
156 1.12.2.3 mycroft if (iobase == IOBASEUNK)
157 1.12.2.3 mycroft return 0;
158 1.12.2.3 mycroft
159 1.12.2.2 mycroft if (!_comprobe(iobase))
160 1.12.2.2 mycroft return 0;
161 1.12.2.2 mycroft
162 1.12.2.3 mycroft if (ia->ia_irq == IRQUNK) {
163 1.12.2.3 mycroft ia->ia_irq = isa_discoverintr(comforceintr, aux);
164 1.12.2.3 mycroft if (ia->ia_irq == IRQNONE)
165 1.12.2.3 mycroft return 0;
166 1.12.2.3 mycroft }
167 1.12.2.2 mycroft
168 1.12.2.2 mycroft ia->ia_iosize = COM_NPORTS;
169 1.12.2.2 mycroft ia->ia_drq = DRQUNK;
170 1.12.2.2 mycroft ia->ia_msize = 0;
171 1.12.2.2 mycroft return 1;
172 1.12.2.2 mycroft }
173 1.12.2.2 mycroft
174 1.12.2.2 mycroft static void
175 1.12.2.2 mycroft comforceintr(aux)
176 1.12.2.2 mycroft void *aux;
177 1.12.2.2 mycroft {
178 1.12.2.2 mycroft struct isa_attach_args *ia = aux;
179 1.12.2.2 mycroft u_short iobase = ia->ia_iobase;
180 1.12.2.2 mycroft
181 1.12.2.2 mycroft outb(iobase + com_fifo, 0);
182 1.12.2.2 mycroft delay(100);
183 1.12.2.2 mycroft outb(iobase + com_mcr, MCR_IENABLE | MCR_LOOPBACK);
184 1.12.2.2 mycroft outb(iobase + com_ier, IER_EMSC);
185 1.12.2.2 mycroft outb(iobase + com_mcr, MCR_IENABLE | MCR_LOOPBACK | MCR_DRS);
186 1.12.2.2 mycroft outb(iobase + com_mcr, MCR_IENABLE | MCR_LOOPBACK);
187 1.12.2.2 mycroft outb(iobase + com_ier, 0);
188 1.12.2.2 mycroft }
189 1.12.2.2 mycroft
190 1.12.2.2 mycroft static void
191 1.12.2.2 mycroft comattach(parent, self, aux)
192 1.12.2.2 mycroft struct device *parent, *self;
193 1.12.2.2 mycroft void *aux;
194 1.12.2.2 mycroft {
195 1.12.2.2 mycroft struct com_softc *sc = (struct com_softc *)self;
196 1.12.2.2 mycroft struct isa_attach_args *ia = aux;
197 1.12.2.2 mycroft u_short iobase = ia->ia_iobase;
198 1.12.2.2 mycroft struct tty *tp;
199 1.12.2.2 mycroft u_char unit = sc->sc_dev.dv_unit;
200 1.1 cgd
201 1.12.2.2 mycroft if (iobase == comconsole)
202 1.12.2.2 mycroft delay(1000);
203 1.1 cgd
204 1.12.2.2 mycroft sc->sc_iobase = iobase;
205 1.12.2.2 mycroft sc->sc_flags = 0;
206 1.1 cgd
207 1.1 cgd /* look for a NS 16550AF UART with FIFOs */
208 1.12.2.2 mycroft outb(iobase + com_fifo,
209 1.12.2.2 mycroft FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_4);
210 1.12.2.2 mycroft delay(100);
211 1.12.2.2 mycroft if ((inb(iobase + com_iir) & IIR_FIFO_MASK) == IIR_FIFO_MASK) {
212 1.12.2.2 mycroft sc->sc_flags |= COM_FIFO;
213 1.12.2.2 mycroft printf(": NS16550\n");
214 1.12.2.2 mycroft } else
215 1.12.2.2 mycroft printf(": NS8250 or NS16450\n");
216 1.12.2.3 mycroft isa_establish(&sc->sc_id, &sc->sc_dev);
217 1.12.2.3 mycroft
218 1.12.2.3 mycroft sc->sc_ih.ih_fun = comintr;
219 1.12.2.3 mycroft sc->sc_ih.ih_arg = sc;
220 1.12.2.3 mycroft intr_establish(ia->ia_irq, &sc->sc_ih, DV_TTY);
221 1.12.2.2 mycroft
222 1.12.2.2 mycroft outb(iobase + com_ier, 0);
223 1.12.2.2 mycroft outb(iobase + com_mcr, MCR_IENABLE);
224 1.12.2.2 mycroft
225 1.1 cgd /*
226 1.1 cgd * Need to reset baud rate, etc. of next print so reset comconsinit.
227 1.1 cgd * Also make sure console is always "hardwired"
228 1.1 cgd */
229 1.12.2.2 mycroft if (iobase == comconsole) {
230 1.12.2.2 mycroft constty = com_tty[unit] = ttymalloc();
231 1.1 cgd comconsinit = 0;
232 1.12.2.2 mycroft sc->sc_flags |= COM_SOFTCAR;
233 1.1 cgd }
234 1.1 cgd }
235 1.1 cgd
236 1.12.2.2 mycroft int
237 1.12.2.2 mycroft comopen(dev, flag, mode, p)
238 1.12.2.2 mycroft dev_t dev;
239 1.12.2.2 mycroft int flag, mode;
240 1.12.2.2 mycroft struct proc *p;
241 1.1 cgd {
242 1.12.2.2 mycroft int unit = COMUNIT(dev);
243 1.12.2.2 mycroft struct com_softc *sc;
244 1.12.2.2 mycroft u_short iobase;
245 1.12.2.2 mycroft struct tty *tp;
246 1.12.2.2 mycroft int s;
247 1.12.2.2 mycroft int error = 0;
248 1.12.2.2 mycroft
249 1.12.2.2 mycroft if (unit >= comcd.cd_ndevs)
250 1.12.2.2 mycroft return ENXIO;
251 1.12.2.2 mycroft sc = comcd.cd_devs[unit];
252 1.12.2.2 mycroft if (!sc)
253 1.12.2.2 mycroft return ENXIO;
254 1.12.2.2 mycroft
255 1.12.2.2 mycroft if (!com_tty[unit])
256 1.11 mycroft tp = com_tty[unit] = ttymalloc();
257 1.12.2.2 mycroft else
258 1.8 deraadt tp = com_tty[unit];
259 1.12.2.2 mycroft
260 1.1 cgd tp->t_oproc = comstart;
261 1.1 cgd tp->t_param = comparam;
262 1.1 cgd tp->t_dev = dev;
263 1.1 cgd if ((tp->t_state & TS_ISOPEN) == 0) {
264 1.1 cgd tp->t_state |= TS_WOPEN;
265 1.1 cgd ttychars(tp);
266 1.12.2.2 mycroft /* preserve previous speed, if any */
267 1.1 cgd if (tp->t_ispeed == 0) {
268 1.1 cgd tp->t_iflag = TTYDEF_IFLAG;
269 1.1 cgd tp->t_oflag = TTYDEF_OFLAG;
270 1.1 cgd tp->t_cflag = TTYDEF_CFLAG;
271 1.1 cgd tp->t_lflag = TTYDEF_LFLAG;
272 1.1 cgd tp->t_ispeed = tp->t_ospeed = comdefaultrate;
273 1.1 cgd }
274 1.1 cgd comparam(tp, &tp->t_termios);
275 1.1 cgd ttsetwater(tp);
276 1.12.2.2 mycroft } else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
277 1.12.2.2 mycroft return EBUSY;
278 1.12.2.2 mycroft
279 1.12.2.1 mycroft s = spltty();
280 1.12.2.2 mycroft iobase = sc->sc_iobase;
281 1.12.2.2 mycroft outb(iobase + com_mcr, MCR_DTR | MCR_RTS | MCR_IENABLE);
282 1.12.2.2 mycroft outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC);
283 1.12.2.2 mycroft
284 1.12.2.2 mycroft if (sc->sc_flags & COM_SOFTCAR || inb(iobase + com_msr) & MSR_DCD)
285 1.1 cgd tp->t_state |= TS_CARR_ON;
286 1.12.2.2 mycroft else
287 1.12.2.2 mycroft tp->t_state &= ~TS_CARR_ON;
288 1.12.2.2 mycroft while ((flag & O_NONBLOCK) == 0 && (tp->t_cflag & CLOCAL) == 0 &&
289 1.1 cgd (tp->t_state & TS_CARR_ON) == 0) {
290 1.12.2.2 mycroft /* wait for carrier; allow signals */
291 1.1 cgd tp->t_state |= TS_WOPEN;
292 1.11 mycroft if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
293 1.12.2.2 mycroft ttopen, 0)) {
294 1.12.2.2 mycroft splx(s);
295 1.12.2.2 mycroft return error;
296 1.12.2.2 mycroft }
297 1.1 cgd }
298 1.12.2.1 mycroft splx(s);
299 1.12.2.2 mycroft
300 1.12.2.2 mycroft return (*linesw[tp->t_line].l_open)(dev, tp);
301 1.1 cgd }
302 1.1 cgd
303 1.12.2.2 mycroft int
304 1.12.2.2 mycroft comclose(dev, flag)
305 1.1 cgd dev_t dev;
306 1.12.2.2 mycroft int flag;
307 1.1 cgd {
308 1.12.2.2 mycroft int unit = COMUNIT(dev);
309 1.12.2.2 mycroft struct com_softc *sc = comcd.cd_devs[unit];
310 1.12.2.2 mycroft u_short iobase = sc->sc_iobase;
311 1.12.2.2 mycroft struct tty *tp = com_tty[unit];
312 1.1 cgd
313 1.1 cgd (*linesw[tp->t_line].l_close)(tp, flag);
314 1.12.2.2 mycroft bic(iobase + com_cfcr, CFCR_SBREAK);
315 1.12.2.2 mycroft outb(iobase + com_ier, 0);
316 1.12.2.2 mycroft if (tp->t_cflag & HUPCL)
317 1.12.2.2 mycroft bic(iobase + com_mcr, MCR_DTR | MCR_RTS);
318 1.1 cgd ttyclose(tp);
319 1.12.2.2 mycroft tp->t_state &= ~(TS_ISOPEN | TS_WOPEN);
320 1.12.2.2 mycroft #ifdef notyet /* XXXX */
321 1.12.2.2 mycroft if (iobase != comconsole) {
322 1.12.2.2 mycroft ttyfree(tp);
323 1.12.2.2 mycroft com_tty[unit] = (struct tty *)NULL;
324 1.12.2.2 mycroft }
325 1.12.2.2 mycroft #endif
326 1.12.2.2 mycroft return 0;
327 1.1 cgd }
328 1.1 cgd
329 1.12.2.2 mycroft int
330 1.1 cgd comread(dev, uio, flag)
331 1.1 cgd dev_t dev;
332 1.1 cgd struct uio *uio;
333 1.12.2.2 mycroft int flag;
334 1.1 cgd {
335 1.12.2.2 mycroft struct tty *tp = com_tty[COMUNIT(dev)];
336 1.1 cgd
337 1.12.2.2 mycroft return (*linesw[tp->t_line].l_read)(tp, uio, flag);
338 1.1 cgd }
339 1.1 cgd
340 1.12.2.2 mycroft int
341 1.1 cgd comwrite(dev, uio, flag)
342 1.1 cgd dev_t dev;
343 1.1 cgd struct uio *uio;
344 1.12.2.2 mycroft int flag;
345 1.1 cgd {
346 1.12.2.2 mycroft struct tty *tp = com_tty[COMUNIT(dev)];
347 1.1 cgd
348 1.12.2.2 mycroft /* XXXX what is this for? */
349 1.12.2.2 mycroft if (constty == tp)
350 1.1 cgd constty = NULL;
351 1.12.2.2 mycroft return (*linesw[tp->t_line].l_write)(tp, uio, flag);
352 1.1 cgd }
353 1.1 cgd
354 1.12.2.2 mycroft static u_char
355 1.12.2.2 mycroft tiocm_xxx2mcr(data)
356 1.12.2.2 mycroft int data;
357 1.12.2.2 mycroft {
358 1.12.2.2 mycroft u_char m = 0;
359 1.12.2.2 mycroft if (data & TIOCM_DTR)
360 1.12.2.2 mycroft m |= MCR_DTR;
361 1.12.2.2 mycroft if (data & TIOCM_RTS)
362 1.12.2.2 mycroft m |= MCR_RTS;
363 1.12.2.2 mycroft return m;
364 1.1 cgd }
365 1.1 cgd
366 1.12.2.2 mycroft int
367 1.1 cgd comioctl(dev, cmd, data, flag)
368 1.1 cgd dev_t dev;
369 1.12.2.2 mycroft int cmd;
370 1.1 cgd caddr_t data;
371 1.12.2.2 mycroft int flag;
372 1.1 cgd {
373 1.12.2.2 mycroft int unit = COMUNIT(dev);
374 1.12.2.2 mycroft struct com_softc *sc = comcd.cd_devs[unit];
375 1.12.2.2 mycroft u_short iobase = sc->sc_iobase;
376 1.12.2.2 mycroft struct tty *tp = com_tty[unit];
377 1.12.2.2 mycroft int error;
378 1.12.2.2 mycroft
379 1.12.2.2 mycroft if (error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag))
380 1.12.2.2 mycroft return error;
381 1.12.2.2 mycroft if (error = ttioctl(tp, cmd, data, flag))
382 1.12.2.2 mycroft return error;
383 1.1 cgd
384 1.1 cgd switch (cmd) {
385 1.12.2.2 mycroft case TIOCSBRK:
386 1.12.2.2 mycroft bis(iobase + com_cfcr, CFCR_SBREAK);
387 1.1 cgd break;
388 1.12.2.2 mycroft case TIOCCBRK:
389 1.12.2.2 mycroft bic(iobase + com_cfcr, CFCR_SBREAK);
390 1.1 cgd break;
391 1.12.2.2 mycroft case TIOCSDTR:
392 1.12.2.2 mycroft bis(iobase + com_mcr, MCR_DTR | MCR_RTS);
393 1.1 cgd break;
394 1.12.2.2 mycroft case TIOCCDTR:
395 1.12.2.2 mycroft bic(iobase + com_mcr, MCR_DTR | MCR_RTS);
396 1.12.2.2 mycroft break;
397 1.12.2.2 mycroft case TIOCMSET:
398 1.12.2.2 mycroft outb(iobase + com_mcr, tiocm_xxx2mcr(*(int *)data) | MCR_IENABLE);
399 1.12.2.2 mycroft break;
400 1.12.2.2 mycroft case TIOCMBIS:
401 1.12.2.2 mycroft bis(iobase + com_mcr, tiocm_xxx2mcr(*(int *)data));
402 1.12.2.2 mycroft break;
403 1.12.2.2 mycroft case TIOCMBIC:
404 1.12.2.2 mycroft bic(iobase + com_mcr, tiocm_xxx2mcr(*(int *)data));
405 1.12.2.2 mycroft break;
406 1.12.2.2 mycroft case TIOCMGET:
407 1.12.2.2 mycroft {
408 1.12.2.2 mycroft u_char m = inb(iobase + com_mcr);
409 1.12.2.2 mycroft int bits = 0;
410 1.12.2.2 mycroft
411 1.12.2.2 mycroft m = inb(iobase + com_mcr);
412 1.12.2.2 mycroft if (m & MCR_DTR)
413 1.12.2.2 mycroft bits |= TIOCM_DTR;
414 1.12.2.2 mycroft if (m & MCR_RTS)
415 1.12.2.2 mycroft bits |= TIOCM_RTS;
416 1.12.2.2 mycroft m = inb(iobase + com_msr);
417 1.12.2.2 mycroft if (m & MSR_CTS)
418 1.12.2.2 mycroft bits |= TIOCM_CTS;
419 1.12.2.2 mycroft if (m & MSR_DSR)
420 1.12.2.2 mycroft bits |= TIOCM_DSR;
421 1.12.2.2 mycroft if (m & (MSR_RI | MSR_TERI))
422 1.12.2.2 mycroft bits |= TIOCM_RI;
423 1.12.2.2 mycroft if (inb(iobase + com_ier))
424 1.12.2.2 mycroft bits |= TIOCM_LE;
425 1.12.2.2 mycroft *(int *)data = bits;
426 1.12.2.2 mycroft break;
427 1.12.2.2 mycroft }
428 1.1 cgd break;
429 1.12.2.2 mycroft default:
430 1.12.2.2 mycroft return ENOTTY;
431 1.1 cgd }
432 1.12.2.2 mycroft
433 1.12.2.2 mycroft return 0;
434 1.1 cgd }
435 1.1 cgd
436 1.12.2.2 mycroft int
437 1.1 cgd comparam(tp, t)
438 1.12.2.2 mycroft struct tty *tp;
439 1.12.2.2 mycroft struct termios *t;
440 1.1 cgd {
441 1.12.2.2 mycroft int unit = COMUNIT(tp->t_dev);
442 1.12.2.2 mycroft struct com_softc *sc = comcd.cd_devs[unit];
443 1.12.2.2 mycroft u_short iobase = sc->sc_iobase;
444 1.12.2.2 mycroft int ospeed = comspeed(t->c_ospeed);
445 1.12.2.2 mycroft u_char cflag = t->c_cflag;
446 1.12.2.2 mycroft u_char cfcr;
447 1.1 cgd
448 1.1 cgd /* check requested parameters */
449 1.1 cgd if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
450 1.12.2.2 mycroft return EINVAL;
451 1.12.2.2 mycroft
452 1.1 cgd /* and copy to tty */
453 1.1 cgd tp->t_ispeed = t->c_ispeed;
454 1.1 cgd tp->t_ospeed = t->c_ospeed;
455 1.1 cgd tp->t_cflag = cflag;
456 1.1 cgd
457 1.12.2.2 mycroft if (ospeed == 0)
458 1.12.2.2 mycroft bic(iobase + com_mcr, MCR_DTR | MCR_RTS);
459 1.12.2.2 mycroft else
460 1.12.2.2 mycroft bis(iobase + com_mcr, MCR_DTR | MCR_RTS);
461 1.12.2.2 mycroft
462 1.12.2.2 mycroft outb(iobase + com_cfcr, CFCR_DLAB);
463 1.12.2.2 mycroft outb(iobase + com_dlbl, ospeed & 0xff);
464 1.12.2.2 mycroft outb(iobase + com_dlbh, ospeed >> 8);
465 1.12.2.2 mycroft
466 1.12.2.2 mycroft switch (cflag & CSIZE) {
467 1.12.2.2 mycroft case CS5:
468 1.12.2.2 mycroft cfcr = CFCR_5BITS;
469 1.12.2.2 mycroft break;
470 1.12.2.2 mycroft case CS6:
471 1.12.2.2 mycroft cfcr = CFCR_6BITS;
472 1.12.2.2 mycroft break;
473 1.12.2.2 mycroft case CS7:
474 1.12.2.2 mycroft cfcr = CFCR_7BITS;
475 1.12.2.2 mycroft break;
476 1.12.2.2 mycroft case CS8:
477 1.12.2.2 mycroft cfcr = CFCR_8BITS;
478 1.12.2.2 mycroft break;
479 1.1 cgd }
480 1.12.2.2 mycroft if (cflag & PARENB) {
481 1.1 cgd cfcr |= CFCR_PENAB;
482 1.12.2.2 mycroft if ((cflag & PARODD) == 0)
483 1.1 cgd cfcr |= CFCR_PEVEN;
484 1.1 cgd }
485 1.12.2.2 mycroft if (cflag & CSTOPB)
486 1.1 cgd cfcr |= CFCR_STOPB;
487 1.12.2.2 mycroft outb(iobase + com_cfcr, cfcr);
488 1.1 cgd
489 1.12.2.2 mycroft /* when not using CRTS_IFLOW, RTS follows DTR */
490 1.12.2.2 mycroft if ((cflag & CRTS_IFLOW) == 0) {
491 1.12.2.2 mycroft u_char mcr = inb(iobase + com_mcr);
492 1.12.2.2 mycroft
493 1.12.2.2 mycroft if (mcr & MCR_DTR)
494 1.12.2.2 mycroft if ((mcr & MCR_RTS) == 0)
495 1.12.2.2 mycroft outb(iobase + com_mcr, mcr | MCR_RTS);
496 1.12.2.2 mycroft else
497 1.12.2.2 mycroft ;
498 1.12.2.2 mycroft else
499 1.12.2.2 mycroft if (mcr & MCR_RTS)
500 1.12.2.2 mycroft outb(iobase + com_mcr, mcr &~ MCR_RTS);
501 1.12.2.2 mycroft else
502 1.12.2.2 mycroft ;
503 1.12.2.2 mycroft }
504 1.1 cgd
505 1.12.2.2 mycroft return 0;
506 1.1 cgd }
507 1.1 cgd
508 1.12 deraadt void
509 1.1 cgd comstart(tp)
510 1.12.2.2 mycroft struct tty *tp;
511 1.1 cgd {
512 1.12.2.2 mycroft int unit = COMUNIT(tp->t_dev);
513 1.12.2.2 mycroft struct com_softc *sc = comcd.cd_devs[unit];
514 1.12.2.2 mycroft u_short iobase = sc->sc_iobase;
515 1.12.2.2 mycroft int s;
516 1.1 cgd
517 1.1 cgd s = spltty();
518 1.12.2.2 mycroft if (tp->t_state & (TS_TTSTOP | TS_BUSY))
519 1.12.2.2 mycroft goto out;
520 1.12.2.2 mycroft if (tp->t_cflag & CCTS_OFLOW && (inb(iobase + com_mcr) & MSR_CTS) == 0)
521 1.1 cgd goto out;
522 1.11 mycroft if (tp->t_outq.c_cc <= tp->t_lowat) {
523 1.12.2.2 mycroft if (tp->t_state & TS_ASLEEP) {
524 1.1 cgd tp->t_state &= ~TS_ASLEEP;
525 1.11 mycroft wakeup((caddr_t)&tp->t_outq);
526 1.1 cgd }
527 1.7 cgd selwakeup(&tp->t_wsel);
528 1.1 cgd }
529 1.11 mycroft if (tp->t_outq.c_cc == 0)
530 1.1 cgd goto out;
531 1.12.2.2 mycroft tp->t_state |= TS_BUSY;
532 1.12.2.2 mycroft if ((inb(iobase + com_lsr) & LSR_TXRDY) == 0)
533 1.12.2.2 mycroft goto out;
534 1.12.2.2 mycroft if (sc->sc_flags & COM_FIFO) {
535 1.12.2.2 mycroft u_char buffer[16], *cp = buffer;
536 1.12.2.2 mycroft int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
537 1.12.2.2 mycroft do {
538 1.12.2.2 mycroft outb(iobase + com_data, *cp++);
539 1.12.2.2 mycroft } while (--n);
540 1.12.2.2 mycroft } else
541 1.12.2.2 mycroft outb(iobase + com_data, getc(&tp->t_outq));
542 1.12.2.2 mycroft out:
543 1.1 cgd splx(s);
544 1.1 cgd }
545 1.1 cgd
546 1.1 cgd /*
547 1.1 cgd * Stop output on a line.
548 1.1 cgd */
549 1.12.2.2 mycroft void
550 1.1 cgd comstop(tp, flag)
551 1.12.2.2 mycroft struct tty *tp;
552 1.1 cgd {
553 1.12.2.2 mycroft int s;
554 1.1 cgd
555 1.1 cgd s = spltty();
556 1.1 cgd if (tp->t_state & TS_BUSY) {
557 1.12.2.2 mycroft if ((tp->t_state & TS_TTSTOP) == 0)
558 1.1 cgd tp->t_state |= TS_FLUSH;
559 1.1 cgd }
560 1.1 cgd splx(s);
561 1.1 cgd }
562 1.1 cgd
563 1.12.2.2 mycroft static void
564 1.12.2.2 mycroft comeint(sc, stat)
565 1.12.2.2 mycroft struct com_softc *sc;
566 1.12.2.2 mycroft int stat;
567 1.12.2.2 mycroft {
568 1.12.2.2 mycroft u_short iobase = sc->sc_iobase;
569 1.12.2.2 mycroft struct tty *tp = com_tty[sc->sc_dev.dv_unit];
570 1.12.2.2 mycroft int c;
571 1.1 cgd
572 1.12.2.2 mycroft c = inb(iobase + com_data);
573 1.12.2.2 mycroft if ((tp->t_state & TS_ISOPEN) == 0)
574 1.12.2.2 mycroft return;
575 1.12.2.2 mycroft if (stat & (LSR_BI | LSR_FE))
576 1.12.2.2 mycroft c |= TTY_FE;
577 1.12.2.2 mycroft else if (stat & LSR_PE)
578 1.12.2.2 mycroft c |= TTY_PE;
579 1.12.2.2 mycroft else if (stat & LSR_OE) {
580 1.12.2.2 mycroft c |= TTY_PE; /* XXX ought to have its own define */
581 1.12.2.2 mycroft log(LOG_WARNING, "com%d: silo overflow\n", sc->sc_dev.dv_unit);
582 1.12.2.2 mycroft }
583 1.12.2.2 mycroft /* XXXX put in FIFO and process later */
584 1.12.2.2 mycroft (*linesw[tp->t_line].l_rint)(c, tp);
585 1.12.2.2 mycroft }
586 1.1 cgd
587 1.12.2.2 mycroft static void
588 1.12.2.2 mycroft commint(sc)
589 1.12.2.2 mycroft struct com_softc *sc;
590 1.12.2.2 mycroft {
591 1.12.2.2 mycroft u_short iobase = sc->sc_iobase;
592 1.12.2.2 mycroft struct tty *tp = com_tty[sc->sc_dev.dv_unit];
593 1.12.2.2 mycroft u_char stat;
594 1.1 cgd
595 1.12.2.2 mycroft stat = inb(iobase + com_msr);
596 1.12.2.2 mycroft if (stat & MSR_DDCD && (sc->sc_flags & COM_SOFTCAR) == 0) {
597 1.12.2.2 mycroft if (stat & MSR_DCD)
598 1.12.2.2 mycroft (void)(*linesw[tp->t_line].l_modem)(tp, 1);
599 1.12.2.2 mycroft else if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0)
600 1.12.2.2 mycroft bic(iobase + com_mcr, MCR_DTR | MCR_RTS);
601 1.12.2.2 mycroft } else if (stat & MSR_DCTS && tp->t_state & TS_ISOPEN &&
602 1.12.2.2 mycroft tp->t_cflag & CRTSCTS) {
603 1.12.2.2 mycroft /* the line is up and we want to do rts/cts flow control */
604 1.12.2.2 mycroft if (stat & MSR_CTS) {
605 1.12.2.2 mycroft tp->t_state &=~ TS_TTSTOP;
606 1.12.2.2 mycroft ttstart(tp);
607 1.12.2.2 mycroft } else
608 1.12.2.2 mycroft tp->t_state |= TS_TTSTOP;
609 1.12.2.2 mycroft }
610 1.12.2.2 mycroft }
611 1.1 cgd
612 1.12.2.2 mycroft static int
613 1.12.2.2 mycroft comintr(arg)
614 1.12.2.2 mycroft void *arg;
615 1.12.2.2 mycroft {
616 1.12.2.2 mycroft struct com_softc *sc = arg;
617 1.12.2.2 mycroft u_short iobase = sc->sc_iobase;
618 1.12.2.2 mycroft struct tty *tp;
619 1.12.2.2 mycroft u_char code;
620 1.12.2.2 mycroft
621 1.12.2.2 mycroft code = inb(iobase + com_iir);
622 1.12.2.2 mycroft if (code & IIR_NOPEND)
623 1.12.2.2 mycroft return 0;
624 1.1 cgd
625 1.12.2.2 mycroft for (;;) {
626 1.12.2.2 mycroft switch (code & IIR_IMASK) {
627 1.12.2.2 mycroft case IIR_RXRDY:
628 1.12.2.2 mycroft case IIR_RXTOUT:
629 1.12.2.2 mycroft tp = com_tty[sc->sc_dev.dv_unit];
630 1.12.2.2 mycroft /* XXXX put in FIFO and process later */
631 1.12.2.2 mycroft #define RCVBYTE() \
632 1.12.2.2 mycroft code = inb(iobase + com_data); \
633 1.12.2.2 mycroft if (tp->t_state & TS_ISOPEN) \
634 1.12.2.2 mycroft (*linesw[tp->t_line].l_rint)(code, tp)
635 1.12.2.2 mycroft RCVBYTE();
636 1.12.2.2 mycroft if (sc->sc_flags & COM_FIFO)
637 1.12.2.2 mycroft while (code = inb(iobase + com_lsr) & LSR_RCV_MASK) {
638 1.12.2.2 mycroft if (code == LSR_RXRDY) {
639 1.12.2.2 mycroft RCVBYTE();
640 1.12.2.2 mycroft } else
641 1.12.2.2 mycroft comeint(sc, code);
642 1.12.2.2 mycroft }
643 1.12.2.2 mycroft break;
644 1.12.2.2 mycroft case IIR_TXRDY:
645 1.12.2.2 mycroft tp = com_tty[sc->sc_dev.dv_unit];
646 1.12.2.2 mycroft tp->t_state &=~ (TS_BUSY | TS_FLUSH);
647 1.12.2.2 mycroft if (tp->t_line)
648 1.12.2.2 mycroft (*linesw[tp->t_line].l_start)(tp);
649 1.12.2.2 mycroft else
650 1.12.2.2 mycroft comstart(tp);
651 1.12.2.2 mycroft break;
652 1.12.2.2 mycroft case IIR_RLS:
653 1.12.2.2 mycroft comeint(sc, inb(iobase + com_lsr));
654 1.12.2.2 mycroft break;
655 1.12.2.2 mycroft default:
656 1.12.2.2 mycroft log(LOG_WARNING, "com%d: weird iir=0x%x\n",
657 1.12.2.2 mycroft sc->sc_dev.dv_unit, code);
658 1.12.2.2 mycroft /* fall through */
659 1.12.2.2 mycroft case IIR_MLSC:
660 1.12.2.2 mycroft commint(sc);
661 1.12.2.2 mycroft break;
662 1.12.2.2 mycroft }
663 1.12.2.2 mycroft
664 1.12.2.2 mycroft code = inb(iobase + com_iir);
665 1.12.2.2 mycroft if (code & IIR_NOPEND)
666 1.12.2.2 mycroft return 1;
667 1.1 cgd }
668 1.1 cgd }
669 1.1 cgd
670 1.12.2.2 mycroft /* XXXXXXXXXXXXXXXX ---- gremlins below here ---- XXXXXXXXXXXXXXXX */
671 1.12.2.2 mycroft
672 1.12.2.2 mycroft #if 0
673 1.1 cgd /*
674 1.1 cgd * Following are all routines needed for COM to act as console
675 1.1 cgd */
676 1.1 cgd #include "i386/i386/cons.h"
677 1.1 cgd
678 1.1 cgd comcnprobe(cp)
679 1.1 cgd struct consdev *cp;
680 1.1 cgd {
681 1.12.2.2 mycroft /* XXXX */
682 1.12.2.2 mycroft if (!_comprobe(CONADDR))
683 1.12.2.2 mycroft return CN_DEAD;
684 1.1 cgd
685 1.1 cgd /* locate the major number */
686 1.1 cgd for (commajor = 0; commajor < nchrdev; commajor++)
687 1.1 cgd if (cdevsw[commajor].d_open == comopen)
688 1.1 cgd break;
689 1.1 cgd
690 1.1 cgd /* initialize required fields */
691 1.2 cgd cp->cn_dev = makedev(commajor, unit);
692 1.1 cgd #ifdef COMCONSOLE
693 1.1 cgd cp->cn_pri = CN_REMOTE; /* Force a serial port console */
694 1.1 cgd #else
695 1.1 cgd cp->cn_pri = CN_NORMAL;
696 1.1 cgd #endif
697 1.1 cgd }
698 1.1 cgd
699 1.1 cgd comcninit(cp)
700 1.1 cgd struct consdev *cp;
701 1.1 cgd {
702 1.12.2.2 mycroft int unit = COMUNIT(cp->cn_dev);
703 1.1 cgd
704 1.12.2.2 mycroft comconsole = CONADDR;
705 1.12.2.2 mycroft comconsinit = 0;
706 1.1 cgd }
707 1.1 cgd
708 1.1 cgd cominit(unit, rate)
709 1.1 cgd int unit, rate;
710 1.1 cgd {
711 1.12.2.2 mycroft int com;
712 1.1 cgd int s;
713 1.1 cgd short stat;
714 1.1 cgd
715 1.1 cgd com = com_addr[unit];
716 1.1 cgd s = splhigh();
717 1.1 cgd outb(com+com_cfcr, CFCR_DLAB);
718 1.12.2.2 mycroft rate = COM_BAUDDIV(rate);
719 1.1 cgd outb(com+com_data, rate & 0xFF);
720 1.1 cgd outb(com+com_ier, rate >> 8);
721 1.1 cgd outb(com+com_cfcr, CFCR_8BITS);
722 1.1 cgd outb(com+com_ier, IER_ERXRDY | IER_ETXRDY);
723 1.2 cgd outb(com+com_fifo, FIFO_ENABLE|FIFO_RCV_RST|FIFO_XMT_RST|FIFO_TRIGGER_4);
724 1.1 cgd stat = inb(com+com_iir);
725 1.1 cgd splx(s);
726 1.1 cgd }
727 1.1 cgd
728 1.1 cgd comcngetc(dev)
729 1.1 cgd {
730 1.12.2.2 mycroft com = com_addr[COMUNIT(dev)];
731 1.1 cgd short stat;
732 1.1 cgd int c, s;
733 1.1 cgd
734 1.1 cgd s = splhigh();
735 1.1 cgd while (((stat = inb(com+com_lsr)) & LSR_RXRDY) == 0)
736 1.1 cgd ;
737 1.1 cgd c = inb(com+com_data);
738 1.1 cgd stat = inb(com+com_iir);
739 1.1 cgd splx(s);
740 1.12.2.2 mycroft return c;
741 1.1 cgd }
742 1.1 cgd
743 1.1 cgd /*
744 1.1 cgd * Console kernel output character routine.
745 1.1 cgd */
746 1.1 cgd comcnputc(dev, c)
747 1.1 cgd dev_t dev;
748 1.12.2.2 mycroft int c;
749 1.1 cgd {
750 1.12.2.2 mycroft com = com_addr[COMUNIT(dev)];
751 1.12.2.2 mycroft int timo;
752 1.1 cgd short stat;
753 1.1 cgd int s = splhigh();
754 1.1 cgd
755 1.1 cgd if (comconsinit == 0) {
756 1.12.2.2 mycroft (void) cominit(COMUNIT(dev), comdefaultrate);
757 1.1 cgd comconsinit = 1;
758 1.1 cgd }
759 1.1 cgd /* wait for any pending transmission to finish */
760 1.1 cgd timo = 50000;
761 1.1 cgd while (((stat = inb(com+com_lsr)) & LSR_TXRDY) == 0 && --timo)
762 1.1 cgd ;
763 1.1 cgd outb(com+com_data, c);
764 1.1 cgd /* wait for this transmission to complete */
765 1.1 cgd timo = 1500000;
766 1.1 cgd while (((stat = inb(com+com_lsr)) & LSR_TXRDY) == 0 && --timo)
767 1.1 cgd ;
768 1.1 cgd /* clear any interrupts generated by this transmission */
769 1.1 cgd stat = inb(com+com_iir);
770 1.1 cgd splx(s);
771 1.1 cgd }
772 1.3 cgd #endif
773