root/drivers/net/plip.c

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

DEFINITIONS

This source file includes following definitions.
  1. plip_init
  2. plip_kick_bh
  3. plip_bh
  4. plip_bh_timeout_error
  5. plip_none
  6. plip_receive
  7. plip_receive_packet
  8. plip_send
  9. plip_send_packet
  10. plip_connection_close
  11. plip_error
  12. plip_interrupt
  13. plip_rebuild_header
  14. plip_tx_packet
  15. plip_open
  16. plip_close
  17. plip_get_stats
  18. plip_config
  19. plip_ioctl
  20. init_module
  21. cleanup_module

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

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