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_probe(struct device
*_dev
)
103 struct snd_seq_device
*dev
= to_seq_dev(_dev
);
105 struct seq_oss_synth
*rec
;
106 struct snd_seq_oss_reg
*reg
= SNDRV_SEQ_DEVICE_ARGPTR(dev
);
109 if ((rec
= kzalloc(sizeof(*rec
), GFP_KERNEL
)) == NULL
) {
110 pr_err("ALSA: seq_oss: can't malloc synth info\n");
113 rec
->seq_device
= -1;
114 rec
->synth_type
= reg
->type
;
115 rec
->synth_subtype
= reg
->subtype
;
116 rec
->nr_voices
= reg
->nvoices
;
117 rec
->oper
= reg
->oper
;
118 rec
->private_data
= reg
->private_data
;
120 snd_use_lock_init(&rec
->use_lock
);
122 /* copy and truncate the name of synth device */
123 strlcpy(rec
->name
, dev
->name
, sizeof(rec
->name
));
126 spin_lock_irqsave(®ister_lock
, flags
);
127 for (i
= 0; i
< max_synth_devs
; i
++) {
128 if (synth_devs
[i
] == NULL
)
131 if (i
>= max_synth_devs
) {
132 if (max_synth_devs
>= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS
) {
133 spin_unlock_irqrestore(®ister_lock
, flags
);
134 pr_err("ALSA: seq_oss: no more synth slot\n");
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_remove(struct device
*_dev
)
155 struct snd_seq_device
*dev
= to_seq_dev(_dev
);
157 struct seq_oss_synth
*rec
= dev
->driver_data
;
160 spin_lock_irqsave(®ister_lock
, flags
);
161 for (index
= 0; index
< max_synth_devs
; index
++) {
162 if (synth_devs
[index
] == rec
)
165 if (index
>= max_synth_devs
) {
166 spin_unlock_irqrestore(®ister_lock
, flags
);
167 pr_err("ALSA: seq_oss: can't unregister synth\n");
170 synth_devs
[index
] = NULL
;
171 if (index
== max_synth_devs
- 1) {
172 for (index
--; index
>= 0; index
--) {
173 if (synth_devs
[index
])
176 max_synth_devs
= index
+ 1;
178 spin_unlock_irqrestore(®ister_lock
, flags
);
179 #ifdef SNDRV_OSS_INFO_DEV_SYNTH
180 if (rec
->seq_device
< SNDRV_CARDS
)
181 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_SYNTH
, rec
->seq_device
);
184 snd_use_lock_sync(&rec
->use_lock
);
193 static struct seq_oss_synth
*
196 struct seq_oss_synth
*rec
;
199 spin_lock_irqsave(®ister_lock
, flags
);
200 rec
= synth_devs
[dev
];
202 snd_use_lock_use(&rec
->use_lock
);
203 spin_unlock_irqrestore(®ister_lock
, flags
);
209 * set up synth tables
213 snd_seq_oss_synth_setup(struct seq_oss_devinfo
*dp
)
216 struct seq_oss_synth
*rec
;
217 struct seq_oss_synthinfo
*info
;
219 dp
->max_synthdev
= max_synth_devs
;
220 dp
->synth_opened
= 0;
221 memset(dp
->synths
, 0, sizeof(dp
->synths
));
222 for (i
= 0; i
< dp
->max_synthdev
; i
++) {
226 if (rec
->oper
.open
== NULL
|| rec
->oper
.close
== NULL
) {
227 snd_use_lock_free(&rec
->use_lock
);
230 info
= &dp
->synths
[i
];
231 info
->arg
.app_index
= dp
->port
;
232 info
->arg
.file_mode
= dp
->file_mode
;
233 info
->arg
.seq_mode
= dp
->seq_mode
;
234 if (dp
->seq_mode
== SNDRV_SEQ_OSS_MODE_SYNTH
)
235 info
->arg
.event_passing
= SNDRV_SEQ_OSS_PROCESS_EVENTS
;
237 info
->arg
.event_passing
= SNDRV_SEQ_OSS_PASS_EVENTS
;
239 if (!try_module_get(rec
->oper
.owner
)) {
240 snd_use_lock_free(&rec
->use_lock
);
243 if (rec
->oper
.open(&info
->arg
, rec
->private_data
) < 0) {
244 module_put(rec
->oper
.owner
);
245 snd_use_lock_free(&rec
->use_lock
);
248 info
->nr_voices
= rec
->nr_voices
;
249 if (info
->nr_voices
> 0) {
250 info
->ch
= kcalloc(info
->nr_voices
, sizeof(struct seq_oss_chinfo
), GFP_KERNEL
);
252 pr_err("ALSA: seq_oss: Cannot malloc voices\n");
253 rec
->oper
.close(&info
->arg
);
254 module_put(rec
->oper
.owner
);
255 snd_use_lock_free(&rec
->use_lock
);
258 reset_channels(info
);
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 rec
->oper
.close(&info
->arg
);
330 module_put(rec
->oper
.owner
);
333 snd_use_lock_free(&rec
->use_lock
);
340 dp
->synth_opened
= 0;
341 dp
->max_synthdev
= 0;
345 * check if the specified device is MIDI mapped device
348 is_midi_dev(struct seq_oss_devinfo
*dp
, int dev
)
350 if (dev
< 0 || dev
>= dp
->max_synthdev
)
352 if (dp
->synths
[dev
].is_midi
)
358 * return synth device information pointer
360 static struct seq_oss_synth
*
361 get_synthdev(struct seq_oss_devinfo
*dp
, int dev
)
363 struct seq_oss_synth
*rec
;
364 if (dev
< 0 || dev
>= dp
->max_synthdev
)
366 if (! dp
->synths
[dev
].opened
)
368 if (dp
->synths
[dev
].is_midi
)
369 return &midi_synth_dev
;
370 if ((rec
= get_sdev(dev
)) == NULL
)
373 snd_use_lock_free(&rec
->use_lock
);
381 * reset note and velocity on each channel.
384 reset_channels(struct seq_oss_synthinfo
*info
)
387 if (info
->ch
== NULL
|| ! info
->nr_voices
)
389 for (i
= 0; i
< info
->nr_voices
; i
++) {
390 info
->ch
[i
].note
= -1;
397 * reset synth device:
398 * call reset callback. if no callback is defined, send a heartbeat
399 * event to the corresponding port.
402 snd_seq_oss_synth_reset(struct seq_oss_devinfo
*dp
, int dev
)
404 struct seq_oss_synth
*rec
;
405 struct seq_oss_synthinfo
*info
;
407 if (snd_BUG_ON(dev
< 0 || dev
>= dp
->max_synthdev
))
409 info
= &dp
->synths
[dev
];
413 info
->sysex
->len
= 0; /* reset sysex */
414 reset_channels(info
);
416 if (midi_synth_dev
.opened
<= 0)
418 snd_seq_oss_midi_reset(dp
, info
->midi_mapped
);
419 /* reopen the device */
420 snd_seq_oss_midi_close(dp
, dev
);
421 if (snd_seq_oss_midi_open(dp
, info
->midi_mapped
,
422 dp
->file_mode
) < 0) {
423 midi_synth_dev
.opened
--;
436 if (rec
->oper
.reset
) {
437 rec
->oper
.reset(&info
->arg
);
439 struct snd_seq_event ev
;
440 memset(&ev
, 0, sizeof(ev
));
441 snd_seq_oss_fill_addr(dp
, &ev
, info
->arg
.addr
.client
,
442 info
->arg
.addr
.port
);
443 ev
.type
= SNDRV_SEQ_EVENT_RESET
;
444 snd_seq_oss_dispatch(dp
, &ev
, 0, 0);
446 snd_use_lock_free(&rec
->use_lock
);
451 * load a patch record:
452 * call load_patch callback function
455 snd_seq_oss_synth_load_patch(struct seq_oss_devinfo
*dp
, int dev
, int fmt
,
456 const char __user
*buf
, int p
, int c
)
458 struct seq_oss_synth
*rec
;
461 if (dev
< 0 || dev
>= dp
->max_synthdev
)
464 if (is_midi_dev(dp
, dev
))
466 if ((rec
= get_synthdev(dp
, dev
)) == NULL
)
469 if (rec
->oper
.load_patch
== NULL
)
472 rc
= rec
->oper
.load_patch(&dp
->synths
[dev
].arg
, fmt
, buf
, p
, c
);
473 snd_use_lock_free(&rec
->use_lock
);
478 * check if the device is valid synth device
481 snd_seq_oss_synth_is_valid(struct seq_oss_devinfo
*dp
, int dev
)
483 struct seq_oss_synth
*rec
;
484 rec
= get_synthdev(dp
, dev
);
486 snd_use_lock_free(&rec
->use_lock
);
494 * receive OSS 6 byte sysex packet:
495 * the full sysex message will be sent if it reaches to the end of data
499 snd_seq_oss_synth_sysex(struct seq_oss_devinfo
*dp
, int dev
, unsigned char *buf
, struct snd_seq_event
*ev
)
503 struct seq_oss_synth_sysex
*sysex
;
505 if (! snd_seq_oss_synth_is_valid(dp
, dev
))
508 sysex
= dp
->synths
[dev
].sysex
;
510 sysex
= kzalloc(sizeof(*sysex
), GFP_KERNEL
);
513 dp
->synths
[dev
].sysex
= sysex
;
517 dest
= sysex
->buf
+ sysex
->len
;
518 /* copy 6 byte packet to the buffer */
519 for (i
= 0; i
< 6; i
++) {
520 if (buf
[i
] == 0xff) {
526 if (sysex
->len
>= MAX_SYSEX_BUFLEN
) {
533 if (sysex
->len
&& send
) {
537 return -EINVAL
; /* skip */
539 /* copy the data to event record and send it */
540 ev
->flags
= SNDRV_SEQ_EVENT_LENGTH_VARIABLE
;
541 if (snd_seq_oss_synth_addr(dp
, dev
, ev
))
543 ev
->data
.ext
.len
= sysex
->len
;
544 ev
->data
.ext
.ptr
= sysex
->buf
;
549 return -EINVAL
; /* skip */
553 * fill the event source/destination addresses
556 snd_seq_oss_synth_addr(struct seq_oss_devinfo
*dp
, int dev
, struct snd_seq_event
*ev
)
558 if (! snd_seq_oss_synth_is_valid(dp
, dev
))
560 snd_seq_oss_fill_addr(dp
, ev
, dp
->synths
[dev
].arg
.addr
.client
,
561 dp
->synths
[dev
].arg
.addr
.port
);
567 * OSS compatible ioctl
570 snd_seq_oss_synth_ioctl(struct seq_oss_devinfo
*dp
, int dev
, unsigned int cmd
, unsigned long addr
)
572 struct seq_oss_synth
*rec
;
575 if (is_midi_dev(dp
, dev
))
577 if ((rec
= get_synthdev(dp
, dev
)) == NULL
)
579 if (rec
->oper
.ioctl
== NULL
)
582 rc
= rec
->oper
.ioctl(&dp
->synths
[dev
].arg
, cmd
, addr
);
583 snd_use_lock_free(&rec
->use_lock
);
589 * send OSS raw events - SEQ_PRIVATE and SEQ_VOLUME
592 snd_seq_oss_synth_raw_event(struct seq_oss_devinfo
*dp
, int dev
, unsigned char *data
, struct snd_seq_event
*ev
)
594 if (! snd_seq_oss_synth_is_valid(dp
, dev
) || is_midi_dev(dp
, dev
))
596 ev
->type
= SNDRV_SEQ_EVENT_OSS
;
597 memcpy(ev
->data
.raw8
.d
, data
, 8);
598 return snd_seq_oss_synth_addr(dp
, dev
, ev
);
603 * create OSS compatible synth_info record
606 snd_seq_oss_synth_make_info(struct seq_oss_devinfo
*dp
, int dev
, struct synth_info
*inf
)
608 struct seq_oss_synth
*rec
;
610 if (dev
< 0 || dev
>= dp
->max_synthdev
)
613 if (dp
->synths
[dev
].is_midi
) {
614 struct midi_info minf
;
615 snd_seq_oss_midi_make_info(dp
, dp
->synths
[dev
].midi_mapped
, &minf
);
616 inf
->synth_type
= SYNTH_TYPE_MIDI
;
617 inf
->synth_subtype
= 0;
620 strlcpy(inf
->name
, minf
.name
, sizeof(inf
->name
));
622 if ((rec
= get_synthdev(dp
, dev
)) == NULL
)
624 inf
->synth_type
= rec
->synth_type
;
625 inf
->synth_subtype
= rec
->synth_subtype
;
626 inf
->nr_voices
= rec
->nr_voices
;
628 strlcpy(inf
->name
, rec
->name
, sizeof(inf
->name
));
629 snd_use_lock_free(&rec
->use_lock
);
635 #ifdef CONFIG_PROC_FS
640 snd_seq_oss_synth_info_read(struct snd_info_buffer
*buf
)
643 struct seq_oss_synth
*rec
;
645 snd_iprintf(buf
, "\nNumber of synth devices: %d\n", max_synth_devs
);
646 for (i
= 0; i
< max_synth_devs
; i
++) {
647 snd_iprintf(buf
, "\nsynth %d: ", i
);
650 snd_iprintf(buf
, "*empty*\n");
653 snd_iprintf(buf
, "[%s]\n", rec
->name
);
654 snd_iprintf(buf
, " type 0x%x : subtype 0x%x : voices %d\n",
655 rec
->synth_type
, rec
->synth_subtype
,
657 snd_iprintf(buf
, " capabilities : ioctl %s / load_patch %s\n",
658 enabled_str((long)rec
->oper
.ioctl
),
659 enabled_str((long)rec
->oper
.load_patch
));
660 snd_use_lock_free(&rec
->use_lock
);
663 #endif /* CONFIG_PROC_FS */