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