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