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