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