File: /usr/src/linux/drivers/net/wan/sealevel.c

1     #define LINUX_21
2     
3     /*
4      *	Sealevel Systems 4021 driver.
5      *
6      *	This program is free software; you can redistribute it and/or
7      *	modify it under the terms of the GNU General Public License
8      *	as published by the Free Software Foundation; either version
9      *	2 of the License, or (at your option) any later version.
10      *
11      *	(c) Copyright 1999 Building Number Three Ltd
12      *	(c) Copyright 2001 Alan Cox.
13      *
14      */
15     
16     #include <linux/module.h>
17     #include <linux/kernel.h>
18     #include <linux/mm.h>
19     #include <linux/net.h>
20     #include <linux/skbuff.h>
21     #include <linux/netdevice.h>
22     #include <linux/if_arp.h>
23     #include <linux/delay.h>
24     #include <linux/ioport.h>
25     #include <net/arp.h>
26     
27     #include <asm/io.h>
28     #include <asm/dma.h>
29     #include <asm/byteorder.h>
30     #include <net/syncppp.h>
31     #include "z85230.h"
32     
33     
34     struct slvl_device
35     {
36     	void *if_ptr;	/* General purpose pointer (used by SPPP) */
37     	struct z8530_channel *chan;
38     	struct ppp_device netdev;
39     	int channel;
40     };
41     
42     
43     struct slvl_board
44     {
45     	struct slvl_device dev[2];
46     	struct z8530_dev board;
47     	int iobase;
48     };
49     
50     /*
51      *	Network driver support routines
52      */
53     
54     /*
55      *	Frame receive. Simple for our card as we do sync ppp and there
56      *	is no funny garbage involved
57      */
58      
59     static void sealevel_input(struct z8530_channel *c, struct sk_buff *skb)
60     {
61     	/* Drop the CRC - its not a good idea to try and negotiate it ;) */
62     	skb_trim(skb, skb->len-2);
63     	skb->protocol=htons(ETH_P_WAN_PPP);
64     	skb->mac.raw=skb->data;
65     	skb->dev=c->netdevice;
66     	/*
67     	 *	Send it to the PPP layer. We dont have time to process
68     	 *	it right now.
69     	 */
70     	netif_rx(skb);
71     	c->netdevice->last_rx = jiffies;
72     }
73      
74     /*
75      *	We've been placed in the UP state
76      */ 
77      
78     static int sealevel_open(struct net_device *d)
79     {
80     	struct slvl_device *slvl=d->priv;
81     	int err = -1;
82     	int unit = slvl->channel;
83     	
84     	/*
85     	 *	Link layer up. 
86     	 */
87     
88     	switch(unit)
89     	{
90     		case 0:
91     			err=z8530_sync_dma_open(d, slvl->chan);
92     			break;
93     		case 1:
94     			err=z8530_sync_open(d, slvl->chan);
95     			break;
96     	}
97     	
98     	if(err)
99     		return err;
100     	/*
101     	 *	Begin PPP
102     	 */
103     	err=sppp_open(d);
104     	if(err)
105     	{
106     		switch(unit)
107     		{
108     			case 0:
109     				z8530_sync_dma_close(d, slvl->chan);
110     				break;
111     			case 1:
112     				z8530_sync_close(d, slvl->chan);
113     				break;
114     		}				
115     		return err;
116     	}
117     	
118     	slvl->chan->rx_function=sealevel_input;
119     	
120     	/*
121     	 *	Go go go
122     	 */
123     	netif_start_queue(d);
124     	MOD_INC_USE_COUNT;
125     	return 0;
126     }
127     
128     static int sealevel_close(struct net_device *d)
129     {
130     	struct slvl_device *slvl=d->priv;
131     	int unit = slvl->channel;
132     	
133     	/*
134     	 *	Discard new frames
135     	 */
136     	
137     	slvl->chan->rx_function=z8530_null_rx;
138     		
139     	/*
140     	 *	PPP off
141     	 */
142     	sppp_close(d);
143     	/*
144     	 *	Link layer down
145     	 */
146     
147     	netif_stop_queue(d);
148     		
149     	switch(unit)
150     	{
151     		case 0:
152     			z8530_sync_dma_close(d, slvl->chan);
153     			break;
154     		case 1:
155     			z8530_sync_close(d, slvl->chan);
156     			break;
157     	}
158     	MOD_DEC_USE_COUNT;
159     	return 0;
160     }
161     
162     static int sealevel_ioctl(struct net_device *d, struct ifreq *ifr, int cmd)
163     {
164     	/* struct slvl_device *slvl=d->priv;
165     	   z8530_ioctl(d,&slvl->sync.chanA,ifr,cmd) */
166     	return sppp_do_ioctl(d, ifr,cmd);
167     }
168     
169     static struct net_device_stats *sealevel_get_stats(struct net_device *d)
170     {
171     	struct slvl_device *slvl=d->priv;
172     	if(slvl)
173     		return z8530_get_stats(slvl->chan);
174     	else
175     		return NULL;
176     }
177     
178     /*
179      *	Passed PPP frames, fire them downwind.
180      */
181      
182     static int sealevel_queue_xmit(struct sk_buff *skb, struct net_device *d)
183     {
184     	struct slvl_device *slvl=d->priv;
185     	return z8530_queue_xmit(slvl->chan, skb);
186     }
187     
188     #ifdef LINUX_21
189     static int sealevel_neigh_setup(struct neighbour *n)
190     {
191     	if (n->nud_state == NUD_NONE) {
192     		n->ops = &arp_broken_ops;
193     		n->output = n->ops->output;
194     	}
195     	return 0;
196     }
197     
198     static int sealevel_neigh_setup_dev(struct net_device *dev, struct neigh_parms *p)
199     {
200     	if (p->tbl->family == AF_INET) {
201     		p->neigh_setup = sealevel_neigh_setup;
202     		p->ucast_probes = 0;
203     		p->mcast_probes = 0;
204     	}
205     	return 0;
206     }
207     
208     #else
209     
210     static int return_0(struct net_device *d)
211     {
212     	return 0;
213     }
214     
215     #endif
216     
217     /*
218      *	Description block for a Comtrol Hostess SV11 card
219      */
220      
221     static struct slvl_board *slvl_init(int iobase, int irq, int txdma, int rxdma, int slow)
222     {
223     	struct z8530_dev *dev;
224     	struct slvl_device *sv;
225     	struct slvl_board *b;
226     	
227     	unsigned long flags;
228     	int u;
229     	
230     	/*
231     	 *	Get the needed I/O space
232     	 */
233     	 
234     	if(check_region(iobase, 8))
235     	{	
236     		printk(KERN_WARNING "sealevel: I/O 0x%X already in use.\n", iobase);
237     		return NULL;
238     	}
239     	request_region(iobase, 8, "Sealevel 4021");
240     	
241     	b=(struct slvl_board *)kmalloc(sizeof(struct slvl_board), GFP_KERNEL);
242     	if(!b)
243     		goto fail3;
244     			
245     	memset(b, 0, sizeof(*sv));
246     
247     	b->dev[0].chan = &b->board.chanA;	
248     	b->dev[0].if_ptr = &b->dev[0].netdev;
249     	b->dev[0].netdev.dev=(struct net_device *)
250     		kmalloc(sizeof(struct net_device), GFP_KERNEL);
251     	if(!b->dev[0].netdev.dev)
252     		goto fail2;
253     
254     	b->dev[1].chan = &b->board.chanB;
255     	b->dev[1].if_ptr = &b->dev[1].netdev;
256     	b->dev[1].netdev.dev=(struct net_device *)
257     		kmalloc(sizeof(struct net_device), GFP_KERNEL);
258     	if(!b->dev[1].netdev.dev)
259     		goto fail1_0;
260     
261     	dev=&b->board;
262     	
263     	/*
264     	 *	Stuff in the I/O addressing
265     	 */
266     	 
267     	dev->active = 0;
268     
269     	b->iobase = iobase;
270     	
271     	/*
272     	 *	Select 8530 delays for the old board
273     	 */
274     	 
275     	if(slow)
276     		iobase |= Z8530_PORT_SLEEP;
277     		
278     	dev->chanA.ctrlio=iobase+1;
279     	dev->chanA.dataio=iobase;
280     	dev->chanB.ctrlio=iobase+3;
281     	dev->chanB.dataio=iobase+2;
282     	
283     	dev->chanA.irqs=&z8530_nop;
284     	dev->chanB.irqs=&z8530_nop;
285     	
286     	/*
287     	 *	Assert DTR enable DMA
288     	 */
289     	 
290     	outb(3|(1<<7), b->iobase+4);	
291     	
292     
293     	/* We want a fast IRQ for this device. Actually we'd like an even faster
294     	   IRQ ;) - This is one driver RtLinux is made for */
295        
296     	if(request_irq(irq, &z8530_interrupt, SA_INTERRUPT, "SeaLevel", dev)<0)
297     	{
298     		printk(KERN_WARNING "sealevel: IRQ %d already in use.\n", irq);
299     		goto fail1_1;
300     	}
301     	
302     	dev->irq=irq;
303     	dev->chanA.private=&b->dev[0];
304     	dev->chanB.private=&b->dev[1];
305     	dev->chanA.netdevice=b->dev[0].netdev.dev;
306     	dev->chanB.netdevice=b->dev[1].netdev.dev;
307     	dev->chanA.dev=dev;
308     	dev->chanB.dev=dev;
309     
310     	dev->chanA.txdma=3;
311     	dev->chanA.rxdma=1;
312     	if(request_dma(dev->chanA.txdma, "SeaLevel (TX)")!=0)
313     		goto fail;
314     		
315     	if(request_dma(dev->chanA.rxdma, "SeaLevel (RX)")!=0)
316     		goto dmafail;
317     	
318     	save_flags(flags);
319     	cli();
320     	
321     	/*
322     	 *	Begin normal initialise
323     	 */
324     	 
325     	if(z8530_init(dev)!=0)
326     	{
327     		printk(KERN_ERR "Z8530 series device not found.\n");
328     		restore_flags(flags);
329     		goto dmafail2;
330     	}
331     	if(dev->type==Z85C30)
332     	{
333     		z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream);
334     		z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream);
335     	}
336     	else
337     	{
338     		z8530_channel_load(&dev->chanA, z8530_hdlc_kilostream_85230);
339     		z8530_channel_load(&dev->chanB, z8530_hdlc_kilostream_85230);
340     	}
341     
342     	/*
343     	 *	Now we can take the IRQ
344     	 */
345     	
346     	restore_flags(flags);
347     
348     	for(u=0; u<2; u++)
349     	{
350     		sv=&b->dev[u];
351     		sv->channel = u;
352     	
353     		if(dev_alloc_name(sv->chan->netdevice,"hdlc%d")>=0)
354     		{
355     			struct net_device *d=sv->chan->netdevice;
356     
357     			/* 
358     			 *	Initialise the PPP components
359     			 */
360     			sppp_attach(&sv->netdev);
361     		
362     			/*
363     			 *	Local fields
364     			 */	
365     			
366     			d->base_addr = iobase;
367     			d->irq = irq;
368     			d->priv = sv;
369     			d->init = NULL;
370     		
371     			d->open = sealevel_open;
372     			d->stop = sealevel_close;
373     			d->hard_start_xmit = sealevel_queue_xmit;
374     			d->get_stats = sealevel_get_stats;
375     			d->set_multicast_list = NULL;
376     			d->do_ioctl = sealevel_ioctl;
377     #ifdef LINUX_21			
378     			d->neigh_setup = sealevel_neigh_setup_dev;
379     #else
380     			d->init = return_0;
381     #endif
382     			d->set_mac_address = NULL;
383     		
384     			if(register_netdev(d)==-1)
385     			{
386     				printk(KERN_ERR "%s: unable to register device.\n",
387     					d->name);
388     				goto fail_unit;
389     			}				
390     
391     			break;
392     		}
393     	}
394     	z8530_describe(dev, "I/O", iobase);
395     	dev->active=1;
396     	return b;
397     
398     fail_unit:
399     	if(u==1)
400     		unregister_netdev(b->dev[0].chan->netdevice);
401     	
402     dmafail2:
403     	free_dma(dev->chanA.rxdma);
404     dmafail:
405     	free_dma(dev->chanA.txdma);
406     fail:
407     	free_irq(irq, dev);
408     fail1_1:
409     	kfree(b->dev[1].netdev.dev);
410     fail1_0:
411     	kfree(b->dev[0].netdev.dev);
412     fail2:
413     	kfree(b);
414     fail3:
415     	release_region(iobase,8);
416     	return NULL;
417     }
418     
419     static void slvl_shutdown(struct slvl_board *b)
420     {
421     	int u;
422     
423     	z8530_shutdown(&b->board);
424     	
425     	for(u=0; u<2; u++)
426     	{
427     		sppp_detach(b->dev[u].netdev.dev);
428     		unregister_netdev(b->dev[u].netdev.dev);
429     	}
430     	
431     	free_irq(b->board.irq, &b->board);
432     	free_dma(b->board.chanA.rxdma);
433     	free_dma(b->board.chanA.txdma);
434     	/* DMA off on the card, drop DTR */
435     	outb(0, b->iobase);
436     	release_region(b->iobase, 8);
437     }
438     
439     #ifdef MODULE
440     
441     static int io=0x238;
442     static int txdma=1;
443     static int rxdma=3;
444     static int irq=5;
445     static int slow=0;
446     
447     #ifdef LINUX_21
448     MODULE_PARM(io,"i");
449     MODULE_PARM_DESC(io, "The I/O base of the Sealevel card");
450     MODULE_PARM(txdma,"i");
451     MODULE_PARM_DESC(txdma, "Transmit DMA channel");
452     MODULE_PARM(rxdma,"i");
453     MODULE_PARM_DESC(rxdma, "Receive DMA channel");
454     MODULE_PARM(irq,"i");
455     MODULE_PARM_DESC(irq, "The interrupt line setting for the SeaLevel card");
456     MODULE_PARM(slow,"i");
457     MODULE_PARM_DESC(slow, "Set this for an older Sealevel card such as the 4012");
458     
459     MODULE_AUTHOR("Alan Cox");
460     MODULE_LICENSE("GPL");
461     MODULE_DESCRIPTION("Modular driver for the SeaLevel 4021");
462     #endif
463     
464     static struct slvl_board *slvl_unit;
465     
466     int init_module(void)
467     {
468     	printk(KERN_INFO "SeaLevel Z85230 Synchronous Driver v 0.01.\n");
469     	printk(KERN_INFO "(c) Copyright 1998, Building Number Three Ltd.\n");	
470     	if((slvl_unit=slvl_init(io,irq, txdma, rxdma, slow))==NULL)
471     		return -ENODEV;
472     	return 0;
473     }
474     
475     void cleanup_module(void)
476     {
477     	if(slvl_unit)
478     		slvl_shutdown(slvl_unit);
479     }
480     
481     #endif
482     
483