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