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