9091030f4a89e74178fdc43482030eb32eee8e26
[linux-2.6-block.git] / sound / core / timer.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Timers abstract layer
4  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5  */
6
7 #include <linux/delay.h>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/time.h>
11 #include <linux/mutex.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/string.h>
15 #include <linux/sched/signal.h>
16 #include <sound/core.h>
17 #include <sound/timer.h>
18 #include <sound/control.h>
19 #include <sound/info.h>
20 #include <sound/minors.h>
21 #include <sound/initval.h>
22 #include <linux/kmod.h>
23
24 /* internal flags */
25 #define SNDRV_TIMER_IFLG_PAUSED         0x00010000
26 #define SNDRV_TIMER_IFLG_DEAD           0x00020000
27
28 #if IS_ENABLED(CONFIG_SND_HRTIMER)
29 #define DEFAULT_TIMER_LIMIT 4
30 #else
31 #define DEFAULT_TIMER_LIMIT 1
32 #endif
33
34 static int timer_limit = DEFAULT_TIMER_LIMIT;
35 static int timer_tstamp_monotonic = 1;
36 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
37 MODULE_DESCRIPTION("ALSA timer interface");
38 MODULE_LICENSE("GPL");
39 module_param(timer_limit, int, 0444);
40 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
41 module_param(timer_tstamp_monotonic, int, 0444);
42 MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
43
44 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
45 MODULE_ALIAS("devname:snd/timer");
46
47 struct snd_timer_user {
48         struct snd_timer_instance *timeri;
49         int tread;              /* enhanced read with timestamps and events */
50         unsigned long ticks;
51         unsigned long overrun;
52         int qhead;
53         int qtail;
54         int qused;
55         int queue_size;
56         bool disconnected;
57         struct snd_timer_read *queue;
58         struct snd_timer_tread *tqueue;
59         spinlock_t qlock;
60         unsigned long last_resolution;
61         unsigned int filter;
62         struct timespec tstamp;         /* trigger tstamp */
63         wait_queue_head_t qchange_sleep;
64         struct fasync_struct *fasync;
65         struct mutex ioctl_lock;
66 };
67
68 /* list of timers */
69 static LIST_HEAD(snd_timer_list);
70
71 /* list of slave instances */
72 static LIST_HEAD(snd_timer_slave_list);
73
74 /* lock for slave active lists */
75 static DEFINE_SPINLOCK(slave_active_lock);
76
77 #define MAX_SLAVE_INSTANCES     1000
78 static int num_slaves;
79
80 static DEFINE_MUTEX(register_mutex);
81
82 static int snd_timer_free(struct snd_timer *timer);
83 static int snd_timer_dev_free(struct snd_device *device);
84 static int snd_timer_dev_register(struct snd_device *device);
85 static int snd_timer_dev_disconnect(struct snd_device *device);
86
87 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
88
89 /*
90  * create a timer instance with the given owner string.
91  */
92 struct snd_timer_instance *snd_timer_instance_new(const char *owner)
93 {
94         struct snd_timer_instance *timeri;
95
96         timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
97         if (timeri == NULL)
98                 return NULL;
99         timeri->owner = kstrdup(owner, GFP_KERNEL);
100         if (! timeri->owner) {
101                 kfree(timeri);
102                 return NULL;
103         }
104         INIT_LIST_HEAD(&timeri->open_list);
105         INIT_LIST_HEAD(&timeri->active_list);
106         INIT_LIST_HEAD(&timeri->ack_list);
107         INIT_LIST_HEAD(&timeri->slave_list_head);
108         INIT_LIST_HEAD(&timeri->slave_active_head);
109
110         return timeri;
111 }
112 EXPORT_SYMBOL(snd_timer_instance_new);
113
114 void snd_timer_instance_free(struct snd_timer_instance *timeri)
115 {
116         if (timeri) {
117                 if (timeri->private_free)
118                         timeri->private_free(timeri);
119                 kfree(timeri->owner);
120                 kfree(timeri);
121         }
122 }
123 EXPORT_SYMBOL(snd_timer_instance_free);
124
125 /*
126  * find a timer instance from the given timer id
127  */
128 static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
129 {
130         struct snd_timer *timer = NULL;
131
132         list_for_each_entry(timer, &snd_timer_list, device_list) {
133                 if (timer->tmr_class != tid->dev_class)
134                         continue;
135                 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
136                      timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
137                     (timer->card == NULL ||
138                      timer->card->number != tid->card))
139                         continue;
140                 if (timer->tmr_device != tid->device)
141                         continue;
142                 if (timer->tmr_subdevice != tid->subdevice)
143                         continue;
144                 return timer;
145         }
146         return NULL;
147 }
148
149 #ifdef CONFIG_MODULES
150
151 static void snd_timer_request(struct snd_timer_id *tid)
152 {
153         switch (tid->dev_class) {
154         case SNDRV_TIMER_CLASS_GLOBAL:
155                 if (tid->device < timer_limit)
156                         request_module("snd-timer-%i", tid->device);
157                 break;
158         case SNDRV_TIMER_CLASS_CARD:
159         case SNDRV_TIMER_CLASS_PCM:
160                 if (tid->card < snd_ecards_limit)
161                         request_module("snd-card-%i", tid->card);
162                 break;
163         default:
164                 break;
165         }
166 }
167
168 #endif
169
170 /* move the slave if it belongs to the master; return 1 if match */
171 static int check_matching_master_slave(struct snd_timer_instance *master,
172                                        struct snd_timer_instance *slave)
173 {
174         if (slave->slave_class != master->slave_class ||
175             slave->slave_id != master->slave_id)
176                 return 0;
177         if (master->timer->num_instances >= master->timer->max_instances)
178                 return -EBUSY;
179         list_move_tail(&slave->open_list, &master->slave_list_head);
180         master->timer->num_instances++;
181         spin_lock_irq(&slave_active_lock);
182         spin_lock(&master->timer->lock);
183         slave->master = master;
184         slave->timer = master->timer;
185         if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
186                 list_add_tail(&slave->active_list, &master->slave_active_head);
187         spin_unlock(&master->timer->lock);
188         spin_unlock_irq(&slave_active_lock);
189         return 1;
190 }
191
192 /*
193  * look for a master instance matching with the slave id of the given slave.
194  * when found, relink the open_link of the slave.
195  *
196  * call this with register_mutex down.
197  */
198 static int snd_timer_check_slave(struct snd_timer_instance *slave)
199 {
200         struct snd_timer *timer;
201         struct snd_timer_instance *master;
202         int err = 0;
203
204         /* FIXME: it's really dumb to look up all entries.. */
205         list_for_each_entry(timer, &snd_timer_list, device_list) {
206                 list_for_each_entry(master, &timer->open_list_head, open_list) {
207                         err = check_matching_master_slave(master, slave);
208                         if (err != 0) /* match found or error */
209                                 goto out;
210                 }
211         }
212  out:
213         return err < 0 ? err : 0;
214 }
215
216 /*
217  * look for slave instances matching with the slave id of the given master.
218  * when found, relink the open_link of slaves.
219  *
220  * call this with register_mutex down.
221  */
222 static int snd_timer_check_master(struct snd_timer_instance *master)
223 {
224         struct snd_timer_instance *slave, *tmp;
225         int err = 0;
226
227         /* check all pending slaves */
228         list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
229                 err = check_matching_master_slave(master, slave);
230                 if (err < 0)
231                         break;
232         }
233         return err < 0 ? err : 0;
234 }
235
236 static void snd_timer_close_locked(struct snd_timer_instance *timeri,
237                                    struct device **card_devp_to_put);
238
239 /*
240  * open a timer instance
241  * when opening a master, the slave id must be here given.
242  */
243 int snd_timer_open(struct snd_timer_instance *timeri,
244                    struct snd_timer_id *tid,
245                    unsigned int slave_id)
246 {
247         struct snd_timer *timer;
248         struct device *card_dev_to_put = NULL;
249         int err;
250
251         mutex_lock(&register_mutex);
252         if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
253                 /* open a slave instance */
254                 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
255                     tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
256                         pr_debug("ALSA: timer: invalid slave class %i\n",
257                                  tid->dev_sclass);
258                         err = -EINVAL;
259                         goto unlock;
260                 }
261                 if (num_slaves >= MAX_SLAVE_INSTANCES) {
262                         err = -EBUSY;
263                         goto unlock;
264                 }
265                 timeri->slave_class = tid->dev_sclass;
266                 timeri->slave_id = tid->device;
267                 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
268                 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
269                 num_slaves++;
270                 err = snd_timer_check_slave(timeri);
271                 if (err < 0)
272                         goto close;
273         }
274
275         /* open a master instance */
276         timer = snd_timer_find(tid);
277 #ifdef CONFIG_MODULES
278         if (!timer) {
279                 mutex_unlock(&register_mutex);
280                 snd_timer_request(tid);
281                 mutex_lock(&register_mutex);
282                 timer = snd_timer_find(tid);
283         }
284 #endif
285         if (!timer) {
286                 err = -ENODEV;
287                 goto unlock;
288         }
289         if (!list_empty(&timer->open_list_head)) {
290                 struct snd_timer_instance *t =
291                         list_entry(timer->open_list_head.next,
292                                     struct snd_timer_instance, open_list);
293                 if (t->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
294                         err = -EBUSY;
295                         goto unlock;
296                 }
297         }
298         if (timer->num_instances >= timer->max_instances) {
299                 err = -EBUSY;
300                 goto unlock;
301         }
302         if (!try_module_get(timer->module)) {
303                 err = -EBUSY;
304                 goto unlock;
305         }
306         /* take a card refcount for safe disconnection */
307         if (timer->card) {
308                 get_device(&timer->card->card_dev);
309                 card_dev_to_put = &timer->card->card_dev;
310         }
311
312         if (list_empty(&timer->open_list_head) && timer->hw.open) {
313                 err = timer->hw.open(timer);
314                 if (err) {
315                         module_put(timer->module);
316                         goto unlock;
317                 }
318         }
319
320         timeri->timer = timer;
321         timeri->slave_class = tid->dev_sclass;
322         timeri->slave_id = slave_id;
323
324         list_add_tail(&timeri->open_list, &timer->open_list_head);
325         timer->num_instances++;
326         err = snd_timer_check_master(timeri);
327  close:
328         if (err < 0)
329                 snd_timer_close_locked(timeri, &card_dev_to_put);
330
331  unlock:
332         mutex_unlock(&register_mutex);
333         /* put_device() is called after unlock for avoiding deadlock */
334         if (err < 0 && card_dev_to_put)
335                 put_device(card_dev_to_put);
336         return err;
337 }
338 EXPORT_SYMBOL(snd_timer_open);
339
340 /*
341  * close a timer instance
342  * call this with register_mutex down.
343  */
344 static void snd_timer_close_locked(struct snd_timer_instance *timeri,
345                                    struct device **card_devp_to_put)
346 {
347         struct snd_timer *timer = timeri->timer;
348         struct snd_timer_instance *slave, *tmp;
349
350         if (timer) {
351                 spin_lock_irq(&timer->lock);
352                 timeri->flags |= SNDRV_TIMER_IFLG_DEAD;
353                 spin_unlock_irq(&timer->lock);
354         }
355
356         if (!list_empty(&timeri->open_list)) {
357                 list_del_init(&timeri->open_list);
358                 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
359                         num_slaves--;
360         }
361
362         /* force to stop the timer */
363         snd_timer_stop(timeri);
364
365         if (timer) {
366                 timer->num_instances--;
367                 /* wait, until the active callback is finished */
368                 spin_lock_irq(&timer->lock);
369                 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
370                         spin_unlock_irq(&timer->lock);
371                         udelay(10);
372                         spin_lock_irq(&timer->lock);
373                 }
374                 spin_unlock_irq(&timer->lock);
375
376                 /* remove slave links */
377                 spin_lock_irq(&slave_active_lock);
378                 spin_lock(&timer->lock);
379                 timeri->timer = NULL;
380                 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
381                                          open_list) {
382                         list_move_tail(&slave->open_list, &snd_timer_slave_list);
383                         timer->num_instances--;
384                         slave->master = NULL;
385                         slave->timer = NULL;
386                         list_del_init(&slave->ack_list);
387                         list_del_init(&slave->active_list);
388                 }
389                 spin_unlock(&timer->lock);
390                 spin_unlock_irq(&slave_active_lock);
391
392                 /* slave doesn't need to release timer resources below */
393                 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
394                         timer = NULL;
395         }
396
397         if (timer) {
398                 if (list_empty(&timer->open_list_head) && timer->hw.close)
399                         timer->hw.close(timer);
400                 /* release a card refcount for safe disconnection */
401                 if (timer->card)
402                         *card_devp_to_put = &timer->card->card_dev;
403                 module_put(timer->module);
404         }
405 }
406
407 /*
408  * close a timer instance
409  */
410 void snd_timer_close(struct snd_timer_instance *timeri)
411 {
412         struct device *card_dev_to_put = NULL;
413
414         if (snd_BUG_ON(!timeri))
415                 return;
416
417         mutex_lock(&register_mutex);
418         snd_timer_close_locked(timeri, &card_dev_to_put);
419         mutex_unlock(&register_mutex);
420         /* put_device() is called after unlock for avoiding deadlock */
421         if (card_dev_to_put)
422                 put_device(card_dev_to_put);
423 }
424 EXPORT_SYMBOL(snd_timer_close);
425
426 static unsigned long snd_timer_hw_resolution(struct snd_timer *timer)
427 {
428         if (timer->hw.c_resolution)
429                 return timer->hw.c_resolution(timer);
430         else
431                 return timer->hw.resolution;
432 }
433
434 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
435 {
436         struct snd_timer * timer;
437         unsigned long ret = 0;
438         unsigned long flags;
439
440         if (timeri == NULL)
441                 return 0;
442         timer = timeri->timer;
443         if (timer) {
444                 spin_lock_irqsave(&timer->lock, flags);
445                 ret = snd_timer_hw_resolution(timer);
446                 spin_unlock_irqrestore(&timer->lock, flags);
447         }
448         return ret;
449 }
450 EXPORT_SYMBOL(snd_timer_resolution);
451
452 static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
453 {
454         struct snd_timer *timer = ti->timer;
455         unsigned long resolution = 0;
456         struct snd_timer_instance *ts;
457         struct timespec tstamp;
458
459         if (timer_tstamp_monotonic)
460                 ktime_get_ts(&tstamp);
461         else
462                 getnstimeofday(&tstamp);
463         if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
464                        event > SNDRV_TIMER_EVENT_PAUSE))
465                 return;
466         if (timer &&
467             (event == SNDRV_TIMER_EVENT_START ||
468              event == SNDRV_TIMER_EVENT_CONTINUE))
469                 resolution = snd_timer_hw_resolution(timer);
470         if (ti->ccallback)
471                 ti->ccallback(ti, event, &tstamp, resolution);
472         if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
473                 return;
474         if (timer == NULL)
475                 return;
476         if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
477                 return;
478         list_for_each_entry(ts, &ti->slave_active_head, active_list)
479                 if (ts->ccallback)
480                         ts->ccallback(ts, event + 100, &tstamp, resolution);
481 }
482
483 /* start/continue a master timer */
484 static int snd_timer_start1(struct snd_timer_instance *timeri,
485                             bool start, unsigned long ticks)
486 {
487         struct snd_timer *timer;
488         int result;
489         unsigned long flags;
490
491         timer = timeri->timer;
492         if (!timer)
493                 return -EINVAL;
494
495         spin_lock_irqsave(&timer->lock, flags);
496         if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) {
497                 result = -EINVAL;
498                 goto unlock;
499         }
500         if (timer->card && timer->card->shutdown) {
501                 result = -ENODEV;
502                 goto unlock;
503         }
504         if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
505                              SNDRV_TIMER_IFLG_START)) {
506                 result = -EBUSY;
507                 goto unlock;
508         }
509
510         if (start)
511                 timeri->ticks = timeri->cticks = ticks;
512         else if (!timeri->cticks)
513                 timeri->cticks = 1;
514         timeri->pticks = 0;
515
516         list_move_tail(&timeri->active_list, &timer->active_list_head);
517         if (timer->running) {
518                 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
519                         goto __start_now;
520                 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
521                 timeri->flags |= SNDRV_TIMER_IFLG_START;
522                 result = 1; /* delayed start */
523         } else {
524                 if (start)
525                         timer->sticks = ticks;
526                 timer->hw.start(timer);
527               __start_now:
528                 timer->running++;
529                 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
530                 result = 0;
531         }
532         snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
533                           SNDRV_TIMER_EVENT_CONTINUE);
534  unlock:
535         spin_unlock_irqrestore(&timer->lock, flags);
536         return result;
537 }
538
539 /* start/continue a slave timer */
540 static int snd_timer_start_slave(struct snd_timer_instance *timeri,
541                                  bool start)
542 {
543         unsigned long flags;
544         int err;
545
546         spin_lock_irqsave(&slave_active_lock, flags);
547         if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) {
548                 err = -EINVAL;
549                 goto unlock;
550         }
551         if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
552                 err = -EBUSY;
553                 goto unlock;
554         }
555         timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
556         if (timeri->master && timeri->timer) {
557                 spin_lock(&timeri->timer->lock);
558                 list_add_tail(&timeri->active_list,
559                               &timeri->master->slave_active_head);
560                 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
561                                   SNDRV_TIMER_EVENT_CONTINUE);
562                 spin_unlock(&timeri->timer->lock);
563         }
564         err = 1; /* delayed start */
565  unlock:
566         spin_unlock_irqrestore(&slave_active_lock, flags);
567         return err;
568 }
569
570 /* stop/pause a master timer */
571 static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
572 {
573         struct snd_timer *timer;
574         int result = 0;
575         unsigned long flags;
576
577         timer = timeri->timer;
578         if (!timer)
579                 return -EINVAL;
580         spin_lock_irqsave(&timer->lock, flags);
581         if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
582                                SNDRV_TIMER_IFLG_START))) {
583                 result = -EBUSY;
584                 goto unlock;
585         }
586         list_del_init(&timeri->ack_list);
587         list_del_init(&timeri->active_list);
588         if (timer->card && timer->card->shutdown)
589                 goto unlock;
590         if (stop) {
591                 timeri->cticks = timeri->ticks;
592                 timeri->pticks = 0;
593         }
594         if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
595             !(--timer->running)) {
596                 timer->hw.stop(timer);
597                 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
598                         timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
599                         snd_timer_reschedule(timer, 0);
600                         if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
601                                 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
602                                 timer->hw.start(timer);
603                         }
604                 }
605         }
606         timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
607         if (stop)
608                 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
609         else
610                 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
611         snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
612                           SNDRV_TIMER_EVENT_PAUSE);
613  unlock:
614         spin_unlock_irqrestore(&timer->lock, flags);
615         return result;
616 }
617
618 /* stop/pause a slave timer */
619 static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
620 {
621         unsigned long flags;
622
623         spin_lock_irqsave(&slave_active_lock, flags);
624         if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
625                 spin_unlock_irqrestore(&slave_active_lock, flags);
626                 return -EBUSY;
627         }
628         timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
629         if (timeri->timer) {
630                 spin_lock(&timeri->timer->lock);
631                 list_del_init(&timeri->ack_list);
632                 list_del_init(&timeri->active_list);
633                 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
634                                   SNDRV_TIMER_EVENT_PAUSE);
635                 spin_unlock(&timeri->timer->lock);
636         }
637         spin_unlock_irqrestore(&slave_active_lock, flags);
638         return 0;
639 }
640
641 /*
642  *  start the timer instance
643  */
644 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
645 {
646         if (timeri == NULL || ticks < 1)
647                 return -EINVAL;
648         if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
649                 return snd_timer_start_slave(timeri, true);
650         else
651                 return snd_timer_start1(timeri, true, ticks);
652 }
653 EXPORT_SYMBOL(snd_timer_start);
654
655 /*
656  * stop the timer instance.
657  *
658  * do not call this from the timer callback!
659  */
660 int snd_timer_stop(struct snd_timer_instance *timeri)
661 {
662         if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
663                 return snd_timer_stop_slave(timeri, true);
664         else
665                 return snd_timer_stop1(timeri, true);
666 }
667 EXPORT_SYMBOL(snd_timer_stop);
668
669 /*
670  * start again..  the tick is kept.
671  */
672 int snd_timer_continue(struct snd_timer_instance *timeri)
673 {
674         /* timer can continue only after pause */
675         if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
676                 return -EINVAL;
677
678         if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
679                 return snd_timer_start_slave(timeri, false);
680         else
681                 return snd_timer_start1(timeri, false, 0);
682 }
683 EXPORT_SYMBOL(snd_timer_continue);
684
685 /*
686  * pause.. remember the ticks left
687  */
688 int snd_timer_pause(struct snd_timer_instance * timeri)
689 {
690         if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
691                 return snd_timer_stop_slave(timeri, false);
692         else
693                 return snd_timer_stop1(timeri, false);
694 }
695 EXPORT_SYMBOL(snd_timer_pause);
696
697 /*
698  * reschedule the timer
699  *
700  * start pending instances and check the scheduling ticks.
701  * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
702  */
703 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
704 {
705         struct snd_timer_instance *ti;
706         unsigned long ticks = ~0UL;
707
708         list_for_each_entry(ti, &timer->active_list_head, active_list) {
709                 if (ti->flags & SNDRV_TIMER_IFLG_START) {
710                         ti->flags &= ~SNDRV_TIMER_IFLG_START;
711                         ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
712                         timer->running++;
713                 }
714                 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
715                         if (ticks > ti->cticks)
716                                 ticks = ti->cticks;
717                 }
718         }
719         if (ticks == ~0UL) {
720                 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
721                 return;
722         }
723         if (ticks > timer->hw.ticks)
724                 ticks = timer->hw.ticks;
725         if (ticks_left != ticks)
726                 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
727         timer->sticks = ticks;
728 }
729
730 /* call callbacks in timer ack list */
731 static void snd_timer_process_callbacks(struct snd_timer *timer,
732                                         struct list_head *head)
733 {
734         struct snd_timer_instance *ti;
735         unsigned long resolution, ticks;
736
737         while (!list_empty(head)) {
738                 ti = list_first_entry(head, struct snd_timer_instance,
739                                       ack_list);
740
741                 /* remove from ack_list and make empty */
742                 list_del_init(&ti->ack_list);
743
744                 if (!(ti->flags & SNDRV_TIMER_IFLG_DEAD)) {
745                         ticks = ti->pticks;
746                         ti->pticks = 0;
747                         resolution = ti->resolution;
748                         ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
749                         spin_unlock(&timer->lock);
750                         if (ti->callback)
751                                 ti->callback(ti, resolution, ticks);
752                         spin_lock(&timer->lock);
753                         ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
754                 }
755         }
756 }
757
758 /* clear pending instances from ack list */
759 static void snd_timer_clear_callbacks(struct snd_timer *timer,
760                                       struct list_head *head)
761 {
762         unsigned long flags;
763
764         spin_lock_irqsave(&timer->lock, flags);
765         while (!list_empty(head))
766                 list_del_init(head->next);
767         spin_unlock_irqrestore(&timer->lock, flags);
768 }
769
770 /*
771  * timer tasklet
772  *
773  */
774 static void snd_timer_tasklet(unsigned long arg)
775 {
776         struct snd_timer *timer = (struct snd_timer *) arg;
777         unsigned long flags;
778
779         if (timer->card && timer->card->shutdown) {
780                 snd_timer_clear_callbacks(timer, &timer->sack_list_head);
781                 return;
782         }
783
784         spin_lock_irqsave(&timer->lock, flags);
785         snd_timer_process_callbacks(timer, &timer->sack_list_head);
786         spin_unlock_irqrestore(&timer->lock, flags);
787 }
788
789 /*
790  * timer interrupt
791  *
792  * ticks_left is usually equal to timer->sticks.
793  *
794  */
795 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
796 {
797         struct snd_timer_instance *ti, *ts, *tmp;
798         unsigned long resolution;
799         struct list_head *ack_list_head;
800         unsigned long flags;
801         int use_tasklet = 0;
802
803         if (timer == NULL)
804                 return;
805
806         if (timer->card && timer->card->shutdown) {
807                 snd_timer_clear_callbacks(timer, &timer->ack_list_head);
808                 return;
809         }
810
811         spin_lock_irqsave(&timer->lock, flags);
812
813         /* remember the current resolution */
814         resolution = snd_timer_hw_resolution(timer);
815
816         /* loop for all active instances
817          * Here we cannot use list_for_each_entry because the active_list of a
818          * processed instance is relinked to done_list_head before the callback
819          * is called.
820          */
821         list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
822                                  active_list) {
823                 if (ti->flags & SNDRV_TIMER_IFLG_DEAD)
824                         continue;
825                 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
826                         continue;
827                 ti->pticks += ticks_left;
828                 ti->resolution = resolution;
829                 if (ti->cticks < ticks_left)
830                         ti->cticks = 0;
831                 else
832                         ti->cticks -= ticks_left;
833                 if (ti->cticks) /* not expired */
834                         continue;
835                 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
836                         ti->cticks = ti->ticks;
837                 } else {
838                         ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
839                         --timer->running;
840                         list_del_init(&ti->active_list);
841                 }
842                 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
843                     (ti->flags & SNDRV_TIMER_IFLG_FAST))
844                         ack_list_head = &timer->ack_list_head;
845                 else
846                         ack_list_head = &timer->sack_list_head;
847                 if (list_empty(&ti->ack_list))
848                         list_add_tail(&ti->ack_list, ack_list_head);
849                 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
850                         ts->pticks = ti->pticks;
851                         ts->resolution = resolution;
852                         if (list_empty(&ts->ack_list))
853                                 list_add_tail(&ts->ack_list, ack_list_head);
854                 }
855         }
856         if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
857                 snd_timer_reschedule(timer, timer->sticks);
858         if (timer->running) {
859                 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
860                         timer->hw.stop(timer);
861                         timer->flags |= SNDRV_TIMER_FLG_CHANGE;
862                 }
863                 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
864                     (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
865                         /* restart timer */
866                         timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
867                         timer->hw.start(timer);
868                 }
869         } else {
870                 timer->hw.stop(timer);
871         }
872
873         /* now process all fast callbacks */
874         snd_timer_process_callbacks(timer, &timer->ack_list_head);
875
876         /* do we have any slow callbacks? */
877         use_tasklet = !list_empty(&timer->sack_list_head);
878         spin_unlock_irqrestore(&timer->lock, flags);
879
880         if (use_tasklet)
881                 tasklet_schedule(&timer->task_queue);
882 }
883 EXPORT_SYMBOL(snd_timer_interrupt);
884
885 /*
886
887  */
888
889 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
890                   struct snd_timer **rtimer)
891 {
892         struct snd_timer *timer;
893         int err;
894         static struct snd_device_ops ops = {
895                 .dev_free = snd_timer_dev_free,
896                 .dev_register = snd_timer_dev_register,
897                 .dev_disconnect = snd_timer_dev_disconnect,
898         };
899
900         if (snd_BUG_ON(!tid))
901                 return -EINVAL;
902         if (tid->dev_class == SNDRV_TIMER_CLASS_CARD ||
903             tid->dev_class == SNDRV_TIMER_CLASS_PCM) {
904                 if (WARN_ON(!card))
905                         return -EINVAL;
906         }
907         if (rtimer)
908                 *rtimer = NULL;
909         timer = kzalloc(sizeof(*timer), GFP_KERNEL);
910         if (!timer)
911                 return -ENOMEM;
912         timer->tmr_class = tid->dev_class;
913         timer->card = card;
914         timer->tmr_device = tid->device;
915         timer->tmr_subdevice = tid->subdevice;
916         if (id)
917                 strlcpy(timer->id, id, sizeof(timer->id));
918         timer->sticks = 1;
919         INIT_LIST_HEAD(&timer->device_list);
920         INIT_LIST_HEAD(&timer->open_list_head);
921         INIT_LIST_HEAD(&timer->active_list_head);
922         INIT_LIST_HEAD(&timer->ack_list_head);
923         INIT_LIST_HEAD(&timer->sack_list_head);
924         spin_lock_init(&timer->lock);
925         tasklet_init(&timer->task_queue, snd_timer_tasklet,
926                      (unsigned long)timer);
927         timer->max_instances = 1000; /* default limit per timer */
928         if (card != NULL) {
929                 timer->module = card->module;
930                 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
931                 if (err < 0) {
932                         snd_timer_free(timer);
933                         return err;
934                 }
935         }
936         if (rtimer)
937                 *rtimer = timer;
938         return 0;
939 }
940 EXPORT_SYMBOL(snd_timer_new);
941
942 static int snd_timer_free(struct snd_timer *timer)
943 {
944         if (!timer)
945                 return 0;
946
947         mutex_lock(&register_mutex);
948         if (! list_empty(&timer->open_list_head)) {
949                 struct list_head *p, *n;
950                 struct snd_timer_instance *ti;
951                 pr_warn("ALSA: timer %p is busy?\n", timer);
952                 list_for_each_safe(p, n, &timer->open_list_head) {
953                         list_del_init(p);
954                         ti = list_entry(p, struct snd_timer_instance, open_list);
955                         ti->timer = NULL;
956                 }
957         }
958         list_del(&timer->device_list);
959         mutex_unlock(&register_mutex);
960
961         if (timer->private_free)
962                 timer->private_free(timer);
963         kfree(timer);
964         return 0;
965 }
966
967 static int snd_timer_dev_free(struct snd_device *device)
968 {
969         struct snd_timer *timer = device->device_data;
970         return snd_timer_free(timer);
971 }
972
973 static int snd_timer_dev_register(struct snd_device *dev)
974 {
975         struct snd_timer *timer = dev->device_data;
976         struct snd_timer *timer1;
977
978         if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
979                 return -ENXIO;
980         if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
981             !timer->hw.resolution && timer->hw.c_resolution == NULL)
982                 return -EINVAL;
983
984         mutex_lock(&register_mutex);
985         list_for_each_entry(timer1, &snd_timer_list, device_list) {
986                 if (timer1->tmr_class > timer->tmr_class)
987                         break;
988                 if (timer1->tmr_class < timer->tmr_class)
989                         continue;
990                 if (timer1->card && timer->card) {
991                         if (timer1->card->number > timer->card->number)
992                                 break;
993                         if (timer1->card->number < timer->card->number)
994                                 continue;
995                 }
996                 if (timer1->tmr_device > timer->tmr_device)
997                         break;
998                 if (timer1->tmr_device < timer->tmr_device)
999                         continue;
1000                 if (timer1->tmr_subdevice > timer->tmr_subdevice)
1001                         break;
1002                 if (timer1->tmr_subdevice < timer->tmr_subdevice)
1003                         continue;
1004                 /* conflicts.. */
1005                 mutex_unlock(&register_mutex);
1006                 return -EBUSY;
1007         }
1008         list_add_tail(&timer->device_list, &timer1->device_list);
1009         mutex_unlock(&register_mutex);
1010         return 0;
1011 }
1012
1013 static int snd_timer_dev_disconnect(struct snd_device *device)
1014 {
1015         struct snd_timer *timer = device->device_data;
1016         struct snd_timer_instance *ti;
1017
1018         mutex_lock(&register_mutex);
1019         list_del_init(&timer->device_list);
1020         /* wake up pending sleepers */
1021         list_for_each_entry(ti, &timer->open_list_head, open_list) {
1022                 if (ti->disconnect)
1023                         ti->disconnect(ti);
1024         }
1025         mutex_unlock(&register_mutex);
1026         return 0;
1027 }
1028
1029 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
1030 {
1031         unsigned long flags;
1032         unsigned long resolution = 0;
1033         struct snd_timer_instance *ti, *ts;
1034
1035         if (timer->card && timer->card->shutdown)
1036                 return;
1037         if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
1038                 return;
1039         if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
1040                        event > SNDRV_TIMER_EVENT_MRESUME))
1041                 return;
1042         spin_lock_irqsave(&timer->lock, flags);
1043         if (event == SNDRV_TIMER_EVENT_MSTART ||
1044             event == SNDRV_TIMER_EVENT_MCONTINUE ||
1045             event == SNDRV_TIMER_EVENT_MRESUME)
1046                 resolution = snd_timer_hw_resolution(timer);
1047         list_for_each_entry(ti, &timer->active_list_head, active_list) {
1048                 if (ti->ccallback)
1049                         ti->ccallback(ti, event, tstamp, resolution);
1050                 list_for_each_entry(ts, &ti->slave_active_head, active_list)
1051                         if (ts->ccallback)
1052                                 ts->ccallback(ts, event, tstamp, resolution);
1053         }
1054         spin_unlock_irqrestore(&timer->lock, flags);
1055 }
1056 EXPORT_SYMBOL(snd_timer_notify);
1057
1058 /*
1059  * exported functions for global timers
1060  */
1061 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
1062 {
1063         struct snd_timer_id tid;
1064
1065         tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1066         tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1067         tid.card = -1;
1068         tid.device = device;
1069         tid.subdevice = 0;
1070         return snd_timer_new(NULL, id, &tid, rtimer);
1071 }
1072 EXPORT_SYMBOL(snd_timer_global_new);
1073
1074 int snd_timer_global_free(struct snd_timer *timer)
1075 {
1076         return snd_timer_free(timer);
1077 }
1078 EXPORT_SYMBOL(snd_timer_global_free);
1079
1080 int snd_timer_global_register(struct snd_timer *timer)
1081 {
1082         struct snd_device dev;
1083
1084         memset(&dev, 0, sizeof(dev));
1085         dev.device_data = timer;
1086         return snd_timer_dev_register(&dev);
1087 }
1088 EXPORT_SYMBOL(snd_timer_global_register);
1089
1090 /*
1091  *  System timer
1092  */
1093
1094 struct snd_timer_system_private {
1095         struct timer_list tlist;
1096         struct snd_timer *snd_timer;
1097         unsigned long last_expires;
1098         unsigned long last_jiffies;
1099         unsigned long correction;
1100 };
1101
1102 static void snd_timer_s_function(struct timer_list *t)
1103 {
1104         struct snd_timer_system_private *priv = from_timer(priv, t,
1105                                                                 tlist);
1106         struct snd_timer *timer = priv->snd_timer;
1107         unsigned long jiff = jiffies;
1108         if (time_after(jiff, priv->last_expires))
1109                 priv->correction += (long)jiff - (long)priv->last_expires;
1110         snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1111 }
1112
1113 static int snd_timer_s_start(struct snd_timer * timer)
1114 {
1115         struct snd_timer_system_private *priv;
1116         unsigned long njiff;
1117
1118         priv = (struct snd_timer_system_private *) timer->private_data;
1119         njiff = (priv->last_jiffies = jiffies);
1120         if (priv->correction > timer->sticks - 1) {
1121                 priv->correction -= timer->sticks - 1;
1122                 njiff++;
1123         } else {
1124                 njiff += timer->sticks - priv->correction;
1125                 priv->correction = 0;
1126         }
1127         priv->last_expires = njiff;
1128         mod_timer(&priv->tlist, njiff);
1129         return 0;
1130 }
1131
1132 static int snd_timer_s_stop(struct snd_timer * timer)
1133 {
1134         struct snd_timer_system_private *priv;
1135         unsigned long jiff;
1136
1137         priv = (struct snd_timer_system_private *) timer->private_data;
1138         del_timer(&priv->tlist);
1139         jiff = jiffies;
1140         if (time_before(jiff, priv->last_expires))
1141                 timer->sticks = priv->last_expires - jiff;
1142         else
1143                 timer->sticks = 1;
1144         priv->correction = 0;
1145         return 0;
1146 }
1147
1148 static int snd_timer_s_close(struct snd_timer *timer)
1149 {
1150         struct snd_timer_system_private *priv;
1151
1152         priv = (struct snd_timer_system_private *)timer->private_data;
1153         del_timer_sync(&priv->tlist);
1154         return 0;
1155 }
1156
1157 static struct snd_timer_hardware snd_timer_system =
1158 {
1159         .flags =        SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1160         .resolution =   1000000000L / HZ,
1161         .ticks =        10000000L,
1162         .close =        snd_timer_s_close,
1163         .start =        snd_timer_s_start,
1164         .stop =         snd_timer_s_stop
1165 };
1166
1167 static void snd_timer_free_system(struct snd_timer *timer)
1168 {
1169         kfree(timer->private_data);
1170 }
1171
1172 static int snd_timer_register_system(void)
1173 {
1174         struct snd_timer *timer;
1175         struct snd_timer_system_private *priv;
1176         int err;
1177
1178         err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1179         if (err < 0)
1180                 return err;
1181         strcpy(timer->name, "system timer");
1182         timer->hw = snd_timer_system;
1183         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1184         if (priv == NULL) {
1185                 snd_timer_free(timer);
1186                 return -ENOMEM;
1187         }
1188         priv->snd_timer = timer;
1189         timer_setup(&priv->tlist, snd_timer_s_function, 0);
1190         timer->private_data = priv;
1191         timer->private_free = snd_timer_free_system;
1192         return snd_timer_global_register(timer);
1193 }
1194
1195 #ifdef CONFIG_SND_PROC_FS
1196 /*
1197  *  Info interface
1198  */
1199
1200 static void snd_timer_proc_read(struct snd_info_entry *entry,
1201                                 struct snd_info_buffer *buffer)
1202 {
1203         struct snd_timer *timer;
1204         struct snd_timer_instance *ti;
1205
1206         mutex_lock(&register_mutex);
1207         list_for_each_entry(timer, &snd_timer_list, device_list) {
1208                 if (timer->card && timer->card->shutdown)
1209                         continue;
1210                 switch (timer->tmr_class) {
1211                 case SNDRV_TIMER_CLASS_GLOBAL:
1212                         snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1213                         break;
1214                 case SNDRV_TIMER_CLASS_CARD:
1215                         snd_iprintf(buffer, "C%i-%i: ",
1216                                     timer->card->number, timer->tmr_device);
1217                         break;
1218                 case SNDRV_TIMER_CLASS_PCM:
1219                         snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1220                                     timer->tmr_device, timer->tmr_subdevice);
1221                         break;
1222                 default:
1223                         snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1224                                     timer->card ? timer->card->number : -1,
1225                                     timer->tmr_device, timer->tmr_subdevice);
1226                 }
1227                 snd_iprintf(buffer, "%s :", timer->name);
1228                 if (timer->hw.resolution)
1229                         snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1230                                     timer->hw.resolution / 1000,
1231                                     timer->hw.resolution % 1000,
1232                                     timer->hw.ticks);
1233                 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1234                         snd_iprintf(buffer, " SLAVE");
1235                 snd_iprintf(buffer, "\n");
1236                 list_for_each_entry(ti, &timer->open_list_head, open_list)
1237                         snd_iprintf(buffer, "  Client %s : %s\n",
1238                                     ti->owner ? ti->owner : "unknown",
1239                                     ti->flags & (SNDRV_TIMER_IFLG_START |
1240                                                  SNDRV_TIMER_IFLG_RUNNING)
1241                                     ? "running" : "stopped");
1242         }
1243         mutex_unlock(&register_mutex);
1244 }
1245
1246 static struct snd_info_entry *snd_timer_proc_entry;
1247
1248 static void __init snd_timer_proc_init(void)
1249 {
1250         struct snd_info_entry *entry;
1251
1252         entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1253         if (entry != NULL) {
1254                 entry->c.text.read = snd_timer_proc_read;
1255                 if (snd_info_register(entry) < 0) {
1256                         snd_info_free_entry(entry);
1257                         entry = NULL;
1258                 }
1259         }
1260         snd_timer_proc_entry = entry;
1261 }
1262
1263 static void __exit snd_timer_proc_done(void)
1264 {
1265         snd_info_free_entry(snd_timer_proc_entry);
1266 }
1267 #else /* !CONFIG_SND_PROC_FS */
1268 #define snd_timer_proc_init()
1269 #define snd_timer_proc_done()
1270 #endif
1271
1272 /*
1273  *  USER SPACE interface
1274  */
1275
1276 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
1277                                      unsigned long resolution,
1278                                      unsigned long ticks)
1279 {
1280         struct snd_timer_user *tu = timeri->callback_data;
1281         struct snd_timer_read *r;
1282         int prev;
1283
1284         spin_lock(&tu->qlock);
1285         if (tu->qused > 0) {
1286                 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1287                 r = &tu->queue[prev];
1288                 if (r->resolution == resolution) {
1289                         r->ticks += ticks;
1290                         goto __wake;
1291                 }
1292         }
1293         if (tu->qused >= tu->queue_size) {
1294                 tu->overrun++;
1295         } else {
1296                 r = &tu->queue[tu->qtail++];
1297                 tu->qtail %= tu->queue_size;
1298                 r->resolution = resolution;
1299                 r->ticks = ticks;
1300                 tu->qused++;
1301         }
1302       __wake:
1303         spin_unlock(&tu->qlock);
1304         kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1305         wake_up(&tu->qchange_sleep);
1306 }
1307
1308 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1309                                             struct snd_timer_tread *tread)
1310 {
1311         if (tu->qused >= tu->queue_size) {
1312                 tu->overrun++;
1313         } else {
1314                 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1315                 tu->qtail %= tu->queue_size;
1316                 tu->qused++;
1317         }
1318 }
1319
1320 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1321                                      int event,
1322                                      struct timespec *tstamp,
1323                                      unsigned long resolution)
1324 {
1325         struct snd_timer_user *tu = timeri->callback_data;
1326         struct snd_timer_tread r1;
1327         unsigned long flags;
1328
1329         if (event >= SNDRV_TIMER_EVENT_START &&
1330             event <= SNDRV_TIMER_EVENT_PAUSE)
1331                 tu->tstamp = *tstamp;
1332         if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1333                 return;
1334         memset(&r1, 0, sizeof(r1));
1335         r1.event = event;
1336         r1.tstamp = *tstamp;
1337         r1.val = resolution;
1338         spin_lock_irqsave(&tu->qlock, flags);
1339         snd_timer_user_append_to_tqueue(tu, &r1);
1340         spin_unlock_irqrestore(&tu->qlock, flags);
1341         kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1342         wake_up(&tu->qchange_sleep);
1343 }
1344
1345 static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
1346 {
1347         struct snd_timer_user *tu = timeri->callback_data;
1348
1349         tu->disconnected = true;
1350         wake_up(&tu->qchange_sleep);
1351 }
1352
1353 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
1354                                       unsigned long resolution,
1355                                       unsigned long ticks)
1356 {
1357         struct snd_timer_user *tu = timeri->callback_data;
1358         struct snd_timer_tread *r, r1;
1359         struct timespec tstamp;
1360         int prev, append = 0;
1361
1362         memset(&r1, 0, sizeof(r1));
1363         memset(&tstamp, 0, sizeof(tstamp));
1364         spin_lock(&tu->qlock);
1365         if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1366                            (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
1367                 spin_unlock(&tu->qlock);
1368                 return;
1369         }
1370         if (tu->last_resolution != resolution || ticks > 0) {
1371                 if (timer_tstamp_monotonic)
1372                         ktime_get_ts(&tstamp);
1373                 else
1374                         getnstimeofday(&tstamp);
1375         }
1376         if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1377             tu->last_resolution != resolution) {
1378                 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1379                 r1.tstamp = tstamp;
1380                 r1.val = resolution;
1381                 snd_timer_user_append_to_tqueue(tu, &r1);
1382                 tu->last_resolution = resolution;
1383                 append++;
1384         }
1385         if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1386                 goto __wake;
1387         if (ticks == 0)
1388                 goto __wake;
1389         if (tu->qused > 0) {
1390                 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1391                 r = &tu->tqueue[prev];
1392                 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1393                         r->tstamp = tstamp;
1394                         r->val += ticks;
1395                         append++;
1396                         goto __wake;
1397                 }
1398         }
1399         r1.event = SNDRV_TIMER_EVENT_TICK;
1400         r1.tstamp = tstamp;
1401         r1.val = ticks;
1402         snd_timer_user_append_to_tqueue(tu, &r1);
1403         append++;
1404       __wake:
1405         spin_unlock(&tu->qlock);
1406         if (append == 0)
1407                 return;
1408         kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1409         wake_up(&tu->qchange_sleep);
1410 }
1411
1412 static int realloc_user_queue(struct snd_timer_user *tu, int size)
1413 {
1414         struct snd_timer_read *queue = NULL;
1415         struct snd_timer_tread *tqueue = NULL;
1416
1417         if (tu->tread) {
1418                 tqueue = kcalloc(size, sizeof(*tqueue), GFP_KERNEL);
1419                 if (!tqueue)
1420                         return -ENOMEM;
1421         } else {
1422                 queue = kcalloc(size, sizeof(*queue), GFP_KERNEL);
1423                 if (!queue)
1424                         return -ENOMEM;
1425         }
1426
1427         spin_lock_irq(&tu->qlock);
1428         kfree(tu->queue);
1429         kfree(tu->tqueue);
1430         tu->queue_size = size;
1431         tu->queue = queue;
1432         tu->tqueue = tqueue;
1433         tu->qhead = tu->qtail = tu->qused = 0;
1434         spin_unlock_irq(&tu->qlock);
1435
1436         return 0;
1437 }
1438
1439 static int snd_timer_user_open(struct inode *inode, struct file *file)
1440 {
1441         struct snd_timer_user *tu;
1442         int err;
1443
1444         err = stream_open(inode, file);
1445         if (err < 0)
1446                 return err;
1447
1448         tu = kzalloc(sizeof(*tu), GFP_KERNEL);
1449         if (tu == NULL)
1450                 return -ENOMEM;
1451         spin_lock_init(&tu->qlock);
1452         init_waitqueue_head(&tu->qchange_sleep);
1453         mutex_init(&tu->ioctl_lock);
1454         tu->ticks = 1;
1455         if (realloc_user_queue(tu, 128) < 0) {
1456                 kfree(tu);
1457                 return -ENOMEM;
1458         }
1459         file->private_data = tu;
1460         return 0;
1461 }
1462
1463 static int snd_timer_user_release(struct inode *inode, struct file *file)
1464 {
1465         struct snd_timer_user *tu;
1466
1467         if (file->private_data) {
1468                 tu = file->private_data;
1469                 file->private_data = NULL;
1470                 mutex_lock(&tu->ioctl_lock);
1471                 if (tu->timeri) {
1472                         snd_timer_close(tu->timeri);
1473                         snd_timer_instance_free(tu->timeri);
1474                 }
1475                 mutex_unlock(&tu->ioctl_lock);
1476                 kfree(tu->queue);
1477                 kfree(tu->tqueue);
1478                 kfree(tu);
1479         }
1480         return 0;
1481 }
1482
1483 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1484 {
1485         id->dev_class = SNDRV_TIMER_CLASS_NONE;
1486         id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1487         id->card = -1;
1488         id->device = -1;
1489         id->subdevice = -1;
1490 }
1491
1492 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1493 {
1494         id->dev_class = timer->tmr_class;
1495         id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1496         id->card = timer->card ? timer->card->number : -1;
1497         id->device = timer->tmr_device;
1498         id->subdevice = timer->tmr_subdevice;
1499 }
1500
1501 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1502 {
1503         struct snd_timer_id id;
1504         struct snd_timer *timer;
1505         struct list_head *p;
1506
1507         if (copy_from_user(&id, _tid, sizeof(id)))
1508                 return -EFAULT;
1509         mutex_lock(&register_mutex);
1510         if (id.dev_class < 0) {         /* first item */
1511                 if (list_empty(&snd_timer_list))
1512                         snd_timer_user_zero_id(&id);
1513                 else {
1514                         timer = list_entry(snd_timer_list.next,
1515                                            struct snd_timer, device_list);
1516                         snd_timer_user_copy_id(&id, timer);
1517                 }
1518         } else {
1519                 switch (id.dev_class) {
1520                 case SNDRV_TIMER_CLASS_GLOBAL:
1521                         id.device = id.device < 0 ? 0 : id.device + 1;
1522                         list_for_each(p, &snd_timer_list) {
1523                                 timer = list_entry(p, struct snd_timer, device_list);
1524                                 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1525                                         snd_timer_user_copy_id(&id, timer);
1526                                         break;
1527                                 }
1528                                 if (timer->tmr_device >= id.device) {
1529                                         snd_timer_user_copy_id(&id, timer);
1530                                         break;
1531                                 }
1532                         }
1533                         if (p == &snd_timer_list)
1534                                 snd_timer_user_zero_id(&id);
1535                         break;
1536                 case SNDRV_TIMER_CLASS_CARD:
1537                 case SNDRV_TIMER_CLASS_PCM:
1538                         if (id.card < 0) {
1539                                 id.card = 0;
1540                         } else {
1541                                 if (id.device < 0) {
1542                                         id.device = 0;
1543                                 } else {
1544                                         if (id.subdevice < 0)
1545                                                 id.subdevice = 0;
1546                                         else if (id.subdevice < INT_MAX)
1547                                                 id.subdevice++;
1548                                 }
1549                         }
1550                         list_for_each(p, &snd_timer_list) {
1551                                 timer = list_entry(p, struct snd_timer, device_list);
1552                                 if (timer->tmr_class > id.dev_class) {
1553                                         snd_timer_user_copy_id(&id, timer);
1554                                         break;
1555                                 }
1556                                 if (timer->tmr_class < id.dev_class)
1557                                         continue;
1558                                 if (timer->card->number > id.card) {
1559                                         snd_timer_user_copy_id(&id, timer);
1560                                         break;
1561                                 }
1562                                 if (timer->card->number < id.card)
1563                                         continue;
1564                                 if (timer->tmr_device > id.device) {
1565                                         snd_timer_user_copy_id(&id, timer);
1566                                         break;
1567                                 }
1568                                 if (timer->tmr_device < id.device)
1569                                         continue;
1570                                 if (timer->tmr_subdevice > id.subdevice) {
1571                                         snd_timer_user_copy_id(&id, timer);
1572                                         break;
1573                                 }
1574                                 if (timer->tmr_subdevice < id.subdevice)
1575                                         continue;
1576                                 snd_timer_user_copy_id(&id, timer);
1577                                 break;
1578                         }
1579                         if (p == &snd_timer_list)
1580                                 snd_timer_user_zero_id(&id);
1581                         break;
1582                 default:
1583                         snd_timer_user_zero_id(&id);
1584                 }
1585         }
1586         mutex_unlock(&register_mutex);
1587         if (copy_to_user(_tid, &id, sizeof(*_tid)))
1588                 return -EFAULT;
1589         return 0;
1590 }
1591
1592 static int snd_timer_user_ginfo(struct file *file,
1593                                 struct snd_timer_ginfo __user *_ginfo)
1594 {
1595         struct snd_timer_ginfo *ginfo;
1596         struct snd_timer_id tid;
1597         struct snd_timer *t;
1598         struct list_head *p;
1599         int err = 0;
1600
1601         ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1602         if (IS_ERR(ginfo))
1603                 return PTR_ERR(ginfo);
1604
1605         tid = ginfo->tid;
1606         memset(ginfo, 0, sizeof(*ginfo));
1607         ginfo->tid = tid;
1608         mutex_lock(&register_mutex);
1609         t = snd_timer_find(&tid);
1610         if (t != NULL) {
1611                 ginfo->card = t->card ? t->card->number : -1;
1612                 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1613                         ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1614                 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1615                 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1616                 ginfo->resolution = t->hw.resolution;
1617                 if (t->hw.resolution_min > 0) {
1618                         ginfo->resolution_min = t->hw.resolution_min;
1619                         ginfo->resolution_max = t->hw.resolution_max;
1620                 }
1621                 list_for_each(p, &t->open_list_head) {
1622                         ginfo->clients++;
1623                 }
1624         } else {
1625                 err = -ENODEV;
1626         }
1627         mutex_unlock(&register_mutex);
1628         if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1629                 err = -EFAULT;
1630         kfree(ginfo);
1631         return err;
1632 }
1633
1634 static int timer_set_gparams(struct snd_timer_gparams *gparams)
1635 {
1636         struct snd_timer *t;
1637         int err;
1638
1639         mutex_lock(&register_mutex);
1640         t = snd_timer_find(&gparams->tid);
1641         if (!t) {
1642                 err = -ENODEV;
1643                 goto _error;
1644         }
1645         if (!list_empty(&t->open_list_head)) {
1646                 err = -EBUSY;
1647                 goto _error;
1648         }
1649         if (!t->hw.set_period) {
1650                 err = -ENOSYS;
1651                 goto _error;
1652         }
1653         err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
1654 _error:
1655         mutex_unlock(&register_mutex);
1656         return err;
1657 }
1658
1659 static int snd_timer_user_gparams(struct file *file,
1660                                   struct snd_timer_gparams __user *_gparams)
1661 {
1662         struct snd_timer_gparams gparams;
1663
1664         if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1665                 return -EFAULT;
1666         return timer_set_gparams(&gparams);
1667 }
1668
1669 static int snd_timer_user_gstatus(struct file *file,
1670                                   struct snd_timer_gstatus __user *_gstatus)
1671 {
1672         struct snd_timer_gstatus gstatus;
1673         struct snd_timer_id tid;
1674         struct snd_timer *t;
1675         int err = 0;
1676
1677         if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1678                 return -EFAULT;
1679         tid = gstatus.tid;
1680         memset(&gstatus, 0, sizeof(gstatus));
1681         gstatus.tid = tid;
1682         mutex_lock(&register_mutex);
1683         t = snd_timer_find(&tid);
1684         if (t != NULL) {
1685                 spin_lock_irq(&t->lock);
1686                 gstatus.resolution = snd_timer_hw_resolution(t);
1687                 if (t->hw.precise_resolution) {
1688                         t->hw.precise_resolution(t, &gstatus.resolution_num,
1689                                                  &gstatus.resolution_den);
1690                 } else {
1691                         gstatus.resolution_num = gstatus.resolution;
1692                         gstatus.resolution_den = 1000000000uL;
1693                 }
1694                 spin_unlock_irq(&t->lock);
1695         } else {
1696                 err = -ENODEV;
1697         }
1698         mutex_unlock(&register_mutex);
1699         if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1700                 err = -EFAULT;
1701         return err;
1702 }
1703
1704 static int snd_timer_user_tselect(struct file *file,
1705                                   struct snd_timer_select __user *_tselect)
1706 {
1707         struct snd_timer_user *tu;
1708         struct snd_timer_select tselect;
1709         char str[32];
1710         int err = 0;
1711
1712         tu = file->private_data;
1713         if (tu->timeri) {
1714                 snd_timer_close(tu->timeri);
1715                 snd_timer_instance_free(tu->timeri);
1716                 tu->timeri = NULL;
1717         }
1718         if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1719                 err = -EFAULT;
1720                 goto __err;
1721         }
1722         sprintf(str, "application %i", current->pid);
1723         if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1724                 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1725         tu->timeri = snd_timer_instance_new(str);
1726         if (!tu->timeri) {
1727                 err = -ENOMEM;
1728                 goto __err;
1729         }
1730
1731         tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1732         tu->timeri->callback = tu->tread
1733                         ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1734         tu->timeri->ccallback = snd_timer_user_ccallback;
1735         tu->timeri->callback_data = (void *)tu;
1736         tu->timeri->disconnect = snd_timer_user_disconnect;
1737
1738         err = snd_timer_open(tu->timeri, &tselect.id, current->pid);
1739         if (err < 0) {
1740                 snd_timer_instance_free(tu->timeri);
1741                 tu->timeri = NULL;
1742         }
1743
1744       __err:
1745         return err;
1746 }
1747
1748 static int snd_timer_user_info(struct file *file,
1749                                struct snd_timer_info __user *_info)
1750 {
1751         struct snd_timer_user *tu;
1752         struct snd_timer_info *info;
1753         struct snd_timer *t;
1754         int err = 0;
1755
1756         tu = file->private_data;
1757         if (!tu->timeri)
1758                 return -EBADFD;
1759         t = tu->timeri->timer;
1760         if (!t)
1761                 return -EBADFD;
1762
1763         info = kzalloc(sizeof(*info), GFP_KERNEL);
1764         if (! info)
1765                 return -ENOMEM;
1766         info->card = t->card ? t->card->number : -1;
1767         if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1768                 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1769         strlcpy(info->id, t->id, sizeof(info->id));
1770         strlcpy(info->name, t->name, sizeof(info->name));
1771         info->resolution = t->hw.resolution;
1772         if (copy_to_user(_info, info, sizeof(*_info)))
1773                 err = -EFAULT;
1774         kfree(info);
1775         return err;
1776 }
1777
1778 static int snd_timer_user_params(struct file *file,
1779                                  struct snd_timer_params __user *_params)
1780 {
1781         struct snd_timer_user *tu;
1782         struct snd_timer_params params;
1783         struct snd_timer *t;
1784         int err;
1785
1786         tu = file->private_data;
1787         if (!tu->timeri)
1788                 return -EBADFD;
1789         t = tu->timeri->timer;
1790         if (!t)
1791                 return -EBADFD;
1792         if (copy_from_user(&params, _params, sizeof(params)))
1793                 return -EFAULT;
1794         if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1795                 u64 resolution;
1796
1797                 if (params.ticks < 1) {
1798                         err = -EINVAL;
1799                         goto _end;
1800                 }
1801
1802                 /* Don't allow resolution less than 1ms */
1803                 resolution = snd_timer_resolution(tu->timeri);
1804                 resolution *= params.ticks;
1805                 if (resolution < 1000000) {
1806                         err = -EINVAL;
1807                         goto _end;
1808                 }
1809         }
1810         if (params.queue_size > 0 &&
1811             (params.queue_size < 32 || params.queue_size > 1024)) {
1812                 err = -EINVAL;
1813                 goto _end;
1814         }
1815         if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1816                               (1<<SNDRV_TIMER_EVENT_TICK)|
1817                               (1<<SNDRV_TIMER_EVENT_START)|
1818                               (1<<SNDRV_TIMER_EVENT_STOP)|
1819                               (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1820                               (1<<SNDRV_TIMER_EVENT_PAUSE)|
1821                               (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1822                               (1<<SNDRV_TIMER_EVENT_RESUME)|
1823                               (1<<SNDRV_TIMER_EVENT_MSTART)|
1824                               (1<<SNDRV_TIMER_EVENT_MSTOP)|
1825                               (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1826                               (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1827                               (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1828                               (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1829                 err = -EINVAL;
1830                 goto _end;
1831         }
1832         snd_timer_stop(tu->timeri);
1833         spin_lock_irq(&t->lock);
1834         tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1835                                SNDRV_TIMER_IFLG_EXCLUSIVE|
1836                                SNDRV_TIMER_IFLG_EARLY_EVENT);
1837         if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1838                 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1839         if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1840                 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1841         if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1842                 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1843         spin_unlock_irq(&t->lock);
1844         if (params.queue_size > 0 &&
1845             (unsigned int)tu->queue_size != params.queue_size) {
1846                 err = realloc_user_queue(tu, params.queue_size);
1847                 if (err < 0)
1848                         goto _end;
1849         }
1850         spin_lock_irq(&tu->qlock);
1851         tu->qhead = tu->qtail = tu->qused = 0;
1852         if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1853                 if (tu->tread) {
1854                         struct snd_timer_tread tread;
1855                         memset(&tread, 0, sizeof(tread));
1856                         tread.event = SNDRV_TIMER_EVENT_EARLY;
1857                         tread.tstamp.tv_sec = 0;
1858                         tread.tstamp.tv_nsec = 0;
1859                         tread.val = 0;
1860                         snd_timer_user_append_to_tqueue(tu, &tread);
1861                 } else {
1862                         struct snd_timer_read *r = &tu->queue[0];
1863                         r->resolution = 0;
1864                         r->ticks = 0;
1865                         tu->qused++;
1866                         tu->qtail++;
1867                 }
1868         }
1869         tu->filter = params.filter;
1870         tu->ticks = params.ticks;
1871         spin_unlock_irq(&tu->qlock);
1872         err = 0;
1873  _end:
1874         if (copy_to_user(_params, &params, sizeof(params)))
1875                 return -EFAULT;
1876         return err;
1877 }
1878
1879 static int snd_timer_user_status(struct file *file,
1880                                  struct snd_timer_status __user *_status)
1881 {
1882         struct snd_timer_user *tu;
1883         struct snd_timer_status status;
1884
1885         tu = file->private_data;
1886         if (!tu->timeri)
1887                 return -EBADFD;
1888         memset(&status, 0, sizeof(status));
1889         status.tstamp = tu->tstamp;
1890         status.resolution = snd_timer_resolution(tu->timeri);
1891         status.lost = tu->timeri->lost;
1892         status.overrun = tu->overrun;
1893         spin_lock_irq(&tu->qlock);
1894         status.queue = tu->qused;
1895         spin_unlock_irq(&tu->qlock);
1896         if (copy_to_user(_status, &status, sizeof(status)))
1897                 return -EFAULT;
1898         return 0;
1899 }
1900
1901 static int snd_timer_user_start(struct file *file)
1902 {
1903         int err;
1904         struct snd_timer_user *tu;
1905
1906         tu = file->private_data;
1907         if (!tu->timeri)
1908                 return -EBADFD;
1909         snd_timer_stop(tu->timeri);
1910         tu->timeri->lost = 0;
1911         tu->last_resolution = 0;
1912         err = snd_timer_start(tu->timeri, tu->ticks);
1913         if (err < 0)
1914                 return err;
1915         return 0;
1916 }
1917
1918 static int snd_timer_user_stop(struct file *file)
1919 {
1920         int err;
1921         struct snd_timer_user *tu;
1922
1923         tu = file->private_data;
1924         if (!tu->timeri)
1925                 return -EBADFD;
1926         err = snd_timer_stop(tu->timeri);
1927         if (err < 0)
1928                 return err;
1929         return 0;
1930 }
1931
1932 static int snd_timer_user_continue(struct file *file)
1933 {
1934         int err;
1935         struct snd_timer_user *tu;
1936
1937         tu = file->private_data;
1938         if (!tu->timeri)
1939                 return -EBADFD;
1940         /* start timer instead of continue if it's not used before */
1941         if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1942                 return snd_timer_user_start(file);
1943         tu->timeri->lost = 0;
1944         err = snd_timer_continue(tu->timeri);
1945         if (err < 0)
1946                 return err;
1947         return 0;
1948 }
1949
1950 static int snd_timer_user_pause(struct file *file)
1951 {
1952         int err;
1953         struct snd_timer_user *tu;
1954
1955         tu = file->private_data;
1956         if (!tu->timeri)
1957                 return -EBADFD;
1958         err = snd_timer_pause(tu->timeri);
1959         if (err < 0)
1960                 return err;
1961         return 0;
1962 }
1963
1964 enum {
1965         SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1966         SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1967         SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1968         SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1969 };
1970
1971 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1972                                  unsigned long arg)
1973 {
1974         struct snd_timer_user *tu;
1975         void __user *argp = (void __user *)arg;
1976         int __user *p = argp;
1977
1978         tu = file->private_data;
1979         switch (cmd) {
1980         case SNDRV_TIMER_IOCTL_PVERSION:
1981                 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1982         case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1983                 return snd_timer_user_next_device(argp);
1984         case SNDRV_TIMER_IOCTL_TREAD:
1985         {
1986                 int xarg, old_tread;
1987
1988                 if (tu->timeri) /* too late */
1989                         return -EBUSY;
1990                 if (get_user(xarg, p))
1991                         return -EFAULT;
1992                 old_tread = tu->tread;
1993                 tu->tread = xarg ? 1 : 0;
1994                 if (tu->tread != old_tread &&
1995                     realloc_user_queue(tu, tu->queue_size) < 0) {
1996                         tu->tread = old_tread;
1997                         return -ENOMEM;
1998                 }
1999                 return 0;
2000         }
2001         case SNDRV_TIMER_IOCTL_GINFO:
2002                 return snd_timer_user_ginfo(file, argp);
2003         case SNDRV_TIMER_IOCTL_GPARAMS:
2004                 return snd_timer_user_gparams(file, argp);
2005         case SNDRV_TIMER_IOCTL_GSTATUS:
2006                 return snd_timer_user_gstatus(file, argp);
2007         case SNDRV_TIMER_IOCTL_SELECT:
2008                 return snd_timer_user_tselect(file, argp);
2009         case SNDRV_TIMER_IOCTL_INFO:
2010                 return snd_timer_user_info(file, argp);
2011         case SNDRV_TIMER_IOCTL_PARAMS:
2012                 return snd_timer_user_params(file, argp);
2013         case SNDRV_TIMER_IOCTL_STATUS:
2014                 return snd_timer_user_status(file, argp);
2015         case SNDRV_TIMER_IOCTL_START:
2016         case SNDRV_TIMER_IOCTL_START_OLD:
2017                 return snd_timer_user_start(file);
2018         case SNDRV_TIMER_IOCTL_STOP:
2019         case SNDRV_TIMER_IOCTL_STOP_OLD:
2020                 return snd_timer_user_stop(file);
2021         case SNDRV_TIMER_IOCTL_CONTINUE:
2022         case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
2023                 return snd_timer_user_continue(file);
2024         case SNDRV_TIMER_IOCTL_PAUSE:
2025         case SNDRV_TIMER_IOCTL_PAUSE_OLD:
2026                 return snd_timer_user_pause(file);
2027         }
2028         return -ENOTTY;
2029 }
2030
2031 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
2032                                  unsigned long arg)
2033 {
2034         struct snd_timer_user *tu = file->private_data;
2035         long ret;
2036
2037         mutex_lock(&tu->ioctl_lock);
2038         ret = __snd_timer_user_ioctl(file, cmd, arg);
2039         mutex_unlock(&tu->ioctl_lock);
2040         return ret;
2041 }
2042
2043 static int snd_timer_user_fasync(int fd, struct file * file, int on)
2044 {
2045         struct snd_timer_user *tu;
2046
2047         tu = file->private_data;
2048         return fasync_helper(fd, file, on, &tu->fasync);
2049 }
2050
2051 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
2052                                    size_t count, loff_t *offset)
2053 {
2054         struct snd_timer_user *tu;
2055         long result = 0, unit;
2056         int qhead;
2057         int err = 0;
2058
2059         tu = file->private_data;
2060         unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
2061         mutex_lock(&tu->ioctl_lock);
2062         spin_lock_irq(&tu->qlock);
2063         while ((long)count - result >= unit) {
2064                 while (!tu->qused) {
2065                         wait_queue_entry_t wait;
2066
2067                         if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
2068                                 err = -EAGAIN;
2069                                 goto _error;
2070                         }
2071
2072                         set_current_state(TASK_INTERRUPTIBLE);
2073                         init_waitqueue_entry(&wait, current);
2074                         add_wait_queue(&tu->qchange_sleep, &wait);
2075
2076                         spin_unlock_irq(&tu->qlock);
2077                         mutex_unlock(&tu->ioctl_lock);
2078                         schedule();
2079                         mutex_lock(&tu->ioctl_lock);
2080                         spin_lock_irq(&tu->qlock);
2081
2082                         remove_wait_queue(&tu->qchange_sleep, &wait);
2083
2084                         if (tu->disconnected) {
2085                                 err = -ENODEV;
2086                                 goto _error;
2087                         }
2088                         if (signal_pending(current)) {
2089                                 err = -ERESTARTSYS;
2090                                 goto _error;
2091                         }
2092                 }
2093
2094                 qhead = tu->qhead++;
2095                 tu->qhead %= tu->queue_size;
2096                 tu->qused--;
2097                 spin_unlock_irq(&tu->qlock);
2098
2099                 if (tu->tread) {
2100                         if (copy_to_user(buffer, &tu->tqueue[qhead],
2101                                          sizeof(struct snd_timer_tread)))
2102                                 err = -EFAULT;
2103                 } else {
2104                         if (copy_to_user(buffer, &tu->queue[qhead],
2105                                          sizeof(struct snd_timer_read)))
2106                                 err = -EFAULT;
2107                 }
2108
2109                 spin_lock_irq(&tu->qlock);
2110                 if (err < 0)
2111                         goto _error;
2112                 result += unit;
2113                 buffer += unit;
2114         }
2115  _error:
2116         spin_unlock_irq(&tu->qlock);
2117         mutex_unlock(&tu->ioctl_lock);
2118         return result > 0 ? result : err;
2119 }
2120
2121 static __poll_t snd_timer_user_poll(struct file *file, poll_table * wait)
2122 {
2123         __poll_t mask;
2124         struct snd_timer_user *tu;
2125
2126         tu = file->private_data;
2127
2128         poll_wait(file, &tu->qchange_sleep, wait);
2129
2130         mask = 0;
2131         spin_lock_irq(&tu->qlock);
2132         if (tu->qused)
2133                 mask |= EPOLLIN | EPOLLRDNORM;
2134         if (tu->disconnected)
2135                 mask |= EPOLLERR;
2136         spin_unlock_irq(&tu->qlock);
2137
2138         return mask;
2139 }
2140
2141 #ifdef CONFIG_COMPAT
2142 #include "timer_compat.c"
2143 #else
2144 #define snd_timer_user_ioctl_compat     NULL
2145 #endif
2146
2147 static const struct file_operations snd_timer_f_ops =
2148 {
2149         .owner =        THIS_MODULE,
2150         .read =         snd_timer_user_read,
2151         .open =         snd_timer_user_open,
2152         .release =      snd_timer_user_release,
2153         .llseek =       no_llseek,
2154         .poll =         snd_timer_user_poll,
2155         .unlocked_ioctl =       snd_timer_user_ioctl,
2156         .compat_ioctl = snd_timer_user_ioctl_compat,
2157         .fasync =       snd_timer_user_fasync,
2158 };
2159
2160 /* unregister the system timer */
2161 static void snd_timer_free_all(void)
2162 {
2163         struct snd_timer *timer, *n;
2164
2165         list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2166                 snd_timer_free(timer);
2167 }
2168
2169 static struct device timer_dev;
2170
2171 /*
2172  *  ENTRY functions
2173  */
2174
2175 static int __init alsa_timer_init(void)
2176 {
2177         int err;
2178
2179         snd_device_initialize(&timer_dev, NULL);
2180         dev_set_name(&timer_dev, "timer");
2181
2182 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2183         snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2184                               "system timer");
2185 #endif
2186
2187         err = snd_timer_register_system();
2188         if (err < 0) {
2189                 pr_err("ALSA: unable to register system timer (%i)\n", err);
2190                 goto put_timer;
2191         }
2192
2193         err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2194                                   &snd_timer_f_ops, NULL, &timer_dev);
2195         if (err < 0) {
2196                 pr_err("ALSA: unable to register timer device (%i)\n", err);
2197                 snd_timer_free_all();
2198                 goto put_timer;
2199         }
2200
2201         snd_timer_proc_init();
2202         return 0;
2203
2204 put_timer:
2205         put_device(&timer_dev);
2206         return err;
2207 }
2208
2209 static void __exit alsa_timer_exit(void)
2210 {
2211         snd_unregister_device(&timer_dev);
2212         snd_timer_free_all();
2213         put_device(&timer_dev);
2214         snd_timer_proc_done();
2215 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2216         snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2217 #endif
2218 }
2219
2220 module_init(alsa_timer_init)
2221 module_exit(alsa_timer_exit)