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