de620.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:28k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * de620.c $Revision: 1.40 $ BETA
  3.  *
  4.  *
  5.  * Linux driver for the D-Link DE-620 Ethernet pocket adapter.
  6.  *
  7.  * Portions (C) Copyright 1993, 1994 by Bjorn Ekwall <bj0rn@blox.se>
  8.  *
  9.  * Based on adapter information gathered from DOS packetdriver
  10.  * sources from D-Link Inc:  (Special thanks to Henry Ngai of D-Link.)
  11.  * Portions (C) Copyright D-Link SYSTEM Inc. 1991, 1992
  12.  * Copyright, 1988, Russell Nelson, Crynwr Software
  13.  *
  14.  * Adapted to the sample network driver core for linux,
  15.  * written by: Donald Becker <becker@super.org>
  16.  * (Now at <becker@scyld.com>)
  17.  *
  18.  * Valuable assistance from:
  19.  * J. Joshua Kopper <kopper@rtsg.mot.com>
  20.  * Olav Kvittem <Olav.Kvittem@uninett.no>
  21.  * Germano Caronni <caronni@nessie.cs.id.ethz.ch>
  22.  * Jeremy Fitzhardinge <jeremy@suite.sw.oz.au>
  23.  *
  24.  *****************************************************************************/
  25. /*
  26.  * This program is free software; you can redistribute it and/or modify
  27.  * it under the terms of the GNU General Public License as published by
  28.  * the Free Software Foundation; either version 2, or (at your option)
  29.  * any later version.
  30.  *
  31.  * This program is distributed in the hope that it will be useful,
  32.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34.  * GNU General Public License for more details.
  35.  *
  36.  * You should have received a copy of the GNU General Public License
  37.  * along with this program; if not, write to the Free Software
  38.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  39.  *
  40.  *****************************************************************************/
  41. static const char version[] =
  42. "de620.c: $Revision: 1.40 $,  Bjorn Ekwall <bj0rn@blox.se>n";
  43. /***********************************************************************
  44.  *
  45.  * "Tuning" section.
  46.  *
  47.  * Compile-time options: (see below for descriptions)
  48.  * -DDE620_IO=0x378 (lpt1)
  49.  * -DDE620_IRQ=7 (lpt1)
  50.  * -DDE602_DEBUG=...
  51.  * -DSHUTDOWN_WHEN_LOST
  52.  * -DCOUNT_LOOPS
  53.  * -DLOWSPEED
  54.  * -DREAD_DELAY
  55.  * -DWRITE_DELAY
  56.  */
  57. /*
  58.  * This driver assumes that the printer port is a "normal",
  59.  * dumb, uni-directional port!
  60.  * If your port is "fancy" in any way, please try to set it to "normal"
  61.  * with your BIOS setup.  I have no access to machines with bi-directional
  62.  * ports, so I can't test such a driver :-(
  63.  * (Yes, I _know_ it is possible to use DE620 with bidirectional ports...)
  64.  *
  65.  * There are some clones of DE620 out there, with different names.
  66.  * If the current driver does not recognize a clone, try to change
  67.  * the following #define to:
  68.  *
  69.  * #define DE620_CLONE 1
  70.  */
  71. #define DE620_CLONE 0
  72. /*
  73.  * If the adapter has problems with high speeds, enable this #define
  74.  * otherwise full printerport speed will be attempted.
  75.  *
  76.  * You can tune the READ_DELAY/WRITE_DELAY below if you enable LOWSPEED
  77.  *
  78. #define LOWSPEED
  79.  */
  80. #ifndef READ_DELAY
  81. #define READ_DELAY 100 /* adapter internal read delay in 100ns units */
  82. #endif
  83. #ifndef WRITE_DELAY
  84. #define WRITE_DELAY 100 /* adapter internal write delay in 100ns units */
  85. #endif
  86. /*
  87.  * Enable this #define if you want the adapter to do a "ifconfig down" on
  88.  * itself when we have detected that something is possibly wrong with it.
  89.  * The default behaviour is to retry with "adapter_init()" until success.
  90.  * This should be used for debugging purposes only.
  91.  *
  92. #define SHUTDOWN_WHEN_LOST
  93.  */
  94. /*
  95.  * Enable debugging by "-DDE620_DEBUG=3" when compiling,
  96.  * OR by enabling the following #define
  97.  *
  98.  * use 0 for production, 1 for verification, >2 for debug
  99.  *
  100. #define DE620_DEBUG 3
  101.  */
  102. #ifdef LOWSPEED
  103. /*
  104.  * Enable this #define if you want to see debugging output that show how long
  105.  * we have to wait before the DE-620 is ready for the next read/write/command.
  106.  *
  107. #define COUNT_LOOPS
  108.  */
  109. #endif
  110. #include <linux/module.h>
  111. #include <linux/kernel.h>
  112. #include <linux/sched.h>
  113. #include <linux/types.h>
  114. #include <linux/fcntl.h>
  115. #include <linux/string.h>
  116. #include <linux/interrupt.h>
  117. #include <linux/ioport.h>
  118. #include <asm/io.h>
  119. #include <linux/in.h>
  120. #include <linux/ptrace.h>
  121. #include <asm/system.h>
  122. #include <linux/errno.h>
  123. #include <linux/init.h>
  124. #include <linux/inet.h>
  125. #include <linux/netdevice.h>
  126. #include <linux/etherdevice.h>
  127. #include <linux/skbuff.h>
  128. /* Constant definitions for the DE-620 registers, commands and bits */
  129. #include "de620.h"
  130. typedef unsigned char byte;
  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. #ifndef DE620_IRQ /* Compile-time configurable */
  141. #define DE620_IRQ 7
  142. #endif
  143. #define DATA_PORT (dev->base_addr)
  144. #define STATUS_PORT (dev->base_addr + 1)
  145. #define COMMAND_PORT (dev->base_addr + 2)
  146. #define RUNT 60 /* Too small Ethernet packet */
  147. #define GIANT 1514 /* largest legal size packet, no fcs */
  148. #ifdef DE620_DEBUG /* Compile-time configurable */
  149. #define PRINTK(x) if (de620_debug >= 2) printk x
  150. #else
  151. #define DE620_DEBUG 0
  152. #define PRINTK(x) /**/
  153. #endif
  154. /*
  155.  * Force media with insmod:
  156.  * insmod de620.o bnc=1
  157.  * or
  158.  * insmod de620.o utp=1
  159.  *
  160.  * Force io and/or irq with insmod:
  161.  * insmod de620.o io=0x378 irq=7
  162.  *
  163.  * Make a clone skip the Ethernet-address range check:
  164.  * insmod de620.o clone=1
  165.  */
  166. static int bnc;
  167. static int utp;
  168. static int io  = DE620_IO;
  169. static int irq = DE620_IRQ;
  170. static int clone = DE620_CLONE;
  171. static unsigned int de620_debug = DE620_DEBUG;
  172. MODULE_PARM(bnc, "i");
  173. MODULE_PARM(utp, "i");
  174. MODULE_PARM(io, "i");
  175. MODULE_PARM(irq, "i");
  176. MODULE_PARM(clone, "i");
  177. MODULE_PARM(de620_debug, "i");
  178. MODULE_PARM_DESC(bnc, "DE-620 set BNC medium (0-1)");
  179. MODULE_PARM_DESC(utp, "DE-620 set UTP medium (0-1)");
  180. MODULE_PARM_DESC(io, "DE-620 I/O base address,required");
  181. MODULE_PARM_DESC(irq, "DE-620 IRQ number,required");
  182. MODULE_PARM_DESC(clone, "Check also for non-D-Link DE-620 clones (0-1)");
  183. MODULE_PARM_DESC(de620_debug, "DE-620 debug level (0-2)");
  184. /***********************************************
  185.  *                                             *
  186.  * Index to functions, as function prototypes. *
  187.  *                                             *
  188.  ***********************************************/
  189. /*
  190.  * Routines used internally. (See also "convenience macros.. below")
  191.  */
  192. /* Put in the device structure. */
  193. static int de620_open(struct net_device *);
  194. static int de620_close(struct net_device *);
  195. static struct net_device_stats *get_stats(struct net_device *);
  196. static void de620_set_multicast_list(struct net_device *);
  197. static int de620_start_xmit(struct sk_buff *, struct net_device *);
  198. /* Dispatch from interrupts. */
  199. static void de620_interrupt(int, void *, struct pt_regs *);
  200. static int de620_rx_intr(struct net_device *);
  201. /* Initialization */
  202. static int adapter_init(struct net_device *);
  203. int de620_probe(struct net_device *);
  204. static int read_eeprom(struct net_device *);
  205. /*
  206.  * D-Link driver variables:
  207.  */
  208. #define SCR_DEF NIBBLEMODE |INTON | SLEEP | AUTOTX
  209. #define TCR_DEF RXPB /* not used: | TXSUCINT | T16INT */
  210. #define DE620_RX_START_PAGE 12 /* 12 pages (=3k) reserved for tx */
  211. #define DEF_NIC_CMD IRQEN | ICEN | DS1
  212. static volatile byte NIC_Cmd;
  213. static volatile byte next_rx_page;
  214. static byte first_rx_page;
  215. static byte last_rx_page;
  216. static byte EIPRegister;
  217. static struct nic {
  218. byte NodeID[6];
  219. byte RAM_Size;
  220. byte Model;
  221. byte Media;
  222. byte SCR;
  223. } nic_data;
  224. /**********************************************************
  225.  *                                                        *
  226.  * Convenience macros/functions for D-Link DE-620 adapter *
  227.  *                                                        *
  228.  **********************************************************/
  229. #define de620_tx_buffs(dd) (inb(STATUS_PORT) & (TXBF0 | TXBF1))
  230. #define de620_flip_ds(dd) NIC_Cmd ^= DS0 | DS1; outb(NIC_Cmd, COMMAND_PORT);
  231. /* Check for ready-status, and return a nibble (high 4 bits) for data input */
  232. #ifdef COUNT_LOOPS
  233. static int tot_cnt;
  234. #endif
  235. static inline byte
  236. de620_ready(struct net_device *dev)
  237. {
  238. byte value;
  239. register short int cnt = 0;
  240. while ((((value = inb(STATUS_PORT)) & READY) == 0) && (cnt <= 1000))
  241. ++cnt;
  242. #ifdef COUNT_LOOPS
  243. tot_cnt += cnt;
  244. #endif
  245. return value & 0xf0; /* nibble */
  246. }
  247. static inline void
  248. de620_send_command(struct net_device *dev, byte cmd)
  249. {
  250. de620_ready(dev);
  251. if (cmd == W_DUMMY)
  252. outb(NIC_Cmd, COMMAND_PORT);
  253. outb(cmd, DATA_PORT);
  254. outb(NIC_Cmd ^ CS0, COMMAND_PORT);
  255. de620_ready(dev);
  256. outb(NIC_Cmd, COMMAND_PORT);
  257. }
  258. static inline void
  259. de620_put_byte(struct net_device *dev, byte value)
  260. {
  261. /* The de620_ready() makes 7 loops, on the average, on a DX2/66 */
  262. de620_ready(dev);
  263. outb(value, DATA_PORT);
  264. de620_flip_ds(dev);
  265. }
  266. static inline byte
  267. de620_read_byte(struct net_device *dev)
  268. {
  269. byte value;
  270. /* The de620_ready() makes 7 loops, on the average, on a DX2/66 */
  271. value = de620_ready(dev); /* High nibble */
  272. de620_flip_ds(dev);
  273. value |= de620_ready(dev) >> 4; /* Low nibble */
  274. return value;
  275. }
  276. static inline void
  277. de620_write_block(struct net_device *dev, byte *buffer, int count)
  278. {
  279. #ifndef LOWSPEED
  280. byte uflip = NIC_Cmd ^ (DS0 | DS1);
  281. byte dflip = NIC_Cmd;
  282. #else /* LOWSPEED */
  283. #ifdef COUNT_LOOPS
  284. int bytes = count;
  285. #endif /* COUNT_LOOPS */
  286. #endif /* LOWSPEED */
  287. #ifdef LOWSPEED
  288. #ifdef COUNT_LOOPS
  289. tot_cnt = 0;
  290. #endif /* COUNT_LOOPS */
  291. /* No further optimization useful, the limit is in the adapter. */
  292. for ( ; count > 0; --count, ++buffer) {
  293. de620_put_byte(dev,*buffer);
  294. }
  295. de620_send_command(dev,W_DUMMY);
  296. #ifdef COUNT_LOOPS
  297. /* trial debug output: loops per byte in de620_ready() */
  298. printk("WRITE(%d)n", tot_cnt/((bytes?bytes:1)));
  299. #endif /* COUNT_LOOPS */
  300. #else /* not LOWSPEED */
  301. for ( ; count > 0; count -=2) {
  302. outb(*buffer++, DATA_PORT);
  303. outb(uflip, COMMAND_PORT);
  304. outb(*buffer++, DATA_PORT);
  305. outb(dflip, COMMAND_PORT);
  306. }
  307. de620_send_command(dev,W_DUMMY);
  308. #endif /* LOWSPEED */
  309. }
  310. static inline void
  311. de620_read_block(struct net_device *dev, byte *data, int count)
  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. tot_cnt = 0;
  321. #endif /* COUNT_LOOPS */
  322. #endif /* LOWSPEED */
  323. #ifdef LOWSPEED
  324. /* No further optimization useful, the limit is in the adapter. */
  325. while (count-- > 0) {
  326. *data++ = de620_read_byte(dev);
  327. de620_flip_ds(dev);
  328. }
  329. #ifdef COUNT_LOOPS
  330. /* trial debug output: loops per byte in de620_ready() */
  331. printk("READ(%d)n", tot_cnt/(2*(bytes?bytes:1)));
  332. #endif /* COUNT_LOOPS */
  333. #else /* not LOWSPEED */
  334. while (count-- > 0) {
  335. value = inb(STATUS_PORT) & 0xf0; /* High nibble */
  336. outb(uflip, COMMAND_PORT);
  337. *data++ = value | inb(STATUS_PORT) >> 4; /* Low nibble */
  338. outb(dflip , COMMAND_PORT);
  339. }
  340. #endif /* LOWSPEED */
  341. }
  342. static inline void
  343. de620_set_delay(struct net_device *dev)
  344. {
  345. de620_ready(dev);
  346. outb(W_DFR, DATA_PORT);
  347. outb(NIC_Cmd ^ CS0, COMMAND_PORT);
  348. de620_ready(dev);
  349. #ifdef LOWSPEED
  350. outb(WRITE_DELAY, DATA_PORT);
  351. #else
  352. outb(0, DATA_PORT);
  353. #endif
  354. de620_flip_ds(dev);
  355. de620_ready(dev);
  356. #ifdef LOWSPEED
  357. outb(READ_DELAY, DATA_PORT);
  358. #else
  359. outb(0, DATA_PORT);
  360. #endif
  361. de620_flip_ds(dev);
  362. }
  363. static inline void
  364. de620_set_register(struct net_device *dev, byte reg, byte value)
  365. {
  366. de620_ready(dev);
  367. outb(reg, DATA_PORT);
  368. outb(NIC_Cmd ^ CS0, COMMAND_PORT);
  369. de620_put_byte(dev, value);
  370. }
  371. static inline byte
  372. de620_get_register(struct net_device *dev, byte reg)
  373. {
  374. byte value;
  375. de620_send_command(dev,reg);
  376. value = de620_read_byte(dev);
  377. de620_send_command(dev,W_DUMMY);
  378. return value;
  379. }
  380. /*********************************************************************
  381.  *
  382.  * Open/initialize the board.
  383.  *
  384.  * This routine should set everything up anew at each open, even
  385.  * registers that "should" only need to be set once at boot, so that
  386.  * there is a non-reboot way to recover if something goes wrong.
  387.  *
  388.  */
  389. static int de620_open(struct net_device *dev)
  390. {
  391. int ret = request_irq(dev->irq, de620_interrupt, 0, dev->name, dev);
  392. if (ret) {
  393. printk (KERN_ERR "%s: unable to get IRQ %dn", dev->name, dev->irq);
  394. return ret;
  395. }
  396. if (adapter_init(dev)) {
  397. ret = -EIO;
  398. goto out_free_irq;
  399. }
  400. netif_start_queue(dev);
  401. return 0;
  402. out_free_irq:
  403. free_irq(dev->irq, dev);
  404. return ret;
  405. }
  406. /************************************************
  407.  *
  408.  * The inverse routine to de620_open().
  409.  *
  410.  */
  411. static int de620_close(struct net_device *dev)
  412. {
  413. netif_stop_queue(dev);
  414. /* disable recv */
  415. de620_set_register(dev, W_TCR, RXOFF);
  416. free_irq(dev->irq, dev);
  417. return 0;
  418. }
  419. /*********************************************
  420.  *
  421.  * Return current statistics
  422.  *
  423.  */
  424. static struct net_device_stats *get_stats(struct net_device *dev)
  425. {
  426. return (struct net_device_stats *)(dev->priv);
  427. }
  428. /*********************************************
  429.  *
  430.  * Set or clear the multicast filter for this adaptor.
  431.  * (no real multicast implemented for the DE-620, but she can be promiscuous...)
  432.  *
  433.  */
  434. static void de620_set_multicast_list(struct net_device *dev)
  435. {
  436. if (dev->mc_count || dev->flags&(IFF_ALLMULTI|IFF_PROMISC))
  437. { /* Enable promiscuous mode */
  438. /*
  439.  * We must make the kernel realise we had to move
  440.  * into promisc mode or we start all out war on
  441.  * the cable. - AC
  442.  */
  443. dev->flags|=IFF_PROMISC;
  444. de620_set_register(dev, W_TCR, (TCR_DEF & ~RXPBM) | RXALL);
  445. }
  446. else
  447. { /* Disable promiscuous mode, use normal mode */
  448. de620_set_register(dev, W_TCR, TCR_DEF);
  449. }
  450. }
  451. /*******************************************************
  452.  *
  453.  * Handle timeouts on transmit
  454.  */
  455.  
  456. static void de620_timeout(struct net_device *dev)
  457. {
  458. printk("%s: transmit timed out, %s?n",
  459. dev->name,
  460. "network cable problem"
  461. );
  462. /* Restart the adapter. */
  463. if (!adapter_init(dev)) /* maybe close it */
  464. netif_wake_queue(dev);
  465. }
  466. /*******************************************************
  467.  *
  468.  * Copy a buffer to the adapter transmit page memory.
  469.  * Start sending.
  470.  */
  471. static int de620_start_xmit(struct sk_buff *skb, struct net_device *dev)
  472. {
  473. unsigned long flags;
  474. int len;
  475. byte *buffer = skb->data;
  476. byte using_txbuf;
  477. using_txbuf = de620_tx_buffs(dev); /* Peek at the adapter */
  478. netif_stop_queue(dev);
  479. if ((len = skb->len) < RUNT)
  480. len = RUNT;
  481. if (len & 1) /* send an even number of bytes */
  482. ++len;
  483. /* Start real output */
  484. save_flags(flags);
  485. cli();
  486. PRINTK(("de620_start_xmit: len=%d, bufs 0x%02xn",
  487. (int)skb->len, using_txbuf));
  488. /* select a free tx buffer. if there is one... */
  489. switch (using_txbuf) {
  490. default: /* both are free: use TXBF0 */
  491. case TXBF1: /* use TXBF0 */
  492. de620_send_command(dev,W_CR | RW0);
  493. using_txbuf |= TXBF0;
  494. break;
  495. case TXBF0: /* use TXBF1 */
  496. de620_send_command(dev,W_CR | RW1);
  497. using_txbuf |= TXBF1;
  498. break;
  499. case (TXBF0 | TXBF1): /* NONE!!! */
  500. printk(KERN_WARNING "%s: No tx-buffer available!n", dev->name);
  501. restore_flags(flags);
  502. return 1;
  503. }
  504. de620_write_block(dev, buffer, len);
  505. dev->trans_start = jiffies;
  506. if(!(using_txbuf == (TXBF0 | TXBF1)))
  507. netif_wake_queue(dev);
  508. ((struct net_device_stats *)(dev->priv))->tx_packets++;
  509. restore_flags(flags); /* interrupts maybe back on */
  510. dev_kfree_skb (skb);
  511. return 0;
  512. }
  513. /*****************************************************
  514.  *
  515.  * Handle the network interface interrupts.
  516.  *
  517.  */
  518. static void de620_interrupt(int irq_in, void *dev_id, struct pt_regs *regs)
  519. {
  520. struct net_device *dev = dev_id;
  521. byte irq_status;
  522. int bogus_count = 0;
  523. int again = 0;
  524. /* This might be deleted now, no crummy drivers present :-) Or..? */
  525. if ((dev == NULL) || (irq != irq_in)) {
  526. printk("%s: bogus interrupt %dn", dev?dev->name:"de620", irq_in);
  527. return;
  528. }
  529. /* Read the status register (_not_ the status port) */
  530. irq_status = de620_get_register(dev, R_STS);
  531. PRINTK(("de620_interrupt (%2.2X)n", irq_status));
  532. if (irq_status & RXGOOD) {
  533. do {
  534. again = de620_rx_intr(dev);
  535. PRINTK(("again=%dn", again));
  536. }
  537. while (again && (++bogus_count < 100));
  538. }
  539. if(de620_tx_buffs(dev) != (TXBF0 | TXBF1))
  540. netif_wake_queue(dev);
  541. }
  542. /**************************************
  543.  *
  544.  * Get a packet from the adapter
  545.  *
  546.  * Send it "upstairs"
  547.  *
  548.  */
  549. static int de620_rx_intr(struct net_device *dev)
  550. {
  551. struct header_buf {
  552. byte status;
  553. byte Rx_NextPage;
  554. unsigned short Rx_ByteCount;
  555. } header_buf;
  556. struct sk_buff *skb;
  557. int size;
  558. byte *buffer;
  559. byte pagelink;
  560. byte curr_page;
  561. PRINTK(("de620_rx_intr: next_rx_page = %dn", next_rx_page));
  562. /* Tell the adapter that we are going to read data, and from where */
  563. de620_send_command(dev, W_CR | RRN);
  564. de620_set_register(dev, W_RSA1, next_rx_page);
  565. de620_set_register(dev, W_RSA0, 0);
  566. /* Deep breath, and away we goooooo */
  567. de620_read_block(dev, (byte *)&header_buf, sizeof(struct header_buf));
  568. PRINTK(("page status=0x%02x, nextpage=%d, packetsize=%dn",
  569. header_buf.status, header_buf.Rx_NextPage, header_buf.Rx_ByteCount));
  570. /* Plausible page header? */
  571. pagelink = header_buf.Rx_NextPage;
  572. if ((pagelink < first_rx_page) || (last_rx_page < pagelink)) {
  573. /* Ouch... Forget it! Skip all and start afresh... */
  574. printk(KERN_WARNING "%s: Ring overrun? Restoring...n", dev->name);
  575. /* You win some, you lose some. And sometimes plenty... */
  576. adapter_init(dev);
  577. netif_wake_queue(dev);
  578. ((struct net_device_stats *)(dev->priv))->rx_over_errors++;
  579. return 0;
  580. }
  581. /* OK, this look good, so far. Let's see if it's consistent... */
  582. /* Let's compute the start of the next packet, based on where we are */
  583. pagelink = next_rx_page +
  584. ((header_buf.Rx_ByteCount + (4 - 1 + 0x100)) >> 8);
  585. /* Are we going to wrap around the page counter? */
  586. if (pagelink > last_rx_page)
  587. pagelink -= (last_rx_page - first_rx_page + 1);
  588. /* Is the _computed_ next page number equal to what the adapter says? */
  589. if (pagelink != header_buf.Rx_NextPage) {
  590. /* Naah, we'll skip this packet. Probably bogus data as well */
  591. printk(KERN_WARNING "%s: Page link out of sync! Restoring...n", dev->name);
  592. next_rx_page = header_buf.Rx_NextPage; /* at least a try... */
  593. de620_send_command(dev, W_DUMMY);
  594. de620_set_register(dev, W_NPRF, next_rx_page);
  595. ((struct net_device_stats *)(dev->priv))->rx_over_errors++;
  596. return 0;
  597. }
  598. next_rx_page = pagelink;
  599. size = header_buf.Rx_ByteCount - 4;
  600. if ((size < RUNT) || (GIANT < size)) {
  601. printk(KERN_WARNING "%s: Illegal packet size: %d!n", dev->name, size);
  602. }
  603. else { /* Good packet? */
  604. skb = dev_alloc_skb(size+2);
  605. if (skb == NULL) { /* Yeah, but no place to put it... */
  606. printk(KERN_WARNING "%s: Couldn't allocate a sk_buff of size %d.n",
  607. dev->name, size);
  608. ((struct net_device_stats *)(dev->priv))->rx_dropped++;
  609. }
  610. else { /* Yep! Go get it! */
  611. skb_reserve(skb,2); /* Align */
  612. skb->dev = dev;
  613. /* skb->data points to the start of sk_buff data area */
  614. buffer = skb_put(skb,size);
  615. /* copy the packet into the buffer */
  616. de620_read_block(dev, buffer, size);
  617. PRINTK(("Read %d bytesn", size));
  618. skb->protocol=eth_type_trans(skb,dev);
  619. netif_rx(skb); /* deliver it "upstairs" */
  620. dev->last_rx = jiffies;
  621. /* count all receives */
  622. ((struct net_device_stats *)(dev->priv))->rx_packets++;
  623. ((struct net_device_stats *)(dev->priv))->rx_bytes += size;
  624. }
  625. }
  626. /* Let's peek ahead to see if we have read the last current packet */
  627. /* NOTE! We're _not_ checking the 'EMPTY'-flag! This seems better... */
  628. curr_page = de620_get_register(dev, R_CPR);
  629. de620_set_register(dev, W_NPRF, next_rx_page);
  630. PRINTK(("next_rx_page=%d CPR=%dn", next_rx_page, curr_page));
  631. return (next_rx_page != curr_page); /* That was slightly tricky... */
  632. }
  633. /*********************************************
  634.  *
  635.  * Reset the adapter to a known state
  636.  *
  637.  */
  638. static int adapter_init(struct net_device *dev)
  639. {
  640. int i;
  641. static int was_down;
  642. if ((nic_data.Model == 3) || (nic_data.Model == 0)) { /* CT */
  643. EIPRegister = NCTL0;
  644. if (nic_data.Media != 1)
  645. EIPRegister |= NIS0; /* not BNC */
  646. }
  647. else if (nic_data.Model == 2) { /* UTP */
  648. EIPRegister = NCTL0 | NIS0;
  649. }
  650. if (utp)
  651. EIPRegister = NCTL0 | NIS0;
  652. if (bnc)
  653. EIPRegister = NCTL0;
  654. de620_send_command(dev, W_CR | RNOP | CLEAR);
  655. de620_send_command(dev, W_CR | RNOP);
  656. de620_set_register(dev, W_SCR, SCR_DEF);
  657. /* disable recv to wait init */
  658. de620_set_register(dev, W_TCR, RXOFF);
  659. /* Set the node ID in the adapter */
  660. for (i = 0; i < 6; ++i) { /* W_PARn = 0xaa + n */
  661. de620_set_register(dev, W_PAR0 + i, dev->dev_addr[i]);
  662. }
  663. de620_set_register(dev, W_EIP, EIPRegister);
  664. next_rx_page = first_rx_page = DE620_RX_START_PAGE;
  665. if (nic_data.RAM_Size)
  666. last_rx_page = nic_data.RAM_Size - 1;
  667. else /* 64k RAM */
  668. last_rx_page = 255;
  669. de620_set_register(dev, W_SPR, first_rx_page); /* Start Page Register*/
  670. de620_set_register(dev, W_EPR, last_rx_page);  /* End Page Register */
  671. de620_set_register(dev, W_CPR, first_rx_page);/*Current Page Register*/
  672. de620_send_command(dev, W_NPR | first_rx_page); /* Next Page Register*/
  673. de620_send_command(dev, W_DUMMY);
  674. de620_set_delay(dev);
  675. /* Final sanity check: Anybody out there? */
  676. /* Let's hope some bits from the statusregister make a good check */
  677. #define CHECK_MASK (  0 | TXSUC |  T16  |  0  | RXCRC | RXSHORT |  0  |  0  )
  678. #define CHECK_OK   (  0 |   0   |  0    |  0  |   0   |   0     |  0  |  0  )
  679.         /* success:   X     0      0       X      0       0        X     X  */
  680.         /* ignore:   EEDI                RXGOOD                   COLS  LNKS*/
  681. if (((i = de620_get_register(dev, R_STS)) & CHECK_MASK) != CHECK_OK) {
  682. printk(KERN_ERR "%s: Something has happened to the DE-620!  Please check it"
  683. #ifdef SHUTDOWN_WHEN_LOST
  684. " and do a new ifconfig"
  685. #endif
  686. "! (%02x)n", dev->name, i);
  687. #ifdef SHUTDOWN_WHEN_LOST
  688. /* Goodbye, cruel world... */
  689. dev->flags &= ~IFF_UP;
  690. de620_close(dev);
  691. #endif
  692. was_down = 1;
  693. return 1; /* failed */
  694. }
  695. if (was_down) {
  696. printk(KERN_WARNING "%s: Thanks, I feel much better now!n", dev->name);
  697. was_down = 0;
  698. }
  699. /* All OK, go ahead... */
  700. de620_set_register(dev, W_TCR, TCR_DEF);
  701. return 0; /* all ok */
  702. }
  703. /******************************************************************************
  704.  *
  705.  * Only start-up code below
  706.  *
  707.  */
  708. /****************************************
  709.  *
  710.  * Check if there is a DE-620 connected
  711.  */
  712. int __init de620_probe(struct net_device *dev)
  713. {
  714. static struct net_device_stats de620_netstats;
  715. int i;
  716. byte checkbyte = 0xa5;
  717. SET_MODULE_OWNER(dev);
  718. /*
  719.  * This is where the base_addr and irq gets set.
  720.  * Tunable at compile-time and insmod-time
  721.  */
  722. dev->base_addr = io;
  723. dev->irq       = irq;
  724. if (de620_debug)
  725. printk(version);
  726. printk(KERN_INFO "D-Link DE-620 pocket adapter");
  727. /* Initially, configure basic nibble mode, so we can read the EEPROM */
  728. NIC_Cmd = DEF_NIC_CMD;
  729. de620_set_register(dev, W_EIP, EIPRegister);
  730. /* Anybody out there? */
  731. de620_set_register(dev, W_CPR, checkbyte);
  732. checkbyte = de620_get_register(dev, R_CPR);
  733. if ((checkbyte != 0xa5) || (read_eeprom(dev) != 0)) {
  734. printk(" not identified in the printer portn");
  735. return -ENODEV;
  736. }
  737. #if 0 /* Not yet */
  738. if (check_region(dev->base_addr, 3)) {
  739. printk(", port 0x%x busyn", dev->base_addr);
  740. return -EBUSY;
  741. }
  742. #endif
  743. if (!request_region(dev->base_addr, 3, "de620")) {
  744. printk(KERN_ERR "io 0x%3lX, which is busy.n", dev->base_addr);
  745. return -EBUSY;
  746. }
  747. /* else, got it! */
  748. printk(", Ethernet Address: %2.2X",
  749. dev->dev_addr[0] = nic_data.NodeID[0]);
  750. for (i = 1; i < ETH_ALEN; i++) {
  751. printk(":%2.2X", dev->dev_addr[i] = nic_data.NodeID[i]);
  752. dev->broadcast[i] = 0xff;
  753. }
  754. printk(" (%dk RAM,",
  755. (nic_data.RAM_Size) ? (nic_data.RAM_Size >> 2) : 64);
  756. if (nic_data.Media == 1)
  757. printk(" BNC)n");
  758. else
  759. printk(" UTP)n");
  760. /* Initialize the device structure. */
  761. dev->priv = &de620_netstats;
  762. memset(dev->priv, 0, sizeof(struct net_device_stats));
  763. dev->get_stats  = get_stats;
  764. dev->open  = de620_open;
  765. dev->stop  = de620_close;
  766. dev->hard_start_xmit  = de620_start_xmit;
  767. dev->tx_timeout  = de620_timeout;
  768. dev->watchdog_timeo = HZ*2;
  769. dev->set_multicast_list = de620_set_multicast_list;
  770. /* base_addr and irq are already set, see above! */
  771. ether_setup(dev);
  772. /* dump eeprom */
  773. if (de620_debug) {
  774. printk("nEEPROM contents:n");
  775. printk("RAM_Size = 0x%02Xn", nic_data.RAM_Size);
  776. printk("NodeID = %02X:%02X:%02X:%02X:%02X:%02Xn",
  777. nic_data.NodeID[0], nic_data.NodeID[1],
  778. nic_data.NodeID[2], nic_data.NodeID[3],
  779. nic_data.NodeID[4], nic_data.NodeID[5]);
  780. printk("Model = %dn", nic_data.Model);
  781. printk("Media = %dn", nic_data.Media);
  782. printk("SCR = 0x%02xn", nic_data.SCR);
  783. }
  784. return 0;
  785. }
  786. /**********************************
  787.  *
  788.  * Read info from on-board EEPROM
  789.  *
  790.  * Note: Bitwise serial I/O to/from the EEPROM vi the status _register_!
  791.  */
  792. #define sendit(dev,data) de620_set_register(dev, W_EIP, data | EIPRegister);
  793. static unsigned short __init ReadAWord(struct net_device *dev, int from)
  794. {
  795. unsigned short data;
  796. int nbits;
  797. /* cs   [__~~] SET SEND STATE */
  798. /* di   [____]                */
  799. /* sck  [_~~_]                */
  800. sendit(dev, 0); sendit(dev, 1); sendit(dev, 5); sendit(dev, 4);
  801. /* Send the 9-bit address from where we want to read the 16-bit word */
  802. for (nbits = 9; nbits > 0; --nbits, from <<= 1) {
  803. if (from & 0x0100) { /* bit set? */
  804. /* cs    [~~~~] SEND 1 */
  805. /* di    [~~~~]        */
  806. /* sck   [_~~_]        */
  807. sendit(dev, 6); sendit(dev, 7); sendit(dev, 7); sendit(dev, 6);
  808. }
  809. else {
  810. /* cs    [~~~~] SEND 0 */
  811. /* di    [____]        */
  812. /* sck   [_~~_]        */
  813. sendit(dev, 4); sendit(dev, 5); sendit(dev, 5); sendit(dev, 4);
  814. }
  815. }
  816. /* Shift in the 16-bit word. The bits appear serially in EEDI (=0x80) */
  817. for (data = 0, nbits = 16; nbits > 0; --nbits) {
  818. /* cs    [~~~~] SEND 0 */
  819. /* di    [____]        */
  820. /* sck   [_~~_]        */
  821. sendit(dev, 4); sendit(dev, 5); sendit(dev, 5); sendit(dev, 4);
  822. data = (data << 1) | ((de620_get_register(dev, R_STS) & EEDI) >> 7);
  823. }
  824. /* cs    [____] RESET SEND STATE */
  825. /* di    [____]                  */
  826. /* sck   [_~~_]                  */
  827. sendit(dev, 0); sendit(dev, 1); sendit(dev, 1); sendit(dev, 0);
  828. return data;
  829. }
  830. static int __init read_eeprom(struct net_device *dev)
  831. {
  832. unsigned short wrd;
  833. /* D-Link Ethernet addresses are in the series  00:80:c8:7X:XX:XX:XX */
  834. wrd = ReadAWord(dev, 0x1aa); /* bytes 0 + 1 of NodeID */
  835. if (!clone && (wrd != htons(0x0080))) /* Valid D-Link ether sequence? */
  836. return -1; /* Nope, not a DE-620 */
  837. nic_data.NodeID[0] = wrd & 0xff;
  838. nic_data.NodeID[1] = wrd >> 8;
  839. wrd = ReadAWord(dev, 0x1ab); /* bytes 2 + 3 of NodeID */
  840. if (!clone && ((wrd & 0xff) != 0xc8)) /* Valid D-Link ether sequence? */
  841. return -1; /* Nope, not a DE-620 */
  842. nic_data.NodeID[2] = wrd & 0xff;
  843. nic_data.NodeID[3] = wrd >> 8;
  844. wrd = ReadAWord(dev, 0x1ac); /* bytes 4 + 5 of NodeID */
  845. nic_data.NodeID[4] = wrd & 0xff;
  846. nic_data.NodeID[5] = wrd >> 8;
  847. wrd = ReadAWord(dev, 0x1ad); /* RAM size in pages (256 bytes). 0 = 64k */
  848. nic_data.RAM_Size = (wrd >> 8);
  849. wrd = ReadAWord(dev, 0x1ae); /* hardware model (CT = 3) */
  850. nic_data.Model = (wrd & 0xff);
  851. wrd = ReadAWord(dev, 0x1af); /* media (indicates BNC/UTP) */
  852. nic_data.Media = (wrd & 0xff);
  853. wrd = ReadAWord(dev, 0x1a8); /* System Configuration Register */
  854. nic_data.SCR = (wrd >> 8);
  855. return 0; /* no errors */
  856. }
  857. /******************************************************************************
  858.  *
  859.  * Loadable module skeleton
  860.  *
  861.  */
  862. #ifdef MODULE
  863. static struct net_device de620_dev;
  864. int init_module(void)
  865. {
  866. de620_dev.init = de620_probe;
  867. if (register_netdev(&de620_dev) != 0)
  868. return -EIO;
  869. return 0;
  870. }
  871. void cleanup_module(void)
  872. {
  873. unregister_netdev(&de620_dev);
  874. release_region(de620_dev.base_addr, 3);
  875. }
  876. #endif /* MODULE */
  877. MODULE_LICENSE("GPL");
  878. /*
  879.  * (add '-DMODULE' when compiling as loadable module)
  880.  *
  881.  * compile-command:
  882.  * gcc -D__KERNEL__ -Wall -Wstrict-prototypes -O2 
  883.  *  -fomit-frame-pointer -m486 
  884.  * -I/usr/src/linux/include -I../../net/inet -c de620.c
  885. */
  886. /*
  887.  * Local variables:
  888.  *  kernel-compile-command: "gcc -D__KERNEL__ -Ilinux/include -I../../net/inet -Wall -Wstrict-prototypes -O2 -m486 -c de620.c"
  889.  *  module-compile-command: "gcc -D__KERNEL__ -DMODULE -Ilinux/include -I../../net/inet -Wall -Wstrict-prototypes -O2 -m486 -c de620.c"
  890.  *  compile-command: "gcc -D__KERNEL__ -DMODULE -Ilinux/include -I../../net/inet -Wall -Wstrict-prototypes -O2 -m486 -c de620.c"
  891.  * End:
  892.  */