OSDN Git Service

UPSTREAM: ALSA: timer: Fix race among timer ioctls
[sagit-ice-cold/kernel_xiaomi_msm8998.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/device.h>
28 #include <linux/module.h>
29 #include <linux/string.h>
30 #include <sound/core.h>
31 #include <sound/timer.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/minors.h>
35 #include <sound/initval.h>
36 #include <linux/kmod.h>
37
38 #if IS_ENABLED(CONFIG_SND_HRTIMER)
39 #define DEFAULT_TIMER_LIMIT 4
40 #elif IS_ENABLED(CONFIG_SND_RTCTIMER)
41 #define DEFAULT_TIMER_LIMIT 2
42 #else
43 #define DEFAULT_TIMER_LIMIT 1
44 #endif
45
46 static int timer_limit = DEFAULT_TIMER_LIMIT;
47 static int timer_tstamp_monotonic = 1;
48 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
49 MODULE_DESCRIPTION("ALSA timer interface");
50 MODULE_LICENSE("GPL");
51 module_param(timer_limit, int, 0444);
52 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
53 module_param(timer_tstamp_monotonic, int, 0444);
54 MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
55
56 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
57 MODULE_ALIAS("devname:snd/timer");
58
59 struct snd_timer_user {
60         struct snd_timer_instance *timeri;
61         int tread;              /* enhanced read with timestamps and events */
62         unsigned long ticks;
63         unsigned long overrun;
64         int qhead;
65         int qtail;
66         int qused;
67         int queue_size;
68         struct snd_timer_read *queue;
69         struct snd_timer_tread *tqueue;
70         spinlock_t qlock;
71         unsigned long last_resolution;
72         unsigned int filter;
73         struct timespec tstamp;         /* trigger tstamp */
74         wait_queue_head_t qchange_sleep;
75         struct fasync_struct *fasync;
76         struct mutex ioctl_lock;
77 };
78
79 /* list of timers */
80 static LIST_HEAD(snd_timer_list);
81
82 /* list of slave instances */
83 static LIST_HEAD(snd_timer_slave_list);
84
85 /* lock for slave active lists */
86 static DEFINE_SPINLOCK(slave_active_lock);
87
88 static DEFINE_MUTEX(register_mutex);
89
90 static int snd_timer_free(struct snd_timer *timer);
91 static int snd_timer_dev_free(struct snd_device *device);
92 static int snd_timer_dev_register(struct snd_device *device);
93 static int snd_timer_dev_disconnect(struct snd_device *device);
94
95 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
96
97 /*
98  * create a timer instance with the given owner string.
99  * when timer is not NULL, increments the module counter
100  */
101 static struct snd_timer_instance *snd_timer_instance_new(char *owner,
102                                                          struct snd_timer *timer)
103 {
104         struct snd_timer_instance *timeri;
105         timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
106         if (timeri == NULL)
107                 return NULL;
108         timeri->owner = kstrdup(owner, GFP_KERNEL);
109         if (! timeri->owner) {
110                 kfree(timeri);
111                 return NULL;
112         }
113         INIT_LIST_HEAD(&timeri->open_list);
114         INIT_LIST_HEAD(&timeri->active_list);
115         INIT_LIST_HEAD(&timeri->ack_list);
116         INIT_LIST_HEAD(&timeri->slave_list_head);
117         INIT_LIST_HEAD(&timeri->slave_active_head);
118
119         timeri->timer = timer;
120         if (timer && !try_module_get(timer->module)) {
121                 kfree(timeri->owner);
122                 kfree(timeri);
123                 return NULL;
124         }
125
126         return timeri;
127 }
128
129 /*
130  * find a timer instance from the given timer id
131  */
132 static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
133 {
134         struct snd_timer *timer = NULL;
135
136         list_for_each_entry(timer, &snd_timer_list, device_list) {
137                 if (timer->tmr_class != tid->dev_class)
138                         continue;
139                 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
140                      timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
141                     (timer->card == NULL ||
142                      timer->card->number != tid->card))
143                         continue;
144                 if (timer->tmr_device != tid->device)
145                         continue;
146                 if (timer->tmr_subdevice != tid->subdevice)
147                         continue;
148                 return timer;
149         }
150         return NULL;
151 }
152
153 #ifdef CONFIG_MODULES
154
155 static void snd_timer_request(struct snd_timer_id *tid)
156 {
157         switch (tid->dev_class) {
158         case SNDRV_TIMER_CLASS_GLOBAL:
159                 if (tid->device < timer_limit)
160                         request_module("snd-timer-%i", tid->device);
161                 break;
162         case SNDRV_TIMER_CLASS_CARD:
163         case SNDRV_TIMER_CLASS_PCM:
164                 if (tid->card < snd_ecards_limit)
165                         request_module("snd-card-%i", tid->card);
166                 break;
167         default:
168                 break;
169         }
170 }
171
172 #endif
173
174 /*
175  * look for a master instance matching with the slave id of the given slave.
176  * when found, relink the open_link of the slave.
177  *
178  * call this with register_mutex down.
179  */
180 static void snd_timer_check_slave(struct snd_timer_instance *slave)
181 {
182         struct snd_timer *timer;
183         struct snd_timer_instance *master;
184
185         /* FIXME: it's really dumb to look up all entries.. */
186         list_for_each_entry(timer, &snd_timer_list, device_list) {
187                 list_for_each_entry(master, &timer->open_list_head, open_list) {
188                         if (slave->slave_class == master->slave_class &&
189                             slave->slave_id == master->slave_id) {
190                                 list_move_tail(&slave->open_list,
191                                                &master->slave_list_head);
192                                 spin_lock_irq(&slave_active_lock);
193                                 slave->master = master;
194                                 slave->timer = master->timer;
195                                 spin_unlock_irq(&slave_active_lock);
196                                 return;
197                         }
198                 }
199         }
200 }
201
202 /*
203  * look for slave instances matching with the slave id of the given master.
204  * when found, relink the open_link of slaves.
205  *
206  * call this with register_mutex down.
207  */
208 static void snd_timer_check_master(struct snd_timer_instance *master)
209 {
210         struct snd_timer_instance *slave, *tmp;
211
212         /* check all pending slaves */
213         list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
214                 if (slave->slave_class == master->slave_class &&
215                     slave->slave_id == master->slave_id) {
216                         list_move_tail(&slave->open_list, &master->slave_list_head);
217                         spin_lock_irq(&slave_active_lock);
218                         spin_lock(&master->timer->lock);
219                         slave->master = master;
220                         slave->timer = master->timer;
221                         if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
222                                 list_add_tail(&slave->active_list,
223                                               &master->slave_active_head);
224                         spin_unlock(&master->timer->lock);
225                         spin_unlock_irq(&slave_active_lock);
226                 }
227         }
228 }
229
230 /*
231  * open a timer instance
232  * when opening a master, the slave id must be here given.
233  */
234 int snd_timer_open(struct snd_timer_instance **ti,
235                    char *owner, struct snd_timer_id *tid,
236                    unsigned int slave_id)
237 {
238         struct snd_timer *timer;
239         struct snd_timer_instance *timeri = NULL;
240
241         if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
242                 /* open a slave instance */
243                 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
244                     tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
245                         pr_debug("ALSA: timer: invalid slave class %i\n",
246                                  tid->dev_sclass);
247                         return -EINVAL;
248                 }
249                 mutex_lock(&register_mutex);
250                 timeri = snd_timer_instance_new(owner, NULL);
251                 if (!timeri) {
252                         mutex_unlock(&register_mutex);
253                         return -ENOMEM;
254                 }
255                 timeri->slave_class = tid->dev_sclass;
256                 timeri->slave_id = tid->device;
257                 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
258                 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
259                 snd_timer_check_slave(timeri);
260                 mutex_unlock(&register_mutex);
261                 *ti = timeri;
262                 return 0;
263         }
264
265         /* open a master instance */
266         mutex_lock(&register_mutex);
267         timer = snd_timer_find(tid);
268 #ifdef CONFIG_MODULES
269         if (!timer) {
270                 mutex_unlock(&register_mutex);
271                 snd_timer_request(tid);
272                 mutex_lock(&register_mutex);
273                 timer = snd_timer_find(tid);
274         }
275 #endif
276         if (!timer) {
277                 mutex_unlock(&register_mutex);
278                 return -ENODEV;
279         }
280         if (!list_empty(&timer->open_list_head)) {
281                 timeri = list_entry(timer->open_list_head.next,
282                                     struct snd_timer_instance, open_list);
283                 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
284                         mutex_unlock(&register_mutex);
285                         return -EBUSY;
286                 }
287         }
288         timeri = snd_timer_instance_new(owner, timer);
289         if (!timeri) {
290                 mutex_unlock(&register_mutex);
291                 return -ENOMEM;
292         }
293         timeri->slave_class = tid->dev_sclass;
294         timeri->slave_id = slave_id;
295         if (list_empty(&timer->open_list_head) && timer->hw.open)
296                 timer->hw.open(timer);
297         list_add_tail(&timeri->open_list, &timer->open_list_head);
298         snd_timer_check_master(timeri);
299         mutex_unlock(&register_mutex);
300         *ti = timeri;
301         return 0;
302 }
303
304 static int _snd_timer_stop(struct snd_timer_instance *timeri,
305                            int keep_flag, int event);
306
307 /*
308  * close a timer instance
309  */
310 int snd_timer_close(struct snd_timer_instance *timeri)
311 {
312         struct snd_timer *timer = NULL;
313         struct snd_timer_instance *slave, *tmp;
314
315         if (snd_BUG_ON(!timeri))
316                 return -ENXIO;
317
318         /* force to stop the timer */
319         snd_timer_stop(timeri);
320
321         if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
322                 /* wait, until the active callback is finished */
323                 spin_lock_irq(&slave_active_lock);
324                 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
325                         spin_unlock_irq(&slave_active_lock);
326                         udelay(10);
327                         spin_lock_irq(&slave_active_lock);
328                 }
329                 spin_unlock_irq(&slave_active_lock);
330                 mutex_lock(&register_mutex);
331                 list_del(&timeri->open_list);
332                 mutex_unlock(&register_mutex);
333         } else {
334                 timer = timeri->timer;
335                 if (snd_BUG_ON(!timer))
336                         goto out;
337                 /* wait, until the active callback is finished */
338                 spin_lock_irq(&timer->lock);
339                 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
340                         spin_unlock_irq(&timer->lock);
341                         udelay(10);
342                         spin_lock_irq(&timer->lock);
343                 }
344                 spin_unlock_irq(&timer->lock);
345                 mutex_lock(&register_mutex);
346                 list_del(&timeri->open_list);
347                 if (timer && list_empty(&timer->open_list_head) &&
348                     timer->hw.close)
349                         timer->hw.close(timer);
350                 /* remove slave links */
351                 spin_lock_irq(&slave_active_lock);
352                 spin_lock(&timer->lock);
353                 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
354                                          open_list) {
355                         list_move_tail(&slave->open_list, &snd_timer_slave_list);
356                         slave->master = NULL;
357                         slave->timer = NULL;
358                         list_del_init(&slave->ack_list);
359                         list_del_init(&slave->active_list);
360                 }
361                 spin_unlock(&timer->lock);
362                 spin_unlock_irq(&slave_active_lock);
363                 mutex_unlock(&register_mutex);
364         }
365  out:
366         if (timeri->private_free)
367                 timeri->private_free(timeri);
368         kfree(timeri->owner);
369         kfree(timeri);
370         if (timer)
371                 module_put(timer->module);
372         return 0;
373 }
374
375 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
376 {
377         struct snd_timer * timer;
378
379         if (timeri == NULL)
380                 return 0;
381         if ((timer = timeri->timer) != NULL) {
382                 if (timer->hw.c_resolution)
383                         return timer->hw.c_resolution(timer);
384                 return timer->hw.resolution;
385         }
386         return 0;
387 }
388
389 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
390 {
391         struct snd_timer *timer;
392         unsigned long flags;
393         unsigned long resolution = 0;
394         struct snd_timer_instance *ts;
395         struct timespec tstamp;
396
397         if (timer_tstamp_monotonic)
398                 ktime_get_ts(&tstamp);
399         else
400                 getnstimeofday(&tstamp);
401         if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
402                        event > SNDRV_TIMER_EVENT_PAUSE))
403                 return;
404         if (event == SNDRV_TIMER_EVENT_START ||
405             event == SNDRV_TIMER_EVENT_CONTINUE)
406                 resolution = snd_timer_resolution(ti);
407         if (ti->ccallback)
408                 ti->ccallback(ti, event, &tstamp, resolution);
409         if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
410                 return;
411         timer = ti->timer;
412         if (timer == NULL)
413                 return;
414         if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
415                 return;
416         spin_lock_irqsave(&timer->lock, flags);
417         list_for_each_entry(ts, &ti->slave_active_head, active_list)
418                 if (ts->ccallback)
419                         ts->ccallback(ti, event + 100, &tstamp, resolution);
420         spin_unlock_irqrestore(&timer->lock, flags);
421 }
422
423 static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
424                             unsigned long sticks)
425 {
426         list_move_tail(&timeri->active_list, &timer->active_list_head);
427         if (timer->running) {
428                 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
429                         goto __start_now;
430                 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
431                 timeri->flags |= SNDRV_TIMER_IFLG_START;
432                 return 1;       /* delayed start */
433         } else {
434                 timer->sticks = sticks;
435                 timer->hw.start(timer);
436               __start_now:
437                 timer->running++;
438                 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
439                 return 0;
440         }
441 }
442
443 static int snd_timer_start_slave(struct snd_timer_instance *timeri)
444 {
445         unsigned long flags;
446
447         spin_lock_irqsave(&slave_active_lock, flags);
448         timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
449         if (timeri->master && timeri->timer) {
450                 spin_lock(&timeri->timer->lock);
451                 list_add_tail(&timeri->active_list,
452                               &timeri->master->slave_active_head);
453                 spin_unlock(&timeri->timer->lock);
454         }
455         spin_unlock_irqrestore(&slave_active_lock, flags);
456         return 1; /* delayed start */
457 }
458
459 /*
460  *  start the timer instance
461  */
462 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
463 {
464         struct snd_timer *timer;
465         int result = -EINVAL;
466         unsigned long flags;
467
468         if (timeri == NULL || ticks < 1)
469                 return -EINVAL;
470         if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
471                 result = snd_timer_start_slave(timeri);
472                 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
473                 return result;
474         }
475         timer = timeri->timer;
476         if (timer == NULL)
477                 return -EINVAL;
478         spin_lock_irqsave(&timer->lock, flags);
479         timeri->ticks = timeri->cticks = ticks;
480         timeri->pticks = 0;
481         result = snd_timer_start1(timer, timeri, ticks);
482         spin_unlock_irqrestore(&timer->lock, flags);
483         snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
484         return result;
485 }
486
487 static int _snd_timer_stop(struct snd_timer_instance * timeri,
488                            int keep_flag, int event)
489 {
490         struct snd_timer *timer;
491         unsigned long flags;
492
493         if (snd_BUG_ON(!timeri))
494                 return -ENXIO;
495
496         if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
497                 if (!keep_flag) {
498                         spin_lock_irqsave(&slave_active_lock, flags);
499                         timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
500                         list_del_init(&timeri->ack_list);
501                         list_del_init(&timeri->active_list);
502                         spin_unlock_irqrestore(&slave_active_lock, flags);
503                 }
504                 goto __end;
505         }
506         timer = timeri->timer;
507         if (!timer)
508                 return -EINVAL;
509         spin_lock_irqsave(&timer->lock, flags);
510         list_del_init(&timeri->ack_list);
511         list_del_init(&timeri->active_list);
512         if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
513             !(--timer->running)) {
514                 timer->hw.stop(timer);
515                 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
516                         timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
517                         snd_timer_reschedule(timer, 0);
518                         if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
519                                 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
520                                 timer->hw.start(timer);
521                         }
522                 }
523         }
524         if (!keep_flag)
525                 timeri->flags &=
526                         ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
527         spin_unlock_irqrestore(&timer->lock, flags);
528       __end:
529         if (event != SNDRV_TIMER_EVENT_RESOLUTION)
530                 snd_timer_notify1(timeri, event);
531         return 0;
532 }
533
534 /*
535  * stop the timer instance.
536  *
537  * do not call this from the timer callback!
538  */
539 int snd_timer_stop(struct snd_timer_instance *timeri)
540 {
541         struct snd_timer *timer;
542         unsigned long flags;
543         int err;
544
545         err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
546         if (err < 0)
547                 return err;
548         timer = timeri->timer;
549         if (!timer)
550                 return -EINVAL;
551         spin_lock_irqsave(&timer->lock, flags);
552         timeri->cticks = timeri->ticks;
553         timeri->pticks = 0;
554         spin_unlock_irqrestore(&timer->lock, flags);
555         return 0;
556 }
557
558 /*
559  * start again..  the tick is kept.
560  */
561 int snd_timer_continue(struct snd_timer_instance *timeri)
562 {
563         struct snd_timer *timer;
564         int result = -EINVAL;
565         unsigned long flags;
566
567         if (timeri == NULL)
568                 return result;
569         if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
570                 return snd_timer_start_slave(timeri);
571         timer = timeri->timer;
572         if (! timer)
573                 return -EINVAL;
574         spin_lock_irqsave(&timer->lock, flags);
575         if (!timeri->cticks)
576                 timeri->cticks = 1;
577         timeri->pticks = 0;
578         result = snd_timer_start1(timer, timeri, timer->sticks);
579         spin_unlock_irqrestore(&timer->lock, flags);
580         snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
581         return result;
582 }
583
584 /*
585  * pause.. remember the ticks left
586  */
587 int snd_timer_pause(struct snd_timer_instance * timeri)
588 {
589         return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
590 }
591
592 /*
593  * reschedule the timer
594  *
595  * start pending instances and check the scheduling ticks.
596  * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
597  */
598 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
599 {
600         struct snd_timer_instance *ti;
601         unsigned long ticks = ~0UL;
602
603         list_for_each_entry(ti, &timer->active_list_head, active_list) {
604                 if (ti->flags & SNDRV_TIMER_IFLG_START) {
605                         ti->flags &= ~SNDRV_TIMER_IFLG_START;
606                         ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
607                         timer->running++;
608                 }
609                 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
610                         if (ticks > ti->cticks)
611                                 ticks = ti->cticks;
612                 }
613         }
614         if (ticks == ~0UL) {
615                 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
616                 return;
617         }
618         if (ticks > timer->hw.ticks)
619                 ticks = timer->hw.ticks;
620         if (ticks_left != ticks)
621                 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
622         timer->sticks = ticks;
623 }
624
625 /*
626  * timer tasklet
627  *
628  */
629 static void snd_timer_tasklet(unsigned long arg)
630 {
631         struct snd_timer *timer = (struct snd_timer *) arg;
632         struct snd_timer_instance *ti;
633         struct list_head *p;
634         unsigned long resolution, ticks;
635         unsigned long flags;
636
637         spin_lock_irqsave(&timer->lock, flags);
638         /* now process all callbacks */
639         while (!list_empty(&timer->sack_list_head)) {
640                 p = timer->sack_list_head.next;         /* get first item */
641                 ti = list_entry(p, struct snd_timer_instance, ack_list);
642
643                 /* remove from ack_list and make empty */
644                 list_del_init(p);
645
646                 ticks = ti->pticks;
647                 ti->pticks = 0;
648                 resolution = ti->resolution;
649
650                 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
651                 spin_unlock(&timer->lock);
652                 if (ti->callback)
653                         ti->callback(ti, resolution, ticks);
654                 spin_lock(&timer->lock);
655                 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
656         }
657         spin_unlock_irqrestore(&timer->lock, flags);
658 }
659
660 /*
661  * timer interrupt
662  *
663  * ticks_left is usually equal to timer->sticks.
664  *
665  */
666 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
667 {
668         struct snd_timer_instance *ti, *ts, *tmp;
669         unsigned long resolution, ticks;
670         struct list_head *p, *ack_list_head;
671         unsigned long flags;
672         int use_tasklet = 0;
673
674         if (timer == NULL)
675                 return;
676
677         spin_lock_irqsave(&timer->lock, flags);
678
679         /* remember the current resolution */
680         if (timer->hw.c_resolution)
681                 resolution = timer->hw.c_resolution(timer);
682         else
683                 resolution = timer->hw.resolution;
684
685         /* loop for all active instances
686          * Here we cannot use list_for_each_entry because the active_list of a
687          * processed instance is relinked to done_list_head before the callback
688          * is called.
689          */
690         list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
691                                  active_list) {
692                 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
693                         continue;
694                 ti->pticks += ticks_left;
695                 ti->resolution = resolution;
696                 if (ti->cticks < ticks_left)
697                         ti->cticks = 0;
698                 else
699                         ti->cticks -= ticks_left;
700                 if (ti->cticks) /* not expired */
701                         continue;
702                 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
703                         ti->cticks = ti->ticks;
704                 } else {
705                         ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
706                         if (--timer->running)
707                                 list_del(&ti->active_list);
708                 }
709                 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
710                     (ti->flags & SNDRV_TIMER_IFLG_FAST))
711                         ack_list_head = &timer->ack_list_head;
712                 else
713                         ack_list_head = &timer->sack_list_head;
714                 if (list_empty(&ti->ack_list))
715                         list_add_tail(&ti->ack_list, ack_list_head);
716                 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
717                         ts->pticks = ti->pticks;
718                         ts->resolution = resolution;
719                         if (list_empty(&ts->ack_list))
720                                 list_add_tail(&ts->ack_list, ack_list_head);
721                 }
722         }
723         if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
724                 snd_timer_reschedule(timer, timer->sticks);
725         if (timer->running) {
726                 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
727                         timer->hw.stop(timer);
728                         timer->flags |= SNDRV_TIMER_FLG_CHANGE;
729                 }
730                 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
731                     (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
732                         /* restart timer */
733                         timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
734                         timer->hw.start(timer);
735                 }
736         } else {
737                 timer->hw.stop(timer);
738         }
739
740         /* now process all fast callbacks */
741         while (!list_empty(&timer->ack_list_head)) {
742                 p = timer->ack_list_head.next;          /* get first item */
743                 ti = list_entry(p, struct snd_timer_instance, ack_list);
744
745                 /* remove from ack_list and make empty */
746                 list_del_init(p);
747
748                 ticks = ti->pticks;
749                 ti->pticks = 0;
750
751                 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
752                 spin_unlock(&timer->lock);
753                 if (ti->callback)
754                         ti->callback(ti, resolution, ticks);
755                 spin_lock(&timer->lock);
756                 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
757         }
758
759         /* do we have any slow callbacks? */
760         use_tasklet = !list_empty(&timer->sack_list_head);
761         spin_unlock_irqrestore(&timer->lock, flags);
762
763         if (use_tasklet)
764                 tasklet_schedule(&timer->task_queue);
765 }
766
767 /*
768
769  */
770
771 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
772                   struct snd_timer **rtimer)
773 {
774         struct snd_timer *timer;
775         int err;
776         static struct snd_device_ops ops = {
777                 .dev_free = snd_timer_dev_free,
778                 .dev_register = snd_timer_dev_register,
779                 .dev_disconnect = snd_timer_dev_disconnect,
780         };
781
782         if (snd_BUG_ON(!tid))
783                 return -EINVAL;
784         if (rtimer)
785                 *rtimer = NULL;
786         timer = kzalloc(sizeof(*timer), GFP_KERNEL);
787         if (!timer)
788                 return -ENOMEM;
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                 pr_warn("ALSA: 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         setup_timer(&priv->tlist, snd_timer_s_function, (unsigned long) timer);
1042         timer->private_data = priv;
1043         timer->private_free = snd_timer_free_system;
1044         return snd_timer_global_register(timer);
1045 }
1046
1047 #ifdef CONFIG_SND_PROC_FS
1048 /*
1049  *  Info interface
1050  */
1051
1052 static void snd_timer_proc_read(struct snd_info_entry *entry,
1053                                 struct snd_info_buffer *buffer)
1054 {
1055         struct snd_timer *timer;
1056         struct snd_timer_instance *ti;
1057
1058         mutex_lock(&register_mutex);
1059         list_for_each_entry(timer, &snd_timer_list, device_list) {
1060                 switch (timer->tmr_class) {
1061                 case SNDRV_TIMER_CLASS_GLOBAL:
1062                         snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1063                         break;
1064                 case SNDRV_TIMER_CLASS_CARD:
1065                         snd_iprintf(buffer, "C%i-%i: ",
1066                                     timer->card->number, timer->tmr_device);
1067                         break;
1068                 case SNDRV_TIMER_CLASS_PCM:
1069                         snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1070                                     timer->tmr_device, timer->tmr_subdevice);
1071                         break;
1072                 default:
1073                         snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1074                                     timer->card ? timer->card->number : -1,
1075                                     timer->tmr_device, timer->tmr_subdevice);
1076                 }
1077                 snd_iprintf(buffer, "%s :", timer->name);
1078                 if (timer->hw.resolution)
1079                         snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1080                                     timer->hw.resolution / 1000,
1081                                     timer->hw.resolution % 1000,
1082                                     timer->hw.ticks);
1083                 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1084                         snd_iprintf(buffer, " SLAVE");
1085                 snd_iprintf(buffer, "\n");
1086                 list_for_each_entry(ti, &timer->open_list_head, open_list)
1087                         snd_iprintf(buffer, "  Client %s : %s\n",
1088                                     ti->owner ? ti->owner : "unknown",
1089                                     ti->flags & (SNDRV_TIMER_IFLG_START |
1090                                                  SNDRV_TIMER_IFLG_RUNNING)
1091                                     ? "running" : "stopped");
1092         }
1093         mutex_unlock(&register_mutex);
1094 }
1095
1096 static struct snd_info_entry *snd_timer_proc_entry;
1097
1098 static void __init snd_timer_proc_init(void)
1099 {
1100         struct snd_info_entry *entry;
1101
1102         entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1103         if (entry != NULL) {
1104                 entry->c.text.read = snd_timer_proc_read;
1105                 if (snd_info_register(entry) < 0) {
1106                         snd_info_free_entry(entry);
1107                         entry = NULL;
1108                 }
1109         }
1110         snd_timer_proc_entry = entry;
1111 }
1112
1113 static void __exit snd_timer_proc_done(void)
1114 {
1115         snd_info_free_entry(snd_timer_proc_entry);
1116 }
1117 #else /* !CONFIG_SND_PROC_FS */
1118 #define snd_timer_proc_init()
1119 #define snd_timer_proc_done()
1120 #endif
1121
1122 /*
1123  *  USER SPACE interface
1124  */
1125
1126 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1127                                      unsigned long resolution,
1128                                      unsigned long ticks)
1129 {
1130         struct snd_timer_user *tu = timeri->callback_data;
1131         struct snd_timer_read *r;
1132         int prev;
1133
1134         spin_lock(&tu->qlock);
1135         if (tu->qused > 0) {
1136                 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1137                 r = &tu->queue[prev];
1138                 if (r->resolution == resolution) {
1139                         r->ticks += ticks;
1140                         goto __wake;
1141                 }
1142         }
1143         if (tu->qused >= tu->queue_size) {
1144                 tu->overrun++;
1145         } else {
1146                 r = &tu->queue[tu->qtail++];
1147                 tu->qtail %= tu->queue_size;
1148                 r->resolution = resolution;
1149                 r->ticks = ticks;
1150                 tu->qused++;
1151         }
1152       __wake:
1153         spin_unlock(&tu->qlock);
1154         kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1155         wake_up(&tu->qchange_sleep);
1156 }
1157
1158 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1159                                             struct snd_timer_tread *tread)
1160 {
1161         if (tu->qused >= tu->queue_size) {
1162                 tu->overrun++;
1163         } else {
1164                 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1165                 tu->qtail %= tu->queue_size;
1166                 tu->qused++;
1167         }
1168 }
1169
1170 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1171                                      int event,
1172                                      struct timespec *tstamp,
1173                                      unsigned long resolution)
1174 {
1175         struct snd_timer_user *tu = timeri->callback_data;
1176         struct snd_timer_tread r1;
1177         unsigned long flags;
1178
1179         if (event >= SNDRV_TIMER_EVENT_START &&
1180             event <= SNDRV_TIMER_EVENT_PAUSE)
1181                 tu->tstamp = *tstamp;
1182         if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1183                 return;
1184         memset(&r1, 0, sizeof(r1));
1185         r1.event = event;
1186         r1.tstamp = *tstamp;
1187         r1.val = resolution;
1188         spin_lock_irqsave(&tu->qlock, flags);
1189         snd_timer_user_append_to_tqueue(tu, &r1);
1190         spin_unlock_irqrestore(&tu->qlock, flags);
1191         kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1192         wake_up(&tu->qchange_sleep);
1193 }
1194
1195 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1196                                       unsigned long resolution,
1197                                       unsigned long ticks)
1198 {
1199         struct snd_timer_user *tu = timeri->callback_data;
1200         struct snd_timer_tread *r, r1;
1201         struct timespec tstamp;
1202         int prev, append = 0;
1203
1204         memset(&tstamp, 0, sizeof(tstamp));
1205         spin_lock(&tu->qlock);
1206         if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1207                            (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1208                 spin_unlock(&tu->qlock);
1209                 return;
1210         }
1211         if (tu->last_resolution != resolution || ticks > 0) {
1212                 if (timer_tstamp_monotonic)
1213                         ktime_get_ts(&tstamp);
1214                 else
1215                         getnstimeofday(&tstamp);
1216         }
1217         if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1218             tu->last_resolution != resolution) {
1219                 memset(&r1, 0, sizeof(r1));
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                         memset(&tread, 0, sizeof(tread));
1686                         tread.event = SNDRV_TIMER_EVENT_EARLY;
1687                         tread.tstamp.tv_sec = 0;
1688                         tread.tstamp.tv_nsec = 0;
1689                         tread.val = 0;
1690                         snd_timer_user_append_to_tqueue(tu, &tread);
1691                 } else {
1692                         struct snd_timer_read *r = &tu->queue[0];
1693                         r->resolution = 0;
1694                         r->ticks = 0;
1695                         tu->qused++;
1696                         tu->qtail++;
1697                 }
1698         }
1699         tu->filter = params.filter;
1700         tu->ticks = params.ticks;
1701         err = 0;
1702  _end:
1703         if (copy_to_user(_params, &params, sizeof(params)))
1704                 return -EFAULT;
1705         return err;
1706 }
1707
1708 static int snd_timer_user_status(struct file *file,
1709                                  struct snd_timer_status __user *_status)
1710 {
1711         struct snd_timer_user *tu;
1712         struct snd_timer_status status;
1713
1714         tu = file->private_data;
1715         if (!tu->timeri)
1716                 return -EBADFD;
1717         memset(&status, 0, sizeof(status));
1718         status.tstamp = tu->tstamp;
1719         status.resolution = snd_timer_resolution(tu->timeri);
1720         status.lost = tu->timeri->lost;
1721         status.overrun = tu->overrun;
1722         spin_lock_irq(&tu->qlock);
1723         status.queue = tu->qused;
1724         spin_unlock_irq(&tu->qlock);
1725         if (copy_to_user(_status, &status, sizeof(status)))
1726                 return -EFAULT;
1727         return 0;
1728 }
1729
1730 static int snd_timer_user_start(struct file *file)
1731 {
1732         int err;
1733         struct snd_timer_user *tu;
1734
1735         tu = file->private_data;
1736         if (!tu->timeri)
1737                 return -EBADFD;
1738         snd_timer_stop(tu->timeri);
1739         tu->timeri->lost = 0;
1740         tu->last_resolution = 0;
1741         return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1742 }
1743
1744 static int snd_timer_user_stop(struct file *file)
1745 {
1746         int err;
1747         struct snd_timer_user *tu;
1748
1749         tu = file->private_data;
1750         if (!tu->timeri)
1751                 return -EBADFD;
1752         return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1753 }
1754
1755 static int snd_timer_user_continue(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         tu->timeri->lost = 0;
1764         return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1765 }
1766
1767 static int snd_timer_user_pause(struct file *file)
1768 {
1769         int err;
1770         struct snd_timer_user *tu;
1771
1772         tu = file->private_data;
1773         if (!tu->timeri)
1774                 return -EBADFD;
1775         return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1776 }
1777
1778 enum {
1779         SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1780         SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1781         SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1782         SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1783 };
1784
1785 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1786                                  unsigned long arg)
1787 {
1788         struct snd_timer_user *tu;
1789         void __user *argp = (void __user *)arg;
1790         int __user *p = argp;
1791
1792         tu = file->private_data;
1793         switch (cmd) {
1794         case SNDRV_TIMER_IOCTL_PVERSION:
1795                 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1796         case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1797                 return snd_timer_user_next_device(argp);
1798         case SNDRV_TIMER_IOCTL_TREAD:
1799         {
1800                 int xarg;
1801
1802                 if (tu->timeri) /* too late */
1803                         return -EBUSY;
1804                 if (get_user(xarg, p))
1805                         return -EFAULT;
1806                 tu->tread = xarg ? 1 : 0;
1807                 return 0;
1808         }
1809         case SNDRV_TIMER_IOCTL_GINFO:
1810                 return snd_timer_user_ginfo(file, argp);
1811         case SNDRV_TIMER_IOCTL_GPARAMS:
1812                 return snd_timer_user_gparams(file, argp);
1813         case SNDRV_TIMER_IOCTL_GSTATUS:
1814                 return snd_timer_user_gstatus(file, argp);
1815         case SNDRV_TIMER_IOCTL_SELECT:
1816                 return snd_timer_user_tselect(file, argp);
1817         case SNDRV_TIMER_IOCTL_INFO:
1818                 return snd_timer_user_info(file, argp);
1819         case SNDRV_TIMER_IOCTL_PARAMS:
1820                 return snd_timer_user_params(file, argp);
1821         case SNDRV_TIMER_IOCTL_STATUS:
1822                 return snd_timer_user_status(file, argp);
1823         case SNDRV_TIMER_IOCTL_START:
1824         case SNDRV_TIMER_IOCTL_START_OLD:
1825                 return snd_timer_user_start(file);
1826         case SNDRV_TIMER_IOCTL_STOP:
1827         case SNDRV_TIMER_IOCTL_STOP_OLD:
1828                 return snd_timer_user_stop(file);
1829         case SNDRV_TIMER_IOCTL_CONTINUE:
1830         case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1831                 return snd_timer_user_continue(file);
1832         case SNDRV_TIMER_IOCTL_PAUSE:
1833         case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1834                 return snd_timer_user_pause(file);
1835         }
1836         return -ENOTTY;
1837 }
1838
1839 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1840                                  unsigned long arg)
1841 {
1842         struct snd_timer_user *tu = file->private_data;
1843         long ret;
1844
1845         mutex_lock(&tu->ioctl_lock);
1846         ret = __snd_timer_user_ioctl(file, cmd, arg);
1847         mutex_unlock(&tu->ioctl_lock);
1848         return ret;
1849 }
1850
1851 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1852 {
1853         struct snd_timer_user *tu;
1854
1855         tu = file->private_data;
1856         return fasync_helper(fd, file, on, &tu->fasync);
1857 }
1858
1859 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1860                                    size_t count, loff_t *offset)
1861 {
1862         struct snd_timer_user *tu;
1863         long result = 0, unit;
1864         int err = 0;
1865
1866         tu = file->private_data;
1867         unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1868         spin_lock_irq(&tu->qlock);
1869         while ((long)count - result >= unit) {
1870                 while (!tu->qused) {
1871                         wait_queue_t wait;
1872
1873                         if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1874                                 err = -EAGAIN;
1875                                 break;
1876                         }
1877
1878                         set_current_state(TASK_INTERRUPTIBLE);
1879                         init_waitqueue_entry(&wait, current);
1880                         add_wait_queue(&tu->qchange_sleep, &wait);
1881
1882                         spin_unlock_irq(&tu->qlock);
1883                         schedule();
1884                         spin_lock_irq(&tu->qlock);
1885
1886                         remove_wait_queue(&tu->qchange_sleep, &wait);
1887
1888                         if (signal_pending(current)) {
1889                                 err = -ERESTARTSYS;
1890                                 break;
1891                         }
1892                 }
1893
1894                 spin_unlock_irq(&tu->qlock);
1895                 if (err < 0)
1896                         goto _error;
1897
1898                 if (tu->tread) {
1899                         if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
1900                                          sizeof(struct snd_timer_tread))) {
1901                                 err = -EFAULT;
1902                                 goto _error;
1903                         }
1904                 } else {
1905                         if (copy_to_user(buffer, &tu->queue[tu->qhead++],
1906                                          sizeof(struct snd_timer_read))) {
1907                                 err = -EFAULT;
1908                                 goto _error;
1909                         }
1910                 }
1911
1912                 tu->qhead %= tu->queue_size;
1913
1914                 result += unit;
1915                 buffer += unit;
1916
1917                 spin_lock_irq(&tu->qlock);
1918                 tu->qused--;
1919         }
1920         spin_unlock_irq(&tu->qlock);
1921  _error:
1922         return result > 0 ? result : err;
1923 }
1924
1925 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1926 {
1927         unsigned int mask;
1928         struct snd_timer_user *tu;
1929
1930         tu = file->private_data;
1931
1932         poll_wait(file, &tu->qchange_sleep, wait);
1933
1934         mask = 0;
1935         if (tu->qused)
1936                 mask |= POLLIN | POLLRDNORM;
1937
1938         return mask;
1939 }
1940
1941 #ifdef CONFIG_COMPAT
1942 #include "timer_compat.c"
1943 #else
1944 #define snd_timer_user_ioctl_compat     NULL
1945 #endif
1946
1947 static const struct file_operations snd_timer_f_ops =
1948 {
1949         .owner =        THIS_MODULE,
1950         .read =         snd_timer_user_read,
1951         .open =         snd_timer_user_open,
1952         .release =      snd_timer_user_release,
1953         .llseek =       no_llseek,
1954         .poll =         snd_timer_user_poll,
1955         .unlocked_ioctl =       snd_timer_user_ioctl,
1956         .compat_ioctl = snd_timer_user_ioctl_compat,
1957         .fasync =       snd_timer_user_fasync,
1958 };
1959
1960 /* unregister the system timer */
1961 static void snd_timer_free_all(void)
1962 {
1963         struct snd_timer *timer, *n;
1964
1965         list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
1966                 snd_timer_free(timer);
1967 }
1968
1969 static struct device timer_dev;
1970
1971 /*
1972  *  ENTRY functions
1973  */
1974
1975 static int __init alsa_timer_init(void)
1976 {
1977         int err;
1978
1979         snd_device_initialize(&timer_dev, NULL);
1980         dev_set_name(&timer_dev, "timer");
1981
1982 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1983         snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1984                               "system timer");
1985 #endif
1986
1987         err = snd_timer_register_system();
1988         if (err < 0) {
1989                 pr_err("ALSA: unable to register system timer (%i)\n", err);
1990                 put_device(&timer_dev);
1991                 return err;
1992         }
1993
1994         err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
1995                                   &snd_timer_f_ops, NULL, &timer_dev);
1996         if (err < 0) {
1997                 pr_err("ALSA: unable to register timer device (%i)\n", err);
1998                 snd_timer_free_all();
1999                 put_device(&timer_dev);
2000                 return err;
2001         }
2002
2003         snd_timer_proc_init();
2004         return 0;
2005 }
2006
2007 static void __exit alsa_timer_exit(void)
2008 {
2009         snd_unregister_device(&timer_dev);
2010         snd_timer_free_all();
2011         put_device(&timer_dev);
2012         snd_timer_proc_done();
2013 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2014         snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2015 #endif
2016 }
2017
2018 module_init(alsa_timer_init)
2019 module_exit(alsa_timer_exit)
2020
2021 EXPORT_SYMBOL(snd_timer_open);
2022 EXPORT_SYMBOL(snd_timer_close);
2023 EXPORT_SYMBOL(snd_timer_resolution);
2024 EXPORT_SYMBOL(snd_timer_start);
2025 EXPORT_SYMBOL(snd_timer_stop);
2026 EXPORT_SYMBOL(snd_timer_continue);
2027 EXPORT_SYMBOL(snd_timer_pause);
2028 EXPORT_SYMBOL(snd_timer_new);
2029 EXPORT_SYMBOL(snd_timer_notify);
2030 EXPORT_SYMBOL(snd_timer_global_new);
2031 EXPORT_SYMBOL(snd_timer_global_free);
2032 EXPORT_SYMBOL(snd_timer_global_register);
2033 EXPORT_SYMBOL(snd_timer_interrupt);