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