File: /usr/src/linux/kernel/dma.c

1     /* $Id: dma.c,v 1.7 1994/12/28 03:35:33 root Exp root $
2      * linux/kernel/dma.c: A DMA channel allocator. Inspired by linux/kernel/irq.c.
3      *
4      * Written by Hennus Bergman, 1992.
5      *
6      * 1994/12/26: Changes by Alex Nash to fix a minor bug in /proc/dma.
7      *   In the previous version the reported device could end up being wrong,
8      *   if a device requested a DMA channel that was already in use.
9      *   [It also happened to remove the sizeof(char *) == sizeof(int)
10      *   assumption introduced because of those /proc/dma patches. -- Hennus]
11      */
12     
13     #include <linux/kernel.h>
14     #include <linux/errno.h>
15     #include <linux/spinlock.h>
16     #include <linux/string.h>
17     #include <asm/dma.h>
18     #include <asm/system.h>
19     
20      
21     
22     /* A note on resource allocation:
23      *
24      * All drivers needing DMA channels, should allocate and release them
25      * through the public routines `request_dma()' and `free_dma()'.
26      *
27      * In order to avoid problems, all processes should allocate resources in
28      * the same sequence and release them in the reverse order.
29      *
30      * So, when allocating DMAs and IRQs, first allocate the IRQ, then the DMA.
31      * When releasing them, first release the DMA, then release the IRQ.
32      * If you don't, you may cause allocation requests to fail unnecessarily.
33      * This doesn't really matter now, but it will once we get real semaphores
34      * in the kernel.
35      */
36     
37     
38     spinlock_t dma_spin_lock = SPIN_LOCK_UNLOCKED;
39     
40     /*
41      *	If our port doesn't define this it has no PC like DMA
42      */
43     
44     #ifdef MAX_DMA_CHANNELS
45     
46     
47     /* Channel n is busy iff dma_chan_busy[n].lock != 0.
48      * DMA0 used to be reserved for DRAM refresh, but apparently not any more...
49      * DMA4 is reserved for cascading.
50      */
51     
52     struct dma_chan {
53     	int  lock;
54     	const char *device_id;
55     };
56     
57     static struct dma_chan dma_chan_busy[MAX_DMA_CHANNELS] = {
58     	{ 0, 0 },
59     	{ 0, 0 },
60     	{ 0, 0 },
61     	{ 0, 0 },
62     	{ 1, "cascade" },
63     	{ 0, 0 },
64     	{ 0, 0 },
65     	{ 0, 0 }
66     };
67     
68     int get_dma_list(char *buf)
69     {
70     	int i, len = 0;
71     
72     	for (i = 0 ; i < MAX_DMA_CHANNELS ; i++) {
73     		if (dma_chan_busy[i].lock) {
74     		    len += sprintf(buf+len, "%2d: %s\n",
75     				   i,
76     				   dma_chan_busy[i].device_id);
77     		}
78     	}
79     	return len;
80     } /* get_dma_list */
81     
82     
83     int request_dma(unsigned int dmanr, const char * device_id)
84     {
85     	if (dmanr >= MAX_DMA_CHANNELS)
86     		return -EINVAL;
87     
88     	if (xchg(&dma_chan_busy[dmanr].lock, 1) != 0)
89     		return -EBUSY;
90     
91     	dma_chan_busy[dmanr].device_id = device_id;
92     
93     	/* old flag was 0, now contains 1 to indicate busy */
94     	return 0;
95     } /* request_dma */
96     
97     
98     void free_dma(unsigned int dmanr)
99     {
100     	if (dmanr >= MAX_DMA_CHANNELS) {
101     		printk("Trying to free DMA%d\n", dmanr);
102     		return;
103     	}
104     
105     	if (xchg(&dma_chan_busy[dmanr].lock, 0) == 0) {
106     		printk("Trying to free free DMA%d\n", dmanr);
107     		return;
108     	}	
109     
110     } /* free_dma */
111     
112     #else
113     
114     int request_dma(unsigned int dmanr, const char *device_id)
115     {
116     	return -EINVAL;
117     }
118     
119     void free_dma(unsigned int dmanr)
120     {
121     }
122     
123     int get_dma_list(char *buf)
124     {	
125     	strcpy(buf, "No DMA\n");
126     	return 7;
127     }
128     #endif
129