This source file includes following definitions.
- snd_seq_prioq_new
- snd_seq_prioq_delete
- compare_timestamp
- compare_timestamp_rel
- snd_seq_prioq_cell_in
- event_is_ready
- snd_seq_prioq_cell_out
- snd_seq_prioq_avail
- prioq_match
- snd_seq_prioq_leave
- prioq_remove_match
- snd_seq_prioq_remove_events
1
2
3
4
5
6
7 #include <linux/time.h>
8 #include <linux/slab.h>
9 #include <sound/core.h>
10 #include "seq_timer.h"
11 #include "seq_prioq.h"
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 struct snd_seq_prioq *snd_seq_prioq_new(void)
43 {
44 struct snd_seq_prioq *f;
45
46 f = kzalloc(sizeof(*f), GFP_KERNEL);
47 if (!f)
48 return NULL;
49
50 spin_lock_init(&f->lock);
51 f->head = NULL;
52 f->tail = NULL;
53 f->cells = 0;
54
55 return f;
56 }
57
58
59 void snd_seq_prioq_delete(struct snd_seq_prioq **fifo)
60 {
61 struct snd_seq_prioq *f = *fifo;
62 *fifo = NULL;
63
64 if (f == NULL) {
65 pr_debug("ALSA: seq: snd_seq_prioq_delete() called with NULL prioq\n");
66 return;
67 }
68
69
70
71
72 if (f->cells > 0) {
73
74 while (f->cells > 0)
75 snd_seq_cell_free(snd_seq_prioq_cell_out(f, NULL));
76 }
77
78 kfree(f);
79 }
80
81
82
83
84
85
86 static inline int compare_timestamp(struct snd_seq_event *a,
87 struct snd_seq_event *b)
88 {
89 if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) {
90
91 return (snd_seq_compare_tick_time(&a->time.tick, &b->time.tick));
92 } else {
93
94 return (snd_seq_compare_real_time(&a->time.time, &b->time.time));
95 }
96 }
97
98
99
100
101
102
103 static inline int compare_timestamp_rel(struct snd_seq_event *a,
104 struct snd_seq_event *b)
105 {
106 if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) {
107
108 if (a->time.tick > b->time.tick)
109 return 1;
110 else if (a->time.tick == b->time.tick)
111 return 0;
112 else
113 return -1;
114 } else {
115
116 if (a->time.time.tv_sec > b->time.time.tv_sec)
117 return 1;
118 else if (a->time.time.tv_sec == b->time.time.tv_sec) {
119 if (a->time.time.tv_nsec > b->time.time.tv_nsec)
120 return 1;
121 else if (a->time.time.tv_nsec == b->time.time.tv_nsec)
122 return 0;
123 else
124 return -1;
125 } else
126 return -1;
127 }
128 }
129
130
131 int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
132 struct snd_seq_event_cell * cell)
133 {
134 struct snd_seq_event_cell *cur, *prev;
135 unsigned long flags;
136 int count;
137 int prior;
138
139 if (snd_BUG_ON(!f || !cell))
140 return -EINVAL;
141
142
143 prior = (cell->event.flags & SNDRV_SEQ_PRIORITY_MASK);
144
145 spin_lock_irqsave(&f->lock, flags);
146
147
148
149
150 if (f->tail && !prior) {
151 if (compare_timestamp(&cell->event, &f->tail->event)) {
152
153 f->tail->next = cell;
154 f->tail = cell;
155 cell->next = NULL;
156 f->cells++;
157 spin_unlock_irqrestore(&f->lock, flags);
158 return 0;
159 }
160 }
161
162
163
164 prev = NULL;
165 cur = f->head;
166
167 count = 10000;
168 while (cur != NULL) {
169
170 int rel = compare_timestamp_rel(&cell->event, &cur->event);
171 if (rel < 0)
172
173 break;
174 else if (rel == 0 && prior)
175
176 break;
177
178
179 prev = cur;
180 cur = cur->next;
181 if (! --count) {
182 spin_unlock_irqrestore(&f->lock, flags);
183 pr_err("ALSA: seq: cannot find a pointer.. infinite loop?\n");
184 return -EINVAL;
185 }
186 }
187
188
189 if (prev != NULL)
190 prev->next = cell;
191 cell->next = cur;
192
193 if (f->head == cur)
194 f->head = cell;
195 if (cur == NULL)
196 f->tail = cell;
197 f->cells++;
198 spin_unlock_irqrestore(&f->lock, flags);
199 return 0;
200 }
201
202
203 static int event_is_ready(struct snd_seq_event *ev, void *current_time)
204 {
205 if ((ev->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK)
206 return snd_seq_compare_tick_time(current_time, &ev->time.tick);
207 else
208 return snd_seq_compare_real_time(current_time, &ev->time.time);
209 }
210
211
212 struct snd_seq_event_cell *snd_seq_prioq_cell_out(struct snd_seq_prioq *f,
213 void *current_time)
214 {
215 struct snd_seq_event_cell *cell;
216 unsigned long flags;
217
218 if (f == NULL) {
219 pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n");
220 return NULL;
221 }
222 spin_lock_irqsave(&f->lock, flags);
223
224 cell = f->head;
225 if (cell && current_time && !event_is_ready(&cell->event, current_time))
226 cell = NULL;
227 if (cell) {
228 f->head = cell->next;
229
230
231 if (f->tail == cell)
232 f->tail = NULL;
233
234 cell->next = NULL;
235 f->cells--;
236 }
237
238 spin_unlock_irqrestore(&f->lock, flags);
239 return cell;
240 }
241
242
243 int snd_seq_prioq_avail(struct snd_seq_prioq * f)
244 {
245 if (f == NULL) {
246 pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n");
247 return 0;
248 }
249 return f->cells;
250 }
251
252 static inline int prioq_match(struct snd_seq_event_cell *cell,
253 int client, int timestamp)
254 {
255 if (cell->event.source.client == client ||
256 cell->event.dest.client == client)
257 return 1;
258 if (!timestamp)
259 return 0;
260 switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) {
261 case SNDRV_SEQ_TIME_STAMP_TICK:
262 if (cell->event.time.tick)
263 return 1;
264 break;
265 case SNDRV_SEQ_TIME_STAMP_REAL:
266 if (cell->event.time.time.tv_sec ||
267 cell->event.time.time.tv_nsec)
268 return 1;
269 break;
270 }
271 return 0;
272 }
273
274
275 void snd_seq_prioq_leave(struct snd_seq_prioq * f, int client, int timestamp)
276 {
277 register struct snd_seq_event_cell *cell, *next;
278 unsigned long flags;
279 struct snd_seq_event_cell *prev = NULL;
280 struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext;
281
282
283 spin_lock_irqsave(&f->lock, flags);
284 cell = f->head;
285 while (cell) {
286 next = cell->next;
287 if (prioq_match(cell, client, timestamp)) {
288
289 if (cell == f->head) {
290 f->head = cell->next;
291 } else {
292 prev->next = cell->next;
293 }
294 if (cell == f->tail)
295 f->tail = cell->next;
296 f->cells--;
297
298 cell->next = NULL;
299 if (freefirst == NULL) {
300 freefirst = cell;
301 } else {
302 freeprev->next = cell;
303 }
304 freeprev = cell;
305 } else {
306 #if 0
307 pr_debug("ALSA: seq: type = %i, source = %i, dest = %i, "
308 "client = %i\n",
309 cell->event.type,
310 cell->event.source.client,
311 cell->event.dest.client,
312 client);
313 #endif
314 prev = cell;
315 }
316 cell = next;
317 }
318 spin_unlock_irqrestore(&f->lock, flags);
319
320
321 while (freefirst) {
322 freenext = freefirst->next;
323 snd_seq_cell_free(freefirst);
324 freefirst = freenext;
325 }
326 }
327
328 static int prioq_remove_match(struct snd_seq_remove_events *info,
329 struct snd_seq_event *ev)
330 {
331 int res;
332
333 if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST) {
334 if (ev->dest.client != info->dest.client ||
335 ev->dest.port != info->dest.port)
336 return 0;
337 }
338 if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST_CHANNEL) {
339 if (! snd_seq_ev_is_channel_type(ev))
340 return 0;
341
342 if (ev->data.note.channel != info->channel)
343 return 0;
344 }
345 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_AFTER) {
346 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
347 res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick);
348 else
349 res = snd_seq_compare_real_time(&ev->time.time, &info->time.time);
350 if (!res)
351 return 0;
352 }
353 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_BEFORE) {
354 if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
355 res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick);
356 else
357 res = snd_seq_compare_real_time(&ev->time.time, &info->time.time);
358 if (res)
359 return 0;
360 }
361 if (info->remove_mode & SNDRV_SEQ_REMOVE_EVENT_TYPE) {
362 if (ev->type != info->type)
363 return 0;
364 }
365 if (info->remove_mode & SNDRV_SEQ_REMOVE_IGNORE_OFF) {
366
367 switch (ev->type) {
368 case SNDRV_SEQ_EVENT_NOTEOFF:
369
370 return 0;
371 default:
372 break;
373 }
374 }
375 if (info->remove_mode & SNDRV_SEQ_REMOVE_TAG_MATCH) {
376 if (info->tag != ev->tag)
377 return 0;
378 }
379
380 return 1;
381 }
382
383
384 void snd_seq_prioq_remove_events(struct snd_seq_prioq * f, int client,
385 struct snd_seq_remove_events *info)
386 {
387 struct snd_seq_event_cell *cell, *next;
388 unsigned long flags;
389 struct snd_seq_event_cell *prev = NULL;
390 struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext;
391
392
393 spin_lock_irqsave(&f->lock, flags);
394 cell = f->head;
395
396 while (cell) {
397 next = cell->next;
398 if (cell->event.source.client == client &&
399 prioq_remove_match(info, &cell->event)) {
400
401
402 if (cell == f->head) {
403 f->head = cell->next;
404 } else {
405 prev->next = cell->next;
406 }
407
408 if (cell == f->tail)
409 f->tail = cell->next;
410 f->cells--;
411
412
413 cell->next = NULL;
414 if (freefirst == NULL) {
415 freefirst = cell;
416 } else {
417 freeprev->next = cell;
418 }
419
420 freeprev = cell;
421 } else {
422 prev = cell;
423 }
424 cell = next;
425 }
426 spin_unlock_irqrestore(&f->lock, flags);
427
428
429 while (freefirst) {
430 freenext = freefirst->next;
431 snd_seq_cell_free(freefirst);
432 freefirst = freenext;
433 }
434 }
435
436