1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OSS compatible sequencer driver
7 * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
10 #include <sound/asoundef.h>
11 #include "seq_oss_midi.h"
12 #include "seq_oss_readq.h"
13 #include "seq_oss_timer.h"
14 #include "seq_oss_event.h"
15 #include <sound/seq_midi_event.h>
16 #include "../seq_lock.h"
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/nospec.h>
25 #define SNDRV_SEQ_OSS_MAX_MIDI_NAME 30
28 * definition of midi device record
31 int seq_device; /* device number */
32 int client; /* sequencer client number */
33 int port; /* sequencer port number */
34 unsigned int flags; /* port capability */
35 int opened; /* flag for opening */
36 unsigned char name[SNDRV_SEQ_OSS_MAX_MIDI_NAME];
37 struct snd_midi_event *coder; /* MIDI event coder */
38 struct seq_oss_devinfo *devinfo; /* assigned OSSseq device */
39 snd_use_lock_t use_lock;
46 static int max_midi_devs;
47 static struct seq_oss_midi *midi_devs[SNDRV_SEQ_OSS_MAX_MIDI_DEVS];
49 static DEFINE_SPINLOCK(register_lock);
54 static struct seq_oss_midi *get_mdev(int dev);
55 static struct seq_oss_midi *get_mididev(struct seq_oss_devinfo *dp, int dev);
56 static int send_synth_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, int dev);
57 static int send_midi_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, struct seq_oss_midi *mdev);
60 * look up the existing ports
61 * this looks a very exhausting job.
64 snd_seq_oss_midi_lookup_ports(int client)
66 struct snd_seq_client_info *clinfo;
67 struct snd_seq_port_info *pinfo;
69 clinfo = kzalloc(sizeof(*clinfo), GFP_KERNEL);
70 pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
71 if (! clinfo || ! pinfo) {
77 while (snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT, clinfo) == 0) {
78 if (clinfo->client == client)
79 continue; /* ignore myself */
80 pinfo->addr.client = clinfo->client;
81 pinfo->addr.port = -1;
82 while (snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT, pinfo) == 0)
83 snd_seq_oss_midi_check_new_port(pinfo);
93 static struct seq_oss_midi *
96 struct seq_oss_midi *mdev;
99 spin_lock_irqsave(®ister_lock, flags);
100 mdev = midi_devs[dev];
102 snd_use_lock_use(&mdev->use_lock);
103 spin_unlock_irqrestore(®ister_lock, flags);
108 * look for the identical slot
110 static struct seq_oss_midi *
111 find_slot(int client, int port)
114 struct seq_oss_midi *mdev;
117 spin_lock_irqsave(®ister_lock, flags);
118 for (i = 0; i < max_midi_devs; i++) {
120 if (mdev && mdev->client == client && mdev->port == port) {
122 snd_use_lock_use(&mdev->use_lock);
123 spin_unlock_irqrestore(®ister_lock, flags);
127 spin_unlock_irqrestore(®ister_lock, flags);
132 #define PERM_WRITE (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_SUBS_WRITE)
133 #define PERM_READ (SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ)
135 * register a new port if it doesn't exist yet
138 snd_seq_oss_midi_check_new_port(struct snd_seq_port_info *pinfo)
141 struct seq_oss_midi *mdev;
144 /* the port must include generic midi */
145 if (! (pinfo->type & SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC))
147 /* either read or write subscribable */
148 if ((pinfo->capability & PERM_WRITE) != PERM_WRITE &&
149 (pinfo->capability & PERM_READ) != PERM_READ)
153 * look for the identical slot
155 if ((mdev = find_slot(pinfo->addr.client, pinfo->addr.port)) != NULL) {
157 snd_use_lock_free(&mdev->use_lock);
162 * allocate midi info record
164 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
168 /* copy the port information */
169 mdev->client = pinfo->addr.client;
170 mdev->port = pinfo->addr.port;
171 mdev->flags = pinfo->capability;
173 snd_use_lock_init(&mdev->use_lock);
175 /* copy and truncate the name of synth device */
176 strlcpy(mdev->name, pinfo->name, sizeof(mdev->name));
178 /* create MIDI coder */
179 if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &mdev->coder) < 0) {
180 pr_err("ALSA: seq_oss: can't malloc midi coder\n");
184 /* OSS sequencer adds running status to all sequences */
185 snd_midi_event_no_status(mdev->coder, 1);
188 * look for en empty slot
190 spin_lock_irqsave(®ister_lock, flags);
191 for (i = 0; i < max_midi_devs; i++) {
192 if (midi_devs[i] == NULL)
195 if (i >= max_midi_devs) {
196 if (max_midi_devs >= SNDRV_SEQ_OSS_MAX_MIDI_DEVS) {
197 spin_unlock_irqrestore(®ister_lock, flags);
198 snd_midi_event_free(mdev->coder);
204 mdev->seq_device = i;
205 midi_devs[mdev->seq_device] = mdev;
206 spin_unlock_irqrestore(®ister_lock, flags);
212 * release the midi device if it was registered
215 snd_seq_oss_midi_check_exit_port(int client, int port)
217 struct seq_oss_midi *mdev;
221 if ((mdev = find_slot(client, port)) != NULL) {
222 spin_lock_irqsave(®ister_lock, flags);
223 midi_devs[mdev->seq_device] = NULL;
224 spin_unlock_irqrestore(®ister_lock, flags);
225 snd_use_lock_free(&mdev->use_lock);
226 snd_use_lock_sync(&mdev->use_lock);
227 snd_midi_event_free(mdev->coder);
230 spin_lock_irqsave(®ister_lock, flags);
231 for (index = max_midi_devs - 1; index >= 0; index--) {
232 if (midi_devs[index])
235 max_midi_devs = index + 1;
236 spin_unlock_irqrestore(®ister_lock, flags);
242 * release the midi device if it was registered
245 snd_seq_oss_midi_clear_all(void)
248 struct seq_oss_midi *mdev;
251 spin_lock_irqsave(®ister_lock, flags);
252 for (i = 0; i < max_midi_devs; i++) {
253 if ((mdev = midi_devs[i]) != NULL) {
254 snd_midi_event_free(mdev->coder);
260 spin_unlock_irqrestore(®ister_lock, flags);
268 snd_seq_oss_midi_setup(struct seq_oss_devinfo *dp)
270 dp->max_mididev = max_midi_devs;
274 * clean up midi tables
277 snd_seq_oss_midi_cleanup(struct seq_oss_devinfo *dp)
280 for (i = 0; i < dp->max_mididev; i++)
281 snd_seq_oss_midi_close(dp, i);
287 * open all midi devices. ignore errors.
290 snd_seq_oss_midi_open_all(struct seq_oss_devinfo *dp, int file_mode)
293 for (i = 0; i < dp->max_mididev; i++)
294 snd_seq_oss_midi_open(dp, i, file_mode);
299 * get the midi device information
301 static struct seq_oss_midi *
302 get_mididev(struct seq_oss_devinfo *dp, int dev)
304 if (dev < 0 || dev >= dp->max_mididev)
306 dev = array_index_nospec(dev, dp->max_mididev);
307 return get_mdev(dev);
312 * open the midi device if not opened yet
315 snd_seq_oss_midi_open(struct seq_oss_devinfo *dp, int dev, int fmode)
318 struct seq_oss_midi *mdev;
319 struct snd_seq_port_subscribe subs;
321 if ((mdev = get_mididev(dp, dev)) == NULL)
325 if (mdev->opened && mdev->devinfo != dp) {
326 snd_use_lock_free(&mdev->use_lock);
331 if (is_write_mode(fmode))
333 if (is_read_mode(fmode))
337 snd_use_lock_free(&mdev->use_lock);
341 /* already opened? */
342 if ((mdev->opened & perm) == perm) {
343 snd_use_lock_free(&mdev->use_lock);
347 perm &= ~mdev->opened;
349 memset(&subs, 0, sizeof(subs));
351 if (perm & PERM_WRITE) {
352 subs.sender = dp->addr;
353 subs.dest.client = mdev->client;
354 subs.dest.port = mdev->port;
355 if (snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs) >= 0)
356 mdev->opened |= PERM_WRITE;
358 if (perm & PERM_READ) {
359 subs.sender.client = mdev->client;
360 subs.sender.port = mdev->port;
361 subs.dest = dp->addr;
362 subs.flags = SNDRV_SEQ_PORT_SUBS_TIMESTAMP;
363 subs.queue = dp->queue; /* queue for timestamps */
364 if (snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs) >= 0)
365 mdev->opened |= PERM_READ;
368 if (! mdev->opened) {
369 snd_use_lock_free(&mdev->use_lock);
374 snd_use_lock_free(&mdev->use_lock);
379 * close the midi device if already opened
382 snd_seq_oss_midi_close(struct seq_oss_devinfo *dp, int dev)
384 struct seq_oss_midi *mdev;
385 struct snd_seq_port_subscribe subs;
387 if ((mdev = get_mididev(dp, dev)) == NULL)
389 if (! mdev->opened || mdev->devinfo != dp) {
390 snd_use_lock_free(&mdev->use_lock);
394 memset(&subs, 0, sizeof(subs));
395 if (mdev->opened & PERM_WRITE) {
396 subs.sender = dp->addr;
397 subs.dest.client = mdev->client;
398 subs.dest.port = mdev->port;
399 snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, &subs);
401 if (mdev->opened & PERM_READ) {
402 subs.sender.client = mdev->client;
403 subs.sender.port = mdev->port;
404 subs.dest = dp->addr;
405 snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, &subs);
409 mdev->devinfo = NULL;
411 snd_use_lock_free(&mdev->use_lock);
416 * change seq capability flags to file mode flags
419 snd_seq_oss_midi_filemode(struct seq_oss_devinfo *dp, int dev)
421 struct seq_oss_midi *mdev;
424 if ((mdev = get_mididev(dp, dev)) == NULL)
428 if (mdev->opened & PERM_WRITE)
429 mode |= SNDRV_SEQ_OSS_FILE_WRITE;
430 if (mdev->opened & PERM_READ)
431 mode |= SNDRV_SEQ_OSS_FILE_READ;
433 snd_use_lock_free(&mdev->use_lock);
438 * reset the midi device and close it:
439 * so far, only close the device.
442 snd_seq_oss_midi_reset(struct seq_oss_devinfo *dp, int dev)
444 struct seq_oss_midi *mdev;
446 if ((mdev = get_mididev(dp, dev)) == NULL)
448 if (! mdev->opened) {
449 snd_use_lock_free(&mdev->use_lock);
453 if (mdev->opened & PERM_WRITE) {
454 struct snd_seq_event ev;
457 memset(&ev, 0, sizeof(ev));
458 ev.dest.client = mdev->client;
459 ev.dest.port = mdev->port;
460 ev.queue = dp->queue;
461 ev.source.port = dp->port;
462 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH) {
463 ev.type = SNDRV_SEQ_EVENT_SENSING;
464 snd_seq_oss_dispatch(dp, &ev, 0, 0);
466 for (c = 0; c < 16; c++) {
467 ev.type = SNDRV_SEQ_EVENT_CONTROLLER;
468 ev.data.control.channel = c;
469 ev.data.control.param = MIDI_CTL_ALL_NOTES_OFF;
470 snd_seq_oss_dispatch(dp, &ev, 0, 0);
471 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) {
472 ev.data.control.param =
473 MIDI_CTL_RESET_CONTROLLERS;
474 snd_seq_oss_dispatch(dp, &ev, 0, 0);
475 ev.type = SNDRV_SEQ_EVENT_PITCHBEND;
476 ev.data.control.value = 0;
477 snd_seq_oss_dispatch(dp, &ev, 0, 0);
481 // snd_seq_oss_midi_close(dp, dev);
482 snd_use_lock_free(&mdev->use_lock);
487 * get client/port of the specified MIDI device
490 snd_seq_oss_midi_get_addr(struct seq_oss_devinfo *dp, int dev, struct snd_seq_addr *addr)
492 struct seq_oss_midi *mdev;
494 if ((mdev = get_mididev(dp, dev)) == NULL)
496 addr->client = mdev->client;
497 addr->port = mdev->port;
498 snd_use_lock_free(&mdev->use_lock);
503 * input callback - this can be atomic
506 snd_seq_oss_midi_input(struct snd_seq_event *ev, int direct, void *private_data)
508 struct seq_oss_devinfo *dp = (struct seq_oss_devinfo *)private_data;
509 struct seq_oss_midi *mdev;
512 if (dp->readq == NULL)
514 if ((mdev = find_slot(ev->source.client, ev->source.port)) == NULL)
516 if (! (mdev->opened & PERM_READ)) {
517 snd_use_lock_free(&mdev->use_lock);
521 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
522 rc = send_synth_event(dp, ev, mdev->seq_device);
524 rc = send_midi_event(dp, ev, mdev);
526 snd_use_lock_free(&mdev->use_lock);
531 * convert ALSA sequencer event to OSS synth event
534 send_synth_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, int dev)
538 memset(&ossev, 0, sizeof(ossev));
541 case SNDRV_SEQ_EVENT_NOTEON:
542 ossev.v.cmd = MIDI_NOTEON; break;
543 case SNDRV_SEQ_EVENT_NOTEOFF:
544 ossev.v.cmd = MIDI_NOTEOFF; break;
545 case SNDRV_SEQ_EVENT_KEYPRESS:
546 ossev.v.cmd = MIDI_KEY_PRESSURE; break;
547 case SNDRV_SEQ_EVENT_CONTROLLER:
548 ossev.l.cmd = MIDI_CTL_CHANGE; break;
549 case SNDRV_SEQ_EVENT_PGMCHANGE:
550 ossev.l.cmd = MIDI_PGM_CHANGE; break;
551 case SNDRV_SEQ_EVENT_CHANPRESS:
552 ossev.l.cmd = MIDI_CHN_PRESSURE; break;
553 case SNDRV_SEQ_EVENT_PITCHBEND:
554 ossev.l.cmd = MIDI_PITCH_BEND; break;
556 return 0; /* not supported */
562 case SNDRV_SEQ_EVENT_NOTEON:
563 case SNDRV_SEQ_EVENT_NOTEOFF:
564 case SNDRV_SEQ_EVENT_KEYPRESS:
565 ossev.v.code = EV_CHN_VOICE;
566 ossev.v.note = ev->data.note.note;
567 ossev.v.parm = ev->data.note.velocity;
568 ossev.v.chn = ev->data.note.channel;
570 case SNDRV_SEQ_EVENT_CONTROLLER:
571 case SNDRV_SEQ_EVENT_PGMCHANGE:
572 case SNDRV_SEQ_EVENT_CHANPRESS:
573 ossev.l.code = EV_CHN_COMMON;
574 ossev.l.p1 = ev->data.control.param;
575 ossev.l.val = ev->data.control.value;
576 ossev.l.chn = ev->data.control.channel;
578 case SNDRV_SEQ_EVENT_PITCHBEND:
579 ossev.l.code = EV_CHN_COMMON;
580 ossev.l.val = ev->data.control.value + 8192;
581 ossev.l.chn = ev->data.control.channel;
585 snd_seq_oss_readq_put_timestamp(dp->readq, ev->time.tick, dp->seq_mode);
586 snd_seq_oss_readq_put_event(dp->readq, &ossev);
592 * decode event and send MIDI bytes to read queue
595 send_midi_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, struct seq_oss_midi *mdev)
600 snd_seq_oss_readq_put_timestamp(dp->readq, ev->time.tick, dp->seq_mode);
601 if (!dp->timer->running)
602 len = snd_seq_oss_timer_start(dp->timer);
603 if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {
604 snd_seq_oss_readq_sysex(dp->readq, mdev->seq_device, ev);
606 len = snd_midi_event_decode(mdev->coder, msg, sizeof(msg), ev);
608 snd_seq_oss_readq_puts(dp->readq, mdev->seq_device, msg, len);
617 * return 0 : enqueued
618 * non-zero : invalid - ignored
621 snd_seq_oss_midi_putc(struct seq_oss_devinfo *dp, int dev, unsigned char c, struct snd_seq_event *ev)
623 struct seq_oss_midi *mdev;
625 if ((mdev = get_mididev(dp, dev)) == NULL)
627 if (snd_midi_event_encode_byte(mdev->coder, c, ev)) {
628 snd_seq_oss_fill_addr(dp, ev, mdev->client, mdev->port);
629 snd_use_lock_free(&mdev->use_lock);
632 snd_use_lock_free(&mdev->use_lock);
637 * create OSS compatible midi_info record
640 snd_seq_oss_midi_make_info(struct seq_oss_devinfo *dp, int dev, struct midi_info *inf)
642 struct seq_oss_midi *mdev;
644 if ((mdev = get_mididev(dp, dev)) == NULL)
647 inf->dev_type = 0; /* FIXME: ?? */
648 inf->capabilities = 0; /* FIXME: ?? */
649 strlcpy(inf->name, mdev->name, sizeof(inf->name));
650 snd_use_lock_free(&mdev->use_lock);
655 #ifdef CONFIG_SND_PROC_FS
662 val &= PERM_READ|PERM_WRITE;
663 if (val == (PERM_READ|PERM_WRITE))
665 else if (val == PERM_READ)
667 else if (val == PERM_WRITE)
674 snd_seq_oss_midi_info_read(struct snd_info_buffer *buf)
677 struct seq_oss_midi *mdev;
679 snd_iprintf(buf, "\nNumber of MIDI devices: %d\n", max_midi_devs);
680 for (i = 0; i < max_midi_devs; i++) {
681 snd_iprintf(buf, "\nmidi %d: ", i);
684 snd_iprintf(buf, "*empty*\n");
687 snd_iprintf(buf, "[%s] ALSA port %d:%d\n", mdev->name,
688 mdev->client, mdev->port);
689 snd_iprintf(buf, " capability %s / opened %s\n",
690 capmode_str(mdev->flags),
691 capmode_str(mdev->opened));
692 snd_use_lock_free(&mdev->use_lock);
695 #endif /* CONFIG_SND_PROC_FS */