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