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