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