1
2
3
4
5
6
7
8
9
10
11
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>
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41spinlock_t dma_spin_lock = SPIN_LOCK_UNLOCKED;
42
43
44
45
46
47#ifdef MAX_DMA_CHANNELS
48
49
50
51
52
53
54
55struct dma_chan {
56 int lock;
57 const char *device_id;
58};
59
60static 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 }
69};
70
71
72int request_dma(unsigned int dmanr, const char * device_id)
73{
74 if (dmanr >= MAX_DMA_CHANNELS)
75 return -EINVAL;
76
77 if (xchg(&dma_chan_busy[dmanr].lock, 1) != 0)
78 return -EBUSY;
79
80 dma_chan_busy[dmanr].device_id = device_id;
81
82
83 return 0;
84}
85
86
87void free_dma(unsigned int dmanr)
88{
89 if (dmanr >= MAX_DMA_CHANNELS) {
90 printk(KERN_WARNING "Trying to free DMA%d\n", dmanr);
91 return;
92 }
93
94 if (xchg(&dma_chan_busy[dmanr].lock, 0) == 0) {
95 printk(KERN_WARNING "Trying to free free DMA%d\n", dmanr);
96 return;
97 }
98
99}
100
101static int proc_dma_show(struct seq_file *m, void *v)
102{
103 int i;
104
105 for (i = 0 ; i < MAX_DMA_CHANNELS ; i++) {
106 if (dma_chan_busy[i].lock) {
107 seq_printf(m, "%2d: %s\n", i,
108 dma_chan_busy[i].device_id);
109 }
110 }
111 return 0;
112}
113
114#else
115
116int request_dma(unsigned int dmanr, const char *device_id)
117{
118 return -EINVAL;
119}
120
121void free_dma(unsigned int dmanr)
122{
123}
124
125static int proc_dma_show(struct seq_file *m, void *v)
126{
127 seq_puts(m, "No DMA\n");
128 return 0;
129}
130
131#endif
132
133#ifdef CONFIG_PROC_FS
134static int proc_dma_open(struct inode *inode, struct file *file)
135{
136 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
137 struct seq_file *m;
138 int res;
139
140 if (!buf)
141 return -ENOMEM;
142 res = single_open(file, proc_dma_show, NULL);
143 if (!res) {
144 m = file->private_data;
145 m->buf = buf;
146 m->size = PAGE_SIZE;
147 } else
148 kfree(buf);
149 return res;
150}
151
152static struct file_operations proc_dma_operations = {
153 .open = proc_dma_open,
154 .read = seq_read,
155 .llseek = seq_lseek,
156 .release = single_release,
157};
158
159static int __init proc_dma_init(void)
160{
161 struct proc_dir_entry *e;
162
163 e = create_proc_entry("dma", 0, NULL);
164 if (e)
165 e->proc_fops = &proc_dma_operations;
166
167 return 0;
168}
169
170__initcall(proc_dma_init);
171#endif
172
173EXPORT_SYMBOL(request_dma);
174EXPORT_SYMBOL(free_dma);
175EXPORT_SYMBOL(dma_spin_lock);
176