Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

plip.c

Go to the documentation of this file.
00001 /* $Id: plip.c,v 1.3.6.2 1997/04/16 15:07:56 phil Exp $ */
00002 /* PLIP: A parallel port "network" driver for Linux. */
00003 /* This driver is for parallel port with 5-bit cable (LapLink (R) cable). */
00004 /*
00005  * Authors:     Donald Becker,  <becker@super.org>
00006  *              Tommy Thorn, <thorn@daimi.aau.dk>
00007  *              Tanabe Hiroyasu, <hiro@sanpo.t.u-tokyo.ac.jp>
00008  *              Alan Cox, <gw4pts@gw4pts.ampr.org>
00009  *              Peter Bauer, <100136.3530@compuserve.com>
00010  *              Niibe Yutaka, <gniibe@mri.co.jp>
00011  *
00012  *              Modularization and ifreq/ifmap support by Alan Cox.
00013  *              Rewritten by Niibe Yutaka.
00014  *              parport-sharing awareness code by Philip Blundell.
00015  *              SMP locking by Niibe Yutaka.
00016  *
00017  * Fixes:
00018  *              Niibe Yutaka
00019  *                - Module initialization.
00020  *                - MTU fix.
00021  *                - Make sure other end is OK, before sending a packet.
00022  *                - Fix immediate timer problem.
00023  *
00024  *              Al Viro
00025  *                - Changed {enable,disable}_irq handling to make it work
00026  *                  with new ("stack") semantics.
00027  *
00028  *              This program is free software; you can redistribute it and/or
00029  *              modify it under the terms of the GNU General Public License
00030  *              as published by the Free Software Foundation; either version
00031  *              2 of the License, or (at your option) any later version.
00032  */
00033 
00034 /*
00035  * Original version and the name 'PLIP' from Donald Becker <becker@super.org>
00036  * inspired by Russ Nelson's parallel port packet driver.
00037  *
00038  * NOTE:
00039  *     Tanabe Hiroyasu had changed the protocol, and it was in Linux v1.0.
00040  *     Because of the necessity to communicate to DOS machines with the
00041  *     Crynwr packet driver, Peter Bauer changed the protocol again
00042  *     back to original protocol.
00043  *
00044  *     This version follows original PLIP protocol.
00045  *     So, this PLIP can't communicate the PLIP of Linux v1.0.
00046  */
00047 
00048 /*
00049  *     To use with DOS box, please do (Turn on ARP switch):
00050  *      # ifconfig plip[0-2] arp
00051  */
00052 static const char *version = "NET3 PLIP version 2.3-parport gniibe@mri.co.jp\n";
00053 
00054 /*
00055   Sources:
00056         Ideas and protocols came from Russ Nelson's <nelson@crynwr.com>
00057         "parallel.asm" parallel port packet driver.
00058 
00059   The "Crynwr" parallel port standard specifies the following protocol:
00060     Trigger by sending '0x08' (this cause interrupt on other end)
00061     count-low octet
00062     count-high octet
00063     ... data octets
00064     checksum octet
00065   Each octet is sent as <wait for rx. '0x1?'> <send 0x10+(octet&0x0F)>
00066                         <wait for rx. '0x0?'> <send 0x00+((octet>>4)&0x0F)>
00067 
00068   The packet is encapsulated as if it were ethernet.
00069 
00070   The cable used is a de facto standard parallel null cable -- sold as
00071   a "LapLink" cable by various places.  You'll need a 12-conductor cable to
00072   make one yourself.  The wiring is:
00073     SLCTIN      17 - 17
00074     GROUND      25 - 25
00075     D0->ERROR   2 - 15          15 - 2
00076     D1->SLCT    3 - 13          13 - 3
00077     D2->PAPOUT  4 - 12          12 - 4
00078     D3->ACK     5 - 10          10 - 5
00079     D4->BUSY    6 - 11          11 - 6
00080   Do not connect the other pins.  They are
00081     D5,D6,D7 are 7,8,9
00082     STROBE is 1, FEED is 14, INIT is 16
00083     extra grounds are 18,19,20,21,22,23,24
00084 */
00085 
00086 #include <linux/module.h>
00087 #include <linux/kernel.h>
00088 #include <linux/sched.h>
00089 #include <linux/types.h>
00090 #include <linux/fcntl.h>
00091 #include <linux/interrupt.h>
00092 #include <linux/string.h>
00093 #include <linux/ptrace.h>
00094 #include <linux/if_ether.h>
00095 #include <asm/system.h>
00096 #include <asm/io.h>
00097 #include <linux/in.h>
00098 #include <linux/errno.h>
00099 #include <linux/delay.h>
00100 #include <linux/lp.h>
00101 #include <linux/init.h>
00102 
00103 #include <linux/netdevice.h>
00104 #include <linux/etherdevice.h>
00105 #include <linux/inetdevice.h>
00106 #include <linux/skbuff.h>
00107 #include <linux/if_plip.h>
00108 
00109 #include <linux/tqueue.h>
00110 #include <linux/ioport.h>
00111 #include <asm/bitops.h>
00112 #include <asm/irq.h>
00113 #include <asm/byteorder.h>
00114 #include <asm/spinlock.h>
00115 
00116 #include <linux/parport.h>
00117 
00118 /* Maximum number of devices to support. */
00119 #define PLIP_MAX  8
00120 
00121 /* Use 0 for production, 1 for verification, >2 for debug */
00122 #ifndef NET_DEBUG
00123 #define NET_DEBUG 1
00124 #endif
00125 static unsigned int net_debug = NET_DEBUG;
00126 
00127 #define ENABLE(irq) enable_irq(irq)
00128 #define DISABLE(irq) disable_irq(irq)
00129 
00130 /* In micro second */
00131 #define PLIP_DELAY_UNIT            1
00132 
00133 /* Connection time out = PLIP_TRIGGER_WAIT * PLIP_DELAY_UNIT usec */
00134 #define PLIP_TRIGGER_WAIT        500
00135 
00136 /* Nibble time out = PLIP_NIBBLE_WAIT * PLIP_DELAY_UNIT usec */
00137 #define PLIP_NIBBLE_WAIT        3000
00138 
00139 #define PAR_INTR_ON             (LP_PINITP|LP_PSELECP|LP_PINTEN)
00140 #define PAR_INTR_OFF            (LP_PINITP|LP_PSELECP)
00141 #define PAR_DATA(dev)           ((dev)->base_addr+0)
00142 #define PAR_STATUS(dev)         ((dev)->base_addr+1)
00143 #define PAR_CONTROL(dev)        ((dev)->base_addr+2)
00144 
00145 /* Bottom halfs */
00146 static void plip_kick_bh(struct device *dev);
00147 static void plip_bh(struct device *dev);
00148 
00149 /* Interrupt handler */
00150 static void plip_interrupt(int irq, void *dev_id, struct pt_regs *regs);
00151 
00152 /* Functions for DEV methods */
00153 static int plip_rebuild_header(struct sk_buff *skb);
00154 static int plip_tx_packet(struct sk_buff *skb, struct device *dev);
00155 static int plip_open(struct device *dev);
00156 static int plip_close(struct device *dev);
00157 static struct net_device_stats *plip_get_stats(struct device *dev);
00158 static int plip_config(struct device *dev, struct ifmap *map);
00159 static int plip_ioctl(struct device *dev, struct ifreq *ifr, int cmd);
00160 static int plip_preempt(void *handle);
00161 static void plip_wakeup(void *handle);
00162 
00163 enum plip_connection_state {
00164         PLIP_CN_NONE=0,
00165         PLIP_CN_RECEIVE,
00166         PLIP_CN_SEND,
00167         PLIP_CN_CLOSING,
00168         PLIP_CN_ERROR
00169 };
00170 
00171 enum plip_packet_state {
00172         PLIP_PK_DONE=0,
00173         PLIP_PK_TRIGGER,
00174         PLIP_PK_LENGTH_LSB,
00175         PLIP_PK_LENGTH_MSB,
00176         PLIP_PK_DATA,
00177         PLIP_PK_CHECKSUM
00178 };
00179 
00180 enum plip_nibble_state {
00181         PLIP_NB_BEGIN,
00182         PLIP_NB_1,
00183         PLIP_NB_2,
00184 };
00185 
00186 struct plip_local {
00187         enum plip_packet_state state;
00188         enum plip_nibble_state nibble;
00189         union {
00190                 struct {
00191 #if defined(__LITTLE_ENDIAN)
00192                         unsigned char lsb;
00193                         unsigned char msb;
00194 #elif defined(__BIG_ENDIAN)
00195                         unsigned char msb;
00196                         unsigned char lsb;
00197 #else
00198 #error  "Please fix the endianness defines in <asm/byteorder.h>"
00199 #endif
00200                 } b;
00201                 unsigned short h;
00202         } length;
00203         unsigned short byte;
00204         unsigned char  checksum;
00205         unsigned char  data;
00206         struct sk_buff *skb;
00207 };
00208 
00209 struct net_local {
00210         struct net_device_stats enet_stats;
00211         struct tq_struct immediate;
00212         struct tq_struct deferred;
00213         struct plip_local snd_data;
00214         struct plip_local rcv_data;
00215         struct pardevice *pardev;
00216         unsigned long  trigger;
00217         unsigned long  nibble;
00218         enum plip_connection_state connection;
00219         unsigned short timeout_count;
00220         int is_deferred;
00221         int port_owner;
00222         int should_relinquish;
00223         int (*orig_rebuild_header)(struct sk_buff *skb);
00224         spinlock_t lock;
00225 };
00226 
00227 /* Entry point of PLIP driver.
00228    Probe the hardware, and register/initialize the driver.
00229 
00230    PLIP is rather weird, because of the way it interacts with the parport
00231    system.  It is _not_ initialised from Space.c.  Instead, plip_init()
00232    is called, and that function makes up a "struct device" for each port, and
00233    then calls us here.
00234 
00235    */
00236 __initfunc(int
00237 plip_init_dev(struct device *dev, struct parport *pb))
00238 {
00239         struct net_local *nl;
00240         struct pardevice *pardev;
00241 
00242         dev->irq = pb->irq;
00243         dev->base_addr = pb->base;
00244 
00245         if (pb->irq == -1) {
00246                 printk(KERN_INFO "plip: %s has no IRQ.\n", pb->name);
00247                 return -ENODEV;
00248         }
00249 
00250         pardev = parport_register_device(pb, dev->name, plip_preempt,
00251                                          plip_wakeup, plip_interrupt, 
00252                                          0, dev);
00253 
00254         if (!pardev)
00255                 return -ENODEV;
00256 
00257         printk(KERN_INFO "%s", version);
00258         printk(KERN_INFO "%s: Parallel port at %#3lx, using IRQ %d\n", dev->name,
00259                dev->base_addr, dev->irq);
00260 
00261         /* Fill in the generic fields of the device structure. */
00262         ether_setup(dev);
00263 
00264         /* Then, override parts of it */
00265         dev->hard_start_xmit    = plip_tx_packet;
00266         dev->open               = plip_open;
00267         dev->stop               = plip_close;
00268         dev->get_stats          = plip_get_stats;
00269         dev->set_config         = plip_config;
00270         dev->do_ioctl           = plip_ioctl;
00271         dev->tx_queue_len       = 10;
00272         dev->flags              = IFF_POINTOPOINT|IFF_NOARP;
00273         memset(dev->dev_addr, 0xfc, ETH_ALEN);
00274 
00275         /* Set the private structure */
00276         dev->priv = kmalloc(sizeof (struct net_local), GFP_KERNEL);
00277         if (dev->priv == NULL) {
00278                 printk(KERN_ERR "%s: out of memory\n", dev->name);
00279                 parport_unregister_device(pardev);
00280                 return -ENOMEM;
00281         }
00282         memset(dev->priv, 0, sizeof(struct net_local));
00283         nl = (struct net_local *) dev->priv;
00284 
00285         nl->orig_rebuild_header = dev->rebuild_header;
00286         dev->rebuild_header     = plip_rebuild_header;
00287         nl->pardev = pardev; 
00288 
00289         nl->port_owner = 0;
00290 
00291         /* Initialize constants */
00292         nl->trigger     = PLIP_TRIGGER_WAIT;
00293         nl->nibble      = PLIP_NIBBLE_WAIT;
00294 
00295         /* Initialize task queue structures */
00296         nl->immediate.next = NULL;
00297         nl->immediate.sync = 0;
00298         nl->immediate.routine = (void *)(void *)plip_bh;
00299         nl->immediate.data = dev;
00300 
00301         nl->deferred.next = NULL;
00302         nl->deferred.sync = 0;
00303         nl->deferred.routine = (void *)(void *)plip_kick_bh;
00304         nl->deferred.data = dev;
00305         spin_lock_init(&nl->lock);
00306 
00307         return 0;
00308 }
00309 
00310 /* Bottom half handler for the delayed request.
00311    This routine is kicked by do_timer().
00312    Request `plip_bh' to be invoked. */
00313 static void
00314 plip_kick_bh(struct device *dev)
00315 {
00316         struct net_local *nl = (struct net_local *)dev->priv;
00317 
00318         if (nl->is_deferred) {
00319                 queue_task(&nl->immediate, &tq_immediate);
00320                 mark_bh(IMMEDIATE_BH);
00321         }
00322 }
00323 
00324 /* Forward declarations of internal routines */
00325 static int plip_none(struct device *, struct net_local *,
00326                      struct plip_local *, struct plip_local *);
00327 static int plip_receive_packet(struct device *, struct net_local *,
00328                                struct plip_local *, struct plip_local *);
00329 static int plip_send_packet(struct device *, struct net_local *,
00330                             struct plip_local *, struct plip_local *);
00331 static int plip_connection_close(struct device *, struct net_local *,
00332                                  struct plip_local *, struct plip_local *);
00333 static int plip_error(struct device *, struct net_local *,
00334                       struct plip_local *, struct plip_local *);
00335 static int plip_bh_timeout_error(struct device *dev, struct net_local *nl,
00336                                  struct plip_local *snd,
00337                                  struct plip_local *rcv,
00338                                  int error);
00339 
00340 #define OK        0
00341 #define TIMEOUT   1
00342 #define ERROR     2
00343 #define HS_TIMEOUT      3
00344 
00345 typedef int (*plip_func)(struct device *dev, struct net_local *nl,
00346                          struct plip_local *snd, struct plip_local *rcv);
00347 
00348 static plip_func connection_state_table[] =
00349 {
00350         plip_none,
00351         plip_receive_packet,
00352         plip_send_packet,
00353         plip_connection_close,
00354         plip_error
00355 };
00356 
00357 /* Bottom half handler of PLIP. */
00358 static void
00359 plip_bh(struct device *dev)
00360 {
00361         struct net_local *nl = (struct net_local *)dev->priv;
00362         struct plip_local *snd = &nl->snd_data;
00363         struct plip_local *rcv = &nl->rcv_data;
00364         plip_func f;
00365         int r;
00366 
00367         nl->is_deferred = 0;
00368         f = connection_state_table[nl->connection];
00369         if ((r = (*f)(dev, nl, snd, rcv)) != OK
00370             && (r = plip_bh_timeout_error(dev, nl, snd, rcv, r)) != OK) {
00371                 nl->is_deferred = 1;
00372                 queue_task(&nl->deferred, &tq_timer);
00373         }
00374 }
00375 
00376 static int
00377 plip_bh_timeout_error(struct device *dev, struct net_local *nl,
00378                       struct plip_local *snd, struct plip_local *rcv,
00379                       int error)
00380 {
00381         unsigned char c0;
00382         /*
00383          * This is tricky. If we got here from the beginning of send (either
00384          * with ERROR or HS_TIMEOUT) we have IRQ enabled. Otherwise it's
00385          * already disabled. With the old variant of {enable,disable}_irq()
00386          * extra disable_irq() was a no-op. Now it became mortal - it's
00387          * unbalanced and thus we'll never re-enable IRQ (until rmmod plip,
00388          * that is). So we have to treat HS_TIMEOUT and ERROR from send
00389          * in a special way.
00390          */
00391 
00392         spin_lock_irq(&nl->lock);
00393         if (nl->connection == PLIP_CN_SEND) {
00394 
00395                 if (error != ERROR) { /* Timeout */
00396                         nl->timeout_count++;
00397                         if ((error == HS_TIMEOUT
00398                              && nl->timeout_count <= 10)
00399                             || nl->timeout_count <= 3) {
00400                                 spin_unlock_irq(&nl->lock);
00401                                 /* Try again later */
00402                                 return TIMEOUT;
00403                         }
00404                         c0 = inb(PAR_STATUS(dev));
00405                         printk(KERN_WARNING "%s: transmit timeout(%d,%02x)\n",
00406                                dev->name, snd->state, c0);
00407                 } else
00408                         error = HS_TIMEOUT;
00409                 nl->enet_stats.tx_errors++;
00410                 nl->enet_stats.tx_aborted_errors++;
00411         } else if (nl->connection == PLIP_CN_RECEIVE) {
00412                 if (rcv->state == PLIP_PK_TRIGGER) {
00413                         /* Transmission was interrupted. */
00414                         spin_unlock_irq(&nl->lock);
00415                         return OK;
00416                 }
00417                 if (error != ERROR) { /* Timeout */
00418                         if (++nl->timeout_count <= 3) {
00419                                 spin_unlock_irq(&nl->lock);
00420                                 /* Try again later */
00421                                 return TIMEOUT;
00422                         }
00423                         c0 = inb(PAR_STATUS(dev));
00424                         printk(KERN_WARNING "%s: receive timeout(%d,%02x)\n",
00425                                dev->name, rcv->state, c0);
00426                 }
00427                 nl->enet_stats.rx_dropped++;
00428         }
00429         rcv->state = PLIP_PK_DONE;
00430         if (rcv->skb) {
00431                 kfree_skb(rcv->skb);
00432                 rcv->skb = NULL;
00433         }
00434         snd->state = PLIP_PK_DONE;
00435         if (snd->skb) {
00436                 dev_kfree_skb(snd->skb);
00437                 snd->skb = NULL;
00438         }
00439         spin_unlock_irq(&nl->lock);
00440         if (error == HS_TIMEOUT) {
00441                 DISABLE(dev->irq);
00442                 synchronize_irq();
00443         }
00444         outb(PAR_INTR_OFF, PAR_CONTROL(dev));
00445         dev->tbusy = 1;
00446         nl->connection = PLIP_CN_ERROR;
00447         outb(0x00, PAR_DATA(dev));
00448 
00449         return TIMEOUT;
00450 }
00451 
00452 static int
00453 plip_none(struct device *dev, struct net_local *nl,
00454           struct plip_local *snd, struct plip_local *rcv)
00455 {
00456         return OK;
00457 }
00458 
00459 /* PLIP_RECEIVE --- receive a byte(two nibbles)
00460    Returns OK on success, TIMEOUT on timeout */
00461 inline static int
00462 plip_receive(unsigned short nibble_timeout, unsigned short status_addr,
00463              enum plip_nibble_state *ns_p, unsigned char *data_p)
00464 {
00465         unsigned char c0, c1;
00466         unsigned int cx;
00467 
00468         switch (*ns_p) {
00469         case PLIP_NB_BEGIN:
00470                 cx = nibble_timeout;
00471                 while (1) {
00472                         c0 = inb(status_addr);
00473                         udelay(PLIP_DELAY_UNIT);
00474                         if ((c0 & 0x80) == 0) {
00475                                 c1 = inb(status_addr);
00476                                 if (c0 == c1)
00477                                         break;
00478                         }
00479                         if (--cx == 0)
00480                                 return TIMEOUT;
00481                 }
00482                 *data_p = (c0 >> 3) & 0x0f;
00483                 outb(0x10, --status_addr); /* send ACK */
00484                 status_addr++;
00485                 *ns_p = PLIP_NB_1;
00486 
00487         case PLIP_NB_1:
00488                 cx = nibble_timeout;
00489                 while (1) {
00490                         c0 = inb(status_addr);
00491                         udelay(PLIP_DELAY_UNIT);
00492                         if (c0 & 0x80) {
00493                                 c1 = inb(status_addr);
00494                                 if (c0 == c1)
00495                                         break;
00496                         }
00497                         if (--cx == 0)
00498                                 return TIMEOUT;
00499                 }
00500                 *data_p |= (c0 << 1) & 0xf0;
00501                 outb(0x00, --status_addr); /* send ACK */
00502                 status_addr++;
00503                 *ns_p = PLIP_NB_BEGIN;
00504         case PLIP_NB_2:
00505                 break;
00506         }
00507         return OK;
00508 }
00509 
00510 /*
00511  *      Determine the packet's protocol ID. The rule here is that we 
00512  *      assume 802.3 if the type field is short enough to be a length.
00513  *      This is normal practice and works for any 'now in use' protocol.
00514  *
00515  *      PLIP is ethernet ish but the daddr might not be valid if unicast.
00516  *      PLIP fortunately has no bus architecture (its Point-to-point).
00517  *
00518  *      We can't fix the daddr thing as that quirk (more bug) is embedded
00519  *      in far too many old systems not all even running Linux.
00520  */
00521  
00522 static unsigned short plip_type_trans(struct sk_buff *skb, struct device *dev)
00523 {
00524         struct ethhdr *eth;
00525         unsigned char *rawp;
00526         
00527         skb->mac.raw=skb->data;
00528         skb_pull(skb,dev->hard_header_len);
00529         eth= skb->mac.ethernet;
00530         
00531         if(*eth->h_dest&1)
00532         {
00533                 if(memcmp(eth->h_dest,dev->broadcast, ETH_ALEN)==0)
00534                         skb->pkt_type=PACKET_BROADCAST;
00535                 else
00536                         skb->pkt_type=PACKET_MULTICAST;
00537         }
00538         
00539         /*
00540          *      This ALLMULTI check should be redundant by 1.4
00541          *      so don't forget to remove it.
00542          */
00543          
00544         if (ntohs(eth->h_proto) >= 1536)
00545                 return eth->h_proto;
00546                 
00547         rawp = skb->data;
00548         
00549         /*
00550          *      This is a magic hack to spot IPX packets. Older Novell breaks
00551          *      the protocol design and runs IPX over 802.3 without an 802.2 LLC
00552          *      layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
00553          *      won't work for fault tolerant netware but does for the rest.
00554          */
00555         if (*(unsigned short *)rawp == 0xFFFF)
00556                 return htons(ETH_P_802_3);
00557                 
00558         /*
00559          *      Real 802.2 LLC
00560          */
00561         return htons(ETH_P_802_2);
00562 }
00563 
00564 
00565 /* PLIP_RECEIVE_PACKET --- receive a packet */
00566 static int
00567 plip_receive_packet(struct device *dev, struct net_local *nl,
00568                     struct plip_local *snd, struct plip_local *rcv)
00569 {
00570         unsigned short status_addr = PAR_STATUS(dev);
00571         unsigned short nibble_timeout = nl->nibble;
00572         unsigned char *lbuf;
00573 
00574         switch (rcv->state) {
00575         case PLIP_PK_TRIGGER:
00576                 DISABLE(dev->irq);
00577                 /* Don't need to synchronize irq, as we can safely ignore it */
00578                 outb(PAR_INTR_OFF, PAR_CONTROL(dev));
00579                 dev->interrupt = 0;
00580                 outb(0x01, PAR_DATA(dev)); /* send ACK */
00581                 if (net_debug > 2)
00582                         printk(KERN_DEBUG "%s: receive start\n", dev->name);
00583                 rcv->state = PLIP_PK_LENGTH_LSB;
00584                 rcv->nibble = PLIP_NB_BEGIN;
00585 
00586         case PLIP_PK_LENGTH_LSB:
00587                 if (snd->state != PLIP_PK_DONE) {
00588                         if (plip_receive(nl->trigger, status_addr,
00589                                          &rcv->nibble, &rcv->length.b.lsb)) {
00590                                 /* collision, here dev->tbusy == 1 */
00591                                 rcv->state = PLIP_PK_DONE;
00592                                 nl->is_deferred = 1;
00593                                 nl->connection = PLIP_CN_SEND;
00594                                 queue_task(&nl->deferred, &tq_timer);
00595                                 outb(PAR_INTR_ON, PAR_CONTROL(dev));
00596                                 ENABLE(dev->irq);
00597                                 return OK;
00598                         }
00599                 } else {
00600                         if (plip_receive(nibble_timeout, status_addr,
00601                                          &rcv->nibble, &rcv->length.b.lsb))
00602                                 return TIMEOUT;
00603                 }
00604                 rcv->state = PLIP_PK_LENGTH_MSB;
00605 
00606         case PLIP_PK_LENGTH_MSB:
00607                 if (plip_receive(nibble_timeout, status_addr,
00608                                  &rcv->nibble, &rcv->length.b.msb))
00609                         return TIMEOUT;
00610                 if (rcv->length.h > dev->mtu + dev->hard_header_len
00611                     || rcv->length.h < 8) {
00612                         printk(KERN_WARNING "%s: bogus packet size %d.\n", dev->name, rcv->length.h);
00613                         return ERROR;
00614                 }
00615                 /* Malloc up new buffer. */
00616                 rcv->skb = dev_alloc_skb(rcv->length.h);
00617                 if (rcv->skb == NULL) {
00618                         printk(KERN_ERR "%s: Memory squeeze.\n", dev->name);
00619                         return ERROR;
00620                 }
00621                 skb_put(rcv->skb,rcv->length.h);
00622                 rcv->skb->dev = dev;
00623                 rcv->state = PLIP_PK_DATA;
00624                 rcv->byte = 0;
00625                 rcv->checksum = 0;
00626 
00627         case PLIP_PK_DATA:
00628                 lbuf = rcv->skb->data;
00629                 do
00630                         if (plip_receive(nibble_timeout, status_addr,
00631                                          &rcv->nibble, &lbuf[rcv->byte]))
00632                                 return TIMEOUT;
00633                 while (++rcv->byte < rcv->length.h);
00634                 do
00635                         rcv->checksum += lbuf[--rcv->byte];
00636                 while (rcv->byte);
00637                 rcv->state = PLIP_PK_CHECKSUM;
00638 
00639         case PLIP_PK_CHECKSUM:
00640                 if (plip_receive(nibble_timeout, status_addr,
00641                                  &rcv->nibble, &rcv->data))
00642                         return TIMEOUT;
00643                 if (rcv->data != rcv->checksum) {
00644                         nl->enet_stats.rx_crc_errors++;
00645                         if (net_debug)
00646                                 printk(KERN_DEBUG "%s: checksum error\n", dev->name);
00647                         return ERROR;
00648                 }
00649                 rcv->state = PLIP_PK_DONE;
00650 
00651         case PLIP_PK_DONE:
00652                 /* Inform the upper layer for the arrival of a packet. */
00653                 rcv->skb->protocol=plip_type_trans(rcv->skb, dev);
00654                 netif_rx(rcv->skb);
00655                 nl->enet_stats.rx_bytes += rcv->length.h;
00656                 nl->enet_stats.rx_packets++;
00657                 rcv->skb = NULL;
00658                 if (net_debug > 2)
00659                         printk(KERN_DEBUG "%s: receive end\n", dev->name);
00660 
00661                 /* Close the connection. */
00662                 outb (0x00, PAR_DATA(dev));
00663                 spin_lock_irq(&nl->lock);
00664                 if (snd->state != PLIP_PK_DONE) {
00665                         nl->connection = PLIP_CN_SEND;
00666                         spin_unlock_irq(&nl->lock);
00667                         queue_task(&nl->immediate, &tq_immediate);
00668                         mark_bh(IMMEDIATE_BH);
00669                         outb(PAR_INTR_ON, PAR_CONTROL(dev));
00670                         ENABLE(dev->irq);
00671                         return OK;
00672                 } else {
00673                         nl->connection = PLIP_CN_NONE;
00674                         spin_unlock_irq(&nl->lock);
00675                         outb(PAR_INTR_ON, PAR_CONTROL(dev));
00676                         ENABLE(dev->irq);
00677                         return OK;
00678                 }
00679         }
00680         return OK;
00681 }
00682 
00683 /* PLIP_SEND --- send a byte (two nibbles)
00684    Returns OK on success, TIMEOUT when timeout    */
00685 inline static int
00686 plip_send(unsigned short nibble_timeout, unsigned short data_addr,
00687           enum plip_nibble_state *ns_p, unsigned char data)
00688 {
00689         unsigned char c0;
00690         unsigned int cx;
00691 
00692         switch (*ns_p) {
00693         case PLIP_NB_BEGIN:
00694                 outb((data & 0x0f), data_addr);
00695                 *ns_p = PLIP_NB_1;
00696 
00697         case PLIP_NB_1:
00698                 outb(0x10 | (data & 0x0f), data_addr);
00699                 cx = nibble_timeout;
00700                 data_addr++;
00701                 while (1) {
00702                         c0 = inb(data_addr);
00703                         if ((c0 & 0x80) == 0)
00704                                 break;
00705                         if (--cx == 0)
00706                                 return TIMEOUT;
00707                         udelay(PLIP_DELAY_UNIT);
00708                 }
00709                 outb(0x10 | (data >> 4), --data_addr);
00710                 *ns_p = PLIP_NB_2;
00711 
00712         case PLIP_NB_2:
00713                 outb((data >> 4), data_addr);
00714                 data_addr++;
00715                 cx = nibble_timeout;
00716                 while (1) {
00717                         c0 = inb(data_addr);
00718                         if (c0 & 0x80)
00719                                 break;
00720                         if (--cx == 0)
00721                                 return TIMEOUT;
00722                         udelay(PLIP_DELAY_UNIT);
00723                 }
00724                 data_addr--;
00725                 *ns_p = PLIP_NB_BEGIN;
00726                 return OK;
00727         }
00728         return OK;
00729 }
00730 
00731 /* PLIP_SEND_PACKET --- send a packet */
00732 static int
00733 plip_send_packet(struct device *dev, struct net_local *nl,
00734                  struct plip_local *snd, struct plip_local *rcv)
00735 {
00736         unsigned short data_addr = PAR_DATA(dev);
00737         unsigned short nibble_timeout = nl->nibble;
00738         unsigned char *lbuf;
00739         unsigned char c0;
00740         unsigned int cx;
00741 
00742         if (snd->skb == NULL || (lbuf = snd->skb->data) == NULL) {
00743                 printk(KERN_DEBUG "%s: send skb lost\n", dev->name);
00744                 snd->state = PLIP_PK_DONE;
00745                 snd->skb = NULL;
00746                 return ERROR;
00747         }
00748 
00749         switch (snd->state) {
00750         case PLIP_PK_TRIGGER:
00751                 if ((inb(PAR_STATUS(dev)) & 0xf8) != 0x80)
00752                         return HS_TIMEOUT;
00753 
00754                 /* Trigger remote rx interrupt. */
00755                 outb(0x08, data_addr);
00756                 cx = nl->trigger;
00757                 while (1) {
00758                         udelay(PLIP_DELAY_UNIT);
00759                         spin_lock_irq(&nl->lock);
00760                         if (nl->connection == PLIP_CN_RECEIVE) {
00761                                 spin_unlock_irq(&nl->lock);
00762                                 /* Interrupted. */
00763                                 nl->enet_stats.collisions++;
00764                                 return OK;
00765                         }
00766                         c0 = inb(PAR_STATUS(dev));
00767                         if (c0 & 0x08) {
00768                                 spin_unlock_irq(&nl->lock);
00769                                 DISABLE(dev->irq);
00770                                 synchronize_irq();
00771                                 if (nl->connection == PLIP_CN_RECEIVE) {
00772                                         /* Interrupted.
00773                                            We don't need to enable irq,
00774                                            as it is soon disabled.    */
00775                                         /* Yes, we do. New variant of
00776                                            {enable,disable}_irq *counts*
00777                                            them.  -- AV  */
00778                                         ENABLE(dev->irq);
00779                                         nl->enet_stats.collisions++;
00780                                         return OK;
00781                                 }
00782                                 outb(PAR_INTR_OFF, PAR_CONTROL(dev));
00783                                 if (net_debug > 2)
00784                                         printk(KERN_DEBUG "%s: send start\n", dev->name);
00785                                 snd->state = PLIP_PK_LENGTH_LSB;
00786                                 snd->nibble = PLIP_NB_BEGIN;
00787                                 nl->timeout_count = 0;
00788                                 break;
00789                         }
00790                         spin_unlock_irq(&nl->lock);
00791                         if (--cx == 0) {
00792                                 outb(0x00, data_addr);
00793                                 return HS_TIMEOUT;
00794                         }
00795                 }
00796 
00797         case PLIP_PK_LENGTH_LSB:
00798                 if (plip_send(nibble_timeout, data_addr,
00799                               &snd->nibble, snd->length.b.lsb))
00800                         return TIMEOUT;
00801                 snd->state = PLIP_PK_LENGTH_MSB;
00802 
00803         case PLIP_PK_LENGTH_MSB:
00804                 if (plip_send(nibble_timeout, data_addr,
00805                               &snd->nibble, snd->length.b.msb))
00806                         return TIMEOUT;
00807                 snd->state = PLIP_PK_DATA;
00808                 snd->byte = 0;
00809                 snd->checksum = 0;
00810 
00811         case PLIP_PK_DATA:
00812                 do
00813                         if (plip_send(nibble_timeout, data_addr,
00814                                       &snd->nibble, lbuf[snd->byte]))
00815                                 return TIMEOUT;
00816                 while (++snd->byte < snd->length.h);
00817                 do
00818                         snd->checksum += lbuf[--snd->byte];
00819                 while (snd->byte);
00820                 snd->state = PLIP_PK_CHECKSUM;
00821 
00822         case PLIP_PK_CHECKSUM:
00823                 if (plip_send(nibble_timeout, data_addr,
00824                               &snd->nibble, snd->checksum))
00825                         return TIMEOUT;
00826 
00827                 nl->enet_stats.tx_bytes += snd->skb->len;
00828                 dev_kfree_skb(snd->skb);
00829                 nl->enet_stats.tx_packets++;
00830                 snd->state = PLIP_PK_DONE;
00831 
00832         case PLIP_PK_DONE:
00833                 /* Close the connection */
00834                 outb (0x00, data_addr);
00835                 snd->skb = NULL;
00836                 if (net_debug > 2)
00837                         printk(KERN_DEBUG "%s: send end\n", dev->name);
00838                 nl->connection = PLIP_CN_CLOSING;
00839                 nl->is_deferred = 1;
00840                 queue_task(&nl->deferred, &tq_timer);
00841                 outb(PAR_INTR_ON, PAR_CONTROL(dev));
00842                 ENABLE(dev->irq);
00843                 return OK;
00844         }
00845         return OK;
00846 }
00847 
00848 static int
00849 plip_connection_close(struct device *dev, struct net_local *nl,
00850                       struct plip_local *snd, struct plip_local *rcv)
00851 {
00852         spin_lock_irq(&nl->lock);
00853         if (nl->connection == PLIP_CN_CLOSING) {
00854                 nl->connection = PLIP_CN_NONE;
00855                 dev->tbusy = 0;
00856                 mark_bh(NET_BH);
00857         }
00858         spin_unlock_irq(&nl->lock);
00859         if (nl->should_relinquish) {
00860                 nl->should_relinquish = nl->port_owner = 0;
00861                 parport_release(nl->pardev);
00862         }
00863         return OK;
00864 }
00865 
00866 /* PLIP_ERROR --- wait till other end settled */
00867 static int
00868 plip_error(struct device *dev, struct net_local *nl,
00869            struct plip_local *snd, struct plip_local *rcv)
00870 {
00871         unsigned char status;
00872 
00873         status = inb(PAR_STATUS(dev));
00874         if ((status & 0xf8) == 0x80) {
00875                 if (net_debug > 2)
00876                         printk(KERN_DEBUG "%s: reset interface.\n", dev->name);
00877                 nl->connection = PLIP_CN_NONE;
00878                 nl->should_relinquish = 0;
00879                 dev->tbusy = 0;
00880                 dev->interrupt = 0;
00881                 outb(PAR_INTR_ON, PAR_CONTROL(dev));
00882                 ENABLE(dev->irq);
00883                 mark_bh(NET_BH);
00884         } else {
00885                 nl->is_deferred = 1;
00886                 queue_task(&nl->deferred, &tq_timer);
00887         }
00888 
00889         return OK;
00890 }
00891 
00892 /* Handle the parallel port interrupts. */
00893 static void
00894 plip_interrupt(int irq, void *dev_id, struct pt_regs * regs)
00895 {
00896         struct device *dev = dev_id;
00897         struct net_local *nl;
00898         struct plip_local *rcv;
00899         unsigned char c0;
00900 
00901         if (dev == NULL) {
00902                 printk(KERN_DEBUG "plip_interrupt: irq %d for unknown device.\n", irq);
00903                 return;
00904         }
00905 
00906         nl = (struct net_local *)dev->priv;
00907         rcv = &nl->rcv_data;
00908 
00909         if (dev->interrupt)
00910                 return;
00911 
00912         c0 = inb(PAR_STATUS(dev));
00913         if ((c0 & 0xf8) != 0xc0) {
00914                 if (net_debug > 1)
00915                         printk(KERN_DEBUG "%s: spurious interrupt\n", dev->name);
00916                 return;
00917         }
00918         dev->interrupt = 1;
00919         if (net_debug > 3)
00920                 printk(KERN_DEBUG "%s: interrupt.\n", dev->name);
00921 
00922         spin_lock_irq(&nl->lock);
00923         switch (nl->connection) {
00924         case PLIP_CN_CLOSING:
00925                 dev->tbusy = 0;
00926         case PLIP_CN_NONE:
00927         case PLIP_CN_SEND:
00928                 dev->last_rx = jiffies;
00929                 rcv->state = PLIP_PK_TRIGGER;
00930                 nl->connection = PLIP_CN_RECEIVE;
00931                 nl->timeout_count = 0;
00932                 queue_task(&nl->immediate, &tq_immediate);
00933                 mark_bh(IMMEDIATE_BH);
00934                 spin_unlock_irq(&nl->lock);
00935                 break;
00936 
00937         case PLIP_CN_RECEIVE:
00938                 /* May occur because there is race condition
00939                    around test and set of dev->interrupt.
00940                    Ignore this interrupt. */
00941                 spin_unlock_irq(&nl->lock);
00942                 break;
00943 
00944         case PLIP_CN_ERROR:
00945                 spin_unlock_irq(&nl->lock);
00946                 printk(KERN_ERR "%s: receive interrupt in error state\n", dev->name);
00947                 break;
00948         }
00949 }
00950 
00951 /* We don't need to send arp, for plip is point-to-point. */
00952 static int
00953 plip_rebuild_header(struct sk_buff *skb)
00954 {
00955         struct device *dev = skb->dev;
00956         struct net_local *nl = (struct net_local *)dev->priv;
00957         struct ethhdr *eth = (struct ethhdr *)skb->data;
00958 
00959         if ((dev->flags & IFF_NOARP)==0)
00960                 return nl->orig_rebuild_header(skb);
00961 
00962         memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
00963         memcpy(eth->h_dest, dev->broadcast, dev->addr_len);
00964         return 0;
00965 }
00966 
00967 static int
00968 plip_tx_packet(struct sk_buff *skb, struct device *dev)
00969 {
00970         struct net_local *nl = (struct net_local *)dev->priv;
00971         struct plip_local *snd = &nl->snd_data;
00972 
00973         if (dev->tbusy)
00974                 return 1;
00975 
00976         /* We may need to grab the bus */
00977         if (!nl->port_owner) {
00978                 if (parport_claim(nl->pardev))
00979                         return 1;
00980                 nl->port_owner = 1;
00981         }
00982 
00983         if (test_and_set_bit(0, (void*)&dev->tbusy) != 0) {
00984                 printk(KERN_WARNING "%s: Transmitter access conflict.\n", dev->name);
00985                 return 1;
00986         }
00987 
00988         if (skb->len > dev->mtu + dev->hard_header_len) {
00989                 printk(KERN_WARNING "%s: packet too big, %d.\n", dev->name, (int)skb->len);
00990                 dev->tbusy = 0;
00991                 return 0;
00992         }
00993 
00994         if (net_debug > 2)
00995                 printk(KERN_DEBUG "%s: send request\n", dev->name);
00996 
00997         spin_lock_irq(&nl->lock);
00998         dev->trans_start = jiffies;
00999         snd->skb = skb;
01000         snd->length.h = skb->len;
01001         snd->state = PLIP_PK_TRIGGER;
01002         if (nl->connection == PLIP_CN_NONE) {
01003                 nl->connection = PLIP_CN_SEND;
01004                 nl->timeout_count = 0;
01005         }
01006         queue_task(&nl->immediate, &tq_immediate);
01007         mark_bh(IMMEDIATE_BH);
01008         spin_unlock_irq(&nl->lock);
01009 
01010         return 0;
01011 }
01012 
01013 /* Open/initialize the board.  This is called (in the current kernel)
01014    sometime after booting when the 'ifconfig' program is run.
01015 
01016    This routine gets exclusive access to the parallel port by allocating
01017    its IRQ line.
01018  */
01019 static int
01020 plip_open(struct device *dev)
01021 {
01022         struct net_local *nl = (struct net_local *)dev->priv;
01023         struct in_device *in_dev;
01024 
01025         /* Grab the port */
01026         if (!nl->port_owner) {
01027                 if (parport_claim(nl->pardev)) return -EAGAIN;
01028                 nl->port_owner = 1;
01029         }
01030 
01031         nl->should_relinquish = 0;
01032 
01033         /* Clear the data port. */
01034         outb (0x00, PAR_DATA(dev));
01035 
01036         /* Enable rx interrupt. */
01037         outb(PAR_INTR_ON, PAR_CONTROL(dev));
01038 
01039         /* Initialize the state machine. */
01040         nl->rcv_data.state = nl->snd_data.state = PLIP_PK_DONE;
01041         nl->rcv_data.skb = nl->snd_data.skb = NULL;
01042         nl->connection = PLIP_CN_NONE;
01043         nl->is_deferred = 0;
01044 
01045         /* Fill in the MAC-level header.
01046            (ab)Use "dev->broadcast" to store point-to-point MAC address.
01047 
01048            PLIP doesn't have a real mac address, but we need to create one
01049            to be DOS compatible.  */
01050         memset(dev->dev_addr,  0xfc, ETH_ALEN);
01051         memset(dev->broadcast, 0xfc, ETH_ALEN);
01052 
01053         if ((in_dev=dev->ip_ptr) != NULL) {
01054                 /*
01055                  *      Any address will do - we take the first
01056                  */
01057                 struct in_ifaddr *ifa=in_dev->ifa_list;
01058                 if (ifa != NULL) {
01059                         memcpy(dev->dev_addr+2, &ifa->ifa_local, 4);
01060                         memcpy(dev->broadcast+2, &ifa->ifa_address, 4);
01061                 }
01062         }
01063 
01064         dev->interrupt = 0;
01065         dev->start = 1;
01066         dev->tbusy = 0;
01067 
01068         MOD_INC_USE_COUNT;
01069         return 0;
01070 }
01071 
01072 /* The inverse routine to plip_open (). */
01073 static int
01074 plip_close(struct device *dev)
01075 {
01076         struct net_local *nl = (struct net_local *)dev->priv;
01077         struct plip_local *snd = &nl->snd_data;
01078         struct plip_local *rcv = &nl->rcv_data;
01079 
01080         dev->tbusy = 1;
01081         dev->start = 0;
01082         DISABLE(dev->irq);
01083         synchronize_irq();
01084 
01085 #ifdef NOTDEF
01086         outb(0x00, PAR_DATA(dev));
01087 #endif
01088         nl->is_deferred = 0;
01089         nl->connection = PLIP_CN_NONE;
01090         if (nl->port_owner) {
01091                 parport_release(nl->pardev);
01092                 nl->port_owner = 0;
01093         }
01094 
01095         snd->state = PLIP_PK_DONE;
01096         if (snd->skb) {
01097                 dev_kfree_skb(snd->skb);
01098                 snd->skb = NULL;
01099         }
01100         rcv->state = PLIP_PK_DONE;
01101         if (rcv->skb) {
01102                 kfree_skb(rcv->skb);
01103                 rcv->skb = NULL;
01104         }
01105 
01106 #ifdef NOTDEF
01107         /* Reset. */
01108         outb(0x00, PAR_CONTROL(dev));
01109 #endif
01110         MOD_DEC_USE_COUNT;
01111         return 0;
01112 }
01113 
01114 static int
01115 plip_preempt(void *handle)
01116 {
01117         struct device *dev = (struct device *)handle;
01118         struct net_local *nl = (struct net_local *)dev->priv;
01119 
01120         /* Stand our ground if a datagram is on the wire */
01121         if (nl->connection != PLIP_CN_NONE) {
01122                 nl->should_relinquish = 1;
01123                 return 1;
01124         }
01125 
01126         nl->port_owner = 0;     /* Remember that we released the bus */
01127         return 0;
01128 }
01129 
01130 static void
01131 plip_wakeup(void *handle)
01132 {
01133         struct device *dev = (struct device *)handle;
01134         struct net_local *nl = (struct net_local *)dev->priv;
01135 
01136         if (nl->port_owner) {
01137                 /* Why are we being woken up? */
01138                 printk(KERN_DEBUG "%s: why am I being woken up?\n", dev->name);
01139                 if (!parport_claim(nl->pardev))
01140                         /* bus_owner is already set (but why?) */
01141                         printk(KERN_DEBUG "%s: I'm broken.\n", dev->name);
01142                 else
01143                         return;
01144         }
01145         
01146         if (!(dev->flags & IFF_UP))
01147                 /* Don't need the port when the interface is down */
01148                 return;
01149 
01150         if (!parport_claim(nl->pardev)) {
01151                 nl->port_owner = 1;
01152                 /* Clear the data port. */
01153                 outb (0x00, PAR_DATA(dev));
01154         }
01155 
01156         return;
01157 }
01158 
01159 static struct n