ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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
28 /*
29  * constants
30  */
31 //#define SNDRV_SEQ_OSS_MAX_TIMEOUT     (unsigned long)(-1)
32 #define SNDRV_SEQ_OSS_MAX_TIMEOUT       (HZ * 3600)
33
34
35 /*
36  * prototypes
37  */
38
39
40 /*
41  * create a read queue
42  */
43 seq_oss_readq_t *
44 snd_seq_oss_readq_new(seq_oss_devinfo_t *dp, int maxlen)
45 {
46         seq_oss_readq_t *q;
47
48         if ((q = snd_kcalloc(sizeof(*q), GFP_KERNEL)) == NULL) {
49                 snd_printk(KERN_ERR "can't malloc read queue\n");
50                 return NULL;
51         }
52
53         if ((q->q = snd_kcalloc(sizeof(evrec_t) * maxlen, GFP_KERNEL)) == NULL) {
54                 snd_printk(KERN_ERR "can't malloc read queue buffer\n");
55                 kfree(q);
56                 return NULL;
57         }
58
59         q->maxlen = maxlen;
60         q->qlen = 0;
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;
66
67         return q;
68 }
69
70 /*
71  * delete the read queue
72  */
73 void
74 snd_seq_oss_readq_delete(seq_oss_readq_t *q)
75 {
76         if (q) {
77                 snd_seq_oss_readq_clear(q);     /* to be sure */
78                 if (q->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(seq_oss_readq_t *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(seq_oss_readq_t *q, int dev, unsigned char *data, int len)
105 {
106         evrec_t rec;
107         int result;
108
109         rec.c[0] = SEQ_MIDIPUTC;
110         rec.c[2] = dev;
111         rec.c[3] = 0;
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  * copy an event to input queue:
124  * return zero if enqueued
125  */
126 int
127 snd_seq_oss_readq_put_event(seq_oss_readq_t *q, evrec_t *ev)
128 {
129         unsigned long flags;
130
131         spin_lock_irqsave(&q->lock, flags);
132         if (q->qlen >= q->maxlen - 1) {
133                 spin_unlock_irqrestore(&q->lock, flags);
134                 return -ENOMEM;
135         }
136
137         memcpy(&q->q[q->tail], ev, ev_length(ev));
138         q->tail = (q->tail + 1) % q->maxlen;
139         q->qlen++;
140
141         /* wake up sleeper */
142         if (waitqueue_active(&q->midi_sleep))
143                 wake_up(&q->midi_sleep);
144
145         spin_unlock_irqrestore(&q->lock, flags);
146
147         return 0;
148 }
149
150
151 /*
152  * pop queue
153  */
154 evrec_t *
155 snd_seq_oss_readq_pick(seq_oss_readq_t *q, int blocking, unsigned long *rflags)
156 {
157         evrec_t *p;
158
159         spin_lock_irqsave(&q->lock, *rflags);
160         if (q->qlen == 0) {
161                 if (blocking) {
162                         spin_unlock(&q->lock);
163                         interruptible_sleep_on_timeout(&q->midi_sleep,
164                                                        q->pre_event_timeout);
165                         spin_lock(&q->lock);
166                 }
167                 if (q->qlen == 0) {
168                         spin_unlock_irqrestore(&q->lock, *rflags);
169                         return NULL;
170                 }
171         }
172         p = q->q + q->head;
173
174         return p;
175 }
176
177 /*
178  * unlock queue
179  */
180 void
181 snd_seq_oss_readq_unlock(seq_oss_readq_t *q, unsigned long flags)
182 {
183         spin_unlock_irqrestore(&q->lock, flags);
184 }
185
186 /*
187  * drain one record and unlock queue
188  */
189 void
190 snd_seq_oss_readq_free(seq_oss_readq_t *q, unsigned long flags)
191 {
192         if (q->qlen > 0) {
193                 q->head = (q->head + 1) % q->maxlen;
194                 q->qlen--;
195         }
196         spin_unlock_irqrestore(&q->lock, flags);
197 }
198
199 /*
200  * polling/select:
201  * return non-zero if readq is not empty.
202  */
203 unsigned int
204 snd_seq_oss_readq_poll(seq_oss_readq_t *q, struct file *file, poll_table *wait)
205 {
206         poll_wait(file, &q->midi_sleep, wait);
207         return q->qlen;
208 }
209
210 /*
211  * put a timestamp
212  */
213 int
214 snd_seq_oss_readq_put_timestamp(seq_oss_readq_t *q, unsigned long curt, int seq_mode)
215 {
216         if (curt != q->input_time) {
217                 evrec_t rec;
218                 switch (seq_mode) {
219                 case SNDRV_SEQ_OSS_MODE_SYNTH:
220                         rec.echo = (curt << 8) | SEQ_WAIT;
221                         snd_seq_oss_readq_put_event(q, &rec);
222                         break;
223                 case SNDRV_SEQ_OSS_MODE_MUSIC:
224                         rec.t.code = EV_TIMING;
225                         rec.t.cmd = TMR_WAIT_ABS;
226                         rec.t.time = curt;
227                         snd_seq_oss_readq_put_event(q, &rec);
228                         break;
229                 }
230                 q->input_time = curt;
231         }
232         return 0;
233 }
234
235
236 /*
237  * proc interface
238  */
239 void
240 snd_seq_oss_readq_info_read(seq_oss_readq_t *q, snd_info_buffer_t *buf)
241 {
242         snd_iprintf(buf, "  read queue [%s] length = %d : tick = %ld\n",
243                     (waitqueue_active(&q->midi_sleep) ? "sleeping":"running"),
244                     q->qlen, q->input_time);
245 }