⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 if_cx.c

📁 基于组件方式开发操作系统的OSKIT源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Cronyx-Sigma adapter driver for FreeBSD. * Supports PPP/HDLC and Cisco/HDLC protocol in synchronous mode, * and asyncronous channels with full modem control. * Keepalive protocol implemented in both Cisco and PPP modes. * * Copyright (C) 1994 Cronyx Ltd. * Author: Serge Vakulenko, <vak@zebub.msk.su> * * This software is distributed with NO WARRANTIES, not even the implied * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * Authors grant any other persons or organisations permission to use * or modify this software as long as this message is kept with the software, * all derivative works or modified versions. * * Version 1.9, Wed Oct  4 18:58:15 MSK 1995 */#undef DEBUG#include "cx.h"#include "bpfilter.h"#include "opt_devfs.h"#include "sppp.h"#if NSPPP <= 0#error The device 'cx' requires sppp.#endif#include <sys/param.h>#include <sys/systm.h>#include <sys/kernel.h>#include <sys/malloc.h>#include <sys/mbuf.h>#include <sys/sockio.h>#include <sys/socket.h>#include <sys/conf.h>#include <net/if.h>#if NBPFILTER > 0#include <net/bpf.h>#endif#include <i386/isa/isa_device.h>#ifdef DEVFSextern struct cdevsw cx_cdevsw;#include <sys/devfsext.h>#endif /*DEVFS*/#define watchdog_func_t void(*)(struct ifnet *)#define start_func_t    void(*)(struct ifnet*)#include <net/if_sppp.h>#include <machine/cronyx.h>#include <i386/isa/cxreg.h>/* XXX exported. */void cxswitch (cx_chan_t *c, cx_soft_opt_t new);static int cxprobe __P((struct isa_device *id));static int cxattach __P((struct isa_device *id));static void cxput __P((cx_chan_t *c, char b));static void cxsend __P((cx_chan_t *c));static void cxrinth __P((cx_chan_t *c));static ointhand2_t cxintr;static int cxtinth __P((cx_chan_t *c));#ifdef DEBUG#   define print(s)     printf s#else#   define print(s)     {/*void*/}#endif#define TXTIMEOUT       10              /* transmit timeout in seconds */#define DMABUFSZ        (6*256)         /* buffer size */#define PPP_HEADER_LEN  4               /* size of PPP header *//* * Under BSDI it's possible to use general p2p protocol scheme, * as well as our own one.  Switching is done via IFF_ALTPHYS flag. * Our ifnet pointer holds the buffer large enough to contain * any of sppp and p2p structures. */#define IFSTRUCTSZ   (sizeof (struct sppp))#define IFNETSZ         (sizeof (struct ifnet))static int cxsioctl (struct ifnet *ifp, u_long cmd, caddr_t data);static void cxstart (struct ifnet *ifp);static void cxwatchdog (struct ifnet *ifp);static void cxinput (cx_chan_t *c, void *buf, unsigned len);extern int cxrinta (cx_chan_t *c);extern void cxtinta (cx_chan_t *c);extern void cxmint (cx_chan_t *c);extern timeout_t cxtimeout;static void cxdown (cx_chan_t *c);static void cxup (cx_chan_t *c);cx_board_t cxboard [NCX];           /* adapter state structures */cx_chan_t *cxchan [NCX*NCHAN];      /* unit to channel struct pointer */static unsigned short irq_valid_values [] = { 3, 5, 7, 10, 11, 12, 15, 0 };static unsigned short drq_valid_values [] = { 5, 6, 7, 0 };static unsigned short port_valid_values [] = {	0x240, 0x260, 0x280, 0x300, 0x320, 0x380, 0x3a0, 0,};/* * Check that the value is contained in the list of correct values. */static int valid (unsigned short value, unsigned short *list){	while (*list)		if (value == *list++)			return (1);	return (0);}/* * Print the mbuf chain, for debug purposes only. */static void printmbuf (struct mbuf *m){	printf ("mbuf:");	for (; m; m=m->m_next) {		if (m->m_flags & M_PKTHDR)			printf (" HDR %d:", m->m_pkthdr.len);		if (m->m_flags & M_EXT)			printf (" EXT:");		printf (" %d", m->m_len);	}	printf ("\n");}/* * Make an mbuf from data. */static struct mbuf *makembuf (void *buf, unsigned len){	struct mbuf *m, *o, *p;	MGETHDR (m, M_DONTWAIT, MT_DATA);	if (! m)		return (0);	if (len >= MINCLSIZE)		MCLGET (m, M_DONTWAIT);	m->m_pkthdr.len = len;	m->m_len = 0;	p = m;	while (len) {		unsigned n = M_TRAILINGSPACE (p);		if (n > len)			n = len;		if (! n) {			/* Allocate new mbuf. */			o = p;			MGET (p, M_DONTWAIT, MT_DATA);			if (! p) {				m_freem (m);				return (0);			}			if (len >= MINCLSIZE)				MCLGET (p, M_DONTWAIT);			p->m_len = 0;			o->m_next = p;			n = M_TRAILINGSPACE (p);			if (n > len)				n = len;		}		bcopy (buf, mtod (p, caddr_t) + p->m_len, n);		p->m_len += n;		buf = (char *)buf + n;		len -= n;	}	return (m);}/* * Test the presence of the adapter on the given i/o port. */static intcxprobe (struct isa_device *id){	int unit = id->id_unit;	int iobase = id->id_iobase;	int irq = id->id_irq;	int drq = id->id_drq;	int irqnum;	irqnum = ffs (irq) - 1;	print (("cx%d: probe iobase=0x%x irq=%d drq=%d\n",		unit, iobase, irqnum, drq));	if (! valid (irqnum, irq_valid_values)) {		printf ("cx%d: Incorrect IRQ: %d\n", unit, irqnum);		return (0);	}	if (! valid (iobase, port_valid_values)) {		printf ("cx%d: Incorrect port address: 0x%x\n", unit, iobase);		return (0);	}	if (! valid (drq, drq_valid_values)) {		printf ("cx%d: Incorrect DMA channel: %d\n", unit, drq);		return (0);	}	if (! cx_probe_board (iobase))		return (0);	return (1);}/* * The adapter is present, initialize the driver structures. */#ifdef DEVFSstatic void *cx_devfs_token;#endif static intcxattach (struct isa_device *id){	int unit = id->id_unit;	int iobase = id->id_iobase;	int irq = id->id_irq;	int drq = id->id_drq;	cx_board_t *b = cxboard + unit;	int i;	struct sppp *sp;	id->id_ointr = cxintr;	/* Initialize the board structure. */	cx_init (b, unit, iobase, ffs(irq)-1, drq);	for (i=0; i<NCHAN; ++i) {		cx_chan_t *c = b->chan + i;		int u = b->num*NCHAN + i;		cxchan[u] = c;		if (c->type == T_NONE)			continue;		/* Allocate the buffer memory. */		c->arbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);		c->brbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);		c->atbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);		c->btbuf = malloc (DMABUFSZ, M_DEVBUF, M_NOWAIT);		/* All buffers should be located in lower 16M of memory! */		if (!c->arbuf || !c->brbuf || !c->atbuf || !c->btbuf) {			printf ("cx%d.%d: No memory for channel buffers\n",				c->board->num, c->num);			c->type = T_NONE;		}		switch (c->type) {		case T_SYNC_RS232:		case T_SYNC_V35:		case T_SYNC_RS449:		case T_UNIV_RS232:		case T_UNIV_RS449:		case T_UNIV_V35:			c->ifp = malloc (IFSTRUCTSZ, M_DEVBUF, M_NOWAIT);			if (! c->ifp) {				printf ("cx%d.%d: No memory for ifnet buffer\n",					c->board->num, c->num);				c->type = T_NONE;				continue;			}			bzero (c->ifp, IFSTRUCTSZ);			c->master = c->ifp;			c->ifp->if_softc = c;			c->ifp->if_unit = u;			c->ifp->if_name = "cx";			c->ifp->if_mtu = PP_MTU;			c->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;			c->ifp->if_ioctl = cxsioctl;			c->ifp->if_start = (start_func_t) cxstart;			c->ifp->if_watchdog = (watchdog_func_t) cxwatchdog;			/* Init routine is never called by upper level? */			sppp_attach (c->ifp);			if_attach (c->ifp);			sp = (struct sppp*) c->ifp;#if NBPFILTER > 0			/* If BPF is in the kernel, call the attach for it. */			bpfattach (c->ifp, DLT_PPP, PPP_HEADER_LEN);#endif		}	}	/* Reset the adapter. */	cx_setup_board (b);	/* Activate the timeout routine. */	if (unit == 0)		timeout (cxtimeout, 0, hz*5);	printf ("cx%d: <Cronyx-%s>\n", unit, b->name);#ifdef DEVFS	cx_devfs_token =		devfs_add_devswf(&cx_cdevsw, 0, DV_CHR, 0, 0, 0600, "cx");#endif	return (1);}struct isa_driver cxdriver = { cxprobe, cxattach, "cx" };/* * Process an ioctl request. */static intcxsioctl (struct ifnet *ifp, u_long cmd, caddr_t data){	cx_chan_t *q, *c = ifp->if_softc;	int error, s, was_up, should_be_up;	/*	 * No socket ioctls while the channel is in async mode.	 */	if (c->type==T_NONE || c->mode==M_ASYNC)		return (EINVAL);	/*	 * Socket ioctls on slave subchannels are not allowed.	 */	if (c->master != c->ifp)		return (EBUSY);	was_up = (ifp->if_flags & IFF_RUNNING) != 0;	error = sppp_ioctl (ifp, cmd, data);	if (error)		return (error);	print (("cxioctl (%d.%d, ", c->board->num, c->num));	switch (cmd) {	default:		print (("0x%x)\n", cmd));		return (0);	case SIOCADDMULTI:		print (("SIOCADDMULTI)\n"));		return (0);	case SIOCDELMULTI:		print (("SIOCDELMULTI)\n"));		return (0);	case SIOCSIFFLAGS:		print (("SIOCSIFFLAGS)\n"));		break;	case SIOCSIFADDR:		print (("SIOCSIFADDR)\n"));		break;	}	/* We get here only in case of SIFFLAGS or SIFADDR. */	s = splimp ();	should_be_up = (ifp->if_flags & IFF_RUNNING) != 0;	if (!was_up && should_be_up) {		/* Interface goes up -- start it. */		cxup (c);		/* Start all slave subchannels. */		for (q=c->slaveq; q; q=q->slaveq)			cxup (q);		cxstart (c->ifp);	} else if (was_up && !should_be_up) {		/* Interface is going down -- stop it. */		cxdown (c);		/* Stop all slave subchannels. */		for (q=c->slaveq; q; q=q->slaveq)			cxdown (q);		/* Flush the interface output queue */		if (! c->sopt.ext)			sppp_flush (c->ifp);	}	splx (s);	return (0);}/* * Stop the interface.  Called on splimp(). */static voidcxdown (cx_chan_t *c){	unsigned short port = c->chip->port;	print (("cx%d.%d: cxdown\n", c->board->num, c->num));	/* The interface is down, stop it */	c->ifp->if_flags &= ~IFF_OACTIVE;	/* Reset the channel (for sync modes only) */		outb (CAR(port), c->num & 3);		outb (STCR(port), STC_ABORTTX | STC_SNDSPC);	cx_setup_chan (c);}/* * Start the interface.  Called on splimp(). */static voidcxup (cx_chan_t *c){	unsigned short port = c->chip->port;		/* The interface is up, start it */	        print (("cx%d.%d: cxup\n", c->board->num, c->num));		/* Initialize channel, enable receiver and transmitter */		cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);		/* Repeat the command, to avoid the rev.H bug */		cx_cmd (port, CCR_INITCH | CCR_ENRX | CCR_ENTX);		/* Start receiver */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -