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_kick_bh
  3. plip_bh
  4. plip_bh_timeout_error
  5. plip_none
  6. plip_receive
  7. plip_receive_packet
  8. plip_send
  9. plip_send_packet
  10. plip_connection_close
  11. plip_error
  12. plip_interrupt
  13. plip_rebuild_header
  14. plip_tx_packet
  15. plip_open
  16. plip_close
  17. plip_get_stats
  18. plip_config
  19. plip_ioctl
  20. init_module
  21. cleanup_module

   1 /* $Id: plip.c,v 1.12 1995/02/11 10:26:05 gniibe Exp $ */
   2 /* PLIP: A parallel port "network" driver for Linux. */
   3 /* This driver is for parallel port with 5-bit cable (LapLink (R) cable). */
   4 /*
   5  * Authors:     Donald Becker,  <becker@super.org>
   6  *              Tommy Thorn, <thorn@daimi.aau.dk>
   7  *              Tanabe Hiroyasu, <hiro@sanpo.t.u-tokyo.ac.jp>
   8  *              Alan Cox, <gw4pts@gw4pts.ampr.org>
   9  *              Peter Bauer, <100136.3530@compuserve.com>
  10  *              Niibe Yutaka, <gniibe@mri.co.jp>
  11  *
  12  *              Modularization and ifreq/ifmap support by Alan Cox.
  13  *              Rewritten by Niibe Yutaka.
  14  *
  15  *              This program is free software; you can redistribute it and/or
  16  *              modify it under the terms of the GNU General Public License
  17  *              as published by the Free Software Foundation; either version
  18  *              2 of the License, or (at your option) any later version.
  19  */
  20 
  21 /*
  22  * Original version and the name 'PLIP' from Donald Becker <becker@super.org>
  23  * inspired by Russ Nelson's parallel port packet driver.
  24  *
  25  * NOTE:
  26  *     Tanabe Hiroyasu had changed the protocol, and it was in Linux v1.0.
  27  *     Because of the necessity to communicate to DOS machines with the
  28  *     Crynwr packet driver, Peter Bauer changed the protocol again
  29  *     back to original protocol.
  30  *
  31  *     This version follows original PLIP protocol. 
  32  *     So, this PLIP can't communicate the PLIP of Linux v1.0.
  33  */
  34 
  35 static char *version = "NET3 PLIP version 2.0 gniibe@mri.co.jp\n";
  36 
  37 /*
  38   Sources:
  39         Ideas and protocols came from Russ Nelson's <nelson@crynwr.com>
  40         "parallel.asm" parallel port packet driver.
  41 
  42   The "Crynwr" parallel port standard specifies the following protocol:
  43     Trigger by sending '0x08' (this cause interrupt on other end)
  44     count-low octet
  45     count-high octet
  46     ... data octets
  47     checksum octet
  48   Each octet is sent as <wait for rx. '0x1?'> <send 0x10+(octet&0x0F)>
  49                         <wait for rx. '0x0?'> <send 0x00+((octet>>4)&0x0F)>
  50 
  51   The packet is encapsulated as if it were ethernet.
  52 
  53   The cable used is a de facto standard parallel null cable -- sold as
  54   a "LapLink" cable by various places.  You'll need a 12-conductor cable to
  55   make one yourself.  The wiring is:
  56     SLCTIN      17 - 17
  57     GROUND      25 - 25
  58     D0->ERROR   2 - 15          15 - 2
  59     D1->SLCT    3 - 13          13 - 3
  60     D2->PAPOUT  4 - 12          12 - 4
  61     D3->ACK     5 - 10          10 - 5
  62     D4->BUSY    6 - 11          11 - 6
  63   Do not connect the other pins.  They are
  64     D5,D6,D7 are 7,8,9
  65     STROBE is 1, FEED is 14, INIT is 16
  66     extra grounds are 18,19,20,21,22,23,24
  67 */
  68 
  69 #ifdef MODULE
  70 #include <linux/module.h>
  71 #include <linux/version.h>
  72 #else
  73 #define MOD_INC_USE_COUNT
  74 #define MOD_DEC_USE_COUNT
  75 #endif
  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 <linux/in.h>
  88 #include <linux/errno.h>
  89 #include <linux/delay.h>
  90 #include <linux/lp.h>
  91 
  92 #include <linux/netdevice.h>
  93 #include <linux/etherdevice.h>
  94 #include <linux/skbuff.h>
  95 #include <linux/if_plip.h>
  96 
  97 #include <linux/tqueue.h>
  98 #include <linux/ioport.h>
  99 #include <asm/bitops.h>
 100 #include <asm/irq.h>
 101 
 102 /* Use 0 for production, 1 for verification, >2 for debug */
 103 #ifndef NET_DEBUG
 104 #define NET_DEBUG 1
 105 #endif
 106 static unsigned int net_debug = NET_DEBUG;
 107 
 108 /* In micro second */
 109 #define PLIP_DELAY_UNIT            1
 110 
 111 /* Connection time out = PLIP_TRIGGER_WAIT * PLIP_DELAY_UNIT usec */
 112 #define PLIP_TRIGGER_WAIT        500
 113 
 114 /* Nibble time out = PLIP_NIBBLE_WAIT * PLIP_DELAY_UNIT usec */
 115 #define PLIP_NIBBLE_WAIT        3000
 116 
 117 #define PAR_INTR_ON             (LP_PINITP|LP_PSELECP|LP_PINTEN)
 118 #define PAR_INTR_OFF            (LP_PINITP|LP_PSELECP)
 119 #define PAR_DATA(dev)           ((dev)->base_addr+0)
 120 #define PAR_STATUS(dev)         ((dev)->base_addr+1)
 121 #define PAR_CONTROL(dev)        ((dev)->base_addr+2)
 122 
 123 /* Bottom halfs */
 124 static void plip_kick_bh(struct device *dev);
 125 static void plip_bh(struct device *dev);
 126 
 127 /* Interrupt handler */
 128 static void plip_interrupt(int irq, struct pt_regs *regs);
 129 
 130 /* Functions for DEV methods */
 131 static int plip_rebuild_header(void *buff, struct device *dev,
 132                                unsigned long raddr, struct sk_buff *skb);
 133 static int plip_tx_packet(struct sk_buff *skb, struct device *dev);
 134 static int plip_open(struct device *dev);
 135 static int plip_close(struct device *dev);
 136 static struct enet_statistics *plip_get_stats(struct device *dev);
 137 static int plip_config(struct device *dev, struct ifmap *map);
 138 static int plip_ioctl(struct device *dev, struct ifreq *ifr, int cmd);
 139 
 140 enum plip_connection_state {
 141         PLIP_CN_NONE=0,
 142         PLIP_CN_RECEIVE,
 143         PLIP_CN_SEND,
 144         PLIP_CN_CLOSING,
 145         PLIP_CN_ERROR
 146 };
 147 
 148 enum plip_packet_state {
 149         PLIP_PK_DONE=0,
 150         PLIP_PK_TRIGGER,
 151         PLIP_PK_LENGTH_LSB,
 152         PLIP_PK_LENGTH_MSB,
 153         PLIP_PK_DATA,
 154         PLIP_PK_CHECKSUM
 155 };
 156 
 157 enum plip_nibble_state {
 158         PLIP_NB_BEGIN,
 159         PLIP_NB_1,
 160         PLIP_NB_2,
 161 };
 162 
 163 struct plip_local {
 164         enum plip_packet_state state;
 165         enum plip_nibble_state nibble;
 166         union {
 167                 struct {
 168 #if defined(__i386__)
 169                         unsigned char lsb;
 170                         unsigned char msb;
 171 #elif defined(__mc68000__)
 172                         unsigned char msb;
 173                         unsigned char lsb;
 174 #elif defined(__MIPSEL__)
 175                         unsigned char lsb;
 176                         unsigned char msb;
 177 #elif defined(__MIPSEB__)
 178                         unsigned char msb;
 179                         unsigned char lsb;
 180 #elif defined(__alpha__)
 181                         unsigned char lsb;
 182                         unsigned char msb;
 183 #else
 184 #error  "Adjust this structure to match your CPU"
 185 #endif                                          
 186                 } b;
 187                 unsigned short h;
 188         } length;
 189         unsigned short byte;
 190         unsigned char  checksum;
 191         unsigned char  data;
 192         struct sk_buff *skb;
 193 };
 194 
 195 struct net_local {
 196         struct enet_statistics enet_stats;
 197         struct tq_struct immediate;
 198         struct tq_struct deferred;
 199         struct plip_local snd_data;
 200         struct plip_local rcv_data;
 201         unsigned long  trigger;
 202         unsigned long  nibble;
 203         enum plip_connection_state connection;
 204         unsigned short timeout_count;
 205         char is_deferred;
 206         int (*orig_rebuild_header)(void *eth, struct device *dev,
 207                                    unsigned long raddr, struct sk_buff *skb);
 208 };
 209 
 210 /* Entry point of PLIP driver.
 211    Probe the hardware, and register/initialize the driver. */
 212 int
 213 plip_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 214 {
 215         struct net_local *nl;
 216 
 217         /* Check region before the probe */
 218         if (check_region(PAR_DATA(dev), 3) < 0)
 219                 return -ENODEV;
 220 
 221         /* Check that there is something at base_addr. */
 222         outb(0, PAR_DATA(dev));
 223         udelay(1000);
 224         if (inb(PAR_DATA(dev)) != 0)
 225                 return -ENODEV;
 226 
 227         printk(version);
 228         printk("%s: Parallel port at %#3lx, ", dev->name, dev->base_addr);
 229         if (dev->irq) {
 230                 printk("using assigned IRQ %d.\n", dev->irq);
 231         } else {
 232                 int irq = 0;
 233 #ifdef MODULE
 234                 /* dev->irq==0 means autoprobe, but we don't try to do so
 235                    with module.  We can change it by ifconfig */
 236 #else
 237                 unsigned int irqs = probe_irq_on();
 238 
 239                 outb(0x00, PAR_CONTROL(dev));
 240                 udelay(1000);
 241                 outb(PAR_INTR_OFF, PAR_CONTROL(dev));
 242                 outb(PAR_INTR_ON, PAR_CONTROL(dev));
 243                 outb(PAR_INTR_OFF, PAR_CONTROL(dev));
 244                 udelay(1000);
 245                 irq = probe_irq_off(irqs);
 246 #endif
 247                 if (irq > 0) {
 248                         dev->irq = irq;
 249                         printk("using probed IRQ %d.\n", dev->irq);
 250                 } else
 251                         printk("failed to detect IRQ(%d) --"
 252                                " Please set IRQ by ifconfig.\n", irq);
 253         }
 254 
 255         request_region(PAR_DATA(dev), 3, dev->name);
 256 
 257         /* Fill in the generic fields of the device structure. */
 258         ether_setup(dev);
 259 
 260         /* Then, override parts of it */
 261         dev->hard_start_xmit    = plip_tx_packet;
 262         dev->open               = plip_open;
 263         dev->stop               = plip_close;
 264         dev->get_stats          = plip_get_stats;
 265         dev->set_config         = plip_config;
 266         dev->do_ioctl           = plip_ioctl;
 267         dev->flags              = IFF_POINTOPOINT|IFF_NOARP;
 268 
 269         /* Set the private structure */
 270         dev->priv = kmalloc(sizeof (struct net_local), GFP_KERNEL);
 271         if (dev->priv == NULL)
 272                 return EAGAIN;
 273         memset(dev->priv, 0, sizeof(struct net_local));
 274         nl = (struct net_local *) dev->priv;
 275 
 276         nl->orig_rebuild_header = dev->rebuild_header;
 277         dev->rebuild_header     = plip_rebuild_header;
 278 
 279         /* Initialize constants */
 280         nl->trigger     = PLIP_TRIGGER_WAIT;
 281         nl->nibble      = PLIP_NIBBLE_WAIT;
 282 
 283         /* Initialize task queue structures */
 284         nl->immediate.next = &tq_last;
 285         nl->immediate.sync = 0;
 286         nl->immediate.routine = (void *)(void *)plip_bh;
 287         nl->immediate.data = dev;
 288 
 289         nl->deferred.next = &tq_last;
 290         nl->deferred.sync = 0;
 291         nl->deferred.routine = (void *)(void *)plip_kick_bh;
 292         nl->deferred.data = dev;
 293 
 294         return 0;
 295 }
 296 
 297 /* Bottom half handler for the delayed request.
 298    This routine is kicked by do_timer().
 299    Request `plip_bh' to be invoked. */
 300 static void
 301 plip_kick_bh(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 302 {
 303         struct net_local *nl = (struct net_local *)dev->priv;
 304 
 305         if (nl->is_deferred) {
 306                 queue_task(&nl->immediate, &tq_immediate);
 307                 mark_bh(IMMEDIATE_BH);
 308         }
 309 }
 310 
 311 /* Forward declarations of internal routines */
 312 static int plip_none(struct device *, struct net_local *,
 313                      struct plip_local *, struct plip_local *);
 314 static int plip_receive_packet(struct device *, struct net_local *,
 315                                struct plip_local *, struct plip_local *);
 316 static int plip_send_packet(struct device *, struct net_local *,
 317                             struct plip_local *, struct plip_local *);
 318 static int plip_connection_close(struct device *, struct net_local *,
 319                                  struct plip_local *, struct plip_local *);
 320 static int plip_error(struct device *, struct net_local *,
 321                       struct plip_local *, struct plip_local *);
 322 static int plip_bh_timeout_error(struct device *dev, struct net_local *nl,
 323                                  struct plip_local *snd,
 324                                  struct plip_local *rcv,
 325                                  int error);
 326 
 327 #define OK        0
 328 #define TIMEOUT   1
 329 #define ERROR     2
 330 
 331 typedef int (*plip_func)(struct device *dev, struct net_local *nl,
 332                          struct plip_local *snd, struct plip_local *rcv);
 333 
 334 static plip_func connection_state_table[] =
 335 {
 336         plip_none,
 337         plip_receive_packet,
 338         plip_send_packet,
 339         plip_connection_close,
 340         plip_error
 341 };
 342 
 343 /* Bottom half handler of PLIP. */
 344 static void
 345 plip_bh(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 346 {
 347         struct net_local *nl = (struct net_local *)dev->priv;
 348         struct plip_local *snd = &nl->snd_data;
 349         struct plip_local *rcv = &nl->rcv_data;
 350         plip_func f;
 351         int r;
 352 
 353         nl->is_deferred = 0;
 354         f = connection_state_table[nl->connection];
 355         if ((r = (*f)(dev, nl, snd, rcv)) != OK
 356             && (r = plip_bh_timeout_error(dev, nl, snd, rcv, r)) != OK) {
 357                 nl->is_deferred = 1;
 358                 queue_task(&nl->deferred, &tq_timer);
 359         }
 360 }
 361 
 362 static int
 363 plip_bh_timeout_error(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 364                       struct plip_local *snd, struct plip_local *rcv,
 365                       int error)
 366 {
 367         unsigned char c0;
 368 
 369         cli();
 370         if (nl->connection == PLIP_CN_SEND) {
 371 
 372                 if (error != ERROR) { /* Timeout */
 373                         nl->timeout_count++;
 374                         if ((snd->state == PLIP_PK_TRIGGER
 375                              && nl->timeout_count <= 10)
 376                             || nl->timeout_count <= 3) {
 377                                 sti();
 378                                 /* Try again later */
 379                                 return TIMEOUT;
 380                         }
 381                         c0 = inb(PAR_STATUS(dev));
 382                         printk("%s: transmit timeout(%d,%02x)\n",
 383                                dev->name, snd->state, c0);
 384                 }
 385                 nl->enet_stats.tx_errors++;
 386                 nl->enet_stats.tx_aborted_errors++;
 387         } else if (nl->connection == PLIP_CN_RECEIVE) {
 388                 if (rcv->state == PLIP_PK_TRIGGER) {
 389                         /* Transmission was interrupted. */
 390                         sti();
 391                         return OK;
 392                 }
 393                 if (error != ERROR) { /* Timeout */
 394                         if (++nl->timeout_count <= 3) {
 395                                 sti();
 396                                 /* Try again later */
 397                                 return TIMEOUT;
 398                         }
 399                         c0 = inb(PAR_STATUS(dev));
 400                         printk("%s: receive timeout(%d,%02x)\n",
 401                                dev->name, rcv->state, c0);
 402                 }
 403                 nl->enet_stats.rx_dropped++;
 404         }
 405         rcv->state = PLIP_PK_DONE;
 406         if (rcv->skb) {
 407                 rcv->skb->free = 1;
 408                 kfree_skb(rcv->skb, FREE_READ);
 409                 rcv->skb = NULL;
 410         }
 411         snd->state = PLIP_PK_DONE;
 412         if (snd->skb) {
 413                 dev_kfree_skb(snd->skb, FREE_WRITE);
 414                 snd->skb = NULL;
 415         }
 416         disable_irq(dev->irq);
 417         outb(PAR_INTR_OFF, PAR_CONTROL(dev));
 418         dev->tbusy = 1;
 419         nl->connection = PLIP_CN_ERROR;
 420         outb(0x00, PAR_DATA(dev));
 421         sti();
 422 
 423         return TIMEOUT;
 424 }
 425 
 426 static int
 427 plip_none(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 428           struct plip_local *snd, struct plip_local *rcv)
 429 {
 430         return OK;
 431 }
 432 
 433 /* PLIP_RECEIVE --- receive a byte(two nibbles)
 434    Returns OK on success, TIMEOUT on timeout */
 435 inline static int
 436 plip_receive(unsigned short nibble_timeout, unsigned short status_addr,
     /* [previous][next][first][last][top][bottom][index][help] */
 437              enum plip_nibble_state *ns_p, unsigned char *data_p)
 438 {
 439         unsigned char c0, c1;
 440         unsigned int cx;
 441 
 442         switch (*ns_p) {
 443         case PLIP_NB_BEGIN:
 444                 cx = nibble_timeout;
 445                 while (1) {
 446                         c0 = inb(status_addr);
 447                         udelay(PLIP_DELAY_UNIT);
 448                         if ((c0 & 0x80) == 0) {
 449                                 c1 = inb(status_addr);
 450                                 if (c0 == c1)
 451                                         break;
 452                         }
 453                         if (--cx == 0)
 454                                 return TIMEOUT;
 455                 }
 456                 *data_p = (c0 >> 3) & 0x0f;
 457                 outb(0x10, --status_addr); /* send ACK */
 458                 status_addr++;
 459                 *ns_p = PLIP_NB_1;
 460 
 461         case PLIP_NB_1:
 462                 cx = nibble_timeout;
 463                 while (1) {
 464                         c0 = inb(status_addr);
 465                         udelay(PLIP_DELAY_UNIT);
 466                         if (c0 & 0x80) {
 467                                 c1 = inb(status_addr);
 468                                 if (c0 == c1)
 469                                         break;
 470                         }
 471                         if (--cx == 0)
 472                                 return TIMEOUT;
 473                 }
 474                 *data_p |= (c0 << 1) & 0xf0;
 475                 outb(0x00, --status_addr); /* send ACK */
 476                 status_addr++;
 477                 *ns_p = PLIP_NB_BEGIN;
 478                 return OK;
 479 
 480         case PLIP_NB_2:
 481         }
 482 }
 483 
 484 /* PLIP_RECEIVE_PACKET --- receive a packet */
 485 static int
 486 plip_receive_packet(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 487                     struct plip_local *snd, struct plip_local *rcv)
 488 {
 489         unsigned short status_addr = PAR_STATUS(dev);
 490         unsigned short nibble_timeout = nl->nibble;
 491         unsigned char *lbuf;
 492 
 493         switch (rcv->state) {
 494         case PLIP_PK_TRIGGER:
 495                 disable_irq(dev->irq);
 496                 outb(PAR_INTR_OFF, PAR_CONTROL(dev));
 497                 dev->interrupt = 0;
 498                 outb(0x01, PAR_DATA(dev)); /* send ACK */
 499                 if (net_debug > 2)
 500                         printk("%s: receive start\n", dev->name);
 501                 rcv->state = PLIP_PK_LENGTH_LSB;
 502                 rcv->nibble = PLIP_NB_BEGIN;
 503 
 504         case PLIP_PK_LENGTH_LSB:
 505                 if (snd->state != PLIP_PK_DONE) {
 506                         if (plip_receive(nl->trigger, status_addr,
 507                                          &rcv->nibble, &rcv->length.b.lsb)) {
 508                                 /* collision, here dev->tbusy == 1 */
 509                                 rcv->state = PLIP_PK_DONE;
 510                                 nl->is_deferred = 1;
 511                                 nl->connection = PLIP_CN_SEND;
 512                                 queue_task(&nl->deferred, &tq_timer);
 513                                 outb(PAR_INTR_ON, PAR_CONTROL(dev));
 514                                 enable_irq(dev->irq);
 515                                 return OK;
 516                         }
 517                 } else {
 518                         if (plip_receive(nibble_timeout, status_addr,
 519                                          &rcv->nibble, &rcv->length.b.lsb))
 520                                 return TIMEOUT;
 521                 }
 522                 rcv->state = PLIP_PK_LENGTH_MSB;
 523 
 524         case PLIP_PK_LENGTH_MSB:
 525                 if (plip_receive(nibble_timeout, status_addr,
 526                                  &rcv->nibble, &rcv->length.b.msb))
 527                         return TIMEOUT;
 528                 if (rcv->length.h > dev->mtu || rcv->length.h < 8) {
 529                         printk("%s: bogus packet size %d.\n", dev->name, rcv->length.h);
 530                         return ERROR;
 531                 }
 532                 /* Malloc up new buffer. */
 533                 rcv->skb = alloc_skb(rcv->length.h, GFP_ATOMIC);
 534                 if (rcv->skb == NULL) {
 535                         printk("%s: Memory squeeze.\n", dev->name);
 536                         return ERROR;
 537                 }
 538                 rcv->skb->len = rcv->length.h;
 539                 rcv->skb->dev = dev;
 540                 rcv->state = PLIP_PK_DATA;
 541                 rcv->byte = 0;
 542                 rcv->checksum = 0;
 543 
 544         case PLIP_PK_DATA:
 545                 lbuf = rcv->skb->data;
 546                 do
 547                         if (plip_receive(nibble_timeout, status_addr, 
 548                                          &rcv->nibble, &lbuf[rcv->byte]))
 549                                 return TIMEOUT;
 550                 while (++rcv->byte < rcv->length.h);
 551                 do
 552                         rcv->checksum += lbuf[--rcv->byte];
 553                 while (rcv->byte);
 554                 rcv->state = PLIP_PK_CHECKSUM;
 555 
 556         case PLIP_PK_CHECKSUM:
 557                 if (plip_receive(nibble_timeout, status_addr,
 558                                  &rcv->nibble, &rcv->data))
 559                         return TIMEOUT;
 560                 if (rcv->data != rcv->checksum) {
 561                         nl->enet_stats.rx_crc_errors++;
 562                         if (net_debug)
 563                                 printk("%s: checksum error\n", dev->name);
 564                         return ERROR;
 565                 }
 566                 rcv->state = PLIP_PK_DONE;
 567 
 568         case PLIP_PK_DONE:
 569                 /* Inform the upper layer for the arrival of a packet. */
 570                 netif_rx(rcv->skb);
 571                 nl->enet_stats.rx_packets++;
 572                 rcv->skb = NULL;
 573                 if (net_debug > 2)
 574                         printk("%s: receive end\n", dev->name);
 575 
 576                 /* Close the connection. */
 577                 outb (0x00, PAR_DATA(dev));
 578                 cli();
 579                 if (snd->state != PLIP_PK_DONE) {
 580                         nl->connection = PLIP_CN_SEND;
 581                         sti();
 582                         queue_task(&nl->immediate, &tq_immediate);
 583                         outb(PAR_INTR_ON, PAR_CONTROL(dev));
 584                         enable_irq(dev->irq);
 585                         return OK;
 586                 } else {
 587                         nl->connection = PLIP_CN_NONE;
 588                         sti();
 589                         outb(PAR_INTR_ON, PAR_CONTROL(dev));
 590                         enable_irq(dev->irq);
 591                         return OK;
 592                 }
 593         }
 594         return OK;
 595 }
 596 
 597 /* PLIP_SEND --- send a byte (two nibbles) 
 598    Returns OK on success, TIMEOUT when timeout    */
 599 inline static int
 600 plip_send(unsigned short nibble_timeout, unsigned short data_addr,
     /* [previous][next][first][last][top][bottom][index][help] */
 601           enum plip_nibble_state *ns_p, unsigned char data)
 602 {
 603         unsigned char c0;
 604         unsigned int cx;
 605 
 606         switch (*ns_p) {
 607         case PLIP_NB_BEGIN:
 608                 outb((data & 0x0f), data_addr);
 609                 *ns_p = PLIP_NB_1;
 610 
 611         case PLIP_NB_1:
 612                 outb(0x10 | (data & 0x0f), data_addr);
 613                 cx = nibble_timeout;
 614                 data_addr++;
 615                 while (1) {
 616                         c0 = inb(data_addr);
 617                         if ((c0 & 0x80) == 0) 
 618                                 break;
 619                         if (--cx == 0)
 620                                 return TIMEOUT;
 621                         udelay(PLIP_DELAY_UNIT);
 622                 }
 623                 outb(0x10 | (data >> 4), --data_addr);
 624                 *ns_p = PLIP_NB_2;
 625 
 626         case PLIP_NB_2:
 627                 outb((data >> 4), data_addr);
 628                 data_addr++;
 629                 cx = nibble_timeout;
 630                 while (1) {
 631                         c0 = inb(data_addr);
 632                         if (c0 & 0x80)
 633                                 break;
 634                         if (--cx == 0)
 635                                 return TIMEOUT;
 636                         udelay(PLIP_DELAY_UNIT);
 637                 }
 638                 data_addr--;
 639                 *ns_p = PLIP_NB_BEGIN;
 640                 return OK;
 641         }
 642 }
 643 
 644 /* PLIP_SEND_PACKET --- send a packet */
 645 static int
 646 plip_send_packet(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 647                  struct plip_local *snd, struct plip_local *rcv)
 648 {
 649         unsigned short data_addr = PAR_DATA(dev);
 650         unsigned short nibble_timeout = nl->nibble;
 651         unsigned char *lbuf;
 652         unsigned char c0;
 653         unsigned int cx;
 654 
 655         if (snd->skb == NULL || (lbuf = snd->skb->data) == NULL) {
 656                 printk("%s: send skb lost\n", dev->name);
 657                 snd->state = PLIP_PK_DONE;
 658                 snd->skb = NULL;
 659                 return ERROR;
 660         }
 661 
 662         switch (snd->state) {
 663         case PLIP_PK_TRIGGER:
 664                 /* Trigger remote rx interrupt. */
 665                 outb(0x08, data_addr);
 666                 cx = nl->trigger;
 667                 while (1) {
 668                         udelay(PLIP_DELAY_UNIT);
 669                         cli();
 670                         if (nl->connection == PLIP_CN_RECEIVE) {
 671                                 sti();
 672                                 /* interrupted */
 673                                 nl->enet_stats.collisions++;
 674                                 if (net_debug > 1)
 675                                         printk("%s: collision.\n", dev->name);
 676                                 return OK;
 677                         }
 678                         c0 = inb(PAR_STATUS(dev));
 679                         if (c0 & 0x08) {
 680                                 disable_irq(dev->irq);
 681                                 outb(PAR_INTR_OFF, PAR_CONTROL(dev));
 682                                 if (net_debug > 2)
 683                                         printk("%s: send start\n", dev->name);
 684                                 snd->state = PLIP_PK_LENGTH_LSB;
 685                                 snd->nibble = PLIP_NB_BEGIN;
 686                                 nl->timeout_count = 0;
 687                                 sti();
 688                                 break;
 689                         }
 690                         sti();
 691                         if (--cx == 0) {
 692                                 outb(0x00, data_addr);
 693                                 return TIMEOUT;
 694                         }
 695                 }
 696 
 697         case PLIP_PK_LENGTH_LSB:
 698                 if (plip_send(nibble_timeout, data_addr,
 699                               &snd->nibble, snd->length.b.lsb))
 700                         return TIMEOUT;
 701                 snd->state = PLIP_PK_LENGTH_MSB;
 702 
 703         case PLIP_PK_LENGTH_MSB:
 704                 if (plip_send(nibble_timeout, data_addr,
 705                               &snd->nibble, snd->length.b.msb))
 706                         return TIMEOUT;
 707                 snd->state = PLIP_PK_DATA;
 708                 snd->byte = 0;
 709                 snd->checksum = 0;
 710 
 711         case PLIP_PK_DATA:
 712                 do
 713                         if (plip_send(nibble_timeout, data_addr,
 714                                       &snd->nibble, lbuf[snd->byte]))
 715                                 return TIMEOUT;
 716                 while (++snd->byte < snd->length.h);
 717                 do
 718                         snd->checksum += lbuf[--snd->byte];
 719                 while (snd->byte);
 720                 snd->state = PLIP_PK_CHECKSUM;
 721 
 722         case PLIP_PK_CHECKSUM:
 723                 if (plip_send(nibble_timeout, data_addr,
 724                               &snd->nibble, snd->checksum))
 725                         return TIMEOUT;
 726 
 727                 dev_kfree_skb(snd->skb, FREE_WRITE);
 728                 nl->enet_stats.tx_packets++;
 729                 snd->state = PLIP_PK_DONE;
 730 
 731         case PLIP_PK_DONE:
 732                 /* Close the connection */
 733                 outb (0x00, data_addr);
 734                 snd->skb = NULL;
 735                 if (net_debug > 2)
 736                         printk("%s: send end\n", dev->name);
 737                 nl->connection = PLIP_CN_CLOSING;
 738                 nl->is_deferred = 1;
 739                 queue_task(&nl->deferred, &tq_timer);
 740                 outb(PAR_INTR_ON, PAR_CONTROL(dev));
 741                 enable_irq(dev->irq);
 742                 return OK;
 743         }
 744         return OK;
 745 }
 746 
 747 static int
 748 plip_connection_close(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 749                       struct plip_local *snd, struct plip_local *rcv)
 750 {
 751         cli();
 752         if (nl->connection == PLIP_CN_CLOSING) {
 753                 nl->connection = PLIP_CN_NONE;
 754                 dev->tbusy = 0;
 755                 mark_bh(NET_BH);
 756         }
 757         sti();
 758         return OK;
 759 }
 760 
 761 /* PLIP_ERROR --- wait till other end settled */
 762 static int
 763 plip_error(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 764            struct plip_local *snd, struct plip_local *rcv)
 765 {
 766         unsigned char status;
 767 
 768         status = inb(PAR_STATUS(dev));
 769         if ((status & 0xf8) == 0x80) {
 770                 if (net_debug > 2)
 771                         printk("%s: reset interface.\n", dev->name);
 772                 nl->connection = PLIP_CN_NONE;
 773                 dev->tbusy = 0;
 774                 dev->interrupt = 0;
 775                 outb(PAR_INTR_ON, PAR_CONTROL(dev));
 776                 enable_irq(dev->irq);
 777                 mark_bh(NET_BH);
 778         } else {
 779                 nl->is_deferred = 1;
 780                 queue_task(&nl->deferred, &tq_timer);
 781         }
 782 
 783         return OK;
 784 }
 785 
 786 /* Handle the parallel port interrupts. */
 787 static void
 788 plip_interrupt(int irq, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 789 {
 790         struct device *dev = (struct device *) irq2dev_map[irq];
 791         struct net_local *nl = (struct net_local *)dev->priv;
 792         struct plip_local *rcv = &nl->rcv_data;
 793         unsigned char c0;
 794 
 795         if (dev == NULL) {
 796                 printk ("plip_interrupt: irq %d for unknown device.\n", irq);
 797                 return;
 798         }
 799 
 800         if (dev->interrupt)
 801                 return;
 802 
 803         c0 = inb(PAR_STATUS(dev));
 804         if ((c0 & 0xf8) != 0xc0) {
 805                 if (net_debug > 1)
 806                         printk("%s: spurious interrupt\n", dev->name);
 807                 return;
 808         }
 809         dev->interrupt = 1;
 810         if (net_debug > 3)
 811                 printk("%s: interrupt.\n", dev->name);
 812 
 813         cli();
 814         switch (nl->connection) {
 815         case PLIP_CN_CLOSING:
 816                 dev->tbusy = 0;
 817         case PLIP_CN_NONE:
 818         case PLIP_CN_SEND:
 819                 dev->last_rx = jiffies;
 820                 rcv->state = PLIP_PK_TRIGGER;
 821                 nl->connection = PLIP_CN_RECEIVE;
 822                 nl->timeout_count = 0;
 823                 queue_task(&nl->immediate, &tq_immediate);
 824                 mark_bh(IMMEDIATE_BH);
 825                 sti();
 826                 break;
 827 
 828         case PLIP_CN_RECEIVE:
 829                 sti();
 830                 printk("%s: receive interrupt when receiving packet\n", dev->name);
 831                 break;
 832 
 833         case PLIP_CN_ERROR:
 834                 sti();
 835                 printk("%s: receive interrupt in error state\n", dev->name);
 836                 break;
 837         }
 838 }
 839 
 840 /* We don't need to send arp, for plip is point-to-point. */
 841 static int
 842 plip_rebuild_header(void *buff, struct device *dev, unsigned long dst,
     /* [previous][next][first][last][top][bottom][index][help] */
 843                     struct sk_buff *skb)
 844 {
 845         struct net_local *nl = (struct net_local *)dev->priv;
 846         struct ethhdr *eth = (struct ethhdr *)buff;
 847         int i;
 848 
 849         if ((dev->flags & IFF_NOARP)==0)
 850                 return nl->orig_rebuild_header(buff, dev, dst, skb);
 851 
 852         if (eth->h_proto != htons(ETH_P_IP)) {
 853                 printk("plip_rebuild_header: Don't know how to resolve type %d addresses?\n", (int)eth->h_proto);
 854                 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
 855                 return 0;
 856         }
 857 
 858         for (i=0; i < ETH_ALEN - sizeof(unsigned long); i++)
 859                 eth->h_dest[i] = 0xfc;
 860         memcpy(&(eth->h_dest[i]), &dst, sizeof(unsigned long));
 861         return 0;
 862 }
 863 
 864 static int
 865 plip_tx_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 866 {
 867         struct net_local *nl = (struct net_local *)dev->priv;
 868         struct plip_local *snd = &nl->snd_data;
 869 
 870         if (dev->tbusy)
 871                 return 1;
 872 
 873         /* If some higher layer thinks we've missed an tx-done interrupt
 874            we are passed NULL. Caution: dev_tint() handles the cli()/sti()
 875            itself. */
 876         if (skb == NULL) {
 877                 dev_tint(dev);
 878                 return 0;
 879         }
 880 
 881         if (set_bit(0, (void*)&dev->tbusy) != 0) {
 882                 printk("%s: Transmitter access conflict.\n", dev->name);
 883                 return 1;
 884         }
 885 
 886         if (skb->len > dev->mtu) {
 887                 printk("%s: packet too big, %d.\n", dev->name, (int)skb->len);
 888                 dev->tbusy = 0;
 889                 return 0;
 890         }
 891 
 892         if (net_debug > 2)
 893                 printk("%s: send request\n", dev->name);
 894 
 895         cli();
 896         dev->trans_start = jiffies;
 897         snd->skb = skb;
 898         snd->length.h = skb->len;
 899         snd->state = PLIP_PK_TRIGGER;
 900         if (nl->connection == PLIP_CN_NONE) {
 901                 nl->connection = PLIP_CN_SEND;
 902                 nl->timeout_count = 0;
 903         }
 904         queue_task(&nl->immediate, &tq_immediate);
 905         mark_bh(IMMEDIATE_BH);
 906         sti();
 907 
 908         return 0;
 909 }
 910 
 911 /* Open/initialize the board.  This is called (in the current kernel)
 912    sometime after booting when the 'ifconfig' program is run.
 913 
 914    This routine gets exclusive access to the parallel port by allocating
 915    its IRQ line.
 916  */
 917 static int
 918 plip_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 919 {
 920         struct net_local *nl = (struct net_local *)dev->priv;
 921         int i;
 922 
 923         if (dev->irq == 0) {
 924                 printk("%s: IRQ is not set.  Please set it by ifconfig.\n", dev->name);
 925                 return -EAGAIN;
 926         }
 927         cli();
 928         if (request_irq(dev->irq , plip_interrupt, 0, dev->name) != 0) {
 929                 sti();
 930                 printk("%s: couldn't get IRQ %d.\n", dev->name, dev->irq);
 931                 return -EAGAIN;
 932         }
 933         irq2dev_map[dev->irq] = dev;
 934         sti();
 935 
 936         /* Clear the data port. */
 937         outb (0x00, PAR_DATA(dev));
 938 
 939         /* Enable rx interrupt. */
 940         outb(PAR_INTR_ON, PAR_CONTROL(dev));
 941 
 942         /* Initialize the state machine. */
 943         nl->rcv_data.state = nl->snd_data.state = PLIP_PK_DONE;
 944         nl->rcv_data.skb = nl->snd_data.skb = NULL;
 945         nl->connection = PLIP_CN_NONE;
 946         nl->is_deferred = 0;
 947 
 948         /* Fill in the MAC-level header. */
 949         for (i=0; i < ETH_ALEN - sizeof(unsigned long); i++)
 950                 dev->dev_addr[i] = 0xfc;
 951         memcpy(&(dev->dev_addr[i]), &dev->pa_addr, sizeof(unsigned long));
 952 
 953         dev->interrupt = 0;
 954         dev->start = 1;
 955         dev->tbusy = 0;
 956         MOD_INC_USE_COUNT;
 957         return 0;
 958 }
 959 
 960 /* The inverse routine to plip_open (). */
 961 static int
 962 plip_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 963 {
 964         struct net_local *nl = (struct net_local *)dev->priv;
 965         struct plip_local *snd = &nl->snd_data;
 966         struct plip_local *rcv = &nl->rcv_data;
 967 
 968         dev->tbusy = 1;
 969         dev->start = 0;
 970         cli();
 971         free_irq(dev->irq);
 972         irq2dev_map[dev->irq] = NULL;
 973         nl->is_deferred = 0;
 974         nl->connection = PLIP_CN_NONE;
 975         sti();
 976         outb(0x00, PAR_DATA(dev));
 977 
 978         snd->state = PLIP_PK_DONE;
 979         if (snd->skb) {
 980                 dev_kfree_skb(snd->skb, FREE_WRITE);
 981                 snd->skb = NULL;
 982         }
 983         rcv->state = PLIP_PK_DONE;
 984         if (rcv->skb) {
 985                 rcv->skb->free = 1;
 986                 kfree_skb(rcv->skb, FREE_READ);
 987                 rcv->skb = NULL;
 988         }
 989 
 990         /* Reset. */
 991         outb(0x00, PAR_CONTROL(dev));
 992         MOD_DEC_USE_COUNT;
 993         return 0;
 994 }
 995 
 996 static struct enet_statistics *
 997 plip_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 998 {
 999         struct net_local *nl = (struct net_local *)dev->priv;
1000         struct enet_statistics *r = &nl->enet_stats;
1001 
1002         return r;
1003 }
1004 
1005 static int
1006 plip_config(struct device *dev, struct ifmap *map)
     /* [previous][next][first][last][top][bottom][index][help] */
1007 {
1008         if (dev->flags & IFF_UP)
1009                 return -EBUSY;
1010 
1011         if (map->base_addr != (unsigned long)-1
1012             && map->base_addr != dev->base_addr)
1013                 printk("%s: You cannot change base_addr of this interface (ignored).\n", dev->name);
1014 
1015         if (map->irq != (unsigned char)-1)
1016                 dev->irq = map->irq;
1017         return 0;
1018 }
1019 
1020 static int
1021 plip_ioctl(struct device *dev, struct ifreq *rq, int cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1022 {
1023         struct net_local *nl = (struct net_local *) dev->priv;
1024         struct plipconf *pc = (struct plipconf *) &rq->ifr_data;
1025         
1026         switch(pc->pcmd) {
1027         case PLIP_GET_TIMEOUT:
1028                 pc->trigger = nl->trigger;
1029                 pc->nibble  = nl->nibble;
1030                 break;
1031         case PLIP_SET_TIMEOUT:
1032                 nl->trigger = pc->trigger;
1033                 nl->nibble  = pc->nibble;
1034                 break;
1035         default:
1036                 return -EOPNOTSUPP;
1037         }
1038         return 0;
1039 }
1040 
1041 #ifdef MODULE
1042 char kernel_version[] = UTS_RELEASE;
1043 
1044 static struct device dev_plip0 = 
1045 {
1046         "plip0" /*"plip"*/,
1047         0, 0, 0, 0,             /* memory */
1048         0x3BC, 5,               /* base, irq */
1049         0, 0, 0, NULL, plip_init 
1050 };
1051 
1052 static struct device dev_plip1 = 
1053 {
1054         "plip1" /*"plip"*/,
1055         0, 0, 0, 0,             /* memory */
1056         0x378, 7,               /* base, irq */
1057         0, 0, 0, NULL, plip_init 
1058 };
1059 
1060 static struct device dev_plip2 = 
1061 {
1062         "plip2" /*"plip"*/,
1063         0, 0, 0, 0,             /* memory */
1064         0x278, 2,               /* base, irq */
1065         0, 0, 0, NULL, plip_init 
1066 };
1067 
1068 int
1069 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1070 {
1071         int devices=0;
1072 
1073         if (register_netdev(&dev_plip0) != 0)
1074                 devices++;
1075         if (register_netdev(&dev_plip1) != 0)
1076                 devices++;
1077         if (register_netdev(&dev_plip2) != 0)
1078                 devices++;
1079         if (devices == 0)
1080                 return -EIO;
1081         return 0;
1082 }
1083 
1084 void
1085 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1086 {
1087         if (dev_plip0.priv) {
1088                 unregister_netdev(&dev_plip0);
1089                 release_region(PAR_DATA(&dev_plip0), 3);
1090                 kfree_s(dev_plip0.priv, sizeof(struct net_local));
1091                 dev_plip0.priv = NULL;
1092         }
1093         if (dev_plip1.priv) {
1094                 unregister_netdev(&dev_plip1);
1095                 release_region(PAR_DATA(&dev_plip1), 3);
1096                 kfree_s(dev_plip1.priv, sizeof(struct net_local));
1097                 dev_plip1.priv = NULL;
1098         }
1099         if (dev_plip2.priv) {
1100                 unregister_netdev(&dev_plip2);
1101                 release_region(PAR_DATA(&dev_plip2), 3);
1102                 kfree_s(dev_plip2.priv, sizeof(struct net_local));
1103                 dev_plip2.priv = NULL;
1104         }
1105 }
1106 #endif /* MODULE */
1107 
1108 /*
1109  * Local variables:
1110  * compile-command: "gcc -DMODULE -DCONFIG_MODVERSIONS -D__KERNEL__ -Wall -Wstrict-prototypes -O2 -g -fomit-frame-pointer -pipe -m486 -c plip.c"
1111  * End:
1112  */

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