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 = dev_alloc_skb(rcv->length.h);
 526                 if (rcv->skb == NULL) {
 527                         printk("%s: Memory squeeze.\n", dev->name);
 528                         return ERROR;
 529                 }
 530                 skb_put(rcv->skb,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                 rcv->skb->protocol=eth_type_trans(rcv->skb, dev);
 563                 netif_rx(rcv->skb);
 564                 nl->enet_stats.rx_packets++;
 565                 rcv->skb = NULL;
 566                 if (net_debug > 2)
 567                         printk("%s: receive end\n", dev->name);
 568 
 569                 /* Close the connection. */
 570                 outb (0x00, PAR_DATA(dev));
 571                 cli();
 572                 if (snd->state != PLIP_PK_DONE) {
 573                         nl->connection = PLIP_CN_SEND;
 574                         sti();
 575                         queue_task(&nl->immediate, &tq_immediate);
 576                         outb(PAR_INTR_ON, PAR_CONTROL(dev));
 577                         enable_irq(dev->irq);
 578                         return OK;
 579                 } else {
 580                         nl->connection = PLIP_CN_NONE;
 581                         sti();
 582                         outb(PAR_INTR_ON, PAR_CONTROL(dev));
 583                         enable_irq(dev->irq);
 584                         return OK;
 585                 }
 586         }
 587         return OK;
 588 }
 589 
 590 /* PLIP_SEND --- send a byte (two nibbles) 
 591    Returns OK on success, TIMEOUT when timeout    */
 592 inline static int
 593 plip_send(unsigned short nibble_timeout, unsigned short data_addr,
     /* [previous][next][first][last][top][bottom][index][help] */
 594           enum plip_nibble_state *ns_p, unsigned char data)
 595 {
 596         unsigned char c0;
 597         unsigned int cx;
 598 
 599         switch (*ns_p) {
 600         case PLIP_NB_BEGIN:
 601                 outb((data & 0x0f), data_addr);
 602                 *ns_p = PLIP_NB_1;
 603 
 604         case PLIP_NB_1:
 605                 outb(0x10 | (data & 0x0f), data_addr);
 606                 cx = nibble_timeout;
 607                 data_addr++;
 608                 while (1) {
 609                         c0 = inb(data_addr);
 610                         if ((c0 & 0x80) == 0) 
 611                                 break;
 612                         if (--cx == 0)
 613                                 return TIMEOUT;
 614                         udelay(PLIP_DELAY_UNIT);
 615                 }
 616                 outb(0x10 | (data >> 4), --data_addr);
 617                 *ns_p = PLIP_NB_2;
 618 
 619         case PLIP_NB_2:
 620                 outb((data >> 4), data_addr);
 621                 data_addr++;
 622                 cx = nibble_timeout;
 623                 while (1) {
 624                         c0 = inb(data_addr);
 625                         if (c0 & 0x80)
 626                                 break;
 627                         if (--cx == 0)
 628                                 return TIMEOUT;
 629                         udelay(PLIP_DELAY_UNIT);
 630                 }
 631                 data_addr--;
 632                 *ns_p = PLIP_NB_BEGIN;
 633                 return OK;
 634         }
 635 }
 636 
 637 /* PLIP_SEND_PACKET --- send a packet */
 638 static int
 639 plip_send_packet(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 640                  struct plip_local *snd, struct plip_local *rcv)
 641 {
 642         unsigned short data_addr = PAR_DATA(dev);
 643         unsigned short nibble_timeout = nl->nibble;
 644         unsigned char *lbuf;
 645         unsigned char c0;
 646         unsigned int cx;
 647 
 648         if (snd->skb == NULL || (lbuf = snd->skb->data) == NULL) {
 649                 printk("%s: send skb lost\n", dev->name);
 650                 snd->state = PLIP_PK_DONE;
 651                 snd->skb = NULL;
 652                 return ERROR;
 653         }
 654 
 655         switch (snd->state) {
 656         case PLIP_PK_TRIGGER:
 657                 /* Trigger remote rx interrupt. */
 658                 outb(0x08, data_addr);
 659                 cx = nl->trigger;
 660                 while (1) {
 661                         udelay(PLIP_DELAY_UNIT);
 662                         cli();
 663                         if (nl->connection == PLIP_CN_RECEIVE) {
 664                                 sti();
 665                                 /* interrupted */
 666                                 nl->enet_stats.collisions++;
 667                                 if (net_debug > 1)
 668                                         printk("%s: collision.\n", dev->name);
 669                                 return OK;
 670                         }
 671                         c0 = inb(PAR_STATUS(dev));
 672                         if (c0 & 0x08) {
 673                                 disable_irq(dev->irq);
 674                                 outb(PAR_INTR_OFF, PAR_CONTROL(dev));
 675                                 if (net_debug > 2)
 676                                         printk("%s: send start\n", dev->name);
 677                                 snd->state = PLIP_PK_LENGTH_LSB;
 678                                 snd->nibble = PLIP_NB_BEGIN;
 679                                 nl->timeout_count = 0;
 680                                 sti();
 681                                 break;
 682                         }
 683                         sti();
 684                         if (--cx == 0) {
 685                                 outb(0x00, data_addr);
 686                                 return TIMEOUT;
 687                         }
 688                 }
 689 
 690         case PLIP_PK_LENGTH_LSB:
 691                 if (plip_send(nibble_timeout, data_addr,
 692                               &snd->nibble, snd->length.b.lsb))
 693                         return TIMEOUT;
 694                 snd->state = PLIP_PK_LENGTH_MSB;
 695 
 696         case PLIP_PK_LENGTH_MSB:
 697                 if (plip_send(nibble_timeout, data_addr,
 698                               &snd->nibble, snd->length.b.msb))
 699                         return TIMEOUT;
 700                 snd->state = PLIP_PK_DATA;
 701                 snd->byte = 0;
 702                 snd->checksum = 0;
 703 
 704         case PLIP_PK_DATA:
 705                 do
 706                         if (plip_send(nibble_timeout, data_addr,
 707                                       &snd->nibble, lbuf[snd->byte]))
 708                                 return TIMEOUT;
 709                 while (++snd->byte < snd->length.h);
 710                 do
 711                         snd->checksum += lbuf[--snd->byte];
 712                 while (snd->byte);
 713                 snd->state = PLIP_PK_CHECKSUM;
 714 
 715         case PLIP_PK_CHECKSUM:
 716                 if (plip_send(nibble_timeout, data_addr,
 717                               &snd->nibble, snd->checksum))
 718                         return TIMEOUT;
 719 
 720                 dev_kfree_skb(snd->skb, FREE_WRITE);
 721                 nl->enet_stats.tx_packets++;
 722                 snd->state = PLIP_PK_DONE;
 723 
 724         case PLIP_PK_DONE:
 725                 /* Close the connection */
 726                 outb (0x00, data_addr);
 727                 snd->skb = NULL;
 728                 if (net_debug > 2)
 729                         printk("%s: send end\n", dev->name);
 730                 nl->connection = PLIP_CN_CLOSING;
 731                 nl->is_deferred = 1;
 732                 queue_task(&nl->deferred, &tq_timer);
 733                 outb(PAR_INTR_ON, PAR_CONTROL(dev));
 734                 enable_irq(dev->irq);
 735                 return OK;
 736         }
 737         return OK;
 738 }
 739 
 740 static int
 741 plip_connection_close(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 742                       struct plip_local *snd, struct plip_local *rcv)
 743 {
 744         cli();
 745         if (nl->connection == PLIP_CN_CLOSING) {
 746                 nl->connection = PLIP_CN_NONE;
 747                 dev->tbusy = 0;
 748                 mark_bh(NET_BH);
 749         }
 750         sti();
 751         return OK;
 752 }
 753 
 754 /* PLIP_ERROR --- wait till other end settled */
 755 static int
 756 plip_error(struct device *dev, struct net_local *nl,
     /* [previous][next][first][last][top][bottom][index][help] */
 757            struct plip_local *snd, struct plip_local *rcv)
 758 {
 759         unsigned char status;
 760 
 761         status = inb(PAR_STATUS(dev));
 762         if ((status & 0xf8) == 0x80) {
 763                 if (net_debug > 2)
 764                         printk("%s: reset interface.\n", dev->name);
 765                 nl->connection = PLIP_CN_NONE;
 766                 dev->tbusy = 0;
 767                 dev->interrupt = 0;
 768                 outb(PAR_INTR_ON, PAR_CONTROL(dev));
 769                 enable_irq(dev->irq);
 770                 mark_bh(NET_BH);
 771         } else {
 772                 nl->is_deferred = 1;
 773                 queue_task(&nl->deferred, &tq_timer);
 774         }
 775 
 776         return OK;
 777 }
 778 
 779 /* Handle the parallel port interrupts. */
 780 static void
 781 plip_interrupt(int irq, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 782 {
 783         struct device *dev = (struct device *) irq2dev_map[irq];
 784         struct net_local *nl = (struct net_local *)dev->priv;
 785         struct plip_local *rcv = &nl->rcv_data;
 786         unsigned char c0;
 787 
 788         if (dev == NULL) {
 789                 printk ("plip_interrupt: irq %d for unknown device.\n", irq);
 790                 return;
 791         }
 792 
 793         if (dev->interrupt)
 794                 return;
 795 
 796         c0 = inb(PAR_STATUS(dev));
 797         if ((c0 & 0xf8) != 0xc0) {
 798                 if (net_debug > 1)
 799                         printk("%s: spurious interrupt\n", dev->name);
 800                 return;
 801         }
 802         dev->interrupt = 1;
 803         if (net_debug > 3)
 804                 printk("%s: interrupt.\n", dev->name);
 805 
 806         cli();
 807         switch (nl->connection) {
 808         case PLIP_CN_CLOSING:
 809                 dev->tbusy = 0;
 810         case PLIP_CN_NONE:
 811         case PLIP_CN_SEND:
 812                 dev->last_rx = jiffies;
 813                 rcv->state = PLIP_PK_TRIGGER;
 814                 nl->connection = PLIP_CN_RECEIVE;
 815                 nl->timeout_count = 0;
 816                 queue_task(&nl->immediate, &tq_immediate);
 817                 mark_bh(IMMEDIATE_BH);
 818                 sti();
 819                 break;
 820 
 821         case PLIP_CN_RECEIVE:
 822                 sti();
 823                 printk("%s: receive interrupt when receiving packet\n", dev->name);
 824                 break;
 825 
 826         case PLIP_CN_ERROR:
 827                 sti();
 828                 printk("%s: receive interrupt in error state\n", dev->name);
 829                 break;
 830         }
 831 }
 832 
 833 /* We don't need to send arp, for plip is point-to-point. */
 834 static int
 835 plip_rebuild_header(void *buff, struct device *dev, unsigned long dst,
     /* [previous][next][first][last][top][bottom][index][help] */
 836                     struct sk_buff *skb)
 837 {
 838         struct net_local *nl = (struct net_local *)dev->priv;
 839         struct ethhdr *eth = (struct ethhdr *)buff;
 840         int i;
 841 
 842         if ((dev->flags & IFF_NOARP)==0)
 843                 return nl->orig_rebuild_header(buff, dev, dst, skb);
 844 
 845         if (eth->h_proto != htons(ETH_P_IP)) {
 846                 printk("plip_rebuild_header: Don't know how to resolve type %d addresses?\n", (int)eth->h_proto);
 847                 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
 848                 return 0;
 849         }
 850 
 851         for (i=0; i < ETH_ALEN - sizeof(unsigned long); i++)
 852                 eth->h_dest[i] = 0xfc;
 853         memcpy(&(eth->h_dest[i]), &dst, sizeof(unsigned long));
 854         return 0;
 855 }
 856 
 857 static int
 858 plip_tx_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 859 {
 860         struct net_local *nl = (struct net_local *)dev->priv;
 861         struct plip_local *snd = &nl->snd_data;
 862 
 863         if (dev->tbusy)
 864                 return 1;
 865 
 866         /* If some higher layer thinks we've missed an tx-done interrupt
 867            we are passed NULL. Caution: dev_tint() handles the cli()/sti()
 868            itself. */
 869         if (skb == NULL) {
 870                 dev_tint(dev);
 871                 return 0;
 872         }
 873 
 874         if (set_bit(0, (void*)&dev->tbusy) != 0) {
 875                 printk("%s: Transmitter access conflict.\n", dev->name);
 876                 return 1;
 877         }
 878 
 879         if (skb->len > dev->mtu) {
 880                 printk("%s: packet too big, %d.\n", dev->name, (int)skb->len);
 881                 dev->tbusy = 0;
 882                 return 0;
 883         }
 884 
 885         if (net_debug > 2)
 886                 printk("%s: send request\n", dev->name);
 887 
 888         cli();
 889         dev->trans_start = jiffies;
 890         snd->skb = skb;
 891         snd->length.h = skb->len;
 892         snd->state = PLIP_PK_TRIGGER;
 893         if (nl->connection == PLIP_CN_NONE) {
 894                 nl->connection = PLIP_CN_SEND;
 895                 nl->timeout_count = 0;
 896         }
 897         queue_task(&nl->immediate, &tq_immediate);
 898         mark_bh(IMMEDIATE_BH);
 899         sti();
 900 
 901         return 0;
 902 }
 903 
 904 /* Open/initialize the board.  This is called (in the current kernel)
 905    sometime after booting when the 'ifconfig' program is run.
 906 
 907    This routine gets exclusive access to the parallel port by allocating
 908    its IRQ line.
 909  */
 910 static int
 911 plip_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 912 {
 913         struct net_local *nl = (struct net_local *)dev->priv;
 914         int i;
 915 
 916         if (dev->irq == 0) {
 917                 printk("%s: IRQ is not set.  Please set it by ifconfig.\n", dev->name);
 918                 return -EAGAIN;
 919         }
 920         cli();
 921         if (request_irq(dev->irq , plip_interrupt, 0, dev->name) != 0) {
 922                 sti();
 923                 printk("%s: couldn't get IRQ %d.\n", dev->name, dev->irq);
 924                 return -EAGAIN;
 925         }
 926         irq2dev_map[dev->irq] = dev;
 927         sti();
 928 
 929         /* Clear the data port. */
 930         outb (0x00, PAR_DATA(dev));
 931 
 932         /* Enable rx interrupt. */
 933         outb(PAR_INTR_ON, PAR_CONTROL(dev));
 934 
 935         /* Initialize the state machine. */
 936         nl->rcv_data.state = nl->snd_data.state = PLIP_PK_DONE;
 937         nl->rcv_data.skb = nl->snd_data.skb = NULL;
 938         nl->connection = PLIP_CN_NONE;
 939         nl->is_deferred = 0;
 940 
 941         /* Fill in the MAC-level header. */
 942         for (i=0; i < ETH_ALEN - sizeof(unsigned long); i++)
 943                 dev->dev_addr[i] = 0xfc;
 944         memcpy(&(dev->dev_addr[i]), &dev->pa_addr, sizeof(unsigned long));
 945 
 946         dev->interrupt = 0;
 947         dev->start = 1;
 948         dev->tbusy = 0;
 949         MOD_INC_USE_COUNT;
 950         return 0;
 951 }
 952 
 953 /* The inverse routine to plip_open (). */
 954 static int
 955 plip_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 956 {
 957         struct net_local *nl = (struct net_local *)dev->priv;
 958         struct plip_local *snd = &nl->snd_data;
 959         struct plip_local *rcv = &nl->rcv_data;
 960 
 961         dev->tbusy = 1;
 962         dev->start = 0;
 963         cli();
 964         free_irq(dev->irq);
 965         irq2dev_map[dev->irq] = NULL;
 966         nl->is_deferred = 0;
 967         nl->connection = PLIP_CN_NONE;
 968         sti();
 969         outb(0x00, PAR_DATA(dev));
 970 
 971         snd->state = PLIP_PK_DONE;
 972         if (snd->skb) {
 973                 dev_kfree_skb(snd->skb, FREE_WRITE);
 974                 snd->skb = NULL;
 975         }
 976         rcv->state = PLIP_PK_DONE;
 977         if (rcv->skb) {
 978                 rcv->skb->free = 1;
 979                 kfree_skb(rcv->skb, FREE_READ);
 980                 rcv->skb = NULL;
 981         }
 982 
 983         /* Reset. */
 984         outb(0x00, PAR_CONTROL(dev));
 985         MOD_DEC_USE_COUNT;
 986         return 0;
 987 }
 988 
 989 static struct enet_statistics *
 990 plip_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 991 {
 992         struct net_local *nl = (struct net_local *)dev->priv;
 993         struct enet_statistics *r = &nl->enet_stats;
 994 
 995         return r;
 996 }
 997 
 998 static int
 999 plip_config(struct device *dev, struct ifmap *map)
     /* [previous][next][first][last][top][bottom][index][help] */
1000 {
1001         if (dev->flags & IFF_UP)
1002                 return -EBUSY;
1003 
1004         if (map->base_addr != (unsigned long)-1
1005             && map->base_addr != dev->base_addr)
1006                 printk("%s: You cannot change base_addr of this interface (ignored).\n", dev->name);
1007 
1008         if (map->irq != (unsigned char)-1)
1009                 dev->irq = map->irq;
1010         return 0;
1011 }
1012 
1013 static int
1014 plip_ioctl(struct device *dev, struct ifreq *rq, int cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1015 {
1016         struct net_local *nl = (struct net_local *) dev->priv;
1017         struct plipconf *pc = (struct plipconf *) &rq->ifr_data;
1018         
1019         switch(pc->pcmd) {
1020         case PLIP_GET_TIMEOUT:
1021                 pc->trigger = nl->trigger;
1022                 pc->nibble  = nl->nibble;
1023                 break;
1024         case PLIP_SET_TIMEOUT:
1025                 nl->trigger = pc->trigger;
1026                 nl->nibble  = pc->nibble;
1027                 break;
1028         default:
1029                 return -EOPNOTSUPP;
1030         }
1031         return 0;
1032 }
1033 
1034 #ifdef MODULE
1035 char kernel_version[] = UTS_RELEASE;
1036 
1037 static struct device dev_plip0 = 
1038 {
1039         "plip0" /*"plip"*/,
1040         0, 0, 0, 0,             /* memory */
1041         0x3BC, 5,               /* base, irq */
1042         0, 0, 0, NULL, plip_init 
1043 };
1044 
1045 static struct device dev_plip1 = 
1046 {
1047         "plip1" /*"plip"*/,
1048         0, 0, 0, 0,             /* memory */
1049         0x378, 7,               /* base, irq */
1050         0, 0, 0, NULL, plip_init 
1051 };
1052 
1053 static struct device dev_plip2 = 
1054 {
1055         "plip2" /*"plip"*/,
1056         0, 0, 0, 0,             /* memory */
1057         0x278, 2,               /* base, irq */
1058         0, 0, 0, NULL, plip_init 
1059 };
1060 
1061 int
1062 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1063 {
1064         int devices=0;
1065 
1066         if (register_netdev(&dev_plip0) != 0)
1067                 devices++;
1068         if (register_netdev(&dev_plip1) != 0)
1069                 devices++;
1070         if (register_netdev(&dev_plip2) != 0)
1071                 devices++;
1072         if (devices == 0)
1073                 return -EIO;
1074         return 0;
1075 }
1076 
1077 void
1078 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1079 {
1080         if (dev_plip0.priv) {
1081                 unregister_netdev(&dev_plip0);
1082                 release_region(PAR_DATA(&dev_plip0), 3);
1083                 kfree_s(dev_plip0.priv, sizeof(struct net_local));
1084                 dev_plip0.priv = NULL;
1085         }
1086         if (dev_plip1.priv) {
1087                 unregister_netdev(&dev_plip1);
1088                 release_region(PAR_DATA(&dev_plip1), 3);
1089                 kfree_s(dev_plip1.priv, sizeof(struct net_local));
1090                 dev_plip1.priv = NULL;
1091         }
1092         if (dev_plip2.priv) {
1093                 unregister_netdev(&dev_plip2);
1094                 release_region(PAR_DATA(&dev_plip2), 3);
1095                 kfree_s(dev_plip2.priv, sizeof(struct net_local));
1096                 dev_plip2.priv = NULL;
1097         }
1098 }
1099 #endif /* MODULE */
1100 
1101 /*
1102  * Local variables:
1103  * compile-command: "gcc -DMODULE -DCONFIG_MODVERSIONS -D__KERNEL__ -Wall -Wstrict-prototypes -O2 -g -fomit-frame-pointer -pipe -m486 -c plip.c"
1104  * End:
1105  */

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