spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / sound / core / seq / oss / seq_oss_midi.c
blob677dc84590c79ae49dca0fb569fe4c98a44c068c
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 __init
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 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))
159 return 0;
160 /* either read or write subscribable */
161 if ((pinfo->capability & PERM_WRITE) != PERM_WRITE &&
162 (pinfo->capability & PERM_READ) != PERM_READ)
163 return 0;
166 * look for the identical slot
168 if ((mdev = find_slot(pinfo->addr.client, pinfo->addr.port)) != NULL) {
169 /* already exists */
170 snd_use_lock_free(&mdev->use_lock);
171 return 0;
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");
179 return -ENOMEM;
182 /* copy the port information */
183 mdev->client = pinfo->addr.client;
184 mdev->port = pinfo->addr.port;
185 mdev->flags = pinfo->capability;
186 mdev->opened = 0;
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");
195 kfree(mdev);
196 return -ENOMEM;
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(&register_lock, flags);
205 for (i = 0; i < max_midi_devs; i++) {
206 if (midi_devs[i] == NULL)
207 break;
209 if (i >= max_midi_devs) {
210 if (max_midi_devs >= SNDRV_SEQ_OSS_MAX_MIDI_DEVS) {
211 spin_unlock_irqrestore(&register_lock, flags);
212 snd_midi_event_free(mdev->coder);
213 kfree(mdev);
214 return -ENOMEM;
216 max_midi_devs++;
218 mdev->seq_device = i;
219 midi_devs[mdev->seq_device] = mdev;
220 spin_unlock_irqrestore(&register_lock, flags);
222 return 0;
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;
232 unsigned long flags;
233 int index;
235 if ((mdev = find_slot(client, port)) != NULL) {
236 spin_lock_irqsave(&register_lock, flags);
237 midi_devs[mdev->seq_device] = NULL;
238 spin_unlock_irqrestore(&register_lock, flags);
239 snd_use_lock_free(&mdev->use_lock);
240 snd_use_lock_sync(&mdev->use_lock);
241 if (mdev->coder)
242 snd_midi_event_free(mdev->coder);
243 kfree(mdev);
245 spin_lock_irqsave(&register_lock, flags);
246 for (index = max_midi_devs - 1; index >= 0; index--) {
247 if (midi_devs[index])
248 break;
250 max_midi_devs = index + 1;
251 spin_unlock_irqrestore(&register_lock, flags);
252 return 0;
257 * release the midi device if it was registered
259 void
260 snd_seq_oss_midi_clear_all(void)
262 int i;
263 struct seq_oss_midi *mdev;
264 unsigned long flags;
266 spin_lock_irqsave(&register_lock, flags);
267 for (i = 0; i < max_midi_devs; i++) {
268 if ((mdev = midi_devs[i]) != NULL) {
269 if (mdev->coder)
270 snd_midi_event_free(mdev->coder);
271 kfree(mdev);
272 midi_devs[i] = NULL;
275 max_midi_devs = 0;
276 spin_unlock_irqrestore(&register_lock, flags);
281 * set up midi tables
283 void
284 snd_seq_oss_midi_setup(struct seq_oss_devinfo *dp)
286 dp->max_mididev = max_midi_devs;
290 * clean up midi tables
292 void
293 snd_seq_oss_midi_cleanup(struct seq_oss_devinfo *dp)
295 int i;
296 for (i = 0; i < dp->max_mididev; i++)
297 snd_seq_oss_midi_close(dp, i);
298 dp->max_mididev = 0;
303 * open all midi devices. ignore errors.
305 void
306 snd_seq_oss_midi_open_all(struct seq_oss_devinfo *dp, int file_mode)
308 int i;
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)
321 return NULL;
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)
332 int perm;
333 struct seq_oss_midi *mdev;
334 struct snd_seq_port_subscribe subs;
336 if ((mdev = get_mididev(dp, dev)) == NULL)
337 return -ENODEV;
339 /* already used? */
340 if (mdev->opened && mdev->devinfo != dp) {
341 snd_use_lock_free(&mdev->use_lock);
342 return -EBUSY;
345 perm = 0;
346 if (is_write_mode(fmode))
347 perm |= PERM_WRITE;
348 if (is_read_mode(fmode))
349 perm |= PERM_READ;
350 perm &= mdev->flags;
351 if (perm == 0) {
352 snd_use_lock_free(&mdev->use_lock);
353 return -ENXIO;
356 /* already opened? */
357 if ((mdev->opened & perm) == perm) {
358 snd_use_lock_free(&mdev->use_lock);
359 return 0;
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);
385 return -ENXIO;
388 mdev->devinfo = dp;
389 snd_use_lock_free(&mdev->use_lock);
390 return 0;
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)
403 return -ENODEV;
404 if (! mdev->opened || mdev->devinfo != dp) {
405 snd_use_lock_free(&mdev->use_lock);
406 return 0;
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);
424 mdev->opened = 0;
425 mdev->devinfo = NULL;
427 snd_use_lock_free(&mdev->use_lock);
428 return 0;
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;
438 int mode;
440 if ((mdev = get_mididev(dp, dev)) == NULL)
441 return 0;
443 mode = 0;
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);
450 return mode;
454 * reset the midi device and close it:
455 * so far, only close the device.
457 void
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)
463 return;
464 if (! mdev->opened) {
465 snd_use_lock_free(&mdev->use_lock);
466 return;
469 if (mdev->opened & PERM_WRITE) {
470 struct snd_seq_event ev;
471 int c;
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
506 void
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)
512 return;
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;
527 int rc;
529 if (dp->readq == NULL)
530 return 0;
531 if ((mdev = find_slot(ev->source.client, ev->source.port)) == NULL)
532 return 0;
533 if (! (mdev->opened & PERM_READ)) {
534 snd_use_lock_free(&mdev->use_lock);
535 return 0;
538 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
539 rc = send_synth_event(dp, ev, mdev->seq_device);
540 else
541 rc = send_midi_event(dp, ev, mdev);
543 snd_use_lock_free(&mdev->use_lock);
544 return rc;
548 * convert ALSA sequencer event to OSS synth event
550 static int
551 send_synth_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, int dev)
553 union evrec ossev;
555 memset(&ossev, 0, sizeof(ossev));
557 switch (ev->type) {
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;
572 default:
573 return 0; /* not supported */
576 ossev.v.dev = dev;
578 switch (ev->type) {
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;
586 break;
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;
594 break;
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;
599 break;
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);
605 return 0;
609 * decode event and send MIDI bytes to read queue
611 static int
612 send_midi_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, struct seq_oss_midi *mdev)
614 char msg[32];
615 int len;
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);
624 } else {
625 len = snd_midi_event_decode(mdev->coder, msg, sizeof(msg), ev);
626 if (len > 0)
627 snd_seq_oss_readq_puts(dp->readq, mdev->seq_device, msg, len);
630 return 0;
635 * dump midi data
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)
645 return -ENODEV;
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);
649 return 0;
651 snd_use_lock_free(&mdev->use_lock);
652 return -EINVAL;
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)
664 return -ENXIO;
665 inf->device = dev;
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);
670 return 0;
674 #ifdef CONFIG_PROC_FS
676 * proc interface
678 static char *
679 capmode_str(int val)
681 val &= PERM_READ|PERM_WRITE;
682 if (val == (PERM_READ|PERM_WRITE))
683 return "read/write";
684 else if (val == PERM_READ)
685 return "read";
686 else if (val == PERM_WRITE)
687 return "write";
688 else
689 return "none";
692 void
693 snd_seq_oss_midi_info_read(struct snd_info_buffer *buf)
695 int i;
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);
701 mdev = get_mdev(i);
702 if (mdev == NULL) {
703 snd_iprintf(buf, "*empty*\n");
704 continue;
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 */