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