1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25static const char *version =
26 "isa-skeleton.c:v1.51 9/24/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43#include <linux/module.h>
44#include <linux/kernel.h>
45#include <linux/types.h>
46#include <linux/fcntl.h>
47#include <linux/interrupt.h>
48#include <linux/ioport.h>
49#include <linux/in.h>
50#include <linux/slab.h>
51#include <linux/string.h>
52#include <linux/spinlock.h>
53#include <linux/errno.h>
54#include <linux/init.h>
55#include <linux/netdevice.h>
56#include <linux/etherdevice.h>
57#include <linux/skbuff.h>
58#include <linux/bitops.h>
59
60#include <asm/system.h>
61#include <asm/io.h>
62#include <asm/dma.h>
63
64
65
66
67
68static const char* cardname = "netcard";
69
70
71
72
73static unsigned int netcard_portlist[] __initdata =
74 { 0x200, 0x240, 0x280, 0x2C0, 0x300, 0x320, 0x340, 0};
75
76
77#ifndef NET_DEBUG
78#define NET_DEBUG 2
79#endif
80static unsigned int net_debug = NET_DEBUG;
81
82
83#define NETCARD_IO_EXTENT 32
84
85#define MY_TX_TIMEOUT ((400*HZ)/1000)
86
87
88struct net_local {
89 struct net_device_stats stats;
90 long open_time;
91
92
93
94
95
96
97 spinlock_t lock;
98};
99
100
101#define SA_ADDR0 0x00
102#define SA_ADDR1 0x42
103#define SA_ADDR2 0x65
104
105
106
107static int netcard_probe1(struct net_device *dev, int ioaddr);
108static int net_open(struct net_device *dev);
109static int net_send_packet(struct sk_buff *skb, struct net_device *dev);
110static irqreturn_t net_interrupt(int irq, void *dev_id);
111static void net_rx(struct net_device *dev);
112static int net_close(struct net_device *dev);
113static struct net_device_stats *net_get_stats(struct net_device *dev);
114static void set_multicast_list(struct net_device *dev);
115static void net_tx_timeout(struct net_device *dev);
116
117
118
119#define tx_done(dev) 1
120static void hardware_send_packet(short ioaddr, char *buf, int length);
121static void chipset_init(struct net_device *dev, int startp);
122
123
124
125
126
127
128
129
130static int __init do_netcard_probe(struct net_device *dev)
131{
132 int i;
133 int base_addr = dev->base_addr;
134 int irq = dev->irq;
135
136 if (base_addr > 0x1ff)
137 return netcard_probe1(dev, base_addr);
138 else if (base_addr != 0)
139 return -ENXIO;
140
141 for (i = 0; netcard_portlist[i]; i++) {
142 int ioaddr = netcard_portlist[i];
143 if (netcard_probe1(dev, ioaddr) == 0)
144 return 0;
145 dev->irq = irq;
146 }
147
148 return -ENODEV;
149}
150
151static void cleanup_card(struct net_device *dev)
152{
153#ifdef jumpered_dma
154 free_dma(dev->dma);
155#endif
156#ifdef jumpered_interrupts
157 free_irq(dev->irq, dev);
158#endif
159 release_region(dev->base_addr, NETCARD_IO_EXTENT);
160}
161
162#ifndef MODULE
163struct net_device * __init netcard_probe(int unit)
164{
165 struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
166 int err;
167
168 if (!dev)
169 return ERR_PTR(-ENOMEM);
170
171 sprintf(dev->name, "eth%d", unit);
172 netdev_boot_setup_check(dev);
173
174 err = do_netcard_probe(dev);
175 if (err)
176 goto out;
177 return dev;
178out:
179 free_netdev(dev);
180 return ERR_PTR(err);
181}
182#endif
183
184
185
186
187
188
189static int __init netcard_probe1(struct net_device *dev, int ioaddr)
190{
191 struct net_local *np;
192 static unsigned version_printed;
193 int i;
194 int err = -ENODEV;
195 DECLARE_MAC_BUF(mac);
196
197
198 if (!request_region(ioaddr, NETCARD_IO_EXTENT, cardname))
199 return -EBUSY;
200
201
202
203
204
205
206 if (inb(ioaddr + 0) != SA_ADDR0
207 || inb(ioaddr + 1) != SA_ADDR1
208 || inb(ioaddr + 2) != SA_ADDR2)
209 goto out;
210
211 if (net_debug && version_printed++ == 0)
212 printk(KERN_DEBUG "%s", version);
213
214 printk(KERN_INFO "%s: %s found at %#3x, ", dev->name, cardname, ioaddr);
215
216
217 dev->base_addr = ioaddr;
218
219
220 for (i = 0; i < 6; i++)
221 dev->dev_addr[i] = inb(ioaddr + i);
222
223 printk("%s", print_mac(mac, dev->dev_addr));
224
225 err = -EAGAIN;
226#ifdef jumpered_interrupts
227
228
229
230
231
232
233
234
235 if (dev->irq == -1)
236 ;
237 else if (dev->irq < 2) {
238 unsigned long irq_mask = probe_irq_on();
239
240
241 dev->irq = probe_irq_off(irq_mask);
242 if (net_debug >= 2)
243 printk(" autoirq is %d", dev->irq);
244 } else if (dev->irq == 2)
245
246
247
248
249 dev->irq = 9;
250
251 {
252 int irqval = request_irq(dev->irq, &net_interrupt, 0, cardname, dev);
253 if (irqval) {
254 printk("%s: unable to get IRQ %d (irqval=%d).\n",
255 dev->name, dev->irq, irqval);
256 goto out;
257 }
258 }
259#endif
260#ifdef jumpered_dma
261
262
263
264
265 if (dev->dma == 0) {
266 if (request_dma(dev->dma, cardname)) {
267 printk("DMA %d allocation failed.\n", dev->dma);
268 goto out1;
269 } else
270 printk(", assigned DMA %d.\n", dev->dma);
271 } else {
272 short dma_status, new_dma_status;
273
274
275 dma_status = ((inb(DMA1_STAT_REG) >> 4) & 0x0f) |
276 (inb(DMA2_STAT_REG) & 0xf0);
277
278 outw(0x1234, ioaddr + 8);
279
280 new_dma_status = ((inb(DMA1_STAT_REG) >> 4) & 0x0f) |
281 (inb(DMA2_STAT_REG) & 0xf0);
282
283
284
285
286 new_dma_status ^= dma_status;
287 new_dma_status &= ~0x10;
288 for (i = 7; i > 0; i--)
289 if (test_bit(i, &new_dma_status)) {
290 dev->dma = i;
291 break;
292 }
293 if (i <= 0) {
294 printk("DMA probe failed.\n");
295 goto out1;
296 }
297 if (request_dma(dev->dma, cardname)) {
298 printk("probed DMA %d allocation failed.\n", dev->dma);
299 goto out1;
300 }
301 }
302#endif
303
304 np = netdev_priv(dev);
305 spin_lock_init(&np->lock);
306
307 dev->open = net_open;
308 dev->stop = net_close;
309 dev->hard_start_xmit = net_send_packet;
310 dev->get_stats = net_get_stats;
311 dev->set_multicast_list = &set_multicast_list;
312
313 dev->tx_timeout = &net_tx_timeout;
314 dev->watchdog_timeo = MY_TX_TIMEOUT;
315
316 err = register_netdev(dev);
317 if (err)
318 goto out2;
319 return 0;
320out2:
321#ifdef jumpered_dma
322 free_dma(dev->dma);
323#endif
324out1:
325#ifdef jumpered_interrupts
326 free_irq(dev->irq, dev);
327#endif
328out:
329 release_region(base_addr, NETCARD_IO_EXTENT);
330 return err;
331}
332
333static void net_tx_timeout(struct net_device *dev)
334{
335 struct net_local *np = netdev_priv(dev);
336
337 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
338 tx_done(dev) ? "IRQ conflict" : "network cable problem");
339
340
341 chipset_init(dev, 1);
342
343 np->stats.tx_errors++;
344
345
346
347
348
349
350
351
352
353
354
355 if (!tx_full(dev))
356 netif_wake_queue(dev);
357}
358
359
360
361
362
363
364
365
366
367static int
368net_open(struct net_device *dev)
369{
370 struct net_local *np = netdev_priv(dev);
371 int ioaddr = dev->base_addr;
372
373
374
375
376 if (request_irq(dev->irq, &net_interrupt, 0, cardname, dev)) {
377 return -EAGAIN;
378 }
379
380
381
382
383 if (request_dma(dev->dma, cardname)) {
384 free_irq(dev->irq, dev);
385 return -EAGAIN;
386 }
387
388
389 chipset_init(dev, 1);
390 outb(0x00, ioaddr);
391 np->open_time = jiffies;
392
393
394
395
396 netif_start_queue(dev);
397
398 return 0;
399}
400
401
402
403
404
405
406static int net_send_packet(struct sk_buff *skb, struct net_device *dev)
407{
408 struct net_local *np = netdev_priv(dev);
409 int ioaddr = dev->base_addr;
410 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
411 unsigned char *buf = skb->data;
412
413
414
415
416
417
418
419
420
421
422#if TX_RING
423
424
425
426
427
428 spin_lock_irq(&np->lock);
429
430 add_to_tx_ring(np, skb, length);
431 dev->trans_start = jiffies;
432
433
434
435
436
437 if (tx_full(dev))
438 netif_stop_queue(dev);
439
440
441
442
443
444 spin_unlock_irq(&np->lock);
445#else
446
447
448
449
450
451
452
453
454 hardware_send_packet(ioaddr, buf, length);
455 np->stats.tx_bytes += skb->len;
456
457 dev->trans_start = jiffies;
458
459
460 if (inw(ioaddr) == 81)
461 np->stats.tx_aborted_errors++;
462 dev_kfree_skb (skb);
463#endif
464
465 return 0;
466}
467
468#if TX_RING
469
470
471
472void net_tx(struct net_device *dev)
473{
474 struct net_local *np = netdev_priv(dev);
475 int entry;
476
477
478
479
480 spin_lock(&np->lock);
481
482 entry = np->tx_old;
483 while (tx_entry_is_sent(np, entry)) {
484 struct sk_buff *skb = np->skbs[entry];
485
486 np->stats.tx_bytes += skb->len;
487 dev_kfree_skb_irq (skb);
488
489 entry = next_tx_entry(np, entry);
490 }
491 np->tx_old = entry;
492
493
494
495
496
497 if (netif_queue_stopped(dev) && ! tx_full(dev))
498 netif_wake_queue(dev);
499
500 spin_unlock(&np->lock);
501}
502#endif
503
504
505
506
507
508static irqreturn_t net_interrupt(int irq, void *dev_id)
509{
510 struct net_device *dev = dev_id;
511 struct net_local *np;
512 int ioaddr, status;
513 int handled = 0;
514
515 ioaddr = dev->base_addr;
516
517 np = netdev_priv(dev);
518 status = inw(ioaddr + 0);
519
520 if (status == 0)
521 goto out;
522 handled = 1;
523
524 if (status & RX_INTR) {
525
526 net_rx(dev);
527 }
528#if TX_RING
529 if (status & TX_INTR) {
530
531 net_tx(dev);
532 np->stats.tx_packets++;
533 netif_wake_queue(dev);
534 }
535#endif
536 if (status & COUNTERS_INTR) {
537
538 np->stats.tx_window_errors++;
539 }
540out:
541 return IRQ_RETVAL(handled);
542}
543
544
545static void
546net_rx(struct net_device *dev)
547{
548 struct net_local *lp = netdev_priv(dev);
549 int ioaddr = dev->base_addr;
550 int boguscount = 10;
551
552 do {
553 int status = inw(ioaddr);
554 int pkt_len = inw(ioaddr);
555
556 if (pkt_len == 0)
557 break;
558
559 if (status & 0x40) {
560 lp->stats.rx_errors++;
561 if (status & 0x20) lp->stats.rx_frame_errors++;
562 if (status & 0x10) lp->stats.rx_over_errors++;
563 if (status & 0x08) lp->stats.rx_crc_errors++;
564 if (status & 0x04) lp->stats.rx_fifo_errors++;
565 } else {
566
567 struct sk_buff *skb;
568
569 lp->stats.rx_bytes+=pkt_len;
570
571 skb = dev_alloc_skb(pkt_len);
572 if (skb == NULL) {
573 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
574 dev->name);
575 lp->stats.rx_dropped++;
576 break;
577 }
578 skb->dev = dev;
579
580
581 memcpy(skb_put(skb,pkt_len), (void*)dev->rmem_start,
582 pkt_len);
583
584 insw(ioaddr, skb->data, (pkt_len + 1) >> 1);
585
586 netif_rx(skb);
587 dev->last_rx = jiffies;
588 lp->stats.rx_packets++;
589 lp->stats.rx_bytes += pkt_len;
590 }
591 } while (--boguscount);
592
593 return;
594}
595
596
597static int
598net_close(struct net_device *dev)
599{
600 struct net_local *lp = netdev_priv(dev);
601 int ioaddr = dev->base_addr;
602
603 lp->open_time = 0;
604
605 netif_stop_queue(dev);
606
607
608
609 disable_dma(dev->dma);
610
611
612 outw(0x00, ioaddr+0);
613
614 free_irq(dev->irq, dev);
615 free_dma(dev->dma);
616
617
618
619 return 0;
620
621}
622
623
624
625
626
627static struct net_device_stats *net_get_stats(struct net_device *dev)
628{
629 struct net_local *lp = netdev_priv(dev);
630 short ioaddr = dev->base_addr;
631
632
633 lp->stats.rx_missed_errors = inw(ioaddr+1);
634 return &lp->stats;
635}
636
637
638
639
640
641
642
643
644static void
645set_multicast_list(struct net_device *dev)
646{
647 short ioaddr = dev->base_addr;
648 if (dev->flags&IFF_PROMISC)
649 {
650
651 outw(MULTICAST|PROMISC, ioaddr);
652 }
653 else if((dev->flags&IFF_ALLMULTI) || dev->mc_count > HW_MAX_ADDRS)
654 {
655
656 hardware_set_filter(NULL);
657
658 outw(MULTICAST, ioaddr);
659 }
660 else if(dev->mc_count)
661 {
662
663 hardware_set_filter(dev->mc_list);
664
665 outw(MULTICAST, ioaddr);
666 }
667 else
668 outw(0, ioaddr);
669}
670
671#ifdef MODULE
672
673static struct net_device *this_device;
674static int io = 0x300;
675static int irq;
676static int dma;
677static int mem;
678MODULE_LICENSE("GPL");
679
680int init_module(void)
681{
682 struct net_device *dev;
683 int result;
684
685 if (io == 0)
686 printk(KERN_WARNING "%s: You shouldn't use auto-probing with insmod!\n",
687 cardname);
688 dev = alloc_etherdev(sizeof(struct net_local));
689 if (!dev)
690 return -ENOMEM;
691
692
693 dev->base_addr = io;
694 dev->irq = irq;
695 dev->dma = dma;
696 dev->mem_start = mem;
697 if (do_netcard_probe(dev) == 0) {
698 this_device = dev;
699 return 0;
700 }
701 free_netdev(dev);
702 return -ENXIO;
703}
704
705void
706cleanup_module(void)
707{
708 unregister_netdev(this_device);
709 cleanup_card(this_device);
710 free_netdev(this_device);
711}
712
713#endif
714
715
716
717
718
719
720
721
722
723
724
725
726