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