vserver 1.9.5.x5
[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 = kcalloc(1, 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 = kcalloc(maxlen, sizeof(evrec_t), 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                 kfree(q->q);
78                 kfree(q);
79         }
80 }
81
82 /*
83  * reset the read queue
84  */
85 void
86 snd_seq_oss_readq_clear(seq_oss_readq_t *q)
87 {
88         if (q->qlen) {
89                 q->qlen = 0;
90                 q->head = q->tail = 0;
91         }
92         /* if someone sleeping, wake'em up */
93         if (waitqueue_active(&q->midi_sleep))
94                 wake_up(&q->midi_sleep);
95         q->input_time = (unsigned long)-1;
96 }
97
98 /*
99  * put a midi byte
100  */
101 int
102 snd_seq_oss_readq_puts(seq_oss_readq_t *q, int dev, unsigned char *data, int len)
103 {
104         evrec_t rec;
105         int result;
106
107         memset(&rec, 0, sizeof(rec));
108         rec.c[0] = SEQ_MIDIPUTC;
109         rec.c[2] = dev;
110
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;
116         }
117         return 0;
118 }
119
120 /*
121  * copy an event to input queue:
122  * return zero if enqueued
123  */
124 int
125 snd_seq_oss_readq_put_event(seq_oss_readq_t *q, evrec_t *ev)
126 {
127         unsigned long flags;
128
129         spin_lock_irqsave(&q->lock, flags);
130         if (q->qlen >= q->maxlen - 1) {
131                 spin_unlock_irqrestore(&q->lock, flags);
132                 return -ENOMEM;
133         }
134
135         memcpy(&q->q[q->tail], ev, sizeof(*ev));
136         q->tail = (q->tail + 1) % q->maxlen;
137         q->qlen++;
138
139         /* wake up sleeper */
140         if (waitqueue_active(&q->midi_sleep))
141                 wake_up(&q->midi_sleep);
142
143         spin_unlock_irqrestore(&q->lock, flags);
144
145         return 0;
146 }
147
148
149 /*
150  * pop queue
151  * caller must hold lock
152  */
153 int
154 snd_seq_oss_readq_pick(seq_oss_readq_t *q, evrec_t *rec)
155 {
156         if (q->qlen == 0)
157                 return -EAGAIN;
158         memcpy(rec, &q->q[q->head], sizeof(*rec));
159         return 0;
160 }
161
162 /*
163  * sleep until ready
164  */
165 void
166 snd_seq_oss_readq_wait(seq_oss_readq_t *q)
167 {
168         interruptible_sleep_on_timeout(&q->midi_sleep, q->pre_event_timeout);
169 }
170
171 /*
172  * drain one record
173  * caller must hold lock
174  */
175 void
176 snd_seq_oss_readq_free(seq_oss_readq_t *q)
177 {
178         if (q->qlen > 0) {
179                 q->head = (q->head + 1) % q->maxlen;
180                 q->qlen--;
181         }
182 }
183
184 /*
185  * polling/select:
186  * return non-zero if readq is not empty.
187  */
188 unsigned int
189 snd_seq_oss_readq_poll(seq_oss_readq_t *q, struct file *file, poll_table *wait)
190 {
191         poll_wait(file, &q->midi_sleep, wait);
192         return q->qlen;
193 }
194
195 /*
196  * put a timestamp
197  */
198 int
199 snd_seq_oss_readq_put_timestamp(seq_oss_readq_t *q, unsigned long curt, int seq_mode)
200 {
201         if (curt != q->input_time) {
202                 evrec_t rec;
203                 memset(&rec, 0, sizeof(rec));
204                 switch (seq_mode) {
205                 case SNDRV_SEQ_OSS_MODE_SYNTH:
206                         rec.echo = (curt << 8) | SEQ_WAIT;
207                         snd_seq_oss_readq_put_event(q, &rec);
208                         break;
209                 case SNDRV_SEQ_OSS_MODE_MUSIC:
210                         rec.t.code = EV_TIMING;
211                         rec.t.cmd = TMR_WAIT_ABS;
212                         rec.t.time = curt;
213                         snd_seq_oss_readq_put_event(q, &rec);
214                         break;
215                 }
216                 q->input_time = curt;
217         }
218         return 0;
219 }
220
221
222 /*
223  * proc interface
224  */
225 void
226 snd_seq_oss_readq_info_read(seq_oss_readq_t *q, snd_info_buffer_t *buf)
227 {
228         snd_iprintf(buf, "  read queue [%s] length = %d : tick = %ld\n",
229                     (waitqueue_active(&q->midi_sleep) ? "sleeping":"running"),
230                     q->qlen, q->input_time);
231 }