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