2 * OSS compatible sequencer driver
4 * seq_oss_readq.c - MIDI input queue
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 "seq_oss_readq.h"
24 #include "seq_oss_event.h"
25 #include <sound/seq_oss_legacy.h>
26 #include "../seq_lock.h"
31 //#define SNDRV_SEQ_OSS_MAX_TIMEOUT (unsigned long)(-1)
32 #define SNDRV_SEQ_OSS_MAX_TIMEOUT (HZ * 3600)
44 snd_seq_oss_readq_new(seq_oss_devinfo_t *dp, int maxlen)
48 if ((q = kcalloc(1, sizeof(*q), GFP_KERNEL)) == NULL) {
49 snd_printk(KERN_ERR "can't malloc read queue\n");
53 if ((q->q = kcalloc(maxlen, sizeof(evrec_t), GFP_KERNEL)) == NULL) {
54 snd_printk(KERN_ERR "can't malloc read queue buffer\n");
61 q->head = q->tail = 0;
62 init_waitqueue_head(&q->midi_sleep);
63 spin_lock_init(&q->lock);
64 q->pre_event_timeout = SNDRV_SEQ_OSS_MAX_TIMEOUT;
65 q->input_time = (unsigned long)-1;
71 * delete the read queue
74 snd_seq_oss_readq_delete(seq_oss_readq_t *q)
84 * reset the read queue
87 snd_seq_oss_readq_clear(seq_oss_readq_t *q)
91 q->head = q->tail = 0;
93 /* if someone sleeping, wake'em up */
94 if (waitqueue_active(&q->midi_sleep))
95 wake_up(&q->midi_sleep);
96 q->input_time = (unsigned long)-1;
103 snd_seq_oss_readq_puts(seq_oss_readq_t *q, int dev, unsigned char *data, int len)
108 memset(&rec, 0, sizeof(rec));
109 rec.c[0] = SEQ_MIDIPUTC;
114 result = snd_seq_oss_readq_put_event(q, &rec);
122 * copy an event to input queue:
123 * return zero if enqueued
126 snd_seq_oss_readq_put_event(seq_oss_readq_t *q, evrec_t *ev)
130 spin_lock_irqsave(&q->lock, flags);
131 if (q->qlen >= q->maxlen - 1) {
132 spin_unlock_irqrestore(&q->lock, flags);
136 memcpy(&q->q[q->tail], ev, sizeof(*ev));
137 q->tail = (q->tail + 1) % q->maxlen;
140 /* wake up sleeper */
141 if (waitqueue_active(&q->midi_sleep))
142 wake_up(&q->midi_sleep);
144 spin_unlock_irqrestore(&q->lock, flags);
152 * caller must hold lock
155 snd_seq_oss_readq_pick(seq_oss_readq_t *q, evrec_t *rec)
159 memcpy(rec, &q->q[q->head], sizeof(*rec));
167 snd_seq_oss_readq_wait(seq_oss_readq_t *q)
169 interruptible_sleep_on_timeout(&q->midi_sleep, q->pre_event_timeout);
174 * caller must hold lock
177 snd_seq_oss_readq_free(seq_oss_readq_t *q)
180 q->head = (q->head + 1) % q->maxlen;
187 * return non-zero if readq is not empty.
190 snd_seq_oss_readq_poll(seq_oss_readq_t *q, struct file *file, poll_table *wait)
192 poll_wait(file, &q->midi_sleep, wait);
200 snd_seq_oss_readq_put_timestamp(seq_oss_readq_t *q, unsigned long curt, int seq_mode)
202 if (curt != q->input_time) {
204 memset(&rec, 0, sizeof(rec));
206 case SNDRV_SEQ_OSS_MODE_SYNTH:
207 rec.echo = (curt << 8) | SEQ_WAIT;
208 snd_seq_oss_readq_put_event(q, &rec);
210 case SNDRV_SEQ_OSS_MODE_MUSIC:
211 rec.t.code = EV_TIMING;
212 rec.t.cmd = TMR_WAIT_ABS;
214 snd_seq_oss_readq_put_event(q, &rec);
217 q->input_time = curt;
227 snd_seq_oss_readq_info_read(seq_oss_readq_t *q, snd_info_buffer_t *buf)
229 snd_iprintf(buf, " read queue [%s] length = %d : tick = %ld\n",
230 (waitqueue_active(&q->midi_sleep) ? "sleeping":"running"),
231 q->qlen, q->input_time);