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