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