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.30 $ 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.30 $,  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 #include <linux/module.h>
 123 #include "../../tools/version.h"
 124 
 125 /* Constant definitions for the DE-620 registers, commands and bits */
 126 #include "de620.h"
 127 
 128 #define netstats enet_statistics
 129 typedef unsigned char byte;
 130 
 131 /*******************************************************
 132  *                                                     *
 133  * Definition of D-Link DE-620 Ethernet Pocket adapter *
 134  * See also "de620.h"                                  *
 135  *                                                     *
 136  *******************************************************/
 137 #ifndef DE620_IO /* Compile-time configurable */
 138 #define DE620_IO 0x378
 139 #endif
 140 
 141 #ifndef DE620_IRQ /* Compile-time configurable */
 142 #define DE620_IRQ       7
 143 #endif
 144 
 145 #define DATA_PORT       (DE620_IO)
 146 #define STATUS_PORT     (DE620_IO + 1)
 147 #define COMMAND_PORT    (DE620_IO + 2)
 148 
 149 #define RUNT 60         /* Too small Ethernet packet */
 150 #define GIANT 1514      /* largest legal size packet, no fcs */
 151 
 152 #ifdef DE620_DEBUG /* Compile-time configurable */
 153 #define PRINTK(x) if (de620_debug >= 2) printk x
 154 #else
 155 #define DE620_DEBUG 0
 156 #define PRINTK(x) /**/
 157 #endif
 158 
 159 /***********************************************
 160  *                                             *
 161  * Index to functions, as function prototypes. *
 162  *                                             *
 163  ***********************************************/
 164 
 165 /*
 166  * Routines used internally. (See also "convenience macros.. below")
 167  */
 168 
 169 /* Put in the device structure. */
 170 static int      de620_open(struct device *);
 171 static int      de620_close(struct device *);
 172 static struct netstats *get_stats(struct device *);
 173 static void     de620_set_multicast_list(struct device *, int, void *);
 174 static int      de620_start_xmit(struct sk_buff *, struct device *);
 175 
 176 /* Dispatch from interrupts. */
 177 static void     de620_interrupt(int);
 178 static int      de620_rx_intr(struct device *);
 179 
 180 /* Initialization */
 181 static int      adapter_init(struct device *);
 182 int             de620_probe(struct device *);
 183 static int      read_eeprom(void);
 184 
 185 
 186 /*
 187  * D-Link driver variables:
 188  */
 189 #define SCR_DEF NIBBLEMODE |INTON | SLEEP | AUTOTX
 190 #define TCR_DEF RXPB                    /* not used: | TXSUCINT | T16INT */
 191 #define DE620_RX_START_PAGE 12          /* 12 pages (=3k) reserved for tx */
 192 #define DEF_NIC_CMD IRQEN | ICEN | DS1
 193 
 194 extern struct device *irq2dev_map[16];
 195 unsigned int de620_debug = DE620_DEBUG;
 196 
 197 static volatile byte    NIC_Cmd;
 198 static volatile byte    next_rx_page;
 199 static byte             first_rx_page;
 200 static byte             last_rx_page;
 201 static byte             EIPRegister;
 202 
 203 static struct nic {
 204         byte    NodeID[6];
 205         byte    RAM_Size;
 206         byte    Model;
 207         byte    Media;
 208         byte    SCR;
 209 } nic_data;
 210 
 211 /**********************************************************
 212  *                                                        *
 213  * Convenience macros/functions for D-Link DE-620 adapter *
 214  *                                                        *
 215  **********************************************************/
 216 #define de620_tx_buffs() (inb(STATUS_PORT) & (TXBF0 | TXBF1))
 217 #define de620_flip_ds() NIC_Cmd ^= DS0 | DS1; outb(NIC_Cmd, COMMAND_PORT);
 218 
 219 /* Check for ready-status, and return a nibble (high 4 bits) for data input */
 220 #ifdef COUNT_LOOPS
 221 static int tot_cnt;
 222 #endif
 223 static inline byte
 224 de620_ready(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 225 {
 226         byte value;
 227         register short int cnt = 0;
 228 
 229         while ((((value = inb(STATUS_PORT)) & READY) == 0) && (cnt <= 1000))
 230                 ++cnt;
 231 
 232 #ifdef COUNT_LOOPS
 233         tot_cnt += cnt;
 234 #endif
 235         return value & 0xf0; /* nibble */
 236 }
 237 
 238 static inline void
 239 de620_send_command(byte cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 240 {
 241         de620_ready();
 242         if (cmd == W_DUMMY)
 243                 outb(NIC_Cmd, COMMAND_PORT);
 244 
 245         outb(cmd, DATA_PORT);
 246 
 247         outb(NIC_Cmd ^ CS0, COMMAND_PORT);
 248         de620_ready();
 249         outb(NIC_Cmd, COMMAND_PORT);
 250 }
 251 
 252 static inline void
 253 de620_put_byte(byte value)
     /* [previous][next][first][last][top][bottom][index][help] */
 254 {
 255         /* The de620_ready() makes 7 loops, on the average, on a DX2/66 */
 256         de620_ready();
 257         outb(value, DATA_PORT);
 258         de620_flip_ds();
 259 }
 260 
 261 static inline byte
 262 de620_read_byte(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 263 {
 264         byte value;
 265 
 266         /* The de620_ready() makes 7 loops, on the average, on a DX2/66 */
 267         value = de620_ready(); /* High nibble */
 268         de620_flip_ds();
 269         value |= de620_ready() >> 4; /* Low nibble */
 270         return value;
 271 }
 272 
 273 static inline void
 274 de620_write_block(byte *buffer, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 275 {
 276 #ifndef LOWSPEED
 277         byte uflip = NIC_Cmd ^ (DS0 | DS1);
 278         byte dflip = NIC_Cmd;
 279 #else /* LOWSPEED */
 280 #ifdef COUNT_LOOPS
 281         int bytes = count;
 282 #endif /* COUNT_LOOPS */
 283 #endif /* LOWSPEED */
 284 
 285 #ifdef LOWSPEED
 286 #ifdef COUNT_LOOPS
 287         tot_cnt = 0;
 288 #endif /* COUNT_LOOPS */
 289         /* No further optimization useful, the limit is in the adapter. */
 290         for ( ; count > 0; --count, ++buffer) {
 291                 de620_put_byte(*buffer);
 292         }
 293         de620_send_command(W_DUMMY);
 294 #ifdef COUNT_LOOPS
 295         /* trial debug output: loops per byte in de620_ready() */
 296         printk("WRITE(%d)\n", tot_cnt/((bytes?bytes:1)));
 297 #endif /* COUNT_LOOPS */
 298 #else /* not LOWSPEED */
 299         for ( ; count > 0; count -=2) {
 300                 outb(*buffer++, DATA_PORT);
 301                 outb(uflip, COMMAND_PORT);
 302                 outb(*buffer++, DATA_PORT);
 303                 outb(dflip, COMMAND_PORT);
 304         }
 305         de620_send_command(W_DUMMY);
 306 #endif /* LOWSPEED */
 307 }
 308 
 309 static inline void
 310 de620_read_block(byte *data, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 311 {
 312 #ifndef LOWSPEED
 313         byte value;
 314         byte uflip = NIC_Cmd ^ (DS0 | DS1);
 315         byte dflip = NIC_Cmd;
 316 #else /* LOWSPEED */
 317 #ifdef COUNT_LOOPS
 318         int bytes = count;
 319 
 320         tot_cnt = 0;
 321 #endif /* COUNT_LOOPS */
 322 #endif /* LOWSPEED */
 323 
 324 #ifdef LOWSPEED
 325         /* No further optimization useful, the limit is in the adapter. */
 326         while (count-- > 0) {
 327                 *data++ = de620_read_byte();
 328                 de620_flip_ds();
 329         }
 330 #ifdef COUNT_LOOPS
 331         /* trial debug output: loops per byte in de620_ready() */
 332         printk("READ(%d)\n", tot_cnt/(2*(bytes?bytes:1)));
 333 #endif /* COUNT_LOOPS */
 334 #else /* not LOWSPEED */
 335         while (count-- > 0) {
 336                 value = inb(STATUS_PORT) & 0xf0; /* High nibble */
 337                 outb(uflip, COMMAND_PORT);
 338                 *data++ = value | inb(STATUS_PORT) >> 4; /* Low nibble */
 339                 outb(dflip , COMMAND_PORT);
 340         }
 341 #endif /* LOWSPEED */
 342 }
 343 
 344 static inline void
 345 de620_set_delay(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 346 {
 347         de620_ready();
 348         outb(W_DFR, DATA_PORT);
 349         outb(NIC_Cmd ^ CS0, COMMAND_PORT);
 350 
 351         de620_ready();
 352 #ifdef LOWSPEED
 353         outb(WRITE_DELAY, DATA_PORT);
 354 #else
 355         outb(0, DATA_PORT);
 356 #endif
 357         de620_flip_ds();
 358 
 359         de620_ready();
 360 #ifdef LOWSPEED
 361         outb(READ_DELAY, DATA_PORT);
 362 #else
 363         outb(0, DATA_PORT);
 364 #endif
 365         de620_flip_ds();
 366 }
 367 
 368 static inline void
 369 de620_set_register(byte reg, byte value)
     /* [previous][next][first][last][top][bottom][index][help] */
 370 {
 371         de620_ready();
 372         outb(reg, DATA_PORT);
 373         outb(NIC_Cmd ^ CS0, COMMAND_PORT);
 374 
 375         de620_put_byte(value);
 376 }
 377 
 378 static inline byte
 379 de620_get_register(byte reg)
     /* [previous][next][first][last][top][bottom][index][help] */
 380 {
 381         byte value;
 382 
 383         de620_send_command(reg);
 384         value = de620_read_byte();
 385         de620_send_command(W_DUMMY);
 386 
 387         return value;
 388 }
 389 
 390 /*********************************************************************
 391  *
 392  * Open/initialize the board.
 393  *
 394  * This routine should set everything up anew at each open, even
 395  * registers that "should" only need to be set once at boot, so that
 396  * there is a non-reboot way to recover if something goes wrong.
 397  *
 398  */
 399 static int
 400 de620_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 401 {
 402         if (request_irq(DE620_IRQ, de620_interrupt)) {
 403                 printk ("%s: unable to get IRQ %d\n", dev->name, DE620_IRQ);
 404                 return 1;
 405         }
 406         irq2dev_map[DE620_IRQ] = dev;
 407 
 408 #ifdef MODULE
 409         MOD_INC_USE_COUNT;
 410 #endif
 411         if (adapter_init(dev)) {
 412                 return 1;
 413         }
 414         dev->start = 1;
 415         return 0;
 416 }
 417 
 418 /************************************************
 419  *
 420  * The inverse routine to de620_open().
 421  *
 422  */
 423 static int
 424 de620_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 425 {
 426         /* disable recv */
 427         de620_set_register(W_TCR, RXOFF);
 428 
 429         free_irq(DE620_IRQ);
 430         irq2dev_map[DE620_IRQ] = NULL;
 431 
 432         dev->start = 0;
 433 #ifdef MODULE
 434         MOD_DEC_USE_COUNT;
 435 #endif
 436         return 0;
 437 }
 438 
 439 /*********************************************
 440  *
 441  * Return current statistics
 442  *
 443  */
 444 static struct netstats *
 445 get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 446 {
 447         return (struct netstats *)(dev->priv);
 448 }
 449 
 450 /*********************************************
 451  *
 452  * Set or clear the multicast filter for this adaptor.
 453  * (no real multicast implemented for the DE-620, but she can be promiscuous...)
 454  *
 455  * num_addrs == -1      Promiscuous mode, receive all packets
 456  * num_addrs == 0       Normal mode, clear multicast list
 457  * num_addrs > 0        Multicast mode, receive normal and MC packets, and do
 458  *                      best-effort filtering.
 459  */
 460 static void
 461 de620_set_multicast_list(struct device *dev, int num_addrs, void *addrs)
     /* [previous][next][first][last][top][bottom][index][help] */
 462 {
 463         if (num_addrs) { /* Enable promiscuous mode */
 464                 de620_set_register(W_TCR, (TCR_DEF & ~RXPBM) | RXALL);
 465         }
 466         else { /* Disable promiscuous mode, use normal mode */
 467                 de620_set_register(W_TCR, TCR_DEF);
 468         }
 469 }
 470 
 471 /*******************************************************
 472  *
 473  * Copy a buffer to the adapter transmit page memory.
 474  * Start sending.
 475  */
 476 static int
 477 de620_start_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 478 {
 479         unsigned long flags;
 480         int len;
 481         int tickssofar;
 482         byte *buffer = skb->data;
 483         byte using_txbuf;
 484 
 485         /*
 486          * If some higher layer thinks we've missed a
 487          * tx-done interrupt we are passed NULL.
 488          * Caution: dev_tint() handles the cli()/sti() itself.
 489          */
 490 
 491         if (skb == NULL) {
 492                 dev_tint(dev);
 493                 return 0;
 494         }
 495 
 496         using_txbuf = de620_tx_buffs(); /* Peek at the adapter */
 497         dev->tbusy = (using_txbuf == (TXBF0 | TXBF1)); /* Boolean! */
 498 
 499         if (dev->tbusy) {       /* Do timeouts, to avoid hangs. */
 500                 tickssofar = jiffies - dev->trans_start;
 501 
 502                 if (tickssofar < 5)
 503                         return 1;
 504 
 505                 /* else */
 506                 printk("%s: transmit timed out (%d), %s?\n",
 507                         dev->name,
 508                         tickssofar,
 509                         "network cable problem"
 510                         );
 511                 /* Restart the adapter. */
 512                 if (adapter_init(dev)) /* maybe close it */
 513                         return 1;
 514         }
 515 
 516         if ((len = skb->len) < RUNT)
 517                 len = RUNT;
 518         if (len & 1) /* send an even number of bytes */
 519                 ++len;
 520 
 521         /* Start real output */
 522         save_flags(flags);
 523         cli();
 524 
 525         PRINTK(("de620_start_xmit: len=%d, bufs 0x%02x\n",
 526                 (int)skb->len, using_txbuf));
 527 
 528         /* select a free tx buffer. if there is one... */
 529         switch (using_txbuf) {
 530         default: /* both are free: use TXBF0 */
 531         case TXBF1: /* use TXBF0 */
 532                 de620_send_command(W_CR | RW0);
 533                 using_txbuf |= TXBF0;
 534                 break;
 535 
 536         case TXBF0: /* use TXBF1 */
 537                 de620_send_command(W_CR | RW1);
 538                 using_txbuf |= TXBF1;
 539                 break;
 540 
 541         case (TXBF0 | TXBF1): /* NONE!!! */
 542                 printk("de620: Ouch! No tx-buffer available!\n");
 543                 restore_flags(flags);
 544                 return 1;
 545                 break;
 546         }
 547         de620_write_block(buffer, len);
 548 
 549         dev->trans_start = jiffies;
 550         dev->tbusy = (using_txbuf == (TXBF0 | TXBF1)); /* Boolean! */
 551 
 552         ((struct netstats *)(dev->priv))->tx_packets++;
 553         
 554         restore_flags(flags); /* interrupts maybe back on */
 555         
 556         dev_kfree_skb (skb, FREE_WRITE);
 557 
 558         return 0;
 559 }
 560 
 561 /*****************************************************
 562  *
 563  * Handle the network interface interrupts.
 564  *
 565  */
 566 static void
 567 de620_interrupt(int reg_ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 568 {
 569         int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
 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 adresses 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] */