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