1*1934Swnj /* dh.c 4.5 12/19/80 */ 213Sbill 3*1934Swnj #include "dh.h" 41561Sbill #if NDH11 > 0 513Sbill /* 61561Sbill * DH-11 driver 71561Sbill * 81561Sbill * Loaded with dhdm if there are DM-11's otherwise with dhfdm. 91561Sbill * 101561Sbill * NB: WE HAVEN'T TESTED dhdm CODE ON VAX. 1113Sbill */ 1213Sbill 1313Sbill #include "../h/param.h" 1413Sbill #include "../h/conf.h" 1513Sbill #include "../h/dir.h" 1613Sbill #include "../h/user.h" 1713Sbill #include "../h/tty.h" 1813Sbill #include "../h/map.h" 1913Sbill #include "../h/pte.h" 2013Sbill #include "../h/uba.h" 21113Sbill #include "../h/bk.h" 221561Sbill #include "../h/clist.h" 231786Sbill #include "../h/mx.h" 2413Sbill 25144Sbill /* 26144Sbill * When running dz's using only SAE (silo alarm) on input 27144Sbill * it is necessary to call dzrint() at clock interrupt time. 28144Sbill * This is unsafe unless spl5()s in tty code are changed to 29144Sbill * spl6()s to block clock interrupts. Note that the dh driver 30144Sbill * currently in use works the same way as the dz, even though 31144Sbill * we could try to more intelligently manage its silo. 32144Sbill * Thus don't take this out if you have no dz's unless you 33144Sbill * change clock.c and dhtimer(). 34144Sbill */ 35144Sbill #define spl5 spl6 36144Sbill 371561Sbill #define UBACVT(x) (cbase + (short)((x)-(char *)cfree)) 3813Sbill 3913Sbill struct tty dh11[NDH11]; 40117Sbill int dhact; 41280Sbill int dhisilo; 4213Sbill int ndh11 = NDH11; 4313Sbill int dhstart(); 4413Sbill int ttrstrt(); 45280Sbill int dh_ubinfo; 46280Sbill int cbase; 47280Sbill int getcbase; 4813Sbill 4913Sbill /* 5013Sbill * Hardware control bits 5113Sbill */ 5213Sbill #define BITS6 01 5313Sbill #define BITS7 02 5413Sbill #define BITS8 03 5513Sbill #define TWOSB 04 5613Sbill #define PENABLE 020 5713Sbill /* DEC manuals incorrectly say this bit causes generation of even parity. */ 5813Sbill #define OPAR 040 5913Sbill #define HDUPLX 040000 6013Sbill 6113Sbill #define IENAB 030100 62105Sbill #define NXM 02000 63105Sbill #define CLRNXM 0400 6413Sbill #define PERROR 010000 6513Sbill #define FRERROR 020000 6613Sbill #define OVERRUN 040000 6713Sbill #define XINT 0100000 6813Sbill #define SSPEED 7 /* standard speed: 300 baud */ 6913Sbill 7013Sbill /* 7113Sbill * DM control bits 7213Sbill */ 7313Sbill #define TURNON 03 /* CD lead + line enable */ 7413Sbill #define TURNOFF 01 /* line enable */ 75168Sbill #define DTR 02 /* data terminal ready */ 7613Sbill #define RQS 04 /* request to send */ 7713Sbill 7813Sbill /* 7913Sbill * Software copy of last dhbar 8013Sbill */ 8113Sbill short dhsar[(NDH11+15)/16]; 8213Sbill 8313Sbill struct device 8413Sbill { 8513Sbill union { 8613Sbill short dhcsr; 8713Sbill char dhcsrl; 8813Sbill } un; 8913Sbill short dhnxch; 9013Sbill short dhlpr; 9113Sbill unsigned short dhcar; 9213Sbill short dhbcr; 9313Sbill unsigned short dhbar; 9413Sbill short dhbreak; 9513Sbill short dhsilo; 9613Sbill }; 9713Sbill 9813Sbill /* 9913Sbill * Open a DH11 line. 10013Sbill */ 10113Sbill /*ARGSUSED*/ 10213Sbill dhopen(dev, flag) 10313Sbill { 10413Sbill register struct tty *tp; 10513Sbill register d; 10613Sbill register struct device *addr; 10713Sbill int s; 10813Sbill 10913Sbill d = minor(dev) & 0177; 11013Sbill if (d >= NDH11) { 11113Sbill u.u_error = ENXIO; 11213Sbill return; 11313Sbill } 11413Sbill tp = &dh11[d]; 11513Sbill addr = DHADDR; 11613Sbill addr += d>>4; 11713Sbill tp->t_addr = (caddr_t)addr; 11813Sbill tp->t_oproc = dhstart; 11913Sbill tp->t_iproc = NULL; 12013Sbill tp->t_state |= WOPEN; 12113Sbill s = spl6(); 122117Sbill if (!getcbase) { 123117Sbill getcbase++; 124717Sbill /* 512+ is a kludge to try to get around a hardware problem */ 125717Sbill dh_ubinfo = uballoc((caddr_t)cfree, 512+NCLIST*sizeof(struct cblock), 0); 126280Sbill cbase = (short)dh_ubinfo; 12713Sbill } 12813Sbill splx(s); 12913Sbill addr->un.dhcsr |= IENAB; 130117Sbill dhact |= (1<<(d>>4)); 13113Sbill if ((tp->t_state&ISOPEN) == 0) { 13213Sbill ttychars(tp); 133168Sbill if (tp->t_ispeed == 0) { 134168Sbill tp->t_ispeed = SSPEED; 135168Sbill tp->t_ospeed = SSPEED; 136168Sbill tp->t_flags = ODDP|EVENP|ECHO; 137168Sbill } 13813Sbill dhparam(d); 13913Sbill } 14013Sbill if (tp->t_state&XCLUDE && u.u_uid!=0) { 14113Sbill u.u_error = EBUSY; 14213Sbill return; 14313Sbill } 14413Sbill dmopen(dev); 14513Sbill (*linesw[tp->t_line].l_open)(dev,tp); 14613Sbill } 14713Sbill 14813Sbill /* 14913Sbill * Close a DH11 line. 15013Sbill */ 15113Sbill /*ARGSUSED*/ 15213Sbill dhclose(dev, flag) 15313Sbill dev_t dev; 15413Sbill int flag; 15513Sbill { 15613Sbill register struct tty *tp; 15713Sbill register d; 15813Sbill 15913Sbill d = minor(dev) & 0177; 16013Sbill tp = &dh11[d]; 16113Sbill (*linesw[tp->t_line].l_close)(tp); 16213Sbill if (tp->t_state&HUPCLS || (tp->t_state&ISOPEN)==0) 163168Sbill dmctl(d, TURNOFF, DMSET); 16413Sbill ttyclose(tp); 16513Sbill } 16613Sbill 16713Sbill /* 16813Sbill * Read from a DH11 line. 16913Sbill */ 17013Sbill dhread(dev) 17113Sbill { 17213Sbill register struct tty *tp; 17313Sbill 17413Sbill tp = &dh11[minor(dev) & 0177]; 17513Sbill (*linesw[tp->t_line].l_read)(tp); 17613Sbill } 17713Sbill 17813Sbill /* 17913Sbill * write on a DH11 line 18013Sbill */ 18113Sbill dhwrite(dev) 18213Sbill { 18313Sbill register struct tty *tp; 18413Sbill 18513Sbill tp = &dh11[minor(dev) & 0177]; 18613Sbill (*linesw[tp->t_line].l_write)(tp); 18713Sbill } 18813Sbill 18913Sbill /* 19013Sbill * DH11 receiver interrupt. 19113Sbill */ 19213Sbill dhrint(dev) 19313Sbill { 19413Sbill register struct tty *tp; 19513Sbill register short c; 19613Sbill register struct device *addr; 197117Sbill register struct tty *tp0; 198139Sbill int s; 19913Sbill 200139Sbill s = spl6(); /* see comment in clock.c */ 20113Sbill addr = DHADDR; 20213Sbill addr += minor(dev) & 0177; 203117Sbill tp0 = &dh11[((minor(dev)&0177)<<4)]; 20413Sbill while ((c = addr->dhnxch) < 0) { /* char. present */ 205117Sbill tp = tp0 + ((c>>8)&017); 20613Sbill if (tp >= &dh11[NDH11]) 20713Sbill continue; 20813Sbill if((tp->t_state&ISOPEN)==0) { 20913Sbill wakeup((caddr_t)tp); 21013Sbill continue; 21113Sbill } 21213Sbill if (c&PERROR) 21313Sbill if ((tp->t_flags&(EVENP|ODDP))==EVENP 21413Sbill || (tp->t_flags&(EVENP|ODDP))==ODDP ) 21513Sbill continue; 21613Sbill if (c&OVERRUN) 21713Sbill printf("O"); 21813Sbill if (c&FRERROR) /* break */ 21913Sbill if (tp->t_flags&RAW) 22013Sbill c = 0; /* null (for getty) */ 22113Sbill else 222168Sbill #ifdef IIASA 223168Sbill continue; 224168Sbill #else 225184Sbill c = tun.t_intrc; 226168Sbill #endif 227139Sbill if (tp->t_line == NETLDISC) { 228117Sbill c &= 0177; 229168Sbill BKINPUT(c, tp); 230117Sbill } else 231117Sbill (*linesw[tp->t_line].l_rint)(c,tp); 23213Sbill } 233139Sbill splx(s); 23413Sbill } 23513Sbill 23613Sbill /* 23713Sbill * stty/gtty for DH11 23813Sbill */ 23913Sbill /*ARGSUSED*/ 24013Sbill dhioctl(dev, cmd, addr, flag) 24113Sbill caddr_t addr; 24213Sbill { 24313Sbill register struct tty *tp; 24413Sbill 24513Sbill tp = &dh11[minor(dev) & 0177]; 246113Sbill cmd = (*linesw[tp->t_line].l_ioctl)(tp, cmd, addr); 247113Sbill if (cmd==0) 248113Sbill return; 2491895Swnj if (ttioctl(tp, cmd, addr, flag)) { 25013Sbill if (cmd==TIOCSETP||cmd==TIOCSETN) 25113Sbill dhparam(dev); 252168Sbill } else switch(cmd) { 253168Sbill case TIOCSBRK: 254168Sbill ((struct device *)(tp->t_addr))->dhbreak |= 1<<(minor(dev)&017); 255168Sbill break; 256168Sbill case TIOCCBRK: 257168Sbill ((struct device *)(tp->t_addr))->dhbreak &= ~(1<<(minor(dev)&017)); 258168Sbill break; 259168Sbill case TIOCSDTR: 260168Sbill dmctl(minor(dev), DTR|RQS, DMBIS); 261168Sbill break; 262168Sbill case TIOCCDTR: 263168Sbill dmctl(minor(dev), DTR|RQS, DMBIC); 264168Sbill break; 265168Sbill default: 26613Sbill u.u_error = ENOTTY; 267168Sbill } 26813Sbill } 26913Sbill 27013Sbill /* 27113Sbill * Set parameters from open or stty into the DH hardware 27213Sbill * registers. 27313Sbill */ 27413Sbill dhparam(dev) 27513Sbill { 27613Sbill register struct tty *tp; 27713Sbill register struct device *addr; 27813Sbill register d; 279300Sbill int s; 28013Sbill 28113Sbill d = minor(dev) & 0177; 28213Sbill tp = &dh11[d]; 28313Sbill addr = (struct device *)tp->t_addr; 284300Sbill s = spl5(); 28513Sbill addr->un.dhcsrl = (d&017) | IENAB; 28613Sbill /* 28713Sbill * Hang up line? 28813Sbill */ 28913Sbill if ((tp->t_ispeed)==0) { 29013Sbill tp->t_state |= HUPCLS; 291168Sbill dmctl(d, TURNOFF, DMSET); 29213Sbill return; 29313Sbill } 29413Sbill d = ((tp->t_ospeed)<<10) | ((tp->t_ispeed)<<6); 29513Sbill if ((tp->t_ispeed) == 4) /* 134.5 baud */ 29613Sbill d |= BITS6|PENABLE|HDUPLX; 29713Sbill else if (tp->t_flags&RAW) 29813Sbill d |= BITS8; 29913Sbill else 30013Sbill d |= BITS7|PENABLE; 30113Sbill if ((tp->t_flags&EVENP) == 0) 30213Sbill d |= OPAR; 30313Sbill if ((tp->t_ospeed) == 3) /* 110 baud */ 30413Sbill d |= TWOSB; 30513Sbill addr->dhlpr = d; 306300Sbill splx(s); 30713Sbill } 30813Sbill 30913Sbill /* 31013Sbill * DH11 transmitter interrupt. 31113Sbill * Restart each line which used to be active but has 31213Sbill * terminated transmission since the last interrupt. 31313Sbill */ 31413Sbill dhxint(dev) 31513Sbill { 31613Sbill register struct tty *tp; 31713Sbill register struct device *addr; 31813Sbill register d; 31913Sbill short ttybit, bar, *sbar; 320144Sbill int s; 32113Sbill 322144Sbill s = spl6(); /* block the clock */ 32313Sbill d = minor(dev) & 0177; 32413Sbill addr = DHADDR + d; 32513Sbill addr->un.dhcsr &= (short)~XINT; 326105Sbill if (addr->un.dhcsr & NXM) { 327105Sbill addr->un.dhcsr |= CLRNXM; 328105Sbill printf("dh clr NXM\n"); 329105Sbill } 33013Sbill sbar = &dhsar[d]; 33113Sbill bar = *sbar & ~addr->dhbar; 33213Sbill d <<= 4; ttybit = 1; 33313Sbill 33413Sbill for(; bar; d++, ttybit <<= 1) { 33513Sbill if(bar&ttybit) { 33613Sbill *sbar &= ~ttybit; 33713Sbill bar &= ~ttybit; 33813Sbill tp = &dh11[d]; 339113Sbill tp->t_state &= ~BUSY; 340113Sbill if (tp->t_state&FLUSH) 341113Sbill tp->t_state &= ~FLUSH; 342113Sbill else { 343113Sbill addr->un.dhcsrl = (d&017)|IENAB; 344219Sbill ndflush(&tp->t_outq, 3451569Sbill (int)(short)addr->dhcar-UBACVT(tp->t_outq.c_cf)); 346113Sbill } 347113Sbill if (tp->t_line) 34813Sbill (*linesw[tp->t_line].l_start)(tp); 349113Sbill else 35013Sbill dhstart(tp); 35113Sbill } 35213Sbill } 353144Sbill splx(s); 35413Sbill } 35513Sbill 35613Sbill /* 35713Sbill * Start (restart) transmission on the given DH11 line. 35813Sbill */ 35913Sbill dhstart(tp) 36013Sbill register struct tty *tp; 36113Sbill { 36213Sbill register struct device *addr; 36313Sbill register short nch; 36413Sbill int s, d; 36513Sbill 36613Sbill /* 36713Sbill * If it's currently active, or delaying, 36813Sbill * no need to do anything. 36913Sbill */ 37013Sbill s = spl5(); 37113Sbill d = tp-dh11; 37213Sbill addr = (struct device *)tp->t_addr; 37313Sbill if (tp->t_state&(TIMEOUT|BUSY|TTSTOP)) 37413Sbill goto out; 37513Sbill 37613Sbill /* 37713Sbill * If the writer was sleeping on output overflow, 37813Sbill * wake him when low tide is reached. 37913Sbill */ 380921Sbill if (tp->t_state&ASLEEP && tp->t_outq.c_cc<=TTLOWAT(tp)) { 38113Sbill tp->t_state &= ~ASLEEP; 38213Sbill if (tp->t_chan) 383168Sbill mcstart(tp->t_chan, (caddr_t)&tp->t_outq); 384168Sbill else 38513Sbill wakeup((caddr_t)&tp->t_outq); 38613Sbill } 38713Sbill 38813Sbill if (tp->t_outq.c_cc == 0) 38913Sbill goto out; 39013Sbill 39113Sbill /* 39213Sbill * Find number of characters to transfer. 39313Sbill */ 39413Sbill if (tp->t_flags & RAW) { 39513Sbill nch = ndqb(&tp->t_outq, 0); 39613Sbill } else { 39713Sbill nch = ndqb(&tp->t_outq, 0200); 39813Sbill if (nch == 0) { 39913Sbill nch = getc(&tp->t_outq); 40013Sbill timeout(ttrstrt, (caddr_t)tp, (nch&0177)+6); 40113Sbill tp->t_state |= TIMEOUT; 40213Sbill goto out; 40313Sbill } 40413Sbill } 40513Sbill /* 40613Sbill * If any characters were set up, start transmission; 40713Sbill */ 40813Sbill if (nch) { 40913Sbill addr->un.dhcsrl = (d&017)|IENAB; 41013Sbill addr->dhcar = UBACVT(tp->t_outq.c_cf); 41113Sbill addr->dhbcr = -nch; 41213Sbill nch = 1<<(d&017); 41313Sbill addr->dhbar |= nch; 41413Sbill dhsar[d>>4] |= nch; 41513Sbill tp->t_state |= BUSY; 41613Sbill } 41713Sbill out: 41813Sbill splx(s); 41913Sbill } 42013Sbill 42113Sbill /* 42213Sbill * Stop output on a line. 42313Sbill * Assume call is made at spl6. 42413Sbill */ 42513Sbill /*ARGSUSED*/ 42613Sbill dhstop(tp, flag) 42713Sbill register struct tty *tp; 42813Sbill { 429113Sbill register struct device *addr; 430113Sbill register d, s; 43113Sbill 432113Sbill addr = (struct device *)tp->t_addr; 43313Sbill s = spl6(); 434113Sbill if (tp->t_state & BUSY) { 435113Sbill d = minor(tp->t_dev); 436113Sbill addr->un.dhcsrl = (d&017) | IENAB; 43713Sbill if ((tp->t_state&TTSTOP)==0) 43813Sbill tp->t_state |= FLUSH; 439113Sbill addr->dhbcr = -1; 440113Sbill } 44113Sbill splx(s); 44213Sbill } 44313Sbill 444117Sbill int dhsilo = 16; 445168Sbill /* 446168Sbill * Silo control is fixed strategy 447168Sbill * here, paralleling only option available 448168Sbill * on DZ-11. 449168Sbill */ 45013Sbill /*ARGSUSED*/ 451168Sbill dhtimer() 45213Sbill { 453168Sbill register d; 454117Sbill register struct device *addr; 455117Sbill 45613Sbill addr = DHADDR; d = 0; 45713Sbill do { 458117Sbill if (dhact & (1<<d)) { 459280Sbill if ((dhisilo & (1<<d)) == 0) { 460280Sbill addr->dhsilo = dhsilo; 461280Sbill dhisilo |= 1<<d; 462280Sbill } 463117Sbill dhrint(d); 464117Sbill } 465117Sbill d++; 46613Sbill addr++; 46713Sbill } while (d < (NDH11+15)/16); 46813Sbill } 469280Sbill 470280Sbill /* 471280Sbill * Reset state of driver if UBA reset was necessary. 472280Sbill * Reset the csrl and lpr registers on open lines, and 473280Sbill * restart transmitters. 474280Sbill */ 475280Sbill dhreset() 476280Sbill { 477280Sbill int d; 478280Sbill register struct tty *tp; 479280Sbill register struct device *addr; 480280Sbill 481280Sbill if (getcbase == 0) 482280Sbill return; 483280Sbill printf(" dh"); 484280Sbill dhisilo = 0; 485280Sbill ubafree(dh_ubinfo); 486280Sbill dh_ubinfo = uballoc((caddr_t)cfree, NCLIST*sizeof (struct cblock), 0); 487280Sbill cbase = (short)dh_ubinfo; 488280Sbill d = 0; 489280Sbill do { 490280Sbill addr = DHADDR + d; 491280Sbill if (dhact & (1<<d)) 492300Sbill addr->un.dhcsr |= IENAB; 493280Sbill d++; 494280Sbill } while (d < (NDH11+15)/16); 495300Sbill for (d = 0; d < NDH11; d++) { 496300Sbill tp = &dh11[d]; 497300Sbill if (tp->t_state & (ISOPEN|WOPEN)) { 498300Sbill dhparam(d); 499300Sbill dmctl(d, TURNON, DMSET); 500300Sbill tp->t_state &= ~BUSY; 501300Sbill dhstart(tp); 502300Sbill } 503300Sbill } 504300Sbill dhtimer(); 505280Sbill } 5061808Sbill #endif 507