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