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