This source file includes following definitions.
- de620_ready
- de620_send_command
- de620_put_byte
- de620_read_byte
- de620_write_block
- de620_read_block
- de620_set_delay
- de620_set_register
- de620_get_register
- de620_open
- de620_close
- get_stats
- de620_set_multicast_list
- de620_start_xmit
- de620_interrupt
- de620_rx_intr
- adapter_init
- de620_probe
- ReadAWord
- read_eeprom
- init_module
- cleanup_module
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 static char *version =
42 "de620.c: $Revision: 1.31 $, Bjorn Ekwall <bj0rn@blox.se>\n";
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 #ifndef READ_DELAY
69 #define READ_DELAY 100
70 #endif
71
72 #ifndef WRITE_DELAY
73 #define WRITE_DELAY 100
74 #endif
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 #ifdef LOWSPEED
96
97
98
99
100
101
102 #endif
103
104 #include <linux/config.h>
105 #include <linux/kernel.h>
106 #include <linux/sched.h>
107 #include <linux/types.h>
108 #include <linux/fcntl.h>
109 #include <linux/string.h>
110 #include <linux/interrupt.h>
111 #include <asm/io.h>
112 #include <netinet/in.h>
113 #include <linux/ptrace.h>
114 #include <asm/system.h>
115 #include <errno.h>
116
117 #include <linux/inet.h>
118 #include <linux/netdevice.h>
119 #include <linux/etherdevice.h>
120 #include <linux/skbuff.h>
121
122 #ifdef MODULE
123 #include <linux/module.h>
124 #include "../../tools/version.h"
125 #endif
126
127
128 #include "de620.h"
129
130 #define netstats enet_statistics
131 typedef unsigned char byte;
132
133
134
135
136
137
138
139 #ifndef DE620_IO
140 #define DE620_IO 0x378
141 #endif
142
143 #ifndef DE620_IRQ
144 #define DE620_IRQ 7
145 #endif
146
147 #define DATA_PORT (DE620_IO)
148 #define STATUS_PORT (DE620_IO + 1)
149 #define COMMAND_PORT (DE620_IO + 2)
150
151 #define RUNT 60
152 #define GIANT 1514
153
154 #ifdef DE620_DEBUG
155 #define PRINTK(x) if (de620_debug >= 2) printk x
156 #else
157 #define DE620_DEBUG 0
158 #define PRINTK(x)
159 #endif
160
161
162
163
164
165
166
167
168
169
170
171
172 static int de620_open(struct device *);
173 static int de620_close(struct device *);
174 static struct netstats *get_stats(struct device *);
175 static void de620_set_multicast_list(struct device *, int, void *);
176 static int de620_start_xmit(struct sk_buff *, struct device *);
177
178
179 static void de620_interrupt(int);
180 static int de620_rx_intr(struct device *);
181
182
183 static int adapter_init(struct device *);
184 int de620_probe(struct device *);
185 static int read_eeprom(void);
186
187
188
189
190
191 #define SCR_DEF NIBBLEMODE |INTON | SLEEP | AUTOTX
192 #define TCR_DEF RXPB
193 #define DE620_RX_START_PAGE 12
194 #define DEF_NIC_CMD IRQEN | ICEN | DS1
195
196 extern struct device *irq2dev_map[16];
197 unsigned int de620_debug = DE620_DEBUG;
198
199 static volatile byte NIC_Cmd;
200 static volatile byte next_rx_page;
201 static byte first_rx_page;
202 static byte last_rx_page;
203 static byte EIPRegister;
204
205 static struct nic {
206 byte NodeID[6];
207 byte RAM_Size;
208 byte Model;
209 byte Media;
210 byte SCR;
211 } nic_data;
212
213
214
215
216
217
218 #define de620_tx_buffs() (inb(STATUS_PORT) & (TXBF0 | TXBF1))
219 #define de620_flip_ds() NIC_Cmd ^= DS0 | DS1; outb(NIC_Cmd, COMMAND_PORT);
220
221
222 #ifdef COUNT_LOOPS
223 static int tot_cnt;
224 #endif
225 static inline byte
226 de620_ready(void)
227 {
228 byte value;
229 register short int cnt = 0;
230
231 while ((((value = inb(STATUS_PORT)) & READY) == 0) && (cnt <= 1000))
232 ++cnt;
233
234 #ifdef COUNT_LOOPS
235 tot_cnt += cnt;
236 #endif
237 return value & 0xf0;
238 }
239
240 static inline void
241 de620_send_command(byte cmd)
242 {
243 de620_ready();
244 if (cmd == W_DUMMY)
245 outb(NIC_Cmd, COMMAND_PORT);
246
247 outb(cmd, DATA_PORT);
248
249 outb(NIC_Cmd ^ CS0, COMMAND_PORT);
250 de620_ready();
251 outb(NIC_Cmd, COMMAND_PORT);
252 }
253
254 static inline void
255 de620_put_byte(byte value)
256 {
257
258 de620_ready();
259 outb(value, DATA_PORT);
260 de620_flip_ds();
261 }
262
263 static inline byte
264 de620_read_byte(void)
265 {
266 byte value;
267
268
269 value = de620_ready();
270 de620_flip_ds();
271 value |= de620_ready() >> 4;
272 return value;
273 }
274
275 static inline void
276 de620_write_block(byte *buffer, int count)
277 {
278 #ifndef LOWSPEED
279 byte uflip = NIC_Cmd ^ (DS0 | DS1);
280 byte dflip = NIC_Cmd;
281 #else
282 #ifdef COUNT_LOOPS
283 int bytes = count;
284 #endif
285 #endif
286
287 #ifdef LOWSPEED
288 #ifdef COUNT_LOOPS
289 tot_cnt = 0;
290 #endif
291
292 for ( ; count > 0; --count, ++buffer) {
293 de620_put_byte(*buffer);
294 }
295 de620_send_command(W_DUMMY);
296 #ifdef COUNT_LOOPS
297
298 printk("WRITE(%d)\n", tot_cnt/((bytes?bytes:1)));
299 #endif
300 #else
301 for ( ; count > 0; count -=2) {
302 outb(*buffer++, DATA_PORT);
303 outb(uflip, COMMAND_PORT);
304 outb(*buffer++, DATA_PORT);
305 outb(dflip, COMMAND_PORT);
306 }
307 de620_send_command(W_DUMMY);
308 #endif
309 }
310
311 static inline void
312 de620_read_block(byte *data, int count)
313 {
314 #ifndef LOWSPEED
315 byte value;
316 byte uflip = NIC_Cmd ^ (DS0 | DS1);
317 byte dflip = NIC_Cmd;
318 #else
319 #ifdef COUNT_LOOPS
320 int bytes = count;
321
322 tot_cnt = 0;
323 #endif
324 #endif
325
326 #ifdef LOWSPEED
327
328 while (count-- > 0) {
329 *data++ = de620_read_byte();
330 de620_flip_ds();
331 }
332 #ifdef COUNT_LOOPS
333
334 printk("READ(%d)\n", tot_cnt/(2*(bytes?bytes:1)));
335 #endif
336 #else
337 while (count-- > 0) {
338 value = inb(STATUS_PORT) & 0xf0;
339 outb(uflip, COMMAND_PORT);
340 *data++ = value | inb(STATUS_PORT) >> 4;
341 outb(dflip , COMMAND_PORT);
342 }
343 #endif
344 }
345
346 static inline void
347 de620_set_delay(void)
348 {
349 de620_ready();
350 outb(W_DFR, DATA_PORT);
351 outb(NIC_Cmd ^ CS0, COMMAND_PORT);
352
353 de620_ready();
354 #ifdef LOWSPEED
355 outb(WRITE_DELAY, DATA_PORT);
356 #else
357 outb(0, DATA_PORT);
358 #endif
359 de620_flip_ds();
360
361 de620_ready();
362 #ifdef LOWSPEED
363 outb(READ_DELAY, DATA_PORT);
364 #else
365 outb(0, DATA_PORT);
366 #endif
367 de620_flip_ds();
368 }
369
370 static inline void
371 de620_set_register(byte reg, byte value)
372 {
373 de620_ready();
374 outb(reg, DATA_PORT);
375 outb(NIC_Cmd ^ CS0, COMMAND_PORT);
376
377 de620_put_byte(value);
378 }
379
380 static inline byte
381 de620_get_register(byte reg)
382 {
383 byte value;
384
385 de620_send_command(reg);
386 value = de620_read_byte();
387 de620_send_command(W_DUMMY);
388
389 return value;
390 }
391
392
393
394
395
396
397
398
399
400
401 static int
402 de620_open(struct device *dev)
403 {
404 if (request_irq(DE620_IRQ, de620_interrupt, 0, "de620")) {
405 printk ("%s: unable to get IRQ %d\n", dev->name, DE620_IRQ);
406 return 1;
407 }
408 irq2dev_map[DE620_IRQ] = dev;
409
410 #ifdef MODULE
411 MOD_INC_USE_COUNT;
412 #endif
413 if (adapter_init(dev)) {
414 return 1;
415 }
416 dev->start = 1;
417 return 0;
418 }
419
420
421
422
423
424
425 static int
426 de620_close(struct device *dev)
427 {
428
429 de620_set_register(W_TCR, RXOFF);
430
431 free_irq(DE620_IRQ);
432 irq2dev_map[DE620_IRQ] = NULL;
433
434 dev->start = 0;
435 #ifdef MODULE
436 MOD_DEC_USE_COUNT;
437 #endif
438 return 0;
439 }
440
441
442
443
444
445
446 static struct netstats *
447 get_stats(struct device *dev)
448 {
449 return (struct netstats *)(dev->priv);
450 }
451
452
453
454
455
456
457
458
459
460
461
462 static void
463 de620_set_multicast_list(struct device *dev, int num_addrs, void *addrs)
464 {
465 if (num_addrs) {
466 de620_set_register(W_TCR, (TCR_DEF & ~RXPBM) | RXALL);
467 }
468 else {
469 de620_set_register(W_TCR, TCR_DEF);
470 }
471 }
472
473
474
475
476
477
478 static int
479 de620_start_xmit(struct sk_buff *skb, struct device *dev)
480 {
481 unsigned long flags;
482 int len;
483 int tickssofar;
484 byte *buffer = skb->data;
485 byte using_txbuf;
486
487
488
489
490
491
492
493 if (skb == NULL) {
494 dev_tint(dev);
495 return 0;
496 }
497
498 using_txbuf = de620_tx_buffs();
499 dev->tbusy = (using_txbuf == (TXBF0 | TXBF1));
500
501 if (dev->tbusy) {
502 tickssofar = jiffies - dev->trans_start;
503
504 if (tickssofar < 5)
505 return 1;
506
507
508 printk("%s: transmit timed out (%d), %s?\n",
509 dev->name,
510 tickssofar,
511 "network cable problem"
512 );
513
514 if (adapter_init(dev))
515 return 1;
516 }
517
518 if ((len = skb->len) < RUNT)
519 len = RUNT;
520 if (len & 1)
521 ++len;
522
523
524 save_flags(flags);
525 cli();
526
527 PRINTK(("de620_start_xmit: len=%d, bufs 0x%02x\n",
528 (int)skb->len, using_txbuf));
529
530
531 switch (using_txbuf) {
532 default:
533 case TXBF1:
534 de620_send_command(W_CR | RW0);
535 using_txbuf |= TXBF0;
536 break;
537
538 case TXBF0:
539 de620_send_command(W_CR | RW1);
540 using_txbuf |= TXBF1;
541 break;
542
543 case (TXBF0 | TXBF1):
544 printk("de620: Ouch! No tx-buffer available!\n");
545 restore_flags(flags);
546 return 1;
547 break;
548 }
549 de620_write_block(buffer, len);
550
551 dev->trans_start = jiffies;
552 dev->tbusy = (using_txbuf == (TXBF0 | TXBF1));
553
554 ((struct netstats *)(dev->priv))->tx_packets++;
555
556 restore_flags(flags);
557
558 dev_kfree_skb (skb, FREE_WRITE);
559
560 return 0;
561 }
562
563
564
565
566
567
568 static void
569 de620_interrupt(int reg_ptr)
570 {
571 int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
572 struct device *dev = irq2dev_map[irq];
573 byte irq_status;
574 int bogus_count = 0;
575 int again = 0;
576
577
578 if ((dev == NULL) || (DE620_IRQ != irq)) {
579 printk("%s: bogus interrupt %d\n", dev?dev->name:"DE620", irq);
580 return;
581 }
582
583 cli();
584 dev->interrupt = 1;
585
586
587 irq_status = de620_get_register(R_STS);
588
589 PRINTK(("de620_interrupt (%2.2X)\n", irq_status));
590
591 if (irq_status & RXGOOD) {
592 do {
593 again = de620_rx_intr(dev);
594 PRINTK(("again=%d\n", again));
595 }
596 while (again && (++bogus_count < 100));
597 }
598
599 dev->tbusy = (de620_tx_buffs() == (TXBF0 | TXBF1));
600
601 dev->interrupt = 0;
602 sti();
603 return;
604 }
605
606
607
608
609
610
611
612
613 static int
614 de620_rx_intr(struct device *dev)
615 {
616 struct header_buf {
617 byte status;
618 byte Rx_NextPage;
619 unsigned short Rx_ByteCount;
620 } header_buf;
621 struct sk_buff *skb;
622 int size;
623 byte *buffer;
624 byte pagelink;
625 byte curr_page;
626
627 PRINTK(("de620_rx_intr: next_rx_page = %d\n", next_rx_page));
628
629
630 de620_send_command(W_CR | RRN);
631 de620_set_register(W_RSA1, next_rx_page);
632 de620_set_register(W_RSA0, 0);
633
634
635 de620_read_block((byte *)&header_buf, sizeof(struct header_buf));
636 PRINTK(("page status=0x%02x, nextpage=%d, packetsize=%d\n",
637 header_buf.status, header_buf.Rx_NextPage, header_buf.Rx_ByteCount));
638
639
640 pagelink = header_buf.Rx_NextPage;
641 if ((pagelink < first_rx_page) || (last_rx_page < pagelink)) {
642
643 printk("%s: Ring overrun? Restoring...\n", dev->name);
644
645 adapter_init(dev);
646 ((struct netstats *)(dev->priv))->rx_over_errors++;
647 return 0;
648 }
649
650
651
652 pagelink = next_rx_page +
653 ((header_buf.Rx_ByteCount + (4 - 1 + 0x100)) >> 8);
654
655
656 if (pagelink > last_rx_page)
657 pagelink -= (last_rx_page - first_rx_page + 1);
658
659
660 if (pagelink != header_buf.Rx_NextPage) {
661
662 printk("%s: Page link out of sync! Restoring...\n", dev->name);
663 next_rx_page = header_buf.Rx_NextPage;
664 de620_send_command(W_DUMMY);
665 de620_set_register(W_NPRF, next_rx_page);
666 ((struct netstats *)(dev->priv))->rx_over_errors++;
667 return 0;
668 }
669 next_rx_page = pagelink;
670
671 size = header_buf.Rx_ByteCount - 4;
672 if ((size < RUNT) || (GIANT < size)) {
673 printk("%s: Illegal packet size: %d!\n", dev->name, size);
674 }
675 else {
676 skb = alloc_skb(size, GFP_ATOMIC);
677 if (skb == NULL) {
678 printk("%s: Couldn't allocate a sk_buff of size %d.\n",
679 dev->name, size);
680 ((struct netstats *)(dev->priv))->rx_dropped++;
681 }
682 else {
683 skb->len = size; skb->dev = dev; skb->free = 1;
684
685 buffer = skb->data;
686
687 de620_read_block(buffer, size);
688 PRINTK(("Read %d bytes\n", size));
689 netif_rx(skb);
690
691 ((struct netstats *)(dev->priv))->rx_packets++;
692 }
693 }
694
695
696
697 curr_page = de620_get_register(R_CPR);
698 de620_set_register(W_NPRF, next_rx_page);
699 PRINTK(("next_rx_page=%d CPR=%d\n", next_rx_page, curr_page));
700
701 return (next_rx_page != curr_page);
702 }
703
704
705
706
707
708
709 static int
710 adapter_init(struct device *dev)
711 {
712 int i;
713 static int was_down = 0;
714
715 if ((nic_data.Model == 3) || (nic_data.Model == 0)) {
716 EIPRegister = NCTL0;
717 if (nic_data.Media != 1)
718 EIPRegister |= NIS0;
719 }
720 else if (nic_data.Model == 2) {
721 EIPRegister = NCTL0 | NIS0;
722 }
723
724 de620_send_command(W_CR | RNOP | CLEAR);
725 de620_send_command(W_CR | RNOP);
726
727 de620_set_register(W_SCR, SCR_DEF);
728
729 de620_set_register(W_TCR, RXOFF);
730
731
732 for (i = 0; i < 6; ++i) {
733 de620_set_register(W_PAR0 + i, dev->dev_addr[i]);
734 }
735
736 de620_set_register(W_EIP, EIPRegister);
737
738 next_rx_page = first_rx_page = DE620_RX_START_PAGE;
739 if (nic_data.RAM_Size)
740 last_rx_page = nic_data.RAM_Size - 1;
741 else
742 last_rx_page = 255;
743
744 de620_set_register(W_SPR, first_rx_page);
745 de620_set_register(W_EPR, last_rx_page);
746 de620_set_register(W_CPR, first_rx_page);
747 de620_send_command(W_NPR | first_rx_page);
748 de620_send_command(W_DUMMY);
749 de620_set_delay();
750
751
752
753 #define CHECK_MASK ( 0 | TXSUC | T16 | 0 | RXCRC | RXSHORT | 0 | 0 )
754 #define CHECK_OK ( 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 )
755
756
757
758 if (((i = de620_get_register(R_STS)) & CHECK_MASK) != CHECK_OK) {
759 printk("Something has happened to the DE-620! Please check it"
760 #ifdef SHUTDOWN_WHEN_LOST
761 " and do a new ifconfig"
762 #endif
763 "! (%02x)\n", i);
764 #ifdef SHUTDOWN_WHEN_LOST
765
766 dev->flags &= ~IFF_UP;
767 de620_close(dev);
768 #endif
769 was_down = 1;
770 return 1;
771 }
772 if (was_down) {
773 printk("Thanks, I feel much better now!\n");
774 was_down = 0;
775 }
776
777
778 de620_set_register(W_TCR, TCR_DEF);
779
780 return 0;
781 }
782
783
784
785
786
787
788
789
790
791
792 int
793 de620_probe(struct device *dev)
794 {
795 static struct netstats de620_netstats;
796 int i;
797 byte checkbyte = 0xa5;
798
799 if (de620_debug)
800 printk(version);
801
802 printk("D-Link DE-620 pocket adapter");
803
804
805 NIC_Cmd = DEF_NIC_CMD;
806 de620_set_register(W_EIP, EIPRegister);
807
808
809 de620_set_register(W_CPR, checkbyte);
810 checkbyte = de620_get_register(R_CPR);
811
812 if ((checkbyte != 0xa5) || (read_eeprom() != 0)) {
813 printk(" not identified in the printer port\n");
814 return ENODEV;
815 }
816
817
818 printk(", Ethernet Address: %2.2X",
819 dev->dev_addr[0] = nic_data.NodeID[0]);
820 for (i = 1; i < ETH_ALEN; i++) {
821 printk(":%2.2X", dev->dev_addr[i] = nic_data.NodeID[i]);
822 dev->broadcast[i] = 0xff;
823 }
824
825 printk(" (%dk RAM,",
826 (nic_data.RAM_Size) ? (nic_data.RAM_Size >> 2) : 64);
827
828 if (nic_data.Media == 1)
829 printk(" BNC)\n");
830 else
831 printk(" UTP)\n");
832
833
834
835 dev->priv = &de620_netstats;
836
837 memset(dev->priv, 0, sizeof(struct netstats));
838 dev->get_stats = get_stats;
839 dev->open = de620_open;
840 dev->stop = de620_close;
841 dev->hard_start_xmit = &de620_start_xmit;
842 dev->set_multicast_list = &de620_set_multicast_list;
843 dev->base_addr = DE620_IO;
844 dev->irq = DE620_IRQ;
845
846 ether_setup(dev);
847
848
849 if (de620_debug) {
850 printk("\nEEPROM contents:\n");
851 printk("RAM_Size = 0x%02X\n", nic_data.RAM_Size);
852 printk("NodeID = %02X:%02X:%02X:%02X:%02X:%02X\n",
853 nic_data.NodeID[0], nic_data.NodeID[1],
854 nic_data.NodeID[2], nic_data.NodeID[3],
855 nic_data.NodeID[4], nic_data.NodeID[5]);
856 printk("Model = %d\n", nic_data.Model);
857 printk("Media = %d\n", nic_data.Media);
858 printk("SCR = 0x%02x\n", nic_data.SCR);
859 }
860
861 return 0;
862 }
863
864
865
866
867
868
869
870 #define sendit(data) de620_set_register(W_EIP, data | EIPRegister);
871
872 static unsigned short
873 ReadAWord(int from)
874 {
875 unsigned short data;
876 int nbits;
877
878
879
880
881 sendit(0); sendit(1); sendit(5); sendit(4);
882
883
884 for (nbits = 9; nbits > 0; --nbits, from <<= 1) {
885 if (from & 0x0100) {
886
887
888
889 sendit(6); sendit(7); sendit(7); sendit(6);
890 }
891 else {
892
893
894
895 sendit(4); sendit(5); sendit(5); sendit(4);
896 }
897 }
898
899
900 for (data = 0, nbits = 16; nbits > 0; --nbits) {
901
902
903
904 sendit(4); sendit(5); sendit(5); sendit(4);
905 data = (data << 1) | ((de620_get_register(R_STS) & EEDI) >> 7);
906 }
907
908
909
910 sendit(0); sendit(1); sendit(1); sendit(0);
911
912 return data;
913 }
914
915 static int
916 read_eeprom(void)
917 {
918 unsigned short wrd;
919
920
921 wrd = ReadAWord(0x1aa);
922 if (wrd != htons(0x0080))
923 return -1;
924 nic_data.NodeID[0] = wrd & 0xff;
925 nic_data.NodeID[1] = wrd >> 8;
926
927 wrd = ReadAWord(0x1ab);
928 if ((wrd & 0xff) != 0xc8)
929 return -1;
930 nic_data.NodeID[2] = wrd & 0xff;
931 nic_data.NodeID[3] = wrd >> 8;
932
933 wrd = ReadAWord(0x1ac);
934 nic_data.NodeID[4] = wrd & 0xff;
935 nic_data.NodeID[5] = wrd >> 8;
936
937 wrd = ReadAWord(0x1ad);
938 nic_data.RAM_Size = (wrd >> 8);
939
940 wrd = ReadAWord(0x1ae);
941 nic_data.Model = (wrd & 0xff);
942
943 wrd = ReadAWord(0x1af);
944 nic_data.Media = (wrd & 0xff);
945
946 wrd = ReadAWord(0x1a8);
947 nic_data.SCR = (wrd >> 8);
948
949 return 0;
950 }
951
952
953
954
955
956
957 #ifdef MODULE
958 char kernel_version[] = UTS_RELEASE;
959 static char nullname[8];
960 static struct device de620_dev = {
961 nullname, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, de620_probe };
962
963 int
964 init_module(void)
965 {
966 if (register_netdev(&de620_dev) != 0)
967 return -EIO;
968 return 0;
969 }
970
971 void
972 cleanup_module(void)
973 {
974 if (MOD_IN_USE)
975 printk("de620: device busy, remove delayed\n");
976 else
977 unregister_netdev(&de620_dev);
978 }
979 #endif
980
981
982
983
984
985
986
987
988