4 * dgb.c $FreeBSD: src/sys/gnu/i386/isa/dgb.c,v 1.56.2.1 2001/02/26 04:23:09 jlemon Exp $
5 * dgb.c $DragonFly: src/sys/platform/pc32/gnu/isa/dgb.c,v 1.20 2008/08/02 01:14:43 dillon Exp $
9 * Stage 1. "Better than nothing".
10 * Stage 2. "Gee, it works!".
12 * Based on sio driver by Bruce Evans and on Linux driver by Troy
13 * De Jongh <troyd@digibd.com> or <troyd@skypoint.com>
14 * which is under GNU General Public License version 2 so this driver
15 * is forced to be under GPL 2 too.
17 * Written by Serge Babkin,
18 * Joint Stock Commercial Bank "Chelindbank"
19 * (Chelyabinsk, Russia)
20 * babkin@hq.icb.chel.su
22 * Assorted hacks to make it more functional and working under 3.0-current.
23 * Fixed broken routines to prevent processes hanging on closed (thanks
24 * to Bruce for his patience and assistance). Thanks also to Maxim Bolotin
25 * <max@run.net> for his patches which did most of the work to get this
26 * running under 2.2/3.0-current.
27 * Implemented ioctls: TIOCMSDTRWAIT, TIOCMGDTRWAIT, TIOCTIMESTAMP &
29 * Sysctl debug flag is now a bitflag, to filter noise during debugging.
30 * David L. Nugent <davidn@blaze.net.au>
33 #include "opt_compat.h"
35 #include "opt_deprecated.h"
36 #ifndef I_WANT_DEPRECATED_STUFF
37 #error "Add options I_WANT_DEPRECATED_STUFF to your kernel config and send a mail to kernel@"
42 /* Helg: i.e.25 times per sec board will be polled */
43 #define POLLSPERSEC 25
44 /* How many charactes can we write to input tty rawq */
45 #define DGB_IBUFSIZE (TTYHOG-100)
47 /* the overall number of ports controlled by this driver */
50 # define NDGBPORTS (NDGB*16)
53 #include <sys/param.h>
54 #include <sys/systm.h>
59 #include <sys/dkstat.h>
60 #include <sys/fcntl.h>
61 #include <sys/kernel.h>
62 #include <sys/sysctl.h>
63 #include <sys/thread2.h>
65 #include <machine/clock.h>
70 #include <bus/isa/isa_device.h>
75 #define DGB_DEBUG /* Enable debugging info via sysctl */
78 #define CALLOUT_MASK 0x80
79 #define CONTROL_MASK 0x60
80 #define CONTROL_INIT_STATE 0x20
81 #define CONTROL_LOCK_STATE 0x40
82 #define UNIT_MASK 0x30000
83 #define PORT_MASK 0x1F
84 #define DEV_TO_UNIT(dev) (MINOR_TO_UNIT(minor(dev)))
85 #define MINOR_MAGIC_MASK (CALLOUT_MASK | CONTROL_MASK)
86 #define MINOR_TO_UNIT(mynor) (((mynor) & UNIT_MASK)>>16)
87 #define MINOR_TO_PORT(mynor) ((mynor) & PORT_MASK)
89 /* types. XXX - should be elsewhere */
90 typedef u_char bool_t; /* boolean */
92 /* digiboard port structure */
96 u_char unit; /* board unit number */
97 u_char pnum; /* port number */
98 u_char omodem; /* FEP output modem status */
99 u_char imodem; /* FEP input modem status */
100 u_char modemfake; /* Modem values to be forced */
101 u_char modem; /* Force values */
128 volatile u_char *txptr;
129 volatile u_char *rxptr;
130 volatile struct board_chan *brdchan;
133 bool_t active_out; /* nonzero if the callout device is open */
134 u_int wopeners; /* # processes waiting for DCD in open() */
137 struct termios it_in; /* should be in struct tty */
138 struct termios it_out;
141 struct termios lt_in; /* should be in struct tty */
142 struct termios lt_out;
145 bool_t do_dcd_timestamp;
146 struct timeval timestamp;
147 struct timeval dcd_timestamp;
149 /* flags of state, are used in sleep() too */
150 u_char closing; /* port is being closed now */
151 u_char draining; /* port is being drained now */
152 u_char used; /* port is being used now */
153 u_char mustdrain; /* data must be waited to drain in dgbparam() */
156 /* Digiboard per-board structure */
158 /* struct board_info */
159 u_char status; /* status: DISABLED/ENABLED */
160 u_char unit; /* unit number */
161 u_char type; /* type of card: PCXE, PCXI, PCXEVE */
162 u_char altpin; /* do we need alternate pin setting ? */
163 int numports; /* number of ports on card */
164 int port; /* I/O port */
165 u_char *vmem; /* virtual memory address */
166 long pmem; /* physical memory address */
167 int mem_seg; /* internal memory segment */
168 struct dgb_p *ports; /* pointer to array of port descriptors */
169 struct tty *ttys; /* pointer to array of TTY structures */
170 volatile struct global_data *mailbox;
171 struct callout dgb_pause;
172 struct callout dgbpoll;
173 struct callout wakeflush;
177 static struct dgb_softc dgb_softc[NDGB];
178 static struct dgb_p dgb_ports[NDGBPORTS];
179 static struct tty dgb_tty[NDGBPORTS];
182 * The public functions in the com module ought to be declared in a com-driver
186 /* Interrupt handling entry points. */
187 static void dgbpoll (void *unit_c);
189 /* Device switch entry points. */
190 #define dgbreset noreset
191 #define dgbmmap nommap
192 #define dgbstrategy nostrategy
194 static int dgbattach (struct isa_device *dev);
195 static int dgbprobe (struct isa_device *dev);
197 static void fepcmd(struct dgb_p *port, unsigned cmd, unsigned op1, unsigned op2,
198 unsigned ncmds, unsigned bytecmd);
200 static void dgbstart (struct tty *tp);
201 static void dgbstop (struct tty *tp, int rw);
202 static int dgbparam (struct tty *tp, struct termios *t);
203 static void dgbhardclose (struct dgb_p *port);
204 static void dgb_drain_or_flush (struct dgb_p *port);
205 static int dgbdrain (struct dgb_p *port);
206 static void dgb_pause (void *chan);
207 static void wakeflush (void *p);
208 static void disc_optim (struct tty *tp, struct termios *t);
211 struct isa_driver dgbdriver = {
212 dgbprobe, dgbattach, "dgb",0
215 static d_open_t dgbopen;
216 static d_close_t dgbclose;
217 static d_ioctl_t dgbioctl;
219 #define CDEV_MAJOR 58
220 static struct cdevsw dgb_cdevsw = {
222 /* maj */ CDEV_MAJOR,
228 /* close */ dgbclose,
230 /* write */ ttywrite,
231 /* ioctl */ dgbioctl,
233 /* strategy */ nostrategy,
236 /* kqfilter */ ttykqfilter
239 static speed_t dgbdefaultrate = TTYDEF_SPEED;
241 static struct speedtab dgbspeedtab[] = {
242 { 0, FEP_B0 }, /* old (sysV-like) Bx codes */
256 { 19200, FEP_B19200 },
257 { 38400, FEP_B38400 },
258 { 57600, (FEP_FASTBAUD|FEP_B50) }, /* B50 & fast baud table */
259 { 115200, (FEP_FASTBAUD|FEP_B110) }, /* B100 & fast baud table */
263 static struct dbgflagtbl
270 { PARODD, PARODD, FEP_PARODD },
271 { PARENB, PARENB, FEP_PARENB },
272 { CSTOPB, CSTOPB, FEP_CSTOPB },
273 { CSIZE, CS5, FEP_CS6 },
274 { CSIZE, CS6, FEP_CS6 },
275 { CSIZE, CS7, FEP_CS7 },
276 { CSIZE, CS8, FEP_CS8 },
277 { CLOCAL, CLOCAL, FEP_CLOCAL },
281 { IGNBRK, IGNBRK, FEP_IGNBRK },
282 { BRKINT, BRKINT, FEP_BRKINT },
283 { IGNPAR, IGNPAR, FEP_IGNPAR },
284 { PARMRK, PARMRK, FEP_PARMRK },
285 { INPCK, INPCK, FEP_INPCK },
286 { ISTRIP, ISTRIP, FEP_ISTRIP },
287 { IXON, IXON, FEP_IXON },
288 { IXOFF, IXOFF, FEP_IXOFF },
289 { IXANY, IXANY, FEP_IXANY },
293 { CRTSCTS, CRTSCTS, CTS|RTS },
294 { CRTSCTS, CCTS_OFLOW, CTS },
295 { CRTSCTS, CRTS_IFLOW, RTS },
299 /* xlat bsd termios flags to dgb sys-v style */
301 dgbflags(struct dbgflagtbl *tbl, tcflag_t input)
306 for (i=0; tbl[i].in_mask != (tcflag_t)-1; i++)
308 if ((input & tbl[i].in_mask) == tbl[i].in_val)
309 output |= tbl[i].out_val;
315 static int dgbdebug=0;
316 SYSCTL_INT(_debug, OID_AUTO, dgb_debug, CTLFLAG_RW, &dgbdebug, 0, "");
319 static __inline int setwin (struct dgb_softc *sc, unsigned addr);
320 static __inline int setinitwin (struct dgb_softc *sc, unsigned addr);
321 static __inline void hidewin (struct dgb_softc *sc);
322 static __inline void towin (struct dgb_softc *sc, int win);
324 /*Helg: to allow recursive dgb...() calls */
326 { /* If we were called and don't want to disturb we need: */
327 int port; /* write to this port */
328 u_char data; /* this data on exit */
329 /* or DATA_WINOFF to close memory window on entry */
330 } BoardMemWinState; /* so several channels and even boards can coexist */
331 #define DATA_WINOFF 0
332 static BoardMemWinState bmws;
334 /* return current memory window state and close window */
335 static BoardMemWinState
338 BoardMemWinState bmwsRet=bmws;
339 if(bmws.data!=DATA_WINOFF)
340 outb(bmws.port, bmws.data=DATA_WINOFF);
344 /* restore memory window state */
346 bmws_set(BoardMemWinState ws)
348 if(ws.data != bmws.data || ws.port!=bmws.port ) {
349 if(bmws.data!=DATA_WINOFF)
350 outb(bmws.port,DATA_WINOFF);
351 if(ws.data!=DATA_WINOFF)
352 outb(ws.port, ws.data);
359 struct dgb_softc *sc;
362 if(sc->type==PCXEVE) {
363 outb(bmws.port=sc->port+1, bmws.data=FEPWIN|(addr>>13));
364 DPRINT3(DB_WIN,"dgb%d: switched to window 0x%x\n",sc->unit,addr>>13);
365 return (addr & 0x1FFF);
367 outb(bmws.port=sc->port,bmws.data=FEPMEM);
374 struct dgb_softc *sc;
377 if(sc->type==PCXEVE) {
378 outb(bmws.port=sc->port+1, bmws.data=FEPWIN|(addr>>13));
379 DPRINT3(DB_WIN,"dgb%d: switched to window 0x%x\n",sc->unit,addr>>13);
380 return (addr & 0x1FFF);
382 outb(bmws.port=sc->port,bmws.data=inb(sc->port)|FEPMEM);
389 struct dgb_softc *sc;
393 outb(bmws.port=sc->port+1, bmws.data);
395 outb(bmws.port=sc->port, bmws.data);
400 struct dgb_softc *sc;
403 if(sc->type==PCXEVE) {
404 outb(bmws.port=sc->port+1, bmws.data=win);
406 outb(bmws.port=sc->port,bmws.data=FEPMEM);
412 struct isa_device *dev;
414 struct dgb_softc *sc= &dgb_softc[dev->id_unit];
416 u_long win_size; /* size of vizible memory window */
417 int unit=dev->id_unit;
419 lwkt_gettoken(&tty_token);
420 sc->unit=dev->id_unit;
421 sc->port=dev->id_iobase;
423 if(dev->id_flags & DGBFLAG_ALTPIN)
428 /* left 24 bits only (ISA address) */
429 sc->pmem=((intptr_t)(void *)dev->id_maddr & 0xFFFFFF);
431 DPRINT4(DB_INFO,"dgb%d: port 0x%x mem 0x%lx\n",unit,sc->port,sc->pmem);
433 outb(sc->port, FEPRST);
436 for(i=0; i< 1000; i++) {
438 if( (inb(sc->port) & FEPMASK) == FEPRST ) {
440 DPRINT3(DB_EXCEPT,"dgb%d: got reset after %d us\n",unit,i);
445 if(sc->status!=ENABLED) {
446 DPRINT2(DB_EXCEPT,"dgb%d: failed to respond\n",dev->id_unit);
447 lwkt_reltoken(&tty_token);
451 /* check type of card and get internal memory characteristics */
460 kprintf("dgb%d: PC/Xi 64K\n",dev->id_unit);
465 kprintf("dgb%d: PC/Xi 128K\n",dev->id_unit);
470 kprintf("dgb%d: PC/Xi 256K\n",dev->id_unit);
472 default: /* case 0x30: */
475 kprintf("dgb%d: PC/Xi 512K\n",dev->id_unit);
484 kprintf("dgb%d: PC/Xm isn't supported\n",dev->id_unit);
486 lwkt_reltoken(&tty_token);
492 if(dev->id_flags==DGBFLAG_NOWIN || ( v&0xC0 )==0) {
494 kprintf("dgb%d: PC/Xe 64K\n",dev->id_unit);
498 kprintf("dgb%d: PC/Xe 64/8K (windowed)\n",dev->id_unit);
500 if((u_long)sc->pmem & ~0xFFE000) {
501 kprintf("dgb%d: warning: address 0x%lx truncated to 0x%lx\n",
502 dev->id_unit, sc->pmem,
503 sc->pmem & 0xFFE000);
505 dev->id_maddr= (u_char *)(void *)(intptr_t)( sc->pmem & 0xFFE000 );
510 /* save size of vizible memory segment */
511 dev->id_msize=win_size;
514 dev->id_maddr=sc->vmem=pmap_mapdev(sc->pmem,dev->id_msize);
516 outb(sc->port, FEPCLR); /* drop RESET */
517 hidewin(sc); /* Helg: to set initial bmws state */
519 lwkt_reltoken(&tty_token);
520 return 4; /* we need I/O space of 4 ports */
525 struct isa_device *dev;
527 int unit=dev->id_unit;
528 struct dgb_softc *sc= &dgb_softc[dev->id_unit];
530 u_char volatile *mem;
531 u_char volatile *ptr;
534 volatile struct board_chan *bc;
537 const volatile ushort *pstat;
542 lwkt_gettoken(&tty_token);
543 if(sc->status!=ENABLED) {
544 DPRINT2(DB_EXCEPT,"dbg%d: try to attach a disabled card\n",unit);
545 lwkt_reltoken(&tty_token);
549 callout_init_mp(&sc->dgb_pause);
550 callout_init_mp(&sc->dgbpoll);
551 callout_init_mp(&sc->wakeflush);
555 DPRINT3(DB_INFO,"dgb%d: internal memory segment 0x%x\n",unit,sc->mem_seg);
557 outb(sc->port, FEPRST); DELAY(1);
559 for(i=0; (inb(sc->port) & FEPMASK) != FEPRST ; i++) {
561 kprintf("dgb%d: 1st reset failed\n",dev->id_unit);
564 lwkt_reltoken(&tty_token);
570 DPRINT3(DB_INFO,"dgb%d: got reset after %d us\n",unit,i);
572 /* for PCXEVE set up interrupt and base address */
574 if(sc->type==PCXEVE) {
575 t=(((u_long)sc->pmem>>8) & 0xFFE0) | 0x10 /* enable windowing */;
577 outb(sc->port+2,t & 0xFF);
578 outb(sc->port+3,t>>8);
579 } else if(sc->type==PCXE) {
580 t=(((u_long)sc->pmem>>8) & 0xFFE0) /* disable windowing */;
581 outb(sc->port+2,t & 0xFF);
582 outb(sc->port+3,t>>8);
586 if(sc->type==PCXI || sc->type==PCXE) {
587 outb(sc->port, FEPRST|FEPMEM); DELAY(1);
589 for(i=0; (inb(sc->port) & FEPMASK) != (FEPRST|FEPMEM) ; i++) {
591 kprintf("dgb%d: 2nd reset failed\n",dev->id_unit);
594 lwkt_reltoken(&tty_token);
600 DPRINT3(DB_INFO,"dgb%d: got memory after %d us\n",unit,i);
605 /* very short memory test */
607 addr=setinitwin(sc,BOTWIN);
608 *(u_long volatile *)(mem+addr) = 0xA55A3CC3;
609 if(*(u_long volatile *)(mem+addr)!=0xA55A3CC3) {
610 kprintf("dgb%d: 1st memory test failed\n",dev->id_unit);
613 lwkt_reltoken(&tty_token);
617 addr=setinitwin(sc,TOPWIN);
618 *(u_long volatile *)(mem+addr) = 0x5AA5C33C;
619 if(*(u_long volatile *)(mem+addr)!=0x5AA5C33C) {
620 kprintf("dgb%d: 2nd memory test failed\n",dev->id_unit);
623 lwkt_reltoken(&tty_token);
627 addr=setinitwin(sc,BIOSCODE+((0xF000-sc->mem_seg)<<4));
628 *(u_long volatile *)(mem+addr) = 0x5AA5C33C;
629 if(*(u_long volatile *)(mem+addr)!=0x5AA5C33C) {
630 kprintf("dgb%d: 3rd (BIOS) memory test failed\n",dev->id_unit);
633 addr=setinitwin(sc,MISCGLOBAL);
634 for(i=0; i<16; i++) {
638 if(sc->type==PCXI || sc->type==PCXE) {
640 addr=BIOSCODE+((0xF000-sc->mem_seg)<<4);
642 DPRINT3(DB_INFO,"dgb%d: BIOS local address=0x%x\n",unit,addr);
646 for(i=0; i<pcxx_nbios; i++, ptr++)
652 for(i=0; i<pcxx_nbios; i++, ptr++)
653 if( *ptr != pcxx_bios[i] ) {
654 DPRINT5(DB_EXCEPT,"dgb%d: wrong code in BIOS at addr 0x%x : \
655 0x%x instead of 0x%x\n", unit, ptr-(mem+addr), *ptr, pcxx_bios[i] );
658 kprintf("dgb%d: 4th memory test (BIOS load) fails\n",unit);
663 outb(sc->port,FEPMEM);
665 for(i=0; (inb(sc->port) & FEPMASK) != FEPMEM ; i++) {
667 kprintf("dgb%d: BIOS start failed\n",dev->id_unit);
670 lwkt_reltoken(&tty_token);
676 DPRINT3(DB_INFO,"dgb%d: reset dropped after %d us\n",unit,i);
678 for(i=0; i<200000; i++) {
679 if( *((ushort volatile *)(mem+MISCGLOBAL)) == *((ushort *)"GD") )
683 kprintf("dgb%d: BIOS download failed\n",dev->id_unit);
684 DPRINT4(DB_EXCEPT,"dgb%d: code=0x%x must be 0x%x\n",
686 *((ushort volatile *)(mem+MISCGLOBAL)),
691 lwkt_reltoken(&tty_token);
695 if(sc->type==PCXEVE) {
697 outb(sc->port+1,0xFF);
699 ptr= mem+(BIOSCODE & 0x1FFF);
701 for(i=0; i<pcxx_nbios; i++)
702 *ptr++ = pcxx_bios[i];
704 ptr= mem+(BIOSCODE & 0x1FFF);
707 for(i=0; i<pcxx_nbios; i++, ptr++)
708 if( *ptr != pcxx_bios[i] ) {
709 DPRINT5(DB_EXCEPT,"dgb%d: wrong code in BIOS at addr 0x%x : \
710 0x%x instead of 0x%x\n", unit, ptr-(mem+addr), *ptr, pcxx_bios[i] );
713 kprintf("dgb%d: 4th memory test (BIOS load) fails\n",unit);
718 outb(sc->port,FEPCLR);
722 for(i=0; (inb(sc->port) & FEPMASK) != FEPCLR ; i++) {
724 kprintf("dgb%d: BIOS start failed\n",dev->id_unit);
727 lwkt_reltoken(&tty_token);
733 DPRINT3(DB_INFO,"dgb%d: reset dropped after %d us\n",unit,i);
735 addr=setwin(sc,MISCGLOBAL);
737 for(i=0; i<200000; i++) {
738 if(*(ushort volatile *)(mem+addr)== *(ushort *)"GD")
742 kprintf("dgb%d: BIOS download failed\n",dev->id_unit);
743 DPRINT5(DB_EXCEPT,"dgb%d: Error#(0x%x,0x%x) code=0x%x\n",
745 *(ushort volatile *)(mem+0xC12),
746 *(ushort volatile *)(mem+0xC14),
747 *(ushort volatile *)(mem+MISCGLOBAL));
751 lwkt_reltoken(&tty_token);
756 DPRINT2(DB_INFO,"dgb%d: BIOS loaded\n",dev->id_unit);
758 addr=setwin(sc,FEPCODE);
762 for(i=0; i<pcxx_ncook; i++)
763 *ptr++ = pcxx_cook[i];
765 addr=setwin(sc,MBOX);
766 *(ushort volatile *)(mem+addr+ 0)=2;
767 *(ushort volatile *)(mem+addr+ 2)=sc->mem_seg+FEPCODESEG;
768 *(ushort volatile *)(mem+addr+ 4)=0;
769 *(ushort volatile *)(mem+addr+ 6)=FEPCODESEG;
770 *(ushort volatile *)(mem+addr+ 8)=0;
771 *(ushort volatile *)(mem+addr+10)=pcxx_ncook;
773 outb(sc->port,FEPMEM|FEPINT); /* send interrupt to BIOS */
774 outb(sc->port,FEPMEM);
776 for(i=0; *(ushort volatile *)(mem+addr)!=0; i++) {
778 kprintf("dgb%d: FEP code download failed\n",unit);
779 DPRINT3(DB_EXCEPT,"dgb%d: code=0x%x must be 0\n", unit,
780 *(ushort volatile *)(mem+addr));
783 lwkt_reltoken(&tty_token);
788 DPRINT2(DB_INFO,"dgb%d: FEP code loaded\n",unit);
790 *(ushort volatile *)(mem+setwin(sc,FEPSTAT))=0;
791 addr=setwin(sc,MBOX);
792 *(ushort volatile *)(mem+addr+0)=1;
793 *(ushort volatile *)(mem+addr+2)=FEPCODESEG;
794 *(ushort volatile *)(mem+addr+4)=0x4;
796 outb(sc->port,FEPINT); /* send interrupt to BIOS */
797 outb(sc->port,FEPCLR);
799 addr=setwin(sc,FEPSTAT);
800 for(i=0; *(ushort volatile *)(mem+addr)!= *(ushort *)"OS"; i++) {
802 kprintf("dgb%d: FEP/OS start failed\n",dev->id_unit);
805 lwkt_reltoken(&tty_token);
810 DPRINT2(DB_INFO,"dgb%d: FEP/OS started\n",dev->id_unit);
812 sc->numports= *(ushort volatile *)(mem+setwin(sc,NPORT));
814 kprintf("dgb%d: %d ports\n",unit,sc->numports);
816 if(sc->numports > MAX_DGB_PORTS) {
817 kprintf("dgb%d: too many ports\n",unit);
820 lwkt_reltoken(&tty_token);
824 if(nports+sc->numports>NDGBPORTS) {
825 kprintf("dgb%d: only %d ports are usable\n", unit, NDGBPORTS-nports);
826 sc->numports=NDGBPORTS-nports;
829 /* allocate port and tty structures */
830 sc->ports=&dgb_ports[nports];
831 sc->ttys=&dgb_tty[nports];
832 nports+=sc->numports;
834 addr=setwin(sc,PORTBASE);
835 pstat=(const ushort volatile *)(mem+addr);
837 for(i=0; i<sc->numports && pstat[i]; i++)
839 sc->ports[i].status=ENABLED;
841 sc->ports[i].status=DISABLED;
842 kprintf("dgb%d: port%d is broken\n", unit, i);
845 /* We should now init per-port structures */
846 bc=(volatile struct board_chan *)(mem + CHANSTRUCT);
847 sc->mailbox=(volatile struct global_data *)(mem + FEP_GLOBAL);
854 for(i=0; i<sc->numports; i++, bc++) {
857 port->tty=&sc->ttys[i];
873 DPRINT2(DB_INFO,"dgb%d: shrinking memory\n",unit);
874 fepcmd(port, SETBUFFER, 32, 0, 0, 0);
878 if(sc->type!=PCXEVE) {
879 port->txptr=mem+((bc->tseg-sc->mem_seg)<<4);
880 port->rxptr=mem+((bc->rseg-sc->mem_seg)<<4);
881 port->txwin=port->rxwin=0;
883 port->txptr=mem+( ((bc->tseg-sc->mem_seg)<<4) & 0x1FFF );
884 port->rxptr=mem+( ((bc->rseg-sc->mem_seg)<<4) & 0x1FFF );
885 port->txwin=FEPWIN | ((bc->tseg-sc->mem_seg)>>9);
886 port->rxwin=FEPWIN | ((bc->rseg-sc->mem_seg)>>9);
891 port->txbufsize=bc->tmax+1;
892 port->rxbufsize=bc->rmax+1;
894 lowwater= (port->txbufsize>=2000) ? 1024 : (port->txbufsize/2);
896 fepcmd(port, STXLWATER, lowwater, 0, 10, 0);
897 fepcmd(port, SRXLWATER, port->rxbufsize/4, 0, 10, 0);
898 fepcmd(port, SRXHWATER, 3*port->rxbufsize/4, 0, 10, 0);
903 port->startc=bc->startc;
904 port->startca=bc->startca;
905 port->stopc=bc->stopc;
906 port->stopca=bc->stopca;
908 /*port->close_delay=50;*/
909 port->close_delay=3 * hz;
910 port->do_timestamp=0;
911 port->do_dcd_timestamp=0;
914 * We don't use all the flags from <sys/ttydefaults.h> since they
915 * are only relevant for logins. It's important to have echo off
916 * initially so that the line doesn't start blathering before the
917 * echo flag can be turned off.
919 port->it_in.c_iflag = TTYDEF_IFLAG;
920 port->it_in.c_oflag = TTYDEF_OFLAG;
921 port->it_in.c_cflag = TTYDEF_CFLAG;
922 port->it_in.c_lflag = TTYDEF_LFLAG;
923 termioschars(&port->it_in);
924 port->it_in.c_ispeed = port->it_in.c_ospeed = dgbdefaultrate;
925 port->it_out = port->it_in;
926 /* MAX_DGB_PORTS is 32 => [0-9a-v] */
927 suffix = i < 10 ? '0' + i : 'a' + i - 10;
928 cdevsw_add(&dgb_cdevsw, 0xffff0000, unit << 16);
929 make_dev(&dgb_cdevsw, (unit<<16)+i,
930 UID_ROOT, GID_WHEEL, 0600, "ttyD%d%c", unit, suffix);
932 make_dev(&dgb_cdevsw, (unit<<16)+i+32,
933 UID_ROOT, GID_WHEEL, 0600, "ttyiD%d%c", unit, suffix);
935 make_dev(&dgb_cdevsw, (unit<<16)+i+64,
936 UID_ROOT, GID_WHEEL, 0600, "ttylD%d%c", unit, suffix);
938 make_dev(&dgb_cdevsw, (unit<<16)+i+128,
939 UID_UUCP, GID_DIALER, 0660, "cuaD%d%c", unit, suffix);
941 make_dev(&dgb_cdevsw, (unit<<16)+i+160,
942 UID_UUCP, GID_DIALER, 0660, "cuaiD%d%c", unit, suffix);
944 make_dev(&dgb_cdevsw, (unit<<16)+i+192,
945 UID_UUCP, GID_DIALER, 0660, "cualD%d%c", unit, suffix);
950 /* register the polling function */
951 callout_reset(&sc->dgbpoll, hz / POLLSPERSEC, dgbpoll, (void *)unit);
953 lwkt_reltoken(&tty_token);
959 dgbopen(cdev_t dev, int flag, int mode, struct thread *td)
961 struct dgb_softc *sc;
968 volatile struct board_chan *bc;
972 lwkt_gettoken(&tty_token);
974 unit=MINOR_TO_UNIT(mynor);
975 pnum=MINOR_TO_PORT(mynor);
978 DPRINT2(DB_EXCEPT,"dgb%d: try to open a nonexisting card\n",unit);
979 lwkt_reltoken(&tty_token);
985 if(sc->status!=ENABLED) {
986 DPRINT2(DB_EXCEPT,"dgb%d: try to open a disabled card\n",unit);
987 lwkt_reltoken(&tty_token);
991 if(pnum>=sc->numports) {
992 DPRINT3(DB_EXCEPT,"dgb%d: try to open non-existing port %d\n",unit,pnum);
993 lwkt_reltoken(&tty_token);
997 if(mynor & CONTROL_MASK) {
998 lwkt_reltoken(&tty_token);
1004 port=&sc->ports[pnum];
1011 while(port->closing) {
1012 error=tsleep(&port->closing, PCATCH, "dgocl", 0);
1015 DPRINT4(DB_OPEN,"dgb%d: port%d: tsleep(dgocl) error=%d\n",unit,pnum,error);
1020 if (tp->t_state & TS_ISOPEN) {
1022 * The device is open, so everything has been initialized.
1025 if (mynor & CALLOUT_MASK) {
1026 if (!port->active_out) {
1028 DPRINT4(DB_OPEN,"dgb%d: port%d: BUSY error=%d\n",unit,pnum,error);
1032 if (port->active_out) {
1033 if (flag & O_NONBLOCK) {
1035 DPRINT4(DB_OPEN,"dgb%d: port%d: BUSY error=%d\n",unit,pnum,error);
1038 error = tsleep(&port->active_out,
1041 DPRINT4(DB_OPEN,"dgb%d: port%d: tsleep(dgbi) error=%d\n",
1049 if (tp->t_state & TS_XCLUDE && priv_check(td, PRIV_ROOT)) {
1055 * The device isn't open, so there are no conflicts.
1056 * Initialize it. Initialization is done twice in many
1057 * cases: to preempt sleeping callin opens if we are
1058 * callout, and to complete a callin open after DCD rises.
1060 tp->t_oproc=dgbstart;
1061 tp->t_param=dgbparam;
1064 tp->t_termios= (mynor & CALLOUT_MASK) ?
1070 port->imodem=bc->mstat;
1071 bc->rout=bc->rin; /* clear input queue */
1073 #ifdef PRINT_BUFSIZE
1074 kprintf("dgb buffers tx=%x:%x rx=%x:%x\n",bc->tseg,bc->tmax,bc->rseg,bc->rmax);
1081 error=dgbparam(tp, &tp->t_termios);
1085 DPRINT4(DB_OPEN,"dgb%d: port%d: dgbparam error=%d\n",unit,pnum,error);
1089 /* handle fake DCD for callout devices */
1090 /* and initial DCD */
1092 if( (port->imodem & port->dcd) || mynor & CALLOUT_MASK )
1093 linesw[tp->t_line].l_modem(tp,1);
1098 * Wait for DCD if necessary.
1100 if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK)
1101 && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) {
1103 error = tsleep(TSA_CARR_ON(tp), PCATCH, "dgdcd", 0);
1106 DPRINT4(DB_OPEN,"dgb%d: port%d: tsleep(dgdcd) error=%d\n",unit,pnum,error);
1112 error = linesw[tp->t_line].l_open(dev, tp);
1113 disc_optim(tp,&tp->t_termios);
1114 DPRINT4(DB_OPEN,"dgb%d: port%d: l_open error=%d\n",unit,pnum,error);
1116 if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
1117 port->active_out = TRUE;
1121 /* If any port is open (i.e. the open() call is completed for it)
1122 * the device is busy
1126 disc_optim(tp,&tp->t_termios);
1129 if( !(tp->t_state & TS_ISOPEN) && port->wopeners==0 )
1132 DPRINT4(DB_OPEN,"dgb%d: port%d: open() returns %d\n",unit,pnum,error);
1134 lwkt_reltoken(&tty_token);
1140 dgbclose(cdev_t dev, int flag, int mode, struct thread *td)
1144 struct dgb_softc *sc;
1150 if(mynor & CONTROL_MASK)
1152 lwkt_gettoken(&tty_token);
1153 unit=MINOR_TO_UNIT(mynor);
1154 pnum=MINOR_TO_PORT(mynor);
1156 sc=&dgb_softc[unit];
1158 port=sc->ports+pnum;
1160 DPRINT3(DB_CLOSE,"dgb%d: port%d: closing\n",unit,pnum);
1162 DPRINT3(DB_CLOSE,"dgb%d: port%d: draining port\n",unit,pnum);
1163 dgb_drain_or_flush(port);
1168 DPRINT3(DB_CLOSE,"dgb%d: port%d: closing line disc\n",unit,pnum);
1169 linesw[tp->t_line].l_close(tp,flag);
1170 disc_optim(tp,&tp->t_termios);
1172 DPRINT3(DB_CLOSE,"dgb%d: port%d: hard closing\n",unit,pnum);
1174 DPRINT3(DB_CLOSE,"dgb%d: port%d: closing tty\n",unit,pnum);
1177 wakeup(&port->closing);
1180 /* mark the card idle when all ports are closed */
1182 for(i=0; i<sc->numports; i++)
1183 if(sc->ports[i].used)
1188 DPRINT3(DB_CLOSE,"dgb%d: port%d: closed\n",unit,pnum);
1190 wakeup(TSA_CARR_ON(tp));
1191 wakeup(&port->active_out);
1194 DPRINT3(DB_CLOSE,"dgb%d: port%d: close exit\n",unit,pnum);
1196 lwkt_reltoken(&tty_token);
1201 * NOTE: Must be called with tty_token held
1207 struct dgb_softc *sc=&dgb_softc[port->unit];
1208 volatile struct board_chan *bc=port->brdchan;
1210 ASSERT_LWKT_TOKEN_HELD(&tty_token);
1213 port->do_timestamp = 0;
1216 bc->idata=0; bc->iempty=0; bc->ilow=0;
1217 if(port->tty->t_cflag & HUPCL) {
1218 port->omodem &= ~(RTS|DTR);
1219 fepcmd(port, SETMODEM, 0, DTR|RTS, 0, 1);
1225 callout_reset(&sc->dgb_pause, hz / 2, dgb_pause, &port->brdchan);
1226 tsleep(&port->brdchan, PCATCH, "dgclo", 0);
1233 lwkt_gettoken(&tty_token);
1234 wakeup((caddr_t)chan);
1235 lwkt_reltoken(&tty_token);
1242 int unit=(int)unit_c;
1245 struct dgb_softc *sc=&dgb_softc[unit];
1248 int event, mstat, lstat;
1249 volatile struct board_chan *bc;
1256 int ibuf_full,obuf_full;
1258 lwkt_gettoken(&tty_token);
1259 BoardMemWinState ws=bmws_get();
1261 if(sc->status==DISABLED) {
1262 kprintf("dgb%d: polling of disabled board stopped\n",unit);
1263 lwkt_reltoken(&tty_token);
1269 head=sc->mailbox->ein;
1270 tail=sc->mailbox->eout;
1273 if(head >= FEP_IMAX-FEP_ISTART
1274 || tail >= FEP_IMAX-FEP_ISTART
1275 || (head|tail) & 03 ) {
1276 kprintf("dgb%d: event queue's head or tail is wrong! hd=%d,tl=%d\n", unit,head,tail);
1280 eventbuf=sc->vmem+tail+FEP_ISTART;
1286 port=&sc->ports[pnum];
1290 if(pnum>=sc->numports || port->status==DISABLED) {
1291 kprintf("dgb%d: port%d: got event on nonexisting port\n",unit,pnum);
1292 } else if(port->used || port->wopeners>0 ) {
1294 int wrapmask=port->rxbufsize-1;
1296 if( !(event & ALL_IND) )
1297 kprintf("dgb%d: port%d: ? event 0x%x mstat 0x%x lstat 0x%x\n",
1298 unit, pnum, event, mstat, lstat);
1300 if(event & DATA_IND) {
1301 DPRINT3(DB_DATA,"dgb%d: port%d: DATA_IND\n",unit,pnum);
1303 rhead=bc->rin & wrapmask;
1304 rtail=bc->rout & wrapmask;
1306 if( !(tp->t_cflag & CREAD) || !port->used ) {
1312 kprintf("dgb%d: port%d: overrun\n", unit, pnum);
1316 if(!(tp->t_state & TS_ISOPEN))
1319 for(ibuf_full=FALSE;rhead!=rtail && !ibuf_full;) {
1320 DPRINT5(DB_RXDATA,"dgb%d: port%d: p rx head=%d tail=%d\n",
1321 unit,pnum,rhead,rtail);
1326 size=port->rxbufsize-rtail;
1328 ptr=__DEVOLATILE(u_char *, port->rxptr+rtail);
1331 if( tp->t_rawq.c_cc + size > DGB_IBUFSIZE ) {
1332 size=DGB_IBUFSIZE-tp->t_rawq.c_cc;
1333 DPRINT1(DB_RXDATA,"*");
1338 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1339 DPRINT1(DB_RXDATA,"!");
1340 towin(sc,port->rxwin);
1343 tp->t_rawcc += size;
1344 b_to_q(ptr,size,&tp->t_rawq);
1350 towin(sc,port->rxwin);
1353 (*linesw[tp->t_line].l_rint)(chr, tp);
1358 rtail= (rtail + size) & wrapmask;
1360 rhead=bc->rin & wrapmask;
1368 if(event & MODEMCHG_IND) {
1369 DPRINT3(DB_MODEM,"dgb%d: port%d: MODEMCHG_IND\n",unit,pnum);
1371 if(mstat & port->dcd) {
1373 linesw[tp->t_line].l_modem(tp,1);
1375 wakeup(TSA_CARR_ON(tp));
1378 linesw[tp->t_line].l_modem(tp,0);
1380 if( port->draining) {
1382 wakeup(&port->draining);
1387 if(event & BREAK_IND) {
1388 if((tp->t_state & TS_ISOPEN) && (tp->t_iflag & IGNBRK)) {
1389 DPRINT3(DB_BREAK,"dgb%d: port%d: BREAK_IND\n",unit,pnum);
1391 linesw[tp->t_line].l_rint(TTY_BI, tp);
1396 /* Helg: with output flow control */
1398 if(event & (LOWTX_IND | EMPTYTX_IND) ) {
1399 DPRINT3(DB_TXDATA,"dgb%d: port%d: LOWTX_IND or EMPTYTX_IND\n",unit,pnum);
1401 if( (event & EMPTYTX_IND ) && tp->t_outq.c_cc==0
1402 && port->draining) {
1404 wakeup(&port->draining);
1405 bc->ilow=0; bc->iempty=0;
1408 int wrapmask=port->txbufsize-1;
1410 for(obuf_full=FALSE; tp->t_outq.c_cc!=0 && !obuf_full; ) {
1411 /* add "last-minute" data to write buffer */
1412 if(!(tp->t_state & TS_BUSY)) {
1414 #ifndef TS_ASLEEP /* post 2.0.5 FreeBSD */
1417 if(tp->t_outq.c_cc <= tp->t_lowat) {
1418 if(tp->t_state & TS_ASLEEP) {
1419 tp->t_state &= ~TS_ASLEEP;
1420 wakeup(TSA_OLOWAT(tp));
1422 /* selwakeup(&tp->t_wsel); */
1429 whead=bc->tin & wrapmask;
1430 wtail=bc->tout & wrapmask;
1435 size=port->txbufsize-whead;
1441 DPRINT5(DB_WR,"dgb: head=%d tail=%d size=%d full=%d\n",
1442 whead,wtail,size,obuf_full);
1443 bc->iempty=1; bc->ilow=1;
1449 towin(sc,port->txwin);
1451 ocount=q_to_b(&tp->t_outq, __DEVOLATILE(u_char *, port->txptr+whead), size);
1456 bc->tin=whead & wrapmask;
1461 DPRINT1(DB_WR," +BUSY\n");
1462 tp->t_state|=TS_BUSY;
1464 DPRINT1(DB_WR," -BUSY\n");
1466 #ifndef TS_ASLEEP /* post 2.0.5 FreeBSD */
1467 /* should clear TS_BUSY before ttwwakeup */
1468 if(tp->t_state & TS_BUSY) {
1469 tp->t_state &= ~TS_BUSY;
1470 linesw[tp->t_line].l_start(tp);
1474 if(tp->t_state & TS_ASLEEP) {
1475 tp->t_state &= ~TS_ASLEEP;
1476 wakeup(TSA_OLOWAT(tp));
1478 tp->t_state &= ~TS_BUSY;
1484 bc->idata=1; /* require event on incoming data */
1488 DPRINT4(DB_EXCEPT,"dgb%d: port%d: got event 0x%x on closed port\n",
1491 bc->idata=bc->iempty=bc->ilow=0;
1494 tail= (tail+4) & (FEP_IMAX-FEP_ISTART-4);
1497 sc->mailbox->eout=tail;
1500 callout_reset(&sc->dgbpoll, hz / POLLSPERSEC, dgbpoll, unit_c);
1501 lwkt_reltoken(&tty_token);
1505 dgbioctl(cdev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1507 struct dgb_softc *sc;
1512 volatile struct board_chan *bc;
1516 lwkt_gettoken(&tty_token);
1517 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1519 struct termios term;
1522 BoardMemWinState ws=bmws_get();
1525 unit=MINOR_TO_UNIT(mynor);
1526 pnum=MINOR_TO_PORT(mynor);
1528 sc=&dgb_softc[unit];
1529 port=&sc->ports[pnum];
1533 if (mynor & CONTROL_MASK) {
1536 switch (mynor & CONTROL_MASK) {
1537 case CONTROL_INIT_STATE:
1538 ct = mynor & CALLOUT_MASK ? &port->it_out : &port->it_in;
1540 case CONTROL_LOCK_STATE:
1541 ct = mynor & CALLOUT_MASK ? &port->lt_out : &port->lt_in;
1544 lwkt_reltoken(&tty_token);
1545 return (ENODEV); /* /dev/nodev */
1549 error = priv_check(td, PRIV_ROOT);
1551 lwkt_reltoken(&tty_token);
1554 *ct = *(struct termios *)data;
1555 lwkt_reltoken(&tty_token);
1558 *(struct termios *)data = *ct;
1559 lwkt_reltoken(&tty_token);
1562 *(int *)data = TTYDISC;
1563 lwkt_reltoken(&tty_token);
1566 bzero(data, sizeof(struct winsize));
1567 lwkt_reltoken(&tty_token);
1570 lwkt_reltoken(&tty_token);
1575 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1576 term = tp->t_termios;
1577 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1578 DPRINT6(DB_PARAM,"dgb%d: port%d: dgbioctl-ISNOW c=0x%x i=0x%x l=0x%x\n",unit,pnum,term.c_cflag,term.c_iflag,term.c_lflag);
1581 error = ttsetcompat(tp, &cmd, data, &term);
1583 lwkt_reltoken(&tty_token);
1587 data = (caddr_t)&term;
1590 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1592 struct termios *dt = (struct termios *)data;
1593 struct termios *lt = mynor & CALLOUT_MASK
1594 ? &port->lt_out : &port->lt_in;
1596 DPRINT6(DB_PARAM,"dgb%d: port%d: dgbioctl-TOSET c=0x%x i=0x%x l=0x%x\n",unit,pnum,dt->c_cflag,dt->c_iflag,dt->c_lflag);
1597 dt->c_iflag = (tp->t_iflag & lt->c_iflag)
1598 | (dt->c_iflag & ~lt->c_iflag);
1599 dt->c_oflag = (tp->t_oflag & lt->c_oflag)
1600 | (dt->c_oflag & ~lt->c_oflag);
1601 dt->c_cflag = (tp->t_cflag & lt->c_cflag)
1602 | (dt->c_cflag & ~lt->c_cflag);
1603 dt->c_lflag = (tp->t_lflag & lt->c_lflag)
1604 | (dt->c_lflag & ~lt->c_lflag);
1605 for (cc = 0; cc < NCCS; ++cc)
1606 if (lt->c_cc[cc] != 0)
1607 dt->c_cc[cc] = tp->t_cc[cc];
1608 if (lt->c_ispeed != 0)
1609 dt->c_ispeed = tp->t_ispeed;
1610 if (lt->c_ospeed != 0)
1611 dt->c_ospeed = tp->t_ospeed;
1617 fepcmd(port, PAUSETX, 0, 0, 0, 0);
1620 lwkt_reltoken(&tty_token);
1622 } else if(cmd==TIOCSTART) {
1625 fepcmd(port, RESUMETX, 0, 0, 0, 0);
1628 lwkt_reltoken(&tty_token);
1632 if(cmd==TIOCSETAW || cmd==TIOCSETAF)
1635 error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, td);
1636 if (error != ENOIOCTL) {
1637 lwkt_reltoken(&tty_token);
1641 error = ttioctl(tp, cmd, data, flag);
1642 disc_optim(tp,&tp->t_termios);
1644 if (error != ENOIOCTL) {
1646 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1647 DPRINT6(DB_PARAM,"dgb%d: port%d: dgbioctl-RES c=0x%x i=0x%x l=0x%x\n",unit,pnum,tp->t_cflag,tp->t_iflag,tp->t_lflag);
1649 lwkt_reltoken(&tty_token);
1655 /* Helg: commented */
1656 /* error=dgbdrain(port);*/
1660 lwkt_reltoken(&tty_token);
1667 /* now it sends 250 millisecond break because I don't know */
1668 /* how to send an infinite break */
1670 fepcmd(port, SENDBREAK, 250, 0, 10, 0);
1675 /* now it's empty */
1678 DPRINT3(DB_MODEM,"dgb%d: port%d: set DTR\n",unit,pnum);
1679 port->omodem |= DTR;
1682 fepcmd(port, SETMODEM, port->omodem, RTS, 0, 1);
1684 if( !(bc->mstat & DTR) ) {
1685 DPRINT3(DB_MODEM,"dgb%d: port%d: DTR is off\n",unit,pnum);
1692 DPRINT3(DB_MODEM,"dgb%d: port%d: reset DTR\n",unit,pnum);
1693 port->omodem &= ~DTR;
1696 fepcmd(port, SETMODEM, port->omodem, RTS|DTR, 0, 1);
1698 if( bc->mstat & DTR ) {
1699 DPRINT3(DB_MODEM,"dgb%d: port%d: DTR is on\n",unit,pnum);
1706 if(*(int *)data & TIOCM_DTR)
1709 port->omodem &=~DTR;
1711 if(*(int *)data & TIOCM_RTS)
1714 port->omodem &=~RTS;
1718 fepcmd(port, SETMODEM, port->omodem, RTS|DTR, 0, 1);
1723 if(*(int *)data & TIOCM_DTR)
1726 if(*(int *)data & TIOCM_RTS)
1731 fepcmd(port, SETMODEM, port->omodem, RTS|DTR, 0, 1);
1736 if(*(int *)data & TIOCM_DTR)
1737 port->omodem &=~DTR;
1739 if(*(int *)data & TIOCM_RTS)
1740 port->omodem &=~RTS;
1744 fepcmd(port, SETMODEM, port->omodem, RTS|DTR, 0, 1);
1750 port->imodem=bc->mstat;
1753 tiocm_xxx = TIOCM_LE; /* XXX - always enabled while open */
1755 DPRINT3(DB_MODEM,"dgb%d: port%d: modem stat -- ",unit,pnum);
1757 if (port->imodem & DTR) {
1758 DPRINT1(DB_MODEM,"DTR ");
1759 tiocm_xxx |= TIOCM_DTR;
1761 if (port->imodem & RTS) {
1762 DPRINT1(DB_MODEM,"RTS ");
1763 tiocm_xxx |= TIOCM_RTS;
1765 if (port->imodem & CTS) {
1766 DPRINT1(DB_MODEM,"CTS ");
1767 tiocm_xxx |= TIOCM_CTS;
1769 if (port->imodem & port->dcd) {
1770 DPRINT1(DB_MODEM,"DCD ");
1771 tiocm_xxx |= TIOCM_CD;
1773 if (port->imodem & port->dsr) {
1774 DPRINT1(DB_MODEM,"DSR ");
1775 tiocm_xxx |= TIOCM_DSR;
1777 if (port->imodem & RI) {
1778 DPRINT1(DB_MODEM,"RI ");
1779 tiocm_xxx |= TIOCM_RI;
1781 *(int *)data = tiocm_xxx;
1782 DPRINT1(DB_MODEM,"--\n");
1785 /* must be root since the wait applies to following logins */
1786 error = priv_check(td, PRIV_ROOT);
1789 lwkt_reltoken(&tty_token);
1792 port->close_delay = *(int *)data * hz / 100;
1795 *(int *)data = port->close_delay * 100 / hz;
1798 port->do_timestamp = TRUE;
1799 *(struct timeval *)data = port->timestamp;
1801 case TIOCDCDTIMESTAMP:
1802 port->do_dcd_timestamp = TRUE;
1803 *(struct timeval *)data = port->dcd_timestamp;
1808 lwkt_reltoken(&tty_token);
1814 lwkt_reltoken(&tty_token);
1822 struct dgb_p *port=p;
1824 lwkt_gettoken(&tty_token);
1825 wakeup(&port->draining);
1826 lwkt_reltoken(&tty_token);
1829 /* wait for the output to drain */
1831 * NOTE: Must be called with tty_token held
1837 struct dgb_softc *sc=&dgb_softc[port->unit];
1838 volatile struct board_chan *bc=port->brdchan;
1842 ASSERT_LWKT_TOKEN_HELD(&tty_token);
1843 BoardMemWinState ws=bmws_get();
1852 DPRINT5(DB_WR,"dgb%d: port%d: drain: head=%d tail=%d\n",
1853 port->unit, port->pnum, head, tail);
1857 callout_reset(&sc->wakeflush, hz, wakeflush,port);
1858 error=tsleep(&port->draining, PCATCH, "dgdrn", 0);
1863 DPRINT4(DB_WR,"dgb%d: port%d: tsleep(dgdrn) error=%d\n",
1864 port->unit,port->pnum,error);
1874 DPRINT5(DB_WR,"dgb%d: port%d: drain: head=%d tail=%d\n",
1875 port->unit, port->pnum, head, tail);
1880 /* wait for the output to drain */
1881 /* or simply clear the buffer it it's stopped */
1883 * NOTE: Must be called with tty_token held
1886 dgb_drain_or_flush(port)
1889 struct tty *tp=port->tty;
1890 struct dgb_softc *sc=&dgb_softc[port->unit];
1891 volatile struct board_chan *bc=port->brdchan;
1896 ASSERT_LWKT_TOKEN_HELD(&tty_token);
1904 while(tail!=head /* && tail!=lasttail */ ) {
1905 DPRINT5(DB_WR,"dgb%d: port%d: flush: head=%d tail=%d\n",
1906 port->unit, port->pnum, head, tail);
1908 /* if there is no carrier simply clean the buffer */
1909 if( !(tp->t_state & TS_CARR_ON) ) {
1918 callout_reset(&sc->wakeflush, hz, wakeflush, port);
1919 error=tsleep(&port->draining, PCATCH, "dgfls", 0);
1924 DPRINT4(DB_WR,"dgb%d: port%d: tsleep(dgfls) error=%d\n",
1925 port->unit,port->pnum,error);
1927 /* silently clean the buffer */
1940 DPRINT5(DB_WR,"dgb%d: port%d: flush: head=%d tail=%d\n",
1941 port->unit, port->pnum, head, tail);
1949 int unit=MINOR_TO_UNIT(minor(tp->t_dev));
1950 int pnum=MINOR_TO_PORT(minor(tp->t_dev));
1951 struct dgb_softc *sc=&dgb_softc[unit];
1952 struct dgb_p *port=&sc->ports[pnum];
1953 volatile struct board_chan *bc=port->brdchan;
1960 lwkt_gettoken(&tty_token);
1961 BoardMemWinState ws=bmws_get();
1963 DPRINT6(DB_PARAM,"dgb%d: port%d: dgbparm c=0x%x i=0x%x l=0x%x\n",unit,pnum,t->c_cflag,t->c_iflag,t->c_lflag);
1965 if(port->mustdrain) {
1966 DPRINT3(DB_PARAM,"dgb%d: port%d: must call dgbdrain()\n",unit,pnum);
1970 cflag=ttspeedtab(t->c_ospeed, dgbspeedtab);
1972 if (t->c_ispeed == 0)
1973 t->c_ispeed = t->c_ospeed;
1975 if (cflag < 0 /* || cflag > 0 && t->c_ispeed != t->c_ospeed */) {
1976 DPRINT4(DB_PARAM,"dgb%d: port%d: invalid cflag=0%o\n",unit,pnum,cflag);
1977 lwkt_reltoken(&tty_token);
1984 if(cflag==0) { /* hangup */
1985 DPRINT3(DB_PARAM,"dgb%d: port%d: hangup\n",unit,pnum);
1989 fepcmd(port, STOUT, (unsigned)head, 0, 0, 0);
1990 mval= port->omodem & ~(DTR|RTS);
1992 cflag |= dgbflags(dgb_cflags, t->c_cflag);
1994 if(cflag!=port->fepcflag) {
1995 port->fepcflag=cflag;
1996 DPRINT5(DB_PARAM,"dgb%d: port%d: set cflag=0x%x c=0x%x\n",
1997 unit,pnum,cflag,t->c_cflag&~CRTSCTS);
1998 fepcmd(port, SETCTRLFLAGS, (unsigned)cflag, 0, 0, 0);
2000 mval= port->omodem | (DTR|RTS);
2003 iflag=dgbflags(dgb_iflags, t->c_iflag);
2004 if(iflag!=port->fepiflag) {
2005 port->fepiflag=iflag;
2006 DPRINT5(DB_PARAM,"dgb%d: port%d: set iflag=0x%x c=0x%x\n",unit,pnum,iflag,t->c_iflag);
2007 fepcmd(port, SETIFLAGS, (unsigned)iflag, 0, 0, 0);
2012 hflow=dgbflags(dgb_flow, t->c_cflag);
2013 if(hflow!=port->hflow) {
2015 DPRINT5(DB_PARAM,"dgb%d: port%d: set hflow=0x%x f=0x%x\n",unit,pnum,hflow,t->c_cflag&CRTSCTS);
2016 fepcmd(port, SETHFLOW, (unsigned)hflow, 0xff, 0, 1);
2019 if(port->omodem != mval) {
2020 DPRINT5(DB_PARAM,"dgb%d: port%d: setting modem parameters 0x%x was 0x%x\n",
2021 unit,pnum,mval,port->omodem);
2023 fepcmd(port, SETMODEM, (unsigned)mval, RTS|DTR, 0, 1);
2026 if(port->fepstartc!=t->c_cc[VSTART] || port->fepstopc!=t->c_cc[VSTOP]) {
2027 DPRINT5(DB_PARAM,"dgb%d: port%d: set startc=%d, stopc=%d\n",unit,pnum,t->c_cc[VSTART],t->c_cc[VSTOP]);
2028 port->fepstartc=t->c_cc[VSTART];
2029 port->fepstopc=t->c_cc[VSTOP];
2030 fepcmd(port, SONOFFC, port->fepstartc, port->fepstopc, 0, 1);
2036 lwkt_reltoken(&tty_token);
2048 struct dgb_softc *sc;
2049 volatile struct board_chan *bc;
2054 lwkt_gettoken(&tty_token);
2055 BoardMemWinState ws=bmws_get();
2057 unit=MINOR_TO_UNIT(minor(tp->t_dev));
2058 pnum=MINOR_TO_PORT(minor(tp->t_dev));
2059 sc=&dgb_softc[unit];
2060 port=&sc->ports[pnum];
2063 wmask=port->txbufsize-1;
2067 while( tp->t_outq.c_cc!=0 ) {
2069 #ifndef TS_ASLEEP /* post 2.0.5 FreeBSD */
2072 if(tp->t_outq.c_cc <= tp->t_lowat) {
2073 if(tp->t_state & TS_ASLEEP) {
2074 tp->t_state &= ~TS_ASLEEP;
2075 wakeup(TSA_OLOWAT(tp));
2077 /*selwakeup(&tp->t_wsel);*/
2083 head=bc->tin & wmask;
2085 do { tail=bc->tout; } while (tail != bc->tout);
2086 tail=bc->tout & wmask;
2088 DPRINT5(DB_WR,"dgb%d: port%d: s tx head=%d tail=%d\n",unit,pnum,head,tail);
2090 #ifdef LEAVE_FREE_CHARS
2092 size=tail-head-LEAVE_FREE_CHARS;
2096 size=port->txbufsize-head;
2097 if(tail+port->txbufsize < head)
2105 size=port->txbufsize-head/*-1*/;
2112 bc->iempty=1; bc->ilow=1;
2115 tp->t_state|=TS_BUSY;
2117 lwkt_reltoken(&tty_token);
2121 towin(sc,port->txwin);
2123 ocount=q_to_b(&tp->t_outq, __DEVOLATILE(u_char *, port->txptr+head), size);
2125 if(head>=port->txbufsize)
2126 head-=port->txbufsize;
2131 DPRINT5(DB_WR,"dgb%d: port%d: tx avail=%d count=%d\n",unit,pnum,size,ocount);
2139 #ifndef TS_ASLEEP /* post 2.0.5 FreeBSD */
2140 if(tp->t_state & TS_BUSY) {
2141 tp->t_state&=~TS_BUSY;
2142 linesw[tp->t_line].l_start(tp);
2146 if(tp->t_state & TS_ASLEEP) {
2147 tp->t_state &= ~TS_ASLEEP;
2148 wakeup(TSA_OLOWAT(tp));
2150 tp->t_state&=~TS_BUSY;
2152 lwkt_reltoken(&tty_token);
2163 struct dgb_softc *sc;
2164 volatile struct board_chan *bc;
2166 lwkt_gettoken(&tty_token);
2167 BoardMemWinState ws=bmws_get();
2169 unit=MINOR_TO_UNIT(minor(tp->t_dev));
2170 pnum=MINOR_TO_PORT(minor(tp->t_dev));
2172 sc=&dgb_softc[unit];
2173 port=&sc->ports[pnum];
2176 DPRINT3(DB_WR,"dgb%d: port%d: stop\n",port->unit, port->pnum);
2182 /* clear output queue */
2184 bc->ilow=0;bc->iempty=0;
2187 /* clear input queue */
2195 lwkt_reltoken(&tty_token);
2199 * NOTE: Must be called with tty_token held
2202 fepcmd(port, cmd, op1, op2, ncmds, bytecmd)
2204 unsigned cmd, op1, op2, ncmds, bytecmd;
2206 struct dgb_softc *sc=&dgb_softc[port->unit];
2207 u_char *mem=sc->vmem;
2208 unsigned tail, head;
2211 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2213 if(port->status==DISABLED) {
2214 kprintf("dgb%d: port%d: FEP command on disabled port\n",
2215 port->unit, port->pnum);
2219 /* setwin(sc,0); Require this to be set by caller */
2220 head=sc->mailbox->cin;
2222 if(head>=(FEP_CMAX-FEP_CSTART) || (head & 3)) {
2223 kprintf("dgb%d: port%d: wrong pointer head of command queue : 0x%x\n",
2224 port->unit, port->pnum, head);
2228 mem[head+FEP_CSTART+0]=cmd;
2229 mem[head+FEP_CSTART+1]=port->pnum;
2231 mem[head+FEP_CSTART+2]=op1;
2232 mem[head+FEP_CSTART+3]=op2;
2234 mem[head+FEP_CSTART+2]=op1&0xff;
2235 mem[head+FEP_CSTART+3]=(op1>>8)&0xff;
2238 DPRINT7(DB_FEP,"dgb%d: port%d: %s cmd=0x%x op1=0x%x op2=0x%x\n", port->unit, port->pnum,
2239 (bytecmd)?"byte":"word", cmd, mem[head+FEP_CSTART+2], mem[head+FEP_CSTART+3]);
2241 head=(head+4) & (FEP_CMAX-FEP_CSTART-4);
2242 sc->mailbox->cin=head;
2246 while (count-- != 0) {
2247 head=sc->mailbox->cin;
2248 tail=sc->mailbox->cout;
2250 n = (head-tail) & (FEP_CMAX-FEP_CSTART-4);
2251 if(n <= ncmds * (sizeof(ushort)*4))
2254 kprintf("dgb%d(%d): timeout on FEP cmd=0x%x\n", port->unit, port->pnum, cmd);
2262 lwkt_gettoken(&tty_token);
2263 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
2264 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
2265 && (!(t->c_iflag & PARMRK)
2266 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
2267 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
2268 && linesw[tp->t_line].l_rint == ttyinput)
2269 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2271 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2272 lwkt_reltoken(&tty_token);