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>
27 #include <linux/module.h>
28 #include <linux/slab.h>
33 #define SNDRV_SEQ_OSS_MAX_SYNTH_NAME 30
34 #define MAX_SYSEX_BUFLEN 128
38 * definition of synth info records
42 struct seq_oss_synth_sysex
{
45 unsigned char buf
[MAX_SYSEX_BUFLEN
];
49 struct seq_oss_synth
{
57 char name
[SNDRV_SEQ_OSS_MAX_SYNTH_NAME
];
58 struct snd_seq_oss_callback oper
;
63 snd_use_lock_t use_lock
;
70 static int max_synth_devs
;
71 static struct seq_oss_synth
*synth_devs
[SNDRV_SEQ_OSS_MAX_SYNTH_DEVS
];
72 static struct seq_oss_synth midi_synth_dev
= {
74 SYNTH_TYPE_MIDI
, /* synth_type */
75 0, /* synth_subtype */
80 static DEFINE_SPINLOCK(register_lock
);
85 static struct seq_oss_synth
*get_synthdev(struct seq_oss_devinfo
*dp
, int dev
);
86 static void reset_channels(struct seq_oss_synthinfo
*info
);
89 * global initialization
92 snd_seq_oss_synth_init(void)
94 snd_use_lock_init(&midi_synth_dev
.use_lock
);
98 * registration of the synth device
101 snd_seq_oss_synth_register(struct snd_seq_device
*dev
)
104 struct seq_oss_synth
*rec
;
105 struct snd_seq_oss_reg
*reg
= SNDRV_SEQ_DEVICE_ARGPTR(dev
);
108 if ((rec
= kzalloc(sizeof(*rec
), GFP_KERNEL
)) == NULL
) {
109 snd_printk(KERN_ERR
"can't malloc synth info\n");
112 rec
->seq_device
= -1;
113 rec
->synth_type
= reg
->type
;
114 rec
->synth_subtype
= reg
->subtype
;
115 rec
->nr_voices
= reg
->nvoices
;
116 rec
->oper
= reg
->oper
;
117 rec
->private_data
= reg
->private_data
;
119 snd_use_lock_init(&rec
->use_lock
);
121 /* copy and truncate the name of synth device */
122 strlcpy(rec
->name
, dev
->name
, sizeof(rec
->name
));
125 spin_lock_irqsave(®ister_lock
, flags
);
126 for (i
= 0; i
< max_synth_devs
; i
++) {
127 if (synth_devs
[i
] == NULL
)
130 if (i
>= max_synth_devs
) {
131 if (max_synth_devs
>= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS
) {
132 spin_unlock_irqrestore(®ister_lock
, flags
);
133 snd_printk(KERN_ERR
"no more synth slot\n");
141 debug_printk(("synth %s registered %d\n", rec
->name
, i
));
142 spin_unlock_irqrestore(®ister_lock
, flags
);
143 dev
->driver_data
= rec
;
144 #ifdef SNDRV_OSS_INFO_DEV_SYNTH
146 snd_oss_info_register(SNDRV_OSS_INFO_DEV_SYNTH
, i
, rec
->name
);
153 snd_seq_oss_synth_unregister(struct snd_seq_device
*dev
)
156 struct seq_oss_synth
*rec
= dev
->driver_data
;
159 spin_lock_irqsave(®ister_lock
, flags
);
160 for (index
= 0; index
< max_synth_devs
; index
++) {
161 if (synth_devs
[index
] == rec
)
164 if (index
>= max_synth_devs
) {
165 spin_unlock_irqrestore(®ister_lock
, flags
);
166 snd_printk(KERN_ERR
"can't unregister synth\n");
169 synth_devs
[index
] = NULL
;
170 if (index
== max_synth_devs
- 1) {
171 for (index
--; index
>= 0; index
--) {
172 if (synth_devs
[index
])
175 max_synth_devs
= index
+ 1;
177 spin_unlock_irqrestore(®ister_lock
, flags
);
178 #ifdef SNDRV_OSS_INFO_DEV_SYNTH
179 if (rec
->seq_device
< SNDRV_CARDS
)
180 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_SYNTH
, rec
->seq_device
);
183 snd_use_lock_sync(&rec
->use_lock
);
192 static struct seq_oss_synth
*
195 struct seq_oss_synth
*rec
;
198 spin_lock_irqsave(®ister_lock
, flags
);
199 rec
= synth_devs
[dev
];
201 snd_use_lock_use(&rec
->use_lock
);
202 spin_unlock_irqrestore(®ister_lock
, flags
);
208 * set up synth tables
212 snd_seq_oss_synth_setup(struct seq_oss_devinfo
*dp
)
215 struct seq_oss_synth
*rec
;
216 struct seq_oss_synthinfo
*info
;
218 dp
->max_synthdev
= max_synth_devs
;
219 dp
->synth_opened
= 0;
220 memset(dp
->synths
, 0, sizeof(dp
->synths
));
221 for (i
= 0; i
< dp
->max_synthdev
; i
++) {
225 if (rec
->oper
.open
== NULL
|| rec
->oper
.close
== NULL
) {
226 snd_use_lock_free(&rec
->use_lock
);
229 info
= &dp
->synths
[i
];
230 info
->arg
.app_index
= dp
->port
;
231 info
->arg
.file_mode
= dp
->file_mode
;
232 info
->arg
.seq_mode
= dp
->seq_mode
;
233 if (dp
->seq_mode
== SNDRV_SEQ_OSS_MODE_SYNTH
)
234 info
->arg
.event_passing
= SNDRV_SEQ_OSS_PROCESS_EVENTS
;
236 info
->arg
.event_passing
= SNDRV_SEQ_OSS_PASS_EVENTS
;
238 if (!try_module_get(rec
->oper
.owner
)) {
239 snd_use_lock_free(&rec
->use_lock
);
242 if (rec
->oper
.open(&info
->arg
, rec
->private_data
) < 0) {
243 module_put(rec
->oper
.owner
);
244 snd_use_lock_free(&rec
->use_lock
);
247 info
->nr_voices
= rec
->nr_voices
;
248 if (info
->nr_voices
> 0) {
249 info
->ch
= kcalloc(info
->nr_voices
, sizeof(struct seq_oss_chinfo
), GFP_KERNEL
);
251 snd_printk(KERN_ERR
"Cannot malloc\n");
252 rec
->oper
.close(&info
->arg
);
253 module_put(rec
->oper
.owner
);
254 snd_use_lock_free(&rec
->use_lock
);
257 reset_channels(info
);
259 debug_printk(("synth %d assigned\n", i
));
263 snd_use_lock_free(&rec
->use_lock
);
269 * set up synth tables for MIDI emulation - /dev/music mode only
273 snd_seq_oss_synth_setup_midi(struct seq_oss_devinfo
*dp
)
277 if (dp
->max_synthdev
>= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS
)
280 for (i
= 0; i
< dp
->max_mididev
; i
++) {
281 struct seq_oss_synthinfo
*info
;
282 info
= &dp
->synths
[dp
->max_synthdev
];
283 if (snd_seq_oss_midi_open(dp
, i
, dp
->file_mode
) < 0)
285 info
->arg
.app_index
= dp
->port
;
286 info
->arg
.file_mode
= dp
->file_mode
;
287 info
->arg
.seq_mode
= dp
->seq_mode
;
288 info
->arg
.private_data
= info
;
290 info
->midi_mapped
= i
;
291 info
->arg
.event_passing
= SNDRV_SEQ_OSS_PASS_EVENTS
;
292 snd_seq_oss_midi_get_addr(dp
, i
, &info
->arg
.addr
);
294 midi_synth_dev
.opened
++;
296 if (dp
->max_synthdev
>= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS
)
303 * clean up synth tables
307 snd_seq_oss_synth_cleanup(struct seq_oss_devinfo
*dp
)
310 struct seq_oss_synth
*rec
;
311 struct seq_oss_synthinfo
*info
;
313 if (snd_BUG_ON(dp
->max_synthdev
>= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS
))
315 for (i
= 0; i
< dp
->max_synthdev
; i
++) {
316 info
= &dp
->synths
[i
];
320 if (midi_synth_dev
.opened
> 0) {
321 snd_seq_oss_midi_close(dp
, info
->midi_mapped
);
322 midi_synth_dev
.opened
--;
328 if (rec
->opened
> 0) {
329 debug_printk(("synth %d closed\n", i
));
330 rec
->oper
.close(&info
->arg
);
331 module_put(rec
->oper
.owner
);
334 snd_use_lock_free(&rec
->use_lock
);
341 dp
->synth_opened
= 0;
342 dp
->max_synthdev
= 0;
346 * check if the specified device is MIDI mapped device
349 is_midi_dev(struct seq_oss_devinfo
*dp
, int dev
)
351 if (dev
< 0 || dev
>= dp
->max_synthdev
)
353 if (dp
->synths
[dev
].is_midi
)
359 * return synth device information pointer
361 static struct seq_oss_synth
*
362 get_synthdev(struct seq_oss_devinfo
*dp
, int dev
)
364 struct seq_oss_synth
*rec
;
365 if (dev
< 0 || dev
>= dp
->max_synthdev
)
367 if (! dp
->synths
[dev
].opened
)
369 if (dp
->synths
[dev
].is_midi
)
370 return &midi_synth_dev
;
371 if ((rec
= get_sdev(dev
)) == NULL
)
374 snd_use_lock_free(&rec
->use_lock
);
382 * reset note and velocity on each channel.
385 reset_channels(struct seq_oss_synthinfo
*info
)
388 if (info
->ch
== NULL
|| ! info
->nr_voices
)
390 for (i
= 0; i
< info
->nr_voices
; i
++) {
391 info
->ch
[i
].note
= -1;
398 * reset synth device:
399 * call reset callback. if no callback is defined, send a heartbeat
400 * event to the corresponding port.
403 snd_seq_oss_synth_reset(struct seq_oss_devinfo
*dp
, int dev
)
405 struct seq_oss_synth
*rec
;
406 struct seq_oss_synthinfo
*info
;
408 if (snd_BUG_ON(dev
< 0 || dev
>= dp
->max_synthdev
))
410 info
= &dp
->synths
[dev
];
414 info
->sysex
->len
= 0; /* reset sysex */
415 reset_channels(info
);
417 if (midi_synth_dev
.opened
<= 0)
419 snd_seq_oss_midi_reset(dp
, info
->midi_mapped
);
420 /* reopen the device */
421 snd_seq_oss_midi_close(dp
, dev
);
422 if (snd_seq_oss_midi_open(dp
, info
->midi_mapped
,
423 dp
->file_mode
) < 0) {
424 midi_synth_dev
.opened
--;
437 if (rec
->oper
.reset
) {
438 rec
->oper
.reset(&info
->arg
);
440 struct snd_seq_event ev
;
441 memset(&ev
, 0, sizeof(ev
));
442 snd_seq_oss_fill_addr(dp
, &ev
, info
->arg
.addr
.client
,
443 info
->arg
.addr
.port
);
444 ev
.type
= SNDRV_SEQ_EVENT_RESET
;
445 snd_seq_oss_dispatch(dp
, &ev
, 0, 0);
447 snd_use_lock_free(&rec
->use_lock
);
452 * load a patch record:
453 * call load_patch callback function
456 snd_seq_oss_synth_load_patch(struct seq_oss_devinfo
*dp
, int dev
, int fmt
,
457 const char __user
*buf
, int p
, int c
)
459 struct seq_oss_synth
*rec
;
462 if (dev
< 0 || dev
>= dp
->max_synthdev
)
465 if (is_midi_dev(dp
, dev
))
467 if ((rec
= get_synthdev(dp
, dev
)) == NULL
)
470 if (rec
->oper
.load_patch
== NULL
)
473 rc
= rec
->oper
.load_patch(&dp
->synths
[dev
].arg
, fmt
, buf
, p
, c
);
474 snd_use_lock_free(&rec
->use_lock
);
479 * check if the device is valid synth device
482 snd_seq_oss_synth_is_valid(struct seq_oss_devinfo
*dp
, int dev
)
484 struct seq_oss_synth
*rec
;
485 rec
= get_synthdev(dp
, dev
);
487 snd_use_lock_free(&rec
->use_lock
);
495 * receive OSS 6 byte sysex packet:
496 * the full sysex message will be sent if it reaches to the end of data
500 snd_seq_oss_synth_sysex(struct seq_oss_devinfo
*dp
, int dev
, unsigned char *buf
, struct snd_seq_event
*ev
)
504 struct seq_oss_synth_sysex
*sysex
;
506 if (! snd_seq_oss_synth_is_valid(dp
, dev
))
509 sysex
= dp
->synths
[dev
].sysex
;
511 sysex
= kzalloc(sizeof(*sysex
), GFP_KERNEL
);
514 dp
->synths
[dev
].sysex
= sysex
;
518 dest
= sysex
->buf
+ sysex
->len
;
519 /* copy 6 byte packet to the buffer */
520 for (i
= 0; i
< 6; i
++) {
521 if (buf
[i
] == 0xff) {
527 if (sysex
->len
>= MAX_SYSEX_BUFLEN
) {
534 if (sysex
->len
&& send
) {
538 return -EINVAL
; /* skip */
540 /* copy the data to event record and send it */
541 ev
->flags
= SNDRV_SEQ_EVENT_LENGTH_VARIABLE
;
542 if (snd_seq_oss_synth_addr(dp
, dev
, ev
))
544 ev
->data
.ext
.len
= sysex
->len
;
545 ev
->data
.ext
.ptr
= sysex
->buf
;
550 return -EINVAL
; /* skip */
554 * fill the event source/destination addresses
557 snd_seq_oss_synth_addr(struct seq_oss_devinfo
*dp
, int dev
, struct snd_seq_event
*ev
)
559 if (! snd_seq_oss_synth_is_valid(dp
, dev
))
561 snd_seq_oss_fill_addr(dp
, ev
, dp
->synths
[dev
].arg
.addr
.client
,
562 dp
->synths
[dev
].arg
.addr
.port
);
568 * OSS compatible ioctl
571 snd_seq_oss_synth_ioctl(struct seq_oss_devinfo
*dp
, int dev
, unsigned int cmd
, unsigned long addr
)
573 struct seq_oss_synth
*rec
;
576 if (is_midi_dev(dp
, dev
))
578 if ((rec
= get_synthdev(dp
, dev
)) == NULL
)
580 if (rec
->oper
.ioctl
== NULL
)
583 rc
= rec
->oper
.ioctl(&dp
->synths
[dev
].arg
, cmd
, addr
);
584 snd_use_lock_free(&rec
->use_lock
);
590 * send OSS raw events - SEQ_PRIVATE and SEQ_VOLUME
593 snd_seq_oss_synth_raw_event(struct seq_oss_devinfo
*dp
, int dev
, unsigned char *data
, struct snd_seq_event
*ev
)
595 if (! snd_seq_oss_synth_is_valid(dp
, dev
) || is_midi_dev(dp
, dev
))
597 ev
->type
= SNDRV_SEQ_EVENT_OSS
;
598 memcpy(ev
->data
.raw8
.d
, data
, 8);
599 return snd_seq_oss_synth_addr(dp
, dev
, ev
);
604 * create OSS compatible synth_info record
607 snd_seq_oss_synth_make_info(struct seq_oss_devinfo
*dp
, int dev
, struct synth_info
*inf
)
609 struct seq_oss_synth
*rec
;
611 if (dev
< 0 || dev
>= dp
->max_synthdev
)
614 if (dp
->synths
[dev
].is_midi
) {
615 struct midi_info minf
;
616 snd_seq_oss_midi_make_info(dp
, dp
->synths
[dev
].midi_mapped
, &minf
);
617 inf
->synth_type
= SYNTH_TYPE_MIDI
;
618 inf
->synth_subtype
= 0;
621 strlcpy(inf
->name
, minf
.name
, sizeof(inf
->name
));
623 if ((rec
= get_synthdev(dp
, dev
)) == NULL
)
625 inf
->synth_type
= rec
->synth_type
;
626 inf
->synth_subtype
= rec
->synth_subtype
;
627 inf
->nr_voices
= rec
->nr_voices
;
629 strlcpy(inf
->name
, rec
->name
, sizeof(inf
->name
));
630 snd_use_lock_free(&rec
->use_lock
);
636 #ifdef CONFIG_PROC_FS
641 snd_seq_oss_synth_info_read(struct snd_info_buffer
*buf
)
644 struct seq_oss_synth
*rec
;
646 snd_iprintf(buf
, "\nNumber of synth devices: %d\n", max_synth_devs
);
647 for (i
= 0; i
< max_synth_devs
; i
++) {
648 snd_iprintf(buf
, "\nsynth %d: ", i
);
651 snd_iprintf(buf
, "*empty*\n");
654 snd_iprintf(buf
, "[%s]\n", rec
->name
);
655 snd_iprintf(buf
, " type 0x%x : subtype 0x%x : voices %d\n",
656 rec
->synth_type
, rec
->synth_subtype
,
658 snd_iprintf(buf
, " capabilities : ioctl %s / load_patch %s\n",
659 enabled_str((long)rec
->oper
.ioctl
),
660 enabled_str((long)rec
->oper
.load_patch
));
661 snd_use_lock_free(&rec
->use_lock
);
664 #endif /* CONFIG_PROC_FS */