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 debug_printk(("check for MIDI client %d port %d\n", pinfo->addr.client, pinfo->addr.port));
157 /* the port must include generic midi */
158 if (! (pinfo->type & SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC))
160 /* either read or write subscribable */
161 if ((pinfo->capability & PERM_WRITE) != PERM_WRITE &&
162 (pinfo->capability & PERM_READ) != PERM_READ)
166 * look for the identical slot
168 if ((mdev = find_slot(pinfo->addr.client, pinfo->addr.port)) != NULL) {
170 snd_use_lock_free(&mdev->use_lock);
175 * allocate midi info record
177 if ((mdev = kzalloc(sizeof(*mdev), GFP_KERNEL)) == NULL) {
178 snd_printk(KERN_ERR "can't malloc midi info\n");
182 /* copy the port information */
183 mdev->client = pinfo->addr.client;
184 mdev->port = pinfo->addr.port;
185 mdev->flags = pinfo->capability;
187 snd_use_lock_init(&mdev->use_lock);
189 /* copy and truncate the name of synth device */
190 strlcpy(mdev->name, pinfo->name, sizeof(mdev->name));
192 /* create MIDI coder */
193 if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &mdev->coder) < 0) {
194 snd_printk(KERN_ERR "can't malloc midi coder\n");
198 /* OSS sequencer adds running status to all sequences */
199 snd_midi_event_no_status(mdev->coder, 1);
202 * look for en empty slot
204 spin_lock_irqsave(®ister_lock, flags);
205 for (i = 0; i < max_midi_devs; i++) {
206 if (midi_devs[i] == NULL)
209 if (i >= max_midi_devs) {
210 if (max_midi_devs >= SNDRV_SEQ_OSS_MAX_MIDI_DEVS) {
211 spin_unlock_irqrestore(®ister_lock, flags);
212 snd_midi_event_free(mdev->coder);
218 mdev->seq_device = i;
219 midi_devs[mdev->seq_device] = mdev;
220 spin_unlock_irqrestore(®ister_lock, flags);
226 * release the midi device if it was registered
229 snd_seq_oss_midi_check_exit_port(int client, int port)
231 struct seq_oss_midi *mdev;
235 if ((mdev = find_slot(client, port)) != NULL) {
236 spin_lock_irqsave(®ister_lock, flags);
237 midi_devs[mdev->seq_device] = NULL;
238 spin_unlock_irqrestore(®ister_lock, flags);
239 snd_use_lock_free(&mdev->use_lock);
240 snd_use_lock_sync(&mdev->use_lock);
242 snd_midi_event_free(mdev->coder);
245 spin_lock_irqsave(®ister_lock, flags);
246 for (index = max_midi_devs - 1; index >= 0; index--) {
247 if (midi_devs[index])
250 max_midi_devs = index + 1;
251 spin_unlock_irqrestore(®ister_lock, flags);
257 * release the midi device if it was registered
260 snd_seq_oss_midi_clear_all(void)
263 struct seq_oss_midi *mdev;
266 spin_lock_irqsave(®ister_lock, flags);
267 for (i = 0; i < max_midi_devs; i++) {
268 if ((mdev = midi_devs[i]) != NULL) {
270 snd_midi_event_free(mdev->coder);
276 spin_unlock_irqrestore(®ister_lock, flags);
284 snd_seq_oss_midi_setup(struct seq_oss_devinfo *dp)
286 dp->max_mididev = max_midi_devs;
290 * clean up midi tables
293 snd_seq_oss_midi_cleanup(struct seq_oss_devinfo *dp)
296 for (i = 0; i < dp->max_mididev; i++)
297 snd_seq_oss_midi_close(dp, i);
303 * open all midi devices. ignore errors.
306 snd_seq_oss_midi_open_all(struct seq_oss_devinfo *dp, int file_mode)
309 for (i = 0; i < dp->max_mididev; i++)
310 snd_seq_oss_midi_open(dp, i, file_mode);
315 * get the midi device information
317 static struct seq_oss_midi *
318 get_mididev(struct seq_oss_devinfo *dp, int dev)
320 if (dev < 0 || dev >= dp->max_mididev)
322 return get_mdev(dev);
327 * open the midi device if not opened yet
330 snd_seq_oss_midi_open(struct seq_oss_devinfo *dp, int dev, int fmode)
333 struct seq_oss_midi *mdev;
334 struct snd_seq_port_subscribe subs;
336 if ((mdev = get_mididev(dp, dev)) == NULL)
340 if (mdev->opened && mdev->devinfo != dp) {
341 snd_use_lock_free(&mdev->use_lock);
346 if (is_write_mode(fmode))
348 if (is_read_mode(fmode))
352 snd_use_lock_free(&mdev->use_lock);
356 /* already opened? */
357 if ((mdev->opened & perm) == perm) {
358 snd_use_lock_free(&mdev->use_lock);
362 perm &= ~mdev->opened;
364 memset(&subs, 0, sizeof(subs));
366 if (perm & PERM_WRITE) {
367 subs.sender = dp->addr;
368 subs.dest.client = mdev->client;
369 subs.dest.port = mdev->port;
370 if (snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs) >= 0)
371 mdev->opened |= PERM_WRITE;
373 if (perm & PERM_READ) {
374 subs.sender.client = mdev->client;
375 subs.sender.port = mdev->port;
376 subs.dest = dp->addr;
377 subs.flags = SNDRV_SEQ_PORT_SUBS_TIMESTAMP;
378 subs.queue = dp->queue; /* queue for timestamps */
379 if (snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs) >= 0)
380 mdev->opened |= PERM_READ;
383 if (! mdev->opened) {
384 snd_use_lock_free(&mdev->use_lock);
389 snd_use_lock_free(&mdev->use_lock);
394 * close the midi device if already opened
397 snd_seq_oss_midi_close(struct seq_oss_devinfo *dp, int dev)
399 struct seq_oss_midi *mdev;
400 struct snd_seq_port_subscribe subs;
402 if ((mdev = get_mididev(dp, dev)) == NULL)
404 if (! mdev->opened || mdev->devinfo != dp) {
405 snd_use_lock_free(&mdev->use_lock);
409 debug_printk(("closing client %d port %d mode %d\n", mdev->client, mdev->port, mdev->opened));
410 memset(&subs, 0, sizeof(subs));
411 if (mdev->opened & PERM_WRITE) {
412 subs.sender = dp->addr;
413 subs.dest.client = mdev->client;
414 subs.dest.port = mdev->port;
415 snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, &subs);
417 if (mdev->opened & PERM_READ) {
418 subs.sender.client = mdev->client;
419 subs.sender.port = mdev->port;
420 subs.dest = dp->addr;
421 snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, &subs);
425 mdev->devinfo = NULL;
427 snd_use_lock_free(&mdev->use_lock);
432 * change seq capability flags to file mode flags
435 snd_seq_oss_midi_filemode(struct seq_oss_devinfo *dp, int dev)
437 struct seq_oss_midi *mdev;
440 if ((mdev = get_mididev(dp, dev)) == NULL)
444 if (mdev->opened & PERM_WRITE)
445 mode |= SNDRV_SEQ_OSS_FILE_WRITE;
446 if (mdev->opened & PERM_READ)
447 mode |= SNDRV_SEQ_OSS_FILE_READ;
449 snd_use_lock_free(&mdev->use_lock);
454 * reset the midi device and close it:
455 * so far, only close the device.
458 snd_seq_oss_midi_reset(struct seq_oss_devinfo *dp, int dev)
460 struct seq_oss_midi *mdev;
462 if ((mdev = get_mididev(dp, dev)) == NULL)
464 if (! mdev->opened) {
465 snd_use_lock_free(&mdev->use_lock);
469 if (mdev->opened & PERM_WRITE) {
470 struct snd_seq_event ev;
473 debug_printk(("resetting client %d port %d\n", mdev->client, mdev->port));
474 memset(&ev, 0, sizeof(ev));
475 ev.dest.client = mdev->client;
476 ev.dest.port = mdev->port;
477 ev.queue = dp->queue;
478 ev.source.port = dp->port;
479 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH) {
480 ev.type = SNDRV_SEQ_EVENT_SENSING;
481 snd_seq_oss_dispatch(dp, &ev, 0, 0);
483 for (c = 0; c < 16; c++) {
484 ev.type = SNDRV_SEQ_EVENT_CONTROLLER;
485 ev.data.control.channel = c;
486 ev.data.control.param = MIDI_CTL_ALL_NOTES_OFF;
487 snd_seq_oss_dispatch(dp, &ev, 0, 0);
488 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) {
489 ev.data.control.param =
490 MIDI_CTL_RESET_CONTROLLERS;
491 snd_seq_oss_dispatch(dp, &ev, 0, 0);
492 ev.type = SNDRV_SEQ_EVENT_PITCHBEND;
493 ev.data.control.value = 0;
494 snd_seq_oss_dispatch(dp, &ev, 0, 0);
498 // snd_seq_oss_midi_close(dp, dev);
499 snd_use_lock_free(&mdev->use_lock);
504 * get client/port of the specified MIDI device
507 snd_seq_oss_midi_get_addr(struct seq_oss_devinfo *dp, int dev, struct snd_seq_addr *addr)
509 struct seq_oss_midi *mdev;
511 if ((mdev = get_mididev(dp, dev)) == NULL)
513 addr->client = mdev->client;
514 addr->port = mdev->port;
515 snd_use_lock_free(&mdev->use_lock);
520 * input callback - this can be atomic
523 snd_seq_oss_midi_input(struct snd_seq_event *ev, int direct, void *private_data)
525 struct seq_oss_devinfo *dp = (struct seq_oss_devinfo *)private_data;
526 struct seq_oss_midi *mdev;
529 if (dp->readq == NULL)
531 if ((mdev = find_slot(ev->source.client, ev->source.port)) == NULL)
533 if (! (mdev->opened & PERM_READ)) {
534 snd_use_lock_free(&mdev->use_lock);
538 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
539 rc = send_synth_event(dp, ev, mdev->seq_device);
541 rc = send_midi_event(dp, ev, mdev);
543 snd_use_lock_free(&mdev->use_lock);
548 * convert ALSA sequencer event to OSS synth event
551 send_synth_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, int dev)
555 memset(&ossev, 0, sizeof(ossev));
558 case SNDRV_SEQ_EVENT_NOTEON:
559 ossev.v.cmd = MIDI_NOTEON; break;
560 case SNDRV_SEQ_EVENT_NOTEOFF:
561 ossev.v.cmd = MIDI_NOTEOFF; break;
562 case SNDRV_SEQ_EVENT_KEYPRESS:
563 ossev.v.cmd = MIDI_KEY_PRESSURE; break;
564 case SNDRV_SEQ_EVENT_CONTROLLER:
565 ossev.l.cmd = MIDI_CTL_CHANGE; break;
566 case SNDRV_SEQ_EVENT_PGMCHANGE:
567 ossev.l.cmd = MIDI_PGM_CHANGE; break;
568 case SNDRV_SEQ_EVENT_CHANPRESS:
569 ossev.l.cmd = MIDI_CHN_PRESSURE; break;
570 case SNDRV_SEQ_EVENT_PITCHBEND:
571 ossev.l.cmd = MIDI_PITCH_BEND; break;
573 return 0; /* not supported */
579 case SNDRV_SEQ_EVENT_NOTEON:
580 case SNDRV_SEQ_EVENT_NOTEOFF:
581 case SNDRV_SEQ_EVENT_KEYPRESS:
582 ossev.v.code = EV_CHN_VOICE;
583 ossev.v.note = ev->data.note.note;
584 ossev.v.parm = ev->data.note.velocity;
585 ossev.v.chn = ev->data.note.channel;
587 case SNDRV_SEQ_EVENT_CONTROLLER:
588 case SNDRV_SEQ_EVENT_PGMCHANGE:
589 case SNDRV_SEQ_EVENT_CHANPRESS:
590 ossev.l.code = EV_CHN_COMMON;
591 ossev.l.p1 = ev->data.control.param;
592 ossev.l.val = ev->data.control.value;
593 ossev.l.chn = ev->data.control.channel;
595 case SNDRV_SEQ_EVENT_PITCHBEND:
596 ossev.l.code = EV_CHN_COMMON;
597 ossev.l.val = ev->data.control.value + 8192;
598 ossev.l.chn = ev->data.control.channel;
602 snd_seq_oss_readq_put_timestamp(dp->readq, ev->time.tick, dp->seq_mode);
603 snd_seq_oss_readq_put_event(dp->readq, &ossev);
609 * decode event and send MIDI bytes to read queue
612 send_midi_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, struct seq_oss_midi *mdev)
617 snd_seq_oss_readq_put_timestamp(dp->readq, ev->time.tick, dp->seq_mode);
618 if (!dp->timer->running)
619 len = snd_seq_oss_timer_start(dp->timer);
620 if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {
621 if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) == SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
622 snd_seq_oss_readq_puts(dp->readq, mdev->seq_device,
623 ev->data.ext.ptr, ev->data.ext.len);
625 len = snd_midi_event_decode(mdev->coder, msg, sizeof(msg), ev);
627 snd_seq_oss_readq_puts(dp->readq, mdev->seq_device, msg, len);
636 * return 0 : enqueued
637 * non-zero : invalid - ignored
640 snd_seq_oss_midi_putc(struct seq_oss_devinfo *dp, int dev, unsigned char c, struct snd_seq_event *ev)
642 struct seq_oss_midi *mdev;
644 if ((mdev = get_mididev(dp, dev)) == NULL)
646 if (snd_midi_event_encode_byte(mdev->coder, c, ev) > 0) {
647 snd_seq_oss_fill_addr(dp, ev, mdev->client, mdev->port);
648 snd_use_lock_free(&mdev->use_lock);
651 snd_use_lock_free(&mdev->use_lock);
656 * create OSS compatible midi_info record
659 snd_seq_oss_midi_make_info(struct seq_oss_devinfo *dp, int dev, struct midi_info *inf)
661 struct seq_oss_midi *mdev;
663 if ((mdev = get_mididev(dp, dev)) == NULL)
666 inf->dev_type = 0; /* FIXME: ?? */
667 inf->capabilities = 0; /* FIXME: ?? */
668 strlcpy(inf->name, mdev->name, sizeof(inf->name));
669 snd_use_lock_free(&mdev->use_lock);
674 #ifdef CONFIG_PROC_FS
681 val &= PERM_READ|PERM_WRITE;
682 if (val == (PERM_READ|PERM_WRITE))
684 else if (val == PERM_READ)
686 else if (val == PERM_WRITE)
693 snd_seq_oss_midi_info_read(struct snd_info_buffer *buf)
696 struct seq_oss_midi *mdev;
698 snd_iprintf(buf, "\nNumber of MIDI devices: %d\n", max_midi_devs);
699 for (i = 0; i < max_midi_devs; i++) {
700 snd_iprintf(buf, "\nmidi %d: ", i);
703 snd_iprintf(buf, "*empty*\n");
706 snd_iprintf(buf, "[%s] ALSA port %d:%d\n", mdev->name,
707 mdev->client, mdev->port);
708 snd_iprintf(buf, " capability %s / opened %s\n",
709 capmode_str(mdev->flags),
710 capmode_str(mdev->opened));
711 snd_use_lock_free(&mdev->use_lock);
714 #endif /* CONFIG_PROC_FS */