ALSA: seq: Fix OSS sysex delivery in OSS emulation
[pandora-kernel.git] / sound / core / seq / oss / seq_oss_readq.c
1 /*
2  * OSS compatible sequencer driver
3  *
4  * seq_oss_readq.c - MIDI input queue
5  *
6  * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
7  *
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.
12  *
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.
17  *
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
21  */
22
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>
29
30 /*
31  * constants
32  */
33 //#define SNDRV_SEQ_OSS_MAX_TIMEOUT     (unsigned long)(-1)
34 #define SNDRV_SEQ_OSS_MAX_TIMEOUT       (HZ * 3600)
35
36
37 /*
38  * prototypes
39  */
40
41
42 /*
43  * create a read queue
44  */
45 struct seq_oss_readq *
46 snd_seq_oss_readq_new(struct seq_oss_devinfo *dp, int maxlen)
47 {
48         struct seq_oss_readq *q;
49
50         if ((q = kzalloc(sizeof(*q), GFP_KERNEL)) == NULL) {
51                 snd_printk(KERN_ERR "can't malloc read queue\n");
52                 return NULL;
53         }
54
55         if ((q->q = kcalloc(maxlen, sizeof(union evrec), GFP_KERNEL)) == NULL) {
56                 snd_printk(KERN_ERR "can't malloc read queue buffer\n");
57                 kfree(q);
58                 return NULL;
59         }
60
61         q->maxlen = maxlen;
62         q->qlen = 0;
63         q->head = q->tail = 0;
64         init_waitqueue_head(&q->midi_sleep);
65         spin_lock_init(&q->lock);
66         q->pre_event_timeout = SNDRV_SEQ_OSS_MAX_TIMEOUT;
67         q->input_time = (unsigned long)-1;
68
69         return q;
70 }
71
72 /*
73  * delete the read queue
74  */
75 void
76 snd_seq_oss_readq_delete(struct seq_oss_readq *q)
77 {
78         if (q) {
79                 kfree(q->q);
80                 kfree(q);
81         }
82 }
83
84 /*
85  * reset the read queue
86  */
87 void
88 snd_seq_oss_readq_clear(struct seq_oss_readq *q)
89 {
90         if (q->qlen) {
91                 q->qlen = 0;
92                 q->head = q->tail = 0;
93         }
94         /* if someone sleeping, wake'em up */
95         if (waitqueue_active(&q->midi_sleep))
96                 wake_up(&q->midi_sleep);
97         q->input_time = (unsigned long)-1;
98 }
99
100 /*
101  * put a midi byte
102  */
103 int
104 snd_seq_oss_readq_puts(struct seq_oss_readq *q, int dev, unsigned char *data, int len)
105 {
106         union evrec rec;
107         int result;
108
109         memset(&rec, 0, sizeof(rec));
110         rec.c[0] = SEQ_MIDIPUTC;
111         rec.c[2] = dev;
112
113         while (len-- > 0) {
114                 rec.c[1] = *data++;
115                 result = snd_seq_oss_readq_put_event(q, &rec);
116                 if (result < 0)
117                         return result;
118         }
119         return 0;
120 }
121
122 /*
123  * put MIDI sysex bytes; the event buffer may be chained, thus it has
124  * to be expanded via snd_seq_dump_var_event().
125  */
126 struct readq_sysex_ctx {
127         struct seq_oss_readq *readq;
128         int dev;
129 };
130
131 static int readq_dump_sysex(void *ptr, void *buf, int count)
132 {
133         struct readq_sysex_ctx *ctx = ptr;
134
135         return snd_seq_oss_readq_puts(ctx->readq, ctx->dev, buf, count);
136 }
137
138 int snd_seq_oss_readq_sysex(struct seq_oss_readq *q, int dev,
139                             struct snd_seq_event *ev)
140 {
141         struct readq_sysex_ctx ctx = {
142                 .readq = q,
143                 .dev = dev
144         };
145
146         if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
147                 return 0;
148         return snd_seq_dump_var_event(ev, readq_dump_sysex, &ctx);
149 }
150
151 /*
152  * copy an event to input queue:
153  * return zero if enqueued
154  */
155 int
156 snd_seq_oss_readq_put_event(struct seq_oss_readq *q, union evrec *ev)
157 {
158         unsigned long flags;
159
160         spin_lock_irqsave(&q->lock, flags);
161         if (q->qlen >= q->maxlen - 1) {
162                 spin_unlock_irqrestore(&q->lock, flags);
163                 return -ENOMEM;
164         }
165
166         memcpy(&q->q[q->tail], ev, sizeof(*ev));
167         q->tail = (q->tail + 1) % q->maxlen;
168         q->qlen++;
169
170         /* wake up sleeper */
171         if (waitqueue_active(&q->midi_sleep))
172                 wake_up(&q->midi_sleep);
173
174         spin_unlock_irqrestore(&q->lock, flags);
175
176         return 0;
177 }
178
179
180 /*
181  * pop queue
182  * caller must hold lock
183  */
184 int
185 snd_seq_oss_readq_pick(struct seq_oss_readq *q, union evrec *rec)
186 {
187         if (q->qlen == 0)
188                 return -EAGAIN;
189         memcpy(rec, &q->q[q->head], sizeof(*rec));
190         return 0;
191 }
192
193 /*
194  * sleep until ready
195  */
196 void
197 snd_seq_oss_readq_wait(struct seq_oss_readq *q)
198 {
199         wait_event_interruptible_timeout(q->midi_sleep,
200                                          (q->qlen > 0 || q->head == q->tail),
201                                          q->pre_event_timeout);
202 }
203
204 /*
205  * drain one record
206  * caller must hold lock
207  */
208 void
209 snd_seq_oss_readq_free(struct seq_oss_readq *q)
210 {
211         if (q->qlen > 0) {
212                 q->head = (q->head + 1) % q->maxlen;
213                 q->qlen--;
214         }
215 }
216
217 /*
218  * polling/select:
219  * return non-zero if readq is not empty.
220  */
221 unsigned int
222 snd_seq_oss_readq_poll(struct seq_oss_readq *q, struct file *file, poll_table *wait)
223 {
224         poll_wait(file, &q->midi_sleep, wait);
225         return q->qlen;
226 }
227
228 /*
229  * put a timestamp
230  */
231 int
232 snd_seq_oss_readq_put_timestamp(struct seq_oss_readq *q, unsigned long curt, int seq_mode)
233 {
234         if (curt != q->input_time) {
235                 union evrec rec;
236                 memset(&rec, 0, sizeof(rec));
237                 switch (seq_mode) {
238                 case SNDRV_SEQ_OSS_MODE_SYNTH:
239                         rec.echo = (curt << 8) | SEQ_WAIT;
240                         snd_seq_oss_readq_put_event(q, &rec);
241                         break;
242                 case SNDRV_SEQ_OSS_MODE_MUSIC:
243                         rec.t.code = EV_TIMING;
244                         rec.t.cmd = TMR_WAIT_ABS;
245                         rec.t.time = curt;
246                         snd_seq_oss_readq_put_event(q, &rec);
247                         break;
248                 }
249                 q->input_time = curt;
250         }
251         return 0;
252 }
253
254
255 #ifdef CONFIG_PROC_FS
256 /*
257  * proc interface
258  */
259 void
260 snd_seq_oss_readq_info_read(struct seq_oss_readq *q, struct snd_info_buffer *buf)
261 {
262         snd_iprintf(buf, "  read queue [%s] length = %d : tick = %ld\n",
263                     (waitqueue_active(&q->midi_sleep) ? "sleeping":"running"),
264                     q->qlen, q->input_time);
265 }
266 #endif /* CONFIG_PROC_FS */