de600.c 13 KB

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