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