1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
32
33
34#define SNDRV_SEQ_OSS_MAX_TIMEOUT (HZ * 3600)
35
36
37
38
39
40
41
42
43
44
45struct seq_oss_readq *
46snd_seq_oss_readq_new(struct seq_oss_devinfo *dp, int maxlen)
47{
48 struct seq_oss_readq *q;
49
50 q = kzalloc(sizeof(*q), GFP_KERNEL);
51 if (!q)
52 return NULL;
53
54 q->q = kcalloc(maxlen, sizeof(union evrec), GFP_KERNEL);
55 if (!q->q) {
56 kfree(q);
57 return NULL;
58 }
59
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;
67
68 return q;
69}
70
71
72
73
74void
75snd_seq_oss_readq_delete(struct seq_oss_readq *q)
76{
77 if (q) {
78 kfree(q->q);
79 kfree(q);
80 }
81}
82
83
84
85
86void
87snd_seq_oss_readq_clear(struct seq_oss_readq *q)
88{
89 if (q->qlen) {
90 q->qlen = 0;
91 q->head = q->tail = 0;
92 }
93
94 if (waitqueue_active(&q->midi_sleep))
95 wake_up(&q->midi_sleep);
96 q->input_time = (unsigned long)-1;
97}
98
99
100
101
102int
103snd_seq_oss_readq_puts(struct seq_oss_readq *q, int dev, unsigned char *data, int len)
104{
105 union evrec 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
123
124
125int
126snd_seq_oss_readq_put_event(struct seq_oss_readq *q, union evrec *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
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
152
153
154int
155snd_seq_oss_readq_pick(struct seq_oss_readq *q, union evrec *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
165
166void
167snd_seq_oss_readq_wait(struct seq_oss_readq *q)
168{
169 wait_event_interruptible_timeout(q->midi_sleep,
170 (q->qlen > 0 || q->head == q->tail),
171 q->pre_event_timeout);
172}
173
174
175
176
177
178void
179snd_seq_oss_readq_free(struct seq_oss_readq *q)
180{
181 if (q->qlen > 0) {
182 q->head = (q->head + 1) % q->maxlen;
183 q->qlen--;
184 }
185}
186
187
188
189
190
191unsigned int
192snd_seq_oss_readq_poll(struct seq_oss_readq *q, struct file *file, poll_table *wait)
193{
194 poll_wait(file, &q->midi_sleep, wait);
195 return q->qlen;
196}
197
198
199
200
201int
202snd_seq_oss_readq_put_timestamp(struct seq_oss_readq *q, unsigned long curt, int seq_mode)
203{
204 if (curt != q->input_time) {
205 union evrec rec;
206 memset(&rec, 0, sizeof(rec));
207 switch (seq_mode) {
208 case SNDRV_SEQ_OSS_MODE_SYNTH:
209 rec.echo = (curt << 8) | SEQ_WAIT;
210 snd_seq_oss_readq_put_event(q, &rec);
211 break;
212 case SNDRV_SEQ_OSS_MODE_MUSIC:
213 rec.t.code = EV_TIMING;
214 rec.t.cmd = TMR_WAIT_ABS;
215 rec.t.time = curt;
216 snd_seq_oss_readq_put_event(q, &rec);
217 break;
218 }
219 q->input_time = curt;
220 }
221 return 0;
222}
223
224
225#ifdef CONFIG_SND_PROC_FS
226
227
228
229void
230snd_seq_oss_readq_info_read(struct seq_oss_readq *q, struct snd_info_buffer *buf)
231{
232 snd_iprintf(buf, " read queue [%s] length = %d : tick = %ld\n",
233 (waitqueue_active(&q->midi_sleep) ? "sleeping":"running"),
234 q->qlen, q->input_time);
235}
236#endif
237