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