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. plip_send_enethdr
  17. plip_rebuild_enethdr
  18. cold_sleep
  19. double_timeoutfactor
  20. plip_get_stats

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

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