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