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