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

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