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