This source file includes following definitions.
- plip_init
- plip_kick_bh
- plip_bh
- plip_bh_timeout_error
- plip_none
- plip_receive
- plip_receive_packet
- plip_send
- plip_send_packet
- plip_connection_close
- plip_error
- plip_interrupt
- plip_rebuild_header
- plip_tx_packet
- plip_open
- plip_close
- plip_get_stats
- plip_config
- plip_ioctl
- 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 static const char *version = "NET3 PLIP version 2.1 gniibe@mri.co.jp\n";
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
82
83
84
85
86
87
88 #ifdef MODULE
89 #include <linux/module.h>
90 #include <linux/version.h>
91 #else
92 #define MOD_INC_USE_COUNT
93 #define MOD_DEC_USE_COUNT
94 #endif
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 <linux/in.h>
107 #include <linux/errno.h>
108 #include <linux/delay.h>
109 #include <linux/lp.h>
110
111 #include <linux/netdevice.h>
112 #include <linux/etherdevice.h>
113 #include <linux/skbuff.h>
114 #include <linux/if_plip.h>
115
116 #include <linux/tqueue.h>
117 #include <linux/ioport.h>
118 #include <asm/bitops.h>
119 #include <asm/irq.h>
120 #include <asm/byteorder.h>
121
122
123 #ifndef NET_DEBUG
124 #define NET_DEBUG 1
125 #endif
126 static unsigned int net_debug = NET_DEBUG;
127
128
129 #define PLIP_DELAY_UNIT 1
130
131
132 #define PLIP_TRIGGER_WAIT 500
133
134
135 #define PLIP_NIBBLE_WAIT 3000
136
137 #define PAR_INTR_ON (LP_PINITP|LP_PSELECP|LP_PINTEN)
138 #define PAR_INTR_OFF (LP_PINITP|LP_PSELECP)
139 #define PAR_DATA(dev) ((dev)->base_addr+0)
140 #define PAR_STATUS(dev) ((dev)->base_addr+1)
141 #define PAR_CONTROL(dev) ((dev)->base_addr+2)
142
143
144 static void plip_kick_bh(struct device *dev);
145 static void plip_bh(struct device *dev);
146
147
148 static void plip_interrupt(int irq, struct pt_regs *regs);
149
150
151 static int plip_rebuild_header(void *buff, struct device *dev,
152 unsigned long raddr, struct sk_buff *skb);
153 static int plip_tx_packet(struct sk_buff *skb, struct device *dev);
154 static int plip_open(struct device *dev);
155 static int plip_close(struct device *dev);
156 static struct enet_statistics *plip_get_stats(struct device *dev);
157 static int plip_config(struct device *dev, struct ifmap *map);
158 static int plip_ioctl(struct device *dev, struct ifreq *ifr, int cmd);
159
160 enum plip_connection_state {
161 PLIP_CN_NONE=0,
162 PLIP_CN_RECEIVE,
163 PLIP_CN_SEND,
164 PLIP_CN_CLOSING,
165 PLIP_CN_ERROR
166 };
167
168 enum plip_packet_state {
169 PLIP_PK_DONE=0,
170 PLIP_PK_TRIGGER,
171 PLIP_PK_LENGTH_LSB,
172 PLIP_PK_LENGTH_MSB,
173 PLIP_PK_DATA,
174 PLIP_PK_CHECKSUM
175 };
176
177 enum plip_nibble_state {
178 PLIP_NB_BEGIN,
179 PLIP_NB_1,
180 PLIP_NB_2,
181 };
182
183 struct plip_local {
184 enum plip_packet_state state;
185 enum plip_nibble_state nibble;
186 union {
187 struct {
188 #if defined(__LITTLE_ENDIAN)
189 unsigned char lsb;
190 unsigned char msb;
191 #elif defined(__BIG_ENDIAN)
192 unsigned char msb;
193 unsigned char lsb;
194 #else
195 #error "Please fix the endianness defines in <asm/byteorder.h>"
196 #endif
197 } b;
198 unsigned short h;
199 } length;
200 unsigned short byte;
201 unsigned char checksum;
202 unsigned char data;
203 struct sk_buff *skb;
204 };
205
206 struct net_local {
207 struct enet_statistics enet_stats;
208 struct tq_struct immediate;
209 struct tq_struct deferred;
210 struct plip_local snd_data;
211 struct plip_local rcv_data;
212 unsigned long trigger;
213 unsigned long nibble;
214 enum plip_connection_state connection;
215 unsigned short timeout_count;
216 char is_deferred;
217 int (*orig_rebuild_header)(void *eth, struct device *dev,
218 unsigned long raddr, struct sk_buff *skb);
219 };
220
221
222
223 int
224 plip_init(struct device *dev)
225 {
226 struct net_local *nl;
227 int iosize = (PAR_DATA(dev) == 0x3bc) ? 3 : 8;
228
229
230 if (check_region(PAR_DATA(dev), iosize) < 0)
231 return -ENODEV;
232
233
234 outb(0, PAR_DATA(dev));
235 udelay(1000);
236 if (inb(PAR_DATA(dev)) != 0)
237 return -ENODEV;
238
239 printk(version);
240 printk("%s: Parallel port at %#3lx, ", dev->name, dev->base_addr);
241 if (dev->irq) {
242 printk("using assigned IRQ %d.\n", dev->irq);
243 } else {
244 int irq = 0;
245 #ifdef MODULE
246
247
248 #else
249 unsigned int irqs = probe_irq_on();
250
251 outb(0x00, PAR_CONTROL(dev));
252 udelay(1000);
253 outb(PAR_INTR_OFF, PAR_CONTROL(dev));
254 outb(PAR_INTR_ON, PAR_CONTROL(dev));
255 outb(PAR_INTR_OFF, PAR_CONTROL(dev));
256 udelay(1000);
257 irq = probe_irq_off(irqs);
258 #endif
259 if (irq > 0) {
260 dev->irq = irq;
261 printk("using probed IRQ %d.\n", dev->irq);
262 } else
263 printk("failed to detect IRQ(%d) --"
264 " Please set IRQ by ifconfig.\n", irq);
265 }
266
267 request_region(PAR_DATA(dev), iosize, dev->name);
268
269
270 ether_setup(dev);
271
272
273 dev->hard_start_xmit = plip_tx_packet;
274 dev->open = plip_open;
275 dev->stop = plip_close;
276 dev->get_stats = plip_get_stats;
277 dev->set_config = plip_config;
278 dev->do_ioctl = plip_ioctl;
279 dev->flags = IFF_POINTOPOINT|IFF_NOARP;
280
281
282 dev->priv = kmalloc(sizeof (struct net_local), GFP_KERNEL);
283 if (dev->priv == NULL) {
284 printk(KERN_ERR "%s: out of memory\n", dev->name);
285 release_region(PAR_DATA(dev), iosize);
286 return -ENOMEM;
287 }
288 memset(dev->priv, 0, sizeof(struct net_local));
289 nl = (struct net_local *) dev->priv;
290
291 nl->orig_rebuild_header = dev->rebuild_header;
292 dev->rebuild_header = plip_rebuild_header;
293
294
295 nl->trigger = PLIP_TRIGGER_WAIT;
296 nl->nibble = PLIP_NIBBLE_WAIT;
297
298
299 nl->immediate.next = &tq_last;
300 nl->immediate.sync = 0;
301 nl->immediate.routine = (void *)(void *)plip_bh;
302 nl->immediate.data = dev;
303
304 nl->deferred.next = &tq_last;
305 nl->deferred.sync = 0;
306 nl->deferred.routine = (void *)(void *)plip_kick_bh;
307 nl->deferred.data = dev;
308
309 return 0;
310 }
311
312
313
314
315 static void
316 plip_kick_bh(struct device *dev)
317 {
318 struct net_local *nl = (struct net_local *)dev->priv;
319
320 if (nl->is_deferred) {
321 queue_task(&nl->immediate, &tq_immediate);
322 mark_bh(IMMEDIATE_BH);
323 }
324 }
325
326
327 static int plip_none(struct device *, struct net_local *,
328 struct plip_local *, struct plip_local *);
329 static int plip_receive_packet(struct device *, struct net_local *,
330 struct plip_local *, struct plip_local *);
331 static int plip_send_packet(struct device *, struct net_local *,
332 struct plip_local *, struct plip_local *);
333 static int plip_connection_close(struct device *, struct net_local *,
334 struct plip_local *, struct plip_local *);
335 static int plip_error(struct device *, struct net_local *,
336 struct plip_local *, struct plip_local *);
337 static int plip_bh_timeout_error(struct device *dev, struct net_local *nl,
338 struct plip_local *snd,
339 struct plip_local *rcv,
340 int error);
341
342 #define OK 0
343 #define TIMEOUT 1
344 #define ERROR 2
345
346 typedef int (*plip_func)(struct device *dev, struct net_local *nl,
347 struct plip_local *snd, struct plip_local *rcv);
348
349 static plip_func connection_state_table[] =
350 {
351 plip_none,
352 plip_receive_packet,
353 plip_send_packet,
354 plip_connection_close,
355 plip_error
356 };
357
358
359 static void
360 plip_bh(struct device *dev)
361 {
362 struct net_local *nl = (struct net_local *)dev->priv;
363 struct plip_local *snd = &nl->snd_data;
364 struct plip_local *rcv = &nl->rcv_data;
365 plip_func f;
366 int r;
367
368 nl->is_deferred = 0;
369 f = connection_state_table[nl->connection];
370 if ((r = (*f)(dev, nl, snd, rcv)) != OK
371 && (r = plip_bh_timeout_error(dev, nl, snd, rcv, r)) != OK) {
372 nl->is_deferred = 1;
373 queue_task(&nl->deferred, &tq_timer);
374 }
375 }
376
377 static int
378 plip_bh_timeout_error(struct device *dev, struct net_local *nl,
379 struct plip_local *snd, struct plip_local *rcv,
380 int error)
381 {
382 unsigned char c0;
383
384 cli();
385 if (nl->connection == PLIP_CN_SEND) {
386
387 if (error != ERROR) {
388 nl->timeout_count++;
389 if ((snd->state == PLIP_PK_TRIGGER
390 && nl->timeout_count <= 10)
391 || nl->timeout_count <= 3) {
392 sti();
393
394 return TIMEOUT;
395 }
396 c0 = inb(PAR_STATUS(dev));
397 printk("%s: transmit timeout(%d,%02x)\n",
398 dev->name, snd->state, c0);
399 }
400 nl->enet_stats.tx_errors++;
401 nl->enet_stats.tx_aborted_errors++;
402 } else if (nl->connection == PLIP_CN_RECEIVE) {
403 if (rcv->state == PLIP_PK_TRIGGER) {
404
405 sti();
406 return OK;
407 }
408 if (error != ERROR) {
409 if (++nl->timeout_count <= 3) {
410 sti();
411
412 return TIMEOUT;
413 }
414 c0 = inb(PAR_STATUS(dev));
415 printk("%s: receive timeout(%d,%02x)\n",
416 dev->name, rcv->state, c0);
417 }
418 nl->enet_stats.rx_dropped++;
419 }
420 rcv->state = PLIP_PK_DONE;
421 if (rcv->skb) {
422 rcv->skb->free = 1;
423 kfree_skb(rcv->skb, FREE_READ);
424 rcv->skb = NULL;
425 }
426 snd->state = PLIP_PK_DONE;
427 if (snd->skb) {
428 dev_kfree_skb(snd->skb, FREE_WRITE);
429 snd->skb = NULL;
430 }
431 disable_irq(dev->irq);
432 outb(PAR_INTR_OFF, PAR_CONTROL(dev));
433 dev->tbusy = 1;
434 nl->connection = PLIP_CN_ERROR;
435 outb(0x00, PAR_DATA(dev));
436 sti();
437
438 return TIMEOUT;
439 }
440
441 static int
442 plip_none(struct device *dev, struct net_local *nl,
443 struct plip_local *snd, struct plip_local *rcv)
444 {
445 return OK;
446 }
447
448
449
450 inline static int
451 plip_receive(unsigned short nibble_timeout, unsigned short status_addr,
452 enum plip_nibble_state *ns_p, unsigned char *data_p)
453 {
454 unsigned char c0, c1;
455 unsigned int cx;
456
457 switch (*ns_p) {
458 case PLIP_NB_BEGIN:
459 cx = nibble_timeout;
460 while (1) {
461 c0 = inb(status_addr);
462 udelay(PLIP_DELAY_UNIT);
463 if ((c0 & 0x80) == 0) {
464 c1 = inb(status_addr);
465 if (c0 == c1)
466 break;
467 }
468 if (--cx == 0)
469 return TIMEOUT;
470 }
471 *data_p = (c0 >> 3) & 0x0f;
472 outb(0x10, --status_addr);
473 status_addr++;
474 *ns_p = PLIP_NB_1;
475
476 case PLIP_NB_1:
477 cx = nibble_timeout;
478 while (1) {
479 c0 = inb(status_addr);
480 udelay(PLIP_DELAY_UNIT);
481 if (c0 & 0x80) {
482 c1 = inb(status_addr);
483 if (c0 == c1)
484 break;
485 }
486 if (--cx == 0)
487 return TIMEOUT;
488 }
489 *data_p |= (c0 << 1) & 0xf0;
490 outb(0x00, --status_addr);
491 status_addr++;
492 *ns_p = PLIP_NB_BEGIN;
493 case PLIP_NB_2:
494 break;
495 }
496 return OK;
497 }
498
499
500 static int
501 plip_receive_packet(struct device *dev, struct net_local *nl,
502 struct plip_local *snd, struct plip_local *rcv)
503 {
504 unsigned short status_addr = PAR_STATUS(dev);
505 unsigned short nibble_timeout = nl->nibble;
506 unsigned char *lbuf;
507
508 switch (rcv->state) {
509 case PLIP_PK_TRIGGER:
510 disable_irq(dev->irq);
511 outb(PAR_INTR_OFF, PAR_CONTROL(dev));
512 dev->interrupt = 0;
513 outb(0x01, PAR_DATA(dev));
514 if (net_debug > 2)
515 printk("%s: receive start\n", dev->name);
516 rcv->state = PLIP_PK_LENGTH_LSB;
517 rcv->nibble = PLIP_NB_BEGIN;
518
519 case PLIP_PK_LENGTH_LSB:
520 if (snd->state != PLIP_PK_DONE) {
521 if (plip_receive(nl->trigger, status_addr,
522 &rcv->nibble, &rcv->length.b.lsb)) {
523
524 rcv->state = PLIP_PK_DONE;
525 nl->is_deferred = 1;
526 nl->connection = PLIP_CN_SEND;
527 queue_task(&nl->deferred, &tq_timer);
528 outb(PAR_INTR_ON, PAR_CONTROL(dev));
529 enable_irq(dev->irq);
530 return OK;
531 }
532 } else {
533 if (plip_receive(nibble_timeout, status_addr,
534 &rcv->nibble, &rcv->length.b.lsb))
535 return TIMEOUT;
536 }
537 rcv->state = PLIP_PK_LENGTH_MSB;
538
539 case PLIP_PK_LENGTH_MSB:
540 if (plip_receive(nibble_timeout, status_addr,
541 &rcv->nibble, &rcv->length.b.msb))
542 return TIMEOUT;
543 if (rcv->length.h > dev->mtu + dev->hard_header_len
544 || rcv->length.h < 8) {
545 printk("%s: bogus packet size %d.\n", dev->name, rcv->length.h);
546 return ERROR;
547 }
548
549 rcv->skb = dev_alloc_skb(rcv->length.h);
550 if (rcv->skb == NULL) {
551 printk("%s: Memory squeeze.\n", dev->name);
552 return ERROR;
553 }
554 skb_put(rcv->skb,rcv->length.h);
555 rcv->skb->dev = dev;
556 rcv->state = PLIP_PK_DATA;
557 rcv->byte = 0;
558 rcv->checksum = 0;
559
560 case PLIP_PK_DATA:
561 lbuf = rcv->skb->data;
562 do
563 if (plip_receive(nibble_timeout, status_addr,
564 &rcv->nibble, &lbuf[rcv->byte]))
565 return TIMEOUT;
566 while (++rcv->byte < rcv->length.h);
567 do
568 rcv->checksum += lbuf[--rcv->byte];
569 while (rcv->byte);
570 rcv->state = PLIP_PK_CHECKSUM;
571
572 case PLIP_PK_CHECKSUM:
573 if (plip_receive(nibble_timeout, status_addr,
574 &rcv->nibble, &rcv->data))
575 return TIMEOUT;
576 if (rcv->data != rcv->checksum) {
577 nl->enet_stats.rx_crc_errors++;
578 if (net_debug)
579 printk("%s: checksum error\n", dev->name);
580 return ERROR;
581 }
582 rcv->state = PLIP_PK_DONE;
583
584 case PLIP_PK_DONE:
585
586 rcv->skb->protocol=eth_type_trans(rcv->skb, dev);
587 netif_rx(rcv->skb);
588 nl->enet_stats.rx_packets++;
589 rcv->skb = NULL;
590 if (net_debug > 2)
591 printk("%s: receive end\n", dev->name);
592
593
594 outb (0x00, PAR_DATA(dev));
595 cli();
596 if (snd->state != PLIP_PK_DONE) {
597 nl->connection = PLIP_CN_SEND;
598 sti();
599 queue_task(&nl->immediate, &tq_immediate);
600 outb(PAR_INTR_ON, PAR_CONTROL(dev));
601 enable_irq(dev->irq);
602 return OK;
603 } else {
604 nl->connection = PLIP_CN_NONE;
605 sti();
606 outb(PAR_INTR_ON, PAR_CONTROL(dev));
607 enable_irq(dev->irq);
608 return OK;
609 }
610 }
611 return OK;
612 }
613
614
615
616 inline static int
617 plip_send(unsigned short nibble_timeout, unsigned short data_addr,
618 enum plip_nibble_state *ns_p, unsigned char data)
619 {
620 unsigned char c0;
621 unsigned int cx;
622
623 switch (*ns_p) {
624 case PLIP_NB_BEGIN:
625 outb((data & 0x0f), data_addr);
626 *ns_p = PLIP_NB_1;
627
628 case PLIP_NB_1:
629 outb(0x10 | (data & 0x0f), data_addr);
630 cx = nibble_timeout;
631 data_addr++;
632 while (1) {
633 c0 = inb(data_addr);
634 if ((c0 & 0x80) == 0)
635 break;
636 if (--cx == 0)
637 return TIMEOUT;
638 udelay(PLIP_DELAY_UNIT);
639 }
640 outb(0x10 | (data >> 4), --data_addr);
641 *ns_p = PLIP_NB_2;
642
643 case PLIP_NB_2:
644 outb((data >> 4), data_addr);
645 data_addr++;
646 cx = nibble_timeout;
647 while (1) {
648 c0 = inb(data_addr);
649 if (c0 & 0x80)
650 break;
651 if (--cx == 0)
652 return TIMEOUT;
653 udelay(PLIP_DELAY_UNIT);
654 }
655 data_addr--;
656 *ns_p = PLIP_NB_BEGIN;
657 return OK;
658 }
659 return OK;
660 }
661
662
663 static int
664 plip_send_packet(struct device *dev, struct net_local *nl,
665 struct plip_local *snd, struct plip_local *rcv)
666 {
667 unsigned short data_addr = PAR_DATA(dev);
668 unsigned short nibble_timeout = nl->nibble;
669 unsigned char *lbuf;
670 unsigned char c0;
671 unsigned int cx;
672
673 if (snd->skb == NULL || (lbuf = snd->skb->data) == NULL) {
674 printk("%s: send skb lost\n", dev->name);
675 snd->state = PLIP_PK_DONE;
676 snd->skb = NULL;
677 return ERROR;
678 }
679
680 switch (snd->state) {
681 case PLIP_PK_TRIGGER:
682 if ((inb(PAR_STATUS(dev)) & 0xf8) != 0x80)
683 return TIMEOUT;
684
685
686 outb(0x08, data_addr);
687 cx = nl->trigger;
688 while (1) {
689 udelay(PLIP_DELAY_UNIT);
690 cli();
691 if (nl->connection == PLIP_CN_RECEIVE) {
692 sti();
693
694 nl->enet_stats.collisions++;
695 if (net_debug > 1)
696 printk("%s: collision.\n", dev->name);
697 return OK;
698 }
699 c0 = inb(PAR_STATUS(dev));
700 if (c0 & 0x08) {
701 disable_irq(dev->irq);
702 outb(PAR_INTR_OFF, PAR_CONTROL(dev));
703 if (net_debug > 2)
704 printk("%s: send start\n", dev->name);
705 snd->state = PLIP_PK_LENGTH_LSB;
706 snd->nibble = PLIP_NB_BEGIN;
707 nl->timeout_count = 0;
708 sti();
709 break;
710 }
711 sti();
712 if (--cx == 0) {
713 outb(0x00, data_addr);
714 return TIMEOUT;
715 }
716 }
717
718 case PLIP_PK_LENGTH_LSB:
719 if (plip_send(nibble_timeout, data_addr,
720 &snd->nibble, snd->length.b.lsb))
721 return TIMEOUT;
722 snd->state = PLIP_PK_LENGTH_MSB;
723
724 case PLIP_PK_LENGTH_MSB:
725 if (plip_send(nibble_timeout, data_addr,
726 &snd->nibble, snd->length.b.msb))
727 return TIMEOUT;
728 snd->state = PLIP_PK_DATA;
729 snd->byte = 0;
730 snd->checksum = 0;
731
732 case PLIP_PK_DATA:
733 do
734 if (plip_send(nibble_timeout, data_addr,
735 &snd->nibble, lbuf[snd->byte]))
736 return TIMEOUT;
737 while (++snd->byte < snd->length.h);
738 do
739 snd->checksum += lbuf[--snd->byte];
740 while (snd->byte);
741 snd->state = PLIP_PK_CHECKSUM;
742
743 case PLIP_PK_CHECKSUM:
744 if (plip_send(nibble_timeout, data_addr,
745 &snd->nibble, snd->checksum))
746 return TIMEOUT;
747
748 dev_kfree_skb(snd->skb, FREE_WRITE);
749 nl->enet_stats.tx_packets++;
750 snd->state = PLIP_PK_DONE;
751
752 case PLIP_PK_DONE:
753
754 outb (0x00, data_addr);
755 snd->skb = NULL;
756 if (net_debug > 2)
757 printk("%s: send end\n", dev->name);
758 nl->connection = PLIP_CN_CLOSING;
759 nl->is_deferred = 1;
760 queue_task(&nl->deferred, &tq_timer);
761 outb(PAR_INTR_ON, PAR_CONTROL(dev));
762 enable_irq(dev->irq);
763 return OK;
764 }
765 return OK;
766 }
767
768 static int
769 plip_connection_close(struct device *dev, struct net_local *nl,
770 struct plip_local *snd, struct plip_local *rcv)
771 {
772 cli();
773 if (nl->connection == PLIP_CN_CLOSING) {
774 nl->connection = PLIP_CN_NONE;
775 dev->tbusy = 0;
776 mark_bh(NET_BH);
777 }
778 sti();
779 return OK;
780 }
781
782
783 static int
784 plip_error(struct device *dev, struct net_local *nl,
785 struct plip_local *snd, struct plip_local *rcv)
786 {
787 unsigned char status;
788
789 status = inb(PAR_STATUS(dev));
790 if ((status & 0xf8) == 0x80) {
791 if (net_debug > 2)
792 printk("%s: reset interface.\n", dev->name);
793 nl->connection = PLIP_CN_NONE;
794 dev->tbusy = 0;
795 dev->interrupt = 0;
796 outb(PAR_INTR_ON, PAR_CONTROL(dev));
797 enable_irq(dev->irq);
798 mark_bh(NET_BH);
799 } else {
800 nl->is_deferred = 1;
801 queue_task(&nl->deferred, &tq_timer);
802 }
803
804 return OK;
805 }
806
807
808 static void
809 plip_interrupt(int irq, struct pt_regs * regs)
810 {
811 struct device *dev = (struct device *) irq2dev_map[irq];
812 struct net_local *nl = (struct net_local *)dev->priv;
813 struct plip_local *rcv = &nl->rcv_data;
814 unsigned char c0;
815
816 if (dev == NULL) {
817 printk ("plip_interrupt: irq %d for unknown device.\n", irq);
818 return;
819 }
820
821 if (dev->interrupt)
822 return;
823
824 c0 = inb(PAR_STATUS(dev));
825 if ((c0 & 0xf8) != 0xc0) {
826 if (net_debug > 1)
827 printk("%s: spurious interrupt\n", dev->name);
828 return;
829 }
830 dev->interrupt = 1;
831 if (net_debug > 3)
832 printk("%s: interrupt.\n", dev->name);
833
834 cli();
835 switch (nl->connection) {
836 case PLIP_CN_CLOSING:
837 dev->tbusy = 0;
838 case PLIP_CN_NONE:
839 case PLIP_CN_SEND:
840 dev->last_rx = jiffies;
841 rcv->state = PLIP_PK_TRIGGER;
842 nl->connection = PLIP_CN_RECEIVE;
843 nl->timeout_count = 0;
844 queue_task(&nl->immediate, &tq_immediate);
845 mark_bh(IMMEDIATE_BH);
846 sti();
847 break;
848
849 case PLIP_CN_RECEIVE:
850 sti();
851 printk("%s: receive interrupt when receiving packet\n", dev->name);
852 break;
853
854 case PLIP_CN_ERROR:
855 sti();
856 printk("%s: receive interrupt in error state\n", dev->name);
857 break;
858 }
859 }
860
861
862 static int
863 plip_rebuild_header(void *buff, struct device *dev, unsigned long dst,
864 struct sk_buff *skb)
865 {
866 struct net_local *nl = (struct net_local *)dev->priv;
867 struct ethhdr *eth = (struct ethhdr *)buff;
868 int i;
869
870 if ((dev->flags & IFF_NOARP)==0)
871 return nl->orig_rebuild_header(buff, dev, dst, skb);
872
873 if (eth->h_proto != htons(ETH_P_IP)) {
874 printk("plip_rebuild_header: Don't know how to resolve type %d addresses?\n", (int)eth->h_proto);
875 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
876 return 0;
877 }
878
879 for (i=0; i < ETH_ALEN - sizeof(unsigned long); i++)
880 eth->h_dest[i] = 0xfc;
881 memcpy(&(eth->h_dest[i]), &dst, sizeof(unsigned long));
882 return 0;
883 }
884
885 static int
886 plip_tx_packet(struct sk_buff *skb, struct device *dev)
887 {
888 struct net_local *nl = (struct net_local *)dev->priv;
889 struct plip_local *snd = &nl->snd_data;
890
891 if (dev->tbusy)
892 return 1;
893
894
895
896
897 if (skb == NULL) {
898 dev_tint(dev);
899 return 0;
900 }
901
902 if (set_bit(0, (void*)&dev->tbusy) != 0) {
903 printk("%s: Transmitter access conflict.\n", dev->name);
904 return 1;
905 }
906
907 if (skb->len > dev->mtu + dev->hard_header_len) {
908 printk("%s: packet too big, %d.\n", dev->name, (int)skb->len);
909 dev->tbusy = 0;
910 return 0;
911 }
912
913 if (net_debug > 2)
914 printk("%s: send request\n", dev->name);
915
916 cli();
917 dev->trans_start = jiffies;
918 snd->skb = skb;
919 snd->length.h = skb->len;
920 snd->state = PLIP_PK_TRIGGER;
921 if (nl->connection == PLIP_CN_NONE) {
922 nl->connection = PLIP_CN_SEND;
923 nl->timeout_count = 0;
924 }
925 queue_task(&nl->immediate, &tq_immediate);
926 mark_bh(IMMEDIATE_BH);
927 sti();
928
929 return 0;
930 }
931
932
933
934
935
936
937
938 static int
939 plip_open(struct device *dev)
940 {
941 struct net_local *nl = (struct net_local *)dev->priv;
942 int i;
943
944 if (dev->irq == 0) {
945 printk("%s: IRQ is not set. Please set it by ifconfig.\n", dev->name);
946 return -EAGAIN;
947 }
948 cli();
949 if (request_irq(dev->irq , plip_interrupt, 0, dev->name) != 0) {
950 sti();
951 printk("%s: couldn't get IRQ %d.\n", dev->name, dev->irq);
952 return -EAGAIN;
953 }
954 irq2dev_map[dev->irq] = dev;
955 sti();
956
957
958 outb (0x00, PAR_DATA(dev));
959
960
961 outb(PAR_INTR_ON, PAR_CONTROL(dev));
962
963
964 nl->rcv_data.state = nl->snd_data.state = PLIP_PK_DONE;
965 nl->rcv_data.skb = nl->snd_data.skb = NULL;
966 nl->connection = PLIP_CN_NONE;
967 nl->is_deferred = 0;
968
969
970 for (i=0; i < ETH_ALEN - sizeof(unsigned long); i++)
971 dev->dev_addr[i] = 0xfc;
972 memcpy(&(dev->dev_addr[i]), &dev->pa_addr, sizeof(unsigned long));
973
974 dev->interrupt = 0;
975 dev->start = 1;
976 dev->tbusy = 0;
977 MOD_INC_USE_COUNT;
978 return 0;
979 }
980
981
982 static int
983 plip_close(struct device *dev)
984 {
985 struct net_local *nl = (struct net_local *)dev->priv;
986 struct plip_local *snd = &nl->snd_data;
987 struct plip_local *rcv = &nl->rcv_data;
988
989 dev->tbusy = 1;
990 dev->start = 0;
991 cli();
992 free_irq(dev->irq);
993 irq2dev_map[dev->irq] = NULL;
994 nl->is_deferred = 0;
995 nl->connection = PLIP_CN_NONE;
996 sti();
997 outb(0x00, PAR_DATA(dev));
998
999 snd->state = PLIP_PK_DONE;
1000 if (snd->skb) {
1001 dev_kfree_skb(snd->skb, FREE_WRITE);
1002 snd->skb = NULL;
1003 }
1004 rcv->state = PLIP_PK_DONE;
1005 if (rcv->skb) {
1006 rcv->skb->free = 1;
1007 kfree_skb(rcv->skb, FREE_READ);
1008 rcv->skb = NULL;
1009 }
1010
1011
1012 outb(0x00, PAR_CONTROL(dev));
1013 MOD_DEC_USE_COUNT;
1014 return 0;
1015 }
1016
1017 static struct enet_statistics *
1018 plip_get_stats(struct device *dev)
1019 {
1020 struct net_local *nl = (struct net_local *)dev->priv;
1021 struct enet_statistics *r = &nl->enet_stats;
1022
1023 return r;
1024 }
1025
1026 static int
1027 plip_config(struct device *dev, struct ifmap *map)
1028 {
1029 if (dev->flags & IFF_UP)
1030 return -EBUSY;
1031
1032 if (map->base_addr != (unsigned long)-1
1033 && map->base_addr != dev->base_addr)
1034 printk("%s: You cannot change base_addr of this interface (ignored).\n", dev->name);
1035
1036 if (map->irq != (unsigned char)-1)
1037 dev->irq = map->irq;
1038 return 0;
1039 }
1040
1041 static int
1042 plip_ioctl(struct device *dev, struct ifreq *rq, int cmd)
1043 {
1044 struct net_local *nl = (struct net_local *) dev->priv;
1045 struct plipconf *pc = (struct plipconf *) &rq->ifr_data;
1046
1047 switch(pc->pcmd) {
1048 case PLIP_GET_TIMEOUT:
1049 pc->trigger = nl->trigger;
1050 pc->nibble = nl->nibble;
1051 break;
1052 case PLIP_SET_TIMEOUT:
1053 nl->trigger = pc->trigger;
1054 nl->nibble = pc->nibble;
1055 break;
1056 default:
1057 return -EOPNOTSUPP;
1058 }
1059 return 0;
1060 }
1061
1062 #ifdef MODULE
1063 char kernel_version[] = UTS_RELEASE;
1064 int io[] = {0, 0, 0};
1065 int irq[] = {0, 0, 0};
1066
1067 static struct device dev_plip[] = {
1068 {
1069 "plip0",
1070 0, 0, 0, 0,
1071 0x3BC, 5,
1072 0, 0, 0, NULL, plip_init
1073 },
1074 {
1075 "plip1",
1076 0, 0, 0, 0,
1077 0x378, 7,
1078 0, 0, 0, NULL, plip_init
1079 },
1080 {
1081 "plip2",
1082 0, 0, 0, 0,
1083 0x278, 2,
1084 0, 0, 0, NULL, plip_init
1085 }
1086 };
1087
1088 int
1089 init_module(void)
1090 {
1091 int no_parameters=1;
1092 int devices=0;
1093 int i;
1094
1095
1096 for (i=0; i < 3; i++) {
1097 int specified=0;
1098
1099 if (io[i] != 0) {
1100 dev_plip[i].base_addr = io[i];
1101 specified++;
1102 }
1103 if (irq[i] != 0) {
1104 dev_plip[i].irq = irq[i];
1105 specified++;
1106 }
1107 if (specified) {
1108 if (register_netdev(&dev_plip[i]) != 0) {
1109 printk(KERN_INFO "plip%d: Not found\n", i);
1110 return -EIO;
1111 }
1112 no_parameters = 0;
1113 }
1114 }
1115 if (!no_parameters)
1116 return 0;
1117
1118
1119 for (i=0; i < 3; i++) {
1120 if (register_netdev(&dev_plip[i]) == 0)
1121 devices++;
1122 }
1123 if (devices == 0) {
1124 printk(KERN_INFO "plip: no interfaces found\n");
1125 return -EIO;
1126 }
1127 return 0;
1128 }
1129
1130 void
1131 cleanup_module(void)
1132 {
1133 int i;
1134
1135 for (i=0; i < 3; i++) {
1136 if (dev_plip[i].priv) {
1137 unregister_netdev(&dev_plip[i]);
1138 release_region(PAR_DATA(&dev_plip[i]), (PAR_DATA(&dev_plip[i]) == 0x3bc)? 3 : 8);
1139 kfree_s(dev_plip[i].priv, sizeof(struct net_local));
1140 dev_plip[i].priv = NULL;
1141 }
1142 }
1143 }
1144 #endif
1145
1146
1147
1148
1149
1150