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