50a3740bf2208232477159b2926a28a192f01d73
[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 <linux/moduleparam.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 MODULE_CLASSES("{sound}");
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 #ifdef SNDRV_SEQ_OSS_DEBUG
44 module_param(seq_oss_debug, int, 0644);
45 MODULE_PARM_DESC(seq_oss_debug, "debug option");
46 int seq_oss_debug = 0;
47 #endif
48
49
50 /*
51  * prototypes
52  */
53 static int register_device(void);
54 static void unregister_device(void);
55 static int register_proc(void);
56 static void unregister_proc(void);
57
58 static int odev_open(struct inode *inode, struct file *file);
59 static int odev_release(struct inode *inode, struct file *file);
60 static ssize_t odev_read(struct file *file, char *buf, size_t count, loff_t *offset);
61 static ssize_t odev_write(struct file *file, const char *buf, size_t count, loff_t *offset);
62 static int odev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
63 static unsigned int odev_poll(struct file *file, poll_table * wait);
64 #ifdef CONFIG_PROC_FS
65 static void info_read(snd_info_entry_t *entry, snd_info_buffer_t *buf);
66 #endif
67
68
69 /*
70  * module interface
71  */
72
73 static int __init alsa_seq_oss_init(void)
74 {
75         int rc;
76         static snd_seq_dev_ops_t ops = {
77                 snd_seq_oss_synth_register,
78                 snd_seq_oss_synth_unregister,
79         };
80
81         if ((rc = register_device()) < 0)
82                 return rc;
83         if ((rc = register_proc()) < 0) {
84                 unregister_device();
85                 return rc;
86         }
87         if ((rc = snd_seq_oss_create_client()) < 0) {
88                 unregister_proc();
89                 unregister_device();
90                 return rc;
91         }
92
93         if ((rc = snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_OSS, &ops,
94                                                  sizeof(snd_seq_oss_reg_t))) < 0) {
95                 snd_seq_oss_delete_client();
96                 unregister_proc();
97                 unregister_device();
98                 return rc;
99         }
100
101         /* success */
102         snd_seq_oss_synth_init();
103         return 0;
104 }
105
106 static void __exit alsa_seq_oss_exit(void)
107 {
108         snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_OSS);
109         snd_seq_oss_delete_client();
110         unregister_proc();
111         unregister_device();
112 }
113
114 module_init(alsa_seq_oss_init)
115 module_exit(alsa_seq_oss_exit)
116
117 /*
118  * ALSA minor device interface
119  */
120
121 static DECLARE_MUTEX(register_mutex);
122
123 static int
124 odev_open(struct inode *inode, struct file *file)
125 {
126         int level, rc;
127
128         if (iminor(inode) == SNDRV_MINOR_OSS_MUSIC)
129                 level = SNDRV_SEQ_OSS_MODE_MUSIC;
130         else
131                 level = SNDRV_SEQ_OSS_MODE_SYNTH;
132
133         down(&register_mutex);
134         rc = snd_seq_oss_open(file, level);
135         up(&register_mutex);
136
137         return rc;
138 }
139
140 static int
141 odev_release(struct inode *inode, struct file *file)
142 {
143         seq_oss_devinfo_t *dp;
144
145         if ((dp = file->private_data) == NULL)
146                 return 0;
147
148         snd_seq_oss_drain_write(dp);
149
150         down(&register_mutex);
151         snd_seq_oss_release(dp);
152         up(&register_mutex);
153
154         return 0;
155 }
156
157 static ssize_t
158 odev_read(struct file *file, char *buf, size_t count, loff_t *offset)
159 {
160         seq_oss_devinfo_t *dp;
161         dp = file->private_data;
162         snd_assert(dp != NULL, return -EIO);
163         return snd_seq_oss_read(dp, buf, count);
164 }
165
166
167 static ssize_t
168 odev_write(struct file *file, const char *buf, size_t count, loff_t *offset)
169 {
170         seq_oss_devinfo_t *dp;
171         dp = file->private_data;
172         snd_assert(dp != NULL, return -EIO);
173         return snd_seq_oss_write(dp, buf, count, file);
174 }
175
176 static int
177 odev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
178 {
179         seq_oss_devinfo_t *dp;
180         dp = file->private_data;
181         snd_assert(dp != NULL, return -EIO);
182         return snd_seq_oss_ioctl(dp, cmd, arg);
183 }
184
185
186 static unsigned int
187 odev_poll(struct file *file, poll_table * wait)
188 {
189         seq_oss_devinfo_t *dp;
190         dp = file->private_data;
191         snd_assert(dp != NULL, return 0);
192         return snd_seq_oss_poll(dp, file, wait);
193 }
194
195 /*
196  * registration of sequencer minor device
197  */
198
199 static struct file_operations seq_oss_f_ops =
200 {
201         .owner =        THIS_MODULE,
202         .read =         odev_read,
203         .write =        odev_write,
204         .open =         odev_open,
205         .release =      odev_release,
206         .poll =         odev_poll,
207         .ioctl =        odev_ioctl,
208 };
209
210 static snd_minor_t seq_oss_reg = {
211         .comment =      "sequencer",
212         .f_ops =        &seq_oss_f_ops,
213 };
214
215 static int __init
216 register_device(void)
217 {
218         int rc;
219
220         down(&register_mutex);
221         if ((rc = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER,
222                                           NULL, 0,
223                                           &seq_oss_reg,
224                                           SNDRV_SEQ_OSS_DEVNAME)) < 0) {
225                 snd_printk(KERN_ERR "can't register device seq\n");
226                 up(&register_mutex);
227                 return rc;
228         }
229         if ((rc = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MUSIC,
230                                           NULL, 0,
231                                           &seq_oss_reg,
232                                           SNDRV_SEQ_OSS_DEVNAME)) < 0) {
233                 snd_printk(KERN_ERR "can't register device music\n");
234                 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, NULL, 0);
235                 up(&register_mutex);
236                 return rc;
237         }
238         debug_printk(("device registered\n"));
239         up(&register_mutex);
240         return 0;
241 }
242
243 static void
244 unregister_device(void)
245 {
246         down(&register_mutex);
247         debug_printk(("device unregistered\n"));
248         if (snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MUSIC, NULL, 0) < 0)                
249                 snd_printk(KERN_ERR "error unregister device music\n");
250         if (snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, NULL, 0) < 0)
251                 snd_printk(KERN_ERR "error unregister device seq\n");
252         up(&register_mutex);
253 }
254
255 /*
256  * /proc interface
257  */
258
259 #ifdef CONFIG_PROC_FS
260
261 static snd_info_entry_t *info_entry;
262
263 static void
264 info_read(snd_info_entry_t *entry, snd_info_buffer_t *buf)
265 {
266         down(&register_mutex);
267         snd_iprintf(buf, "OSS sequencer emulation version %s\n", SNDRV_SEQ_OSS_VERSION_STR);
268         snd_seq_oss_system_info_read(buf);
269         snd_seq_oss_synth_info_read(buf);
270         snd_seq_oss_midi_info_read(buf);
271         up(&register_mutex);
272 }
273
274 #endif /* CONFIG_PROC_FS */
275
276 static int __init
277 register_proc(void)
278 {
279 #ifdef CONFIG_PROC_FS
280         snd_info_entry_t *entry;
281
282         entry = snd_info_create_module_entry(THIS_MODULE, SNDRV_SEQ_OSS_PROCNAME, snd_seq_root);
283         if (entry == NULL)
284                 return -ENOMEM;
285
286         entry->content = SNDRV_INFO_CONTENT_TEXT;
287         entry->private_data = NULL;
288         entry->c.text.read_size = 1024;
289         entry->c.text.read = info_read;
290         if (snd_info_register(entry) < 0) {
291                 snd_info_free_entry(entry);
292                 return -ENOMEM;
293         }
294         info_entry = entry;
295 #endif
296         return 0;
297 }
298
299 static void
300 unregister_proc(void)
301 {
302 #ifdef CONFIG_PROC_FS
303         if (info_entry)
304                 snd_info_unregister(info_entry);
305         info_entry = NULL;
306 #endif
307 }