setup enviroment for compilation
[linux-2.4.21-pre4.git] / drivers / net / plip.c
1 /* $Id: plip.c,v 1.1.1.1 2005/04/11 02:50:27 jack Exp $ */
2 /* PLIP: A parallel port "network" driver for Linux. */
3 /* This driver is for parallel port with 5-bit cable (LapLink (R) cable). */
4 /*
5  * Authors:     Donald Becker <becker@scyld.com>
6  *              Tommy Thorn <thorn@daimi.aau.dk>
7  *              Tanabe Hiroyasu <hiro@sanpo.t.u-tokyo.ac.jp>
8  *              Alan Cox <gw4pts@gw4pts.ampr.org>
9  *              Peter Bauer <100136.3530@compuserve.com>
10  *              Niibe Yutaka <gniibe@mri.co.jp>
11  *              Nimrod Zimerman <zimerman@mailandnews.com>
12  *
13  * Enhancements:
14  *              Modularization and ifreq/ifmap support by Alan Cox.
15  *              Rewritten by Niibe Yutaka.
16  *              parport-sharing awareness code by Philip Blundell.
17  *              SMP locking by Niibe Yutaka.
18  *              Support for parallel ports with no IRQ (poll mode),
19  *              Modifications to use the parallel port API 
20  *              by Nimrod Zimerman.
21  *
22  * Fixes:
23  *              Niibe Yutaka
24  *                - Module initialization.
25  *                - MTU fix.
26  *                - Make sure other end is OK, before sending a packet.
27  *                - Fix immediate timer problem.
28  *
29  *              Al Viro
30  *                - Changed {enable,disable}_irq handling to make it work
31  *                  with new ("stack") semantics.
32  *
33  *              This program is free software; you can redistribute it and/or
34  *              modify it under the terms of the GNU General Public License
35  *              as published by the Free Software Foundation; either version
36  *              2 of the License, or (at your option) any later version.
37  */
38
39 /*
40  * Original version and the name 'PLIP' from Donald Becker <becker@scyld.com>
41  * inspired by Russ Nelson's parallel port packet driver.
42  *
43  * NOTE:
44  *     Tanabe Hiroyasu had changed the protocol, and it was in Linux v1.0.
45  *     Because of the necessity to communicate to DOS machines with the
46  *     Crynwr packet driver, Peter Bauer changed the protocol again
47  *     back to original protocol.
48  *
49  *     This version follows original PLIP protocol.
50  *     So, this PLIP can't communicate the PLIP of Linux v1.0.
51  */
52
53 /*
54  *     To use with DOS box, please do (Turn on ARP switch):
55  *      # ifconfig plip[0-2] arp
56  */
57 static const char version[] = "NET3 PLIP version 2.4-parport gniibe@mri.co.jp\n";
58
59 /*
60   Sources:
61         Ideas and protocols came from Russ Nelson's <nelson@crynwr.com>
62         "parallel.asm" parallel port packet driver.
63
64   The "Crynwr" parallel port standard specifies the following protocol:
65     Trigger by sending nibble '0x8' (this causes interrupt on other end)
66     count-low octet
67     count-high octet
68     ... data octets
69     checksum octet
70   Each octet is sent as <wait for rx. '0x1?'> <send 0x10+(octet&0x0F)>
71                         <wait for rx. '0x0?'> <send 0x00+((octet>>4)&0x0F)>
72
73   The packet is encapsulated as if it were ethernet.
74
75   The cable used is a de facto standard parallel null cable -- sold as
76   a "LapLink" cable by various places.  You'll need a 12-conductor cable to
77   make one yourself.  The wiring is:
78     SLCTIN      17 - 17
79     GROUND      25 - 25
80     D0->ERROR   2 - 15          15 - 2
81     D1->SLCT    3 - 13          13 - 3
82     D2->PAPOUT  4 - 12          12 - 4
83     D3->ACK     5 - 10          10 - 5
84     D4->BUSY    6 - 11          11 - 6
85   Do not connect the other pins.  They are
86     D5,D6,D7 are 7,8,9
87     STROBE is 1, FEED is 14, INIT is 16
88     extra grounds are 18,19,20,21,22,23,24
89 */
90
91 #include <linux/module.h>
92 #include <linux/kernel.h>
93 #include <linux/sched.h>
94 #include <linux/types.h>
95 #include <linux/fcntl.h>
96 #include <linux/interrupt.h>
97 #include <linux/string.h>
98 #include <linux/ptrace.h>
99 #include <linux/if_ether.h>
100 #include <asm/system.h>
101 #include <linux/in.h>
102 #include <linux/errno.h>
103 #include <linux/delay.h>
104 #include <linux/lp.h>
105 #include <linux/init.h>
106
107 #include <linux/netdevice.h>
108 #include <linux/etherdevice.h>
109 #include <linux/inetdevice.h>
110 #include <linux/skbuff.h>
111 #include <linux/if_plip.h>
112 #include <net/neighbour.h>
113
114 #include <linux/tqueue.h>
115 #include <linux/ioport.h>
116 #include <linux/spinlock.h>
117 #include <asm/bitops.h>
118 #include <asm/irq.h>
119 #include <asm/byteorder.h>
120 #include <asm/semaphore.h>
121
122 #include <linux/parport.h>
123
124 /* Maximum number of devices to support. */
125 #define PLIP_MAX  8
126
127 /* Use 0 for production, 1 for verification, >2 for debug */
128 #ifndef NET_DEBUG
129 #define NET_DEBUG 1
130 #endif
131 static unsigned int net_debug = NET_DEBUG;
132
133 #define ENABLE(irq)  if (irq != -1) enable_irq(irq)
134 #define DISABLE(irq) if (irq != -1) disable_irq(irq)
135
136 /* In micro second */
137 #define PLIP_DELAY_UNIT            1
138
139 /* Connection time out = PLIP_TRIGGER_WAIT * PLIP_DELAY_UNIT usec */
140 #define PLIP_TRIGGER_WAIT        500
141
142 /* Nibble time out = PLIP_NIBBLE_WAIT * PLIP_DELAY_UNIT usec */
143 #define PLIP_NIBBLE_WAIT        3000
144
145 /* Bottom halves */
146 static void plip_kick_bh(struct net_device *dev);
147 static void plip_bh(struct net_device *dev);
148 static void plip_timer_bh(struct net_device *dev);
149
150 /* Interrupt handler */
151 static void plip_interrupt(int irq, void *dev_id, struct pt_regs *regs);
152
153 /* Functions for DEV methods */
154 static int plip_tx_packet(struct sk_buff *skb, struct net_device *dev);
155 static int plip_hard_header(struct sk_buff *skb, struct net_device *dev,
156                             unsigned short type, void *daddr,
157                             void *saddr, unsigned len);
158 static int plip_hard_header_cache(struct neighbour *neigh,
159                                   struct hh_cache *hh);
160 static int plip_open(struct net_device *dev);
161 static int plip_close(struct net_device *dev);
162 static struct net_device_stats *plip_get_stats(struct net_device *dev);
163 static int plip_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
164 static int plip_preempt(void *handle);
165 static void plip_wakeup(void *handle);
166 \f
167 enum plip_connection_state {
168         PLIP_CN_NONE=0,
169         PLIP_CN_RECEIVE,
170         PLIP_CN_SEND,
171         PLIP_CN_CLOSING,
172         PLIP_CN_ERROR
173 };
174
175 enum plip_packet_state {
176         PLIP_PK_DONE=0,
177         PLIP_PK_TRIGGER,
178         PLIP_PK_LENGTH_LSB,
179         PLIP_PK_LENGTH_MSB,
180         PLIP_PK_DATA,
181         PLIP_PK_CHECKSUM
182 };
183
184 enum plip_nibble_state {
185         PLIP_NB_BEGIN,
186         PLIP_NB_1,
187         PLIP_NB_2,
188 };
189
190 struct plip_local {
191         enum plip_packet_state state;
192         enum plip_nibble_state nibble;
193         union {
194                 struct {
195 #if defined(__LITTLE_ENDIAN)
196                         unsigned char lsb;
197                         unsigned char msb;
198 #elif defined(__BIG_ENDIAN)
199                         unsigned char msb;
200                         unsigned char lsb;
201 #else
202 #error  "Please fix the endianness defines in <asm/byteorder.h>"
203 #endif
204                 } b;
205                 unsigned short h;
206         } length;
207         unsigned short byte;
208         unsigned char  checksum;
209         unsigned char  data;
210         struct sk_buff *skb;
211 };
212
213 struct net_local {
214         struct net_device_stats enet_stats;
215         struct tq_struct immediate;
216         struct tq_struct deferred;
217         struct tq_struct timer;
218         struct plip_local snd_data;
219         struct plip_local rcv_data;
220         struct pardevice *pardev;
221         unsigned long  trigger;
222         unsigned long  nibble;
223         enum plip_connection_state connection;
224         unsigned short timeout_count;
225         int is_deferred;
226         int port_owner;
227         int should_relinquish;
228         int (*orig_hard_header)(struct sk_buff *skb, struct net_device *dev,
229                                 unsigned short type, void *daddr,
230                                 void *saddr, unsigned len);
231         int (*orig_hard_header_cache)(struct neighbour *neigh,
232                                       struct hh_cache *hh);
233         spinlock_t lock;
234         atomic_t kill_timer;
235         struct semaphore killed_timer_sem;
236 };
237 \f
238 inline static void enable_parport_interrupts (struct net_device *dev)
239 {
240         if (dev->irq != -1)
241         {
242                 struct parport *port =
243                    ((struct net_local *)dev->priv)->pardev->port;
244                 port->ops->enable_irq (port);
245         }
246 }
247
248 inline static void disable_parport_interrupts (struct net_device *dev)
249 {
250         if (dev->irq != -1)
251         {
252                 struct parport *port =
253                    ((struct net_local *)dev->priv)->pardev->port;
254                 port->ops->disable_irq (port);
255         }
256 }
257
258 inline static void write_data (struct net_device *dev, unsigned char data)
259 {
260         struct parport *port =
261            ((struct net_local *)dev->priv)->pardev->port;
262
263         port->ops->write_data (port, data);
264 }
265
266 inline static unsigned char read_status (struct net_device *dev)
267 {
268         struct parport *port =
269            ((struct net_local *)dev->priv)->pardev->port;
270
271         return port->ops->read_status (port);
272 }
273 \f
274 /* Entry point of PLIP driver.
275    Probe the hardware, and register/initialize the driver.
276
277    PLIP is rather weird, because of the way it interacts with the parport
278    system.  It is _not_ initialised from Space.c.  Instead, plip_init()
279    is called, and that function makes up a "struct net_device" for each port, and
280    then calls us here.
281
282    */
283 int __init
284 plip_init_dev(struct net_device *dev, struct parport *pb)
285 {
286         struct net_local *nl;
287         struct pardevice *pardev;
288
289         SET_MODULE_OWNER(dev);
290         dev->irq = pb->irq;
291         dev->base_addr = pb->base;
292
293         if (pb->irq == -1) {
294                 printk(KERN_INFO "plip: %s has no IRQ. Using IRQ-less mode,"
295                                  "which is fairly inefficient!\n", pb->name);
296         }
297
298         pardev = parport_register_device(pb, dev->name, plip_preempt,
299                                          plip_wakeup, plip_interrupt, 
300                                          0, dev);
301
302         if (!pardev)
303                 return -ENODEV;
304
305         printk(KERN_INFO "%s", version);
306         if (dev->irq != -1)
307                 printk(KERN_INFO "%s: Parallel port at %#3lx, using IRQ %d.\n",
308                        dev->name, dev->base_addr, dev->irq);
309         else
310                 printk(KERN_INFO "%s: Parallel port at %#3lx, not using IRQ.\n",
311                        dev->name, dev->base_addr);
312
313         /* Fill in the generic fields of the device structure. */
314         ether_setup(dev);
315
316         /* Then, override parts of it */
317         dev->hard_start_xmit     = plip_tx_packet;
318         dev->open                = plip_open;
319         dev->stop                = plip_close;
320         dev->get_stats           = plip_get_stats;
321         dev->do_ioctl            = plip_ioctl;
322         dev->header_cache_update = NULL;
323         dev->tx_queue_len        = 10;
324         dev->flags               = IFF_POINTOPOINT|IFF_NOARP;
325         memset(dev->dev_addr, 0xfc, ETH_ALEN);
326
327         /* Set the private structure */
328         dev->priv = kmalloc(sizeof (struct net_local), GFP_KERNEL);
329         if (dev->priv == NULL) {
330                 printk(KERN_ERR "%s: out of memory\n", dev->name);
331                 parport_unregister_device(pardev);
332                 return -ENOMEM;
333         }
334         memset(dev->priv, 0, sizeof(struct net_local));
335         nl = (struct net_local *) dev->priv;
336
337         nl->orig_hard_header    = dev->hard_header;
338         dev->hard_header        = plip_hard_header;
339
340         nl->orig_hard_header_cache = dev->hard_header_cache;
341         dev->hard_header_cache     = plip_hard_header_cache;
342
343         nl->pardev = pardev; 
344
345         nl->port_owner = 0;
346
347         /* Initialize constants */
348         nl->trigger     = PLIP_TRIGGER_WAIT;
349         nl->nibble      = PLIP_NIBBLE_WAIT;
350
351         /* Initialize task queue structures */
352         INIT_LIST_HEAD(&nl->immediate.list);
353         nl->immediate.sync = 0;
354         nl->immediate.routine = (void (*)(void *))plip_bh;
355         nl->immediate.data = dev;
356
357         INIT_LIST_HEAD(&nl->deferred.list);
358         nl->deferred.sync = 0;
359         nl->deferred.routine = (void (*)(void *))plip_kick_bh;
360         nl->deferred.data = dev;
361
362         if (dev->irq == -1) {
363                 INIT_LIST_HEAD(&nl->timer.list);
364                 nl->timer.sync = 0;
365                 nl->timer.routine = (void (*)(void *))plip_timer_bh;
366                 nl->timer.data = dev;
367         }
368
369         spin_lock_init(&nl->lock);
370
371         return 0;
372 }
373 \f
374 /* Bottom half handler for the delayed request.
375    This routine is kicked by do_timer().
376    Request `plip_bh' to be invoked. */
377 static void
378 plip_kick_bh(struct net_device *dev)
379 {
380         struct net_local *nl = (struct net_local *)dev->priv;
381
382         if (nl->is_deferred) {
383                 queue_task(&nl->immediate, &tq_immediate);
384                 mark_bh(IMMEDIATE_BH);
385         }
386 }
387
388 /* Forward declarations of internal routines */
389 static int plip_none(struct net_device *, struct net_local *,
390                      struct plip_local *, struct plip_local *);
391 static int plip_receive_packet(struct net_device *, struct net_local *,
392                                struct plip_local *, struct plip_local *);
393 static int plip_send_packet(struct net_device *, struct net_local *,
394                             struct plip_local *, struct plip_local *);
395 static int plip_connection_close(struct net_device *, struct net_local *,
396                                  struct plip_local *, struct plip_local *);
397 static int plip_error(struct net_device *, struct net_local *,
398                       struct plip_local *, struct plip_local *);
399 static int plip_bh_timeout_error(struct net_device *dev, struct net_local *nl,
400                                  struct plip_local *snd,
401                                  struct plip_local *rcv,
402                                  int error);
403
404 #define OK        0
405 #define TIMEOUT   1
406 #define ERROR     2
407 #define HS_TIMEOUT      3
408
409 typedef int (*plip_func)(struct net_device *dev, struct net_local *nl,
410                          struct plip_local *snd, struct plip_local *rcv);
411
412 static plip_func connection_state_table[] =
413 {
414         plip_none,
415         plip_receive_packet,
416         plip_send_packet,
417         plip_connection_close,
418         plip_error
419 };
420
421 /* Bottom half handler of PLIP. */
422 static void
423 plip_bh(struct net_device *dev)
424 {
425         struct net_local *nl = (struct net_local *)dev->priv;
426         struct plip_local *snd = &nl->snd_data;
427         struct plip_local *rcv = &nl->rcv_data;
428         plip_func f;
429         int r;
430
431         nl->is_deferred = 0;
432         f = connection_state_table[nl->connection];
433         if ((r = (*f)(dev, nl, snd, rcv)) != OK
434             && (r = plip_bh_timeout_error(dev, nl, snd, rcv, r)) != OK) {
435                 nl->is_deferred = 1;
436                 queue_task(&nl->deferred, &tq_timer);
437         }
438 }
439
440 static void
441 plip_timer_bh(struct net_device *dev)
442 {
443         struct net_local *nl = (struct net_local *)dev->priv;
444         
445         if (!(atomic_read (&nl->kill_timer))) {
446                 plip_interrupt (-1, dev, NULL);
447
448                 queue_task (&nl->timer, &tq_timer);
449         }
450         else {
451                 up (&nl->killed_timer_sem);
452         }
453 }
454
455 static int
456 plip_bh_timeout_error(struct net_device *dev, struct net_local *nl,
457                       struct plip_local *snd, struct plip_local *rcv,
458                       int error)
459 {
460         unsigned char c0;
461         /*
462          * This is tricky. If we got here from the beginning of send (either
463          * with ERROR or HS_TIMEOUT) we have IRQ enabled. Otherwise it's
464          * already disabled. With the old variant of {enable,disable}_irq()
465          * extra disable_irq() was a no-op. Now it became mortal - it's
466          * unbalanced and thus we'll never re-enable IRQ (until rmmod plip,
467          * that is). So we have to treat HS_TIMEOUT and ERROR from send
468          * in a special way.
469          */
470
471         spin_lock_irq(&nl->lock);
472         if (nl->connection == PLIP_CN_SEND) {
473
474                 if (error != ERROR) { /* Timeout */
475                         nl->timeout_count++;
476                         if ((error == HS_TIMEOUT
477                              && nl->timeout_count <= 10)
478                             || nl->timeout_count <= 3) {
479                                 spin_unlock_irq(&nl->lock);
480                                 /* Try again later */
481                                 return TIMEOUT;
482                         }
483                         c0 = read_status(dev);
484                         printk(KERN_WARNING "%s: transmit timeout(%d,%02x)\n",
485                                dev->name, snd->state, c0);
486                 } else
487                         error = HS_TIMEOUT;
488                 nl->enet_stats.tx_errors++;
489                 nl->enet_stats.tx_aborted_errors++;
490         } else if (nl->connection == PLIP_CN_RECEIVE) {
491                 if (rcv->state == PLIP_PK_TRIGGER) {
492                         /* Transmission was interrupted. */
493                         spin_unlock_irq(&nl->lock);
494                         return OK;
495                 }
496                 if (error != ERROR) { /* Timeout */
497                         if (++nl->timeout_count <= 3) {
498                                 spin_unlock_irq(&nl->lock);
499                                 /* Try again later */
500                                 return TIMEOUT;
501                         }
502                         c0 = read_status(dev);
503                         printk(KERN_WARNING "%s: receive timeout(%d,%02x)\n",
504                                dev->name, rcv->state, c0);
505                 }
506                 nl->enet_stats.rx_dropped++;
507         }
508         rcv->state = PLIP_PK_DONE;
509         if (rcv->skb) {
510                 kfree_skb(rcv->skb);
511                 rcv->skb = NULL;
512         }
513         snd->state = PLIP_PK_DONE;
514         if (snd->skb) {
515                 dev_kfree_skb(snd->skb);
516                 snd->skb = NULL;
517         }
518         spin_unlock_irq(&nl->lock);
519         if (error == HS_TIMEOUT) {
520                 DISABLE(dev->irq);
521                 synchronize_irq();
522         }
523         disable_parport_interrupts (dev);
524         netif_stop_queue (dev);
525         nl->connection = PLIP_CN_ERROR;
526         write_data (dev, 0x00);
527
528         return TIMEOUT;
529 }
530 \f
531 static int
532 plip_none(struct net_device *dev, struct net_local *nl,
533           struct plip_local *snd, struct plip_local *rcv)
534 {
535         return OK;
536 }
537
538 /* PLIP_RECEIVE --- receive a byte(two nibbles)
539    Returns OK on success, TIMEOUT on timeout */
540 inline static int
541 plip_receive(unsigned short nibble_timeout, struct net_device *dev,
542              enum plip_nibble_state *ns_p, unsigned char *data_p)
543 {
544         unsigned char c0, c1;
545         unsigned int cx;
546
547         switch (*ns_p) {
548         case PLIP_NB_BEGIN:
549                 cx = nibble_timeout;
550                 while (1) {
551                         c0 = read_status(dev);
552                         udelay(PLIP_DELAY_UNIT);
553                         if ((c0 & 0x80) == 0) {
554                                 c1 = read_status(dev);
555                                 if (c0 == c1)
556                                         break;
557                         }
558                         if (--cx == 0)
559                                 return TIMEOUT;
560                 }
561                 *data_p = (c0 >> 3) & 0x0f;
562                 write_data (dev, 0x10); /* send ACK */
563                 *ns_p = PLIP_NB_1;
564
565         case PLIP_NB_1:
566                 cx = nibble_timeout;
567                 while (1) {
568                         c0 = read_status(dev);
569                         udelay(PLIP_DELAY_UNIT);
570                         if (c0 & 0x80) {
571                                 c1 = read_status(dev);
572                                 if (c0 == c1)
573                                         break;
574                         }
575                         if (--cx == 0)
576                                 return TIMEOUT;
577                 }
578                 *data_p |= (c0 << 1) & 0xf0;
579                 write_data (dev, 0x00); /* send ACK */
580                 *ns_p = PLIP_NB_BEGIN;
581         case PLIP_NB_2:
582                 break;
583         }
584         return OK;
585 }
586
587 /*
588  *      Determine the packet's protocol ID. The rule here is that we 
589  *      assume 802.3 if the type field is short enough to be a length.
590  *      This is normal practice and works for any 'now in use' protocol.
591  *
592  *      PLIP is ethernet ish but the daddr might not be valid if unicast.
593  *      PLIP fortunately has no bus architecture (its Point-to-point).
594  *
595  *      We can't fix the daddr thing as that quirk (more bug) is embedded
596  *      in far too many old systems not all even running Linux.
597  */
598  
599 static unsigned short plip_type_trans(struct sk_buff *skb, struct net_device *dev)
600 {
601         struct ethhdr *eth;
602         unsigned char *rawp;
603         
604         skb->mac.raw=skb->data;
605         skb_pull(skb,dev->hard_header_len);
606         eth= skb->mac.ethernet;
607         
608         if(*eth->h_dest&1)
609         {
610                 if(memcmp(eth->h_dest,dev->broadcast, ETH_ALEN)==0)
611                         skb->pkt_type=PACKET_BROADCAST;
612                 else
613                         skb->pkt_type=PACKET_MULTICAST;
614         }
615         
616         /*
617          *      This ALLMULTI check should be redundant by 1.4
618          *      so don't forget to remove it.
619          */
620          
621         if (ntohs(eth->h_proto) >= 1536)
622                 return eth->h_proto;
623                 
624         rawp = skb->data;
625         
626         /*
627          *      This is a magic hack to spot IPX packets. Older Novell breaks
628          *      the protocol design and runs IPX over 802.3 without an 802.2 LLC
629          *      layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
630          *      won't work for fault tolerant netware but does for the rest.
631          */
632         if (*(unsigned short *)rawp == 0xFFFF)
633                 return htons(ETH_P_802_3);
634                 
635         /*
636          *      Real 802.2 LLC
637          */
638         return htons(ETH_P_802_2);
639 }
640
641
642 /* PLIP_RECEIVE_PACKET --- receive a packet */
643 static int
644 plip_receive_packet(struct net_device *dev, struct net_local *nl,
645                     struct plip_local *snd, struct plip_local *rcv)
646 {
647         unsigned short nibble_timeout = nl->nibble;
648         unsigned char *lbuf;
649
650         switch (rcv->state) {
651         case PLIP_PK_TRIGGER:
652                 DISABLE(dev->irq);
653                 /* Don't need to synchronize irq, as we can safely ignore it */
654                 disable_parport_interrupts (dev);
655                 write_data (dev, 0x01); /* send ACK */
656                 if (net_debug > 2)
657                         printk(KERN_DEBUG "%s: receive start\n", dev->name);
658                 rcv->state = PLIP_PK_LENGTH_LSB;
659                 rcv->nibble = PLIP_NB_BEGIN;
660
661         case PLIP_PK_LENGTH_LSB:
662                 if (snd->state != PLIP_PK_DONE) {
663                         if (plip_receive(nl->trigger, dev,
664                                          &rcv->nibble, &rcv->length.b.lsb)) {
665                                 /* collision, here dev->tbusy == 1 */
666                                 rcv->state = PLIP_PK_DONE;
667                                 nl->is_deferred = 1;
668                                 nl->connection = PLIP_CN_SEND;
669                                 queue_task(&nl->deferred, &tq_timer);
670                                 enable_parport_interrupts (dev);
671                                 ENABLE(dev->irq);
672                                 return OK;
673                         }
674                 } else {
675                         if (plip_receive(nibble_timeout, dev,
676                                          &rcv->nibble, &rcv->length.b.lsb))
677                                 return TIMEOUT;
678                 }
679                 rcv->state = PLIP_PK_LENGTH_MSB;
680
681         case PLIP_PK_LENGTH_MSB:
682                 if (plip_receive(nibble_timeout, dev,
683                                  &rcv->nibble, &rcv->length.b.msb))
684                         return TIMEOUT;
685                 if (rcv->length.h > dev->mtu + dev->hard_header_len
686                     || rcv->length.h < 8) {
687                         printk(KERN_WARNING "%s: bogus packet size %d.\n", dev->name, rcv->length.h);
688                         return ERROR;
689                 }
690                 /* Malloc up new buffer. */
691                 rcv->skb = dev_alloc_skb(rcv->length.h + 2);
692                 if (rcv->skb == NULL) {
693                         printk(KERN_ERR "%s: Memory squeeze.\n", dev->name);
694                         return ERROR;
695                 }
696                 skb_reserve(rcv->skb, 2);       /* Align IP on 16 byte boundaries */
697                 skb_put(rcv->skb,rcv->length.h);
698                 rcv->skb->dev = dev;
699                 rcv->state = PLIP_PK_DATA;
700                 rcv->byte = 0;
701                 rcv->checksum = 0;
702
703         case PLIP_PK_DATA:
704                 lbuf = rcv->skb->data;
705                 do
706                         if (plip_receive(nibble_timeout, dev,
707                                          &rcv->nibble, &lbuf[rcv->byte]))
708                                 return TIMEOUT;
709                 while (++rcv->byte < rcv->length.h);
710                 do
711                         rcv->checksum += lbuf[--rcv->byte];
712                 while (rcv->byte);
713                 rcv->state = PLIP_PK_CHECKSUM;
714
715         case PLIP_PK_CHECKSUM:
716                 if (plip_receive(nibble_timeout, dev,
717                                  &rcv->nibble, &rcv->data))
718                         return TIMEOUT;
719                 if (rcv->data != rcv->checksum) {
720                         nl->enet_stats.rx_crc_errors++;
721                         if (net_debug)
722                                 printk(KERN_DEBUG "%s: checksum error\n", dev->name);
723                         return ERROR;
724                 }
725                 rcv->state = PLIP_PK_DONE;
726
727         case PLIP_PK_DONE:
728                 /* Inform the upper layer for the arrival of a packet. */
729                 rcv->skb->protocol=plip_type_trans(rcv->skb, dev);
730                 netif_rx(rcv->skb);
731                 dev->last_rx = jiffies;
732                 nl->enet_stats.rx_bytes += rcv->length.h;
733                 nl->enet_stats.rx_packets++;
734                 rcv->skb = NULL;
735                 if (net_debug > 2)
736                         printk(KERN_DEBUG "%s: receive end\n", dev->name);
737
738                 /* Close the connection. */
739                 write_data (dev, 0x00);
740                 spin_lock_irq(&nl->lock);
741                 if (snd->state != PLIP_PK_DONE) {
742                         nl->connection = PLIP_CN_SEND;
743                         spin_unlock_irq(&nl->lock);
744                         queue_task(&nl->immediate, &tq_immediate);
745                         mark_bh(IMMEDIATE_BH);
746                         enable_parport_interrupts (dev);
747                         ENABLE(dev->irq);
748                         return OK;
749                 } else {
750                         nl->connection = PLIP_CN_NONE;
751                         spin_unlock_irq(&nl->lock);
752                         enable_parport_interrupts (dev);
753                         ENABLE(dev->irq);
754                         return OK;
755                 }
756         }
757         return OK;
758 }
759
760 /* PLIP_SEND --- send a byte (two nibbles)
761    Returns OK on success, TIMEOUT when timeout    */
762 inline static int
763 plip_send(unsigned short nibble_timeout, struct net_device *dev,
764           enum plip_nibble_state *ns_p, unsigned char data)
765 {
766         unsigned char c0;
767         unsigned int cx;
768
769         switch (*ns_p) {
770         case PLIP_NB_BEGIN:
771                 write_data (dev, data & 0x0f);
772                 *ns_p = PLIP_NB_1;
773
774         case PLIP_NB_1:
775                 write_data (dev, 0x10 | (data & 0x0f));
776                 cx = nibble_timeout;
777                 while (1) {
778                         c0 = read_status(dev);
779                         if ((c0 & 0x80) == 0)
780                                 break;
781                         if (--cx == 0)
782                                 return TIMEOUT;
783                         udelay(PLIP_DELAY_UNIT);
784                 }
785                 write_data (dev, 0x10 | (data >> 4));
786                 *ns_p = PLIP_NB_2;
787
788         case PLIP_NB_2:
789                 write_data (dev, (data >> 4));
790                 cx = nibble_timeout;
791                 while (1) {
792                         c0 = read_status(dev);
793                         if (c0 & 0x80)
794                                 break;
795                         if (--cx == 0)
796                                 return TIMEOUT;
797                         udelay(PLIP_DELAY_UNIT);
798                 }
799                 *ns_p = PLIP_NB_BEGIN;
800                 return OK;
801         }
802         return OK;
803 }
804
805 /* PLIP_SEND_PACKET --- send a packet */
806 static int
807 plip_send_packet(struct net_device *dev, struct net_local *nl,
808                  struct plip_local *snd, struct plip_local *rcv)
809 {
810         unsigned short nibble_timeout = nl->nibble;
811         unsigned char *lbuf;
812         unsigned char c0;
813         unsigned int cx;
814
815         if (snd->skb == NULL || (lbuf = snd->skb->data) == NULL) {
816                 printk(KERN_DEBUG "%s: send skb lost\n", dev->name);
817                 snd->state = PLIP_PK_DONE;
818                 snd->skb = NULL;
819                 return ERROR;
820         }
821
822         switch (snd->state) {
823         case PLIP_PK_TRIGGER:
824                 if ((read_status(dev) & 0xf8) != 0x80)
825                         return HS_TIMEOUT;
826
827                 /* Trigger remote rx interrupt. */
828                 write_data (dev, 0x08);
829                 cx = nl->trigger;
830                 while (1) {
831                         udelay(PLIP_DELAY_UNIT);
832                         spin_lock_irq(&nl->lock);
833                         if (nl->connection == PLIP_CN_RECEIVE) {
834                                 spin_unlock_irq(&nl->lock);
835                                 /* Interrupted. */
836                                 nl->enet_stats.collisions++;
837                                 return OK;
838                         }
839                         c0 = read_status(dev);
840                         if (c0 & 0x08) {
841                                 spin_unlock_irq(&nl->lock);
842                                 DISABLE(dev->irq);
843                                 synchronize_irq();
844                                 if (nl->connection == PLIP_CN_RECEIVE) {
845                                         /* Interrupted.
846                                            We don't need to enable irq,
847                                            as it is soon disabled.    */
848                                         /* Yes, we do. New variant of
849                                            {enable,disable}_irq *counts*
850                                            them.  -- AV  */
851                                         ENABLE(dev->irq);
852                                         nl->enet_stats.collisions++;
853                                         return OK;
854                                 }
855                                 disable_parport_interrupts (dev);
856                                 if (net_debug > 2)
857                                         printk(KERN_DEBUG "%s: send start\n", dev->name);
858                                 snd->state = PLIP_PK_LENGTH_LSB;
859                                 snd->nibble = PLIP_NB_BEGIN;
860                                 nl->timeout_count = 0;
861                                 break;
862                         }
863                         spin_unlock_irq(&nl->lock);
864                         if (--cx == 0) {
865                                 write_data (dev, 0x00);
866                                 return HS_TIMEOUT;
867                         }
868                 }
869
870         case PLIP_PK_LENGTH_LSB:
871                 if (plip_send(nibble_timeout, dev,
872                               &snd->nibble, snd->length.b.lsb))
873                         return TIMEOUT;
874                 snd->state = PLIP_PK_LENGTH_MSB;
875
876         case PLIP_PK_LENGTH_MSB:
877                 if (plip_send(nibble_timeout, dev,
878                               &snd->nibble, snd->length.b.msb))
879                         return TIMEOUT;
880                 snd->state = PLIP_PK_DATA;
881                 snd->byte = 0;
882                 snd->checksum = 0;
883
884         case PLIP_PK_DATA:
885                 do
886                         if (plip_send(nibble_timeout, dev,
887                                       &snd->nibble, lbuf[snd->byte]))
888                                 return TIMEOUT;
889                 while (++snd->byte < snd->length.h);
890                 do
891                         snd->checksum += lbuf[--snd->byte];
892                 while (snd->byte);
893                 snd->state = PLIP_PK_CHECKSUM;
894
895         case PLIP_PK_CHECKSUM:
896                 if (plip_send(nibble_timeout, dev,
897                               &snd->nibble, snd->checksum))
898                         return TIMEOUT;
899
900                 nl->enet_stats.tx_bytes += snd->skb->len;
901                 dev_kfree_skb(snd->skb);
902                 nl->enet_stats.tx_packets++;
903                 snd->state = PLIP_PK_DONE;
904
905         case PLIP_PK_DONE:
906                 /* Close the connection */
907                 write_data (dev, 0x00);
908                 snd->skb = NULL;
909                 if (net_debug > 2)
910                         printk(KERN_DEBUG "%s: send end\n", dev->name);
911                 nl->connection = PLIP_CN_CLOSING;
912                 nl->is_deferred = 1;
913                 queue_task(&nl->deferred, &tq_timer);
914                 enable_parport_interrupts (dev);
915                 ENABLE(dev->irq);
916                 return OK;
917         }
918         return OK;
919 }
920
921 static int
922 plip_connection_close(struct net_device *dev, struct net_local *nl,
923                       struct plip_local *snd, struct plip_local *rcv)
924 {
925         spin_lock_irq(&nl->lock);
926         if (nl->connection == PLIP_CN_CLOSING) {
927                 nl->connection = PLIP_CN_NONE;
928                 netif_wake_queue (dev);
929         }
930         spin_unlock_irq(&nl->lock);
931         if (nl->should_relinquish) {
932                 nl->should_relinquish = nl->port_owner = 0;
933                 parport_release(nl->pardev);
934         }
935         return OK;
936 }
937
938 /* PLIP_ERROR --- wait till other end settled */
939 static int
940 plip_error(struct net_device *dev, struct net_local *nl,
941            struct plip_local *snd, struct plip_local *rcv)
942 {
943         unsigned char status;
944
945         status = read_status(dev);
946         if ((status & 0xf8) == 0x80) {
947                 if (net_debug > 2)
948                         printk(KERN_DEBUG "%s: reset interface.\n", dev->name);
949                 nl->connection = PLIP_CN_NONE;
950                 nl->should_relinquish = 0;
951                 netif_start_queue (dev);
952                 enable_parport_interrupts (dev);
953                 ENABLE(dev->irq);
954                 netif_wake_queue (dev);
955         } else {
956                 nl->is_deferred = 1;
957                 queue_task(&nl->deferred, &tq_timer);
958         }
959
960         return OK;
961 }
962 \f
963 /* Handle the parallel port interrupts. */
964 static void
965 plip_interrupt(int irq, void *dev_id, struct pt_regs * regs)
966 {
967         struct net_device *dev = dev_id;
968         struct net_local *nl;
969         struct plip_local *rcv;
970         unsigned char c0;
971
972         if (dev == NULL) {
973                 printk(KERN_DEBUG "plip_interrupt: irq %d for unknown device.\n", irq);
974                 return;
975         }
976
977         nl = (struct net_local *)dev->priv;
978         rcv = &nl->rcv_data;
979
980         spin_lock_irq (&nl->lock);
981
982         c0 = read_status(dev);
983         if ((c0 & 0xf8) != 0xc0) {
984                 if ((dev->irq != -1) && (net_debug > 1))
985                         printk(KERN_DEBUG "%s: spurious interrupt\n", dev->name);
986                 spin_unlock_irq (&nl->lock);
987                 return;
988         }
989
990         if (net_debug > 3)
991                 printk(KERN_DEBUG "%s: interrupt.\n", dev->name);
992
993         switch (nl->connection) {
994         case PLIP_CN_CLOSING:
995                 netif_wake_queue (dev);
996         case PLIP_CN_NONE:
997         case PLIP_CN_SEND:
998                 rcv->state = PLIP_PK_TRIGGER;
999                 nl->connection = PLIP_CN_RECEIVE;
1000                 nl->timeout_count = 0;
1001                 queue_task(&nl->immediate, &tq_immediate);
1002                 mark_bh(IMMEDIATE_BH);
1003                 break;
1004
1005         case PLIP_CN_RECEIVE:
1006                 /* May occur because there is race condition
1007                    around test and set of dev->interrupt.
1008                    Ignore this interrupt. */
1009                 break;
1010
1011         case PLIP_CN_ERROR:
1012                 printk(KERN_ERR "%s: receive interrupt in error state\n", dev->name);
1013                 break;
1014         }
1015
1016         spin_unlock_irq(&nl->lock);
1017 }
1018 \f
1019 static int
1020 plip_tx_packet(struct sk_buff *skb, struct net_device *dev)
1021 {
1022         struct net_local *nl = (struct net_local *)dev->priv;
1023         struct plip_local *snd = &nl->snd_data;
1024
1025         if (netif_queue_stopped(dev))
1026                 return 1;
1027
1028         /* We may need to grab the bus */
1029         if (!nl->port_owner) {
1030                 if (parport_claim(nl->pardev))
1031                         return 1;
1032                 nl->port_owner = 1;
1033         }
1034
1035         netif_stop_queue (dev);
1036         
1037         if (skb->len > dev->mtu + dev->hard_header_len) {
1038                 printk(KERN_WARNING "%s: packet too big, %d.\n", dev->name, (int)skb->len);
1039                 netif_start_queue (dev);
1040                 return 1;
1041         }
1042
1043         if (net_debug > 2)
1044                 printk(KERN_DEBUG "%s: send request\n", dev->name);
1045
1046         spin_lock_irq(&nl->lock);
1047         dev->trans_start = jiffies;
1048         snd->skb = skb;
1049         snd->length.h = skb->len;
1050         snd->state = PLIP_PK_TRIGGER;
1051         if (nl->connection == PLIP_CN_NONE) {
1052                 nl->connection = PLIP_CN_SEND;
1053                 nl->timeout_count = 0;
1054         }
1055         queue_task(&nl->immediate, &tq_immediate);
1056         mark_bh(IMMEDIATE_BH);
1057         spin_unlock_irq(&nl->lock);
1058         
1059         return 0;
1060 }
1061
1062 static void
1063 plip_rewrite_address(struct net_device *dev, struct ethhdr *eth)
1064 {
1065         struct in_device *in_dev;
1066
1067         if ((in_dev=dev->ip_ptr) != NULL) {
1068                 /* Any address will do - we take the first */
1069                 struct in_ifaddr *ifa=in_dev->ifa_list;
1070                 if (ifa != NULL) {
1071                         memcpy(eth->h_source, dev->dev_addr, 6);
1072                         memset(eth->h_dest, 0xfc, 2);
1073                         memcpy(eth->h_dest+2, &ifa->ifa_address, 4);
1074                 }
1075         }
1076 }
1077
1078 static int
1079 plip_hard_header(struct sk_buff *skb, struct net_device *dev,
1080                  unsigned short type, void *daddr,
1081                  void *saddr, unsigned len)
1082 {
1083         struct net_local *nl = (struct net_local *)dev->priv;
1084         int ret;
1085
1086         if ((ret = nl->orig_hard_header(skb, dev, type, daddr, saddr, len)) >= 0)
1087                 plip_rewrite_address (dev, (struct ethhdr *)skb->data);
1088
1089         return ret;
1090 }
1091
1092 int plip_hard_header_cache(struct neighbour *neigh,
1093                            struct hh_cache *hh)
1094 {
1095         struct net_local *nl = (struct net_local *)neigh->dev->priv;
1096         int ret;
1097         
1098         if ((ret = nl->orig_hard_header_cache(neigh, hh)) == 0)
1099         {
1100                 struct ethhdr *eth = (struct ethhdr*)(((u8*)hh->hh_data) + 2);
1101                 plip_rewrite_address (neigh->dev, eth);
1102         }
1103         
1104         return ret;
1105 }                          
1106
1107 /* Open/initialize the board.  This is called (in the current kernel)
1108    sometime after booting when the 'ifconfig' program is run.
1109
1110    This routine gets exclusive access to the parallel port by allocating
1111    its IRQ line.
1112  */
1113 static int
1114 plip_open(struct net_device *dev)
1115 {
1116         struct net_local *nl = (struct net_local *)dev->priv;
1117         struct in_device *in_dev;
1118
1119         /* Grab the port */
1120         if (!nl->port_owner) {
1121                 if (parport_claim(nl->pardev)) return -EAGAIN;
1122                 nl->port_owner = 1;
1123         }
1124
1125         nl->should_relinquish = 0;
1126
1127         /* Clear the data port. */
1128         write_data (dev, 0x00);
1129
1130         /* Enable rx interrupt. */
1131         enable_parport_interrupts (dev);
1132         if (dev->irq == -1)
1133         {
1134                 atomic_set (&nl->kill_timer, 0);
1135                 queue_task (&nl->timer, &tq_timer);
1136         }
1137
1138         /* Initialize the state machine. */
1139         nl->rcv_data.state = nl->snd_data.state = PLIP_PK_DONE;
1140         nl->rcv_data.skb = nl->snd_data.skb = NULL;
1141         nl->connection = PLIP_CN_NONE;
1142         nl->is_deferred = 0;
1143
1144         /* Fill in the MAC-level header.
1145            We used to abuse dev->broadcast to store the point-to-point
1146            MAC address, but we no longer do it. Instead, we fetch the
1147            interface address whenever it is needed, which is cheap enough
1148            because we use the hh_cache. Actually, abusing dev->broadcast
1149            didn't work, because when using plip_open the point-to-point
1150            address isn't yet known.
1151            PLIP doesn't have a real MAC address, but we need it to be
1152            DOS compatible, and to properly support taps (otherwise,
1153            when the device address isn't identical to the address of a
1154            received frame, the kernel incorrectly drops it).             */
1155
1156         if ((in_dev=dev->ip_ptr) != NULL) {
1157                 /* Any address will do - we take the first. We already
1158                    have the first two bytes filled with 0xfc, from
1159                    plip_init_dev(). */
1160                 struct in_ifaddr *ifa=in_dev->ifa_list;
1161                 if (ifa != NULL) {
1162                         memcpy(dev->dev_addr+2, &ifa->ifa_local, 4);
1163                 }
1164         }
1165
1166         netif_start_queue (dev);
1167
1168         return 0;
1169 }
1170
1171 /* The inverse routine to plip_open (). */
1172 static int
1173 plip_close(struct net_device *dev)
1174 {
1175         struct net_local *nl = (struct net_local *)dev->priv;
1176         struct plip_local *snd = &nl->snd_data;
1177         struct plip_local *rcv = &nl->rcv_data;
1178
1179         netif_stop_queue (dev);
1180         DISABLE(dev->irq);
1181         synchronize_irq();
1182
1183         if (dev->irq == -1)
1184         {
1185                 init_MUTEX_LOCKED (&nl->killed_timer_sem);
1186                 atomic_set (&nl->kill_timer, 1);
1187                 down (&nl->killed_timer_sem);
1188         }
1189
1190 #ifdef NOTDEF
1191         outb(0x00, PAR_DATA(dev));
1192 #endif
1193         nl->is_deferred = 0;
1194         nl->connection = PLIP_CN_NONE;
1195         if (nl->port_owner) {
1196                 parport_release(nl->pardev);
1197                 nl->port_owner = 0;
1198         }
1199
1200         snd->state = PLIP_PK_DONE;
1201         if (snd->skb) {
1202                 dev_kfree_skb(snd->skb);
1203                 snd->skb = NULL;
1204         }
1205         rcv->state = PLIP_PK_DONE;
1206         if (rcv->skb) {
1207                 kfree_skb(rcv->skb);
1208                 rcv->skb = NULL;
1209         }
1210
1211 #ifdef NOTDEF
1212         /* Reset. */
1213         outb(0x00, PAR_CONTROL(dev));
1214 #endif
1215         return 0;
1216 }
1217
1218 static int
1219 plip_preempt(void *handle)
1220 {
1221         struct net_device *dev = (struct net_device *)handle;
1222         struct net_local *nl = (struct net_local *)dev->priv;
1223
1224         /* Stand our ground if a datagram is on the wire */
1225         if (nl->connection != PLIP_CN_NONE) {
1226                 nl->should_relinquish = 1;
1227                 return 1;
1228         }
1229
1230         nl->port_owner = 0;     /* Remember that we released the bus */
1231         return 0;
1232 }
1233
1234 static void
1235 plip_wakeup(void *handle)
1236 {
1237         struct net_device *dev = (struct net_device *)handle;
1238         struct net_local *nl = (struct net_local *)dev->priv;
1239
1240         if (nl->port_owner) {
1241                 /* Why are we being woken up? */
1242                 printk(KERN_DEBUG "%s: why am I being woken up?\n", dev->name);
1243                 if (!parport_claim(nl->pardev))
1244                         /* bus_owner is already set (but why?) */
1245                         printk(KERN_DEBUG "%s: I'm broken.\n", dev->name);
1246                 else
1247                         return;
1248         }
1249         
1250         if (!(dev->flags & IFF_UP))
1251                 /* Don't need the port when the interface is down */
1252                 return;
1253
1254         if (!parport_claim(nl->pardev)) {
1255                 nl->port_owner = 1;
1256                 /* Clear the data port. */
1257                 write_data (dev, 0x00);
1258         }
1259
1260         return;
1261 }
1262
1263 static struct net_device_stats *
1264 plip_get_stats(struct net_device *dev)
1265 {
1266         struct net_local *nl = (struct net_local *)dev->priv;
1267         struct net_device_stats *r = &nl->enet_stats;
1268
1269         return r;
1270 }
1271
1272 static int
1273 plip_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1274 {
1275         struct net_local *nl = (struct net_local *) dev->priv;
1276         struct plipconf *pc = (struct plipconf *) &rq->ifr_data;
1277
1278         switch(pc->pcmd) {
1279         case PLIP_GET_TIMEOUT:
1280                 pc->trigger = nl->trigger;
1281                 pc->nibble  = nl->nibble;
1282                 break;
1283         case PLIP_SET_TIMEOUT:
1284                 if(!capable(CAP_NET_ADMIN))
1285                         return -EPERM;
1286                 nl->trigger = pc->trigger;
1287                 nl->nibble  = pc->nibble;
1288                 break;
1289         default:
1290                 return -EOPNOTSUPP;
1291         }
1292         return 0;
1293 }
1294 \f
1295 static int parport[PLIP_MAX] = { [0 ... PLIP_MAX-1] = -1 };
1296 static int timid;
1297
1298 MODULE_PARM(parport, "1-" __MODULE_STRING(PLIP_MAX) "i");
1299 MODULE_PARM(timid, "1i");
1300 MODULE_PARM_DESC(parport, "List of parport device numbers to use by plip");
1301
1302 static struct net_device *dev_plip[PLIP_MAX] = { NULL, };
1303
1304 static int inline 
1305 plip_searchfor(int list[], int a)
1306 {
1307         int i;
1308         for (i = 0; i < PLIP_MAX && list[i] != -1; i++) {
1309                 if (list[i] == a) return 1;
1310         }
1311         return 0;
1312 }
1313
1314 /* plip_attach() is called (by the parport code) when a port is
1315  * available to use. */
1316 static void plip_attach (struct parport *port)
1317 {
1318         static int i;
1319
1320         if ((parport[0] == -1 && (!timid || !port->devices)) || 
1321             plip_searchfor(parport, port->number)) {
1322                 if (i == PLIP_MAX) {
1323                         printk(KERN_ERR "plip: too many devices\n");
1324                         return;
1325                 }
1326                 dev_plip[i] = kmalloc(sizeof(struct net_device),
1327                                       GFP_KERNEL);
1328                 if (!dev_plip[i]) {
1329                         printk(KERN_ERR "plip: memory squeeze\n");
1330                         return;
1331                 }
1332                 memset(dev_plip[i], 0, sizeof(struct net_device));
1333                 sprintf(dev_plip[i]->name, "plip%d", i);
1334                 dev_plip[i]->priv = port;
1335                 if (plip_init_dev(dev_plip[i],port) ||
1336                     register_netdev(dev_plip[i])) {
1337                         kfree(dev_plip[i]);
1338                         dev_plip[i] = NULL;
1339                 } else {
1340                         i++;
1341                 }
1342         }
1343 }
1344
1345 /* plip_detach() is called (by the parport code) when a port is
1346  * no longer available to use. */
1347 static void plip_detach (struct parport *port)
1348 {
1349         /* Nothing to do */
1350 }
1351
1352 static struct parport_driver plip_driver = {
1353         name:   "plip",
1354         attach: plip_attach,
1355         detach: plip_detach
1356 };
1357
1358 static void __exit plip_cleanup_module (void)
1359 {
1360         int i;
1361
1362         parport_unregister_driver (&plip_driver);
1363
1364         for (i=0; i < PLIP_MAX; i++) {
1365                 if (dev_plip[i]) {
1366                         struct net_local *nl =
1367                                 (struct net_local *)dev_plip[i]->priv;
1368                         unregister_netdev(dev_plip[i]);
1369                         if (nl->port_owner)
1370                                 parport_release(nl->pardev);
1371                         parport_unregister_device(nl->pardev);
1372                         kfree(dev_plip[i]->priv);
1373                         kfree(dev_plip[i]);
1374                         dev_plip[i] = NULL;
1375                 }
1376         }
1377 }
1378
1379 #ifndef MODULE
1380
1381 static int parport_ptr;
1382
1383 static int __init plip_setup(char *str)
1384 {
1385         int ints[4];
1386
1387         str = get_options(str, ARRAY_SIZE(ints), ints);
1388
1389         /* Ugh. */
1390         if (!strncmp(str, "parport", 7)) {
1391                 int n = simple_strtoul(str+7, NULL, 10);
1392                 if (parport_ptr < PLIP_MAX)
1393                         parport[parport_ptr++] = n;
1394                 else
1395                         printk(KERN_INFO "plip: too many ports, %s ignored.\n",
1396                                str);
1397         } else if (!strcmp(str, "timid")) {
1398                 timid = 1;
1399         } else {
1400                 if (ints[0] == 0 || ints[1] == 0) {
1401                         /* disable driver on "plip=" or "plip=0" */
1402                         parport[0] = -2;
1403                 } else {
1404                         printk(KERN_WARNING "warning: 'plip=0x%x' ignored\n", 
1405                                ints[1]);
1406                 }
1407         }
1408         return 1;
1409 }
1410
1411 __setup("plip=", plip_setup);
1412
1413 #endif /* !MODULE */
1414
1415 static int __init plip_init (void)
1416 {
1417         if (parport[0] == -2)
1418                 return 0;
1419
1420         if (parport[0] != -1 && timid) {
1421                 printk(KERN_WARNING "plip: warning, ignoring `timid' since specific ports given.\n");
1422                 timid = 0;
1423         }
1424
1425         if (parport_register_driver (&plip_driver)) {
1426                 printk (KERN_WARNING "plip: couldn't register driver\n");
1427                 return 1;
1428         }
1429
1430         return 0;
1431 }
1432
1433 module_init(plip_init);
1434 module_exit(plip_cleanup_module);
1435 MODULE_LICENSE("GPL");
1436
1437 /*
1438  * Local variables:
1439  * compile-command: "gcc -DMODULE -DMODVERSIONS -D__KERNEL__ -Wall -Wstrict-prototypes -O2 -g -fomit-frame-pointer -pipe -c plip.c"
1440  * End:
1441  */