ARM: dts: Kirkwood: Fix QNAP TS219 power-off
[linux/fpc-iii.git] / sound / core / seq / oss / seq_oss_midi.c
blobaaff9ee326957b5a393495126e128b9f6b5efd98
1 /*
2 * OSS compatible sequencer driver
4 * MIDI 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 <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>
35 * constants
37 #define SNDRV_SEQ_OSS_MAX_MIDI_NAME 30
40 * definition of midi device record
42 struct seq_oss_midi {
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;
56 * midi device table
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);
64 * prototypes
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.
75 int
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) {
84 kfree(clinfo);
85 kfree(pinfo);
86 return -ENOMEM;
88 clinfo->client = -1;
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);
97 kfree(clinfo);
98 kfree(pinfo);
99 return 0;
105 static struct seq_oss_midi *
106 get_mdev(int dev)
108 struct seq_oss_midi *mdev;
109 unsigned long flags;
111 spin_lock_irqsave(&register_lock, flags);
112 mdev = midi_devs[dev];
113 if (mdev)
114 snd_use_lock_use(&mdev->use_lock);
115 spin_unlock_irqrestore(&register_lock, flags);
116 return mdev;
120 * look for the identical slot
122 static struct seq_oss_midi *
123 find_slot(int client, int port)
125 int i;
126 struct seq_oss_midi *mdev;
127 unsigned long flags;
129 spin_lock_irqsave(&register_lock, flags);
130 for (i = 0; i < max_midi_devs; i++) {
131 mdev = midi_devs[i];
132 if (mdev && mdev->client == client && mdev->port == port) {
133 /* found! */
134 snd_use_lock_use(&mdev->use_lock);
135 spin_unlock_irqrestore(&register_lock, flags);
136 return mdev;
139 spin_unlock_irqrestore(&register_lock, flags);
140 return NULL;
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)
152 int i;
153 struct seq_oss_midi *mdev;
154 unsigned long flags;
156 /* the port must include generic midi */
157 if (! (pinfo->type & SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC))
158 return 0;
159 /* either read or write subscribable */
160 if ((pinfo->capability & PERM_WRITE) != PERM_WRITE &&
161 (pinfo->capability & PERM_READ) != PERM_READ)
162 return 0;
165 * look for the identical slot
167 if ((mdev = find_slot(pinfo->addr.client, pinfo->addr.port)) != NULL) {
168 /* already exists */
169 snd_use_lock_free(&mdev->use_lock);
170 return 0;
174 * allocate midi info record
176 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
177 if (!mdev)
178 return -ENOMEM;
180 /* copy the port information */
181 mdev->client = pinfo->addr.client;
182 mdev->port = pinfo->addr.port;
183 mdev->flags = pinfo->capability;
184 mdev->opened = 0;
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");
193 kfree(mdev);
194 return -ENOMEM;
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(&register_lock, flags);
203 for (i = 0; i < max_midi_devs; i++) {
204 if (midi_devs[i] == NULL)
205 break;
207 if (i >= max_midi_devs) {
208 if (max_midi_devs >= SNDRV_SEQ_OSS_MAX_MIDI_DEVS) {
209 spin_unlock_irqrestore(&register_lock, flags);
210 snd_midi_event_free(mdev->coder);
211 kfree(mdev);
212 return -ENOMEM;
214 max_midi_devs++;
216 mdev->seq_device = i;
217 midi_devs[mdev->seq_device] = mdev;
218 spin_unlock_irqrestore(&register_lock, flags);
220 return 0;
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;
230 unsigned long flags;
231 int index;
233 if ((mdev = find_slot(client, port)) != NULL) {
234 spin_lock_irqsave(&register_lock, flags);
235 midi_devs[mdev->seq_device] = NULL;
236 spin_unlock_irqrestore(&register_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);
240 kfree(mdev);
242 spin_lock_irqsave(&register_lock, flags);
243 for (index = max_midi_devs - 1; index >= 0; index--) {
244 if (midi_devs[index])
245 break;
247 max_midi_devs = index + 1;
248 spin_unlock_irqrestore(&register_lock, flags);
249 return 0;
254 * release the midi device if it was registered
256 void
257 snd_seq_oss_midi_clear_all(void)
259 int i;
260 struct seq_oss_midi *mdev;
261 unsigned long flags;
263 spin_lock_irqsave(&register_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);
267 kfree(mdev);
268 midi_devs[i] = NULL;
271 max_midi_devs = 0;
272 spin_unlock_irqrestore(&register_lock, flags);
277 * set up midi tables
279 void
280 snd_seq_oss_midi_setup(struct seq_oss_devinfo *dp)
282 dp->max_mididev = max_midi_devs;
286 * clean up midi tables
288 void
289 snd_seq_oss_midi_cleanup(struct seq_oss_devinfo *dp)
291 int i;
292 for (i = 0; i < dp->max_mididev; i++)
293 snd_seq_oss_midi_close(dp, i);
294 dp->max_mididev = 0;
299 * open all midi devices. ignore errors.
301 void
302 snd_seq_oss_midi_open_all(struct seq_oss_devinfo *dp, int file_mode)
304 int i;
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)
317 return NULL;
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)
328 int perm;
329 struct seq_oss_midi *mdev;
330 struct snd_seq_port_subscribe subs;
332 if ((mdev = get_mididev(dp, dev)) == NULL)
333 return -ENODEV;
335 /* already used? */
336 if (mdev->opened && mdev->devinfo != dp) {
337 snd_use_lock_free(&mdev->use_lock);
338 return -EBUSY;
341 perm = 0;
342 if (is_write_mode(fmode))
343 perm |= PERM_WRITE;
344 if (is_read_mode(fmode))
345 perm |= PERM_READ;
346 perm &= mdev->flags;
347 if (perm == 0) {
348 snd_use_lock_free(&mdev->use_lock);
349 return -ENXIO;
352 /* already opened? */
353 if ((mdev->opened & perm) == perm) {
354 snd_use_lock_free(&mdev->use_lock);
355 return 0;
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);
381 return -ENXIO;
384 mdev->devinfo = dp;
385 snd_use_lock_free(&mdev->use_lock);
386 return 0;
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)
399 return -ENODEV;
400 if (! mdev->opened || mdev->devinfo != dp) {
401 snd_use_lock_free(&mdev->use_lock);
402 return 0;
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);
419 mdev->opened = 0;
420 mdev->devinfo = NULL;
422 snd_use_lock_free(&mdev->use_lock);
423 return 0;
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;
433 int mode;
435 if ((mdev = get_mididev(dp, dev)) == NULL)
436 return 0;
438 mode = 0;
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);
445 return mode;
449 * reset the midi device and close it:
450 * so far, only close the device.
452 void
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)
458 return;
459 if (! mdev->opened) {
460 snd_use_lock_free(&mdev->use_lock);
461 return;
464 if (mdev->opened & PERM_WRITE) {
465 struct snd_seq_event ev;
466 int c;
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
500 void
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)
506 return;
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;
521 int rc;
523 if (dp->readq == NULL)
524 return 0;
525 if ((mdev = find_slot(ev->source.client, ev->source.port)) == NULL)
526 return 0;
527 if (! (mdev->opened & PERM_READ)) {
528 snd_use_lock_free(&mdev->use_lock);
529 return 0;
532 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
533 rc = send_synth_event(dp, ev, mdev->seq_device);
534 else
535 rc = send_midi_event(dp, ev, mdev);
537 snd_use_lock_free(&mdev->use_lock);
538 return rc;
542 * convert ALSA sequencer event to OSS synth event
544 static int
545 send_synth_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, int dev)
547 union evrec ossev;
549 memset(&ossev, 0, sizeof(ossev));
551 switch (ev->type) {
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;
566 default:
567 return 0; /* not supported */
570 ossev.v.dev = dev;
572 switch (ev->type) {
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;
580 break;
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;
588 break;
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;
593 break;
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);
599 return 0;
603 * decode event and send MIDI bytes to read queue
605 static int
606 send_midi_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, struct seq_oss_midi *mdev)
608 char msg[32];
609 int len;
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);
618 } else {
619 len = snd_midi_event_decode(mdev->coder, msg, sizeof(msg), ev);
620 if (len > 0)
621 snd_seq_oss_readq_puts(dp->readq, mdev->seq_device, msg, len);
624 return 0;
629 * dump midi data
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)
639 return -ENODEV;
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);
643 return 0;
645 snd_use_lock_free(&mdev->use_lock);
646 return -EINVAL;
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)
658 return -ENXIO;
659 inf->device = dev;
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);
664 return 0;
668 #ifdef CONFIG_SND_PROC_FS
670 * proc interface
672 static char *
673 capmode_str(int val)
675 val &= PERM_READ|PERM_WRITE;
676 if (val == (PERM_READ|PERM_WRITE))
677 return "read/write";
678 else if (val == PERM_READ)
679 return "read";
680 else if (val == PERM_WRITE)
681 return "write";
682 else
683 return "none";
686 void
687 snd_seq_oss_midi_info_read(struct snd_info_buffer *buf)
689 int i;
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);
695 mdev = get_mdev(i);
696 if (mdev == NULL) {
697 snd_iprintf(buf, "*empty*\n");
698 continue;
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 */