This source file includes following definitions.
- d_link_read_status
- d_link_read_byte
- d_link_open
- d_link_close
- get_stats
- trigger_interrupt
- d_link_start_xmit
- d_link_interrupt
- d_link_tx_intr
- d_link_rx_intr
- d_link_init
- adapter_init
- d_link_rspace
1 static char *version =
2 "d_link.c: $Revision: 0.32 $, 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 D_LINK_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 #ifdef D_LINK_DEBUG
59 #define PRINTK(x) if (d_link_debug >= 2) printk x
60 #else
61 #define D_LINK_DEBUG 0
62 #define PRINTK(x)
63 #endif
64 static unsigned int d_link_debug = D_LINK_DEBUG;
65
66 #include <linux/config.h>
67 #include <linux/kernel.h>
68 #include <linux/sched.h>
69 #include <linux/types.h>
70 #include <linux/fcntl.h>
71 #include <linux/string.h>
72 #include <linux/interrupt.h>
73 #include <asm/io.h>
74 #include <netinet/in.h>
75 #include <linux/ptrace.h>
76 #include <asm/system.h>
77 #include <errno.h>
78
79 #include "inet.h"
80 #include "dev.h"
81 #include "eth.h"
82 #include "ip.h"
83 #include "route.h"
84 #include "protocol.h"
85 #include "tcp.h"
86 #include "skbuff.h"
87 #include "sock.h"
88 #include "arp.h"
89
90 #define netstats enet_statistics
91
92 #ifndef HAVE_ALLOC_SKB
93 #define alloc_skb(size,pri) (struct sk_buff *)kmalloc(size,pri)
94 #endif
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 #ifndef D_LINK_IO
114 #define D_LINK_IO 0x378
115 #endif
116
117 #define DATA_PORT (D_LINK_IO)
118 #define STATUS_PORT (D_LINK_IO + 1)
119 #define COMMAND_PORT (D_LINK_IO + 2)
120
121 #ifndef D_LINK_IRQ
122 #define D_LINK_IRQ 7
123 #endif
124
125
126
127
128
129
130
131
132
133
134
135
136 #define SELECT_NIC 0x04
137 #define SELECT_PRN 0x1c
138 #define NML_PRN 0xec
139 #define IRQEN 0x10
140
141
142
143
144 #define RX_BUSY 0x80
145 #define RX_GOOD 0x40
146 #define TX_FAILED16 0x10
147 #define TX_BUSY 0x08
148
149
150
151
152
153
154
155 #define WRITE_DATA 0x00
156 #define READ_DATA 0x01
157 #define STATUS 0x02
158 #define COMMAND 0x03
159 #define NULL_COMMAND 0x04
160 #define RX_LEN 0x05
161 #define TX_ADDR 0x06
162 #define RW_ADDR 0x07
163 #define HI_NIBBLE 0x08
164
165
166
167
168
169 #define RX_ALL 0x01
170 #define RX_BP 0x02
171 #define RX_MBP 0x03
172
173 #define TX_ENABLE 0x04
174 #define RX_ENABLE 0x08
175
176 #define RESET 0x80
177 #define STOP_RESET 0x00
178
179
180
181
182
183 #define RX_PAGE2_SELECT 0x10
184 #define RX_BASE_PAGE 0x20
185 #define FLIP_IRQ 0x40
186
187
188
189
190
191
192
193
194
195
196
197
198 #define MEM_2K 0x0800
199 #define MEM_4K 0x1000
200 #define MEM_6K 0x1800
201 #define NODE_ADDRESS 0x2000
202
203 #define RUNT 60
204
205
206
207
208
209
210
211
212
213
214
215
216 static unsigned long d_link_rspace(struct sock *sk);
217
218
219 static int d_link_read_status(struct device *dev);
220 static unsigned char d_link_read_byte(unsigned char type, struct device *dev);
221
222
223 static int d_link_open(struct device *dev);
224 static int d_link_close(struct device *dev);
225 static struct netstats *get_stats(struct device *dev);
226 static int d_link_start_xmit(struct sk_buff *skb, struct device *dev);
227
228
229 static void d_link_interrupt(int reg_ptr);
230 static int d_link_tx_intr(struct device *dev, int irq_status);
231 static void d_link_rx_intr(struct device *dev);
232
233
234 static void trigger_interrupt(struct device *dev);
235 int d_link_init(struct device *dev);
236 static void adapter_init(struct device *dev);
237
238
239
240
241 extern struct device *irq2dev_map[16];
242 static volatile int rx_page = 0;
243
244 #define TX_PAGES 2
245 static volatile int tx_fifo[TX_PAGES];
246 static volatile int tx_fifo_in = 0;
247 static volatile int tx_fifo_out = 0;
248 static volatile int free_tx_pages = TX_PAGES;
249
250
251
252
253
254 #define select_prn() outb_p(SELECT_PRN, COMMAND_PORT); D_LINK_SLOW_DOWN
255 #define select_nic() outb_p(SELECT_NIC, COMMAND_PORT); D_LINK_SLOW_DOWN
256
257
258 #define d_link_put_byte(data) ( \
259 outb_p(((data) << 4) | WRITE_DATA , DATA_PORT), \
260 outb_p(((data) & 0xf0) | WRITE_DATA | HI_NIBBLE, DATA_PORT))
261
262
263
264
265
266 #define d_link_put_command(cmd) ( \
267 outb_p(( rx_page << 4) | COMMAND , DATA_PORT), \
268 outb_p(( rx_page & 0xf0) | COMMAND | HI_NIBBLE, DATA_PORT), \
269 outb_p(((rx_page | cmd) << 4) | COMMAND , DATA_PORT), \
270 outb_p(((rx_page | cmd) & 0xf0) | COMMAND | HI_NIBBLE, DATA_PORT))
271
272 #define d_link_setup_address(addr,type) ( \
273 outb_p((((addr) << 4) & 0xf0) | type , DATA_PORT), \
274 outb_p(( (addr) & 0xf0) | type | HI_NIBBLE, DATA_PORT), \
275 outb_p((((addr) >> 4) & 0xf0) | type , DATA_PORT), \
276 outb_p((((addr) >> 8) & 0xf0) | type | HI_NIBBLE, DATA_PORT))
277
278 #define rx_page_adr() ((rx_page & RX_PAGE2_SELECT)?(MEM_6K):(MEM_4K))
279
280
281 #define next_rx_page() (rx_page ^= RX_PAGE2_SELECT)
282
283 #define tx_page_adr(a) (((a) + 1) * MEM_2K)
284
285 static inline int
286 d_link_read_status(struct device *dev)
287 {
288 int status;
289
290 outb_p(STATUS, DATA_PORT);
291 status = inb(STATUS_PORT);
292 outb_p(NULL_COMMAND | HI_NIBBLE, DATA_PORT);
293
294 return status;
295 }
296
297 static inline unsigned char
298 d_link_read_byte(unsigned char type, struct device *dev) {
299 unsigned char lo;
300
301 (void)outb_p((type), DATA_PORT);
302 lo = ((unsigned char)inb(STATUS_PORT)) >> 4;
303 (void)outb_p((type) | HI_NIBBLE, DATA_PORT);
304 return ((unsigned char)inb(STATUS_PORT) & (unsigned char)0xf0) | lo;
305 }
306
307
308
309
310
311
312
313
314
315 static int
316 d_link_open(struct device *dev)
317 {
318 extern struct proto tcp_prot;
319
320 if (request_irq(D_LINK_IRQ, d_link_interrupt)) {
321 printk ("%s: unable to get IRQ %d\n", dev->name, D_LINK_IRQ);
322 return 1;
323 }
324 irq2dev_map[D_LINK_IRQ] = dev;
325
326 adapter_init(dev);
327
328
329
330
331
332
333
334
335
336
337 tcp_prot.rspace = d_link_rspace;
338
339 return 0;
340 }
341
342
343
344
345 static int
346 d_link_close(struct device *dev)
347 {
348 select_nic();
349 rx_page = 0;
350 d_link_put_command(RESET);
351 d_link_put_command(STOP_RESET);
352 d_link_put_command(0);
353 select_prn();
354
355 free_irq(D_LINK_IRQ);
356 irq2dev_map[D_LINK_IRQ] = NULL;
357 dev->start = 0;
358 tcp_prot.rspace = sock_rspace;
359
360 return 0;
361 }
362
363 static struct netstats *
364 get_stats(struct device *dev)
365 {
366 return (struct netstats *)(dev->priv);
367 }
368
369 static inline void
370 trigger_interrupt(struct device *dev)
371 {
372 d_link_put_command(FLIP_IRQ);
373 select_prn();
374 D_LINK_SLOW_DOWN;
375 select_nic();
376 d_link_put_command(0);
377 }
378
379
380
381
382
383 static int
384 d_link_start_xmit(struct sk_buff *skb, struct device *dev)
385 {
386 int transmit_from;
387 int len;
388 int tickssofar;
389 unsigned char *buffer = (unsigned char *)(skb + 1);
390
391
392
393
394
395
396
397 if (skb == NULL) {
398 dev_tint(dev);
399 return 0;
400 }
401
402
403 if (!skb->arp)
404 if(dev->rebuild_header(skb + 1, dev)) {
405 skb->dev = dev;
406 arp_queue (skb);
407 return 0;
408 }
409 skb->arp = 1;
410
411 if (free_tx_pages <= 0) {
412 tickssofar = jiffies - dev->trans_start;
413
414 if (tickssofar < 5)
415 return 1;
416
417
418 printk("%s: transmit timed out (%d), %s?\n",
419 dev->name,
420 tickssofar,
421 "network cable problem"
422 );
423
424 adapter_init(dev);
425 }
426
427
428 PRINTK(("d_link_start_xmit:len=%d, page %d/%d\n", skb->len, tx_fifo_in, free_tx_pages));
429
430 if ((len = skb->len) < RUNT)
431 len = RUNT;
432
433 cli();
434 select_nic();
435
436 tx_fifo[tx_fifo_in] = transmit_from = tx_page_adr(tx_fifo_in) - len;
437 tx_fifo_in = (tx_fifo_in + 1) % TX_PAGES;
438
439 d_link_setup_address(transmit_from, RW_ADDR);
440 for ( ; len > 0; --len, ++buffer)
441 d_link_put_byte(*buffer);
442
443 if (free_tx_pages-- == TX_PAGES) {
444 dev->trans_start = jiffies;
445 dev->tbusy = 0;
446
447 d_link_setup_address(transmit_from, TX_ADDR);
448 d_link_put_command(TX_ENABLE);
449 }
450 else {
451 dev->tbusy = !free_tx_pages;
452 select_prn();
453 }
454
455 sti();
456
457 if (skb->free)
458 kfree_skb (skb, FREE_WRITE);
459
460 return 0;
461 }
462
463
464
465
466
467 static void
468 d_link_interrupt(int reg_ptr)
469 {
470 int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
471 struct device *dev = irq2dev_map[irq];
472 unsigned char irq_status;
473 int retrig = 0;
474 int boguscount = 0;
475
476
477 if ((dev == NULL) || (dev->start == 0) || (D_LINK_IRQ != irq)) {
478 printk("%s: bogus interrupt %d\n", dev?dev->name:"DE-600", irq);
479 return;
480 }
481
482 dev->interrupt = 1;
483 select_nic();
484 irq_status = d_link_read_status(dev);
485
486 do {
487 PRINTK(("d_link_interrupt (%2.2X)\n", irq_status));
488
489 if (irq_status & RX_GOOD)
490 d_link_rx_intr(dev);
491 else if (!(irq_status & RX_BUSY))
492 d_link_put_command(RX_ENABLE);
493
494
495 if (free_tx_pages < TX_PAGES)
496 retrig = d_link_tx_intr(dev, irq_status);
497 else
498 retrig = 0;
499
500 irq_status = d_link_read_status(dev);
501 } while ( (irq_status & RX_GOOD) || ((++boguscount < 10) && retrig) );
502
503
504
505
506
507
508
509 dev->interrupt = 0;
510 select_prn();
511
512 if (retrig)
513 trigger_interrupt(dev);
514
515 sti();
516 return;
517 }
518
519 static int
520 d_link_tx_intr(struct device *dev, int irq_status)
521 {
522
523
524
525
526 mark_bh(INET_BH);
527
528 if (irq_status & TX_BUSY)
529 return 1;
530
531
532
533 if (!(irq_status & TX_FAILED16)) {
534 tx_fifo_out = (tx_fifo_out + 1) % TX_PAGES;
535 ++free_tx_pages;
536 ((struct netstats *)(dev->priv))->tx_packets++;
537 dev->tbusy = 0;
538 }
539
540
541 if ((free_tx_pages < TX_PAGES) || (irq_status & TX_FAILED16)) {
542 dev->trans_start = jiffies;
543 d_link_setup_address(tx_fifo[tx_fifo_out], TX_ADDR);
544 d_link_put_command(TX_ENABLE);
545 return 1;
546 }
547
548
549 return 0;
550 }
551
552
553
554
555 static void
556 d_link_rx_intr(struct device *dev)
557 {
558 struct sk_buff *skb;
559 int i;
560 int read_from;
561 int size;
562 int sksize;
563 register unsigned char *buffer;
564
565 cli();
566
567 size = d_link_read_byte(RX_LEN, dev);
568 size += (d_link_read_byte(RX_LEN, dev) << 8);
569 size -= 4;
570
571
572 read_from = rx_page_adr();
573 next_rx_page();
574 d_link_put_command(RX_ENABLE);
575 sti();
576
577 if ((size < 32) || (size > 1535))
578 printk("%s: Bogus packet size %d.\n", dev->name, size);
579
580 sksize = sizeof(struct sk_buff) + size;
581 skb = alloc_skb(sksize, GFP_ATOMIC);
582 sti();
583 if (skb == NULL) {
584 printk("%s: Couldn't allocate a sk_buff of size %d.\n",
585 dev->name, sksize);
586 return;
587 }
588
589
590 skb->lock = 0;
591 skb->mem_len = sksize;
592 skb->mem_addr = skb;
593
594 buffer = (unsigned char *)(skb + 1);
595
596
597 d_link_setup_address(read_from, RW_ADDR);
598 for (i = size; i > 0; --i, ++buffer)
599 *buffer = d_link_read_byte(READ_DATA, dev);
600
601 ((struct netstats *)(dev->priv))->rx_packets++;
602
603 if (dev_rint((unsigned char *)skb, size, IN_SKBUFF, dev))
604 printk("%s: receive buffers full.\n", dev->name);
605
606
607
608
609
610 }
611
612 int
613 d_link_init(struct device *dev)
614 {
615 int i;
616
617 printk("%s: D-Link DE-600 pocket adapter", dev->name);
618
619 if (d_link_debug > 1)
620 printk(version);
621
622
623 rx_page = 0;
624 select_nic();
625 (void)d_link_read_status(dev);
626 d_link_put_command(RESET);
627 d_link_put_command(STOP_RESET);
628 if (d_link_read_status(dev) & 0xf0) {
629 printk(": not at I/O %#3x.\n", DATA_PORT);
630 return ENODEV;
631 }
632
633
634
635
636
637
638
639 d_link_setup_address(NODE_ADDRESS, RW_ADDR);
640 for (i = 0; i < ETH_ALEN; i++) {
641 dev->dev_addr[i] = d_link_read_byte(READ_DATA, dev);
642 dev->broadcast[i] = 0xff;
643 }
644
645
646 if ((dev->dev_addr[1] == 0xde) && (dev->dev_addr[2] == 0x15)) {
647
648 dev->dev_addr[0] = 0x00;
649 dev->dev_addr[1] = 0x80;
650 dev->dev_addr[2] = 0xc8;
651 dev->dev_addr[3] &= 0x0f;
652 dev->dev_addr[3] |= 0x70;
653 } else {
654 printk(" not identified in the printer port\n");
655 return ENODEV;
656 }
657
658 printk(", Ethernet Address: %2.2X", dev->dev_addr[0]);
659 for (i = 1; i < ETH_ALEN; i++)
660 printk(":%2.2X",dev->dev_addr[i]);
661 printk("\n");
662
663
664 dev->priv = kmalloc(sizeof(struct netstats), GFP_KERNEL);
665 memset(dev->priv, 0, sizeof(struct netstats));
666
667 for (i = 0; i < DEV_NUMBUFFS; i++)
668 dev->buffs[i] = NULL;
669
670 dev->get_stats = get_stats;
671 dev->hard_header = eth_header;
672 dev->add_arp = eth_add_arp;
673 dev->queue_xmit = dev_queue_xmit;
674 dev->rebuild_header = eth_rebuild_header;
675 dev->type_trans = eth_type_trans;
676
677 dev->open = d_link_open;
678 dev->stop = d_link_close;
679 dev->hard_start_xmit = &d_link_start_xmit;
680
681
682 dev->type = ARPHRD_ETHER;
683 dev->hard_header_len = ETH_HLEN;
684 dev->mtu = 1500;
685 dev->addr_len = ETH_ALEN;
686
687
688 dev->flags = IFF_BROADCAST;
689 dev->family = AF_INET;
690 dev->pa_addr = 0;
691 dev->pa_brdaddr = 0;
692 dev->pa_mask = 0;
693 dev->pa_alen = sizeof(unsigned long);
694
695 select_prn();
696 return 0;
697 }
698
699 static void
700 adapter_init(struct device *dev)
701 {
702 int i;
703
704 cli();
705 dev->tbusy = 0;
706 dev->interrupt = 0;
707 dev->start = 1;
708
709 select_nic();
710 rx_page = 0;
711 d_link_put_command(RESET);
712 d_link_put_command(STOP_RESET);
713
714 tx_fifo_in = 0;
715 tx_fifo_out = 0;
716 free_tx_pages = TX_PAGES;
717
718
719 d_link_setup_address(NODE_ADDRESS, RW_ADDR);
720 for (i = 0; i < ETH_ALEN; i++)
721 d_link_put_byte(dev->dev_addr[i]);
722
723
724 rx_page = RX_BP | RX_BASE_PAGE;
725 d_link_setup_address(MEM_4K, RW_ADDR);
726
727 d_link_put_command(RX_ENABLE);
728 select_prn();
729 sti();
730 }
731
732 #define D_LINK_MIN_WINDOW 1024
733 #define D_LINK_MAX_WINDOW 2048
734 #define D_LINK_TCP_WINDOW_DIFF 1024
735
736
737
738
739
740
741
742
743
744
745
746
747 #define min(a,b) ((a)<(b)?(a):(b))
748 static unsigned long
749 d_link_rspace(struct sock *sk)
750 {
751 int amt;
752
753 if (sk != NULL) {
754
755
756
757
758 sk->max_unacked = D_LINK_MAX_WINDOW - D_LINK_TCP_WINDOW_DIFF;
759
760 if (sk->rmem_alloc >= SK_RMEM_MAX-2*D_LINK_MIN_WINDOW) return(0);
761 amt = min((SK_RMEM_MAX-sk->rmem_alloc)/2-D_LINK_MIN_WINDOW, D_LINK_MAX_WINDOW);
762 if (amt < 0) return(0);
763 return(amt);
764 }
765 return(0);
766 }