root/drivers/net/de620.c

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

DEFINITIONS

This source file includes following definitions.
  1. de620_ready
  2. de620_send_command
  3. de620_put_byte
  4. de620_read_byte
  5. de620_write_block
  6. de620_read_block
  7. de620_set_delay
  8. de620_set_register
  9. de620_get_register
  10. de620_open
  11. de620_close
  12. get_stats
  13. de620_set_multicast_list
  14. de620_start_xmit
  15. de620_interrupt
  16. de620_rx_intr
  17. adapter_init
  18. de620_probe
  19. ReadAWord
  20. read_eeprom
  21. init_module
  22. cleanup_module

   1 /*
   2  *      de620.c $Revision: 1.31 $ BETA
   3  *
   4  *
   5  *      Linux driver for the D-Link DE-620 Ethernet pocket adapter.
   6  *
   7  *      Portions (C) Copyright 1993, 1994 by Bjorn Ekwall <bj0rn@blox.se>
   8  *
   9  *      Based on adapter information gathered from DOS packetdriver
  10  *      sources from D-Link Inc:  (Special thanks to Henry Ngai of D-Link.)
  11  *              Portions (C) Copyright D-Link SYSTEM Inc. 1991, 1992
  12  *              Copyright, 1988, Russell Nelson, Crynwr Software
  13  *
  14  *      Adapted to the sample network driver core for linux,
  15  *      written by: Donald Becker <becker@super.org>
  16  *              (Now at <becker@cesdis.gsfc.nasa.gov>
  17  *
  18  *      Valuable assistance from:
  19  *              J. Joshua Kopper <kopper@rtsg.mot.com>
  20  *              Olav Kvittem <Olav.Kvittem@uninett.no>
  21  *              Germano Caronni <caronni@nessie.cs.id.ethz.ch>
  22  *              Jeremy Fitzhardinge <jeremy@suite.sw.oz.au>
  23  *
  24  *****************************************************************************/
  25 /*
  26  *      This program is free software; you can redistribute it and/or modify
  27  *      it under the terms of the GNU General Public License as published by
  28  *      the Free Software Foundation; either version 2, or (at your option)
  29  *      any later version.
  30  *
  31  *      This program is distributed in the hope that it will be useful,
  32  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  33  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34  *      GNU General Public License for more details.
  35  *
  36  *      You should have received a copy of the GNU General Public License
  37  *      along with this program; if not, write to the Free Software
  38  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
  39  *
  40  *****************************************************************************/
  41 static const char *version =
  42         "de620.c: $Revision: 1.31 $,  Bjorn Ekwall <bj0rn@blox.se>\n";
  43 
  44 /***********************************************************************
  45  *
  46  * "Tuning" section.
  47  *
  48  * Compile-time options: (see below for descriptions)
  49  * -DDE620_IO=0x378     (lpt1)
  50  * -DDE620_IRQ=7        (lpt1)
  51  * -DDE602_DEBUG=...
  52  * -DSHUTDOWN_WHEN_LOST
  53  * -DCOUNT_LOOPS
  54  * -DLOWSPEED
  55  * -DREAD_DELAY
  56  * -DWRITE_DELAY
  57  */
  58 
  59 /*
  60  * If the adapter has problems with high speeds, enable this #define
  61  * otherwise full printerport speed will be attempted.
  62  *
  63  * You can tune the READ_DELAY/WRITE_DELAY below if you enable LOWSPEED
  64  *
  65 #define LOWSPEED
  66  */
  67 
  68 #ifndef READ_DELAY
  69 #define READ_DELAY 100  /* adapter internal read delay in 100ns units */
  70 #endif
  71 
  72 #ifndef WRITE_DELAY
  73 #define WRITE_DELAY 100 /* adapter internal write delay in 100ns units */
  74 #endif
  75 
  76 /*
  77  * Enable this #define if you want the adapter to do a "ifconfig down" on
  78  * itself when we have detected that something is possibly wrong with it.
  79  * The default behaviour is to retry with "adapter_init()" until success.
  80  * This should be used for debugging purposes only.
  81  *
  82 #define SHUTDOWN_WHEN_LOST
  83  */
  84 
  85 /*
  86  * Enable debugging by "-DDE620_DEBUG=3" when compiling,
  87  * OR in "./CONFIG"
  88  * OR by enabling the following #define
  89  *
  90  * use 0 for production, 1 for verification, >2 for debug
  91  *
  92 #define DE620_DEBUG 3
  93  */
  94 
  95 #ifdef LOWSPEED
  96 /*
  97  * Enable this #define if you want to see debugging output that show how long
  98  * we have to wait before the DE-620 is ready for the next read/write/command.
  99  *
 100 #define COUNT_LOOPS
 101  */
 102 #endif
 103 static int bnc = 0, utp = 0;
 104 /*
 105  * Force media with insmod:
 106  *      insmod de620.o bnc=1
 107  * or
 108  *      insmod de620.o utp=1
 109  */
 110 
 111 #ifdef MODULE
 112 #include <linux/module.h>
 113 #include <linux/version.h>
 114 #endif
 115 
 116 #include <linux/kernel.h>
 117 #include <linux/sched.h>
 118 #include <linux/types.h>
 119 #include <linux/fcntl.h>
 120 #include <linux/string.h>
 121 #include <linux/interrupt.h>
 122 #include <linux/ioport.h>
 123 #include <asm/io.h>
 124 #include <linux/in.h>
 125 #include <linux/ptrace.h>
 126 #include <asm/system.h>
 127 #include <linux/errno.h>
 128 
 129 #include <linux/inet.h>
 130 #include <linux/netdevice.h>
 131 #include <linux/etherdevice.h>
 132 #include <linux/skbuff.h>
 133 
 134 /* Constant definitions for the DE-620 registers, commands and bits */
 135 #include "de620.h"
 136 
 137 #define netstats enet_statistics
 138 typedef unsigned char byte;
 139 
 140 /*******************************************************
 141  *                                                     *
 142  * Definition of D-Link DE-620 Ethernet Pocket adapter *
 143  * See also "de620.h"                                  *
 144  *                                                     *
 145  *******************************************************/
 146 #ifndef DE620_IO /* Compile-time configurable */
 147 #define DE620_IO 0x378
 148 #endif
 149 
 150 #ifndef DE620_IRQ /* Compile-time configurable */
 151 #define DE620_IRQ       7
 152 #endif
 153 
 154 #define DATA_PORT       (dev->base_addr)
 155 #define STATUS_PORT     (dev->base_addr + 1)
 156 #define COMMAND_PORT    (dev->base_addr + 2)
 157 
 158 #define RUNT 60         /* Too small Ethernet packet */
 159 #define GIANT 1514      /* largest legal size packet, no fcs */
 160 
 161 #ifdef DE620_DEBUG /* Compile-time configurable */
 162 #define PRINTK(x) if (de620_debug >= 2) printk x
 163 #else
 164 #define DE620_DEBUG 0
 165 #define PRINTK(x) /**/
 166 #endif
 167 
 168 /***********************************************
 169  *                                             *
 170  * Index to functions, as function prototypes. *
 171  *                                             *
 172  ***********************************************/
 173 
 174 /*
 175  * Routines used internally. (See also "convenience macros.. below")
 176  */
 177 
 178 /* Put in the device structure. */
 179 static int      de620_open(struct device *);
 180 static int      de620_close(struct device *);
 181 static struct netstats *get_stats(struct device *);
 182 static void     de620_set_multicast_list(struct device *, int, void *);
 183 static int      de620_start_xmit(struct sk_buff *, struct device *);
 184 
 185 /* Dispatch from interrupts. */
 186 static void     de620_interrupt(int, struct pt_regs *);
 187 static int      de620_rx_intr(struct device *);
 188 
 189 /* Initialization */
 190 static int      adapter_init(struct device *);
 191 int             de620_probe(struct device *);
 192 static int      read_eeprom(struct device *);
 193 
 194 
 195 /*
 196  * D-Link driver variables:
 197  */
 198 #define SCR_DEF NIBBLEMODE |INTON | SLEEP | AUTOTX
 199 #define TCR_DEF RXPB                    /* not used: | TXSUCINT | T16INT */
 200 #define DE620_RX_START_PAGE 12          /* 12 pages (=3k) reserved for tx */
 201 #define DEF_NIC_CMD IRQEN | ICEN | DS1
 202 
 203 unsigned int de620_debug = DE620_DEBUG;
 204 
 205 static volatile byte    NIC_Cmd;
 206 static volatile byte    next_rx_page;
 207 static byte             first_rx_page;
 208 static byte             last_rx_page;
 209 static byte             EIPRegister;
 210 
 211 static struct nic {
 212         byte    NodeID[6];
 213         byte    RAM_Size;
 214         byte    Model;
 215         byte    Media;
 216         byte    SCR;
 217 } nic_data;
 218 
 219 /**********************************************************
 220  *                                                        *
 221  * Convenience macros/functions for D-Link DE-620 adapter *
 222  *                                                        *
 223  **********************************************************/
 224 #define de620_tx_buffs(dd) (inb(STATUS_PORT) & (TXBF0 | TXBF1))
 225 #define de620_flip_ds(dd) NIC_Cmd ^= DS0 | DS1; outb(NIC_Cmd, COMMAND_PORT);
 226 
 227 /* Check for ready-status, and return a nibble (high 4 bits) for data input */
 228 #ifdef COUNT_LOOPS
 229 static int tot_cnt;
 230 #endif
 231 static inline byte
 232 de620_ready(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 233 {
 234         byte value;
 235         register short int cnt = 0;
 236 
 237         while ((((value = inb(STATUS_PORT)) & READY) == 0) && (cnt <= 1000))
 238                 ++cnt;
 239 
 240 #ifdef COUNT_LOOPS
 241         tot_cnt += cnt;
 242 #endif
 243         return value & 0xf0; /* nibble */
 244 }
 245 
 246 static inline void
 247 de620_send_command(struct device *dev, byte cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 248 {
 249         de620_ready(dev);
 250         if (cmd == W_DUMMY)
 251                 outb(NIC_Cmd, COMMAND_PORT);
 252 
 253         outb(cmd, DATA_PORT);
 254 
 255         outb(NIC_Cmd ^ CS0, COMMAND_PORT);
 256         de620_ready(dev);
 257         outb(NIC_Cmd, COMMAND_PORT);
 258 }
 259 
 260 static inline void
 261 de620_put_byte(struct device *dev, byte value)
     /* [previous][next][first][last][top][bottom][index][help] */
 262 {
 263         /* The de620_ready() makes 7 loops, on the average, on a DX2/66 */
 264         de620_ready(dev);
 265         outb(value, DATA_PORT);
 266         de620_flip_ds(dev);
 267 }
 268 
 269 static inline byte
 270 de620_read_byte(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 271 {
 272         byte value;
 273 
 274         /* The de620_ready() makes 7 loops, on the average, on a DX2/66 */
 275         value = de620_ready(dev); /* High nibble */
 276         de620_flip_ds(dev);
 277         value |= de620_ready(dev) >> 4; /* Low nibble */
 278         return value;
 279 }
 280 
 281 static inline void
 282 de620_write_block(struct device *dev, byte *buffer, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 283 {
 284 #ifndef LOWSPEED
 285         byte uflip = NIC_Cmd ^ (DS0 | DS1);
 286         byte dflip = NIC_Cmd;
 287 #else /* LOWSPEED */
 288 #ifdef COUNT_LOOPS
 289         int bytes = count;
 290 #endif /* COUNT_LOOPS */
 291 #endif /* LOWSPEED */
 292 
 293 #ifdef LOWSPEED
 294 #ifdef COUNT_LOOPS
 295         tot_cnt = 0;
 296 #endif /* COUNT_LOOPS */
 297         /* No further optimization useful, the limit is in the adapter. */
 298         for ( ; count > 0; --count, ++buffer) {
 299                 de620_put_byte(dev,*buffer);
 300         }
 301         de620_send_command(dev,W_DUMMY);
 302 #ifdef COUNT_LOOPS
 303         /* trial debug output: loops per byte in de620_ready() */
 304         printk("WRITE(%d)\n", tot_cnt/((bytes?bytes:1)));
 305 #endif /* COUNT_LOOPS */
 306 #else /* not LOWSPEED */
 307         for ( ; count > 0; count -=2) {
 308                 outb(*buffer++, DATA_PORT);
 309                 outb(uflip, COMMAND_PORT);
 310                 outb(*buffer++, DATA_PORT);
 311                 outb(dflip, COMMAND_PORT);
 312         }
 313         de620_send_command(dev,W_DUMMY);
 314 #endif /* LOWSPEED */
 315 }
 316 
 317 static inline void
 318 de620_read_block(struct device *dev, byte *data, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 319 {
 320 #ifndef LOWSPEED
 321         byte value;
 322         byte uflip = NIC_Cmd ^ (DS0 | DS1);
 323         byte dflip = NIC_Cmd;
 324 #else /* LOWSPEED */
 325 #ifdef COUNT_LOOPS
 326         int bytes = count;
 327 
 328         tot_cnt = 0;
 329 #endif /* COUNT_LOOPS */
 330 #endif /* LOWSPEED */
 331 
 332 #ifdef LOWSPEED
 333         /* No further optimization useful, the limit is in the adapter. */
 334         while (count-- > 0) {
 335                 *data++ = de620_read_byte(dev);
 336                 de620_flip_ds(dev);
 337         }
 338 #ifdef COUNT_LOOPS
 339         /* trial debug output: loops per byte in de620_ready() */
 340         printk("READ(%d)\n", tot_cnt/(2*(bytes?bytes:1)));
 341 #endif /* COUNT_LOOPS */
 342 #else /* not LOWSPEED */
 343         while (count-- > 0) {
 344                 value = inb(STATUS_PORT) & 0xf0; /* High nibble */
 345                 outb(uflip, COMMAND_PORT);
 346                 *data++ = value | inb(STATUS_PORT) >> 4; /* Low nibble */
 347                 outb(dflip , COMMAND_PORT);
 348         }
 349 #endif /* LOWSPEED */
 350 }
 351 
 352 static inline void
 353 de620_set_delay(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 354 {
 355         de620_ready(dev);
 356         outb(W_DFR, DATA_PORT);
 357         outb(NIC_Cmd ^ CS0, COMMAND_PORT);
 358 
 359         de620_ready(dev);
 360 #ifdef LOWSPEED
 361         outb(WRITE_DELAY, DATA_PORT);
 362 #else
 363         outb(0, DATA_PORT);
 364 #endif
 365         de620_flip_ds(dev);
 366 
 367         de620_ready(dev);
 368 #ifdef LOWSPEED
 369         outb(READ_DELAY, DATA_PORT);
 370 #else
 371         outb(0, DATA_PORT);
 372 #endif
 373         de620_flip_ds(dev);
 374 }
 375 
 376 static inline void
 377 de620_set_register(struct device *dev, byte reg, byte value)
     /* [previous][next][first][last][top][bottom][index][help] */
 378 {
 379         de620_ready(dev);
 380         outb(reg, DATA_PORT);
 381         outb(NIC_Cmd ^ CS0, COMMAND_PORT);
 382 
 383         de620_put_byte(dev, value);
 384 }
 385 
 386 static inline byte
 387 de620_get_register(struct device *dev, byte reg)
     /* [previous][next][first][last][top][bottom][index][help] */
 388 {
 389         byte value;
 390 
 391         de620_send_command(dev,reg);
 392         value = de620_read_byte(dev);
 393         de620_send_command(dev,W_DUMMY);
 394 
 395         return value;
 396 }
 397 
 398 /*********************************************************************
 399  *
 400  * Open/initialize the board.
 401  *
 402  * This routine should set everything up anew at each open, even
 403  * registers that "should" only need to be set once at boot, so that
 404  * there is a non-reboot way to recover if something goes wrong.
 405  *
 406  */
 407 static int
 408 de620_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 409 {
 410         if (request_irq(DE620_IRQ, de620_interrupt, 0, "de620")) {
 411                 printk ("%s: unable to get IRQ %d\n", dev->name, DE620_IRQ);
 412                 return 1;
 413         }
 414         irq2dev_map[DE620_IRQ] = dev;
 415 
 416 #ifdef MODULE
 417         MOD_INC_USE_COUNT;
 418 #endif
 419         if (adapter_init(dev)) {
 420                 return 1;
 421         }
 422         dev->start = 1;
 423         return 0;
 424 }
 425 
 426 /************************************************
 427  *
 428  * The inverse routine to de620_open().
 429  *
 430  */
 431 static int
 432 de620_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 433 {
 434         /* disable recv */
 435         de620_set_register(dev, W_TCR, RXOFF);
 436 
 437         free_irq(DE620_IRQ);
 438         irq2dev_map[DE620_IRQ] = NULL;
 439 
 440         dev->start = 0;
 441 #ifdef MODULE
 442         MOD_DEC_USE_COUNT;
 443 #endif
 444         return 0;
 445 }
 446 
 447 /*********************************************
 448  *
 449  * Return current statistics
 450  *
 451  */
 452 static struct netstats *
 453 get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 454 {
 455         return (struct netstats *)(dev->priv);
 456 }
 457 
 458 /*********************************************
 459  *
 460  * Set or clear the multicast filter for this adaptor.
 461  * (no real multicast implemented for the DE-620, but she can be promiscuous...)
 462  *
 463  * num_addrs == -1      Promiscuous mode, receive all packets
 464  * num_addrs == 0       Normal mode, clear multicast list
 465  * num_addrs > 0        Multicast mode, receive normal and MC packets, and do
 466  *                      best-effort filtering.
 467  */
 468 static void
 469 de620_set_multicast_list(struct device *dev, int num_addrs, void *addrs)
     /* [previous][next][first][last][top][bottom][index][help] */
 470 {
 471         if (num_addrs) 
 472         { /* Enable promiscuous mode */
 473                 /*
 474                  *      We must make the kernel realise we had to move
 475                  *      into promisc mode or we start all out war on
 476                  *      the cable. - AC
 477                  */
 478                 dev->flags|=IFF_PROMISC;                
 479         
 480                 de620_set_register(dev, W_TCR, (TCR_DEF & ~RXPBM) | RXALL);
 481         }
 482         else { /* Disable promiscuous mode, use normal mode */
 483                 de620_set_register(dev, W_TCR, TCR_DEF);
 484         }
 485 }
 486 
 487 /*******************************************************
 488  *
 489  * Copy a buffer to the adapter transmit page memory.
 490  * Start sending.
 491  */
 492 static int
 493 de620_start_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 494 {
 495         unsigned long flags;
 496         int len;
 497         int tickssofar;
 498         byte *buffer = skb->data;
 499         byte using_txbuf;
 500 
 501         /*
 502          * If some higher layer thinks we've missed a
 503          * tx-done interrupt we are passed NULL.
 504          * Caution: dev_tint() handles the cli()/sti() itself.
 505          */
 506 
 507         if (skb == NULL) {
 508                 dev_tint(dev);
 509                 return 0;
 510         }
 511 
 512         using_txbuf = de620_tx_buffs(dev); /* Peek at the adapter */
 513         dev->tbusy = (using_txbuf == (TXBF0 | TXBF1)); /* Boolean! */
 514 
 515         if (dev->tbusy) {       /* Do timeouts, to avoid hangs. */
 516                 tickssofar = jiffies - dev->trans_start;
 517 
 518                 if (tickssofar < 5)
 519                         return 1;
 520 
 521                 /* else */
 522                 printk("%s: transmit timed out (%d), %s?\n",
 523                         dev->name,
 524                         tickssofar,
 525                         "network cable problem"
 526                         );
 527                 /* Restart the adapter. */
 528                 if (adapter_init(dev)) /* maybe close it */
 529                         return 1;
 530         }
 531 
 532         if ((len = skb->len) < RUNT)
 533                 len = RUNT;
 534         if (len & 1) /* send an even number of bytes */
 535                 ++len;
 536 
 537         /* Start real output */
 538         save_flags(flags);
 539         cli();
 540 
 541         PRINTK(("de620_start_xmit: len=%d, bufs 0x%02x\n",
 542                 (int)skb->len, using_txbuf));
 543 
 544         /* select a free tx buffer. if there is one... */
 545         switch (using_txbuf) {
 546         default: /* both are free: use TXBF0 */
 547         case TXBF1: /* use TXBF0 */
 548                 de620_send_command(dev,W_CR | RW0);
 549                 using_txbuf |= TXBF0;
 550                 break;
 551 
 552         case TXBF0: /* use TXBF1 */
 553                 de620_send_command(dev,W_CR | RW1);
 554                 using_txbuf |= TXBF1;
 555                 break;
 556 
 557         case (TXBF0 | TXBF1): /* NONE!!! */
 558                 printk("de620: Ouch! No tx-buffer available!\n");
 559                 restore_flags(flags);
 560                 return 1;
 561                 break;
 562         }
 563         de620_write_block(dev, buffer, len);
 564 
 565         dev->trans_start = jiffies;
 566         dev->tbusy = (using_txbuf == (TXBF0 | TXBF1)); /* Boolean! */
 567 
 568         ((struct netstats *)(dev->priv))->tx_packets++;
 569         
 570         restore_flags(flags); /* interrupts maybe back on */
 571         
 572         dev_kfree_skb (skb, FREE_WRITE);
 573 
 574         return 0;
 575 }
 576 
 577 /*****************************************************
 578  *
 579  * Handle the network interface interrupts.
 580  *
 581  */
 582 static void
 583 de620_interrupt(int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 584 {
 585         struct device *dev = irq2dev_map[irq];
 586         byte irq_status;
 587         int bogus_count = 0;
 588         int again = 0;
 589 
 590         /* This might be deleted now, no crummy drivers present :-) Or..? */
 591         if ((dev == NULL) || (DE620_IRQ != irq)) {
 592                 printk("%s: bogus interrupt %d\n", dev?dev->name:"DE620", irq);
 593                 return;
 594         }
 595 
 596         cli();
 597         dev->interrupt = 1;
 598 
 599         /* Read the status register (_not_ the status port) */
 600         irq_status = de620_get_register(dev, R_STS);
 601 
 602         PRINTK(("de620_interrupt (%2.2X)\n", irq_status));
 603 
 604         if (irq_status & RXGOOD) {
 605                 do {
 606                         again = de620_rx_intr(dev);
 607                         PRINTK(("again=%d\n", again));
 608                 }
 609                 while (again && (++bogus_count < 100));
 610         }
 611 
 612         dev->tbusy = (de620_tx_buffs(dev) == (TXBF0 | TXBF1)); /* Boolean! */
 613 
 614         dev->interrupt = 0;
 615         sti();
 616         return;
 617 }
 618 
 619 /**************************************
 620  *
 621  * Get a packet from the adapter
 622  *
 623  * Send it "upstairs"
 624  *
 625  */
 626 static int
 627 de620_rx_intr(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 628 {
 629         struct header_buf {
 630                 byte            status;
 631                 byte            Rx_NextPage;
 632                 unsigned short  Rx_ByteCount;
 633         } header_buf;
 634         struct sk_buff *skb;
 635         int size;
 636         byte *buffer;
 637         byte pagelink;
 638         byte curr_page;
 639 
 640         PRINTK(("de620_rx_intr: next_rx_page = %d\n", next_rx_page));
 641 
 642         /* Tell the adapter that we are going to read data, and from where */
 643         de620_send_command(dev, W_CR | RRN);
 644         de620_set_register(dev, W_RSA1, next_rx_page);
 645         de620_set_register(dev, W_RSA0, 0);
 646 
 647         /* Deep breath, and away we goooooo */
 648         de620_read_block(dev, (byte *)&header_buf, sizeof(struct header_buf));
 649         PRINTK(("page status=0x%02x, nextpage=%d, packetsize=%d\n",
 650         header_buf.status, header_buf.Rx_NextPage, header_buf.Rx_ByteCount));
 651 
 652         /* Plausible page header? */
 653         pagelink = header_buf.Rx_NextPage;
 654         if ((pagelink < first_rx_page) || (last_rx_page < pagelink)) {
 655                 /* Ouch... Forget it! Skip all and start afresh... */
 656                 printk("%s: Ring overrun? Restoring...\n", dev->name);
 657                 /* You win some, you loose some. And sometimes plenty... */
 658                 adapter_init(dev);
 659                 ((struct netstats *)(dev->priv))->rx_over_errors++;
 660                 return 0;
 661         }
 662 
 663         /* OK, this look good, so far. Let's see if it's consistent... */
 664         /* Let's compute the start of the next packet, based on where we are */
 665         pagelink = next_rx_page +
 666                 ((header_buf.Rx_ByteCount + (4 - 1 + 0x100)) >> 8);
 667 
 668         /* Are we going to wrap around the page counter? */
 669         if (pagelink > last_rx_page)
 670                 pagelink -= (last_rx_page - first_rx_page + 1);
 671 
 672         /* Is the _computed_ next page number equal to what the adapter says? */
 673         if (pagelink != header_buf.Rx_NextPage) {
 674                 /* Naah, we'll skip this packet. Probably bogus data as well */
 675                 printk("%s: Page link out of sync! Restoring...\n", dev->name);
 676                 next_rx_page = header_buf.Rx_NextPage; /* at least a try... */
 677                 de620_send_command(dev, W_DUMMY);
 678                 de620_set_register(dev, W_NPRF, next_rx_page);
 679                 ((struct netstats *)(dev->priv))->rx_over_errors++;
 680                 return 0;
 681         }
 682         next_rx_page = pagelink;
 683 
 684         size = header_buf.Rx_ByteCount - 4;
 685         if ((size < RUNT) || (GIANT < size)) {
 686                 printk("%s: Illegal packet size: %d!\n", dev->name, size);
 687         }
 688         else { /* Good packet? */
 689                 skb = dev_alloc_skb(size+2);
 690                 if (skb == NULL) { /* Yeah, but no place to put it... */
 691                         printk("%s: Couldn't allocate a sk_buff of size %d.\n",
 692                                 dev->name, size);
 693                         ((struct netstats *)(dev->priv))->rx_dropped++;
 694                 }
 695                 else { /* Yep! Go get it! */
 696                         skb_reserve(skb,2);     /* Align */
 697                         skb->dev = dev;
 698                         skb->free = 1;
 699                         /* skb->data points to the start of sk_buff data area */
 700                         buffer = skb_put(skb,size);
 701                         /* copy the packet into the buffer */
 702                         de620_read_block(dev, buffer, size);
 703                         PRINTK(("Read %d bytes\n", size));
 704                         skb->protocol=eth_type_trans(skb,dev);
 705                         netif_rx(skb); /* deliver it "upstairs" */
 706                         /* count all receives */
 707                         ((struct netstats *)(dev->priv))->rx_packets++;
 708                 }
 709         }
 710 
 711         /* Let's peek ahead to see if we have read the last current packet */
 712         /* NOTE! We're _not_ checking the 'EMPTY'-flag! This seems better... */
 713         curr_page = de620_get_register(dev, R_CPR);
 714         de620_set_register(dev, W_NPRF, next_rx_page);
 715         PRINTK(("next_rx_page=%d CPR=%d\n", next_rx_page, curr_page));
 716 
 717         return (next_rx_page != curr_page); /* That was slightly tricky... */
 718 }
 719 
 720 /*********************************************
 721  *
 722  * Reset the adapter to a known state
 723  *
 724  */
 725 static int
 726 adapter_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 727 {
 728         int i;
 729         static int was_down = 0;
 730 
 731         if ((nic_data.Model == 3) || (nic_data.Model == 0)) { /* CT */
 732                 EIPRegister = NCTL0;
 733                 if (nic_data.Media != 1)
 734                         EIPRegister |= NIS0;    /* not BNC */
 735         }
 736         else if (nic_data.Model == 2) { /* UTP */
 737                 EIPRegister = NCTL0 | NIS0;
 738         }
 739 
 740         if (utp)
 741                 EIPRegister = NCTL0 | NIS0;
 742         if (bnc)
 743                 EIPRegister = NCTL0;
 744 
 745         de620_send_command(dev, W_CR | RNOP | CLEAR);
 746         de620_send_command(dev, W_CR | RNOP);
 747 
 748         de620_set_register(dev, W_SCR, SCR_DEF);
 749         /* disable recv to wait init */
 750         de620_set_register(dev, W_TCR, RXOFF);
 751 
 752         /* Set the node ID in the adapter */
 753         for (i = 0; i < 6; ++i) { /* W_PARn = 0xaa + n */
 754                 de620_set_register(dev, W_PAR0 + i, dev->dev_addr[i]);
 755         }
 756 
 757         de620_set_register(dev, W_EIP, EIPRegister);
 758 
 759         next_rx_page = first_rx_page = DE620_RX_START_PAGE;
 760         if (nic_data.RAM_Size)
 761                 last_rx_page = nic_data.RAM_Size - 1;
 762         else /* 64k RAM */
 763                 last_rx_page = 255;
 764 
 765         de620_set_register(dev, W_SPR, first_rx_page); /* Start Page Register*/
 766         de620_set_register(dev, W_EPR, last_rx_page);  /* End Page Register */
 767         de620_set_register(dev, W_CPR, first_rx_page);/*Current Page Register*/
 768         de620_send_command(dev, W_NPR | first_rx_page); /* Next Page Register*/
 769         de620_send_command(dev, W_DUMMY);
 770         de620_set_delay(dev);
 771 
 772         /* Final sanity check: Anybody out there? */
 773         /* Let's hope some bits from the statusregister make a good check */
 774 #define CHECK_MASK (  0 | TXSUC |  T16  |  0  | RXCRC | RXSHORT |  0  |  0  )
 775 #define CHECK_OK   (  0 |   0   |  0    |  0  |   0   |   0     |  0  |  0  )
 776         /* success:   X     0      0       X      0       0        X     X  */
 777         /* ignore:   EEDI                RXGOOD                   COLS  LNKS*/
 778 
 779         if (((i = de620_get_register(dev, R_STS)) & CHECK_MASK) != CHECK_OK) {
 780                 printk("Something has happened to the DE-620!  Please check it"
 781 #ifdef SHUTDOWN_WHEN_LOST
 782                         " and do a new ifconfig"
 783 #endif
 784                         "! (%02x)\n", i);
 785 #ifdef SHUTDOWN_WHEN_LOST
 786                 /* Goodbye, cruel world... */
 787                 dev->flags &= ~IFF_UP;
 788                 de620_close(dev);
 789 #endif
 790                 was_down = 1;
 791                 return 1; /* failed */
 792         }
 793         if (was_down) {
 794                 printk("Thanks, I feel much better now!\n");
 795                 was_down = 0;
 796         }
 797 
 798         /* All OK, go ahead... */
 799         de620_set_register(dev, W_TCR, TCR_DEF);
 800 
 801         return 0; /* all ok */
 802 }
 803 
 804 /******************************************************************************
 805  *
 806  * Only start-up code below
 807  *
 808  */
 809 /****************************************
 810  *
 811  * Check if there is a DE-620 connected
 812  */
 813 int
 814 de620_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 815 {
 816         static struct netstats de620_netstats;
 817         int i;
 818         byte checkbyte = 0xa5;
 819 
 820         if (de620_debug)
 821                 printk(version);
 822 
 823         printk("D-Link DE-620 pocket adapter");
 824 
 825         /* Initially, configure basic nibble mode, so we can read the EEPROM */
 826         NIC_Cmd = DEF_NIC_CMD;
 827         de620_set_register(dev, W_EIP, EIPRegister);
 828 
 829         /* Anybody out there? */
 830         de620_set_register(dev, W_CPR, checkbyte);
 831         checkbyte = de620_get_register(dev, R_CPR);
 832 
 833         if ((checkbyte != 0xa5) || (read_eeprom(dev) != 0)) {
 834                 printk(" not identified in the printer port\n");
 835                 return ENODEV;
 836         }
 837 
 838 #if 0 /* Not yet */
 839         if (check_region(DE620_IO, 3)) {
 840                 printk(", port 0x%x busy\n", DE620_IO);
 841                 return EBUSY;
 842         }
 843 #endif
 844         request_region(DE620_IO, 3, "de620");
 845 
 846         /* else, got it! */
 847         printk(", Ethernet Address: %2.2X",
 848                 dev->dev_addr[0] = nic_data.NodeID[0]);
 849         for (i = 1; i < ETH_ALEN; i++) {
 850                 printk(":%2.2X", dev->dev_addr[i] = nic_data.NodeID[i]);
 851                 dev->broadcast[i] = 0xff;
 852         }
 853 
 854         printk(" (%dk RAM,",
 855                 (nic_data.RAM_Size) ? (nic_data.RAM_Size >> 2) : 64);
 856 
 857         if (nic_data.Media == 1)
 858                 printk(" BNC)\n");
 859         else
 860                 printk(" UTP)\n");
 861 
 862         /* Initialize the device structure. */
 863         /*dev->priv = kmalloc(sizeof(struct netstats), GFP_KERNEL);*/
 864         dev->priv = &de620_netstats;
 865 
 866         memset(dev->priv, 0, sizeof(struct netstats));
 867         dev->get_stats = get_stats;
 868         dev->open = de620_open;
 869         dev->stop = de620_close;
 870         dev->hard_start_xmit = &de620_start_xmit;
 871         dev->set_multicast_list = &de620_set_multicast_list;
 872         dev->base_addr = DE620_IO;
 873         dev->irq = DE620_IRQ;
 874 
 875         ether_setup(dev);
 876         
 877         /* dump eeprom */
 878         if (de620_debug) {
 879                 printk("\nEEPROM contents:\n");
 880                 printk("RAM_Size = 0x%02X\n", nic_data.RAM_Size);
 881                 printk("NodeID = %02X:%02X:%02X:%02X:%02X:%02X\n",
 882                         nic_data.NodeID[0], nic_data.NodeID[1],
 883                         nic_data.NodeID[2], nic_data.NodeID[3],
 884                         nic_data.NodeID[4], nic_data.NodeID[5]);
 885                 printk("Model = %d\n", nic_data.Model);
 886                 printk("Media = %d\n", nic_data.Media);
 887                 printk("SCR = 0x%02x\n", nic_data.SCR);
 888         }
 889 
 890         return 0;
 891 }
 892 
 893 /**********************************
 894  *
 895  * Read info from on-board EEPROM
 896  *
 897  * Note: Bitwise serial I/O to/from the EEPROM vi the status _register_!
 898  */
 899 #define sendit(dev,data) de620_set_register(dev, W_EIP, data | EIPRegister);
 900 
 901 static unsigned short
 902 ReadAWord(struct device *dev, int from)
     /* [previous][next][first][last][top][bottom][index][help] */
 903 {
 904         unsigned short data;
 905         int nbits;
 906 
 907         /* cs   [__~~] SET SEND STATE */
 908         /* di   [____]                */
 909         /* sck  [_~~_]                */
 910         sendit(dev, 0); sendit(dev, 1); sendit(dev, 5); sendit(dev, 4);
 911 
 912         /* Send the 9-bit address from where we want to read the 16-bit word */
 913         for (nbits = 9; nbits > 0; --nbits, from <<= 1) {
 914                 if (from & 0x0100) { /* bit set? */
 915                         /* cs    [~~~~] SEND 1 */
 916                         /* di    [~~~~]        */
 917                         /* sck   [_~~_]        */
 918                         sendit(dev, 6); sendit(dev, 7); sendit(dev, 7); sendit(dev, 6);
 919                 }
 920                 else {
 921                         /* cs    [~~~~] SEND 0 */
 922                         /* di    [____]        */
 923                         /* sck   [_~~_]        */
 924                         sendit(dev, 4); sendit(dev, 5); sendit(dev, 5); sendit(dev, 4);
 925                 }
 926         }
 927 
 928         /* Shift in the 16-bit word. The bits appear serially in EEDI (=0x80) */
 929         for (data = 0, nbits = 16; nbits > 0; --nbits) {
 930                 /* cs    [~~~~] SEND 0 */
 931                 /* di    [____]        */
 932                 /* sck   [_~~_]        */
 933                 sendit(dev, 4); sendit(dev, 5); sendit(dev, 5); sendit(dev, 4);
 934                 data = (data << 1) | ((de620_get_register(dev, R_STS) & EEDI) >> 7);
 935         }
 936         /* cs    [____] RESET SEND STATE */
 937         /* di    [____]                  */
 938         /* sck   [_~~_]                  */
 939         sendit(dev, 0); sendit(dev, 1); sendit(dev, 1); sendit(dev, 0);
 940 
 941         return data;
 942 }
 943 
 944 static int
 945 read_eeprom(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 946 {
 947         unsigned short wrd;
 948 
 949         /* D-Link Ethernet addresses are in the series  00:80:c8:7X:XX:XX:XX */
 950         wrd = ReadAWord(dev, 0x1aa);    /* bytes 0 + 1 of NodeID */
 951         if (wrd != htons(0x0080)) /* Valid D-Link ether sequence? */
 952                 return -1; /* Nope, not a DE-620 */
 953         nic_data.NodeID[0] = wrd & 0xff;
 954         nic_data.NodeID[1] = wrd >> 8;
 955 
 956         wrd = ReadAWord(dev, 0x1ab);    /* bytes 2 + 3 of NodeID */
 957         if ((wrd & 0xff) != 0xc8) /* Valid D-Link ether sequence? */
 958                 return -1; /* Nope, not a DE-620 */
 959         nic_data.NodeID[2] = wrd & 0xff;
 960         nic_data.NodeID[3] = wrd >> 8;
 961 
 962         wrd = ReadAWord(dev, 0x1ac);    /* bytes 4 + 5 of NodeID */
 963         nic_data.NodeID[4] = wrd & 0xff;
 964         nic_data.NodeID[5] = wrd >> 8;
 965 
 966         wrd = ReadAWord(dev, 0x1ad);    /* RAM size in pages (256 bytes). 0 = 64k */
 967         nic_data.RAM_Size = (wrd >> 8);
 968 
 969         wrd = ReadAWord(dev, 0x1ae);    /* hardware model (CT = 3) */
 970         nic_data.Model = (wrd & 0xff);
 971 
 972         wrd = ReadAWord(dev, 0x1af); /* media (indicates BNC/UTP) */
 973         nic_data.Media = (wrd & 0xff);
 974 
 975         wrd = ReadAWord(dev, 0x1a8); /* System Configuration Register */
 976         nic_data.SCR = (wrd >> 8);
 977 
 978         return 0; /* no errors */
 979 }
 980 
 981 /******************************************************************************
 982  *
 983  * Loadable module skeleton
 984  *
 985  */
 986 #ifdef MODULE
 987 char kernel_version[] = UTS_RELEASE;
 988 static char nullname[8] = "";
 989 static struct device de620_dev = {
 990         nullname, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, de620_probe };
 991 
 992 int de620_io  = DE620_IO;
 993 int de620_irq = DE620_IRQ;
 994 
 995 int
 996 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 997 {
 998         de620_dev.base_addr = de620_io;
 999         de620_dev.irq       = de620_irq;
1000         if (register_netdev(&de620_dev) != 0)
1001                 return -EIO;
1002         return 0;
1003 }
1004 
1005 void
1006 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1007 {
1008         unregister_netdev(&de620_dev);
1009         release_region(de620_dev.base_addr, 3);
1010 }
1011 #endif /* MODULE */
1012 
1013 /*
1014  * (add '-DMODULE' when compiling as loadable module)
1015  *
1016  * compile-command:
1017  *      gcc -D__KERNEL__ -Wall -Wstrict-prototypes -O2 \
1018  *       -fomit-frame-pointer -m486 \
1019  *      -I/usr/src/linux/include -I../../net/inet -c de620.c
1020 */
1021 /*
1022  * Local variables:
1023  *  kernel-compile-command: "gcc -D__KERNEL__ -Ilinux/include -I../../net/inet -Wall -Wstrict-prototypes -O2 -m486 -c de620.c"
1024  *  module-compile-command: "gcc -D__KERNEL__ -DMODULE -Ilinux/include -I../../net/inet -Wall -Wstrict-prototypes -O2 -m486 -c de620.c"
1025  *  compile-command: "gcc -D__KERNEL__ -DMODULE -Ilinux/include -I../../net/inet -Wall -Wstrict-prototypes -O2 -m486 -c de620.c"
1026  * End:
1027  */

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