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