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, struct pt_regs *);
 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 irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 569 {
 570         struct device *dev = irq2dev_map[irq];
 571         byte irq_status;
 572         int bogus_count = 0;
 573         int again = 0;
 574 
 575         /* This might be deleted now, no crummy drivers present :-) Or..? */
 576         if ((dev == NULL) || (DE620_IRQ != irq)) {
 577                 printk("%s: bogus interrupt %d\n", dev?dev->name:"DE620", irq);
 578                 return;
 579         }
 580 
 581         cli();
 582         dev->interrupt = 1;
 583 
 584         /* Read the status register (_not_ the status port) */
 585         irq_status = de620_get_register(R_STS);
 586 
 587         PRINTK(("de620_interrupt (%2.2X)\n", irq_status));
 588 
 589         if (irq_status & RXGOOD) {
 590                 do {
 591                         again = de620_rx_intr(dev);
 592                         PRINTK(("again=%d\n", again));
 593                 }
 594                 while (again && (++bogus_count < 100));
 595         }
 596 
 597         dev->tbusy = (de620_tx_buffs() == (TXBF0 | TXBF1)); /* Boolean! */
 598 
 599         dev->interrupt = 0;
 600         sti();
 601         return;
 602 }
 603 
 604 /**************************************
 605  *
 606  * Get a packet from the adapter
 607  *
 608  * Send it "upstairs"
 609  *
 610  */
 611 static int
 612 de620_rx_intr(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 613 {
 614         struct header_buf {
 615                 byte            status;
 616                 byte            Rx_NextPage;
 617                 unsigned short  Rx_ByteCount;
 618         } header_buf;
 619         struct sk_buff *skb;
 620         int size;
 621         byte *buffer;
 622         byte pagelink;
 623         byte curr_page;
 624 
 625         PRINTK(("de620_rx_intr: next_rx_page = %d\n", next_rx_page));
 626 
 627         /* Tell the adapter that we are going to read data, and from where */
 628         de620_send_command(W_CR | RRN);
 629         de620_set_register(W_RSA1, next_rx_page);
 630         de620_set_register(W_RSA0, 0);
 631 
 632         /* Deep breath, and away we goooooo */
 633         de620_read_block((byte *)&header_buf, sizeof(struct header_buf));
 634         PRINTK(("page status=0x%02x, nextpage=%d, packetsize=%d\n",
 635         header_buf.status, header_buf.Rx_NextPage, header_buf.Rx_ByteCount));
 636 
 637         /* Plausible page header? */
 638         pagelink = header_buf.Rx_NextPage;
 639         if ((pagelink < first_rx_page) || (last_rx_page < pagelink)) {
 640                 /* Ouch... Forget it! Skip all and start afresh... */
 641                 printk("%s: Ring overrun? Restoring...\n", dev->name);
 642                 /* You win some, you loose some. And sometimes plenty... */
 643                 adapter_init(dev);
 644                 ((struct netstats *)(dev->priv))->rx_over_errors++;
 645                 return 0;
 646         }
 647 
 648         /* OK, this look good, so far. Let's see if it's consistent... */
 649         /* Let's compute the start of the next packet, based on where we are */
 650         pagelink = next_rx_page +
 651                 ((header_buf.Rx_ByteCount + (4 - 1 + 0x100)) >> 8);
 652 
 653         /* Are we going to wrap around the page counter? */
 654         if (pagelink > last_rx_page)
 655                 pagelink -= (last_rx_page - first_rx_page + 1);
 656 
 657         /* Is the _computed_ next page number equal to what the adapter says? */
 658         if (pagelink != header_buf.Rx_NextPage) {
 659                 /* Naah, we'll skip this packet. Probably bogus data as well */
 660                 printk("%s: Page link out of sync! Restoring...\n", dev->name);
 661                 next_rx_page = header_buf.Rx_NextPage; /* at least a try... */
 662                 de620_send_command(W_DUMMY);
 663                 de620_set_register(W_NPRF, next_rx_page);
 664                 ((struct netstats *)(dev->priv))->rx_over_errors++;
 665                 return 0;
 666         }
 667         next_rx_page = pagelink;
 668 
 669         size = header_buf.Rx_ByteCount - 4;
 670         if ((size < RUNT) || (GIANT < size)) {
 671                 printk("%s: Illegal packet size: %d!\n", dev->name, size);
 672         }
 673         else { /* Good packet? */
 674                 skb = alloc_skb(size, GFP_ATOMIC);
 675                 if (skb == NULL) { /* Yeah, but no place to put it... */
 676                         printk("%s: Couldn't allocate a sk_buff of size %d.\n",
 677                                 dev->name, size);
 678                         ((struct netstats *)(dev->priv))->rx_dropped++;
 679                 }
 680                 else { /* Yep! Go get it! */
 681                         skb->len = size; skb->dev = dev; skb->free = 1;
 682                         /* skb->data points to the start of sk_buff data area */
 683                         buffer = skb->data;
 684                         /* copy the packet into the buffer */
 685                         de620_read_block(buffer, size);
 686                         PRINTK(("Read %d bytes\n", size));
 687                         netif_rx(skb); /* deliver it "upstairs" */
 688                         /* count all receives */
 689                         ((struct netstats *)(dev->priv))->rx_packets++;
 690                 }
 691         }
 692 
 693         /* Let's peek ahead to see if we have read the last current packet */
 694         /* NOTE! We're _not_ checking the 'EMPTY'-flag! This seems better... */
 695         curr_page = de620_get_register(R_CPR);
 696         de620_set_register(W_NPRF, next_rx_page);
 697         PRINTK(("next_rx_page=%d CPR=%d\n", next_rx_page, curr_page));
 698 
 699         return (next_rx_page != curr_page); /* That was slightly tricky... */
 700 }
 701 
 702 /*********************************************
 703  *
 704  * Reset the adapter to a known state
 705  *
 706  */
 707 static int
 708 adapter_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 709 {
 710         int i;
 711         static int was_down = 0;
 712 
 713         if ((nic_data.Model == 3) || (nic_data.Model == 0)) { /* CT */
 714                 EIPRegister = NCTL0;
 715                 if (nic_data.Media != 1)
 716                         EIPRegister |= NIS0;    /* not BNC */
 717         }
 718         else if (nic_data.Model == 2) { /* UTP */
 719                 EIPRegister = NCTL0 | NIS0;
 720         }
 721 
 722         de620_send_command(W_CR | RNOP | CLEAR);
 723         de620_send_command(W_CR | RNOP);
 724 
 725         de620_set_register(W_SCR, SCR_DEF);
 726         /* disable recv to wait init */
 727         de620_set_register(W_TCR, RXOFF);
 728 
 729         /* Set the node ID in the adapter */
 730         for (i = 0; i < 6; ++i) { /* W_PARn = 0xaa + n */
 731                 de620_set_register(W_PAR0 + i, dev->dev_addr[i]);
 732         }
 733 
 734         de620_set_register(W_EIP, EIPRegister);
 735 
 736         next_rx_page = first_rx_page = DE620_RX_START_PAGE;
 737         if (nic_data.RAM_Size)
 738                 last_rx_page = nic_data.RAM_Size - 1;
 739         else /* 64k RAM */
 740                 last_rx_page = 255;
 741 
 742         de620_set_register(W_SPR, first_rx_page); /* Start Page Register */
 743         de620_set_register(W_EPR, last_rx_page);  /* End Page Register */
 744         de620_set_register(W_CPR, first_rx_page); /* Current Page Register */
 745         de620_send_command(W_NPR | first_rx_page); /* Next Page Register */
 746         de620_send_command(W_DUMMY);
 747         de620_set_delay();
 748 
 749         /* Final sanity check: Anybody out there? */
 750         /* Let's hope some bits from the statusregister make a good check */
 751 #define CHECK_MASK (  0 | TXSUC |  T16  |  0  | RXCRC | RXSHORT |  0  |  0  )
 752 #define CHECK_OK   (  0 |   0   |  0    |  0  |   0   |   0     |  0  |  0  )
 753         /* success:   X     0      0       X      0       0        X     X  */
 754         /* ignore:   EEDI                RXGOOD                   COLS  LNKS*/
 755 
 756         if (((i = de620_get_register(R_STS)) & CHECK_MASK) != CHECK_OK) {
 757                 printk("Something has happened to the DE-620!  Please check it"
 758 #ifdef SHUTDOWN_WHEN_LOST
 759                         " and do a new ifconfig"
 760 #endif
 761                         "! (%02x)\n", i);
 762 #ifdef SHUTDOWN_WHEN_LOST
 763                 /* Goodbye, cruel world... */
 764                 dev->flags &= ~IFF_UP;
 765                 de620_close(dev);
 766 #endif
 767                 was_down = 1;
 768                 return 1; /* failed */
 769         }
 770         if (was_down) {
 771                 printk("Thanks, I feel much better now!\n");
 772                 was_down = 0;
 773         }
 774 
 775         /* All OK, go ahead... */
 776         de620_set_register(W_TCR, TCR_DEF);
 777 
 778         return 0; /* all ok */
 779 }
 780 
 781 /******************************************************************************
 782  *
 783  * Only start-up code below
 784  *
 785  */
 786 /****************************************
 787  *
 788  * Check if there is a DE-620 connected
 789  */
 790 int
 791 de620_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 792 {
 793         static struct netstats de620_netstats;
 794         int i;
 795         byte checkbyte = 0xa5;
 796 
 797         if (de620_debug)
 798                 printk(version);
 799 
 800         printk("D-Link DE-620 pocket adapter");
 801 
 802         /* Initially, configure basic nibble mode, so we can read the EEPROM */
 803         NIC_Cmd = DEF_NIC_CMD;
 804         de620_set_register(W_EIP, EIPRegister);
 805 
 806         /* Anybody out there? */
 807         de620_set_register(W_CPR, checkbyte);
 808         checkbyte = de620_get_register(R_CPR);
 809 
 810         if ((checkbyte != 0xa5) || (read_eeprom() != 0)) {
 811                 printk(" not identified in the printer port\n");
 812                 return ENODEV;
 813         }
 814 
 815         /* else, got it! */
 816         printk(", Ethernet Address: %2.2X",
 817                 dev->dev_addr[0] = nic_data.NodeID[0]);
 818         for (i = 1; i < ETH_ALEN; i++) {
 819                 printk(":%2.2X", dev->dev_addr[i] = nic_data.NodeID[i]);
 820                 dev->broadcast[i] = 0xff;
 821         }
 822 
 823         printk(" (%dk RAM,",
 824                 (nic_data.RAM_Size) ? (nic_data.RAM_Size >> 2) : 64);
 825 
 826         if (nic_data.Media == 1)
 827                 printk(" BNC)\n");
 828         else
 829                 printk(" UTP)\n");
 830 
 831         /* Initialize the device structure. */
 832         /*dev->priv = kmalloc(sizeof(struct netstats), GFP_KERNEL);*/
 833         dev->priv = &de620_netstats;
 834 
 835         memset(dev->priv, 0, sizeof(struct netstats));
 836         dev->get_stats = get_stats;
 837         dev->open = de620_open;
 838         dev->stop = de620_close;
 839         dev->hard_start_xmit = &de620_start_xmit;
 840         dev->set_multicast_list = &de620_set_multicast_list;
 841         dev->base_addr = DE620_IO;
 842         dev->irq = DE620_IRQ;
 843 
 844         ether_setup(dev);
 845         
 846         /* dump eeprom */
 847         if (de620_debug) {
 848                 printk("\nEEPROM contents:\n");
 849                 printk("RAM_Size = 0x%02X\n", nic_data.RAM_Size);
 850                 printk("NodeID = %02X:%02X:%02X:%02X:%02X:%02X\n",
 851                         nic_data.NodeID[0], nic_data.NodeID[1],
 852                         nic_data.NodeID[2], nic_data.NodeID[3],
 853                         nic_data.NodeID[4], nic_data.NodeID[5]);
 854                 printk("Model = %d\n", nic_data.Model);
 855                 printk("Media = %d\n", nic_data.Media);
 856                 printk("SCR = 0x%02x\n", nic_data.SCR);
 857         }
 858 
 859         return 0;
 860 }
 861 
 862 /**********************************
 863  *
 864  * Read info from on-board EEPROM
 865  *
 866  * Note: Bitwise serial I/O to/from the EEPROM vi the status _register_!
 867  */
 868 #define sendit(data) de620_set_register(W_EIP, data | EIPRegister);
 869 
 870 static unsigned short
 871 ReadAWord(int from)
     /* [previous][next][first][last][top][bottom][index][help] */
 872 {
 873         unsigned short data;
 874         int nbits;
 875 
 876         /* cs   [__~~] SET SEND STATE */
 877         /* di   [____]                */
 878         /* sck  [_~~_]                */
 879         sendit(0); sendit(1); sendit(5); sendit(4);
 880 
 881         /* Send the 9-bit address from where we want to read the 16-bit word */
 882         for (nbits = 9; nbits > 0; --nbits, from <<= 1) {
 883                 if (from & 0x0100) { /* bit set? */
 884                         /* cs    [~~~~] SEND 1 */
 885                         /* di    [~~~~]        */
 886                         /* sck   [_~~_]        */
 887                         sendit(6); sendit(7); sendit(7); sendit(6);
 888                 }
 889                 else {
 890                         /* cs    [~~~~] SEND 0 */
 891                         /* di    [____]        */
 892                         /* sck   [_~~_]        */
 893                         sendit(4); sendit(5); sendit(5); sendit(4);
 894                 }
 895         }
 896 
 897         /* Shift in the 16-bit word. The bits appear serially in EEDI (=0x80) */
 898         for (data = 0, nbits = 16; nbits > 0; --nbits) {
 899                 /* cs    [~~~~] SEND 0 */
 900                 /* di    [____]        */
 901                 /* sck   [_~~_]        */
 902                 sendit(4); sendit(5); sendit(5); sendit(4);
 903                 data = (data << 1) | ((de620_get_register(R_STS) & EEDI) >> 7);
 904         }
 905         /* cs    [____] RESET SEND STATE */
 906         /* di    [____]                  */
 907         /* sck   [_~~_]                  */
 908         sendit(0); sendit(1); sendit(1); sendit(0);
 909 
 910         return data;
 911 }
 912 
 913 static int
 914 read_eeprom(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 915 {
 916         unsigned short wrd;
 917 
 918         /* D-Link Ethernet addresses are in the series  00:80:c8:7X:XX:XX:XX */
 919         wrd = ReadAWord(0x1aa); /* bytes 0 + 1 of NodeID */
 920         if (wrd != htons(0x0080)) /* Valid D-Link ether sequence? */
 921                 return -1; /* Nope, not a DE-620 */
 922         nic_data.NodeID[0] = wrd & 0xff;
 923         nic_data.NodeID[1] = wrd >> 8;
 924 
 925         wrd = ReadAWord(0x1ab); /* bytes 2 + 3 of NodeID */
 926         if ((wrd & 0xff) != 0xc8) /* Valid D-Link ether sequence? */
 927                 return -1; /* Nope, not a DE-620 */
 928         nic_data.NodeID[2] = wrd & 0xff;
 929         nic_data.NodeID[3] = wrd >> 8;
 930 
 931         wrd = ReadAWord(0x1ac); /* bytes 4 + 5 of NodeID */
 932         nic_data.NodeID[4] = wrd & 0xff;
 933         nic_data.NodeID[5] = wrd >> 8;
 934 
 935         wrd = ReadAWord(0x1ad); /* RAM size in pages (256 bytes). 0 = 64k */
 936         nic_data.RAM_Size = (wrd >> 8);
 937 
 938         wrd = ReadAWord(0x1ae); /* hardware model (CT = 3) */
 939         nic_data.Model = (wrd & 0xff);
 940 
 941         wrd = ReadAWord(0x1af); /* media (indicates BNC/UTP) */
 942         nic_data.Media = (wrd & 0xff);
 943 
 944         wrd = ReadAWord(0x1a8); /* System Configuration Register */
 945         nic_data.SCR = (wrd >> 8);
 946 
 947         return 0; /* no errors */
 948 }
 949 
 950 /******************************************************************************
 951  *
 952  * Loadable module skeleton
 953  *
 954  */
 955 #ifdef MODULE
 956 char kernel_version[] = UTS_RELEASE;
 957 static char nullname[8];
 958 static struct device de620_dev = {
 959         nullname, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, de620_probe };
 960 
 961 int
 962 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 963 {
 964         if (register_netdev(&de620_dev) != 0)
 965                 return -EIO;
 966         return 0;
 967 }
 968 
 969 void
 970 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 971 {
 972         if (MOD_IN_USE)
 973                 printk("de620: device busy, remove delayed\n");
 974         else
 975                 unregister_netdev(&de620_dev);
 976 }
 977 #endif /* MODULE */
 978 
 979 /*
 980  * (add '-DMODULE' when compiling as loadable module)
 981  *
 982  * compile-command:
 983  *      gcc -D__KERNEL__ -Wall -Wstrict-prototypes -O2 \
 984  *       -fomit-frame-pointer -m486 \
 985  *      -I/usr/src/linux/include -I../../net/inet -c de620.c
 986  */

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