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