root/drivers/net/plip.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. plip_init
  2. plip_open
  3. plip_close
  4. plip_tx_packet
  5. plip_header
  6. plip_device_clear
  7. plip_receiver_error
  8. get_byte
  9. plip_interrupt
  10. plip_receive_packet
  11. send_byte
  12. plip_send_start
  13. plip_send_packet
  14. plip_set_physicaladdr
  15. plip_addrcmp
  16. cold_sleep
  17. double_timeoutfactor
  18. plip_get_stats
  19. init_module
  20. cleanup_module

   1 /*
   2  *       Plip.c: A parallel port "network" driver for linux. 
   3  */
   4 
   5 /*
   6  *      Developement History:
   7  *
   8  *      Original version and the name 'PLIP' from Donald Becker  <becker@super.org>
   9  *              inspired by Russ Nelson's parallel port packet driver.
  10  *      Further development by Tommy Thorn <thorn@daimi.aau.dk>
  11  *      Some changes by Tanabe Hiroyasu <hiro@sanpo.t.u-tokyo.ac.jp>
  12  *      Upgraded for PL12 by Donald Becker
  13  *      Minor hacks by Alan Cox <gw4pts@gw4pts.ampr.org> to get it working
  14  *              more reliably (Ha!)
  15  *     Changes even more Peter Bauer (100136.3530@compuserve.com)
  16  *     Protocol changed back to original plip as in crynwr's packet-drivers.
  17  *     Tested this against ncsa-telnet 2.3 and pcip_pkt using plip.com (which
  18  *     contains "version        equ     0" and ";History:562,1" in the firts 2
  19  *    source-lines                                              28-Mar-94
  20  *
  21  *      Modularised it (Alan Cox). Will upgrade to Niibe's PLIP once its settled
  22  *      down better.
  23  *      
  24  *
  25  *  This is parallel port packet pusher.  It's actually more general
  26  *  than the "IP" in its name suggests -- but 'plip' is just such a
  27  *  great name!
  28  * 
  29  *    
  30  *   Bugs: Please read this: The PLIP driver is a nasty hack and like all nasty hacks 
  31  *         has some 'features'.
  32  *
  33  *      Can lock machines solid if one end goes down or crashes, or due to cable faults.
  34  *      Can lock both machines solid on a broadcast collision.
  35  *      Some laptops don't have all the wires we use.
  36  *      Doesn't match the original Russ Nelson protocol so won't talk to Amiga or PC drivers.
  37  *      Waits far too long with interrupts off [X is unbearable, forget action games, xntp is a joke]
  38  *      Doesn't work on some fast 486DX machines
  39  *
  40  *      If it works be thankful, if not fix it!
  41  *
  42  *  Info:
  43  *      I <Alan> got 15K/second NFS throughput (about 20-25K second IP). I also got some ethernet cards
  44  *      so don't ask me for help. This code needs a real major rewrite. Any volunteers ?
  45  *
  46  ***** So we can all compare loads of different PLIP drivers for a bit I've modularised this beastie too.
  47  ***** In addition a seperate bidirectional plip module can be done.
  48  *
  49  *      WARNING: The PRE 1.1.16 plip will NOT work with this PLIP driver. We
  50  *      can't avoid this due to an error in the old plip module. If you must
  51  *      mix PLIP's you'll need to fix the _OLD_ one to use 0xFC 0xFC as its
  52  *      MAC header not 0xFD.
  53  *
  54  */
  55 
  56 static char *version =
  57     "NET3 "
  58 #ifdef MODULE
  59     "MODULAR "    
  60 #endif    
  61     "PLIP.010 (from plip.c:v0.15 for 0.99pl12+, 8/11/93)\n";
  62 
  63 #include <linux/config.h>
  64 
  65 /*
  66   Sources:
  67         Ideas and protocols came from Russ Nelson's (nelson@crynwr.com)
  68         "parallel.asm" parallel port packet driver.
  69         TANABE Hiroyasu changes the protocol.
  70   The "Crynwr" parallel port standard specifies the following protocol:
  71    send header nibble '8'
  72    type octet '0xfd' or '0xfc'
  73    count-low octet
  74    count-high octet
  75    ... data octets
  76    checksum octet
  77 Each octet is sent as <wait for rx. '0x1?'> <send 0x10+(octet&0x0F)>
  78                         <wait for rx. '0x0?'> <send 0x00+((octet>>4)&0x0F)>
  79 
  80 The cable used is a de facto standard parallel null cable -- sold as
  81 a "LapLink" cable by various places.  You'll need a 10-conductor cable to
  82 make one yourself.  The wiring is:
  83     INIT        16 - 16         SLCTIN  17 - 17
  84     GROUND      25 - 25
  85     D0->ERROR   2 - 15          15 - 2
  86     D1->SLCT    3 - 13          13 - 3
  87     D2->PAPOUT  4 - 12          12 - 4
  88     D3->ACK     5 - 10          10 - 5
  89     D4->BUSY    6 - 11          11 - 6
  90   Do not connect the other pins.  They are
  91     D5,D6,D7 are 7,8,9
  92     STROBE is 1, FEED is 14
  93     extra grounds are 18,19,20,21,22,23,24
  94 */
  95 
  96 #include <linux/kernel.h>
  97 #include <linux/sched.h>
  98 #include <linux/types.h>
  99 #include <linux/fcntl.h>
 100 #include <linux/interrupt.h>
 101 #include <linux/string.h>
 102 #include <linux/ptrace.h>
 103 #include <linux/if_ether.h>
 104 #include <asm/system.h>
 105 #include <asm/io.h>
 106 #include <netinet/in.h>
 107 #include <errno.h>
 108 
 109 #include <linux/netdevice.h>
 110 #include <linux/etherdevice.h>
 111 #include <linux/skbuff.h>
 112 
 113 #ifdef MODULE
 114 #include <linux/module.h>
 115 #include "../../tools/version.h"
 116 #endif
 117 
 118 #ifdef PRINTK
 119 #undef PRINTK
 120 #endif
 121 #ifdef PRINTK2
 122 #undef PRINTK2
 123 #endif
 124 
 125 #define PLIP_DEBUG      /* debugging */
 126 #undef  PLIP_DEBUG2     /* debugging with more varbose report */
 127 
 128 #ifdef PLIP_DEBUG
 129 #define PRINTK(x) printk x
 130 #else
 131 #define PRINTK(x) /**/
 132 #endif
 133 #ifdef PLIP_DEBUG2
 134 #define PRINTK2(x) printk x
 135 #else
 136 #define PRINTK2(x) /**/
 137 #endif
 138 
 139 /* The map from IRQ number (as passed to the interrupt handler) to
 140    'struct device'. */
 141 extern struct device *irq2dev_map[16];
 142 
 143 /* Network statistics, with the same names as 'struct enet_statistics'. */
 144 #define netstats enet_statistics
 145 
 146 /* constants */
 147 #define PAR_DATA        0
 148 #define PAR_STATUS      1
 149 #define PAR_CONTROL     2
 150 #define PLIP_MTU 1600
 151 #define PLIP_HEADER_TYPE1 0xfd
 152 #define PLIP_HEADER_TYPE2 0xfc
 153 
 154 /* Index to functions, as function prototypes. */
 155 extern int plip_probe(int ioaddr, struct device *dev);
 156 static int plip_open(struct device *dev);
 157 static int plip_close(struct device *dev);
 158 static int plip_tx_packet(struct sk_buff *skb, struct device *dev);
 159 static int plip_header (unsigned char *buff, struct device *dev,
 160                  unsigned short type, void *dest,
 161                  void *source, unsigned len, struct sk_buff *skb);
 162 
 163 /* variables used internally. */
 164 #define INITIALTIMEOUTFACTOR 4
 165 #define MAXTIMEOUTFACTOR 20
 166 static int timeoutfactor = INITIALTIMEOUTFACTOR;
 167 
 168 /* Routines used internally. */
 169 static void plip_device_clear(struct device *dev);
 170 static void plip_receiver_error(struct device *dev);
 171 static void plip_set_physicaladdr(struct device *dev, unsigned long ipaddr);
 172 static int plip_addrcmp(struct ethhdr *eth);
 173 static void cold_sleep(int tics);
 174 static void plip_interrupt(int reg_ptr); /* Dispatch from interrupts. */
 175 static int plip_receive_packet(struct device *dev);
 176 static int plip_send_packet(struct device *dev, unsigned char *buf, int length);
 177 static int plip_send_start(struct device *dev, struct ethhdr *eth);
 178 static void double_timeoutfactor(void);
 179 static struct enet_statistics *plip_get_stats(struct device *dev);
 180 
 181 int
 182 plip_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 183 {
 184     int port_base = dev->base_addr;
 185     int i;
 186 
 187     /* Check that there is something at base_addr. */
 188     outb(0x00, port_base + PAR_CONTROL);
 189     outb(0x55, port_base + PAR_DATA);
 190     if (inb(port_base + PAR_DATA) != 0x55)
 191         return -ENODEV;
 192 
 193     /* Alpha testers must have the version number to report bugs. */
 194 #ifdef PLIP_DEBUG
 195     {
 196         static int version_shown = 0;
 197         if (! version_shown)
 198             printk(version), version_shown++;
 199     }
 200 #endif
 201 
 202     /* Initialize the device structure. */
 203     dev->priv = kmalloc(sizeof(struct netstats), GFP_KERNEL);
 204     memset(dev->priv, 0, sizeof(struct netstats));
 205 
 206     for (i = 0; i < DEV_NUMBUFFS; i++)
 207         skb_queue_head_init(&dev->buffs[i]);
 208 
 209     dev->hard_header = &plip_header;
 210     dev->rebuild_header = eth_rebuild_header;
 211     dev->type_trans = eth_type_trans;
 212 
 213     dev->open = &plip_open;
 214     dev->stop = &plip_close;
 215     dev->hard_start_xmit = &plip_tx_packet;
 216     dev->get_stats = &plip_get_stats;
 217 
 218     /* These are ethernet specific. */
 219     dev->type = ARPHRD_ETHER;
 220     dev->hard_header_len = ETH_HLEN;
 221     dev->mtu = PLIP_MTU;        /* PLIP may later negotiate max pkt size */
 222     dev->addr_len = ETH_ALEN;
 223     for (i = 0; i < dev->addr_len; i++) {
 224         dev->broadcast[i]=0xff;
 225         dev->dev_addr[i] = 0;
 226     }
 227     printk("%s: configured for parallel port at %#3x, IRQ %d.\n",
 228            dev->name, dev->base_addr, dev->irq);
 229 
 230     /* initialize internal value */
 231     timeoutfactor = INITIALTIMEOUTFACTOR;
 232     return 0;
 233 }
 234 
 235 /* Open/initialize the board.  This is called (in the current kernel)
 236    sometime after booting when the 'config <dev->name>' program is
 237    run.
 238 
 239    This routine gets exclusive access to the parallel port by allocating
 240    its IRQ line.
 241    */
 242    
 243 static int plip_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 244 {
 245     if (dev->irq == 0)
 246         dev->irq = 7;
 247     cli();
 248     if (request_irq(dev->irq , &plip_interrupt) != 0) {
 249         sti();
 250         PRINTK(("%s: couldn't get IRQ %d.\n", dev->name, dev->irq));
 251         return -EAGAIN;
 252     }
 253 
 254     irq2dev_map[dev->irq] = dev;
 255     sti();
 256     plip_device_clear(dev);
 257     dev->tbusy = 0;
 258     dev->interrupt = 0;
 259     dev->start = 1;
 260 #ifdef MODULE
 261     MOD_INC_USE_COUNT;
 262 #endif        
 263     return 0;
 264 }
 265 
 266 /* The inverse routine to plip_open(). */
 267 static int
 268 plip_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 269 {
 270     dev->tbusy = 1;
 271     dev->start = 0;
 272     cli();
 273     free_irq(dev->irq);
 274     irq2dev_map[dev->irq] = NULL;
 275     sti();
 276     outb(0x00, dev->base_addr);         /* Release the interrupt. */
 277 #ifdef MODULE
 278     MOD_DEC_USE_COUNT;
 279 #endif        
 280     return 0;
 281 }
 282 
 283 static int
 284 plip_tx_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 285 {
 286     int ret_val;
 287 
 288     if (dev->tbusy || dev->interrupt) { /* Do timeouts, to avoid hangs. */
 289         int tickssofar = jiffies - dev->trans_start;
 290         if (tickssofar < 50)
 291             return 1;
 292         printk("%s: transmit timed out\n", dev->name);
 293         /* Try to restart the adaptor. */
 294         plip_device_clear(dev);
 295         return 0;
 296     }
 297 
 298     /* If some higher layer thinks we've missed an tx-done interrupt
 299        we are passed NULL. Caution: dev_tint() handles the cli()/sti()
 300        itself. */
 301     if (skb == NULL) {
 302         dev_tint(dev);
 303         return 0;
 304     }
 305 
 306     dev->trans_start = jiffies;
 307     ret_val = plip_send_packet(dev, skb->data, skb->len);
 308     if (skb->free)
 309         kfree_skb (skb, FREE_WRITE);
 310     dev->tbusy = 0;
 311     mark_bh (NET_BH);
 312     return 0/*ret_val*/;
 313 }
 314 
 315 static int
 316 plip_header (unsigned char *buff, struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 317              unsigned short type, void *daddr                ,
 318              void *saddr, unsigned len, struct sk_buff *skb)
 319 {
 320     if (dev->dev_addr[0] == 0) {
 321         /* set physical address */
 322         plip_set_physicaladdr(dev, dev->pa_addr);
 323     }
 324     return eth_header(buff, dev, type, daddr, saddr, len, skb);
 325 }
 326 
 327 static void
 328   plip_device_clear(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 329 {
 330     dev->interrupt = 0;
 331     dev->tbusy = 0;
 332     outb(0x00, dev->base_addr + PAR_DATA);
 333     outb(0x10, dev->base_addr + PAR_CONTROL);           /* Enable the rx interrupt. */
 334 }
 335 
 336 static void
 337   plip_receiver_error(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 338 {
 339     dev->interrupt = 0;
 340     dev->tbusy = 0;
 341     outb(0x02, dev->base_addr + PAR_DATA);
 342     outb(0x10, dev->base_addr + PAR_CONTROL);           /* Enable the rx interrupt. */
 343 }
 344 
 345 static int
 346   get_byte(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 347 {
 348     unsigned char val, oldval;
 349     unsigned char low_nibble;
 350     int timeout;
 351     int error = 0;
 352     val = inb(dev->base_addr + PAR_STATUS);
 353     timeout = jiffies + timeoutfactor * 2;
 354     do {
 355         oldval = val;
 356         val = inb(dev->base_addr + PAR_STATUS);
 357         if ( oldval != val ) continue; /* it's unstable */
 358         if ( timeout < jiffies ) {
 359             error++;
 360             break;
 361         }
 362     } while ( (val & 0x80) );
 363     val = inb(dev->base_addr + PAR_STATUS);
 364     low_nibble = (val >> 3) & 0x0f;
 365     outb(0x10, dev->base_addr + PAR_DATA);
 366     timeout = jiffies + timeoutfactor * 2;
 367     do {
 368         oldval = val;
 369         val = inb(dev->base_addr + PAR_STATUS);
 370         if (oldval != val) continue; /* it's unstable */
 371         if ( timeout < jiffies ) {
 372             error++;
 373             break;
 374         }
 375     } while ( !(val & 0x80) );
 376     val = inb(dev->base_addr + PAR_STATUS);
 377     PRINTK2(("%02x %s ", low_nibble | ((val << 1) & 0xf0),
 378                error ? "t":""));
 379     outb(0x00, dev->base_addr + PAR_DATA);
 380     if (error) {
 381         /* timeout error */
 382         double_timeoutfactor();
 383         return -1;
 384     }
 385     return low_nibble | ((val << 1) & 0xf0);
 386 }
 387 
 388 /* The typical workload of the driver:
 389    Handle the parallel port interrupts. */
 390 static void
 391   plip_interrupt(int reg_ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 392 {
 393     int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
 394     struct device *dev = irq2dev_map[irq];
 395     struct netstats *localstats;
 396 
 397     if (dev == NULL) {
 398         PRINTK(("plip_interrupt(): irq %d for unknown device.\n", irq));
 399         return;
 400     }
 401     localstats = (struct netstats*) dev->priv;
 402     if (dev->tbusy || dev->interrupt) return;
 403     dev->interrupt = 1;
 404     outb(0x00, dev->base_addr + PAR_CONTROL);  /* Disable the rx interrupt. */
 405     sti(); /* Allow other interrupts. */
 406     PRINTK2(("%s: interrupt.  ", dev->name));
 407 
 408     {
 409         /* check whether the interrupt is valid or not.*/
 410         int timeout = jiffies + timeoutfactor;
 411         while ((inb(dev->base_addr + PAR_STATUS) & 0xf8) != 0xc0) {
 412             if ( timeout < jiffies ) {
 413                 PRINTK2(("%s: No interrupt (status=%#02x)!\n",
 414                          dev->name, inb(dev->base_addr + PAR_STATUS)));
 415                 plip_device_clear(dev);
 416                 return;
 417             }
 418         }
 419     }
 420     if (plip_receive_packet(dev)) {
 421         /* get some error while receiving data */
 422         localstats->rx_errors++;
 423         plip_receiver_error(dev);
 424     } else {
 425         plip_device_clear(dev);
 426     }
 427 }
 428 
 429 static int
 430 plip_receive_packet(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 431 {
 432     unsigned length;
 433     int checksum = 0;
 434     struct sk_buff *skb;
 435     struct netstats *localstats;
 436     struct ethhdr eth;
 437 
 438     localstats = (struct netstats*) dev->priv;
 439     
 440     outb(1, dev->base_addr + PAR_DATA);         /* Ack: 'Ready' */
 441 
 442     {
 443         /* get header octet and length of packet */
 444         
 445         length = get_byte(dev);
 446         length |= get_byte(dev) << 8;
 447         {
 448          int i;
 449          unsigned char *eth_p = (unsigned char*)&eth;
 450          for ( i = 0; i < sizeof(eth); i++, eth_p++) {
 451              *eth_p = get_byte(dev);
 452          }
 453         }
 454         PRINTK2(("length = %d\n", length));
 455         if (length > dev->mtu || length < 8) {
 456             PRINTK2(("%s: bogus packet size %d.\n", dev->name, length));
 457             return 1;
 458         }
 459     }
 460     {
 461         /* get skb area from kernel and 
 462          * set appropriate values to skb
 463          */
 464         skb = alloc_skb(length, GFP_ATOMIC);
 465         if (skb == NULL) {
 466             PRINTK(("%s: Couldn't allocate a sk_buff of size %d.\n",
 467                     dev->name,length));
 468             return 1;
 469         }
 470         skb->lock = 0;
 471     }
 472     {
 473         /* phase of receiving the data */
 474         /* 'skb->data' points to the start of sk_buff data area. */
 475         unsigned char *buf = skb->data;
 476         unsigned char *eth_p = (unsigned char *)&eth;
 477         int i;
 478         for ( i = 0; i < sizeof(eth); i++) {
 479             checksum += *eth_p;
 480             *buf++ = *eth_p++;
 481         }
 482         for ( i = 0; i < length - sizeof(eth); i++) {
 483             unsigned char new_byte = get_byte(dev);
 484             checksum += new_byte;
 485             *buf++ = new_byte;
 486         }
 487         checksum &= 0xff;
 488         if (checksum != get_byte(dev)) {
 489             localstats->rx_crc_errors++;
 490             PRINTK(("checksum error\n"));
 491             return 1;
 492         } else if(dev_rint((unsigned char *)skb, length, IN_SKBUFF, dev)) {
 493             printk("%s: rcv buff full.\n", dev->name);
 494             localstats->rx_dropped++;
 495             return 1;
 496         }
 497     }
 498     {
 499         /* phase of terminating this connection */
 500         int timeout;
 501 
 502         timeout = jiffies + length * timeoutfactor / 16;
 503         outb(0x00, dev->base_addr + PAR_DATA);
 504         /* Wait for the remote end to reset. */
 505         while ( (inb(dev->base_addr + PAR_STATUS) & 0xf8) != 0x80 ) {
 506             if (timeout < jiffies ) {
 507                 double_timeoutfactor();
 508                 PRINTK(("Remote has not reset.\n"));
 509                 break;
 510             }
 511         }
 512     }
 513     localstats->rx_packets++;
 514     return 0;
 515 }
 516 
 517 
 518 static int send_byte(struct device *dev, unsigned char val)
     /* [previous][next][first][last][top][bottom][index][help] */
 519 {
 520     int timeout;
 521     int error = 0;
 522     PRINTK2((" S%02x", val));
 523     outb((val & 0xf), dev->base_addr); /* this makes data bits more stable */
 524                                        /* (especially the &0xf :-> PB )    */
 525     outb(0x10 | (val & 0xf), dev->base_addr);
 526     timeout = jiffies + timeoutfactor;
 527     while( inb(dev->base_addr+PAR_STATUS) & 0x80 )
 528         if ( timeout < jiffies ) {
 529             error++;
 530             break;
 531         }
 532     outb(0x10 | (val >> 4), dev->base_addr);
 533     outb(val >> 4, dev->base_addr);
 534     timeout = jiffies + timeoutfactor;
 535     while( (inb(dev->base_addr+PAR_STATUS) & 0x80) == 0 )
 536         if ( timeout < jiffies ) {
 537             error++;
 538             break;
 539         }
 540     if (error) {
 541         /* timeout error */
 542         double_timeoutfactor();
 543         PRINTK2(("t"));
 544         return -1;
 545     }
 546     return 0;
 547 }
 548 /*
 549  * plip_send_start
 550  * trigger remoto rx interrupt and establish a connection.
 551  * 
 552  * return value
 553  * 0 : establish the connection
 554  * -1 : connection failed.
 555  */
 556 static int
 557 plip_send_start(struct device *dev, struct ethhdr *eth)
     /* [previous][next][first][last][top][bottom][index][help] */
 558 {       
 559     int timeout;
 560     int status;
 561     int lasttrigger;
 562     struct netstats *localstats = (struct netstats*) dev->priv;
 563 
 564     /* This starts the packet protocol by triggering a remote IRQ. */
 565     timeout = jiffies + timeoutfactor * 16;
 566     lasttrigger = jiffies;
 567     while ( ((status = inb(dev->base_addr+PAR_STATUS)) & 0x08) == 0 ) {
 568         dev->tbusy = 1;
 569         outb(0x00, dev->base_addr + PAR_CONTROL); /* Disable my rx intr. */
 570         outb(0x08, dev->base_addr + PAR_DATA);  /* Trigger remote rx intr. */
 571         if (status & 0x40) {
 572             /* The remote end is also trying to send a packet.
 573              * Only one end may go to the receiving phase,
 574              * so we use the "ethernet" address (set from the IP address)
 575              * to determine which end dominates.
 576              */
 577             if ( plip_addrcmp(eth) > 0 ) {
 578                 localstats->collisions++;
 579                 PRINTK2(("both ends are trying to send a packet.\n"));
 580                 if (plip_receive_packet(dev)) {
 581                     /* get some error while receiving data */
 582                     localstats->rx_errors++;
 583                     outb(0x02, dev->base_addr + PAR_DATA);
 584                 } else {
 585                     outb(0x00, dev->base_addr + PAR_DATA);
 586                 }
 587                 cold_sleep(2); /* make sure that remote end is ready */
 588             }
 589             continue; /* restart send sequence */
 590         }
 591         if (lasttrigger != jiffies) {
 592             /* trigger again */
 593             outb(0x00, dev->base_addr + PAR_DATA);
 594             cold_sleep(1);
 595             lasttrigger = jiffies;
 596         }
 597         if (timeout < jiffies) {
 598             double_timeoutfactor();
 599             plip_device_clear(dev);
 600             localstats->tx_errors++;
 601             PRINTK(("%s: Connect failed in send_packet().\n",
 602                     dev->name));
 603             /* We failed to send the packet.  To emulate the ethernet we
 604                should pretent the send worked fine */
 605             return -1;
 606         }
 607     }
 608     return 0;
 609 }
 610 static int
 611 plip_send_packet(struct device *dev, unsigned char *buf, int length)
     /* [previous][next][first][last][top][bottom][index][help] */
 612 {
 613     int error = 0;
 614     struct netstats *localstats;
 615 
 616     PRINTK2(("%s: plip_send_packet(%d) %02x %02x %02x %02x %02x...",
 617            dev->name, length, buf[0], buf[1], buf[2], buf[3], buf[4]));
 618     if (length > dev->mtu) {
 619         printk("%s: packet too big, %d.\n", dev->name, length);
 620         return 0;
 621     }
 622     localstats = (struct netstats*) dev->priv;
 623 
 624     {
 625         /* phase of checking remote status */
 626         int i;
 627         int timeout = jiffies + timeoutfactor * 8;
 628         while ( (i = (inb(dev->base_addr+PAR_STATUS) & 0xe8)) != 0x80 ) {
 629             if (i == 0x78) {
 630                 /* probably cable is not connected */
 631                 /* Implementation Note:
 632                  * This status should result in 'Network unreachable'.
 633                  * but I don't know the way.
 634                  */
 635                 return 0;
 636             }
 637             if (timeout < jiffies) {
 638                 /* remote end is not ready */
 639                 double_timeoutfactor();
 640                 localstats->tx_errors++;
 641                 PRINTK(("remote end is not ready.\n"));
 642                 return 1; /* Failed to send the packet */
 643             }
 644         }
 645     }
 646     /* phase of making a connection */
 647     if (plip_send_start(dev, (struct ethhdr *)buf) < 0)
 648         return 1;
 649 
 650     {
 651         /* send packet's length 
 652            the byte order has changed now and then. Today it's sent as in 
 653            the original crynwr-plip ...
 654            Gruss PB
 655          */
 656         send_byte(dev, length); 
 657         send_byte(dev, length >> 8);
 658     }
 659     {
 660         /* phase of sending data */
 661         int i;
 662         int checksum = 0;
 663 
 664         for ( i = 0; i < sizeof(struct ethhdr); i++ ) {
 665             send_byte(dev, *buf);
 666             checksum += *buf++;
 667         }
 668 
 669         for (i = 0; i < length - sizeof(struct ethhdr); i++) {
 670             checksum += buf[i];
 671             if (send_byte(dev, buf[i]) < 0) {
 672                 error++;
 673                 break;
 674             }
 675         }
 676         send_byte(dev, checksum & 0xff);
 677     }
 678     {
 679         /* phase of terminating this connection */
 680         int timeout;
 681         
 682         outb(0x00, dev->base_addr + PAR_DATA);
 683         /* Wait for the remote end to reset. */
 684         timeout = jiffies + ((length * timeoutfactor) >> 4);
 685         while ((inb(dev->base_addr + PAR_STATUS) & 0xe8) != 0x80) {
 686             if (timeout < jiffies ) {   
 687                 double_timeoutfactor();
 688                 PRINTK(("Remote end has not reset.\n"));
 689                 error++;
 690                 break;
 691             }
 692         }
 693         if (inb(dev->base_addr + PAR_STATUS) & 0x10) {
 694             /* receiver reports error */
 695             error++;
 696         }
 697     }
 698     plip_device_clear(dev);
 699     localstats->tx_packets++;
 700     PRINTK2(("plip_send_packet(%d) done.\n", length));
 701     return error?1:0;
 702 }
 703 
 704 /*
 705  * some trivial functions
 706  */
 707 static void
 708 plip_set_physicaladdr(struct device *dev, unsigned long ipaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 709 {
 710     /*
 711      * set physical address to
 712      *  0xfc.0xfc.ipaddr
 713      */
 714 
 715     unsigned char *addr = dev->dev_addr;
 716     int i;
 717 
 718     if ((ipaddr >> 24) == 0 || (ipaddr >> 24) == 0xff) return;
 719     PRINTK2(("%s: set physical address to %08x\n", dev->name, ipaddr));
 720     for (i=0; i < ETH_ALEN - sizeof(unsigned long); i++) {
 721         addr[i] = 0xfd;
 722     }
 723     memcpy(&(addr[i]), &ipaddr, sizeof(unsigned long));
 724 }
 725 
 726 static int
 727 plip_addrcmp(struct ethhdr *eth)
     /* [previous][next][first][last][top][bottom][index][help] */
 728 {
 729     int i;
 730     for ( i = ETH_ALEN - 1; i >= 0; i-- ) {
 731         if (eth->h_dest[i] > eth->h_source[i]) return -1;
 732         if (eth->h_dest[i] < eth->h_source[i]) return 1;
 733     }
 734     PRINTK2(("h_dest = %08x%04x h_source = %08x%04x\n",
 735             *(long*)&eth->h_dest[2],*(short*)&eth->h_dest[0],
 736             *(long*)&eth->h_source[2],*(short*)&eth->h_source[0]));
 737     return 0;
 738 }
 739 
 740 /* This function is evil, evil, evil.  This should be a
 741    _kernel_, rescheduling sleep!. */
 742 static void
 743 cold_sleep(int tics)
     /* [previous][next][first][last][top][bottom][index][help] */
 744 {
 745     int start = jiffies;
 746     while(jiffies < start + tics)
 747         ; /* do nothing */
 748     return;
 749 }
 750 
 751 static void
 752   double_timeoutfactor()
     /* [previous][next][first][last][top][bottom][index][help] */
 753 {
 754     timeoutfactor *= 2;
 755     if (timeoutfactor >= MAXTIMEOUTFACTOR) {
 756         timeoutfactor = MAXTIMEOUTFACTOR;
 757     }
 758     return;
 759 }
 760 
 761 static struct enet_statistics *
 762 plip_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 763 {
 764     struct netstats *localstats = (struct netstats*) dev->priv;
 765     return localstats;
 766 }
 767 
 768 /*
 769  * Local variables:
 770  *  compile-command: "gcc -D__KERNEL__ -Wall -O6 -fomit-frame-pointer -x c++ -c plip.c"
 771  *  version-control: t
 772  *  kept-new-versions: 5
 773  * End:
 774  */
 775 
 776 #ifdef MODULE
 777 char kernel_version[] = UTS_RELEASE;
 778 
 779 static struct device dev_plip0 = 
 780 {
 781         "plip0" /*"plip"*/,
 782         0, 0, 0, 0,             /* memory */
 783         0x3BC, 5,               /* base, irq */
 784         0, 0, 0, NULL, plip_init 
 785 };
 786 
 787 static struct device dev_plip1 = 
 788 {
 789         "plip1" /*"plip"*/,
 790         0, 0, 0, 0,             /* memory */
 791         0x378, 7,               /* base, irq */
 792         0, 0, 0, NULL, plip_init 
 793 };
 794 
 795 static struct device dev_plip2 = 
 796 {
 797         "plip2" /*"plip"*/,
 798         0, 0, 0, 0,             /* memory */
 799         0x278, 2,               /* base, irq */
 800         0, 0, 0, NULL, plip_init 
 801 };
 802 
 803 int
 804 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 805 {
 806         int err;
 807 
 808         if ( ((err=register_netdev(&dev_plip0)) == 0) &&
 809              ((err=register_netdev(&dev_plip1)) == 0) &&
 810              ((err=register_netdev(&dev_plip2)) == 0)
 811            )
 812         {
 813                 if(err==-EEXIST)
 814                         printk("plip devices already present. Module not loaded.\n");
 815                 return err;
 816         }
 817         return 0;
 818 }
 819 
 820 void
 821 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 822 {
 823         if (MOD_IN_USE)
 824                 printk("plip: device busy, remove delayed\n");
 825         else
 826         {
 827                 unregister_netdev(&dev_plip0);
 828                 if(dev_plip0.priv)
 829                 {
 830                         kfree_s(dev_plip0.priv,sizeof(struct netstats));
 831                         dev_plip0.priv=NULL;
 832                 }
 833                 unregister_netdev(&dev_plip1);
 834                 if(dev_plip1.priv)
 835                 {
 836                         kfree_s(dev_plip1.priv,sizeof(struct netstats));
 837                         dev_plip0.priv=NULL;
 838                 }
 839                 unregister_netdev(&dev_plip2);
 840                 if(dev_plip2.priv)
 841                 {
 842                         kfree_s(dev_plip2.priv,sizeof(struct netstats));
 843                         dev_plip2.priv=NULL;
 844                 }
 845         }
 846 }
 847 #endif /* MODULE */

/* [previous][next][first][last][top][bottom][index][help] */