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

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