xref: /linux/sound/core/seq/oss/seq_oss.c (revision 05a54fa773284d1a7923cdfdd8f0c8dabb98bd26)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * OSS compatible sequencer driver
4  *
5  * registration of device and proc
6  *
7  * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
8  */
9 
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/compat.h>
14 #include <sound/core.h>
15 #include <sound/minors.h>
16 #include <sound/initval.h>
17 #include "seq_oss_device.h"
18 #include "seq_oss_synth.h"
19 
20 /*
21  * module option
22  */
23 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
24 MODULE_DESCRIPTION("OSS-compatible sequencer module");
25 MODULE_LICENSE("GPL");
26 /* Takashi says this is really only for sound-service-0-, but this is OK. */
27 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_SEQUENCER);
28 MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_MUSIC);
29 
30 
31 /*
32  * prototypes
33  */
34 static int register_device(void);
35 static void unregister_device(void);
36 #ifdef CONFIG_SND_PROC_FS
37 static int register_proc(void);
38 static void unregister_proc(void);
39 #else
40 static inline int register_proc(void) { return 0; }
41 static inline void unregister_proc(void) {}
42 #endif
43 
44 static int odev_open(struct inode *inode, struct file *file);
45 static int odev_release(struct inode *inode, struct file *file);
46 static ssize_t odev_read(struct file *file, char __user *buf, size_t count, loff_t *offset);
47 static ssize_t odev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset);
48 static long odev_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
49 static __poll_t odev_poll(struct file *file, poll_table * wait);
50 
51 
52 /*
53  * module interface
54  */
55 
56 static struct snd_seq_driver seq_oss_synth_driver = {
57 	.driver = {
58 		.name = KBUILD_MODNAME,
59 		.probe = snd_seq_oss_synth_probe,
60 		.remove = snd_seq_oss_synth_remove,
61 	},
62 	.id = SNDRV_SEQ_DEV_ID_OSS,
63 	.argsize = sizeof(struct snd_seq_oss_reg),
64 };
65 
66 static int __init alsa_seq_oss_init(void)
67 {
68 	int rc;
69 
70 	rc = register_device();
71 	if (rc < 0)
72 		goto error;
73 	rc = register_proc();
74 	if (rc < 0) {
75 		unregister_device();
76 		goto error;
77 	}
78 	rc = snd_seq_oss_create_client();
79 	if (rc < 0) {
80 		unregister_proc();
81 		unregister_device();
82 		goto error;
83 	}
84 
85 	rc = snd_seq_driver_register(&seq_oss_synth_driver);
86 	if (rc < 0) {
87 		snd_seq_oss_delete_client();
88 		unregister_proc();
89 		unregister_device();
90 		goto error;
91 	}
92 
93 	/* success */
94 	snd_seq_oss_synth_init();
95 
96  error:
97 	return rc;
98 }
99 
100 static void __exit alsa_seq_oss_exit(void)
101 {
102 	snd_seq_driver_unregister(&seq_oss_synth_driver);
103 	snd_seq_oss_delete_client();
104 	unregister_proc();
105 	unregister_device();
106 }
107 
108 module_init(alsa_seq_oss_init)
109 module_exit(alsa_seq_oss_exit)
110 
111 /*
112  * ALSA minor device interface
113  */
114 
115 static DEFINE_MUTEX(register_mutex);
116 
117 static int
118 odev_open(struct inode *inode, struct file *file)
119 {
120 	int level;
121 
122 	if (iminor(inode) == SNDRV_MINOR_OSS_MUSIC)
123 		level = SNDRV_SEQ_OSS_MODE_MUSIC;
124 	else
125 		level = SNDRV_SEQ_OSS_MODE_SYNTH;
126 
127 	guard(mutex)(&register_mutex);
128 	return snd_seq_oss_open(file, level);
129 }
130 
131 static int
132 odev_release(struct inode *inode, struct file *file)
133 {
134 	struct seq_oss_devinfo *dp;
135 
136 	dp = file->private_data;
137 	if (!dp)
138 		return 0;
139 
140 	guard(mutex)(&register_mutex);
141 	snd_seq_oss_release(dp);
142 	return 0;
143 }
144 
145 static ssize_t
146 odev_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
147 {
148 	struct seq_oss_devinfo *dp;
149 	dp = file->private_data;
150 	if (snd_BUG_ON(!dp))
151 		return -ENXIO;
152 	return snd_seq_oss_read(dp, buf, count);
153 }
154 
155 
156 static ssize_t
157 odev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
158 {
159 	struct seq_oss_devinfo *dp;
160 	dp = file->private_data;
161 	if (snd_BUG_ON(!dp))
162 		return -ENXIO;
163 	return snd_seq_oss_write(dp, buf, count, file);
164 }
165 
166 static long
167 odev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
168 {
169 	struct seq_oss_devinfo *dp;
170 	long rc;
171 
172 	dp = file->private_data;
173 	if (snd_BUG_ON(!dp))
174 		return -ENXIO;
175 
176 	if (cmd != SNDCTL_SEQ_SYNC &&
177 	    mutex_lock_interruptible(&register_mutex))
178 		return -ERESTARTSYS;
179 	rc = snd_seq_oss_ioctl(dp, cmd, arg);
180 	if (cmd != SNDCTL_SEQ_SYNC)
181 		mutex_unlock(&register_mutex);
182 	return rc;
183 }
184 
185 #ifdef CONFIG_COMPAT
186 static long odev_ioctl_compat(struct file *file, unsigned int cmd,
187 			      unsigned long arg)
188 {
189 	return odev_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
190 }
191 #else
192 #define odev_ioctl_compat	NULL
193 #endif
194 
195 static __poll_t
196 odev_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 EPOLLERR;
202 	return snd_seq_oss_poll(dp, file, wait);
203 }
204 
205 /*
206  * registration of sequencer minor device
207  */
208 
209 static 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 
222 static int __init
223 register_device(void)
224 {
225 	int rc;
226 
227 	guard(mutex)(&register_mutex);
228 	rc = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER,
229 				     NULL, 0,
230 				     &seq_oss_f_ops, NULL);
231 	if (rc < 0) {
232 		pr_err("ALSA: seq_oss: can't register device seq\n");
233 		return rc;
234 	}
235 	rc = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MUSIC,
236 				     NULL, 0,
237 				     &seq_oss_f_ops, NULL);
238 	if (rc < 0) {
239 		pr_err("ALSA: seq_oss: can't register device music\n");
240 		snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, NULL, 0);
241 		return rc;
242 	}
243 	return 0;
244 }
245 
246 static void
247 unregister_device(void)
248 {
249 	guard(mutex)(&register_mutex);
250 	if (snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MUSIC, NULL, 0) < 0)
251 		pr_err("ALSA: seq_oss: error unregister device music\n");
252 	if (snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, NULL, 0) < 0)
253 		pr_err("ALSA: seq_oss: error unregister device seq\n");
254 }
255 
256 /*
257  * /proc interface
258  */
259 
260 #ifdef CONFIG_SND_PROC_FS
261 
262 static struct snd_info_entry *info_entry;
263 
264 static void
265 info_read(struct snd_info_entry *entry, struct snd_info_buffer *buf)
266 {
267 	guard(mutex)(&register_mutex);
268 	snd_iprintf(buf, "OSS sequencer emulation version %s\n", SNDRV_SEQ_OSS_VERSION_STR);
269 	snd_seq_oss_system_info_read(buf);
270 	snd_seq_oss_synth_info_read(buf);
271 	snd_seq_oss_midi_info_read(buf);
272 }
273 
274 
275 static int __init
276 register_proc(void)
277 {
278 	struct snd_info_entry *entry;
279 
280 	entry = snd_info_create_module_entry(THIS_MODULE, SNDRV_SEQ_OSS_PROCNAME, snd_seq_root);
281 	if (entry == NULL)
282 		return -ENOMEM;
283 
284 	entry->content = SNDRV_INFO_CONTENT_TEXT;
285 	entry->private_data = NULL;
286 	entry->c.text.read = info_read;
287 	if (snd_info_register(entry) < 0) {
288 		snd_info_free_entry(entry);
289 		return -ENOMEM;
290 	}
291 	info_entry = entry;
292 	return 0;
293 }
294 
295 static void
296 unregister_proc(void)
297 {
298 	snd_info_free_entry(info_entry);
299 	info_entry = NULL;
300 }
301 #endif /* CONFIG_SND_PROC_FS */
302