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