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