UPSTREAM: ALSA: timer: Harden slave timer list handling
[firefly-linux-kernel-4.4.55.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 tread_sem;
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         r1.event = event;
1185         r1.tstamp = *tstamp;
1186         r1.val = resolution;
1187         spin_lock_irqsave(&tu->qlock, flags);
1188         snd_timer_user_append_to_tqueue(tu, &r1);
1189         spin_unlock_irqrestore(&tu->qlock, flags);
1190         kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1191         wake_up(&tu->qchange_sleep);
1192 }
1193
1194 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1195                                       unsigned long resolution,
1196                                       unsigned long ticks)
1197 {
1198         struct snd_timer_user *tu = timeri->callback_data;
1199         struct snd_timer_tread *r, r1;
1200         struct timespec tstamp;
1201         int prev, append = 0;
1202
1203         memset(&tstamp, 0, sizeof(tstamp));
1204         spin_lock(&tu->qlock);
1205         if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1206                            (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1207                 spin_unlock(&tu->qlock);
1208                 return;
1209         }
1210         if (tu->last_resolution != resolution || ticks > 0) {
1211                 if (timer_tstamp_monotonic)
1212                         ktime_get_ts(&tstamp);
1213                 else
1214                         getnstimeofday(&tstamp);
1215         }
1216         if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1217             tu->last_resolution != resolution) {
1218                 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1219                 r1.tstamp = tstamp;
1220                 r1.val = resolution;
1221                 snd_timer_user_append_to_tqueue(tu, &r1);
1222                 tu->last_resolution = resolution;
1223                 append++;
1224         }
1225         if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1226                 goto __wake;
1227         if (ticks == 0)
1228                 goto __wake;
1229         if (tu->qused > 0) {
1230                 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1231                 r = &tu->tqueue[prev];
1232                 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1233                         r->tstamp = tstamp;
1234                         r->val += ticks;
1235                         append++;
1236                         goto __wake;
1237                 }
1238         }
1239         r1.event = SNDRV_TIMER_EVENT_TICK;
1240         r1.tstamp = tstamp;
1241         r1.val = ticks;
1242         snd_timer_user_append_to_tqueue(tu, &r1);
1243         append++;
1244       __wake:
1245         spin_unlock(&tu->qlock);
1246         if (append == 0)
1247                 return;
1248         kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1249         wake_up(&tu->qchange_sleep);
1250 }
1251
1252 static int snd_timer_user_open(struct inode *inode, struct file *file)
1253 {
1254         struct snd_timer_user *tu;
1255         int err;
1256
1257         err = nonseekable_open(inode, file);
1258         if (err < 0)
1259                 return err;
1260
1261         tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1262         if (tu == NULL)
1263                 return -ENOMEM;
1264         spin_lock_init(&tu->qlock);
1265         init_waitqueue_head(&tu->qchange_sleep);
1266         mutex_init(&tu->tread_sem);
1267         tu->ticks = 1;
1268         tu->queue_size = 128;
1269         tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1270                             GFP_KERNEL);
1271         if (tu->queue == NULL) {
1272                 kfree(tu);
1273                 return -ENOMEM;
1274         }
1275         file->private_data = tu;
1276         return 0;
1277 }
1278
1279 static int snd_timer_user_release(struct inode *inode, struct file *file)
1280 {
1281         struct snd_timer_user *tu;
1282
1283         if (file->private_data) {
1284                 tu = file->private_data;
1285                 file->private_data = NULL;
1286                 if (tu->timeri)
1287                         snd_timer_close(tu->timeri);
1288                 kfree(tu->queue);
1289                 kfree(tu->tqueue);
1290                 kfree(tu);
1291         }
1292         return 0;
1293 }
1294
1295 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1296 {
1297         id->dev_class = SNDRV_TIMER_CLASS_NONE;
1298         id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1299         id->card = -1;
1300         id->device = -1;
1301         id->subdevice = -1;
1302 }
1303
1304 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1305 {
1306         id->dev_class = timer->tmr_class;
1307         id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1308         id->card = timer->card ? timer->card->number : -1;
1309         id->device = timer->tmr_device;
1310         id->subdevice = timer->tmr_subdevice;
1311 }
1312
1313 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1314 {
1315         struct snd_timer_id id;
1316         struct snd_timer *timer;
1317         struct list_head *p;
1318
1319         if (copy_from_user(&id, _tid, sizeof(id)))
1320                 return -EFAULT;
1321         mutex_lock(&register_mutex);
1322         if (id.dev_class < 0) {         /* first item */
1323                 if (list_empty(&snd_timer_list))
1324                         snd_timer_user_zero_id(&id);
1325                 else {
1326                         timer = list_entry(snd_timer_list.next,
1327                                            struct snd_timer, device_list);
1328                         snd_timer_user_copy_id(&id, timer);
1329                 }
1330         } else {
1331                 switch (id.dev_class) {
1332                 case SNDRV_TIMER_CLASS_GLOBAL:
1333                         id.device = id.device < 0 ? 0 : id.device + 1;
1334                         list_for_each(p, &snd_timer_list) {
1335                                 timer = list_entry(p, struct snd_timer, device_list);
1336                                 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1337                                         snd_timer_user_copy_id(&id, timer);
1338                                         break;
1339                                 }
1340                                 if (timer->tmr_device >= id.device) {
1341                                         snd_timer_user_copy_id(&id, timer);
1342                                         break;
1343                                 }
1344                         }
1345                         if (p == &snd_timer_list)
1346                                 snd_timer_user_zero_id(&id);
1347                         break;
1348                 case SNDRV_TIMER_CLASS_CARD:
1349                 case SNDRV_TIMER_CLASS_PCM:
1350                         if (id.card < 0) {
1351                                 id.card = 0;
1352                         } else {
1353                                 if (id.card < 0) {
1354                                         id.card = 0;
1355                                 } else {
1356                                         if (id.device < 0) {
1357                                                 id.device = 0;
1358                                         } else {
1359                                                 if (id.subdevice < 0) {
1360                                                         id.subdevice = 0;
1361                                                 } else {
1362                                                         id.subdevice++;
1363                                                 }
1364                                         }
1365                                 }
1366                         }
1367                         list_for_each(p, &snd_timer_list) {
1368                                 timer = list_entry(p, struct snd_timer, device_list);
1369                                 if (timer->tmr_class > id.dev_class) {
1370                                         snd_timer_user_copy_id(&id, timer);
1371                                         break;
1372                                 }
1373                                 if (timer->tmr_class < id.dev_class)
1374                                         continue;
1375                                 if (timer->card->number > id.card) {
1376                                         snd_timer_user_copy_id(&id, timer);
1377                                         break;
1378                                 }
1379                                 if (timer->card->number < id.card)
1380                                         continue;
1381                                 if (timer->tmr_device > id.device) {
1382                                         snd_timer_user_copy_id(&id, timer);
1383                                         break;
1384                                 }
1385                                 if (timer->tmr_device < id.device)
1386                                         continue;
1387                                 if (timer->tmr_subdevice > id.subdevice) {
1388                                         snd_timer_user_copy_id(&id, timer);
1389                                         break;
1390                                 }
1391                                 if (timer->tmr_subdevice < id.subdevice)
1392                                         continue;
1393                                 snd_timer_user_copy_id(&id, timer);
1394                                 break;
1395                         }
1396                         if (p == &snd_timer_list)
1397                                 snd_timer_user_zero_id(&id);
1398                         break;
1399                 default:
1400                         snd_timer_user_zero_id(&id);
1401                 }
1402         }
1403         mutex_unlock(&register_mutex);
1404         if (copy_to_user(_tid, &id, sizeof(*_tid)))
1405                 return -EFAULT;
1406         return 0;
1407 }
1408
1409 static int snd_timer_user_ginfo(struct file *file,
1410                                 struct snd_timer_ginfo __user *_ginfo)
1411 {
1412         struct snd_timer_ginfo *ginfo;
1413         struct snd_timer_id tid;
1414         struct snd_timer *t;
1415         struct list_head *p;
1416         int err = 0;
1417
1418         ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1419         if (IS_ERR(ginfo))
1420                 return PTR_ERR(ginfo);
1421
1422         tid = ginfo->tid;
1423         memset(ginfo, 0, sizeof(*ginfo));
1424         ginfo->tid = tid;
1425         mutex_lock(&register_mutex);
1426         t = snd_timer_find(&tid);
1427         if (t != NULL) {
1428                 ginfo->card = t->card ? t->card->number : -1;
1429                 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1430                         ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1431                 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1432                 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1433                 ginfo->resolution = t->hw.resolution;
1434                 if (t->hw.resolution_min > 0) {
1435                         ginfo->resolution_min = t->hw.resolution_min;
1436                         ginfo->resolution_max = t->hw.resolution_max;
1437                 }
1438                 list_for_each(p, &t->open_list_head) {
1439                         ginfo->clients++;
1440                 }
1441         } else {
1442                 err = -ENODEV;
1443         }
1444         mutex_unlock(&register_mutex);
1445         if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1446                 err = -EFAULT;
1447         kfree(ginfo);
1448         return err;
1449 }
1450
1451 static int snd_timer_user_gparams(struct file *file,
1452                                   struct snd_timer_gparams __user *_gparams)
1453 {
1454         struct snd_timer_gparams gparams;
1455         struct snd_timer *t;
1456         int err;
1457
1458         if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1459                 return -EFAULT;
1460         mutex_lock(&register_mutex);
1461         t = snd_timer_find(&gparams.tid);
1462         if (!t) {
1463                 err = -ENODEV;
1464                 goto _error;
1465         }
1466         if (!list_empty(&t->open_list_head)) {
1467                 err = -EBUSY;
1468                 goto _error;
1469         }
1470         if (!t->hw.set_period) {
1471                 err = -ENOSYS;
1472                 goto _error;
1473         }
1474         err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1475 _error:
1476         mutex_unlock(&register_mutex);
1477         return err;
1478 }
1479
1480 static int snd_timer_user_gstatus(struct file *file,
1481                                   struct snd_timer_gstatus __user *_gstatus)
1482 {
1483         struct snd_timer_gstatus gstatus;
1484         struct snd_timer_id tid;
1485         struct snd_timer *t;
1486         int err = 0;
1487
1488         if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1489                 return -EFAULT;
1490         tid = gstatus.tid;
1491         memset(&gstatus, 0, sizeof(gstatus));
1492         gstatus.tid = tid;
1493         mutex_lock(&register_mutex);
1494         t = snd_timer_find(&tid);
1495         if (t != NULL) {
1496                 if (t->hw.c_resolution)
1497                         gstatus.resolution = t->hw.c_resolution(t);
1498                 else
1499                         gstatus.resolution = t->hw.resolution;
1500                 if (t->hw.precise_resolution) {
1501                         t->hw.precise_resolution(t, &gstatus.resolution_num,
1502                                                  &gstatus.resolution_den);
1503                 } else {
1504                         gstatus.resolution_num = gstatus.resolution;
1505                         gstatus.resolution_den = 1000000000uL;
1506                 }
1507         } else {
1508                 err = -ENODEV;
1509         }
1510         mutex_unlock(&register_mutex);
1511         if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1512                 err = -EFAULT;
1513         return err;
1514 }
1515
1516 static int snd_timer_user_tselect(struct file *file,
1517                                   struct snd_timer_select __user *_tselect)
1518 {
1519         struct snd_timer_user *tu;
1520         struct snd_timer_select tselect;
1521         char str[32];
1522         int err = 0;
1523
1524         tu = file->private_data;
1525         mutex_lock(&tu->tread_sem);
1526         if (tu->timeri) {
1527                 snd_timer_close(tu->timeri);
1528                 tu->timeri = NULL;
1529         }
1530         if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1531                 err = -EFAULT;
1532                 goto __err;
1533         }
1534         sprintf(str, "application %i", current->pid);
1535         if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1536                 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1537         err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1538         if (err < 0)
1539                 goto __err;
1540
1541         kfree(tu->queue);
1542         tu->queue = NULL;
1543         kfree(tu->tqueue);
1544         tu->tqueue = NULL;
1545         if (tu->tread) {
1546                 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1547                                      GFP_KERNEL);
1548                 if (tu->tqueue == NULL)
1549                         err = -ENOMEM;
1550         } else {
1551                 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1552                                     GFP_KERNEL);
1553                 if (tu->queue == NULL)
1554                         err = -ENOMEM;
1555         }
1556
1557         if (err < 0) {
1558                 snd_timer_close(tu->timeri);
1559                 tu->timeri = NULL;
1560         } else {
1561                 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1562                 tu->timeri->callback = tu->tread
1563                         ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1564                 tu->timeri->ccallback = snd_timer_user_ccallback;
1565                 tu->timeri->callback_data = (void *)tu;
1566         }
1567
1568       __err:
1569         mutex_unlock(&tu->tread_sem);
1570         return err;
1571 }
1572
1573 static int snd_timer_user_info(struct file *file,
1574                                struct snd_timer_info __user *_info)
1575 {
1576         struct snd_timer_user *tu;
1577         struct snd_timer_info *info;
1578         struct snd_timer *t;
1579         int err = 0;
1580
1581         tu = file->private_data;
1582         if (!tu->timeri)
1583                 return -EBADFD;
1584         t = tu->timeri->timer;
1585         if (!t)
1586                 return -EBADFD;
1587
1588         info = kzalloc(sizeof(*info), GFP_KERNEL);
1589         if (! info)
1590                 return -ENOMEM;
1591         info->card = t->card ? t->card->number : -1;
1592         if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1593                 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1594         strlcpy(info->id, t->id, sizeof(info->id));
1595         strlcpy(info->name, t->name, sizeof(info->name));
1596         info->resolution = t->hw.resolution;
1597         if (copy_to_user(_info, info, sizeof(*_info)))
1598                 err = -EFAULT;
1599         kfree(info);
1600         return err;
1601 }
1602
1603 static int snd_timer_user_params(struct file *file,
1604                                  struct snd_timer_params __user *_params)
1605 {
1606         struct snd_timer_user *tu;
1607         struct snd_timer_params params;
1608         struct snd_timer *t;
1609         struct snd_timer_read *tr;
1610         struct snd_timer_tread *ttr;
1611         int err;
1612
1613         tu = file->private_data;
1614         if (!tu->timeri)
1615                 return -EBADFD;
1616         t = tu->timeri->timer;
1617         if (!t)
1618                 return -EBADFD;
1619         if (copy_from_user(&params, _params, sizeof(params)))
1620                 return -EFAULT;
1621         if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1622                 err = -EINVAL;
1623                 goto _end;
1624         }
1625         if (params.queue_size > 0 &&
1626             (params.queue_size < 32 || params.queue_size > 1024)) {
1627                 err = -EINVAL;
1628                 goto _end;
1629         }
1630         if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1631                               (1<<SNDRV_TIMER_EVENT_TICK)|
1632                               (1<<SNDRV_TIMER_EVENT_START)|
1633                               (1<<SNDRV_TIMER_EVENT_STOP)|
1634                               (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1635                               (1<<SNDRV_TIMER_EVENT_PAUSE)|
1636                               (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1637                               (1<<SNDRV_TIMER_EVENT_RESUME)|
1638                               (1<<SNDRV_TIMER_EVENT_MSTART)|
1639                               (1<<SNDRV_TIMER_EVENT_MSTOP)|
1640                               (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1641                               (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1642                               (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1643                               (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1644                 err = -EINVAL;
1645                 goto _end;
1646         }
1647         snd_timer_stop(tu->timeri);
1648         spin_lock_irq(&t->lock);
1649         tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1650                                SNDRV_TIMER_IFLG_EXCLUSIVE|
1651                                SNDRV_TIMER_IFLG_EARLY_EVENT);
1652         if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1653                 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1654         if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1655                 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1656         if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1657                 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1658         spin_unlock_irq(&t->lock);
1659         if (params.queue_size > 0 &&
1660             (unsigned int)tu->queue_size != params.queue_size) {
1661                 if (tu->tread) {
1662                         ttr = kmalloc(params.queue_size * sizeof(*ttr),
1663                                       GFP_KERNEL);
1664                         if (ttr) {
1665                                 kfree(tu->tqueue);
1666                                 tu->queue_size = params.queue_size;
1667                                 tu->tqueue = ttr;
1668                         }
1669                 } else {
1670                         tr = kmalloc(params.queue_size * sizeof(*tr),
1671                                      GFP_KERNEL);
1672                         if (tr) {
1673                                 kfree(tu->queue);
1674                                 tu->queue_size = params.queue_size;
1675                                 tu->queue = tr;
1676                         }
1677                 }
1678         }
1679         tu->qhead = tu->qtail = tu->qused = 0;
1680         if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1681                 if (tu->tread) {
1682                         struct snd_timer_tread tread;
1683                         tread.event = SNDRV_TIMER_EVENT_EARLY;
1684                         tread.tstamp.tv_sec = 0;
1685                         tread.tstamp.tv_nsec = 0;
1686                         tread.val = 0;
1687                         snd_timer_user_append_to_tqueue(tu, &tread);
1688                 } else {
1689                         struct snd_timer_read *r = &tu->queue[0];
1690                         r->resolution = 0;
1691                         r->ticks = 0;
1692                         tu->qused++;
1693                         tu->qtail++;
1694                 }
1695         }
1696         tu->filter = params.filter;
1697         tu->ticks = params.ticks;
1698         err = 0;
1699  _end:
1700         if (copy_to_user(_params, &params, sizeof(params)))
1701                 return -EFAULT;
1702         return err;
1703 }
1704
1705 static int snd_timer_user_status(struct file *file,
1706                                  struct snd_timer_status __user *_status)
1707 {
1708         struct snd_timer_user *tu;
1709         struct snd_timer_status status;
1710
1711         tu = file->private_data;
1712         if (!tu->timeri)
1713                 return -EBADFD;
1714         memset(&status, 0, sizeof(status));
1715         status.tstamp = tu->tstamp;
1716         status.resolution = snd_timer_resolution(tu->timeri);
1717         status.lost = tu->timeri->lost;
1718         status.overrun = tu->overrun;
1719         spin_lock_irq(&tu->qlock);
1720         status.queue = tu->qused;
1721         spin_unlock_irq(&tu->qlock);
1722         if (copy_to_user(_status, &status, sizeof(status)))
1723                 return -EFAULT;
1724         return 0;
1725 }
1726
1727 static int snd_timer_user_start(struct file *file)
1728 {
1729         int err;
1730         struct snd_timer_user *tu;
1731
1732         tu = file->private_data;
1733         if (!tu->timeri)
1734                 return -EBADFD;
1735         snd_timer_stop(tu->timeri);
1736         tu->timeri->lost = 0;
1737         tu->last_resolution = 0;
1738         return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1739 }
1740
1741 static int snd_timer_user_stop(struct file *file)
1742 {
1743         int err;
1744         struct snd_timer_user *tu;
1745
1746         tu = file->private_data;
1747         if (!tu->timeri)
1748                 return -EBADFD;
1749         return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1750 }
1751
1752 static int snd_timer_user_continue(struct file *file)
1753 {
1754         int err;
1755         struct snd_timer_user *tu;
1756
1757         tu = file->private_data;
1758         if (!tu->timeri)
1759                 return -EBADFD;
1760         tu->timeri->lost = 0;
1761         return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1762 }
1763
1764 static int snd_timer_user_pause(struct file *file)
1765 {
1766         int err;
1767         struct snd_timer_user *tu;
1768
1769         tu = file->private_data;
1770         if (!tu->timeri)
1771                 return -EBADFD;
1772         return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1773 }
1774
1775 enum {
1776         SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1777         SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1778         SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1779         SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1780 };
1781
1782 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1783                                  unsigned long arg)
1784 {
1785         struct snd_timer_user *tu;
1786         void __user *argp = (void __user *)arg;
1787         int __user *p = argp;
1788
1789         tu = file->private_data;
1790         switch (cmd) {
1791         case SNDRV_TIMER_IOCTL_PVERSION:
1792                 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1793         case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1794                 return snd_timer_user_next_device(argp);
1795         case SNDRV_TIMER_IOCTL_TREAD:
1796         {
1797                 int xarg;
1798
1799                 mutex_lock(&tu->tread_sem);
1800                 if (tu->timeri) {       /* too late */
1801                         mutex_unlock(&tu->tread_sem);
1802                         return -EBUSY;
1803                 }
1804                 if (get_user(xarg, p)) {
1805                         mutex_unlock(&tu->tread_sem);
1806                         return -EFAULT;
1807                 }
1808                 tu->tread = xarg ? 1 : 0;
1809                 mutex_unlock(&tu->tread_sem);
1810                 return 0;
1811         }
1812         case SNDRV_TIMER_IOCTL_GINFO:
1813                 return snd_timer_user_ginfo(file, argp);
1814         case SNDRV_TIMER_IOCTL_GPARAMS:
1815                 return snd_timer_user_gparams(file, argp);
1816         case SNDRV_TIMER_IOCTL_GSTATUS:
1817                 return snd_timer_user_gstatus(file, argp);
1818         case SNDRV_TIMER_IOCTL_SELECT:
1819                 return snd_timer_user_tselect(file, argp);
1820         case SNDRV_TIMER_IOCTL_INFO:
1821                 return snd_timer_user_info(file, argp);
1822         case SNDRV_TIMER_IOCTL_PARAMS:
1823                 return snd_timer_user_params(file, argp);
1824         case SNDRV_TIMER_IOCTL_STATUS:
1825                 return snd_timer_user_status(file, argp);
1826         case SNDRV_TIMER_IOCTL_START:
1827         case SNDRV_TIMER_IOCTL_START_OLD:
1828                 return snd_timer_user_start(file);
1829         case SNDRV_TIMER_IOCTL_STOP:
1830         case SNDRV_TIMER_IOCTL_STOP_OLD:
1831                 return snd_timer_user_stop(file);
1832         case SNDRV_TIMER_IOCTL_CONTINUE:
1833         case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
1834                 return snd_timer_user_continue(file);
1835         case SNDRV_TIMER_IOCTL_PAUSE:
1836         case SNDRV_TIMER_IOCTL_PAUSE_OLD:
1837                 return snd_timer_user_pause(file);
1838         }
1839         return -ENOTTY;
1840 }
1841
1842 static int snd_timer_user_fasync(int fd, struct file * file, int on)
1843 {
1844         struct snd_timer_user *tu;
1845
1846         tu = file->private_data;
1847         return fasync_helper(fd, file, on, &tu->fasync);
1848 }
1849
1850 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1851                                    size_t count, loff_t *offset)
1852 {
1853         struct snd_timer_user *tu;
1854         long result = 0, unit;
1855         int err = 0;
1856
1857         tu = file->private_data;
1858         unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1859         spin_lock_irq(&tu->qlock);
1860         while ((long)count - result >= unit) {
1861                 while (!tu->qused) {
1862                         wait_queue_t wait;
1863
1864                         if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1865                                 err = -EAGAIN;
1866                                 break;
1867                         }
1868
1869                         set_current_state(TASK_INTERRUPTIBLE);
1870                         init_waitqueue_entry(&wait, current);
1871                         add_wait_queue(&tu->qchange_sleep, &wait);
1872
1873                         spin_unlock_irq(&tu->qlock);
1874                         schedule();
1875                         spin_lock_irq(&tu->qlock);
1876
1877                         remove_wait_queue(&tu->qchange_sleep, &wait);
1878
1879                         if (signal_pending(current)) {
1880                                 err = -ERESTARTSYS;
1881                                 break;
1882                         }
1883                 }
1884
1885                 spin_unlock_irq(&tu->qlock);
1886                 if (err < 0)
1887                         goto _error;
1888
1889                 if (tu->tread) {
1890                         if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
1891                                          sizeof(struct snd_timer_tread))) {
1892                                 err = -EFAULT;
1893                                 goto _error;
1894                         }
1895                 } else {
1896                         if (copy_to_user(buffer, &tu->queue[tu->qhead++],
1897                                          sizeof(struct snd_timer_read))) {
1898                                 err = -EFAULT;
1899                                 goto _error;
1900                         }
1901                 }
1902
1903                 tu->qhead %= tu->queue_size;
1904
1905                 result += unit;
1906                 buffer += unit;
1907
1908                 spin_lock_irq(&tu->qlock);
1909                 tu->qused--;
1910         }
1911         spin_unlock_irq(&tu->qlock);
1912  _error:
1913         return result > 0 ? result : err;
1914 }
1915
1916 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1917 {
1918         unsigned int mask;
1919         struct snd_timer_user *tu;
1920
1921         tu = file->private_data;
1922
1923         poll_wait(file, &tu->qchange_sleep, wait);
1924
1925         mask = 0;
1926         if (tu->qused)
1927                 mask |= POLLIN | POLLRDNORM;
1928
1929         return mask;
1930 }
1931
1932 #ifdef CONFIG_COMPAT
1933 #include "timer_compat.c"
1934 #else
1935 #define snd_timer_user_ioctl_compat     NULL
1936 #endif
1937
1938 static const struct file_operations snd_timer_f_ops =
1939 {
1940         .owner =        THIS_MODULE,
1941         .read =         snd_timer_user_read,
1942         .open =         snd_timer_user_open,
1943         .release =      snd_timer_user_release,
1944         .llseek =       no_llseek,
1945         .poll =         snd_timer_user_poll,
1946         .unlocked_ioctl =       snd_timer_user_ioctl,
1947         .compat_ioctl = snd_timer_user_ioctl_compat,
1948         .fasync =       snd_timer_user_fasync,
1949 };
1950
1951 /* unregister the system timer */
1952 static void snd_timer_free_all(void)
1953 {
1954         struct snd_timer *timer, *n;
1955
1956         list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
1957                 snd_timer_free(timer);
1958 }
1959
1960 static struct device timer_dev;
1961
1962 /*
1963  *  ENTRY functions
1964  */
1965
1966 static int __init alsa_timer_init(void)
1967 {
1968         int err;
1969
1970         snd_device_initialize(&timer_dev, NULL);
1971         dev_set_name(&timer_dev, "timer");
1972
1973 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
1974         snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1975                               "system timer");
1976 #endif
1977
1978         err = snd_timer_register_system();
1979         if (err < 0) {
1980                 pr_err("ALSA: unable to register system timer (%i)\n", err);
1981                 put_device(&timer_dev);
1982                 return err;
1983         }
1984
1985         err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
1986                                   &snd_timer_f_ops, NULL, &timer_dev);
1987         if (err < 0) {
1988                 pr_err("ALSA: unable to register timer device (%i)\n", err);
1989                 snd_timer_free_all();
1990                 put_device(&timer_dev);
1991                 return err;
1992         }
1993
1994         snd_timer_proc_init();
1995         return 0;
1996 }
1997
1998 static void __exit alsa_timer_exit(void)
1999 {
2000         snd_unregister_device(&timer_dev);
2001         snd_timer_free_all();
2002         put_device(&timer_dev);
2003         snd_timer_proc_done();
2004 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2005         snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2006 #endif
2007 }
2008
2009 module_init(alsa_timer_init)
2010 module_exit(alsa_timer_exit)
2011
2012 EXPORT_SYMBOL(snd_timer_open);
2013 EXPORT_SYMBOL(snd_timer_close);
2014 EXPORT_SYMBOL(snd_timer_resolution);
2015 EXPORT_SYMBOL(snd_timer_start);
2016 EXPORT_SYMBOL(snd_timer_stop);
2017 EXPORT_SYMBOL(snd_timer_continue);
2018 EXPORT_SYMBOL(snd_timer_pause);
2019 EXPORT_SYMBOL(snd_timer_new);
2020 EXPORT_SYMBOL(snd_timer_notify);
2021 EXPORT_SYMBOL(snd_timer_global_new);
2022 EXPORT_SYMBOL(snd_timer_global_free);
2023 EXPORT_SYMBOL(snd_timer_global_register);
2024 EXPORT_SYMBOL(snd_timer_interrupt);