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