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 if ((rc = register_device()) < 0) 71 goto error; 72 if ((rc = register_proc()) < 0) { 73 unregister_device(); 74 goto error; 75 } 76 if ((rc = snd_seq_oss_create_client()) < 0) { 77 unregister_proc(); 78 unregister_device(); 79 goto error; 80 } 81 82 rc = snd_seq_driver_register(&seq_oss_synth_driver); 83 if (rc < 0) { 84 snd_seq_oss_delete_client(); 85 unregister_proc(); 86 unregister_device(); 87 goto error; 88 } 89 90 /* success */ 91 snd_seq_oss_synth_init(); 92 93 error: 94 return rc; 95 } 96 97 static void __exit alsa_seq_oss_exit(void) 98 { 99 snd_seq_driver_unregister(&seq_oss_synth_driver); 100 snd_seq_oss_delete_client(); 101 unregister_proc(); 102 unregister_device(); 103 } 104 105 module_init(alsa_seq_oss_init) 106 module_exit(alsa_seq_oss_exit) 107 108 /* 109 * ALSA minor device interface 110 */ 111 112 static DEFINE_MUTEX(register_mutex); 113 114 static int 115 odev_open(struct inode *inode, struct file *file) 116 { 117 int level, rc; 118 119 if (iminor(inode) == SNDRV_MINOR_OSS_MUSIC) 120 level = SNDRV_SEQ_OSS_MODE_MUSIC; 121 else 122 level = SNDRV_SEQ_OSS_MODE_SYNTH; 123 124 mutex_lock(®ister_mutex); 125 rc = snd_seq_oss_open(file, level); 126 mutex_unlock(®ister_mutex); 127 128 return rc; 129 } 130 131 static int 132 odev_release(struct inode *inode, struct file *file) 133 { 134 struct seq_oss_devinfo *dp; 135 136 if ((dp = file->private_data) == NULL) 137 return 0; 138 139 mutex_lock(®ister_mutex); 140 snd_seq_oss_release(dp); 141 mutex_unlock(®ister_mutex); 142 143 return 0; 144 } 145 146 static ssize_t 147 odev_read(struct file *file, char __user *buf, size_t count, loff_t *offset) 148 { 149 struct seq_oss_devinfo *dp; 150 dp = file->private_data; 151 if (snd_BUG_ON(!dp)) 152 return -ENXIO; 153 return snd_seq_oss_read(dp, buf, count); 154 } 155 156 157 static ssize_t 158 odev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset) 159 { 160 struct seq_oss_devinfo *dp; 161 dp = file->private_data; 162 if (snd_BUG_ON(!dp)) 163 return -ENXIO; 164 return snd_seq_oss_write(dp, buf, count, file); 165 } 166 167 static long 168 odev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 169 { 170 struct seq_oss_devinfo *dp; 171 long rc; 172 173 dp = file->private_data; 174 if (snd_BUG_ON(!dp)) 175 return -ENXIO; 176 177 mutex_lock(®ister_mutex); 178 rc = snd_seq_oss_ioctl(dp, cmd, arg); 179 mutex_unlock(®ister_mutex); 180 return rc; 181 } 182 183 #ifdef CONFIG_COMPAT 184 static long odev_ioctl_compat(struct file *file, unsigned int cmd, 185 unsigned long arg) 186 { 187 return odev_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 188 } 189 #else 190 #define odev_ioctl_compat NULL 191 #endif 192 193 static __poll_t 194 odev_poll(struct file *file, poll_table * wait) 195 { 196 struct seq_oss_devinfo *dp; 197 dp = file->private_data; 198 if (snd_BUG_ON(!dp)) 199 return EPOLLERR; 200 return snd_seq_oss_poll(dp, file, wait); 201 } 202 203 /* 204 * registration of sequencer minor device 205 */ 206 207 static const struct file_operations seq_oss_f_ops = 208 { 209 .owner = THIS_MODULE, 210 .read = odev_read, 211 .write = odev_write, 212 .open = odev_open, 213 .release = odev_release, 214 .poll = odev_poll, 215 .unlocked_ioctl = odev_ioctl, 216 .compat_ioctl = odev_ioctl_compat, 217 .llseek = noop_llseek, 218 }; 219 220 static int __init 221 register_device(void) 222 { 223 int rc; 224 225 mutex_lock(®ister_mutex); 226 if ((rc = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, 227 NULL, 0, 228 &seq_oss_f_ops, NULL)) < 0) { 229 pr_err("ALSA: seq_oss: can't register device seq\n"); 230 mutex_unlock(®ister_mutex); 231 return rc; 232 } 233 if ((rc = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MUSIC, 234 NULL, 0, 235 &seq_oss_f_ops, NULL)) < 0) { 236 pr_err("ALSA: seq_oss: can't register device music\n"); 237 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, NULL, 0); 238 mutex_unlock(®ister_mutex); 239 return rc; 240 } 241 mutex_unlock(®ister_mutex); 242 return 0; 243 } 244 245 static void 246 unregister_device(void) 247 { 248 mutex_lock(®ister_mutex); 249 if (snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MUSIC, NULL, 0) < 0) 250 pr_err("ALSA: seq_oss: error unregister device music\n"); 251 if (snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_SEQUENCER, NULL, 0) < 0) 252 pr_err("ALSA: seq_oss: error unregister device seq\n"); 253 mutex_unlock(®ister_mutex); 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 mutex_lock(®ister_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 mutex_unlock(®ister_mutex); 273 } 274 275 276 static int __init 277 register_proc(void) 278 { 279 struct snd_info_entry *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 = info_read; 288 if (snd_info_register(entry) < 0) { 289 snd_info_free_entry(entry); 290 return -ENOMEM; 291 } 292 info_entry = entry; 293 return 0; 294 } 295 296 static void 297 unregister_proc(void) 298 { 299 snd_info_free_entry(info_entry); 300 info_entry = NULL; 301 } 302 #endif /* CONFIG_SND_PROC_FS */ 303