if_cx.c revision 151700
1139749Simp/*- 2123120Simp * Cronyx-Sigma adapter driver for FreeBSD. 3123120Simp * Supports PPP/HDLC and Cisco/HDLC protocol in synchronous mode, 4123120Simp * and asyncronous channels with full modem control. 5123120Simp * Keepalive protocol implemented in both Cisco and PPP modes. 6123120Simp * 7123120Simp * Copyright (C) 1994-2002 Cronyx Engineering. 8123120Simp * Author: Serge Vakulenko, <vak@cronyx.ru> 9123120Simp * 10125813Srik * Copyright (C) 1999-2004 Cronyx Engineering. 11123120Simp * Rewritten on DDK, ported to NETGRAPH, rewritten for FreeBSD 3.x-5.x by 12123120Simp * Kurakin Roman, <rik@cronyx.ru> 13123120Simp * 14123120Simp * This software is distributed with NO WARRANTIES, not even the implied 15123120Simp * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 16123120Simp * 17123120Simp * Authors grant any other persons or organisations a permission to use, 18123120Simp * modify and redistribute this software in source and binary forms, 19123120Simp * as long as this message is kept with the software, all derivative 20123120Simp * works or modified versions. 21123120Simp * 22130985Srik * Cronyx Id: if_cx.c,v 1.1.2.34 2004/06/23 17:09:13 rik Exp $ 23123120Simp */ 24130985Srik 25123120Simp#include <sys/cdefs.h> 26123120Simp__FBSDID("$FreeBSD: head/sys/dev/cx/if_cx.c 151700 2005-10-26 15:52:16Z jhb $"); 27123120Simp 28123120Simp#include <sys/param.h> 29123120Simp 30123120Simp#include <sys/systm.h> 31123120Simp#include <sys/kernel.h> 32129879Sphk#include <sys/module.h> 33123120Simp#include <sys/proc.h> 34123120Simp#include <sys/mbuf.h> 35123120Simp#include <sys/sockio.h> 36123120Simp#include <sys/malloc.h> 37123120Simp#include <sys/socket.h> 38138823Srik#include <sys/sysctl.h> 39123120Simp#include <sys/conf.h> 40123120Simp#include <sys/errno.h> 41136480Sphk#include <sys/serial.h> 42123120Simp#include <sys/tty.h> 43130971Srik#include <sys/bus.h> 44130971Srik#include <machine/bus.h> 45130971Srik#include <sys/rman.h> 46130971Srik#include <isa/isavar.h> 47123120Simp#include <sys/fcntl.h> 48123120Simp#include <sys/interrupt.h> 49123120Simp#include <vm/vm.h> 50123120Simp#include <vm/pmap.h> 51123120Simp#include <net/if.h> 52123120Simp#include <machine/cpufunc.h> 53123120Simp#include <machine/cserial.h> 54123120Simp#include <machine/clock.h> 55130971Srik#include <machine/resource.h> 56130971Srik#include <dev/cx/machdep.h> 57130971Srik#include <dev/cx/cxddk.h> 58130971Srik#include <dev/cx/cronyxfw.h> 59123120Simp#include "opt_ng_cronyx.h" 60123120Simp#ifdef NETGRAPH_CRONYX 61123120Simp# include "opt_netgraph.h" 62123120Simp# include <netgraph/ng_message.h> 63123120Simp# include <netgraph/netgraph.h> 64130971Srik# include <dev/cx/ng_cx.h> 65123120Simp#else 66123120Simp# include <net/if_types.h> 67123120Simp# include <net/if_sppp.h> 68123120Simp# define PP_CISCO IFF_LINK2 69123120Simp# include <net/bpf.h> 70123120Simp#endif 71123120Simp 72136472Sphk#define NCX 1 73130095Sphk 74129029Srik/* If we don't have Cronyx's sppp version, we don't have fr support via sppp */ 75129029Srik#ifndef PP_FR 76129029Srik#define PP_FR 0 77129029Srik#endif 78129029Srik 79123120Simp#define CX_DEBUG(d,s) ({if (d->chan->debug) {\ 80123120Simp printf ("%s: ", d->name); printf s;}}) 81123120Simp#define CX_DEBUG2(d,s) ({if (d->chan->debug>1) {\ 82123120Simp printf ("%s: ", d->name); printf s;}}) 83123120Simp 84138823Srik#define CX_LOCK_NAME "cxX" 85138823Srik 86138823Srikstatic int cx_mpsafenet = 1; 87138823SrikTUNABLE_INT("debug.cx.mpsafenet", &cx_mpsafenet); 88138823SrikSYSCTL_NODE(_debug, OID_AUTO, cx, CTLFLAG_RD, 0, "Cronyx Sigma Adapters"); 89138823SrikSYSCTL_INT(_debug_cx, OID_AUTO, mpsafenet, CTLFLAG_RD, &cx_mpsafenet, 0, 90138823Srik "Enable/disable MPSAFE network support for Cronyx Sigma Adapters"); 91138823Srik 92138823Srik#define CX_LOCK(_bd) do { \ 93138823Srik if (cx_mpsafenet) \ 94138823Srik mtx_lock (&(_bd)->cx_mtx); \ 95138823Srik } while (0) 96138823Srik#define CX_UNLOCK(_bd) do { \ 97138823Srik if (cx_mpsafenet) \ 98138823Srik mtx_unlock (&(_bd)->cx_mtx); \ 99138823Srik } while (0) 100138823Srik#define CX_LOCK_ASSERT(_bd) do { \ 101138823Srik if (cx_mpsafenet) \ 102138823Srik mtx_assert (&(_bd)->cx_mtx, MA_OWNED); \ 103138823Srik } while (0) 104138823Srik 105123120Simptypedef struct _async_q { 106123120Simp int beg; 107123120Simp int end; 108123120Simp #define BF_SZ 14400 109123120Simp int buf[BF_SZ+1]; 110123120Simp} async_q; 111123120Simp 112123120Simp#define AQ_GSZ(q) ((BF_SZ + (q)->end - (q)->beg)%BF_SZ) 113123120Simp#define AQ_PUSH(q,c) {*((q)->buf + (q)->end) = c;\ 114123120Simp (q)->end = ((q)->end + 1)%BF_SZ;} 115123120Simp#define AQ_POP(q,c) {c = *((q)->buf + (q)->beg);\ 116123120Simp (q)->beg = ((q)->beg + 1)%BF_SZ;} 117123120Simp 118123120Simpstatic void cx_identify __P((driver_t *, device_t)); 119123120Simpstatic int cx_probe __P((device_t)); 120123120Simpstatic int cx_attach __P((device_t)); 121123120Simpstatic int cx_detach __P((device_t)); 122136480Sphkstatic t_open_t cx_topen; 123136480Sphkstatic t_modem_t cx_tmodem; 124136480Sphkstatic t_close_t cx_tclose; 125123120Simp 126123120Simpstatic device_method_t cx_isa_methods [] = { 127123120Simp DEVMETHOD(device_identify, cx_identify), 128123120Simp DEVMETHOD(device_probe, cx_probe), 129123120Simp DEVMETHOD(device_attach, cx_attach), 130123120Simp DEVMETHOD(device_detach, cx_detach), 131123120Simp {0, 0} 132123120Simp}; 133123120Simp 134130985Sriktypedef struct _cx_dma_mem_t { 135130985Srik unsigned long phys; 136130985Srik void *virt; 137130985Srik size_t size; 138130985Srik bus_dma_tag_t dmat; 139130985Srik bus_dmamap_t mapp; 140130985Srik} cx_dma_mem_t; 141123120Simp 142123120Simptypedef struct _drv_t { 143123120Simp char name [8]; 144123120Simp cx_chan_t *chan; 145123120Simp cx_board_t *board; 146130985Srik cx_dma_mem_t dmamem; 147130240Srik struct tty *tty; 148138823Srik struct callout dcd_timeout_handle; 149123120Simp unsigned callout; 150123120Simp unsigned lock; 151123120Simp int open_dev; 152123120Simp int cd; 153123120Simp int running; 154123120Simp#ifdef NETGRAPH 155123120Simp char nodename [NG_NODELEN+1]; 156123120Simp hook_p hook; 157123120Simp hook_p debug_hook; 158123120Simp node_p node; 159123120Simp struct ifqueue lo_queue; 160123120Simp struct ifqueue hi_queue; 161123120Simp short timeout; 162138823Srik struct callout timeout_handle; 163123120Simp#else 164138823Srik struct ifqueue queue; 165147256Sbrooks struct ifnet *ifp; 166123120Simp#endif 167138823Srik struct cdev *devt; 168138823Srik async_q aqueue; 169130971Srik#define CX_READ 1 170130971Srik#define CX_WRITE 2 171123120Simp int intr_action; 172123120Simp short atimeout; 173123120Simp} drv_t; 174123120Simp 175130985Sriktypedef struct _bdrv_t { 176130985Srik cx_board_t *board; 177130985Srik struct resource *base_res; 178130985Srik struct resource *drq_res; 179130985Srik struct resource *irq_res; 180130985Srik int base_rid; 181130985Srik int drq_rid; 182130985Srik int irq_rid; 183130985Srik void *intrhand; 184130985Srik drv_t channel [NCHAN]; 185138823Srik struct mtx cx_mtx; 186130985Srik} bdrv_t; 187130985Srik 188130985Srikstatic driver_t cx_isa_driver = { 189130985Srik "cx", 190130985Srik cx_isa_methods, 191130985Srik sizeof (bdrv_t), 192130985Srik}; 193130985Srik 194130985Srikstatic devclass_t cx_devclass; 195130985Srik 196123120Simpextern long csigma_fw_len; 197123120Simpextern const char *csigma_fw_version; 198123120Simpextern const char *csigma_fw_date; 199123120Simpextern const char *csigma_fw_copyright; 200123120Simpextern const cr_dat_tst_t csigma_fw_tvec[]; 201123120Simpextern const u_char csigma_fw_data[]; 202123120Simpstatic void cx_oproc (struct tty *tp); 203123120Simpstatic int cx_param (struct tty *tp, struct termios *t); 204123120Simpstatic void cx_stop (struct tty *tp, int flag); 205123120Simpstatic void cx_receive (cx_chan_t *c, char *data, int len); 206123120Simpstatic void cx_transmit (cx_chan_t *c, void *attachment, int len); 207123120Simpstatic void cx_error (cx_chan_t *c, int data); 208123120Simpstatic void cx_modem (cx_chan_t *c); 209123120Simpstatic void cx_up (drv_t *d); 210123120Simpstatic void cx_start (drv_t *d); 211123120Simpstatic void cx_softintr (void *); 212123120Simpstatic void *cx_fast_ih; 213123120Simpstatic void cx_down (drv_t *d); 214123120Simpstatic void cx_watchdog (drv_t *d); 215123120Simpstatic void cx_carrier (void *arg); 216123120Simp 217123120Simp#ifdef NETGRAPH 218123120Simpextern struct ng_type typestruct; 219123120Simp#else 220123120Simpstatic void cx_ifstart (struct ifnet *ifp); 221123120Simpstatic void cx_tlf (struct sppp *sp); 222123120Simpstatic void cx_tls (struct sppp *sp); 223123120Simpstatic void cx_ifwatchdog (struct ifnet *ifp); 224123120Simpstatic int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data); 225123120Simpstatic void cx_initialize (void *softc); 226123120Simp#endif 227123120Simp 228123120Simpstatic cx_board_t *adapter [NCX]; 229123120Simpstatic drv_t *channel [NCX*NCHAN]; 230138823Srikstatic struct callout led_timo [NCX]; 231138823Srikstatic struct callout timeout_handle; 232123120Simp 233150622Srikstatic int cx_open (struct cdev *dev, int flag, int mode, struct thread *td); 234150622Srikstatic int cx_close (struct cdev *dev, int flag, int mode, struct thread *td); 235150622Srikstatic int cx_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td); 236149847Sobrienstatic struct cdevsw cx_cdevsw = { 237149847Sobrien .d_version = D_VERSION, 238149847Sobrien .d_open = cx_open, 239149847Sobrien .d_close = cx_close, 240149847Sobrien .d_ioctl = cx_ioctl, 241149847Sobrien .d_name = "cx", 242149847Sobrien .d_flags = D_TTY | D_NEEDGIANT, 243149847Sobrien}; 244149847Sobrien 245150622Srikstatic int MY_SOFT_INTR; 246150622Srik 247123120Simp/* 248123120Simp * Print the mbuf chain, for debug purposes only. 249123120Simp */ 250123120Simpstatic void printmbuf (struct mbuf *m) 251123120Simp{ 252123120Simp printf ("mbuf:"); 253123120Simp for (; m; m=m->m_next) { 254123120Simp if (m->m_flags & M_PKTHDR) 255123120Simp printf (" HDR %d:", m->m_pkthdr.len); 256123120Simp if (m->m_flags & M_EXT) 257123120Simp printf (" EXT:"); 258123120Simp printf (" %d", m->m_len); 259123120Simp } 260123120Simp printf ("\n"); 261123120Simp} 262123120Simp 263123120Simp/* 264123120Simp * Make an mbuf from data. 265123120Simp */ 266123120Simpstatic struct mbuf *makembuf (void *buf, u_int len) 267123120Simp{ 268123120Simp struct mbuf *m, *o, *p; 269123120Simp 270123120Simp MGETHDR (m, M_DONTWAIT, MT_DATA); 271123120Simp 272123120Simp if (! m) 273123120Simp return 0; 274123120Simp 275123120Simp if (len >= MINCLSIZE) 276123120Simp MCLGET (m, M_DONTWAIT); 277123120Simp 278123120Simp m->m_pkthdr.len = len; 279123120Simp m->m_len = 0; 280123120Simp 281123120Simp p = m; 282123120Simp while (len) { 283123120Simp u_int n = M_TRAILINGSPACE (p); 284123120Simp if (n > len) 285123120Simp n = len; 286123120Simp if (! n) { 287123120Simp /* Allocate new mbuf. */ 288123120Simp o = p; 289123120Simp MGET (p, M_DONTWAIT, MT_DATA); 290123120Simp if (! p) { 291123120Simp m_freem (m); 292123120Simp return 0; 293123120Simp } 294123120Simp if (len >= MINCLSIZE) 295123120Simp MCLGET (p, M_DONTWAIT); 296123120Simp p->m_len = 0; 297123120Simp o->m_next = p; 298123120Simp 299123120Simp n = M_TRAILINGSPACE (p); 300123120Simp if (n > len) 301123120Simp n = len; 302123120Simp } 303123120Simp bcopy (buf, mtod (p, caddr_t) + p->m_len, n); 304123120Simp 305123120Simp p->m_len += n; 306123120Simp buf = n + (char*) buf; 307123120Simp len -= n; 308123120Simp } 309123120Simp return m; 310123120Simp} 311123120Simp 312123120Simp/* 313123120Simp * Recover after lost transmit interrupts. 314123120Simp */ 315123120Simpstatic void cx_timeout (void *arg) 316123120Simp{ 317123120Simp drv_t *d; 318138823Srik int s, i, k; 319123120Simp 320138823Srik for (i = 0; i < NCX; i++) { 321138823Srik if (adapter[i] == NULL) 322123120Simp continue; 323138823Srik for (k = 0; k < NCHAN; ++k) { 324138823Srik d = channel[i * NCHAN + k]; 325138823Srik if (! d) 326138823Srik continue; 327138823Srik s = splhigh (); 328138823Srik CX_LOCK ((bdrv_t *)d->board->sys); 329138823Srik if (d->atimeout == 1 && d->tty && d->tty->t_state & TS_BUSY) { 330138823Srik d->tty->t_state &= ~TS_BUSY; 331138823Srik if (d->tty->t_dev) { 332138823Srik d->intr_action |= CX_WRITE; 333138823Srik MY_SOFT_INTR = 1; 334138823Srik swi_sched (cx_fast_ih, 0); 335138823Srik } 336138823Srik CX_DEBUG (d, ("cx_timeout\n")); 337123120Simp } 338138823Srik if (d->atimeout) 339138823Srik d->atimeout--; 340138823Srik CX_UNLOCK ((bdrv_t *)d->board->sys); 341138823Srik splx (s); 342123120Simp } 343123120Simp } 344138823Srik callout_reset (&timeout_handle, hz*5, cx_timeout, 0); 345123120Simp} 346123120Simp 347123120Simpstatic void cx_led_off (void *arg) 348123120Simp{ 349123120Simp cx_board_t *b = arg; 350138823Srik bdrv_t *bd = b->sys; 351138823Srik int s; 352123120Simp 353138823Srik s = splhigh (); 354138823Srik CX_LOCK (bd); 355123120Simp cx_led (b, 0); 356138823Srik CX_UNLOCK (bd); 357123120Simp splx (s); 358123120Simp} 359123120Simp 360123120Simp/* 361123120Simp * Activate interupt handler from DDK. 362123120Simp */ 363123120Simpstatic void cx_intr (void *arg) 364123120Simp{ 365123120Simp bdrv_t *bd = arg; 366123120Simp cx_board_t *b = bd->board; 367138823Srik#ifndef NETGRAPH 368138823Srik int i; 369138823Srik#endif 370123120Simp int s = splhigh (); 371123120Simp 372138823Srik CX_LOCK (bd); 373123120Simp /* Turn LED on. */ 374123120Simp cx_led (b, 1); 375123120Simp 376123120Simp cx_int_handler (b); 377123120Simp 378123120Simp /* Turn LED off 50 msec later. */ 379138823Srik callout_reset (&led_timo[b->num], hz/20, cx_led_off, b); 380138823Srik CX_UNLOCK (bd); 381123120Simp splx (s); 382138823Srik 383138823Srik#ifndef NETGRAPH 384138823Srik /* Pass packets in a lock-free state */ 385138823Srik for (i = 0; i < NCHAN && b->chan[i].type; i++) { 386138823Srik drv_t *d = b->chan[i].sys; 387138823Srik struct mbuf *m; 388147858Srik if (!d || !d->running) 389147858Srik continue; 390138823Srik while (_IF_QLEN(&d->queue)) { 391138823Srik IF_DEQUEUE (&d->queue,m); 392138823Srik if (!m) 393138823Srik continue; 394147256Sbrooks sppp_input (d->ifp, m); 395138823Srik } 396138823Srik } 397138823Srik#endif 398123120Simp} 399123120Simp 400123120Simpstatic int probe_irq (cx_board_t *b, int irq) 401123120Simp{ 402123120Simp int mask, busy, cnt; 403123120Simp 404123120Simp /* Clear pending irq, if any. */ 405123120Simp cx_probe_irq (b, -irq); 406123120Simp DELAY (100); 407123120Simp for (cnt=0; cnt<5; ++cnt) { 408123120Simp /* Get the mask of pending irqs, assuming they are busy. 409123120Simp * Activate the adapter on given irq. */ 410123120Simp busy = cx_probe_irq (b, irq); 411123120Simp DELAY (100); 412123120Simp 413123120Simp /* Get the mask of active irqs. 414123120Simp * Deactivate our irq. */ 415123120Simp mask = cx_probe_irq (b, -irq); 416123120Simp DELAY (100); 417123120Simp if ((mask & ~busy) == 1 << irq) { 418123120Simp cx_probe_irq (b, 0); 419123120Simp /* printf ("cx%d: irq %d ok, mask=0x%04x, busy=0x%04x\n", 420123120Simp b->num, irq, mask, busy); */ 421123120Simp return 1; 422123120Simp } 423123120Simp } 424123120Simp /* printf ("cx%d: irq %d not functional, mask=0x%04x, busy=0x%04x\n", 425123120Simp b->num, irq, mask, busy); */ 426123120Simp cx_probe_irq (b, 0); 427123120Simp return 0; 428123120Simp} 429123120Simp 430123120Simpstatic short porttab [] = { 431123120Simp 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0, 432123120Simp 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0 433123120Simp}; 434123120Simpstatic char dmatab [] = { 7, 6, 5, 0 }; 435123120Simpstatic char irqtab [] = { 5, 10, 11, 7, 3, 15, 12, 0 }; 436123120Simp 437123120Simpstatic int cx_is_free_res (device_t dev, int rid, int type, u_long start, 438123120Simp u_long end, u_long count) 439123120Simp{ 440123120Simp struct resource *res; 441123120Simp 442123120Simp if (!(res = bus_alloc_resource (dev, type, &rid, start, end, count, 443123120Simp RF_ALLOCATED))) 444123120Simp return 0; 445123120Simp 446123120Simp bus_release_resource (dev, type, rid, res); 447123120Simp 448123120Simp return 1; 449123120Simp} 450123120Simp 451123120Simpstatic void cx_identify (driver_t *driver, device_t dev) 452123120Simp{ 453123120Simp u_long iobase, rescount; 454123120Simp int devcount; 455123120Simp device_t *devices; 456123120Simp device_t child; 457123120Simp devclass_t my_devclass; 458123120Simp int i, k; 459123120Simp 460123120Simp if ((my_devclass = devclass_find ("cx")) == NULL) 461123120Simp return; 462123120Simp 463123120Simp devclass_get_devices (my_devclass, &devices, &devcount); 464123120Simp 465123120Simp if (devcount == 0) { 466123120Simp /* We should find all devices by our self. We could alter other 467123120Simp * devices, but we don't have a choise 468123120Simp */ 469123120Simp for (i = 0; (iobase = porttab [i]) != 0; i++) { 470133647Srik if (!cx_is_free_res (dev, 0, SYS_RES_IOPORT, 471123120Simp iobase, iobase + NPORT, NPORT)) 472123120Simp continue; 473123120Simp if (cx_probe_board (iobase, -1, -1) == 0) 474123120Simp continue; 475123120Simp 476123120Simp devcount++; 477123120Simp 478123120Simp child = BUS_ADD_CHILD (dev, ISA_ORDER_SPECULATIVE, "cx", 479123120Simp -1); 480123120Simp 481123120Simp if (child == NULL) 482123120Simp return; 483123120Simp 484123120Simp device_set_desc_copy (child, "Cronyx Sigma"); 485123120Simp device_set_driver (child, driver); 486123120Simp bus_set_resource (child, SYS_RES_IOPORT, 0, 487123120Simp iobase, NPORT); 488123120Simp 489123120Simp if (devcount >= NCX) 490123120Simp break; 491123120Simp } 492123120Simp } else { 493123120Simp static short porttab [] = { 494123120Simp 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0, 495123120Simp 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0 496123120Simp }; 497123120Simp /* Lets check user choise. 498123120Simp */ 499123120Simp for (k = 0; k < devcount; k++) { 500123120Simp if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0, 501123120Simp &iobase, &rescount) != 0) 502123120Simp continue; 503123120Simp 504123120Simp for (i = 0; porttab [i] != 0; i++) { 505123120Simp if (porttab [i] != iobase) 506123120Simp continue; 507133647Srik if (!cx_is_free_res (devices[k], 0, SYS_RES_IOPORT, 508123120Simp iobase, iobase + NPORT, NPORT)) 509123120Simp continue; 510123120Simp if (cx_probe_board (iobase, -1, -1) == 0) 511123120Simp continue; 512123120Simp porttab [i] = -1; 513123120Simp device_set_desc_copy (devices[k], "Cronyx Sigma"); 514123120Simp break; 515123120Simp } 516123120Simp 517123120Simp if (porttab [i] == 0) { 518123120Simp device_delete_child ( 519123120Simp device_get_parent (devices[k]), 520123120Simp devices [k]); 521123120Simp devices[k] = 0; 522123120Simp continue; 523123120Simp } 524123120Simp } 525123120Simp for (k = 0; k < devcount; k++) { 526123120Simp if (devices[k] == 0) 527123120Simp continue; 528123120Simp if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0, 529123120Simp &iobase, &rescount) == 0) 530123120Simp continue; 531123120Simp for (i = 0; (iobase = porttab [i]) != 0; i++) { 532123120Simp if (porttab [i] == -1) { 533123120Simp continue; 534123120Simp } 535133647Srik if (!cx_is_free_res (devices[k], 0, SYS_RES_IOPORT, 536123120Simp iobase, iobase + NPORT, NPORT)) 537123120Simp continue; 538123120Simp if (cx_probe_board (iobase, -1, -1) == 0) 539123120Simp continue; 540123120Simp 541123120Simp bus_set_resource (devices[k], SYS_RES_IOPORT, 0, 542123120Simp iobase, NPORT); 543123120Simp porttab [i] = -1; 544123120Simp device_set_desc_copy (devices[k], "Cronyx Sigma"); 545123120Simp break; 546123120Simp } 547123120Simp if (porttab [i] == 0) { 548123120Simp device_delete_child ( 549123120Simp device_get_parent (devices[k]), 550123120Simp devices [k]); 551123120Simp } 552123120Simp } 553123120Simp free (devices, M_TEMP); 554123120Simp } 555123120Simp 556123120Simp return; 557123120Simp} 558123120Simp 559123120Simpstatic int cx_probe (device_t dev) 560123120Simp{ 561123120Simp int unit = device_get_unit (dev); 562123120Simp int i; 563123120Simp u_long iobase, rescount; 564123120Simp 565123120Simp if (!device_get_desc (dev) || 566123120Simp strcmp (device_get_desc (dev), "Cronyx Sigma")) 567123120Simp return ENXIO; 568123120Simp 569123120Simp if (bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount) != 0) { 570123120Simp printf ("cx%d: Couldn't get IOPORT\n", unit); 571123120Simp return ENXIO; 572123120Simp } 573123120Simp 574133647Srik if (!cx_is_free_res (dev, 0, SYS_RES_IOPORT, 575123120Simp iobase, iobase + NPORT, NPORT)) { 576123120Simp printf ("cx%d: Resource IOPORT isn't free %lx\n", unit, iobase); 577123120Simp return ENXIO; 578123120Simp } 579123120Simp 580123120Simp for (i = 0; porttab [i] != 0; i++) { 581123120Simp if (porttab [i] == iobase) { 582123120Simp porttab [i] = -1; 583123120Simp break; 584123120Simp } 585123120Simp } 586123120Simp 587123120Simp if (porttab [i] == 0) { 588123120Simp return ENXIO; 589123120Simp } 590123120Simp 591123120Simp if (!cx_probe_board (iobase, -1, -1)) { 592123120Simp printf ("cx%d: probing for Sigma at %lx faild\n", unit, iobase); 593123120Simp return ENXIO; 594123120Simp } 595123120Simp 596123120Simp return 0; 597123120Simp} 598123120Simp 599130985Srikstatic void 600130985Srikcx_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error) 601130985Srik{ 602130985Srik unsigned long *addr; 603130985Srik 604130985Srik if (error) 605130985Srik return; 606130985Srik 607130985Srik KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 608130985Srik addr = arg; 609130985Srik *addr = segs->ds_addr; 610130985Srik} 611130985Srik 612130985Srikstatic int 613130985Srikcx_bus_dma_mem_alloc (int bnum, int cnum, cx_dma_mem_t *dmem) 614130985Srik{ 615130985Srik int error; 616130985Srik 617130985Srik error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_24BIT, 618130985Srik BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1, 619130985Srik dmem->size, 0, NULL, NULL, &dmem->dmat); 620130985Srik if (error) { 621130985Srik if (cnum >= 0) printf ("cx%d-%d: ", bnum, cnum); 622130985Srik else printf ("cx%d: ", bnum); 623130985Srik printf ("couldn't allocate tag for dma memory\n"); 624130985Srik return 0; 625130985Srik } 626130985Srik error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt, 627130985Srik BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp); 628130985Srik if (error) { 629130985Srik if (cnum >= 0) printf ("cx%d-%d: ", bnum, cnum); 630130985Srik else printf ("cx%d: ", bnum); 631130985Srik printf ("couldn't allocate mem for dma memory\n"); 632130985Srik bus_dma_tag_destroy (dmem->dmat); 633130985Srik return 0; 634130985Srik } 635130985Srik error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt, 636130985Srik dmem->size, cx_bus_dmamap_addr, &dmem->phys, 0); 637130985Srik if (error) { 638130985Srik if (cnum >= 0) printf ("cx%d-%d: ", bnum, cnum); 639130985Srik else printf ("cx%d: ", bnum); 640130985Srik printf ("couldn't load mem map for dma memory\n"); 641130985Srik bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp); 642130985Srik bus_dma_tag_destroy (dmem->dmat); 643130985Srik return 0; 644130985Srik } 645130985Srik return 1; 646130985Srik} 647130985Srik 648130985Srikstatic void 649130985Srikcx_bus_dma_mem_free (cx_dma_mem_t *dmem) 650130985Srik{ 651130985Srik bus_dmamap_unload (dmem->dmat, dmem->mapp); 652130985Srik bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp); 653130985Srik bus_dma_tag_destroy (dmem->dmat); 654130985Srik} 655130985Srik 656123120Simp/* 657123120Simp * The adapter is present, initialize the driver structures. 658123120Simp */ 659123120Simpstatic int cx_attach (device_t dev) 660123120Simp{ 661123120Simp bdrv_t *bd = device_get_softc (dev); 662123120Simp u_long iobase, drq, irq, rescount; 663123120Simp int unit = device_get_unit (dev); 664138823Srik char *cx_ln = CX_LOCK_NAME; 665123120Simp cx_board_t *b; 666123120Simp cx_chan_t *c; 667123120Simp drv_t *d; 668130971Srik int i; 669130971Srik int s; 670123120Simp 671123120Simp KASSERT ((bd != NULL), ("cx%d: NULL device softc\n", unit)); 672123120Simp 673123120Simp bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount); 674123120Simp bd->base_rid = 0; 675123120Simp bd->base_res = bus_alloc_resource (dev, SYS_RES_IOPORT, &bd->base_rid, 676123120Simp iobase, iobase + NPORT, NPORT, RF_ACTIVE); 677123120Simp if (! bd->base_res) { 678123120Simp printf ("cx%d: cannot allocate base address\n", unit); 679123120Simp return ENXIO; 680123120Simp } 681123120Simp 682123120Simp if (bus_get_resource (dev, SYS_RES_DRQ, 0, &drq, &rescount) != 0) { 683123120Simp for (i = 0; (drq = dmatab [i]) != 0; i++) { 684133647Srik if (!cx_is_free_res (dev, 0, SYS_RES_DRQ, 685123120Simp drq, drq + 1, 1)) 686123120Simp continue; 687123120Simp bus_set_resource (dev, SYS_RES_DRQ, 0, drq, 1); 688123120Simp break; 689123120Simp } 690123120Simp 691123120Simp if (dmatab[i] == 0) { 692123120Simp bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, 693123120Simp bd->base_res); 694123120Simp printf ("cx%d: Couldn't get DRQ\n", unit); 695123120Simp return ENXIO; 696123120Simp } 697123120Simp } 698123120Simp 699123120Simp bd->drq_rid = 0; 700123120Simp bd->drq_res = bus_alloc_resource (dev, SYS_RES_DRQ, &bd->drq_rid, 701123120Simp drq, drq + 1, 1, RF_ACTIVE); 702123120Simp if (! bd->drq_res) { 703123120Simp printf ("cx%d: cannot allocate drq\n", unit); 704123120Simp bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, 705123120Simp bd->base_res); 706123120Simp return ENXIO; 707123120Simp } 708123120Simp 709123120Simp if (bus_get_resource (dev, SYS_RES_IRQ, 0, &irq, &rescount) != 0) { 710123120Simp for (i = 0; (irq = irqtab [i]) != 0; i++) { 711133647Srik if (!cx_is_free_res (dev, 0, SYS_RES_IRQ, 712123120Simp irq, irq + 1, 1)) 713123120Simp continue; 714123120Simp bus_set_resource (dev, SYS_RES_IRQ, 0, irq, 1); 715123120Simp break; 716123120Simp } 717123120Simp 718123120Simp if (irqtab[i] == 0) { 719123120Simp bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, 720123120Simp bd->drq_res); 721123120Simp bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, 722123120Simp bd->base_res); 723123120Simp printf ("cx%d: Couldn't get IRQ\n", unit); 724123120Simp return ENXIO; 725123120Simp } 726123120Simp } 727123120Simp 728123120Simp bd->irq_rid = 0; 729123120Simp bd->irq_res = bus_alloc_resource (dev, SYS_RES_IRQ, &bd->irq_rid, 730123120Simp irq, irq + 1, 1, RF_ACTIVE); 731123120Simp if (! bd->irq_res) { 732123120Simp printf ("cx%d: Couldn't allocate irq\n", unit); 733123120Simp bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, 734123120Simp bd->drq_res); 735123120Simp bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, 736123120Simp bd->base_res); 737123120Simp return ENXIO; 738123120Simp } 739123120Simp 740123120Simp b = malloc (sizeof (cx_board_t), M_DEVBUF, M_WAITOK); 741123120Simp if (!b) { 742123120Simp printf ("cx:%d: Couldn't allocate memory\n", unit); 743123120Simp return (ENXIO); 744123120Simp } 745123120Simp adapter[unit] = b; 746123120Simp bzero (b, sizeof(cx_board_t)); 747123120Simp 748123120Simp if (! cx_open_board (b, unit, iobase, irq, drq)) { 749123120Simp printf ("cx%d: error loading firmware\n", unit); 750123120Simp free (b, M_DEVBUF); 751123120Simp bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, 752123120Simp bd->irq_res); 753123120Simp bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, 754123120Simp bd->drq_res); 755123120Simp bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, 756123120Simp bd->base_res); 757123120Simp return ENXIO; 758123120Simp } 759123120Simp 760123120Simp bd->board = b; 761123120Simp 762138823Srik cx_ln[2] = '0' + unit; 763138823Srik mtx_init (&bd->cx_mtx, cx_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE); 764123120Simp if (! probe_irq (b, irq)) { 765123120Simp printf ("cx%d: irq %ld not functional\n", unit, irq); 766123120Simp bd->board = 0; 767123120Simp adapter [unit] = 0; 768138823Srik mtx_destroy (&bd->cx_mtx); 769123120Simp free (b, M_DEVBUF); 770123120Simp bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, 771123120Simp bd->irq_res); 772123120Simp bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, 773123120Simp bd->drq_res); 774123120Simp bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, 775123120Simp bd->base_res); 776123120Simp return ENXIO; 777123120Simp } 778138823Srik b->sys = bd; 779138823Srik callout_init (&led_timo[b->num], cx_mpsafenet ? CALLOUT_MPSAFE : 0); 780123120Simp s = splhigh (); 781138823Srik if (bus_setup_intr (dev, bd->irq_res, 782138823Srik INTR_TYPE_NET|(cx_mpsafenet?INTR_MPSAFE:0), 783138823Srik cx_intr, bd, &bd->intrhand)) { 784123120Simp printf ("cx%d: Can't setup irq %ld\n", unit, irq); 785123120Simp bd->board = 0; 786138823Srik b->sys = 0; 787123120Simp adapter [unit] = 0; 788138823Srik mtx_destroy (&bd->cx_mtx); 789123120Simp free (b, M_DEVBUF); 790123120Simp bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, 791123120Simp bd->irq_res); 792123120Simp bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, 793123120Simp bd->drq_res); 794123120Simp bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, 795123120Simp bd->base_res); 796123120Simp splx (s); 797123120Simp return ENXIO; 798123120Simp } 799123120Simp 800138823Srik CX_LOCK (bd); 801123120Simp cx_init (b, b->num, b->port, irq, drq); 802123120Simp cx_setup_board (b, 0, 0, 0); 803138823Srik CX_UNLOCK (bd); 804123120Simp 805123120Simp printf ("cx%d: <Cronyx-Sigma-%s>\n", b->num, b->name); 806123120Simp 807123120Simp for (c=b->chan; c<b->chan+NCHAN; ++c) { 808123120Simp if (c->type == T_NONE) 809123120Simp continue; 810130985Srik d = &bd->channel[c->num]; 811130985Srik d->dmamem.size = sizeof(cx_buf_t); 812130985Srik if (! cx_bus_dma_mem_alloc (unit, c->num, &d->dmamem)) 813130985Srik continue; 814123120Simp d->board = b; 815123120Simp d->chan = c; 816123120Simp d->open_dev = 0; 817123120Simp c->sys = d; 818138823Srik channel [b->num*NCHAN + c->num] = d; 819138823Srik sprintf (d->name, "cx%d.%d", b->num, c->num); 820123120Simp 821123120Simp switch (c->type) { 822123120Simp case T_SYNC_RS232: 823123120Simp case T_SYNC_V35: 824123120Simp case T_SYNC_RS449: 825123120Simp case T_UNIV: 826123120Simp case T_UNIV_RS232: 827123120Simp case T_UNIV_RS449: 828123120Simp case T_UNIV_V35: 829123120Simp#ifdef NETGRAPH 830123120Simp if (ng_make_node_common (&typestruct, &d->node) != 0) { 831123120Simp printf ("%s: cannot make common node\n", d->name); 832123120Simp channel [b->num*NCHAN + c->num] = 0; 833123120Simp c->sys = 0; 834130985Srik cx_bus_dma_mem_free (&d->dmamem); 835123120Simp continue; 836123120Simp } 837123120Simp NG_NODE_SET_PRIVATE (d->node, d); 838123120Simp sprintf (d->nodename, "%s%d", NG_CX_NODE_TYPE, 839123120Simp c->board->num*NCHAN + c->num); 840123120Simp if (ng_name_node (d->node, d->nodename)) { 841123120Simp printf ("%s: cannot name node\n", d->nodename); 842123120Simp NG_NODE_UNREF (d->node); 843123120Simp channel [b->num*NCHAN + c->num] = 0; 844123120Simp c->sys = 0; 845130985Srik cx_bus_dma_mem_free (&d->dmamem); 846123120Simp continue; 847123120Simp } 848123120Simp d->lo_queue.ifq_maxlen = IFQ_MAXLEN; 849123120Simp d->hi_queue.ifq_maxlen = IFQ_MAXLEN; 850123120Simp mtx_init (&d->lo_queue.ifq_mtx, "cx_queue_lo", NULL, MTX_DEF); 851123120Simp mtx_init (&d->hi_queue.ifq_mtx, "cx_queue_hi", NULL, MTX_DEF); 852138823Srik callout_init (&d->timeout_handle, 853138823Srik cx_mpsafenet ? CALLOUT_MPSAFE : 0); 854123120Simp#else /*NETGRAPH*/ 855147256Sbrooks d->ifp = if_alloc(IFT_PPP); 856147256Sbrooks if (d->ifp == NULL) { 857147256Sbrooks printf ("%s: cannot if_alloc() common interface\n", 858147256Sbrooks d->name); 859147256Sbrooks channel [b->num*NCHAN + c->num] = 0; 860147256Sbrooks c->sys = 0; 861147256Sbrooks cx_bus_dma_mem_free (&d->dmamem); 862147256Sbrooks continue; 863147256Sbrooks } 864147856Srik d->ifp->if_softc = d; 865147256Sbrooks if_initname (d->ifp, "cx", b->num * NCHAN + c->num); 866147856Srik d->ifp->if_mtu = PP_MTU; 867147861Srik d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 868147861Srik if (!cx_mpsafenet) 869147861Srik d->ifp->if_flags |= IFF_NEEDSGIANT; 870147256Sbrooks d->ifp->if_ioctl = cx_sioctl; 871147256Sbrooks d->ifp->if_start = cx_ifstart; 872147256Sbrooks d->ifp->if_watchdog = cx_ifwatchdog; 873147856Srik d->ifp->if_init = cx_initialize; 874138823Srik d->queue.ifq_maxlen = 2; 875138823Srik mtx_init (&d->queue.ifq_mtx, "cx_queue", NULL, MTX_DEF); 876147256Sbrooks sppp_attach (d->ifp); 877147256Sbrooks if_attach (d->ifp); 878147856Srik IFP2SP(d->ifp)->pp_tlf = cx_tlf; 879147856Srik IFP2SP(d->ifp)->pp_tls = cx_tls; 880123120Simp /* If BPF is in the kernel, call the attach for it. 881123120Simp * Size of PPP header is 4 bytes. */ 882147256Sbrooks bpfattach (d->ifp, DLT_PPP, 4); 883123120Simp#endif /*NETGRAPH*/ 884123120Simp } 885136480Sphk d->tty = ttyalloc (); 886147856Srik d->tty->t_open = cx_topen; 887147856Srik d->tty->t_close = cx_tclose; 888147856Srik d->tty->t_param = cx_param; 889147856Srik d->tty->t_stop = cx_stop; 890147856Srik d->tty->t_modem = cx_tmodem; 891147860Srik d->tty->t_oproc = cx_oproc; 892147856Srik d->tty->t_sc = d; 893138823Srik CX_LOCK (bd); 894130985Srik cx_start_chan (c, d->dmamem.virt, d->dmamem.phys); 895123120Simp cx_register_receive (c, &cx_receive); 896123120Simp cx_register_transmit (c, &cx_transmit); 897123120Simp cx_register_error (c, &cx_error); 898123120Simp cx_register_modem (c, &cx_modem); 899138823Srik CX_UNLOCK (bd); 900136480Sphk 901151383Sphk ttycreate(d->tty, TS_CALLOUT, "x%r%r", b->num, c->num); 902136480Sphk d->devt = make_dev (&cx_cdevsw, b->num*NCHAN + c->num + 64, UID_ROOT, GID_WHEEL, 0600, "cx%d", b->num*NCHAN + c->num); 903136480Sphk d->devt->si_drv1 = d; 904138823Srik callout_init (&d->dcd_timeout_handle, 905138823Srik cx_mpsafenet ? CALLOUT_MPSAFE : 0); 906123120Simp } 907123120Simp splx (s); 908123120Simp 909123120Simp return 0; 910123120Simp} 911123120Simp 912123120Simpstatic int cx_detach (device_t dev) 913123120Simp{ 914123120Simp bdrv_t *bd = device_get_softc (dev); 915123120Simp cx_board_t *b = bd->board; 916123120Simp cx_chan_t *c; 917138823Srik int s; 918123120Simp 919138823Srik KASSERT (mtx_initialized (&bd->cx_mtx), ("cx mutex not initialized")); 920138823Srik 921138823Srik s = splhigh (); 922138823Srik CX_LOCK (bd); 923123120Simp /* Check if the device is busy (open). */ 924123120Simp for (c = b->chan; c < b->chan + NCHAN; ++c) { 925123120Simp drv_t *d = (drv_t*) c->sys; 926123120Simp 927123120Simp if (!d || d->chan->type == T_NONE) 928123120Simp continue; 929123120Simp if (d->lock) { 930138823Srik CX_UNLOCK (bd); 931123120Simp splx (s); 932123120Simp return EBUSY; 933123120Simp } 934130240Srik if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) && 935123120Simp (d->open_dev|0x2)) { 936138823Srik CX_UNLOCK (bd); 937123120Simp splx (s); 938123120Simp return EBUSY; 939123120Simp } 940123120Simp if (d->running) { 941138823Srik CX_UNLOCK (bd); 942123120Simp splx (s); 943123120Simp return EBUSY; 944123120Simp } 945123120Simp } 946123120Simp 947123120Simp /* Deactivate the timeout routine. And soft interrupt*/ 948138823Srik callout_stop (&led_timo[b->num]); 949123120Simp 950123120Simp for (c = b->chan; c < b->chan + NCHAN; ++c) { 951123120Simp drv_t *d = c->sys; 952123120Simp 953123120Simp if (!d || d->chan->type == T_NONE) 954123120Simp continue; 955123120Simp 956138823Srik callout_stop (&d->dcd_timeout_handle); 957123120Simp } 958138823Srik CX_UNLOCK (bd); 959123120Simp bus_teardown_intr (dev, bd->irq_res, bd->intrhand); 960123120Simp bus_deactivate_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res); 961123120Simp bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res); 962123120Simp 963123120Simp bus_deactivate_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res); 964123120Simp bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res); 965123120Simp 966123120Simp bus_deactivate_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->irq_res); 967123120Simp bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->base_res); 968123120Simp 969138823Srik CX_LOCK (bd); 970123120Simp cx_close_board (b); 971123120Simp 972123120Simp /* Detach the interfaces, free buffer memory. */ 973123120Simp for (c = b->chan; c < b->chan + NCHAN; ++c) { 974123120Simp drv_t *d = (drv_t*) c->sys; 975123120Simp 976123120Simp if (!d || d->chan->type == T_NONE) 977123120Simp continue; 978130301Srik 979130301Srik if (d->tty) { 980136480Sphk ttyfree (d->tty); 981130301Srik d->tty = NULL; 982130301Srik } 983130301Srik 984123120Simp#ifdef NETGRAPH 985123120Simp if (d->node) { 986123120Simp ng_rmnode_self (d->node); 987123120Simp NG_NODE_UNREF (d->node); 988123120Simp d->node = NULL; 989123120Simp } 990123120Simp mtx_destroy (&d->lo_queue.ifq_mtx); 991123120Simp mtx_destroy (&d->hi_queue.ifq_mtx); 992123120Simp#else 993123120Simp /* Detach from the packet filter list of interfaces. */ 994147256Sbrooks bpfdetach (d->ifp); 995123120Simp /* Detach from the sync PPP list. */ 996147256Sbrooks sppp_detach (d->ifp); 997123120Simp 998147256Sbrooks if_detach (d->ifp); 999147256Sbrooks if_free(d->ifp); 1000138823Srik /* XXXRIK: check interconnection with irq handler */ 1001138823Srik IF_DRAIN (&d->queue); 1002138823Srik mtx_destroy (&d->queue.ifq_mtx); 1003123120Simp#endif 1004136480Sphk destroy_dev (d->devt); 1005123120Simp } 1006123120Simp 1007123120Simp cx_led_off (b); 1008138823Srik CX_UNLOCK (bd); 1009138823Srik callout_drain (&led_timo[b->num]); 1010138823Srik for (c = b->chan; c < b->chan + NCHAN; ++c) { 1011138823Srik drv_t *d = c->sys; 1012138823Srik 1013138823Srik if (!d || d->chan->type == T_NONE) 1014138823Srik continue; 1015138823Srik 1016138823Srik callout_drain (&d->dcd_timeout_handle); 1017138823Srik } 1018123120Simp splx (s); 1019123120Simp 1020123120Simp s = splhigh (); 1021123120Simp for (c = b->chan; c < b->chan + NCHAN; ++c) { 1022123120Simp drv_t *d = (drv_t*) c->sys; 1023123120Simp 1024123120Simp if (!d || d->chan->type == T_NONE) 1025123120Simp continue; 1026123120Simp 1027123120Simp /* Deallocate buffers. */ 1028130985Srik cx_bus_dma_mem_free (&d->dmamem); 1029123120Simp } 1030123120Simp bd->board = 0; 1031123120Simp adapter [b->num] = 0; 1032123120Simp free (b, M_DEVBUF); 1033123120Simp splx (s); 1034138823Srik 1035138823Srik mtx_destroy (&bd->cx_mtx); 1036123120Simp 1037123120Simp return 0; 1038123120Simp} 1039123120Simp 1040123120Simp#ifndef NETGRAPH 1041123120Simpstatic void cx_ifstart (struct ifnet *ifp) 1042123120Simp{ 1043133644Srik drv_t *d = ifp->if_softc; 1044138823Srik bdrv_t *bd = d->board->sys; 1045123120Simp 1046138823Srik CX_LOCK (bd); 1047123120Simp cx_start (d); 1048138823Srik CX_UNLOCK (bd); 1049123120Simp} 1050123120Simp 1051123120Simpstatic void cx_ifwatchdog (struct ifnet *ifp) 1052123120Simp{ 1053133644Srik drv_t *d = ifp->if_softc; 1054123120Simp 1055123120Simp cx_watchdog (d); 1056123120Simp} 1057123120Simp 1058123120Simpstatic void cx_tlf (struct sppp *sp) 1059123120Simp{ 1060147256Sbrooks drv_t *d = SP2IFP(sp)->if_softc; 1061123120Simp 1062123120Simp CX_DEBUG (d, ("cx_tlf\n")); 1063123120Simp/* cx_set_dtr (d->chan, 0);*/ 1064123120Simp/* cx_set_rts (d->chan, 0);*/ 1065147256Sbrooks if (!(IFP2SP(d->ifp)->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO)) 1066138352Srik sp->pp_down (sp); 1067123120Simp} 1068123120Simp 1069123120Simpstatic void cx_tls (struct sppp *sp) 1070123120Simp{ 1071147256Sbrooks drv_t *d = SP2IFP(sp)->if_softc; 1072123120Simp 1073123120Simp CX_DEBUG (d, ("cx_tls\n")); 1074147256Sbrooks if (!(IFP2SP(d->ifp)->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO)) 1075138352Srik sp->pp_up (sp); 1076123120Simp} 1077123120Simp 1078123120Simp/* 1079123120Simp * Initialization of interface. 1080123120Simp * It seems to be never called by upper level. 1081123120Simp */ 1082123120Simpstatic void cx_initialize (void *softc) 1083123120Simp{ 1084123120Simp drv_t *d = softc; 1085123120Simp 1086123120Simp CX_DEBUG (d, ("cx_initialize\n")); 1087123120Simp} 1088123120Simp 1089123120Simp/* 1090123120Simp * Process an ioctl request. 1091123120Simp */ 1092123120Simpstatic int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data) 1093123120Simp{ 1094123120Simp drv_t *d = ifp->if_softc; 1095138823Srik bdrv_t *bd = d->board->sys; 1096123120Simp int error, s, was_up, should_be_up; 1097123120Simp 1098123120Simp /* No socket ioctls while the channel is in async mode. */ 1099123120Simp if (d->chan->type == T_NONE || d->chan->mode == M_ASYNC) 1100123120Simp return EBUSY; 1101123120Simp 1102123120Simp /* Socket ioctls on slave subchannels are not allowed. */ 1103148887Srwatson was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0; 1104123120Simp error = sppp_ioctl (ifp, cmd, data); 1105123120Simp if (error) 1106123120Simp return error; 1107123120Simp 1108123120Simp if (! (ifp->if_flags & IFF_DEBUG)) 1109123120Simp d->chan->debug = 0; 1110123120Simp else if (! d->chan->debug) 1111123120Simp d->chan->debug = 1; 1112123120Simp 1113123120Simp switch (cmd) { 1114133644Srik default: CX_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0; 1115123120Simp case SIOCADDMULTI: CX_DEBUG2 (d, ("SIOCADDMULTI\n")); return 0; 1116123120Simp case SIOCDELMULTI: CX_DEBUG2 (d, ("SIOCDELMULTI\n")); return 0; 1117123120Simp case SIOCSIFFLAGS: CX_DEBUG2 (d, ("SIOCSIFFLAGS\n")); break; 1118123120Simp case SIOCSIFADDR: CX_DEBUG2 (d, ("SIOCSIFADDR\n")); break; 1119123120Simp } 1120123120Simp 1121123120Simp /* We get here only in case of SIFFLAGS or SIFADDR. */ 1122123120Simp s = splhigh (); 1123138823Srik CX_LOCK (bd); 1124148887Srwatson should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0; 1125123120Simp if (!was_up && should_be_up) { 1126123120Simp /* Interface goes up -- start it. */ 1127123120Simp cx_up (d); 1128123120Simp cx_start (d); 1129123120Simp } else if (was_up && !should_be_up) { 1130123120Simp /* Interface is going down -- stop it. */ 1131147256Sbrooks /* if ((IFP2SP(d->ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/ 1132123120Simp cx_down (d); 1133123120Simp } 1134138823Srik CX_UNLOCK (bd); 1135123120Simp splx (s); 1136123120Simp return 0; 1137123120Simp} 1138123120Simp#endif /*NETGRAPH*/ 1139123120Simp 1140123120Simp/* 1141123120Simp * Stop the interface. Called on splimp(). 1142123120Simp */ 1143123120Simpstatic void cx_down (drv_t *d) 1144123120Simp{ 1145123120Simp int s = splhigh (); 1146123120Simp CX_DEBUG (d, ("cx_down\n")); 1147123120Simp cx_set_dtr (d->chan, 0); 1148123120Simp cx_set_rts (d->chan, 0); 1149123120Simp d->running = 0; 1150123120Simp splx (s); 1151123120Simp} 1152123120Simp 1153123120Simp/* 1154123120Simp * Start the interface. Called on splimp(). 1155123120Simp */ 1156123120Simpstatic void cx_up (drv_t *d) 1157123120Simp{ 1158123120Simp int s = splhigh (); 1159123120Simp CX_DEBUG (d, ("cx_up\n")); 1160123120Simp cx_set_dtr (d->chan, 1); 1161123120Simp cx_set_rts (d->chan, 1); 1162123120Simp d->running = 1; 1163123120Simp splx (s); 1164123120Simp} 1165123120Simp 1166123120Simp/* 1167123120Simp * Start output on the (slave) interface. Get another datagram to send 1168123120Simp * off of the interface queue, and copy it to the interface 1169123120Simp * before starting the output. 1170123120Simp */ 1171123120Simpstatic void cx_send (drv_t *d) 1172123120Simp{ 1173123120Simp struct mbuf *m; 1174123120Simp u_short len; 1175123120Simp 1176123120Simp CX_DEBUG2 (d, ("cx_send\n")); 1177123120Simp 1178123120Simp /* No output if the interface is down. */ 1179123120Simp if (! d->running) 1180123120Simp return; 1181123120Simp 1182123120Simp /* No output if the modem is off. */ 1183123120Simp if (! cx_get_dsr (d->chan) && ! cx_get_loop(d->chan)) 1184123120Simp return; 1185123120Simp 1186123120Simp if (cx_buf_free (d->chan)) { 1187123120Simp /* Get the packet to send. */ 1188123120Simp#ifdef NETGRAPH 1189123120Simp IF_DEQUEUE (&d->hi_queue, m); 1190123120Simp if (! m) 1191123120Simp IF_DEQUEUE (&d->lo_queue, m); 1192123120Simp#else 1193147256Sbrooks m = sppp_dequeue (d->ifp); 1194123120Simp#endif 1195123120Simp if (! m) 1196123120Simp return; 1197130971Srik#ifndef NETGRAPH 1198147256Sbrooks if (d->ifp->if_bpf) 1199147256Sbrooks BPF_MTAP (d->ifp, m); 1200123120Simp#endif 1201147862Srik len = m_length (m, NULL); 1202123120Simp if (! m->m_next) 1203123120Simp cx_send_packet (d->chan, (u_char*)mtod (m, caddr_t), 1204123120Simp len, 0); 1205123120Simp else { 1206123120Simp u_char buf [DMABUFSZ]; 1207123120Simp m_copydata (m, 0, len, buf); 1208123120Simp cx_send_packet (d->chan, buf, len, 0); 1209123120Simp } 1210123120Simp m_freem (m); 1211123120Simp 1212123120Simp /* Set up transmit timeout, 10 seconds. */ 1213123120Simp#ifdef NETGRAPH 1214123120Simp d->timeout = 10; 1215123120Simp#else 1216147256Sbrooks d->ifp->if_timer = 10; 1217123120Simp#endif 1218123120Simp } 1219123120Simp#ifndef NETGRAPH 1220148887Srwatson d->ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1221123120Simp#endif 1222123120Simp} 1223123120Simp 1224123120Simp/* 1225123120Simp * Start output on the interface. 1226123120Simp * Always called on splimp(). 1227123120Simp */ 1228123120Simpstatic void cx_start (drv_t *d) 1229123120Simp{ 1230123120Simp int s = splhigh (); 1231123120Simp if (d->running) { 1232123120Simp if (! d->chan->dtr) 1233123120Simp cx_set_dtr (d->chan, 1); 1234123120Simp if (! d->chan->rts) 1235123120Simp cx_set_rts (d->chan, 1); 1236123120Simp cx_send (d); 1237123120Simp } 1238123120Simp splx (s); 1239123120Simp} 1240123120Simp 1241123120Simp/* 1242123120Simp * Handle transmit timeouts. 1243123120Simp * Recover after lost transmit interrupts. 1244123120Simp * Always called on splimp(). 1245123120Simp */ 1246123120Simpstatic void cx_watchdog (drv_t *d) 1247123120Simp{ 1248138823Srik bdrv_t *bd = d->board->sys; 1249138823Srik 1250123120Simp int s = splhigh (); 1251138823Srik CX_LOCK (bd); 1252123120Simp CX_DEBUG (d, ("device timeout\n")); 1253123120Simp if (d->running) { 1254123120Simp cx_setup_chan (d->chan); 1255123120Simp cx_start_chan (d->chan, 0, 0); 1256123120Simp cx_set_dtr (d->chan, 1); 1257123120Simp cx_set_rts (d->chan, 1); 1258123120Simp cx_start (d); 1259123120Simp } 1260138823Srik CX_UNLOCK (bd); 1261123120Simp splx (s); 1262123120Simp} 1263123120Simp 1264123120Simp/* 1265123120Simp * Transmit callback function. 1266123120Simp */ 1267123120Simpstatic void cx_transmit (cx_chan_t *c, void *attachment, int len) 1268123120Simp{ 1269123120Simp drv_t *d = c->sys; 1270123120Simp 1271123120Simp if (!d) 1272123120Simp return; 1273123120Simp 1274130240Srik if (c->mode == M_ASYNC && d->tty) { 1275130240Srik d->tty->t_state &= ~(TS_BUSY | TS_FLUSH); 1276123120Simp d->atimeout = 0; 1277130240Srik if (d->tty->t_dev) { 1278123120Simp d->intr_action |= CX_WRITE; 1279123120Simp MY_SOFT_INTR = 1; 1280123120Simp swi_sched (cx_fast_ih, 0); 1281123120Simp } 1282123120Simp return; 1283123120Simp } 1284123120Simp#ifdef NETGRAPH 1285123120Simp d->timeout = 0; 1286123120Simp#else 1287147256Sbrooks ++d->ifp->if_opackets; 1288148887Srwatson d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1289147256Sbrooks d->ifp->if_timer = 0; 1290123120Simp#endif 1291123120Simp cx_start (d); 1292123120Simp} 1293123120Simp 1294123120Simp/* 1295123120Simp * Process the received packet. 1296123120Simp */ 1297123120Simpstatic void cx_receive (cx_chan_t *c, char *data, int len) 1298123120Simp{ 1299123120Simp drv_t *d = c->sys; 1300123120Simp struct mbuf *m; 1301123120Simp char *cc = data; 1302138823Srik#ifdef NETGRAPH 1303123120Simp int error; 1304123120Simp#endif 1305123120Simp 1306123120Simp if (!d) 1307123120Simp return; 1308123120Simp 1309130240Srik if (c->mode == M_ASYNC && d->tty) { 1310130240Srik if (d->tty->t_state & TS_ISOPEN) { 1311123120Simp async_q *q = &d->aqueue; 1312123120Simp int size = BF_SZ - 1 - AQ_GSZ (q); 1313123120Simp 1314123120Simp if (len <= 0 && !size) 1315123120Simp return; 1316123120Simp 1317123120Simp if (len > size) { 1318133644Srik c->ierrs++; 1319123120Simp cx_error (c, CX_OVERRUN); 1320123120Simp len = size - 1; 1321123120Simp } 1322123120Simp 1323123120Simp while (len--) { 1324123120Simp AQ_PUSH (q, *(unsigned char *)cc); 1325123120Simp cc++; 1326123120Simp } 1327123120Simp 1328123120Simp d->intr_action |= CX_READ; 1329123120Simp MY_SOFT_INTR = 1; 1330123120Simp swi_sched (cx_fast_ih, 0); 1331123120Simp } 1332123120Simp return; 1333123120Simp } 1334123120Simp if (! d->running) 1335123120Simp return; 1336123120Simp 1337123120Simp m = makembuf (data, len); 1338123120Simp if (! m) { 1339123120Simp CX_DEBUG (d, ("no memory for packet\n")); 1340123120Simp#ifndef NETGRAPH 1341147256Sbrooks ++d->ifp->if_iqdrops; 1342123120Simp#endif 1343123120Simp return; 1344123120Simp } 1345123120Simp if (c->debug > 1) 1346123120Simp printmbuf (m); 1347123120Simp#ifdef NETGRAPH 1348123120Simp m->m_pkthdr.rcvif = 0; 1349123120Simp NG_SEND_DATA_ONLY (error, d->hook, m); 1350123120Simp#else 1351147256Sbrooks ++d->ifp->if_ipackets; 1352147256Sbrooks m->m_pkthdr.rcvif = d->ifp; 1353123120Simp /* Check if there's a BPF listener on this interface. 1354123120Simp * If so, hand off the raw packet to bpf. */ 1355147256Sbrooks if (d->ifp->if_bpf) 1356147256Sbrooks BPF_TAP (d->ifp, data, len); 1357138823Srik IF_ENQUEUE (&d->queue, m); 1358123120Simp#endif 1359123120Simp} 1360123120Simp 1361123120Simp#define CONDITION(t,tp) (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))\ 1362123120Simp && (!(tp->t_iflag & BRKINT) || (tp->t_iflag & IGNBRK))\ 1363123120Simp && (!(tp->t_iflag & PARMRK)\ 1364123120Simp || (tp->t_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))\ 1365123120Simp && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))\ 1366130203Sphk && linesw[tp->t_line]->l_rint == ttyinput) 1367123120Simp 1368123120Simp/* 1369123120Simp * Error callback function. 1370123120Simp */ 1371123120Simpstatic void cx_error (cx_chan_t *c, int data) 1372123120Simp{ 1373123120Simp drv_t *d = c->sys; 1374123120Simp async_q *q; 1375123120Simp 1376123120Simp if (!d) 1377123120Simp return; 1378123120Simp 1379123120Simp q = &(d->aqueue); 1380123120Simp 1381123120Simp switch (data) { 1382123120Simp case CX_FRAME: 1383123120Simp CX_DEBUG (d, ("frame error\n")); 1384130240Srik if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) 1385123120Simp && (AQ_GSZ (q) < BF_SZ - 1) 1386130240Srik && (!CONDITION((&d->tty->t_termios), (d->tty)) 1387130240Srik || !(d->tty->t_iflag & (IGNPAR | PARMRK)))) { 1388123120Simp AQ_PUSH (q, TTY_FE); 1389123120Simp d->intr_action |= CX_READ; 1390123120Simp MY_SOFT_INTR = 1; 1391123120Simp swi_sched (cx_fast_ih, 0); 1392123120Simp } 1393123120Simp#ifndef NETGRAPH 1394123120Simp else 1395147256Sbrooks ++d->ifp->if_ierrors; 1396123120Simp#endif 1397123120Simp break; 1398123120Simp case CX_CRC: 1399123120Simp CX_DEBUG (d, ("crc error\n")); 1400130240Srik if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) 1401123120Simp && (AQ_GSZ (q) < BF_SZ - 1) 1402130240Srik && (!CONDITION((&d->tty->t_termios), (d->tty)) 1403130240Srik || !(d->tty->t_iflag & INPCK) 1404130240Srik || !(d->tty->t_iflag & (IGNPAR | PARMRK)))) { 1405123120Simp AQ_PUSH (q, TTY_PE); 1406123120Simp d->intr_action |= CX_READ; 1407123120Simp MY_SOFT_INTR = 1; 1408123120Simp swi_sched (cx_fast_ih, 0); 1409123120Simp } 1410123120Simp#ifndef NETGRAPH 1411123120Simp else 1412147256Sbrooks ++d->ifp->if_ierrors; 1413123120Simp#endif 1414123120Simp break; 1415123120Simp case CX_OVERRUN: 1416123120Simp CX_DEBUG (d, ("overrun error\n")); 1417123120Simp#ifdef TTY_OE 1418130240Srik if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) 1419123120Simp && (AQ_GSZ (q) < BF_SZ - 1) 1420130240Srik && (!CONDITION((&d->tty->t_termios), (d->tty)))) { 1421123120Simp AQ_PUSH (q, TTY_OE); 1422123120Simp d->intr_action |= CX_READ; 1423123120Simp MY_SOFT_INTR = 1; 1424123120Simp swi_sched (cx_fast_ih, 0); 1425123120Simp } 1426123120Simp#endif 1427123120Simp#ifndef NETGRAPH 1428123120Simp else { 1429147256Sbrooks ++d->ifp->if_collisions; 1430147256Sbrooks ++d->ifp->if_ierrors; 1431123120Simp } 1432123120Simp#endif 1433123120Simp break; 1434123120Simp case CX_OVERFLOW: 1435123120Simp CX_DEBUG (d, ("overflow error\n")); 1436123120Simp#ifndef NETGRAPH 1437123120Simp if (c->mode != M_ASYNC) 1438147256Sbrooks ++d->ifp->if_ierrors; 1439123120Simp#endif 1440123120Simp break; 1441123120Simp case CX_UNDERRUN: 1442123120Simp CX_DEBUG (d, ("underrun error\n")); 1443123120Simp if (c->mode != M_ASYNC) { 1444123120Simp#ifdef NETGRAPH 1445123120Simp d->timeout = 0; 1446123120Simp#else 1447147256Sbrooks ++d->ifp->if_oerrors; 1448148887Srwatson d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1449147256Sbrooks d->ifp->if_timer = 0; 1450123120Simp cx_start (d); 1451123120Simp#endif 1452123120Simp } 1453123120Simp break; 1454123120Simp case CX_BREAK: 1455123120Simp CX_DEBUG (d, ("break error\n")); 1456130240Srik if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) 1457123120Simp && (AQ_GSZ (q) < BF_SZ - 1) 1458130240Srik && (!CONDITION((&d->tty->t_termios), (d->tty)) 1459130240Srik || !(d->tty->t_iflag & (IGNBRK | BRKINT | PARMRK)))) { 1460123120Simp AQ_PUSH (q, TTY_BI); 1461123120Simp d->intr_action |= CX_READ; 1462123120Simp MY_SOFT_INTR = 1; 1463123120Simp swi_sched (cx_fast_ih, 0); 1464123120Simp } 1465123120Simp#ifndef NETGRAPH 1466123120Simp else 1467147256Sbrooks ++d->ifp->if_ierrors; 1468123120Simp#endif 1469123120Simp break; 1470123120Simp default: 1471123120Simp CX_DEBUG (d, ("error #%d\n", data)); 1472123120Simp } 1473123120Simp} 1474123120Simp 1475136480Sphkstatic int cx_topen (struct tty *tp, struct cdev *dev) 1476123120Simp{ 1477138823Srik bdrv_t *bd; 1478123120Simp drv_t *d; 1479123120Simp 1480136480Sphk d = tp->t_sc; 1481138823Srik CX_DEBUG2 (d, ("cx_open (serial)\n")); 1482138823Srik 1483138823Srik bd = d->board->sys; 1484138823Srik 1485136480Sphk if (d->chan->mode != M_ASYNC) 1486136480Sphk return (EBUSY); 1487138823Srik 1488123120Simp d->open_dev |= 0x2; 1489138823Srik CX_LOCK (bd); 1490136480Sphk cx_start_chan (d->chan, 0, 0); 1491136480Sphk cx_set_dtr (d->chan, 1); 1492136480Sphk cx_set_rts (d->chan, 1); 1493136480Sphk d->cd = cx_get_cd (d->chan); 1494138823Srik CX_UNLOCK (bd); 1495138823Srik 1496138823Srik CX_DEBUG2 (d, ("cx_open done\n")); 1497138823Srik 1498138823Srik return 0; 1499123120Simp} 1500123120Simp 1501136480Sphkstatic void cx_tclose (struct tty *tp) 1502123120Simp{ 1503136480Sphk drv_t *d; 1504138823Srik bdrv_t *bd; 1505123120Simp 1506136480Sphk d = tp->t_sc; 1507138823Srik CX_DEBUG2 (d, ("cx_close\n")); 1508138823Srik bd = d->board->sys; 1509138823Srik CX_LOCK (bd); 1510123120Simp /* Disable receiver. 1511123120Simp * Transmitter continues sending the queued data. */ 1512123120Simp cx_enable_receive (d->chan, 0); 1513138823Srik CX_UNLOCK (bd); 1514123120Simp d->open_dev &= ~0x2; 1515123120Simp} 1516123120Simp 1517136480Sphkstatic int cx_tmodem (struct tty *tp, int sigon, int sigoff) 1518123120Simp{ 1519136480Sphk drv_t *d; 1520138823Srik bdrv_t *bd; 1521123120Simp 1522136480Sphk d = tp->t_sc; 1523138823Srik bd = d->board->sys; 1524123120Simp 1525138823Srik CX_LOCK (bd); 1526136480Sphk if (!sigon && !sigoff) { 1527136480Sphk if (cx_get_dsr (d->chan)) sigon |= SER_DSR; 1528136480Sphk if (cx_get_cd (d->chan)) sigon |= SER_DCD; 1529136480Sphk if (cx_get_cts (d->chan)) sigon |= SER_CTS; 1530136480Sphk if (d->chan->dtr) sigon |= SER_DTR; 1531136480Sphk if (d->chan->rts) sigon |= SER_RTS; 1532147859Srik CX_UNLOCK (bd); 1533136480Sphk return sigon; 1534136480Sphk } 1535136480Sphk 1536136480Sphk if (sigon & SER_DTR) 1537136480Sphk cx_set_dtr (d->chan, 1); 1538136480Sphk if (sigoff & SER_DTR) 1539136480Sphk cx_set_dtr (d->chan, 0); 1540136480Sphk if (sigon & SER_RTS) 1541136480Sphk cx_set_rts (d->chan, 1); 1542136480Sphk if (sigoff & SER_RTS) 1543136480Sphk cx_set_rts (d->chan, 0); 1544138823Srik CX_UNLOCK (bd); 1545138823Srik 1546136480Sphk return (0); 1547123120Simp} 1548123120Simp 1549150622Srikstatic int cx_open (struct cdev *dev, int flag, int mode, struct thread *td) 1550150622Srik{ 1551150622Srik int unit; 1552150622Srik drv_t *d; 1553150622Srik 1554150622Srik d = dev->si_drv1; 1555150622Srik unit = d->chan->num; 1556150622Srik 1557150622Srik CX_DEBUG2 (d, ("cx_open unit=%d, flag=0x%x, mode=0x%x\n", 1558150622Srik unit, flag, mode)); 1559150622Srik 1560150622Srik d->open_dev |= 0x1; 1561150622Srik 1562150622Srik CX_DEBUG2 (d, ("cx_open done\n")); 1563150622Srik 1564150622Srik return 0; 1565150622Srik} 1566150622Srik 1567150622Srikstatic int cx_close (struct cdev *dev, int flag, int mode, struct thread *td) 1568150622Srik{ 1569150622Srik drv_t *d; 1570150622Srik 1571150622Srik d = dev->si_drv1; 1572150622Srik CX_DEBUG2 (d, ("cx_close\n")); 1573150622Srik d->open_dev &= ~0x1; 1574150622Srik return 0; 1575150622Srik} 1576150622Srik 1577150622Srikstatic int cx_modem_status (drv_t *d) 1578150622Srik{ 1579150622Srik bdrv_t *bd = d->board->sys; 1580150622Srik int status = 0, s = splhigh (); 1581150622Srik CX_LOCK (bd); 1582150622Srik /* Already opened by someone or network interface is up? */ 1583150622Srik if ((d->chan->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) && 1584150622Srik (d->open_dev|0x2)) || (d->chan->mode != M_ASYNC && d->running)) 1585150622Srik status = TIOCM_LE; /* always enabled while open */ 1586150622Srik 1587150622Srik if (cx_get_dsr (d->chan)) status |= TIOCM_DSR; 1588150622Srik if (cx_get_cd (d->chan)) status |= TIOCM_CD; 1589150622Srik if (cx_get_cts (d->chan)) status |= TIOCM_CTS; 1590150622Srik if (d->chan->dtr) status |= TIOCM_DTR; 1591150622Srik if (d->chan->rts) status |= TIOCM_RTS; 1592150622Srik CX_UNLOCK (bd); 1593150622Srik splx (s); 1594150622Srik return status; 1595150622Srik} 1596150622Srik 1597150622Srikstatic int cx_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 1598150622Srik{ 1599150622Srik drv_t *d; 1600150622Srik bdrv_t *bd; 1601150622Srik cx_chan_t *c; 1602150622Srik struct serial_statistics *st; 1603150622Srik int error, s; 1604150622Srik char mask[16]; 1605150622Srik 1606150622Srik d = dev->si_drv1; 1607150622Srik c = d->chan; 1608150622Srik 1609150622Srik bd = d->board->sys; 1610150622Srik 1611150622Srik switch (cmd) { 1612150622Srik case SERIAL_GETREGISTERED: 1613150622Srik CX_DEBUG2 (d, ("ioctl: getregistered\n")); 1614150622Srik bzero (mask, sizeof(mask)); 1615150622Srik for (s=0; s<NCX*NCHAN; ++s) 1616150622Srik if (channel [s]) 1617150622Srik mask [s/8] |= 1 << (s & 7); 1618150622Srik bcopy (mask, data, sizeof (mask)); 1619150622Srik return 0; 1620150622Srik 1621150622Srik case SERIAL_GETPORT: 1622150622Srik CX_DEBUG2 (d, ("ioctl: getport\n")); 1623150622Srik s = splhigh (); 1624150622Srik CX_LOCK (bd); 1625150622Srik *(int *)data = cx_get_port (c); 1626150622Srik CX_UNLOCK (bd); 1627150622Srik splx (s); 1628150622Srik if (*(int *)data<0) 1629150622Srik return (EINVAL); 1630150622Srik else 1631150622Srik return 0; 1632150622Srik 1633150622Srik case SERIAL_SETPORT: 1634150622Srik CX_DEBUG2 (d, ("ioctl: setproto\n")); 1635150622Srik /* Only for superuser! */ 1636150622Srik error = suser (td); 1637150622Srik if (error) 1638150622Srik return error; 1639150622Srik 1640150622Srik s = splhigh (); 1641150622Srik CX_LOCK (bd); 1642150622Srik cx_set_port (c, *(int *)data); 1643150622Srik CX_UNLOCK (bd); 1644150622Srik splx (s); 1645150622Srik return 0; 1646150622Srik 1647150622Srik#ifndef NETGRAPH 1648150622Srik case SERIAL_GETPROTO: 1649150622Srik CX_DEBUG2 (d, ("ioctl: getproto\n")); 1650150622Srik s = splhigh (); 1651150622Srik CX_LOCK (bd); 1652150622Srik strcpy ((char*)data, (c->mode == M_ASYNC) ? "async" : 1653150622Srik (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" : 1654150622Srik (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp"); 1655150622Srik CX_UNLOCK (bd); 1656150622Srik splx (s); 1657150622Srik return 0; 1658150622Srik 1659150622Srik case SERIAL_SETPROTO: 1660150622Srik CX_DEBUG2 (d, ("ioctl: setproto\n")); 1661150622Srik /* Only for superuser! */ 1662150622Srik error = suser (td); 1663150622Srik if (error) 1664150622Srik return error; 1665150622Srik if (c->mode == M_ASYNC) 1666150622Srik return EBUSY; 1667150622Srik if (d->ifp->if_drv_flags & IFF_DRV_RUNNING) 1668150622Srik return EBUSY; 1669150622Srik if (! strcmp ("cisco", (char*)data)) { 1670150622Srik IFP2SP(d->ifp)->pp_flags &= ~(PP_FR); 1671150622Srik IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE; 1672150622Srik d->ifp->if_flags |= PP_CISCO; 1673150622Srik } else if (! strcmp ("fr", (char*)data)) { 1674150622Srik d->ifp->if_flags &= ~(PP_CISCO); 1675150622Srik IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE; 1676150622Srik } else if (! strcmp ("ppp", (char*)data)) { 1677150622Srik IFP2SP(d->ifp)->pp_flags &= ~(PP_FR | PP_KEEPALIVE); 1678150622Srik d->ifp->if_flags &= ~(PP_CISCO); 1679150622Srik } else 1680150622Srik return EINVAL; 1681150622Srik return 0; 1682150622Srik 1683150622Srik case SERIAL_GETKEEPALIVE: 1684150622Srik CX_DEBUG2 (d, ("ioctl: getkeepalive\n")); 1685150622Srik if ((IFP2SP(d->ifp)->pp_flags & PP_FR) || 1686150622Srik (d->ifp->if_flags & PP_CISCO) || 1687150622Srik (c->mode == M_ASYNC)) 1688150622Srik return EINVAL; 1689150622Srik s = splhigh (); 1690150622Srik CX_LOCK (bd); 1691150622Srik *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0; 1692150622Srik CX_UNLOCK (bd); 1693150622Srik splx (s); 1694150622Srik return 0; 1695150622Srik 1696150622Srik case SERIAL_SETKEEPALIVE: 1697150622Srik CX_DEBUG2 (d, ("ioctl: setkeepalive\n")); 1698150622Srik /* Only for superuser! */ 1699150622Srik error = suser (td); 1700150622Srik if (error) 1701150622Srik return error; 1702150622Srik if ((IFP2SP(d->ifp)->pp_flags & PP_FR) || 1703150622Srik (d->ifp->if_flags & PP_CISCO)) 1704150622Srik return EINVAL; 1705150622Srik s = splhigh (); 1706150622Srik CX_LOCK (bd); 1707150622Srik if (*(int*)data) 1708150622Srik IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE; 1709150622Srik else 1710150622Srik IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE; 1711150622Srik CX_UNLOCK (bd); 1712150622Srik splx (s); 1713150622Srik return 0; 1714150622Srik#endif /*NETGRAPH*/ 1715150622Srik 1716150622Srik case SERIAL_GETMODE: 1717150622Srik CX_DEBUG2 (d, ("ioctl: getmode\n")); 1718150622Srik s = splhigh (); 1719150622Srik CX_LOCK (bd); 1720150622Srik *(int*)data = (c->mode == M_ASYNC) ? 1721150622Srik SERIAL_ASYNC : SERIAL_HDLC; 1722150622Srik CX_UNLOCK (bd); 1723150622Srik splx (s); 1724150622Srik return 0; 1725150622Srik 1726150622Srik case SERIAL_SETMODE: 1727150622Srik CX_DEBUG2 (d, ("ioctl: setmode\n")); 1728150622Srik /* Only for superuser! */ 1729150622Srik error = suser (td); 1730150622Srik if (error) 1731150622Srik return error; 1732150622Srik 1733150622Srik /* Somebody is waiting for carrier? */ 1734150622Srik if (d->lock) 1735150622Srik return EBUSY; 1736150622Srik /* /dev/ttyXX is already opened by someone? */ 1737150622Srik if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) && 1738150622Srik (d->open_dev|0x2)) 1739150622Srik return EBUSY; 1740150622Srik /* Network interface is up? 1741150622Srik * Cannot change to async mode. */ 1742150622Srik if (c->mode != M_ASYNC && d->running && 1743150622Srik (*(int*)data == SERIAL_ASYNC)) 1744150622Srik return EBUSY; 1745150622Srik 1746150622Srik s = splhigh (); 1747150622Srik CX_LOCK (bd); 1748150622Srik if (c->mode == M_HDLC && *(int*)data == SERIAL_ASYNC) { 1749150622Srik cx_set_mode (c, M_ASYNC); 1750150622Srik cx_enable_receive (c, 0); 1751150622Srik cx_enable_transmit (c, 0); 1752150622Srik } else if (c->mode == M_ASYNC && *(int*)data == SERIAL_HDLC) { 1753150622Srik cx_set_mode (c, M_HDLC); 1754150622Srik cx_enable_receive (c, 1); 1755150622Srik cx_enable_transmit (c, 1); 1756150622Srik } 1757150622Srik CX_UNLOCK (bd); 1758150622Srik splx (s); 1759150622Srik return 0; 1760150622Srik 1761150622Srik case SERIAL_GETSTAT: 1762150622Srik CX_DEBUG2 (d, ("ioctl: getestat\n")); 1763150622Srik st = (struct serial_statistics*) data; 1764150622Srik s = splhigh (); 1765150622Srik CX_LOCK (bd); 1766150622Srik st->rintr = c->rintr; 1767150622Srik st->tintr = c->tintr; 1768150622Srik st->mintr = c->mintr; 1769150622Srik st->ibytes = c->ibytes; 1770150622Srik st->ipkts = c->ipkts; 1771150622Srik st->ierrs = c->ierrs; 1772150622Srik st->obytes = c->obytes; 1773150622Srik st->opkts = c->opkts; 1774150622Srik st->oerrs = c->oerrs; 1775150622Srik CX_UNLOCK (bd); 1776150622Srik splx (s); 1777150622Srik return 0; 1778150622Srik 1779150622Srik case SERIAL_CLRSTAT: 1780150622Srik CX_DEBUG2 (d, ("ioctl: clrstat\n")); 1781150622Srik /* Only for superuser! */ 1782150622Srik error = suser (td); 1783150622Srik if (error) 1784150622Srik return error; 1785150622Srik s = splhigh (); 1786150622Srik CX_LOCK (bd); 1787150622Srik c->rintr = 0; 1788150622Srik c->tintr = 0; 1789150622Srik c->mintr = 0; 1790150622Srik c->ibytes = 0; 1791150622Srik c->ipkts = 0; 1792150622Srik c->ierrs = 0; 1793150622Srik c->obytes = 0; 1794150622Srik c->opkts = 0; 1795150622Srik c->oerrs = 0; 1796150622Srik CX_UNLOCK (bd); 1797150622Srik splx (s); 1798150622Srik return 0; 1799150622Srik 1800150622Srik case SERIAL_GETBAUD: 1801150622Srik CX_DEBUG2 (d, ("ioctl: getbaud\n")); 1802150622Srik if (c->mode == M_ASYNC) 1803150622Srik return EINVAL; 1804150622Srik s = splhigh (); 1805150622Srik CX_LOCK (bd); 1806150622Srik *(long*)data = cx_get_baud(c); 1807150622Srik CX_UNLOCK (bd); 1808150622Srik splx (s); 1809150622Srik return 0; 1810150622Srik 1811150622Srik case SERIAL_SETBAUD: 1812150622Srik CX_DEBUG2 (d, ("ioctl: setbaud\n")); 1813150622Srik /* Only for superuser! */ 1814150622Srik error = suser (td); 1815150622Srik if (error) 1816150622Srik return error; 1817150622Srik if (c->mode == M_ASYNC) 1818150622Srik return EINVAL; 1819150622Srik s = splhigh (); 1820150622Srik CX_LOCK (bd); 1821150622Srik cx_set_baud (c, *(long*)data); 1822150622Srik CX_UNLOCK (bd); 1823150622Srik splx (s); 1824150622Srik return 0; 1825150622Srik 1826150622Srik case SERIAL_GETLOOP: 1827150622Srik CX_DEBUG2 (d, ("ioctl: getloop\n")); 1828150622Srik if (c->mode == M_ASYNC) 1829150622Srik return EINVAL; 1830150622Srik s = splhigh (); 1831150622Srik CX_LOCK (bd); 1832150622Srik *(int*)data = cx_get_loop (c); 1833150622Srik CX_UNLOCK (bd); 1834150622Srik splx (s); 1835150622Srik return 0; 1836150622Srik 1837150622Srik case SERIAL_SETLOOP: 1838150622Srik CX_DEBUG2 (d, ("ioctl: setloop\n")); 1839150622Srik /* Only for superuser! */ 1840150622Srik error = suser (td); 1841150622Srik if (error) 1842150622Srik return error; 1843150622Srik if (c->mode == M_ASYNC) 1844150622Srik return EINVAL; 1845150622Srik s = splhigh (); 1846150622Srik CX_LOCK (bd); 1847150622Srik cx_set_loop (c, *(int*)data); 1848150622Srik CX_UNLOCK (bd); 1849150622Srik splx (s); 1850150622Srik return 0; 1851150622Srik 1852150622Srik case SERIAL_GETDPLL: 1853150622Srik CX_DEBUG2 (d, ("ioctl: getdpll\n")); 1854150622Srik if (c->mode == M_ASYNC) 1855150622Srik return EINVAL; 1856150622Srik s = splhigh (); 1857150622Srik CX_LOCK (bd); 1858150622Srik *(int*)data = cx_get_dpll (c); 1859150622Srik CX_UNLOCK (bd); 1860150622Srik splx (s); 1861150622Srik return 0; 1862150622Srik 1863150622Srik case SERIAL_SETDPLL: 1864150622Srik CX_DEBUG2 (d, ("ioctl: setdpll\n")); 1865150622Srik /* Only for superuser! */ 1866150622Srik error = suser (td); 1867150622Srik if (error) 1868150622Srik return error; 1869150622Srik if (c->mode == M_ASYNC) 1870150622Srik return EINVAL; 1871150622Srik s = splhigh (); 1872150622Srik CX_LOCK (bd); 1873150622Srik cx_set_dpll (c, *(int*)data); 1874150622Srik CX_UNLOCK (bd); 1875150622Srik splx (s); 1876150622Srik return 0; 1877150622Srik 1878150622Srik case SERIAL_GETNRZI: 1879150622Srik CX_DEBUG2 (d, ("ioctl: getnrzi\n")); 1880150622Srik if (c->mode == M_ASYNC) 1881150622Srik return EINVAL; 1882150622Srik s = splhigh (); 1883150622Srik CX_LOCK (bd); 1884150622Srik *(int*)data = cx_get_nrzi (c); 1885150622Srik CX_UNLOCK (bd); 1886150622Srik splx (s); 1887150622Srik return 0; 1888150622Srik 1889150622Srik case SERIAL_SETNRZI: 1890150622Srik CX_DEBUG2 (d, ("ioctl: setnrzi\n")); 1891150622Srik /* Only for superuser! */ 1892150622Srik error = suser (td); 1893150622Srik if (error) 1894150622Srik return error; 1895150622Srik if (c->mode == M_ASYNC) 1896150622Srik return EINVAL; 1897150622Srik s = splhigh (); 1898150622Srik CX_LOCK (bd); 1899150622Srik cx_set_nrzi (c, *(int*)data); 1900150622Srik CX_UNLOCK (bd); 1901150622Srik splx (s); 1902150622Srik return 0; 1903150622Srik 1904150622Srik case SERIAL_GETDEBUG: 1905150622Srik CX_DEBUG2 (d, ("ioctl: getdebug\n")); 1906150622Srik s = splhigh (); 1907150622Srik CX_LOCK (bd); 1908150622Srik *(int*)data = c->debug; 1909150622Srik CX_UNLOCK (bd); 1910150622Srik splx (s); 1911150622Srik return 0; 1912150622Srik 1913150622Srik case SERIAL_SETDEBUG: 1914150622Srik CX_DEBUG2 (d, ("ioctl: setdebug\n")); 1915150622Srik /* Only for superuser! */ 1916150622Srik error = suser (td); 1917150622Srik if (error) 1918150622Srik return error; 1919150622Srik s = splhigh (); 1920150622Srik CX_LOCK (bd); 1921150622Srik c->debug = *(int*)data; 1922150622Srik CX_UNLOCK (bd); 1923150622Srik splx (s); 1924150622Srik#ifndef NETGRAPH 1925150622Srik if (d->chan->debug) 1926150622Srik d->ifp->if_flags |= IFF_DEBUG; 1927150622Srik else 1928150622Srik d->ifp->if_flags &= (~IFF_DEBUG); 1929150622Srik#endif 1930150622Srik return 0; 1931150622Srik } 1932150622Srik 1933150622Srik switch (cmd) { 1934150622Srik case TIOCSDTR: /* Set DTR */ 1935150622Srik CX_DEBUG2 (d, ("ioctl: tiocsdtr\n")); 1936150622Srik s = splhigh (); 1937150622Srik CX_LOCK (bd); 1938150622Srik cx_set_dtr (c, 1); 1939150622Srik CX_UNLOCK (bd); 1940150622Srik splx (s); 1941150622Srik return 0; 1942150622Srik 1943150622Srik case TIOCCDTR: /* Clear DTR */ 1944150622Srik CX_DEBUG2 (d, ("ioctl: tioccdtr\n")); 1945150622Srik s = splhigh (); 1946150622Srik CX_LOCK (bd); 1947150622Srik cx_set_dtr (c, 0); 1948150622Srik CX_UNLOCK (bd); 1949150622Srik splx (s); 1950150622Srik return 0; 1951150622Srik 1952150622Srik case TIOCMSET: /* Set DTR/RTS */ 1953150622Srik CX_DEBUG2 (d, ("ioctl: tiocmset\n")); 1954150622Srik s = splhigh (); 1955150622Srik CX_LOCK (bd); 1956150622Srik cx_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0); 1957150622Srik cx_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0); 1958150622Srik CX_UNLOCK (bd); 1959150622Srik splx (s); 1960150622Srik return 0; 1961150622Srik 1962150622Srik case TIOCMBIS: /* Add DTR/RTS */ 1963150622Srik CX_DEBUG2 (d, ("ioctl: tiocmbis\n")); 1964150622Srik s = splhigh (); 1965150622Srik CX_LOCK (bd); 1966150622Srik if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 1); 1967150622Srik if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 1); 1968150622Srik CX_UNLOCK (bd); 1969150622Srik splx (s); 1970150622Srik return 0; 1971150622Srik 1972150622Srik case TIOCMBIC: /* Clear DTR/RTS */ 1973150622Srik CX_DEBUG2 (d, ("ioctl: tiocmbic\n")); 1974150622Srik s = splhigh (); 1975150622Srik CX_LOCK (bd); 1976150622Srik if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 0); 1977150622Srik if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 0); 1978150622Srik CX_UNLOCK (bd); 1979150622Srik splx (s); 1980150622Srik return 0; 1981150622Srik 1982150622Srik case TIOCMGET: /* Get modem status */ 1983150622Srik CX_DEBUG2 (d, ("ioctl: tiocmget\n")); 1984150622Srik *(int*)data = cx_modem_status (d); 1985150622Srik return 0; 1986150622Srik 1987150622Srik } 1988150622Srik 1989150622Srik CX_DEBUG2 (d, ("ioctl: 0x%lx\n", cmd)); 1990150622Srik return ENOTTY; 1991150622Srik} 1992150622Srik 1993123120Simpvoid cx_softintr (void *unused) 1994123120Simp{ 1995123120Simp drv_t *d; 1996138823Srik bdrv_t *bd; 1997123120Simp async_q *q; 1998123120Simp int i, s, ic, k; 1999123120Simp while (MY_SOFT_INTR) { 2000123120Simp MY_SOFT_INTR = 0; 2001123120Simp for (i=0; i<NCX*NCHAN; ++i) { 2002123120Simp d = channel [i]; 2003123120Simp if (!d || !d->chan || d->chan->type == T_NONE 2004130240Srik || d->chan->mode != M_ASYNC || !d->tty 2005130240Srik || !d->tty->t_dev) 2006123120Simp continue; 2007138823Srik bd = d->board->sys; 2008123120Simp s = splhigh (); 2009138823Srik CX_LOCK (bd); 2010123120Simp if (d->intr_action & CX_READ) { 2011123120Simp q = &(d->aqueue); 2012130240Srik if (d->tty->t_state & TS_CAN_BYPASS_L_RINT) { 2013123120Simp k = AQ_GSZ(q); 2014130240Srik if (d->tty->t_rawq.c_cc + k > 2015130240Srik d->tty->t_ihiwat 2016130240Srik && (d->tty->t_cflag & CRTS_IFLOW 2017130240Srik || d->tty->t_iflag & IXOFF) 2018130240Srik && !(d->tty->t_state & TS_TBLOCK)) 2019130240Srik ttyblock(d->tty); 2020130240Srik d->tty->t_rawcc += k; 2021123120Simp while (k>0) { 2022123120Simp k--; 2023123120Simp AQ_POP (q, ic); 2024138823Srik CX_UNLOCK (bd); 2025123120Simp splx (s); 2026130240Srik putc (ic, &d->tty->t_rawq); 2027123120Simp s = splhigh (); 2028138823Srik CX_LOCK (bd); 2029123120Simp } 2030130240Srik ttwakeup(d->tty); 2031130240Srik if (d->tty->t_state & TS_TTSTOP 2032130240Srik && (d->tty->t_iflag & IXANY 2033130240Srik || d->tty->t_cc[VSTART] == 2034130240Srik d->tty->t_cc[VSTOP])) { 2035130240Srik d->tty->t_state &= ~TS_TTSTOP; 2036130240Srik d->tty->t_lflag &= ~FLUSHO; 2037123120Simp d->intr_action |= CX_WRITE; 2038123120Simp } 2039123120Simp } else { 2040123120Simp while (q->end != q->beg) { 2041123120Simp AQ_POP (q, ic); 2042138823Srik CX_UNLOCK (bd); 2043123120Simp splx (s); 2044130240Srik ttyld_rint (d->tty, ic); 2045123120Simp s = splhigh (); 2046138823Srik CX_LOCK (bd); 2047123120Simp } 2048123120Simp } 2049123120Simp d->intr_action &= ~CX_READ; 2050123120Simp } 2051123120Simp splx (s); 2052138823Srik CX_UNLOCK (bd); 2053123120Simp 2054123120Simp s = splhigh (); 2055138823Srik CX_LOCK (bd); 2056123120Simp if (d->intr_action & CX_WRITE) { 2057130240Srik if (d->tty->t_line) 2058130240Srik ttyld_start (d->tty); 2059123120Simp else 2060130240Srik cx_oproc (d->tty); 2061123120Simp d->intr_action &= ~CX_WRITE; 2062123120Simp } 2063138823Srik CX_UNLOCK (bd); 2064123120Simp splx (s); 2065123120Simp 2066123120Simp } 2067123120Simp } 2068123120Simp} 2069123120Simp 2070123120Simp/* 2071123120Simp * Fill transmitter buffer with data. 2072123120Simp */ 2073123120Simpstatic void cx_oproc (struct tty *tp) 2074123120Simp{ 2075136480Sphk int s, k; 2076136480Sphk drv_t *d; 2077138823Srik bdrv_t *bd; 2078123120Simp static u_char buf[DMABUFSZ]; 2079123120Simp u_char *p; 2080123120Simp u_short len = 0, sublen = 0; 2081123120Simp 2082138823Srik d = tp->t_sc; 2083138823Srik bd = d->board->sys; 2084136480Sphk 2085123120Simp CX_DEBUG2 (d, ("cx_oproc\n")); 2086138823Srik 2087138823Srik s = splhigh (); 2088138823Srik CX_LOCK (bd); 2089138823Srik 2090123120Simp if (tp->t_cflag & CRTSCTS && (tp->t_state & TS_TBLOCK) && d->chan->rts) 2091123120Simp cx_set_rts (d->chan, 0); 2092123120Simp else if (tp->t_cflag & CRTSCTS && ! (tp->t_state & TS_TBLOCK) && ! d->chan->rts) 2093123120Simp cx_set_rts (d->chan, 1); 2094123120Simp 2095123120Simp if (! (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))) { 2096123120Simp /* Start transmitter. */ 2097123120Simp cx_enable_transmit (d->chan, 1); 2098123120Simp 2099123120Simp /* Is it busy? */ 2100123120Simp if (! cx_buf_free (d->chan)) { 2101123120Simp tp->t_state |= TS_BUSY; 2102138823Srik CX_UNLOCK (bd); 2103123120Simp splx (s); 2104123120Simp return; 2105123120Simp } 2106123120Simp if (tp->t_iflag & IXOFF) { 2107123120Simp p = (buf + (DMABUFSZ/2)); 2108123120Simp sublen = q_to_b (&tp->t_outq, p, (DMABUFSZ/2)); 2109123120Simp k = sublen; 2110123120Simp while (k--) { 2111123120Simp /* Send XON/XOFF out of band. */ 2112123120Simp if (*p == tp->t_cc[VSTOP]) { 2113123120Simp cx_xflow_ctl (d->chan, 0); 2114123120Simp p++; 2115123120Simp continue; 2116123120Simp } 2117123120Simp if (*p == tp->t_cc[VSTART]) { 2118123120Simp cx_xflow_ctl (d->chan, 1); 2119123120Simp p++; 2120123120Simp continue; 2121123120Simp } 2122123120Simp buf[len] = *p; 2123123120Simp len++; 2124123120Simp p++; 2125123120Simp } 2126123120Simp } else { 2127123120Simp p = buf; 2128123120Simp len = q_to_b (&tp->t_outq, p, (DMABUFSZ/2)); 2129123120Simp } 2130123120Simp if (len) { 2131123120Simp cx_send_packet (d->chan, buf, len, 0); 2132123120Simp tp->t_state |= TS_BUSY; 2133123120Simp d->atimeout = 10; 2134123120Simp CX_DEBUG2 (d, ("out %d bytes\n", len)); 2135123120Simp } 2136123120Simp } 2137123120Simp ttwwakeup (tp); 2138138823Srik CX_UNLOCK (bd); 2139123120Simp splx (s); 2140123120Simp} 2141123120Simp 2142123120Simpstatic int cx_param (struct tty *tp, struct termios *t) 2143123120Simp{ 2144136480Sphk drv_t *d; 2145138823Srik bdrv_t *bd; 2146123120Simp int s, bits, parity; 2147123120Simp 2148136480Sphk d = tp->t_sc; 2149138823Srik bd = d->board->sys; 2150138823Srik 2151123120Simp s = splhigh (); 2152138823Srik CX_LOCK (bd); 2153123120Simp if (t->c_ospeed == 0) { 2154123120Simp /* Clear DTR and RTS. */ 2155123120Simp cx_set_dtr (d->chan, 0); 2156138823Srik CX_UNLOCK (bd); 2157123120Simp splx (s); 2158123120Simp CX_DEBUG2 (d, ("cx_param (hangup)\n")); 2159123120Simp return 0; 2160123120Simp } 2161123120Simp CX_DEBUG2 (d, ("cx_param\n")); 2162123120Simp 2163123120Simp /* Check requested parameters. */ 2164123120Simp if (t->c_ospeed < 300 || t->c_ospeed > 256*1024) { 2165138823Srik CX_UNLOCK (bd); 2166123120Simp splx (s); 2167133644Srik return EINVAL; 2168123120Simp } 2169123120Simp if (t->c_ispeed && (t->c_ispeed < 300 || t->c_ispeed > 256*1024)) { 2170138823Srik CX_UNLOCK (bd); 2171123120Simp splx (s); 2172133644Srik return EINVAL; 2173123120Simp } 2174123120Simp 2175123120Simp /* And copy them to tty and channel structures. */ 2176123120Simp tp->t_ispeed = t->c_ispeed = tp->t_ospeed = t->c_ospeed; 2177123120Simp tp->t_cflag = t->c_cflag; 2178123120Simp 2179123120Simp /* Set character length and parity mode. */ 2180123120Simp switch (t->c_cflag & CSIZE) { 2181123120Simp default: 2182123120Simp case CS8: bits = 8; break; 2183123120Simp case CS7: bits = 7; break; 2184123120Simp case CS6: bits = 6; break; 2185123120Simp case CS5: bits = 5; break; 2186123120Simp } 2187123120Simp 2188123120Simp parity = ((t->c_cflag & PARENB) ? 1 : 0) * 2189123120Simp (1 + ((t->c_cflag & PARODD) ? 0 : 1)); 2190123120Simp 2191123120Simp /* Set current channel number. */ 2192123120Simp if (! d->chan->dtr) 2193123120Simp cx_set_dtr (d->chan, 1); 2194123120Simp 2195130240Srik ttyldoptim (tp); 2196123120Simp cx_set_async_param (d->chan, t->c_ospeed, bits, parity, (t->c_cflag & CSTOPB), 2197123120Simp !(t->c_cflag & PARENB), (t->c_cflag & CRTSCTS), 2198123120Simp (t->c_iflag & IXON), (t->c_iflag & IXANY), 2199123120Simp t->c_cc[VSTART], t->c_cc[VSTOP]); 2200138823Srik CX_UNLOCK (bd); 2201123120Simp splx (s); 2202123120Simp return 0; 2203123120Simp} 2204123120Simp 2205123120Simp/* 2206123120Simp * Stop output on a line 2207123120Simp */ 2208123120Simpstatic void cx_stop (struct tty *tp, int flag) 2209123120Simp{ 2210136480Sphk drv_t *d; 2211138823Srik bdrv_t *bd; 2212123120Simp int s; 2213123120Simp 2214136480Sphk d = tp->t_sc; 2215138823Srik bd = d->board->sys; 2216138823Srik 2217123120Simp s = splhigh (); 2218138823Srik CX_LOCK (bd); 2219123120Simp if (tp->t_state & TS_BUSY) { 2220123120Simp /* Stop transmitter */ 2221123120Simp CX_DEBUG2 (d, ("cx_stop\n")); 2222123120Simp cx_transmitter_ctl (d->chan, 0); 2223123120Simp } 2224138823Srik CX_UNLOCK (bd); 2225123120Simp splx (s); 2226123120Simp} 2227123120Simp 2228123120Simp/* 2229123120Simp * Process the (delayed) carrier signal setup. 2230123120Simp */ 2231123120Simpstatic void cx_carrier (void *arg) 2232123120Simp{ 2233123120Simp drv_t *d = arg; 2234138823Srik bdrv_t *bd = d->board->sys; 2235123120Simp cx_chan_t *c = d->chan; 2236123120Simp int s, cd; 2237123120Simp 2238123120Simp s = splhigh (); 2239138823Srik CX_LOCK (bd); 2240123120Simp cd = cx_get_cd (c); 2241123120Simp if (d->cd != cd) { 2242123120Simp if (cd) { 2243123120Simp CX_DEBUG (d, ("carrier on\n")); 2244123120Simp d->cd = 1; 2245138823Srik CX_UNLOCK (bd); 2246123120Simp splx (s); 2247130240Srik if (d->tty) 2248130240Srik ttyld_modem(d->tty, 1); 2249123120Simp } else { 2250123120Simp CX_DEBUG (d, ("carrier loss\n")); 2251123120Simp d->cd = 0; 2252138823Srik CX_UNLOCK (bd); 2253123120Simp splx (s); 2254130240Srik if (d->tty) 2255130240Srik ttyld_modem(d->tty, 0); 2256123120Simp } 2257147859Srik } else { 2258147859Srik CX_UNLOCK (bd); 2259147859Srik splx (s); 2260123120Simp } 2261123120Simp} 2262123120Simp 2263123120Simp/* 2264123120Simp * Modem signal callback function. 2265123120Simp */ 2266123120Simpstatic void cx_modem (cx_chan_t *c) 2267123120Simp{ 2268123120Simp drv_t *d = c->sys; 2269123120Simp 2270123120Simp if (!d || c->mode != M_ASYNC) 2271123120Simp return; 2272123120Simp /* Handle carrier detect/loss. */ 2273123120Simp /* Carrier changed - delay processing DCD for a while 2274123120Simp * to give both sides some time to initialize. */ 2275138823Srik callout_reset (&d->dcd_timeout_handle, hz/2, cx_carrier, d); 2276123120Simp} 2277123120Simp 2278123120Simp#ifdef NETGRAPH 2279123120Simpstatic int ng_cx_constructor (node_p node) 2280123120Simp{ 2281123120Simp drv_t *d = NG_NODE_PRIVATE (node); 2282123120Simp CX_DEBUG (d, ("Constructor\n")); 2283123120Simp return EINVAL; 2284123120Simp} 2285123120Simp 2286123120Simpstatic int ng_cx_newhook (node_p node, hook_p hook, const char *name) 2287123120Simp{ 2288123120Simp int s; 2289123120Simp drv_t *d = NG_NODE_PRIVATE (node); 2290138823Srik bdrv_t *bd = d->board->sys; 2291123120Simp 2292123120Simp if (d->chan->mode == M_ASYNC) 2293123120Simp return EINVAL; 2294123120Simp 2295123120Simp /* Attach debug hook */ 2296123120Simp if (strcmp (name, NG_CX_HOOK_DEBUG) == 0) { 2297123120Simp NG_HOOK_SET_PRIVATE (hook, NULL); 2298123120Simp d->debug_hook = hook; 2299123120Simp return 0; 2300123120Simp } 2301123120Simp 2302123120Simp /* Check for raw hook */ 2303123120Simp if (strcmp (name, NG_CX_HOOK_RAW) != 0) 2304123120Simp return EINVAL; 2305123120Simp 2306123120Simp NG_HOOK_SET_PRIVATE (hook, d); 2307123120Simp d->hook = hook; 2308123120Simp s = splhigh (); 2309138823Srik CX_LOCK (bd); 2310123120Simp cx_up (d); 2311138823Srik CX_UNLOCK (bd); 2312123120Simp splx (s); 2313123120Simp return 0; 2314123120Simp} 2315123120Simp 2316123120Simpstatic int print_modems (char *s, cx_chan_t *c, int need_header) 2317123120Simp{ 2318123120Simp int status = cx_modem_status (c->sys); 2319123120Simp int length = 0; 2320123120Simp 2321123120Simp if (need_header) 2322123120Simp length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n"); 2323123120Simp length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n", 2324123120Simp status & TIOCM_LE ? "On" : "-", 2325123120Simp status & TIOCM_DTR ? "On" : "-", 2326123120Simp status & TIOCM_DSR ? "On" : "-", 2327123120Simp status & TIOCM_RTS ? "On" : "-", 2328123120Simp status & TIOCM_CTS ? "On" : "-", 2329123120Simp status & TIOCM_CD ? "On" : "-"); 2330123120Simp return length; 2331123120Simp} 2332123120Simp 2333123120Simpstatic int print_stats (char *s, cx_chan_t *c, int need_header) 2334123120Simp{ 2335123120Simp int length = 0; 2336123120Simp 2337123120Simp if (need_header) 2338123120Simp length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n"); 2339123120Simp length += sprintf (s + length, "%7ld %7ld %7ld %8ld %7ld %7ld %8ld %7ld %7ld\n", 2340123120Simp c->rintr, c->tintr, c->mintr, c->ibytes, c->ipkts, 2341123120Simp c->ierrs, c->obytes, c->opkts, c->oerrs); 2342123120Simp return length; 2343123120Simp} 2344123120Simp 2345123120Simpstatic int print_chan (char *s, cx_chan_t *c) 2346123120Simp{ 2347123120Simp drv_t *d = c->sys; 2348123120Simp int length = 0; 2349123120Simp 2350123120Simp length += sprintf (s + length, "cx%d", c->board->num * NCHAN + c->num); 2351123120Simp if (d->chan->debug) 2352123120Simp length += sprintf (s + length, " debug=%d", d->chan->debug); 2353123120Simp 2354123120Simp if (cx_get_baud (c)) 2355123120Simp length += sprintf (s + length, " %ld", cx_get_baud (c)); 2356123120Simp else 2357123120Simp length += sprintf (s + length, " extclock"); 2358123120Simp 2359123120Simp if (c->mode == M_HDLC) { 2360123120Simp length += sprintf (s + length, " dpll=%s", cx_get_dpll (c) ? "on" : "off"); 2361123120Simp length += sprintf (s + length, " nrzi=%s", cx_get_nrzi (c) ? "on" : "off"); 2362123120Simp } 2363123120Simp 2364123120Simp length += sprintf (s + length, " loop=%s", cx_get_loop (c) ? "on\n" : "off\n"); 2365123120Simp return length; 2366123120Simp} 2367123120Simp 2368123120Simpstatic int ng_cx_rcvmsg (node_p node, item_p item, hook_p lasthook) 2369123120Simp{ 2370123120Simp drv_t *d = NG_NODE_PRIVATE (node); 2371123120Simp struct ng_mesg *msg; 2372123120Simp struct ng_mesg *resp = NULL; 2373123120Simp int error = 0; 2374123120Simp 2375123120Simp if (!d) 2376123120Simp return EINVAL; 2377123120Simp 2378123120Simp CX_DEBUG (d, ("Rcvmsg\n")); 2379123120Simp NGI_GET_MSG (item, msg); 2380123120Simp switch (msg->header.typecookie) { 2381123120Simp default: 2382123120Simp error = EINVAL; 2383123120Simp break; 2384123120Simp 2385123120Simp case NGM_CX_COOKIE: 2386123120Simp printf ("Don't forget to implement\n"); 2387123120Simp error = EINVAL; 2388123120Simp break; 2389123120Simp 2390123120Simp case NGM_GENERIC_COOKIE: 2391123120Simp switch (msg->header.cmd) { 2392123120Simp default: 2393123120Simp error = EINVAL; 2394123120Simp break; 2395123120Simp 2396123120Simp case NGM_TEXT_STATUS: { 2397123120Simp char *s; 2398123120Simp int l = 0; 2399123120Simp int dl = sizeof (struct ng_mesg) + 730; 2400123120Simp 2401123120Simp NG_MKRESPONSE (resp, msg, dl, M_NOWAIT); 2402123120Simp if (! resp) { 2403123120Simp error = ENOMEM; 2404123120Simp break; 2405123120Simp } 2406123120Simp bzero (resp, dl); 2407123120Simp s = (resp)->data; 2408123120Simp l += print_chan (s + l, d->chan); 2409123120Simp l += print_stats (s + l, d->chan, 1); 2410123120Simp l += print_modems (s + l, d->chan, 1); 2411123120Simp strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRLEN); 2412123120Simp } 2413123120Simp break; 2414123120Simp } 2415123120Simp break; 2416123120Simp } 2417123120Simp NG_RESPOND_MSG (error, node, item, resp); 2418123120Simp NG_FREE_MSG (msg); 2419123120Simp return error; 2420123120Simp} 2421123120Simp 2422123120Simpstatic int ng_cx_rcvdata (hook_p hook, item_p item) 2423123120Simp{ 2424123120Simp drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook)); 2425123120Simp struct mbuf *m; 2426131108Sjulian struct ng_tag_prio *ptag; 2427138823Srik bdrv_t *bd; 2428123120Simp struct ifqueue *q; 2429123120Simp int s; 2430123120Simp 2431123120Simp NGI_GET_M (item, m); 2432123120Simp NG_FREE_ITEM (item); 2433123120Simp if (! NG_HOOK_PRIVATE (hook) || ! d) { 2434123120Simp NG_FREE_M (m); 2435123120Simp return ENETDOWN; 2436123120Simp } 2437131108Sjulian 2438138823Srik bd = d->board->sys; 2439131108Sjulian /* Check for high priority data */ 2440131108Sjulian if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE, 2441131108Sjulian NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) ) 2442131108Sjulian q = &d->hi_queue; 2443131108Sjulian else 2444131108Sjulian q = &d->lo_queue; 2445131108Sjulian 2446123120Simp s = splhigh (); 2447138823Srik CX_LOCK (bd); 2448123120Simp IF_LOCK (q); 2449123120Simp if (_IF_QFULL (q)) { 2450123120Simp _IF_DROP (q); 2451123120Simp IF_UNLOCK (q); 2452138823Srik CX_UNLOCK (bd); 2453123120Simp splx (s); 2454123120Simp NG_FREE_M (m); 2455123120Simp return ENOBUFS; 2456123120Simp } 2457123120Simp _IF_ENQUEUE (q, m); 2458123120Simp IF_UNLOCK (q); 2459123120Simp cx_start (d); 2460138823Srik CX_UNLOCK (bd); 2461123120Simp splx (s); 2462123120Simp return 0; 2463123120Simp} 2464123120Simp 2465123120Simpstatic int ng_cx_rmnode (node_p node) 2466123120Simp{ 2467123120Simp drv_t *d = NG_NODE_PRIVATE (node); 2468138823Srik bdrv_t *bd; 2469123120Simp 2470123120Simp CX_DEBUG (d, ("Rmnode\n")); 2471123120Simp if (d && d->running) { 2472123120Simp int s = splhigh (); 2473138823Srik bd = d->board->sys; 2474138823Srik CX_LOCK (bd); 2475123120Simp cx_down (d); 2476138823Srik CX_UNLOCK (bd); 2477123120Simp splx (s); 2478123120Simp } 2479123120Simp#ifdef KLD_MODULE 2480132464Sjulian if (node->nd_flags & NGF_REALLY_DIE) { 2481123120Simp NG_NODE_SET_PRIVATE (node, NULL); 2482123120Simp NG_NODE_UNREF (node); 2483123120Simp } 2484132464Sjulian NG_NODE_REVIVE(node); /* Persistant node */ 2485123120Simp#endif 2486123120Simp return 0; 2487123120Simp} 2488123120Simp 2489123120Simpstatic void ng_cx_watchdog (void *arg) 2490123120Simp{ 2491123120Simp drv_t *d = arg; 2492123120Simp 2493123120Simp if (d->timeout == 1) 2494123120Simp cx_watchdog (d); 2495123120Simp if (d->timeout) 2496123120Simp d->timeout--; 2497138823Srik callout_reset (&d->timeout_handle, hz, ng_cx_watchdog, d); 2498123120Simp} 2499123120Simp 2500123120Simpstatic int ng_cx_connect (hook_p hook) 2501123120Simp{ 2502123120Simp drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook)); 2503123120Simp 2504138823Srik callout_reset (&d->timeout_handle, hz, ng_cx_watchdog, d); 2505123120Simp return 0; 2506123120Simp} 2507123120Simp 2508123120Simpstatic int ng_cx_disconnect (hook_p hook) 2509123120Simp{ 2510123120Simp drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook)); 2511138823Srik bdrv_t *bd = d->board->sys; 2512123120Simp int s; 2513123120Simp 2514123120Simp s = splhigh (); 2515138823Srik CX_LOCK (bd); 2516123120Simp if (NG_HOOK_PRIVATE (hook)) 2517123120Simp cx_down (d); 2518138823Srik CX_UNLOCK (bd); 2519123120Simp splx (s); 2520138823Srik /* If we were wait it than it reasserted now, just stop it. */ 2521138823Srik if (!callout_drain (&d->timeout_handle)) 2522138823Srik callout_stop (&d->timeout_handle); 2523123120Simp return 0; 2524123120Simp} 2525123120Simp#endif /*NETGRAPH*/ 2526123120Simp 2527123120Simpstatic int cx_modevent (module_t mod, int type, void *unused) 2528123120Simp{ 2529123120Simp static int load_count = 0; 2530123120Simp 2531138823Srik if (!debug_mpsafenet && cx_mpsafenet) { 2532138823Srik printf ("WORNING! Network stack is not MPSAFE. " 2533138823Srik "Turning off debug.cx.mpsafenet.\n"); 2534138823Srik cx_mpsafenet = 0; 2535138823Srik } 2536138823Srik if (cx_mpsafenet) 2537138823Srik cx_cdevsw.d_flags &= ~D_NEEDGIANT; 2538138823Srik 2539123120Simp switch (type) { 2540123120Simp case MOD_LOAD: 2541138823Srik#ifdef NETGRAPH 2542123120Simp if (ng_newtype (&typestruct)) 2543123120Simp printf ("Failed to register ng_cx\n"); 2544123120Simp#endif 2545123120Simp ++load_count; 2546138823Srik 2547138823Srik callout_init (&timeout_handle, cx_mpsafenet?CALLOUT_MPSAFE:0); 2548138823Srik callout_reset (&timeout_handle, hz*5, cx_timeout, 0); 2549123120Simp /* Software interrupt. */ 2550151690Sru swi_add(&tty_intr_event, "cx", cx_softintr, NULL, SWI_TTY, 2551138823Srik (cx_mpsafenet?INTR_MPSAFE:0), &cx_fast_ih); 2552123120Simp break; 2553123120Simp case MOD_UNLOAD: 2554123120Simp if (load_count == 1) { 2555123120Simp printf ("Removing device entry for Sigma\n"); 2556138823Srik#ifdef NETGRAPH 2557123120Simp ng_rmtype (&typestruct); 2558123120Simp#endif 2559123120Simp } 2560138823Srik /* If we were wait it than it reasserted now, just stop it. */ 2561138823Srik if (!callout_drain (&timeout_handle)) 2562138823Srik callout_stop (&timeout_handle); 2563151700Sjhb swi_remove (cx_fast_ih); 2564123120Simp --load_count; 2565123120Simp break; 2566123120Simp case MOD_SHUTDOWN: 2567123120Simp break; 2568123120Simp } 2569123120Simp return 0; 2570123120Simp} 2571123120Simp 2572123120Simp#ifdef NETGRAPH 2573123120Simpstatic struct ng_type typestruct = { 2574129837Srik .version = NG_ABI_VERSION, 2575129837Srik .name = NG_CX_NODE_TYPE, 2576129837Srik .constructor = ng_cx_constructor, 2577129837Srik .rcvmsg = ng_cx_rcvmsg, 2578129837Srik .shutdown = ng_cx_rmnode, 2579129837Srik .newhook = ng_cx_newhook, 2580129837Srik .connect = ng_cx_connect, 2581129837Srik .rcvdata = ng_cx_rcvdata, 2582130985Srik .disconnect = ng_cx_disconnect, 2583123120Simp}; 2584123120Simp#endif /*NETGRAPH*/ 2585123120Simp 2586123120Simp#ifdef NETGRAPH 2587123120SimpMODULE_DEPEND (ng_cx, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION); 2588123120Simp#else 2589123120SimpMODULE_DEPEND (isa_cx, sppp, 1, 1, 1); 2590123120Simp#endif 2591123120SimpDRIVER_MODULE (cx, isa, cx_isa_driver, cx_devclass, cx_modevent, NULL); 2592138823SrikMODULE_VERSION (cx, 1); 2593