de600.c 14 KB

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