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