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