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