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

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