2 * OSS compatible sequencer driver
4 * synth device handlers
6 * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
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.
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.
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
23 #include "seq_oss_synth.h"
24 #include "seq_oss_midi.h"
25 #include "../seq_lock.h"
26 #include <linux/init.h>
31 #define SNDRV_SEQ_OSS_MAX_SYNTH_NAME 30
32 #define MAX_SYSEX_BUFLEN 128
36 * definition of synth info records
40 struct seq_oss_synth_sysex {
43 unsigned char buf[MAX_SYSEX_BUFLEN];
47 struct seq_oss_synth {
55 char name[SNDRV_SEQ_OSS_MAX_SYNTH_NAME];
56 struct snd_seq_oss_callback oper;
61 snd_use_lock_t use_lock;
68 static int max_synth_devs;
69 static struct seq_oss_synth *synth_devs[SNDRV_SEQ_OSS_MAX_SYNTH_DEVS];
70 static struct seq_oss_synth midi_synth_dev = {
72 SYNTH_TYPE_MIDI, /* synth_type */
73 0, /* synth_subtype */
78 static DEFINE_SPINLOCK(register_lock);
83 static struct seq_oss_synth *get_synthdev(struct seq_oss_devinfo *dp, int dev);
84 static void reset_channels(struct seq_oss_synthinfo *info);
87 * global initialization
90 snd_seq_oss_synth_init(void)
92 snd_use_lock_init(&midi_synth_dev.use_lock);
96 * registration of the synth device
99 snd_seq_oss_synth_register(struct snd_seq_device *dev)
102 struct seq_oss_synth *rec;
103 struct snd_seq_oss_reg *reg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
106 if ((rec = kzalloc(sizeof(*rec), GFP_KERNEL)) == NULL) {
107 snd_printk(KERN_ERR "can't malloc synth info\n");
110 rec->seq_device = -1;
111 rec->synth_type = reg->type;
112 rec->synth_subtype = reg->subtype;
113 rec->nr_voices = reg->nvoices;
114 rec->oper = reg->oper;
115 rec->private_data = reg->private_data;
117 snd_use_lock_init(&rec->use_lock);
119 /* copy and truncate the name of synth device */
120 strlcpy(rec->name, dev->name, sizeof(rec->name));
123 spin_lock_irqsave(®ister_lock, flags);
124 for (i = 0; i < max_synth_devs; i++) {
125 if (synth_devs[i] == NULL)
128 if (i >= max_synth_devs) {
129 if (max_synth_devs >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS) {
130 spin_unlock_irqrestore(®ister_lock, flags);
131 snd_printk(KERN_ERR "no more synth slot\n");
139 debug_printk(("synth %s registered %d\n", rec->name, i));
140 spin_unlock_irqrestore(®ister_lock, flags);
141 dev->driver_data = rec;
142 #ifdef SNDRV_OSS_INFO_DEV_SYNTH
144 snd_oss_info_register(SNDRV_OSS_INFO_DEV_SYNTH, i, rec->name);
151 snd_seq_oss_synth_unregister(struct snd_seq_device *dev)
154 struct seq_oss_synth *rec = dev->driver_data;
157 spin_lock_irqsave(®ister_lock, flags);
158 for (index = 0; index < max_synth_devs; index++) {
159 if (synth_devs[index] == rec)
162 if (index >= max_synth_devs) {
163 spin_unlock_irqrestore(®ister_lock, flags);
164 snd_printk(KERN_ERR "can't unregister synth\n");
167 synth_devs[index] = NULL;
168 if (index == max_synth_devs - 1) {
169 for (index--; index >= 0; index--) {
170 if (synth_devs[index])
173 max_synth_devs = index + 1;
175 spin_unlock_irqrestore(®ister_lock, flags);
176 #ifdef SNDRV_OSS_INFO_DEV_SYNTH
177 if (rec->seq_device < SNDRV_CARDS)
178 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_SYNTH, rec->seq_device);
181 snd_use_lock_sync(&rec->use_lock);
190 static struct seq_oss_synth *
193 struct seq_oss_synth *rec;
196 spin_lock_irqsave(®ister_lock, flags);
197 rec = synth_devs[dev];
199 snd_use_lock_use(&rec->use_lock);
200 spin_unlock_irqrestore(®ister_lock, flags);
206 * set up synth tables
210 snd_seq_oss_synth_setup(struct seq_oss_devinfo *dp)
213 struct seq_oss_synth *rec;
214 struct seq_oss_synthinfo *info;
216 dp->max_synthdev = max_synth_devs;
217 dp->synth_opened = 0;
218 memset(dp->synths, 0, sizeof(dp->synths));
219 for (i = 0; i < dp->max_synthdev; i++) {
223 if (rec->oper.open == NULL || rec->oper.close == NULL) {
224 snd_use_lock_free(&rec->use_lock);
227 info = &dp->synths[i];
228 info->arg.app_index = dp->port;
229 info->arg.file_mode = dp->file_mode;
230 info->arg.seq_mode = dp->seq_mode;
231 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH)
232 info->arg.event_passing = SNDRV_SEQ_OSS_PROCESS_EVENTS;
234 info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS;
236 if (!try_module_get(rec->oper.owner)) {
237 snd_use_lock_free(&rec->use_lock);
240 if (rec->oper.open(&info->arg, rec->private_data) < 0) {
241 module_put(rec->oper.owner);
242 snd_use_lock_free(&rec->use_lock);
245 info->nr_voices = rec->nr_voices;
246 if (info->nr_voices > 0) {
247 info->ch = kcalloc(info->nr_voices, sizeof(struct seq_oss_chinfo), GFP_KERNEL);
249 snd_printk(KERN_ERR "Cannot malloc\n");
250 rec->oper.close(&info->arg);
251 module_put(rec->oper.owner);
252 snd_use_lock_free(&rec->use_lock);
255 reset_channels(info);
257 debug_printk(("synth %d assigned\n", i));
261 snd_use_lock_free(&rec->use_lock);
267 * set up synth tables for MIDI emulation - /dev/music mode only
271 snd_seq_oss_synth_setup_midi(struct seq_oss_devinfo *dp)
275 if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)
278 for (i = 0; i < dp->max_mididev; i++) {
279 struct seq_oss_synthinfo *info;
280 info = &dp->synths[dp->max_synthdev];
281 if (snd_seq_oss_midi_open(dp, i, dp->file_mode) < 0)
283 info->arg.app_index = dp->port;
284 info->arg.file_mode = dp->file_mode;
285 info->arg.seq_mode = dp->seq_mode;
286 info->arg.private_data = info;
288 info->midi_mapped = i;
289 info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS;
290 snd_seq_oss_midi_get_addr(dp, i, &info->arg.addr);
292 midi_synth_dev.opened++;
294 if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)
301 * clean up synth tables
305 snd_seq_oss_synth_cleanup(struct seq_oss_devinfo *dp)
308 struct seq_oss_synth *rec;
309 struct seq_oss_synthinfo *info;
311 snd_assert(dp->max_synthdev <= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS, return);
312 for (i = 0; i < dp->max_synthdev; i++) {
313 info = &dp->synths[i];
317 if (midi_synth_dev.opened > 0) {
318 snd_seq_oss_midi_close(dp, info->midi_mapped);
319 midi_synth_dev.opened--;
325 if (rec->opened > 0) {
326 debug_printk(("synth %d closed\n", i));
327 rec->oper.close(&info->arg);
328 module_put(rec->oper.owner);
331 snd_use_lock_free(&rec->use_lock);
338 dp->synth_opened = 0;
339 dp->max_synthdev = 0;
343 * check if the specified device is MIDI mapped device
346 is_midi_dev(struct seq_oss_devinfo *dp, int dev)
348 if (dev < 0 || dev >= dp->max_synthdev)
350 if (dp->synths[dev].is_midi)
356 * return synth device information pointer
358 static struct seq_oss_synth *
359 get_synthdev(struct seq_oss_devinfo *dp, int dev)
361 struct seq_oss_synth *rec;
362 if (dev < 0 || dev >= dp->max_synthdev)
364 if (! dp->synths[dev].opened)
366 if (dp->synths[dev].is_midi)
367 return &midi_synth_dev;
368 if ((rec = get_sdev(dev)) == NULL)
371 snd_use_lock_free(&rec->use_lock);
379 * reset note and velocity on each channel.
382 reset_channels(struct seq_oss_synthinfo *info)
385 if (info->ch == NULL || ! info->nr_voices)
387 for (i = 0; i < info->nr_voices; i++) {
388 info->ch[i].note = -1;
395 * reset synth device:
396 * call reset callback. if no callback is defined, send a heartbeat
397 * event to the corresponding port.
400 snd_seq_oss_synth_reset(struct seq_oss_devinfo *dp, int dev)
402 struct seq_oss_synth *rec;
403 struct seq_oss_synthinfo *info;
405 snd_assert(dev >= 0 && dev < dp->max_synthdev, return);
406 info = &dp->synths[dev];
410 info->sysex->len = 0; /* reset sysex */
411 reset_channels(info);
413 if (midi_synth_dev.opened <= 0)
415 snd_seq_oss_midi_reset(dp, info->midi_mapped);
416 /* reopen the device */
417 snd_seq_oss_midi_close(dp, dev);
418 if (snd_seq_oss_midi_open(dp, info->midi_mapped,
419 dp->file_mode) < 0) {
420 midi_synth_dev.opened--;
433 if (rec->oper.reset) {
434 rec->oper.reset(&info->arg);
436 struct snd_seq_event ev;
437 memset(&ev, 0, sizeof(ev));
438 snd_seq_oss_fill_addr(dp, &ev, info->arg.addr.client,
439 info->arg.addr.port);
440 ev.type = SNDRV_SEQ_EVENT_RESET;
441 snd_seq_oss_dispatch(dp, &ev, 0, 0);
443 snd_use_lock_free(&rec->use_lock);
448 * load a patch record:
449 * call load_patch callback function
452 snd_seq_oss_synth_load_patch(struct seq_oss_devinfo *dp, int dev, int fmt,
453 const char __user *buf, int p, int c)
455 struct seq_oss_synth *rec;
458 if (dev < 0 || dev >= dp->max_synthdev)
461 if (is_midi_dev(dp, dev))
463 if ((rec = get_synthdev(dp, dev)) == NULL)
466 if (rec->oper.load_patch == NULL)
469 rc = rec->oper.load_patch(&dp->synths[dev].arg, fmt, buf, p, c);
470 snd_use_lock_free(&rec->use_lock);
475 * check if the device is valid synth device
478 snd_seq_oss_synth_is_valid(struct seq_oss_devinfo *dp, int dev)
480 struct seq_oss_synth *rec;
481 rec = get_synthdev(dp, dev);
483 snd_use_lock_free(&rec->use_lock);
491 * receive OSS 6 byte sysex packet:
492 * the full sysex message will be sent if it reaches to the end of data
496 snd_seq_oss_synth_sysex(struct seq_oss_devinfo *dp, int dev, unsigned char *buf, struct snd_seq_event *ev)
500 struct seq_oss_synth_sysex *sysex;
502 if (! snd_seq_oss_synth_is_valid(dp, dev))
505 sysex = dp->synths[dev].sysex;
507 sysex = kzalloc(sizeof(*sysex), GFP_KERNEL);
510 dp->synths[dev].sysex = sysex;
514 dest = sysex->buf + sysex->len;
515 /* copy 6 byte packet to the buffer */
516 for (i = 0; i < 6; i++) {
517 if (buf[i] == 0xff) {
523 if (sysex->len >= MAX_SYSEX_BUFLEN) {
530 if (sysex->len && send) {
534 return -EINVAL; /* skip */
536 /* copy the data to event record and send it */
537 ev->flags = SNDRV_SEQ_EVENT_LENGTH_VARIABLE;
538 if (snd_seq_oss_synth_addr(dp, dev, ev))
540 ev->data.ext.len = sysex->len;
541 ev->data.ext.ptr = sysex->buf;
546 return -EINVAL; /* skip */
550 * fill the event source/destination addresses
553 snd_seq_oss_synth_addr(struct seq_oss_devinfo *dp, int dev, struct snd_seq_event *ev)
555 if (! snd_seq_oss_synth_is_valid(dp, dev))
557 snd_seq_oss_fill_addr(dp, ev, dp->synths[dev].arg.addr.client,
558 dp->synths[dev].arg.addr.port);
564 * OSS compatible ioctl
567 snd_seq_oss_synth_ioctl(struct seq_oss_devinfo *dp, int dev, unsigned int cmd, unsigned long addr)
569 struct seq_oss_synth *rec;
572 if (is_midi_dev(dp, dev))
574 if ((rec = get_synthdev(dp, dev)) == NULL)
576 if (rec->oper.ioctl == NULL)
579 rc = rec->oper.ioctl(&dp->synths[dev].arg, cmd, addr);
580 snd_use_lock_free(&rec->use_lock);
586 * send OSS raw events - SEQ_PRIVATE and SEQ_VOLUME
589 snd_seq_oss_synth_raw_event(struct seq_oss_devinfo *dp, int dev, unsigned char *data, struct snd_seq_event *ev)
591 if (! snd_seq_oss_synth_is_valid(dp, dev) || is_midi_dev(dp, dev))
593 ev->type = SNDRV_SEQ_EVENT_OSS;
594 memcpy(ev->data.raw8.d, data, 8);
595 return snd_seq_oss_synth_addr(dp, dev, ev);
600 * create OSS compatible synth_info record
603 snd_seq_oss_synth_make_info(struct seq_oss_devinfo *dp, int dev, struct synth_info *inf)
605 struct seq_oss_synth *rec;
607 if (dev < 0 || dev >= dp->max_synthdev)
610 if (dp->synths[dev].is_midi) {
611 struct midi_info minf;
612 snd_seq_oss_midi_make_info(dp, dp->synths[dev].midi_mapped, &minf);
613 inf->synth_type = SYNTH_TYPE_MIDI;
614 inf->synth_subtype = 0;
617 strlcpy(inf->name, minf.name, sizeof(inf->name));
619 if ((rec = get_synthdev(dp, dev)) == NULL)
621 inf->synth_type = rec->synth_type;
622 inf->synth_subtype = rec->synth_subtype;
623 inf->nr_voices = rec->nr_voices;
625 strlcpy(inf->name, rec->name, sizeof(inf->name));
626 snd_use_lock_free(&rec->use_lock);
632 #ifdef CONFIG_PROC_FS
637 snd_seq_oss_synth_info_read(struct snd_info_buffer *buf)
640 struct seq_oss_synth *rec;
642 snd_iprintf(buf, "\nNumber of synth devices: %d\n", max_synth_devs);
643 for (i = 0; i < max_synth_devs; i++) {
644 snd_iprintf(buf, "\nsynth %d: ", i);
647 snd_iprintf(buf, "*empty*\n");
650 snd_iprintf(buf, "[%s]\n", rec->name);
651 snd_iprintf(buf, " type 0x%x : subtype 0x%x : voices %d\n",
652 rec->synth_type, rec->synth_subtype,
654 snd_iprintf(buf, " capabilities : ioctl %s / load_patch %s\n",
655 enabled_str((long)rec->oper.ioctl),
656 enabled_str((long)rec->oper.load_patch));
657 snd_use_lock_free(&rec->use_lock);
660 #endif /* CONFIG_PROC_FS */