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/slab.h>
32 #define SNDRV_SEQ_OSS_MAX_SYNTH_NAME 30
33 #define MAX_SYSEX_BUFLEN 128
37 * definition of synth info records
41 struct seq_oss_synth_sysex
{
44 unsigned char buf
[MAX_SYSEX_BUFLEN
];
48 struct seq_oss_synth
{
56 char name
[SNDRV_SEQ_OSS_MAX_SYNTH_NAME
];
57 struct snd_seq_oss_callback oper
;
62 snd_use_lock_t use_lock
;
69 static int max_synth_devs
;
70 static struct seq_oss_synth
*synth_devs
[SNDRV_SEQ_OSS_MAX_SYNTH_DEVS
];
71 static struct seq_oss_synth midi_synth_dev
= {
73 SYNTH_TYPE_MIDI
, /* synth_type */
74 0, /* synth_subtype */
79 static DEFINE_SPINLOCK(register_lock
);
84 static struct seq_oss_synth
*get_synthdev(struct seq_oss_devinfo
*dp
, int dev
);
85 static void reset_channels(struct seq_oss_synthinfo
*info
);
88 * global initialization
91 snd_seq_oss_synth_init(void)
93 snd_use_lock_init(&midi_synth_dev
.use_lock
);
97 * registration of the synth device
100 snd_seq_oss_synth_register(struct snd_seq_device
*dev
)
103 struct seq_oss_synth
*rec
;
104 struct snd_seq_oss_reg
*reg
= SNDRV_SEQ_DEVICE_ARGPTR(dev
);
107 if ((rec
= kzalloc(sizeof(*rec
), GFP_KERNEL
)) == NULL
) {
108 snd_printk(KERN_ERR
"can't malloc synth info\n");
111 rec
->seq_device
= -1;
112 rec
->synth_type
= reg
->type
;
113 rec
->synth_subtype
= reg
->subtype
;
114 rec
->nr_voices
= reg
->nvoices
;
115 rec
->oper
= reg
->oper
;
116 rec
->private_data
= reg
->private_data
;
118 snd_use_lock_init(&rec
->use_lock
);
120 /* copy and truncate the name of synth device */
121 strlcpy(rec
->name
, dev
->name
, sizeof(rec
->name
));
124 spin_lock_irqsave(®ister_lock
, flags
);
125 for (i
= 0; i
< max_synth_devs
; i
++) {
126 if (synth_devs
[i
] == NULL
)
129 if (i
>= max_synth_devs
) {
130 if (max_synth_devs
>= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS
) {
131 spin_unlock_irqrestore(®ister_lock
, flags
);
132 snd_printk(KERN_ERR
"no more synth slot\n");
140 debug_printk(("synth %s registered %d\n", rec
->name
, i
));
141 spin_unlock_irqrestore(®ister_lock
, flags
);
142 dev
->driver_data
= rec
;
143 #ifdef SNDRV_OSS_INFO_DEV_SYNTH
145 snd_oss_info_register(SNDRV_OSS_INFO_DEV_SYNTH
, i
, rec
->name
);
152 snd_seq_oss_synth_unregister(struct snd_seq_device
*dev
)
155 struct seq_oss_synth
*rec
= dev
->driver_data
;
158 spin_lock_irqsave(®ister_lock
, flags
);
159 for (index
= 0; index
< max_synth_devs
; index
++) {
160 if (synth_devs
[index
] == rec
)
163 if (index
>= max_synth_devs
) {
164 spin_unlock_irqrestore(®ister_lock
, flags
);
165 snd_printk(KERN_ERR
"can't unregister synth\n");
168 synth_devs
[index
] = NULL
;
169 if (index
== max_synth_devs
- 1) {
170 for (index
--; index
>= 0; index
--) {
171 if (synth_devs
[index
])
174 max_synth_devs
= index
+ 1;
176 spin_unlock_irqrestore(®ister_lock
, flags
);
177 #ifdef SNDRV_OSS_INFO_DEV_SYNTH
178 if (rec
->seq_device
< SNDRV_CARDS
)
179 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_SYNTH
, rec
->seq_device
);
182 snd_use_lock_sync(&rec
->use_lock
);
191 static struct seq_oss_synth
*
194 struct seq_oss_synth
*rec
;
197 spin_lock_irqsave(®ister_lock
, flags
);
198 rec
= synth_devs
[dev
];
200 snd_use_lock_use(&rec
->use_lock
);
201 spin_unlock_irqrestore(®ister_lock
, flags
);
207 * set up synth tables
211 snd_seq_oss_synth_setup(struct seq_oss_devinfo
*dp
)
214 struct seq_oss_synth
*rec
;
215 struct seq_oss_synthinfo
*info
;
217 dp
->max_synthdev
= max_synth_devs
;
218 dp
->synth_opened
= 0;
219 memset(dp
->synths
, 0, sizeof(dp
->synths
));
220 for (i
= 0; i
< dp
->max_synthdev
; i
++) {
224 if (rec
->oper
.open
== NULL
|| rec
->oper
.close
== NULL
) {
225 snd_use_lock_free(&rec
->use_lock
);
228 info
= &dp
->synths
[i
];
229 info
->arg
.app_index
= dp
->port
;
230 info
->arg
.file_mode
= dp
->file_mode
;
231 info
->arg
.seq_mode
= dp
->seq_mode
;
232 if (dp
->seq_mode
== SNDRV_SEQ_OSS_MODE_SYNTH
)
233 info
->arg
.event_passing
= SNDRV_SEQ_OSS_PROCESS_EVENTS
;
235 info
->arg
.event_passing
= SNDRV_SEQ_OSS_PASS_EVENTS
;
237 if (!try_module_get(rec
->oper
.owner
)) {
238 snd_use_lock_free(&rec
->use_lock
);
241 if (rec
->oper
.open(&info
->arg
, rec
->private_data
) < 0) {
242 module_put(rec
->oper
.owner
);
243 snd_use_lock_free(&rec
->use_lock
);
246 info
->nr_voices
= rec
->nr_voices
;
247 if (info
->nr_voices
> 0) {
248 info
->ch
= kcalloc(info
->nr_voices
, sizeof(struct seq_oss_chinfo
), GFP_KERNEL
);
250 snd_printk(KERN_ERR
"Cannot malloc\n");
251 rec
->oper
.close(&info
->arg
);
252 module_put(rec
->oper
.owner
);
253 snd_use_lock_free(&rec
->use_lock
);
256 reset_channels(info
);
258 debug_printk(("synth %d assigned\n", i
));
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 debug_printk(("synth %d closed\n", i
));
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 */