• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <linux/compat.h>
27 #include <sound/core.h>
28 #include <sound/minors.h>
29 #include <sound/initval.h>
30 #include "seq_oss_device.h"
31 #include "seq_oss_synth.h"
32 
33 /*
34  * module option
35  */
36 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
37 MODULE_DESCRIPTION("OSS-compatible sequencer module");
38 MODULE_LICENSE("GPL");
39 /* Takashi says this is really only for sound-service-0-, but this is OK. */
40 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_SEQUENCER);
41 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_MUSIC);
42 
43 
44 /*
45  * prototypes
46  */
47 static int register_device(void);
48 static void unregister_device(void);
49 #ifdef CONFIG_SND_PROC_FS
50 static int register_proc(void);
51 static void unregister_proc(void);
52 #else
register_proc(void)53 static inline int register_proc(void) { return 0; }
unregister_proc(void)54 static inline void unregister_proc(void) {}
55 #endif
56 
57 static int odev_open(struct inode *inode, struct file *file);
58 static int odev_release(struct inode *inode, struct file *file);
59 static ssize_t odev_read(struct file *file, char __user *buf, size_t count, loff_t *offset);
60 static ssize_t odev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset);
61 static long odev_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
62 static __poll_t odev_poll(struct file *file, poll_table * wait);
63 
64 
65 /*
66  * module interface
67  */
68 
69 static struct snd_seq_driver seq_oss_synth_driver = {
70 	.driver = {
71 		.name = KBUILD_MODNAME,
72 		.probe = snd_seq_oss_synth_probe,
73 		.remove = snd_seq_oss_synth_remove,
74 	},
75 	.id = SNDRV_SEQ_DEV_ID_OSS,
76 	.argsize = sizeof(struct snd_seq_oss_reg),
77 };
78 
alsa_seq_oss_init(void)79 static int __init alsa_seq_oss_init(void)
80 {
81 	int rc;
82 
83 	if ((rc = register_device()) < 0)
84 		goto error;
85 	if ((rc = register_proc()) < 0) {
86 		unregister_device();
87 		goto error;
88 	}
89 	if ((rc = snd_seq_oss_create_client()) < 0) {
90 		unregister_proc();
91 		unregister_device();
92 		goto error;
93 	}
94 
95 	rc = snd_seq_driver_register(&seq_oss_synth_driver);
96 	if (rc < 0) {
97 		snd_seq_oss_delete_client();
98 		unregister_proc();
99 		unregister_device();
100 		goto error;
101 	}
102 
103 	/* success */
104 	snd_seq_oss_synth_init();
105 
106  error:
107 	return rc;
108 }
109 
alsa_seq_oss_exit(void)110 static void __exit alsa_seq_oss_exit(void)
111 {
112 	snd_seq_driver_unregister(&seq_oss_synth_driver);
113 	snd_seq_oss_delete_client();
114 	unregister_proc();
115 	unregister_device();
116 }
117 
118 module_init(alsa_seq_oss_init)
119 module_exit(alsa_seq_oss_exit)
120 
121 /*
122  * ALSA minor device interface
123  */
124 
125 static DEFINE_MUTEX(register_mutex);
126 
127 static int
odev_open(struct inode * inode,struct file * file)128 odev_open(struct inode *inode, struct file *file)
129 {
130 	int level, rc;
131 
132 	if (iminor(inode) == SNDRV_MINOR_OSS_MUSIC)
133 		level = SNDRV_SEQ_OSS_MODE_MUSIC;
134 	else
135 		level = SNDRV_SEQ_OSS_MODE_SYNTH;
136 
137 	mutex_lock(&register_mutex);
138 	rc = snd_seq_oss_open(file, level);
139 	mutex_unlock(&register_mutex);
140 
141 	return rc;
142 }
143 
144 static int
odev_release(struct inode * inode,struct file * file)145 odev_release(struct inode *inode, struct file *file)
146 {
147 	struct seq_oss_devinfo *dp;
148 
149 	if ((dp = file->private_data) == NULL)
150 		return 0;
151 
152 	mutex_lock(&register_mutex);
153 	snd_seq_oss_release(dp);
154 	mutex_unlock(&register_mutex);
155 
156 	return 0;
157 }
158 
159 static ssize_t
odev_read(struct file * file,char __user * buf,size_t count,loff_t * offset)160 odev_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
161 {
162 	struct seq_oss_devinfo *dp;
163 	dp = file->private_data;
164 	if (snd_BUG_ON(!dp))
165 		return -ENXIO;
166 	return snd_seq_oss_read(dp, buf, count);
167 }
168 
169 
170 static ssize_t
odev_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)171 odev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
172 {
173 	struct seq_oss_devinfo *dp;
174 	dp = file->private_data;
175 	if (snd_BUG_ON(!dp))
176 		return -ENXIO;
177 	return snd_seq_oss_write(dp, buf, count, file);
178 }
179 
180 static long
odev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)181 odev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
182 {
183 	struct seq_oss_devinfo *dp;
184 	long rc;
185 
186 	dp = file->private_data;
187 	if (snd_BUG_ON(!dp))
188 		return -ENXIO;
189 
190 	if (cmd != SNDCTL_SEQ_SYNC &&
191 	    mutex_lock_interruptible(&register_mutex))
192 		return -ERESTARTSYS;
193 	rc = snd_seq_oss_ioctl(dp, cmd, arg);
194 	if (cmd != SNDCTL_SEQ_SYNC)
195 		mutex_unlock(&register_mutex);
196 	return rc;
197 }
198 
199 #ifdef CONFIG_COMPAT
odev_ioctl_compat(struct file * file,unsigned int cmd,unsigned long arg)200 static long odev_ioctl_compat(struct file *file, unsigned int cmd,
201 			      unsigned long arg)
202 {
203 	return odev_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
204 }
205 #else
206 #define odev_ioctl_compat	NULL
207 #endif
208 
209 static __poll_t
odev_poll(struct file * file,poll_table * wait)210 odev_poll(struct file *file, poll_table * wait)
211 {
212 	struct seq_oss_devinfo *dp;
213 	dp = file->private_data;
214 	if (snd_BUG_ON(!dp))
215 		return EPOLLERR;
216 	return snd_seq_oss_poll(dp, file, wait);
217 }
218 
219 /*
220  * registration of sequencer minor device
221  */
222 
223 static const struct file_operations seq_oss_f_ops =
224 {
225 	.owner =	THIS_MODULE,
226 	.read =		odev_read,
227 	.write =	odev_write,
228 	.open =		odev_open,
229 	.release =	odev_release,
230 	.poll =		odev_poll,
231 	.unlocked_ioctl =	odev_ioctl,
232 	.compat_ioctl =	odev_ioctl_compat,
233 	.llseek =	noop_llseek,
234 };
235 
236 static int __init
register_device(void)237 register_device(void)
238 {
239 	int rc;
240 
241 	mutex_lock(&register_mutex);
242 	if ((rc = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER,
243 					  NULL, 0,
244 					  &seq_oss_f_ops, NULL)) < 0) {
245 		pr_err("ALSA: seq_oss: can't register device seq\n");
246 		mutex_unlock(&register_mutex);
247 		return rc;
248 	}
249 	if ((rc = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MUSIC,
250 					  NULL, 0,
251 					  &seq_oss_f_ops, NULL)) < 0) {
252 		pr_err("ALSA: seq_oss: can't register device music\n");
253 		snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, NULL, 0);
254 		mutex_unlock(&register_mutex);
255 		return rc;
256 	}
257 	mutex_unlock(&register_mutex);
258 	return 0;
259 }
260 
261 static void
unregister_device(void)262 unregister_device(void)
263 {
264 	mutex_lock(&register_mutex);
265 	if (snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MUSIC, NULL, 0) < 0)
266 		pr_err("ALSA: seq_oss: error unregister device music\n");
267 	if (snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, NULL, 0) < 0)
268 		pr_err("ALSA: seq_oss: error unregister device seq\n");
269 	mutex_unlock(&register_mutex);
270 }
271 
272 /*
273  * /proc interface
274  */
275 
276 #ifdef CONFIG_SND_PROC_FS
277 
278 static struct snd_info_entry *info_entry;
279 
280 static void
info_read(struct snd_info_entry * entry,struct snd_info_buffer * buf)281 info_read(struct snd_info_entry *entry, struct snd_info_buffer *buf)
282 {
283 	mutex_lock(&register_mutex);
284 	snd_iprintf(buf, "OSS sequencer emulation version %s\n", SNDRV_SEQ_OSS_VERSION_STR);
285 	snd_seq_oss_system_info_read(buf);
286 	snd_seq_oss_synth_info_read(buf);
287 	snd_seq_oss_midi_info_read(buf);
288 	mutex_unlock(&register_mutex);
289 }
290 
291 
292 static int __init
register_proc(void)293 register_proc(void)
294 {
295 	struct snd_info_entry *entry;
296 
297 	entry = snd_info_create_module_entry(THIS_MODULE, SNDRV_SEQ_OSS_PROCNAME, snd_seq_root);
298 	if (entry == NULL)
299 		return -ENOMEM;
300 
301 	entry->content = SNDRV_INFO_CONTENT_TEXT;
302 	entry->private_data = NULL;
303 	entry->c.text.read = info_read;
304 	if (snd_info_register(entry) < 0) {
305 		snd_info_free_entry(entry);
306 		return -ENOMEM;
307 	}
308 	info_entry = entry;
309 	return 0;
310 }
311 
312 static void
unregister_proc(void)313 unregister_proc(void)
314 {
315 	snd_info_free_entry(info_entry);
316 	info_entry = NULL;
317 }
318 #endif /* CONFIG_SND_PROC_FS */
319