This source file includes following definitions.
- plip_init
- plip_open
- plip_close
- plip_tx_packet
- plip_header
- plip_device_clear
- plip_receiver_error
- get_byte
- plip_interrupt
- plip_receive_packet
- send_byte
- plip_send_start
- plip_send_packet
- plip_set_physicaladdr
- plip_addrcmp
- plip_send_enethdr
- plip_rebuild_enethdr
- cold_sleep
- double_timeoutfactor
- plip_get_stats
1
2
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 static char *version =
42 "Net2Debugged PLIP 1.01 (from plip.c:v0.15 for 0.99pl12+, 8/11/93)\n";
43
44 #include <linux/config.h>
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 #include <linux/kernel.h>
78 #include <linux/sched.h>
79 #include <linux/types.h>
80 #include <linux/fcntl.h>
81 #include <linux/interrupt.h>
82 #include <linux/string.h>
83 #include <linux/ptrace.h>
84 #include <linux/if_ether.h>
85 #include <asm/system.h>
86 #include <asm/io.h>
87 #include <netinet/in.h>
88 #include <errno.h>
89
90 #include "dev.h"
91 #include "eth.h"
92 #include "ip.h"
93 #include "protocol.h"
94 #include "tcp.h"
95 #include "skbuff.h"
96 #include "sock.h"
97 #include "arp.h"
98
99 #ifdef PRINTK
100 #undef PRINTK
101 #endif
102 #ifdef PRINTK2
103 #undef PRINTK2
104 #endif
105
106 #define PLIP_DEBUG
107 #undef PLIP_DEBUG2
108
109 #ifdef PLIP_DEBUG
110 #define PRINTK(x) printk x
111 #else
112 #define PRINTK(x)
113 #endif
114 #ifdef PLIP_DEBUG2
115 #define PRINTK2(x) printk x
116 #else
117 #define PRINTK2(x)
118 #endif
119
120
121
122 extern struct device *irq2dev_map[16];
123
124
125 #define netstats enet_statistics
126
127
128 #define PAR_DATA 0
129 #define PAR_STATUS 1
130 #define PAR_CONTROL 2
131 #define PLIP_MTU 1600
132 #define PLIP_HEADER_TYPE1 0xfd
133 #define PLIP_HEADER_TYPE2 0xfc
134
135
136 extern int plip_probe(int ioaddr, struct device *dev);
137 static int plip_open(struct device *dev);
138 static int plip_close(struct device *dev);
139 static int plip_tx_packet(struct sk_buff *skb, struct device *dev);
140 static int plip_header (unsigned char *buff, struct device *dev,
141 unsigned short type, unsigned long h_dest,
142 unsigned long h_source, unsigned len);
143
144
145 #define INITIALTIMEOUTFACTOR 4
146 #define MAXTIMEOUTFACTOR 20
147 static int timeoutfactor = INITIALTIMEOUTFACTOR;
148
149
150 static void plip_device_clear(struct device *dev);
151 static void plip_receiver_error(struct device *dev);
152 static void plip_set_physicaladdr(struct device *dev, unsigned long ipaddr);
153 static int plip_addrcmp(struct ethhdr *eth);
154 static int plip_send_enethdr(struct device *dev, struct ethhdr *eth);
155 static int plip_rebuild_enethdr(struct device *dev, struct ethhdr *eth,
156 unsigned char h_dest, unsigned char h_source,
157 unsigned short type);
158 static void cold_sleep(int tics);
159 static void plip_interrupt(int reg_ptr);
160 static int plip_receive_packet(struct device *dev);
161 static int plip_send_packet(struct device *dev, unsigned char *buf, int length);
162 static int plip_send_start(struct device *dev, struct ethhdr *eth);
163 static void double_timeoutfactor(void);
164 static struct enet_statistics *plip_get_stats(struct device *dev);
165
166 int
167 plip_init(struct device *dev)
168 {
169 int port_base = dev->base_addr;
170 int i;
171
172
173 outb(0x00, port_base + PAR_CONTROL);
174 outb(0x55, port_base + PAR_DATA);
175 if (inb(port_base + PAR_DATA) != 0x55)
176 return -ENODEV;
177
178
179 #ifdef PLIP_DEBUG
180 {
181 static int version_shown = 0;
182 if (! version_shown)
183 printk(version), version_shown++;
184 }
185 #endif
186
187
188 dev->priv = kmalloc(sizeof(struct netstats), GFP_KERNEL);
189 memset(dev->priv, 0, sizeof(struct netstats));
190
191 for (i = 0; i < DEV_NUMBUFFS; i++)
192 dev->buffs[i] = NULL;
193 dev->hard_header = &plip_header;
194 dev->add_arp = eth_add_arp;
195 dev->queue_xmit = dev_queue_xmit;
196 dev->rebuild_header = eth_rebuild_header;
197 dev->type_trans = eth_type_trans;
198
199 dev->open = &plip_open;
200 dev->stop = &plip_close;
201 dev->hard_start_xmit = &plip_tx_packet;
202 dev->get_stats = &plip_get_stats;
203
204
205 dev->type = ARPHRD_ETHER;
206 dev->hard_header_len = ETH_HLEN;
207 dev->mtu = PLIP_MTU;
208 dev->addr_len = ETH_ALEN;
209 for (i = 0; i < dev->addr_len; i++) {
210 dev->broadcast[i]=0xff;
211 dev->dev_addr[i] = 0;
212 }
213 printk("%s: configured for parallel port at %#3x, IRQ %d.\n",
214 dev->name, dev->base_addr, dev->irq);
215
216
217 timeoutfactor = INITIALTIMEOUTFACTOR;
218 return 0;
219 }
220
221
222
223
224
225
226
227
228 static int
229 plip_open(struct device *dev)
230 {
231 if (dev->irq == 0)
232 dev->irq = 7;
233 cli();
234 if (request_irq(dev->irq , &plip_interrupt) != 0) {
235 sti();
236 PRINTK(("%s: couldn't get IRQ %d.\n", dev->name, dev->irq));
237 return -EAGAIN;
238 }
239
240 irq2dev_map[dev->irq] = dev;
241 sti();
242 plip_device_clear(dev);
243 dev->tbusy = 0;
244 dev->interrupt = 0;
245 dev->start = 1;
246 return 0;
247 }
248
249
250 static int
251 plip_close(struct device *dev)
252 {
253 dev->tbusy = 1;
254 dev->start = 0;
255 cli();
256 free_irq(dev->irq);
257 irq2dev_map[dev->irq] = NULL;
258 sti();
259 outb(0x00, dev->base_addr);
260 return 0;
261 }
262
263 static int
264 plip_tx_packet(struct sk_buff *skb, struct device *dev)
265 {
266 int ret_val;
267
268 if (dev->tbusy || dev->interrupt) {
269 int tickssofar = jiffies - dev->trans_start;
270 if (tickssofar < 50)
271 return 1;
272 printk("%s: transmit timed out\n", dev->name);
273
274 plip_device_clear(dev);
275 return 0;
276 }
277
278
279
280
281 if (skb == NULL) {
282 dev_tint(dev);
283 return 0;
284 }
285
286
287
288 if (!skb->arp && dev->rebuild_header(skb+1, dev)) {
289 skb->dev = dev;
290 arp_queue (skb);
291 return 0;
292 }
293 skb->arp=1;
294
295 dev->trans_start = jiffies;
296 ret_val = plip_send_packet(dev, (unsigned char *)(skb+1), skb->len);
297 if (skb->free)
298 kfree_skb (skb, FREE_WRITE);
299 dev->tbusy = 0;
300 mark_bh (INET_BH);
301 return 0;
302 }
303
304 static int
305 plip_header (unsigned char *buff, struct device *dev,
306 unsigned short type, unsigned long h_dest,
307 unsigned long h_source, unsigned len)
308 {
309 if (dev->dev_addr[0] == 0) {
310
311 plip_set_physicaladdr(dev, h_source);
312 }
313 return eth_header(buff, dev, type, h_dest, h_source, len);
314 }
315
316 static void
317 plip_device_clear(struct device *dev)
318 {
319 dev->interrupt = 0;
320 dev->tbusy = 0;
321 outb(0x00, dev->base_addr + PAR_DATA);
322 outb(0x10, dev->base_addr + PAR_CONTROL);
323 }
324
325 static void
326 plip_receiver_error(struct device *dev)
327 {
328 dev->interrupt = 0;
329 dev->tbusy = 0;
330 outb(0x02, dev->base_addr + PAR_DATA);
331 outb(0x10, dev->base_addr + PAR_CONTROL);
332 }
333
334 static int
335 get_byte(struct device *dev)
336 {
337 unsigned char val, oldval;
338 unsigned char low_nibble;
339 int timeout;
340 int error = 0;
341 val = inb(dev->base_addr + PAR_STATUS);
342 timeout = jiffies + timeoutfactor * 2;
343 do {
344 oldval = val;
345 val = inb(dev->base_addr + PAR_STATUS);
346 if ( oldval != val ) continue;
347 if ( timeout < jiffies ) {
348 error++;
349 break;
350 }
351 } while ( (val & 0x80) );
352 val = inb(dev->base_addr + PAR_STATUS);
353 low_nibble = (val >> 3) & 0x0f;
354 outb(0x11, dev->base_addr + PAR_DATA);
355 timeout = jiffies + timeoutfactor * 2;
356 do {
357 oldval = val;
358 val = inb(dev->base_addr + PAR_STATUS);
359 if (oldval != val) continue;
360 if ( timeout < jiffies ) {
361 error++;
362 break;
363 }
364 } while ( !(val & 0x80) );
365 val = inb(dev->base_addr + PAR_STATUS);
366 PRINTK2(("%02x %s ", low_nibble | ((val << 1) & 0xf0),
367 error ? "t":""));
368 outb(0x01, dev->base_addr + PAR_DATA);
369 if (error) {
370
371 double_timeoutfactor();
372 return -1;
373 }
374 return low_nibble | ((val << 1) & 0xf0);
375 }
376
377
378
379 static void
380 plip_interrupt(int reg_ptr)
381 {
382 int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
383 struct device *dev = irq2dev_map[irq];
384 struct netstats *localstats;
385
386 if (dev == NULL) {
387 PRINTK(("plip_interrupt(): irq %d for unknown device.\n", irq));
388 return;
389 }
390 localstats = (struct netstats*) dev->priv;
391 if (dev->tbusy || dev->interrupt) return;
392 dev->interrupt = 1;
393 outb(0x00, dev->base_addr + PAR_CONTROL);
394 sti();
395 PRINTK2(("%s: interrupt. ", dev->name));
396
397 {
398
399 int timeout = jiffies + timeoutfactor;
400 while ((inb(dev->base_addr + PAR_STATUS) & 0xf8) != 0xc0) {
401 if ( timeout < jiffies ) {
402 PRINTK2(("%s: No interrupt (status=%#02x)!\n",
403 dev->name, inb(dev->base_addr + PAR_STATUS)));
404 plip_device_clear(dev);
405 return;
406 }
407 }
408 }
409 if (plip_receive_packet(dev)) {
410
411 localstats->rx_errors++;
412 plip_receiver_error(dev);
413 } else {
414 plip_device_clear(dev);
415 }
416 }
417
418 static int
419 plip_receive_packet(struct device *dev)
420 {
421 int plip_type;
422 unsigned length;
423 int checksum = 0;
424 struct sk_buff *skb;
425 struct netstats *localstats;
426 struct ethhdr eth;
427
428 localstats = (struct netstats*) dev->priv;
429
430 outb(1, dev->base_addr + PAR_DATA);
431
432 {
433
434 plip_type = get_byte(dev);
435 if (plip_type < 0) return 1;
436 length = get_byte(dev) << 8;
437 length |= get_byte(dev);
438 switch ( plip_type ) {
439 case PLIP_HEADER_TYPE1:
440 {
441 int i;
442 unsigned char *eth_p = (unsigned char*)ð
443 for ( i = 0; i < sizeof(eth); i++, eth_p++) {
444 *eth_p = get_byte(dev);
445 }
446 }
447 break;
448 case PLIP_HEADER_TYPE2:
449 {
450 unsigned char h_dest, h_source;
451 unsigned short type;
452 h_dest = get_byte(dev);
453 h_source = get_byte(dev);
454 type = get_byte(dev) << 8;
455 type |= get_byte(dev);
456 plip_rebuild_enethdr(dev, ð, h_dest, h_source, type);
457 }
458 break;
459 default:
460 PRINTK(("%s: wrong header octet\n", dev->name));
461 }
462 PRINTK2(("length = %d\n", length));
463 if (length > dev->mtu || length < 8) {
464 PRINTK2(("%s: bogus packet size %d.\n", dev->name, length));
465 return 1;
466 }
467 }
468 {
469
470
471
472 int sksize;
473 sksize = sizeof(struct sk_buff) + length;
474 skb = alloc_skb(sksize, GFP_ATOMIC);
475 if (skb == NULL) {
476 PRINTK(("%s: Couldn't allocate a sk_buff of size %d.\n",
477 dev->name, sksize));
478 return 1;
479 }
480 skb->lock = 0;
481 skb->mem_len = sksize;
482 skb->mem_addr = skb;
483 }
484 {
485
486
487 unsigned char *buf = (unsigned char *) (skb+1);
488 unsigned char *eth_p = (unsigned char *)ð
489 int i;
490 for ( i = 0; i < sizeof(eth); i++) {
491 checksum += *eth_p;
492 *buf++ = *eth_p++;
493 }
494 for ( i = 0; i < length - sizeof(eth); i++) {
495 unsigned char new_byte = get_byte(dev);
496 checksum += new_byte;
497 *buf++ = new_byte;
498 }
499 checksum &= 0xff;
500 if (checksum != get_byte(dev)) {
501 localstats->rx_crc_errors++;
502 PRINTK(("checksum error\n"));
503 return 1;
504 } else if(dev_rint((unsigned char *)skb, length, IN_SKBUFF, dev)) {
505 printk("%s: rcv buff full.\n", dev->name);
506 localstats->rx_dropped++;
507 return 1;
508 }
509 }
510 {
511
512 int timeout;
513
514 timeout = jiffies + length * timeoutfactor / 16;
515 outb(0x00, dev->base_addr + PAR_DATA);
516
517 while ( (inb(dev->base_addr + PAR_STATUS) & 0xf8) != 0x80 ) {
518 if (timeout < jiffies ) {
519 double_timeoutfactor();
520 PRINTK(("Remote has not reset.\n"));
521 break;
522 }
523 }
524 }
525 localstats->rx_packets++;
526 return 0;
527 }
528
529
530 static int send_byte(struct device *dev, unsigned char val)
531 {
532 int timeout;
533 int error = 0;
534 if (!(inb(dev->base_addr+PAR_STATUS) & 0x08)) {
535 PRINTK(("remote end become unready while sending\n"));
536 return -1;
537 }
538 PRINTK2((" S%02x", val));
539 outb(val, dev->base_addr);
540 outb(0x10 | val, dev->base_addr);
541 timeout = jiffies + timeoutfactor;
542 while( inb(dev->base_addr+PAR_STATUS) & 0x80 )
543 if ( timeout < jiffies ) {
544 error++;
545 break;
546 }
547 outb(0x10 | (val >> 4), dev->base_addr);
548 outb(val >> 4, dev->base_addr);
549 timeout = jiffies + timeoutfactor;
550 while( (inb(dev->base_addr+PAR_STATUS) & 0x80) == 0 )
551 if ( timeout < jiffies ) {
552 error++;
553 break;
554 }
555 if (error) {
556
557 double_timeoutfactor();
558 PRINTK2(("t"));
559 return -1;
560 }
561 return 0;
562 }
563
564
565
566
567
568
569
570
571 static int
572 plip_send_start(struct device *dev, struct ethhdr *eth)
573 {
574 int timeout;
575 int status;
576 int lasttrigger;
577 struct netstats *localstats = (struct netstats*) dev->priv;
578
579
580 timeout = jiffies + timeoutfactor * 16;
581 lasttrigger = jiffies;
582 while ( ((status = inb(dev->base_addr+PAR_STATUS)) & 0x08) == 0 ) {
583 dev->tbusy = 1;
584 outb(0x00, dev->base_addr + PAR_CONTROL);
585 outb(0x08, dev->base_addr + PAR_DATA);
586 if (status & 0x40) {
587
588
589
590
591
592 if ( plip_addrcmp(eth) > 0 ) {
593 localstats->collisions++;
594 PRINTK2(("both ends are trying to send a packet.\n"));
595 if (plip_receive_packet(dev)) {
596
597 localstats->rx_errors++;
598 outb(0x02, dev->base_addr + PAR_DATA);
599 } else {
600 outb(0x00, dev->base_addr + PAR_DATA);
601 }
602 cold_sleep(2);
603 }
604 continue;
605 }
606 if (lasttrigger != jiffies) {
607
608 outb(0x00, dev->base_addr + PAR_DATA);
609 cold_sleep(1);
610 lasttrigger = jiffies;
611 }
612 if (timeout < jiffies) {
613 double_timeoutfactor();
614 plip_device_clear(dev);
615 localstats->tx_errors++;
616 PRINTK(("%s: Connect failed in send_packet().\n",
617 dev->name));
618
619
620 return -1;
621 }
622 }
623 return 0;
624 }
625 static int
626 plip_send_packet(struct device *dev, unsigned char *buf, int length)
627 {
628 int error = 0;
629 int plip_type;
630 struct netstats *localstats;
631
632 PRINTK2(("%s: plip_send_packet(%d) %02x %02x %02x %02x %02x...",
633 dev->name, length, buf[0], buf[1], buf[2], buf[3], buf[4]));
634 if (length > dev->mtu) {
635 printk("%s: packet too big, %d.\n", dev->name, length);
636 return 0;
637 }
638 localstats = (struct netstats*) dev->priv;
639
640 {
641
642 int i;
643 int timeout = jiffies + timeoutfactor * 8;
644 while ( (i = (inb(dev->base_addr+PAR_STATUS) & 0xe8)) != 0x80 ) {
645 if (i == 0x78) {
646
647
648
649
650
651 return 0;
652 }
653 if (timeout < jiffies) {
654
655 double_timeoutfactor();
656 localstats->tx_errors++;
657 PRINTK(("remote end is not ready.\n"));
658 return 1;
659 }
660 }
661 }
662
663 if (plip_send_start(dev, (struct ethhdr *)buf) < 0)
664 return 1;
665
666
667 {
668
669
670
671 int i;
672 struct ethhdr *eth = (struct ethhdr *)buf;
673
674 plip_type = PLIP_HEADER_TYPE2;
675 for ( i = 0; i < ETH_ALEN - 1; i++)
676 if (eth->h_dest[i] != eth->h_source[i])
677 plip_type = PLIP_HEADER_TYPE1;
678 }
679
680 send_byte(dev, plip_type);
681
682 {
683
684
685
686
687
688
689
690 send_byte(dev, length >> 8); send_byte(dev, length);
691 }
692 {
693
694 int i;
695 int checksum = 0;
696
697 if (plip_type == PLIP_HEADER_TYPE2) {
698 plip_send_enethdr(dev, (struct ethhdr*)buf);
699 }
700 for ( i = 0; i < sizeof(struct ethhdr); i++ ) {
701 if (plip_type == PLIP_HEADER_TYPE1) {
702 send_byte(dev, *buf);
703 }
704 checksum += *buf++;
705 }
706
707 for (i = 0; i < length - sizeof(struct ethhdr); i++) {
708 checksum += buf[i];
709 if (send_byte(dev, buf[i]) < 0) {
710 error++;
711 break;
712 }
713 }
714 send_byte(dev, checksum & 0xff);
715 }
716 {
717
718 int timeout;
719
720 outb(0x00, dev->base_addr + PAR_DATA);
721
722 timeout = jiffies + ((length * timeoutfactor) >> 4);
723 while ((inb(dev->base_addr + PAR_STATUS) & 0xe8) != 0x80) {
724 if (timeout < jiffies ) {
725 double_timeoutfactor();
726 PRINTK(("Remote end has not reset.\n"));
727 error++;
728 break;
729 }
730 }
731 if (inb(dev->base_addr + PAR_STATUS) & 0x10) {
732
733 error++;
734 }
735 }
736 plip_device_clear(dev);
737 localstats->tx_packets++;
738 PRINTK2(("plip_send_packet(%d) done.\n", length));
739 return error?1:0;
740 }
741
742
743
744
745 static void
746 plip_set_physicaladdr(struct device *dev, unsigned long ipaddr)
747 {
748
749
750
751
752
753 unsigned char *addr = dev->dev_addr;
754 int i;
755
756 if ((ipaddr >> 24) == 0 || (ipaddr >> 24) == 0xff) return;
757 PRINTK2(("%s: set physical address to %08x\n", dev->name, ipaddr));
758 for (i=0; i < ETH_ALEN - sizeof(unsigned long); i++) {
759 addr[i] = 0xfd;
760 }
761 memcpy(&(addr[i]), &ipaddr, sizeof(unsigned long));
762 }
763
764 static int
765 plip_addrcmp(struct ethhdr *eth)
766 {
767 int i;
768 for ( i = ETH_ALEN - 1; i >= 0; i-- ) {
769 if (eth->h_dest[i] > eth->h_source[i]) return -1;
770 if (eth->h_dest[i] < eth->h_source[i]) return 1;
771 }
772 PRINTK2(("h_dest = %08x%04x h_source = %08x%04x\n",
773 *(long*)ð->h_dest[2],*(short*)ð->h_dest[0],
774 *(long*)ð->h_source[2],*(short*)ð->h_source[0]));
775 return 0;
776 }
777
778 static int
779 plip_send_enethdr(struct device *dev, struct ethhdr *eth)
780 {
781 send_byte(dev, eth->h_dest[ETH_ALEN-1]);
782 send_byte(dev, eth->h_source[ETH_ALEN-1]);
783 send_byte(dev, eth->h_proto >> 8);
784 send_byte(dev, eth->h_proto);
785 return 0;
786 }
787
788 static int
789 plip_rebuild_enethdr(struct device *dev, struct ethhdr *eth,
790 unsigned char dest, unsigned char source,
791 unsigned short type)
792 {
793 eth->h_proto = type;
794 memcpy(eth->h_dest, dev->dev_addr, ETH_ALEN-1);
795 eth->h_dest[ETH_ALEN-1] = dest;
796 memcpy(eth->h_source, dev->dev_addr, ETH_ALEN-1);
797 eth->h_source[ETH_ALEN-1] = source;
798 return 0;
799 }
800
801
802
803 static void
804 cold_sleep(int tics)
805 {
806 int start = jiffies;
807 while(jiffies < start + tics)
808 ;
809 return;
810 }
811
812 static void
813 double_timeoutfactor()
814 {
815 timeoutfactor *= 2;
816 if (timeoutfactor >= MAXTIMEOUTFACTOR) {
817 timeoutfactor = MAXTIMEOUTFACTOR;
818 }
819 return;
820 }
821
822 static struct enet_statistics *
823 plip_get_stats(struct device *dev)
824 {
825 struct netstats *localstats = (struct netstats*) dev->priv;
826 return localstats;
827 }
828
829
830
831
832
833
834
835