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