1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90#define DRV_NAME "3c501"
91#define DRV_VERSION "2001/11/17"
92
93
94static const char version[] =
95 DRV_NAME ".c: " DRV_VERSION " Alan Cox (alan@redhat.com).\n";
96
97
98
99
100
101
102#include <linux/module.h>
103
104#include <linux/kernel.h>
105#include <linux/sched.h>
106#include <linux/fcntl.h>
107#include <linux/ioport.h>
108#include <linux/interrupt.h>
109#include <linux/slab.h>
110#include <linux/string.h>
111#include <linux/errno.h>
112#include <linux/config.h>
113#include <linux/spinlock.h>
114#include <linux/ethtool.h>
115
116#include <asm/uaccess.h>
117#include <asm/bitops.h>
118#include <asm/io.h>
119
120#include <linux/netdevice.h>
121#include <linux/etherdevice.h>
122#include <linux/skbuff.h>
123#include <linux/init.h>
124
125
126
127static unsigned int netcard_portlist[] __initdata = {
128 0x280, 0x300, 0
129};
130
131
132
133
134
135
136int el1_probe(struct net_device *dev);
137static int el1_probe1(struct net_device *dev, int ioaddr);
138static int el_open(struct net_device *dev);
139static void el_timeout(struct net_device *dev);
140static int el_start_xmit(struct sk_buff *skb, struct net_device *dev);
141static void el_interrupt(int irq, void *dev_id, struct pt_regs *regs);
142static void el_receive(struct net_device *dev);
143static void el_reset(struct net_device *dev);
144static int el1_close(struct net_device *dev);
145static struct net_device_stats *el1_get_stats(struct net_device *dev);
146static void set_multicast_list(struct net_device *dev);
147static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
148
149#define EL1_IO_EXTENT 16
150
151#ifndef EL_DEBUG
152#define EL_DEBUG 0
153#endif
154#define debug el_debug
155static int el_debug = EL_DEBUG;
156
157
158
159
160
161struct net_local
162{
163 struct net_device_stats stats;
164 int tx_pkt_start;
165 int collisions;
166 int loading;
167 int txing;
168 spinlock_t lock;
169};
170
171
172#define RX_STATUS (ioaddr + 0x06)
173#define RX_CMD RX_STATUS
174#define TX_STATUS (ioaddr + 0x07)
175#define TX_CMD TX_STATUS
176#define GP_LOW (ioaddr + 0x08)
177#define GP_HIGH (ioaddr + 0x09)
178#define RX_BUF_CLR (ioaddr + 0x0A)
179#define RX_LOW (ioaddr + 0x0A)
180#define RX_HIGH (ioaddr + 0x0B)
181#define SAPROM (ioaddr + 0x0C)
182#define AX_STATUS (ioaddr + 0x0E)
183#define AX_CMD AX_STATUS
184#define DATAPORT (ioaddr + 0x0F)
185#define TX_RDY 0x08
186
187#define EL1_DATAPTR 0x08
188#define EL1_RXPTR 0x0A
189#define EL1_SAPROM 0x0C
190#define EL1_DATAPORT 0x0f
191
192
193
194
195
196#define AX_OFF 0x00
197#define AX_SYS 0x40
198#define AX_XMIT 0x44
199#define AX_RX 0x48
200#define AX_LOOP 0x0C
201#define AX_RESET 0x80
202
203
204
205
206
207
208#define RX_NORM 0xA8
209#define RX_PROM 0x68
210#define RX_MULT 0xE8
211#define TX_NORM 0x0A
212
213
214
215
216
217#define TX_COLLISION 0x02
218#define TX_16COLLISIONS 0x04
219#define TX_READY 0x08
220
221#define RX_RUNT 0x08
222#define RX_MISSED 0x01
223#define RX_GOOD 0x30
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243int __init el1_probe(struct net_device *dev)
244{
245 int i;
246 int base_addr = dev->base_addr;
247
248 SET_MODULE_OWNER(dev);
249
250 if (base_addr > 0x1ff)
251 return el1_probe1(dev, base_addr);
252 else if (base_addr != 0)
253 return -ENXIO;
254
255 for (i = 0; netcard_portlist[i]; i++)
256 if (el1_probe1(dev, netcard_portlist[i]) == 0)
257 return 0;
258
259 return -ENODEV;
260}
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275static int __init el1_probe1(struct net_device *dev, int ioaddr)
276{
277 struct net_local *lp;
278 const char *mname;
279 unsigned char station_addr[6];
280 int autoirq = 0;
281 int i;
282
283
284
285
286
287 if (!request_region(ioaddr, EL1_IO_EXTENT, dev->name))
288 return -ENODEV;
289
290
291
292
293
294 for (i = 0; i < 6; i++)
295 {
296 outw(i, ioaddr + EL1_DATAPTR);
297 station_addr[i] = inb(ioaddr + EL1_SAPROM);
298 }
299
300
301
302
303
304 if (station_addr[0] == 0x02 && station_addr[1] == 0x60
305 && station_addr[2] == 0x8c)
306 {
307 mname = "3c501";
308 } else if (station_addr[0] == 0x00 && station_addr[1] == 0x80
309 && station_addr[2] == 0xC8)
310 {
311 mname = "NP943";
312 }
313 else {
314 release_region(ioaddr, EL1_IO_EXTENT);
315 return -ENODEV;
316 }
317
318
319
320
321
322
323 if (dev->irq < 2)
324 {
325 unsigned long irq_mask, delay;
326
327 irq_mask = probe_irq_on();
328 inb(RX_STATUS);
329 inb(TX_STATUS);
330 outb(AX_LOOP + 1, AX_CMD);
331
332 outb(0x00, AX_CMD);
333
334 delay = jiffies + HZ/50;
335 while (time_before(jiffies, delay)) ;
336 autoirq = probe_irq_off(irq_mask);
337
338 if (autoirq == 0)
339 {
340 printk(KERN_WARNING "%s probe at %#x failed to detect IRQ line.\n",
341 mname, ioaddr);
342 release_region(ioaddr, EL1_IO_EXTENT);
343 return -EAGAIN;
344 }
345 }
346
347 outb(AX_RESET+AX_LOOP, AX_CMD);
348 dev->base_addr = ioaddr;
349 memcpy(dev->dev_addr, station_addr, ETH_ALEN);
350
351 if (dev->mem_start & 0xf)
352 el_debug = dev->mem_start & 0x7;
353 if (autoirq)
354 dev->irq = autoirq;
355
356 printk(KERN_INFO "%s: %s EtherLink at %#lx, using %sIRQ %d.\n", dev->name, mname, dev->base_addr,
357 autoirq ? "auto":"assigned ", dev->irq);
358
359#ifdef CONFIG_IP_MULTICAST
360 printk(KERN_WARNING "WARNING: Use of the 3c501 in a multicast kernel is NOT recommended.\n");
361#endif
362
363 if (el_debug)
364 printk(KERN_DEBUG "%s", version);
365
366
367
368
369
370 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
371 if (dev->priv == NULL) {
372 release_region(ioaddr, EL1_IO_EXTENT);
373 return -ENOMEM;
374 }
375 memset(dev->priv, 0, sizeof(struct net_local));
376
377 lp=dev->priv;
378 spin_lock_init(&lp->lock);
379
380
381
382
383
384 dev->open = &el_open;
385 dev->hard_start_xmit = &el_start_xmit;
386 dev->tx_timeout = &el_timeout;
387 dev->watchdog_timeo = HZ;
388 dev->stop = &el1_close;
389 dev->get_stats = &el1_get_stats;
390 dev->set_multicast_list = &set_multicast_list;
391 dev->do_ioctl = netdev_ioctl;
392
393
394
395
396
397 ether_setup(dev);
398
399 return 0;
400}
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415static int el_open(struct net_device *dev)
416{
417 int retval;
418 int ioaddr = dev->base_addr;
419 struct net_local *lp = (struct net_local *)dev->priv;
420 unsigned long flags;
421
422 if (el_debug > 2)
423 printk(KERN_DEBUG "%s: Doing el_open()...", dev->name);
424
425 if ((retval = request_irq(dev->irq, &el_interrupt, 0, dev->name, dev)))
426 return retval;
427
428 spin_lock_irqsave(&lp->lock, flags);
429 el_reset(dev);
430 spin_unlock_irqrestore(&lp->lock, flags);
431
432 lp->txing = 0;
433 outb(AX_RX, AX_CMD);
434 netif_start_queue(dev);
435 return 0;
436}
437
438
439
440
441
442
443
444
445
446
447static void el_timeout(struct net_device *dev)
448{
449 struct net_local *lp = (struct net_local *)dev->priv;
450 int ioaddr = dev->base_addr;
451
452 if (el_debug)
453 printk (KERN_DEBUG "%s: transmit timed out, txsr %#2x axsr=%02x rxsr=%02x.\n",
454 dev->name, inb(TX_STATUS), inb(AX_STATUS), inb(RX_STATUS));
455 lp->stats.tx_errors++;
456 outb(TX_NORM, TX_CMD);
457 outb(RX_NORM, RX_CMD);
458 outb(AX_OFF, AX_CMD);
459 outb(AX_RX, AX_CMD);
460 lp->txing = 0;
461 netif_wake_queue(dev);
462}
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487static int el_start_xmit(struct sk_buff *skb, struct net_device *dev)
488{
489 struct net_local *lp = (struct net_local *)dev->priv;
490 int ioaddr = dev->base_addr;
491 unsigned long flags;
492
493
494
495
496
497
498
499 spin_lock_irqsave(&lp->lock, flags);
500
501
502
503
504
505 netif_stop_queue(dev);
506
507 do
508 {
509 int gp_start = 0x800 - (ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN);
510 unsigned char *buf = skb->data;
511
512 lp->tx_pkt_start = gp_start;
513 lp->collisions = 0;
514
515 lp->stats.tx_bytes += skb->len;
516
517
518
519
520
521
522 outb_p(AX_SYS, AX_CMD);
523 inb_p(RX_STATUS);
524 inb_p(TX_STATUS);
525
526 lp->loading = 1;
527 lp->txing = 1;
528
529
530
531
532
533
534 spin_unlock_irqrestore(&lp->lock, flags);
535
536 outw(0x00, RX_BUF_CLR);
537 outw(gp_start, GP_LOW);
538 outsb(DATAPORT,buf,skb->len);
539 outw(gp_start, GP_LOW);
540
541 if(lp->loading != 2)
542 {
543 outb(AX_XMIT, AX_CMD);
544 lp->loading=0;
545 dev->trans_start = jiffies;
546 if (el_debug > 2)
547 printk(KERN_DEBUG " queued xmit.\n");
548 dev_kfree_skb (skb);
549 return 0;
550 }
551
552 if(el_debug>2)
553 printk(KERN_DEBUG "%s: burped during tx load.\n", dev->name);
554 spin_lock_irqsave(&lp->lock, flags);
555 }
556 while(1);
557
558}
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584static void el_interrupt(int irq, void *dev_id, struct pt_regs *regs)
585{
586 struct net_device *dev = dev_id;
587 struct net_local *lp;
588 int ioaddr;
589 int axsr;
590
591 ioaddr = dev->base_addr;
592 lp = (struct net_local *)dev->priv;
593
594 spin_lock(&lp->lock);
595
596
597
598
599
600 axsr = inb(AX_STATUS);
601
602
603
604
605
606 if (el_debug > 3)
607 printk(KERN_DEBUG "%s: el_interrupt() aux=%#02x", dev->name, axsr);
608
609 if(lp->loading==1 && !lp->txing)
610 printk(KERN_WARNING "%s: Inconsistent state loading while not in tx\n",
611 dev->name);
612
613 if (lp->txing)
614 {
615
616
617
618
619
620
621 int txsr = inb(TX_STATUS);
622
623 if(lp->loading==1)
624 {
625 if(el_debug > 2)
626 {
627 printk(KERN_DEBUG "%s: Interrupt while loading [", dev->name);
628 printk(KERN_DEBUG " txsr=%02x gp=%04x rp=%04x]\n", txsr, inw(GP_LOW),inw(RX_LOW));
629 }
630 lp->loading=2;
631 spin_unlock(&lp->lock);
632 return;
633 }
634
635 if (el_debug > 6)
636 printk(KERN_DEBUG " txsr=%02x gp=%04x rp=%04x", txsr, inw(GP_LOW),inw(RX_LOW));
637
638 if ((axsr & 0x80) && (txsr & TX_READY) == 0)
639 {
640
641
642
643
644 if(el_debug>1)
645 printk(KERN_DEBUG "%s: Unusual interrupt during Tx, txsr=%02x axsr=%02x"
646 " gp=%03x rp=%03x.\n", dev->name, txsr, axsr,
647 inw(ioaddr + EL1_DATAPTR), inw(ioaddr + EL1_RXPTR));
648 lp->txing = 0;
649 netif_wake_queue(dev);
650 }
651 else if (txsr & TX_16COLLISIONS)
652 {
653
654
655
656 if (el_debug)
657 printk (KERN_DEBUG "%s: Transmit failed 16 times, Ethernet jammed?\n",dev->name);
658 outb(AX_SYS, AX_CMD);
659 lp->txing = 0;
660 lp->stats.tx_aborted_errors++;
661 netif_wake_queue(dev);
662 }
663 else if (txsr & TX_COLLISION)
664 {
665
666
667
668
669 if (el_debug > 6)
670 printk(KERN_DEBUG " retransmitting after a collision.\n");
671
672
673
674
675 outb(AX_SYS, AX_CMD);
676 outw(lp->tx_pkt_start, GP_LOW);
677 outb(AX_XMIT, AX_CMD);
678 lp->stats.collisions++;
679 spin_unlock(&lp->lock);
680 return;
681 }
682 else
683 {
684
685
686
687 lp->stats.tx_packets++;
688 if (el_debug > 6)
689 printk(KERN_DEBUG " Tx succeeded %s\n",
690 (txsr & TX_RDY) ? "." : "but tx is busy!");
691
692
693
694
695 lp->txing = 0;
696 netif_wake_queue(dev);
697 }
698 }
699 else
700 {
701
702
703
704
705 int rxsr = inb(RX_STATUS);
706 if (el_debug > 5)
707 printk(KERN_DEBUG " rxsr=%02x txsr=%02x rp=%04x", rxsr, inb(TX_STATUS),inw(RX_LOW));
708
709
710
711 if (rxsr & RX_MISSED)
712 lp->stats.rx_missed_errors++;
713 else if (rxsr & RX_RUNT)
714 {
715 lp->stats.rx_length_errors++;
716 if (el_debug > 5)
717 printk(KERN_DEBUG " runt.\n");
718 }
719 else if (rxsr & RX_GOOD)
720 {
721
722
723
724 el_receive(dev);
725 }
726 else
727 {
728
729
730
731 if (el_debug > 2)
732 printk(KERN_DEBUG "%s: No packet seen, rxsr=%02x **resetting 3c501***\n",
733 dev->name, rxsr);
734 el_reset(dev);
735 }
736 if (el_debug > 3)
737 printk(KERN_DEBUG ".\n");
738 }
739
740
741
742
743
744 outb(AX_RX, AX_CMD);
745 outw(0x00, RX_BUF_CLR);
746 inb(RX_STATUS);
747 inb(TX_STATUS);
748 spin_unlock(&lp->lock);
749 return;
750}
751
752
753
754
755
756
757
758
759
760
761
762
763static void el_receive(struct net_device *dev)
764{
765 struct net_local *lp = (struct net_local *)dev->priv;
766 int ioaddr = dev->base_addr;
767 int pkt_len;
768 struct sk_buff *skb;
769
770 pkt_len = inw(RX_LOW);
771
772 if (el_debug > 4)
773 printk(KERN_DEBUG " el_receive %d.\n", pkt_len);
774
775 if ((pkt_len < 60) || (pkt_len > 1536))
776 {
777 if (el_debug)
778 printk(KERN_DEBUG "%s: bogus packet, length=%d\n", dev->name, pkt_len);
779 lp->stats.rx_over_errors++;
780 return;
781 }
782
783
784
785
786
787 outb(AX_SYS, AX_CMD);
788 skb = dev_alloc_skb(pkt_len+2);
789
790
791
792
793
794 outw(0x00, GP_LOW);
795 if (skb == NULL)
796 {
797 printk(KERN_INFO "%s: Memory squeeze, dropping packet.\n", dev->name);
798 lp->stats.rx_dropped++;
799 return;
800 }
801 else
802 {
803 skb_reserve(skb,2);
804 skb->dev = dev;
805
806
807
808
809
810 insb(DATAPORT, skb_put(skb,pkt_len), pkt_len);
811 skb->protocol=eth_type_trans(skb,dev);
812 netif_rx(skb);
813 dev->last_rx = jiffies;
814 lp->stats.rx_packets++;
815 lp->stats.rx_bytes+=pkt_len;
816 }
817 return;
818}
819
820
821
822
823
824
825
826
827
828
829static void el_reset(struct net_device *dev)
830{
831 struct net_local *lp = (struct net_local *)dev->priv;
832 int ioaddr = dev->base_addr;
833
834 if (el_debug> 2)
835 printk(KERN_INFO "3c501 reset...");
836 outb(AX_RESET, AX_CMD);
837 outb(AX_LOOP, AX_CMD);
838 {
839 int i;
840 for (i = 0; i < 6; i++)
841 outb(dev->dev_addr[i], ioaddr + i);
842 }
843
844 outw(0, RX_BUF_CLR);
845 outb(TX_NORM, TX_CMD);
846 outb(RX_NORM, RX_CMD);
847 inb(RX_STATUS);
848 inb(TX_STATUS);
849 lp->txing = 0;
850}
851
852
853
854
855
856
857
858
859
860
861
862
863static int el1_close(struct net_device *dev)
864{
865 int ioaddr = dev->base_addr;
866
867 if (el_debug > 2)
868 printk(KERN_INFO "%s: Shutting down Ethernet card at %#x.\n", dev->name, ioaddr);
869
870 netif_stop_queue(dev);
871
872
873
874
875
876 free_irq(dev->irq, dev);
877 outb(AX_RESET, AX_CMD);
878
879 return 0;
880}
881
882
883
884
885
886
887
888
889
890
891
892
893static struct net_device_stats *el1_get_stats(struct net_device *dev)
894{
895 struct net_local *lp = (struct net_local *)dev->priv;
896 return &lp->stats;
897}
898
899
900
901
902
903
904
905
906
907
908
909static void set_multicast_list(struct net_device *dev)
910{
911 int ioaddr = dev->base_addr;
912
913 if(dev->flags&IFF_PROMISC)
914 {
915 outb(RX_PROM, RX_CMD);
916 inb(RX_STATUS);
917 }
918 else if (dev->mc_list || dev->flags&IFF_ALLMULTI)
919 {
920 outb(RX_MULT, RX_CMD);
921 inb(RX_STATUS);
922 }
923 else
924 {
925 outb(RX_NORM, RX_CMD);
926 inb(RX_STATUS);
927 }
928}
929
930
931
932
933
934
935
936
937
938static int netdev_ethtool_ioctl (struct net_device *dev, void *useraddr)
939{
940 u32 ethcmd;
941
942
943
944
945 if (get_user(ethcmd, (u32 *)useraddr))
946 return -EFAULT;
947
948 switch (ethcmd) {
949
950 case ETHTOOL_GDRVINFO: {
951 struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
952 strcpy (info.driver, DRV_NAME);
953 strcpy (info.version, DRV_VERSION);
954 sprintf(info.bus_info, "ISA 0x%lx", dev->base_addr);
955 if (copy_to_user (useraddr, &info, sizeof (info)))
956 return -EFAULT;
957 return 0;
958 }
959
960
961 case ETHTOOL_GMSGLVL: {
962 struct ethtool_value edata = {ETHTOOL_GMSGLVL};
963 edata.data = debug;
964 if (copy_to_user(useraddr, &edata, sizeof(edata)))
965 return -EFAULT;
966 return 0;
967 }
968
969 case ETHTOOL_SMSGLVL: {
970 struct ethtool_value edata;
971 if (copy_from_user(&edata, useraddr, sizeof(edata)))
972 return -EFAULT;
973 debug = edata.data;
974 return 0;
975 }
976
977 default:
978 break;
979 }
980
981 return -EOPNOTSUPP;
982}
983
984
985
986
987
988
989
990
991
992
993static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
994{
995 int rc = 0;
996
997 switch (cmd) {
998 case SIOCETHTOOL:
999 rc = netdev_ethtool_ioctl(dev, (void *) rq->ifr_data);
1000 break;
1001
1002 default:
1003 rc = -EOPNOTSUPP;
1004 break;
1005 }
1006
1007 return rc;
1008}
1009
1010#ifdef MODULE
1011
1012static struct net_device dev_3c501 = {
1013 .init = el1_probe,
1014 .base_addr = 0x280,
1015 .irq = 5,
1016};
1017
1018static int io=0x280;
1019static int irq=5;
1020MODULE_PARM(io, "i");
1021MODULE_PARM(irq, "i");
1022MODULE_PARM_DESC(io, "EtherLink I/O base address");
1023MODULE_PARM_DESC(irq, "EtherLink IRQ number");
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037int init_module(void)
1038{
1039 dev_3c501.irq=irq;
1040 dev_3c501.base_addr=io;
1041 if (register_netdev(&dev_3c501) != 0)
1042 return -EIO;
1043 return 0;
1044}
1045
1046
1047
1048
1049
1050
1051
1052
1053void cleanup_module(void)
1054{
1055
1056
1057
1058
1059 unregister_netdev(&dev_3c501);
1060
1061
1062
1063
1064
1065 kfree(dev_3c501.priv);
1066 dev_3c501.priv = NULL;
1067
1068
1069
1070
1071 release_region(dev_3c501.base_addr, EL1_IO_EXTENT);
1072}
1073
1074#endif
1075MODULE_LICENSE("GPL");
1076
1077
1078
1079
1080
1081
1082
1083
1084