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

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