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