[NETFILTER]: Fix wrong option spelling in Makefile for CONFIG_BRIDGE_EBT_ULOG
[deliverable/linux.git] / sound / core / timer.c
CommitLineData
1da177e4
LT
1/*
2 * Timers abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <sound/driver.h>
23#include <linux/delay.h>
24#include <linux/init.h>
25#include <linux/smp_lock.h>
26#include <linux/slab.h>
27#include <linux/time.h>
28#include <linux/moduleparam.h>
543537bd 29#include <linux/string.h>
1da177e4
LT
30#include <sound/core.h>
31#include <sound/timer.h>
32#include <sound/control.h>
33#include <sound/info.h>
34#include <sound/minors.h>
35#include <sound/initval.h>
36#include <linux/kmod.h>
37#ifdef CONFIG_KERNELD
38#include <linux/kerneld.h>
39#endif
40
41#if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
42#define DEFAULT_TIMER_LIMIT 3
43#elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
44#define DEFAULT_TIMER_LIMIT 2
45#else
46#define DEFAULT_TIMER_LIMIT 1
47#endif
48
49static int timer_limit = DEFAULT_TIMER_LIMIT;
50MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Takashi Iwai <tiwai@suse.de>");
51MODULE_DESCRIPTION("ALSA timer interface");
52MODULE_LICENSE("GPL");
53module_param(timer_limit, int, 0444);
54MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
55
53d2f744
TI
56struct snd_timer_user {
57 struct snd_timer_instance *timeri;
6b172a85 58 int tread; /* enhanced read with timestamps and events */
1da177e4
LT
59 unsigned long ticks;
60 unsigned long overrun;
61 int qhead;
62 int qtail;
63 int qused;
64 int queue_size;
53d2f744
TI
65 struct snd_timer_read *queue;
66 struct snd_timer_tread *tqueue;
1da177e4
LT
67 spinlock_t qlock;
68 unsigned long last_resolution;
69 unsigned int filter;
70 struct timespec tstamp; /* trigger tstamp */
71 wait_queue_head_t qchange_sleep;
72 struct fasync_struct *fasync;
c1935b4d 73 struct semaphore tread_sem;
53d2f744 74};
1da177e4
LT
75
76/* list of timers */
77static LIST_HEAD(snd_timer_list);
78
79/* list of slave instances */
80static LIST_HEAD(snd_timer_slave_list);
81
82/* lock for slave active lists */
83static DEFINE_SPINLOCK(slave_active_lock);
84
85static DECLARE_MUTEX(register_mutex);
86
53d2f744
TI
87static int snd_timer_free(struct snd_timer *timer);
88static int snd_timer_dev_free(struct snd_device *device);
89static int snd_timer_dev_register(struct snd_device *device);
90static int snd_timer_dev_unregister(struct snd_device *device);
1da177e4 91
53d2f744 92static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
1da177e4
LT
93
94/*
95 * create a timer instance with the given owner string.
96 * when timer is not NULL, increments the module counter
97 */
53d2f744
TI
98static struct snd_timer_instance *snd_timer_instance_new(char *owner,
99 struct snd_timer *timer)
1da177e4 100{
53d2f744 101 struct snd_timer_instance *timeri;
ca2c0966 102 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
1da177e4
LT
103 if (timeri == NULL)
104 return NULL;
543537bd 105 timeri->owner = kstrdup(owner, GFP_KERNEL);
1da177e4
LT
106 if (! timeri->owner) {
107 kfree(timeri);
108 return NULL;
109 }
110 INIT_LIST_HEAD(&timeri->open_list);
111 INIT_LIST_HEAD(&timeri->active_list);
112 INIT_LIST_HEAD(&timeri->ack_list);
113 INIT_LIST_HEAD(&timeri->slave_list_head);
114 INIT_LIST_HEAD(&timeri->slave_active_head);
115
116 timeri->timer = timer;
de24214d 117 if (timer && !try_module_get(timer->module)) {
1da177e4
LT
118 kfree(timeri->owner);
119 kfree(timeri);
120 return NULL;
121 }
122
123 return timeri;
124}
125
126/*
127 * find a timer instance from the given timer id
128 */
53d2f744 129static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
1da177e4 130{
53d2f744 131 struct snd_timer *timer = NULL;
1da177e4
LT
132 struct list_head *p;
133
134 list_for_each(p, &snd_timer_list) {
53d2f744 135 timer = list_entry(p, struct snd_timer, device_list);
1da177e4
LT
136
137 if (timer->tmr_class != tid->dev_class)
138 continue;
139 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
140 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
141 (timer->card == NULL ||
142 timer->card->number != tid->card))
143 continue;
144 if (timer->tmr_device != tid->device)
145 continue;
146 if (timer->tmr_subdevice != tid->subdevice)
147 continue;
148 return timer;
149 }
150 return NULL;
151}
152
153#ifdef CONFIG_KMOD
154
53d2f744 155static void snd_timer_request(struct snd_timer_id *tid)
1da177e4
LT
156{
157 if (! current->fs->root)
158 return;
159 switch (tid->dev_class) {
160 case SNDRV_TIMER_CLASS_GLOBAL:
161 if (tid->device < timer_limit)
162 request_module("snd-timer-%i", tid->device);
163 break;
164 case SNDRV_TIMER_CLASS_CARD:
165 case SNDRV_TIMER_CLASS_PCM:
166 if (tid->card < snd_ecards_limit)
167 request_module("snd-card-%i", tid->card);
168 break;
169 default:
170 break;
171 }
172}
173
174#endif
175
176/*
177 * look for a master instance matching with the slave id of the given slave.
178 * when found, relink the open_link of the slave.
179 *
180 * call this with register_mutex down.
181 */
53d2f744 182static void snd_timer_check_slave(struct snd_timer_instance *slave)
1da177e4 183{
53d2f744
TI
184 struct snd_timer *timer;
185 struct snd_timer_instance *master;
1da177e4
LT
186 struct list_head *p, *q;
187
188 /* FIXME: it's really dumb to look up all entries.. */
189 list_for_each(p, &snd_timer_list) {
53d2f744 190 timer = list_entry(p, struct snd_timer, device_list);
1da177e4 191 list_for_each(q, &timer->open_list_head) {
53d2f744 192 master = list_entry(q, struct snd_timer_instance, open_list);
1da177e4
LT
193 if (slave->slave_class == master->slave_class &&
194 slave->slave_id == master->slave_id) {
195 list_del(&slave->open_list);
6b172a85
CL
196 list_add_tail(&slave->open_list,
197 &master->slave_list_head);
1da177e4
LT
198 spin_lock_irq(&slave_active_lock);
199 slave->master = master;
200 slave->timer = master->timer;
201 spin_unlock_irq(&slave_active_lock);
202 return;
203 }
204 }
205 }
206}
207
208/*
209 * look for slave instances matching with the slave id of the given master.
210 * when found, relink the open_link of slaves.
211 *
212 * call this with register_mutex down.
213 */
53d2f744 214static void snd_timer_check_master(struct snd_timer_instance *master)
1da177e4 215{
53d2f744 216 struct snd_timer_instance *slave;
1da177e4
LT
217 struct list_head *p, *n;
218
219 /* check all pending slaves */
220 list_for_each_safe(p, n, &snd_timer_slave_list) {
53d2f744 221 slave = list_entry(p, struct snd_timer_instance, open_list);
1da177e4
LT
222 if (slave->slave_class == master->slave_class &&
223 slave->slave_id == master->slave_id) {
224 list_del(p);
225 list_add_tail(p, &master->slave_list_head);
226 spin_lock_irq(&slave_active_lock);
227 slave->master = master;
228 slave->timer = master->timer;
229 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
6b172a85
CL
230 list_add_tail(&slave->active_list,
231 &master->slave_active_head);
1da177e4
LT
232 spin_unlock_irq(&slave_active_lock);
233 }
234 }
235}
236
237/*
238 * open a timer instance
239 * when opening a master, the slave id must be here given.
240 */
53d2f744
TI
241int snd_timer_open(struct snd_timer_instance **ti,
242 char *owner, struct snd_timer_id *tid,
1da177e4
LT
243 unsigned int slave_id)
244{
53d2f744
TI
245 struct snd_timer *timer;
246 struct snd_timer_instance *timeri = NULL;
6b172a85 247
1da177e4
LT
248 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
249 /* open a slave instance */
250 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
251 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
252 snd_printd("invalid slave class %i\n", tid->dev_sclass);
253 return -EINVAL;
254 }
255 down(&register_mutex);
256 timeri = snd_timer_instance_new(owner, NULL);
2fd43d11
CL
257 if (!timeri) {
258 up(&register_mutex);
259 return -ENOMEM;
260 }
1da177e4
LT
261 timeri->slave_class = tid->dev_sclass;
262 timeri->slave_id = tid->device;
263 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
264 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
265 snd_timer_check_slave(timeri);
266 up(&register_mutex);
267 *ti = timeri;
268 return 0;
269 }
270
271 /* open a master instance */
272 down(&register_mutex);
273 timer = snd_timer_find(tid);
274#ifdef CONFIG_KMOD
275 if (timer == NULL) {
276 up(&register_mutex);
277 snd_timer_request(tid);
278 down(&register_mutex);
279 timer = snd_timer_find(tid);
280 }
281#endif
2fd43d11 282 if (!timer) {
1da177e4
LT
283 up(&register_mutex);
284 return -ENODEV;
285 }
2fd43d11
CL
286 if (!list_empty(&timer->open_list_head)) {
287 timeri = list_entry(timer->open_list_head.next,
53d2f744 288 struct snd_timer_instance, open_list);
2fd43d11
CL
289 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
290 up(&register_mutex);
291 return -EBUSY;
292 }
293 }
294 timeri = snd_timer_instance_new(owner, timer);
295 if (!timeri) {
296 up(&register_mutex);
297 return -ENOMEM;
298 }
299 timeri->slave_class = tid->dev_sclass;
300 timeri->slave_id = slave_id;
301 if (list_empty(&timer->open_list_head) && timer->hw.open)
302 timer->hw.open(timer);
303 list_add_tail(&timeri->open_list, &timer->open_list_head);
304 snd_timer_check_master(timeri);
1da177e4
LT
305 up(&register_mutex);
306 *ti = timeri;
307 return 0;
308}
309
53d2f744
TI
310static int _snd_timer_stop(struct snd_timer_instance *timeri,
311 int keep_flag, int event);
1da177e4
LT
312
313/*
314 * close a timer instance
315 */
53d2f744 316int snd_timer_close(struct snd_timer_instance *timeri)
1da177e4 317{
53d2f744 318 struct snd_timer *timer = NULL;
1da177e4 319 struct list_head *p, *n;
53d2f744 320 struct snd_timer_instance *slave;
1da177e4
LT
321
322 snd_assert(timeri != NULL, return -ENXIO);
323
324 /* force to stop the timer */
325 snd_timer_stop(timeri);
326
327 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
328 /* wait, until the active callback is finished */
329 spin_lock_irq(&slave_active_lock);
330 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
331 spin_unlock_irq(&slave_active_lock);
332 udelay(10);
333 spin_lock_irq(&slave_active_lock);
334 }
335 spin_unlock_irq(&slave_active_lock);
336 down(&register_mutex);
337 list_del(&timeri->open_list);
338 up(&register_mutex);
339 } else {
340 timer = timeri->timer;
341 /* wait, until the active callback is finished */
342 spin_lock_irq(&timer->lock);
343 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
344 spin_unlock_irq(&timer->lock);
345 udelay(10);
346 spin_lock_irq(&timer->lock);
347 }
348 spin_unlock_irq(&timer->lock);
349 down(&register_mutex);
350 list_del(&timeri->open_list);
6b172a85
CL
351 if (timer && list_empty(&timer->open_list_head) &&
352 timer->hw.close)
1da177e4
LT
353 timer->hw.close(timer);
354 /* remove slave links */
355 list_for_each_safe(p, n, &timeri->slave_list_head) {
53d2f744 356 slave = list_entry(p, struct snd_timer_instance, open_list);
1da177e4
LT
357 spin_lock_irq(&slave_active_lock);
358 _snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
359 list_del(p);
360 list_add_tail(p, &snd_timer_slave_list);
361 slave->master = NULL;
362 slave->timer = NULL;
363 spin_unlock_irq(&slave_active_lock);
364 }
365 up(&register_mutex);
366 }
367 if (timeri->private_free)
368 timeri->private_free(timeri);
369 kfree(timeri->owner);
370 kfree(timeri);
de24214d
CL
371 if (timer)
372 module_put(timer->module);
1da177e4
LT
373 return 0;
374}
375
53d2f744 376unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
1da177e4 377{
53d2f744 378 struct snd_timer * timer;
1da177e4
LT
379
380 if (timeri == NULL)
381 return 0;
382 if ((timer = timeri->timer) != NULL) {
383 if (timer->hw.c_resolution)
384 return timer->hw.c_resolution(timer);
385 return timer->hw.resolution;
386 }
387 return 0;
388}
389
53d2f744 390static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
1da177e4 391{
53d2f744 392 struct snd_timer *timer;
1da177e4
LT
393 unsigned long flags;
394 unsigned long resolution = 0;
53d2f744 395 struct snd_timer_instance *ts;
1da177e4
LT
396 struct list_head *n;
397 struct timespec tstamp;
398
07799e75 399 getnstimeofday(&tstamp);
6b172a85
CL
400 snd_assert(event >= SNDRV_TIMER_EVENT_START &&
401 event <= SNDRV_TIMER_EVENT_PAUSE, return);
402 if (event == SNDRV_TIMER_EVENT_START ||
403 event == SNDRV_TIMER_EVENT_CONTINUE)
1da177e4
LT
404 resolution = snd_timer_resolution(ti);
405 if (ti->ccallback)
406 ti->ccallback(ti, SNDRV_TIMER_EVENT_START, &tstamp, resolution);
407 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
408 return;
409 timer = ti->timer;
410 if (timer == NULL)
411 return;
412 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
413 return;
414 spin_lock_irqsave(&timer->lock, flags);
415 list_for_each(n, &ti->slave_active_head) {
53d2f744 416 ts = list_entry(n, struct snd_timer_instance, active_list);
1da177e4
LT
417 if (ts->ccallback)
418 ts->ccallback(ti, event + 100, &tstamp, resolution);
419 }
420 spin_unlock_irqrestore(&timer->lock, flags);
421}
422
53d2f744 423static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
6b172a85 424 unsigned long sticks)
1da177e4
LT
425{
426 list_del(&timeri->active_list);
427 list_add_tail(&timeri->active_list, &timer->active_list_head);
428 if (timer->running) {
429 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
430 goto __start_now;
431 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
432 timeri->flags |= SNDRV_TIMER_IFLG_START;
433 return 1; /* delayed start */
434 } else {
435 timer->sticks = sticks;
436 timer->hw.start(timer);
437 __start_now:
438 timer->running++;
439 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
440 return 0;
441 }
442}
443
53d2f744 444static int snd_timer_start_slave(struct snd_timer_instance *timeri)
1da177e4
LT
445{
446 unsigned long flags;
447
448 spin_lock_irqsave(&slave_active_lock, flags);
449 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
450 if (timeri->master)
6b172a85
CL
451 list_add_tail(&timeri->active_list,
452 &timeri->master->slave_active_head);
1da177e4
LT
453 spin_unlock_irqrestore(&slave_active_lock, flags);
454 return 1; /* delayed start */
455}
456
457/*
458 * start the timer instance
6b172a85 459 */
53d2f744 460int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
1da177e4 461{
53d2f744 462 struct snd_timer *timer;
1da177e4
LT
463 int result = -EINVAL;
464 unsigned long flags;
465
466 if (timeri == NULL || ticks < 1)
467 return -EINVAL;
468 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
469 result = snd_timer_start_slave(timeri);
470 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
471 return result;
472 }
473 timer = timeri->timer;
474 if (timer == NULL)
475 return -EINVAL;
476 spin_lock_irqsave(&timer->lock, flags);
477 timeri->ticks = timeri->cticks = ticks;
478 timeri->pticks = 0;
479 result = snd_timer_start1(timer, timeri, ticks);
480 spin_unlock_irqrestore(&timer->lock, flags);
481 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
482 return result;
483}
484
53d2f744
TI
485static int _snd_timer_stop(struct snd_timer_instance * timeri,
486 int keep_flag, int event)
1da177e4 487{
53d2f744 488 struct snd_timer *timer;
1da177e4
LT
489 unsigned long flags;
490
491 snd_assert(timeri != NULL, return -ENXIO);
492
493 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
494 if (!keep_flag) {
495 spin_lock_irqsave(&slave_active_lock, flags);
496 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
497 spin_unlock_irqrestore(&slave_active_lock, flags);
498 }
499 goto __end;
500 }
501 timer = timeri->timer;
502 if (!timer)
503 return -EINVAL;
504 spin_lock_irqsave(&timer->lock, flags);
505 list_del_init(&timeri->ack_list);
506 list_del_init(&timeri->active_list);
507 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
508 !(--timer->running)) {
509 timer->hw.stop(timer);
510 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
511 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
512 snd_timer_reschedule(timer, 0);
513 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
514 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
515 timer->hw.start(timer);
516 }
517 }
518 }
519 if (!keep_flag)
6b172a85
CL
520 timeri->flags &=
521 ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
1da177e4
LT
522 spin_unlock_irqrestore(&timer->lock, flags);
523 __end:
524 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
525 snd_timer_notify1(timeri, event);
526 return 0;
527}
528
529/*
530 * stop the timer instance.
531 *
532 * do not call this from the timer callback!
533 */
53d2f744 534int snd_timer_stop(struct snd_timer_instance *timeri)
1da177e4 535{
53d2f744 536 struct snd_timer *timer;
1da177e4
LT
537 unsigned long flags;
538 int err;
539
540 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
541 if (err < 0)
542 return err;
543 timer = timeri->timer;
544 spin_lock_irqsave(&timer->lock, flags);
545 timeri->cticks = timeri->ticks;
546 timeri->pticks = 0;
547 spin_unlock_irqrestore(&timer->lock, flags);
548 return 0;
549}
550
551/*
552 * start again.. the tick is kept.
553 */
53d2f744 554int snd_timer_continue(struct snd_timer_instance *timeri)
1da177e4 555{
53d2f744 556 struct snd_timer *timer;
1da177e4
LT
557 int result = -EINVAL;
558 unsigned long flags;
559
560 if (timeri == NULL)
561 return result;
562 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
563 return snd_timer_start_slave(timeri);
564 timer = timeri->timer;
565 if (! timer)
566 return -EINVAL;
567 spin_lock_irqsave(&timer->lock, flags);
568 if (!timeri->cticks)
569 timeri->cticks = 1;
570 timeri->pticks = 0;
571 result = snd_timer_start1(timer, timeri, timer->sticks);
572 spin_unlock_irqrestore(&timer->lock, flags);
573 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
574 return result;
575}
576
577/*
578 * pause.. remember the ticks left
579 */
53d2f744 580int snd_timer_pause(struct snd_timer_instance * timeri)
1da177e4
LT
581{
582 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
583}
584
585/*
586 * reschedule the timer
587 *
588 * start pending instances and check the scheduling ticks.
589 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
590 */
53d2f744 591static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
1da177e4 592{
53d2f744 593 struct snd_timer_instance *ti;
1da177e4
LT
594 unsigned long ticks = ~0UL;
595 struct list_head *p;
596
597 list_for_each(p, &timer->active_list_head) {
53d2f744 598 ti = list_entry(p, struct snd_timer_instance, active_list);
1da177e4
LT
599 if (ti->flags & SNDRV_TIMER_IFLG_START) {
600 ti->flags &= ~SNDRV_TIMER_IFLG_START;
601 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
602 timer->running++;
603 }
604 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
605 if (ticks > ti->cticks)
606 ticks = ti->cticks;
607 }
608 }
609 if (ticks == ~0UL) {
610 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
611 return;
612 }
613 if (ticks > timer->hw.ticks)
614 ticks = timer->hw.ticks;
615 if (ticks_left != ticks)
616 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
617 timer->sticks = ticks;
618}
619
620/*
621 * timer tasklet
622 *
623 */
624static void snd_timer_tasklet(unsigned long arg)
625{
53d2f744
TI
626 struct snd_timer *timer = (struct snd_timer *) arg;
627 struct snd_timer_instance *ti;
1da177e4
LT
628 struct list_head *p;
629 unsigned long resolution, ticks;
630
631 spin_lock(&timer->lock);
632 /* now process all callbacks */
633 while (!list_empty(&timer->sack_list_head)) {
634 p = timer->sack_list_head.next; /* get first item */
53d2f744 635 ti = list_entry(p, struct snd_timer_instance, ack_list);
1da177e4
LT
636
637 /* remove from ack_list and make empty */
638 list_del_init(p);
6b172a85 639
1da177e4
LT
640 ticks = ti->pticks;
641 ti->pticks = 0;
642 resolution = ti->resolution;
643
644 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
645 spin_unlock(&timer->lock);
646 if (ti->callback)
647 ti->callback(ti, resolution, ticks);
648 spin_lock(&timer->lock);
649 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
650 }
651 spin_unlock(&timer->lock);
652}
653
654/*
655 * timer interrupt
656 *
657 * ticks_left is usually equal to timer->sticks.
658 *
659 */
53d2f744 660void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
1da177e4 661{
53d2f744 662 struct snd_timer_instance *ti, *ts;
1da177e4 663 unsigned long resolution, ticks;
6b172a85 664 struct list_head *p, *q, *n, *ack_list_head;
b32425ac 665 unsigned long flags;
1da177e4
LT
666 int use_tasklet = 0;
667
668 if (timer == NULL)
669 return;
670
b32425ac 671 spin_lock_irqsave(&timer->lock, flags);
1da177e4
LT
672
673 /* remember the current resolution */
674 if (timer->hw.c_resolution)
675 resolution = timer->hw.c_resolution(timer);
676 else
677 resolution = timer->hw.resolution;
678
679 /* loop for all active instances
6b172a85
CL
680 * Here we cannot use list_for_each because the active_list of a
681 * processed instance is relinked to done_list_head before the callback
682 * is called.
1da177e4
LT
683 */
684 list_for_each_safe(p, n, &timer->active_list_head) {
53d2f744 685 ti = list_entry(p, struct snd_timer_instance, active_list);
1da177e4
LT
686 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
687 continue;
688 ti->pticks += ticks_left;
689 ti->resolution = resolution;
690 if (ti->cticks < ticks_left)
691 ti->cticks = 0;
692 else
693 ti->cticks -= ticks_left;
694 if (ti->cticks) /* not expired */
695 continue;
696 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
697 ti->cticks = ti->ticks;
698 } else {
699 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
700 if (--timer->running)
701 list_del(p);
702 }
6b172a85
CL
703 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
704 (ti->flags & SNDRV_TIMER_IFLG_FAST))
705 ack_list_head = &timer->ack_list_head;
706 else
707 ack_list_head = &timer->sack_list_head;
708 if (list_empty(&ti->ack_list))
709 list_add_tail(&ti->ack_list, ack_list_head);
1da177e4 710 list_for_each(q, &ti->slave_active_head) {
53d2f744 711 ts = list_entry(q, struct snd_timer_instance, active_list);
1da177e4
LT
712 ts->pticks = ti->pticks;
713 ts->resolution = resolution;
6b172a85
CL
714 if (list_empty(&ts->ack_list))
715 list_add_tail(&ts->ack_list, ack_list_head);
1da177e4
LT
716 }
717 }
718 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
719 snd_timer_reschedule(timer, ticks_left);
720 if (timer->running) {
721 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
722 timer->hw.stop(timer);
723 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
724 }
725 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
726 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
727 /* restart timer */
728 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
729 timer->hw.start(timer);
730 }
731 } else {
732 timer->hw.stop(timer);
733 }
734
735 /* now process all fast callbacks */
736 while (!list_empty(&timer->ack_list_head)) {
737 p = timer->ack_list_head.next; /* get first item */
53d2f744 738 ti = list_entry(p, struct snd_timer_instance, ack_list);
6b172a85 739
1da177e4
LT
740 /* remove from ack_list and make empty */
741 list_del_init(p);
6b172a85 742
1da177e4
LT
743 ticks = ti->pticks;
744 ti->pticks = 0;
745
746 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
747 spin_unlock(&timer->lock);
748 if (ti->callback)
749 ti->callback(ti, resolution, ticks);
750 spin_lock(&timer->lock);
751 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
752 }
753
754 /* do we have any slow callbacks? */
755 use_tasklet = !list_empty(&timer->sack_list_head);
b32425ac 756 spin_unlock_irqrestore(&timer->lock, flags);
1da177e4
LT
757
758 if (use_tasklet)
759 tasklet_hi_schedule(&timer->task_queue);
760}
761
762/*
763
764 */
765
53d2f744
TI
766int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
767 struct snd_timer **rtimer)
1da177e4 768{
53d2f744 769 struct snd_timer *timer;
1da177e4 770 int err;
53d2f744 771 static struct snd_device_ops ops = {
1da177e4
LT
772 .dev_free = snd_timer_dev_free,
773 .dev_register = snd_timer_dev_register,
774 .dev_unregister = snd_timer_dev_unregister
775 };
776
777 snd_assert(tid != NULL, return -EINVAL);
778 snd_assert(rtimer != NULL, return -EINVAL);
779 *rtimer = NULL;
ca2c0966 780 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
73e77ba0
TI
781 if (timer == NULL) {
782 snd_printk(KERN_ERR "timer: cannot allocate\n");
1da177e4 783 return -ENOMEM;
73e77ba0 784 }
1da177e4
LT
785 timer->tmr_class = tid->dev_class;
786 timer->card = card;
787 timer->tmr_device = tid->device;
788 timer->tmr_subdevice = tid->subdevice;
789 if (id)
790 strlcpy(timer->id, id, sizeof(timer->id));
791 INIT_LIST_HEAD(&timer->device_list);
792 INIT_LIST_HEAD(&timer->open_list_head);
793 INIT_LIST_HEAD(&timer->active_list_head);
794 INIT_LIST_HEAD(&timer->ack_list_head);
795 INIT_LIST_HEAD(&timer->sack_list_head);
796 spin_lock_init(&timer->lock);
6b172a85
CL
797 tasklet_init(&timer->task_queue, snd_timer_tasklet,
798 (unsigned long)timer);
1da177e4 799 if (card != NULL) {
de24214d 800 timer->module = card->module;
6b172a85
CL
801 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
802 if (err < 0) {
1da177e4
LT
803 snd_timer_free(timer);
804 return err;
805 }
806 }
807 *rtimer = timer;
808 return 0;
809}
810
53d2f744 811static int snd_timer_free(struct snd_timer *timer)
1da177e4
LT
812{
813 snd_assert(timer != NULL, return -ENXIO);
814 if (timer->private_free)
815 timer->private_free(timer);
816 kfree(timer);
817 return 0;
818}
819
53d2f744 820static int snd_timer_dev_free(struct snd_device *device)
1da177e4 821{
53d2f744 822 struct snd_timer *timer = device->device_data;
1da177e4
LT
823 return snd_timer_free(timer);
824}
825
53d2f744 826static int snd_timer_dev_register(struct snd_device *dev)
1da177e4 827{
53d2f744
TI
828 struct snd_timer *timer = dev->device_data;
829 struct snd_timer *timer1;
1da177e4
LT
830 struct list_head *p;
831
6b172a85
CL
832 snd_assert(timer != NULL && timer->hw.start != NULL &&
833 timer->hw.stop != NULL, return -ENXIO);
1da177e4
LT
834 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
835 !timer->hw.resolution && timer->hw.c_resolution == NULL)
836 return -EINVAL;
837
838 down(&register_mutex);
839 list_for_each(p, &snd_timer_list) {
53d2f744 840 timer1 = list_entry(p, struct snd_timer, device_list);
1da177e4
LT
841 if (timer1->tmr_class > timer->tmr_class)
842 break;
843 if (timer1->tmr_class < timer->tmr_class)
844 continue;
845 if (timer1->card && timer->card) {
846 if (timer1->card->number > timer->card->number)
847 break;
848 if (timer1->card->number < timer->card->number)
849 continue;
850 }
851 if (timer1->tmr_device > timer->tmr_device)
852 break;
853 if (timer1->tmr_device < timer->tmr_device)
854 continue;
855 if (timer1->tmr_subdevice > timer->tmr_subdevice)
856 break;
857 if (timer1->tmr_subdevice < timer->tmr_subdevice)
858 continue;
859 /* conflicts.. */
860 up(&register_mutex);
861 return -EBUSY;
862 }
863 list_add_tail(&timer->device_list, p);
864 up(&register_mutex);
865 return 0;
866}
867
53d2f744 868static int snd_timer_unregister(struct snd_timer *timer)
1da177e4
LT
869{
870 struct list_head *p, *n;
53d2f744 871 struct snd_timer_instance *ti;
1da177e4
LT
872
873 snd_assert(timer != NULL, return -ENXIO);
874 down(&register_mutex);
875 if (! list_empty(&timer->open_list_head)) {
876 snd_printk(KERN_WARNING "timer 0x%lx is busy?\n", (long)timer);
877 list_for_each_safe(p, n, &timer->open_list_head) {
878 list_del_init(p);
53d2f744 879 ti = list_entry(p, struct snd_timer_instance, open_list);
1da177e4
LT
880 ti->timer = NULL;
881 }
882 }
883 list_del(&timer->device_list);
884 up(&register_mutex);
885 return snd_timer_free(timer);
886}
887
53d2f744 888static int snd_timer_dev_unregister(struct snd_device *device)
1da177e4 889{
53d2f744 890 struct snd_timer *timer = device->device_data;
1da177e4
LT
891 return snd_timer_unregister(timer);
892}
893
53d2f744 894void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
1da177e4
LT
895{
896 unsigned long flags;
897 unsigned long resolution = 0;
53d2f744 898 struct snd_timer_instance *ti, *ts;
1da177e4
LT
899 struct list_head *p, *n;
900
7c22f1aa
TI
901 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
902 return;
6b172a85
CL
903 snd_assert(event >= SNDRV_TIMER_EVENT_MSTART &&
904 event <= SNDRV_TIMER_EVENT_MRESUME, return);
1da177e4 905 spin_lock_irqsave(&timer->lock, flags);
a501dfa3
JK
906 if (event == SNDRV_TIMER_EVENT_MSTART ||
907 event == SNDRV_TIMER_EVENT_MCONTINUE ||
908 event == SNDRV_TIMER_EVENT_MRESUME) {
1da177e4
LT
909 if (timer->hw.c_resolution)
910 resolution = timer->hw.c_resolution(timer);
911 else
912 resolution = timer->hw.resolution;
913 }
914 list_for_each(p, &timer->active_list_head) {
53d2f744 915 ti = list_entry(p, struct snd_timer_instance, active_list);
1da177e4
LT
916 if (ti->ccallback)
917 ti->ccallback(ti, event, tstamp, resolution);
918 list_for_each(n, &ti->slave_active_head) {
53d2f744 919 ts = list_entry(n, struct snd_timer_instance, active_list);
1da177e4
LT
920 if (ts->ccallback)
921 ts->ccallback(ts, event, tstamp, resolution);
922 }
923 }
924 spin_unlock_irqrestore(&timer->lock, flags);
925}
926
927/*
928 * exported functions for global timers
929 */
53d2f744 930int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
1da177e4 931{
53d2f744 932 struct snd_timer_id tid;
6b172a85 933
1da177e4
LT
934 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
935 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
936 tid.card = -1;
937 tid.device = device;
938 tid.subdevice = 0;
939 return snd_timer_new(NULL, id, &tid, rtimer);
940}
941
53d2f744 942int snd_timer_global_free(struct snd_timer *timer)
1da177e4
LT
943{
944 return snd_timer_free(timer);
945}
946
53d2f744 947int snd_timer_global_register(struct snd_timer *timer)
1da177e4 948{
53d2f744 949 struct snd_device dev;
1da177e4
LT
950
951 memset(&dev, 0, sizeof(dev));
952 dev.device_data = timer;
953 return snd_timer_dev_register(&dev);
954}
955
53d2f744 956int snd_timer_global_unregister(struct snd_timer *timer)
1da177e4
LT
957{
958 return snd_timer_unregister(timer);
959}
960
6b172a85 961/*
1da177e4
LT
962 * System timer
963 */
964
965struct snd_timer_system_private {
966 struct timer_list tlist;
967 struct timer * timer;
968 unsigned long last_expires;
969 unsigned long last_jiffies;
970 unsigned long correction;
971};
972
1da177e4
LT
973static void snd_timer_s_function(unsigned long data)
974{
53d2f744 975 struct snd_timer *timer = (struct snd_timer *)data;
1da177e4
LT
976 struct snd_timer_system_private *priv = timer->private_data;
977 unsigned long jiff = jiffies;
978 if (time_after(jiff, priv->last_expires))
979 priv->correction = (long)jiff - (long)priv->last_expires;
980 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
981}
982
53d2f744 983static int snd_timer_s_start(struct snd_timer * timer)
1da177e4
LT
984{
985 struct snd_timer_system_private *priv;
986 unsigned long njiff;
987
988 priv = (struct snd_timer_system_private *) timer->private_data;
989 njiff = (priv->last_jiffies = jiffies);
990 if (priv->correction > timer->sticks - 1) {
991 priv->correction -= timer->sticks - 1;
992 njiff++;
993 } else {
994 njiff += timer->sticks - priv->correction;
995 priv->correction -= timer->sticks;
996 }
997 priv->last_expires = priv->tlist.expires = njiff;
998 add_timer(&priv->tlist);
999 return 0;
1000}
1001
53d2f744 1002static int snd_timer_s_stop(struct snd_timer * timer)
1da177e4
LT
1003{
1004 struct snd_timer_system_private *priv;
1005 unsigned long jiff;
1006
1007 priv = (struct snd_timer_system_private *) timer->private_data;
1008 del_timer(&priv->tlist);
1009 jiff = jiffies;
1010 if (time_before(jiff, priv->last_expires))
1011 timer->sticks = priv->last_expires - jiff;
1012 else
1013 timer->sticks = 1;
1014 return 0;
1015}
1016
53d2f744 1017static struct snd_timer_hardware snd_timer_system =
1da177e4
LT
1018{
1019 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1020 .resolution = 1000000000L / HZ,
1021 .ticks = 10000000L,
1022 .start = snd_timer_s_start,
1023 .stop = snd_timer_s_stop
1024};
1025
53d2f744 1026static void snd_timer_free_system(struct snd_timer *timer)
1da177e4
LT
1027{
1028 kfree(timer->private_data);
1029}
1030
1031static int snd_timer_register_system(void)
1032{
53d2f744 1033 struct snd_timer *timer;
1da177e4
LT
1034 struct snd_timer_system_private *priv;
1035 int err;
1036
6b172a85
CL
1037 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1038 if (err < 0)
1da177e4
LT
1039 return err;
1040 strcpy(timer->name, "system timer");
1041 timer->hw = snd_timer_system;
ca2c0966 1042 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1da177e4
LT
1043 if (priv == NULL) {
1044 snd_timer_free(timer);
1045 return -ENOMEM;
1046 }
1047 init_timer(&priv->tlist);
1048 priv->tlist.function = snd_timer_s_function;
1049 priv->tlist.data = (unsigned long) timer;
1050 timer->private_data = priv;
1051 timer->private_free = snd_timer_free_system;
1052 return snd_timer_global_register(timer);
1053}
1054
e28563cc 1055#ifdef CONFIG_PROC_FS
1da177e4
LT
1056/*
1057 * Info interface
1058 */
1059
53d2f744
TI
1060static void snd_timer_proc_read(struct snd_info_entry *entry,
1061 struct snd_info_buffer *buffer)
1da177e4
LT
1062{
1063 unsigned long flags;
53d2f744
TI
1064 struct snd_timer *timer;
1065 struct snd_timer_instance *ti;
1da177e4
LT
1066 struct list_head *p, *q;
1067
1068 down(&register_mutex);
1069 list_for_each(p, &snd_timer_list) {
53d2f744 1070 timer = list_entry(p, struct snd_timer, device_list);
1da177e4
LT
1071 switch (timer->tmr_class) {
1072 case SNDRV_TIMER_CLASS_GLOBAL:
1073 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1074 break;
1075 case SNDRV_TIMER_CLASS_CARD:
6b172a85
CL
1076 snd_iprintf(buffer, "C%i-%i: ",
1077 timer->card->number, timer->tmr_device);
1da177e4
LT
1078 break;
1079 case SNDRV_TIMER_CLASS_PCM:
6b172a85
CL
1080 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1081 timer->tmr_device, timer->tmr_subdevice);
1da177e4
LT
1082 break;
1083 default:
6b172a85
CL
1084 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1085 timer->card ? timer->card->number : -1,
1086 timer->tmr_device, timer->tmr_subdevice);
1da177e4
LT
1087 }
1088 snd_iprintf(buffer, "%s :", timer->name);
1089 if (timer->hw.resolution)
6b172a85
CL
1090 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1091 timer->hw.resolution / 1000,
1092 timer->hw.resolution % 1000,
1093 timer->hw.ticks);
1da177e4
LT
1094 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1095 snd_iprintf(buffer, " SLAVE");
1096 snd_iprintf(buffer, "\n");
1097 spin_lock_irqsave(&timer->lock, flags);
1098 list_for_each(q, &timer->open_list_head) {
53d2f744 1099 ti = list_entry(q, struct snd_timer_instance, open_list);
6b172a85
CL
1100 snd_iprintf(buffer, " Client %s : %s\n",
1101 ti->owner ? ti->owner : "unknown",
1102 ti->flags & (SNDRV_TIMER_IFLG_START |
1103 SNDRV_TIMER_IFLG_RUNNING)
1104 ? "running" : "stopped");
1da177e4
LT
1105 }
1106 spin_unlock_irqrestore(&timer->lock, flags);
1107 }
1108 up(&register_mutex);
1109}
1110
e28563cc
TI
1111static struct snd_info_entry *snd_timer_proc_entry = NULL;
1112
1113static void __init snd_timer_proc_init(void)
1114{
1115 struct snd_info_entry *entry;
1116
1117 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1118 if (entry != NULL) {
1119 entry->c.text.read_size = SNDRV_TIMER_DEVICES * 128;
1120 entry->c.text.read = snd_timer_proc_read;
1121 if (snd_info_register(entry) < 0) {
1122 snd_info_free_entry(entry);
1123 entry = NULL;
1124 }
1125 }
1126 snd_timer_proc_entry = entry;
1127}
1128
1129static void __exit snd_timer_proc_done(void)
1130{
1131 snd_info_unregister(snd_timer_proc_entry);
1132}
1133#else /* !CONFIG_PROC_FS */
1134#define snd_timer_proc_init()
1135#define snd_timer_proc_done()
1136#endif
1137
1da177e4
LT
1138/*
1139 * USER SPACE interface
1140 */
1141
53d2f744 1142static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1da177e4
LT
1143 unsigned long resolution,
1144 unsigned long ticks)
1145{
53d2f744
TI
1146 struct snd_timer_user *tu = timeri->callback_data;
1147 struct snd_timer_read *r;
1da177e4 1148 int prev;
6b172a85 1149
1da177e4
LT
1150 spin_lock(&tu->qlock);
1151 if (tu->qused > 0) {
1152 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1153 r = &tu->queue[prev];
1154 if (r->resolution == resolution) {
1155 r->ticks += ticks;
1156 goto __wake;
1157 }
1158 }
1159 if (tu->qused >= tu->queue_size) {
1160 tu->overrun++;
1161 } else {
1162 r = &tu->queue[tu->qtail++];
1163 tu->qtail %= tu->queue_size;
1164 r->resolution = resolution;
1165 r->ticks = ticks;
1166 tu->qused++;
1167 }
1168 __wake:
1169 spin_unlock(&tu->qlock);
1170 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1171 wake_up(&tu->qchange_sleep);
1172}
1173
53d2f744
TI
1174static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1175 struct snd_timer_tread *tread)
1da177e4
LT
1176{
1177 if (tu->qused >= tu->queue_size) {
1178 tu->overrun++;
1179 } else {
1180 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1181 tu->qtail %= tu->queue_size;
1182 tu->qused++;
1183 }
1184}
1185
53d2f744
TI
1186static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1187 int event,
1da177e4
LT
1188 struct timespec *tstamp,
1189 unsigned long resolution)
1190{
53d2f744
TI
1191 struct snd_timer_user *tu = timeri->callback_data;
1192 struct snd_timer_tread r1;
1da177e4 1193
6b172a85
CL
1194 if (event >= SNDRV_TIMER_EVENT_START &&
1195 event <= SNDRV_TIMER_EVENT_PAUSE)
1da177e4
LT
1196 tu->tstamp = *tstamp;
1197 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1198 return;
1199 r1.event = event;
1200 r1.tstamp = *tstamp;
1201 r1.val = resolution;
1202 spin_lock(&tu->qlock);
1203 snd_timer_user_append_to_tqueue(tu, &r1);
1204 spin_unlock(&tu->qlock);
1205 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1206 wake_up(&tu->qchange_sleep);
1207}
1208
53d2f744 1209static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1da177e4
LT
1210 unsigned long resolution,
1211 unsigned long ticks)
1212{
53d2f744
TI
1213 struct snd_timer_user *tu = timeri->callback_data;
1214 struct snd_timer_tread *r, r1;
1da177e4
LT
1215 struct timespec tstamp;
1216 int prev, append = 0;
1217
07799e75 1218 memset(&tstamp, 0, sizeof(tstamp));
1da177e4 1219 spin_lock(&tu->qlock);
6b172a85
CL
1220 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1221 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1da177e4
LT
1222 spin_unlock(&tu->qlock);
1223 return;
1224 }
1225 if (tu->last_resolution != resolution || ticks > 0)
07799e75 1226 getnstimeofday(&tstamp);
6b172a85
CL
1227 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1228 tu->last_resolution != resolution) {
1da177e4
LT
1229 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1230 r1.tstamp = tstamp;
1231 r1.val = resolution;
1232 snd_timer_user_append_to_tqueue(tu, &r1);
1233 tu->last_resolution = resolution;
1234 append++;
1235 }
1236 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1237 goto __wake;
1238 if (ticks == 0)
1239 goto __wake;
1240 if (tu->qused > 0) {
1241 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1242 r = &tu->tqueue[prev];
1243 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1244 r->tstamp = tstamp;
1245 r->val += ticks;
1246 append++;
1247 goto __wake;
1248 }
1249 }
1250 r1.event = SNDRV_TIMER_EVENT_TICK;
1251 r1.tstamp = tstamp;
1252 r1.val = ticks;
1253 snd_timer_user_append_to_tqueue(tu, &r1);
1254 append++;
1255 __wake:
1256 spin_unlock(&tu->qlock);
1257 if (append == 0)
1258 return;
1259 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1260 wake_up(&tu->qchange_sleep);
1261}
1262
1263static int snd_timer_user_open(struct inode *inode, struct file *file)
1264{
53d2f744 1265 struct snd_timer_user *tu;
6b172a85 1266
ca2c0966 1267 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1da177e4
LT
1268 if (tu == NULL)
1269 return -ENOMEM;
1270 spin_lock_init(&tu->qlock);
1271 init_waitqueue_head(&tu->qchange_sleep);
c1935b4d 1272 init_MUTEX(&tu->tread_sem);
1da177e4
LT
1273 tu->ticks = 1;
1274 tu->queue_size = 128;
53d2f744 1275 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
6b172a85 1276 GFP_KERNEL);
1da177e4
LT
1277 if (tu->queue == NULL) {
1278 kfree(tu);
1279 return -ENOMEM;
1280 }
1281 file->private_data = tu;
1282 return 0;
1283}
1284
1285static int snd_timer_user_release(struct inode *inode, struct file *file)
1286{
53d2f744 1287 struct snd_timer_user *tu;
1da177e4
LT
1288
1289 if (file->private_data) {
1290 tu = file->private_data;
1291 file->private_data = NULL;
1292 fasync_helper(-1, file, 0, &tu->fasync);
1293 if (tu->timeri)
1294 snd_timer_close(tu->timeri);
1295 kfree(tu->queue);
1296 kfree(tu->tqueue);
1297 kfree(tu);
1298 }
1299 return 0;
1300}
1301
53d2f744 1302static void snd_timer_user_zero_id(struct snd_timer_id *id)
1da177e4
LT
1303{
1304 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1305 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1306 id->card = -1;
1307 id->device = -1;
1308 id->subdevice = -1;
1309}
1310
53d2f744 1311static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1da177e4
LT
1312{
1313 id->dev_class = timer->tmr_class;
1314 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1315 id->card = timer->card ? timer->card->number : -1;
1316 id->device = timer->tmr_device;
1317 id->subdevice = timer->tmr_subdevice;
1318}
1319
53d2f744 1320static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1da177e4 1321{
53d2f744
TI
1322 struct snd_timer_id id;
1323 struct snd_timer *timer;
1da177e4 1324 struct list_head *p;
6b172a85 1325
1da177e4
LT
1326 if (copy_from_user(&id, _tid, sizeof(id)))
1327 return -EFAULT;
1328 down(&register_mutex);
1329 if (id.dev_class < 0) { /* first item */
1330 if (list_empty(&snd_timer_list))
1331 snd_timer_user_zero_id(&id);
1332 else {
9dfba380 1333 timer = list_entry(snd_timer_list.next,
53d2f744 1334 struct snd_timer, device_list);
1da177e4
LT
1335 snd_timer_user_copy_id(&id, timer);
1336 }
1337 } else {
1338 switch (id.dev_class) {
1339 case SNDRV_TIMER_CLASS_GLOBAL:
1340 id.device = id.device < 0 ? 0 : id.device + 1;
1341 list_for_each(p, &snd_timer_list) {
53d2f744 1342 timer = list_entry(p, struct snd_timer, device_list);
1da177e4
LT
1343 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1344 snd_timer_user_copy_id(&id, timer);
1345 break;
1346 }
1347 if (timer->tmr_device >= id.device) {
1348 snd_timer_user_copy_id(&id, timer);
1349 break;
1350 }
1351 }
1352 if (p == &snd_timer_list)
1353 snd_timer_user_zero_id(&id);
1354 break;
1355 case SNDRV_TIMER_CLASS_CARD:
1356 case SNDRV_TIMER_CLASS_PCM:
1357 if (id.card < 0) {
1358 id.card = 0;
1359 } else {
1360 if (id.card < 0) {
1361 id.card = 0;
1362 } else {
1363 if (id.device < 0) {
1364 id.device = 0;
1365 } else {
6b172a85
CL
1366 if (id.subdevice < 0) {
1367 id.subdevice = 0;
1368 } else {
1369 id.subdevice++;
1370 }
1da177e4
LT
1371 }
1372 }
1373 }
1374 list_for_each(p, &snd_timer_list) {
53d2f744 1375 timer = list_entry(p, struct snd_timer, device_list);
1da177e4
LT
1376 if (timer->tmr_class > id.dev_class) {
1377 snd_timer_user_copy_id(&id, timer);
1378 break;
1379 }
1380 if (timer->tmr_class < id.dev_class)
1381 continue;
1382 if (timer->card->number > id.card) {
1383 snd_timer_user_copy_id(&id, timer);
1384 break;
1385 }
1386 if (timer->card->number < id.card)
1387 continue;
1388 if (timer->tmr_device > id.device) {
1389 snd_timer_user_copy_id(&id, timer);
1390 break;
1391 }
1392 if (timer->tmr_device < id.device)
1393 continue;
1394 if (timer->tmr_subdevice > id.subdevice) {
1395 snd_timer_user_copy_id(&id, timer);
1396 break;
1397 }
1398 if (timer->tmr_subdevice < id.subdevice)
1399 continue;
1400 snd_timer_user_copy_id(&id, timer);
1401 break;
1402 }
1403 if (p == &snd_timer_list)
1404 snd_timer_user_zero_id(&id);
1405 break;
1406 default:
1407 snd_timer_user_zero_id(&id);
1408 }
1409 }
1410 up(&register_mutex);
1411 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1412 return -EFAULT;
1413 return 0;
6b172a85 1414}
1da177e4 1415
6b172a85 1416static int snd_timer_user_ginfo(struct file *file,
53d2f744 1417 struct snd_timer_ginfo __user *_ginfo)
1da177e4 1418{
53d2f744
TI
1419 struct snd_timer_ginfo *ginfo;
1420 struct snd_timer_id tid;
1421 struct snd_timer *t;
1da177e4
LT
1422 struct list_head *p;
1423 int err = 0;
1424
1425 ginfo = kmalloc(sizeof(*ginfo), GFP_KERNEL);
1426 if (! ginfo)
1427 return -ENOMEM;
1428 if (copy_from_user(ginfo, _ginfo, sizeof(*ginfo))) {
1429 kfree(ginfo);
1430 return -EFAULT;
1431 }
1432 tid = ginfo->tid;
1433 memset(ginfo, 0, sizeof(*ginfo));
1434 ginfo->tid = tid;
1435 down(&register_mutex);
1436 t = snd_timer_find(&tid);
1437 if (t != NULL) {
1438 ginfo->card = t->card ? t->card->number : -1;
1439 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1440 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1441 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1442 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1443 ginfo->resolution = t->hw.resolution;
1444 if (t->hw.resolution_min > 0) {
1445 ginfo->resolution_min = t->hw.resolution_min;
1446 ginfo->resolution_max = t->hw.resolution_max;
1447 }
1448 list_for_each(p, &t->open_list_head) {
1449 ginfo->clients++;
1450 }
1451 } else {
1452 err = -ENODEV;
1453 }
1454 up(&register_mutex);
1455 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1456 err = -EFAULT;
1457 kfree(ginfo);
1458 return err;
1459}
1460
6b172a85 1461static int snd_timer_user_gparams(struct file *file,
53d2f744 1462 struct snd_timer_gparams __user *_gparams)
1da177e4 1463{
53d2f744
TI
1464 struct snd_timer_gparams gparams;
1465 struct snd_timer *t;
1da177e4
LT
1466 int err;
1467
1468 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1469 return -EFAULT;
1470 down(&register_mutex);
1471 t = snd_timer_find(&gparams.tid);
6b172a85 1472 if (!t) {
1da177e4 1473 err = -ENODEV;
6b172a85
CL
1474 goto _error;
1475 }
1476 if (!list_empty(&t->open_list_head)) {
1477 err = -EBUSY;
1478 goto _error;
1479 }
1480 if (!t->hw.set_period) {
1481 err = -ENOSYS;
1482 goto _error;
1da177e4 1483 }
6b172a85
CL
1484 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1485_error:
1da177e4
LT
1486 up(&register_mutex);
1487 return err;
1488}
1489
6b172a85 1490static int snd_timer_user_gstatus(struct file *file,
53d2f744 1491 struct snd_timer_gstatus __user *_gstatus)
1da177e4 1492{
53d2f744
TI
1493 struct snd_timer_gstatus gstatus;
1494 struct snd_timer_id tid;
1495 struct snd_timer *t;
1da177e4
LT
1496 int err = 0;
1497
1498 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1499 return -EFAULT;
1500 tid = gstatus.tid;
1501 memset(&gstatus, 0, sizeof(gstatus));
1502 gstatus.tid = tid;
1503 down(&register_mutex);
1504 t = snd_timer_find(&tid);
1505 if (t != NULL) {
1506 if (t->hw.c_resolution)
1507 gstatus.resolution = t->hw.c_resolution(t);
1508 else
1509 gstatus.resolution = t->hw.resolution;
1510 if (t->hw.precise_resolution) {
6b172a85
CL
1511 t->hw.precise_resolution(t, &gstatus.resolution_num,
1512 &gstatus.resolution_den);
1da177e4
LT
1513 } else {
1514 gstatus.resolution_num = gstatus.resolution;
1515 gstatus.resolution_den = 1000000000uL;
1516 }
1517 } else {
1518 err = -ENODEV;
1519 }
1520 up(&register_mutex);
1521 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1522 err = -EFAULT;
1523 return err;
1524}
1525
6b172a85 1526static int snd_timer_user_tselect(struct file *file,
53d2f744 1527 struct snd_timer_select __user *_tselect)
1da177e4 1528{
53d2f744
TI
1529 struct snd_timer_user *tu;
1530 struct snd_timer_select tselect;
1da177e4 1531 char str[32];
c1935b4d 1532 int err = 0;
6b172a85 1533
1da177e4 1534 tu = file->private_data;
c1935b4d
JK
1535 down(&tu->tread_sem);
1536 if (tu->timeri) {
1da177e4 1537 snd_timer_close(tu->timeri);
c1935b4d
JK
1538 tu->timeri = NULL;
1539 }
1540 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1541 err = -EFAULT;
1542 goto __err;
1543 }
1da177e4
LT
1544 sprintf(str, "application %i", current->pid);
1545 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1546 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
6b172a85
CL
1547 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1548 if (err < 0)
c1935b4d 1549 goto __err;
1da177e4 1550
4d572776
JJ
1551 kfree(tu->queue);
1552 tu->queue = NULL;
1553 kfree(tu->tqueue);
1554 tu->tqueue = NULL;
1da177e4 1555 if (tu->tread) {
53d2f744 1556 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
6b172a85 1557 GFP_KERNEL);
c1935b4d
JK
1558 if (tu->tqueue == NULL)
1559 err = -ENOMEM;
1da177e4 1560 } else {
53d2f744 1561 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
6b172a85 1562 GFP_KERNEL);
c1935b4d
JK
1563 if (tu->queue == NULL)
1564 err = -ENOMEM;
1da177e4 1565 }
6b172a85 1566
c1935b4d
JK
1567 if (err < 0) {
1568 snd_timer_close(tu->timeri);
1569 tu->timeri = NULL;
1570 } else {
1571 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
6b172a85
CL
1572 tu->timeri->callback = tu->tread
1573 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
c1935b4d
JK
1574 tu->timeri->ccallback = snd_timer_user_ccallback;
1575 tu->timeri->callback_data = (void *)tu;
1576 }
1577
1578 __err:
1579 up(&tu->tread_sem);
1580 return err;
1da177e4
LT
1581}
1582
6b172a85 1583static int snd_timer_user_info(struct file *file,
53d2f744 1584 struct snd_timer_info __user *_info)
1da177e4 1585{
53d2f744
TI
1586 struct snd_timer_user *tu;
1587 struct snd_timer_info *info;
1588 struct snd_timer *t;
1da177e4
LT
1589 int err = 0;
1590
1591 tu = file->private_data;
1592 snd_assert(tu->timeri != NULL, return -ENXIO);
1593 t = tu->timeri->timer;
1594 snd_assert(t != NULL, return -ENXIO);
1595
ca2c0966 1596 info = kzalloc(sizeof(*info), GFP_KERNEL);
1da177e4
LT
1597 if (! info)
1598 return -ENOMEM;
1599 info->card = t->card ? t->card->number : -1;
1600 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1601 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1602 strlcpy(info->id, t->id, sizeof(info->id));
1603 strlcpy(info->name, t->name, sizeof(info->name));
1604 info->resolution = t->hw.resolution;
1605 if (copy_to_user(_info, info, sizeof(*_info)))
1606 err = -EFAULT;
1607 kfree(info);
1608 return err;
1609}
1610
6b172a85 1611static int snd_timer_user_params(struct file *file,
53d2f744 1612 struct snd_timer_params __user *_params)
1da177e4 1613{
53d2f744
TI
1614 struct snd_timer_user *tu;
1615 struct snd_timer_params params;
1616 struct snd_timer *t;
1617 struct snd_timer_read *tr;
1618 struct snd_timer_tread *ttr;
1da177e4 1619 int err;
6b172a85 1620
1da177e4
LT
1621 tu = file->private_data;
1622 snd_assert(tu->timeri != NULL, return -ENXIO);
1623 t = tu->timeri->timer;
1624 snd_assert(t != NULL, return -ENXIO);
1625 if (copy_from_user(&params, _params, sizeof(params)))
1626 return -EFAULT;
1627 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1628 err = -EINVAL;
1629 goto _end;
1630 }
6b172a85
CL
1631 if (params.queue_size > 0 &&
1632 (params.queue_size < 32 || params.queue_size > 1024)) {
1da177e4
LT
1633 err = -EINVAL;
1634 goto _end;
1635 }
1636 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1637 (1<<SNDRV_TIMER_EVENT_TICK)|
1638 (1<<SNDRV_TIMER_EVENT_START)|
1639 (1<<SNDRV_TIMER_EVENT_STOP)|
1640 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1641 (1<<SNDRV_TIMER_EVENT_PAUSE)|
a501dfa3
JK
1642 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1643 (1<<SNDRV_TIMER_EVENT_RESUME)|
1da177e4
LT
1644 (1<<SNDRV_TIMER_EVENT_MSTART)|
1645 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1646 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
65d11d95
JK
1647 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1648 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
a501dfa3 1649 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1da177e4
LT
1650 err = -EINVAL;
1651 goto _end;
1652 }
1653 snd_timer_stop(tu->timeri);
1654 spin_lock_irq(&t->lock);
1655 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1656 SNDRV_TIMER_IFLG_EXCLUSIVE|
1657 SNDRV_TIMER_IFLG_EARLY_EVENT);
1658 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1659 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1660 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1661 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1662 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1663 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1664 spin_unlock_irq(&t->lock);
6b172a85
CL
1665 if (params.queue_size > 0 &&
1666 (unsigned int)tu->queue_size != params.queue_size) {
1da177e4 1667 if (tu->tread) {
6b172a85
CL
1668 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1669 GFP_KERNEL);
1da177e4
LT
1670 if (ttr) {
1671 kfree(tu->tqueue);
1672 tu->queue_size = params.queue_size;
1673 tu->tqueue = ttr;
1674 }
1675 } else {
6b172a85
CL
1676 tr = kmalloc(params.queue_size * sizeof(*tr),
1677 GFP_KERNEL);
1da177e4
LT
1678 if (tr) {
1679 kfree(tu->queue);
1680 tu->queue_size = params.queue_size;
1681 tu->queue = tr;
1682 }
1683 }
1684 }
1685 tu->qhead = tu->qtail = tu->qused = 0;
1686 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1687 if (tu->tread) {
53d2f744 1688 struct snd_timer_tread tread;
1da177e4
LT
1689 tread.event = SNDRV_TIMER_EVENT_EARLY;
1690 tread.tstamp.tv_sec = 0;
1691 tread.tstamp.tv_nsec = 0;
1692 tread.val = 0;
1693 snd_timer_user_append_to_tqueue(tu, &tread);
1694 } else {
53d2f744 1695 struct snd_timer_read *r = &tu->queue[0];
1da177e4
LT
1696 r->resolution = 0;
1697 r->ticks = 0;
1698 tu->qused++;
1699 tu->qtail++;
1700 }
1da177e4
LT
1701 }
1702 tu->filter = params.filter;
1703 tu->ticks = params.ticks;
1704 err = 0;
1705 _end:
1706 if (copy_to_user(_params, &params, sizeof(params)))
1707 return -EFAULT;
1708 return err;
1709}
1710
6b172a85 1711static int snd_timer_user_status(struct file *file,
53d2f744 1712 struct snd_timer_status __user *_status)
1da177e4 1713{
53d2f744
TI
1714 struct snd_timer_user *tu;
1715 struct snd_timer_status status;
6b172a85 1716
1da177e4
LT
1717 tu = file->private_data;
1718 snd_assert(tu->timeri != NULL, return -ENXIO);
1719 memset(&status, 0, sizeof(status));
1720 status.tstamp = tu->tstamp;
1721 status.resolution = snd_timer_resolution(tu->timeri);
1722 status.lost = tu->timeri->lost;
1723 status.overrun = tu->overrun;
1724 spin_lock_irq(&tu->qlock);
1725 status.queue = tu->qused;
1726 spin_unlock_irq(&tu->qlock);
1727 if (copy_to_user(_status, &status, sizeof(status)))
1728 return -EFAULT;
1729 return 0;
1730}
1731
1732static int snd_timer_user_start(struct file *file)
1733{
1734 int err;
53d2f744 1735 struct snd_timer_user *tu;
6b172a85 1736
1da177e4
LT
1737 tu = file->private_data;
1738 snd_assert(tu->timeri != NULL, return -ENXIO);
1739 snd_timer_stop(tu->timeri);
1740 tu->timeri->lost = 0;
1741 tu->last_resolution = 0;
1742 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1743}
1744
1745static int snd_timer_user_stop(struct file *file)
1746{
1747 int err;
53d2f744 1748 struct snd_timer_user *tu;
6b172a85 1749
1da177e4
LT
1750 tu = file->private_data;
1751 snd_assert(tu->timeri != NULL, return -ENXIO);
1752 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1753}
1754
1755static int snd_timer_user_continue(struct file *file)
1756{
1757 int err;
53d2f744 1758 struct snd_timer_user *tu;
6b172a85 1759
1da177e4
LT
1760 tu = file->private_data;
1761 snd_assert(tu->timeri != NULL, return -ENXIO);
1762 tu->timeri->lost = 0;
1763 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1764}
1765
15790a6b
TI
1766static int snd_timer_user_pause(struct file *file)
1767{
1768 int err;
53d2f744 1769 struct snd_timer_user *tu;
6b172a85 1770
15790a6b
TI
1771 tu = file->private_data;
1772 snd_assert(tu->timeri != NULL, return -ENXIO);
d138b445 1773 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
15790a6b
TI
1774}
1775
8c50b37c
TI
1776enum {
1777 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1778 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1779 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1780 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1781};
1782
6b172a85
CL
1783static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1784 unsigned long arg)
1da177e4 1785{
53d2f744 1786 struct snd_timer_user *tu;
1da177e4
LT
1787 void __user *argp = (void __user *)arg;
1788 int __user *p = argp;
6b172a85 1789
1da177e4
LT
1790 tu = file->private_data;
1791 switch (cmd) {
1792 case SNDRV_TIMER_IOCTL_PVERSION:
1793 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1794 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1795 return snd_timer_user_next_device(argp);
1796 case SNDRV_TIMER_IOCTL_TREAD:
1797 {
1798 int xarg;
6b172a85 1799
c1935b4d
JK
1800 down(&tu->tread_sem);
1801 if (tu->timeri) { /* too late */
1802 up(&tu->tread_sem);
1da177e4 1803 return -EBUSY;
c1935b4d
JK
1804 }
1805 if (get_user(xarg, p)) {
1806 up(&tu->tread_sem);
1da177e4 1807 return -EFAULT;
c1935b4d 1808 }
1da177e4 1809 tu->tread = xarg ? 1 : 0;
c1935b4d 1810 up(&tu->tread_sem);
1da177e4
LT
1811 return 0;
1812 }
1813 case SNDRV_TIMER_IOCTL_GINFO:
1814 return snd_timer_user_ginfo(file, argp);
1815 case SNDRV_TIMER_IOCTL_GPARAMS:
1816 return snd_timer_user_gparams(file, argp);
1817 case SNDRV_TIMER_IOCTL_GSTATUS:
1818 return snd_timer_user_gstatus(file, argp);
1819 case SNDRV_TIMER_IOCTL_SELECT:
1820 return snd_timer_user_tselect(file, argp);
1821 case SNDRV_TIMER_IOCTL_INFO:
1822 return snd_timer_user_info(file, argp);
1823 case SNDRV_TIMER_IOCTL_PARAMS:
1824 return snd_timer_user_params(file, argp);
1825 case SNDRV_TIMER_IOCTL_STATUS:
1826 return snd_timer_user_status(file, argp);
1827 case SNDRV_TIMER_IOCTL_START:
8c50b37c 1828 case SNDRV_TIMER_IOCTL_START_OLD:
1da177e4
LT
1829 return snd_timer_user_start(file);
1830 case SNDRV_TIMER_IOCTL_STOP:
8c50b37c 1831 case SNDRV_TIMER_IOCTL_STOP_OLD:
1da177e4
LT
1832 return snd_timer_user_stop(file);
1833 case SNDRV_TIMER_IOCTL_CONTINUE:
8c50b37c 1834 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1da177e4 1835 return snd_timer_user_continue(file);
15790a6b 1836 case SNDRV_TIMER_IOCTL_PAUSE:
8c50b37c 1837 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
15790a6b 1838 return snd_timer_user_pause(file);
1da177e4
LT
1839 }
1840 return -ENOTTY;
1841}
1842
1843static int snd_timer_user_fasync(int fd, struct file * file, int on)
1844{
53d2f744 1845 struct snd_timer_user *tu;
1da177e4 1846 int err;
6b172a85 1847
1da177e4
LT
1848 tu = file->private_data;
1849 err = fasync_helper(fd, file, on, &tu->fasync);
1850 if (err < 0)
1851 return err;
1852 return 0;
1853}
1854
6b172a85
CL
1855static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1856 size_t count, loff_t *offset)
1da177e4 1857{
53d2f744 1858 struct snd_timer_user *tu;
1da177e4
LT
1859 long result = 0, unit;
1860 int err = 0;
6b172a85 1861
1da177e4 1862 tu = file->private_data;
53d2f744 1863 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1da177e4
LT
1864 spin_lock_irq(&tu->qlock);
1865 while ((long)count - result >= unit) {
1866 while (!tu->qused) {
1867 wait_queue_t wait;
1868
1869 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1870 err = -EAGAIN;
1871 break;
1872 }
1873
1874 set_current_state(TASK_INTERRUPTIBLE);
1875 init_waitqueue_entry(&wait, current);
1876 add_wait_queue(&tu->qchange_sleep, &wait);
1877
1878 spin_unlock_irq(&tu->qlock);
1879 schedule();
1880 spin_lock_irq(&tu->qlock);
1881
1882 remove_wait_queue(&tu->qchange_sleep, &wait);
1883
1884 if (signal_pending(current)) {
1885 err = -ERESTARTSYS;
1886 break;
1887 }
1888 }
1889
1890 spin_unlock_irq(&tu->qlock);
1891 if (err < 0)
1892 goto _error;
1893
1894 if (tu->tread) {
6b172a85 1895 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
53d2f744 1896 sizeof(struct snd_timer_tread))) {
1da177e4
LT
1897 err = -EFAULT;
1898 goto _error;
1899 }
1900 } else {
6b172a85 1901 if (copy_to_user(buffer, &tu->queue[tu->qhead++],
53d2f744 1902 sizeof(struct snd_timer_read))) {
1da177e4
LT
1903 err = -EFAULT;
1904 goto _error;
1905 }
1906 }
1907
1908 tu->qhead %= tu->queue_size;
1909
1910 result += unit;
1911 buffer += unit;
1912
1913 spin_lock_irq(&tu->qlock);
1914 tu->qused--;
1915 }
1916 spin_unlock_irq(&tu->qlock);
1917 _error:
1918 return result > 0 ? result : err;
1919}
1920
1921static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1922{
1923 unsigned int mask;
53d2f744 1924 struct snd_timer_user *tu;
1da177e4
LT
1925
1926 tu = file->private_data;
1927
1928 poll_wait(file, &tu->qchange_sleep, wait);
6b172a85 1929
1da177e4
LT
1930 mask = 0;
1931 if (tu->qused)
1932 mask |= POLLIN | POLLRDNORM;
1933
1934 return mask;
1935}
1936
1937#ifdef CONFIG_COMPAT
1938#include "timer_compat.c"
1939#else
1940#define snd_timer_user_ioctl_compat NULL
1941#endif
1942
1943static struct file_operations snd_timer_f_ops =
1944{
1945 .owner = THIS_MODULE,
1946 .read = snd_timer_user_read,
1947 .open = snd_timer_user_open,
1948 .release = snd_timer_user_release,
1949 .poll = snd_timer_user_poll,
1950 .unlocked_ioctl = snd_timer_user_ioctl,
1951 .compat_ioctl = snd_timer_user_ioctl_compat,
1952 .fasync = snd_timer_user_fasync,
1953};
1954
1da177e4
LT
1955/*
1956 * ENTRY functions
1957 */
1958
1da177e4
LT
1959static int __init alsa_timer_init(void)
1960{
1961 int err;
1da177e4
LT
1962
1963#ifdef SNDRV_OSS_INFO_DEV_TIMERS
6b172a85
CL
1964 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1965 "system timer");
1da177e4 1966#endif
e28563cc 1967
1da177e4 1968 if ((err = snd_timer_register_system()) < 0)
6b172a85
CL
1969 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
1970 err);
f87135f5
CL
1971 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
1972 &snd_timer_f_ops, NULL, "timer")) < 0)
6b172a85
CL
1973 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
1974 err);
e28563cc 1975 snd_timer_proc_init();
1da177e4
LT
1976 return 0;
1977}
1978
1979static void __exit alsa_timer_exit(void)
1980{
1981 struct list_head *p, *n;
1982
1983 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1984 /* unregister the system timer */
1985 list_for_each_safe(p, n, &snd_timer_list) {
53d2f744 1986 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
1da177e4
LT
1987 snd_timer_unregister(timer);
1988 }
e28563cc 1989 snd_timer_proc_done();
1da177e4
LT
1990#ifdef SNDRV_OSS_INFO_DEV_TIMERS
1991 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1992#endif
1993}
1994
1995module_init(alsa_timer_init)
1996module_exit(alsa_timer_exit)
1997
1998EXPORT_SYMBOL(snd_timer_open);
1999EXPORT_SYMBOL(snd_timer_close);
2000EXPORT_SYMBOL(snd_timer_resolution);
2001EXPORT_SYMBOL(snd_timer_start);
2002EXPORT_SYMBOL(snd_timer_stop);
2003EXPORT_SYMBOL(snd_timer_continue);
2004EXPORT_SYMBOL(snd_timer_pause);
2005EXPORT_SYMBOL(snd_timer_new);
2006EXPORT_SYMBOL(snd_timer_notify);
2007EXPORT_SYMBOL(snd_timer_global_new);
2008EXPORT_SYMBOL(snd_timer_global_free);
2009EXPORT_SYMBOL(snd_timer_global_register);
2010EXPORT_SYMBOL(snd_timer_global_unregister);
2011EXPORT_SYMBOL(snd_timer_interrupt);
This page took 0.204605 seconds and 5 git commands to generate.