[PATCH] create a kstrdup library function
[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 = kcalloc(1, 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 = kcalloc(1, 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 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 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         snd_runtime_check(timer->hw.flags & SNDRV_TIMER_HW_SLAVE, return);      
883         snd_assert(event >= SNDRV_TIMER_EVENT_MSTART && event <= SNDRV_TIMER_EVENT_MPAUSE, return);
884         spin_lock_irqsave(&timer->lock, flags);
885         if (event == SNDRV_TIMER_EVENT_MSTART || event == SNDRV_TIMER_EVENT_MCONTINUE) {
886                 if (timer->hw.c_resolution)
887                         resolution = timer->hw.c_resolution(timer);
888                 else
889                         resolution = timer->hw.resolution;
890         }
891         list_for_each(p, &timer->active_list_head) {
892                 ti = (snd_timer_instance_t *)list_entry(p, snd_timer_instance_t, active_list);
893                 if (ti->ccallback)
894                         ti->ccallback(ti, event, tstamp, resolution);
895                 list_for_each(n, &ti->slave_active_head) {
896                         ts = (snd_timer_instance_t *)list_entry(n, snd_timer_instance_t, active_list);
897                         if (ts->ccallback)
898                                 ts->ccallback(ts, event, tstamp, resolution);
899                 }
900         }
901         spin_unlock_irqrestore(&timer->lock, flags);
902 }
903
904 /*
905  * exported functions for global timers
906  */
907 int snd_timer_global_new(char *id, int device, snd_timer_t **rtimer)
908 {
909         snd_timer_id_t tid;
910         
911         tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
912         tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
913         tid.card = -1;
914         tid.device = device;
915         tid.subdevice = 0;
916         return snd_timer_new(NULL, id, &tid, rtimer);
917 }
918
919 int snd_timer_global_free(snd_timer_t *timer)
920 {
921         return snd_timer_free(timer);
922 }
923
924 int snd_timer_global_register(snd_timer_t *timer)
925 {
926         snd_device_t dev;
927
928         memset(&dev, 0, sizeof(dev));
929         dev.device_data = timer;
930         return snd_timer_dev_register(&dev);
931 }
932
933 int snd_timer_global_unregister(snd_timer_t *timer)
934 {
935         return snd_timer_unregister(timer);
936 }
937
938 /* 
939  *  System timer
940  */
941
942 struct snd_timer_system_private {
943         struct timer_list tlist;
944         struct timer * timer;
945         unsigned long last_expires;
946         unsigned long last_jiffies;
947         unsigned long correction;
948 };
949
950 static void snd_timer_s_function(unsigned long data)
951 {
952         snd_timer_t *timer = (snd_timer_t *)data;
953         struct snd_timer_system_private *priv = timer->private_data;
954         unsigned long jiff = jiffies;
955         if (time_after(jiff, priv->last_expires))
956                 priv->correction = (long)jiff - (long)priv->last_expires;
957         snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
958 }
959
960 static int snd_timer_s_start(snd_timer_t * timer)
961 {
962         struct snd_timer_system_private *priv;
963         unsigned long njiff;
964
965         priv = (struct snd_timer_system_private *) timer->private_data;
966         njiff = (priv->last_jiffies = jiffies);
967         if (priv->correction > timer->sticks - 1) {
968                 priv->correction -= timer->sticks - 1;
969                 njiff++;
970         } else {
971                 njiff += timer->sticks - priv->correction;
972                 priv->correction -= timer->sticks;
973         }
974         priv->last_expires = priv->tlist.expires = njiff;
975         add_timer(&priv->tlist);
976         return 0;
977 }
978
979 static int snd_timer_s_stop(snd_timer_t * timer)
980 {
981         struct snd_timer_system_private *priv;
982         unsigned long jiff;
983
984         priv = (struct snd_timer_system_private *) timer->private_data;
985         del_timer(&priv->tlist);
986         jiff = jiffies;
987         if (time_before(jiff, priv->last_expires))
988                 timer->sticks = priv->last_expires - jiff;
989         else
990                 timer->sticks = 1;
991         return 0;
992 }
993
994 static struct _snd_timer_hardware snd_timer_system =
995 {
996         .flags =        SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
997         .resolution =   1000000000L / HZ,
998         .ticks =        10000000L,
999         .start =        snd_timer_s_start,
1000         .stop =         snd_timer_s_stop
1001 };
1002
1003 static void snd_timer_free_system(snd_timer_t *timer)
1004 {
1005         kfree(timer->private_data);
1006 }
1007
1008 static int snd_timer_register_system(void)
1009 {
1010         snd_timer_t *timer;
1011         struct snd_timer_system_private *priv;
1012         int err;
1013
1014         if ((err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer)) < 0)
1015                 return err;
1016         strcpy(timer->name, "system timer");
1017         timer->hw = snd_timer_system;
1018         priv = kcalloc(1, sizeof(*priv), GFP_KERNEL);
1019         if (priv == NULL) {
1020                 snd_timer_free(timer);
1021                 return -ENOMEM;
1022         }
1023         init_timer(&priv->tlist);
1024         priv->tlist.function = snd_timer_s_function;
1025         priv->tlist.data = (unsigned long) timer;
1026         timer->private_data = priv;
1027         timer->private_free = snd_timer_free_system;
1028         return snd_timer_global_register(timer);
1029 }
1030
1031 /*
1032  *  Info interface
1033  */
1034
1035 static void snd_timer_proc_read(snd_info_entry_t *entry,
1036                                 snd_info_buffer_t * buffer)
1037 {
1038         unsigned long flags;
1039         snd_timer_t *timer;
1040         snd_timer_instance_t *ti;
1041         struct list_head *p, *q;
1042
1043         down(&register_mutex);
1044         list_for_each(p, &snd_timer_list) {
1045                 timer = (snd_timer_t *)list_entry(p, snd_timer_t, device_list);
1046                 switch (timer->tmr_class) {
1047                 case SNDRV_TIMER_CLASS_GLOBAL:
1048                         snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1049                         break;
1050                 case SNDRV_TIMER_CLASS_CARD:
1051                         snd_iprintf(buffer, "C%i-%i: ", timer->card->number, timer->tmr_device);
1052                         break;
1053                 case SNDRV_TIMER_CLASS_PCM:
1054                         snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number, timer->tmr_device, timer->tmr_subdevice);
1055                         break;
1056                 default:
1057                         snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class, timer->card ? timer->card->number : -1, timer->tmr_device, timer->tmr_subdevice);
1058                 }
1059                 snd_iprintf(buffer, "%s :", timer->name);
1060                 if (timer->hw.resolution)
1061                         snd_iprintf(buffer, " %lu.%03luus (%lu ticks)", timer->hw.resolution / 1000, timer->hw.resolution % 1000, timer->hw.ticks);
1062                 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1063                         snd_iprintf(buffer, " SLAVE");
1064                 snd_iprintf(buffer, "\n");
1065                 spin_lock_irqsave(&timer->lock, flags);
1066                 list_for_each(q, &timer->open_list_head) {
1067                         ti = (snd_timer_instance_t *)list_entry(q, snd_timer_instance_t, open_list);
1068                         snd_iprintf(buffer, "  Client %s : %s : lost interrupts %li\n",
1069                                         ti->owner ? ti->owner : "unknown",
1070                                         ti->flags & (SNDRV_TIMER_IFLG_START|SNDRV_TIMER_IFLG_RUNNING) ? "running" : "stopped",
1071                                         ti->lost);
1072                 }
1073                 spin_unlock_irqrestore(&timer->lock, flags);
1074         }
1075         up(&register_mutex);
1076 }
1077
1078 /*
1079  *  USER SPACE interface
1080  */
1081
1082 static void snd_timer_user_interrupt(snd_timer_instance_t *timeri,
1083                                      unsigned long resolution,
1084                                      unsigned long ticks)
1085 {
1086         snd_timer_user_t *tu = timeri->callback_data;
1087         snd_timer_read_t *r;
1088         int prev;
1089         
1090         spin_lock(&tu->qlock);
1091         if (tu->qused > 0) {
1092                 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1093                 r = &tu->queue[prev];
1094                 if (r->resolution == resolution) {
1095                         r->ticks += ticks;
1096                         goto __wake;
1097                 }
1098         }
1099         if (tu->qused >= tu->queue_size) {
1100                 tu->overrun++;
1101         } else {
1102                 r = &tu->queue[tu->qtail++];
1103                 tu->qtail %= tu->queue_size;
1104                 r->resolution = resolution;
1105                 r->ticks = ticks;
1106                 tu->qused++;
1107         }
1108       __wake:
1109         spin_unlock(&tu->qlock);
1110         kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1111         wake_up(&tu->qchange_sleep);
1112 }
1113
1114 static void snd_timer_user_append_to_tqueue(snd_timer_user_t *tu, snd_timer_tread_t *tread)
1115 {
1116         if (tu->qused >= tu->queue_size) {
1117                 tu->overrun++;
1118         } else {
1119                 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1120                 tu->qtail %= tu->queue_size;
1121                 tu->qused++;
1122         }
1123 }
1124
1125 static void snd_timer_user_ccallback(snd_timer_instance_t *timeri,
1126                                      enum sndrv_timer_event event,
1127                                      struct timespec *tstamp,
1128                                      unsigned long resolution)
1129 {
1130         snd_timer_user_t *tu = timeri->callback_data;
1131         snd_timer_tread_t r1;
1132
1133         if (event >= SNDRV_TIMER_EVENT_START && event <= SNDRV_TIMER_EVENT_PAUSE)
1134                 tu->tstamp = *tstamp;
1135         if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1136                 return;
1137         r1.event = event;
1138         r1.tstamp = *tstamp;
1139         r1.val = resolution;
1140         spin_lock(&tu->qlock);
1141         snd_timer_user_append_to_tqueue(tu, &r1);
1142         spin_unlock(&tu->qlock);
1143         kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1144         wake_up(&tu->qchange_sleep);
1145 }
1146
1147 static void snd_timer_user_tinterrupt(snd_timer_instance_t *timeri,
1148                                       unsigned long resolution,
1149                                       unsigned long ticks)
1150 {
1151         snd_timer_user_t *tu = timeri->callback_data;
1152         snd_timer_tread_t *r, r1;
1153         struct timespec tstamp;
1154         int prev, append = 0;
1155
1156         snd_timestamp_zero(&tstamp);
1157         spin_lock(&tu->qlock);
1158         if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION)|(1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1159                 spin_unlock(&tu->qlock);
1160                 return;
1161         }
1162         if (tu->last_resolution != resolution || ticks > 0)
1163                 snd_timestamp_now(&tstamp, 1);
1164         if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) && tu->last_resolution != resolution) {
1165                 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1166                 r1.tstamp = tstamp;
1167                 r1.val = resolution;
1168                 snd_timer_user_append_to_tqueue(tu, &r1);
1169                 tu->last_resolution = resolution;
1170                 append++;
1171         }
1172         if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1173                 goto __wake;
1174         if (ticks == 0)
1175                 goto __wake;
1176         if (tu->qused > 0) {
1177                 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1178                 r = &tu->tqueue[prev];
1179                 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1180                         r->tstamp = tstamp;
1181                         r->val += ticks;
1182                         append++;
1183                         goto __wake;
1184                 }
1185         }
1186         r1.event = SNDRV_TIMER_EVENT_TICK;
1187         r1.tstamp = tstamp;
1188         r1.val = ticks;
1189         snd_timer_user_append_to_tqueue(tu, &r1);
1190         append++;
1191       __wake:
1192         spin_unlock(&tu->qlock);
1193         if (append == 0)
1194                 return;
1195         kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1196         wake_up(&tu->qchange_sleep);
1197 }
1198
1199 static int snd_timer_user_open(struct inode *inode, struct file *file)
1200 {
1201         snd_timer_user_t *tu;
1202         
1203         tu = kcalloc(1, sizeof(*tu), GFP_KERNEL);
1204         if (tu == NULL)
1205                 return -ENOMEM;
1206         spin_lock_init(&tu->qlock);
1207         init_waitqueue_head(&tu->qchange_sleep);
1208         init_MUTEX(&tu->tread_sem);
1209         tu->ticks = 1;
1210         tu->queue_size = 128;
1211         tu->queue = (snd_timer_read_t *)kmalloc(tu->queue_size * sizeof(snd_timer_read_t), GFP_KERNEL);
1212         if (tu->queue == NULL) {
1213                 kfree(tu);
1214                 return -ENOMEM;
1215         }
1216         file->private_data = tu;
1217         return 0;
1218 }
1219
1220 static int snd_timer_user_release(struct inode *inode, struct file *file)
1221 {
1222         snd_timer_user_t *tu;
1223
1224         if (file->private_data) {
1225                 tu = file->private_data;
1226                 file->private_data = NULL;
1227                 fasync_helper(-1, file, 0, &tu->fasync);
1228                 if (tu->timeri)
1229                         snd_timer_close(tu->timeri);
1230                 kfree(tu->queue);
1231                 kfree(tu->tqueue);
1232                 kfree(tu);
1233         }
1234         return 0;
1235 }
1236
1237 static void snd_timer_user_zero_id(snd_timer_id_t *id)
1238 {
1239         id->dev_class = SNDRV_TIMER_CLASS_NONE;
1240         id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1241         id->card = -1;
1242         id->device = -1;
1243         id->subdevice = -1;
1244 }
1245
1246 static void snd_timer_user_copy_id(snd_timer_id_t *id, snd_timer_t *timer)
1247 {
1248         id->dev_class = timer->tmr_class;
1249         id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1250         id->card = timer->card ? timer->card->number : -1;
1251         id->device = timer->tmr_device;
1252         id->subdevice = timer->tmr_subdevice;
1253 }
1254
1255 static int snd_timer_user_next_device(snd_timer_id_t __user *_tid)
1256 {
1257         snd_timer_id_t id;
1258         snd_timer_t *timer;
1259         struct list_head *p;
1260         
1261         if (copy_from_user(&id, _tid, sizeof(id)))
1262                 return -EFAULT;
1263         down(&register_mutex);
1264         if (id.dev_class < 0) {         /* first item */
1265                 if (list_empty(&snd_timer_list))
1266                         snd_timer_user_zero_id(&id);
1267                 else {
1268                         timer = (snd_timer_t *)list_entry(snd_timer_list.next, snd_timer_t, device_list);
1269                         snd_timer_user_copy_id(&id, timer);
1270                 }
1271         } else {
1272                 switch (id.dev_class) {
1273                 case SNDRV_TIMER_CLASS_GLOBAL:
1274                         id.device = id.device < 0 ? 0 : id.device + 1;
1275                         list_for_each(p, &snd_timer_list) {
1276                                 timer = (snd_timer_t *)list_entry(p, snd_timer_t, device_list);
1277                                 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1278                                         snd_timer_user_copy_id(&id, timer);
1279                                         break;
1280                                 }
1281                                 if (timer->tmr_device >= id.device) {
1282                                         snd_timer_user_copy_id(&id, timer);
1283                                         break;
1284                                 }
1285                         }
1286                         if (p == &snd_timer_list)
1287                                 snd_timer_user_zero_id(&id);
1288                         break;
1289                 case SNDRV_TIMER_CLASS_CARD:
1290                 case SNDRV_TIMER_CLASS_PCM:
1291                         if (id.card < 0) {
1292                                 id.card = 0;
1293                         } else {
1294                                 if (id.card < 0) {
1295                                         id.card = 0;
1296                                 } else {
1297                                         if (id.device < 0) {
1298                                                 id.device = 0;
1299                                         } else {
1300                                                 id.subdevice = id.subdevice < 0 ? 0 : id.subdevice + 1;
1301                                         }
1302                                 }
1303                         }
1304                         list_for_each(p, &snd_timer_list) {
1305                                 timer = (snd_timer_t *)list_entry(p, snd_timer_t, device_list);
1306                                 if (timer->tmr_class > id.dev_class) {
1307                                         snd_timer_user_copy_id(&id, timer);
1308                                         break;
1309                                 }
1310                                 if (timer->tmr_class < id.dev_class)
1311                                         continue;
1312                                 if (timer->card->number > id.card) {
1313                                         snd_timer_user_copy_id(&id, timer);
1314                                         break;
1315                                 }
1316                                 if (timer->card->number < id.card)
1317                                         continue;
1318                                 if (timer->tmr_device > id.device) {
1319                                         snd_timer_user_copy_id(&id, timer);
1320                                         break;
1321                                 }
1322                                 if (timer->tmr_device < id.device)
1323                                         continue;
1324                                 if (timer->tmr_subdevice > id.subdevice) {
1325                                         snd_timer_user_copy_id(&id, timer);
1326                                         break;
1327                                 }
1328                                 if (timer->tmr_subdevice < id.subdevice)
1329                                         continue;
1330                                 snd_timer_user_copy_id(&id, timer);
1331                                 break;
1332                         }
1333                         if (p == &snd_timer_list)
1334                                 snd_timer_user_zero_id(&id);
1335                         break;
1336                 default:
1337                         snd_timer_user_zero_id(&id);
1338                 }
1339         }
1340         up(&register_mutex);
1341         if (copy_to_user(_tid, &id, sizeof(*_tid)))
1342                 return -EFAULT;
1343         return 0;
1344
1345
1346 static int snd_timer_user_ginfo(struct file *file, snd_timer_ginfo_t __user *_ginfo)
1347 {
1348         snd_timer_ginfo_t *ginfo;
1349         snd_timer_id_t tid;
1350         snd_timer_t *t;
1351         struct list_head *p;
1352         int err = 0;
1353
1354         ginfo = kmalloc(sizeof(*ginfo), GFP_KERNEL);
1355         if (! ginfo)
1356                 return -ENOMEM;
1357         if (copy_from_user(ginfo, _ginfo, sizeof(*ginfo))) {
1358                 kfree(ginfo);
1359                 return -EFAULT;
1360         }
1361         tid = ginfo->tid;
1362         memset(ginfo, 0, sizeof(*ginfo));
1363         ginfo->tid = tid;
1364         down(&register_mutex);
1365         t = snd_timer_find(&tid);
1366         if (t != NULL) {
1367                 ginfo->card = t->card ? t->card->number : -1;
1368                 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1369                         ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1370                 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1371                 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1372                 ginfo->resolution = t->hw.resolution;
1373                 if (t->hw.resolution_min > 0) {
1374                         ginfo->resolution_min = t->hw.resolution_min;
1375                         ginfo->resolution_max = t->hw.resolution_max;
1376                 }
1377                 list_for_each(p, &t->open_list_head) {
1378                         ginfo->clients++;
1379                 }
1380         } else {
1381                 err = -ENODEV;
1382         }
1383         up(&register_mutex);
1384         if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1385                 err = -EFAULT;
1386         kfree(ginfo);
1387         return err;
1388 }
1389
1390 static int snd_timer_user_gparams(struct file *file, snd_timer_gparams_t __user *_gparams)
1391 {
1392         snd_timer_gparams_t gparams;
1393         snd_timer_t *t;
1394         int err;
1395
1396         if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1397                 return -EFAULT;
1398         down(&register_mutex);
1399         t = snd_timer_find(&gparams.tid);
1400         if (t != NULL) {
1401                 if (list_empty(&t->open_list_head)) {
1402                         if (t->hw.set_period)
1403                                 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1404                         else
1405                                 err = -ENOSYS;
1406                 } else {
1407                         err = -EBUSY;
1408                 }
1409         } else {
1410                 err = -ENODEV;
1411         }
1412         up(&register_mutex);
1413         return err;
1414 }
1415
1416 static int snd_timer_user_gstatus(struct file *file, snd_timer_gstatus_t __user *_gstatus)
1417 {
1418         snd_timer_gstatus_t gstatus;
1419         snd_timer_id_t tid;
1420         snd_timer_t *t;
1421         int err = 0;
1422
1423         if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1424                 return -EFAULT;
1425         tid = gstatus.tid;
1426         memset(&gstatus, 0, sizeof(gstatus));
1427         gstatus.tid = tid;
1428         down(&register_mutex);
1429         t = snd_timer_find(&tid);
1430         if (t != NULL) {
1431                 if (t->hw.c_resolution)
1432                         gstatus.resolution = t->hw.c_resolution(t);
1433                 else
1434                         gstatus.resolution = t->hw.resolution;
1435                 if (t->hw.precise_resolution) {
1436                         t->hw.precise_resolution(t, &gstatus.resolution_num, &gstatus.resolution_den);
1437                 } else {
1438                         gstatus.resolution_num = gstatus.resolution;
1439                         gstatus.resolution_den = 1000000000uL;
1440                 }
1441         } else {
1442                 err = -ENODEV;
1443         }
1444         up(&register_mutex);
1445         if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1446                 err = -EFAULT;
1447         return err;
1448 }
1449
1450 static int snd_timer_user_tselect(struct file *file, snd_timer_select_t __user *_tselect)
1451 {
1452         snd_timer_user_t *tu;
1453         snd_timer_select_t tselect;
1454         char str[32];
1455         int err = 0;
1456         
1457         tu = file->private_data;
1458         down(&tu->tread_sem);
1459         if (tu->timeri) {
1460                 snd_timer_close(tu->timeri);
1461                 tu->timeri = NULL;
1462         }
1463         if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1464                 err = -EFAULT;
1465                 goto __err;
1466         }
1467         sprintf(str, "application %i", current->pid);
1468         if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1469                 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1470         if ((err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid)) < 0)
1471                 goto __err;
1472
1473         kfree(tu->queue);
1474         tu->queue = NULL;
1475         kfree(tu->tqueue);
1476         tu->tqueue = NULL;
1477         if (tu->tread) {
1478                 tu->tqueue = (snd_timer_tread_t *)kmalloc(tu->queue_size * sizeof(snd_timer_tread_t), GFP_KERNEL);
1479                 if (tu->tqueue == NULL)
1480                         err = -ENOMEM;
1481         } else {
1482                 tu->queue = (snd_timer_read_t *)kmalloc(tu->queue_size * sizeof(snd_timer_read_t), GFP_KERNEL);
1483                 if (tu->queue == NULL)
1484                         err = -ENOMEM;
1485         }
1486         
1487         if (err < 0) {
1488                 snd_timer_close(tu->timeri);
1489                 tu->timeri = NULL;
1490         } else {
1491                 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1492                 tu->timeri->callback = tu->tread ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1493                 tu->timeri->ccallback = snd_timer_user_ccallback;
1494                 tu->timeri->callback_data = (void *)tu;
1495         }
1496
1497       __err:
1498         up(&tu->tread_sem);
1499         return err;
1500 }
1501
1502 static int snd_timer_user_info(struct file *file, snd_timer_info_t __user *_info)
1503 {
1504         snd_timer_user_t *tu;
1505         snd_timer_info_t *info;
1506         snd_timer_t *t;
1507         int err = 0;
1508
1509         tu = file->private_data;
1510         snd_assert(tu->timeri != NULL, return -ENXIO);
1511         t = tu->timeri->timer;
1512         snd_assert(t != NULL, return -ENXIO);
1513
1514         info = kcalloc(1, sizeof(*info), GFP_KERNEL);
1515         if (! info)
1516                 return -ENOMEM;
1517         info->card = t->card ? t->card->number : -1;
1518         if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1519                 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1520         strlcpy(info->id, t->id, sizeof(info->id));
1521         strlcpy(info->name, t->name, sizeof(info->name));
1522         info->resolution = t->hw.resolution;
1523         if (copy_to_user(_info, info, sizeof(*_info)))
1524                 err = -EFAULT;
1525         kfree(info);
1526         return err;
1527 }
1528
1529 static int snd_timer_user_params(struct file *file, snd_timer_params_t __user *_params)
1530 {
1531         snd_timer_user_t *tu;
1532         snd_timer_params_t params;
1533         snd_timer_t *t;
1534         snd_timer_read_t *tr;
1535         snd_timer_tread_t *ttr;
1536         int err;
1537         
1538         tu = file->private_data;
1539         snd_assert(tu->timeri != NULL, return -ENXIO);
1540         t = tu->timeri->timer;
1541         snd_assert(t != NULL, return -ENXIO);
1542         if (copy_from_user(&params, _params, sizeof(params)))
1543                 return -EFAULT;
1544         if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1545                 err = -EINVAL;
1546                 goto _end;
1547         }
1548         if (params.queue_size > 0 && (params.queue_size < 32 || params.queue_size > 1024)) {
1549                 err = -EINVAL;
1550                 goto _end;
1551         }
1552         if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1553                               (1<<SNDRV_TIMER_EVENT_TICK)|
1554                               (1<<SNDRV_TIMER_EVENT_START)|
1555                               (1<<SNDRV_TIMER_EVENT_STOP)|
1556                               (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1557                               (1<<SNDRV_TIMER_EVENT_PAUSE)|
1558                               (1<<SNDRV_TIMER_EVENT_MSTART)|
1559                               (1<<SNDRV_TIMER_EVENT_MSTOP)|
1560                               (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1561                               (1<<SNDRV_TIMER_EVENT_MPAUSE))) {
1562                 err = -EINVAL;
1563                 goto _end;
1564         }
1565         snd_timer_stop(tu->timeri);
1566         spin_lock_irq(&t->lock);
1567         tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1568                                SNDRV_TIMER_IFLG_EXCLUSIVE|
1569                                SNDRV_TIMER_IFLG_EARLY_EVENT);
1570         if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1571                 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1572         if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1573                 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1574         if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1575                 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1576         spin_unlock_irq(&t->lock);
1577         if (params.queue_size > 0 && (unsigned int)tu->queue_size != params.queue_size) {
1578                 if (tu->tread) {
1579                         ttr = (snd_timer_tread_t *)kmalloc(params.queue_size * sizeof(snd_timer_tread_t), GFP_KERNEL);
1580                         if (ttr) {
1581                                 kfree(tu->tqueue);
1582                                 tu->queue_size = params.queue_size;
1583                                 tu->tqueue = ttr;
1584                         }
1585                 } else {
1586                         tr = (snd_timer_read_t *)kmalloc(params.queue_size * sizeof(snd_timer_read_t), GFP_KERNEL);
1587                         if (tr) {
1588                                 kfree(tu->queue);
1589                                 tu->queue_size = params.queue_size;
1590                                 tu->queue = tr;
1591                         }
1592                 }
1593         }
1594         tu->qhead = tu->qtail = tu->qused = 0;
1595         if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1596                 if (tu->tread) {
1597                         snd_timer_tread_t tread;
1598                         tread.event = SNDRV_TIMER_EVENT_EARLY;
1599                         tread.tstamp.tv_sec = 0;
1600                         tread.tstamp.tv_nsec = 0;
1601                         tread.val = 0;
1602                         snd_timer_user_append_to_tqueue(tu, &tread);
1603                 } else {
1604                         snd_timer_read_t *r = &tu->queue[0];
1605                         r->resolution = 0;
1606                         r->ticks = 0;
1607                         tu->qused++;
1608                         tu->qtail++;
1609                 }
1610                 
1611         }
1612         tu->filter = params.filter;
1613         tu->ticks = params.ticks;
1614         err = 0;
1615  _end:
1616         if (copy_to_user(_params, &params, sizeof(params)))
1617                 return -EFAULT;
1618         return err;
1619 }
1620
1621 static int snd_timer_user_status(struct file *file, snd_timer_status_t __user *_status)
1622 {
1623         snd_timer_user_t *tu;
1624         snd_timer_status_t status;
1625         
1626         tu = file->private_data;
1627         snd_assert(tu->timeri != NULL, return -ENXIO);
1628         memset(&status, 0, sizeof(status));
1629         status.tstamp = tu->tstamp;
1630         status.resolution = snd_timer_resolution(tu->timeri);
1631         status.lost = tu->timeri->lost;
1632         status.overrun = tu->overrun;
1633         spin_lock_irq(&tu->qlock);
1634         status.queue = tu->qused;
1635         spin_unlock_irq(&tu->qlock);
1636         if (copy_to_user(_status, &status, sizeof(status)))
1637                 return -EFAULT;
1638         return 0;
1639 }
1640
1641 static int snd_timer_user_start(struct file *file)
1642 {
1643         int err;
1644         snd_timer_user_t *tu;
1645                 
1646         tu = file->private_data;
1647         snd_assert(tu->timeri != NULL, return -ENXIO);
1648         snd_timer_stop(tu->timeri);
1649         tu->timeri->lost = 0;
1650         tu->last_resolution = 0;
1651         return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1652 }
1653
1654 static int snd_timer_user_stop(struct file *file)
1655 {
1656         int err;
1657         snd_timer_user_t *tu;
1658                 
1659         tu = file->private_data;
1660         snd_assert(tu->timeri != NULL, return -ENXIO);
1661         return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1662 }
1663
1664 static int snd_timer_user_continue(struct file *file)
1665 {
1666         int err;
1667         snd_timer_user_t *tu;
1668                 
1669         tu = file->private_data;
1670         snd_assert(tu->timeri != NULL, return -ENXIO);
1671         tu->timeri->lost = 0;
1672         return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1673 }
1674
1675 static int snd_timer_user_pause(struct file *file)
1676 {
1677         int err;
1678         snd_timer_user_t *tu;
1679                 
1680         tu = file->private_data;
1681         snd_assert(tu->timeri != NULL, return -ENXIO);
1682         return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1683 }
1684
1685 enum {
1686         SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1687         SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1688         SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1689         SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1690 };
1691
1692 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1693 {
1694         snd_timer_user_t *tu;
1695         void __user *argp = (void __user *)arg;
1696         int __user *p = argp;
1697         
1698         tu = file->private_data;
1699         switch (cmd) {
1700         case SNDRV_TIMER_IOCTL_PVERSION:
1701                 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1702         case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1703                 return snd_timer_user_next_device(argp);
1704         case SNDRV_TIMER_IOCTL_TREAD:
1705         {
1706                 int xarg;
1707                 
1708                 down(&tu->tread_sem);
1709                 if (tu->timeri) {       /* too late */
1710                         up(&tu->tread_sem);
1711                         return -EBUSY;
1712                 }
1713                 if (get_user(xarg, p)) {
1714                         up(&tu->tread_sem);
1715                         return -EFAULT;
1716                 }
1717                 tu->tread = xarg ? 1 : 0;
1718                 up(&tu->tread_sem);
1719                 return 0;
1720         }
1721         case SNDRV_TIMER_IOCTL_GINFO:
1722                 return snd_timer_user_ginfo(file, argp);
1723         case SNDRV_TIMER_IOCTL_GPARAMS:
1724                 return snd_timer_user_gparams(file, argp);
1725         case SNDRV_TIMER_IOCTL_GSTATUS:
1726                 return snd_timer_user_gstatus(file, argp);
1727         case SNDRV_TIMER_IOCTL_SELECT:
1728                 return snd_timer_user_tselect(file, argp);
1729         case SNDRV_TIMER_IOCTL_INFO:
1730                 return snd_timer_user_info(file, argp);
1731         case SNDRV_TIMER_IOCTL_PARAMS:
1732                 return snd_timer_user_params(file, argp);
1733         case SNDRV_TIMER_IOCTL_STATUS:
1734                 return snd_timer_user_status(file, argp);
1735         case SNDRV_TIMER_IOCTL_START:
1736         case SNDRV_TIMER_IOCTL_START_OLD:
1737                 return snd_timer_user_start(file);
1738         case SNDRV_TIMER_IOCTL_STOP:
1739         case SNDRV_TIMER_IOCTL_STOP_OLD:
1740                 return snd_timer_user_stop(file);
1741         case SNDRV_TIMER_IOCTL_CONTINUE:
1742         case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1743                 return snd_timer_user_continue(file);
1744         case SNDRV_TIMER_IOCTL_PAUSE:
1745         case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1746                 return snd_timer_user_pause(file);
1747         }
1748         return -ENOTTY;
1749 }
1750
1751 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1752 {
1753         snd_timer_user_t *tu;
1754         int err;
1755         
1756         tu = file->private_data;
1757         err = fasync_helper(fd, file, on, &tu->fasync);
1758         if (err < 0)
1759                 return err;
1760         return 0;
1761 }
1762
1763 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer, size_t count, loff_t *offset)
1764 {
1765         snd_timer_user_t *tu;
1766         long result = 0, unit;
1767         int err = 0;
1768         
1769         tu = file->private_data;
1770         unit = tu->tread ? sizeof(snd_timer_tread_t) : sizeof(snd_timer_read_t);
1771         spin_lock_irq(&tu->qlock);
1772         while ((long)count - result >= unit) {
1773                 while (!tu->qused) {
1774                         wait_queue_t wait;
1775
1776                         if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1777                                 err = -EAGAIN;
1778                                 break;
1779                         }
1780
1781                         set_current_state(TASK_INTERRUPTIBLE);
1782                         init_waitqueue_entry(&wait, current);
1783                         add_wait_queue(&tu->qchange_sleep, &wait);
1784
1785                         spin_unlock_irq(&tu->qlock);
1786                         schedule();
1787                         spin_lock_irq(&tu->qlock);
1788
1789                         remove_wait_queue(&tu->qchange_sleep, &wait);
1790
1791                         if (signal_pending(current)) {
1792                                 err = -ERESTARTSYS;
1793                                 break;
1794                         }
1795                 }
1796
1797                 spin_unlock_irq(&tu->qlock);
1798                 if (err < 0)
1799                         goto _error;
1800
1801                 if (tu->tread) {
1802                         if (copy_to_user(buffer, &tu->tqueue[tu->qhead++], sizeof(snd_timer_tread_t))) {
1803                                 err = -EFAULT;
1804                                 goto _error;
1805                         }
1806                 } else {
1807                         if (copy_to_user(buffer, &tu->queue[tu->qhead++], sizeof(snd_timer_read_t))) {
1808                                 err = -EFAULT;
1809                                 goto _error;
1810                         }
1811                 }
1812
1813                 tu->qhead %= tu->queue_size;
1814
1815                 result += unit;
1816                 buffer += unit;
1817
1818                 spin_lock_irq(&tu->qlock);
1819                 tu->qused--;
1820         }
1821         spin_unlock_irq(&tu->qlock);
1822  _error:
1823         return result > 0 ? result : err;
1824 }
1825
1826 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1827 {
1828         unsigned int mask;
1829         snd_timer_user_t *tu;
1830
1831         tu = file->private_data;
1832
1833         poll_wait(file, &tu->qchange_sleep, wait);
1834         
1835         mask = 0;
1836         if (tu->qused)
1837                 mask |= POLLIN | POLLRDNORM;
1838
1839         return mask;
1840 }
1841
1842 #ifdef CONFIG_COMPAT
1843 #include "timer_compat.c"
1844 #else
1845 #define snd_timer_user_ioctl_compat     NULL
1846 #endif
1847
1848 static struct file_operations snd_timer_f_ops =
1849 {
1850         .owner =        THIS_MODULE,
1851         .read =         snd_timer_user_read,
1852         .open =         snd_timer_user_open,
1853         .release =      snd_timer_user_release,
1854         .poll =         snd_timer_user_poll,
1855         .unlocked_ioctl =       snd_timer_user_ioctl,
1856         .compat_ioctl = snd_timer_user_ioctl_compat,
1857         .fasync =       snd_timer_user_fasync,
1858 };
1859
1860 static snd_minor_t snd_timer_reg =
1861 {
1862         .comment =      "timer",
1863         .f_ops =        &snd_timer_f_ops,
1864 };
1865
1866 /*
1867  *  ENTRY functions
1868  */
1869
1870 static snd_info_entry_t *snd_timer_proc_entry = NULL;
1871
1872 static int __init alsa_timer_init(void)
1873 {
1874         int err;
1875         snd_info_entry_t *entry;
1876
1877 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1878         snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1, "system timer");
1879 #endif
1880         if ((entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL)) != NULL) {
1881                 entry->c.text.read_size = SNDRV_TIMER_DEVICES * 128;
1882                 entry->c.text.read = snd_timer_proc_read;
1883                 if (snd_info_register(entry) < 0) {
1884                         snd_info_free_entry(entry);
1885                         entry = NULL;
1886                 }
1887         }
1888         snd_timer_proc_entry = entry;
1889         if ((err = snd_timer_register_system()) < 0)
1890                 snd_printk(KERN_ERR "unable to register system timer (%i)\n", err);
1891         if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER,
1892                                         NULL, 0, &snd_timer_reg, "timer"))<0)
1893                 snd_printk(KERN_ERR "unable to register timer device (%i)\n", err);
1894         return 0;
1895 }
1896
1897 static void __exit alsa_timer_exit(void)
1898 {
1899         struct list_head *p, *n;
1900
1901         snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1902         /* unregister the system timer */
1903         list_for_each_safe(p, n, &snd_timer_list) {
1904                 snd_timer_t *timer = (snd_timer_t *)list_entry(p, snd_timer_t, device_list);
1905                 snd_timer_unregister(timer);
1906         }
1907         if (snd_timer_proc_entry) {
1908                 snd_info_unregister(snd_timer_proc_entry);
1909                 snd_timer_proc_entry = NULL;
1910         }
1911 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1912         snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1913 #endif
1914 }
1915
1916 module_init(alsa_timer_init)
1917 module_exit(alsa_timer_exit)
1918
1919 EXPORT_SYMBOL(snd_timer_open);
1920 EXPORT_SYMBOL(snd_timer_close);
1921 EXPORT_SYMBOL(snd_timer_resolution);
1922 EXPORT_SYMBOL(snd_timer_start);
1923 EXPORT_SYMBOL(snd_timer_stop);
1924 EXPORT_SYMBOL(snd_timer_continue);
1925 EXPORT_SYMBOL(snd_timer_pause);
1926 EXPORT_SYMBOL(snd_timer_new);
1927 EXPORT_SYMBOL(snd_timer_notify);
1928 EXPORT_SYMBOL(snd_timer_global_new);
1929 EXPORT_SYMBOL(snd_timer_global_free);
1930 EXPORT_SYMBOL(snd_timer_global_register);
1931 EXPORT_SYMBOL(snd_timer_global_unregister);
1932 EXPORT_SYMBOL(snd_timer_interrupt);