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