ALSA: seq: oss: Don't drain at closing a client
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / core / seq / oss / seq_oss.c
CommitLineData
1da177e4
LT
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
1da177e4 23#include <linux/init.h>
65a77217 24#include <linux/module.h>
1a60d4c5 25#include <linux/mutex.h>
1da177e4
LT
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 */
35MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
36MODULE_DESCRIPTION("OSS-compatible sequencer module");
37MODULE_LICENSE("GPL");
38/* Takashi says this is really only for sound-service-0-, but this is OK. */
39MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_SEQUENCER);
40MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_MUSIC);
41
42#ifdef SNDRV_SEQ_OSS_DEBUG
43module_param(seq_oss_debug, int, 0644);
44MODULE_PARM_DESC(seq_oss_debug, "debug option");
45int seq_oss_debug = 0;
46#endif
47
48
49/*
50 * prototypes
51 */
52static int register_device(void);
53static void unregister_device(void);
04f141a8 54#ifdef CONFIG_PROC_FS
1da177e4
LT
55static int register_proc(void);
56static void unregister_proc(void);
04f141a8
TI
57#else
58static inline int register_proc(void) { return 0; }
59static inline void unregister_proc(void) {}
60#endif
1da177e4
LT
61
62static int odev_open(struct inode *inode, struct file *file);
63static int odev_release(struct inode *inode, struct file *file);
64static ssize_t odev_read(struct file *file, char __user *buf, size_t count, loff_t *offset);
65static ssize_t odev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset);
66static long odev_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
67static unsigned int odev_poll(struct file *file, poll_table * wait);
1da177e4
LT
68
69
70/*
71 * module interface
72 */
73
74static int __init alsa_seq_oss_init(void)
75{
76 int rc;
080dece3 77 static struct snd_seq_dev_ops ops = {
1da177e4
LT
78 snd_seq_oss_synth_register,
79 snd_seq_oss_synth_unregister,
80 };
81
82 snd_seq_autoload_lock();
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 if ((rc = snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_OSS, &ops,
080dece3 96 sizeof(struct snd_seq_oss_reg))) < 0) {
1da177e4
LT
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 snd_seq_autoload_unlock();
108 return rc;
109}
110
111static void __exit alsa_seq_oss_exit(void)
112{
113 snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_OSS);
114 snd_seq_oss_delete_client();
115 unregister_proc();
116 unregister_device();
117}
118
119module_init(alsa_seq_oss_init)
120module_exit(alsa_seq_oss_exit)
121
122/*
123 * ALSA minor device interface
124 */
125
1a60d4c5 126static DEFINE_MUTEX(register_mutex);
1da177e4
LT
127
128static int
129odev_open(struct inode *inode, struct file *file)
130{
131 int level, rc;
132
133 if (iminor(inode) == SNDRV_MINOR_OSS_MUSIC)
134 level = SNDRV_SEQ_OSS_MODE_MUSIC;
135 else
136 level = SNDRV_SEQ_OSS_MODE_SYNTH;
137
1a60d4c5 138 mutex_lock(&register_mutex);
1da177e4 139 rc = snd_seq_oss_open(file, level);
1a60d4c5 140 mutex_unlock(&register_mutex);
1da177e4
LT
141
142 return rc;
143}
144
145static int
146odev_release(struct inode *inode, struct file *file)
147{
080dece3 148 struct seq_oss_devinfo *dp;
1da177e4
LT
149
150 if ((dp = file->private_data) == NULL)
151 return 0;
152
1a60d4c5 153 mutex_lock(&register_mutex);
1da177e4 154 snd_seq_oss_release(dp);
1a60d4c5 155 mutex_unlock(&register_mutex);
1da177e4
LT
156
157 return 0;
158}
159
160static ssize_t
161odev_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
162{
080dece3 163 struct seq_oss_devinfo *dp;
1da177e4 164 dp = file->private_data;
7eaa943c
TI
165 if (snd_BUG_ON(!dp))
166 return -ENXIO;
1da177e4
LT
167 return snd_seq_oss_read(dp, buf, count);
168}
169
170
171static ssize_t
172odev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
173{
080dece3 174 struct seq_oss_devinfo *dp;
1da177e4 175 dp = file->private_data;
7eaa943c
TI
176 if (snd_BUG_ON(!dp))
177 return -ENXIO;
1da177e4
LT
178 return snd_seq_oss_write(dp, buf, count, file);
179}
180
181static long
182odev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
183{
080dece3 184 struct seq_oss_devinfo *dp;
1da177e4 185 dp = file->private_data;
7eaa943c
TI
186 if (snd_BUG_ON(!dp))
187 return -ENXIO;
1da177e4
LT
188 return snd_seq_oss_ioctl(dp, cmd, arg);
189}
190
191#ifdef CONFIG_COMPAT
192#define odev_ioctl_compat odev_ioctl
193#else
194#define odev_ioctl_compat NULL
195#endif
196
197static unsigned int
198odev_poll(struct file *file, poll_table * wait)
199{
080dece3 200 struct seq_oss_devinfo *dp;
1da177e4 201 dp = file->private_data;
7eaa943c
TI
202 if (snd_BUG_ON(!dp))
203 return -ENXIO;
1da177e4
LT
204 return snd_seq_oss_poll(dp, file, wait);
205}
206
207/*
208 * registration of sequencer minor device
209 */
210
9c2e08c5 211static const struct file_operations seq_oss_f_ops =
1da177e4
LT
212{
213 .owner = THIS_MODULE,
214 .read = odev_read,
215 .write = odev_write,
216 .open = odev_open,
217 .release = odev_release,
218 .poll = odev_poll,
219 .unlocked_ioctl = odev_ioctl,
220 .compat_ioctl = odev_ioctl_compat,
6038f373 221 .llseek = noop_llseek,
1da177e4
LT
222};
223
1da177e4
LT
224static int __init
225register_device(void)
226{
227 int rc;
228
1a60d4c5 229 mutex_lock(&register_mutex);
1da177e4
LT
230 if ((rc = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER,
231 NULL, 0,
f87135f5 232 &seq_oss_f_ops, NULL,
1da177e4
LT
233 SNDRV_SEQ_OSS_DEVNAME)) < 0) {
234 snd_printk(KERN_ERR "can't register device seq\n");
1a60d4c5 235 mutex_unlock(&register_mutex);
1da177e4
LT
236 return rc;
237 }
238 if ((rc = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MUSIC,
239 NULL, 0,
f87135f5 240 &seq_oss_f_ops, NULL,
1da177e4
LT
241 SNDRV_SEQ_OSS_DEVNAME)) < 0) {
242 snd_printk(KERN_ERR "can't register device music\n");
243 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, NULL, 0);
1a60d4c5 244 mutex_unlock(&register_mutex);
1da177e4
LT
245 return rc;
246 }
247 debug_printk(("device registered\n"));
1a60d4c5 248 mutex_unlock(&register_mutex);
1da177e4
LT
249 return 0;
250}
251
252static void
253unregister_device(void)
254{
1a60d4c5 255 mutex_lock(&register_mutex);
1da177e4
LT
256 debug_printk(("device unregistered\n"));
257 if (snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MUSIC, NULL, 0) < 0)
258 snd_printk(KERN_ERR "error unregister device music\n");
259 if (snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, NULL, 0) < 0)
260 snd_printk(KERN_ERR "error unregister device seq\n");
1a60d4c5 261 mutex_unlock(&register_mutex);
1da177e4
LT
262}
263
264/*
265 * /proc interface
266 */
267
268#ifdef CONFIG_PROC_FS
269
080dece3 270static struct snd_info_entry *info_entry;
1da177e4
LT
271
272static void
080dece3 273info_read(struct snd_info_entry *entry, struct snd_info_buffer *buf)
1da177e4 274{
1a60d4c5 275 mutex_lock(&register_mutex);
1da177e4
LT
276 snd_iprintf(buf, "OSS sequencer emulation version %s\n", SNDRV_SEQ_OSS_VERSION_STR);
277 snd_seq_oss_system_info_read(buf);
278 snd_seq_oss_synth_info_read(buf);
279 snd_seq_oss_midi_info_read(buf);
1a60d4c5 280 mutex_unlock(&register_mutex);
1da177e4
LT
281}
282
1da177e4
LT
283
284static int __init
285register_proc(void)
286{
080dece3 287 struct snd_info_entry *entry;
1da177e4
LT
288
289 entry = snd_info_create_module_entry(THIS_MODULE, SNDRV_SEQ_OSS_PROCNAME, snd_seq_root);
290 if (entry == NULL)
291 return -ENOMEM;
292
293 entry->content = SNDRV_INFO_CONTENT_TEXT;
294 entry->private_data = NULL;
1da177e4
LT
295 entry->c.text.read = info_read;
296 if (snd_info_register(entry) < 0) {
297 snd_info_free_entry(entry);
298 return -ENOMEM;
299 }
300 info_entry = entry;
1da177e4
LT
301 return 0;
302}
303
304static void
305unregister_proc(void)
306{
746d4a02 307 snd_info_free_entry(info_entry);
1da177e4 308 info_entry = NULL;
1da177e4 309}
04f141a8 310#endif /* CONFIG_PROC_FS */