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_tx_packet
  3. plip_open
  4. plip_close
  5. plip_header
  6. plip_get_stats
  7. plip_rebuild_header
  8. plip_device_clear
  9. plip_error
  10. plip_receive
  11. plip_receive_packet
  12. plip_interrupt
  13. plip_send
  14. plip_send_packet
  15. plip_config
  16. plip_ioctl
  17. init_module
  18. cleanup_module

   1 /* plip.c: A parallel port "network" driver for linux. */
   2 /* This driver is for parallel port with 5-bit cable (LapLink (R) cable). */
   3 /*
   4  * Authors:     Donald Becker,  <becker@super.org>
   5  *              Tommy Thorn, <thorn@daimi.aau.dk>
   6  *              Tanabe Hiroyasu, <hiro@sanpo.t.u-tokyo.ac.jp>
   7  *              Alan Cox, <gw4pts@gw4pts.ampr.org>
   8  *              Peter Bauer, <100136.3530@compuserve.com>
   9  *              Niibe Yutaka, <gniibe@mri.co.jp>
  10  *
  11  *              This is the all improved state based PLIP that Niibe Yutaka has contributed.
  12  *
  13  *              Modularization by Alan Cox. I also added the plipconfig program to tune the timeouts
  14  *              and ifmap support for funny serial port settings or setting odd values using the 
  15  *              modular plip. I also took the panic() calls out. I don't like panic - especially when
  16  *              it can be avoided.
  17  *
  18  *              This program is free software; you can redistribute it and/or
  19  *              modify it under the terms of the GNU General Public License
  20  *              as published by the Free Software Foundation; either version
  21  *              2 of the License, or (at your option) any later version.
  22  */
  23 
  24 /*
  25  * Original version and the name 'PLIP' from Donald Becker <becker@super.org>
  26  * inspired by Russ Nelson's parallel port packet driver.
  27  */
  28 
  29 static char *version =
  30     "NET3 "
  31 #ifdef MODULE
  32     "MODULAR "    
  33 #endif    
  34     "PLIP.010+ gniibe@mri.co.jp\n";
  35 
  36 #include <linux/config.h>
  37 
  38 /*
  39   Sources:
  40         Ideas and protocols came from Russ Nelson's <nelson@crynwr.com>
  41         "parallel.asm" parallel port packet driver.
  42 
  43   The "Crynwr" parallel port standard specifies the following protocol:
  44    send header nibble '8'
  45    count-low octet
  46    count-high octet
  47    ... data octets
  48    checksum octet
  49   Each octet is sent as <wait for rx. '0x1?'> <send 0x10+(octet&0x0F)>
  50                         <wait for rx. '0x0?'> <send 0x00+((octet>>4)&0x0F)>
  51 
  52 The cable used is a de facto standard parallel null cable -- sold as
  53 a "LapLink" cable by various places.  You'll need a 10-conductor cable to
  54 make one yourself.  The wiring is:
  55     SLCTIN      17 - 17
  56     GROUND      25 - 25
  57     D0->ERROR   2 - 15          15 - 2
  58     D1->SLCT    3 - 13          13 - 3
  59     D2->PAPOUT  4 - 12          12 - 4
  60     D3->ACK     5 - 10          10 - 5
  61     D4->BUSY    6 - 11          11 - 6
  62   Do not connect the other pins.  They are
  63     D5,D6,D7 are 7,8,9
  64     STROBE is 1, FEED is 14, INIT is 16
  65     extra grounds are 18,19,20,21,22,23,24
  66 */
  67 
  68 #include <linux/kernel.h>
  69 #include <linux/sched.h>
  70 #include <linux/types.h>
  71 #include <linux/fcntl.h>
  72 #include <linux/interrupt.h>
  73 #include <linux/string.h>
  74 #include <linux/ptrace.h>
  75 #include <linux/if_ether.h>
  76 #include <asm/system.h>
  77 #include <asm/io.h>
  78 #include <netinet/in.h>
  79 #include <errno.h>
  80 #include <linux/delay.h>
  81 
  82 #include <linux/netdevice.h>
  83 #include <linux/etherdevice.h>
  84 #include <linux/skbuff.h>
  85 #include <linux/if_plip.h>
  86 
  87 #include <linux/timer.h>
  88 #include <linux/ioport.h>
  89 #include <asm/bitops.h>
  90 #include <asm/irq.h>
  91 
  92 #ifdef MODULE
  93 #include <linux/module.h>
  94 #include "../../tools/version.h"
  95 #endif
  96 
  97 /* use 0 for production, 1 for verification, >2 for debug */
  98 #ifndef NET_DEBUG
  99 #define NET_DEBUG 3
 100 #endif
 101 static unsigned int net_debug = NET_DEBUG;
 102 
 103 /* constants */
 104 #define PLIP_MTU 1500
 105 
 106 /* In micro second */
 107 #define PLIP_DELAY_UNIT            1
 108 
 109 /* Connection time out = PLIP_TRIGGER_WAIT * PLIP_DELAY_UNIT usec */
 110 #define PLIP_TRIGGER_WAIT        500
 111 
 112 /* Nibble time out = PLIP_NIBBLE_WAIT * PLIP_DELAY_UNIT usec */
 113 #define PLIP_NIBBLE_WAIT        3000
 114 
 115 #define PAR_DATA(dev)           (dev->base_addr+0)
 116 #define PAR_STATUS(dev)         (dev->base_addr+1)
 117 #define PAR_CONTROL(dev)        (dev->base_addr+2)
 118 
 119 /* Index to functions, as function prototypes. */
 120 static int plip_tx_packet(struct sk_buff *skb, struct device *dev);
 121 static int plip_open(struct device *dev);
 122 static int plip_close(struct device *dev);
 123 static int plip_header(unsigned char *buff, struct device *dev,
 124                        unsigned short type, void *dest,
 125                        void *source, unsigned len, struct sk_buff *skb);
 126 static struct enet_statistics *plip_get_stats(struct device *dev);
 127 static int plip_rebuild_header(void *buff, struct device *dev,
 128                                unsigned long raddr, struct sk_buff *skb);
 129 
 130 enum plip_state {
 131     PLIP_ST_DONE=0,
 132     PLIP_ST_TRANSMIT_BEGIN,
 133     PLIP_ST_TRIGGER,
 134     PLIP_ST_LENGTH_LSB,
 135     PLIP_ST_LENGTH_MSB,
 136     PLIP_ST_DATA,
 137     PLIP_ST_CHECKSUM,
 138     PLIP_ST_ERROR
 139 };
 140 
 141 enum plip_nibble_state {
 142     PLIP_NST_BEGIN,
 143     PLIP_NST_1,
 144     PLIP_NST_2,
 145     PLIP_NST_END
 146 };
 147 
 148 #define PLIP_STATE_STRING(x) \
 149     (((x) == PLIP_ST_DONE)?"0":\
 150      ((x) == PLIP_ST_TRANSMIT_BEGIN)?"b":\
 151      ((x) == PLIP_ST_TRIGGER)?"t":\
 152      ((x) == PLIP_ST_LENGTH_LSB)?"l":\
 153      ((x) == PLIP_ST_LENGTH_MSB)?"m":\
 154      ((x) == PLIP_ST_DATA)?"d":\
 155      ((x) == PLIP_ST_CHECKSUM)?"s":"B")
 156 
 157 struct plip_local {
 158     enum plip_state state;
 159     enum plip_nibble_state nibble;
 160     unsigned short length;
 161     unsigned short count;
 162     unsigned short byte;
 163     unsigned char  checksum;
 164     unsigned char  data;
 165     struct sk_buff *skb;
 166 };
 167 
 168 struct net_local {
 169     struct enet_statistics e;
 170     struct timer_list tl;
 171     struct plip_local snd_data;
 172     struct plip_local rcv_data;
 173     unsigned long  trigger_us;
 174     unsigned long  nibble_us;
 175     unsigned long  unit_us;
 176 };
 177 
 178 /* Routines used internally. */
 179 static void plip_device_clear(struct device *dev);
 180 static void plip_error(struct device *dev);
 181 static int plip_receive(struct device *dev, enum plip_nibble_state *ns_p,
 182                         unsigned char *data_p);
 183 static void plip_receive_packet(struct device *dev);
 184 static void plip_interrupt(int reg_ptr);
 185 static int plip_send(struct device *dev, enum plip_nibble_state *ns_p,
 186                      unsigned char data);
 187 static void plip_send_packet(struct device *dev);
 188 static int plip_ioctl(struct device *dev, struct ifreq *ifr);
 189 static int plip_config(struct device *dev, struct ifmap *map);
 190 
 191 
 192 int
 193 plip_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 194 {
 195     int i;
 196     struct net_local *pl;
 197 
 198     /* Check that there is something at base_addr. */
 199     outb(0x00, PAR_CONTROL(dev));
 200     outb(0x00, PAR_DATA(dev));
 201     if (inb(PAR_DATA(dev)) != 0x00)
 202         return -ENODEV;
 203 
 204     /* Alpha testers must have the version number to report bugs. */
 205     if (net_debug)
 206         printk(version);
 207 
 208     if (dev->irq) {
 209         printk("%s: configured for parallel port at %#3x, IRQ %d.\n",
 210                dev->name, dev->base_addr, dev->irq);
 211     } else {
 212         printk("%s: configured for parallel port at %#3x",
 213                dev->name, dev->base_addr);
 214         autoirq_setup(0);
 215         outb(0x00, PAR_CONTROL(dev));
 216         outb(0x10, PAR_CONTROL(dev));
 217         outb(0x00, PAR_CONTROL(dev));
 218         dev->irq = autoirq_report(1);
 219         if (dev->irq)
 220             printk(", probed IRQ %d.\n", dev->irq);
 221         else {
 222             printk(", failed to detect IRQ line.\n");
 223             return -ENODEV;
 224         }
 225     }
 226 
 227     /* Initialize the device structure. */
 228     dev->rmem_end       = (unsigned long) NULL;
 229     dev->rmem_start     = (unsigned long) NULL;
 230     dev->mem_end        = (unsigned long) NULL;
 231     dev->mem_start      = (unsigned long) NULL;
 232 
 233     dev->priv = kmalloc(sizeof (struct net_local), GFP_KERNEL);
 234     memset(dev->priv, 0, sizeof(struct net_local));
 235     pl = (struct net_local *) dev->priv;
 236     
 237     pl->trigger_us      =       PLIP_TRIGGER_WAIT;
 238     pl->nibble_us       =       PLIP_NIBBLE_WAIT;
 239 
 240     dev->mtu                    = PLIP_MTU;
 241     dev->hard_start_xmit        = plip_tx_packet;
 242     dev->open                   = plip_open;
 243     dev->stop                   = plip_close;
 244     dev->hard_header            = plip_header;
 245     dev->type_trans             = eth_type_trans;
 246     dev->get_stats              = plip_get_stats;
 247     dev->set_config             = plip_config;
 248     dev->do_ioctl               = plip_ioctl;
 249     
 250 
 251     dev->hard_header_len        = ETH_HLEN;
 252     dev->addr_len               = ETH_ALEN;
 253     dev->type                   = ARPHRD_ETHER;
 254     dev->rebuild_header         = plip_rebuild_header;
 255 
 256     for (i = 0; i < DEV_NUMBUFFS; i++)
 257         skb_queue_head_init(&dev->buffs[i]);
 258 
 259     for (i = 0; i < dev->addr_len; i++) {
 260         dev->broadcast[i]=0xff;
 261         dev->dev_addr[i] = 0;
 262     }
 263 
 264     /* New-style flags. */
 265     dev->flags          = 0;
 266     dev->family         = AF_INET;
 267     dev->pa_addr        = 0;
 268     dev->pa_brdaddr     = 0;
 269     dev->pa_dstaddr     = 0;
 270     dev->pa_mask        = 0;
 271     dev->pa_alen        = sizeof(unsigned long);
 272 
 273     return 0;
 274 }
 275 
 276 static int
 277 plip_tx_packet (struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 278 {
 279     struct net_local *lp = (struct net_local *)dev->priv;
 280     struct plip_local *snd = &lp->snd_data;
 281 
 282     if (dev->tbusy) {
 283         /* it is sending a packet now */
 284         int tickssofar = jiffies - dev->trans_start;
 285         if (tickssofar < 100)   /* please try later, again */
 286             return 1;
 287 
 288         /* something wrong... force to reset */
 289         printk("%s: transmit timed out, cable problem??\n", dev->name);
 290         plip_device_clear(dev);
 291     }
 292 
 293     /* If some higher layer thinks we've missed an tx-done interrupt
 294        we are passed NULL. Caution: dev_tint() handles the cli()/sti()
 295        itself. */
 296     if (skb == NULL) {
 297         dev_tint(dev);
 298         return 0;
 299     }
 300 
 301     cli();
 302     if (set_bit(0, (void *)&dev->tbusy) != 0) {
 303         sti();
 304         printk("%s: Transmitter access conflict.\n", dev->name);
 305         return 1;
 306     }
 307     if (dev->interrupt) {
 308         sti();
 309         return 1;
 310     }
 311     snd->state = PLIP_ST_TRANSMIT_BEGIN;
 312     sti();
 313 
 314     dev->trans_start = jiffies;
 315     if (net_debug > 4)
 316         printk("Ss");
 317 
 318     if (skb->len > dev->mtu) {
 319         printk("%s: packet too big, %d.\n", dev->name, (int)skb->len);
 320         return 0;
 321     }
 322 
 323     snd->skb = skb;
 324     snd->length = skb->len;
 325     snd->count = 0;
 326 
 327     cli();
 328     if (dev->interrupt == 0) {
 329         /* set timer */
 330         lp->tl.expires = 0;
 331         lp->tl.data = (unsigned long)dev;
 332         lp->tl.function = (void (*)(unsigned long))plip_send_packet;
 333         add_timer(&lp->tl);
 334         mark_bh(TIMER_BH);
 335     }
 336     snd->state = PLIP_ST_TRIGGER;
 337     sti();
 338 
 339     return 0;
 340 }
 341 
 342 /* Open/initialize the board.  This is called (in the current kernel)
 343    sometime after booting when the 'ifconfig' program is
 344    run.
 345 
 346    This routine gets exclusive access to the parallel port by allocating
 347    its IRQ line.
 348    */
 349 static int
 350 plip_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 351 {
 352     struct net_local *lp = (struct net_local *)dev->priv;
 353     struct plip_local *rcv = &lp->rcv_data;
 354 
 355     rcv->skb = alloc_skb(dev->mtu, GFP_KERNEL);
 356     if (rcv->skb == NULL) {
 357         printk("%s: couldn't get memory for receiving packet.\n", dev->name);
 358         return -EAGAIN;
 359     }
 360     rcv->skb->len = dev->mtu;
 361     rcv->skb->dev = dev;
 362     cli();
 363     if (request_irq(dev->irq , plip_interrupt, 0, "plip") != 0) {
 364         sti();
 365         printk("%s: couldn't get IRQ %d.\n", dev->name, dev->irq);
 366         return -EAGAIN;
 367     }
 368     irq2dev_map[dev->irq] = dev;
 369     sti();
 370     /* enable rx interrupt. */
 371     outb(0x10, PAR_CONTROL(dev));
 372     plip_device_clear(dev);
 373     dev->start = 1;
 374 #ifdef MODULE
 375     MOD_INC_USE_COUNT;
 376 #endif        
 377     return 0;
 378 }
 379 
 380 /* The inverse routine to plip_open (). */
 381 static int
 382 plip_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 383 {
 384     struct net_local *lp = (struct net_local *)dev->priv;
 385 
 386     dev->tbusy = 1;
 387     dev->start = 0;
 388     cli();
 389     free_irq(dev->irq);
 390     irq2dev_map[dev->irq] = NULL;
 391     sti();
 392     outb(0x00, PAR_DATA(dev));
 393     /* make sure that we don't register the timer */
 394     del_timer(&lp->tl);
 395     /* release the interrupt. */
 396     outb(0x00, PAR_CONTROL(dev));
 397 #ifdef MODULE
 398     MOD_DEC_USE_COUNT;
 399 #endif        
 400     return 0;
 401 }
 402 
 403 /* Fill in the MAC-level header. */
 404 static int
 405 plip_header(unsigned char *buff, struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 406             unsigned short type, void *daddr,
 407             void *saddr, unsigned len, struct sk_buff *skb)
 408 {
 409     int i;
 410 
 411     if (dev->dev_addr[0] == 0) {
 412         for (i=0; i < ETH_ALEN - sizeof(unsigned long); i++)
 413             dev->dev_addr[i] = 0xfc;
 414         memcpy(&(dev->dev_addr[i]), &dev->pa_addr, sizeof(unsigned long));
 415     }
 416 
 417     return eth_header(buff, dev, type, daddr, saddr, len, skb);
 418 }
 419 
 420 static struct enet_statistics *
 421 plip_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 422 {
 423     struct enet_statistics *localstats = (struct enet_statistics*)dev->priv;
 424     return localstats;
 425 }
 426 
 427 /* We don't need to send arp, for plip is point-to-point. */
 428 static int
 429 plip_rebuild_header(void *buff, struct device *dev, unsigned long dst,
     /* [previous][next][first][last][top][bottom][index][help] */
 430                     struct sk_buff *skb)
 431 {
 432     struct ethhdr *eth = (struct ethhdr *)buff;
 433     int i;
 434 
 435     if (eth->h_proto != htons(ETH_P_IP)) {
 436         printk("plip_rebuild_header: Don't know how to resolve type %d addresses?\n",(int)eth->h_proto);
 437         memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
 438         return 0;
 439     }
 440 
 441     for (i=0; i < ETH_ALEN - sizeof(unsigned long); i++)
 442         eth->h_dest[i] = 0xfc;
 443     memcpy(&(eth->h_dest[i]), &dst, sizeof(unsigned long));
 444     return 0;
 445 }
 446 
 447 static void
 448 plip_device_clear(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 449 {
 450     struct net_local *lp = (struct net_local *)dev->priv;
 451 
 452     outb (0x00, PAR_DATA(dev));
 453     lp->snd_data.state = PLIP_ST_DONE;
 454     lp->rcv_data.state = PLIP_ST_DONE;
 455     cli();
 456     dev->tbusy = 0;
 457     dev->interrupt = 0;
 458     /* make sure that we don't register the timer */
 459     del_timer(&lp->tl);
 460     sti();
 461     enable_irq(dev->irq);
 462 }
 463 
 464 static void
 465 plip_error(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 466 {
 467     struct net_local *lp = (struct net_local *)dev->priv;
 468     struct plip_local *snd = &((struct net_local *)dev->priv)->snd_data;
 469     struct plip_local *rcv = &lp->rcv_data;
 470     unsigned char status;
 471 
 472     outb(0x00, PAR_DATA(dev));
 473     cli();
 474     del_timer(&lp->tl);
 475     snd->state = PLIP_ST_ERROR;
 476     sti();
 477     if (rcv->skb == NULL) {
 478         rcv->skb = alloc_skb(dev->mtu, GFP_ATOMIC);
 479         if (rcv->skb == NULL) {
 480             printk("%s: couldn't get memory.\n", dev->name);
 481             goto again;
 482         }
 483         rcv->skb->len = dev->mtu;
 484         rcv->skb->dev = dev;
 485     }
 486 
 487     status = inb(PAR_STATUS(dev));
 488     if ((status & 0xf8) == 0x80) {
 489         plip_device_clear(dev);
 490         mark_bh(NET_BH);
 491     } else {
 492     again:
 493         lp->tl.expires = 1;
 494         lp->tl.data = (unsigned long)dev;
 495         lp->tl.function = (void (*)(unsigned long))plip_error;
 496         add_timer(&lp->tl);
 497     }
 498 }
 499 
 500 /* PLIP_RECEIVE --- receive a byte(two nibbles)
 501    Return 0 on success, return 1 on failure */
 502 static int
 503 plip_receive(struct device *dev, enum plip_nibble_state *ns_p,
     /* [previous][next][first][last][top][bottom][index][help] */
 504              unsigned char *data_p)
 505 {
 506     unsigned char c0, c1;
 507     unsigned int cx;
 508     struct net_local *nl=(struct net_local *)dev->priv;
 509 
 510     while (1)
 511         switch (*ns_p) {
 512         case PLIP_NST_BEGIN:
 513             cx = nl->nibble_us;
 514             while (1) {
 515                 c0 = inb(PAR_STATUS(dev));
 516                 udelay(PLIP_DELAY_UNIT);
 517                 if ((c0 & 0x80) == 0) {
 518                     c1 = inb(PAR_STATUS(dev));
 519                     if (c0 == c1)
 520                         break;
 521                 }
 522                 if (--cx == 0)
 523                     return 1;
 524             }
 525             *data_p = (c0 >> 3) & 0x0f;
 526             outb(0x10, PAR_DATA(dev)); /* send ACK */
 527             *ns_p = PLIP_NST_1;
 528             break;
 529 
 530         case PLIP_NST_1:
 531             cx = nl->nibble_us;
 532             while (1) {
 533                 c0 = inb(PAR_STATUS(dev));
 534                 udelay(PLIP_DELAY_UNIT);
 535                 if (c0 & 0x80) {
 536                     c1 = inb(PAR_STATUS(dev));
 537                     if (c0 == c1)
 538                         break;
 539                 }
 540                 if (--cx == 0)
 541                     return 1;
 542             }
 543             *data_p |= (c0 << 1) & 0xf0;
 544             outb(0x00, PAR_DATA(dev)); /* send ACK */
 545             *ns_p = PLIP_NST_2;
 546             return 0;
 547             break;
 548 
 549         default:
 550             printk("plip:receive state error\n");
 551             *ns_p = PLIP_NST_2;     
 552             return 1;
 553             break;
 554         }
 555 }
 556 
 557 static void
 558 plip_receive_packet(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 559 {
 560     struct net_local *lp = (struct net_local *)dev->priv;
 561     struct enet_statistics *stats = (struct enet_statistics *) dev->priv;
 562     struct plip_local *snd = &lp->snd_data;
 563     struct plip_local *rcv = &lp->rcv_data;
 564     unsigned char *lbuf = rcv->skb->data;
 565     unsigned char c0;
 566     unsigned char *s =  PLIP_STATE_STRING(rcv->state);
 567 
 568     if (net_debug > 4)
 569         printk("R%s",s);
 570 
 571     while (1) {
 572         switch (rcv->state) {
 573         case PLIP_ST_TRIGGER:
 574             disable_irq(dev->irq);
 575             rcv->state = PLIP_ST_LENGTH_LSB;
 576             rcv->nibble = PLIP_NST_BEGIN;
 577             break;
 578 
 579         case PLIP_ST_LENGTH_LSB:
 580             if (plip_receive(dev, &rcv->nibble, (unsigned char *)&rcv->length))
 581                 goto try_again;
 582 
 583             rcv->state = PLIP_ST_LENGTH_MSB;
 584             rcv->nibble = PLIP_NST_BEGIN;
 585             break;
 586 
 587         case PLIP_ST_LENGTH_MSB:
 588             if (plip_receive(dev, &rcv->nibble,
 589                              (unsigned char *)&rcv->length+1))
 590                 goto try_again;
 591 
 592             if (rcv->length > rcv->skb->len || rcv->length < 8) {
 593                 printk("%s: bogus packet size %d.\n", dev->name, rcv->length);
 594                 plip_error(dev);
 595                 return;
 596             }
 597             rcv->skb->len = rcv->length;
 598             rcv->state = PLIP_ST_DATA;
 599             rcv->nibble = PLIP_NST_BEGIN;
 600             rcv->byte = 0;
 601             rcv->checksum = 0;
 602             break;
 603 
 604         case PLIP_ST_DATA:
 605             if (plip_receive(dev, &rcv->nibble, &lbuf[rcv->byte]))
 606                 goto try_again;
 607 
 608             rcv->checksum += lbuf[rcv->byte];
 609             rcv->byte++;
 610             rcv->nibble = PLIP_NST_BEGIN;
 611             if (rcv->byte == rcv->length)
 612                 rcv->state = PLIP_ST_CHECKSUM;
 613             break;
 614 
 615         case PLIP_ST_CHECKSUM:
 616             if (plip_receive(dev, &rcv->nibble, &rcv->data))
 617                 goto try_again;
 618             if (rcv->data != rcv->checksum) {
 619                 stats->rx_crc_errors++;
 620                 if (net_debug)
 621                     printk("%s: checksum error\n", dev->name);
 622                 plip_error(dev);
 623                 return;
 624             }
 625 
 626             rcv->state = PLIP_ST_DONE;
 627             netif_rx(rcv->skb);
 628 
 629             /* Malloc up new buffer. */
 630             rcv->skb = alloc_skb(dev->mtu, GFP_ATOMIC);
 631             if (rcv->skb == NULL) {
 632                 printk("%s: Memory squeeze.\n", dev->name);
 633                 plip_error(dev);
 634                 return;
 635             }
 636             rcv->skb->len = dev->mtu;
 637             rcv->skb->dev = dev;
 638             stats->rx_packets++;
 639             if (net_debug > 4)
 640                 printk("R(%4.4d)", rcv->length);
 641 
 642             if (snd->state == PLIP_ST_TRANSMIT_BEGIN) {
 643                 dev->interrupt = 0;
 644                 enable_irq(dev->irq);
 645             } else if (snd->state == PLIP_ST_TRIGGER) {
 646                 cli();
 647                 dev->interrupt = 0;
 648                 if (net_debug > 3)
 649                     printk("%%");
 650                 lp->tl.expires = 0;
 651                 lp->tl.data = (unsigned long)dev;
 652                 lp->tl.function
 653                     = (void (*)(unsigned long))plip_send_packet;
 654                 add_timer(&lp->tl);
 655                 mark_bh(TIMER_BH);
 656                 enable_irq(dev->irq);
 657                 sti();
 658             } else
 659                 plip_device_clear(dev);
 660             return;
 661 
 662         default:
 663             printk("plip: bad STATE?? %04d", rcv->state);
 664             plip_device_clear(dev);
 665             return;
 666         }
 667     }
 668 
 669  try_again:
 670     if (++rcv->count > 2) { /* timeout */
 671         s = PLIP_STATE_STRING(rcv->state);
 672         c0 = inb(PAR_STATUS(dev));
 673         stats->rx_dropped++;
 674         if (net_debug > 1)
 675             printk("%s: receive timeout(%s,%02x)... reset interface.\n",
 676                    dev->name, s, (unsigned int)c0);
 677         plip_error(dev);
 678     } else {
 679         s =  PLIP_STATE_STRING(rcv->state);
 680         if (net_debug > 3)
 681             printk("r%s",s);
 682 
 683         /* set timer */
 684         lp->tl.expires = 1;
 685         lp->tl.data = (unsigned long)dev;
 686         lp->tl.function = (void (*)(unsigned long))plip_receive_packet;
 687         add_timer(&lp->tl);
 688     }
 689 }
 690 
 691 /* Handle the parallel port interrupts. */
 692 static void
 693 plip_interrupt(int reg_ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 694 {
 695     int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
 696     struct device *dev = (struct device *) irq2dev_map[irq];
 697     struct net_local *lp = (struct net_local *)dev->priv;
 698     struct plip_local *rcv = &lp->rcv_data;
 699     struct plip_local *snd = &lp->snd_data;
 700     unsigned char c0;
 701 
 702     if (dev == NULL) {
 703         if (net_debug)
 704             printk ("plip_interrupt: irq %d for unknown device.\n", irq);
 705         return;
 706     }
 707 
 708     if (dev->interrupt) {
 709         if (net_debug > 3)
 710             printk("2");
 711         return;
 712     }
 713 
 714     if (dev->tbusy) {
 715         if (snd->state > PLIP_ST_TRIGGER) {
 716             printk("%s: rx interrupt in transmission\n", dev->name);
 717             return;
 718         }
 719         if (net_debug > 3)
 720             printk("3");
 721     }
 722 
 723     if (snd->state == PLIP_ST_ERROR)
 724         return;
 725 
 726     c0 = inb(PAR_STATUS(dev));
 727     if ((c0 & 0xf8) != 0xc0) {
 728         if (net_debug > 3)
 729             printk("?");
 730         return;
 731     }
 732 
 733     dev->interrupt = 1;
 734 
 735     if (net_debug > 3)
 736         printk("!");
 737 
 738     dev->last_rx = jiffies;
 739     outb(0x01, PAR_DATA(dev));   /* send ACK */
 740     rcv->state = PLIP_ST_TRIGGER;
 741     rcv->count = 0;
 742 
 743     /* set timer */
 744     del_timer(&lp->tl);
 745     lp->tl.expires = 0;
 746     lp->tl.data = (unsigned long)dev;
 747     lp->tl.function = (void (*)(unsigned long))plip_receive_packet;
 748     add_timer(&lp->tl);
 749     mark_bh (TIMER_BH);
 750 }
 751 
 752 /* PLIP_SEND --- send a byte (two nibbles) 
 753    Return 0 on success, return 1 on failure */
 754 static int
 755 plip_send(struct device *dev, enum plip_nibble_state *ns_p, unsigned char data)
     /* [previous][next][first][last][top][bottom][index][help] */
 756 {
 757     unsigned char c0;
 758     unsigned int cx;
 759     struct net_local *nl= (struct net_local *)dev->priv;
 760 
 761     while (1)
 762         switch (*ns_p) {
 763         case PLIP_NST_BEGIN:
 764             outb((data & 0x0f), PAR_DATA(dev));
 765             *ns_p = PLIP_NST_1;
 766             break;
 767 
 768         case PLIP_NST_1:
 769             outb(0x10 | (data & 0x0f), PAR_DATA(dev));
 770             cx = nl->nibble_us;
 771             while (1) {
 772                 c0 = inb(PAR_STATUS(dev));
 773                 if ((c0 & 0x80) == 0) 
 774                     break;
 775                 if (--cx == 0) /* time out */
 776                     return 1;
 777                 udelay(PLIP_DELAY_UNIT);
 778             }
 779             outb(0x10 | (data >> 4), PAR_DATA(dev));
 780             *ns_p = PLIP_NST_2;
 781             break;
 782 
 783         case PLIP_NST_2:
 784             outb((data >> 4), PAR_DATA(dev));
 785             cx = nl->nibble_us;
 786             while (1) {
 787                 c0 = inb(PAR_STATUS(dev));
 788                 if (c0 & 0x80)
 789                     break;
 790                 if (--cx == 0) /* time out */
 791                     return 1;
 792                 udelay(PLIP_DELAY_UNIT);
 793             }
 794             return 0;
 795 
 796         default:
 797             printk("plip:send state error\n");
 798             return 1;
 799         }
 800 }
 801 
 802 static void
 803 plip_send_packet(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 804 {
 805     struct enet_statistics *stats = (struct enet_statistics *) dev->priv;
 806     struct net_local *lp = (struct net_local *)dev->priv;
 807     struct plip_local *snd = &lp->snd_data;
 808     unsigned char *lbuf = snd->skb->data;
 809     unsigned char c0;
 810     unsigned int cx;
 811     unsigned char *s =  PLIP_STATE_STRING(snd->state);
 812 
 813     if (net_debug > 4)
 814         printk("S%s",s);
 815 
 816     while (1) {
 817         switch (snd->state) {
 818         case PLIP_ST_TRIGGER:
 819             /* Trigger remote rx interrupt. */
 820             outb(0x08, PAR_DATA(dev));
 821             cx = lp->trigger_us;
 822             while (1) {
 823                 if (dev->interrupt) {
 824                     stats->collisions++;
 825                     if (net_debug > 3)
 826                         printk("$");
 827                     mark_bh(TIMER_BH);
 828                     return;
 829                 }
 830                 cli();
 831                 c0 = inb(PAR_STATUS(dev));
 832                 if (c0 & 0x08) {
 833                     disable_irq(dev->irq);
 834                     if (net_debug > 3)
 835                         printk("+");
 836                     /* OK, connection established! */
 837                     snd->state = PLIP_ST_LENGTH_LSB;
 838                     snd->nibble = PLIP_NST_BEGIN;
 839                     snd->count = 0;
 840                     sti();
 841                     break;
 842                 }
 843                 sti();
 844                 udelay(PLIP_DELAY_UNIT);
 845                 if (--cx == 0) {
 846                     outb(0x00, PAR_DATA(dev));
 847                     goto try_again;
 848                 }
 849             }
 850             break;
 851 
 852         case PLIP_ST_LENGTH_LSB:
 853             if (plip_send(dev, &snd->nibble, snd->length & 0xff)) /* timeout */
 854                 goto try_again;
 855 
 856             snd->state = PLIP_ST_LENGTH_MSB;
 857             snd->nibble = PLIP_NST_BEGIN;
 858             break;
 859 
 860         case PLIP_ST_LENGTH_MSB:
 861             if (plip_send(dev, &snd->nibble, snd->length >> 8)) /* timeout */
 862                 goto try_again;
 863 
 864             snd->state = PLIP_ST_DATA;
 865             snd->nibble = PLIP_NST_BEGIN;
 866             snd->byte = 0;
 867             snd->checksum = 0;
 868             break;
 869 
 870         case PLIP_ST_DATA:
 871             if (plip_send(dev, &snd->nibble, lbuf[snd->byte])) /* timeout */
 872                 goto try_again;
 873 
 874             snd->nibble = PLIP_NST_BEGIN;
 875             snd->checksum += lbuf[snd->byte];
 876             snd->byte++;
 877             if (snd->byte == snd->length)
 878                 snd->state = PLIP_ST_CHECKSUM;
 879             break;
 880 
 881         case PLIP_ST_CHECKSUM:
 882             if (plip_send(dev, &snd->nibble, snd->checksum)) /* timeout */
 883                 goto try_again;
 884 
 885             mark_bh(NET_BH);
 886             plip_device_clear(dev);
 887             if (net_debug > 4)
 888                 printk("S(%4.4d)", snd->length);
 889             dev_kfree_skb(snd->skb, FREE_WRITE);
 890             stats->tx_packets++;
 891             return;
 892 
 893         default:
 894             printk("plip: BAD STATE?? %04d", snd->state);
 895             plip_device_clear(dev);
 896             return;
 897         }
 898     }
 899 
 900  try_again:
 901     if (++snd->count > 3) {
 902         /* timeout */
 903         s =  PLIP_STATE_STRING(snd->state);
 904         c0 = inb(PAR_STATUS(dev));
 905         stats->tx_errors++;
 906         stats->tx_aborted_errors++;
 907         if (net_debug > 1)
 908             printk("%s: transmit timeout(%s,%02x)... reset interface.\n",
 909                    dev->name, s, (unsigned int)c0);
 910         dev_kfree_skb(snd->skb,FREE_WRITE);
 911         plip_error(dev);
 912     } else {
 913         s =  PLIP_STATE_STRING(snd->state);
 914         if (net_debug > 3)
 915             printk("s%s",s);
 916 
 917         cli();
 918         if (dev->interrupt == 0) {
 919             /* set timer */
 920             lp->tl.expires = 1;
 921             lp->tl.data = (unsigned long)dev;
 922             lp->tl.function = (void (*)(unsigned long))plip_send_packet;
 923             add_timer(&lp->tl);
 924         }
 925         sti();
 926     }
 927 }
 928 
 929 static int plip_config(struct device *dev, struct ifmap *map)
     /* [previous][next][first][last][top][bottom][index][help] */
 930 {
 931         if(dev->flags&IFF_UP)
 932                 return -EBUSY;
 933 /*
 934  *      We could probe this for verification, but since they told us
 935  *      to do it then they can suffer.
 936  */
 937         if(map->base_addr!= (unsigned short)-1)
 938                 dev->base_addr=map->base_addr;
 939         if(map->irq!= (unsigned char)-1)
 940                 dev->irq= map->irq;
 941         return 0;
 942 }
 943 
 944 static int plip_ioctl(struct device *dev, struct ifreq *rq)
     /* [previous][next][first][last][top][bottom][index][help] */
 945 {
 946         struct net_local *nl = (struct net_local *) dev->priv;
 947         struct plipconf *pc = (struct plipconf *) &rq->ifr_data;
 948         
 949         switch(pc->pcmd)
 950         {
 951                 case PLIP_GET_TIMEOUT:
 952                         pc->trigger=nl->trigger_us;
 953                         pc->nibble=nl->nibble_us;
 954                         pc->unit=nl->unit_us;
 955                         break;
 956                 case PLIP_SET_TIMEOUT:
 957                         nl->trigger_us=pc->trigger;
 958                         nl->nibble_us=pc->nibble;
 959                         nl->unit_us=pc->unit;
 960                         break;
 961                 default:
 962                         return -EOPNOTSUPP;
 963         }
 964         return 0;
 965 }
 966 
 967 
 968 #ifdef MODULE
 969 char kernel_version[] = UTS_RELEASE;
 970 
 971 static struct device dev_plip0 = 
 972 {
 973         "plip0" /*"plip"*/,
 974         0, 0, 0, 0,             /* memory */
 975         0x3BC, 5,               /* base, irq */
 976         0, 0, 0, NULL, plip_init 
 977 };
 978 
 979 static struct device dev_plip1 = 
 980 {
 981         "plip1" /*"plip"*/,
 982         0, 0, 0, 0,             /* memory */
 983         0x378, 7,               /* base, irq */
 984         0, 0, 0, NULL, plip_init 
 985 };
 986 
 987 static struct device dev_plip2 = 
 988 {
 989         "plip2" /*"plip"*/,
 990         0, 0, 0, 0,             /* memory */
 991         0x278, 2,               /* base, irq */
 992         0, 0, 0, NULL, plip_init 
 993 };
 994 
 995 int
 996 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 997 {
 998         int err;
 999 
1000         if ( ((err=register_netdev(&dev_plip0)) == 0) &&
1001              ((err=register_netdev(&dev_plip1)) == 0) &&
1002              ((err=register_netdev(&dev_plip2)) == 0)
1003            )
1004         {
1005                 if(err==-EEXIST)
1006                         printk("plip devices already present. Module not loaded.\n");
1007                 return err;
1008         }
1009         return 0;
1010 }
1011 
1012 void
1013 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1014 {
1015         if (MOD_IN_USE)
1016                 printk("plip: device busy, remove delayed\n");
1017         else
1018         {
1019                 unregister_netdev(&dev_plip0);
1020                 if(dev_plip0.priv)
1021                 {
1022                         kfree_s(dev_plip0.priv,sizeof(struct net_local));
1023                         dev_plip0.priv=NULL;
1024                 }
1025                 unregister_netdev(&dev_plip1);
1026                 if(dev_plip1.priv)
1027                 {
1028                         kfree_s(dev_plip1.priv,sizeof(struct net_local));
1029                         dev_plip0.priv=NULL;
1030                 }
1031                 unregister_netdev(&dev_plip2);
1032                 if(dev_plip2.priv)
1033                 {
1034                         kfree_s(dev_plip2.priv,sizeof(struct net_local));
1035                         dev_plip2.priv=NULL;
1036                 }
1037         }
1038 }
1039 #endif /* MODULE */
1040 
1041 /*
1042  * Local variables:
1043  * compile-command: "gcc -D__KERNEL__ -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -c plip.c"
1044  * c-indent-level: 4
1045  * c-continued-statement-offset: 4
1046  * c-brace-offset: -4
1047  * c-argdecl-indent: 4
1048  * c-label-offset: -4
1049  * version-control: t
1050  * kept-new-versions: 10
1051  * End:
1052  */

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