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