de600.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. static const char version[] = "de600.c: $Revision: 1.41-2.5 $, Bjorn Ekwall (bj0rn@blox.se)\n";
  2. /*
  3. * de600.c
  4. *
  5. * Linux driver for the D-Link DE-600 Ethernet pocket adapter.
  6. *
  7. * Portions (C) Copyright 1993, 1994 by Bjorn Ekwall
  8. * The Author may be reached as bj0rn@blox.se
  9. *
  10. * Based on adapter information gathered from DE600.ASM by D-Link Inc.,
  11. * as included on disk C in the v.2.11 of PC/TCP from FTP Software.
  12. * For DE600.asm:
  13. * Portions (C) Copyright 1990 D-Link, Inc.
  14. * Copyright, 1988-1992, Russell Nelson, Crynwr Software
  15. *
  16. * Adapted to the sample network driver core for linux,
  17. * written by: Donald Becker <becker@super.org>
  18. * (Now at <becker@scyld.com>)
  19. *
  20. **************************************************************/
  21. /*
  22. * This program is free software; you can redistribute it and/or modify
  23. * it under the terms of the GNU General Public License as published by
  24. * the Free Software Foundation; either version 2, or (at your option)
  25. * any later version.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU General Public License
  33. * along with this program; if not, write to the Free Software
  34. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35. *
  36. **************************************************************/
  37. /* Add more time here if your adapter won't work OK: */
  38. #define DE600_SLOW_DOWN udelay(delay_time)
  39. /* use 0 for production, 1 for verification, >2 for debug */
  40. #ifdef DE600_DEBUG
  41. #define PRINTK(x) if (de600_debug >= 2) printk x
  42. #else
  43. #define DE600_DEBUG 0
  44. #define PRINTK(x) /**/
  45. #endif
  46. #include <linux/module.h>
  47. #include <linux/kernel.h>
  48. #include <linux/types.h>
  49. #include <linux/fcntl.h>
  50. #include <linux/string.h>
  51. #include <linux/interrupt.h>
  52. #include <linux/ioport.h>
  53. #include <linux/in.h>
  54. #include <asm/system.h>
  55. #include <linux/errno.h>
  56. #include <linux/init.h>
  57. #include <linux/delay.h>
  58. #include <linux/inet.h>
  59. #include <linux/netdevice.h>
  60. #include <linux/etherdevice.h>
  61. #include <linux/skbuff.h>
  62. #include <asm/io.h>
  63. #include "de600.h"
  64. static unsigned int de600_debug = DE600_DEBUG;
  65. module_param(de600_debug, int, 0);
  66. MODULE_PARM_DESC(de600_debug, "DE-600 debug level (0-2)");
  67. static unsigned int check_lost = 1;
  68. module_param(check_lost, bool, 0);
  69. MODULE_PARM_DESC(check_lost, "If set then check for unplugged de600");
  70. static unsigned int delay_time = 10;
  71. module_param(delay_time, int, 0);
  72. MODULE_PARM_DESC(delay_time, "DE-600 deley on I/O in microseconds");
  73. /*
  74. * D-Link driver variables:
  75. */
  76. static volatile int rx_page;
  77. #define TX_PAGES 2
  78. static volatile int tx_fifo[TX_PAGES];
  79. static volatile int tx_fifo_in;
  80. static volatile int tx_fifo_out;
  81. static volatile int free_tx_pages = TX_PAGES;
  82. static int was_down;
  83. static DEFINE_SPINLOCK(de600_lock);
  84. static inline u8 de600_read_status(struct net_device *dev)
  85. {
  86. u8 status;
  87. outb_p(STATUS, DATA_PORT);
  88. status = inb(STATUS_PORT);
  89. outb_p(NULL_COMMAND | HI_NIBBLE, DATA_PORT);
  90. return status;
  91. }
  92. static inline u8 de600_read_byte(unsigned char type, struct net_device *dev)
  93. {
  94. /* dev used by macros */
  95. u8 lo;
  96. outb_p((type), DATA_PORT);
  97. lo = ((unsigned char)inb(STATUS_PORT)) >> 4;
  98. outb_p((type) | HI_NIBBLE, DATA_PORT);
  99. return ((unsigned char)inb(STATUS_PORT) & (unsigned char)0xf0) | lo;
  100. }
  101. /*
  102. * Open/initialize the board. This is called (in the current kernel)
  103. * after booting when 'ifconfig <dev->name> $IP_ADDR' is run (in rc.inet1).
  104. *
  105. * This routine should set everything up anew at each open, even
  106. * registers that "should" only need to be set once at boot, so that
  107. * there is a non-reboot way to recover if something goes wrong.
  108. */
  109. static int de600_open(struct net_device *dev)
  110. {
  111. unsigned long flags;
  112. int ret = request_irq(DE600_IRQ, de600_interrupt, 0, dev->name, dev);
  113. if (ret) {
  114. printk(KERN_ERR "%s: unable to get IRQ %d\n", dev->name, DE600_IRQ);
  115. return ret;
  116. }
  117. spin_lock_irqsave(&de600_lock, flags);
  118. ret = adapter_init(dev);
  119. spin_unlock_irqrestore(&de600_lock, flags);
  120. return ret;
  121. }
  122. /*
  123. * The inverse routine to de600_open().
  124. */
  125. static int de600_close(struct net_device *dev)
  126. {
  127. select_nic();
  128. rx_page = 0;
  129. de600_put_command(RESET);
  130. de600_put_command(STOP_RESET);
  131. de600_put_command(0);
  132. select_prn();
  133. free_irq(DE600_IRQ, dev);
  134. return 0;
  135. }
  136. static inline void trigger_interrupt(struct net_device *dev)
  137. {
  138. de600_put_command(FLIP_IRQ);
  139. select_prn();
  140. DE600_SLOW_DOWN;
  141. select_nic();
  142. de600_put_command(0);
  143. }
  144. /*
  145. * Copy a buffer to the adapter transmit page memory.
  146. * Start sending.
  147. */
  148. static int de600_start_xmit(struct sk_buff *skb, struct net_device *dev)
  149. {
  150. unsigned long flags;
  151. int transmit_from;
  152. int len;
  153. int tickssofar;
  154. u8 *buffer = skb->data;
  155. int i;
  156. if (free_tx_pages <= 0) { /* Do timeouts, to avoid hangs. */
  157. tickssofar = jiffies - dev->trans_start;
  158. if (tickssofar < 5)
  159. return 1;
  160. /* else */
  161. printk(KERN_WARNING "%s: transmit timed out (%d), %s?\n", dev->name, tickssofar, "network cable problem");
  162. /* Restart the adapter. */
  163. spin_lock_irqsave(&de600_lock, flags);
  164. if (adapter_init(dev)) {
  165. spin_unlock_irqrestore(&de600_lock, flags);
  166. return 1;
  167. }
  168. spin_unlock_irqrestore(&de600_lock, flags);
  169. }
  170. /* Start real output */
  171. PRINTK(("de600_start_xmit:len=%d, page %d/%d\n", skb->len, tx_fifo_in, free_tx_pages));
  172. if ((len = skb->len) < RUNT)
  173. len = RUNT;
  174. spin_lock_irqsave(&de600_lock, flags);
  175. select_nic();
  176. tx_fifo[tx_fifo_in] = transmit_from = tx_page_adr(tx_fifo_in) - len;
  177. tx_fifo_in = (tx_fifo_in + 1) % TX_PAGES; /* Next free tx page */
  178. if(check_lost)
  179. {
  180. /* This costs about 40 instructions per packet... */
  181. de600_setup_address(NODE_ADDRESS, RW_ADDR);
  182. de600_read_byte(READ_DATA, dev);
  183. if (was_down || (de600_read_byte(READ_DATA, dev) != 0xde)) {
  184. if (adapter_init(dev)) {
  185. spin_unlock_irqrestore(&de600_lock, flags);
  186. return 1;
  187. }
  188. }
  189. }
  190. de600_setup_address(transmit_from, RW_ADDR);
  191. for (i = 0; i < skb->len ; ++i, ++buffer)
  192. de600_put_byte(*buffer);
  193. for (; i < len; ++i)
  194. de600_put_byte(0);
  195. if (free_tx_pages-- == TX_PAGES) { /* No transmission going on */
  196. dev->trans_start = jiffies;
  197. netif_start_queue(dev); /* allow more packets into adapter */
  198. /* Send page and generate a faked interrupt */
  199. de600_setup_address(transmit_from, TX_ADDR);
  200. de600_put_command(TX_ENABLE);
  201. }
  202. else {
  203. if (free_tx_pages)
  204. netif_start_queue(dev);
  205. else
  206. netif_stop_queue(dev);
  207. select_prn();
  208. }
  209. spin_unlock_irqrestore(&de600_lock, flags);
  210. dev_kfree_skb(skb);
  211. return 0;
  212. }
  213. /*
  214. * The typical workload of the driver:
  215. * Handle the network interface interrupts.
  216. */
  217. static irqreturn_t de600_interrupt(int irq, void *dev_id)
  218. {
  219. struct net_device *dev = dev_id;
  220. u8 irq_status;
  221. int retrig = 0;
  222. int boguscount = 0;
  223. spin_lock(&de600_lock);
  224. select_nic();
  225. irq_status = de600_read_status(dev);
  226. do {
  227. PRINTK(("de600_interrupt (%02X)\n", irq_status));
  228. if (irq_status & RX_GOOD)
  229. de600_rx_intr(dev);
  230. else if (!(irq_status & RX_BUSY))
  231. de600_put_command(RX_ENABLE);
  232. /* Any transmission in progress? */
  233. if (free_tx_pages < TX_PAGES)
  234. retrig = de600_tx_intr(dev, irq_status);
  235. else
  236. retrig = 0;
  237. irq_status = de600_read_status(dev);
  238. } while ( (irq_status & RX_GOOD) || ((++boguscount < 100) && retrig) );
  239. /*
  240. * Yeah, it _looks_ like busy waiting, smells like busy waiting
  241. * and I know it's not PC, but please, it will only occur once
  242. * in a while and then only for a loop or so (< 1ms for sure!)
  243. */
  244. /* Enable adapter interrupts */
  245. select_prn();
  246. if (retrig)
  247. trigger_interrupt(dev);
  248. spin_unlock(&de600_lock);
  249. return IRQ_HANDLED;
  250. }
  251. static int de600_tx_intr(struct net_device *dev, int irq_status)
  252. {
  253. /*
  254. * Returns 1 if tx still not done
  255. */
  256. /* Check if current transmission is done yet */
  257. if (irq_status & TX_BUSY)
  258. return 1; /* tx not done, try again */
  259. /* else */
  260. /* If last transmission OK then bump fifo index */
  261. if (!(irq_status & TX_FAILED16)) {
  262. tx_fifo_out = (tx_fifo_out + 1) % TX_PAGES;
  263. ++free_tx_pages;
  264. dev->stats.tx_packets++;
  265. netif_wake_queue(dev);
  266. }
  267. /* More to send, or resend last packet? */
  268. if ((free_tx_pages < TX_PAGES) || (irq_status & TX_FAILED16)) {
  269. dev->trans_start = jiffies;
  270. de600_setup_address(tx_fifo[tx_fifo_out], TX_ADDR);
  271. de600_put_command(TX_ENABLE);
  272. return 1;
  273. }
  274. /* else */
  275. return 0;
  276. }
  277. /*
  278. * We have a good packet, get it out of the adapter.
  279. */
  280. static void de600_rx_intr(struct net_device *dev)
  281. {
  282. struct sk_buff *skb;
  283. int i;
  284. int read_from;
  285. int size;
  286. unsigned char *buffer;
  287. /* Get size of received packet */
  288. size = de600_read_byte(RX_LEN, dev); /* low byte */
  289. size += (de600_read_byte(RX_LEN, dev) << 8); /* high byte */
  290. size -= 4; /* Ignore trailing 4 CRC-bytes */
  291. /* Tell adapter where to store next incoming packet, enable receiver */
  292. read_from = rx_page_adr();
  293. next_rx_page();
  294. de600_put_command(RX_ENABLE);
  295. if ((size < 32) || (size > 1535)) {
  296. printk(KERN_WARNING "%s: Bogus packet size %d.\n", dev->name, size);
  297. if (size > 10000)
  298. adapter_init(dev);
  299. return;
  300. }
  301. skb = dev_alloc_skb(size+2);
  302. if (skb == NULL) {
  303. printk("%s: Couldn't allocate a sk_buff of size %d.\n", dev->name, size);
  304. return;
  305. }
  306. /* else */
  307. skb_reserve(skb,2); /* Align */
  308. /* 'skb->data' points to the start of sk_buff data area. */
  309. buffer = skb_put(skb,size);
  310. /* copy the packet into the buffer */
  311. de600_setup_address(read_from, RW_ADDR);
  312. for (i = size; i > 0; --i, ++buffer)
  313. *buffer = de600_read_byte(READ_DATA, dev);
  314. skb->protocol=eth_type_trans(skb,dev);
  315. netif_rx(skb);
  316. /* update stats */
  317. dev->stats.rx_packets++; /* count all receives */
  318. dev->stats.rx_bytes += size; /* count all received bytes */
  319. /*
  320. * If any worth-while packets have been received, netif_rx()
  321. * will work on them when we get to the tasklets.
  322. */
  323. }
  324. static const struct net_device_ops de600_netdev_ops = {
  325. .ndo_open = de600_open,
  326. .ndo_stop = de600_close,
  327. .ndo_start_xmit = de600_start_xmit,
  328. .ndo_change_mtu = eth_change_mtu,
  329. .ndo_set_mac_address = eth_mac_addr,
  330. .ndo_validate_addr = eth_validate_addr,
  331. };
  332. static struct net_device * __init de600_probe(void)
  333. {
  334. int i;
  335. struct net_device *dev;
  336. int err;
  337. dev = alloc_etherdev(0);
  338. if (!dev)
  339. return ERR_PTR(-ENOMEM);
  340. if (!request_region(DE600_IO, 3, "de600")) {
  341. printk(KERN_WARNING "DE600: port 0x%x busy\n", DE600_IO);
  342. err = -EBUSY;
  343. goto out;
  344. }
  345. printk(KERN_INFO "%s: D-Link DE-600 pocket adapter", dev->name);
  346. /* Alpha testers must have the version number to report bugs. */
  347. if (de600_debug > 1)
  348. printk(version);
  349. /* probe for adapter */
  350. err = -ENODEV;
  351. rx_page = 0;
  352. select_nic();
  353. (void)de600_read_status(dev);
  354. de600_put_command(RESET);
  355. de600_put_command(STOP_RESET);
  356. if (de600_read_status(dev) & 0xf0) {
  357. printk(": not at I/O %#3x.\n", DATA_PORT);
  358. goto out1;
  359. }
  360. /*
  361. * Maybe we found one,
  362. * have to check if it is a D-Link DE-600 adapter...
  363. */
  364. /* Get the adapter ethernet address from the ROM */
  365. de600_setup_address(NODE_ADDRESS, RW_ADDR);
  366. for (i = 0; i < ETH_ALEN; i++) {
  367. dev->dev_addr[i] = de600_read_byte(READ_DATA, dev);
  368. dev->broadcast[i] = 0xff;
  369. }
  370. /* Check magic code */
  371. if ((dev->dev_addr[1] == 0xde) && (dev->dev_addr[2] == 0x15)) {
  372. /* OK, install real address */
  373. dev->dev_addr[0] = 0x00;
  374. dev->dev_addr[1] = 0x80;
  375. dev->dev_addr[2] = 0xc8;
  376. dev->dev_addr[3] &= 0x0f;
  377. dev->dev_addr[3] |= 0x70;
  378. } else {
  379. printk(" not identified in the printer port\n");
  380. goto out1;
  381. }
  382. printk(", Ethernet Address: %pM\n", dev->dev_addr);
  383. dev->netdev_ops = &de600_netdev_ops;
  384. dev->flags&=~IFF_MULTICAST;
  385. select_prn();
  386. err = register_netdev(dev);
  387. if (err)
  388. goto out1;
  389. return dev;
  390. out1:
  391. release_region(DE600_IO, 3);
  392. out:
  393. free_netdev(dev);
  394. return ERR_PTR(err);
  395. }
  396. static int adapter_init(struct net_device *dev)
  397. {
  398. int i;
  399. select_nic();
  400. rx_page = 0; /* used by RESET */
  401. de600_put_command(RESET);
  402. de600_put_command(STOP_RESET);
  403. /* Check if it is still there... */
  404. /* Get the some bytes of the adapter ethernet address from the ROM */
  405. de600_setup_address(NODE_ADDRESS, RW_ADDR);
  406. de600_read_byte(READ_DATA, dev);
  407. if ((de600_read_byte(READ_DATA, dev) != 0xde) ||
  408. (de600_read_byte(READ_DATA, dev) != 0x15)) {
  409. /* was: if (de600_read_status(dev) & 0xf0) { */
  410. printk("Something has happened to the DE-600! Please check it and do a new ifconfig!\n");
  411. /* Goodbye, cruel world... */
  412. dev->flags &= ~IFF_UP;
  413. de600_close(dev);
  414. was_down = 1;
  415. netif_stop_queue(dev); /* Transmit busy... */
  416. return 1; /* failed */
  417. }
  418. if (was_down) {
  419. printk(KERN_INFO "%s: Thanks, I feel much better now!\n", dev->name);
  420. was_down = 0;
  421. }
  422. tx_fifo_in = 0;
  423. tx_fifo_out = 0;
  424. free_tx_pages = TX_PAGES;
  425. /* set the ether address. */
  426. de600_setup_address(NODE_ADDRESS, RW_ADDR);
  427. for (i = 0; i < ETH_ALEN; i++)
  428. de600_put_byte(dev->dev_addr[i]);
  429. /* where to start saving incoming packets */
  430. rx_page = RX_BP | RX_BASE_PAGE;
  431. de600_setup_address(MEM_4K, RW_ADDR);
  432. /* Enable receiver */
  433. de600_put_command(RX_ENABLE);
  434. select_prn();
  435. netif_start_queue(dev);
  436. return 0; /* OK */
  437. }
  438. static struct net_device *de600_dev;
  439. static int __init de600_init(void)
  440. {
  441. de600_dev = de600_probe();
  442. if (IS_ERR(de600_dev))
  443. return PTR_ERR(de600_dev);
  444. return 0;
  445. }
  446. static void __exit de600_exit(void)
  447. {
  448. unregister_netdev(de600_dev);
  449. release_region(DE600_IO, 3);
  450. free_netdev(de600_dev);
  451. }
  452. module_init(de600_init);
  453. module_exit(de600_exit);
  454. MODULE_LICENSE("GPL");