ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / core / seq / oss / seq_oss.c
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 <sound/driver.h>
24 #include <linux/init.h>
25 #include <sound/core.h>
26 #include <sound/minors.h>
27 #include <sound/initval.h>
28 #include "seq_oss_device.h"
29 #include "seq_oss_synth.h"
30
31 /*
32  * module option
33  */
34 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
35 MODULE_DESCRIPTION("OSS-compatible sequencer module");
36 MODULE_LICENSE("GPL");
37 MODULE_CLASSES("{sound}");
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 #ifdef SNDRV_SEQ_OSS_DEBUG
43 MODULE_PARM(seq_oss_debug, "i");
44 MODULE_PARM_DESC(seq_oss_debug, "debug option");
45 int seq_oss_debug = 0;
46 #endif
47
48
49 /*
50  * prototypes
51  */
52 static int register_device(void);
53 static void unregister_device(void);
54 static int register_proc(void);
55 static void unregister_proc(void);
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 *buf, size_t count, loff_t *offset);
60 static ssize_t odev_write(struct file *file, const char *buf, size_t count, loff_t *offset);
61 static int odev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
62 static unsigned int odev_poll(struct file *file, poll_table * wait);
63 #ifdef CONFIG_PROC_FS
64 static void info_read(snd_info_entry_t *entry, snd_info_buffer_t *buf);
65 #endif
66
67
68 /*
69  * module interface
70  */
71
72 static int __init alsa_seq_oss_init(void)
73 {
74         int rc;
75         static snd_seq_dev_ops_t ops = {
76                 snd_seq_oss_synth_register,
77                 snd_seq_oss_synth_unregister,
78         };
79
80         if ((rc = register_device()) < 0)
81                 return rc;
82         if ((rc = register_proc()) < 0) {
83                 unregister_device();
84                 return rc;
85         }
86         if ((rc = snd_seq_oss_create_client()) < 0) {
87                 unregister_proc();
88                 unregister_device();
89                 return rc;
90         }
91
92         if ((rc = snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_OSS, &ops,
93                                                  sizeof(snd_seq_oss_reg_t))) < 0) {
94                 snd_seq_oss_delete_client();
95                 unregister_proc();
96                 unregister_device();
97                 return rc;
98         }
99
100         /* success */
101         snd_seq_oss_synth_init();
102         return 0;
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 DECLARE_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         down(&register_mutex);
133         rc = snd_seq_oss_open(file, level);
134         up(&register_mutex);
135
136         return rc;
137 }
138
139 static int
140 odev_release(struct inode *inode, struct file *file)
141 {
142         seq_oss_devinfo_t *dp;
143
144         if ((dp = file->private_data) == NULL)
145                 return 0;
146
147         snd_seq_oss_drain_write(dp);
148
149         down(&register_mutex);
150         snd_seq_oss_release(dp);
151         up(&register_mutex);
152
153         return 0;
154 }
155
156 static ssize_t
157 odev_read(struct file *file, char *buf, size_t count, loff_t *offset)
158 {
159         seq_oss_devinfo_t *dp;
160         dp = file->private_data;
161         snd_assert(dp != NULL, return -EIO);
162         return snd_seq_oss_read(dp, buf, count);
163 }
164
165
166 static ssize_t
167 odev_write(struct file *file, const char *buf, size_t count, loff_t *offset)
168 {
169         seq_oss_devinfo_t *dp;
170         dp = file->private_data;
171         snd_assert(dp != NULL, return -EIO);
172         return snd_seq_oss_write(dp, buf, count, file);
173 }
174
175 static int
176 odev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
177 {
178         seq_oss_devinfo_t *dp;
179         dp = file->private_data;
180         snd_assert(dp != NULL, return -EIO);
181         return snd_seq_oss_ioctl(dp, cmd, arg);
182 }
183
184
185 static unsigned int
186 odev_poll(struct file *file, poll_table * wait)
187 {
188         seq_oss_devinfo_t *dp;
189         dp = file->private_data;
190         snd_assert(dp != NULL, return 0);
191         return snd_seq_oss_poll(dp, file, wait);
192 }
193
194 /*
195  * registration of sequencer minor device
196  */
197
198 static struct file_operations seq_oss_f_ops =
199 {
200         .owner =        THIS_MODULE,
201         .read =         odev_read,
202         .write =        odev_write,
203         .open =         odev_open,
204         .release =      odev_release,
205         .poll =         odev_poll,
206         .ioctl =        odev_ioctl,
207 };
208
209 static snd_minor_t seq_oss_reg = {
210         .comment =      "sequencer",
211         .f_ops =        &seq_oss_f_ops,
212 };
213
214 static int __init
215 register_device(void)
216 {
217         int rc;
218
219         down(&register_mutex);
220         if ((rc = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER,
221                                           NULL, 0,
222                                           &seq_oss_reg,
223                                           SNDRV_SEQ_OSS_DEVNAME)) < 0) {
224                 snd_printk(KERN_ERR "can't register device seq\n");
225                 up(&register_mutex);
226                 return rc;
227         }
228         if ((rc = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MUSIC,
229                                           NULL, 0,
230                                           &seq_oss_reg,
231                                           SNDRV_SEQ_OSS_DEVNAME)) < 0) {
232                 snd_printk(KERN_ERR "can't register device music\n");
233                 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, NULL, 0);
234                 up(&register_mutex);
235                 return rc;
236         }
237         debug_printk(("device registered\n"));
238         up(&register_mutex);
239         return 0;
240 }
241
242 static void
243 unregister_device(void)
244 {
245         down(&register_mutex);
246         debug_printk(("device unregistered\n"));
247         if (snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MUSIC, NULL, 0) < 0)                
248                 snd_printk(KERN_ERR "error unregister device music\n");
249         if (snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, NULL, 0) < 0)
250                 snd_printk(KERN_ERR "error unregister device seq\n");
251         up(&register_mutex);
252 }
253
254 /*
255  * /proc interface
256  */
257
258 #ifdef CONFIG_PROC_FS
259
260 static snd_info_entry_t *info_entry;
261
262 static void
263 info_read(snd_info_entry_t *entry, snd_info_buffer_t *buf)
264 {
265         down(&register_mutex);
266         snd_iprintf(buf, "OSS sequencer emulation version %s\n", SNDRV_SEQ_OSS_VERSION_STR);
267         snd_seq_oss_system_info_read(buf);
268         snd_seq_oss_synth_info_read(buf);
269         snd_seq_oss_midi_info_read(buf);
270         up(&register_mutex);
271 }
272
273 #endif /* CONFIG_PROC_FS */
274
275 static int __init
276 register_proc(void)
277 {
278 #ifdef CONFIG_PROC_FS
279         snd_info_entry_t *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_size = 1024;
288         entry->c.text.read = info_read;
289         if (snd_info_register(entry) < 0) {
290                 snd_info_free_entry(entry);
291                 return -ENOMEM;
292         }
293         info_entry = entry;
294 #endif
295         return 0;
296 }
297
298 static void
299 unregister_proc(void)
300 {
301 #ifdef CONFIG_PROC_FS
302         if (info_entry)
303                 snd_info_unregister(info_entry);
304         info_entry = NULL;
305 #endif
306 }