This source file includes following definitions.
- de600_read_status
- de600_read_byte
- de600_open
- de600_close
- get_stats
- trigger_interrupt
- de600_start_xmit
- de600_interrupt
- de600_tx_intr
- de600_rx_intr
- de600_probe
- adapter_init
- de600_rspace
- init_module
- cleanup_module
1 static char *version =
2 "de600.c: $Revision: 1.40 $, Bjorn Ekwall (bj0rn@blox.se)\n";
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 #define DE600_SLOW_DOWN SLOW_DOWN_IO; SLOW_DOWN_IO; SLOW_DOWN_IO
44
45
46
47
48
49
50 #define SLOW_IO_BY_JUMPING
51
52
53
54
55
56
57
58
59 #define CHECK_LOST_DE600
60
61
62
63
64
65
66
67
68
69 #define SHUTDOWN_WHEN_LOST
70
71
72
73
74
75
76
77
78
79
80
81
82 #undef FAKE_SMALL_MAX
83
84
85 #ifdef DE600_DEBUG
86 #define PRINTK(x) if (de600_debug >= 2) printk x
87 #else
88 #define DE600_DEBUG 0
89 #define PRINTK(x)
90 #endif
91 unsigned int de600_debug = DE600_DEBUG;
92
93 #include <linux/config.h>
94 #include <linux/kernel.h>
95 #include <linux/sched.h>
96 #include <linux/types.h>
97 #include <linux/fcntl.h>
98 #include <linux/string.h>
99 #include <linux/interrupt.h>
100 #include <asm/io.h>
101 #include <netinet/in.h>
102 #include <linux/ptrace.h>
103 #include <asm/system.h>
104 #include <errno.h>
105
106 #include <linux/inet.h>
107 #include <linux/netdevice.h>
108 #include <linux/etherdevice.h>
109 #include <linux/skbuff.h>
110
111 #ifdef MODULE
112 #include <linux/module.h>
113 #include "../../tools/version.h"
114 #endif
115
116 #ifdef FAKE_SMALL_MAX
117 static unsigned long de600_rspace(struct sock *sk);
118 #include "../../net/inet/sock.h"
119 #endif
120
121 #define netstats enet_statistics
122 typedef unsigned char byte;
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 #ifndef DE600_IO
142 #define DE600_IO 0x378
143 #endif
144
145 #define DATA_PORT (DE600_IO)
146 #define STATUS_PORT (DE600_IO + 1)
147 #define COMMAND_PORT (DE600_IO + 2)
148
149 #ifndef DE600_IRQ
150 #define DE600_IRQ 7
151 #endif
152
153
154
155
156
157
158
159
160
161
162
163
164 #define SELECT_NIC 0x04
165 #define SELECT_PRN 0x1c
166 #define NML_PRN 0xec
167 #define IRQEN 0x10
168
169
170
171
172 #define RX_BUSY 0x80
173 #define RX_GOOD 0x40
174 #define TX_FAILED16 0x10
175 #define TX_BUSY 0x08
176
177
178
179
180
181
182
183 #define WRITE_DATA 0x00
184 #define READ_DATA 0x01
185 #define STATUS 0x02
186 #define COMMAND 0x03
187 #define NULL_COMMAND 0x04
188 #define RX_LEN 0x05
189 #define TX_ADDR 0x06
190 #define RW_ADDR 0x07
191 #define HI_NIBBLE 0x08
192
193
194
195
196
197 #define RX_ALL 0x01
198 #define RX_BP 0x02
199 #define RX_MBP 0x03
200
201 #define TX_ENABLE 0x04
202 #define RX_ENABLE 0x08
203
204 #define RESET 0x80
205 #define STOP_RESET 0x00
206
207
208
209
210
211 #define RX_PAGE2_SELECT 0x10
212 #define RX_BASE_PAGE 0x20
213 #define FLIP_IRQ 0x40
214
215
216
217
218
219
220
221
222
223
224
225
226 #define MEM_2K 0x0800
227 #define MEM_4K 0x1000
228 #define MEM_6K 0x1800
229 #define NODE_ADDRESS 0x2000
230
231 #define RUNT 60
232
233
234
235
236
237
238
239
240
241
242
243 static byte de600_read_status(struct device *dev);
244 static byte de600_read_byte(unsigned char type, struct device *dev);
245
246
247 static int de600_open(struct device *dev);
248 static int de600_close(struct device *dev);
249 static struct netstats *get_stats(struct device *dev);
250 static int de600_start_xmit(struct sk_buff *skb, struct device *dev);
251
252
253 static void de600_interrupt(int reg_ptr);
254 static int de600_tx_intr(struct device *dev, int irq_status);
255 static void de600_rx_intr(struct device *dev);
256
257
258 static void trigger_interrupt(struct device *dev);
259 int de600_probe(struct device *dev);
260 static int adapter_init(struct device *dev);
261
262
263
264
265 extern struct device *irq2dev_map[16];
266 static volatile int rx_page = 0;
267
268 #define TX_PAGES 2
269 static volatile int tx_fifo[TX_PAGES];
270 static volatile int tx_fifo_in = 0;
271 static volatile int tx_fifo_out = 0;
272 static volatile int free_tx_pages = TX_PAGES;
273 static int was_down = 0;
274
275
276
277
278
279 #define select_prn() outb_p(SELECT_PRN, COMMAND_PORT); DE600_SLOW_DOWN
280 #define select_nic() outb_p(SELECT_NIC, COMMAND_PORT); DE600_SLOW_DOWN
281
282
283 #define de600_put_byte(data) ( \
284 outb_p(((data) << 4) | WRITE_DATA , DATA_PORT), \
285 outb_p(((data) & 0xf0) | WRITE_DATA | HI_NIBBLE, DATA_PORT))
286
287
288
289
290
291 #define de600_put_command(cmd) ( \
292 outb_p(( rx_page << 4) | COMMAND , DATA_PORT), \
293 outb_p(( rx_page & 0xf0) | COMMAND | HI_NIBBLE, DATA_PORT), \
294 outb_p(((rx_page | cmd) << 4) | COMMAND , DATA_PORT), \
295 outb_p(((rx_page | cmd) & 0xf0) | COMMAND | HI_NIBBLE, DATA_PORT))
296
297 #define de600_setup_address(addr,type) ( \
298 outb_p((((addr) << 4) & 0xf0) | type , DATA_PORT), \
299 outb_p(( (addr) & 0xf0) | type | HI_NIBBLE, DATA_PORT), \
300 outb_p((((addr) >> 4) & 0xf0) | type , DATA_PORT), \
301 outb_p((((addr) >> 8) & 0xf0) | type | HI_NIBBLE, DATA_PORT))
302
303 #define rx_page_adr() ((rx_page & RX_PAGE2_SELECT)?(MEM_6K):(MEM_4K))
304
305
306 #define next_rx_page() (rx_page ^= RX_PAGE2_SELECT)
307
308 #define tx_page_adr(a) (((a) + 1) * MEM_2K)
309
310 static inline byte
311 de600_read_status(struct device *dev)
312 {
313 byte status;
314
315 outb_p(STATUS, DATA_PORT);
316 status = inb(STATUS_PORT);
317 outb_p(NULL_COMMAND | HI_NIBBLE, DATA_PORT);
318
319 return status;
320 }
321
322 static inline byte
323 de600_read_byte(unsigned char type, struct device *dev) {
324 byte lo;
325
326 (void)outb_p((type), DATA_PORT);
327 lo = ((unsigned char)inb(STATUS_PORT)) >> 4;
328 (void)outb_p((type) | HI_NIBBLE, DATA_PORT);
329 return ((unsigned char)inb(STATUS_PORT) & (unsigned char)0xf0) | lo;
330 }
331
332
333
334
335
336
337
338
339
340 static int
341 de600_open(struct device *dev)
342 {
343 if (request_irq(DE600_IRQ, de600_interrupt, 0, "de600")) {
344 printk ("%s: unable to get IRQ %d\n", dev->name, DE600_IRQ);
345 return 1;
346 }
347 irq2dev_map[DE600_IRQ] = dev;
348
349 #ifdef MODULE
350 MOD_INC_USE_COUNT;
351 #endif
352 dev->start = 1;
353 if (adapter_init(dev)) {
354 return 1;
355 }
356
357 return 0;
358 }
359
360
361
362
363 static int
364 de600_close(struct device *dev)
365 {
366 select_nic();
367 rx_page = 0;
368 de600_put_command(RESET);
369 de600_put_command(STOP_RESET);
370 de600_put_command(0);
371 select_prn();
372
373 if (dev->start) {
374 free_irq(DE600_IRQ);
375 irq2dev_map[DE600_IRQ] = NULL;
376 dev->start = 0;
377 #ifdef MODULE
378 MOD_DEC_USE_COUNT;
379 #endif
380 }
381 return 0;
382 }
383
384 static struct netstats *
385 get_stats(struct device *dev)
386 {
387 return (struct netstats *)(dev->priv);
388 }
389
390 static inline void
391 trigger_interrupt(struct device *dev)
392 {
393 de600_put_command(FLIP_IRQ);
394 select_prn();
395 DE600_SLOW_DOWN;
396 select_nic();
397 de600_put_command(0);
398 }
399
400
401
402
403
404 static int
405 de600_start_xmit(struct sk_buff *skb, struct device *dev)
406 {
407 int transmit_from;
408 int len;
409 int tickssofar;
410 byte *buffer = skb->data;
411
412
413
414
415
416
417
418 if (skb == NULL) {
419 dev_tint(dev);
420 return 0;
421 }
422
423 if (free_tx_pages <= 0) {
424 tickssofar = jiffies - dev->trans_start;
425
426 if (tickssofar < 5)
427 return 1;
428
429
430 printk("%s: transmit timed out (%d), %s?\n",
431 dev->name,
432 tickssofar,
433 "network cable problem"
434 );
435
436 if (adapter_init(dev)) {
437 return 1;
438 }
439 }
440
441
442 PRINTK(("de600_start_xmit:len=%d, page %d/%d\n", skb->len, tx_fifo_in, free_tx_pages));
443
444 if ((len = skb->len) < RUNT)
445 len = RUNT;
446
447 cli();
448 select_nic();
449 tx_fifo[tx_fifo_in] = transmit_from = tx_page_adr(tx_fifo_in) - len;
450 tx_fifo_in = (tx_fifo_in + 1) % TX_PAGES;
451
452 #ifdef CHECK_LOST_DE600
453
454 de600_setup_address(NODE_ADDRESS, RW_ADDR);
455 de600_read_byte(READ_DATA, dev);
456 if (was_down || (de600_read_byte(READ_DATA, dev) != 0xde)) {
457 if (adapter_init(dev)) {
458 sti();
459 return 1;
460 }
461 }
462 #endif
463
464 de600_setup_address(transmit_from, RW_ADDR);
465 for ( ; len > 0; --len, ++buffer)
466 de600_put_byte(*buffer);
467
468 if (free_tx_pages-- == TX_PAGES) {
469 dev->trans_start = jiffies;
470 dev->tbusy = 0;
471
472 de600_setup_address(transmit_from, TX_ADDR);
473 de600_put_command(TX_ENABLE);
474 }
475 else {
476 dev->tbusy = !free_tx_pages;
477 select_prn();
478 }
479
480 sti();
481
482 #ifdef FAKE_SMALL_MAX
483
484 if (skb->sk && (skb->sk->protocol == IPPROTO_TCP) &&
485 (skb->sk->prot->rspace != &de600_rspace))
486 skb->sk->prot->rspace = de600_rspace;
487 #endif
488
489 dev_kfree_skb (skb, FREE_WRITE);
490
491 return 0;
492 }
493
494
495
496
497
498 static void
499 de600_interrupt(int reg_ptr)
500 {
501 int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
502 struct device *dev = irq2dev_map[irq];
503 byte irq_status;
504 int retrig = 0;
505 int boguscount = 0;
506
507
508 if ((dev == NULL) || (dev->start == 0) || (DE600_IRQ != irq)) {
509 printk("%s: bogus interrupt %d\n", dev?dev->name:"DE-600", irq);
510 return;
511 }
512
513 dev->interrupt = 1;
514 select_nic();
515 irq_status = de600_read_status(dev);
516
517 do {
518 PRINTK(("de600_interrupt (%02X)\n", irq_status));
519
520 if (irq_status & RX_GOOD)
521 de600_rx_intr(dev);
522 else if (!(irq_status & RX_BUSY))
523 de600_put_command(RX_ENABLE);
524
525
526 if (free_tx_pages < TX_PAGES)
527 retrig = de600_tx_intr(dev, irq_status);
528 else
529 retrig = 0;
530
531 irq_status = de600_read_status(dev);
532 } while ( (irq_status & RX_GOOD) || ((++boguscount < 100) && retrig) );
533
534
535
536
537
538
539
540 dev->interrupt = 0;
541 select_prn();
542
543 if (retrig)
544 trigger_interrupt(dev);
545
546 sti();
547 return;
548 }
549
550 static int
551 de600_tx_intr(struct device *dev, int irq_status)
552 {
553
554
555
556
557 mark_bh(NET_BH);
558
559 if (irq_status & TX_BUSY)
560 return 1;
561
562
563
564 if (!(irq_status & TX_FAILED16)) {
565 tx_fifo_out = (tx_fifo_out + 1) % TX_PAGES;
566 ++free_tx_pages;
567 ((struct netstats *)(dev->priv))->tx_packets++;
568 dev->tbusy = 0;
569 }
570
571
572 if ((free_tx_pages < TX_PAGES) || (irq_status & TX_FAILED16)) {
573 dev->trans_start = jiffies;
574 de600_setup_address(tx_fifo[tx_fifo_out], TX_ADDR);
575 de600_put_command(TX_ENABLE);
576 return 1;
577 }
578
579
580 return 0;
581 }
582
583
584
585
586 static void
587 de600_rx_intr(struct device *dev)
588 {
589 struct sk_buff *skb;
590 int i;
591 int read_from;
592 int size;
593 register unsigned char *buffer;
594
595 cli();
596
597 size = de600_read_byte(RX_LEN, dev);
598 size += (de600_read_byte(RX_LEN, dev) << 8);
599 size -= 4;
600
601
602 read_from = rx_page_adr();
603 next_rx_page();
604 de600_put_command(RX_ENABLE);
605 sti();
606
607 if ((size < 32) || (size > 1535)) {
608 printk("%s: Bogus packet size %d.\n", dev->name, size);
609 if (size > 10000)
610 adapter_init(dev);
611 return;
612 }
613
614 skb = alloc_skb(size, GFP_ATOMIC);
615 sti();
616 if (skb == NULL) {
617 printk("%s: Couldn't allocate a sk_buff of size %d.\n",
618 dev->name, size);
619 return;
620 }
621
622
623 skb->lock = 0;
624
625 buffer = skb->data;
626
627
628 de600_setup_address(read_from, RW_ADDR);
629 for (i = size; i > 0; --i, ++buffer)
630 *buffer = de600_read_byte(READ_DATA, dev);
631
632 ((struct netstats *)(dev->priv))->rx_packets++;
633
634 if (dev_rint((unsigned char *)skb, size, IN_SKBUFF, dev))
635 printk("%s: receive buffers full.\n", dev->name);
636
637
638
639
640
641 }
642
643 int
644 de600_probe(struct device *dev)
645 {
646 int i;
647 static struct netstats de600_netstats;
648
649
650 printk("%s: D-Link DE-600 pocket adapter", dev->name);
651
652 if (de600_debug > 1)
653 printk(version);
654
655
656 rx_page = 0;
657 select_nic();
658 (void)de600_read_status(dev);
659 de600_put_command(RESET);
660 de600_put_command(STOP_RESET);
661 if (de600_read_status(dev) & 0xf0) {
662 printk(": not at I/O %#3x.\n", DATA_PORT);
663 return ENODEV;
664 }
665
666
667
668
669
670
671
672 de600_setup_address(NODE_ADDRESS, RW_ADDR);
673 for (i = 0; i < ETH_ALEN; i++) {
674 dev->dev_addr[i] = de600_read_byte(READ_DATA, dev);
675 dev->broadcast[i] = 0xff;
676 }
677
678
679 if ((dev->dev_addr[1] == 0xde) && (dev->dev_addr[2] == 0x15)) {
680
681 dev->dev_addr[0] = 0x00;
682 dev->dev_addr[1] = 0x80;
683 dev->dev_addr[2] = 0xc8;
684 dev->dev_addr[3] &= 0x0f;
685 dev->dev_addr[3] |= 0x70;
686 } else {
687 printk(" not identified in the printer port\n");
688 return ENODEV;
689 }
690
691 printk(", Ethernet Address: %02X", dev->dev_addr[0]);
692 for (i = 1; i < ETH_ALEN; i++)
693 printk(":%02X",dev->dev_addr[i]);
694 printk("\n");
695
696
697
698 dev->priv = &de600_netstats;
699
700 memset(dev->priv, 0, sizeof(struct netstats));
701 dev->get_stats = get_stats;
702
703 dev->open = de600_open;
704 dev->stop = de600_close;
705 dev->hard_start_xmit = &de600_start_xmit;
706
707 ether_setup(dev);
708
709 select_prn();
710 return 0;
711 }
712
713 static int
714 adapter_init(struct device *dev)
715 {
716 int i;
717 long flags;
718
719 save_flags(flags);
720 cli();
721
722 select_nic();
723 rx_page = 0;
724 de600_put_command(RESET);
725 de600_put_command(STOP_RESET);
726 #ifdef CHECK_LOST_DE600
727
728
729 de600_setup_address(NODE_ADDRESS, RW_ADDR);
730 de600_read_byte(READ_DATA, dev);
731 if ((de600_read_byte(READ_DATA, dev) != 0xde) ||
732 (de600_read_byte(READ_DATA, dev) != 0x15)) {
733
734 printk("Something has happened to the DE-600! Please check it"
735 #ifdef SHUTDOWN_WHEN_LOST
736 " and do a new ifconfig"
737 #endif
738 "!\n");
739 #ifdef SHUTDOWN_WHEN_LOST
740
741 dev->flags &= ~IFF_UP;
742 de600_close(dev);
743 #endif
744 was_down = 1;
745 dev->tbusy = 1;
746 return 1;
747 }
748 #endif
749 if (was_down) {
750 printk("Thanks, I feel much better now!\n");
751 was_down = 0;
752 }
753
754 dev->tbusy = 0;
755 dev->interrupt = 0;
756 tx_fifo_in = 0;
757 tx_fifo_out = 0;
758 free_tx_pages = TX_PAGES;
759
760
761 de600_setup_address(NODE_ADDRESS, RW_ADDR);
762 for (i = 0; i < ETH_ALEN; i++)
763 de600_put_byte(dev->dev_addr[i]);
764
765
766 rx_page = RX_BP | RX_BASE_PAGE;
767 de600_setup_address(MEM_4K, RW_ADDR);
768
769 de600_put_command(RX_ENABLE);
770 select_prn();
771 restore_flags(flags);
772
773 return 0;
774 }
775
776 #ifdef FAKE_SMALL_MAX
777
778
779
780 #define DE600_MIN_WINDOW 1024
781 #define DE600_MAX_WINDOW 2048
782 #define DE600_TCP_WINDOW_DIFF 1024
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800 #define min(a,b) ((a)<(b)?(a):(b))
801 static unsigned long
802 de600_rspace(struct sock *sk)
803 {
804 int amt;
805
806 if (sk != NULL) {
807
808
809
810
811
812
813 if (sk->rmem_alloc >= sk->rcvbuf-2*DE600_MIN_WINDOW) return(0);
814 amt = min((sk->rcvbuf-sk->rmem_alloc)/2, DE600_MAX_WINDOW);
815 if (amt < 0) return(0);
816 return(amt);
817 }
818 return(0);
819 }
820 #endif
821
822 #ifdef MODULE
823 char kernel_version[] = UTS_RELEASE;
824 static char nullname[8];
825 static struct device de600_dev = {
826 nullname, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, de600_probe };
827
828 int
829 init_module(void)
830 {
831 if (register_netdev(&de600_dev) != 0)
832 return -EIO;
833 return 0;
834 }
835
836 void
837 cleanup_module(void)
838 {
839 if (MOD_IN_USE)
840 printk("de600: device busy, remove delayed\n");
841 else
842 unregister_netdev(&de600_dev);
843 }
844 #endif