[PATCH] hlist constification
[linux-2.6/history.git] / kernel / dma.c
blob7ad7fbf83a837bf3c44cf9424bcb0a88f07f0daf
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.
4 * Written by Hennus Bergman, 1992.
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]
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/spinlock.h>
16 #include <linux/string.h>
17 #include <linux/seq_file.h>
18 #include <linux/proc_fs.h>
19 #include <linux/init.h>
20 #include <asm/dma.h>
21 #include <asm/system.h>
25 /* A note on resource allocation:
27 * All drivers needing DMA channels, should allocate and release them
28 * through the public routines `request_dma()' and `free_dma()'.
30 * In order to avoid problems, all processes should allocate resources in
31 * the same sequence and release them in the reverse order.
33 * So, when allocating DMAs and IRQs, first allocate the IRQ, then the DMA.
34 * When releasing them, first release the DMA, then release the IRQ.
35 * If you don't, you may cause allocation requests to fail unnecessarily.
36 * This doesn't really matter now, but it will once we get real semaphores
37 * in the kernel.
41 spinlock_t dma_spin_lock = SPIN_LOCK_UNLOCKED;
44 * If our port doesn't define this it has no PC like DMA
47 #ifdef MAX_DMA_CHANNELS
50 /* Channel n is busy iff dma_chan_busy[n].lock != 0.
51 * DMA0 used to be reserved for DRAM refresh, but apparently not any more...
52 * DMA4 is reserved for cascading.
55 struct dma_chan {
56 int lock;
57 const char *device_id;
60 static struct dma_chan dma_chan_busy[MAX_DMA_CHANNELS] = {
61 { 0, 0 },
62 { 0, 0 },
63 { 0, 0 },
64 { 0, 0 },
65 { 1, "cascade" },
66 { 0, 0 },
67 { 0, 0 },
68 { 0, 0 }
72 int request_dma(unsigned int dmanr, const char * device_id)
74 if (dmanr >= MAX_DMA_CHANNELS)
75 return -EINVAL;
77 if (xchg(&dma_chan_busy[dmanr].lock, 1) != 0)
78 return -EBUSY;
80 dma_chan_busy[dmanr].device_id = device_id;
82 /* old flag was 0, now contains 1 to indicate busy */
83 return 0;
84 } /* request_dma */
87 void free_dma(unsigned int dmanr)
89 if (dmanr >= MAX_DMA_CHANNELS) {
90 printk(KERN_WARNING "Trying to free DMA%d\n", dmanr);
91 return;
94 if (xchg(&dma_chan_busy[dmanr].lock, 0) == 0) {
95 printk(KERN_WARNING "Trying to free free DMA%d\n", dmanr);
96 return;
99 } /* free_dma */
101 #else
103 int request_dma(unsigned int dmanr, const char *device_id)
105 return -EINVAL;
108 void free_dma(unsigned int dmanr)
112 #endif
114 #ifdef CONFIG_PROC_FS
116 #ifdef MAX_DMA_CHANNELS
117 static int proc_dma_show(struct seq_file *m, void *v)
119 int i;
121 for (i = 0 ; i < MAX_DMA_CHANNELS ; i++) {
122 if (dma_chan_busy[i].lock) {
123 seq_printf(m, "%2d: %s\n", i,
124 dma_chan_busy[i].device_id);
127 return 0;
129 #else
130 static int proc_dma_show(struct seq_file *m, void *v)
132 seq_puts(m, "No DMA\n");
133 return 0;
135 #endif /* MAX_DMA_CHANNELS */
137 static int proc_dma_open(struct inode *inode, struct file *file)
139 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
140 struct seq_file *m;
141 int res;
143 if (!buf)
144 return -ENOMEM;
145 res = single_open(file, proc_dma_show, NULL);
146 if (!res) {
147 m = file->private_data;
148 m->buf = buf;
149 m->size = PAGE_SIZE;
150 } else
151 kfree(buf);
152 return res;
155 static struct file_operations proc_dma_operations = {
156 .open = proc_dma_open,
157 .read = seq_read,
158 .llseek = seq_lseek,
159 .release = single_release,
162 static int __init proc_dma_init(void)
164 struct proc_dir_entry *e;
166 e = create_proc_entry("dma", 0, NULL);
167 if (e)
168 e->proc_fops = &proc_dma_operations;
170 return 0;
173 __initcall(proc_dma_init);
174 #endif
176 EXPORT_SYMBOL(request_dma);
177 EXPORT_SYMBOL(free_dma);
178 EXPORT_SYMBOL(dma_spin_lock);