com.c revision 1.26 1 1.1 cgd /*-
2 1.21 mycroft * Copyright (c) 1993, 1994 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.26 cgd * $Id: com.c,v 1.26 1994/03/23 01:28:23 cgd Exp $
36 1.1 cgd */
37 1.1 cgd
38 1.1 cgd /*
39 1.1 cgd * COM driver, based on HP dca driver
40 1.1 cgd * uses National Semiconductor NS16450/NS16550AF UART
41 1.1 cgd */
42 1.21 mycroft #include "com.h"
43 1.26 cgd #include "ast.h"
44 1.21 mycroft
45 1.14 mycroft #include <sys/param.h>
46 1.14 mycroft #include <sys/systm.h>
47 1.14 mycroft #include <sys/ioctl.h>
48 1.14 mycroft #include <sys/select.h>
49 1.14 mycroft #include <sys/tty.h>
50 1.14 mycroft #include <sys/proc.h>
51 1.14 mycroft #include <sys/user.h>
52 1.14 mycroft #include <sys/conf.h>
53 1.14 mycroft #include <sys/file.h>
54 1.14 mycroft #include <sys/uio.h>
55 1.14 mycroft #include <sys/kernel.h>
56 1.14 mycroft #include <sys/syslog.h>
57 1.14 mycroft #include <sys/types.h>
58 1.21 mycroft #include <sys/device.h>
59 1.14 mycroft
60 1.21 mycroft #include <machine/cpu.h>
61 1.14 mycroft #include <machine/pio.h>
62 1.14 mycroft
63 1.14 mycroft #include <i386/isa/isa_device.h>
64 1.14 mycroft #include <i386/isa/comreg.h>
65 1.14 mycroft #include <i386/isa/ic/ns16550.h>
66 1.14 mycroft
67 1.21 mycroft struct com_softc {
68 1.21 mycroft struct device sc_dev;
69 1.1 cgd
70 1.21 mycroft u_short sc_iobase;
71 1.22 cgd u_char sc_hwflags;
72 1.22 cgd #define COM_HW_MULTI 0x01
73 1.22 cgd #define COM_HW_FIFO 0x02
74 1.22 cgd #define COM_HW_CONSOLE 0x40
75 1.22 cgd u_char sc_swflags;
76 1.22 cgd #define COM_SW_SOFTCAR 0x01
77 1.22 cgd #define COM_SW_CLOCAL 0x02
78 1.22 cgd #define COM_SW_CRTSCTS 0x04
79 1.25 cgd #define COM_SW_MDMBUF 0x08
80 1.21 mycroft u_char sc_msr, sc_mcr;
81 1.21 mycroft } com_softc[NCOM];
82 1.21 mycroft /* XXXX should be in com_softc, but not ready for that yet */
83 1.21 mycroft struct tty *com_tty[NCOM];
84 1.21 mycroft
85 1.21 mycroft int comprobe __P((struct isa_device *));
86 1.21 mycroft int comattach __P((struct isa_device *));
87 1.21 mycroft int comopen __P((dev_t, int, int, struct proc *));
88 1.21 mycroft int comclose __P((dev_t, int, int, struct proc *));
89 1.21 mycroft int comintr __P((int));
90 1.21 mycroft int comparam __P((struct tty *, struct termios *));
91 1.21 mycroft void comstart __P((struct tty *));
92 1.1 cgd
93 1.1 cgd struct isa_driver comdriver = {
94 1.1 cgd comprobe, comattach, "com"
95 1.1 cgd };
96 1.1 cgd
97 1.21 mycroft int comdefaultrate = TTYDEF_SPEED;
98 1.1 cgd #ifdef COMCONSOLE
99 1.1 cgd int comconsole = COMCONSOLE;
100 1.1 cgd #else
101 1.1 cgd int comconsole = -1;
102 1.1 cgd #endif
103 1.1 cgd int comconsinit;
104 1.1 cgd int commajor;
105 1.1 cgd
106 1.1 cgd #ifdef KGDB
107 1.14 mycroft #include <machine/remote-sl.h>
108 1.1 cgd extern int kgdb_dev;
109 1.1 cgd extern int kgdb_rate;
110 1.1 cgd extern int kgdb_debug_init;
111 1.1 cgd #endif
112 1.1 cgd
113 1.21 mycroft #define COMUNIT(x) (minor(x))
114 1.1 cgd
115 1.21 mycroft #define bis(c, b) do { const register u_short com_ad = (c); \
116 1.21 mycroft outb(com_ad, inb(com_ad) | (b)); } while(0)
117 1.21 mycroft #define bic(c, b) do { const register u_short com_ad = (c); \
118 1.21 mycroft outb(com_ad, inb(com_ad) & ~(b)); } while(0)
119 1.21 mycroft
120 1.21 mycroft int
121 1.21 mycroft comspeed(speed)
122 1.21 mycroft long speed;
123 1.1 cgd {
124 1.21 mycroft #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
125 1.21 mycroft
126 1.21 mycroft int x, err;
127 1.21 mycroft
128 1.21 mycroft if (speed == 0)
129 1.21 mycroft return 0;
130 1.21 mycroft if (speed < 0)
131 1.21 mycroft return -1;
132 1.21 mycroft x = divrnd((COM_FREQ / 16), speed);
133 1.21 mycroft if (x <= 0)
134 1.21 mycroft return -1;
135 1.21 mycroft err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
136 1.21 mycroft if (err < 0)
137 1.21 mycroft err = -err;
138 1.21 mycroft if (err > COM_TOLERANCE)
139 1.21 mycroft return -1;
140 1.21 mycroft return x;
141 1.21 mycroft
142 1.21 mycroft #undef divrnd(n, q)
143 1.21 mycroft }
144 1.21 mycroft
145 1.21 mycroft int
146 1.21 mycroft comprobe1(iobase)
147 1.21 mycroft u_short iobase;
148 1.21 mycroft {
149 1.21 mycroft
150 1.1 cgd /* force access to id reg */
151 1.21 mycroft outb(iobase + com_cfcr, 0);
152 1.21 mycroft outb(iobase + com_iir, 0);
153 1.21 mycroft if (inb(iobase + com_iir) & 0x38)
154 1.21 mycroft return 0;
155 1.21 mycroft
156 1.21 mycroft return 1;
157 1.1 cgd }
158 1.1 cgd
159 1.21 mycroft int
160 1.21 mycroft comprobe(isa_dev)
161 1.21 mycroft struct isa_device *isa_dev;
162 1.21 mycroft {
163 1.21 mycroft struct com_softc *sc = &com_softc[isa_dev->id_unit];
164 1.21 mycroft u_short iobase = isa_dev->id_iobase;
165 1.21 mycroft
166 1.26 cgd if (isa_dev->id_parent) {
167 1.26 cgd if (iobase == 0) {
168 1.26 cgd /*
169 1.26 cgd * For multiport cards, the iobase may be left
170 1.26 cgd * unspecified (zero) for slave ports. In
171 1.26 cgd * that case we calculate it from the master
172 1.26 cgd * (parent) iobase setting and the slave port
173 1.26 cgd * number (physid).
174 1.26 cgd */
175 1.26 cgd iobase = isa_dev->id_iobase
176 1.26 cgd = isa_dev->id_parent->id_iobase +
177 1.26 cgd (8 * isa_dev->id_physid);
178 1.26 cgd }
179 1.26 cgd }
180 1.26 cgd
181 1.21 mycroft /* XXX HACK */
182 1.21 mycroft sprintf(sc->sc_dev.dv_xname, "%s%d", comdriver.name, isa_dev->id_unit);
183 1.21 mycroft sc->sc_dev.dv_unit = isa_dev->id_unit;
184 1.21 mycroft
185 1.21 mycroft if (!comprobe1(iobase))
186 1.21 mycroft return 0;
187 1.21 mycroft
188 1.21 mycroft return COM_NPORTS;
189 1.21 mycroft }
190 1.1 cgd
191 1.1 cgd int
192 1.21 mycroft comattach(isa_dev)
193 1.21 mycroft struct isa_device *isa_dev;
194 1.1 cgd {
195 1.21 mycroft int unit = isa_dev->id_unit;
196 1.21 mycroft struct com_softc *sc = &com_softc[unit];
197 1.21 mycroft u_short iobase = isa_dev->id_iobase;
198 1.21 mycroft struct tty *tp;
199 1.1 cgd
200 1.1 cgd if (unit == comconsole)
201 1.20 mycroft delay(1000);
202 1.21 mycroft
203 1.21 mycroft sc->sc_iobase = iobase;
204 1.26 cgd sc->sc_hwflags = 0;
205 1.22 cgd sc->sc_swflags = 0;
206 1.21 mycroft
207 1.21 mycroft printf("%s: ", sc->sc_dev.dv_xname);
208 1.1 cgd
209 1.26 cgd printf("%s", sc->sc_dev.dv_xname);
210 1.26 cgd #if NAST > 0
211 1.26 cgd if (isa_dev->id_parent) {
212 1.26 cgd printf(" at 0x%x %s%d slave %d",
213 1.26 cgd isa_dev->id_iobase,
214 1.26 cgd isa_dev->id_parent->id_driver->name,
215 1.26 cgd isa_dev->id_parent->id_unit,
216 1.26 cgd isa_dev->id_physid);
217 1.26 cgd astslave(isa_dev, unit);
218 1.26 cgd sc->sc_hwflags |= COM_HW_MULTI;
219 1.26 cgd }
220 1.26 cgd #endif
221 1.26 cgd printf(": ");
222 1.26 cgd
223 1.1 cgd /* look for a NS 16550AF UART with FIFOs */
224 1.21 mycroft outb(iobase + com_fifo,
225 1.21 mycroft FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
226 1.20 mycroft delay(100);
227 1.21 mycroft if ((inb(iobase + com_iir) & IIR_FIFO_MASK) == IIR_FIFO_MASK)
228 1.21 mycroft if ((inb(iobase + com_fifo) & FIFO_TRIGGER_14) == FIFO_TRIGGER_14) {
229 1.22 cgd sc->sc_hwflags |= COM_HW_FIFO;
230 1.21 mycroft printf("ns16550a, working fifo\n");
231 1.21 mycroft } else
232 1.21 mycroft printf("ns82550 or ns16550, broken fifo\n");
233 1.21 mycroft else
234 1.21 mycroft printf("ns82450 or ns16450, no fifo\n");
235 1.21 mycroft outb(iobase + com_fifo, 0);
236 1.21 mycroft
237 1.21 mycroft /* disable interrupts */
238 1.21 mycroft outb(iobase + com_ier, 0);
239 1.21 mycroft outb(iobase + com_mcr, 0);
240 1.1 cgd
241 1.1 cgd #ifdef KGDB
242 1.2 cgd if (kgdb_dev == makedev(commajor, unit)) {
243 1.1 cgd if (comconsole == unit)
244 1.1 cgd kgdb_dev = -1; /* can't debug over console port */
245 1.1 cgd else {
246 1.1 cgd (void) cominit(unit, kgdb_rate);
247 1.1 cgd if (kgdb_debug_init) {
248 1.1 cgd /*
249 1.1 cgd * Print prefix of device name,
250 1.1 cgd * let kgdb_connect print the rest.
251 1.1 cgd */
252 1.21 mycroft printf("%s: ", sc->sc_dev.dv_xname);
253 1.1 cgd kgdb_connect(1);
254 1.1 cgd } else
255 1.21 mycroft printf("%s: kgdb enabled\n",
256 1.21 mycroft sc->sc_dev.dv_xname);
257 1.1 cgd }
258 1.1 cgd }
259 1.1 cgd #endif
260 1.21 mycroft
261 1.1 cgd /*
262 1.1 cgd * Need to reset baud rate, etc. of next print so reset comconsinit.
263 1.21 mycroft * Also make sure console is always "hardwired".
264 1.1 cgd */
265 1.1 cgd if (unit == comconsole) {
266 1.1 cgd comconsinit = 0;
267 1.22 cgd sc->sc_hwflags |= COM_HW_CONSOLE;
268 1.22 cgd sc->sc_swflags |= COM_SW_SOFTCAR;
269 1.1 cgd }
270 1.1 cgd }
271 1.1 cgd
272 1.21 mycroft int
273 1.21 mycroft comopen(dev, flag, mode, p)
274 1.21 mycroft dev_t dev;
275 1.21 mycroft int flag, mode;
276 1.21 mycroft struct proc *p;
277 1.1 cgd {
278 1.21 mycroft int unit = COMUNIT(dev);
279 1.21 mycroft struct com_softc *sc;
280 1.21 mycroft u_short iobase;
281 1.21 mycroft struct tty *tp;
282 1.21 mycroft int s;
283 1.1 cgd int error = 0;
284 1.1 cgd
285 1.21 mycroft if (unit > NCOM)
286 1.21 mycroft return ENXIO;
287 1.21 mycroft sc = &com_softc[unit];
288 1.21 mycroft if (!sc->sc_iobase)
289 1.21 mycroft return ENXIO;
290 1.21 mycroft
291 1.21 mycroft s = spltty();
292 1.21 mycroft
293 1.21 mycroft if (!com_tty[unit])
294 1.11 mycroft tp = com_tty[unit] = ttymalloc();
295 1.21 mycroft else
296 1.8 deraadt tp = com_tty[unit];
297 1.21 mycroft
298 1.1 cgd tp->t_oproc = comstart;
299 1.1 cgd tp->t_param = comparam;
300 1.1 cgd tp->t_dev = dev;
301 1.1 cgd if ((tp->t_state & TS_ISOPEN) == 0) {
302 1.1 cgd tp->t_state |= TS_WOPEN;
303 1.1 cgd ttychars(tp);
304 1.16 ws tp->t_iflag = TTYDEF_IFLAG;
305 1.16 ws tp->t_oflag = TTYDEF_OFLAG;
306 1.16 ws tp->t_cflag = TTYDEF_CFLAG;
307 1.22 cgd if (sc->sc_swflags & COM_SW_CLOCAL)
308 1.22 cgd tp->t_cflag |= CLOCAL;
309 1.22 cgd if (sc->sc_swflags & COM_SW_CRTSCTS)
310 1.22 cgd tp->t_cflag |= CRTSCTS;
311 1.25 cgd if (sc->sc_swflags & COM_SW_MDMBUF)
312 1.25 cgd tp->t_cflag |= MDMBUF;
313 1.16 ws tp->t_lflag = TTYDEF_LFLAG;
314 1.16 ws tp->t_ispeed = tp->t_ospeed = comdefaultrate;
315 1.1 cgd comparam(tp, &tp->t_termios);
316 1.1 cgd ttsetwater(tp);
317 1.21 mycroft
318 1.21 mycroft iobase = sc->sc_iobase;
319 1.21 mycroft /* flush any pending I/O */
320 1.22 cgd if (sc->sc_hwflags & COM_HW_FIFO)
321 1.21 mycroft outb(iobase + com_fifo,
322 1.21 mycroft FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
323 1.21 mycroft FIFO_TRIGGER_8);
324 1.21 mycroft (void) inb(iobase + com_lsr);
325 1.21 mycroft (void) inb(iobase + com_data);
326 1.21 mycroft /* you turn me on, baby */
327 1.26 cgd sc->sc_mcr = MCR_DTR | MCR_RTS;
328 1.26 cgd if (!(sc->sc_hwflags & COM_HW_MULTI))
329 1.26 cgd sc->sc_mcr |= MCR_IENABLE;
330 1.26 cgd outb(iobase + com_mcr, sc->sc_mcr);
331 1.21 mycroft outb(iobase + com_ier,
332 1.21 mycroft IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC);
333 1.21 mycroft
334 1.21 mycroft sc->sc_msr = inb(iobase + com_msr);
335 1.25 cgd if (sc->sc_swflags & COM_SW_SOFTCAR || sc->sc_msr & MSR_DCD ||
336 1.25 cgd tp->t_lflag&MDMBUF)
337 1.21 mycroft tp->t_state |= TS_CARR_ON;
338 1.21 mycroft else
339 1.21 mycroft tp->t_state &= ~TS_CARR_ON;
340 1.21 mycroft } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0) {
341 1.21 mycroft splx(s);
342 1.21 mycroft return EBUSY;
343 1.21 mycroft }
344 1.21 mycroft
345 1.21 mycroft /* wait for carrier if necessary */
346 1.21 mycroft if ((flag & O_NONBLOCK) == 0)
347 1.21 mycroft while ((tp->t_cflag & CLOCAL) == 0 &&
348 1.21 mycroft (tp->t_state & TS_CARR_ON) == 0) {
349 1.21 mycroft tp->t_state |= TS_WOPEN;
350 1.21 mycroft error = ttysleep(tp, (caddr_t)&tp->t_rawq,
351 1.21 mycroft TTIPRI | PCATCH, ttopen, 0);
352 1.21 mycroft if (error) {
353 1.21 mycroft /* XXX should turn off chip if we're the
354 1.21 mycroft only waiter */
355 1.21 mycroft splx(s);
356 1.21 mycroft return error;
357 1.21 mycroft }
358 1.21 mycroft }
359 1.21 mycroft splx(s);
360 1.21 mycroft
361 1.21 mycroft return (*linesw[tp->t_line].l_open)(dev, tp);
362 1.1 cgd }
363 1.1 cgd
364 1.21 mycroft int
365 1.1 cgd comclose(dev, flag, mode, p)
366 1.1 cgd dev_t dev;
367 1.1 cgd int flag, mode;
368 1.1 cgd struct proc *p;
369 1.1 cgd {
370 1.21 mycroft int unit = COMUNIT(dev);
371 1.21 mycroft struct com_softc *sc = &com_softc[unit];
372 1.21 mycroft u_short iobase = sc->sc_iobase;
373 1.21 mycroft struct tty *tp = com_tty[unit];
374 1.21 mycroft
375 1.1 cgd (*linesw[tp->t_line].l_close)(tp, flag);
376 1.1 cgd #ifdef KGDB
377 1.1 cgd /* do not disable interrupts if debugging */
378 1.2 cgd if (kgdb_dev != makedev(commajor, unit))
379 1.1 cgd #endif
380 1.21 mycroft {
381 1.21 mycroft bic(iobase + com_cfcr, CFCR_SBREAK);
382 1.21 mycroft outb(iobase + com_ier, 0);
383 1.22 cgd if (tp->t_cflag & HUPCL &&
384 1.22 cgd (sc->sc_swflags & COM_SW_SOFTCAR) == 0)
385 1.21 mycroft /* XXX perhaps only clear DTR */
386 1.21 mycroft outb(iobase + com_mcr, 0);
387 1.21 mycroft }
388 1.1 cgd ttyclose(tp);
389 1.21 mycroft #ifdef notyet /* XXXX */
390 1.21 mycroft if (unit != comconsole) {
391 1.21 mycroft ttyfree(tp);
392 1.21 mycroft com_tty[unit] = (struct tty *)NULL;
393 1.21 mycroft }
394 1.13 cgd #endif
395 1.21 mycroft return 0;
396 1.1 cgd }
397 1.1 cgd
398 1.21 mycroft int
399 1.1 cgd comread(dev, uio, flag)
400 1.1 cgd dev_t dev;
401 1.1 cgd struct uio *uio;
402 1.21 mycroft int flag;
403 1.1 cgd {
404 1.21 mycroft struct tty *tp = com_tty[COMUNIT(dev)];
405 1.1 cgd
406 1.1 cgd return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
407 1.1 cgd }
408 1.1 cgd
409 1.21 mycroft int
410 1.1 cgd comwrite(dev, uio, flag)
411 1.1 cgd dev_t dev;
412 1.1 cgd struct uio *uio;
413 1.21 mycroft int flag;
414 1.1 cgd {
415 1.21 mycroft struct tty *tp = com_tty[COMUNIT(dev)];
416 1.1 cgd
417 1.1 cgd return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
418 1.1 cgd }
419 1.1 cgd
420 1.21 mycroft static u_char
421 1.21 mycroft tiocm_xxx2mcr(data)
422 1.21 mycroft int data;
423 1.21 mycroft {
424 1.21 mycroft u_char m = 0;
425 1.21 mycroft
426 1.21 mycroft if (data & TIOCM_DTR)
427 1.21 mycroft m |= MCR_DTR;
428 1.21 mycroft if (data & TIOCM_RTS)
429 1.21 mycroft m |= MCR_RTS;
430 1.21 mycroft return m;
431 1.1 cgd }
432 1.1 cgd
433 1.21 mycroft int
434 1.19 mycroft comioctl(dev, cmd, data, flag, p)
435 1.1 cgd dev_t dev;
436 1.19 mycroft int cmd;
437 1.1 cgd caddr_t data;
438 1.19 mycroft int flag;
439 1.19 mycroft struct proc *p;
440 1.1 cgd {
441 1.21 mycroft int unit = COMUNIT(dev);
442 1.21 mycroft struct com_softc *sc = &com_softc[unit];
443 1.21 mycroft u_short iobase = sc->sc_iobase;
444 1.21 mycroft struct tty *tp = com_tty[unit];
445 1.21 mycroft int error;
446 1.21 mycroft
447 1.19 mycroft error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
448 1.1 cgd if (error >= 0)
449 1.21 mycroft return error;
450 1.19 mycroft error = ttioctl(tp, cmd, data, flag, p);
451 1.1 cgd if (error >= 0)
452 1.21 mycroft return error;
453 1.1 cgd
454 1.1 cgd switch (cmd) {
455 1.1 cgd case TIOCSBRK:
456 1.21 mycroft bis(iobase + com_cfcr, CFCR_SBREAK);
457 1.1 cgd break;
458 1.1 cgd case TIOCCBRK:
459 1.21 mycroft bic(iobase + com_cfcr, CFCR_SBREAK);
460 1.1 cgd break;
461 1.1 cgd case TIOCSDTR:
462 1.21 mycroft outb(iobase + com_mcr, sc->sc_mcr |= (MCR_DTR | MCR_RTS));
463 1.1 cgd break;
464 1.1 cgd case TIOCCDTR:
465 1.21 mycroft outb(iobase + com_mcr, sc->sc_mcr &= ~(MCR_DTR | MCR_RTS));
466 1.1 cgd break;
467 1.1 cgd case TIOCMSET:
468 1.21 mycroft sc->sc_mcr &= ~(MCR_DTR | MCR_RTS);
469 1.1 cgd case TIOCMBIS:
470 1.21 mycroft outb(iobase + com_mcr,
471 1.21 mycroft sc->sc_mcr |= tiocm_xxx2mcr(*(int *)data));
472 1.1 cgd break;
473 1.1 cgd case TIOCMBIC:
474 1.21 mycroft outb(iobase + com_mcr,
475 1.21 mycroft sc->sc_mcr &= ~tiocm_xxx2mcr(*(int *)data));
476 1.1 cgd break;
477 1.21 mycroft case TIOCMGET: {
478 1.21 mycroft u_char m;
479 1.21 mycroft int bits = 0;
480 1.21 mycroft
481 1.21 mycroft m = sc->sc_mcr;
482 1.21 mycroft if (m & MCR_DTR)
483 1.21 mycroft bits |= TIOCM_DTR;
484 1.21 mycroft if (m & MCR_RTS)
485 1.21 mycroft bits |= TIOCM_RTS;
486 1.21 mycroft m = sc->sc_msr;
487 1.21 mycroft if (m & MSR_DCD)
488 1.21 mycroft bits |= TIOCM_CD;
489 1.21 mycroft if (m & MSR_CTS)
490 1.21 mycroft bits |= TIOCM_CTS;
491 1.21 mycroft if (m & MSR_DSR)
492 1.21 mycroft bits |= TIOCM_DSR;
493 1.21 mycroft if (m & (MSR_RI | MSR_TERI))
494 1.21 mycroft bits |= TIOCM_RI;
495 1.21 mycroft if (inb(iobase + com_ier))
496 1.21 mycroft bits |= TIOCM_LE;
497 1.21 mycroft *(int *)data = bits;
498 1.1 cgd break;
499 1.21 mycroft }
500 1.22 cgd case TIOCGFLAGS: {
501 1.22 cgd int bits = 0;
502 1.22 cgd
503 1.22 cgd if (sc->sc_swflags & COM_SW_SOFTCAR)
504 1.22 cgd bits |= TIOCFLAG_SOFTCAR;
505 1.22 cgd if (sc->sc_swflags & COM_SW_CLOCAL)
506 1.22 cgd bits |= TIOCFLAG_CLOCAL;
507 1.22 cgd if (sc->sc_swflags & COM_SW_CRTSCTS)
508 1.22 cgd bits |= TIOCFLAG_CRTSCTS;
509 1.25 cgd if (sc->sc_swflags & COM_SW_MDMBUF)
510 1.25 cgd bits |= TIOCFLAG_MDMBUF;
511 1.22 cgd
512 1.22 cgd *(int *)data = bits;
513 1.22 cgd break;
514 1.22 cgd }
515 1.22 cgd case TIOCSFLAGS: {
516 1.22 cgd int userbits, driverbits = 0;
517 1.22 cgd
518 1.23 cgd error = suser(p->p_ucred, &p->p_acflag);
519 1.23 cgd if (error != 0)
520 1.23 cgd return(EPERM);
521 1.22 cgd
522 1.22 cgd userbits = *(int *)data;
523 1.23 cgd if ((userbits & TIOCFLAG_SOFTCAR) ||
524 1.22 cgd (sc->sc_hwflags & COM_HW_CONSOLE))
525 1.23 cgd driverbits |= COM_SW_SOFTCAR;
526 1.22 cgd if (userbits & TIOCFLAG_CLOCAL)
527 1.22 cgd driverbits |= COM_SW_CLOCAL;
528 1.22 cgd if (userbits & TIOCFLAG_CRTSCTS)
529 1.22 cgd driverbits |= COM_SW_CRTSCTS;
530 1.22 cgd
531 1.23 cgd sc->sc_swflags = driverbits;
532 1.22 cgd break;
533 1.22 cgd }
534 1.1 cgd default:
535 1.21 mycroft return ENOTTY;
536 1.1 cgd }
537 1.21 mycroft
538 1.21 mycroft return 0;
539 1.1 cgd }
540 1.1 cgd
541 1.21 mycroft int
542 1.1 cgd comparam(tp, t)
543 1.21 mycroft struct tty *tp;
544 1.21 mycroft struct termios *t;
545 1.1 cgd {
546 1.21 mycroft struct com_softc *sc = &com_softc[COMUNIT(tp->t_dev)];
547 1.21 mycroft u_short iobase = sc->sc_iobase;
548 1.21 mycroft int ospeed = comspeed(t->c_ospeed);
549 1.21 mycroft u_char cfcr;
550 1.21 mycroft int s;
551 1.21 mycroft
552 1.1 cgd /* check requested parameters */
553 1.21 mycroft if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
554 1.21 mycroft return EINVAL;
555 1.21 mycroft
556 1.21 mycroft switch (t->c_cflag & CSIZE) {
557 1.1 cgd case CS5:
558 1.21 mycroft cfcr = CFCR_5BITS;
559 1.21 mycroft break;
560 1.1 cgd case CS6:
561 1.21 mycroft cfcr = CFCR_6BITS;
562 1.21 mycroft break;
563 1.1 cgd case CS7:
564 1.21 mycroft cfcr = CFCR_7BITS;
565 1.21 mycroft break;
566 1.1 cgd case CS8:
567 1.21 mycroft cfcr = CFCR_8BITS;
568 1.21 mycroft break;
569 1.1 cgd }
570 1.21 mycroft if (t->c_cflag & PARENB) {
571 1.1 cgd cfcr |= CFCR_PENAB;
572 1.21 mycroft if ((t->c_cflag & PARODD) == 0)
573 1.1 cgd cfcr |= CFCR_PEVEN;
574 1.1 cgd }
575 1.21 mycroft if (t->c_cflag & CSTOPB)
576 1.1 cgd cfcr |= CFCR_STOPB;
577 1.1 cgd
578 1.21 mycroft s = spltty();
579 1.21 mycroft
580 1.21 mycroft /* and copy to tty */
581 1.21 mycroft tp->t_ispeed = t->c_ispeed;
582 1.21 mycroft tp->t_ospeed = t->c_ospeed;
583 1.21 mycroft tp->t_cflag = t->c_cflag;
584 1.21 mycroft
585 1.21 mycroft if (ospeed == 0)
586 1.21 mycroft outb(iobase + com_mcr, sc->sc_mcr &= ~MCR_DTR);
587 1.21 mycroft else
588 1.21 mycroft outb(iobase + com_mcr, sc->sc_mcr |= MCR_DTR);
589 1.21 mycroft
590 1.21 mycroft outb(iobase + com_cfcr, cfcr | CFCR_DLAB);
591 1.21 mycroft outb(iobase + com_dlbl, ospeed);
592 1.21 mycroft outb(iobase + com_dlbh, ospeed>>8);
593 1.21 mycroft outb(iobase + com_cfcr, cfcr);
594 1.21 mycroft
595 1.22 cgd /* When not using CRTSCTS, RTS follows DTR. */
596 1.22 cgd if ((t->c_cflag & CRTSCTS) == 0) {
597 1.21 mycroft if (sc->sc_mcr & MCR_DTR) {
598 1.21 mycroft if ((sc->sc_mcr & MCR_RTS) == 0)
599 1.21 mycroft outb(iobase + com_mcr, sc->sc_mcr |= MCR_RTS);
600 1.21 mycroft } else {
601 1.21 mycroft if (sc->sc_mcr & MCR_RTS)
602 1.21 mycroft outb(iobase + com_mcr, sc->sc_mcr &= ~MCR_RTS);
603 1.21 mycroft }
604 1.21 mycroft }
605 1.21 mycroft
606 1.25 cgd /*
607 1.25 cgd * If CTS is off and CRTSCTS is changed, we must toggle TS_TTSTOP.
608 1.25 cgd * XXX should be done at tty layer.
609 1.25 cgd */
610 1.21 mycroft if ((sc->sc_msr & MSR_CTS) == 0 &&
611 1.22 cgd (tp->t_cflag & CRTSCTS) != (t->c_cflag & CRTSCTS)) {
612 1.22 cgd if ((t->c_cflag & CRTSCTS) == 0) {
613 1.25 cgd tp->t_state &= ~TS_TTSTOP;
614 1.25 cgd ttstart(tp);
615 1.25 cgd } else
616 1.25 cgd tp->t_state |= TS_TTSTOP;
617 1.25 cgd }
618 1.25 cgd
619 1.25 cgd /*
620 1.25 cgd * If DCD is off and MDMBUF is changed, we must toggle TS_TTSTOP.
621 1.25 cgd * XXX should be done at tty layer.
622 1.25 cgd */
623 1.25 cgd if ((sc->sc_swflags & COM_SW_SOFTCAR) == 0 &&
624 1.25 cgd (sc->sc_msr & MSR_DCD) == 0 &&
625 1.25 cgd (tp->t_cflag & MDMBUF) != (t->c_cflag & MDMBUF)) {
626 1.25 cgd if ((t->c_cflag & MDMBUF) == 0) {
627 1.21 mycroft tp->t_state &= ~TS_TTSTOP;
628 1.21 mycroft ttstart(tp);
629 1.21 mycroft } else
630 1.21 mycroft tp->t_state |= TS_TTSTOP;
631 1.21 mycroft }
632 1.1 cgd
633 1.21 mycroft splx(s);
634 1.21 mycroft return 0;
635 1.1 cgd }
636 1.21 mycroft
637 1.12 deraadt void
638 1.1 cgd comstart(tp)
639 1.21 mycroft struct tty *tp;
640 1.1 cgd {
641 1.21 mycroft struct com_softc *sc = &com_softc[COMUNIT(tp->t_dev)];
642 1.21 mycroft u_short iobase = sc->sc_iobase;
643 1.21 mycroft int s;
644 1.21 mycroft
645 1.1 cgd s = spltty();
646 1.21 mycroft if (tp->t_state & (TS_TTSTOP | TS_BUSY))
647 1.21 mycroft goto out;
648 1.21 mycroft #if 0 /* XXXX I think this is handled adequately by commint() and comparam(). */
649 1.22 cgd if (tp->t_cflag & CRTSCTS && (sc->sc_mcr & MSR_CTS) == 0)
650 1.1 cgd goto out;
651 1.21 mycroft #endif
652 1.11 mycroft if (tp->t_outq.c_cc <= tp->t_lowat) {
653 1.21 mycroft if (tp->t_state & TS_ASLEEP) {
654 1.1 cgd tp->t_state &= ~TS_ASLEEP;
655 1.11 mycroft wakeup((caddr_t)&tp->t_outq);
656 1.1 cgd }
657 1.7 cgd selwakeup(&tp->t_wsel);
658 1.1 cgd }
659 1.11 mycroft if (tp->t_outq.c_cc == 0)
660 1.1 cgd goto out;
661 1.21 mycroft tp->t_state |= TS_BUSY;
662 1.21 mycroft if ((inb(iobase + com_lsr) & LSR_TXRDY) == 0)
663 1.21 mycroft goto out;
664 1.22 cgd if (sc->sc_hwflags & COM_HW_FIFO) {
665 1.21 mycroft u_char buffer[16], *cp = buffer;
666 1.21 mycroft int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
667 1.21 mycroft do {
668 1.21 mycroft outb(iobase + com_data, *cp++);
669 1.21 mycroft } while (--n);
670 1.21 mycroft } else
671 1.21 mycroft outb(iobase + com_data, getc(&tp->t_outq));
672 1.1 cgd out:
673 1.1 cgd splx(s);
674 1.1 cgd }
675 1.21 mycroft
676 1.1 cgd /*
677 1.1 cgd * Stop output on a line.
678 1.1 cgd */
679 1.21 mycroft void
680 1.1 cgd comstop(tp, flag)
681 1.21 mycroft struct tty *tp;
682 1.1 cgd {
683 1.21 mycroft int s;
684 1.1 cgd
685 1.1 cgd s = spltty();
686 1.21 mycroft if (tp->t_state & TS_BUSY)
687 1.21 mycroft if ((tp->t_state & TS_TTSTOP) == 0)
688 1.1 cgd tp->t_state |= TS_FLUSH;
689 1.1 cgd splx(s);
690 1.1 cgd }
691 1.1 cgd
692 1.21 mycroft static inline void
693 1.21 mycroft comeint(sc, stat)
694 1.21 mycroft struct com_softc *sc;
695 1.21 mycroft int stat;
696 1.21 mycroft {
697 1.21 mycroft u_short iobase = sc->sc_iobase;
698 1.21 mycroft int unit = sc->sc_dev.dv_unit;
699 1.21 mycroft struct tty *tp = com_tty[unit];
700 1.21 mycroft int c;
701 1.1 cgd
702 1.21 mycroft c = inb(iobase + com_data);
703 1.21 mycroft if ((tp->t_state & TS_ISOPEN) == 0) {
704 1.21 mycroft #ifdef KGDB
705 1.21 mycroft /* we don't care about parity errors */
706 1.21 mycroft if (((stat & (LSR_BI | LSR_FE | LSR_PE)) == LSR_PE) &&
707 1.21 mycroft kgdb_dev == makedev(commajor, unit) && c == FRAME_END)
708 1.21 mycroft kgdb_connect(0); /* trap into kgdb */
709 1.21 mycroft #endif
710 1.21 mycroft return;
711 1.21 mycroft }
712 1.21 mycroft if (stat & (LSR_BI | LSR_FE))
713 1.21 mycroft c |= TTY_FE;
714 1.21 mycroft else if (stat & LSR_PE)
715 1.21 mycroft c |= TTY_PE;
716 1.21 mycroft if (stat & LSR_OE)
717 1.21 mycroft log(LOG_WARNING, "%s: silo overflow\n", sc->sc_dev.dv_xname);
718 1.21 mycroft /* XXXX put in FIFO and process later */
719 1.21 mycroft (*linesw[tp->t_line].l_rint)(c, tp);
720 1.21 mycroft }
721 1.21 mycroft
722 1.21 mycroft static inline void
723 1.21 mycroft commint(sc)
724 1.21 mycroft struct com_softc *sc;
725 1.21 mycroft {
726 1.21 mycroft u_short iobase = sc->sc_iobase;
727 1.21 mycroft struct tty *tp = com_tty[sc->sc_dev.dv_unit];
728 1.21 mycroft u_char msr, delta;
729 1.21 mycroft
730 1.21 mycroft msr = inb(iobase + com_msr);
731 1.21 mycroft delta = msr ^ sc->sc_msr;
732 1.21 mycroft sc->sc_msr = msr;
733 1.1 cgd
734 1.22 cgd if (delta & MSR_DCD && (sc->sc_swflags & COM_SW_SOFTCAR) == 0) {
735 1.21 mycroft if (msr & MSR_DCD)
736 1.21 mycroft (void)(*linesw[tp->t_line].l_modem)(tp, 1);
737 1.21 mycroft else if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0)
738 1.21 mycroft outb(iobase + com_mcr,
739 1.21 mycroft sc->sc_mcr &= ~(MCR_DTR | MCR_RTS));
740 1.21 mycroft }
741 1.22 cgd if (delta & MSR_CTS && tp->t_cflag & CRTSCTS) {
742 1.21 mycroft /* the line is up and we want to do rts/cts flow control */
743 1.21 mycroft if (msr & MSR_CTS) {
744 1.21 mycroft tp->t_state &= ~TS_TTSTOP;
745 1.21 mycroft ttstart(tp);
746 1.21 mycroft } else
747 1.21 mycroft tp->t_state |= TS_TTSTOP;
748 1.21 mycroft }
749 1.21 mycroft }
750 1.1 cgd
751 1.21 mycroft int
752 1.21 mycroft comintr(unit)
753 1.21 mycroft int unit;
754 1.21 mycroft {
755 1.21 mycroft struct com_softc *sc = &com_softc[unit];
756 1.21 mycroft u_short iobase = sc->sc_iobase;
757 1.21 mycroft struct tty *tp;
758 1.21 mycroft u_char code;
759 1.21 mycroft
760 1.21 mycroft code = inb(iobase + com_iir) & IIR_IMASK;
761 1.21 mycroft if (code & IIR_NOPEND)
762 1.21 mycroft return 0;
763 1.21 mycroft
764 1.21 mycroft for (;;) {
765 1.21 mycroft if (code & IIR_RXRDY) {
766 1.21 mycroft tp = com_tty[sc->sc_dev.dv_unit];
767 1.21 mycroft /* XXXX put in FIFO and process later */
768 1.21 mycroft while (code = (inb(iobase + com_lsr) & LSR_RCV_MASK)) {
769 1.21 mycroft if (code == LSR_RXRDY) {
770 1.21 mycroft code = inb(iobase + com_data);
771 1.21 mycroft if (tp->t_state & TS_ISOPEN)
772 1.21 mycroft (*linesw[tp->t_line].l_rint)(code, tp);
773 1.21 mycroft #ifdef KGDB
774 1.21 mycroft else {
775 1.21 mycroft if (kgdb_dev == makedev(commajor, unit) &&
776 1.21 mycroft code == FRAME_END)
777 1.21 mycroft kgdb_connect(0);
778 1.21 mycroft }
779 1.21 mycroft #endif
780 1.21 mycroft } else
781 1.21 mycroft comeint(sc, code);
782 1.21 mycroft }
783 1.21 mycroft } else if (code == IIR_TXRDY) {
784 1.21 mycroft tp = com_tty[sc->sc_dev.dv_unit];
785 1.21 mycroft tp->t_state &= ~TS_BUSY;
786 1.21 mycroft if (tp->t_state & TS_FLUSH)
787 1.21 mycroft tp->t_state &= ~TS_FLUSH;
788 1.21 mycroft else
789 1.21 mycroft if (tp->t_line)
790 1.21 mycroft (*linesw[tp->t_line].l_start)(tp);
791 1.21 mycroft else
792 1.21 mycroft comstart(tp);
793 1.21 mycroft } else if (code == IIR_MLSC) {
794 1.21 mycroft commint(sc);
795 1.21 mycroft } else {
796 1.21 mycroft log(LOG_WARNING, "%s: weird interrupt: iir=0x%02x\n",
797 1.21 mycroft sc->sc_dev.dv_xname, code);
798 1.21 mycroft }
799 1.21 mycroft code = inb(iobase + com_iir) & IIR_IMASK;
800 1.21 mycroft if (code & IIR_NOPEND)
801 1.21 mycroft return 1;
802 1.1 cgd }
803 1.1 cgd }
804 1.1 cgd
805 1.1 cgd /*
806 1.1 cgd * Following are all routines needed for COM to act as console
807 1.1 cgd */
808 1.17 cgd #include <dev/cons.h>
809 1.1 cgd
810 1.1 cgd comcnprobe(cp)
811 1.1 cgd struct consdev *cp;
812 1.1 cgd {
813 1.21 mycroft int unit = CONUNIT;
814 1.21 mycroft
815 1.21 mycroft if (!comprobe1(CONADDR)) {
816 1.21 mycroft cp->cn_pri = CN_DEAD;
817 1.21 mycroft return;
818 1.21 mycroft }
819 1.1 cgd
820 1.1 cgd /* locate the major number */
821 1.1 cgd for (commajor = 0; commajor < nchrdev; commajor++)
822 1.1 cgd if (cdevsw[commajor].d_open == comopen)
823 1.1 cgd break;
824 1.1 cgd
825 1.21 mycroft com_softc[unit].sc_iobase = CONADDR;
826 1.1 cgd
827 1.1 cgd /* initialize required fields */
828 1.2 cgd cp->cn_dev = makedev(commajor, unit);
829 1.1 cgd #ifdef COMCONSOLE
830 1.1 cgd cp->cn_pri = CN_REMOTE; /* Force a serial port console */
831 1.1 cgd #else
832 1.1 cgd cp->cn_pri = CN_NORMAL;
833 1.1 cgd #endif
834 1.1 cgd }
835 1.1 cgd
836 1.1 cgd comcninit(cp)
837 1.1 cgd struct consdev *cp;
838 1.1 cgd {
839 1.21 mycroft int unit = CONUNIT;
840 1.1 cgd
841 1.1 cgd cominit(unit, comdefaultrate);
842 1.1 cgd comconsole = unit;
843 1.21 mycroft comconsinit = 0;
844 1.1 cgd }
845 1.1 cgd
846 1.1 cgd cominit(unit, rate)
847 1.1 cgd int unit, rate;
848 1.1 cgd {
849 1.21 mycroft int s = splhigh();
850 1.21 mycroft u_short iobase = com_softc[unit].sc_iobase;
851 1.21 mycroft u_char stat;
852 1.1 cgd
853 1.21 mycroft outb(iobase + com_cfcr, CFCR_DLAB);
854 1.21 mycroft rate = comspeed(comdefaultrate);
855 1.21 mycroft outb(iobase + com_dlbl, rate);
856 1.21 mycroft outb(iobase + com_dlbh, rate >> 8);
857 1.21 mycroft outb(iobase + com_cfcr, CFCR_8BITS);
858 1.21 mycroft outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY);
859 1.21 mycroft outb(iobase + com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_4);
860 1.21 mycroft stat = inb(iobase + com_iir);
861 1.1 cgd splx(s);
862 1.1 cgd }
863 1.1 cgd
864 1.1 cgd comcngetc(dev)
865 1.21 mycroft dev_t dev;
866 1.1 cgd {
867 1.21 mycroft int s = splhigh();
868 1.21 mycroft u_short iobase = com_softc[COMUNIT(dev)].sc_iobase;
869 1.21 mycroft u_char stat, c;
870 1.1 cgd
871 1.21 mycroft while (((stat = inb(iobase + com_lsr)) & LSR_RXRDY) == 0)
872 1.1 cgd ;
873 1.21 mycroft c = inb(iobase + com_data);
874 1.21 mycroft stat = inb(iobase + com_iir);
875 1.1 cgd splx(s);
876 1.21 mycroft return c;
877 1.1 cgd }
878 1.1 cgd
879 1.1 cgd /*
880 1.1 cgd * Console kernel output character routine.
881 1.1 cgd */
882 1.1 cgd comcnputc(dev, c)
883 1.1 cgd dev_t dev;
884 1.21 mycroft int c;
885 1.1 cgd {
886 1.21 mycroft int s = splhigh();
887 1.21 mycroft u_short iobase = com_softc[COMUNIT(dev)].sc_iobase;
888 1.21 mycroft u_char stat;
889 1.1 cgd register int timo;
890 1.1 cgd
891 1.1 cgd #ifdef KGDB
892 1.1 cgd if (dev != kgdb_dev)
893 1.1 cgd #endif
894 1.1 cgd if (comconsinit == 0) {
895 1.21 mycroft (void) cominit(COMUNIT(dev), comdefaultrate);
896 1.1 cgd comconsinit = 1;
897 1.1 cgd }
898 1.1 cgd /* wait for any pending transmission to finish */
899 1.1 cgd timo = 50000;
900 1.21 mycroft while (((stat = inb(iobase + com_lsr)) & LSR_TXRDY) == 0 && --timo)
901 1.1 cgd ;
902 1.21 mycroft outb(iobase + com_data, c);
903 1.1 cgd /* wait for this transmission to complete */
904 1.1 cgd timo = 1500000;
905 1.21 mycroft while (((stat = inb(iobase + com_lsr)) & LSR_TXRDY) == 0 && --timo)
906 1.1 cgd ;
907 1.1 cgd /* clear any interrupts generated by this transmission */
908 1.21 mycroft stat = inb(iobase + com_iir);
909 1.1 cgd splx(s);
910 1.2 cgd }
911