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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* isa-skeleton.c: A network driver outline for linux.
  2.  *
  3.  * Written 1993-94 by Donald Becker.
  4.  *
  5.  * Copyright 1993 United States Government as represented by the
  6.  * Director, National Security Agency.
  7.  *
  8.  * This software may be used and distributed according to the terms
  9.  * of the GNU General Public License, incorporated herein by reference.
  10.  *
  11.  * The author may be reached as becker@scyld.com, or C/O
  12.  * Scyld Computing Corporation
  13.  * 410 Severn Ave., Suite 210
  14.  * Annapolis MD 21403
  15.  *
  16.  * This file is an outline for writing a network device driver for the
  17.  * the Linux operating system.
  18.  *
  19.  * To write (or understand) a driver, have a look at the "loopback.c" file to
  20.  * get a feel of what is going on, and then use the code below as a skeleton
  21.  * for the new driver.
  22.  *
  23.  */
  24. static const char *version =
  25. "isa-skeleton.c:v1.51 9/24/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)n";
  26. /*
  27.  *  Sources:
  28.  * List your sources of programming information to document that
  29.  * the driver is your own creation, and give due credit to others
  30.  * that contributed to the work. Remember that GNU project code
  31.  * cannot use proprietary or trade secret information. Interface
  32.  * definitions are generally considered non-copyrightable to the
  33.  * extent that the same names and structures must be used to be
  34.  * compatible.
  35.  *
  36.  * Finally, keep in mind that the Linux kernel is has an API, not
  37.  * ABI. Proprietary object-code-only distributions are not permitted
  38.  * under the GPL.
  39.  */
  40. #include <linux/module.h>
  41. #include <linux/kernel.h>
  42. #include <linux/sched.h>
  43. #include <linux/types.h>
  44. #include <linux/fcntl.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/ptrace.h>
  47. #include <linux/ioport.h>
  48. #include <linux/in.h>
  49. #include <linux/slab.h>
  50. #include <linux/string.h>
  51. #include <asm/system.h>
  52. #include <asm/bitops.h>
  53. #include <linux/spinlock.h>
  54. #include <asm/io.h>
  55. #include <asm/dma.h>
  56. #include <linux/errno.h>
  57. #include <linux/init.h>
  58. #include <linux/netdevice.h>
  59. #include <linux/etherdevice.h>
  60. #include <linux/skbuff.h>
  61. /*
  62.  * The name of the card. Is used for messages and in the requests for
  63.  * io regions, irqs and dma channels
  64.  */
  65. static const char* cardname = "netcard";
  66. /* First, a few definitions that the brave might change. */
  67. /* A zero-terminated list of I/O addresses to be probed. */
  68. static unsigned int netcard_portlist[] __initdata =
  69.    { 0x200, 0x240, 0x280, 0x2C0, 0x300, 0x320, 0x340, 0};
  70. /* use 0 for production, 1 for verification, >2 for debug */
  71. #ifndef NET_DEBUG
  72. #define NET_DEBUG 2
  73. #endif
  74. static unsigned int net_debug = NET_DEBUG;
  75. /* The number of low I/O ports used by the ethercard. */
  76. #define NETCARD_IO_EXTENT 32
  77. #define MY_TX_TIMEOUT  ((400*HZ)/1000)
  78. /* Information that need to be kept for each board. */
  79. struct net_local {
  80. struct net_device_stats stats;
  81. long open_time; /* Useless example local info. */
  82. /* Tx control lock.  This protects the transmit buffer ring
  83.  * state along with the "tx full" state of the driver.  This
  84.  * means all netif_queue flow control actions are protected
  85.  * by this lock as well.
  86.  */
  87. spinlock_t lock;
  88. };
  89. /* The station (ethernet) address prefix, used for IDing the board. */
  90. #define SA_ADDR0 0x00
  91. #define SA_ADDR1 0x42
  92. #define SA_ADDR2 0x65
  93. /* Index to functions, as function prototypes. */
  94. extern int netcard_probe(struct net_device *dev);
  95. static int netcard_probe1(struct net_device *dev, int ioaddr);
  96. static int net_open(struct net_device *dev);
  97. static int net_send_packet(struct sk_buff *skb, struct net_device *dev);
  98. static void net_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  99. static void net_rx(struct net_device *dev);
  100. static int net_close(struct net_device *dev);
  101. static struct net_device_stats *net_get_stats(struct net_device *dev);
  102. static void set_multicast_list(struct net_device *dev);
  103. static void     net_tx_timeout(struct net_device *dev);
  104. /* Example routines you must write ;->. */
  105. #define tx_done(dev) 1
  106. static void hardware_send_packet(short ioaddr, char *buf, int length);
  107. static void  chipset_init(struct net_device *dev, int startp);
  108. /*
  109.  * Check for a network adaptor of this type, and return '0' iff one exists.
  110.  * If dev->base_addr == 0, probe all likely locations.
  111.  * If dev->base_addr == 1, always return failure.
  112.  * If dev->base_addr == 2, allocate space for the device and return success
  113.  * (detachable devices only).
  114.  */
  115. int __init 
  116. netcard_probe(struct net_device *dev)
  117. {
  118. int i;
  119. int base_addr = dev->base_addr;
  120. SET_MODULE_OWNER(dev);
  121. if (base_addr > 0x1ff)    /* Check a single specified location. */
  122. return netcard_probe1(dev, base_addr);
  123. else if (base_addr != 0)  /* Don't probe at all. */
  124. return -ENXIO;
  125. for (i = 0; netcard_portlist[i]; i++) {
  126. int ioaddr = netcard_portlist[i];
  127. if (check_region(ioaddr, NETCARD_IO_EXTENT))
  128. continue;
  129. if (netcard_probe1(dev, ioaddr) == 0)
  130. return 0;
  131. }
  132. return -ENODEV;
  133. }
  134. /*
  135.  * This is the real probe routine. Linux has a history of friendly device
  136.  * probes on the ISA bus. A good device probes avoids doing writes, and
  137.  * verifies that the correct device exists and functions.
  138.  */
  139. static int __init netcard_probe1(struct net_device *dev, int ioaddr)
  140. {
  141. struct net_local *np;
  142. static unsigned version_printed;
  143. int i;
  144. /*
  145.  * For ethernet adaptors the first three octets of the station address 
  146.  * contains the manufacturer's unique code. That might be a good probe
  147.  * method. Ideally you would add additional checks.
  148.  */ 
  149. if (inb(ioaddr + 0) != SA_ADDR0
  150. ||  inb(ioaddr + 1) != SA_ADDR1
  151. ||  inb(ioaddr + 2) != SA_ADDR2) {
  152. return -ENODEV;
  153. }
  154. if (net_debug  &&  version_printed++ == 0)
  155. printk(KERN_DEBUG "%s", version);
  156. printk(KERN_INFO "%s: %s found at %#3x, ", dev->name, cardname, ioaddr);
  157. /* Fill in the 'dev' fields. */
  158. dev->base_addr = ioaddr;
  159. /* Retrieve and print the ethernet address. */
  160. for (i = 0; i < 6; i++)
  161. printk(" %2.2x", dev->dev_addr[i] = inb(ioaddr + i));
  162. #ifdef jumpered_interrupts
  163. /*
  164.  * If this board has jumpered interrupts, allocate the interrupt
  165.  * vector now. There is no point in waiting since no other device
  166.  * can use the interrupt, and this marks the irq as busy. Jumpered
  167.  * interrupts are typically not reported by the boards, and we must
  168.  * used autoIRQ to find them.
  169.  */
  170. if (dev->irq == -1)
  171. ; /* Do nothing: a user-level program will set it. */
  172. else if (dev->irq < 2) { /* "Auto-IRQ" */
  173. autoirq_setup(0);
  174. /* Trigger an interrupt here. */
  175. dev->irq = autoirq_report(0);
  176. if (net_debug >= 2)
  177. printk(" autoirq is %d", dev->irq);
  178. } else if (dev->irq == 2)
  179. /*
  180.  * Fixup for users that don't know that IRQ 2 is really
  181.  * IRQ9, or don't know which one to set.
  182.  */
  183. dev->irq = 9;
  184. {
  185. int irqval = request_irq(dev->irq, &net_interrupt, 0, cardname, dev);
  186. if (irqval) {
  187. printk("%s: unable to get IRQ %d (irqval=%d).n",
  188.    dev->name, dev->irq, irqval);
  189. return -EAGAIN;
  190. }
  191. }
  192. #endif /* jumpered interrupt */
  193. #ifdef jumpered_dma
  194. /*
  195.  * If we use a jumpered DMA channel, that should be probed for and
  196.  * allocated here as well. See lance.c for an example.
  197.  */
  198. if (dev->dma == 0) {
  199. if (request_dma(dev->dma, cardname)) {
  200. printk("DMA %d allocation failed.n", dev->dma);
  201. return -EAGAIN;
  202. } else
  203. printk(", assigned DMA %d.n", dev->dma);
  204. } else {
  205. short dma_status, new_dma_status;
  206. /* Read the DMA channel status registers. */
  207. dma_status = ((inb(DMA1_STAT_REG) >> 4) & 0x0f) |
  208. (inb(DMA2_STAT_REG) & 0xf0);
  209. /* Trigger a DMA request, perhaps pause a bit. */
  210. outw(0x1234, ioaddr + 8);
  211. /* Re-read the DMA status registers. */
  212. new_dma_status = ((inb(DMA1_STAT_REG) >> 4) & 0x0f) |
  213. (inb(DMA2_STAT_REG) & 0xf0);
  214. /*
  215.  * Eliminate the old and floating requests,
  216.  * and DMA4 the cascade.
  217.  */
  218. new_dma_status ^= dma_status;
  219. new_dma_status &= ~0x10;
  220. for (i = 7; i > 0; i--)
  221. if (test_bit(i, &new_dma_status)) {
  222. dev->dma = i;
  223. break;
  224. }
  225. if (i <= 0) {
  226. printk("DMA probe failed.n");
  227. return -EAGAIN;
  228. if (request_dma(dev->dma, cardname)) {
  229. printk("probed DMA %d allocation failed.n", dev->dma);
  230. return -EAGAIN;
  231. }
  232. }
  233. #endif /* jumpered DMA */
  234. /* Initialize the device structure. */
  235. if (dev->priv == NULL) {
  236. dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
  237. if (dev->priv == NULL)
  238. return -ENOMEM;
  239. }
  240. memset(dev->priv, 0, sizeof(struct net_local));
  241. np = (struct net_local *)dev->priv;
  242. spin_lock_init(&np->lock);
  243. /* Grab the region so that no one else tries to probe our ioports. */
  244. request_region(ioaddr, NETCARD_IO_EXTENT, cardname);
  245. dev->open = net_open;
  246. dev->stop = net_close;
  247. dev->hard_start_xmit = net_send_packet;
  248. dev->get_stats = net_get_stats;
  249. dev->set_multicast_list = &set_multicast_list;
  250.         dev->tx_timeout = &net_tx_timeout;
  251.         dev->watchdog_timeo = MY_TX_TIMEOUT; 
  252. /* Fill in the fields of the device structure with ethernet values. */
  253. ether_setup(dev);
  254. return 0;
  255. }
  256. static void net_tx_timeout(struct net_device *dev)
  257. {
  258. struct net_local *np = (struct net_local *)dev->priv;
  259. printk(KERN_WARNING "%s: transmit timed out, %s?n", dev->name,
  260.        tx_done(dev) ? "IRQ conflict" : "network cable problem");
  261. /* Try to restart the adaptor. */
  262. chipset_init(dev, 1);
  263. np->stats.tx_errors++;
  264. /* If we have space available to accept new transmit
  265.  * requests, wake up the queueing layer.  This would
  266.  * be the case if the chipset_init() call above just
  267.  * flushes out the tx queue and empties it.
  268.  *
  269.  * If instead, the tx queue is retained then the
  270.  * netif_wake_queue() call should be placed in the
  271.  * TX completion interrupt handler of the driver instead
  272.  * of here.
  273.  */
  274. if (!tx_full(dev))
  275. netif_wake_queue(dev);
  276. }
  277. /*
  278.  * Open/initialize the board. This is called (in the current kernel)
  279.  * sometime after booting when the 'ifconfig' program is run.
  280.  *
  281.  * This routine should set everything up anew at each open, even
  282.  * registers that "should" only need to be set once at boot, so that
  283.  * there is non-reboot way to recover if something goes wrong.
  284.  */
  285. static int
  286. net_open(struct net_device *dev)
  287. {
  288. struct net_local *np = (struct net_local *)dev->priv;
  289. int ioaddr = dev->base_addr;
  290. /*
  291.  * This is used if the interrupt line can turned off (shared).
  292.  * See 3c503.c for an example of selecting the IRQ at config-time.
  293.  */
  294. if (request_irq(dev->irq, &net_interrupt, 0, cardname, dev)) {
  295. return -EAGAIN;
  296. }
  297. /*
  298.  * Always allocate the DMA channel after the IRQ,
  299.  * and clean up on failure.
  300.  */
  301. if (request_dma(dev->dma, cardname)) {
  302. free_irq(dev->irq, dev);
  303. return -EAGAIN;
  304. }
  305. /* Reset the hardware here. Don't forget to set the station address. */
  306. chipset_init(dev, 1);
  307. outb(0x00, ioaddr);
  308. np->open_time = jiffies;
  309. /* We are now ready to accept transmit requeusts from
  310.  * the queueing layer of the networking.
  311.  */
  312. netif_start_queue(dev);
  313. return 0;
  314. }
  315. /* This will only be invoked if your driver is _not_ in XOFF state.
  316.  * What this means is that you need not check it, and that this
  317.  * invariant will hold if you make sure that the netif_*_queue()
  318.  * calls are done at the proper times.
  319.  */
  320. static int net_send_packet(struct sk_buff *skb, struct net_device *dev)
  321. {
  322. struct net_local *np = (struct net_local *)dev->priv;
  323. int ioaddr = dev->base_addr;
  324. short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
  325. unsigned char *buf = skb->data;
  326. /* If some error occurs while trying to transmit this
  327.  * packet, you should return '1' from this function.
  328.  * In such a case you _may not_ do anything to the
  329.  * SKB, it is still owned by the network queueing
  330.  * layer when an error is returned.  This means you
  331.  * may not modify any SKB fields, you may not free
  332.  * the SKB, etc.
  333.  */
  334. #if TX_RING
  335. /* This is the most common case for modern hardware.
  336.  * The spinlock protects this code from the TX complete
  337.  * hardware interrupt handler.  Queue flow control is
  338.  * thus managed under this lock as well.
  339.  */
  340. spin_lock_irq(&np->lock);
  341. add_to_tx_ring(np, skb, length);
  342. dev->trans_start = jiffies;
  343. /* If we just used up the very last entry in the
  344.  * TX ring on this device, tell the queueing
  345.  * layer to send no more.
  346.  */
  347. if (tx_full(dev))
  348. netif_stop_queue(dev);
  349. /* When the TX completion hw interrupt arrives, this
  350.  * is when the transmit statistics are updated.
  351.  */
  352. spin_unlock_irq(&np->lock);
  353. #else
  354. /* This is the case for older hardware which takes
  355.  * a single transmit buffer at a time, and it is
  356.  * just written to the device via PIO.
  357.  *
  358.  * No spin locking is needed since there is no TX complete
  359.  * event.  If by chance your card does have a TX complete
  360.  * hardware IRQ then you may need to utilize np->lock here.
  361.  */
  362. hardware_send_packet(ioaddr, buf, length);
  363. np->stats.tx_bytes += skb->len;
  364. dev->trans_start = jiffies;
  365. /* You might need to clean up and record Tx statistics here. */
  366. if (inw(ioaddr) == /*RU*/81)
  367. np->stats.tx_aborted_errors++;
  368. dev_kfree_skb (skb);
  369. #endif
  370. return 0;
  371. }
  372. #if TX_RING
  373. /* This handles TX complete events posted by the device
  374.  * via interrupts.
  375.  */
  376. void net_tx(struct net_device *dev)
  377. {
  378. struct net_local *np = (struct net_local *)dev->priv;
  379. int entry;
  380. /* This protects us from concurrent execution of
  381.  * our dev->hard_start_xmit function above.
  382.  */
  383. spin_lock(&np->lock);
  384. entry = np->tx_old;
  385. while (tx_entry_is_sent(np, entry)) {
  386. struct sk_buff *skb = np->skbs[entry];
  387. np->stats.tx_bytes += skb->len;
  388. dev_kfree_skb_irq (skb);
  389. entry = next_tx_entry(np, entry);
  390. }
  391. np->tx_old = entry;
  392. /* If we had stopped the queue due to a "tx full"
  393.  * condition, and space has now been made available,
  394.  * wake up the queue.
  395.  */
  396. if (netif_queue_stopped(dev) && ! tx_full(dev))
  397. netif_wake_queue(dev);
  398. spin_unlock(&np->lock);
  399. }
  400. #endif
  401. /*
  402.  * The typical workload of the driver:
  403.  * Handle the network interface interrupts.
  404.  */
  405. static void net_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  406. {
  407. struct net_device *dev = dev_id;
  408. struct net_local *np;
  409. int ioaddr, status;
  410. ioaddr = dev->base_addr;
  411. np = (struct net_local *)dev->priv;
  412. status = inw(ioaddr + 0);
  413. if (status & RX_INTR) {
  414. /* Got a packet(s). */
  415. net_rx(dev);
  416. }
  417. #if TX_RING
  418. if (status & TX_INTR) {
  419. /* Transmit complete. */
  420. net_tx(dev);
  421. np->stats.tx_packets++;
  422. netif_wake_queue(dev);
  423. }
  424. #endif
  425. if (status & COUNTERS_INTR) {
  426. /* Increment the appropriate 'localstats' field. */
  427. np->stats.tx_window_errors++;
  428. }
  429. }
  430. /* We have a good packet(s), get it/them out of the buffers. */
  431. static void
  432. net_rx(struct net_device *dev)
  433. {
  434. struct net_local *lp = (struct net_local *)dev->priv;
  435. int ioaddr = dev->base_addr;
  436. int boguscount = 10;
  437. do {
  438. int status = inw(ioaddr);
  439. int pkt_len = inw(ioaddr);
  440.   
  441. if (pkt_len == 0) /* Read all the frames? */
  442. break; /* Done for now */
  443. if (status & 0x40) { /* There was an error. */
  444. lp->stats.rx_errors++;
  445. if (status & 0x20) lp->stats.rx_frame_errors++;
  446. if (status & 0x10) lp->stats.rx_over_errors++;
  447. if (status & 0x08) lp->stats.rx_crc_errors++;
  448. if (status & 0x04) lp->stats.rx_fifo_errors++;
  449. } else {
  450. /* Malloc up new buffer. */
  451. struct sk_buff *skb;
  452. lp->stats.rx_bytes+=pkt_len;
  453. skb = dev_alloc_skb(pkt_len);
  454. if (skb == NULL) {
  455. printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.n",
  456.    dev->name);
  457. lp->stats.rx_dropped++;
  458. break;
  459. }
  460. skb->dev = dev;
  461. /* 'skb->data' points to the start of sk_buff data area. */
  462. memcpy(skb_put(skb,pkt_len), (void*)dev->rmem_start,
  463.    pkt_len);
  464. /* or */
  465. insw(ioaddr, skb->data, (pkt_len + 1) >> 1);
  466. netif_rx(skb);
  467. dev->last_rx = jiffies;
  468. lp->stats.rx_packets++;
  469. lp->stats.rx_bytes += pkt_len;
  470. }
  471. } while (--boguscount);
  472. return;
  473. }
  474. /* The inverse routine to net_open(). */
  475. static int
  476. net_close(struct net_device *dev)
  477. {
  478. struct net_local *lp = (struct net_local *)dev->priv;
  479. int ioaddr = dev->base_addr;
  480. lp->open_time = 0;
  481. netif_stop_queue(dev);
  482. /* Flush the Tx and disable Rx here. */
  483. disable_dma(dev->dma);
  484. /* If not IRQ or DMA jumpered, free up the line. */
  485. outw(0x00, ioaddr+0); /* Release the physical interrupt line. */
  486. free_irq(dev->irq, dev);
  487. free_dma(dev->dma);
  488. /* Update the statistics here. */
  489. return 0;
  490. }
  491. /*
  492.  * Get the current statistics.
  493.  * This may be called with the card open or closed.
  494.  */
  495. static struct net_device_stats *net_get_stats(struct net_device *dev)
  496. {
  497. struct net_local *lp = (struct net_local *)dev->priv;
  498. short ioaddr = dev->base_addr;
  499. /* Update the statistics from the device registers. */
  500. lp->stats.rx_missed_errors = inw(ioaddr+1);
  501. return &lp->stats;
  502. }
  503. /*
  504.  * Set or clear the multicast filter for this adaptor.
  505.  * num_addrs == -1 Promiscuous mode, receive all packets
  506.  * num_addrs == 0 Normal mode, clear multicast list
  507.  * num_addrs > 0 Multicast mode, receive normal and MC packets,
  508.  * and do best-effort filtering.
  509.  */
  510. static void
  511. set_multicast_list(struct net_device *dev)
  512. {
  513. short ioaddr = dev->base_addr;
  514. if (dev->flags&IFF_PROMISC)
  515. {
  516. /* Enable promiscuous mode */
  517. outw(MULTICAST|PROMISC, ioaddr);
  518. }
  519. else if((dev->flags&IFF_ALLMULTI) || dev->mc_count > HW_MAX_ADDRS)
  520. {
  521. /* Disable promiscuous mode, use normal mode. */
  522. hardware_set_filter(NULL);
  523. outw(MULTICAST, ioaddr);
  524. }
  525. else if(dev->mc_count)
  526. {
  527. /* Walk the address list, and load the filter */
  528. hardware_set_filter(dev->mc_list);
  529. outw(MULTICAST, ioaddr);
  530. }
  531. else 
  532. outw(0, ioaddr);
  533. }
  534. #ifdef MODULE
  535. static struct net_device this_device;
  536. static int io = 0x300;
  537. static int irq;
  538. static int dma;
  539. static int mem;
  540. MODULE_LICENSE("GPL");
  541. int init_module(void)
  542. {
  543. int result;
  544. if (io == 0)
  545. printk(KERN_WARNING "%s: You shouldn't use auto-probing with insmod!n",
  546.    cardname);
  547. /* Copy the parameters from insmod into the device structure. */
  548. this_device.base_addr = io;
  549. this_device.irq       = irq;
  550. this_device.dma       = dma;
  551. this_device.mem_start = mem;
  552. this_device.init      = netcard_probe;
  553. if ((result = register_netdev(&this_device)) != 0)
  554. return result;
  555. return 0;
  556. }
  557. void
  558. cleanup_module(void)
  559. {
  560. /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
  561. unregister_netdev(&this_device);
  562. /*
  563.  * If we don't do this, we can't re-insmod it later.
  564.  * Release irq/dma here, when you have jumpered versions and
  565.  * allocate them in net_probe1().
  566.  */
  567. /*
  568.    free_irq(this_device.irq, dev);
  569.    free_dma(this_device.dma);
  570. */
  571. release_region(this_device.base_addr, NETCARD_IO_EXTENT);
  572. if (this_device.priv)
  573. kfree(this_device.priv);
  574. }
  575. #endif /* MODULE */
  576. /*
  577.  * Local variables:
  578.  *  compile-command:
  579.  * gcc -D__KERNEL__ -Wall -Wstrict-prototypes -Wwrite-strings
  580.  * -Wredundant-decls -O2 -m486 -c skeleton.c
  581.  *  version-control: t
  582.  *  kept-new-versions: 5
  583.  *  tab-width: 4
  584.  *  c-indent-level: 4
  585.  * End:
  586.  */