FreeBSD/Linux Kernel Cross Reference
sys/i386/isa/cy.c
1 /*-
2 * cyclades cyclom-y serial driver
3 * Andrew Herbert <andrew@werple.apana.org.au>, 17 August 1993
4 *
5 * Copyright (c) 1993 Andrew Herbert.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name Andrew Herbert may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
22 * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * $FreeBSD$
31 */
32
33 #include "opt_compat.h"
34 #include "cy.h"
35
36 /*
37 * TODO:
38 * Atomic COR change.
39 * Consoles.
40 */
41
42 /*
43 * Temporary compile-time configuration options.
44 */
45 #define RxFifoThreshold (CD1400_RX_FIFO_SIZE / 2)
46 /* Number of chars in the receiver FIFO before an
47 * an interrupt is generated. Should depend on
48 * line speed. Needs to be about 6 on a 486DX33
49 * for 4 active ports at 115200 bps. Why doesn't
50 * 10 work?
51 */
52 #define PollMode /* Use polling-based irq service routine, not the
53 * hardware svcack lines. Must be defined for
54 * Cyclom-16Y boards. Less efficient for Cyclom-8Ys,
55 * and stops 4 * 115200 bps from working.
56 */
57 #undef Smarts /* Enable slightly more CD1400 intelligence. Mainly
58 * the output CR/LF processing, plus we can avoid a
59 * few checks usually done in ttyinput().
60 *
61 * XXX not fully implemented, and not particularly
62 * worthwhile.
63 */
64 #undef CyDebug /* Include debugging code (not very expensive). */
65
66 /* These will go away. */
67 #undef SOFT_CTS_OFLOW
68 #define SOFT_HOTCHAR
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/tty.h>
73 #include <sys/proc.h>
74 #include <sys/conf.h>
75 #include <sys/dkstat.h>
76 #include <sys/fcntl.h>
77 #include <sys/interrupt.h>
78 #include <sys/kernel.h>
79 #include <sys/malloc.h>
80 #include <sys/syslog.h>
81 #include <machine/clock.h>
82 #include <machine/ipl.h>
83 #ifndef SMP
84 #include <machine/lock.h>
85 #endif
86 #include <machine/psl.h>
87
88 #include <i386/isa/isa_device.h>
89 #include <i386/isa/cyreg.h>
90 #include <i386/isa/ic/cd1400.h>
91
92 #ifdef SMP
93 #define disable_intr() COM_DISABLE_INTR()
94 #define enable_intr() COM_ENABLE_INTR()
95 #endif /* SMP */
96
97 /*
98 * Dictionary so that I can name everything *sio* or *com* to compare with
99 * sio.c. There is also lots of ugly formatting and unnecessary ifdefs to
100 * simplify the comparision. These will go away.
101 */
102 #define LSR_BI CD1400_RDSR_BREAK
103 #define LSR_FE CD1400_RDSR_FE
104 #define LSR_OE CD1400_RDSR_OE
105 #define LSR_PE CD1400_RDSR_PE
106 #define MCR_DTR CD1400_MSVR2_DTR
107 #define MCR_RTS CD1400_MSVR1_RTS
108 #define MSR_CTS CD1400_MSVR2_CTS
109 #define MSR_DCD CD1400_MSVR2_CD
110 #define MSR_DSR CD1400_MSVR2_DSR
111 #define MSR_RI CD1400_MSVR2_RI
112 #define NSIO (NCY * CY_MAX_PORTS)
113 #define comconsole cyconsole
114 #define comdefaultrate cydefaultrate
115 #define com_events cy_events
116 #define comhardclose cyhardclose
117 #define commctl cymctl
118 #define comparam cyparam
119 #define comspeed cyspeed
120 #define comstart cystart
121 #define comwakeup cywakeup
122 #define nsio_tty ncy_tty
123 #define p_com_addr p_cy_addr
124 #define sioattach cyattach
125 #define sioclose cyclose
126 #define siodriver cydriver
127 #define siodtrwakeup cydtrwakeup
128 #define sioinput cyinput
129 #define siointr cyintr
130 #define siointr1 cyintr1
131 #define sioioctl cyioctl
132 #define sioopen cyopen
133 #define siopoll cypoll
134 #define sioprobe cyprobe
135 #define siosettimeout cysettimeout
136 #define siosetwater cysetwater
137 #define comstop cystop
138 #define siowrite cywrite
139 #define sio_registered cy_registered
140 #define sio_timeout cy_timeout
141 #define sio_timeout_handle cy_timeout_handle
142 #define sio_timeouts_until_log cy_timeouts_until_log
143 #define sio_tty cy_tty
144
145 #define CY_MAX_PORTS (CD1400_NO_OF_CHANNELS * CY_MAX_CD1400s)
146
147 /* We encode the cyclom unit number (cyu) in spare bits in the IVR's. */
148 #define CD1400_xIVR_CHAN_SHIFT 3
149 #define CD1400_xIVR_CHAN 0x1F
150
151 /*
152 * ETC states. com->etc may also contain a hardware ETC command value,
153 * meaning that execution of that command is pending.
154 */
155 #define ETC_NONE 0 /* we depend on bzero() setting this */
156 #define ETC_BREAK_STARTING 1
157 #define ETC_BREAK_STARTED 2
158 #define ETC_BREAK_ENDING 3
159 #define ETC_BREAK_ENDED 4
160
161 #define LOTS_OF_EVENTS 64 /* helps separate urgent events from input */
162
163 #define CALLOUT_MASK 0x80
164 #define CONTROL_MASK 0x60
165 #define CONTROL_INIT_STATE 0x20
166 #define CONTROL_LOCK_STATE 0x40
167 #define DEV_TO_UNIT(dev) (MINOR_TO_UNIT(minor(dev)))
168 #define MINOR_MAGIC_MASK (CALLOUT_MASK | CONTROL_MASK)
169 /*
170 * Not all of the magic is parametrized in the following macros. 16 and
171 * 0xff are related to the bitfields in a udev_t. CY_MAX_PORTS must be
172 * ((0xff & ~MINOR_MAGIC_MASK) + 1) for things to work.
173 */
174 #define MINOR_TO_UNIT(mynor) (((mynor) >> 16) * CY_MAX_PORTS \
175 | (((mynor) & 0xff) & ~MINOR_MAGIC_MASK))
176 #define UNIT_TO_MINOR(unit) (((unit) / CY_MAX_PORTS) << 16 \
177 | (((unit) & 0xff) & ~MINOR_MAGIC_MASK))
178
179 /*
180 * com state bits.
181 * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher
182 * than the other bits so that they can be tested as a group without masking
183 * off the low bits.
184 *
185 * The following com and tty flags correspond closely:
186 * CS_BUSY = TS_BUSY (maintained by comstart(), siopoll() and
187 * comstop())
188 * CS_TTGO = ~TS_TTSTOP (maintained by comparam() and comstart())
189 * CS_CTS_OFLOW = CCTS_OFLOW (maintained by comparam())
190 * CS_RTS_IFLOW = CRTS_IFLOW (maintained by comparam())
191 * TS_FLUSH is not used.
192 * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON.
193 * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state).
194 */
195 #define CS_BUSY 0x80 /* output in progress */
196 #define CS_TTGO 0x40 /* output not stopped by XOFF */
197 #define CS_ODEVREADY 0x20 /* external device h/w ready (CTS) */
198 #define CS_CHECKMSR 1 /* check of MSR scheduled */
199 #define CS_CTS_OFLOW 2 /* use CTS output flow control */
200 #define CS_DTR_OFF 0x10 /* DTR held off */
201 #define CS_ODONE 4 /* output completed */
202 #define CS_RTS_IFLOW 8 /* use RTS input flow control */
203 #define CSE_ODONE 1 /* output transmitted */
204
205 static char const * const error_desc[] = {
206 #define CE_OVERRUN 0
207 "silo overflow",
208 #define CE_INTERRUPT_BUF_OVERFLOW 1
209 "interrupt-level buffer overflow",
210 #define CE_TTY_BUF_OVERFLOW 2
211 "tty-level buffer overflow",
212 };
213
214 #define CE_NTYPES 3
215 #define CE_RECORD(com, errnum) (++(com)->delta_error_counts[errnum])
216
217 /* types. XXX - should be elsewhere */
218 typedef u_char bool_t; /* boolean */
219 typedef u_char volatile *cy_addr;
220
221 /* queue of linear buffers */
222 struct lbq {
223 u_char *l_head; /* next char to process */
224 u_char *l_tail; /* one past the last char to process */
225 struct lbq *l_next; /* next in queue */
226 bool_t l_queued; /* nonzero if queued */
227 };
228
229 /* com device structure */
230 struct com_s {
231 u_char state; /* miscellaneous flag bits */
232 bool_t active_out; /* nonzero if the callout device is open */
233 #if 0
234 u_char cfcr_image; /* copy of value written to CFCR */
235 #endif
236 u_char etc; /* pending Embedded Transmit Command */
237 u_char extra_state; /* more flag bits, separate for order trick */
238 #if 0
239 u_char fifo_image; /* copy of value written to FIFO */
240 #endif
241 u_char gfrcr_image; /* copy of value read from GFRCR */
242 #if 0
243 bool_t hasfifo; /* nonzero for 16550 UARTs */
244 bool_t loses_outints; /* nonzero if device loses output interrupts */
245 #endif
246 u_char mcr_dtr; /* MCR bit that is wired to DTR */
247 u_char mcr_image; /* copy of value written to MCR */
248 u_char mcr_rts; /* MCR bit that is wired to RTS */
249 #if 0
250 #ifdef COM_MULTIPORT
251 bool_t multiport; /* is this unit part of a multiport device? */
252 #endif /* COM_MULTIPORT */
253 bool_t no_irq; /* nonzero if irq is not attached */
254 bool_t poll; /* nonzero if polling is required */
255 bool_t poll_output; /* nonzero if polling for output is required */
256 #endif
257 int unit; /* unit number */
258 int dtr_wait; /* time to hold DTR down on close (* 1/hz) */
259 #if 0
260 u_int tx_fifo_size;
261 #endif
262 u_int wopeners; /* # processes waiting for DCD in open() */
263
264 /*
265 * The high level of the driver never reads status registers directly
266 * because there would be too many side effects to handle conveniently.
267 * Instead, it reads copies of the registers stored here by the
268 * interrupt handler.
269 */
270 u_char last_modem_status; /* last MSR read by intr handler */
271 u_char prev_modem_status; /* last MSR handled by high level */
272
273 u_char hotchar; /* ldisc-specific char to be handled ASAP */
274 u_char *ibuf; /* start of input buffer */
275 u_char *ibufend; /* end of input buffer */
276 u_char *ibufold; /* old input buffer, to be freed */
277 u_char *ihighwater; /* threshold in input buffer */
278 u_char *iptr; /* next free spot in input buffer */
279 int ibufsize; /* size of ibuf (not include error bytes) */
280 int ierroff; /* offset of error bytes in ibuf */
281
282 struct lbq obufq; /* head of queue of output buffers */
283 struct lbq obufs[2]; /* output buffers */
284
285 int cy_align; /* index for register alignment */
286 cy_addr cy_iobase; /* base address of this port's cyclom */
287 cy_addr iobase; /* base address of this port's cd1400 */
288 int mcr_rts_reg; /* cd1400 reg number of reg holding mcr_rts */
289
290 struct tty *tp; /* cross reference */
291
292 /* Initial state. */
293 struct termios it_in; /* should be in struct tty */
294 struct termios it_out;
295
296 /* Lock state. */
297 struct termios lt_in; /* should be in struct tty */
298 struct termios lt_out;
299
300 bool_t do_timestamp;
301 bool_t do_dcd_timestamp;
302 struct timeval timestamp;
303 struct timeval dcd_timestamp;
304
305 u_long bytes_in; /* statistics */
306 u_long bytes_out;
307 u_int delta_error_counts[CE_NTYPES];
308 u_long error_counts[CE_NTYPES];
309
310 u_int recv_exception; /* exception chars received */
311 u_int mdm; /* modem signal changes */
312 #ifdef CyDebug
313 u_int start_count; /* no. of calls to comstart() */
314 u_int start_real; /* no. of calls that did something */
315 #endif
316 u_char car; /* CD1400 CAR shadow (if first unit in cd) */
317 u_char channel_control;/* CD1400 CCR control command shadow */
318 u_char cor[3]; /* CD1400 COR1-3 shadows */
319 u_char intr_enable; /* CD1400 SRER shadow */
320
321 /*
322 * Data area for output buffers. Someday we should build the output
323 * buffer queue without copying data.
324 */
325 u_char obuf1[256];
326 u_char obuf2[256];
327 };
328
329 /* PCI driver entry point. */
330 int cyattach_common __P((cy_addr cy_iobase, int cy_align));
331 ointhand2_t siointr;
332
333 static int cy_units __P((cy_addr cy_iobase, int cy_align));
334 static int sioattach __P((struct isa_device *dev));
335 static void cd1400_channel_cmd __P((struct com_s *com, int cmd));
336 static void cd1400_channel_cmd_wait __P((struct com_s *com));
337 static void cd_etc __P((struct com_s *com, int etc));
338 static int cd_getreg __P((struct com_s *com, int reg));
339 static void cd_setreg __P((struct com_s *com, int reg, int val));
340 static timeout_t siodtrwakeup;
341 static void comhardclose __P((struct com_s *com));
342 static void sioinput __P((struct com_s *com));
343 #if 0
344 static void siointr1 __P((struct com_s *com));
345 #endif
346 static int commctl __P((struct com_s *com, int bits, int how));
347 static int comparam __P((struct tty *tp, struct termios *t));
348 static swihand_t siopoll;
349 static int sioprobe __P((struct isa_device *dev));
350 static void siosettimeout __P((void));
351 static int siosetwater __P((struct com_s *com, speed_t speed));
352 static int comspeed __P((speed_t speed, u_long cy_clock,
353 int *prescaler_io));
354 static void comstart __P((struct tty *tp));
355 static void comstop __P((struct tty *tp, int rw));
356 static timeout_t comwakeup;
357 static void disc_optim __P((struct tty *tp, struct termios *t,
358 struct com_s *com));
359
360 #ifdef CyDebug
361 void cystatus __P((int unit));
362 #endif
363
364 static char driver_name[] = "cy";
365
366 /* table and macro for fast conversion from a unit number to its com struct */
367 static struct com_s *p_com_addr[NSIO];
368 #define com_addr(unit) (p_com_addr[unit])
369
370 struct isa_driver siodriver = {
371 sioprobe, sioattach, driver_name
372 };
373
374 static d_open_t sioopen;
375 static d_close_t sioclose;
376 static d_write_t siowrite;
377 static d_ioctl_t sioioctl;
378
379 #define CDEV_MAJOR 48
380 static struct cdevsw sio_cdevsw = {
381 /* open */ sioopen,
382 /* close */ sioclose,
383 /* read */ ttyread,
384 /* write */ siowrite,
385 /* ioctl */ sioioctl,
386 /* poll */ ttypoll,
387 /* mmap */ nommap,
388 /* strategy */ nostrategy,
389 /* name */ driver_name,
390 /* maj */ CDEV_MAJOR,
391 /* dump */ nodump,
392 /* psize */ nopsize,
393 /* flags */ D_TTY | D_KQFILTER,
394 /* bmaj */ -1,
395 /* kqfilter */ ttykqfilter,
396 };
397
398 static int comconsole = -1;
399 static speed_t comdefaultrate = TTYDEF_SPEED;
400 static u_int com_events; /* input chars + weighted output completions */
401 static bool_t sio_registered;
402 static int sio_timeout;
403 static int sio_timeouts_until_log;
404 static struct callout_handle sio_timeout_handle
405 = CALLOUT_HANDLE_INITIALIZER(&sio_timeout_handle);
406 #if 0 /* XXX */
407 static struct tty *sio_tty[NSIO];
408 #else
409 static struct tty sio_tty[NSIO];
410 #endif
411 static const int nsio_tty = NSIO;
412
413 #ifdef CyDebug
414 static u_int cd_inbs;
415 static u_int cy_inbs;
416 static u_int cd_outbs;
417 static u_int cy_outbs;
418 static u_int cy_svrr_probes;
419 static u_int cy_timeouts;
420 #endif
421
422 static int cy_chip_offset[] = {
423 0x0000, 0x0400, 0x0800, 0x0c00, 0x0200, 0x0600, 0x0a00, 0x0e00,
424 };
425 static int cy_nr_cd1400s[NCY];
426 static int cy_total_devices;
427 #undef RxFifoThreshold
428 static int volatile RxFifoThreshold = (CD1400_RX_FIFO_SIZE / 2);
429
430 static int
431 sioprobe(dev)
432 struct isa_device *dev;
433 {
434 cy_addr iobase;
435
436 iobase = (cy_addr)dev->id_maddr;
437 if (iobase == NULL)
438 return (0);
439
440 /* Cyclom-16Y hardware reset (Cyclom-8Ys don't care) */
441 cy_inb(iobase, CY16_RESET, 0); /* XXX? */
442 DELAY(500); /* wait for the board to get its act together */
443
444 /* this is needed to get the board out of reset */
445 cy_outb(iobase, CY_CLEAR_INTR, 0, 0);
446 DELAY(500);
447
448 return (cy_units(iobase, 0) == 0 ? 0 : -1);
449 }
450
451 static int
452 cy_units(cy_iobase, cy_align)
453 cy_addr cy_iobase;
454 int cy_align;
455 {
456 int cyu;
457 u_char firmware_version;
458 int i;
459 cy_addr iobase;
460
461 for (cyu = 0; cyu < CY_MAX_CD1400s; ++cyu) {
462 iobase = cy_iobase + (cy_chip_offset[cyu] << cy_align);
463
464 /* wait for chip to become ready for new command */
465 for (i = 0; i < 10; i++) {
466 DELAY(50);
467 if (!cd_inb(iobase, CD1400_CCR, cy_align))
468 break;
469 }
470
471 /* clear the GFRCR register */
472 cd_outb(iobase, CD1400_GFRCR, cy_align, 0);
473
474 /* issue a reset command */
475 cd_outb(iobase, CD1400_CCR, cy_align,
476 CD1400_CCR_CMDRESET | CD1400_CCR_FULLRESET);
477
478 /* wait for the CD1400 to initialize itself */
479 for (i = 0; i < 200; i++) {
480 DELAY(50);
481
482 /* retrieve firmware version */
483 firmware_version = cd_inb(iobase, CD1400_GFRCR,
484 cy_align);
485 if ((firmware_version & 0xf0) == 0x40)
486 break;
487 }
488
489 /*
490 * Anything in the 0x40-0x4F range is fine.
491 * If one CD1400 is bad then we don't support higher
492 * numbered good ones on this board.
493 */
494 if ((firmware_version & 0xf0) != 0x40)
495 break;
496 }
497 return (cyu);
498 }
499
500 static int
501 sioattach(isdp)
502 struct isa_device *isdp;
503 {
504 int adapter;
505
506 adapter = cyattach_common((cy_addr) isdp->id_maddr, 0);
507 if (adapter < 0)
508 return (0);
509
510 /*
511 * XXX
512 * This kludge is to allow ISA/PCI device specifications in the
513 * kernel config file to be in any order.
514 */
515 if (isdp->id_unit != adapter) {
516 printf("cy%d: attached as cy%d\n", isdp->id_unit, adapter);
517 isdp->id_unit = adapter; /* XXX */
518 }
519 isdp->id_ointr = siointr;
520 /* isdp->id_ri_flags |= RI_FAST; XXX unimplemented - use newbus! */
521 return (1);
522 }
523
524 int
525 cyattach_common(cy_iobase, cy_align)
526 cy_addr cy_iobase;
527 int cy_align;
528 {
529 int adapter;
530 int cyu;
531 u_char firmware_version;
532 cy_addr iobase;
533 int minorbase;
534 int ncyu;
535 int unit;
536
537 adapter = cy_total_devices;
538 if ((u_int)adapter >= NCY) {
539 printf(
540 "cy%d: can't attach adapter: insufficient cy devices configured\n",
541 adapter);
542 return (-1);
543 }
544 ncyu = cy_units(cy_iobase, cy_align);
545 if (ncyu == 0)
546 return (-1);
547 cy_nr_cd1400s[adapter] = ncyu;
548 cy_total_devices++;
549
550 unit = adapter * CY_MAX_PORTS;
551 for (cyu = 0; cyu < ncyu; ++cyu) {
552 int cdu;
553
554 iobase = (cy_addr) (cy_iobase
555 + (cy_chip_offset[cyu] << cy_align));
556 firmware_version = cd_inb(iobase, CD1400_GFRCR, cy_align);
557
558 /* Set up a receive timeout period of than 1+ ms. */
559 cd_outb(iobase, CD1400_PPR, cy_align,
560 howmany(CY_CLOCK(firmware_version)
561 / CD1400_PPR_PRESCALER, 1000));
562
563 for (cdu = 0; cdu < CD1400_NO_OF_CHANNELS; ++cdu, ++unit) {
564 struct com_s *com;
565 int s;
566
567 com = malloc(sizeof *com, M_DEVBUF, M_NOWAIT);
568 if (com == NULL)
569 break;
570 bzero(com, sizeof *com);
571 com->unit = unit;
572 com->gfrcr_image = firmware_version;
573 if (CY_RTS_DTR_SWAPPED(firmware_version)) {
574 com->mcr_dtr = MCR_RTS;
575 com->mcr_rts = MCR_DTR;
576 com->mcr_rts_reg = CD1400_MSVR2;
577 } else {
578 com->mcr_dtr = MCR_DTR;
579 com->mcr_rts = MCR_RTS;
580 com->mcr_rts_reg = CD1400_MSVR1;
581 }
582 com->dtr_wait = 3 * hz;
583 com->obufs[0].l_head = com->obuf1;
584 com->obufs[1].l_head = com->obuf2;
585
586 com->cy_align = cy_align;
587 com->cy_iobase = cy_iobase;
588 com->iobase = iobase;
589 com->car = ~CD1400_CAR_CHAN;
590
591 /*
592 * We don't use all the flags from <sys/ttydefaults.h> since they
593 * are only relevant for logins. It's important to have echo off
594 * initially so that the line doesn't start blathering before the
595 * echo flag can be turned off.
596 */
597 com->it_in.c_iflag = 0;
598 com->it_in.c_oflag = 0;
599 com->it_in.c_cflag = TTYDEF_CFLAG;
600 com->it_in.c_lflag = 0;
601 if (unit == comconsole) {
602 com->it_in.c_iflag = TTYDEF_IFLAG;
603 com->it_in.c_oflag = TTYDEF_OFLAG;
604 com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL;
605 com->it_in.c_lflag = TTYDEF_LFLAG;
606 com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL;
607 }
608 if (siosetwater(com, com->it_in.c_ispeed) != 0) {
609 enable_intr();
610 free(com, M_DEVBUF);
611 return (0);
612 }
613 enable_intr();
614 termioschars(&com->it_in);
615 com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate;
616 com->it_out = com->it_in;
617
618 s = spltty();
619 com_addr(unit) = com;
620 splx(s);
621
622 if (!sio_registered) {
623 cdevsw_add(&sio_cdevsw);
624 register_swi(SWI_TTY, siopoll);
625 sio_registered = TRUE;
626 }
627 minorbase = UNIT_TO_MINOR(unit);
628 make_dev(&sio_cdevsw, minorbase,
629 UID_ROOT, GID_WHEEL, 0600, "ttyc%r%r", adapter,
630 unit % CY_MAX_PORTS);
631 make_dev(&sio_cdevsw, minorbase | CONTROL_INIT_STATE,
632 UID_ROOT, GID_WHEEL, 0600, "ttyic%r%r", adapter,
633 unit % CY_MAX_PORTS);
634 make_dev(&sio_cdevsw, minorbase | CONTROL_LOCK_STATE,
635 UID_ROOT, GID_WHEEL, 0600, "ttylc%r%r", adapter,
636 unit % CY_MAX_PORTS);
637 make_dev(&sio_cdevsw, minorbase | CALLOUT_MASK,
638 UID_UUCP, GID_DIALER, 0660, "cuac%r%r", adapter,
639 unit % CY_MAX_PORTS);
640 make_dev(&sio_cdevsw, minorbase | CALLOUT_MASK | CONTROL_INIT_STATE,
641 UID_UUCP, GID_DIALER, 0660, "cuaic%r%r", adapter,
642 unit % CY_MAX_PORTS);
643 make_dev(&sio_cdevsw, minorbase | CALLOUT_MASK | CONTROL_LOCK_STATE,
644 UID_UUCP, GID_DIALER, 0660, "cualc%r%r", adapter,
645 unit % CY_MAX_PORTS);
646 }
647 }
648
649 /* ensure an edge for the next interrupt */
650 cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
651
652 return (adapter);
653 }
654
655 static int
656 sioopen(dev, flag, mode, p)
657 dev_t dev;
658 int flag;
659 int mode;
660 struct proc *p;
661 {
662 struct com_s *com;
663 int error;
664 int mynor;
665 int s;
666 struct tty *tp;
667 int unit;
668
669 mynor = minor(dev);
670 unit = MINOR_TO_UNIT(mynor);
671 if ((u_int) unit >= NSIO || (com = com_addr(unit)) == NULL)
672 return (ENXIO);
673 if (mynor & CONTROL_MASK)
674 return (0);
675 #if 0 /* XXX */
676 tp = com->tp = sio_tty[unit] = ttymalloc(sio_tty[unit]);
677 #else
678 tp = com->tp = &sio_tty[unit];
679 #endif
680 dev->si_tty = tp;
681 s = spltty();
682 /*
683 * We jump to this label after all non-interrupted sleeps to pick
684 * up any changes of the device state.
685 */
686 open_top:
687 while (com->state & CS_DTR_OFF) {
688 error = tsleep(&com->dtr_wait, TTIPRI | PCATCH, "cydtr", 0);
689 if (error != 0)
690 goto out;
691 }
692 if (tp->t_state & TS_ISOPEN) {
693 /*
694 * The device is open, so everything has been initialized.
695 * Handle conflicts.
696 */
697 if (mynor & CALLOUT_MASK) {
698 if (!com->active_out) {
699 error = EBUSY;
700 goto out;
701 }
702 } else {
703 if (com->active_out) {
704 if (flag & O_NONBLOCK) {
705 error = EBUSY;
706 goto out;
707 }
708 error = tsleep(&com->active_out,
709 TTIPRI | PCATCH, "cybi", 0);
710 if (error != 0)
711 goto out;
712 goto open_top;
713 }
714 }
715 if (tp->t_state & TS_XCLUDE &&
716 suser(p)) {
717 error = EBUSY;
718 goto out;
719 }
720 } else {
721 /*
722 * The device isn't open, so there are no conflicts.
723 * Initialize it. Initialization is done twice in many
724 * cases: to preempt sleeping callin opens if we are
725 * callout, and to complete a callin open after DCD rises.
726 */
727 tp->t_oproc = comstart;
728 tp->t_stop = comstop;
729 tp->t_param = comparam;
730 tp->t_dev = dev;
731 tp->t_termios = mynor & CALLOUT_MASK
732 ? com->it_out : com->it_in;
733
734 /* Encode per-board unit in LIVR for access in intr routines. */
735 cd_setreg(com, CD1400_LIVR,
736 (unit & CD1400_xIVR_CHAN) << CD1400_xIVR_CHAN_SHIFT);
737
738 (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET);
739 #if 0
740 com->poll = com->no_irq;
741 com->poll_output = com->loses_outints;
742 #endif
743 ++com->wopeners;
744 error = comparam(tp, &tp->t_termios);
745 --com->wopeners;
746 if (error != 0)
747 goto out;
748 #if 0
749 if (com->hasfifo) {
750 /*
751 * (Re)enable and flush fifos.
752 *
753 * Certain SMC chips cause problems if the fifos
754 * are enabled while input is ready. Turn off the
755 * fifo if necessary to clear the input. We test
756 * the input ready bit after enabling the fifos
757 * since we've already enabled them in comparam()
758 * and to handle races between enabling and fresh
759 * input.
760 */
761 while (TRUE) {
762 outb(iobase + com_fifo,
763 FIFO_RCV_RST | FIFO_XMT_RST
764 | com->fifo_image);
765 DELAY(100);
766 if (!(inb(com->line_status_port) & LSR_RXRDY))
767 break;
768 outb(iobase + com_fifo, 0);
769 DELAY(100);
770 (void) inb(com->data_port);
771 }
772 }
773
774 disable_intr();
775 (void) inb(com->line_status_port);
776 (void) inb(com->data_port);
777 com->prev_modem_status = com->last_modem_status
778 = inb(com->modem_status_port);
779 outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS
780 | IER_EMSC);
781 enable_intr();
782 #else /* !0 */
783 /*
784 * Flush fifos. This requires a full channel reset which
785 * also disables the transmitter and receiver. Recover
786 * from this.
787 */
788 cd1400_channel_cmd(com,
789 CD1400_CCR_CMDRESET | CD1400_CCR_CHANRESET);
790 cd1400_channel_cmd(com, com->channel_control);
791
792 disable_intr();
793 com->prev_modem_status = com->last_modem_status
794 = cd_getreg(com, CD1400_MSVR2);
795 cd_setreg(com, CD1400_SRER,
796 com->intr_enable
797 = CD1400_SRER_MDMCH | CD1400_SRER_RXDATA);
798 enable_intr();
799 #endif /* 0 */
800 /*
801 * Handle initial DCD. Callout devices get a fake initial
802 * DCD (trapdoor DCD). If we are callout, then any sleeping
803 * callin opens get woken up and resume sleeping on "cybi"
804 * instead of "cydcd".
805 */
806 /*
807 * XXX `mynor & CALLOUT_MASK' should be
808 * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where
809 * TRAPDOOR_CARRIER is the default initial state for callout
810 * devices and SOFT_CARRIER is like CLOCAL except it hides
811 * the true carrier.
812 */
813 if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK)
814 (*linesw[tp->t_line].l_modem)(tp, 1);
815 }
816 /*
817 * Wait for DCD if necessary.
818 */
819 if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK)
820 && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) {
821 ++com->wopeners;
822 error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "cydcd", 0);
823 --com->wopeners;
824 if (error != 0)
825 goto out;
826 goto open_top;
827 }
828 error = (*linesw[tp->t_line].l_open)(dev, tp);
829 disc_optim(tp, &tp->t_termios, com);
830 if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
831 com->active_out = TRUE;
832 siosettimeout();
833 out:
834 splx(s);
835 if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0)
836 comhardclose(com);
837 return (error);
838 }
839
840 static int
841 sioclose(dev, flag, mode, p)
842 dev_t dev;
843 int flag;
844 int mode;
845 struct proc *p;
846 {
847 struct com_s *com;
848 int mynor;
849 int s;
850 struct tty *tp;
851
852 mynor = minor(dev);
853 if (mynor & CONTROL_MASK)
854 return (0);
855 com = com_addr(MINOR_TO_UNIT(mynor));
856 tp = com->tp;
857 s = spltty();
858 cd_etc(com, CD1400_ETC_STOPBREAK);
859 (*linesw[tp->t_line].l_close)(tp, flag);
860 disc_optim(tp, &tp->t_termios, com);
861 comstop(tp, FREAD | FWRITE);
862 comhardclose(com);
863 ttyclose(tp);
864 siosettimeout();
865 splx(s);
866 #ifdef broken /* session holds a ref to the tty; can't deallocate */
867 ttyfree(tp);
868 com->tp = sio_tty[unit] = NULL;
869 #endif
870 return (0);
871 }
872
873 static void
874 comhardclose(com)
875 struct com_s *com;
876 {
877 cy_addr iobase;
878 int s;
879 struct tty *tp;
880 int unit;
881
882 unit = com->unit;
883 iobase = com->iobase;
884 s = spltty();
885 #if 0
886 com->poll = FALSE;
887 com->poll_output = FALSE;
888 #endif
889 com->do_timestamp = 0;
890 #if 0
891 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
892 #else
893 /* XXX */
894 disable_intr();
895 com->etc = ETC_NONE;
896 cd_setreg(com, CD1400_COR2, com->cor[1] &= ~CD1400_COR2_ETC);
897 enable_intr();
898 cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
899 #endif
900
901 {
902 #if 0
903 outb(iobase + com_ier, 0);
904 #else
905 disable_intr();
906 cd_setreg(com, CD1400_SRER, com->intr_enable = 0);
907 enable_intr();
908 #endif
909 tp = com->tp;
910 if ((tp->t_cflag & HUPCL)
911 /*
912 * XXX we will miss any carrier drop between here and the
913 * next open. Perhaps we should watch DCD even when the
914 * port is closed; it is not sufficient to check it at
915 * the next open because it might go up and down while
916 * we're not watching.
917 */
918 || (!com->active_out
919 && !(com->prev_modem_status & MSR_DCD)
920 && !(com->it_in.c_cflag & CLOCAL))
921 || !(tp->t_state & TS_ISOPEN)) {
922 (void)commctl(com, TIOCM_DTR, DMBIC);
923
924 /* Disable receiver (leave transmitter enabled). */
925 com->channel_control = CD1400_CCR_CMDCHANCTL
926 | CD1400_CCR_XMTEN
927 | CD1400_CCR_RCVDIS;
928 cd1400_channel_cmd(com, com->channel_control);
929
930 if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) {
931 timeout(siodtrwakeup, com, com->dtr_wait);
932 com->state |= CS_DTR_OFF;
933 }
934 }
935 }
936 #if 0
937 if (com->hasfifo) {
938 /*
939 * Disable fifos so that they are off after controlled
940 * reboots. Some BIOSes fail to detect 16550s when the
941 * fifos are enabled.
942 */
943 outb(iobase + com_fifo, 0);
944 }
945 #endif
946 com->active_out = FALSE;
947 wakeup(&com->active_out);
948 wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */
949 splx(s);
950 }
951
952 static int
953 siowrite(dev, uio, flag)
954 dev_t dev;
955 struct uio *uio;
956 int flag;
957 {
958 int mynor;
959 struct tty *tp;
960 int unit;
961
962 mynor = minor(dev);
963 if (mynor & CONTROL_MASK)
964 return (ENODEV);
965
966 unit = MINOR_TO_UNIT(mynor);
967 tp = com_addr(unit)->tp;
968 /*
969 * (XXX) We disallow virtual consoles if the physical console is
970 * a serial port. This is in case there is a display attached that
971 * is not the console. In that situation we don't need/want the X
972 * server taking over the console.
973 */
974 if (constty != NULL && unit == comconsole)
975 constty = NULL;
976 #ifdef Smarts
977 /* XXX duplicate ttwrite(), but without so much output processing on
978 * CR & LF chars. Hardly worth the effort, given that high-throughput
979 * sessions are raw anyhow.
980 */
981 #else
982 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
983 #endif
984 }
985
986 static void
987 siodtrwakeup(chan)
988 void *chan;
989 {
990 struct com_s *com;
991
992 com = (struct com_s *)chan;
993 com->state &= ~CS_DTR_OFF;
994 wakeup(&com->dtr_wait);
995 }
996
997 static void
998 sioinput(com)
999 struct com_s *com;
1000 {
1001 u_char *buf;
1002 int incc;
1003 u_char line_status;
1004 int recv_data;
1005 struct tty *tp;
1006
1007 buf = com->ibuf;
1008 tp = com->tp;
1009 if (!(tp->t_state & TS_ISOPEN)) {
1010 com_events -= (com->iptr - com->ibuf);
1011 com->iptr = com->ibuf;
1012 return;
1013 }
1014 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1015 /*
1016 * Avoid the grotesquely inefficient lineswitch routine
1017 * (ttyinput) in "raw" mode. It usually takes about 450
1018 * instructions (that's without canonical processing or echo!).
1019 * slinput is reasonably fast (usually 40 instructions plus
1020 * call overhead).
1021 */
1022 do {
1023 enable_intr();
1024 incc = com->iptr - buf;
1025 if (tp->t_rawq.c_cc + incc > tp->t_ihiwat
1026 && (com->state & CS_RTS_IFLOW
1027 || tp->t_iflag & IXOFF)
1028 && !(tp->t_state & TS_TBLOCK))
1029 ttyblock(tp);
1030 com->delta_error_counts[CE_TTY_BUF_OVERFLOW]
1031 += b_to_q((char *)buf, incc, &tp->t_rawq);
1032 buf += incc;
1033 tk_nin += incc;
1034 tk_rawcc += incc;
1035 tp->t_rawcc += incc;
1036 ttwakeup(tp);
1037 if (tp->t_state & TS_TTSTOP
1038 && (tp->t_iflag & IXANY
1039 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1040 tp->t_state &= ~TS_TTSTOP;
1041 tp->t_lflag &= ~FLUSHO;
1042 comstart(tp);
1043 }
1044 disable_intr();
1045 } while (buf < com->iptr);
1046 } else {
1047 do {
1048 enable_intr();
1049 line_status = buf[com->ierroff];
1050 recv_data = *buf++;
1051 if (line_status
1052 & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) {
1053 if (line_status & LSR_BI)
1054 recv_data |= TTY_BI;
1055 if (line_status & LSR_FE)
1056 recv_data |= TTY_FE;
1057 if (line_status & LSR_OE)
1058 recv_data |= TTY_OE;
1059 if (line_status & LSR_PE)
1060 recv_data |= TTY_PE;
1061 }
1062 (*linesw[tp->t_line].l_rint)(recv_data, tp);
1063 disable_intr();
1064 } while (buf < com->iptr);
1065 }
1066 com_events -= (com->iptr - com->ibuf);
1067 com->iptr = com->ibuf;
1068
1069 /*
1070 * There is now room for another low-level buffer full of input,
1071 * so enable RTS if it is now disabled and there is room in the
1072 * high-level buffer.
1073 */
1074 if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & com->mcr_rts) &&
1075 !(tp->t_state & TS_TBLOCK))
1076 #if 0
1077 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
1078 #else
1079 cd_setreg(com, com->mcr_rts_reg,
1080 com->mcr_image |= com->mcr_rts);
1081 #endif
1082 }
1083
1084 void
1085 siointr(unit)
1086 int unit;
1087 {
1088 int baseu;
1089 int cy_align;
1090 cy_addr cy_iobase;
1091 int cyu;
1092 cy_addr iobase;
1093 u_char status;
1094
1095 COM_LOCK(); /* XXX could this be placed down lower in the loop? */
1096
1097 baseu = unit * CY_MAX_PORTS;
1098 cy_align = com_addr(baseu)->cy_align;
1099 cy_iobase = com_addr(baseu)->cy_iobase;
1100
1101 /* check each CD1400 in turn */
1102 for (cyu = 0; cyu < cy_nr_cd1400s[unit]; ++cyu) {
1103 iobase = (cy_addr) (cy_iobase
1104 + (cy_chip_offset[cyu] << cy_align));
1105 /* poll to see if it has any work */
1106 status = cd_inb(iobase, CD1400_SVRR, cy_align);
1107 if (status == 0)
1108 continue;
1109 #ifdef CyDebug
1110 ++cy_svrr_probes;
1111 #endif
1112 /* service requests as appropriate, giving priority to RX */
1113 if (status & CD1400_SVRR_RXRDY) {
1114 struct com_s *com;
1115 u_int count;
1116 u_char *ioptr;
1117 u_char line_status;
1118 u_char recv_data;
1119 u_char serv_type;
1120 #ifdef PollMode
1121 u_char save_rir;
1122 #endif
1123
1124 #ifdef PollMode
1125 save_rir = cd_inb(iobase, CD1400_RIR, cy_align);
1126
1127 /* enter rx service */
1128 cd_outb(iobase, CD1400_CAR, cy_align, save_rir);
1129 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1130 = save_rir & CD1400_CAR_CHAN;
1131
1132 serv_type = cd_inb(iobase, CD1400_RIVR, cy_align);
1133 com = com_addr(baseu
1134 + ((serv_type >> CD1400_xIVR_CHAN_SHIFT)
1135 & CD1400_xIVR_CHAN));
1136 #else
1137 /* ack receive service */
1138 serv_type = cy_inb(iobase, CY8_SVCACKR, cy_align);
1139
1140 com = com_addr(baseu +
1141 + ((serv_type >> CD1400_xIVR_CHAN_SHIFT)
1142 & CD1400_xIVR_CHAN));
1143 #endif
1144
1145 if (serv_type & CD1400_RIVR_EXCEPTION) {
1146 ++com->recv_exception;
1147 line_status = cd_inb(iobase, CD1400_RDSR, cy_align);
1148 /* break/unnattached error bits or real input? */
1149 recv_data = cd_inb(iobase, CD1400_RDSR, cy_align);
1150 #ifndef SOFT_HOTCHAR
1151 if (line_status & CD1400_RDSR_SPECIAL
1152 && com->hotchar != 0)
1153 setsofttty();
1154 #endif
1155 #if 1 /* XXX "intelligent" PFO error handling would break O error handling */
1156 if (line_status & (LSR_PE|LSR_FE|LSR_BI)) {
1157 /*
1158 Don't store PE if IGNPAR and BI if IGNBRK,
1159 this hack allows "raw" tty optimization
1160 works even if IGN* is set.
1161 */
1162 if ( com->tp == NULL
1163 || !(com->tp->t_state & TS_ISOPEN)
1164 || ((line_status & (LSR_PE|LSR_FE))
1165 && (com->tp->t_iflag & IGNPAR))
1166 || ((line_status & LSR_BI)
1167 && (com->tp->t_iflag & IGNBRK)))
1168 goto cont;
1169 if ( (line_status & (LSR_PE|LSR_FE))
1170 && (com->tp->t_state & TS_CAN_BYPASS_L_RINT)
1171 && ((line_status & LSR_FE)
1172 || ((line_status & LSR_PE)
1173 && (com->tp->t_iflag & INPCK))))
1174 recv_data = 0;
1175 }
1176 #endif /* 1 */
1177 ++com->bytes_in;
1178 #ifdef SOFT_HOTCHAR
1179 if (com->hotchar != 0 && recv_data == com->hotchar)
1180 setsofttty();
1181 #endif
1182 ioptr = com->iptr;
1183 if (ioptr >= com->ibufend)
1184 CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW);
1185 else {
1186 if (com->do_timestamp)
1187 microtime(&com->timestamp);
1188 ++com_events;
1189 ioptr[0] = recv_data;
1190 ioptr[com->ierroff] = line_status;
1191 com->iptr = ++ioptr;
1192 if (ioptr == com->ihighwater
1193 && com->state & CS_RTS_IFLOW)
1194 #if 0
1195 outb(com->modem_ctl_port,
1196 com->mcr_image &= ~MCR_RTS);
1197 #else
1198 cd_outb(iobase, com->mcr_rts_reg,
1199 cy_align,
1200 com->mcr_image &=
1201 ~com->mcr_rts);
1202 #endif
1203 if (line_status & LSR_OE)
1204 CE_RECORD(com, CE_OVERRUN);
1205 }
1206 goto cont;
1207 } else {
1208 int ifree;
1209
1210 count = cd_inb(iobase, CD1400_RDCR, cy_align);
1211 if (!count)
1212 goto cont;
1213 com->bytes_in += count;
1214 ioptr = com->iptr;
1215 ifree = com->ibufend - ioptr;
1216 if (count > ifree) {
1217 count -= ifree;
1218 com_events += ifree;
1219 if (ifree != 0) {
1220 if (com->do_timestamp)
1221 microtime(&com->timestamp);
1222 do {
1223 recv_data = cd_inb(iobase,
1224 CD1400_RDSR,
1225 cy_align);
1226 #ifdef SOFT_HOTCHAR
1227 if (com->hotchar != 0
1228 && recv_data
1229 == com->hotchar)
1230 setsofttty();
1231 #endif
1232 ioptr[0] = recv_data;
1233 ioptr[com->ierroff] = 0;
1234 ++ioptr;
1235 } while (--ifree != 0);
1236 }
1237 com->delta_error_counts
1238 [CE_INTERRUPT_BUF_OVERFLOW] += count;
1239 do {
1240 recv_data = cd_inb(iobase, CD1400_RDSR,
1241 cy_align);
1242 #ifdef SOFT_HOTCHAR
1243 if (com->hotchar != 0
1244 && recv_data == com->hotchar)
1245 setsofttty();
1246 #endif
1247 } while (--count != 0);
1248 } else {
1249 if (com->do_timestamp)
1250 microtime(&com->timestamp);
1251 if (ioptr <= com->ihighwater
1252 && ioptr + count > com->ihighwater
1253 && com->state & CS_RTS_IFLOW)
1254 #if 0
1255 outb(com->modem_ctl_port,
1256 com->mcr_image &= ~MCR_RTS);
1257 #else
1258 cd_outb(iobase, com->mcr_rts_reg,
1259 cy_align,
1260 com->mcr_image
1261 &= ~com->mcr_rts);
1262 #endif
1263 com_events += count;
1264 do {
1265 recv_data = cd_inb(iobase, CD1400_RDSR,
1266 cy_align);
1267 #ifdef SOFT_HOTCHAR
1268 if (com->hotchar != 0
1269 && recv_data == com->hotchar)
1270 setsofttty();
1271 #endif
1272 ioptr[0] = recv_data;
1273 ioptr[com->ierroff] = 0;
1274 ++ioptr;
1275 } while (--count != 0);
1276 }
1277 com->iptr = ioptr;
1278 }
1279 cont:
1280
1281 /* terminate service context */
1282 #ifdef PollMode
1283 cd_outb(iobase, CD1400_RIR, cy_align,
1284 save_rir
1285 & ~(CD1400_RIR_RDIREQ | CD1400_RIR_RBUSY));
1286 #else
1287 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1288 #endif
1289 }
1290 if (status & CD1400_SVRR_MDMCH) {
1291 struct com_s *com;
1292 u_char modem_status;
1293 #ifdef PollMode
1294 u_char save_mir;
1295 #else
1296 u_char vector;
1297 #endif
1298
1299 #ifdef PollMode
1300 save_mir = cd_inb(iobase, CD1400_MIR, cy_align);
1301
1302 /* enter modem service */
1303 cd_outb(iobase, CD1400_CAR, cy_align, save_mir);
1304 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1305 = save_mir & CD1400_CAR_CHAN;
1306
1307 com = com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS
1308 + (save_mir & CD1400_MIR_CHAN));
1309 #else
1310 /* ack modem service */
1311 vector = cy_inb(iobase, CY8_SVCACKM, cy_align);
1312
1313 com = com_addr(baseu
1314 + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1315 & CD1400_xIVR_CHAN));
1316 #endif
1317 ++com->mdm;
1318 modem_status = cd_inb(iobase, CD1400_MSVR2, cy_align);
1319 if (modem_status != com->last_modem_status) {
1320 if (com->do_dcd_timestamp
1321 && !(com->last_modem_status & MSR_DCD)
1322 && modem_status & MSR_DCD)
1323 microtime(&com->dcd_timestamp);
1324
1325 /*
1326 * Schedule high level to handle DCD changes. Note
1327 * that we don't use the delta bits anywhere. Some
1328 * UARTs mess them up, and it's easy to remember the
1329 * previous bits and calculate the delta.
1330 */
1331 com->last_modem_status = modem_status;
1332 if (!(com->state & CS_CHECKMSR)) {
1333 com_events += LOTS_OF_EVENTS;
1334 com->state |= CS_CHECKMSR;
1335 setsofttty();
1336 }
1337
1338 #ifdef SOFT_CTS_OFLOW
1339 /* handle CTS change immediately for crisp flow ctl */
1340 if (com->state & CS_CTS_OFLOW) {
1341 if (modem_status & MSR_CTS) {
1342 com->state |= CS_ODEVREADY;
1343 if (com->state >= (CS_BUSY | CS_TTGO
1344 | CS_ODEVREADY)
1345 && !(com->intr_enable
1346 & CD1400_SRER_TXRDY))
1347 cd_outb(iobase, CD1400_SRER,
1348 cy_align,
1349 com->intr_enable
1350 = com->intr_enable
1351 & ~CD1400_SRER_TXMPTY
1352 | CD1400_SRER_TXRDY);
1353 } else {
1354 com->state &= ~CS_ODEVREADY;
1355 if (com->intr_enable
1356 & CD1400_SRER_TXRDY)
1357 cd_outb(iobase, CD1400_SRER,
1358 cy_align,
1359 com->intr_enable
1360 = com->intr_enable
1361 & ~CD1400_SRER_TXRDY
1362 | CD1400_SRER_TXMPTY);
1363 }
1364 }
1365 #endif
1366 }
1367
1368 /* terminate service context */
1369 #ifdef PollMode
1370 cd_outb(iobase, CD1400_MIR, cy_align,
1371 save_mir
1372 & ~(CD1400_MIR_RDIREQ | CD1400_MIR_RBUSY));
1373 #else
1374 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1375 #endif
1376 }
1377 if (status & CD1400_SVRR_TXRDY) {
1378 struct com_s *com;
1379 #ifdef PollMode
1380 u_char save_tir;
1381 #else
1382 u_char vector;
1383 #endif
1384
1385 #ifdef PollMode
1386 save_tir = cd_inb(iobase, CD1400_TIR, cy_align);
1387
1388 /* enter tx service */
1389 cd_outb(iobase, CD1400_CAR, cy_align, save_tir);
1390 com_addr(baseu + cyu * CD1400_NO_OF_CHANNELS)->car
1391 = save_tir & CD1400_CAR_CHAN;
1392
1393 com = com_addr(baseu
1394 + cyu * CD1400_NO_OF_CHANNELS
1395 + (save_tir & CD1400_TIR_CHAN));
1396 #else
1397 /* ack transmit service */
1398 vector = cy_inb(iobase, CY8_SVCACKT, cy_align);
1399
1400 com = com_addr(baseu
1401 + ((vector >> CD1400_xIVR_CHAN_SHIFT)
1402 & CD1400_xIVR_CHAN));
1403 #endif
1404
1405 if (com->etc != ETC_NONE) {
1406 if (com->intr_enable & CD1400_SRER_TXRDY) {
1407 /*
1408 * Here due to sloppy SRER_TXRDY
1409 * enabling. Ignore. Come back when
1410 * tx is empty.
1411 */
1412 cd_outb(iobase, CD1400_SRER, cy_align,
1413 com->intr_enable
1414 = (com->intr_enable
1415 & ~CD1400_SRER_TXRDY)
1416 | CD1400_SRER_TXMPTY);
1417 goto terminate_tx_service;
1418 }
1419 switch (com->etc) {
1420 case CD1400_ETC_SENDBREAK:
1421 case CD1400_ETC_STOPBREAK:
1422 /*
1423 * Start the command. Come back on
1424 * next tx empty interrupt, hopefully
1425 * after command has been executed.
1426 */
1427 cd_outb(iobase, CD1400_COR2, cy_align,
1428 com->cor[1] |= CD1400_COR2_ETC);
1429 cd_outb(iobase, CD1400_TDR, cy_align,
1430 CD1400_ETC_CMD);
1431 cd_outb(iobase, CD1400_TDR, cy_align,
1432 com->etc);
1433 if (com->etc == CD1400_ETC_SENDBREAK)
1434 com->etc = ETC_BREAK_STARTING;
1435 else
1436 com->etc = ETC_BREAK_ENDING;
1437 goto terminate_tx_service;
1438 case ETC_BREAK_STARTING:
1439 /*
1440 * BREAK is now on. Continue with
1441 * SRER_TXMPTY processing, hopefully
1442 * don't come back.
1443 */
1444 com->etc = ETC_BREAK_STARTED;
1445 break;
1446 case ETC_BREAK_STARTED:
1447 /*
1448 * Came back due to sloppy SRER_TXMPTY
1449 * enabling. Hope again.
1450 */
1451 break;
1452 case ETC_BREAK_ENDING:
1453 /*
1454 * BREAK is now off. Continue with
1455 * SRER_TXMPTY processing and don't
1456 * come back. The SWI handler will
1457 * restart tx interrupts if necessary.
1458 */
1459 cd_outb(iobase, CD1400_COR2, cy_align,
1460 com->cor[1]
1461 &= ~CD1400_COR2_ETC);
1462 com->etc = ETC_BREAK_ENDED;
1463 if (!(com->state & CS_ODONE)) {
1464 com_events += LOTS_OF_EVENTS;
1465 com->state |= CS_ODONE;
1466 setsofttty();
1467 }
1468 break;
1469 case ETC_BREAK_ENDED:
1470 /*
1471 * Shouldn't get here. Hope again.
1472 */
1473 break;
1474 }
1475 }
1476 if (com->intr_enable & CD1400_SRER_TXMPTY) {
1477 if (!(com->extra_state & CSE_ODONE)) {
1478 com_events += LOTS_OF_EVENTS;
1479 com->extra_state |= CSE_ODONE;
1480 setsofttty();
1481 }
1482 cd_outb(iobase, CD1400_SRER, cy_align,
1483 com->intr_enable
1484 &= ~CD1400_SRER_TXMPTY);
1485 goto terminate_tx_service;
1486 }
1487 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
1488 u_char *ioptr;
1489 u_int ocount;
1490
1491 ioptr = com->obufq.l_head;
1492 ocount = com->obufq.l_tail - ioptr;
1493 if (ocount > CD1400_TX_FIFO_SIZE)
1494 ocount = CD1400_TX_FIFO_SIZE;
1495 com->bytes_out += ocount;
1496 do
1497 cd_outb(iobase, CD1400_TDR, cy_align,
1498 *ioptr++);
1499 while (--ocount != 0);
1500 com->obufq.l_head = ioptr;
1501 if (ioptr >= com->obufq.l_tail) {
1502 struct lbq *qp;
1503
1504 qp = com->obufq.l_next;
1505 qp->l_queued = FALSE;
1506 qp = qp->l_next;
1507 if (qp != NULL) {
1508 com->obufq.l_head = qp->l_head;
1509 com->obufq.l_tail = qp->l_tail;
1510 com->obufq.l_next = qp;
1511 } else {
1512 /* output just completed */
1513 com->state &= ~CS_BUSY;
1514
1515 /*
1516 * The setting of CSE_ODONE may be
1517 * stale here. We currently only
1518 * use it when CS_BUSY is set, and
1519 * fixing it when we clear CS_BUSY
1520 * is easiest.
1521 */
1522 if (com->extra_state & CSE_ODONE) {
1523 com_events -= LOTS_OF_EVENTS;
1524 com->extra_state &= ~CSE_ODONE;
1525 }
1526
1527 cd_outb(iobase, CD1400_SRER, cy_align,
1528 com->intr_enable
1529 = (com->intr_enable
1530 & ~CD1400_SRER_TXRDY)
1531 | CD1400_SRER_TXMPTY);
1532 }
1533 if (!(com->state & CS_ODONE)) {
1534 com_events += LOTS_OF_EVENTS;
1535 com->state |= CS_ODONE;
1536
1537 /* handle at high level ASAP */
1538 setsofttty();
1539 }
1540 }
1541 }
1542
1543 /* terminate service context */
1544 terminate_tx_service:
1545 #ifdef PollMode
1546 cd_outb(iobase, CD1400_TIR, cy_align,
1547 save_tir
1548 & ~(CD1400_TIR_RDIREQ | CD1400_TIR_RBUSY));
1549 #else
1550 cd_outb(iobase, CD1400_EOSRR, cy_align, 0);
1551 #endif
1552 }
1553 }
1554
1555 /* ensure an edge for the next interrupt */
1556 cy_outb(cy_iobase, CY_CLEAR_INTR, cy_align, 0);
1557
1558 schedsofttty();
1559
1560 COM_UNLOCK();
1561 }
1562
1563 #if 0
1564 static void
1565 siointr1(com)
1566 struct com_s *com;
1567 {
1568 }
1569 #endif
1570
1571 static int
1572 sioioctl(dev, cmd, data, flag, p)
1573 dev_t dev;
1574 u_long cmd;
1575 caddr_t data;
1576 int flag;
1577 struct proc *p;
1578 {
1579 struct com_s *com;
1580 int error;
1581 int mynor;
1582 int s;
1583 struct tty *tp;
1584 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1585 int oldcmd;
1586 struct termios term;
1587 #endif
1588
1589 mynor = minor(dev);
1590 com = com_addr(MINOR_TO_UNIT(mynor));
1591 if (mynor & CONTROL_MASK) {
1592 struct termios *ct;
1593
1594 switch (mynor & CONTROL_MASK) {
1595 case CONTROL_INIT_STATE:
1596 ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in;
1597 break;
1598 case CONTROL_LOCK_STATE:
1599 ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in;
1600 break;
1601 default:
1602 return (ENODEV); /* /dev/nodev */
1603 }
1604 switch (cmd) {
1605 case TIOCSETA:
1606 error = suser(p);
1607 if (error != 0)
1608 return (error);
1609 *ct = *(struct termios *)data;
1610 return (0);
1611 case TIOCGETA:
1612 *(struct termios *)data = *ct;
1613 return (0);
1614 case TIOCGETD:
1615 *(int *)data = TTYDISC;
1616 return (0);
1617 case TIOCGWINSZ:
1618 bzero(data, sizeof(struct winsize));
1619 return (0);
1620 default:
1621 return (ENOTTY);
1622 }
1623 }
1624 tp = com->tp;
1625 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1626 term = tp->t_termios;
1627 oldcmd = cmd;
1628 error = ttsetcompat(tp, &cmd, data, &term);
1629 if (error != 0)
1630 return (error);
1631 if (cmd != oldcmd)
1632 data = (caddr_t)&term;
1633 #endif
1634 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1635 int cc;
1636 struct termios *dt = (struct termios *)data;
1637 struct termios *lt = mynor & CALLOUT_MASK
1638 ? &com->lt_out : &com->lt_in;
1639
1640 dt->c_iflag = (tp->t_iflag & lt->c_iflag)
1641 | (dt->c_iflag & ~lt->c_iflag);
1642 dt->c_oflag = (tp->t_oflag & lt->c_oflag)
1643 | (dt->c_oflag & ~lt->c_oflag);
1644 dt->c_cflag = (tp->t_cflag & lt->c_cflag)
1645 | (dt->c_cflag & ~lt->c_cflag);
1646 dt->c_lflag = (tp->t_lflag & lt->c_lflag)
1647 | (dt->c_lflag & ~lt->c_lflag);
1648 for (cc = 0; cc < NCCS; ++cc)
1649 if (lt->c_cc[cc] != 0)
1650 dt->c_cc[cc] = tp->t_cc[cc];
1651 if (lt->c_ispeed != 0)
1652 dt->c_ispeed = tp->t_ispeed;
1653 if (lt->c_ospeed != 0)
1654 dt->c_ospeed = tp->t_ospeed;
1655 }
1656 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1657 if (error != ENOIOCTL)
1658 return (error);
1659 s = spltty();
1660 error = ttioctl(tp, cmd, data, flag);
1661 disc_optim(tp, &tp->t_termios, com);
1662 if (error != ENOIOCTL) {
1663 splx(s);
1664 return (error);
1665 }
1666 switch (cmd) {
1667 case TIOCSBRK:
1668 #if 0
1669 outb(iobase + com_cfcr, com->cfcr_image |= CFCR_SBREAK);
1670 #else
1671 cd_etc(com, CD1400_ETC_SENDBREAK);
1672 #endif
1673 break;
1674 case TIOCCBRK:
1675 #if 0
1676 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
1677 #else
1678 cd_etc(com, CD1400_ETC_STOPBREAK);
1679 #endif
1680 break;
1681 case TIOCSDTR:
1682 (void)commctl(com, TIOCM_DTR, DMBIS);
1683 break;
1684 case TIOCCDTR:
1685 (void)commctl(com, TIOCM_DTR, DMBIC);
1686 break;
1687 /*
1688 * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The
1689 * changes get undone on the next call to comparam().
1690 */
1691 case TIOCMSET:
1692 (void)commctl(com, *(int *)data, DMSET);
1693 break;
1694 case TIOCMBIS:
1695 (void)commctl(com, *(int *)data, DMBIS);
1696 break;
1697 case TIOCMBIC:
1698 (void)commctl(com, *(int *)data, DMBIC);
1699 break;
1700 case TIOCMGET:
1701 *(int *)data = commctl(com, 0, DMGET);
1702 break;
1703 case TIOCMSDTRWAIT:
1704 /* must be root since the wait applies to following logins */
1705 error = suser(p);
1706 if (error != 0) {
1707 splx(s);
1708 return (error);
1709 }
1710 com->dtr_wait = *(int *)data * hz / 100;
1711 break;
1712 case TIOCMGDTRWAIT:
1713 *(int *)data = com->dtr_wait * 100 / hz;
1714 break;
1715 case TIOCTIMESTAMP:
1716 com->do_timestamp = TRUE;
1717 *(struct timeval *)data = com->timestamp;
1718 break;
1719 case TIOCDCDTIMESTAMP:
1720 com->do_dcd_timestamp = TRUE;
1721 *(struct timeval *)data = com->dcd_timestamp;
1722 break;
1723 default:
1724 splx(s);
1725 return (ENOTTY);
1726 }
1727 splx(s);
1728 return (0);
1729 }
1730
1731 static void
1732 siopoll()
1733 {
1734 int unit;
1735
1736 #ifdef CyDebug
1737 ++cy_timeouts;
1738 #endif
1739 if (com_events == 0)
1740 return;
1741 repeat:
1742 for (unit = 0; unit < NSIO; ++unit) {
1743 struct com_s *com;
1744 int incc;
1745 struct tty *tp;
1746
1747 com = com_addr(unit);
1748 if (com == NULL)
1749 continue;
1750 tp = com->tp;
1751 if (tp == NULL) {
1752 /*
1753 * XXX forget any events related to closed devices
1754 * (actually never opened devices) so that we don't
1755 * loop.
1756 */
1757 disable_intr();
1758 incc = com->iptr - com->ibuf;
1759 com->iptr = com->ibuf;
1760 if (com->state & CS_CHECKMSR) {
1761 incc += LOTS_OF_EVENTS;
1762 com->state &= ~CS_CHECKMSR;
1763 }
1764 com_events -= incc;
1765 enable_intr();
1766 if (incc != 0)
1767 log(LOG_DEBUG,
1768 "sio%d: %d events for device with no tp\n",
1769 unit, incc);
1770 continue;
1771 }
1772 if (com->iptr != com->ibuf) {
1773 disable_intr();
1774 sioinput(com);
1775 enable_intr();
1776 }
1777 if (com->state & CS_CHECKMSR) {
1778 u_char delta_modem_status;
1779
1780 disable_intr();
1781 delta_modem_status = com->last_modem_status
1782 ^ com->prev_modem_status;
1783 com->prev_modem_status = com->last_modem_status;
1784 com_events -= LOTS_OF_EVENTS;
1785 com->state &= ~CS_CHECKMSR;
1786 enable_intr();
1787 if (delta_modem_status & MSR_DCD)
1788 (*linesw[tp->t_line].l_modem)
1789 (tp, com->prev_modem_status & MSR_DCD);
1790 }
1791 if (com->extra_state & CSE_ODONE) {
1792 disable_intr();
1793 com_events -= LOTS_OF_EVENTS;
1794 com->extra_state &= ~CSE_ODONE;
1795 enable_intr();
1796 if (!(com->state & CS_BUSY)) {
1797 tp->t_state &= ~TS_BUSY;
1798 ttwwakeup(com->tp);
1799 }
1800 if (com->etc != ETC_NONE) {
1801 if (com->etc == ETC_BREAK_ENDED)
1802 com->etc = ETC_NONE;
1803 wakeup(&com->etc);
1804 }
1805 }
1806 if (com->state & CS_ODONE) {
1807 disable_intr();
1808 com_events -= LOTS_OF_EVENTS;
1809 com->state &= ~CS_ODONE;
1810 enable_intr();
1811 (*linesw[tp->t_line].l_start)(tp);
1812 }
1813 if (com_events == 0)
1814 break;
1815 }
1816 if (com_events >= LOTS_OF_EVENTS)
1817 goto repeat;
1818 }
1819
1820 static int
1821 comparam(tp, t)
1822 struct tty *tp;
1823 struct termios *t;
1824 {
1825 int bits;
1826 int cflag;
1827 struct com_s *com;
1828 u_char cor_change;
1829 u_long cy_clock;
1830 int idivisor;
1831 int iflag;
1832 int iprescaler;
1833 int itimeout;
1834 int odivisor;
1835 int oprescaler;
1836 u_char opt;
1837 int s;
1838 int unit;
1839
1840 /* do historical conversions */
1841 if (t->c_ispeed == 0)
1842 t->c_ispeed = t->c_ospeed;
1843
1844 unit = DEV_TO_UNIT(tp->t_dev);
1845 com = com_addr(unit);
1846
1847 /* check requested parameters */
1848 cy_clock = CY_CLOCK(com->gfrcr_image);
1849 idivisor = comspeed(t->c_ispeed, cy_clock, &iprescaler);
1850 if (idivisor < 0)
1851 return (EINVAL);
1852 odivisor = comspeed(t->c_ospeed, cy_clock, &oprescaler);
1853 if (odivisor < 0)
1854 return (EINVAL);
1855
1856 /* parameters are OK, convert them to the com struct and the device */
1857 s = spltty();
1858 if (odivisor == 0)
1859 (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */
1860 else
1861 (void)commctl(com, TIOCM_DTR, DMBIS);
1862
1863 /*
1864 * This returns with interrupts disabled so that we can complete
1865 * the speed change atomically.
1866 */
1867 (void) siosetwater(com, t->c_ispeed);
1868
1869 /* XXX we don't actually change the speed atomically. */
1870 enable_intr();
1871
1872 if (idivisor != 0) {
1873 cd_setreg(com, CD1400_RBPR, idivisor);
1874 cd_setreg(com, CD1400_RCOR, iprescaler);
1875 }
1876 if (odivisor != 0) {
1877 cd_setreg(com, CD1400_TBPR, odivisor);
1878 cd_setreg(com, CD1400_TCOR, oprescaler);
1879 }
1880
1881 /*
1882 * channel control
1883 * receiver enable
1884 * transmitter enable (always set)
1885 */
1886 cflag = t->c_cflag;
1887 opt = CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTEN
1888 | (cflag & CREAD ? CD1400_CCR_RCVEN : CD1400_CCR_RCVDIS);
1889 if (opt != com->channel_control) {
1890 com->channel_control = opt;
1891 cd1400_channel_cmd(com, opt);
1892 }
1893
1894 #ifdef Smarts
1895 /* set special chars */
1896 /* XXX if one is _POSIX_VDISABLE, can't use some others */
1897 if (t->c_cc[VSTOP] != _POSIX_VDISABLE)
1898 cd_setreg(com, CD1400_SCHR1, t->c_cc[VSTOP]);
1899 if (t->c_cc[VSTART] != _POSIX_VDISABLE)
1900 cd_setreg(com, CD1400_SCHR2, t->c_cc[VSTART]);
1901 if (t->c_cc[VINTR] != _POSIX_VDISABLE)
1902 cd_setreg(com, CD1400_SCHR3, t->c_cc[VINTR]);
1903 if (t->c_cc[VSUSP] != _POSIX_VDISABLE)
1904 cd_setreg(com, CD1400_SCHR4, t->c_cc[VSUSP]);
1905 #endif
1906
1907 /*
1908 * set channel option register 1 -
1909 * parity mode
1910 * stop bits
1911 * char length
1912 */
1913 opt = 0;
1914 /* parity */
1915 if (cflag & PARENB) {
1916 if (cflag & PARODD)
1917 opt |= CD1400_COR1_PARODD;
1918 opt |= CD1400_COR1_PARNORMAL;
1919 }
1920 iflag = t->c_iflag;
1921 if (!(iflag & INPCK))
1922 opt |= CD1400_COR1_NOINPCK;
1923 bits = 1 + 1;
1924 /* stop bits */
1925 if (cflag & CSTOPB) {
1926 ++bits;
1927 opt |= CD1400_COR1_STOP2;
1928 }
1929 /* char length */
1930 switch (cflag & CSIZE) {
1931 case CS5:
1932 bits += 5;
1933 opt |= CD1400_COR1_CS5;
1934 break;
1935 case CS6:
1936 bits += 6;
1937 opt |= CD1400_COR1_CS6;
1938 break;
1939 case CS7:
1940 bits += 7;
1941 opt |= CD1400_COR1_CS7;
1942 break;
1943 default:
1944 bits += 8;
1945 opt |= CD1400_COR1_CS8;
1946 break;
1947 }
1948 cor_change = 0;
1949 if (opt != com->cor[0]) {
1950 cor_change |= CD1400_CCR_COR1;
1951 cd_setreg(com, CD1400_COR1, com->cor[0] = opt);
1952 }
1953
1954 /*
1955 * Set receive time-out period, normally to max(one char time, 5 ms).
1956 */
1957 if (t->c_ispeed == 0)
1958 itimeout = cd_getreg(com, CD1400_RTPR);
1959 else {
1960 itimeout = (1000 * bits + t->c_ispeed - 1) / t->c_ispeed;
1961 #ifdef SOFT_HOTCHAR
1962 #define MIN_RTP 1
1963 #else
1964 #define MIN_RTP 5
1965 #endif
1966 if (itimeout < MIN_RTP)
1967 itimeout = MIN_RTP;
1968 }
1969 if (!(t->c_lflag & ICANON) && t->c_cc[VMIN] != 0 && t->c_cc[VTIME] != 0
1970 && t->c_cc[VTIME] * 10 > itimeout)
1971 itimeout = t->c_cc[VTIME] * 10;
1972 if (itimeout > 255)
1973 itimeout = 255;
1974 cd_setreg(com, CD1400_RTPR, itimeout);
1975
1976 /*
1977 * set channel option register 2 -
1978 * flow control
1979 */
1980 opt = 0;
1981 #ifdef Smarts
1982 if (iflag & IXANY)
1983 opt |= CD1400_COR2_IXANY;
1984 if (iflag & IXOFF)
1985 opt |= CD1400_COR2_IXOFF;
1986 #endif
1987 #ifndef SOFT_CTS_OFLOW
1988 if (cflag & CCTS_OFLOW)
1989 opt |= CD1400_COR2_CCTS_OFLOW;
1990 #endif
1991 disable_intr();
1992 if (opt != com->cor[1]) {
1993 cor_change |= CD1400_CCR_COR2;
1994 cd_setreg(com, CD1400_COR2, com->cor[1] = opt);
1995 }
1996 enable_intr();
1997
1998 /*
1999 * set channel option register 3 -
2000 * receiver FIFO interrupt threshold
2001 * flow control
2002 */
2003 opt = RxFifoThreshold;
2004 #ifdef Smarts
2005 if (t->c_lflag & ICANON)
2006 opt |= CD1400_COR3_SCD34; /* detect INTR & SUSP chars */
2007 if (iflag & IXOFF)
2008 /* detect and transparently handle START and STOP chars */
2009 opt |= CD1400_COR3_FCT | CD1400_COR3_SCD12;
2010 #endif
2011 if (opt != com->cor[2]) {
2012 cor_change |= CD1400_CCR_COR3;
2013 cd_setreg(com, CD1400_COR3, com->cor[2] = opt);
2014 }
2015
2016 /* notify the CD1400 if COR1-3 have changed */
2017 if (cor_change)
2018 cd1400_channel_cmd(com, CD1400_CCR_CMDCORCHG | cor_change);
2019
2020 /*
2021 * set channel option register 4 -
2022 * CR/NL processing
2023 * break processing
2024 * received exception processing
2025 */
2026 opt = 0;
2027 if (iflag & IGNCR)
2028 opt |= CD1400_COR4_IGNCR;
2029 #ifdef Smarts
2030 /*
2031 * we need a new ttyinput() for this, as we don't want to
2032 * have ICRNL && INLCR being done in both layers, or to have
2033 * synchronisation problems
2034 */
2035 if (iflag & ICRNL)
2036 opt |= CD1400_COR4_ICRNL;
2037 if (iflag & INLCR)
2038 opt |= CD1400_COR4_INLCR;
2039 #endif
2040 if (iflag & IGNBRK)
2041 opt |= CD1400_COR4_IGNBRK | CD1400_COR4_NOBRKINT;
2042 /*
2043 * The `-ignbrk -brkint parmrk' case is not handled by the hardware,
2044 * so only tell the hardware about -brkint if -parmrk.
2045 */
2046 if (!(iflag & (BRKINT | PARMRK)))
2047 opt |= CD1400_COR4_NOBRKINT;
2048 #if 0
2049 /* XXX using this "intelligence" breaks reporting of overruns. */
2050 if (iflag & IGNPAR)
2051 opt |= CD1400_COR4_PFO_DISCARD;
2052 else {
2053 if (iflag & PARMRK)
2054 opt |= CD1400_COR4_PFO_ESC;
2055 else
2056 opt |= CD1400_COR4_PFO_NUL;
2057 }
2058 #else
2059 opt |= CD1400_COR4_PFO_EXCEPTION;
2060 #endif
2061 cd_setreg(com, CD1400_COR4, opt);
2062
2063 /*
2064 * set channel option register 5 -
2065 */
2066 opt = 0;
2067 if (iflag & ISTRIP)
2068 opt |= CD1400_COR5_ISTRIP;
2069 if (t->c_iflag & IEXTEN)
2070 /* enable LNEXT (e.g. ctrl-v quoting) handling */
2071 opt |= CD1400_COR5_LNEXT;
2072 #ifdef Smarts
2073 if (t->c_oflag & ONLCR)
2074 opt |= CD1400_COR5_ONLCR;
2075 if (t->c_oflag & OCRNL)
2076 opt |= CD1400_COR5_OCRNL;
2077 #endif
2078 cd_setreg(com, CD1400_COR5, opt);
2079
2080 /*
2081 * We always generate modem status change interrupts for CD changes.
2082 * Among other things, this is necessary to track TS_CARR_ON for
2083 * pstat to print even when the driver doesn't care. CD changes
2084 * should be rare so interrupts for them are not worth extra code to
2085 * avoid. We avoid interrupts for other modem status changes (except
2086 * for CTS changes when SOFT_CTS_OFLOW is configured) since this is
2087 * simplest and best.
2088 */
2089
2090 /*
2091 * set modem change option register 1
2092 * generate modem interrupts on which 1 -> 0 input transitions
2093 * also controls auto-DTR output flow-control, which we don't use
2094 */
2095 opt = CD1400_MCOR1_CDzd;
2096 #ifdef SOFT_CTS_OFLOW
2097 if (cflag & CCTS_OFLOW)
2098 opt |= CD1400_MCOR1_CTSzd;
2099 #endif
2100 cd_setreg(com, CD1400_MCOR1, opt);
2101
2102 /*
2103 * set modem change option register 2
2104 * generate modem interrupts on specific 0 -> 1 input transitions
2105 */
2106 opt = CD1400_MCOR2_CDod;
2107 #ifdef SOFT_CTS_OFLOW
2108 if (cflag & CCTS_OFLOW)
2109 opt |= CD1400_MCOR2_CTSod;
2110 #endif
2111 cd_setreg(com, CD1400_MCOR2, opt);
2112
2113 /*
2114 * XXX should have done this long ago, but there is too much state
2115 * to change all atomically.
2116 */
2117 disable_intr();
2118
2119 com->state &= ~CS_TTGO;
2120 if (!(tp->t_state & TS_TTSTOP))
2121 com->state |= CS_TTGO;
2122 if (cflag & CRTS_IFLOW) {
2123 com->state |= CS_RTS_IFLOW;
2124 /*
2125 * If CS_RTS_IFLOW just changed from off to on, the change
2126 * needs to be propagated to MCR_RTS. This isn't urgent,
2127 * so do it later by calling comstart() instead of repeating
2128 * a lot of code from comstart() here.
2129 */
2130 } else if (com->state & CS_RTS_IFLOW) {
2131 com->state &= ~CS_RTS_IFLOW;
2132 /*
2133 * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS
2134 * on here, since comstart() won't do it later.
2135 */
2136 #if 0
2137 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2138 #else
2139 cd_setreg(com, com->mcr_rts_reg,
2140 com->mcr_image |= com->mcr_rts);
2141 #endif
2142 }
2143
2144 /*
2145 * Set up state to handle output flow control.
2146 * XXX - worth handling MDMBUF (DCD) flow control at the lowest level?
2147 * Now has 10+ msec latency, while CTS flow has 50- usec latency.
2148 */
2149 com->state |= CS_ODEVREADY;
2150 #ifdef SOFT_CTS_OFLOW
2151 com->state &= ~CS_CTS_OFLOW;
2152 if (cflag & CCTS_OFLOW) {
2153 com->state |= CS_CTS_OFLOW;
2154 if (!(com->last_modem_status & MSR_CTS))
2155 com->state &= ~CS_ODEVREADY;
2156 }
2157 #endif
2158 /* XXX shouldn't call functions while intrs are disabled. */
2159 disc_optim(tp, t, com);
2160 #if 0
2161 /*
2162 * Recover from fiddling with CS_TTGO. We used to call siointr1()
2163 * unconditionally, but that defeated the careful discarding of
2164 * stale input in sioopen().
2165 */
2166 if (com->state >= (CS_BUSY | CS_TTGO))
2167 siointr1(com);
2168 #endif
2169 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
2170 if (!(com->intr_enable & CD1400_SRER_TXRDY))
2171 cd_setreg(com, CD1400_SRER,
2172 com->intr_enable
2173 = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2174 | CD1400_SRER_TXRDY);
2175 } else {
2176 if (com->intr_enable & CD1400_SRER_TXRDY)
2177 cd_setreg(com, CD1400_SRER,
2178 com->intr_enable
2179 = (com->intr_enable & ~CD1400_SRER_TXRDY)
2180 | CD1400_SRER_TXMPTY);
2181 }
2182
2183 enable_intr();
2184 splx(s);
2185 comstart(tp);
2186 if (com->ibufold != NULL) {
2187 free(com->ibufold, M_DEVBUF);
2188 com->ibufold = NULL;
2189 }
2190 return (0);
2191 }
2192
2193 static int
2194 siosetwater(com, speed)
2195 struct com_s *com;
2196 speed_t speed;
2197 {
2198 int cp4ticks;
2199 u_char *ibuf;
2200 int ibufsize;
2201 struct tty *tp;
2202
2203 /*
2204 * Make the buffer size large enough to handle a softtty interrupt
2205 * latency of about 2 ticks without loss of throughput or data
2206 * (about 3 ticks if input flow control is not used or not honoured,
2207 * but a bit less for CS5-CS7 modes).
2208 */
2209 cp4ticks = speed / 10 / hz * 4;
2210 for (ibufsize = 128; ibufsize < cp4ticks;)
2211 ibufsize <<= 1;
2212 if (ibufsize == com->ibufsize) {
2213 disable_intr();
2214 return (0);
2215 }
2216
2217 /*
2218 * Allocate input buffer. The extra factor of 2 in the size is
2219 * to allow for an error byte for each input byte.
2220 */
2221 ibuf = malloc(2 * ibufsize, M_DEVBUF, M_NOWAIT);
2222 if (ibuf == NULL) {
2223 disable_intr();
2224 return (ENOMEM);
2225 }
2226
2227 /* Initialize non-critical variables. */
2228 com->ibufold = com->ibuf;
2229 com->ibufsize = ibufsize;
2230 tp = com->tp;
2231 if (tp != NULL) {
2232 tp->t_ififosize = 2 * ibufsize;
2233 tp->t_ispeedwat = (speed_t)-1;
2234 tp->t_ospeedwat = (speed_t)-1;
2235 }
2236
2237 /*
2238 * Read current input buffer, if any. Continue with interrupts
2239 * disabled.
2240 */
2241 disable_intr();
2242 if (com->iptr != com->ibuf)
2243 sioinput(com);
2244
2245 /*-
2246 * Initialize critical variables, including input buffer watermarks.
2247 * The external device is asked to stop sending when the buffer
2248 * exactly reaches high water, or when the high level requests it.
2249 * The high level is notified immediately (rather than at a later
2250 * clock tick) when this watermark is reached.
2251 * The buffer size is chosen so the watermark should almost never
2252 * be reached.
2253 * The low watermark is invisibly 0 since the buffer is always
2254 * emptied all at once.
2255 */
2256 com->iptr = com->ibuf = ibuf;
2257 com->ibufend = ibuf + ibufsize;
2258 com->ierroff = ibufsize;
2259 com->ihighwater = ibuf + 3 * ibufsize / 4;
2260 return (0);
2261 }
2262
2263 static void
2264 comstart(tp)
2265 struct tty *tp;
2266 {
2267 struct com_s *com;
2268 int s;
2269 #ifdef CyDebug
2270 bool_t started;
2271 #endif
2272 int unit;
2273
2274 unit = DEV_TO_UNIT(tp->t_dev);
2275 com = com_addr(unit);
2276 s = spltty();
2277
2278 #ifdef CyDebug
2279 ++com->start_count;
2280 started = FALSE;
2281 #endif
2282
2283 disable_intr();
2284 if (tp->t_state & TS_TTSTOP) {
2285 com->state &= ~CS_TTGO;
2286 if (com->intr_enable & CD1400_SRER_TXRDY)
2287 cd_setreg(com, CD1400_SRER,
2288 com->intr_enable
2289 = (com->intr_enable & ~CD1400_SRER_TXRDY)
2290 | CD1400_SRER_TXMPTY);
2291 } else {
2292 com->state |= CS_TTGO;
2293 if (com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)
2294 && !(com->intr_enable & CD1400_SRER_TXRDY))
2295 cd_setreg(com, CD1400_SRER,
2296 com->intr_enable
2297 = (com->intr_enable & ~CD1400_SRER_TXMPTY)
2298 | CD1400_SRER_TXRDY);
2299 }
2300 if (tp->t_state & TS_TBLOCK) {
2301 if (com->mcr_image & com->mcr_rts && com->state & CS_RTS_IFLOW)
2302 #if 0
2303 outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS);
2304 #else
2305 cd_setreg(com, com->mcr_rts_reg,
2306 com->mcr_image &= ~com->mcr_rts);
2307 #endif
2308 } else {
2309 if (!(com->mcr_image & com->mcr_rts)
2310 && com->iptr < com->ihighwater
2311 && com->state & CS_RTS_IFLOW)
2312 #if 0
2313 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2314 #else
2315 cd_setreg(com, com->mcr_rts_reg,
2316 com->mcr_image |= com->mcr_rts);
2317 #endif
2318 }
2319 enable_intr();
2320 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
2321 ttwwakeup(tp);
2322 splx(s);
2323 return;
2324 }
2325 if (tp->t_outq.c_cc != 0) {
2326 struct lbq *qp;
2327 struct lbq *next;
2328
2329 if (!com->obufs[0].l_queued) {
2330 #ifdef CyDebug
2331 started = TRUE;
2332 #endif
2333 com->obufs[0].l_tail
2334 = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1,
2335 sizeof com->obuf1);
2336 com->obufs[0].l_next = NULL;
2337 com->obufs[0].l_queued = TRUE;
2338 disable_intr();
2339 if (com->state & CS_BUSY) {
2340 qp = com->obufq.l_next;
2341 while ((next = qp->l_next) != NULL)
2342 qp = next;
2343 qp->l_next = &com->obufs[0];
2344 } else {
2345 com->obufq.l_head = com->obufs[0].l_head;
2346 com->obufq.l_tail = com->obufs[0].l_tail;
2347 com->obufq.l_next = &com->obufs[0];
2348 com->state |= CS_BUSY;
2349 if (com->state >= (CS_BUSY | CS_TTGO
2350 | CS_ODEVREADY))
2351 cd_setreg(com, CD1400_SRER,
2352 com->intr_enable
2353 = (com->intr_enable
2354 & ~CD1400_SRER_TXMPTY)
2355 | CD1400_SRER_TXRDY);
2356 }
2357 enable_intr();
2358 }
2359 if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) {
2360 #ifdef CyDebug
2361 started = TRUE;
2362 #endif
2363 com->obufs[1].l_tail
2364 = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2,
2365 sizeof com->obuf2);
2366 com->obufs[1].l_next = NULL;
2367 com->obufs[1].l_queued = TRUE;
2368 disable_intr();
2369 if (com->state & CS_BUSY) {
2370 qp = com->obufq.l_next;
2371 while ((next = qp->l_next) != NULL)
2372 qp = next;
2373 qp->l_next = &com->obufs[1];
2374 } else {
2375 com->obufq.l_head = com->obufs[1].l_head;
2376 com->obufq.l_tail = com->obufs[1].l_tail;
2377 com->obufq.l_next = &com->obufs[1];
2378 com->state |= CS_BUSY;
2379 if (com->state >= (CS_BUSY | CS_TTGO
2380 | CS_ODEVREADY))
2381 cd_setreg(com, CD1400_SRER,
2382 com->intr_enable
2383 = (com->intr_enable
2384 & ~CD1400_SRER_TXMPTY)
2385 | CD1400_SRER_TXRDY);
2386 }
2387 enable_intr();
2388 }
2389 tp->t_state |= TS_BUSY;
2390 }
2391 #ifdef CyDebug
2392 if (started)
2393 ++com->start_real;
2394 #endif
2395 #if 0
2396 disable_intr();
2397 if (com->state >= (CS_BUSY | CS_TTGO))
2398 siointr1(com); /* fake interrupt to start output */
2399 enable_intr();
2400 #endif
2401 ttwwakeup(tp);
2402 splx(s);
2403 }
2404
2405 static void
2406 comstop(tp, rw)
2407 struct tty *tp;
2408 int rw;
2409 {
2410 struct com_s *com;
2411 bool_t wakeup_etc;
2412
2413 com = com_addr(DEV_TO_UNIT(tp->t_dev));
2414 wakeup_etc = FALSE;
2415 disable_intr();
2416 if (rw & FWRITE) {
2417 com->obufs[0].l_queued = FALSE;
2418 com->obufs[1].l_queued = FALSE;
2419 if (com->extra_state & CSE_ODONE) {
2420 com_events -= LOTS_OF_EVENTS;
2421 com->extra_state &= ~CSE_ODONE;
2422 if (com->etc != ETC_NONE) {
2423 if (com->etc == ETC_BREAK_ENDED)
2424 com->etc = ETC_NONE;
2425 wakeup_etc = TRUE;
2426 }
2427 }
2428 com->tp->t_state &= ~TS_BUSY;
2429 if (com->state & CS_ODONE)
2430 com_events -= LOTS_OF_EVENTS;
2431 com->state &= ~(CS_ODONE | CS_BUSY);
2432 }
2433 if (rw & FREAD) {
2434 /* XXX no way to reset only input fifo. */
2435 com_events -= (com->iptr - com->ibuf);
2436 com->iptr = com->ibuf;
2437 }
2438 enable_intr();
2439 if (wakeup_etc)
2440 wakeup(&com->etc);
2441 if (rw & FWRITE && com->etc == ETC_NONE)
2442 cd1400_channel_cmd(com, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
2443 comstart(tp);
2444 }
2445
2446 static int
2447 commctl(com, bits, how)
2448 struct com_s *com;
2449 int bits;
2450 int how;
2451 {
2452 int mcr;
2453 int msr;
2454
2455 if (how == DMGET) {
2456 if (com->channel_control & CD1400_CCR_RCVEN)
2457 bits |= TIOCM_LE;
2458 mcr = com->mcr_image;
2459 if (mcr & com->mcr_dtr)
2460 bits |= TIOCM_DTR;
2461 if (mcr & com->mcr_rts)
2462 /* XXX wired on for Cyclom-8Ys */
2463 bits |= TIOCM_RTS;
2464
2465 /*
2466 * We must read the modem status from the hardware because
2467 * we don't generate modem status change interrupts for all
2468 * changes, so com->prev_modem_status is not guaranteed to
2469 * be up to date. This is safe, unlike for sio, because
2470 * reading the status register doesn't clear pending modem
2471 * status change interrupts.
2472 */
2473 msr = cd_getreg(com, CD1400_MSVR2);
2474
2475 if (msr & MSR_CTS)
2476 bits |= TIOCM_CTS;
2477 if (msr & MSR_DCD)
2478 bits |= TIOCM_CD;
2479 if (msr & MSR_DSR)
2480 bits |= TIOCM_DSR;
2481 if (msr & MSR_RI)
2482 /* XXX not connected except for Cyclom-16Y? */
2483 bits |= TIOCM_RI;
2484 return (bits);
2485 }
2486 mcr = 0;
2487 if (bits & TIOCM_DTR)
2488 mcr |= com->mcr_dtr;
2489 if (bits & TIOCM_RTS)
2490 mcr |= com->mcr_rts;
2491 disable_intr();
2492 switch (how) {
2493 case DMSET:
2494 com->mcr_image = mcr;
2495 cd_setreg(com, CD1400_MSVR1, mcr);
2496 cd_setreg(com, CD1400_MSVR2, mcr);
2497 break;
2498 case DMBIS:
2499 com->mcr_image = mcr = com->mcr_image | mcr;
2500 cd_setreg(com, CD1400_MSVR1, mcr);
2501 cd_setreg(com, CD1400_MSVR2, mcr);
2502 break;
2503 case DMBIC:
2504 com->mcr_image = mcr = com->mcr_image & ~mcr;
2505 cd_setreg(com, CD1400_MSVR1, mcr);
2506 cd_setreg(com, CD1400_MSVR2, mcr);
2507 break;
2508 }
2509 enable_intr();
2510 return (0);
2511 }
2512
2513 static void
2514 siosettimeout()
2515 {
2516 struct com_s *com;
2517 bool_t someopen;
2518 int unit;
2519
2520 /*
2521 * Set our timeout period to 1 second if no polled devices are open.
2522 * Otherwise set it to max(1/200, 1/hz).
2523 * Enable timeouts iff some device is open.
2524 */
2525 untimeout(comwakeup, (void *)NULL, sio_timeout_handle);
2526 sio_timeout = hz;
2527 someopen = FALSE;
2528 for (unit = 0; unit < NSIO; ++unit) {
2529 com = com_addr(unit);
2530 if (com != NULL && com->tp != NULL
2531 && com->tp->t_state & TS_ISOPEN) {
2532 someopen = TRUE;
2533 #if 0
2534 if (com->poll || com->poll_output) {
2535 sio_timeout = hz > 200 ? hz / 200 : 1;
2536 break;
2537 }
2538 #endif
2539 }
2540 }
2541 if (someopen) {
2542 sio_timeouts_until_log = hz / sio_timeout;
2543 sio_timeout_handle = timeout(comwakeup, (void *)NULL,
2544 sio_timeout);
2545 } else {
2546 /* Flush error messages, if any. */
2547 sio_timeouts_until_log = 1;
2548 comwakeup((void *)NULL);
2549 untimeout(comwakeup, (void *)NULL, sio_timeout_handle);
2550 }
2551 }
2552
2553 static void
2554 comwakeup(chan)
2555 void *chan;
2556 {
2557 struct com_s *com;
2558 int unit;
2559
2560 sio_timeout_handle = timeout(comwakeup, (void *)NULL, sio_timeout);
2561
2562 #if 0
2563 /*
2564 * Recover from lost output interrupts.
2565 * Poll any lines that don't use interrupts.
2566 */
2567 for (unit = 0; unit < NSIO; ++unit) {
2568 com = com_addr(unit);
2569 if (com != NULL
2570 && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) {
2571 disable_intr();
2572 siointr1(com);
2573 enable_intr();
2574 }
2575 }
2576 #endif
2577
2578 /*
2579 * Check for and log errors, but not too often.
2580 */
2581 if (--sio_timeouts_until_log > 0)
2582 return;
2583 sio_timeouts_until_log = hz / sio_timeout;
2584 for (unit = 0; unit < NSIO; ++unit) {
2585 int errnum;
2586
2587 com = com_addr(unit);
2588 if (com == NULL)
2589 continue;
2590 for (errnum = 0; errnum < CE_NTYPES; ++errnum) {
2591 u_int delta;
2592 u_long total;
2593
2594 disable_intr();
2595 delta = com->delta_error_counts[errnum];
2596 com->delta_error_counts[errnum] = 0;
2597 enable_intr();
2598 if (delta == 0)
2599 continue;
2600 total = com->error_counts[errnum] += delta;
2601 log(LOG_ERR, "cy%d: %u more %s%s (total %lu)\n",
2602 unit, delta, error_desc[errnum],
2603 delta == 1 ? "" : "s", total);
2604 }
2605 }
2606 }
2607
2608 static void
2609 disc_optim(tp, t, com)
2610 struct tty *tp;
2611 struct termios *t;
2612 struct com_s *com;
2613 {
2614 #ifndef SOFT_HOTCHAR
2615 u_char opt;
2616 #endif
2617
2618 /*
2619 * XXX can skip a lot more cases if Smarts. Maybe
2620 * (IGNCR | ISTRIP | IXON) in c_iflag. But perhaps we
2621 * shouldn't skip if (TS_CNTTB | TS_LNCH) is set in t_state.
2622 */
2623 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
2624 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
2625 && (!(t->c_iflag & PARMRK)
2626 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
2627 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
2628 && linesw[tp->t_line].l_rint == ttyinput)
2629 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2630 else
2631 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2632 com->hotchar = linesw[tp->t_line].l_hotchar;
2633 #ifndef SOFT_HOTCHAR
2634 opt = com->cor[2] & ~CD1400_COR3_SCD34;
2635 if (com->hotchar != 0) {
2636 cd_setreg(com, CD1400_SCHR3, com->hotchar);
2637 cd_setreg(com, CD1400_SCHR4, com->hotchar);
2638 opt |= CD1400_COR3_SCD34;
2639 }
2640 if (opt != com->cor[2]) {
2641 cd_setreg(com, CD1400_COR3, com->cor[2] = opt);
2642 cd1400_channel_cmd(com, CD1400_CCR_CMDCORCHG | CD1400_CCR_COR3);
2643 }
2644 #endif
2645 }
2646
2647 #ifdef Smarts
2648 /* standard line discipline input routine */
2649 int
2650 cyinput(c, tp)
2651 int c;
2652 struct tty *tp;
2653 {
2654 /* XXX duplicate ttyinput(), but without the IXOFF/IXON/ISTRIP/IPARMRK
2655 * bits, as they are done by the CD1400. Hardly worth the effort,
2656 * given that high-throughput sessions are raw anyhow.
2657 */
2658 }
2659 #endif /* Smarts */
2660
2661 static int
2662 comspeed(speed, cy_clock, prescaler_io)
2663 speed_t speed;
2664 u_long cy_clock;
2665 int *prescaler_io;
2666 {
2667 int actual;
2668 int error;
2669 int divider;
2670 int prescaler;
2671 int prescaler_unit;
2672
2673 if (speed == 0)
2674 return (0);
2675 if (speed < 0 || speed > 150000)
2676 return (-1);
2677
2678 /* determine which prescaler to use */
2679 for (prescaler_unit = 4, prescaler = 2048; prescaler_unit;
2680 prescaler_unit--, prescaler >>= 2) {
2681 if (cy_clock / prescaler / speed > 63)
2682 break;
2683 }
2684
2685 divider = (cy_clock / prescaler * 2 / speed + 1) / 2; /* round off */
2686 if (divider > 255)
2687 divider = 255;
2688 actual = cy_clock/prescaler/divider;
2689
2690 /* 10 times error in percent: */
2691 error = ((actual - (long)speed) * 2000 / (long)speed + 1) / 2;
2692
2693 /* 3.0% max error tolerance */
2694 if (error < -30 || error > 30)
2695 return (-1);
2696
2697 #if 0
2698 printf("prescaler = %d (%d)\n", prescaler, prescaler_unit);
2699 printf("divider = %d (%x)\n", divider, divider);
2700 printf("actual = %d\n", actual);
2701 printf("error = %d\n", error);
2702 #endif
2703
2704 *prescaler_io = prescaler_unit;
2705 return (divider);
2706 }
2707
2708 static void
2709 cd1400_channel_cmd(com, cmd)
2710 struct com_s *com;
2711 int cmd;
2712 {
2713 cd1400_channel_cmd_wait(com);
2714 cd_setreg(com, CD1400_CCR, cmd);
2715 cd1400_channel_cmd_wait(com);
2716 }
2717
2718 static void
2719 cd1400_channel_cmd_wait(com)
2720 struct com_s *com;
2721 {
2722 struct timeval start;
2723 struct timeval tv;
2724 long usec;
2725
2726 if (cd_getreg(com, CD1400_CCR) == 0)
2727 return;
2728 microtime(&start);
2729 for (;;) {
2730 if (cd_getreg(com, CD1400_CCR) == 0)
2731 return;
2732 microtime(&tv);
2733 usec = 1000000 * (tv.tv_sec - start.tv_sec) +
2734 tv.tv_usec - start.tv_usec;
2735 if (usec >= 5000) {
2736 log(LOG_ERR,
2737 "cy%d: channel command timeout (%ld usec)\n",
2738 com->unit, usec);
2739 return;
2740 }
2741 }
2742 }
2743
2744 static void
2745 cd_etc(com, etc)
2746 struct com_s *com;
2747 int etc;
2748 {
2749 /*
2750 * We can't change the hardware's ETC state while there are any
2751 * characters in the tx fifo, since those characters would be
2752 * interpreted as commands! Unputting characters from the fifo
2753 * is difficult, so we wait up to 12 character times for the fifo
2754 * to drain. The command will be delayed for up to 2 character
2755 * times for the tx to become empty. Unputting characters from
2756 * the tx holding and shift registers is impossible, so we wait
2757 * for the tx to become empty so that the command is sure to be
2758 * executed soon after we issue it.
2759 */
2760 disable_intr();
2761 if (com->etc == etc) {
2762 enable_intr();
2763 goto wait;
2764 }
2765 if ((etc == CD1400_ETC_SENDBREAK
2766 && (com->etc == ETC_BREAK_STARTING
2767 || com->etc == ETC_BREAK_STARTED))
2768 || (etc == CD1400_ETC_STOPBREAK
2769 && (com->etc == ETC_BREAK_ENDING || com->etc == ETC_BREAK_ENDED
2770 || com->etc == ETC_NONE))) {
2771 enable_intr();
2772 return;
2773 }
2774 com->etc = etc;
2775 cd_setreg(com, CD1400_SRER,
2776 com->intr_enable
2777 = (com->intr_enable & ~CD1400_SRER_TXRDY) | CD1400_SRER_TXMPTY);
2778 enable_intr();
2779 wait:
2780 while (com->etc == etc
2781 && tsleep(&com->etc, TTIPRI | PCATCH, "cyetc", 0) == 0)
2782 continue;
2783 }
2784
2785 static int
2786 cd_getreg(com, reg)
2787 struct com_s *com;
2788 int reg;
2789 {
2790 struct com_s *basecom;
2791 u_char car;
2792 int cy_align;
2793 u_long ef;
2794 cy_addr iobase;
2795 int val;
2796
2797 basecom = com_addr(com->unit & ~(CD1400_NO_OF_CHANNELS - 1));
2798 car = com->unit & CD1400_CAR_CHAN;
2799 cy_align = com->cy_align;
2800 iobase = com->iobase;
2801 ef = read_eflags();
2802 if (ef & PSL_I)
2803 disable_intr();
2804 if (basecom->car != car)
2805 cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2806 val = cd_inb(iobase, reg, cy_align);
2807 if (ef & PSL_I)
2808 enable_intr();
2809 return (val);
2810 }
2811
2812 static void
2813 cd_setreg(com, reg, val)
2814 struct com_s *com;
2815 int reg;
2816 int val;
2817 {
2818 struct com_s *basecom;
2819 u_char car;
2820 int cy_align;
2821 u_long ef;
2822 cy_addr iobase;
2823
2824 basecom = com_addr(com->unit & ~(CD1400_NO_OF_CHANNELS - 1));
2825 car = com->unit & CD1400_CAR_CHAN;
2826 cy_align = com->cy_align;
2827 iobase = com->iobase;
2828 ef = read_eflags();
2829 if (ef & PSL_I)
2830 disable_intr();
2831 if (basecom->car != car)
2832 cd_outb(iobase, CD1400_CAR, cy_align, basecom->car = car);
2833 cd_outb(iobase, reg, cy_align, val);
2834 if (ef & PSL_I)
2835 enable_intr();
2836 }
2837
2838 #ifdef CyDebug
2839 /* useful in ddb */
2840 void
2841 cystatus(unit)
2842 int unit;
2843 {
2844 struct com_s *com;
2845 cy_addr iobase;
2846 u_int ocount;
2847 struct tty *tp;
2848
2849 com = com_addr(unit);
2850 printf("info for channel %d\n", unit);
2851 printf("------------------\n");
2852 printf("total cyclom service probes:\t%d\n", cy_svrr_probes);
2853 printf("calls to upper layer:\t\t%d\n", cy_timeouts);
2854 if (com == NULL)
2855 return;
2856 iobase = com->iobase;
2857 printf("\n");
2858 printf("cd1400 base address:\\tt%p\n", iobase);
2859 printf("saved channel_control:\t\t0x%02x\n", com->channel_control);
2860 printf("saved cor1-3:\t\t\t0x%02x 0x%02x 0x%02x\n",
2861 com->cor[0], com->cor[1], com->cor[2]);
2862 printf("service request enable reg:\t0x%02x (0x%02x cached)\n",
2863 cd_getreg(com, CD1400_SRER), com->intr_enable);
2864 printf("service request register:\t0x%02x\n",
2865 cd_inb(iobase, CD1400_SVRR, com->cy_align));
2866 printf("modem status:\t\t\t0x%02x (0x%02x cached)\n",
2867 cd_getreg(com, CD1400_MSVR2), com->prev_modem_status);
2868 printf("rx/tx/mdm interrupt registers:\t0x%02x 0x%02x 0x%02x\n",
2869 cd_inb(iobase, CD1400_RIR, com->cy_align),
2870 cd_inb(iobase, CD1400_TIR, com->cy_align),
2871 cd_inb(iobase, CD1400_MIR, com->cy_align));
2872 printf("\n");
2873 printf("com state:\t\t\t0x%02x\n", com->state);
2874 printf("calls to comstart():\t\t%d (%d useful)\n",
2875 com->start_count, com->start_real);
2876 printf("rx buffer chars free:\t\t%d\n", com->iptr - com->ibuf);
2877 ocount = 0;
2878 if (com->obufs[0].l_queued)
2879 ocount += com->obufs[0].l_tail - com->obufs[0].l_head;
2880 if (com->obufs[1].l_queued)
2881 ocount += com->obufs[1].l_tail - com->obufs[1].l_head;
2882 printf("tx buffer chars:\t\t%u\n", ocount);
2883 printf("received chars:\t\t\t%d\n", com->bytes_in);
2884 printf("received exceptions:\t\t%d\n", com->recv_exception);
2885 printf("modem signal deltas:\t\t%d\n", com->mdm);
2886 printf("transmitted chars:\t\t%d\n", com->bytes_out);
2887 printf("\n");
2888 tp = com->tp;
2889 if (tp != NULL) {
2890 printf("tty state:\t\t\t0x%08x\n", tp->t_state);
2891 printf(
2892 "upper layer queue lengths:\t%d raw, %d canon, %d output\n",
2893 tp->t_rawq.c_cc, tp->t_canq.c_cc, tp->t_outq.c_cc);
2894 } else
2895 printf("tty state:\t\t\tclosed\n");
2896 }
2897 #endif /* CyDebug */
Cache object: 5bb01982db55376d9d5eda19585d2af0
|