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 wake_up(&q->midi_sleep);
95 q->input_time = (unsigned long)-1;
96}
97
98
99
100
101int
102snd_seq_oss_readq_puts(struct seq_oss_readq *q, int dev, unsigned char *data, int len)
103{
104 union evrec 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
122
123
124struct readq_sysex_ctx {
125 struct seq_oss_readq *readq;
126 int dev;
127};
128
129static int readq_dump_sysex(void *ptr, void *buf, int count)
130{
131 struct readq_sysex_ctx *ctx = ptr;
132
133 return snd_seq_oss_readq_puts(ctx->readq, ctx->dev, buf, count);
134}
135
136int snd_seq_oss_readq_sysex(struct seq_oss_readq *q, int dev,
137 struct snd_seq_event *ev)
138{
139 struct readq_sysex_ctx ctx = {
140 .readq = q,
141 .dev = dev
142 };
143
144 if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
145 return 0;
146 return snd_seq_dump_var_event(ev, readq_dump_sysex, &ctx);
147}
148
149
150
151
152
153int
154snd_seq_oss_readq_put_event(struct seq_oss_readq *q, union evrec *ev)
155{
156 unsigned long flags;
157
158 spin_lock_irqsave(&q->lock, flags);
159 if (q->qlen >= q->maxlen - 1) {
160 spin_unlock_irqrestore(&q->lock, flags);
161 return -ENOMEM;
162 }
163
164 memcpy(&q->q[q->tail], ev, sizeof(*ev));
165 q->tail = (q->tail + 1) % q->maxlen;
166 q->qlen++;
167
168
169 wake_up(&q->midi_sleep);
170
171 spin_unlock_irqrestore(&q->lock, flags);
172
173 return 0;
174}
175
176
177
178
179
180
181int
182snd_seq_oss_readq_pick(struct seq_oss_readq *q, union evrec *rec)
183{
184 if (q->qlen == 0)
185 return -EAGAIN;
186 memcpy(rec, &q->q[q->head], sizeof(*rec));
187 return 0;
188}
189
190
191
192
193void
194snd_seq_oss_readq_wait(struct seq_oss_readq *q)
195{
196 wait_event_interruptible_timeout(q->midi_sleep,
197 (q->qlen > 0 || q->head == q->tail),
198 q->pre_event_timeout);
199}
200
201
202
203
204
205void
206snd_seq_oss_readq_free(struct seq_oss_readq *q)
207{
208 if (q->qlen > 0) {
209 q->head = (q->head + 1) % q->maxlen;
210 q->qlen--;
211 }
212}
213
214
215
216
217
218unsigned int
219snd_seq_oss_readq_poll(struct seq_oss_readq *q, struct file *file, poll_table *wait)
220{
221 poll_wait(file, &q->midi_sleep, wait);
222 return q->qlen;
223}
224
225
226
227
228int
229snd_seq_oss_readq_put_timestamp(struct seq_oss_readq *q, unsigned long curt, int seq_mode)
230{
231 if (curt != q->input_time) {
232 union evrec rec;
233 memset(&rec, 0, sizeof(rec));
234 switch (seq_mode) {
235 case SNDRV_SEQ_OSS_MODE_SYNTH:
236 rec.echo = (curt << 8) | SEQ_WAIT;
237 snd_seq_oss_readq_put_event(q, &rec);
238 break;
239 case SNDRV_SEQ_OSS_MODE_MUSIC:
240 rec.t.code = EV_TIMING;
241 rec.t.cmd = TMR_WAIT_ABS;
242 rec.t.time = curt;
243 snd_seq_oss_readq_put_event(q, &rec);
244 break;
245 }
246 q->input_time = curt;
247 }
248 return 0;
249}
250
251
252#ifdef CONFIG_SND_PROC_FS
253
254
255
256void
257snd_seq_oss_readq_info_read(struct seq_oss_readq *q, struct snd_info_buffer *buf)
258{
259 snd_iprintf(buf, " read queue [%s] length = %d : tick = %ld\n",
260 (waitqueue_active(&q->midi_sleep) ? "sleeping":"running"),
261 q->qlen, q->input_time);
262}
263#endif
264