Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / sound / core / seq / oss / seq_oss_readq.c
blobf5de79f29f1e1a2aae91ede5d1bec3a65675fbdd
1 /*
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"
27 #include <linux/wait.h>
30 * constants
32 //#define SNDRV_SEQ_OSS_MAX_TIMEOUT (unsigned long)(-1)
33 #define SNDRV_SEQ_OSS_MAX_TIMEOUT (HZ * 3600)
37 * prototypes
42 * create a read queue
44 struct seq_oss_readq *
45 snd_seq_oss_readq_new(struct seq_oss_devinfo *dp, int maxlen)
47 struct seq_oss_readq *q;
49 if ((q = kzalloc(sizeof(*q), GFP_KERNEL)) == NULL) {
50 snd_printk(KERN_ERR "can't malloc read queue\n");
51 return NULL;
54 if ((q->q = kcalloc(maxlen, sizeof(union evrec), GFP_KERNEL)) == NULL) {
55 snd_printk(KERN_ERR "can't malloc read queue buffer\n");
56 kfree(q);
57 return NULL;
60 q->maxlen = maxlen;
61 q->qlen = 0;
62 q->head = q->tail = 0;
63 init_waitqueue_head(&q->midi_sleep);
64 spin_lock_init(&q->lock);
65 q->pre_event_timeout = SNDRV_SEQ_OSS_MAX_TIMEOUT;
66 q->input_time = (unsigned long)-1;
68 return q;
72 * delete the read queue
74 void
75 snd_seq_oss_readq_delete(struct seq_oss_readq *q)
77 if (q) {
78 kfree(q->q);
79 kfree(q);
84 * reset the read queue
86 void
87 snd_seq_oss_readq_clear(struct seq_oss_readq *q)
89 if (q->qlen) {
90 q->qlen = 0;
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;
100 * put a midi byte
103 snd_seq_oss_readq_puts(struct seq_oss_readq *q, int dev, unsigned char *data, int len)
105 union evrec rec;
106 int result;
108 memset(&rec, 0, sizeof(rec));
109 rec.c[0] = SEQ_MIDIPUTC;
110 rec.c[2] = dev;
112 while (len-- > 0) {
113 rec.c[1] = *data++;
114 result = snd_seq_oss_readq_put_event(q, &rec);
115 if (result < 0)
116 return result;
118 return 0;
122 * copy an event to input queue:
123 * return zero if enqueued
126 snd_seq_oss_readq_put_event(struct seq_oss_readq *q, union evrec *ev)
128 unsigned long flags;
130 spin_lock_irqsave(&q->lock, flags);
131 if (q->qlen >= q->maxlen - 1) {
132 spin_unlock_irqrestore(&q->lock, flags);
133 return -ENOMEM;
136 memcpy(&q->q[q->tail], ev, sizeof(*ev));
137 q->tail = (q->tail + 1) % q->maxlen;
138 q->qlen++;
140 /* wake up sleeper */
141 if (waitqueue_active(&q->midi_sleep))
142 wake_up(&q->midi_sleep);
144 spin_unlock_irqrestore(&q->lock, flags);
146 return 0;
151 * pop queue
152 * caller must hold lock
155 snd_seq_oss_readq_pick(struct seq_oss_readq *q, union evrec *rec)
157 if (q->qlen == 0)
158 return -EAGAIN;
159 memcpy(rec, &q->q[q->head], sizeof(*rec));
160 return 0;
164 * sleep until ready
166 void
167 snd_seq_oss_readq_wait(struct seq_oss_readq *q)
169 wait_event_interruptible_timeout(q->midi_sleep,
170 (q->qlen > 0 || q->head == q->tail),
171 q->pre_event_timeout);
175 * drain one record
176 * caller must hold lock
178 void
179 snd_seq_oss_readq_free(struct seq_oss_readq *q)
181 if (q->qlen > 0) {
182 q->head = (q->head + 1) % q->maxlen;
183 q->qlen--;
188 * polling/select:
189 * return non-zero if readq is not empty.
191 unsigned int
192 snd_seq_oss_readq_poll(struct seq_oss_readq *q, struct file *file, poll_table *wait)
194 poll_wait(file, &q->midi_sleep, wait);
195 return q->qlen;
199 * put a timestamp
202 snd_seq_oss_readq_put_timestamp(struct seq_oss_readq *q, unsigned long curt, int seq_mode)
204 if (curt != q->input_time) {
205 union evrec rec;
206 memset(&rec, 0, sizeof(rec));
207 switch (seq_mode) {
208 case SNDRV_SEQ_OSS_MODE_SYNTH:
209 rec.echo = (curt << 8) | SEQ_WAIT;
210 snd_seq_oss_readq_put_event(q, &rec);
211 break;
212 case SNDRV_SEQ_OSS_MODE_MUSIC:
213 rec.t.code = EV_TIMING;
214 rec.t.cmd = TMR_WAIT_ABS;
215 rec.t.time = curt;
216 snd_seq_oss_readq_put_event(q, &rec);
217 break;
219 q->input_time = curt;
221 return 0;
225 #ifdef CONFIG_PROC_FS
227 * proc interface
229 void
230 snd_seq_oss_readq_info_read(struct seq_oss_readq *q, struct snd_info_buffer *buf)
232 snd_iprintf(buf, " read queue [%s] length = %d : tick = %ld\n",
233 (waitqueue_active(&q->midi_sleep) ? "sleeping":"running"),
234 q->qlen, q->input_time);
236 #endif /* CONFIG_PROC_FS */