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