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

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