1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OSS compatible sequencer driver
5 * synth device handlers
7 * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
10 #include "seq_oss_synth.h"
11 #include "seq_oss_midi.h"
12 #include "../seq_lock.h"
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/nospec.h>
21 #define SNDRV_SEQ_OSS_MAX_SYNTH_NAME 30
22 #define MAX_SYSEX_BUFLEN 128
26 * definition of synth info records
30 struct seq_oss_synth_sysex {
33 unsigned char buf[MAX_SYSEX_BUFLEN];
37 struct seq_oss_synth {
45 char name[SNDRV_SEQ_OSS_MAX_SYNTH_NAME];
46 struct snd_seq_oss_callback oper;
51 snd_use_lock_t use_lock;
58 static int max_synth_devs;
59 static struct seq_oss_synth *synth_devs[SNDRV_SEQ_OSS_MAX_SYNTH_DEVS];
60 static struct seq_oss_synth midi_synth_dev = {
62 .synth_type = SYNTH_TYPE_MIDI,
68 static DEFINE_SPINLOCK(register_lock);
73 static struct seq_oss_synth *get_synthdev(struct seq_oss_devinfo *dp, int dev);
74 static void reset_channels(struct seq_oss_synthinfo *info);
77 * global initialization
80 snd_seq_oss_synth_init(void)
82 snd_use_lock_init(&midi_synth_dev.use_lock);
86 * registration of the synth device
89 snd_seq_oss_synth_probe(struct device *_dev)
91 struct snd_seq_device *dev = to_seq_dev(_dev);
93 struct seq_oss_synth *rec;
94 struct snd_seq_oss_reg *reg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
97 rec = kzalloc(sizeof(*rec), GFP_KERNEL);
100 rec->seq_device = -1;
101 rec->synth_type = reg->type;
102 rec->synth_subtype = reg->subtype;
103 rec->nr_voices = reg->nvoices;
104 rec->oper = reg->oper;
105 rec->private_data = reg->private_data;
107 snd_use_lock_init(&rec->use_lock);
109 /* copy and truncate the name of synth device */
110 strlcpy(rec->name, dev->name, sizeof(rec->name));
113 spin_lock_irqsave(®ister_lock, flags);
114 for (i = 0; i < max_synth_devs; i++) {
115 if (synth_devs[i] == NULL)
118 if (i >= max_synth_devs) {
119 if (max_synth_devs >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS) {
120 spin_unlock_irqrestore(®ister_lock, flags);
121 pr_err("ALSA: seq_oss: no more synth slot\n");
129 spin_unlock_irqrestore(®ister_lock, flags);
130 dev->driver_data = rec;
131 #ifdef SNDRV_OSS_INFO_DEV_SYNTH
133 snd_oss_info_register(SNDRV_OSS_INFO_DEV_SYNTH, i, rec->name);
140 snd_seq_oss_synth_remove(struct device *_dev)
142 struct snd_seq_device *dev = to_seq_dev(_dev);
144 struct seq_oss_synth *rec = dev->driver_data;
147 spin_lock_irqsave(®ister_lock, flags);
148 for (index = 0; index < max_synth_devs; index++) {
149 if (synth_devs[index] == rec)
152 if (index >= max_synth_devs) {
153 spin_unlock_irqrestore(®ister_lock, flags);
154 pr_err("ALSA: seq_oss: can't unregister synth\n");
157 synth_devs[index] = NULL;
158 if (index == max_synth_devs - 1) {
159 for (index--; index >= 0; index--) {
160 if (synth_devs[index])
163 max_synth_devs = index + 1;
165 spin_unlock_irqrestore(®ister_lock, flags);
166 #ifdef SNDRV_OSS_INFO_DEV_SYNTH
167 if (rec->seq_device < SNDRV_CARDS)
168 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_SYNTH, rec->seq_device);
171 snd_use_lock_sync(&rec->use_lock);
180 static struct seq_oss_synth *
183 struct seq_oss_synth *rec;
186 spin_lock_irqsave(®ister_lock, flags);
187 rec = synth_devs[dev];
189 snd_use_lock_use(&rec->use_lock);
190 spin_unlock_irqrestore(®ister_lock, flags);
196 * set up synth tables
200 snd_seq_oss_synth_setup(struct seq_oss_devinfo *dp)
203 struct seq_oss_synth *rec;
204 struct seq_oss_synthinfo *info;
206 dp->max_synthdev = max_synth_devs;
207 dp->synth_opened = 0;
208 memset(dp->synths, 0, sizeof(dp->synths));
209 for (i = 0; i < dp->max_synthdev; i++) {
213 if (rec->oper.open == NULL || rec->oper.close == NULL) {
214 snd_use_lock_free(&rec->use_lock);
217 info = &dp->synths[i];
218 info->arg.app_index = dp->port;
219 info->arg.file_mode = dp->file_mode;
220 info->arg.seq_mode = dp->seq_mode;
221 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH)
222 info->arg.event_passing = SNDRV_SEQ_OSS_PROCESS_EVENTS;
224 info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS;
226 if (!try_module_get(rec->oper.owner)) {
227 snd_use_lock_free(&rec->use_lock);
230 if (rec->oper.open(&info->arg, rec->private_data) < 0) {
231 module_put(rec->oper.owner);
232 snd_use_lock_free(&rec->use_lock);
235 info->nr_voices = rec->nr_voices;
236 if (info->nr_voices > 0) {
237 info->ch = kcalloc(info->nr_voices, sizeof(struct seq_oss_chinfo), GFP_KERNEL);
239 rec->oper.close(&info->arg);
240 module_put(rec->oper.owner);
241 snd_use_lock_free(&rec->use_lock);
244 reset_channels(info);
249 snd_use_lock_free(&rec->use_lock);
255 * set up synth tables for MIDI emulation - /dev/music mode only
259 snd_seq_oss_synth_setup_midi(struct seq_oss_devinfo *dp)
263 if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)
266 for (i = 0; i < dp->max_mididev; i++) {
267 struct seq_oss_synthinfo *info;
268 info = &dp->synths[dp->max_synthdev];
269 if (snd_seq_oss_midi_open(dp, i, dp->file_mode) < 0)
271 info->arg.app_index = dp->port;
272 info->arg.file_mode = dp->file_mode;
273 info->arg.seq_mode = dp->seq_mode;
274 info->arg.private_data = info;
276 info->midi_mapped = i;
277 info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS;
278 snd_seq_oss_midi_get_addr(dp, i, &info->arg.addr);
280 midi_synth_dev.opened++;
282 if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)
289 * clean up synth tables
293 snd_seq_oss_synth_cleanup(struct seq_oss_devinfo *dp)
296 struct seq_oss_synth *rec;
297 struct seq_oss_synthinfo *info;
299 if (snd_BUG_ON(dp->max_synthdev > SNDRV_SEQ_OSS_MAX_SYNTH_DEVS))
301 for (i = 0; i < dp->max_synthdev; i++) {
302 info = &dp->synths[i];
306 if (midi_synth_dev.opened > 0) {
307 snd_seq_oss_midi_close(dp, info->midi_mapped);
308 midi_synth_dev.opened--;
314 if (rec->opened > 0) {
315 rec->oper.close(&info->arg);
316 module_put(rec->oper.owner);
319 snd_use_lock_free(&rec->use_lock);
326 dp->synth_opened = 0;
327 dp->max_synthdev = 0;
330 static struct seq_oss_synthinfo *
331 get_synthinfo_nospec(struct seq_oss_devinfo *dp, int dev)
333 if (dev < 0 || dev >= dp->max_synthdev)
335 dev = array_index_nospec(dev, SNDRV_SEQ_OSS_MAX_SYNTH_DEVS);
336 return &dp->synths[dev];
340 * return synth device information pointer
342 static struct seq_oss_synth *
343 get_synthdev(struct seq_oss_devinfo *dp, int dev)
345 struct seq_oss_synth *rec;
346 struct seq_oss_synthinfo *info = get_synthinfo_nospec(dp, dev);
353 rec = &midi_synth_dev;
354 snd_use_lock_use(&rec->use_lock);
361 snd_use_lock_free(&rec->use_lock);
369 * reset note and velocity on each channel.
372 reset_channels(struct seq_oss_synthinfo *info)
375 if (info->ch == NULL || ! info->nr_voices)
377 for (i = 0; i < info->nr_voices; i++) {
378 info->ch[i].note = -1;
385 * reset synth device:
386 * call reset callback. if no callback is defined, send a heartbeat
387 * event to the corresponding port.
390 snd_seq_oss_synth_reset(struct seq_oss_devinfo *dp, int dev)
392 struct seq_oss_synth *rec;
393 struct seq_oss_synthinfo *info;
395 info = get_synthinfo_nospec(dp, dev);
396 if (!info || !info->opened)
399 info->sysex->len = 0; /* reset sysex */
400 reset_channels(info);
402 if (midi_synth_dev.opened <= 0)
404 snd_seq_oss_midi_reset(dp, info->midi_mapped);
405 /* reopen the device */
406 snd_seq_oss_midi_close(dp, dev);
407 if (snd_seq_oss_midi_open(dp, info->midi_mapped,
408 dp->file_mode) < 0) {
409 midi_synth_dev.opened--;
422 if (rec->oper.reset) {
423 rec->oper.reset(&info->arg);
425 struct snd_seq_event ev;
426 memset(&ev, 0, sizeof(ev));
427 snd_seq_oss_fill_addr(dp, &ev, info->arg.addr.client,
428 info->arg.addr.port);
429 ev.type = SNDRV_SEQ_EVENT_RESET;
430 snd_seq_oss_dispatch(dp, &ev, 0, 0);
432 snd_use_lock_free(&rec->use_lock);
437 * load a patch record:
438 * call load_patch callback function
441 snd_seq_oss_synth_load_patch(struct seq_oss_devinfo *dp, int dev, int fmt,
442 const char __user *buf, int p, int c)
444 struct seq_oss_synth *rec;
445 struct seq_oss_synthinfo *info;
448 info = get_synthinfo_nospec(dp, dev);
454 if ((rec = get_synthdev(dp, dev)) == NULL)
457 if (rec->oper.load_patch == NULL)
460 rc = rec->oper.load_patch(&info->arg, fmt, buf, p, c);
461 snd_use_lock_free(&rec->use_lock);
466 * check if the device is valid synth device and return the synth info
468 struct seq_oss_synthinfo *
469 snd_seq_oss_synth_info(struct seq_oss_devinfo *dp, int dev)
471 struct seq_oss_synth *rec;
473 rec = get_synthdev(dp, dev);
475 snd_use_lock_free(&rec->use_lock);
476 return get_synthinfo_nospec(dp, dev);
483 * receive OSS 6 byte sysex packet:
484 * the full sysex message will be sent if it reaches to the end of data
488 snd_seq_oss_synth_sysex(struct seq_oss_devinfo *dp, int dev, unsigned char *buf, struct snd_seq_event *ev)
492 struct seq_oss_synth_sysex *sysex;
493 struct seq_oss_synthinfo *info;
495 info = snd_seq_oss_synth_info(dp, dev);
501 sysex = kzalloc(sizeof(*sysex), GFP_KERNEL);
508 dest = sysex->buf + sysex->len;
509 /* copy 6 byte packet to the buffer */
510 for (i = 0; i < 6; i++) {
511 if (buf[i] == 0xff) {
517 if (sysex->len >= MAX_SYSEX_BUFLEN) {
524 if (sysex->len && send) {
528 return -EINVAL; /* skip */
530 /* copy the data to event record and send it */
531 ev->flags = SNDRV_SEQ_EVENT_LENGTH_VARIABLE;
532 if (snd_seq_oss_synth_addr(dp, dev, ev))
534 ev->data.ext.len = sysex->len;
535 ev->data.ext.ptr = sysex->buf;
540 return -EINVAL; /* skip */
544 * fill the event source/destination addresses
547 snd_seq_oss_synth_addr(struct seq_oss_devinfo *dp, int dev, struct snd_seq_event *ev)
549 struct seq_oss_synthinfo *info = snd_seq_oss_synth_info(dp, dev);
553 snd_seq_oss_fill_addr(dp, ev, info->arg.addr.client,
554 info->arg.addr.port);
560 * OSS compatible ioctl
563 snd_seq_oss_synth_ioctl(struct seq_oss_devinfo *dp, int dev, unsigned int cmd, unsigned long addr)
565 struct seq_oss_synth *rec;
566 struct seq_oss_synthinfo *info;
569 info = get_synthinfo_nospec(dp, dev);
570 if (!info || info->is_midi)
572 if ((rec = get_synthdev(dp, dev)) == NULL)
574 if (rec->oper.ioctl == NULL)
577 rc = rec->oper.ioctl(&info->arg, cmd, addr);
578 snd_use_lock_free(&rec->use_lock);
584 * send OSS raw events - SEQ_PRIVATE and SEQ_VOLUME
587 snd_seq_oss_synth_raw_event(struct seq_oss_devinfo *dp, int dev, unsigned char *data, struct snd_seq_event *ev)
589 struct seq_oss_synthinfo *info;
591 info = snd_seq_oss_synth_info(dp, dev);
592 if (!info || info->is_midi)
594 ev->type = SNDRV_SEQ_EVENT_OSS;
595 memcpy(ev->data.raw8.d, data, 8);
596 return snd_seq_oss_synth_addr(dp, dev, ev);
601 * create OSS compatible synth_info record
604 snd_seq_oss_synth_make_info(struct seq_oss_devinfo *dp, int dev, struct synth_info *inf)
606 struct seq_oss_synth *rec;
607 struct seq_oss_synthinfo *info = get_synthinfo_nospec(dp, dev);
613 struct midi_info minf;
614 snd_seq_oss_midi_make_info(dp, info->midi_mapped, &minf);
615 inf->synth_type = SYNTH_TYPE_MIDI;
616 inf->synth_subtype = 0;
619 strlcpy(inf->name, minf.name, sizeof(inf->name));
621 if ((rec = get_synthdev(dp, dev)) == NULL)
623 inf->synth_type = rec->synth_type;
624 inf->synth_subtype = rec->synth_subtype;
625 inf->nr_voices = rec->nr_voices;
627 strlcpy(inf->name, rec->name, sizeof(inf->name));
628 snd_use_lock_free(&rec->use_lock);
634 #ifdef CONFIG_SND_PROC_FS
639 snd_seq_oss_synth_info_read(struct snd_info_buffer *buf)
642 struct seq_oss_synth *rec;
644 snd_iprintf(buf, "\nNumber of synth devices: %d\n", max_synth_devs);
645 for (i = 0; i < max_synth_devs; i++) {
646 snd_iprintf(buf, "\nsynth %d: ", i);
649 snd_iprintf(buf, "*empty*\n");
652 snd_iprintf(buf, "[%s]\n", rec->name);
653 snd_iprintf(buf, " type 0x%x : subtype 0x%x : voices %d\n",
654 rec->synth_type, rec->synth_subtype,
656 snd_iprintf(buf, " capabilities : ioctl %s / load_patch %s\n",
657 enabled_str((long)rec->oper.ioctl),
658 enabled_str((long)rec->oper.load_patch));
659 snd_use_lock_free(&rec->use_lock);
662 #endif /* CONFIG_SND_PROC_FS */