root/drivers/net/plip.c

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

DEFINITIONS

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

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

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