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