Linux 4.16.11
[linux/fpc-iii.git] / sound / core / seq / oss / seq_oss_readq.c
blob06b21226b4e7081ed6584a96cddbcd36af966a88
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>
28 #include <linux/slab.h>
31 * constants
33 //#define SNDRV_SEQ_OSS_MAX_TIMEOUT (unsigned long)(-1)
34 #define SNDRV_SEQ_OSS_MAX_TIMEOUT (HZ * 3600)
38 * prototypes
43 * create a read queue
45 struct seq_oss_readq *
46 snd_seq_oss_readq_new(struct seq_oss_devinfo *dp, int maxlen)
48 struct seq_oss_readq *q;
50 q = kzalloc(sizeof(*q), GFP_KERNEL);
51 if (!q)
52 return NULL;
54 q->q = kcalloc(maxlen, sizeof(union evrec), GFP_KERNEL);
55 if (!q->q) {
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 wake_up(&q->midi_sleep);
95 q->input_time = (unsigned long)-1;
99 * put a midi byte
102 snd_seq_oss_readq_puts(struct seq_oss_readq *q, int dev, unsigned char *data, int len)
104 union evrec rec;
105 int result;
107 memset(&rec, 0, sizeof(rec));
108 rec.c[0] = SEQ_MIDIPUTC;
109 rec.c[2] = dev;
111 while (len-- > 0) {
112 rec.c[1] = *data++;
113 result = snd_seq_oss_readq_put_event(q, &rec);
114 if (result < 0)
115 return result;
117 return 0;
121 * put MIDI sysex bytes; the event buffer may be chained, thus it has
122 * to be expanded via snd_seq_dump_var_event().
124 struct readq_sysex_ctx {
125 struct seq_oss_readq *readq;
126 int dev;
129 static int readq_dump_sysex(void *ptr, void *buf, int count)
131 struct readq_sysex_ctx *ctx = ptr;
133 return snd_seq_oss_readq_puts(ctx->readq, ctx->dev, buf, count);
136 int snd_seq_oss_readq_sysex(struct seq_oss_readq *q, int dev,
137 struct snd_seq_event *ev)
139 struct readq_sysex_ctx ctx = {
140 .readq = q,
141 .dev = dev
144 if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
145 return 0;
146 return snd_seq_dump_var_event(ev, readq_dump_sysex, &ctx);
150 * copy an event to input queue:
151 * return zero if enqueued
154 snd_seq_oss_readq_put_event(struct seq_oss_readq *q, union evrec *ev)
156 unsigned long flags;
158 spin_lock_irqsave(&q->lock, flags);
159 if (q->qlen >= q->maxlen - 1) {
160 spin_unlock_irqrestore(&q->lock, flags);
161 return -ENOMEM;
164 memcpy(&q->q[q->tail], ev, sizeof(*ev));
165 q->tail = (q->tail + 1) % q->maxlen;
166 q->qlen++;
168 /* wake up sleeper */
169 wake_up(&q->midi_sleep);
171 spin_unlock_irqrestore(&q->lock, flags);
173 return 0;
178 * pop queue
179 * caller must hold lock
182 snd_seq_oss_readq_pick(struct seq_oss_readq *q, union evrec *rec)
184 if (q->qlen == 0)
185 return -EAGAIN;
186 memcpy(rec, &q->q[q->head], sizeof(*rec));
187 return 0;
191 * sleep until ready
193 void
194 snd_seq_oss_readq_wait(struct seq_oss_readq *q)
196 wait_event_interruptible_timeout(q->midi_sleep,
197 (q->qlen > 0 || q->head == q->tail),
198 q->pre_event_timeout);
202 * drain one record
203 * caller must hold lock
205 void
206 snd_seq_oss_readq_free(struct seq_oss_readq *q)
208 if (q->qlen > 0) {
209 q->head = (q->head + 1) % q->maxlen;
210 q->qlen--;
215 * polling/select:
216 * return non-zero if readq is not empty.
218 unsigned int
219 snd_seq_oss_readq_poll(struct seq_oss_readq *q, struct file *file, poll_table *wait)
221 poll_wait(file, &q->midi_sleep, wait);
222 return q->qlen;
226 * put a timestamp
229 snd_seq_oss_readq_put_timestamp(struct seq_oss_readq *q, unsigned long curt, int seq_mode)
231 if (curt != q->input_time) {
232 union evrec rec;
233 memset(&rec, 0, sizeof(rec));
234 switch (seq_mode) {
235 case SNDRV_SEQ_OSS_MODE_SYNTH:
236 rec.echo = (curt << 8) | SEQ_WAIT;
237 snd_seq_oss_readq_put_event(q, &rec);
238 break;
239 case SNDRV_SEQ_OSS_MODE_MUSIC:
240 rec.t.code = EV_TIMING;
241 rec.t.cmd = TMR_WAIT_ABS;
242 rec.t.time = curt;
243 snd_seq_oss_readq_put_event(q, &rec);
244 break;
246 q->input_time = curt;
248 return 0;
252 #ifdef CONFIG_SND_PROC_FS
254 * proc interface
256 void
257 snd_seq_oss_readq_info_read(struct seq_oss_readq *q, struct snd_info_buffer *buf)
259 snd_iprintf(buf, " read queue [%s] length = %d : tick = %ld\n",
260 (waitqueue_active(&q->midi_sleep) ? "sleeping":"running"),
261 q->qlen, q->input_time);
263 #endif /* CONFIG_SND_PROC_FS */