2 * OSS compatible sequencer driver
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 <sound/asoundef.h>
24 #include "seq_oss_midi.h"
25 #include "seq_oss_readq.h"
26 #include "seq_oss_timer.h"
27 #include "seq_oss_event.h"
28 #include <sound/seq_midi_event.h>
29 #include "../seq_lock.h"
30 #include <linux/init.h>
31 #include <linux/slab.h>
37 #define SNDRV_SEQ_OSS_MAX_MIDI_NAME 30
40 * definition of midi device record
43 int seq_device
; /* device number */
44 int client
; /* sequencer client number */
45 int port
; /* sequencer port number */
46 unsigned int flags
; /* port capability */
47 int opened
; /* flag for opening */
48 unsigned char name
[SNDRV_SEQ_OSS_MAX_MIDI_NAME
];
49 struct snd_midi_event
*coder
; /* MIDI event coder */
50 struct seq_oss_devinfo
*devinfo
; /* assigned OSSseq device */
51 snd_use_lock_t use_lock
;
58 static int max_midi_devs
;
59 static struct seq_oss_midi
*midi_devs
[SNDRV_SEQ_OSS_MAX_MIDI_DEVS
];
61 static DEFINE_SPINLOCK(register_lock
);
66 static struct seq_oss_midi
*get_mdev(int dev
);
67 static struct seq_oss_midi
*get_mididev(struct seq_oss_devinfo
*dp
, int dev
);
68 static int send_synth_event(struct seq_oss_devinfo
*dp
, struct snd_seq_event
*ev
, int dev
);
69 static int send_midi_event(struct seq_oss_devinfo
*dp
, struct snd_seq_event
*ev
, struct seq_oss_midi
*mdev
);
72 * look up the existing ports
73 * this looks a very exhausting job.
76 snd_seq_oss_midi_lookup_ports(int client
)
78 struct snd_seq_client_info
*clinfo
;
79 struct snd_seq_port_info
*pinfo
;
81 clinfo
= kzalloc(sizeof(*clinfo
), GFP_KERNEL
);
82 pinfo
= kzalloc(sizeof(*pinfo
), GFP_KERNEL
);
83 if (! clinfo
|| ! pinfo
) {
89 while (snd_seq_kernel_client_ctl(client
, SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT
, clinfo
) == 0) {
90 if (clinfo
->client
== client
)
91 continue; /* ignore myself */
92 pinfo
->addr
.client
= clinfo
->client
;
93 pinfo
->addr
.port
= -1;
94 while (snd_seq_kernel_client_ctl(client
, SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT
, pinfo
) == 0)
95 snd_seq_oss_midi_check_new_port(pinfo
);
105 static struct seq_oss_midi
*
108 struct seq_oss_midi
*mdev
;
111 spin_lock_irqsave(®ister_lock
, flags
);
112 mdev
= midi_devs
[dev
];
114 snd_use_lock_use(&mdev
->use_lock
);
115 spin_unlock_irqrestore(®ister_lock
, flags
);
120 * look for the identical slot
122 static struct seq_oss_midi
*
123 find_slot(int client
, int port
)
126 struct seq_oss_midi
*mdev
;
129 spin_lock_irqsave(®ister_lock
, flags
);
130 for (i
= 0; i
< max_midi_devs
; i
++) {
132 if (mdev
&& mdev
->client
== client
&& mdev
->port
== port
) {
134 snd_use_lock_use(&mdev
->use_lock
);
135 spin_unlock_irqrestore(®ister_lock
, flags
);
139 spin_unlock_irqrestore(®ister_lock
, flags
);
144 #define PERM_WRITE (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_SUBS_WRITE)
145 #define PERM_READ (SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ)
147 * register a new port if it doesn't exist yet
150 snd_seq_oss_midi_check_new_port(struct snd_seq_port_info
*pinfo
)
153 struct seq_oss_midi
*mdev
;
156 /* the port must include generic midi */
157 if (! (pinfo
->type
& SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
))
159 /* either read or write subscribable */
160 if ((pinfo
->capability
& PERM_WRITE
) != PERM_WRITE
&&
161 (pinfo
->capability
& PERM_READ
) != PERM_READ
)
165 * look for the identical slot
167 if ((mdev
= find_slot(pinfo
->addr
.client
, pinfo
->addr
.port
)) != NULL
) {
169 snd_use_lock_free(&mdev
->use_lock
);
174 * allocate midi info record
176 mdev
= kzalloc(sizeof(*mdev
), GFP_KERNEL
);
180 /* copy the port information */
181 mdev
->client
= pinfo
->addr
.client
;
182 mdev
->port
= pinfo
->addr
.port
;
183 mdev
->flags
= pinfo
->capability
;
185 snd_use_lock_init(&mdev
->use_lock
);
187 /* copy and truncate the name of synth device */
188 strlcpy(mdev
->name
, pinfo
->name
, sizeof(mdev
->name
));
190 /* create MIDI coder */
191 if (snd_midi_event_new(MAX_MIDI_EVENT_BUF
, &mdev
->coder
) < 0) {
192 pr_err("ALSA: seq_oss: can't malloc midi coder\n");
196 /* OSS sequencer adds running status to all sequences */
197 snd_midi_event_no_status(mdev
->coder
, 1);
200 * look for en empty slot
202 spin_lock_irqsave(®ister_lock
, flags
);
203 for (i
= 0; i
< max_midi_devs
; i
++) {
204 if (midi_devs
[i
] == NULL
)
207 if (i
>= max_midi_devs
) {
208 if (max_midi_devs
>= SNDRV_SEQ_OSS_MAX_MIDI_DEVS
) {
209 spin_unlock_irqrestore(®ister_lock
, flags
);
210 snd_midi_event_free(mdev
->coder
);
216 mdev
->seq_device
= i
;
217 midi_devs
[mdev
->seq_device
] = mdev
;
218 spin_unlock_irqrestore(®ister_lock
, flags
);
224 * release the midi device if it was registered
227 snd_seq_oss_midi_check_exit_port(int client
, int port
)
229 struct seq_oss_midi
*mdev
;
233 if ((mdev
= find_slot(client
, port
)) != NULL
) {
234 spin_lock_irqsave(®ister_lock
, flags
);
235 midi_devs
[mdev
->seq_device
] = NULL
;
236 spin_unlock_irqrestore(®ister_lock
, flags
);
237 snd_use_lock_free(&mdev
->use_lock
);
238 snd_use_lock_sync(&mdev
->use_lock
);
239 snd_midi_event_free(mdev
->coder
);
242 spin_lock_irqsave(®ister_lock
, flags
);
243 for (index
= max_midi_devs
- 1; index
>= 0; index
--) {
244 if (midi_devs
[index
])
247 max_midi_devs
= index
+ 1;
248 spin_unlock_irqrestore(®ister_lock
, flags
);
254 * release the midi device if it was registered
257 snd_seq_oss_midi_clear_all(void)
260 struct seq_oss_midi
*mdev
;
263 spin_lock_irqsave(®ister_lock
, flags
);
264 for (i
= 0; i
< max_midi_devs
; i
++) {
265 if ((mdev
= midi_devs
[i
]) != NULL
) {
266 snd_midi_event_free(mdev
->coder
);
272 spin_unlock_irqrestore(®ister_lock
, flags
);
280 snd_seq_oss_midi_setup(struct seq_oss_devinfo
*dp
)
282 dp
->max_mididev
= max_midi_devs
;
286 * clean up midi tables
289 snd_seq_oss_midi_cleanup(struct seq_oss_devinfo
*dp
)
292 for (i
= 0; i
< dp
->max_mididev
; i
++)
293 snd_seq_oss_midi_close(dp
, i
);
299 * open all midi devices. ignore errors.
302 snd_seq_oss_midi_open_all(struct seq_oss_devinfo
*dp
, int file_mode
)
305 for (i
= 0; i
< dp
->max_mididev
; i
++)
306 snd_seq_oss_midi_open(dp
, i
, file_mode
);
311 * get the midi device information
313 static struct seq_oss_midi
*
314 get_mididev(struct seq_oss_devinfo
*dp
, int dev
)
316 if (dev
< 0 || dev
>= dp
->max_mididev
)
318 return get_mdev(dev
);
323 * open the midi device if not opened yet
326 snd_seq_oss_midi_open(struct seq_oss_devinfo
*dp
, int dev
, int fmode
)
329 struct seq_oss_midi
*mdev
;
330 struct snd_seq_port_subscribe subs
;
332 if ((mdev
= get_mididev(dp
, dev
)) == NULL
)
336 if (mdev
->opened
&& mdev
->devinfo
!= dp
) {
337 snd_use_lock_free(&mdev
->use_lock
);
342 if (is_write_mode(fmode
))
344 if (is_read_mode(fmode
))
348 snd_use_lock_free(&mdev
->use_lock
);
352 /* already opened? */
353 if ((mdev
->opened
& perm
) == perm
) {
354 snd_use_lock_free(&mdev
->use_lock
);
358 perm
&= ~mdev
->opened
;
360 memset(&subs
, 0, sizeof(subs
));
362 if (perm
& PERM_WRITE
) {
363 subs
.sender
= dp
->addr
;
364 subs
.dest
.client
= mdev
->client
;
365 subs
.dest
.port
= mdev
->port
;
366 if (snd_seq_kernel_client_ctl(dp
->cseq
, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT
, &subs
) >= 0)
367 mdev
->opened
|= PERM_WRITE
;
369 if (perm
& PERM_READ
) {
370 subs
.sender
.client
= mdev
->client
;
371 subs
.sender
.port
= mdev
->port
;
372 subs
.dest
= dp
->addr
;
373 subs
.flags
= SNDRV_SEQ_PORT_SUBS_TIMESTAMP
;
374 subs
.queue
= dp
->queue
; /* queue for timestamps */
375 if (snd_seq_kernel_client_ctl(dp
->cseq
, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT
, &subs
) >= 0)
376 mdev
->opened
|= PERM_READ
;
379 if (! mdev
->opened
) {
380 snd_use_lock_free(&mdev
->use_lock
);
385 snd_use_lock_free(&mdev
->use_lock
);
390 * close the midi device if already opened
393 snd_seq_oss_midi_close(struct seq_oss_devinfo
*dp
, int dev
)
395 struct seq_oss_midi
*mdev
;
396 struct snd_seq_port_subscribe subs
;
398 if ((mdev
= get_mididev(dp
, dev
)) == NULL
)
400 if (! mdev
->opened
|| mdev
->devinfo
!= dp
) {
401 snd_use_lock_free(&mdev
->use_lock
);
405 memset(&subs
, 0, sizeof(subs
));
406 if (mdev
->opened
& PERM_WRITE
) {
407 subs
.sender
= dp
->addr
;
408 subs
.dest
.client
= mdev
->client
;
409 subs
.dest
.port
= mdev
->port
;
410 snd_seq_kernel_client_ctl(dp
->cseq
, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT
, &subs
);
412 if (mdev
->opened
& PERM_READ
) {
413 subs
.sender
.client
= mdev
->client
;
414 subs
.sender
.port
= mdev
->port
;
415 subs
.dest
= dp
->addr
;
416 snd_seq_kernel_client_ctl(dp
->cseq
, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT
, &subs
);
420 mdev
->devinfo
= NULL
;
422 snd_use_lock_free(&mdev
->use_lock
);
427 * change seq capability flags to file mode flags
430 snd_seq_oss_midi_filemode(struct seq_oss_devinfo
*dp
, int dev
)
432 struct seq_oss_midi
*mdev
;
435 if ((mdev
= get_mididev(dp
, dev
)) == NULL
)
439 if (mdev
->opened
& PERM_WRITE
)
440 mode
|= SNDRV_SEQ_OSS_FILE_WRITE
;
441 if (mdev
->opened
& PERM_READ
)
442 mode
|= SNDRV_SEQ_OSS_FILE_READ
;
444 snd_use_lock_free(&mdev
->use_lock
);
449 * reset the midi device and close it:
450 * so far, only close the device.
453 snd_seq_oss_midi_reset(struct seq_oss_devinfo
*dp
, int dev
)
455 struct seq_oss_midi
*mdev
;
457 if ((mdev
= get_mididev(dp
, dev
)) == NULL
)
459 if (! mdev
->opened
) {
460 snd_use_lock_free(&mdev
->use_lock
);
464 if (mdev
->opened
& PERM_WRITE
) {
465 struct snd_seq_event ev
;
468 memset(&ev
, 0, sizeof(ev
));
469 ev
.dest
.client
= mdev
->client
;
470 ev
.dest
.port
= mdev
->port
;
471 ev
.queue
= dp
->queue
;
472 ev
.source
.port
= dp
->port
;
473 if (dp
->seq_mode
== SNDRV_SEQ_OSS_MODE_SYNTH
) {
474 ev
.type
= SNDRV_SEQ_EVENT_SENSING
;
475 snd_seq_oss_dispatch(dp
, &ev
, 0, 0);
477 for (c
= 0; c
< 16; c
++) {
478 ev
.type
= SNDRV_SEQ_EVENT_CONTROLLER
;
479 ev
.data
.control
.channel
= c
;
480 ev
.data
.control
.param
= MIDI_CTL_ALL_NOTES_OFF
;
481 snd_seq_oss_dispatch(dp
, &ev
, 0, 0);
482 if (dp
->seq_mode
== SNDRV_SEQ_OSS_MODE_MUSIC
) {
483 ev
.data
.control
.param
=
484 MIDI_CTL_RESET_CONTROLLERS
;
485 snd_seq_oss_dispatch(dp
, &ev
, 0, 0);
486 ev
.type
= SNDRV_SEQ_EVENT_PITCHBEND
;
487 ev
.data
.control
.value
= 0;
488 snd_seq_oss_dispatch(dp
, &ev
, 0, 0);
492 // snd_seq_oss_midi_close(dp, dev);
493 snd_use_lock_free(&mdev
->use_lock
);
498 * get client/port of the specified MIDI device
501 snd_seq_oss_midi_get_addr(struct seq_oss_devinfo
*dp
, int dev
, struct snd_seq_addr
*addr
)
503 struct seq_oss_midi
*mdev
;
505 if ((mdev
= get_mididev(dp
, dev
)) == NULL
)
507 addr
->client
= mdev
->client
;
508 addr
->port
= mdev
->port
;
509 snd_use_lock_free(&mdev
->use_lock
);
514 * input callback - this can be atomic
517 snd_seq_oss_midi_input(struct snd_seq_event
*ev
, int direct
, void *private_data
)
519 struct seq_oss_devinfo
*dp
= (struct seq_oss_devinfo
*)private_data
;
520 struct seq_oss_midi
*mdev
;
523 if (dp
->readq
== NULL
)
525 if ((mdev
= find_slot(ev
->source
.client
, ev
->source
.port
)) == NULL
)
527 if (! (mdev
->opened
& PERM_READ
)) {
528 snd_use_lock_free(&mdev
->use_lock
);
532 if (dp
->seq_mode
== SNDRV_SEQ_OSS_MODE_MUSIC
)
533 rc
= send_synth_event(dp
, ev
, mdev
->seq_device
);
535 rc
= send_midi_event(dp
, ev
, mdev
);
537 snd_use_lock_free(&mdev
->use_lock
);
542 * convert ALSA sequencer event to OSS synth event
545 send_synth_event(struct seq_oss_devinfo
*dp
, struct snd_seq_event
*ev
, int dev
)
549 memset(&ossev
, 0, sizeof(ossev
));
552 case SNDRV_SEQ_EVENT_NOTEON
:
553 ossev
.v
.cmd
= MIDI_NOTEON
; break;
554 case SNDRV_SEQ_EVENT_NOTEOFF
:
555 ossev
.v
.cmd
= MIDI_NOTEOFF
; break;
556 case SNDRV_SEQ_EVENT_KEYPRESS
:
557 ossev
.v
.cmd
= MIDI_KEY_PRESSURE
; break;
558 case SNDRV_SEQ_EVENT_CONTROLLER
:
559 ossev
.l
.cmd
= MIDI_CTL_CHANGE
; break;
560 case SNDRV_SEQ_EVENT_PGMCHANGE
:
561 ossev
.l
.cmd
= MIDI_PGM_CHANGE
; break;
562 case SNDRV_SEQ_EVENT_CHANPRESS
:
563 ossev
.l
.cmd
= MIDI_CHN_PRESSURE
; break;
564 case SNDRV_SEQ_EVENT_PITCHBEND
:
565 ossev
.l
.cmd
= MIDI_PITCH_BEND
; break;
567 return 0; /* not supported */
573 case SNDRV_SEQ_EVENT_NOTEON
:
574 case SNDRV_SEQ_EVENT_NOTEOFF
:
575 case SNDRV_SEQ_EVENT_KEYPRESS
:
576 ossev
.v
.code
= EV_CHN_VOICE
;
577 ossev
.v
.note
= ev
->data
.note
.note
;
578 ossev
.v
.parm
= ev
->data
.note
.velocity
;
579 ossev
.v
.chn
= ev
->data
.note
.channel
;
581 case SNDRV_SEQ_EVENT_CONTROLLER
:
582 case SNDRV_SEQ_EVENT_PGMCHANGE
:
583 case SNDRV_SEQ_EVENT_CHANPRESS
:
584 ossev
.l
.code
= EV_CHN_COMMON
;
585 ossev
.l
.p1
= ev
->data
.control
.param
;
586 ossev
.l
.val
= ev
->data
.control
.value
;
587 ossev
.l
.chn
= ev
->data
.control
.channel
;
589 case SNDRV_SEQ_EVENT_PITCHBEND
:
590 ossev
.l
.code
= EV_CHN_COMMON
;
591 ossev
.l
.val
= ev
->data
.control
.value
+ 8192;
592 ossev
.l
.chn
= ev
->data
.control
.channel
;
596 snd_seq_oss_readq_put_timestamp(dp
->readq
, ev
->time
.tick
, dp
->seq_mode
);
597 snd_seq_oss_readq_put_event(dp
->readq
, &ossev
);
603 * decode event and send MIDI bytes to read queue
606 send_midi_event(struct seq_oss_devinfo
*dp
, struct snd_seq_event
*ev
, struct seq_oss_midi
*mdev
)
611 snd_seq_oss_readq_put_timestamp(dp
->readq
, ev
->time
.tick
, dp
->seq_mode
);
612 if (!dp
->timer
->running
)
613 len
= snd_seq_oss_timer_start(dp
->timer
);
614 if (ev
->type
== SNDRV_SEQ_EVENT_SYSEX
) {
615 if ((ev
->flags
& SNDRV_SEQ_EVENT_LENGTH_MASK
) == SNDRV_SEQ_EVENT_LENGTH_VARIABLE
)
616 snd_seq_oss_readq_puts(dp
->readq
, mdev
->seq_device
,
617 ev
->data
.ext
.ptr
, ev
->data
.ext
.len
);
619 len
= snd_midi_event_decode(mdev
->coder
, msg
, sizeof(msg
), ev
);
621 snd_seq_oss_readq_puts(dp
->readq
, mdev
->seq_device
, msg
, len
);
630 * return 0 : enqueued
631 * non-zero : invalid - ignored
634 snd_seq_oss_midi_putc(struct seq_oss_devinfo
*dp
, int dev
, unsigned char c
, struct snd_seq_event
*ev
)
636 struct seq_oss_midi
*mdev
;
638 if ((mdev
= get_mididev(dp
, dev
)) == NULL
)
640 if (snd_midi_event_encode_byte(mdev
->coder
, c
, ev
) > 0) {
641 snd_seq_oss_fill_addr(dp
, ev
, mdev
->client
, mdev
->port
);
642 snd_use_lock_free(&mdev
->use_lock
);
645 snd_use_lock_free(&mdev
->use_lock
);
650 * create OSS compatible midi_info record
653 snd_seq_oss_midi_make_info(struct seq_oss_devinfo
*dp
, int dev
, struct midi_info
*inf
)
655 struct seq_oss_midi
*mdev
;
657 if ((mdev
= get_mididev(dp
, dev
)) == NULL
)
660 inf
->dev_type
= 0; /* FIXME: ?? */
661 inf
->capabilities
= 0; /* FIXME: ?? */
662 strlcpy(inf
->name
, mdev
->name
, sizeof(inf
->name
));
663 snd_use_lock_free(&mdev
->use_lock
);
668 #ifdef CONFIG_SND_PROC_FS
675 val
&= PERM_READ
|PERM_WRITE
;
676 if (val
== (PERM_READ
|PERM_WRITE
))
678 else if (val
== PERM_READ
)
680 else if (val
== PERM_WRITE
)
687 snd_seq_oss_midi_info_read(struct snd_info_buffer
*buf
)
690 struct seq_oss_midi
*mdev
;
692 snd_iprintf(buf
, "\nNumber of MIDI devices: %d\n", max_midi_devs
);
693 for (i
= 0; i
< max_midi_devs
; i
++) {
694 snd_iprintf(buf
, "\nmidi %d: ", i
);
697 snd_iprintf(buf
, "*empty*\n");
700 snd_iprintf(buf
, "[%s] ALSA port %d:%d\n", mdev
->name
,
701 mdev
->client
, mdev
->port
);
702 snd_iprintf(buf
, " capability %s / opened %s\n",
703 capmode_str(mdev
->flags
),
704 capmode_str(mdev
->opened
));
705 snd_use_lock_free(&mdev
->use_lock
);
708 #endif /* CONFIG_SND_PROC_FS */