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