ser.c revision 1.3 1 1.1 mw /*
2 1.1 mw * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
3 1.1 mw * All rights reserved.
4 1.1 mw *
5 1.1 mw * Redistribution and use in source and binary forms, with or without
6 1.1 mw * modification, are permitted provided that the following conditions
7 1.1 mw * are met:
8 1.1 mw * 1. Redistributions of source code must retain the above copyright
9 1.1 mw * notice, this list of conditions and the following disclaimer.
10 1.1 mw * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 mw * notice, this list of conditions and the following disclaimer in the
12 1.1 mw * documentation and/or other materials provided with the distribution.
13 1.1 mw * 3. All advertising materials mentioning features or use of this software
14 1.1 mw * must display the following acknowledgement:
15 1.1 mw * This product includes software developed by the University of
16 1.1 mw * California, Berkeley and its contributors.
17 1.1 mw * 4. Neither the name of the University nor the names of its contributors
18 1.1 mw * may be used to endorse or promote products derived from this software
19 1.1 mw * without specific prior written permission.
20 1.1 mw *
21 1.1 mw * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 mw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 mw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 mw * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 mw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 mw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 mw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 mw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 mw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 mw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 mw * SUCH DAMAGE.
32 1.1 mw *
33 1.2 mycroft * from: @(#)ser.c 7.12 (Berkeley) 6/27/91
34 1.3 mw * $Id: ser.c,v 1.3 1993/09/02 18:08:14 mw Exp $
35 1.1 mw */
36 1.1 mw
37 1.1 mw #include "ser.h"
38 1.1 mw
39 1.1 mw #if NSER > 0
40 1.1 mw #include "sys/param.h"
41 1.1 mw #include "sys/systm.h"
42 1.1 mw #include "sys/ioctl.h"
43 1.1 mw #include "sys/tty.h"
44 1.1 mw #include "sys/proc.h"
45 1.1 mw #include "sys/conf.h"
46 1.1 mw #include "sys/file.h"
47 1.1 mw #include "sys/malloc.h"
48 1.1 mw #include "sys/uio.h"
49 1.1 mw #include "sys/kernel.h"
50 1.1 mw #include "sys/syslog.h"
51 1.1 mw
52 1.1 mw #include "device.h"
53 1.1 mw #include "serreg.h"
54 1.1 mw #include "machine/cpu.h"
55 1.1 mw
56 1.1 mw #include "../amiga/custom.h"
57 1.1 mw #include "../amiga/cia.h"
58 1.1 mw
59 1.1 mw int serprobe();
60 1.1 mw struct driver serdriver = {
61 1.1 mw serprobe, "ser",
62 1.1 mw };
63 1.1 mw
64 1.1 mw int serstart(), serparam(), serintr();
65 1.3 mw extern int ttrstrt();
66 1.1 mw int sersoftCAR;
67 1.1 mw int ser_active;
68 1.1 mw int ser_hasfifo;
69 1.1 mw int nser = NSER;
70 1.1 mw #ifdef SERCONSOLE
71 1.1 mw int serconsole = SERCONSOLE;
72 1.1 mw #else
73 1.1 mw int serconsole = -1;
74 1.1 mw #endif
75 1.1 mw int serconsinit;
76 1.1 mw int serdefaultrate = TTYDEF_SPEED;
77 1.1 mw int sermajor;
78 1.1 mw struct serdevice *ser_addr[NSER];
79 1.1 mw struct tty ser_cons;
80 1.3 mw #if 0
81 1.1 mw struct tty *ser_tty[NSER] = { &ser_cons };
82 1.3 mw #else
83 1.3 mw struct tty *ser_tty[NSER];
84 1.3 mw #endif
85 1.1 mw
86 1.1 mw struct speedtab serspeedtab[] = {
87 1.1 mw 0, 0,
88 1.1 mw 50, SERBRD(50),
89 1.1 mw 75, SERBRD(75),
90 1.1 mw 110, SERBRD(110),
91 1.1 mw 134, SERBRD(134),
92 1.1 mw 150, SERBRD(150),
93 1.1 mw 200, SERBRD(200),
94 1.1 mw 300, SERBRD(300),
95 1.1 mw 600, SERBRD(600),
96 1.1 mw 1200, SERBRD(1200),
97 1.1 mw 1800, SERBRD(1800),
98 1.1 mw 2400, SERBRD(2400),
99 1.1 mw 4800, SERBRD(4800),
100 1.1 mw 9600, SERBRD(9600),
101 1.1 mw 19200, SERBRD(19200),
102 1.1 mw 38400, SERBRD(38400),
103 1.1 mw -1, -1
104 1.1 mw };
105 1.1 mw
106 1.1 mw
107 1.1 mw /* since this UART is not particularly bright (nice put), we'll have to do
108 1.1 mw parity stuff on our own. this table contains the 8th bit in 7bit character
109 1.1 mw mode, for even parity. If you want odd parity, flip the bit. (for
110 1.1 mw generation of the table, see genpar.c) */
111 1.1 mw
112 1.1 mw u_char even_parity[] = {
113 1.1 mw 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
114 1.1 mw 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
115 1.1 mw 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
116 1.1 mw 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
117 1.1 mw 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
118 1.1 mw 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
119 1.1 mw 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
120 1.1 mw 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
121 1.1 mw };
122 1.1 mw
123 1.1 mw
124 1.1 mw /* since we don't get interrupts for changes on the modem control line,
125 1.1 mw well have to fake them by comparing current settings to the settings
126 1.1 mw we remembered on last invocation. */
127 1.1 mw u_char last_ciab_pra;
128 1.1 mw
129 1.1 mw extern struct tty *constty;
130 1.1 mw #ifdef KGDB
131 1.1 mw #include "machine/remote-sl.h"
132 1.1 mw
133 1.1 mw extern dev_t kgdb_dev;
134 1.1 mw extern int kgdb_rate;
135 1.1 mw extern int kgdb_debug_init;
136 1.1 mw #endif
137 1.1 mw
138 1.1 mw #if 0
139 1.1 mw #define UNIT(x) minor(x)
140 1.1 mw #else
141 1.1 mw /* just always force this to 0, so we can later interprete special
142 1.1 mw settings out of the unit number.. */
143 1.1 mw #define UNIT(x) 0
144 1.1 mw #endif
145 1.1 mw
146 1.1 mw #ifdef DEBUG
147 1.1 mw long fifoin[17];
148 1.1 mw long fifoout[17];
149 1.1 mw long serintrcount[16];
150 1.1 mw long sermintcount[16];
151 1.1 mw #endif
152 1.1 mw
153 1.1 mw serprobe(ad)
154 1.1 mw register struct amiga_device *ad;
155 1.1 mw {
156 1.1 mw register struct serdevice *ser;
157 1.1 mw register int unit;
158 1.1 mw
159 1.1 mw ser = (struct serdevice *) ad->amiga_addr;
160 1.1 mw unit = ad->amiga_unit;
161 1.1 mw if (unit == serconsole)
162 1.1 mw DELAY(100000);
163 1.1 mw
164 1.1 mw ad->amiga_ipl = 2;
165 1.1 mw ser_addr[unit] = ser;
166 1.1 mw ser_active |= 1 << unit;
167 1.1 mw sersoftCAR = ad->amiga_flags;
168 1.1 mw #ifdef KGDB
169 1.1 mw if (kgdb_dev == makedev(sermajor, unit)) {
170 1.1 mw if (serconsole == unit)
171 1.1 mw kgdb_dev = NODEV; /* can't debug over console port */
172 1.1 mw else {
173 1.1 mw (void) serinit(unit, kgdb_rate);
174 1.1 mw serconsinit = 1; /* don't re-init in serputc */
175 1.1 mw if (kgdb_debug_init) {
176 1.1 mw /*
177 1.1 mw * Print prefix of device name,
178 1.1 mw * let kgdb_connect print the rest.
179 1.1 mw */
180 1.1 mw printf("ser%d: ", unit);
181 1.1 mw kgdb_connect(1);
182 1.1 mw } else
183 1.1 mw printf("ser%d: kgdb enabled\n", unit);
184 1.1 mw }
185 1.1 mw }
186 1.1 mw #endif
187 1.1 mw /*
188 1.1 mw * Need to reset baud rate, etc. of next print so reset serconsinit.
189 1.1 mw * Also make sure console is always "hardwired."
190 1.1 mw */
191 1.1 mw if (unit == serconsole) {
192 1.1 mw serconsinit = 0;
193 1.1 mw sersoftCAR |= (1 << unit);
194 1.1 mw }
195 1.1 mw return (1);
196 1.1 mw }
197 1.1 mw
198 1.1 mw /* ARGSUSED */
199 1.1 mw #ifdef __STDC__
200 1.1 mw seropen(dev_t dev, int flag, int mode, struct proc *p)
201 1.1 mw #else
202 1.1 mw seropen(dev, flag, mode, p)
203 1.1 mw dev_t dev;
204 1.1 mw int flag, mode;
205 1.1 mw struct proc *p;
206 1.1 mw #endif
207 1.1 mw {
208 1.1 mw register struct tty *tp;
209 1.1 mw register int unit;
210 1.1 mw int error = 0;
211 1.3 mw int s;
212 1.1 mw
213 1.1 mw unit = minor (dev);
214 1.1 mw
215 1.1 mw if (unit == 1)
216 1.1 mw {
217 1.1 mw unit = 0;
218 1.1 mw sersoftCAR = 0;
219 1.1 mw }
220 1.1 mw else if (unit == 2)
221 1.1 mw {
222 1.1 mw unit = 0;
223 1.1 mw sersoftCAR = 0xff;
224 1.1 mw }
225 1.1 mw else
226 1.1 mw unit = 0;
227 1.1 mw
228 1.1 mw if (unit >= NSER || (ser_active & (1 << unit)) == 0)
229 1.1 mw return (ENXIO);
230 1.1 mw if(!ser_tty[unit]) {
231 1.3 mw #if 0
232 1.1 mw MALLOC(tp, struct tty *, sizeof(struct tty), M_TTYS, M_WAITOK);
233 1.1 mw bzero(tp, sizeof(struct tty));
234 1.1 mw ser_tty[unit] = tp;
235 1.3 mw #else
236 1.3 mw tp = ser_tty[unit] = ttymalloc();
237 1.3 mw /* default values are not optimal for this device, increase
238 1.3 mw buffers */
239 1.3 mw clfree(&tp->t_rawq);
240 1.3 mw clfree(&tp->t_canq);
241 1.3 mw clfree(&tp->t_outq);
242 1.3 mw clalloc(&tp->t_rawq, 8192, 1);
243 1.3 mw clalloc(&tp->t_canq, 8192, 1);
244 1.3 mw clalloc(&tp->t_outq, 8192, 0);
245 1.3 mw
246 1.3 mw #endif
247 1.1 mw } else
248 1.1 mw tp = ser_tty[unit];
249 1.1 mw tp->t_oproc = serstart;
250 1.1 mw tp->t_param = serparam;
251 1.1 mw tp->t_dev = dev;
252 1.1 mw if ((tp->t_state & TS_ISOPEN) == 0) {
253 1.1 mw tp->t_state |= TS_WOPEN;
254 1.1 mw ttychars(tp);
255 1.1 mw if (tp->t_ispeed == 0) {
256 1.1 mw tp->t_iflag = TTYDEF_IFLAG | IXOFF; /* XXXXX */
257 1.1 mw tp->t_oflag = TTYDEF_OFLAG;
258 1.1 mw #if 0
259 1.1 mw tp->t_cflag = TTYDEF_CFLAG;
260 1.1 mw #else
261 1.1 mw tp->t_cflag = (CREAD | CS8 | CLOCAL); /* XXXXX */
262 1.1 mw #endif
263 1.1 mw tp->t_lflag = TTYDEF_LFLAG;
264 1.1 mw tp->t_ispeed = tp->t_ospeed = serdefaultrate;
265 1.1 mw }
266 1.1 mw serparam(tp, &tp->t_termios);
267 1.1 mw ttsetwater(tp);
268 1.1 mw } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0)
269 1.1 mw return (EBUSY);
270 1.1 mw (void) sermctl (dev, TIOCM_DTR | TIOCM_RTS, DMSET);
271 1.1 mw if ((sersoftCAR & (1 << unit)) || (sermctl(dev, 0, DMGET) & TIOCM_CD))
272 1.1 mw tp->t_state |= TS_CARR_ON;
273 1.3 mw s = spltty();
274 1.1 mw while ((flag&O_NONBLOCK) == 0 && (tp->t_cflag&CLOCAL) == 0 &&
275 1.1 mw (tp->t_state & TS_CARR_ON) == 0) {
276 1.1 mw tp->t_state |= TS_WOPEN;
277 1.3 mw if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
278 1.1 mw ttopen, 0))
279 1.1 mw break;
280 1.1 mw }
281 1.3 mw splx (s);
282 1.1 mw if (error == 0)
283 1.1 mw error = (*linesw[tp->t_line].l_open)(dev, tp);
284 1.1 mw return (error);
285 1.1 mw }
286 1.1 mw
287 1.1 mw /*ARGSUSED*/
288 1.1 mw serclose(dev, flag, mode, p)
289 1.1 mw dev_t dev;
290 1.1 mw int flag, mode;
291 1.1 mw struct proc *p;
292 1.1 mw {
293 1.1 mw register struct tty *tp;
294 1.1 mw register struct serdevice *ser;
295 1.1 mw register int unit;
296 1.1 mw
297 1.1 mw unit = UNIT(dev);
298 1.1 mw
299 1.1 mw ser = ser_addr[unit];
300 1.1 mw tp = ser_tty[unit];
301 1.1 mw (*linesw[tp->t_line].l_close)(tp, flag);
302 1.1 mw custom.adkcon = ADKCONF_UARTBRK; /* clear break */
303 1.1 mw #ifdef KGDB
304 1.1 mw /* do not disable interrupts if debugging */
305 1.1 mw if (dev != kgdb_dev)
306 1.1 mw #endif
307 1.3 mw custom.intena = INTF_RBF|INTF_TBE; /* clear interrupt enable */
308 1.3 mw custom.intreq = INTF_RBF|INTF_TBE; /* and interrupt request */
309 1.1 mw #if 0
310 1.1 mw /* if the device is closed, it's close, no matter whether we deal with modem
311 1.1 mw control signals nor not. */
312 1.1 mw if (tp->t_cflag&HUPCL || tp->t_state&TS_WOPEN ||
313 1.1 mw (tp->t_state&TS_ISOPEN) == 0)
314 1.1 mw #endif
315 1.1 mw (void) sermctl(dev, 0, DMSET);
316 1.1 mw ttyclose(tp);
317 1.1 mw #if 0
318 1.1 mw if (tp != &ser_cons)
319 1.1 mw {
320 1.3 mw #if 0
321 1.1 mw FREE(tp, M_TTYS);
322 1.3 mw #else
323 1.3 mw ttyfree (tp);
324 1.3 mw #endif
325 1.1 mw ser_tty[unit] = (struct tty *)NULL;
326 1.1 mw }
327 1.1 mw #endif
328 1.1 mw return (0);
329 1.1 mw }
330 1.1 mw
331 1.1 mw serread(dev, uio, flag)
332 1.1 mw dev_t dev;
333 1.1 mw struct uio *uio;
334 1.1 mw {
335 1.1 mw register struct tty *tp = ser_tty[UNIT(dev)];
336 1.3 mw int error;
337 1.3 mw
338 1.3 mw if (! tp)
339 1.3 mw return ENXIO;
340 1.1 mw
341 1.3 mw error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
342 1.3 mw
343 1.1 mw return error;
344 1.1 mw }
345 1.1 mw
346 1.1 mw serwrite(dev, uio, flag)
347 1.1 mw dev_t dev;
348 1.1 mw struct uio *uio;
349 1.1 mw {
350 1.1 mw int unit = UNIT(dev);
351 1.1 mw register struct tty *tp = ser_tty[unit];
352 1.1 mw
353 1.3 mw if (! tp)
354 1.3 mw return ENXIO;
355 1.3 mw
356 1.1 mw /*
357 1.1 mw * (XXX) We disallow virtual consoles if the physical console is
358 1.1 mw * a serial port. This is in case there is a display attached that
359 1.1 mw * is not the console. In that situation we don't need/want the X
360 1.1 mw * server taking over the console.
361 1.1 mw */
362 1.1 mw if (constty && unit == serconsole)
363 1.1 mw constty = NULL;
364 1.1 mw return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
365 1.1 mw }
366 1.3 mw
367 1.3 mw
368 1.3 mw /* don't do any processing of data here, so we store the raw code
369 1.3 mw obtained from the uart register. In theory, 110kBaud gibes you
370 1.3 mw 11kcps, so 16k buffer should be more than enough, interrupt
371 1.3 mw latency of 1s should never happen, or something is seriously
372 1.3 mw wrong.. */
373 1.3 mw #define SERIBUF_SIZE 16738
374 1.3 mw static u_short serbuf[SERIBUF_SIZE];
375 1.3 mw static u_short *sbrpt = serbuf;
376 1.3 mw static u_short *sbwpt = serbuf;
377 1.3 mw
378 1.3 mw
379 1.3 mw /* this is a replacement for the lack of a hardware fifo. 32k should be
380 1.3 mw enough (there's only one unit anyway, so this is not going to
381 1.3 mw accumulate). */
382 1.3 mw void
383 1.3 mw ser_fastint ()
384 1.3 mw {
385 1.3 mw /* we're at RBE-level, which is higher than VBL-level which is used
386 1.3 mw to periodically transmit contents of this buffer up one layer,
387 1.3 mw so no spl-raising is necessary. */
388 1.3 mw
389 1.3 mw register u_short ints, code;
390 1.3 mw
391 1.3 mw ints = custom.intreqr & INTF_RBF;
392 1.3 mw if (! ints)
393 1.3 mw return;
394 1.3 mw
395 1.3 mw /* clear interrupt */
396 1.3 mw custom.intreq = ints;
397 1.3 mw /* this register contains both data and status bits! */
398 1.3 mw code = custom.serdatr;
399 1.3 mw
400 1.3 mw /* should really not happen, but you never know.. buffer
401 1.3 mw overflow. */
402 1.3 mw if (sbwpt + 1 == sbrpt
403 1.3 mw || (sbwpt == serbuf + SERIBUF_SIZE - 1 && sbrpt == serbuf))
404 1.3 mw {
405 1.3 mw log (LOG_WARNING, "ser_fastint: buffer overflow!");
406 1.3 mw return;
407 1.3 mw }
408 1.3 mw
409 1.3 mw *sbwpt++ = code;
410 1.3 mw if (sbwpt == serbuf + SERIBUF_SIZE)
411 1.3 mw sbwpt = serbuf;
412 1.3 mw }
413 1.3 mw
414 1.3 mw
415 1.3 mw int
416 1.1 mw serintr(unit)
417 1.1 mw register int unit;
418 1.1 mw {
419 1.1 mw register struct serdevice *ser;
420 1.3 mw int s1, s2;
421 1.1 mw
422 1.1 mw ser = ser_addr[unit];
423 1.1 mw
424 1.3 mw /* make sure we're not interrupted by another
425 1.3 mw vbl, but allow level5 ints */
426 1.3 mw s1 = spltty();
427 1.1 mw
428 1.3 mw /* ok, pass along any acumulated information .. */
429 1.3 mw while (sbrpt != sbwpt)
430 1.3 mw {
431 1.3 mw /* no collision with ser_fastint() */
432 1.3 mw sereint (unit, *sbrpt, ser);
433 1.3 mw /* lock against ser_fastint() */
434 1.3 mw s2 = spl5();
435 1.3 mw {
436 1.3 mw sbrpt++;
437 1.3 mw if (sbrpt == serbuf + SERIBUF_SIZE)
438 1.3 mw sbrpt = serbuf;
439 1.3 mw }
440 1.3 mw splx (s2);
441 1.3 mw }
442 1.1 mw
443 1.3 mw splx (s1);
444 1.1 mw
445 1.3 mw #if 0
446 1.3 mw /* add the code below if you really need it */
447 1.1 mw {
448 1.1 mw /*
449 1.1 mw * Process a received byte. Inline for speed...
450 1.1 mw */
451 1.1 mw #ifdef KGDB
452 1.1 mw #define RCVBYTE() \
453 1.1 mw ch = code & 0xff; \
454 1.1 mw if ((tp->t_state & TS_ISOPEN) == 0) { \
455 1.1 mw if (ch == FRAME_END && \
456 1.1 mw kgdb_dev == makedev(sermajor, unit)) \
457 1.1 mw kgdb_connect(0); /* trap into kgdb */ \
458 1.1 mw }
459 1.1 mw #else
460 1.1 mw #define RCVBYTE()
461 1.1 mw #endif
462 1.1 mw RCVBYTE();
463 1.1 mw /* sereint does the receive-processing */
464 1.1 mw sereint (unit, code, ser);
465 1.1 mw }
466 1.3 mw #endif
467 1.1 mw }
468 1.1 mw
469 1.1 mw sereint(unit, stat, ser)
470 1.1 mw register int unit, stat;
471 1.1 mw register struct serdevice *ser;
472 1.1 mw {
473 1.1 mw register struct tty *tp;
474 1.1 mw register int c;
475 1.1 mw register u_char ch;
476 1.1 mw
477 1.1 mw tp = ser_tty[unit];
478 1.1 mw if ((tp->t_state & TS_ISOPEN) == 0) {
479 1.1 mw #ifdef KGDB
480 1.1 mw /* we don't care about parity errors */
481 1.1 mw if (kgdb_dev == makedev(sermajor, unit) && c == FRAME_END)
482 1.1 mw kgdb_connect(0); /* trap into kgdb */
483 1.1 mw #endif
484 1.1 mw return;
485 1.1 mw }
486 1.1 mw ch = stat & 0xff;
487 1.1 mw c = ch;
488 1.1 mw /* all databits 0 including stop indicate break condition */
489 1.1 mw if (!(stat & 0x1ff))
490 1.1 mw c |= TTY_FE;
491 1.1 mw
492 1.1 mw /* if parity checking enabled, check parity */
493 1.1 mw else if ((tp->t_cflag & PARENB) &&
494 1.1 mw (((ch >> 7) + even_parity[ch & 0x7f] + !!(tp->t_cflag & PARODD)) & 1))
495 1.1 mw c |= TTY_PE;
496 1.1 mw
497 1.1 mw if (stat & SERDATRF_OVRUN)
498 1.1 mw log(LOG_WARNING, "ser%d: silo overflow\n", unit);
499 1.1 mw
500 1.1 mw (*linesw[tp->t_line].l_rint)(c, tp);
501 1.1 mw }
502 1.1 mw
503 1.3 mw /* this interrupt is periodically invoked in the vertical blank
504 1.3 mw interrupt. It's used to keep track of the modem control lines
505 1.3 mw and (new with the fast_int code) to move accumulated data
506 1.3 mw up into the tty layer. */
507 1.3 mw void
508 1.1 mw sermint(unit)
509 1.1 mw register int unit;
510 1.1 mw {
511 1.1 mw register struct tty *tp;
512 1.1 mw register u_char stat, last, istat;
513 1.1 mw register struct serdevice *ser;
514 1.1 mw
515 1.1 mw tp = ser_tty[unit];
516 1.3 mw if (!tp)
517 1.3 mw return;
518 1.3 mw
519 1.3 mw if ((tp->t_state & (TS_ISOPEN|TS_WOPEN)) == 0)
520 1.3 mw {
521 1.3 mw sbrpt = sbwpt = serbuf;
522 1.3 mw return;
523 1.3 mw }
524 1.3 mw
525 1.3 mw
526 1.3 mw /* first empty buffer */
527 1.3 mw serintr (0);
528 1.3 mw
529 1.1 mw stat = ciab.pra;
530 1.1 mw last = last_ciab_pra;
531 1.1 mw last_ciab_pra = stat;
532 1.1 mw
533 1.1 mw /* check whether any interesting signal changed state */
534 1.1 mw istat = stat ^ last;
535 1.1 mw
536 1.1 mw if ((istat & CIAB_PRA_CD) && (sersoftCAR & (1 << unit)) == 0)
537 1.1 mw {
538 1.1 mw if (ISDCD (stat))
539 1.1 mw (*linesw[tp->t_line].l_modem)(tp, 1);
540 1.1 mw else if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0)
541 1.1 mw {
542 1.1 mw CLRDTR (stat);
543 1.1 mw CLRRTS (stat);
544 1.1 mw ciab.pra = stat;
545 1.1 mw last_ciab_pra = stat;
546 1.1 mw }
547 1.1 mw }
548 1.1 mw else if ((istat & CIAB_PRA_CTS) && (tp->t_state & TS_ISOPEN) &&
549 1.1 mw (tp->t_flags & CRTSCTS))
550 1.1 mw {
551 1.1 mw /* the line is up and we want to do rts/cts flow control */
552 1.1 mw if (ISCTS (stat))
553 1.1 mw {
554 1.1 mw tp->t_state &=~ TS_TTSTOP;
555 1.1 mw ttstart(tp);
556 1.3 mw /* cause tbe-int if we were stuck there */
557 1.3 mw custom.intreq = INTF_SETCLR | INTF_TBE;
558 1.1 mw }
559 1.1 mw else
560 1.1 mw tp->t_state |= TS_TTSTOP;
561 1.3 mw }
562 1.1 mw }
563 1.1 mw
564 1.1 mw serioctl(dev, cmd, data, flag)
565 1.1 mw dev_t dev;
566 1.1 mw caddr_t data;
567 1.1 mw {
568 1.1 mw register struct tty *tp;
569 1.1 mw register int unit = UNIT(dev);
570 1.1 mw register struct serdevice *ser;
571 1.1 mw register int error;
572 1.1 mw
573 1.1 mw tp = ser_tty[unit];
574 1.3 mw if (! tp)
575 1.3 mw return ENXIO;
576 1.3 mw
577 1.1 mw error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
578 1.1 mw if (error >= 0)
579 1.1 mw return (error);
580 1.1 mw error = ttioctl(tp, cmd, data, flag);
581 1.1 mw if (error >= 0)
582 1.1 mw return (error);
583 1.1 mw
584 1.1 mw ser = ser_addr[unit];
585 1.1 mw switch (cmd) {
586 1.1 mw
587 1.1 mw case TIOCSBRK:
588 1.1 mw custom.adkcon = ADKCONF_SETCLR | ADKCONF_UARTBRK;
589 1.1 mw break;
590 1.1 mw
591 1.1 mw case TIOCCBRK:
592 1.1 mw custom.adkcon = ADKCONF_UARTBRK;
593 1.1 mw break;
594 1.1 mw
595 1.1 mw case TIOCSDTR:
596 1.1 mw (void) sermctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIS);
597 1.1 mw break;
598 1.1 mw
599 1.1 mw case TIOCCDTR:
600 1.1 mw (void) sermctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIC);
601 1.1 mw break;
602 1.1 mw
603 1.1 mw case TIOCMSET:
604 1.1 mw (void) sermctl(dev, *(int *)data, DMSET);
605 1.1 mw break;
606 1.1 mw
607 1.1 mw case TIOCMBIS:
608 1.1 mw (void) sermctl(dev, *(int *)data, DMBIS);
609 1.1 mw break;
610 1.1 mw
611 1.1 mw case TIOCMBIC:
612 1.1 mw (void) sermctl(dev, *(int *)data, DMBIC);
613 1.1 mw break;
614 1.1 mw
615 1.1 mw case TIOCMGET:
616 1.1 mw *(int *)data = sermctl(dev, 0, DMGET);
617 1.1 mw break;
618 1.1 mw
619 1.1 mw default:
620 1.1 mw return (ENOTTY);
621 1.1 mw }
622 1.1 mw return (0);
623 1.1 mw }
624 1.1 mw
625 1.1 mw serparam(tp, t)
626 1.1 mw register struct tty *tp;
627 1.1 mw register struct termios *t;
628 1.1 mw {
629 1.1 mw register struct serdevice *ser;
630 1.1 mw register int cfcr, cflag = t->c_cflag;
631 1.1 mw int unit = UNIT(tp->t_dev);
632 1.1 mw int ospeed = ttspeedtab(t->c_ospeed, serspeedtab);
633 1.1 mw
634 1.1 mw /* check requested parameters */
635 1.1 mw if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
636 1.1 mw return (EINVAL);
637 1.1 mw /* and copy to tty */
638 1.1 mw tp->t_ispeed = t->c_ispeed;
639 1.1 mw tp->t_ospeed = t->c_ospeed;
640 1.1 mw tp->t_cflag = cflag;
641 1.1 mw
642 1.3 mw custom.intena = INTF_SETCLR | INTF_RBF | INTF_TBE;
643 1.1 mw last_ciab_pra = ciab.pra;
644 1.1 mw
645 1.3 mw if (ospeed == 0)
646 1.3 mw {
647 1.3 mw (void) sermctl(unit, 0, DMSET); /* hang up line */
648 1.3 mw return (0);
649 1.3 mw }
650 1.3 mw else
651 1.3 mw {
652 1.3 mw /* make sure any previous hangup is undone, ie.
653 1.3 mw reenable DTR. */
654 1.3 mw (void) sermctl (tp->t_dev, TIOCM_DTR | TIOCM_RTS, DMSET);
655 1.3 mw }
656 1.1 mw /* set the baud rate */
657 1.1 mw custom.serper = (0<<15) | ospeed; /* select 8 bit mode (instead of 9 bit) */
658 1.1 mw
659 1.1 mw return (0);
660 1.1 mw }
661 1.3 mw
662 1.3 mw
663 1.3 mw static void
664 1.3 mw ser_putchar (tp, c)
665 1.3 mw struct tty *tp;
666 1.3 mw unsigned short c;
667 1.3 mw {
668 1.3 mw /* handle truncation of character if necessary */
669 1.3 mw if ((tp->t_cflag & CSIZE) == CS7)
670 1.3 mw c &= 0x7f;
671 1.3 mw
672 1.3 mw /* handle parity if necessary (forces CS7) */
673 1.3 mw if (tp->t_cflag & PARENB)
674 1.3 mw {
675 1.3 mw if (even_parity[c & 0x7f])
676 1.3 mw c |= 0x80;
677 1.3 mw if (tp->t_cflag & PARODD)
678 1.3 mw c ^= 0x80;
679 1.3 mw }
680 1.3 mw
681 1.3 mw /* add stop bit(s) */
682 1.3 mw if (tp->t_cflag & CSTOPB)
683 1.3 mw c |= 0x300;
684 1.3 mw else
685 1.3 mw c |= 0x100;
686 1.3 mw
687 1.3 mw custom.serdat = c;
688 1.3 mw }
689 1.3 mw
690 1.3 mw
691 1.3 mw #define SEROBUF_SIZE 1024
692 1.3 mw static u_char ser_outbuf[SEROBUF_SIZE];
693 1.3 mw static u_char *sob_ptr=ser_outbuf, *sob_end=ser_outbuf;
694 1.3 mw void
695 1.3 mw ser_outintr ()
696 1.3 mw {
697 1.3 mw struct tty *tp = ser_tty[0]; /* hmmmmm */
698 1.3 mw unsigned short c;
699 1.3 mw int s;
700 1.3 mw
701 1.3 mw if (! tp)
702 1.3 mw return;
703 1.3 mw
704 1.3 mw if (! (custom.intreqr & INTF_TBE))
705 1.3 mw return;
706 1.3 mw
707 1.3 mw /* clear interrupt */
708 1.3 mw custom.intreq = INTF_TBE;
709 1.3 mw
710 1.3 mw if (sob_ptr == sob_end)
711 1.3 mw {
712 1.3 mw s = spltty();
713 1.3 mw /* hm, probably not necessary in every case, but WHEN is
714 1.3 mw it necessary? */
715 1.3 mw tp->t_state |= TS_TIMEOUT;
716 1.3 mw timeout (ttrstrt, tp, 1);
717 1.3 mw tp->t_state &= ~TS_BUSY;
718 1.3 mw splx (s);
719 1.3 mw return;
720 1.3 mw }
721 1.3 mw
722 1.3 mw if (tp->t_state & TS_TTSTOP)
723 1.3 mw return;
724 1.3 mw
725 1.3 mw ser_putchar (tp, *sob_ptr++);
726 1.3 mw }
727 1.1 mw
728 1.1 mw serstart(tp)
729 1.1 mw register struct tty *tp;
730 1.1 mw {
731 1.3 mw register int cc, s;
732 1.3 mw int unit;
733 1.1 mw register struct serdevice *ser;
734 1.3 mw int hiwat = 0;
735 1.3 mw
736 1.3 mw if (! (tp->t_state & TS_ISOPEN))
737 1.3 mw return;
738 1.3 mw
739 1.1 mw unit = UNIT(tp->t_dev);
740 1.1 mw ser = ser_addr[unit];
741 1.3 mw
742 1.1 mw s = spltty();
743 1.3 mw if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP)) {
744 1.3 mw splx(s);
745 1.3 mw return;
746 1.3 mw }
747 1.3 mw tp->t_state |= TS_BUSY;
748 1.3 mw cc = tp->t_outq.c_cc;
749 1.3 mw if (cc <= tp->t_lowat) {
750 1.3 mw if (tp->t_state & TS_ASLEEP) {
751 1.1 mw tp->t_state &= ~TS_ASLEEP;
752 1.3 mw wakeup(&tp->t_outq);
753 1.1 mw }
754 1.3 mw selwakeup(&tp->t_wsel);
755 1.3 mw }
756 1.3 mw /*
757 1.3 mw * Limit the amount of output we do in one burst
758 1.3 mw * to prevent hogging the CPU.
759 1.3 mw */
760 1.3 mw if (cc > SEROBUF_SIZE) {
761 1.3 mw hiwat++;
762 1.3 mw cc = SEROBUF_SIZE;
763 1.1 mw }
764 1.3 mw cc = q_to_b (&tp->t_outq, ser_outbuf, cc);
765 1.3 mw if (cc > 0)
766 1.3 mw {
767 1.3 mw sob_ptr = ser_outbuf;
768 1.3 mw sob_end = ser_outbuf + cc;
769 1.3 mw /* get first character out, then have tbe-interrupts blow out
770 1.3 mw further characters, until buffer is empty, and TS_BUSY
771 1.3 mw gets cleared. */
772 1.3 mw ser_putchar (tp, *sob_ptr++);
773 1.3 mw }
774 1.1 mw else
775 1.3 mw tp->t_state &= ~TS_BUSY;
776 1.1 mw
777 1.1 mw splx(s);
778 1.1 mw }
779 1.1 mw
780 1.1 mw /*
781 1.1 mw * Stop output on a line.
782 1.1 mw */
783 1.1 mw /*ARGSUSED*/
784 1.1 mw serstop(tp, flag)
785 1.1 mw register struct tty *tp;
786 1.1 mw {
787 1.1 mw register int s;
788 1.1 mw
789 1.1 mw s = spltty();
790 1.1 mw if (tp->t_state & TS_BUSY) {
791 1.1 mw if ((tp->t_state&TS_TTSTOP)==0)
792 1.1 mw tp->t_state |= TS_FLUSH;
793 1.1 mw }
794 1.1 mw splx(s);
795 1.1 mw }
796 1.1 mw
797 1.1 mw sermctl(dev, bits, how)
798 1.1 mw dev_t dev;
799 1.1 mw int bits, how;
800 1.1 mw {
801 1.1 mw register struct serdevice *ser;
802 1.1 mw register int unit;
803 1.1 mw u_char ub;
804 1.1 mw int s;
805 1.1 mw
806 1.1 mw unit = UNIT(dev);
807 1.1 mw ser = ser_addr[unit];
808 1.1 mw
809 1.1 mw /* convert TIOCM* mask into CIA mask (which is really low-active!!) */
810 1.1 mw if (how != DMGET)
811 1.1 mw {
812 1.1 mw ub = 0;
813 1.1 mw if (bits & TIOCM_DTR) ub |= CIAB_PRA_DTR;
814 1.1 mw if (bits & TIOCM_RTS) ub |= CIAB_PRA_RTS;
815 1.1 mw if (bits & TIOCM_CTS) ub |= CIAB_PRA_CTS;
816 1.1 mw if (bits & TIOCM_CD) ub |= CIAB_PRA_CD;
817 1.1 mw if (bits & TIOCM_RI) ub |= CIAB_PRA_SEL; /* collision with /dev/par ! */
818 1.1 mw if (bits & TIOCM_DSR) ub |= CIAB_PRA_DSR;
819 1.1 mw }
820 1.1 mw
821 1.1 mw
822 1.1 mw s = spltty();
823 1.1 mw switch (how) {
824 1.1 mw
825 1.1 mw case DMSET:
826 1.1 mw /* invert and set */
827 1.1 mw ciab.pra = ~ub;
828 1.1 mw break;
829 1.1 mw
830 1.1 mw case DMBIC:
831 1.1 mw ciab.pra |= ub;
832 1.1 mw ub = ~ciab.pra;
833 1.1 mw break;
834 1.1 mw
835 1.1 mw case DMBIS:
836 1.1 mw ciab.pra &= ~ub;
837 1.1 mw ub = ~ciab.pra;
838 1.1 mw break;
839 1.1 mw
840 1.1 mw case DMGET:
841 1.1 mw ub = ~ciab.pra;
842 1.1 mw break;
843 1.1 mw }
844 1.1 mw (void) splx(s);
845 1.1 mw
846 1.1 mw bits = 0;
847 1.1 mw if (ub & CIAB_PRA_DTR) bits |= TIOCM_DTR;
848 1.1 mw if (ub & CIAB_PRA_RTS) bits |= TIOCM_RTS;
849 1.1 mw if (ub & CIAB_PRA_CTS) bits |= TIOCM_CTS;
850 1.1 mw if (ub & CIAB_PRA_CD) bits |= TIOCM_CD;
851 1.1 mw if (ub & CIAB_PRA_SEL) bits |= TIOCM_RI;
852 1.1 mw if (ub & CIAB_PRA_DSR) bits |= TIOCM_DSR;
853 1.1 mw
854 1.1 mw return bits;
855 1.1 mw }
856 1.1 mw
857 1.1 mw /*
858 1.1 mw * Following are all routines needed for SER to act as console
859 1.1 mw */
860 1.1 mw #include "../amiga/cons.h"
861 1.1 mw
862 1.1 mw sercnprobe(cp)
863 1.1 mw struct consdev *cp;
864 1.1 mw {
865 1.1 mw int unit = CONUNIT;
866 1.1 mw /* locate the major number */
867 1.1 mw for (sermajor = 0; sermajor < nchrdev; sermajor++)
868 1.1 mw if (cdevsw[sermajor].d_open == seropen)
869 1.1 mw break;
870 1.1 mw
871 1.1 mw /* XXX: ick */
872 1.1 mw unit = CONUNIT;
873 1.1 mw
874 1.1 mw /* initialize required fields */
875 1.1 mw cp->cn_dev = makedev(sermajor, unit);
876 1.3 mw #if 0
877 1.3 mw /* on ser it really doesn't matter whether we're later
878 1.3 mw using the tty interface or single-character io thru
879 1.3 mw cnputc, so don't reach out to later on remember that
880 1.3 mw our console is here (see ite.c) */
881 1.1 mw cp->cn_tp = ser_tty[unit];
882 1.3 mw #endif
883 1.1 mw cp->cn_pri = CN_NORMAL;
884 1.1 mw
885 1.1 mw /*
886 1.1 mw * If serconsole is initialized, raise our priority.
887 1.1 mw */
888 1.1 mw if (serconsole == unit)
889 1.1 mw cp->cn_pri = CN_REMOTE;
890 1.1 mw #ifdef KGDB
891 1.1 mw if (major(kgdb_dev) == 1) /* XXX */
892 1.1 mw kgdb_dev = makedev(sermajor, minor(kgdb_dev));
893 1.1 mw #endif
894 1.1 mw }
895 1.1 mw
896 1.1 mw sercninit(cp)
897 1.1 mw struct consdev *cp;
898 1.1 mw {
899 1.1 mw int unit = UNIT(cp->cn_dev);
900 1.1 mw
901 1.1 mw serinit(unit, serdefaultrate);
902 1.1 mw serconsole = unit;
903 1.1 mw serconsinit = 1;
904 1.1 mw }
905 1.1 mw
906 1.1 mw serinit(unit, rate)
907 1.1 mw int unit, rate;
908 1.1 mw {
909 1.1 mw int s;
910 1.1 mw
911 1.1 mw #ifdef lint
912 1.1 mw stat = unit; if (stat) return;
913 1.1 mw #endif
914 1.1 mw s = splhigh();
915 1.1 mw /* might want to fiddle with the CIA later ??? */
916 1.1 mw custom.serper = ttspeedtab(rate, serspeedtab);
917 1.1 mw splx(s);
918 1.1 mw }
919 1.1 mw
920 1.1 mw sercngetc(dev)
921 1.1 mw {
922 1.1 mw u_short stat;
923 1.1 mw int c, s;
924 1.1 mw
925 1.1 mw #ifdef lint
926 1.1 mw stat = dev; if (stat) return (0);
927 1.1 mw #endif
928 1.1 mw s = splhigh();
929 1.1 mw while (!((stat = custom.serdatr & 0xffff) & SERDATRF_RBF))
930 1.1 mw ;
931 1.1 mw c = stat & 0xff;
932 1.1 mw /* clear interrupt */
933 1.1 mw custom.intreq = INTF_RBF;
934 1.1 mw splx(s);
935 1.1 mw return (c);
936 1.1 mw }
937 1.1 mw
938 1.1 mw /*
939 1.1 mw * Console kernel output character routine.
940 1.1 mw */
941 1.1 mw sercnputc(dev, c)
942 1.1 mw dev_t dev;
943 1.1 mw register int c;
944 1.1 mw {
945 1.1 mw register int timo;
946 1.1 mw short stat;
947 1.1 mw int s = splhigh();
948 1.1 mw
949 1.1 mw #ifdef lint
950 1.1 mw stat = dev; if (stat) return;
951 1.1 mw #endif
952 1.1 mw if (serconsinit == 0) {
953 1.1 mw (void) serinit(UNIT(dev), serdefaultrate);
954 1.1 mw serconsinit = 1;
955 1.1 mw }
956 1.1 mw /* wait for any pending transmission to finish */
957 1.1 mw timo = 50000;
958 1.1 mw while (! (custom.serdatr & SERDATRF_TBE) && --timo)
959 1.1 mw ;
960 1.1 mw custom.serdat = (c&0xff) | 0x100;
961 1.1 mw /* wait for this transmission to complete */
962 1.1 mw timo = 1500000;
963 1.1 mw while (! (custom.serdatr & SERDATRF_TBE) && --timo)
964 1.1 mw ;
965 1.1 mw /* wait for the device (my vt100..) to process the data, since
966 1.1 mw we don't do flow-control with cnputc */
967 1.1 mw for (timo = 0; timo < 30000; timo++) ;
968 1.1 mw
969 1.1 mw /* clear any interrupts generated by this transmission */
970 1.1 mw custom.intreq = INTF_TBE;
971 1.1 mw splx(s);
972 1.1 mw }
973 1.1 mw
974 1.1 mw
975 1.1 mw serspit(c)
976 1.1 mw int c;
977 1.1 mw {
978 1.1 mw register struct Custom *cu asm("a2") = CUSTOMbase;
979 1.1 mw register int timo asm("d2");
980 1.1 mw extern int cold;
981 1.1 mw int s;
982 1.1 mw
983 1.1 mw if (c == 10)
984 1.1 mw serspit (13);
985 1.1 mw
986 1.1 mw s = splhigh();
987 1.1 mw
988 1.1 mw /* wait for any pending transmission to finish */
989 1.1 mw timo = 500000;
990 1.1 mw while (! (cu->serdatr & (SERDATRF_TBE|SERDATRF_TSRE)) && --timo)
991 1.1 mw ;
992 1.1 mw cu->serdat = (c&0xff) | 0x100;
993 1.1 mw /* wait for this transmission to complete */
994 1.1 mw timo = 15000000;
995 1.1 mw while (! (cu->serdatr & SERDATRF_TBE) && --timo)
996 1.1 mw ;
997 1.1 mw /* clear any interrupts generated by this transmission */
998 1.1 mw cu->intreq = INTF_TBE;
999 1.1 mw
1000 1.1 mw for (timo = 0; timo < 30000; timo++) ;
1001 1.1 mw
1002 1.1 mw splx (s);
1003 1.1 mw }
1004 1.1 mw
1005 1.3 mw serspits(cp)
1006 1.3 mw char *cp;
1007 1.3 mw {
1008 1.3 mw while (*cp)
1009 1.3 mw serspit(*cp++);
1010 1.3 mw }
1011 1.3 mw
1012 1.1 mw int
1013 1.1 mw serselect(dev, rw, p)
1014 1.1 mw dev_t dev;
1015 1.1 mw int rw;
1016 1.1 mw struct proc *p;
1017 1.1 mw {
1018 1.1 mw register struct tty *tp = ser_tty[UNIT(dev)];
1019 1.1 mw int nread;
1020 1.1 mw int s = spltty();
1021 1.1 mw struct proc *selp;
1022 1.1 mw
1023 1.1 mw switch (rw) {
1024 1.1 mw
1025 1.1 mw case FREAD:
1026 1.1 mw nread = ttnread(tp);
1027 1.1 mw if (nread > 0 ||
1028 1.1 mw ((tp->t_cflag&CLOCAL) == 0 && (tp->t_state&TS_CARR_ON) == 0))
1029 1.1 mw goto win;
1030 1.1 mw selrecord(p, &tp->t_rsel);
1031 1.1 mw break;
1032 1.1 mw
1033 1.1 mw case FWRITE:
1034 1.3 mw if (tp->t_outq.c_cc <= tp->t_lowat)
1035 1.1 mw goto win;
1036 1.1 mw selrecord(p, &tp->t_wsel);
1037 1.1 mw break;
1038 1.1 mw }
1039 1.1 mw splx(s);
1040 1.1 mw return (0);
1041 1.1 mw win:
1042 1.1 mw splx(s);
1043 1.1 mw return (1);
1044 1.1 mw }
1045 1.1 mw
1046 1.1 mw #endif
1047