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

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