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