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