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

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