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