2 * dgb.c $FreeBSD: src/sys/gnu/i386/isa/dgb.c,v 1.56.2.1 2001/02/26 04:23:09 jlemon Exp $
3 * dgb.c $DragonFly: src/sys/platform/pc32/gnu/isa/dgb.c,v 1.19 2006/12/23 00:27:03 swildner Exp $
7 * Stage 1. "Better than nothing".
8 * Stage 2. "Gee, it works!".
10 * Based on sio driver by Bruce Evans and on Linux driver by Troy
11 * De Jongh <troyd@digibd.com> or <troyd@skypoint.com>
12 * which is under GNU General Public License version 2 so this driver
13 * is forced to be under GPL 2 too.
15 * Written by Serge Babkin,
16 * Joint Stock Commercial Bank "Chelindbank"
17 * (Chelyabinsk, Russia)
18 * babkin@hq.icb.chel.su
20 * Assorted hacks to make it more functional and working under 3.0-current.
21 * Fixed broken routines to prevent processes hanging on closed (thanks
22 * to Bruce for his patience and assistance). Thanks also to Maxim Bolotin
23 * <max@run.net> for his patches which did most of the work to get this
24 * running under 2.2/3.0-current.
25 * Implemented ioctls: TIOCMSDTRWAIT, TIOCMGDTRWAIT, TIOCTIMESTAMP &
27 * Sysctl debug flag is now a bitflag, to filter noise during debugging.
28 * David L. Nugent <davidn@blaze.net.au>
31 #include "opt_compat.h"
33 #include "opt_deprecated.h"
34 #ifndef I_WANT_DEPRECATED_STUFF
35 #error "Add options I_WANT_DEPRECATED_STUFF to your kernel config and send a mail to kernel@"
40 /* Helg: i.e.25 times per sec board will be polled */
41 #define POLLSPERSEC 25
42 /* How many charactes can we write to input tty rawq */
43 #define DGB_IBUFSIZE (TTYHOG-100)
45 /* the overall number of ports controlled by this driver */
48 # define NDGBPORTS (NDGB*16)
51 #include <sys/param.h>
52 #include <sys/systm.h>
56 #include <sys/dkstat.h>
57 #include <sys/fcntl.h>
58 #include <sys/kernel.h>
59 #include <sys/sysctl.h>
60 #include <sys/thread2.h>
62 #include <machine/clock.h>
67 #include <bus/isa/i386/isa_device.h>
72 #define DGB_DEBUG /* Enable debugging info via sysctl */
75 #define CALLOUT_MASK 0x80
76 #define CONTROL_MASK 0x60
77 #define CONTROL_INIT_STATE 0x20
78 #define CONTROL_LOCK_STATE 0x40
79 #define UNIT_MASK 0x30000
80 #define PORT_MASK 0x1F
81 #define DEV_TO_UNIT(dev) (MINOR_TO_UNIT(minor(dev)))
82 #define MINOR_MAGIC_MASK (CALLOUT_MASK | CONTROL_MASK)
83 #define MINOR_TO_UNIT(mynor) (((mynor) & UNIT_MASK)>>16)
84 #define MINOR_TO_PORT(mynor) ((mynor) & PORT_MASK)
86 /* types. XXX - should be elsewhere */
87 typedef u_char bool_t; /* boolean */
89 /* digiboard port structure */
93 u_char unit; /* board unit number */
94 u_char pnum; /* port number */
95 u_char omodem; /* FEP output modem status */
96 u_char imodem; /* FEP input modem status */
97 u_char modemfake; /* Modem values to be forced */
98 u_char modem; /* Force values */
125 volatile u_char *txptr;
126 volatile u_char *rxptr;
127 volatile struct board_chan *brdchan;
130 bool_t active_out; /* nonzero if the callout device is open */
131 u_int wopeners; /* # processes waiting for DCD in open() */
134 struct termios it_in; /* should be in struct tty */
135 struct termios it_out;
138 struct termios lt_in; /* should be in struct tty */
139 struct termios lt_out;
142 bool_t do_dcd_timestamp;
143 struct timeval timestamp;
144 struct timeval dcd_timestamp;
146 /* flags of state, are used in sleep() too */
147 u_char closing; /* port is being closed now */
148 u_char draining; /* port is being drained now */
149 u_char used; /* port is being used now */
150 u_char mustdrain; /* data must be waited to drain in dgbparam() */
153 /* Digiboard per-board structure */
155 /* struct board_info */
156 u_char status; /* status: DISABLED/ENABLED */
157 u_char unit; /* unit number */
158 u_char type; /* type of card: PCXE, PCXI, PCXEVE */
159 u_char altpin; /* do we need alternate pin setting ? */
160 int numports; /* number of ports on card */
161 int port; /* I/O port */
162 u_char *vmem; /* virtual memory address */
163 long pmem; /* physical memory address */
164 int mem_seg; /* internal memory segment */
165 struct dgb_p *ports; /* pointer to array of port descriptors */
166 struct tty *ttys; /* pointer to array of TTY structures */
167 volatile struct global_data *mailbox;
168 struct callout dgb_pause;
169 struct callout dgbpoll;
170 struct callout wakeflush;
174 static struct dgb_softc dgb_softc[NDGB];
175 static struct dgb_p dgb_ports[NDGBPORTS];
176 static struct tty dgb_tty[NDGBPORTS];
179 * The public functions in the com module ought to be declared in a com-driver
183 /* Interrupt handling entry points. */
184 static void dgbpoll (void *unit_c);
186 /* Device switch entry points. */
187 #define dgbreset noreset
188 #define dgbmmap nommap
189 #define dgbstrategy nostrategy
191 static int dgbattach (struct isa_device *dev);
192 static int dgbprobe (struct isa_device *dev);
194 static void fepcmd(struct dgb_p *port, unsigned cmd, unsigned op1, unsigned op2,
195 unsigned ncmds, unsigned bytecmd);
197 static void dgbstart (struct tty *tp);
198 static void dgbstop (struct tty *tp, int rw);
199 static int dgbparam (struct tty *tp, struct termios *t);
200 static void dgbhardclose (struct dgb_p *port);
201 static void dgb_drain_or_flush (struct dgb_p *port);
202 static int dgbdrain (struct dgb_p *port);
203 static void dgb_pause (void *chan);
204 static void wakeflush (void *p);
205 static void disc_optim (struct tty *tp, struct termios *t);
208 struct isa_driver dgbdriver = {
209 dgbprobe, dgbattach, "dgb",0
212 static d_open_t dgbopen;
213 static d_close_t dgbclose;
214 static d_ioctl_t dgbioctl;
216 #define CDEV_MAJOR 58
217 static struct cdevsw dgb_cdevsw = {
219 /* maj */ CDEV_MAJOR,
220 /* flags */ D_TTY | D_KQFILTER,
225 /* close */ dgbclose,
227 /* write */ ttywrite,
228 /* ioctl */ dgbioctl,
231 /* strategy */ nostrategy,
234 /* kqfilter */ ttykqfilter
237 static speed_t dgbdefaultrate = TTYDEF_SPEED;
239 static struct speedtab dgbspeedtab[] = {
240 { 0, FEP_B0 }, /* old (sysV-like) Bx codes */
254 { 19200, FEP_B19200 },
255 { 38400, FEP_B38400 },
256 { 57600, (FEP_FASTBAUD|FEP_B50) }, /* B50 & fast baud table */
257 { 115200, (FEP_FASTBAUD|FEP_B110) }, /* B100 & fast baud table */
261 static struct dbgflagtbl
268 { PARODD, PARODD, FEP_PARODD },
269 { PARENB, PARENB, FEP_PARENB },
270 { CSTOPB, CSTOPB, FEP_CSTOPB },
271 { CSIZE, CS5, FEP_CS6 },
272 { CSIZE, CS6, FEP_CS6 },
273 { CSIZE, CS7, FEP_CS7 },
274 { CSIZE, CS8, FEP_CS8 },
275 { CLOCAL, CLOCAL, FEP_CLOCAL },
279 { IGNBRK, IGNBRK, FEP_IGNBRK },
280 { BRKINT, BRKINT, FEP_BRKINT },
281 { IGNPAR, IGNPAR, FEP_IGNPAR },
282 { PARMRK, PARMRK, FEP_PARMRK },
283 { INPCK, INPCK, FEP_INPCK },
284 { ISTRIP, ISTRIP, FEP_ISTRIP },
285 { IXON, IXON, FEP_IXON },
286 { IXOFF, IXOFF, FEP_IXOFF },
287 { IXANY, IXANY, FEP_IXANY },
291 { CRTSCTS, CRTSCTS, CTS|RTS },
292 { CRTSCTS, CCTS_OFLOW, CTS },
293 { CRTSCTS, CRTS_IFLOW, RTS },
297 /* xlat bsd termios flags to dgb sys-v style */
299 dgbflags(struct dbgflagtbl *tbl, tcflag_t input)
304 for (i=0; tbl[i].in_mask != (tcflag_t)-1; i++)
306 if ((input & tbl[i].in_mask) == tbl[i].in_val)
307 output |= tbl[i].out_val;
313 static int dgbdebug=0;
314 SYSCTL_INT(_debug, OID_AUTO, dgb_debug, CTLFLAG_RW, &dgbdebug, 0, "");
317 static __inline int setwin (struct dgb_softc *sc, unsigned addr);
318 static __inline int setinitwin (struct dgb_softc *sc, unsigned addr);
319 static __inline void hidewin (struct dgb_softc *sc);
320 static __inline void towin (struct dgb_softc *sc, int win);
322 /*Helg: to allow recursive dgb...() calls */
324 { /* If we were called and don't want to disturb we need: */
325 int port; /* write to this port */
326 u_char data; /* this data on exit */
327 /* or DATA_WINOFF to close memory window on entry */
328 } BoardMemWinState; /* so several channels and even boards can coexist */
329 #define DATA_WINOFF 0
330 static BoardMemWinState bmws;
332 /* return current memory window state and close window */
333 static BoardMemWinState
336 BoardMemWinState bmwsRet=bmws;
337 if(bmws.data!=DATA_WINOFF)
338 outb(bmws.port, bmws.data=DATA_WINOFF);
342 /* restore memory window state */
344 bmws_set(BoardMemWinState ws)
346 if(ws.data != bmws.data || ws.port!=bmws.port ) {
347 if(bmws.data!=DATA_WINOFF)
348 outb(bmws.port,DATA_WINOFF);
349 if(ws.data!=DATA_WINOFF)
350 outb(ws.port, ws.data);
357 struct dgb_softc *sc;
360 if(sc->type==PCXEVE) {
361 outb(bmws.port=sc->port+1, bmws.data=FEPWIN|(addr>>13));
362 DPRINT3(DB_WIN,"dgb%d: switched to window 0x%x\n",sc->unit,addr>>13);
363 return (addr & 0x1FFF);
365 outb(bmws.port=sc->port,bmws.data=FEPMEM);
372 struct dgb_softc *sc;
375 if(sc->type==PCXEVE) {
376 outb(bmws.port=sc->port+1, bmws.data=FEPWIN|(addr>>13));
377 DPRINT3(DB_WIN,"dgb%d: switched to window 0x%x\n",sc->unit,addr>>13);
378 return (addr & 0x1FFF);
380 outb(bmws.port=sc->port,bmws.data=inb(sc->port)|FEPMEM);
387 struct dgb_softc *sc;
391 outb(bmws.port=sc->port+1, bmws.data);
393 outb(bmws.port=sc->port, bmws.data);
398 struct dgb_softc *sc;
401 if(sc->type==PCXEVE) {
402 outb(bmws.port=sc->port+1, bmws.data=win);
404 outb(bmws.port=sc->port,bmws.data=FEPMEM);
410 struct isa_device *dev;
412 struct dgb_softc *sc= &dgb_softc[dev->id_unit];
414 u_long win_size; /* size of vizible memory window */
415 int unit=dev->id_unit;
417 sc->unit=dev->id_unit;
418 sc->port=dev->id_iobase;
420 if(dev->id_flags & DGBFLAG_ALTPIN)
425 /* left 24 bits only (ISA address) */
426 sc->pmem=((intptr_t)(void *)dev->id_maddr & 0xFFFFFF);
428 DPRINT4(DB_INFO,"dgb%d: port 0x%x mem 0x%lx\n",unit,sc->port,sc->pmem);
430 outb(sc->port, FEPRST);
433 for(i=0; i< 1000; i++) {
435 if( (inb(sc->port) & FEPMASK) == FEPRST ) {
437 DPRINT3(DB_EXCEPT,"dgb%d: got reset after %d us\n",unit,i);
442 if(sc->status!=ENABLED) {
443 DPRINT2(DB_EXCEPT,"dgb%d: failed to respond\n",dev->id_unit);
447 /* check type of card and get internal memory characteristics */
456 kprintf("dgb%d: PC/Xi 64K\n",dev->id_unit);
461 kprintf("dgb%d: PC/Xi 128K\n",dev->id_unit);
466 kprintf("dgb%d: PC/Xi 256K\n",dev->id_unit);
468 default: /* case 0x30: */
471 kprintf("dgb%d: PC/Xi 512K\n",dev->id_unit);
480 kprintf("dgb%d: PC/Xm isn't supported\n",dev->id_unit);
487 if(dev->id_flags==DGBFLAG_NOWIN || ( v&0xC0 )==0) {
489 kprintf("dgb%d: PC/Xe 64K\n",dev->id_unit);
493 kprintf("dgb%d: PC/Xe 64/8K (windowed)\n",dev->id_unit);
495 if((u_long)sc->pmem & ~0xFFE000) {
496 kprintf("dgb%d: warning: address 0x%lx truncated to 0x%lx\n",
497 dev->id_unit, sc->pmem,
498 sc->pmem & 0xFFE000);
500 dev->id_maddr= (u_char *)(void *)(intptr_t)( sc->pmem & 0xFFE000 );
505 /* save size of vizible memory segment */
506 dev->id_msize=win_size;
509 dev->id_maddr=sc->vmem=pmap_mapdev(sc->pmem,dev->id_msize);
511 outb(sc->port, FEPCLR); /* drop RESET */
512 hidewin(sc); /* Helg: to set initial bmws state */
514 return 4; /* we need I/O space of 4 ports */
519 struct isa_device *dev;
521 int unit=dev->id_unit;
522 struct dgb_softc *sc= &dgb_softc[dev->id_unit];
524 u_char volatile *mem;
525 u_char volatile *ptr;
528 volatile struct board_chan *bc;
531 const volatile ushort *pstat;
536 if(sc->status!=ENABLED) {
537 DPRINT2(DB_EXCEPT,"dbg%d: try to attach a disabled card\n",unit);
541 callout_init(&sc->dgb_pause);
542 callout_init(&sc->dgbpoll);
543 callout_init(&sc->wakeflush);
547 DPRINT3(DB_INFO,"dgb%d: internal memory segment 0x%x\n",unit,sc->mem_seg);
549 outb(sc->port, FEPRST); DELAY(1);
551 for(i=0; (inb(sc->port) & FEPMASK) != FEPRST ; i++) {
553 kprintf("dgb%d: 1st reset failed\n",dev->id_unit);
561 DPRINT3(DB_INFO,"dgb%d: got reset after %d us\n",unit,i);
563 /* for PCXEVE set up interrupt and base address */
565 if(sc->type==PCXEVE) {
566 t=(((u_long)sc->pmem>>8) & 0xFFE0) | 0x10 /* enable windowing */;
568 outb(sc->port+2,t & 0xFF);
569 outb(sc->port+3,t>>8);
570 } else if(sc->type==PCXE) {
571 t=(((u_long)sc->pmem>>8) & 0xFFE0) /* disable windowing */;
572 outb(sc->port+2,t & 0xFF);
573 outb(sc->port+3,t>>8);
577 if(sc->type==PCXI || sc->type==PCXE) {
578 outb(sc->port, FEPRST|FEPMEM); DELAY(1);
580 for(i=0; (inb(sc->port) & FEPMASK) != (FEPRST|FEPMEM) ; i++) {
582 kprintf("dgb%d: 2nd reset failed\n",dev->id_unit);
590 DPRINT3(DB_INFO,"dgb%d: got memory after %d us\n",unit,i);
595 /* very short memory test */
597 addr=setinitwin(sc,BOTWIN);
598 *(u_long volatile *)(mem+addr) = 0xA55A3CC3;
599 if(*(u_long volatile *)(mem+addr)!=0xA55A3CC3) {
600 kprintf("dgb%d: 1st memory test failed\n",dev->id_unit);
606 addr=setinitwin(sc,TOPWIN);
607 *(u_long volatile *)(mem+addr) = 0x5AA5C33C;
608 if(*(u_long volatile *)(mem+addr)!=0x5AA5C33C) {
609 kprintf("dgb%d: 2nd memory test failed\n",dev->id_unit);
615 addr=setinitwin(sc,BIOSCODE+((0xF000-sc->mem_seg)<<4));
616 *(u_long volatile *)(mem+addr) = 0x5AA5C33C;
617 if(*(u_long volatile *)(mem+addr)!=0x5AA5C33C) {
618 kprintf("dgb%d: 3rd (BIOS) memory test failed\n",dev->id_unit);
621 addr=setinitwin(sc,MISCGLOBAL);
622 for(i=0; i<16; i++) {
626 if(sc->type==PCXI || sc->type==PCXE) {
628 addr=BIOSCODE+((0xF000-sc->mem_seg)<<4);
630 DPRINT3(DB_INFO,"dgb%d: BIOS local address=0x%x\n",unit,addr);
634 for(i=0; i<pcxx_nbios; i++, ptr++)
640 for(i=0; i<pcxx_nbios; i++, ptr++)
641 if( *ptr != pcxx_bios[i] ) {
642 DPRINT5(DB_EXCEPT,"dgb%d: wrong code in BIOS at addr 0x%x : \
643 0x%x instead of 0x%x\n", unit, ptr-(mem+addr), *ptr, pcxx_bios[i] );
646 kprintf("dgb%d: 4th memory test (BIOS load) fails\n",unit);
651 outb(sc->port,FEPMEM);
653 for(i=0; (inb(sc->port) & FEPMASK) != FEPMEM ; i++) {
655 kprintf("dgb%d: BIOS start failed\n",dev->id_unit);
663 DPRINT3(DB_INFO,"dgb%d: reset dropped after %d us\n",unit,i);
665 for(i=0; i<200000; i++) {
666 if( *((ushort volatile *)(mem+MISCGLOBAL)) == *((ushort *)"GD") )
670 kprintf("dgb%d: BIOS download failed\n",dev->id_unit);
671 DPRINT4(DB_EXCEPT,"dgb%d: code=0x%x must be 0x%x\n",
673 *((ushort volatile *)(mem+MISCGLOBAL)),
681 if(sc->type==PCXEVE) {
683 outb(sc->port+1,0xFF);
685 ptr= mem+(BIOSCODE & 0x1FFF);
687 for(i=0; i<pcxx_nbios; i++)
688 *ptr++ = pcxx_bios[i];
690 ptr= mem+(BIOSCODE & 0x1FFF);
693 for(i=0; i<pcxx_nbios; i++, ptr++)
694 if( *ptr != pcxx_bios[i] ) {
695 DPRINT5(DB_EXCEPT,"dgb%d: wrong code in BIOS at addr 0x%x : \
696 0x%x instead of 0x%x\n", unit, ptr-(mem+addr), *ptr, pcxx_bios[i] );
699 kprintf("dgb%d: 4th memory test (BIOS load) fails\n",unit);
704 outb(sc->port,FEPCLR);
708 for(i=0; (inb(sc->port) & FEPMASK) != FEPCLR ; i++) {
710 kprintf("dgb%d: BIOS start failed\n",dev->id_unit);
718 DPRINT3(DB_INFO,"dgb%d: reset dropped after %d us\n",unit,i);
720 addr=setwin(sc,MISCGLOBAL);
722 for(i=0; i<200000; i++) {
723 if(*(ushort volatile *)(mem+addr)== *(ushort *)"GD")
727 kprintf("dgb%d: BIOS download failed\n",dev->id_unit);
728 DPRINT5(DB_EXCEPT,"dgb%d: Error#(0x%x,0x%x) code=0x%x\n",
730 *(ushort volatile *)(mem+0xC12),
731 *(ushort volatile *)(mem+0xC14),
732 *(ushort volatile *)(mem+MISCGLOBAL));
740 DPRINT2(DB_INFO,"dgb%d: BIOS loaded\n",dev->id_unit);
742 addr=setwin(sc,FEPCODE);
746 for(i=0; i<pcxx_ncook; i++)
747 *ptr++ = pcxx_cook[i];
749 addr=setwin(sc,MBOX);
750 *(ushort volatile *)(mem+addr+ 0)=2;
751 *(ushort volatile *)(mem+addr+ 2)=sc->mem_seg+FEPCODESEG;
752 *(ushort volatile *)(mem+addr+ 4)=0;
753 *(ushort volatile *)(mem+addr+ 6)=FEPCODESEG;
754 *(ushort volatile *)(mem+addr+ 8)=0;
755 *(ushort volatile *)(mem+addr+10)=pcxx_ncook;
757 outb(sc->port,FEPMEM|FEPINT); /* send interrupt to BIOS */
758 outb(sc->port,FEPMEM);
760 for(i=0; *(ushort volatile *)(mem+addr)!=0; i++) {
762 kprintf("dgb%d: FEP code download failed\n",unit);
763 DPRINT3(DB_EXCEPT,"dgb%d: code=0x%x must be 0\n", unit,
764 *(ushort volatile *)(mem+addr));
771 DPRINT2(DB_INFO,"dgb%d: FEP code loaded\n",unit);
773 *(ushort volatile *)(mem+setwin(sc,FEPSTAT))=0;
774 addr=setwin(sc,MBOX);
775 *(ushort volatile *)(mem+addr+0)=1;
776 *(ushort volatile *)(mem+addr+2)=FEPCODESEG;
777 *(ushort volatile *)(mem+addr+4)=0x4;
779 outb(sc->port,FEPINT); /* send interrupt to BIOS */
780 outb(sc->port,FEPCLR);
782 addr=setwin(sc,FEPSTAT);
783 for(i=0; *(ushort volatile *)(mem+addr)!= *(ushort *)"OS"; i++) {
785 kprintf("dgb%d: FEP/OS start failed\n",dev->id_unit);
792 DPRINT2(DB_INFO,"dgb%d: FEP/OS started\n",dev->id_unit);
794 sc->numports= *(ushort volatile *)(mem+setwin(sc,NPORT));
796 kprintf("dgb%d: %d ports\n",unit,sc->numports);
798 if(sc->numports > MAX_DGB_PORTS) {
799 kprintf("dgb%d: too many ports\n",unit);
805 if(nports+sc->numports>NDGBPORTS) {
806 kprintf("dgb%d: only %d ports are usable\n", unit, NDGBPORTS-nports);
807 sc->numports=NDGBPORTS-nports;
810 /* allocate port and tty structures */
811 sc->ports=&dgb_ports[nports];
812 sc->ttys=&dgb_tty[nports];
813 nports+=sc->numports;
815 addr=setwin(sc,PORTBASE);
816 pstat=(const ushort volatile *)(mem+addr);
818 for(i=0; i<sc->numports && pstat[i]; i++)
820 sc->ports[i].status=ENABLED;
822 sc->ports[i].status=DISABLED;
823 kprintf("dgb%d: port%d is broken\n", unit, i);
826 /* We should now init per-port structures */
827 bc=(volatile struct board_chan *)(mem + CHANSTRUCT);
828 sc->mailbox=(volatile struct global_data *)(mem + FEP_GLOBAL);
835 for(i=0; i<sc->numports; i++, bc++) {
838 port->tty=&sc->ttys[i];
854 DPRINT2(DB_INFO,"dgb%d: shrinking memory\n",unit);
855 fepcmd(port, SETBUFFER, 32, 0, 0, 0);
859 if(sc->type!=PCXEVE) {
860 port->txptr=mem+((bc->tseg-sc->mem_seg)<<4);
861 port->rxptr=mem+((bc->rseg-sc->mem_seg)<<4);
862 port->txwin=port->rxwin=0;
864 port->txptr=mem+( ((bc->tseg-sc->mem_seg)<<4) & 0x1FFF );
865 port->rxptr=mem+( ((bc->rseg-sc->mem_seg)<<4) & 0x1FFF );
866 port->txwin=FEPWIN | ((bc->tseg-sc->mem_seg)>>9);
867 port->rxwin=FEPWIN | ((bc->rseg-sc->mem_seg)>>9);
872 port->txbufsize=bc->tmax+1;
873 port->rxbufsize=bc->rmax+1;
875 lowwater= (port->txbufsize>=2000) ? 1024 : (port->txbufsize/2);
877 fepcmd(port, STXLWATER, lowwater, 0, 10, 0);
878 fepcmd(port, SRXLWATER, port->rxbufsize/4, 0, 10, 0);
879 fepcmd(port, SRXHWATER, 3*port->rxbufsize/4, 0, 10, 0);
884 port->startc=bc->startc;
885 port->startca=bc->startca;
886 port->stopc=bc->stopc;
887 port->stopca=bc->stopca;
889 /*port->close_delay=50;*/
890 port->close_delay=3 * hz;
891 port->do_timestamp=0;
892 port->do_dcd_timestamp=0;
895 * We don't use all the flags from <sys/ttydefaults.h> since they
896 * are only relevant for logins. It's important to have echo off
897 * initially so that the line doesn't start blathering before the
898 * echo flag can be turned off.
900 port->it_in.c_iflag = TTYDEF_IFLAG;
901 port->it_in.c_oflag = TTYDEF_OFLAG;
902 port->it_in.c_cflag = TTYDEF_CFLAG;
903 port->it_in.c_lflag = TTYDEF_LFLAG;
904 termioschars(&port->it_in);
905 port->it_in.c_ispeed = port->it_in.c_ospeed = dgbdefaultrate;
906 port->it_out = port->it_in;
907 /* MAX_DGB_PORTS is 32 => [0-9a-v] */
908 suffix = i < 10 ? '0' + i : 'a' + i - 10;
909 cdevsw_add(&dgb_cdevsw, 0xffff0000, unit << 16);
910 make_dev(&dgb_cdevsw, (unit<<16)+i,
911 UID_ROOT, GID_WHEEL, 0600, "ttyD%d%c", unit, suffix);
913 make_dev(&dgb_cdevsw, (unit<<16)+i+32,
914 UID_ROOT, GID_WHEEL, 0600, "ttyiD%d%c", unit, suffix);
916 make_dev(&dgb_cdevsw, (unit<<16)+i+64,
917 UID_ROOT, GID_WHEEL, 0600, "ttylD%d%c", unit, suffix);
919 make_dev(&dgb_cdevsw, (unit<<16)+i+128,
920 UID_UUCP, GID_DIALER, 0660, "cuaD%d%c", unit, suffix);
922 make_dev(&dgb_cdevsw, (unit<<16)+i+160,
923 UID_UUCP, GID_DIALER, 0660, "cuaiD%d%c", unit, suffix);
925 make_dev(&dgb_cdevsw, (unit<<16)+i+192,
926 UID_UUCP, GID_DIALER, 0660, "cualD%d%c", unit, suffix);
931 /* register the polling function */
932 callout_reset(&sc->dgbpoll, hz / POLLSPERSEC, dgbpoll, (void *)unit);
939 dgbopen(cdev_t dev, int flag, int mode, struct thread *td)
941 struct dgb_softc *sc;
948 volatile struct board_chan *bc;
953 unit=MINOR_TO_UNIT(mynor);
954 pnum=MINOR_TO_PORT(mynor);
957 DPRINT2(DB_EXCEPT,"dgb%d: try to open a nonexisting card\n",unit);
963 if(sc->status!=ENABLED) {
964 DPRINT2(DB_EXCEPT,"dgb%d: try to open a disabled card\n",unit);
968 if(pnum>=sc->numports) {
969 DPRINT3(DB_EXCEPT,"dgb%d: try to open non-existing port %d\n",unit,pnum);
973 if(mynor & CONTROL_MASK)
978 port=&sc->ports[pnum];
985 while(port->closing) {
986 error=tsleep(&port->closing, PCATCH, "dgocl", 0);
989 DPRINT4(DB_OPEN,"dgb%d: port%d: tsleep(dgocl) error=%d\n",unit,pnum,error);
994 if (tp->t_state & TS_ISOPEN) {
996 * The device is open, so everything has been initialized.
999 if (mynor & CALLOUT_MASK) {
1000 if (!port->active_out) {
1002 DPRINT4(DB_OPEN,"dgb%d: port%d: BUSY error=%d\n",unit,pnum,error);
1006 if (port->active_out) {
1007 if (flag & O_NONBLOCK) {
1009 DPRINT4(DB_OPEN,"dgb%d: port%d: BUSY error=%d\n",unit,pnum,error);
1012 error = tsleep(&port->active_out,
1015 DPRINT4(DB_OPEN,"dgb%d: port%d: tsleep(dgbi) error=%d\n",
1023 if (tp->t_state & TS_XCLUDE && suser(td)) {
1029 * The device isn't open, so there are no conflicts.
1030 * Initialize it. Initialization is done twice in many
1031 * cases: to preempt sleeping callin opens if we are
1032 * callout, and to complete a callin open after DCD rises.
1034 tp->t_oproc=dgbstart;
1035 tp->t_param=dgbparam;
1038 tp->t_termios= (mynor & CALLOUT_MASK) ?
1044 port->imodem=bc->mstat;
1045 bc->rout=bc->rin; /* clear input queue */
1047 #ifdef PRINT_BUFSIZE
1048 kprintf("dgb buffers tx=%x:%x rx=%x:%x\n",bc->tseg,bc->tmax,bc->rseg,bc->rmax);
1055 error=dgbparam(tp, &tp->t_termios);
1059 DPRINT4(DB_OPEN,"dgb%d: port%d: dgbparam error=%d\n",unit,pnum,error);
1063 /* handle fake DCD for callout devices */
1064 /* and initial DCD */
1066 if( (port->imodem & port->dcd) || mynor & CALLOUT_MASK )
1067 linesw[tp->t_line].l_modem(tp,1);
1072 * Wait for DCD if necessary.
1074 if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK)
1075 && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) {
1077 error = tsleep(TSA_CARR_ON(tp), PCATCH, "dgdcd", 0);
1080 DPRINT4(DB_OPEN,"dgb%d: port%d: tsleep(dgdcd) error=%d\n",unit,pnum,error);
1086 error = linesw[tp->t_line].l_open(dev, tp);
1087 disc_optim(tp,&tp->t_termios);
1088 DPRINT4(DB_OPEN,"dgb%d: port%d: l_open error=%d\n",unit,pnum,error);
1090 if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
1091 port->active_out = TRUE;
1095 /* If any port is open (i.e. the open() call is completed for it)
1096 * the device is busy
1100 disc_optim(tp,&tp->t_termios);
1103 if( !(tp->t_state & TS_ISOPEN) && port->wopeners==0 )
1106 DPRINT4(DB_OPEN,"dgb%d: port%d: open() returns %d\n",unit,pnum,error);
1113 dgbclose(cdev_t dev, int flag, int mode, struct thread *td)
1117 struct dgb_softc *sc;
1123 if(mynor & CONTROL_MASK)
1125 unit=MINOR_TO_UNIT(mynor);
1126 pnum=MINOR_TO_PORT(mynor);
1128 sc=&dgb_softc[unit];
1130 port=sc->ports+pnum;
1132 DPRINT3(DB_CLOSE,"dgb%d: port%d: closing\n",unit,pnum);
1134 DPRINT3(DB_CLOSE,"dgb%d: port%d: draining port\n",unit,pnum);
1135 dgb_drain_or_flush(port);
1140 DPRINT3(DB_CLOSE,"dgb%d: port%d: closing line disc\n",unit,pnum);
1141 linesw[tp->t_line].l_close(tp,flag);
1142 disc_optim(tp,&tp->t_termios);
1144 DPRINT3(DB_CLOSE,"dgb%d: port%d: hard closing\n",unit,pnum);
1146 DPRINT3(DB_CLOSE,"dgb%d: port%d: closing tty\n",unit,pnum);
1149 wakeup(&port->closing);
1152 /* mark the card idle when all ports are closed */
1154 for(i=0; i<sc->numports; i++)
1155 if(sc->ports[i].used)
1160 DPRINT3(DB_CLOSE,"dgb%d: port%d: closed\n",unit,pnum);
1162 wakeup(TSA_CARR_ON(tp));
1163 wakeup(&port->active_out);
1166 DPRINT3(DB_CLOSE,"dgb%d: port%d: close exit\n",unit,pnum);
1175 struct dgb_softc *sc=&dgb_softc[port->unit];
1176 volatile struct board_chan *bc=port->brdchan;
1179 port->do_timestamp = 0;
1182 bc->idata=0; bc->iempty=0; bc->ilow=0;
1183 if(port->tty->t_cflag & HUPCL) {
1184 port->omodem &= ~(RTS|DTR);
1185 fepcmd(port, SETMODEM, 0, DTR|RTS, 0, 1);
1191 callout_reset(&sc->dgb_pause, hz / 2, dgb_pause, &port->brdchan);
1192 tsleep(&port->brdchan, PCATCH, "dgclo", 0);
1199 wakeup((caddr_t)chan);
1206 int unit=(int)unit_c;
1209 struct dgb_softc *sc=&dgb_softc[unit];
1212 int event, mstat, lstat;
1213 volatile struct board_chan *bc;
1220 int ibuf_full,obuf_full;
1222 BoardMemWinState ws=bmws_get();
1224 if(sc->status==DISABLED) {
1225 kprintf("dgb%d: polling of disabled board stopped\n",unit);
1231 head=sc->mailbox->ein;
1232 tail=sc->mailbox->eout;
1235 if(head >= FEP_IMAX-FEP_ISTART
1236 || tail >= FEP_IMAX-FEP_ISTART
1237 || (head|tail) & 03 ) {
1238 kprintf("dgb%d: event queue's head or tail is wrong! hd=%d,tl=%d\n", unit,head,tail);
1242 eventbuf=sc->vmem+tail+FEP_ISTART;
1248 port=&sc->ports[pnum];
1252 if(pnum>=sc->numports || port->status==DISABLED) {
1253 kprintf("dgb%d: port%d: got event on nonexisting port\n",unit,pnum);
1254 } else if(port->used || port->wopeners>0 ) {
1256 int wrapmask=port->rxbufsize-1;
1258 if( !(event & ALL_IND) )
1259 kprintf("dgb%d: port%d: ? event 0x%x mstat 0x%x lstat 0x%x\n",
1260 unit, pnum, event, mstat, lstat);
1262 if(event & DATA_IND) {
1263 DPRINT3(DB_DATA,"dgb%d: port%d: DATA_IND\n",unit,pnum);
1265 rhead=bc->rin & wrapmask;
1266 rtail=bc->rout & wrapmask;
1268 if( !(tp->t_cflag & CREAD) || !port->used ) {
1274 kprintf("dgb%d: port%d: overrun\n", unit, pnum);
1278 if(!(tp->t_state & TS_ISOPEN))
1281 for(ibuf_full=FALSE;rhead!=rtail && !ibuf_full;) {
1282 DPRINT5(DB_RXDATA,"dgb%d: port%d: p rx head=%d tail=%d\n",
1283 unit,pnum,rhead,rtail);
1288 size=port->rxbufsize-rtail;
1290 ptr=__DEVOLATILE(u_char *, port->rxptr+rtail);
1293 if( tp->t_rawq.c_cc + size > DGB_IBUFSIZE ) {
1294 size=DGB_IBUFSIZE-tp->t_rawq.c_cc;
1295 DPRINT1(DB_RXDATA,"*");
1300 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1301 DPRINT1(DB_RXDATA,"!");
1302 towin(sc,port->rxwin);
1305 tp->t_rawcc += size;
1306 b_to_q(ptr,size,&tp->t_rawq);
1312 towin(sc,port->rxwin);
1315 (*linesw[tp->t_line].l_rint)(chr, tp);
1320 rtail= (rtail + size) & wrapmask;
1322 rhead=bc->rin & wrapmask;
1330 if(event & MODEMCHG_IND) {
1331 DPRINT3(DB_MODEM,"dgb%d: port%d: MODEMCHG_IND\n",unit,pnum);
1333 if(mstat & port->dcd) {
1335 linesw[tp->t_line].l_modem(tp,1);
1337 wakeup(TSA_CARR_ON(tp));
1340 linesw[tp->t_line].l_modem(tp,0);
1342 if( port->draining) {
1344 wakeup(&port->draining);
1349 if(event & BREAK_IND) {
1350 if((tp->t_state & TS_ISOPEN) && (tp->t_iflag & IGNBRK)) {
1351 DPRINT3(DB_BREAK,"dgb%d: port%d: BREAK_IND\n",unit,pnum);
1353 linesw[tp->t_line].l_rint(TTY_BI, tp);
1358 /* Helg: with output flow control */
1360 if(event & (LOWTX_IND | EMPTYTX_IND) ) {
1361 DPRINT3(DB_TXDATA,"dgb%d: port%d: LOWTX_IND or EMPTYTX_IND\n",unit,pnum);
1363 if( (event & EMPTYTX_IND ) && tp->t_outq.c_cc==0
1364 && port->draining) {
1366 wakeup(&port->draining);
1367 bc->ilow=0; bc->iempty=0;
1370 int wrapmask=port->txbufsize-1;
1372 for(obuf_full=FALSE; tp->t_outq.c_cc!=0 && !obuf_full; ) {
1373 /* add "last-minute" data to write buffer */
1374 if(!(tp->t_state & TS_BUSY)) {
1376 #ifndef TS_ASLEEP /* post 2.0.5 FreeBSD */
1379 if(tp->t_outq.c_cc <= tp->t_lowat) {
1380 if(tp->t_state & TS_ASLEEP) {
1381 tp->t_state &= ~TS_ASLEEP;
1382 wakeup(TSA_OLOWAT(tp));
1384 /* selwakeup(&tp->t_wsel); */
1391 whead=bc->tin & wrapmask;
1392 wtail=bc->tout & wrapmask;
1397 size=port->txbufsize-whead;
1403 DPRINT5(DB_WR,"dgb: head=%d tail=%d size=%d full=%d\n",
1404 whead,wtail,size,obuf_full);
1405 bc->iempty=1; bc->ilow=1;
1411 towin(sc,port->txwin);
1413 ocount=q_to_b(&tp->t_outq, __DEVOLATILE(u_char *, port->txptr+whead), size);
1418 bc->tin=whead & wrapmask;
1423 DPRINT1(DB_WR," +BUSY\n");
1424 tp->t_state|=TS_BUSY;
1426 DPRINT1(DB_WR," -BUSY\n");
1428 #ifndef TS_ASLEEP /* post 2.0.5 FreeBSD */
1429 /* should clear TS_BUSY before ttwwakeup */
1430 if(tp->t_state & TS_BUSY) {
1431 tp->t_state &= ~TS_BUSY;
1432 linesw[tp->t_line].l_start(tp);
1436 if(tp->t_state & TS_ASLEEP) {
1437 tp->t_state &= ~TS_ASLEEP;
1438 wakeup(TSA_OLOWAT(tp));
1440 tp->t_state &= ~TS_BUSY;
1446 bc->idata=1; /* require event on incoming data */
1450 DPRINT4(DB_EXCEPT,"dgb%d: port%d: got event 0x%x on closed port\n",
1453 bc->idata=bc->iempty=bc->ilow=0;
1456 tail= (tail+4) & (FEP_IMAX-FEP_ISTART-4);
1459 sc->mailbox->eout=tail;
1462 callout_reset(&sc->dgbpoll, hz / POLLSPERSEC, dgbpoll, unit_c);
1466 dgbioctl(cdev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1468 struct dgb_softc *sc;
1473 volatile struct board_chan *bc;
1477 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1479 struct termios term;
1482 BoardMemWinState ws=bmws_get();
1485 unit=MINOR_TO_UNIT(mynor);
1486 pnum=MINOR_TO_PORT(mynor);
1488 sc=&dgb_softc[unit];
1489 port=&sc->ports[pnum];
1493 if (mynor & CONTROL_MASK) {
1496 switch (mynor & CONTROL_MASK) {
1497 case CONTROL_INIT_STATE:
1498 ct = mynor & CALLOUT_MASK ? &port->it_out : &port->it_in;
1500 case CONTROL_LOCK_STATE:
1501 ct = mynor & CALLOUT_MASK ? &port->lt_out : &port->lt_in;
1504 return (ENODEV); /* /dev/nodev */
1511 *ct = *(struct termios *)data;
1514 *(struct termios *)data = *ct;
1517 *(int *)data = TTYDISC;
1520 bzero(data, sizeof(struct winsize));
1527 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1528 term = tp->t_termios;
1529 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1530 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);
1533 error = ttsetcompat(tp, &cmd, data, &term);
1537 data = (caddr_t)&term;
1540 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1542 struct termios *dt = (struct termios *)data;
1543 struct termios *lt = mynor & CALLOUT_MASK
1544 ? &port->lt_out : &port->lt_in;
1546 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);
1547 dt->c_iflag = (tp->t_iflag & lt->c_iflag)
1548 | (dt->c_iflag & ~lt->c_iflag);
1549 dt->c_oflag = (tp->t_oflag & lt->c_oflag)
1550 | (dt->c_oflag & ~lt->c_oflag);
1551 dt->c_cflag = (tp->t_cflag & lt->c_cflag)
1552 | (dt->c_cflag & ~lt->c_cflag);
1553 dt->c_lflag = (tp->t_lflag & lt->c_lflag)
1554 | (dt->c_lflag & ~lt->c_lflag);
1555 for (cc = 0; cc < NCCS; ++cc)
1556 if (lt->c_cc[cc] != 0)
1557 dt->c_cc[cc] = tp->t_cc[cc];
1558 if (lt->c_ispeed != 0)
1559 dt->c_ispeed = tp->t_ispeed;
1560 if (lt->c_ospeed != 0)
1561 dt->c_ospeed = tp->t_ospeed;
1567 fepcmd(port, PAUSETX, 0, 0, 0, 0);
1571 } else if(cmd==TIOCSTART) {
1574 fepcmd(port, RESUMETX, 0, 0, 0, 0);
1580 if(cmd==TIOCSETAW || cmd==TIOCSETAF)
1583 error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, td);
1584 if (error != ENOIOCTL)
1587 error = ttioctl(tp, cmd, data, flag);
1588 disc_optim(tp,&tp->t_termios);
1590 if (error != ENOIOCTL) {
1592 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
1593 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);
1600 /* Helg: commented */
1601 /* error=dgbdrain(port);*/
1611 /* now it sends 250 millisecond break because I don't know */
1612 /* how to send an infinite break */
1614 fepcmd(port, SENDBREAK, 250, 0, 10, 0);
1619 /* now it's empty */
1622 DPRINT3(DB_MODEM,"dgb%d: port%d: set DTR\n",unit,pnum);
1623 port->omodem |= DTR;
1626 fepcmd(port, SETMODEM, port->omodem, RTS, 0, 1);
1628 if( !(bc->mstat & DTR) ) {
1629 DPRINT3(DB_MODEM,"dgb%d: port%d: DTR is off\n",unit,pnum);
1636 DPRINT3(DB_MODEM,"dgb%d: port%d: reset DTR\n",unit,pnum);
1637 port->omodem &= ~DTR;
1640 fepcmd(port, SETMODEM, port->omodem, RTS|DTR, 0, 1);
1642 if( bc->mstat & DTR ) {
1643 DPRINT3(DB_MODEM,"dgb%d: port%d: DTR is on\n",unit,pnum);
1650 if(*(int *)data & TIOCM_DTR)
1653 port->omodem &=~DTR;
1655 if(*(int *)data & TIOCM_RTS)
1658 port->omodem &=~RTS;
1662 fepcmd(port, SETMODEM, port->omodem, RTS|DTR, 0, 1);
1667 if(*(int *)data & TIOCM_DTR)
1670 if(*(int *)data & TIOCM_RTS)
1675 fepcmd(port, SETMODEM, port->omodem, RTS|DTR, 0, 1);
1680 if(*(int *)data & TIOCM_DTR)
1681 port->omodem &=~DTR;
1683 if(*(int *)data & TIOCM_RTS)
1684 port->omodem &=~RTS;
1688 fepcmd(port, SETMODEM, port->omodem, RTS|DTR, 0, 1);
1694 port->imodem=bc->mstat;
1697 tiocm_xxx = TIOCM_LE; /* XXX - always enabled while open */
1699 DPRINT3(DB_MODEM,"dgb%d: port%d: modem stat -- ",unit,pnum);
1701 if (port->imodem & DTR) {
1702 DPRINT1(DB_MODEM,"DTR ");
1703 tiocm_xxx |= TIOCM_DTR;
1705 if (port->imodem & RTS) {
1706 DPRINT1(DB_MODEM,"RTS ");
1707 tiocm_xxx |= TIOCM_RTS;
1709 if (port->imodem & CTS) {
1710 DPRINT1(DB_MODEM,"CTS ");
1711 tiocm_xxx |= TIOCM_CTS;
1713 if (port->imodem & port->dcd) {
1714 DPRINT1(DB_MODEM,"DCD ");
1715 tiocm_xxx |= TIOCM_CD;
1717 if (port->imodem & port->dsr) {
1718 DPRINT1(DB_MODEM,"DSR ");
1719 tiocm_xxx |= TIOCM_DSR;
1721 if (port->imodem & RI) {
1722 DPRINT1(DB_MODEM,"RI ");
1723 tiocm_xxx |= TIOCM_RI;
1725 *(int *)data = tiocm_xxx;
1726 DPRINT1(DB_MODEM,"--\n");
1729 /* must be root since the wait applies to following logins */
1735 port->close_delay = *(int *)data * hz / 100;
1738 *(int *)data = port->close_delay * 100 / hz;
1741 port->do_timestamp = TRUE;
1742 *(struct timeval *)data = port->timestamp;
1744 case TIOCDCDTIMESTAMP:
1745 port->do_dcd_timestamp = TRUE;
1746 *(struct timeval *)data = port->dcd_timestamp;
1763 struct dgb_p *port=p;
1765 wakeup(&port->draining);
1768 /* wait for the output to drain */
1774 struct dgb_softc *sc=&dgb_softc[port->unit];
1775 volatile struct board_chan *bc=port->brdchan;
1779 BoardMemWinState ws=bmws_get();
1788 DPRINT5(DB_WR,"dgb%d: port%d: drain: head=%d tail=%d\n",
1789 port->unit, port->pnum, head, tail);
1793 callout_reset(&sc->wakeflush, hz, wakeflush,port);
1794 error=tsleep(&port->draining, PCATCH, "dgdrn", 0);
1799 DPRINT4(DB_WR,"dgb%d: port%d: tsleep(dgdrn) error=%d\n",
1800 port->unit,port->pnum,error);
1810 DPRINT5(DB_WR,"dgb%d: port%d: drain: head=%d tail=%d\n",
1811 port->unit, port->pnum, head, tail);
1816 /* wait for the output to drain */
1817 /* or simply clear the buffer it it's stopped */
1820 dgb_drain_or_flush(port)
1823 struct tty *tp=port->tty;
1824 struct dgb_softc *sc=&dgb_softc[port->unit];
1825 volatile struct board_chan *bc=port->brdchan;
1837 while(tail!=head /* && tail!=lasttail */ ) {
1838 DPRINT5(DB_WR,"dgb%d: port%d: flush: head=%d tail=%d\n",
1839 port->unit, port->pnum, head, tail);
1841 /* if there is no carrier simply clean the buffer */
1842 if( !(tp->t_state & TS_CARR_ON) ) {
1851 callout_reset(&sc->wakeflush, hz, wakeflush, port);
1852 error=tsleep(&port->draining, PCATCH, "dgfls", 0);
1857 DPRINT4(DB_WR,"dgb%d: port%d: tsleep(dgfls) error=%d\n",
1858 port->unit,port->pnum,error);
1860 /* silently clean the buffer */
1873 DPRINT5(DB_WR,"dgb%d: port%d: flush: head=%d tail=%d\n",
1874 port->unit, port->pnum, head, tail);
1882 int unit=MINOR_TO_UNIT(minor(tp->t_dev));
1883 int pnum=MINOR_TO_PORT(minor(tp->t_dev));
1884 struct dgb_softc *sc=&dgb_softc[unit];
1885 struct dgb_p *port=&sc->ports[pnum];
1886 volatile struct board_chan *bc=port->brdchan;
1893 BoardMemWinState ws=bmws_get();
1895 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);
1897 if(port->mustdrain) {
1898 DPRINT3(DB_PARAM,"dgb%d: port%d: must call dgbdrain()\n",unit,pnum);
1902 cflag=ttspeedtab(t->c_ospeed, dgbspeedtab);
1904 if (t->c_ispeed == 0)
1905 t->c_ispeed = t->c_ospeed;
1907 if (cflag < 0 /* || cflag > 0 && t->c_ispeed != t->c_ospeed */) {
1908 DPRINT4(DB_PARAM,"dgb%d: port%d: invalid cflag=0%o\n",unit,pnum,cflag);
1915 if(cflag==0) { /* hangup */
1916 DPRINT3(DB_PARAM,"dgb%d: port%d: hangup\n",unit,pnum);
1920 fepcmd(port, STOUT, (unsigned)head, 0, 0, 0);
1921 mval= port->omodem & ~(DTR|RTS);
1923 cflag |= dgbflags(dgb_cflags, t->c_cflag);
1925 if(cflag!=port->fepcflag) {
1926 port->fepcflag=cflag;
1927 DPRINT5(DB_PARAM,"dgb%d: port%d: set cflag=0x%x c=0x%x\n",
1928 unit,pnum,cflag,t->c_cflag&~CRTSCTS);
1929 fepcmd(port, SETCTRLFLAGS, (unsigned)cflag, 0, 0, 0);
1931 mval= port->omodem | (DTR|RTS);
1934 iflag=dgbflags(dgb_iflags, t->c_iflag);
1935 if(iflag!=port->fepiflag) {
1936 port->fepiflag=iflag;
1937 DPRINT5(DB_PARAM,"dgb%d: port%d: set iflag=0x%x c=0x%x\n",unit,pnum,iflag,t->c_iflag);
1938 fepcmd(port, SETIFLAGS, (unsigned)iflag, 0, 0, 0);
1943 hflow=dgbflags(dgb_flow, t->c_cflag);
1944 if(hflow!=port->hflow) {
1946 DPRINT5(DB_PARAM,"dgb%d: port%d: set hflow=0x%x f=0x%x\n",unit,pnum,hflow,t->c_cflag&CRTSCTS);
1947 fepcmd(port, SETHFLOW, (unsigned)hflow, 0xff, 0, 1);
1950 if(port->omodem != mval) {
1951 DPRINT5(DB_PARAM,"dgb%d: port%d: setting modem parameters 0x%x was 0x%x\n",
1952 unit,pnum,mval,port->omodem);
1954 fepcmd(port, SETMODEM, (unsigned)mval, RTS|DTR, 0, 1);
1957 if(port->fepstartc!=t->c_cc[VSTART] || port->fepstopc!=t->c_cc[VSTOP]) {
1958 DPRINT5(DB_PARAM,"dgb%d: port%d: set startc=%d, stopc=%d\n",unit,pnum,t->c_cc[VSTART],t->c_cc[VSTOP]);
1959 port->fepstartc=t->c_cc[VSTART];
1960 port->fepstopc=t->c_cc[VSTOP];
1961 fepcmd(port, SONOFFC, port->fepstartc, port->fepstopc, 0, 1);
1978 struct dgb_softc *sc;
1979 volatile struct board_chan *bc;
1984 BoardMemWinState ws=bmws_get();
1986 unit=MINOR_TO_UNIT(minor(tp->t_dev));
1987 pnum=MINOR_TO_PORT(minor(tp->t_dev));
1988 sc=&dgb_softc[unit];
1989 port=&sc->ports[pnum];
1992 wmask=port->txbufsize-1;
1996 while( tp->t_outq.c_cc!=0 ) {
1998 #ifndef TS_ASLEEP /* post 2.0.5 FreeBSD */
2001 if(tp->t_outq.c_cc <= tp->t_lowat) {
2002 if(tp->t_state & TS_ASLEEP) {
2003 tp->t_state &= ~TS_ASLEEP;
2004 wakeup(TSA_OLOWAT(tp));
2006 /*selwakeup(&tp->t_wsel);*/
2012 head=bc->tin & wmask;
2014 do { tail=bc->tout; } while (tail != bc->tout);
2015 tail=bc->tout & wmask;
2017 DPRINT5(DB_WR,"dgb%d: port%d: s tx head=%d tail=%d\n",unit,pnum,head,tail);
2019 #ifdef LEAVE_FREE_CHARS
2021 size=tail-head-LEAVE_FREE_CHARS;
2025 size=port->txbufsize-head;
2026 if(tail+port->txbufsize < head)
2034 size=port->txbufsize-head/*-1*/;
2041 bc->iempty=1; bc->ilow=1;
2044 tp->t_state|=TS_BUSY;
2049 towin(sc,port->txwin);
2051 ocount=q_to_b(&tp->t_outq, __DEVOLATILE(u_char *, port->txptr+head), size);
2053 if(head>=port->txbufsize)
2054 head-=port->txbufsize;
2059 DPRINT5(DB_WR,"dgb%d: port%d: tx avail=%d count=%d\n",unit,pnum,size,ocount);
2067 #ifndef TS_ASLEEP /* post 2.0.5 FreeBSD */
2068 if(tp->t_state & TS_BUSY) {
2069 tp->t_state&=~TS_BUSY;
2070 linesw[tp->t_line].l_start(tp);
2074 if(tp->t_state & TS_ASLEEP) {
2075 tp->t_state &= ~TS_ASLEEP;
2076 wakeup(TSA_OLOWAT(tp));
2078 tp->t_state&=~TS_BUSY;
2090 struct dgb_softc *sc;
2091 volatile struct board_chan *bc;
2093 BoardMemWinState ws=bmws_get();
2095 unit=MINOR_TO_UNIT(minor(tp->t_dev));
2096 pnum=MINOR_TO_PORT(minor(tp->t_dev));
2098 sc=&dgb_softc[unit];
2099 port=&sc->ports[pnum];
2102 DPRINT3(DB_WR,"dgb%d: port%d: stop\n",port->unit, port->pnum);
2108 /* clear output queue */
2110 bc->ilow=0;bc->iempty=0;
2113 /* clear input queue */
2124 fepcmd(port, cmd, op1, op2, ncmds, bytecmd)
2126 unsigned cmd, op1, op2, ncmds, bytecmd;
2128 struct dgb_softc *sc=&dgb_softc[port->unit];
2129 u_char *mem=sc->vmem;
2130 unsigned tail, head;
2133 if(port->status==DISABLED) {
2134 kprintf("dgb%d: port%d: FEP command on disabled port\n",
2135 port->unit, port->pnum);
2139 /* setwin(sc,0); Require this to be set by caller */
2140 head=sc->mailbox->cin;
2142 if(head>=(FEP_CMAX-FEP_CSTART) || (head & 3)) {
2143 kprintf("dgb%d: port%d: wrong pointer head of command queue : 0x%x\n",
2144 port->unit, port->pnum, head);
2148 mem[head+FEP_CSTART+0]=cmd;
2149 mem[head+FEP_CSTART+1]=port->pnum;
2151 mem[head+FEP_CSTART+2]=op1;
2152 mem[head+FEP_CSTART+3]=op2;
2154 mem[head+FEP_CSTART+2]=op1&0xff;
2155 mem[head+FEP_CSTART+3]=(op1>>8)&0xff;
2158 DPRINT7(DB_FEP,"dgb%d: port%d: %s cmd=0x%x op1=0x%x op2=0x%x\n", port->unit, port->pnum,
2159 (bytecmd)?"byte":"word", cmd, mem[head+FEP_CSTART+2], mem[head+FEP_CSTART+3]);
2161 head=(head+4) & (FEP_CMAX-FEP_CSTART-4);
2162 sc->mailbox->cin=head;
2166 while (count-- != 0) {
2167 head=sc->mailbox->cin;
2168 tail=sc->mailbox->cout;
2170 n = (head-tail) & (FEP_CMAX-FEP_CSTART-4);
2171 if(n <= ncmds * (sizeof(ushort)*4))
2174 kprintf("dgb%d(%d): timeout on FEP cmd=0x%x\n", port->unit, port->pnum, cmd);
2182 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
2183 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
2184 && (!(t->c_iflag & PARMRK)
2185 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
2186 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
2187 && linesw[tp->t_line].l_rint == ttyinput)
2188 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2190 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;