Import 2.3.52pre1
[davej-history.git] / drivers / net / wan / sbni.c
blobbe665f0aee5bf9a4eb1340006ebdcff6f877a0b2
1 /*
2 * Driver for Granch SBNI-12 leased line network adapters.
3 *
4 * Copyright 1997 - 1999, Granch ltd.
5 * Written 1999 by Yaroslav Polyakov (xenon@granch.ru).
7 * This software may be used and distributed according to the terms
8 * of the GNU Public License, incorporated herein by reference.
9 *
10 * // Whole developers team:
11 * // Yaroslav Polyakov (xenon@granch.ru)
12 * // - main developer of this version
13 * // Alexey Zverev (zverev@granch.ru)
14 * // - previous SBNI driver for linux
15 * // Alexey Chirkov (chirkov@granch.ru)
16 * // - all the hardware work and consulting
17 * // Max Khon (max@iclub.nsu.ru)
18 * // - first SBNI driver for linux
19 * // --------------------------------------------
20 * // also I thank:
21 * // Max Krasnyansky (max@uznet.net)
22 * // - for bug hunting and many ideas
23 * // Alan Cox (Alan.Cox@linux.org)
24 * // - for consulting in some hardcore questions
25 * // Donald Becker (becker@cesdis.gsfc.nasa.gov)
26 * // - for pretty nice skeleton
28 * More info and useful utilities to work w/ SBNI you can find at
29 * http://www.granch.ru.
31 * 3.0.0 = Initial Revision, Yaroslav Polyakov (24 Feb 1999)
32 * - added pre-calculation for CRC, fixed bug with "len-2" frames,
33 * - removed outbound fragmentation (MTU=1000), written CRC-calculation
34 * - on asm, added work with hard_headers and now we have our own cache
35 * - for them, optionally supported word-interchange on some chipsets,
36 * - something else I cant remember ;)
38 * 3.0.1 = just fixed some bugs (14 apr 1999).
39 * - fixed statistical tx bug
40 * - fixed wrong creation dates (1998 -> 1999) in driver source code ;)
41 * - fixed source address bug.
42 * - fixed permanent nirvana bug
44 * 3.1.0 = (Katyusha) (26 apr 1999)
45 * - Added balancing feature
47 * 3.1.1 = (Medea) (5 aug 1999)
48 * - Fixed mac.raw bug
49 * - Thanks to tolix@olviko.ru and
50 * - to Barnaul Brewery, producers of my favorite beer "Medea".
56 #undef GOODBUS16
57 #define CRCASM
58 #define KATYUSHA
60 #include <linux/version.h>
62 #include <linux/module.h>
63 #include <linux/kernel.h>
64 #include <linux/sched.h>
65 #include <linux/ptrace.h>
66 #include <linux/fcntl.h>
67 #include <linux/ioport.h>
68 #include <linux/interrupt.h>
69 #include <linux/malloc.h>
70 #include <linux/string.h>
71 #include <linux/errno.h>
72 #include <linux/spinlock.h>
74 #include <asm/io.h>
75 #include <asm/types.h>
76 #include <asm/byteorder.h>
79 #include <linux/netdevice.h>
80 #include <linux/etherdevice.h>
81 #include <linux/skbuff.h>
82 #include <linux/timer.h>
83 #include <linux/config.h> /* for CONFIG_INET. do we need this?*/
85 #include <net/arp.h>
87 #include <asm/uaccess.h>
88 #include <linux/init.h>
90 #include "sbni.h"
93 static const char *version =
94 "sbni.c: ver. 3.1.1 Medea 5 Aug 1999 Yaroslav Polyakov (xenon@granch.ru)\n";
96 int sbni_probe(struct net_device *dev);
97 static int sbni_probe1(struct net_device *dev, int ioaddr);
98 static int sbni_open(struct net_device *dev);
99 static int sbni_start_xmit(struct sk_buff *skb, struct net_device *dev);
100 static void sbni_interrupt(int irq, void *dev_id, struct pt_regs *regs);
101 static int sbni_close(struct net_device *dev);
102 static void sbni_drop_tx_queue(struct net_device *dev);
103 static struct enet_statistics *sbni_get_stats(struct net_device *dev);
104 static void card_start(struct net_device *dev);
105 static inline unsigned short sbni_recv(struct net_device *dev);
106 void change_level(struct net_device *dev);
107 static inline void sbni_xmit(struct net_device *dev);
108 static inline void sbni_get_packet(struct net_device* dev);
109 static void sbni_watchdog(unsigned long arg);
110 static void set_multicast_list(struct net_device *dev);
111 static int sbni_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
112 static int sbni_set_mac_address(struct net_device *dev, void *addr);
113 unsigned long calc_crc(char *mem, int len, unsigned initial);
114 void sbni_nirvana(struct net_device *dev);
115 static int sbni_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
116 void *daddr, void *saddr, unsigned len);
118 static int sbni_rebuild_header(struct sk_buff *skb);
119 static int sbni_header_cache(struct neighbour *neigh, struct hh_cache *hh);
121 static inline void sbni_outs(int port, void *data, int len);
122 static inline void sbni_ins(int port, void *data, int len);
126 #define SIZE_OF_TIMEOUT_RXL_TAB 4
127 static u_char timeout_rxl_tab[] = {
128 0x03, 0x05, 0x08, 0x0b
131 static u_char rxl_tab[] = {
132 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08,
133 0x0a, 0x0c, 0x0f, 0x16, 0x18, 0x1a, 0x1c, 0x1f
136 /* A zero-terminated list of I/O addresses to be probed */
137 static unsigned int netcard_portlist[] = {
138 0x210, 0x2c0, 0x2d0, 0x2f0, 0x220, 0x230, 0x240, 0x250,
139 0x260, 0x290, 0x2a0, 0x2b0, 0x224, 0x234, 0x244, 0x254,
140 0x264, 0x294, 0x2a4, 0x2b4, 0};
142 static unsigned char magic_reply[] = {
143 0x5a,0x06,0x30,0x00,0x00,0x50,0x65,0x44,0x20
146 static int def_baud = DEF_RATE;
147 static int def_rxl = DEF_RXL_DELTA;
148 static long def_mac = 0;
152 * CRC-32 stuff
155 #define CRC32(c,crc) (crc32tab[((size_t)(crc) ^ (c)) & 0xff] ^ (((crc) >> 8) & 0x00FFFFFF))
156 /* CRC generator 0xEDB88320 */
157 /* CRC remainder 0x2144DF1C */
158 /* CRC initial value 0x00000000 */
159 #define CRC32_REMAINDER 0x2144DF1C
160 #define CRC32_INITIAL 0x00000000
162 static unsigned long crc32tab[] = {
163 0xD202EF8D, 0xA505DF1B, 0x3C0C8EA1, 0x4B0BBE37,
164 0xD56F2B94, 0xA2681B02, 0x3B614AB8, 0x4C667A2E,
165 0xDCD967BF, 0xABDE5729, 0x32D70693, 0x45D03605,
166 0xDBB4A3A6, 0xACB39330, 0x35BAC28A, 0x42BDF21C,
167 0xCFB5FFE9, 0xB8B2CF7F, 0x21BB9EC5, 0x56BCAE53,
168 0xC8D83BF0, 0xBFDF0B66, 0x26D65ADC, 0x51D16A4A,
169 0xC16E77DB, 0xB669474D, 0x2F6016F7, 0x58672661,
170 0xC603B3C2, 0xB1048354, 0x280DD2EE, 0x5F0AE278,
171 0xE96CCF45, 0x9E6BFFD3, 0x0762AE69, 0x70659EFF,
172 0xEE010B5C, 0x99063BCA, 0x000F6A70, 0x77085AE6,
173 0xE7B74777, 0x90B077E1, 0x09B9265B, 0x7EBE16CD,
174 0xE0DA836E, 0x97DDB3F8, 0x0ED4E242, 0x79D3D2D4,
175 0xF4DBDF21, 0x83DCEFB7, 0x1AD5BE0D, 0x6DD28E9B,
176 0xF3B61B38, 0x84B12BAE, 0x1DB87A14, 0x6ABF4A82,
177 0xFA005713, 0x8D076785, 0x140E363F, 0x630906A9,
178 0xFD6D930A, 0x8A6AA39C, 0x1363F226, 0x6464C2B0,
179 0xA4DEAE1D, 0xD3D99E8B, 0x4AD0CF31, 0x3DD7FFA7,
180 0xA3B36A04, 0xD4B45A92, 0x4DBD0B28, 0x3ABA3BBE,
181 0xAA05262F, 0xDD0216B9, 0x440B4703, 0x330C7795,
182 0xAD68E236, 0xDA6FD2A0, 0x4366831A, 0x3461B38C,
183 0xB969BE79, 0xCE6E8EEF, 0x5767DF55, 0x2060EFC3,
184 0xBE047A60, 0xC9034AF6, 0x500A1B4C, 0x270D2BDA,
185 0xB7B2364B, 0xC0B506DD, 0x59BC5767, 0x2EBB67F1,
186 0xB0DFF252, 0xC7D8C2C4, 0x5ED1937E, 0x29D6A3E8,
187 0x9FB08ED5, 0xE8B7BE43, 0x71BEEFF9, 0x06B9DF6F,
188 0x98DD4ACC, 0xEFDA7A5A, 0x76D32BE0, 0x01D41B76,
189 0x916B06E7, 0xE66C3671, 0x7F6567CB, 0x0862575D,
190 0x9606C2FE, 0xE101F268, 0x7808A3D2, 0x0F0F9344,
191 0x82079EB1, 0xF500AE27, 0x6C09FF9D, 0x1B0ECF0B,
192 0x856A5AA8, 0xF26D6A3E, 0x6B643B84, 0x1C630B12,
193 0x8CDC1683, 0xFBDB2615, 0x62D277AF, 0x15D54739,
194 0x8BB1D29A, 0xFCB6E20C, 0x65BFB3B6, 0x12B88320,
195 0x3FBA6CAD, 0x48BD5C3B, 0xD1B40D81, 0xA6B33D17,
196 0x38D7A8B4, 0x4FD09822, 0xD6D9C998, 0xA1DEF90E,
197 0x3161E49F, 0x4666D409, 0xDF6F85B3, 0xA868B525,
198 0x360C2086, 0x410B1010, 0xD80241AA, 0xAF05713C,
199 0x220D7CC9, 0x550A4C5F, 0xCC031DE5, 0xBB042D73,
200 0x2560B8D0, 0x52678846, 0xCB6ED9FC, 0xBC69E96A,
201 0x2CD6F4FB, 0x5BD1C46D, 0xC2D895D7, 0xB5DFA541,
202 0x2BBB30E2, 0x5CBC0074, 0xC5B551CE, 0xB2B26158,
203 0x04D44C65, 0x73D37CF3, 0xEADA2D49, 0x9DDD1DDF,
204 0x03B9887C, 0x74BEB8EA, 0xEDB7E950, 0x9AB0D9C6,
205 0x0A0FC457, 0x7D08F4C1, 0xE401A57B, 0x930695ED,
206 0x0D62004E, 0x7A6530D8, 0xE36C6162, 0x946B51F4,
207 0x19635C01, 0x6E646C97, 0xF76D3D2D, 0x806A0DBB,
208 0x1E0E9818, 0x6909A88E, 0xF000F934, 0x8707C9A2,
209 0x17B8D433, 0x60BFE4A5, 0xF9B6B51F, 0x8EB18589,
210 0x10D5102A, 0x67D220BC, 0xFEDB7106, 0x89DC4190,
211 0x49662D3D, 0x3E611DAB, 0xA7684C11, 0xD06F7C87,
212 0x4E0BE924, 0x390CD9B2, 0xA0058808, 0xD702B89E,
213 0x47BDA50F, 0x30BA9599, 0xA9B3C423, 0xDEB4F4B5,
214 0x40D06116, 0x37D75180, 0xAEDE003A, 0xD9D930AC,
215 0x54D13D59, 0x23D60DCF, 0xBADF5C75, 0xCDD86CE3,
216 0x53BCF940, 0x24BBC9D6, 0xBDB2986C, 0xCAB5A8FA,
217 0x5A0AB56B, 0x2D0D85FD, 0xB404D447, 0xC303E4D1,
218 0x5D677172, 0x2A6041E4, 0xB369105E, 0xC46E20C8,
219 0x72080DF5, 0x050F3D63, 0x9C066CD9, 0xEB015C4F,
220 0x7565C9EC, 0x0262F97A, 0x9B6BA8C0, 0xEC6C9856,
221 0x7CD385C7, 0x0BD4B551, 0x92DDE4EB, 0xE5DAD47D,
222 0x7BBE41DE, 0x0CB97148, 0x95B020F2, 0xE2B71064,
223 0x6FBF1D91, 0x18B82D07, 0x81B17CBD, 0xF6B64C2B,
224 0x68D2D988, 0x1FD5E91E, 0x86DCB8A4, 0xF1DB8832,
225 0x616495A3, 0x1663A535, 0x8F6AF48F, 0xF86DC419,
226 0x660951BA, 0x110E612C, 0x88073096, 0xFF000000
229 static inline void sbni_outs(int port, void *data, int len)
231 #ifdef GOODBUS16
232 outsw(port,data,len/2);
233 if(len & 1)
234 outb(((char*)data)[len - 1],port);
235 #else
236 outsb(port,data,len);
237 #endif
240 static inline void sbni_ins(int port, void *data, int len)
242 #ifdef GOODBUS16
243 insw(port,data,len/2);
244 if(len & 1)
245 ((char*)data)[len - 1] = inb(port);
246 #else
247 insb(port,data,len);
248 #endif
252 static int sbni_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
253 void *daddr, void *saddr, unsigned len)
255 struct sbni_hard_header *hh = (struct sbni_hard_header *)
256 skb_push(skb, sizeof(struct sbni_hard_header));
259 if(type!=ETH_P_802_3)
260 hh->h_proto = htons(type);
261 else
262 hh->h_proto = htons(len);
264 if(saddr)
265 memcpy(hh->h_source,saddr,dev->addr_len);
266 else
267 memcpy(hh->h_source,dev->dev_addr,dev->addr_len);
269 if(daddr)
271 memcpy(hh->h_dest,daddr,dev->addr_len);
272 return dev->hard_header_len;
274 return -dev->hard_header_len;
278 int sbni_header_cache(struct neighbour *neigh, struct hh_cache *hh)
280 unsigned short type = hh->hh_type;
281 struct sbni_hard_header *sbni = (struct sbni_hard_header*)
282 (((u8*)hh->hh_data) - 8);
283 struct net_device *dev = neigh->dev;
286 if (type == __constant_htons(ETH_P_802_3))
287 return -1;
289 sbni->h_proto = type;
290 memcpy(sbni->h_source, dev->dev_addr, dev->addr_len);
291 memcpy(sbni->h_dest, neigh->ha, dev->addr_len);
292 return 0;
295 static int sbni_rebuild_header(struct sk_buff *skb)
297 struct sbni_hard_header *hh = (struct sbni_hard_header *)skb;
299 * Only ARP/IP is currently supported
303 * Try to get ARP to resolve the header.
306 #ifdef CONFIG_INET
307 return arp_find((unsigned char*)hh->h_dest, skb)? 1 : 0;
308 #else
309 return 0;
310 #endif
313 static void sbni_header_cache_update(struct hh_cache *hh, struct net_device *dev, unsigned char * haddr)
315 memcpy(((u8*)hh->hh_data) + 2, haddr, dev->addr_len);
320 #ifdef HAVE_DEVLIST
321 struct netdev_entry sbni_drv = {
322 "sbni", sbni_probe1, SBNI_IO_EXTENT, netcard_portlist
325 #else
327 int __init sbni_probe(struct net_device *dev)
329 int i;
330 int base_addr = dev ? dev->base_addr : 0;
332 DP( printk("%s: sbni_probe\n", dev->name); )
334 if(base_addr > 0x1ff) /* Check a single specified location. */
335 return sbni_probe1(dev, base_addr);
336 else if(base_addr != 0) /* Don't probe at all. */
337 return ENXIO;
338 for(i = 0; (base_addr = netcard_portlist[i]); i++)
340 if(!check_region(base_addr, SBNI_IO_EXTENT) && base_addr != 1)
342 /* Lock this address, or later we'll try it again */
343 netcard_portlist[i] = 1;
344 if(sbni_probe1(dev, base_addr) == 0)
345 return 0;
348 return ENODEV;
351 #endif /* have devlist*/
354 * The actual probe.
358 Valid combinations in CSR0 (for probing):
360 VALID_DECODER 0000,0011,1011,1010
362 ; 0 ; -
363 TR_REQ ; 1 ; +
364 TR_RDY ; 2 ; -
365 TR_RDY TR_REQ ; 3 ; +
366 BU_EMP ; 4 ; +
367 BU_EMP TR_REQ ; 5 ; +
368 BU_EMP TR_RDY ; 6 ; -
369 BU_EMP TR_RDY TR_REQ ; 7 ; +
370 RC_RDY ; 8 ; +
371 RC_RDY TR_REQ ; 9 ; +
372 RC_RDY TR_RDY ; 10 ; -
373 RC_RDY TR_RDY TR_REQ ; 11 ; -
374 RC_RDY BU_EMP ; 12 ; -
375 RC_RDY BU_EMP TR_REQ ; 13 ; -
376 RC_RDY BU_EMP TR_RDY ; 14 ; -
377 RC_RDY BU_EMP TR_RDY TR_REQ ; 15 ; -
379 #define VALID_DECODER (2 + 8 + 0x10 + 0x20 + 0x80 + 0x100 + 0x200)
381 static int __init sbni_probe1(struct net_device *dev, int ioaddr)
384 int autoirq = 0;
385 int bad_card = 0;
386 unsigned char csr0;
387 struct net_local* lp;
388 static int version_printed = 0;
390 DP( printk("%s: sbni_probe1 ioaddr=%d\n", dev->name, ioaddr); )
392 if(check_region(ioaddr, SBNI_IO_EXTENT) < 0)
393 return -ENODEV;
394 if(version_printed++ == 0)
395 printk(version);
397 /* check for valid combination in CSR0 */
398 csr0 = inb(ioaddr + CSR0);
399 if(csr0 == 0xff || csr0 == 0)
400 bad_card = 1;
401 else
403 csr0 &= ~EN_INT;
404 if(csr0 & BU_EMP)
405 csr0 |= EN_INT;
406 if((VALID_DECODER & (1 << (csr0 >> 4))) == 0)
407 bad_card = 1;
410 if(bad_card)
411 return ENODEV;
412 else
413 outb(0, ioaddr + CSR0);
414 if(dev->irq < 2)
416 DP( printk("%s: autoprobing\n", dev->name); );
417 autoirq_setup(5);
418 outb(EN_INT | TR_REQ, ioaddr + CSR0);
419 outb(PR_RES, ioaddr + CSR1);
420 autoirq = autoirq_report(5);
422 if(autoirq == 0)
424 printk("sbni probe at %#x failed to detect IRQ line\n", ioaddr);
425 return EAGAIN;
428 /* clear FIFO buffer */
429 outb(0, ioaddr + CSR0);
431 if(autoirq)
432 dev->irq = autoirq;
435 int irqval=request_irq(dev->irq, sbni_interrupt, 0, dev->name, dev);
436 if (irqval)
438 printk (" unable to get IRQ %d (irqval=%d).\n", dev->irq, irqval);
439 return EAGAIN;
444 * Initialize the device structure.
447 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
448 if(dev->priv == NULL)
450 DP( printk("%s: cannot allocate memory\n", dev->name); )
451 return -ENOMEM;
454 memset(dev->priv, 0, sizeof(struct net_local));
455 dev->base_addr = ioaddr;
456 request_region(ioaddr, SBNI_IO_EXTENT, "sbni");
459 * generate Ethernet address (0x00ff01xxxxxx)
462 *(u16*)dev->dev_addr = htons(0x00ff);
463 *(u32*)(dev->dev_addr+2) = htonl(((def_mac ? def_mac : (u32) dev->priv) & 0x00ffffff) | 0x01000000);
465 lp = dev->priv;
466 if(def_rxl < 0)
468 /* autodetect receive level */
469 lp->rxl_curr = 0xf;
470 lp->rxl_delta = -1;
471 } else {
472 /* fixed receive level */
473 lp->rxl_curr = def_rxl & 0xf;
474 lp->rxl_delta = 0;
476 lp->csr1.rxl = rxl_tab[lp->rxl_curr];
477 lp->csr1.rate = def_baud & 3;
478 lp->frame_len = DEF_FRAME_LEN;
479 printk("%s: sbni adapter at %#lx, using %sIRQ %d, MAC: 00:ff:01:%x:%x:%x\n",
480 dev->name, dev->base_addr, autoirq ? "auto":"assigned ", dev->irq,
481 *(unsigned char*)(dev->dev_addr+3),
482 *(unsigned char*)(dev->dev_addr+4),
483 *(unsigned char*)(dev->dev_addr+5)
486 printk("%s: receive level: ", dev->name);
487 if(lp->rxl_delta == 0)
488 printk ("%#1x (fixed)", lp->rxl_curr);
489 else
490 printk ("autodetect");
491 printk(", baud rate: %u\n", (unsigned)lp->csr1.rate);
494 * The SBNI-specific entries in the device structure.
496 dev->open = &sbni_open;
497 dev->hard_start_xmit = &sbni_start_xmit;
498 dev->stop = &sbni_close;
499 dev->get_stats = &sbni_get_stats;
500 dev->set_multicast_list = &set_multicast_list;
501 dev->set_mac_address = &sbni_set_mac_address;
502 dev->do_ioctl = &sbni_ioctl;
505 * Setup the generic properties
508 ether_setup(dev);
510 dev->hard_header = sbni_header;
511 dev->hard_header_len = sizeof(struct sbni_hard_header);
512 dev->rebuild_header=sbni_rebuild_header;
513 dev->mtu = DEF_FRAME_LEN;
515 dev->hard_header_cache = sbni_header_cache;
516 dev->header_cache_update = sbni_header_cache_update;
518 spin_lock_init(&lp->lock);
519 lp->m=dev;
520 lp->me=dev;
521 lp->next_lp=NULL;
523 return 0;
527 * Open/initialize the board.
530 static int sbni_open(struct net_device *dev)
532 struct net_local* lp = (struct net_local*)dev->priv;
533 struct timer_list* watchdog = &lp->watchdog;
534 unsigned long flags;
536 DP( printk("%s: sbni_open\n", dev->name); )
538 save_flags(flags);
539 cli();
540 lp->currframe = NULL;
542 card_start(dev);
543 /* set timer watchdog */
544 init_timer(watchdog);
545 watchdog->expires = jiffies + SBNI_TIMEOUT;
546 watchdog->data = (unsigned long)dev;
547 watchdog->function = sbni_watchdog;
548 add_timer(watchdog);
549 DP( printk("%s: sbni timer watchdog initialized\n", dev->name); );
551 restore_flags(flags);
553 netif_start_queue(dev);
554 MOD_INC_USE_COUNT;
555 return 0;
558 static int sbni_close(struct net_device *dev)
560 int ioaddr = dev->base_addr;
561 struct net_local* lp = (struct net_local*) dev->priv;
562 struct timer_list* watchdog = &lp->watchdog;
563 unsigned long flags;
565 DP( printk("%s: sbni_close\n", dev->name); )
567 netif_stop_queue(dev);
569 save_flags(flags);
570 cli();
571 sbni_drop_tx_queue(dev);
572 del_timer(watchdog);
573 outb(0, ioaddr + CSR0);
574 restore_flags(flags);
576 MOD_DEC_USE_COUNT;
577 return 0;
580 static int sbni_start_xmit(struct sk_buff *skb, struct net_device *dev)
582 struct net_local *lp = (struct net_local*)dev->priv;
583 struct sbni_hard_header *hh=(struct sbni_hard_header *)skb->data;
584 unsigned long flags;
586 #ifdef KATYUSHA
587 struct net_local *nl;
588 int stop;
589 #endif
591 DP( printk("%s: sbni_start_xmit In \n", dev->name); );
594 if(lp->me != dev)
595 panic("sbni: lp->me != dev !!!\nMail to developer (xenon@granch.ru) if you noticed this error\n");
597 hh->number = 1;
598 hh->reserv = 0;
600 hh->packetlen = (skb->len - sizeof (unsigned short) -
601 (sizeof(struct sbni_hard_header) - SBNI_HH_SZ))
602 | PACKET_SEND_OK | PACKET_FIRST_FRAME;
604 /* we should use hairy method to calculate crc because of extra bytes are
605 livin between hard header and data*/
606 hh->crc = calc_crc((void*)&hh->packetlen, SBNI_HH_SZ - sizeof(unsigned), CRC32_INITIAL);
607 hh->crc = calc_crc(skb->data + sizeof(struct sbni_hard_header),
608 skb->len - sizeof(struct sbni_hard_header),
609 hh->crc);
611 spin_lock_irqsave(&lp->lock, flags);
612 #ifdef KATYUSHA
613 /* looking for first idle device */
614 for (stop=0,nl=lp; nl && !stop; nl=nl->next_lp)
616 if((!nl->currframe) && (nl->carrier)) /* if idle */
618 skb->dev = lp->me;
619 nl->currframe = skb;
620 /* set request for transmit */
621 outb(inb(nl->me->base_addr + CSR0) | TR_REQ,
622 nl->me->base_addr + CSR0);
623 stop=1;
627 if(!stop) /* we havent found any idle.*/
629 skb_queue_tail(&lp->queue,skb);
630 outb(inb(dev->base_addr + CSR0) | TR_REQ, dev->base_addr + CSR0);
633 #else
634 if (lp->currframe || 1)
636 skb_queue_tail(&lp->queue,skb);
639 else
641 lp->currframe = skb;
643 /* set request for transmit */
644 outb(inb(dev->base_addr + CSR0) | TR_REQ, dev->base_addr + CSR0);
645 #endif
646 spin_unlock_irqrestore(&lp->lock, flags);
647 return 0;
650 static void card_start(struct net_device *dev)
652 struct net_local *lp = (struct net_local*)dev->priv;
654 DP( printk("%s: card_start\n",dev->name); )
655 lp->wait_frame_number = 0;
656 lp->inppos = lp->outpos = 0;
657 lp->eth_trans_buffer_len = 0;
658 lp->tr_err = TR_ERROR_COUNT;
659 lp->last_receive_OK = FALSE;
660 lp->tr_resend = FALSE;
661 lp->timer_ticks = CHANGE_LEVEL_START_TICKS;
662 lp->timeout_rxl = 0;
664 lp->waitack=0;
665 skb_queue_head_init(&lp->queue);
666 sbni_drop_tx_queue(dev);
667 /* Reset the card and set start parameters */
668 outb(PR_RES | *(char*)&lp->csr1, dev->base_addr + CSR1);
669 outb(EN_INT, dev->base_addr + CSR0);
672 void sbni_nirvana(struct net_device *dev)
674 sbni_outs(dev->base_addr+DAT,magic_reply,9);
677 static inline unsigned short sbni_recv(struct net_device *dev)
679 struct net_local *lp = (struct net_local*)dev->priv;
680 unsigned long crc;
681 unsigned short packetlen = 0;
682 unsigned short packetinf, packetfirst, receiveframeresend;
683 unsigned char current_frame;
684 unsigned int i, j;
685 unsigned char delme,rcv_res=RCV_WR;
687 lp->in_stats.all_rx_number++;
689 if((delme=inb(dev->base_addr + DAT)) == SBNI_SIG)
691 crc = CRC32_INITIAL;
692 *(((unsigned char *)&packetlen) + 0) = inb(dev->base_addr + DAT);
693 crc = CRC32(*(((unsigned char *)&packetlen) + 0), crc);
694 *(((unsigned char *)&packetlen) + 1) = inb(dev->base_addr + DAT);
695 crc = CRC32(*(((unsigned char *)&packetlen) + 1), crc);
696 packetinf = packetlen & PACKET_INF_MASK;
697 packetfirst = packetlen & PACKET_FIRST_FRAME;
698 receiveframeresend = packetlen & RECEIVE_FRAME_RESEND;
699 packetlen = packetlen & PACKET_LEN_MASK;
702 if((packetlen <= SB_MAX_BUFFER_ARRAY - 3) && (packetlen >= 6))
704 /* read frame number */
705 current_frame = inb(dev->base_addr + DAT);
706 crc = CRC32(current_frame, crc);
707 /* read HandShake counter */
708 lp->HSCounter = inb(dev->base_addr + DAT);
709 crc = CRC32(lp->HSCounter, crc);
710 packetlen -= 2;
712 sbni_ins(dev->base_addr + DAT, lp->eth_rcv_buffer + lp->inppos, packetlen);
714 for(i = lp->inppos; i < (packetlen + lp->inppos); i++)
716 crc = CRC32(lp->eth_rcv_buffer[i], crc);
719 if(crc == CRC32_REMAINDER)
721 if(packetlen > 4)
722 rcv_res=RCV_OK;
723 else if(packetlen == 4)
724 rcv_res=RCV_NO;
726 if(lp->waitack && packetinf == PACKET_RESEND)
727 lp->in_stats.resend_tx_number++;
730 switch(packetinf)
732 case PACKET_SEND_OK:
734 lp->tr_err = TR_ERROR_COUNT;
735 lp->tr_resend = FALSE;
736 /* if(lp->trans_frame_number){ */
737 lp->outpos += lp->realframelen;
739 /* SendComplete
740 * not supported
742 DP( printk("%s: sbni_recv SendComplete\n",dev->name); );
744 * We sucessfully sent current packet
747 if(lp->waitack)
749 dev_kfree_skb(lp->currframe);
750 lp->stats.tx_packets++;
751 #ifdef KATYUSHA
752 lp->currframe=skb_dequeue(&(((struct net_local*) (lp->m->priv))->queue));
753 #else
754 lp->currframe=skb_dequeue(&lp->queue);
755 #endif
756 lp->in_stats.all_tx_number++;
757 lp->waitack=0;
761 * reset output active flags
763 netif_wake_queue(dev);
764 /*} if */
766 case PACKET_RESEND:
768 if(lp->tr_err) /**/
769 lp->tr_err--;
770 if(lp->ok_curr < 0xffffffff)
771 lp->ok_curr++;
772 if(packetlen > 4 && !(lp->last_receive_OK && receiveframeresend))
774 if(packetfirst)
776 if(lp->wait_frame_number)
778 for(i = lp->inppos, j = 0;
779 i < (lp->inppos + packetlen - 4);
780 i++, j++)
781 lp->eth_rcv_buffer[j] = lp->eth_rcv_buffer[i];
783 lp->wait_frame_number = current_frame;
784 lp->inppos = 0;
786 if(current_frame == lp->wait_frame_number)
788 lp->inppos += (packetlen - 4);
789 if(lp->wait_frame_number == 1)
791 sbni_get_packet(dev);
792 lp->inppos = 0;
794 lp->wait_frame_number--;
797 lp->last_receive_OK = TRUE;
798 break;
800 default:
801 break;
804 else
806 DP(printk("%s: bad CRC32\n",dev->name));
807 change_level(dev);
810 else
812 DP(printk("%s: bad len\n ",dev->name));
813 change_level(dev);
814 lp->stats.rx_over_errors++;
817 else
819 DP(printk("%s: bad sig\n",dev->name));
820 change_level(dev);
822 outb(inb(dev->base_addr + CSR0) ^ CT_ZER, dev->base_addr + CSR0);
823 return (rcv_res);
826 void change_level(struct net_device *dev)
828 struct net_local *lp = (struct net_local*)dev->priv;
830 lp->in_stats.bad_rx_number++;
831 lp->stats.tx_errors++;
832 if(lp->rxl_delta == 0)
833 return;
835 * set new rxl_delta value
837 if(lp->rxl_curr == 0)
838 lp->rxl_delta = 1;
839 else if(lp->rxl_curr == 0xf)
840 lp->rxl_delta = -1;
841 else if(lp->ok_curr < lp->ok_prev)
842 lp->rxl_delta = -lp->rxl_delta;
844 * set new rxl_curr value
846 lp->csr1.rxl = rxl_tab[lp->rxl_curr += lp->rxl_delta];
847 outb(*(char*)&lp->csr1, dev->base_addr + CSR1);
851 * update ok_prev/ok_curr counters
853 lp->ok_prev = lp->ok_curr;
854 lp->ok_curr = 0;
856 DP( printk("%s: receive error, rxl_curr = %d, rxl_delta = %d\n",\
857 dev->name,lp->rxl_curr, lp->rxl_delta); )
861 static inline void sbni_xmit(struct net_device *dev)
863 struct net_local* lp = (struct net_local *)dev->priv;
864 struct sk_buff *skb;
866 skb=lp->currframe;
868 DP( printk("%s: sbni_xmit CSR0=%02x\n",dev->name, (unsigned char)inb(dev->base_addr + CSR0)); );
870 /* push signature*/
871 outb(SBNI_SIG, dev->base_addr + DAT);
873 /* push frame w/o crc [HAiRY]*/
874 sbni_outs(dev->base_addr + DAT,
875 &((struct sbni_hard_header *)(skb->data))->packetlen,
876 SBNI_HH_SZ - sizeof(unsigned));
878 sbni_outs(dev->base_addr + DAT,
879 skb->data + sizeof(struct sbni_hard_header),
880 skb->len - sizeof(struct sbni_hard_header)); /* ÕÓÐÅÅÍ ÅÝÅ */
882 /* push crc */
883 sbni_outs(dev->base_addr + DAT, skb->data, sizeof(unsigned));
885 lp->waitack=1;
889 * The typical workload of the driver:
890 * Handle the ether interface interrupts.
892 static void sbni_interrupt(int irq, void *dev_id, struct pt_regs *regs)
894 struct net_device *dev = dev_id;
895 struct net_local* lp;
896 u_char csr0;
897 unsigned short rcv_res = RCV_NO;
900 if(dev == NULL || dev->irq != irq)
902 printk("sbni: irq %d for unknown device\n", irq);
903 return;
906 csr0 = inb(dev->base_addr + CSR0);
907 DP( printk("%s: entering interrupt handler, CSR0 = %02x\n", dev->name, csr0); )
909 lp=dev->priv;
911 spin_lock(&lp->lock);
913 if(!lp->carrier)
914 lp->carrier=1;
917 * Disable adapter interrupts
919 outb((csr0 & ~EN_INT) | TR_REQ, dev->base_addr + CSR0);
920 lp->timer_ticks = CHANGE_LEVEL_START_TICKS;
921 csr0 = inb(dev->base_addr + CSR0);
923 if(csr0 & (TR_RDY | RC_RDY))
925 if(csr0 & RC_RDY)
926 rcv_res = sbni_recv(dev);
928 if((lp->currframe) && (rcv_res != RCV_WR))
929 sbni_xmit(dev);
930 else if (rcv_res == RCV_OK)
931 sbni_nirvana(dev);
933 csr0 = inb(dev->base_addr + CSR0);
934 DP( printk("%s: CSR0 = %02x\n",dev->name, (u_int)csr0); );
938 DP( printk("%s: leaving interrupt handler, CSR0 = %02x\n",dev->name, csr0 | EN_INT); );
940 /* here we should send pong */
941 outb(inb(dev->base_addr+CSR0) & ~TR_REQ, dev->base_addr + CSR0);
942 if(lp->currframe)
943 outb(inb(dev->base_addr+CSR0) | TR_REQ, dev->base_addr + CSR0);
944 else
945 csr0 = inb(dev->base_addr + CSR0);
948 * Enable adapter interrupts
951 outb(csr0 | EN_INT, dev->base_addr + CSR0);
952 spin_unlock(&lp->lock);
955 static struct enet_statistics *sbni_get_stats(struct net_device *dev)
957 struct net_local *lp = (struct net_local *)dev->priv;
958 return &lp->stats;
961 static inline void sbni_get_packet(struct net_device* dev)
963 struct net_local* lp = (struct net_local*)dev->priv;
964 struct sk_buff* skb;
965 unsigned char *rawp;
969 skb = dev_alloc_skb(lp->inppos - ETH_HLEN + sizeof(struct sbni_hard_header));
971 if(skb == NULL)
973 DP( printk("%s: Memory squeeze, dropping packet.\n", dev->name); )
974 lp->stats.rx_dropped++;
975 return;
976 } else {
977 #ifdef KATYUSHA
978 skb->dev = lp->m;
979 #else
980 skb->dev = dev;
981 #endif
982 memcpy((unsigned char*)skb_put(skb, lp->inppos + 8)+8,
983 lp->eth_rcv_buffer,
984 lp->inppos);
987 skb->mac.raw = skb->data + 8;
989 if((*(char*)lp->eth_rcv_buffer) & 1)
991 if(memcmp(lp->eth_rcv_buffer,dev->broadcast, ETH_ALEN)==0)
992 skb->pkt_type=PACKET_BROADCAST;
993 else
994 skb->pkt_type=PACKET_MULTICAST;
996 else if(dev->flags&(IFF_PROMISC|IFF_ALLMULTI))
998 if(memcmp(lp->eth_rcv_buffer,dev->dev_addr, ETH_ALEN))
999 skb->pkt_type=PACKET_OTHERHOST;
1002 if( htons(*((unsigned short*)(&lp->eth_rcv_buffer[2*ETH_ALEN]))) >= 1536)
1003 skb->protocol = *((unsigned short*)(&lp->eth_rcv_buffer[2*ETH_ALEN]));
1004 else
1006 rawp = (unsigned char*)(&lp->eth_rcv_buffer[2*ETH_ALEN]);
1007 if (*(unsigned short *)rawp == 0xFFFF)
1008 skb->protocol=htons(ETH_P_802_3);
1009 else
1010 skb->protocol=htons(ETH_P_802_2);
1014 skb_pull(skb,SBNI_HH_SZ);
1016 netif_rx(skb);
1017 lp->stats.rx_packets++;
1019 return;
1022 static void sbni_watchdog(unsigned long arg)
1024 struct net_device* dev = (struct net_device*)arg;
1025 struct net_local* lp = (struct net_local *)dev->priv;
1026 u_char csr0;
1030 DP( printk("%s: watchdog start\n",dev->name); )
1032 * if no pong received and transmission is not in progress
1033 * then assume error
1035 cli();
1036 csr0 = inb(dev->base_addr + CSR0);
1037 if(csr0 & (RC_CHK | TR_REQ))
1039 if(lp->timer_ticks)
1041 if(csr0 & (RC_RDY | BU_EMP))
1043 lp->timer_ticks--;
1046 else
1048 if(lp->rxl_delta)
1050 lp->ok_prev = lp->ok_curr;
1051 lp->ok_curr = 0;
1052 lp->rxl_curr = timeout_rxl_tab[lp->timeout_rxl];
1053 lp->timeout_rxl++;
1054 if(lp->timeout_rxl > SIZE_OF_TIMEOUT_RXL_TAB - 1)
1055 lp->timeout_rxl = 0;
1056 lp->csr1.rxl = rxl_tab[lp->rxl_curr];
1058 * update ok_prev/ok_curr counters
1060 lp->ok_prev = lp->ok_curr;
1061 lp->ok_curr = 0;
1063 if(lp->tr_err)
1064 lp->tr_err--;
1065 else
1067 /* Drop the queue of tx packets */
1068 sbni_drop_tx_queue(dev);
1069 lp->carrier=0;
1073 * send pong
1076 csr0 = inb(dev->base_addr + CSR0);
1077 outb(csr0 & ~TR_REQ, dev->base_addr + CSR0);
1078 outb(*(char*)(&lp->csr1) | PR_RES, dev->base_addr + CSR1);
1079 lp->in_stats.timeout_number++;
1082 sti();
1083 outb(csr0 | RC_CHK, dev->base_addr + CSR0);
1084 if(netif_running(dev))
1086 struct timer_list* watchdog = &lp->watchdog;
1087 init_timer(watchdog);
1088 watchdog->expires = jiffies + SBNI_TIMEOUT;
1089 watchdog->data = arg;
1090 watchdog->function = sbni_watchdog;
1091 add_timer(watchdog);
1095 static void sbni_drop_tx_queue(struct net_device *dev)
1097 struct net_local* lp = (struct net_local *)dev->priv,*nl;
1098 struct sk_buff *tmp;
1100 /* first of all, we should try to gift our packets to another interface */
1102 nl=(struct net_local *)lp->m->priv;
1103 if(nl==lp)
1104 nl=lp->next_lp;
1106 if(nl)
1108 /* we found device*/
1109 if(lp->currframe)
1111 if(!nl->currframe)
1113 nl->currframe=lp->currframe;
1115 else
1117 skb_queue_head(&((struct net_local*)(lp->m->priv))->queue,lp->currframe);
1120 lp->currframe=NULL;
1122 if(!nl->currframe)
1123 nl->currframe=skb_dequeue(&(((struct net_local*)(lp->m->priv))->queue));
1125 /* set request for transmit */
1126 outb(inb(nl->me->base_addr + CSR0) | TR_REQ, nl->me->base_addr + CSR0);
1129 else
1131 /* *sigh*, we should forget this packets */
1132 nl=lp->m->priv;
1134 while((tmp = skb_dequeue(&nl->queue)) != NULL)
1136 dev_kfree_skb(tmp);
1137 lp->stats.tx_packets++;
1140 if (lp->currframe)
1142 dev_kfree_skb(lp->currframe);
1143 lp->currframe = NULL;
1144 lp->stats.tx_packets++;
1147 lp->waitack=0;
1148 netif_wake_queue(dev);
1150 DP( printk("%s: queue dropping stoped\n",dev->name); );
1154 * Set or clear the multicast filter for this adaptor.
1155 * num_addrs == -1 Promiscuous mode, receive all packets
1156 * num_addrs == 0 Normal mode, clear multicast list
1157 * num_addrs > 0 Multicast mode, receive normal and MC packets,
1158 * and do best-effort filtering.
1161 static void set_multicast_list(struct net_device *dev)
1164 * always enabled promiscuous mode.
1166 return;
1169 static int sbni_set_mac_address(struct net_device *dev, void *addr)
1171 /* struct net_local *lp = (struct net_local *)dev->priv; */
1172 struct sockaddr *saddr = addr;
1174 if(netif_running(dev))
1176 /* Only possible while card isn't started */
1177 return -EBUSY;
1179 memcpy(dev->dev_addr, saddr->sa_data, dev->addr_len);
1180 return (0);
1183 static int sbni_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1185 struct net_local* lp = (struct net_local *)dev->priv,*tlp;
1186 struct net_device *slave;
1187 int error = 0;
1188 char tmpstr[6];
1191 switch(cmd)
1193 case SIOCDEVGETINSTATS:
1195 struct sbni_in_stats *in_stats = (struct sbni_in_stats *)ifr->ifr_data;
1196 DP( printk("%s: SIOCDEVGETINSTATS %08x\n",dev->name,(unsigned)in_stats);)
1197 if(copy_to_user((void *)in_stats, (void *)(&(lp->in_stats)), sizeof(struct sbni_in_stats)))
1198 return -EFAULT;
1199 break;
1201 case SIOCDEVRESINSTATS:
1203 if(!capable(CAP_NET_ADMIN))
1204 return -EPERM;
1205 DP( printk("%s: SIOCDEVRESINSTATS\n",dev->name); )
1206 lp->in_stats.all_rx_number = 0;
1207 lp->in_stats.bad_rx_number = 0;
1208 lp->in_stats.timeout_number = 0;
1209 lp->in_stats.all_tx_number = 0;
1210 lp->in_stats.resend_tx_number = 0;
1211 break;
1213 case SIOCDEVGHWSTATE:
1215 struct sbni_flags flags;
1216 flags.rxl = lp->rxl_curr;
1217 flags.rate = lp->csr1.rate;
1218 flags.fixed_rxl = (lp->rxl_delta == 0);
1219 flags.fixed_rate = 1;
1220 ifr->ifr_data = *(caddr_t*)&flags;
1221 DP( printk("%s: get flags (0x%02x)\n",dev->name, (unsigned char)ifr->ifr_data); )
1222 break;
1224 case SIOCDEVSHWSTATE:
1226 struct sbni_flags flags;
1227 DP( printk("%s: SIOCDEVSHWSTATE flags=0x%02x\n",dev->name, (unsigned char)ifr->ifr_data); )
1228 /* root only */
1229 if(!capable(CAP_NET_ADMIN))
1230 return -EPERM;
1231 flags = *(struct sbni_flags*)&ifr->ifr_data;
1232 if(flags.fixed_rxl)
1234 lp->rxl_delta = 0;
1235 lp->rxl_curr = flags.rxl;
1237 else
1239 lp->rxl_delta = DEF_RXL_DELTA;
1240 lp->rxl_curr = DEF_RXL;
1242 lp->csr1.rxl = rxl_tab[lp->rxl_curr];
1243 if(flags.fixed_rate)
1244 lp->csr1.rate = flags.rate;
1245 else
1246 lp->csr1.rate = DEF_RATE;
1248 * Don't be afraid...
1250 outb(*(char*)(&lp->csr1) | PR_RES, dev->base_addr + CSR1);
1252 DP( printk("%s: set flags (0x%02x)\n receive level: %u, baud rate: %u\n",\
1253 dev->name, (unsigned char)ifr->ifr_data, (unsigned)lp->rxl_curr, (unsigned)lp->csr1.rate); )
1254 break;
1257 case SIOCDEVENSLAVE:
1258 if(!capable(CAP_NET_ADMIN))
1259 return -EPERM;
1260 if(copy_from_user( tmpstr, ifr->ifr_data, 6))
1261 return -EFAULT;
1262 slave = dev_get_by_name(tmpstr);
1263 if(!(slave && slave->flags & IFF_UP && dev->flags & IFF_UP))
1265 printk("%s: Both devices should be UP to enslave!\n",dev->name);
1266 if (slave)
1267 dev_put(slave);
1268 return -EINVAL;
1271 if(slave)
1273 if(!((dev->flags & IFF_SLAVE) || (slave->flags & IFF_SLAVE)))
1275 /* drop queue*/
1276 sbni_drop_tx_queue(slave);
1277 slave->flags |= IFF_SLAVE;
1278 ((struct net_local *)(slave->priv))->m=dev;
1279 while(lp->next_lp) //tail it after last slave
1280 lp=lp->next_lp;
1281 lp->next_lp=slave->priv;
1282 lp=(struct net_local *)dev->priv;
1283 dev->flags |= IFF_MASTER;
1285 else
1287 printk("%s: one of devices is already slave!\n",dev->name);
1288 error = -EBUSY;
1290 dev_put(slave);
1292 else
1294 printk("%s: can't find device %s to enslave\n",dev->name,ifr->ifr_data);
1295 return -ENOENT;
1297 break;
1299 case SIOCDEVEMANSIPATE:
1300 if(!capable(CAP_NET_ADMIN))
1301 return -EPERM;
1303 if(dev->flags & IFF_SLAVE)
1305 dev->flags &= ~IFF_SLAVE;
1306 /* exclude us from masters slavelist*/
1307 for(tlp=lp->m->priv;tlp->next_lp!=lp && tlp->next_lp;tlp=tlp->next_lp);
1308 if(tlp->next_lp)
1310 tlp->next_lp = lp->next_lp;
1311 if(!((struct net_local *)lp->m->priv)->next_lp)
1313 lp->m->flags &= ~IFF_MASTER;
1315 lp->next_lp=NULL;
1316 lp->m=dev;
1318 else
1320 printk("%s: Ooops. drivers structure is mangled!\n",dev->name);
1321 return -EIO;
1324 else
1326 printk("%s: isn't slave device!\n",dev->name);
1327 return -EINVAL;
1329 break;
1331 default:
1332 DP( printk("%s: invalid ioctl: 0x%x\n",dev->name, cmd); )
1333 error = -EINVAL;
1335 return (error);
1340 #ifdef CRCASM
1342 unsigned long calc_crc(char *mem, int len, unsigned initial)
1344 unsigned crc, dummy_len;
1345 __asm__ (
1346 "xorl %%eax,%%eax\n\t"
1347 "1:\n\t"
1348 "lodsb\n\t"
1349 "xorb %%dl,%%al\n\t"
1350 "shrl $8,%%edx\n\t"
1351 "xorl (%%edi,%%eax,4),%%edx\n\t"
1352 "loop 1b"
1353 : "=d" (crc), "=c" (dummy_len)
1354 : "S" (mem), "D" (&crc32tab[0]), "1" (len), "0" (initial)
1355 : "eax"
1357 return crc;
1360 #else
1362 unsigned long calc_crc(char *mem, int len, unsigned initial)
1364 unsigned crc;
1365 crc = initial;
1367 for(;len;mem++,len--)
1369 crc = CRC32(*mem, crc);
1371 return(crc);
1373 #endif /* CRCASM */
1374 #ifdef MODULE
1376 static int io[SBNI_MAX_NUM_CARDS] = { 0 };
1377 static int irq[SBNI_MAX_NUM_CARDS] = { 0 };
1378 static int rxl[SBNI_MAX_NUM_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
1379 static int baud[SBNI_MAX_NUM_CARDS] = { 0 };
1380 static long mac[SBNI_MAX_NUM_CARDS] = { 0 };
1382 MODULE_PARM(io, "1-" __MODULE_STRING(SBNI_MAX_NUM_CARDS) "i");
1383 MODULE_PARM(irq, "1-" __MODULE_STRING(SBNI_MAX_NUM_CARDS) "i");
1384 MODULE_PARM(rxl, "1-" __MODULE_STRING(SBNI_MAX_NUM_CARDS) "i");
1385 MODULE_PARM(baud, "1-" __MODULE_STRING(SBNI_MAX_NUM_CARDS) "i");
1386 MODULE_PARM(mac, "1-" __MODULE_STRING(SBNI_MAX_NUM_CARDS) "i");
1389 static int sbniautodetect = -1;
1391 static struct net_device dev_sbni[SBNI_MAX_NUM_CARDS] = {
1393 "sbni0",
1394 0, 0, 0, 0, /* memory */
1395 0, 0, /* base, irq */
1396 0, 0, 0, NULL, sbni_probe
1399 "sbni1",
1400 0, 0, 0, 0, /* memory */
1401 0, 0, /* base, irq */
1402 0, 0, 0, NULL, sbni_probe
1405 "sbni2",
1406 0, 0, 0, 0, /* memory */
1407 0, 0, /* base, irq */
1408 0, 0, 0, NULL, sbni_probe
1411 "sbni3",
1412 0, 0, 0, 0, /* memory */
1413 0, 0, /* base, irq */
1414 0, 0, 0, NULL, sbni_probe
1417 "sbni4",
1418 0, 0, 0, 0, /* memory */
1419 0, 0, /* base, irq */
1420 0, 0, 0, NULL, sbni_probe
1423 "sbni5",
1424 0, 0, 0, 0, /* memory */
1425 0, 0, /* base, irq */
1426 0, 0, 0, NULL, sbni_probe
1429 "sbni6",
1430 0, 0, 0, 0, /* memory */
1431 0, 0, /* base, irq */
1432 0, 0, 0, NULL, sbni_probe
1435 "sbni7",
1436 0, 0, 0, 0, /* memory */
1437 0, 0, /* base, irq */
1438 0, 0, 0, NULL, sbni_probe
1442 int init_module(void)
1444 int devices = 0;
1445 int installed = 0;
1446 int i;
1448 /* My simple plug for this huge init_module. "XenON */
1450 if(sbniautodetect != -1)
1452 /* Autodetect mode */
1453 printk("sbni: Autodetect mode (not recommended!) ...\n");
1454 if(!sbniautodetect)
1455 sbniautodetect=SBNI_MAX_NUM_CARDS;
1456 printk("Trying to find %d SBNI cards...\n", sbniautodetect);
1457 if(sbniautodetect > SBNI_MAX_NUM_CARDS)
1459 sbniautodetect = SBNI_MAX_NUM_CARDS;
1460 printk("sbni: You want to detect too many cards. Truncated to %d\n", SBNI_MAX_NUM_CARDS);
1462 for(i = 0; i < sbniautodetect; i++)
1464 if(!register_netdev(&dev_sbni[i]))
1465 installed++;
1467 if(installed)
1468 return 0;
1469 else
1470 return -EIO;
1473 /* Manual mode */
1474 for(i = 0; i < SBNI_MAX_NUM_CARDS; i++)
1476 if((io[i] != 0) || (irq[i] != 0))
1477 devices++;
1479 for(i = 0; i < devices; i++)
1481 dev_sbni[i].irq = irq[i];
1482 dev_sbni[i].base_addr = io[i];
1483 def_rxl = rxl[i];
1484 def_baud = baud[i];
1485 def_mac = mac[i];
1486 if(register_netdev(&dev_sbni[i]))
1487 printk("sbni: card not found!\n");
1488 else
1489 installed++;
1491 if(installed)
1492 return 0;
1493 else
1494 return -EIO;
1497 void cleanup_module(void)
1499 int i;
1500 for(i = 0; i < 4; i++)
1502 if(dev_sbni[i].priv)
1504 free_irq(dev_sbni[i].irq, &dev_sbni[i]);
1505 release_region(dev_sbni[i].base_addr, SBNI_IO_EXTENT);
1506 unregister_netdev(&dev_sbni[i]);
1507 kfree(dev_sbni[i].priv);
1508 dev_sbni[i].priv = NULL;
1512 #endif /* MODULE */