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