xref: /openbmc/linux/sound/core/seq/oss/seq_oss.c (revision afb46f79)
1 /*
2  * OSS compatible sequencer driver
3  *
4  * registration of device and proc
5  *
6  * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22 
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <sound/core.h>
27 #include <sound/minors.h>
28 #include <sound/initval.h>
29 #include "seq_oss_device.h"
30 #include "seq_oss_synth.h"
31 
32 /*
33  * module option
34  */
35 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
36 MODULE_DESCRIPTION("OSS-compatible sequencer module");
37 MODULE_LICENSE("GPL");
38 /* Takashi says this is really only for sound-service-0-, but this is OK. */
39 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_SEQUENCER);
40 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_MUSIC);
41 
42 
43 /*
44  * prototypes
45  */
46 static int register_device(void);
47 static void unregister_device(void);
48 #ifdef CONFIG_PROC_FS
49 static int register_proc(void);
50 static void unregister_proc(void);
51 #else
52 static inline int register_proc(void) { return 0; }
53 static inline void unregister_proc(void) {}
54 #endif
55 
56 static int odev_open(struct inode *inode, struct file *file);
57 static int odev_release(struct inode *inode, struct file *file);
58 static ssize_t odev_read(struct file *file, char __user *buf, size_t count, loff_t *offset);
59 static ssize_t odev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset);
60 static long odev_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
61 static unsigned int odev_poll(struct file *file, poll_table * wait);
62 
63 
64 /*
65  * module interface
66  */
67 
68 static int __init alsa_seq_oss_init(void)
69 {
70 	int rc;
71 	static struct snd_seq_dev_ops ops = {
72 		snd_seq_oss_synth_register,
73 		snd_seq_oss_synth_unregister,
74 	};
75 
76 	snd_seq_autoload_lock();
77 	if ((rc = register_device()) < 0)
78 		goto error;
79 	if ((rc = register_proc()) < 0) {
80 		unregister_device();
81 		goto error;
82 	}
83 	if ((rc = snd_seq_oss_create_client()) < 0) {
84 		unregister_proc();
85 		unregister_device();
86 		goto error;
87 	}
88 
89 	if ((rc = snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_OSS, &ops,
90 						 sizeof(struct snd_seq_oss_reg))) < 0) {
91 		snd_seq_oss_delete_client();
92 		unregister_proc();
93 		unregister_device();
94 		goto error;
95 	}
96 
97 	/* success */
98 	snd_seq_oss_synth_init();
99 
100  error:
101 	snd_seq_autoload_unlock();
102 	return rc;
103 }
104 
105 static void __exit alsa_seq_oss_exit(void)
106 {
107 	snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_OSS);
108 	snd_seq_oss_delete_client();
109 	unregister_proc();
110 	unregister_device();
111 }
112 
113 module_init(alsa_seq_oss_init)
114 module_exit(alsa_seq_oss_exit)
115 
116 /*
117  * ALSA minor device interface
118  */
119 
120 static DEFINE_MUTEX(register_mutex);
121 
122 static int
123 odev_open(struct inode *inode, struct file *file)
124 {
125 	int level, rc;
126 
127 	if (iminor(inode) == SNDRV_MINOR_OSS_MUSIC)
128 		level = SNDRV_SEQ_OSS_MODE_MUSIC;
129 	else
130 		level = SNDRV_SEQ_OSS_MODE_SYNTH;
131 
132 	mutex_lock(&register_mutex);
133 	rc = snd_seq_oss_open(file, level);
134 	mutex_unlock(&register_mutex);
135 
136 	return rc;
137 }
138 
139 static int
140 odev_release(struct inode *inode, struct file *file)
141 {
142 	struct seq_oss_devinfo *dp;
143 
144 	if ((dp = file->private_data) == NULL)
145 		return 0;
146 
147 	snd_seq_oss_drain_write(dp);
148 
149 	mutex_lock(&register_mutex);
150 	snd_seq_oss_release(dp);
151 	mutex_unlock(&register_mutex);
152 
153 	return 0;
154 }
155 
156 static ssize_t
157 odev_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
158 {
159 	struct seq_oss_devinfo *dp;
160 	dp = file->private_data;
161 	if (snd_BUG_ON(!dp))
162 		return -ENXIO;
163 	return snd_seq_oss_read(dp, buf, count);
164 }
165 
166 
167 static ssize_t
168 odev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
169 {
170 	struct seq_oss_devinfo *dp;
171 	dp = file->private_data;
172 	if (snd_BUG_ON(!dp))
173 		return -ENXIO;
174 	return snd_seq_oss_write(dp, buf, count, file);
175 }
176 
177 static long
178 odev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
179 {
180 	struct seq_oss_devinfo *dp;
181 	dp = file->private_data;
182 	if (snd_BUG_ON(!dp))
183 		return -ENXIO;
184 	return snd_seq_oss_ioctl(dp, cmd, arg);
185 }
186 
187 #ifdef CONFIG_COMPAT
188 #define odev_ioctl_compat	odev_ioctl
189 #else
190 #define odev_ioctl_compat	NULL
191 #endif
192 
193 static unsigned int
194 odev_poll(struct file *file, poll_table * wait)
195 {
196 	struct seq_oss_devinfo *dp;
197 	dp = file->private_data;
198 	if (snd_BUG_ON(!dp))
199 		return -ENXIO;
200 	return snd_seq_oss_poll(dp, file, wait);
201 }
202 
203 /*
204  * registration of sequencer minor device
205  */
206 
207 static const struct file_operations seq_oss_f_ops =
208 {
209 	.owner =	THIS_MODULE,
210 	.read =		odev_read,
211 	.write =	odev_write,
212 	.open =		odev_open,
213 	.release =	odev_release,
214 	.poll =		odev_poll,
215 	.unlocked_ioctl =	odev_ioctl,
216 	.compat_ioctl =	odev_ioctl_compat,
217 	.llseek =	noop_llseek,
218 };
219 
220 static int __init
221 register_device(void)
222 {
223 	int rc;
224 
225 	mutex_lock(&register_mutex);
226 	if ((rc = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER,
227 					  NULL, 0,
228 					  &seq_oss_f_ops, NULL)) < 0) {
229 		pr_err("ALSA: seq_oss: can't register device seq\n");
230 		mutex_unlock(&register_mutex);
231 		return rc;
232 	}
233 	if ((rc = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MUSIC,
234 					  NULL, 0,
235 					  &seq_oss_f_ops, NULL)) < 0) {
236 		pr_err("ALSA: seq_oss: can't register device music\n");
237 		snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, NULL, 0);
238 		mutex_unlock(&register_mutex);
239 		return rc;
240 	}
241 	mutex_unlock(&register_mutex);
242 	return 0;
243 }
244 
245 static void
246 unregister_device(void)
247 {
248 	mutex_lock(&register_mutex);
249 	if (snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MUSIC, NULL, 0) < 0)
250 		pr_err("ALSA: seq_oss: error unregister device music\n");
251 	if (snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, NULL, 0) < 0)
252 		pr_err("ALSA: seq_oss: error unregister device seq\n");
253 	mutex_unlock(&register_mutex);
254 }
255 
256 /*
257  * /proc interface
258  */
259 
260 #ifdef CONFIG_PROC_FS
261 
262 static struct snd_info_entry *info_entry;
263 
264 static void
265 info_read(struct snd_info_entry *entry, struct snd_info_buffer *buf)
266 {
267 	mutex_lock(&register_mutex);
268 	snd_iprintf(buf, "OSS sequencer emulation version %s\n", SNDRV_SEQ_OSS_VERSION_STR);
269 	snd_seq_oss_system_info_read(buf);
270 	snd_seq_oss_synth_info_read(buf);
271 	snd_seq_oss_midi_info_read(buf);
272 	mutex_unlock(&register_mutex);
273 }
274 
275 
276 static int __init
277 register_proc(void)
278 {
279 	struct snd_info_entry *entry;
280 
281 	entry = snd_info_create_module_entry(THIS_MODULE, SNDRV_SEQ_OSS_PROCNAME, snd_seq_root);
282 	if (entry == NULL)
283 		return -ENOMEM;
284 
285 	entry->content = SNDRV_INFO_CONTENT_TEXT;
286 	entry->private_data = NULL;
287 	entry->c.text.read = info_read;
288 	if (snd_info_register(entry) < 0) {
289 		snd_info_free_entry(entry);
290 		return -ENOMEM;
291 	}
292 	info_entry = entry;
293 	return 0;
294 }
295 
296 static void
297 unregister_proc(void)
298 {
299 	snd_info_free_entry(info_entry);
300 	info_entry = NULL;
301 }
302 #endif /* CONFIG_PROC_FS */
303