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.15 1995/10/03 01:47: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  *
  30  *              This program is free software; you can redistribute it and/or
  31  *              modify it under the terms of the GNU General Public License
  32  *              as published by the Free Software Foundation; either version
  33  *              2 of the License, or (at your option) any later version.
  34  */
  35 
  36 /*
  37  * Original version and the name 'PLIP' from Donald Becker <becker@super.org>
  38  * inspired by Russ Nelson's parallel port packet driver.
  39  *
  40  * NOTE:
  41  *     Tanabe Hiroyasu had changed the protocol, and it was in Linux v1.0.
  42  *     Because of the necessity to communicate to DOS machines with the
  43  *     Crynwr packet driver, Peter Bauer changed the protocol again
  44  *     back to original protocol.
  45  *
  46  *     This version follows original PLIP protocol. 
  47  *     So, this PLIP can't communicate the PLIP of Linux v1.0.
  48  */
  49 
  50 /*
  51  *     To use with DOS box, please do (Turn on ARP switch):
  52  *      # ifconfig plip[0-2] arp
  53  */
  54 static const char *version = "NET3 PLIP version 2.1 gniibe@mri.co.jp\n";
  55 
  56 /*
  57   Sources:
  58         Ideas and protocols came from Russ Nelson's <nelson@crynwr.com>
  59         "parallel.asm" parallel port packet driver.
  60 
  61   The "Crynwr" parallel port standard specifies the following protocol:
  62     Trigger by sending '0x08' (this cause interrupt on other end)
  63     count-low octet
  64     count-high octet
  65     ... data octets
  66     checksum octet
  67   Each octet is sent as <wait for rx. '0x1?'> <send 0x10+(octet&0x0F)>
  68                         <wait for rx. '0x0?'> <send 0x00+((octet>>4)&0x0F)>
  69 
  70   The packet is encapsulated as if it were ethernet.
  71 
  72   The cable used is a de facto standard parallel null cable -- sold as
  73   a "LapLink" cable by various places.  You'll need a 12-conductor cable to
  74   make one yourself.  The wiring is:
  75     SLCTIN      17 - 17
  76     GROUND      25 - 25
  77     D0->ERROR   2 - 15          15 - 2
  78     D1->SLCT    3 - 13          13 - 3
  79     D2->PAPOUT  4 - 12          12 - 4
  80     D3->ACK     5 - 10          10 - 5
  81     D4->BUSY    6 - 11          11 - 6
  82   Do not connect the other pins.  They are
  83     D5,D6,D7 are 7,8,9
  84     STROBE is 1, FEED is 14, INIT is 16
  85     extra grounds are 18,19,20,21,22,23,24
  86 */
  87 
  88 #include <linux/module.h>
  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 + dev->hard_header_len
 538                     || rcv->length.h < 8) {
 539                         printk("%s: bogus packet size %d.\n", dev->name, rcv->length.h);
 540                         return ERROR;
 541                 }
 542                 /* Malloc up new buffer. */
 543                 rcv->skb = dev_alloc_skb(rcv->length.h);
 544                 if (rcv->skb == NULL) {
 545                         printk("%s: Memory squeeze.\n", dev->name);
 546                         return ERROR;
 547                 }
 548                 skb_put(rcv->skb,rcv->length.h);
 549                 rcv->skb->dev = dev;
 550                 rcv->state = PLIP_PK_DATA;
 551                 rcv->byte = 0;
 552                 rcv->checksum = 0;
 553 
 554         case PLIP_PK_DATA:
 555                 lbuf = rcv->skb->data;
 556                 do
 557                         if (plip_receive(nibble_timeout, status_addr, 
 558                                          &rcv->nibble, &lbuf[rcv->byte]))
 559                                 return TIMEOUT;
 560                 while (++rcv->byte < rcv->length.h);
 561                 do
 562                         rcv->checksum += lbuf[--rcv->byte];
 563                 while (rcv->byte);
 564                 rcv->state = PLIP_PK_CHECKSUM;
 565 
 566         case PLIP_PK_CHECKSUM:
 567                 if (plip_receive(nibble_timeout, status_addr,
 568                                  &rcv->nibble, &rcv->data))
 569                         return TIMEOUT;
 570                 if (rcv->data != rcv->checksum) {
 571                         nl->enet_stats.rx_crc_errors++;
 572                         if (net_debug)
 573                                 printk("%s: checksum error\n", dev->name);
 574                         return ERROR;
 575                 }
 576                 rcv->state = PLIP_PK_DONE;
 577 
 578         case PLIP_PK_DONE:
 579                 /* Inform the upper layer for the arrival of a packet. */
 580                 rcv->skb->protocol=eth_type_trans(rcv->skb, dev);
 581                 netif_rx(rcv->skb);
 582                 nl->enet_stats.rx_packets++;
 583                 rcv->skb = NULL;
 584                 if (net_debug > 2)
 585                         printk("%s: receive end\n", dev->name);
 586 
 587                 /* Close the connection. */
 588                 outb (0x00, PAR_DATA(dev));
 589                 cli();
 590                 if (snd->state != PLIP_PK_DONE) {
 591                         nl->connection = PLIP_CN_SEND;
 592                         sti();
 593                         queue_task(&nl->immediate, &tq_immediate);
 594                         outb(PAR_INTR_ON, PAR_CONTROL(dev));
 595                         enable_irq(dev->irq);
 596                         return OK;
 597                 } else {
 598                         nl->connection = PLIP_CN_NONE;
 599                         sti();
 600                         outb(PAR_INTR_ON, PAR_CONTROL(dev));
 601                         enable_irq(dev->irq);
 602                         return OK;
 603                 }
 604         }
 605         return OK;
 606 }
 607 
 608 /* PLIP_SEND --- send a byte (two nibbles) 
 609    Returns OK on success, TIMEOUT when timeout    */
 610 inline static int
 611 plip_send(unsigned short nibble_timeout, unsigned short data_addr,
     /* [previous][next][first][last][top][bottom][index][help] */
 612           enum plip_nibble_state *ns_p, unsigned char data)
 613 {
 614         unsigned char c0;
 615         unsigned int cx;
 616 
 617         switch (*ns_p) {
 618         case PLIP_NB_BEGIN:
 619                 outb((data & 0x0f), data_addr);
 620                 *ns_p = PLIP_NB_1;
 621 
 622         case PLIP_NB_1:
 623                 outb(0x10 | (data & 0x0f), data_addr);
 624                 cx = nibble_timeout;
 625                 data_addr++;
 626                 while (1) {
 627                         c0 = inb(data_addr);
 628                         if ((c0 & 0x80) == 0) 
 629                                 break;
 630                         if (--cx == 0)
 631                                 return TIMEOUT;
 632                         udelay(PLIP_DELAY_UNIT);
 633                 }
 634                 outb(0x10 | (data >> 4), --data_addr);
 635                 *ns_p = PLIP_NB_2;
 636 
 637         case PLIP_NB_2:
 638                 outb((data >> 4), data_addr);
 639                 data_addr++;
 640                 cx = nibble_timeout;
 641                 while (1) {
 642                         c0 = inb(data_addr);
 643                         if (c0 & 0x80)
 644                                 break;
 645                         if (--cx == 0)
 646                                 return TIMEOUT;
 647                         udelay(PLIP_DELAY_UNIT);
 648                 }
 649                 data_addr--;
 650                 *ns_p = PLIP_NB_BEGIN;
 651                 return OK;
 652         }
 653         return OK;
 654 }
 655 
 656 /* PLIP_SEND_PACKET --- send a packet */
 657 static int
 658 plip_send_packet(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 659                  struct plip_local *snd, struct plip_local *rcv)
 660 {
 661         unsigned short data_addr = PAR_DATA(dev);
 662         unsigned short nibble_timeout = nl->nibble;
 663         unsigned char *lbuf;
 664         unsigned char c0;
 665         unsigned int cx;
 666 
 667         if (snd->skb == NULL || (lbuf = snd->skb->data) == NULL) {
 668                 printk("%s: send skb lost\n", dev->name);
 669                 snd->state = PLIP_PK_DONE;
 670                 snd->skb = NULL;
 671                 return ERROR;
 672         }
 673 
 674         switch (snd->state) {
 675         case PLIP_PK_TRIGGER:
 676                 if ((inb(PAR_STATUS(dev)) & 0xf8) != 0x80)
 677                         return TIMEOUT;
 678 
 679                 /* Trigger remote rx interrupt. */
 680                 outb(0x08, data_addr);
 681                 cx = nl->trigger;
 682                 while (1) {
 683                         udelay(PLIP_DELAY_UNIT);
 684                         cli();
 685                         if (nl->connection == PLIP_CN_RECEIVE) {
 686                                 sti();
 687                                 /* interrupted */
 688                                 nl->enet_stats.collisions++;
 689                                 if (net_debug > 1)
 690                                         printk("%s: collision.\n", dev->name);
 691                                 return OK;
 692                         }
 693                         c0 = inb(PAR_STATUS(dev));
 694                         if (c0 & 0x08) {
 695                                 disable_irq(dev->irq);
 696                                 outb(PAR_INTR_OFF, PAR_CONTROL(dev));
 697                                 if (net_debug > 2)
 698                                         printk("%s: send start\n", dev->name);
 699                                 snd->state = PLIP_PK_LENGTH_LSB;
 700                                 snd->nibble = PLIP_NB_BEGIN;
 701                                 nl->timeout_count = 0;
 702                                 sti();
 703                                 break;
 704                         }
 705                         sti();
 706                         if (--cx == 0) {
 707                                 outb(0x00, data_addr);
 708                                 return TIMEOUT;
 709                         }
 710                 }
 711 
 712         case PLIP_PK_LENGTH_LSB:
 713                 if (plip_send(nibble_timeout, data_addr,
 714                               &snd->nibble, snd->length.b.lsb))
 715                         return TIMEOUT;
 716                 snd->state = PLIP_PK_LENGTH_MSB;
 717 
 718         case PLIP_PK_LENGTH_MSB:
 719                 if (plip_send(nibble_timeout, data_addr,
 720                               &snd->nibble, snd->length.b.msb))
 721                         return TIMEOUT;
 722                 snd->state = PLIP_PK_DATA;
 723                 snd->byte = 0;
 724                 snd->checksum = 0;
 725 
 726         case PLIP_PK_DATA:
 727                 do
 728                         if (plip_send(nibble_timeout, data_addr,
 729                                       &snd->nibble, lbuf[snd->byte]))
 730                                 return TIMEOUT;
 731                 while (++snd->byte < snd->length.h);
 732                 do
 733                         snd->checksum += lbuf[--snd->byte];
 734                 while (snd->byte);
 735                 snd->state = PLIP_PK_CHECKSUM;
 736 
 737         case PLIP_PK_CHECKSUM:
 738                 if (plip_send(nibble_timeout, data_addr,
 739                               &snd->nibble, snd->checksum))
 740                         return TIMEOUT;
 741 
 742                 dev_kfree_skb(snd->skb, FREE_WRITE);
 743                 nl->enet_stats.tx_packets++;
 744                 snd->state = PLIP_PK_DONE;
 745 
 746         case PLIP_PK_DONE:
 747                 /* Close the connection */
 748                 outb (0x00, data_addr);
 749                 snd->skb = NULL;
 750                 if (net_debug > 2)
 751                         printk("%s: send end\n", dev->name);
 752                 nl->connection = PLIP_CN_CLOSING;
 753                 nl->is_deferred = 1;
 754                 queue_task(&nl->deferred, &tq_timer);
 755                 outb(PAR_INTR_ON, PAR_CONTROL(dev));
 756                 enable_irq(dev->irq);
 757                 return OK;
 758         }
 759         return OK;
 760 }
 761 
 762 static int
 763 plip_connection_close(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 764                       struct plip_local *snd, struct plip_local *rcv)
 765 {
 766         cli();
 767         if (nl->connection == PLIP_CN_CLOSING) {
 768                 nl->connection = PLIP_CN_NONE;
 769                 dev->tbusy = 0;
 770                 mark_bh(NET_BH);
 771         }
 772         sti();
 773         return OK;
 774 }
 775 
 776 /* PLIP_ERROR --- wait till other end settled */
 777 static int
 778 plip_error(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 779            struct plip_local *snd, struct plip_local *rcv)
 780 {
 781         unsigned char status;
 782 
 783         status = inb(PAR_STATUS(dev));
 784         if ((status & 0xf8) == 0x80) {
 785                 if (net_debug > 2)
 786                         printk("%s: reset interface.\n", dev->name);
 787                 nl->connection = PLIP_CN_NONE;
 788                 dev->tbusy = 0;
 789                 dev->interrupt = 0;
 790                 outb(PAR_INTR_ON, PAR_CONTROL(dev));
 791                 enable_irq(dev->irq);
 792                 mark_bh(NET_BH);
 793         } else {
 794                 nl->is_deferred = 1;
 795                 queue_task(&nl->deferred, &tq_timer);
 796         }
 797 
 798         return OK;
 799 }
 800 
 801 /* Handle the parallel port interrupts. */
 802 static void
 803 plip_interrupt(int irq, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 804 {
 805         struct device *dev = (struct device *) irq2dev_map[irq];
 806         struct net_local *nl = (struct net_local *)dev->priv;
 807         struct plip_local *rcv = &nl->rcv_data;
 808         unsigned char c0;
 809 
 810         if (dev == NULL) {
 811                 printk ("plip_interrupt: irq %d for unknown device.\n", irq);
 812                 return;
 813         }
 814 
 815         if (dev->interrupt)
 816                 return;
 817 
 818         c0 = inb(PAR_STATUS(dev));
 819         if ((c0 & 0xf8) != 0xc0) {
 820                 if (net_debug > 1)
 821                         printk("%s: spurious interrupt\n", dev->name);
 822                 return;
 823         }
 824         dev->interrupt = 1;
 825         if (net_debug > 3)
 826                 printk("%s: interrupt.\n", dev->name);
 827 
 828         cli();
 829         switch (nl->connection) {
 830         case PLIP_CN_CLOSING:
 831                 dev->tbusy = 0;
 832         case PLIP_CN_NONE:
 833         case PLIP_CN_SEND:
 834                 dev->last_rx = jiffies;
 835                 rcv->state = PLIP_PK_TRIGGER;
 836                 nl->connection = PLIP_CN_RECEIVE;
 837                 nl->timeout_count = 0;
 838                 queue_task(&nl->immediate, &tq_immediate);
 839                 mark_bh(IMMEDIATE_BH);
 840                 sti();
 841                 break;
 842 
 843         case PLIP_CN_RECEIVE:
 844                 sti();
 845                 printk("%s: receive interrupt when receiving packet\n", dev->name);
 846                 break;
 847 
 848         case PLIP_CN_ERROR:
 849                 sti();
 850                 printk("%s: receive interrupt in error state\n", dev->name);
 851                 break;
 852         }
 853 }
 854 
 855 /* We don't need to send arp, for plip is point-to-point. */
 856 static int
 857 plip_rebuild_header(void *buff, struct device *dev, unsigned long dst,
     /* [previous][next][first][last][top][bottom][index][help] */
 858                     struct sk_buff *skb)
 859 {
 860         struct net_local *nl = (struct net_local *)dev->priv;
 861         struct ethhdr *eth = (struct ethhdr *)buff;
 862         int i;
 863 
 864         if ((dev->flags & IFF_NOARP)==0)
 865                 return nl->orig_rebuild_header(buff, dev, dst, skb);
 866 
 867         if (eth->h_proto != htons(ETH_P_IP)) {
 868                 printk("plip_rebuild_header: Don't know how to resolve type %d addresses?\n", (int)eth->h_proto);
 869                 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
 870                 return 0;
 871         }
 872 
 873         for (i=0; i < ETH_ALEN - sizeof(unsigned long); i++)
 874                 eth->h_dest[i] = 0xfc;
 875         memcpy(&(eth->h_dest[i]), &dst, sizeof(unsigned long));
 876         return 0;
 877 }
 878 
 879 static int
 880 plip_tx_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 881 {
 882         struct net_local *nl = (struct net_local *)dev->priv;
 883         struct plip_local *snd = &nl->snd_data;
 884 
 885         if (dev->tbusy)
 886                 return 1;
 887 
 888         /* If some higher layer thinks we've missed an tx-done interrupt
 889            we are passed NULL. Caution: dev_tint() handles the cli()/sti()
 890            itself. */
 891         if (skb == NULL) {
 892                 dev_tint(dev);
 893                 return 0;
 894         }
 895 
 896         if (set_bit(0, (void*)&dev->tbusy) != 0) {
 897                 printk("%s: Transmitter access conflict.\n", dev->name);
 898                 return 1;
 899         }
 900 
 901         if (skb->len > dev->mtu + dev->hard_header_len) {
 902                 printk("%s: packet too big, %d.\n", dev->name, (int)skb->len);
 903                 dev->tbusy = 0;
 904                 return 0;
 905         }
 906 
 907         if (net_debug > 2)
 908                 printk("%s: send request\n", dev->name);
 909 
 910         cli();
 911         dev->trans_start = jiffies;
 912         snd->skb = skb;
 913         snd->length.h = skb->len;
 914         snd->state = PLIP_PK_TRIGGER;
 915         if (nl->connection == PLIP_CN_NONE) {
 916                 nl->connection = PLIP_CN_SEND;
 917                 nl->timeout_count = 0;
 918         }
 919         queue_task(&nl->immediate, &tq_immediate);
 920         mark_bh(IMMEDIATE_BH);
 921         sti();
 922 
 923         return 0;
 924 }
 925 
 926 /* Open/initialize the board.  This is called (in the current kernel)
 927    sometime after booting when the 'ifconfig' program is run.
 928 
 929    This routine gets exclusive access to the parallel port by allocating
 930    its IRQ line.
 931  */
 932 static int
 933 plip_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 934 {
 935         struct net_local *nl = (struct net_local *)dev->priv;
 936         int i;
 937 
 938         if (dev->irq == 0) {
 939                 printk("%s: IRQ is not set.  Please set it by ifconfig.\n", dev->name);
 940                 return -EAGAIN;
 941         }
 942         cli();
 943         if (request_irq(dev->irq , plip_interrupt, 0, dev->name) != 0) {
 944                 sti();
 945                 printk("%s: couldn't get IRQ %d.\n", dev->name, dev->irq);
 946                 return -EAGAIN;
 947         }
 948         irq2dev_map[dev->irq] = dev;
 949         sti();
 950 
 951         /* Clear the data port. */
 952         outb (0x00, PAR_DATA(dev));
 953 
 954         /* Enable rx interrupt. */
 955         outb(PAR_INTR_ON, PAR_CONTROL(dev));
 956 
 957         /* Initialize the state machine. */
 958         nl->rcv_data.state = nl->snd_data.state = PLIP_PK_DONE;
 959         nl->rcv_data.skb = nl->snd_data.skb = NULL;
 960         nl->connection = PLIP_CN_NONE;
 961         nl->is_deferred = 0;
 962 
 963         /* Fill in the MAC-level header. */
 964         for (i=0; i < ETH_ALEN - sizeof(unsigned long); i++)
 965                 dev->dev_addr[i] = 0xfc;
 966         memcpy(&(dev->dev_addr[i]), &dev->pa_addr, sizeof(unsigned long));
 967 
 968         dev->interrupt = 0;
 969         dev->start = 1;
 970         dev->tbusy = 0;
 971         MOD_INC_USE_COUNT;
 972         return 0;
 973 }
 974 
 975 /* The inverse routine to plip_open (). */
 976 static int
 977 plip_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 978 {
 979         struct net_local *nl = (struct net_local *)dev->priv;
 980         struct plip_local *snd = &nl->snd_data;
 981         struct plip_local *rcv = &nl->rcv_data;
 982 
 983         dev->tbusy = 1;
 984         dev->start = 0;
 985         cli();
 986         free_irq(dev->irq);
 987         irq2dev_map[dev->irq] = NULL;
 988         nl->is_deferred = 0;
 989         nl->connection = PLIP_CN_NONE;
 990         sti();
 991         outb(0x00, PAR_DATA(dev));
 992 
 993         snd->state = PLIP_PK_DONE;
 994         if (snd->skb) {
 995                 dev_kfree_skb(snd->skb, FREE_WRITE);
 996                 snd->skb = NULL;
 997         }
 998         rcv->state = PLIP_PK_DONE;
 999         if (rcv->skb) {
1000                 rcv->skb->free = 1;
1001                 kfree_skb(rcv->skb, FREE_READ);
1002                 rcv->skb = NULL;
1003         }
1004 
1005         /* Reset. */
1006         outb(0x00, PAR_CONTROL(dev));
1007         MOD_DEC_USE_COUNT;
1008         return 0;
1009 }
1010 
1011 static struct enet_statistics *
1012 plip_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1013 {
1014         struct net_local *nl = (struct net_local *)dev->priv;
1015         struct enet_statistics *r = &nl->enet_stats;
1016 
1017         return r;
1018 }
1019 
1020 static int
1021 plip_config(struct device *dev, struct ifmap *map)
     /* [previous][next][first][last][top][bottom][index][help] */
1022 {
1023         if (dev->flags & IFF_UP)
1024                 return -EBUSY;
1025 
1026         if (map->base_addr != (unsigned long)-1
1027             && map->base_addr != dev->base_addr)
1028                 printk("%s: You cannot change base_addr of this interface (ignored).\n", dev->name);
1029 
1030         if (map->irq != (unsigned char)-1)
1031                 dev->irq = map->irq;
1032         return 0;
1033 }
1034 
1035 static int
1036 plip_ioctl(struct device *dev, struct ifreq *rq, int cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1037 {
1038         struct net_local *nl = (struct net_local *) dev->priv;
1039         struct plipconf *pc = (struct plipconf *) &rq->ifr_data;
1040         
1041         switch(pc->pcmd) {
1042         case PLIP_GET_TIMEOUT:
1043                 pc->trigger = nl->trigger;
1044                 pc->nibble  = nl->nibble;
1045                 break;
1046         case PLIP_SET_TIMEOUT:
1047                 nl->trigger = pc->trigger;
1048                 nl->nibble  = pc->nibble;
1049                 break;
1050         default:
1051                 return -EOPNOTSUPP;
1052         }
1053         return 0;
1054 }
1055 
1056 #ifdef MODULE
1057 static int io[] = {0, 0, 0};
1058 static int irq[] = {0, 0, 0};
1059 
1060 static struct device dev_plip[] = {
1061         {
1062                 "plip0",
1063                 0, 0, 0, 0,             /* memory */
1064                 0x3BC, 5,               /* base, irq */
1065                 0, 0, 0, NULL, plip_init 
1066         },
1067         {
1068                 "plip1",
1069                 0, 0, 0, 0,             /* memory */
1070                 0x378, 7,               /* base, irq */
1071                 0, 0, 0, NULL, plip_init 
1072         },
1073         {
1074                 "plip2",
1075                 0, 0, 0, 0,             /* memory */
1076                 0x278, 2,               /* base, irq */
1077                 0, 0, 0, NULL, plip_init 
1078         }
1079 };
1080 
1081 int
1082 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1083 {
1084         int no_parameters=1;
1085         int devices=0;
1086         int i;
1087 
1088         /* When user feeds parameters, use them */
1089         for (i=0; i < 3; i++) {
1090                 int specified=0;
1091 
1092                 if (io[i] != 0) {
1093                         dev_plip[i].base_addr = io[i];
1094                         specified++;
1095                 }
1096                 if (irq[i] != 0) {
1097                         dev_plip[i].irq = irq[i];
1098                         specified++;
1099                 }
1100                 if (specified) {
1101                         if (register_netdev(&dev_plip[i]) != 0) {
1102                                 printk(KERN_INFO "plip%d: Not found\n", i);
1103                                 return -EIO;
1104                         }
1105                         no_parameters = 0;
1106                 }
1107         }
1108         if (!no_parameters)
1109                 return 0;
1110 
1111         /* No parameters.  Default action is probing all interfaces. */
1112         for (i=0; i < 3; i++) { 
1113                 if (register_netdev(&dev_plip[i]) == 0)
1114                         devices++;
1115         }
1116         if (devices == 0) {
1117                 printk(KERN_INFO "plip: no interfaces found\n");
1118                 return -EIO;
1119         }
1120         return 0;
1121 }
1122 
1123 void
1124 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1125 {
1126         int i;
1127 
1128         for (i=0; i < 3; i++) {
1129                 if (dev_plip[i].priv) {
1130                         unregister_netdev(&dev_plip[i]);
1131                         release_region(PAR_DATA(&dev_plip[i]), (PAR_DATA(&dev_plip[i]) == 0x3bc)? 3 : 8);
1132                         kfree_s(dev_plip[i].priv, sizeof(struct net_local));
1133                         dev_plip[i].priv = NULL;
1134                 }
1135         }
1136 }
1137 #endif /* MODULE */
1138 
1139 /*
1140  * Local variables:
1141  * compile-command: "gcc -DMODULE -DMODVERSIONS -D__KERNEL__ -Wall -Wstrict-prototypes -O2 -g -fomit-frame-pointer -pipe -m486 -c plip.c"
1142  * End:
1143  */

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