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>
29 #include <linux/nospec.h>
34 #define SNDRV_SEQ_OSS_MAX_SYNTH_NAME 30
35 #define MAX_SYSEX_BUFLEN 128
39 * definition of synth info records
43 struct seq_oss_synth_sysex
{
46 unsigned char buf
[MAX_SYSEX_BUFLEN
];
50 struct seq_oss_synth
{
58 char name
[SNDRV_SEQ_OSS_MAX_SYNTH_NAME
];
59 struct snd_seq_oss_callback oper
;
64 snd_use_lock_t use_lock
;
71 static int max_synth_devs
;
72 static struct seq_oss_synth
*synth_devs
[SNDRV_SEQ_OSS_MAX_SYNTH_DEVS
];
73 static struct seq_oss_synth midi_synth_dev
= {
75 .synth_type
= SYNTH_TYPE_MIDI
,
81 static DEFINE_SPINLOCK(register_lock
);
86 static struct seq_oss_synth
*get_synthdev(struct seq_oss_devinfo
*dp
, int dev
);
87 static void reset_channels(struct seq_oss_synthinfo
*info
);
90 * global initialization
93 snd_seq_oss_synth_init(void)
95 snd_use_lock_init(&midi_synth_dev
.use_lock
);
99 * registration of the synth device
102 snd_seq_oss_synth_probe(struct device
*_dev
)
104 struct snd_seq_device
*dev
= to_seq_dev(_dev
);
106 struct seq_oss_synth
*rec
;
107 struct snd_seq_oss_reg
*reg
= SNDRV_SEQ_DEVICE_ARGPTR(dev
);
110 rec
= kzalloc(sizeof(*rec
), GFP_KERNEL
);
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 rec
->oper
.close(&info
->arg
);
253 module_put(rec
->oper
.owner
);
254 snd_use_lock_free(&rec
->use_lock
);
257 reset_channels(info
);
262 snd_use_lock_free(&rec
->use_lock
);
268 * set up synth tables for MIDI emulation - /dev/music mode only
272 snd_seq_oss_synth_setup_midi(struct seq_oss_devinfo
*dp
)
276 if (dp
->max_synthdev
>= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS
)
279 for (i
= 0; i
< dp
->max_mididev
; i
++) {
280 struct seq_oss_synthinfo
*info
;
281 info
= &dp
->synths
[dp
->max_synthdev
];
282 if (snd_seq_oss_midi_open(dp
, i
, dp
->file_mode
) < 0)
284 info
->arg
.app_index
= dp
->port
;
285 info
->arg
.file_mode
= dp
->file_mode
;
286 info
->arg
.seq_mode
= dp
->seq_mode
;
287 info
->arg
.private_data
= info
;
289 info
->midi_mapped
= i
;
290 info
->arg
.event_passing
= SNDRV_SEQ_OSS_PASS_EVENTS
;
291 snd_seq_oss_midi_get_addr(dp
, i
, &info
->arg
.addr
);
293 midi_synth_dev
.opened
++;
295 if (dp
->max_synthdev
>= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS
)
302 * clean up synth tables
306 snd_seq_oss_synth_cleanup(struct seq_oss_devinfo
*dp
)
309 struct seq_oss_synth
*rec
;
310 struct seq_oss_synthinfo
*info
;
312 if (snd_BUG_ON(dp
->max_synthdev
> SNDRV_SEQ_OSS_MAX_SYNTH_DEVS
))
314 for (i
= 0; i
< dp
->max_synthdev
; i
++) {
315 info
= &dp
->synths
[i
];
319 if (midi_synth_dev
.opened
> 0) {
320 snd_seq_oss_midi_close(dp
, info
->midi_mapped
);
321 midi_synth_dev
.opened
--;
327 if (rec
->opened
> 0) {
328 rec
->oper
.close(&info
->arg
);
329 module_put(rec
->oper
.owner
);
332 snd_use_lock_free(&rec
->use_lock
);
339 dp
->synth_opened
= 0;
340 dp
->max_synthdev
= 0;
343 static struct seq_oss_synthinfo
*
344 get_synthinfo_nospec(struct seq_oss_devinfo
*dp
, int dev
)
346 if (dev
< 0 || dev
>= dp
->max_synthdev
)
348 dev
= array_index_nospec(dev
, SNDRV_SEQ_OSS_MAX_SYNTH_DEVS
);
349 return &dp
->synths
[dev
];
353 * return synth device information pointer
355 static struct seq_oss_synth
*
356 get_synthdev(struct seq_oss_devinfo
*dp
, int dev
)
358 struct seq_oss_synth
*rec
;
359 struct seq_oss_synthinfo
*info
= get_synthinfo_nospec(dp
, dev
);
366 rec
= &midi_synth_dev
;
367 snd_use_lock_use(&rec
->use_lock
);
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 info
= get_synthinfo_nospec(dp
, dev
);
409 if (!info
|| !info
->opened
)
412 info
->sysex
->len
= 0; /* reset sysex */
413 reset_channels(info
);
415 if (midi_synth_dev
.opened
<= 0)
417 snd_seq_oss_midi_reset(dp
, info
->midi_mapped
);
418 /* reopen the device */
419 snd_seq_oss_midi_close(dp
, dev
);
420 if (snd_seq_oss_midi_open(dp
, info
->midi_mapped
,
421 dp
->file_mode
) < 0) {
422 midi_synth_dev
.opened
--;
435 if (rec
->oper
.reset
) {
436 rec
->oper
.reset(&info
->arg
);
438 struct snd_seq_event ev
;
439 memset(&ev
, 0, sizeof(ev
));
440 snd_seq_oss_fill_addr(dp
, &ev
, info
->arg
.addr
.client
,
441 info
->arg
.addr
.port
);
442 ev
.type
= SNDRV_SEQ_EVENT_RESET
;
443 snd_seq_oss_dispatch(dp
, &ev
, 0, 0);
445 snd_use_lock_free(&rec
->use_lock
);
450 * load a patch record:
451 * call load_patch callback function
454 snd_seq_oss_synth_load_patch(struct seq_oss_devinfo
*dp
, int dev
, int fmt
,
455 const char __user
*buf
, int p
, int c
)
457 struct seq_oss_synth
*rec
;
458 struct seq_oss_synthinfo
*info
;
461 info
= get_synthinfo_nospec(dp
, dev
);
467 if ((rec
= get_synthdev(dp
, dev
)) == NULL
)
470 if (rec
->oper
.load_patch
== NULL
)
473 rc
= rec
->oper
.load_patch(&info
->arg
, fmt
, buf
, p
, c
);
474 snd_use_lock_free(&rec
->use_lock
);
479 * check if the device is valid synth device and return the synth info
481 struct seq_oss_synthinfo
*
482 snd_seq_oss_synth_info(struct seq_oss_devinfo
*dp
, int dev
)
484 struct seq_oss_synth
*rec
;
486 rec
= get_synthdev(dp
, dev
);
488 snd_use_lock_free(&rec
->use_lock
);
489 return get_synthinfo_nospec(dp
, dev
);
496 * receive OSS 6 byte sysex packet:
497 * the full sysex message will be sent if it reaches to the end of data
501 snd_seq_oss_synth_sysex(struct seq_oss_devinfo
*dp
, int dev
, unsigned char *buf
, struct snd_seq_event
*ev
)
505 struct seq_oss_synth_sysex
*sysex
;
506 struct seq_oss_synthinfo
*info
;
508 info
= snd_seq_oss_synth_info(dp
, dev
);
514 sysex
= kzalloc(sizeof(*sysex
), GFP_KERNEL
);
521 dest
= sysex
->buf
+ sysex
->len
;
522 /* copy 6 byte packet to the buffer */
523 for (i
= 0; i
< 6; i
++) {
524 if (buf
[i
] == 0xff) {
530 if (sysex
->len
>= MAX_SYSEX_BUFLEN
) {
537 if (sysex
->len
&& send
) {
541 return -EINVAL
; /* skip */
543 /* copy the data to event record and send it */
544 ev
->flags
= SNDRV_SEQ_EVENT_LENGTH_VARIABLE
;
545 if (snd_seq_oss_synth_addr(dp
, dev
, ev
))
547 ev
->data
.ext
.len
= sysex
->len
;
548 ev
->data
.ext
.ptr
= sysex
->buf
;
553 return -EINVAL
; /* skip */
557 * fill the event source/destination addresses
560 snd_seq_oss_synth_addr(struct seq_oss_devinfo
*dp
, int dev
, struct snd_seq_event
*ev
)
562 struct seq_oss_synthinfo
*info
= snd_seq_oss_synth_info(dp
, dev
);
566 snd_seq_oss_fill_addr(dp
, ev
, info
->arg
.addr
.client
,
567 info
->arg
.addr
.port
);
573 * OSS compatible ioctl
576 snd_seq_oss_synth_ioctl(struct seq_oss_devinfo
*dp
, int dev
, unsigned int cmd
, unsigned long addr
)
578 struct seq_oss_synth
*rec
;
579 struct seq_oss_synthinfo
*info
;
582 info
= get_synthinfo_nospec(dp
, dev
);
583 if (!info
|| info
->is_midi
)
585 if ((rec
= get_synthdev(dp
, dev
)) == NULL
)
587 if (rec
->oper
.ioctl
== NULL
)
590 rc
= rec
->oper
.ioctl(&info
->arg
, cmd
, addr
);
591 snd_use_lock_free(&rec
->use_lock
);
597 * send OSS raw events - SEQ_PRIVATE and SEQ_VOLUME
600 snd_seq_oss_synth_raw_event(struct seq_oss_devinfo
*dp
, int dev
, unsigned char *data
, struct snd_seq_event
*ev
)
602 struct seq_oss_synthinfo
*info
;
604 info
= snd_seq_oss_synth_info(dp
, dev
);
605 if (!info
|| info
->is_midi
)
607 ev
->type
= SNDRV_SEQ_EVENT_OSS
;
608 memcpy(ev
->data
.raw8
.d
, data
, 8);
609 return snd_seq_oss_synth_addr(dp
, dev
, ev
);
614 * create OSS compatible synth_info record
617 snd_seq_oss_synth_make_info(struct seq_oss_devinfo
*dp
, int dev
, struct synth_info
*inf
)
619 struct seq_oss_synth
*rec
;
621 if (dev
< 0 || dev
>= dp
->max_synthdev
)
624 if (dp
->synths
[dev
].is_midi
) {
625 struct midi_info minf
;
626 snd_seq_oss_midi_make_info(dp
, dp
->synths
[dev
].midi_mapped
, &minf
);
627 inf
->synth_type
= SYNTH_TYPE_MIDI
;
628 inf
->synth_subtype
= 0;
631 strlcpy(inf
->name
, minf
.name
, sizeof(inf
->name
));
633 if ((rec
= get_synthdev(dp
, dev
)) == NULL
)
635 inf
->synth_type
= rec
->synth_type
;
636 inf
->synth_subtype
= rec
->synth_subtype
;
637 inf
->nr_voices
= rec
->nr_voices
;
639 strlcpy(inf
->name
, rec
->name
, sizeof(inf
->name
));
640 snd_use_lock_free(&rec
->use_lock
);
646 #ifdef CONFIG_SND_PROC_FS
651 snd_seq_oss_synth_info_read(struct snd_info_buffer
*buf
)
654 struct seq_oss_synth
*rec
;
656 snd_iprintf(buf
, "\nNumber of synth devices: %d\n", max_synth_devs
);
657 for (i
= 0; i
< max_synth_devs
; i
++) {
658 snd_iprintf(buf
, "\nsynth %d: ", i
);
661 snd_iprintf(buf
, "*empty*\n");
664 snd_iprintf(buf
, "[%s]\n", rec
->name
);
665 snd_iprintf(buf
, " type 0x%x : subtype 0x%x : voices %d\n",
666 rec
->synth_type
, rec
->synth_subtype
,
668 snd_iprintf(buf
, " capabilities : ioctl %s / load_patch %s\n",
669 enabled_str((long)rec
->oper
.ioctl
),
670 enabled_str((long)rec
->oper
.load_patch
));
671 snd_use_lock_free(&rec
->use_lock
);
674 #endif /* CONFIG_SND_PROC_FS */