1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OSS compatible sequencer driver
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 mdev = find_slot(pinfo->addr.client, pinfo->addr.port);
158 snd_use_lock_free(&mdev->use_lock);
163 * allocate midi info record
165 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
169 /* copy the port information */
170 mdev->client = pinfo->addr.client;
171 mdev->port = pinfo->addr.port;
172 mdev->flags = pinfo->capability;
174 snd_use_lock_init(&mdev->use_lock);
176 /* copy and truncate the name of synth device */
177 strscpy(mdev->name, pinfo->name, sizeof(mdev->name));
179 /* create MIDI coder */
180 if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &mdev->coder) < 0) {
181 pr_err("ALSA: seq_oss: can't malloc midi coder\n");
185 /* OSS sequencer adds running status to all sequences */
186 snd_midi_event_no_status(mdev->coder, 1);
189 * look for en empty slot
191 spin_lock_irqsave(®ister_lock, flags);
192 for (i = 0; i < max_midi_devs; i++) {
193 if (midi_devs[i] == NULL)
196 if (i >= max_midi_devs) {
197 if (max_midi_devs >= SNDRV_SEQ_OSS_MAX_MIDI_DEVS) {
198 spin_unlock_irqrestore(®ister_lock, flags);
199 snd_midi_event_free(mdev->coder);
205 mdev->seq_device = i;
206 midi_devs[mdev->seq_device] = mdev;
207 spin_unlock_irqrestore(®ister_lock, flags);
213 * release the midi device if it was registered
216 snd_seq_oss_midi_check_exit_port(int client, int port)
218 struct seq_oss_midi *mdev;
222 mdev = find_slot(client, port);
224 spin_lock_irqsave(®ister_lock, flags);
225 midi_devs[mdev->seq_device] = NULL;
226 spin_unlock_irqrestore(®ister_lock, flags);
227 snd_use_lock_free(&mdev->use_lock);
228 snd_use_lock_sync(&mdev->use_lock);
229 snd_midi_event_free(mdev->coder);
232 spin_lock_irqsave(®ister_lock, flags);
233 for (index = max_midi_devs - 1; index >= 0; index--) {
234 if (midi_devs[index])
237 max_midi_devs = index + 1;
238 spin_unlock_irqrestore(®ister_lock, flags);
244 * release the midi device if it was registered
247 snd_seq_oss_midi_clear_all(void)
250 struct seq_oss_midi *mdev;
253 spin_lock_irqsave(®ister_lock, flags);
254 for (i = 0; i < max_midi_devs; i++) {
257 snd_midi_event_free(mdev->coder);
263 spin_unlock_irqrestore(®ister_lock, flags);
271 snd_seq_oss_midi_setup(struct seq_oss_devinfo *dp)
273 spin_lock_irq(®ister_lock);
274 dp->max_mididev = max_midi_devs;
275 spin_unlock_irq(®ister_lock);
279 * clean up midi tables
282 snd_seq_oss_midi_cleanup(struct seq_oss_devinfo *dp)
285 for (i = 0; i < dp->max_mididev; i++)
286 snd_seq_oss_midi_close(dp, i);
292 * open all midi devices. ignore errors.
295 snd_seq_oss_midi_open_all(struct seq_oss_devinfo *dp, int file_mode)
298 for (i = 0; i < dp->max_mididev; i++)
299 snd_seq_oss_midi_open(dp, i, file_mode);
304 * get the midi device information
306 static struct seq_oss_midi *
307 get_mididev(struct seq_oss_devinfo *dp, int dev)
309 if (dev < 0 || dev >= dp->max_mididev)
311 dev = array_index_nospec(dev, dp->max_mididev);
312 return get_mdev(dev);
317 * open the midi device if not opened yet
320 snd_seq_oss_midi_open(struct seq_oss_devinfo *dp, int dev, int fmode)
323 struct seq_oss_midi *mdev;
324 struct snd_seq_port_subscribe subs;
326 mdev = get_mididev(dp, dev);
331 if (mdev->opened && mdev->devinfo != dp) {
332 snd_use_lock_free(&mdev->use_lock);
337 if (is_write_mode(fmode))
339 if (is_read_mode(fmode))
343 snd_use_lock_free(&mdev->use_lock);
347 /* already opened? */
348 if ((mdev->opened & perm) == perm) {
349 snd_use_lock_free(&mdev->use_lock);
353 perm &= ~mdev->opened;
355 memset(&subs, 0, sizeof(subs));
357 if (perm & PERM_WRITE) {
358 subs.sender = dp->addr;
359 subs.dest.client = mdev->client;
360 subs.dest.port = mdev->port;
361 if (snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs) >= 0)
362 mdev->opened |= PERM_WRITE;
364 if (perm & PERM_READ) {
365 subs.sender.client = mdev->client;
366 subs.sender.port = mdev->port;
367 subs.dest = dp->addr;
368 subs.flags = SNDRV_SEQ_PORT_SUBS_TIMESTAMP;
369 subs.queue = dp->queue; /* queue for timestamps */
370 if (snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs) >= 0)
371 mdev->opened |= PERM_READ;
374 if (! mdev->opened) {
375 snd_use_lock_free(&mdev->use_lock);
380 snd_use_lock_free(&mdev->use_lock);
385 * close the midi device if already opened
388 snd_seq_oss_midi_close(struct seq_oss_devinfo *dp, int dev)
390 struct seq_oss_midi *mdev;
391 struct snd_seq_port_subscribe subs;
393 mdev = get_mididev(dp, dev);
396 if (! mdev->opened || mdev->devinfo != dp) {
397 snd_use_lock_free(&mdev->use_lock);
401 memset(&subs, 0, sizeof(subs));
402 if (mdev->opened & PERM_WRITE) {
403 subs.sender = dp->addr;
404 subs.dest.client = mdev->client;
405 subs.dest.port = mdev->port;
406 snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, &subs);
408 if (mdev->opened & PERM_READ) {
409 subs.sender.client = mdev->client;
410 subs.sender.port = mdev->port;
411 subs.dest = dp->addr;
412 snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, &subs);
416 mdev->devinfo = NULL;
418 snd_use_lock_free(&mdev->use_lock);
423 * change seq capability flags to file mode flags
426 snd_seq_oss_midi_filemode(struct seq_oss_devinfo *dp, int dev)
428 struct seq_oss_midi *mdev;
431 mdev = get_mididev(dp, dev);
436 if (mdev->opened & PERM_WRITE)
437 mode |= SNDRV_SEQ_OSS_FILE_WRITE;
438 if (mdev->opened & PERM_READ)
439 mode |= SNDRV_SEQ_OSS_FILE_READ;
441 snd_use_lock_free(&mdev->use_lock);
446 * reset the midi device and close it:
447 * so far, only close the device.
450 snd_seq_oss_midi_reset(struct seq_oss_devinfo *dp, int dev)
452 struct seq_oss_midi *mdev;
454 mdev = get_mididev(dp, dev);
457 if (! mdev->opened) {
458 snd_use_lock_free(&mdev->use_lock);
462 if (mdev->opened & PERM_WRITE) {
463 struct snd_seq_event ev;
466 memset(&ev, 0, sizeof(ev));
467 ev.dest.client = mdev->client;
468 ev.dest.port = mdev->port;
469 ev.queue = dp->queue;
470 ev.source.port = dp->port;
471 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH) {
472 ev.type = SNDRV_SEQ_EVENT_SENSING;
473 snd_seq_oss_dispatch(dp, &ev, 0, 0);
475 for (c = 0; c < 16; c++) {
476 ev.type = SNDRV_SEQ_EVENT_CONTROLLER;
477 ev.data.control.channel = c;
478 ev.data.control.param = MIDI_CTL_ALL_NOTES_OFF;
479 snd_seq_oss_dispatch(dp, &ev, 0, 0);
480 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) {
481 ev.data.control.param =
482 MIDI_CTL_RESET_CONTROLLERS;
483 snd_seq_oss_dispatch(dp, &ev, 0, 0);
484 ev.type = SNDRV_SEQ_EVENT_PITCHBEND;
485 ev.data.control.value = 0;
486 snd_seq_oss_dispatch(dp, &ev, 0, 0);
490 // snd_seq_oss_midi_close(dp, dev);
491 snd_use_lock_free(&mdev->use_lock);
496 * get client/port of the specified MIDI device
499 snd_seq_oss_midi_get_addr(struct seq_oss_devinfo *dp, int dev, struct snd_seq_addr *addr)
501 struct seq_oss_midi *mdev;
503 mdev = get_mididev(dp, dev);
506 addr->client = mdev->client;
507 addr->port = mdev->port;
508 snd_use_lock_free(&mdev->use_lock);
513 * input callback - this can be atomic
516 snd_seq_oss_midi_input(struct snd_seq_event *ev, int direct, void *private_data)
518 struct seq_oss_devinfo *dp = (struct seq_oss_devinfo *)private_data;
519 struct seq_oss_midi *mdev;
522 if (dp->readq == NULL)
524 mdev = find_slot(ev->source.client, ev->source.port);
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 snd_seq_oss_readq_sysex(dp->readq, mdev->seq_device, ev);
616 snd_midi_event_reset_decode(mdev->coder);
618 len = snd_midi_event_decode(mdev->coder, msg, sizeof(msg), ev);
620 snd_seq_oss_readq_puts(dp->readq, mdev->seq_device, msg, len);
629 * return 0 : enqueued
630 * non-zero : invalid - ignored
633 snd_seq_oss_midi_putc(struct seq_oss_devinfo *dp, int dev, unsigned char c, struct snd_seq_event *ev)
635 struct seq_oss_midi *mdev;
637 mdev = get_mididev(dp, dev);
640 if (snd_midi_event_encode_byte(mdev->coder, c, ev)) {
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 mdev = get_mididev(dp, dev);
661 inf->dev_type = 0; /* FIXME: ?? */
662 inf->capabilities = 0; /* FIXME: ?? */
663 strscpy(inf->name, mdev->name, sizeof(inf->name));
664 snd_use_lock_free(&mdev->use_lock);
669 #ifdef CONFIG_SND_PROC_FS
676 val &= PERM_READ|PERM_WRITE;
677 if (val == (PERM_READ|PERM_WRITE))
679 else if (val == PERM_READ)
681 else if (val == PERM_WRITE)
688 snd_seq_oss_midi_info_read(struct snd_info_buffer *buf)
691 struct seq_oss_midi *mdev;
693 snd_iprintf(buf, "\nNumber of MIDI devices: %d\n", max_midi_devs);
694 for (i = 0; i < max_midi_devs; i++) {
695 snd_iprintf(buf, "\nmidi %d: ", i);
698 snd_iprintf(buf, "*empty*\n");
701 snd_iprintf(buf, "[%s] ALSA port %d:%d\n", mdev->name,
702 mdev->client, mdev->port);
703 snd_iprintf(buf, " capability %s / opened %s\n",
704 capmode_str(mdev->flags),
705 capmode_str(mdev->opened));
706 snd_use_lock_free(&mdev->use_lock);
709 #endif /* CONFIG_SND_PROC_FS */