GNU Linux-libre 4.9.301-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 fasync_struct *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         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         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         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                 kfree(tu->queue);
1443                 kfree(tu->tqueue);
1444                 kfree(tu);
1445         }
1446         return 0;
1447 }
1448
1449 static void snd_timer_user_zero_id(struct snd_timer_id *id)
1450 {
1451         id->dev_class = SNDRV_TIMER_CLASS_NONE;
1452         id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1453         id->card = -1;
1454         id->device = -1;
1455         id->subdevice = -1;
1456 }
1457
1458 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1459 {
1460         id->dev_class = timer->tmr_class;
1461         id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1462         id->card = timer->card ? timer->card->number : -1;
1463         id->device = timer->tmr_device;
1464         id->subdevice = timer->tmr_subdevice;
1465 }
1466
1467 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
1468 {
1469         struct snd_timer_id id;
1470         struct snd_timer *timer;
1471         struct list_head *p;
1472
1473         if (copy_from_user(&id, _tid, sizeof(id)))
1474                 return -EFAULT;
1475         mutex_lock(&register_mutex);
1476         if (id.dev_class < 0) {         /* first item */
1477                 if (list_empty(&snd_timer_list))
1478                         snd_timer_user_zero_id(&id);
1479                 else {
1480                         timer = list_entry(snd_timer_list.next,
1481                                            struct snd_timer, device_list);
1482                         snd_timer_user_copy_id(&id, timer);
1483                 }
1484         } else {
1485                 switch (id.dev_class) {
1486                 case SNDRV_TIMER_CLASS_GLOBAL:
1487                         id.device = id.device < 0 ? 0 : id.device + 1;
1488                         list_for_each(p, &snd_timer_list) {
1489                                 timer = list_entry(p, struct snd_timer, device_list);
1490                                 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1491                                         snd_timer_user_copy_id(&id, timer);
1492                                         break;
1493                                 }
1494                                 if (timer->tmr_device >= id.device) {
1495                                         snd_timer_user_copy_id(&id, timer);
1496                                         break;
1497                                 }
1498                         }
1499                         if (p == &snd_timer_list)
1500                                 snd_timer_user_zero_id(&id);
1501                         break;
1502                 case SNDRV_TIMER_CLASS_CARD:
1503                 case SNDRV_TIMER_CLASS_PCM:
1504                         if (id.card < 0) {
1505                                 id.card = 0;
1506                         } else {
1507                                 if (id.card < 0) {
1508                                         id.card = 0;
1509                                 } else {
1510                                         if (id.device < 0) {
1511                                                 id.device = 0;
1512                                         } else {
1513                                                 if (id.subdevice < 0) {
1514                                                         id.subdevice = 0;
1515                                                 } else {
1516                                                         id.subdevice++;
1517                                                 }
1518                                         }
1519                                 }
1520                         }
1521                         list_for_each(p, &snd_timer_list) {
1522                                 timer = list_entry(p, struct snd_timer, device_list);
1523                                 if (timer->tmr_class > id.dev_class) {
1524                                         snd_timer_user_copy_id(&id, timer);
1525                                         break;
1526                                 }
1527                                 if (timer->tmr_class < id.dev_class)
1528                                         continue;
1529                                 if (timer->card->number > id.card) {
1530                                         snd_timer_user_copy_id(&id, timer);
1531                                         break;
1532                                 }
1533                                 if (timer->card->number < id.card)
1534                                         continue;
1535                                 if (timer->tmr_device > id.device) {
1536                                         snd_timer_user_copy_id(&id, timer);
1537                                         break;
1538                                 }
1539                                 if (timer->tmr_device < id.device)
1540                                         continue;
1541                                 if (timer->tmr_subdevice > id.subdevice) {
1542                                         snd_timer_user_copy_id(&id, timer);
1543                                         break;
1544                                 }
1545                                 if (timer->tmr_subdevice < id.subdevice)
1546                                         continue;
1547                                 snd_timer_user_copy_id(&id, timer);
1548                                 break;
1549                         }
1550                         if (p == &snd_timer_list)
1551                                 snd_timer_user_zero_id(&id);
1552                         break;
1553                 default:
1554                         snd_timer_user_zero_id(&id);
1555                 }
1556         }
1557         mutex_unlock(&register_mutex);
1558         if (copy_to_user(_tid, &id, sizeof(*_tid)))
1559                 return -EFAULT;
1560         return 0;
1561 }
1562
1563 static int snd_timer_user_ginfo(struct file *file,
1564                                 struct snd_timer_ginfo __user *_ginfo)
1565 {
1566         struct snd_timer_ginfo *ginfo;
1567         struct snd_timer_id tid;
1568         struct snd_timer *t;
1569         struct list_head *p;
1570         int err = 0;
1571
1572         ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1573         if (IS_ERR(ginfo))
1574                 return PTR_ERR(ginfo);
1575
1576         tid = ginfo->tid;
1577         memset(ginfo, 0, sizeof(*ginfo));
1578         ginfo->tid = tid;
1579         mutex_lock(&register_mutex);
1580         t = snd_timer_find(&tid);
1581         if (t != NULL) {
1582                 ginfo->card = t->card ? t->card->number : -1;
1583                 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1584                         ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1585                 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1586                 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1587                 ginfo->resolution = t->hw.resolution;
1588                 if (t->hw.resolution_min > 0) {
1589                         ginfo->resolution_min = t->hw.resolution_min;
1590                         ginfo->resolution_max = t->hw.resolution_max;
1591                 }
1592                 list_for_each(p, &t->open_list_head) {
1593                         ginfo->clients++;
1594                 }
1595         } else {
1596                 err = -ENODEV;
1597         }
1598         mutex_unlock(&register_mutex);
1599         if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1600                 err = -EFAULT;
1601         kfree(ginfo);
1602         return err;
1603 }
1604
1605 static int timer_set_gparams(struct snd_timer_gparams *gparams)
1606 {
1607         struct snd_timer *t;
1608         int err;
1609
1610         mutex_lock(&register_mutex);
1611         t = snd_timer_find(&gparams->tid);
1612         if (!t) {
1613                 err = -ENODEV;
1614                 goto _error;
1615         }
1616         if (!list_empty(&t->open_list_head)) {
1617                 err = -EBUSY;
1618                 goto _error;
1619         }
1620         if (!t->hw.set_period) {
1621                 err = -ENOSYS;
1622                 goto _error;
1623         }
1624         err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
1625 _error:
1626         mutex_unlock(&register_mutex);
1627         return err;
1628 }
1629
1630 static int snd_timer_user_gparams(struct file *file,
1631                                   struct snd_timer_gparams __user *_gparams)
1632 {
1633         struct snd_timer_gparams gparams;
1634
1635         if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1636                 return -EFAULT;
1637         return timer_set_gparams(&gparams);
1638 }
1639
1640 static int snd_timer_user_gstatus(struct file *file,
1641                                   struct snd_timer_gstatus __user *_gstatus)
1642 {
1643         struct snd_timer_gstatus gstatus;
1644         struct snd_timer_id tid;
1645         struct snd_timer *t;
1646         int err = 0;
1647
1648         if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1649                 return -EFAULT;
1650         tid = gstatus.tid;
1651         memset(&gstatus, 0, sizeof(gstatus));
1652         gstatus.tid = tid;
1653         mutex_lock(&register_mutex);
1654         t = snd_timer_find(&tid);
1655         if (t != NULL) {
1656                 if (t->hw.c_resolution)
1657                         gstatus.resolution = t->hw.c_resolution(t);
1658                 else
1659                         gstatus.resolution = t->hw.resolution;
1660                 if (t->hw.precise_resolution) {
1661                         t->hw.precise_resolution(t, &gstatus.resolution_num,
1662                                                  &gstatus.resolution_den);
1663                 } else {
1664                         gstatus.resolution_num = gstatus.resolution;
1665                         gstatus.resolution_den = 1000000000uL;
1666                 }
1667         } else {
1668                 err = -ENODEV;
1669         }
1670         mutex_unlock(&register_mutex);
1671         if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1672                 err = -EFAULT;
1673         return err;
1674 }
1675
1676 static int snd_timer_user_tselect(struct file *file,
1677                                   struct snd_timer_select __user *_tselect)
1678 {
1679         struct snd_timer_user *tu;
1680         struct snd_timer_select tselect;
1681         char str[32];
1682         int err = 0;
1683
1684         tu = file->private_data;
1685         if (tu->timeri) {
1686                 snd_timer_close(tu->timeri);
1687                 tu->timeri = NULL;
1688         }
1689         if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1690                 err = -EFAULT;
1691                 goto __err;
1692         }
1693         sprintf(str, "application %i", current->pid);
1694         if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1695                 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1696         err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1697         if (err < 0)
1698                 goto __err;
1699
1700         tu->qhead = tu->qtail = tu->qused = 0;
1701         kfree(tu->queue);
1702         tu->queue = NULL;
1703         kfree(tu->tqueue);
1704         tu->tqueue = NULL;
1705         if (tu->tread) {
1706                 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
1707                                      GFP_KERNEL);
1708                 if (tu->tqueue == NULL)
1709                         err = -ENOMEM;
1710         } else {
1711                 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
1712                                     GFP_KERNEL);
1713                 if (tu->queue == NULL)
1714                         err = -ENOMEM;
1715         }
1716
1717         if (err < 0) {
1718                 snd_timer_close(tu->timeri);
1719                 tu->timeri = NULL;
1720         } else {
1721                 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
1722                 tu->timeri->callback = tu->tread
1723                         ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1724                 tu->timeri->ccallback = snd_timer_user_ccallback;
1725                 tu->timeri->callback_data = (void *)tu;
1726                 tu->timeri->disconnect = snd_timer_user_disconnect;
1727         }
1728
1729       __err:
1730         return err;
1731 }
1732
1733 static int snd_timer_user_info(struct file *file,
1734                                struct snd_timer_info __user *_info)
1735 {
1736         struct snd_timer_user *tu;
1737         struct snd_timer_info *info;
1738         struct snd_timer *t;
1739         int err = 0;
1740
1741         tu = file->private_data;
1742         if (!tu->timeri)
1743                 return -EBADFD;
1744         t = tu->timeri->timer;
1745         if (!t)
1746                 return -EBADFD;
1747
1748         info = kzalloc(sizeof(*info), GFP_KERNEL);
1749         if (! info)
1750                 return -ENOMEM;
1751         info->card = t->card ? t->card->number : -1;
1752         if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1753                 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1754         strlcpy(info->id, t->id, sizeof(info->id));
1755         strlcpy(info->name, t->name, sizeof(info->name));
1756         info->resolution = t->hw.resolution;
1757         if (copy_to_user(_info, info, sizeof(*_info)))
1758                 err = -EFAULT;
1759         kfree(info);
1760         return err;
1761 }
1762
1763 static int snd_timer_user_params(struct file *file,
1764                                  struct snd_timer_params __user *_params)
1765 {
1766         struct snd_timer_user *tu;
1767         struct snd_timer_params params;
1768         struct snd_timer *t;
1769         struct snd_timer_read *tr;
1770         struct snd_timer_tread *ttr;
1771         int err;
1772
1773         tu = file->private_data;
1774         if (!tu->timeri)
1775                 return -EBADFD;
1776         t = tu->timeri->timer;
1777         if (!t)
1778                 return -EBADFD;
1779         if (copy_from_user(&params, _params, sizeof(params)))
1780                 return -EFAULT;
1781         if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1782                 u64 resolution;
1783
1784                 if (params.ticks < 1) {
1785                         err = -EINVAL;
1786                         goto _end;
1787                 }
1788
1789                 /* Don't allow resolution less than 1ms */
1790                 resolution = snd_timer_resolution(tu->timeri);
1791                 resolution *= params.ticks;
1792                 if (resolution < 1000000) {
1793                         err = -EINVAL;
1794                         goto _end;
1795                 }
1796         }
1797         if (params.queue_size > 0 &&
1798             (params.queue_size < 32 || params.queue_size > 1024)) {
1799                 err = -EINVAL;
1800                 goto _end;
1801         }
1802         if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1803                               (1<<SNDRV_TIMER_EVENT_TICK)|
1804                               (1<<SNDRV_TIMER_EVENT_START)|
1805                               (1<<SNDRV_TIMER_EVENT_STOP)|
1806                               (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1807                               (1<<SNDRV_TIMER_EVENT_PAUSE)|
1808                               (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1809                               (1<<SNDRV_TIMER_EVENT_RESUME)|
1810                               (1<<SNDRV_TIMER_EVENT_MSTART)|
1811                               (1<<SNDRV_TIMER_EVENT_MSTOP)|
1812                               (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1813                               (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1814                               (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1815                               (1<<SNDRV_TIMER_EVENT_MRESUME))) {
1816                 err = -EINVAL;
1817                 goto _end;
1818         }
1819         snd_timer_stop(tu->timeri);
1820         spin_lock_irq(&t->lock);
1821         tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1822                                SNDRV_TIMER_IFLG_EXCLUSIVE|
1823                                SNDRV_TIMER_IFLG_EARLY_EVENT);
1824         if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1825                 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1826         if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1827                 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1828         if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1829                 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1830         spin_unlock_irq(&t->lock);
1831         if (params.queue_size > 0 &&
1832             (unsigned int)tu->queue_size != params.queue_size) {
1833                 if (tu->tread) {
1834                         ttr = kmalloc(params.queue_size * sizeof(*ttr),
1835                                       GFP_KERNEL);
1836                         if (ttr) {
1837                                 kfree(tu->tqueue);
1838                                 tu->queue_size = params.queue_size;
1839                                 tu->tqueue = ttr;
1840                         }
1841                 } else {
1842                         tr = kmalloc(params.queue_size * sizeof(*tr),
1843                                      GFP_KERNEL);
1844                         if (tr) {
1845                                 kfree(tu->queue);
1846                                 tu->queue_size = params.queue_size;
1847                                 tu->queue = tr;
1848                         }
1849                 }
1850         }
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         err = 0;
1872  _end:
1873         if (copy_to_user(_params, &params, sizeof(params)))
1874                 return -EFAULT;
1875         return err;
1876 }
1877
1878 static int snd_timer_user_status(struct file *file,
1879                                  struct snd_timer_status __user *_status)
1880 {
1881         struct snd_timer_user *tu;
1882         struct snd_timer_status status;
1883
1884         tu = file->private_data;
1885         if (!tu->timeri)
1886                 return -EBADFD;
1887         memset(&status, 0, sizeof(status));
1888         status.tstamp = tu->tstamp;
1889         status.resolution = snd_timer_resolution(tu->timeri);
1890         status.lost = tu->timeri->lost;
1891         status.overrun = tu->overrun;
1892         spin_lock_irq(&tu->qlock);
1893         status.queue = tu->qused;
1894         spin_unlock_irq(&tu->qlock);
1895         if (copy_to_user(_status, &status, sizeof(status)))
1896                 return -EFAULT;
1897         return 0;
1898 }
1899
1900 static int snd_timer_user_start(struct file *file)
1901 {
1902         int err;
1903         struct snd_timer_user *tu;
1904
1905         tu = file->private_data;
1906         if (!tu->timeri)
1907                 return -EBADFD;
1908         snd_timer_stop(tu->timeri);
1909         tu->timeri->lost = 0;
1910         tu->last_resolution = 0;
1911         return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1912 }
1913
1914 static int snd_timer_user_stop(struct file *file)
1915 {
1916         int err;
1917         struct snd_timer_user *tu;
1918
1919         tu = file->private_data;
1920         if (!tu->timeri)
1921                 return -EBADFD;
1922         return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1923 }
1924
1925 static int snd_timer_user_continue(struct file *file)
1926 {
1927         int err;
1928         struct snd_timer_user *tu;
1929
1930         tu = file->private_data;
1931         if (!tu->timeri)
1932                 return -EBADFD;
1933         /* start timer instead of continue if it's not used before */
1934         if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1935                 return snd_timer_user_start(file);
1936         tu->timeri->lost = 0;
1937         return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1938 }
1939
1940 static int snd_timer_user_pause(struct file *file)
1941 {
1942         int err;
1943         struct snd_timer_user *tu;
1944
1945         tu = file->private_data;
1946         if (!tu->timeri)
1947                 return -EBADFD;
1948         return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
1949 }
1950
1951 enum {
1952         SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1953         SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1954         SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1955         SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1956 };
1957
1958 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1959                                  unsigned long arg)
1960 {
1961         struct snd_timer_user *tu;
1962         void __user *argp = (void __user *)arg;
1963         int __user *p = argp;
1964
1965         tu = file->private_data;
1966         switch (cmd) {
1967         case SNDRV_TIMER_IOCTL_PVERSION:
1968                 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1969         case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1970                 return snd_timer_user_next_device(argp);
1971         case SNDRV_TIMER_IOCTL_TREAD:
1972         {
1973                 int xarg;
1974
1975                 if (tu->timeri) /* too late */
1976                         return -EBUSY;
1977                 if (get_user(xarg, p))
1978                         return -EFAULT;
1979                 tu->tread = xarg ? 1 : 0;
1980                 return 0;
1981         }
1982         case SNDRV_TIMER_IOCTL_GINFO:
1983                 return snd_timer_user_ginfo(file, argp);
1984         case SNDRV_TIMER_IOCTL_GPARAMS:
1985                 return snd_timer_user_gparams(file, argp);
1986         case SNDRV_TIMER_IOCTL_GSTATUS:
1987                 return snd_timer_user_gstatus(file, argp);
1988         case SNDRV_TIMER_IOCTL_SELECT:
1989                 return snd_timer_user_tselect(file, argp);
1990         case SNDRV_TIMER_IOCTL_INFO:
1991                 return snd_timer_user_info(file, argp);
1992         case SNDRV_TIMER_IOCTL_PARAMS:
1993                 return snd_timer_user_params(file, argp);
1994         case SNDRV_TIMER_IOCTL_STATUS:
1995                 return snd_timer_user_status(file, argp);
1996         case SNDRV_TIMER_IOCTL_START:
1997         case SNDRV_TIMER_IOCTL_START_OLD:
1998                 return snd_timer_user_start(file);
1999         case SNDRV_TIMER_IOCTL_STOP:
2000         case SNDRV_TIMER_IOCTL_STOP_OLD:
2001                 return snd_timer_user_stop(file);
2002         case SNDRV_TIMER_IOCTL_CONTINUE:
2003         case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
2004                 return snd_timer_user_continue(file);
2005         case SNDRV_TIMER_IOCTL_PAUSE:
2006         case SNDRV_TIMER_IOCTL_PAUSE_OLD:
2007                 return snd_timer_user_pause(file);
2008         }
2009         return -ENOTTY;
2010 }
2011
2012 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
2013                                  unsigned long arg)
2014 {
2015         struct snd_timer_user *tu = file->private_data;
2016         long ret;
2017
2018         mutex_lock(&tu->ioctl_lock);
2019         ret = __snd_timer_user_ioctl(file, cmd, arg);
2020         mutex_unlock(&tu->ioctl_lock);
2021         return ret;
2022 }
2023
2024 static int snd_timer_user_fasync(int fd, struct file * file, int on)
2025 {
2026         struct snd_timer_user *tu;
2027
2028         tu = file->private_data;
2029         return fasync_helper(fd, file, on, &tu->fasync);
2030 }
2031
2032 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
2033                                    size_t count, loff_t *offset)
2034 {
2035         struct snd_timer_user *tu;
2036         long result = 0, unit;
2037         int qhead;
2038         int err = 0;
2039
2040         tu = file->private_data;
2041         unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
2042         mutex_lock(&tu->ioctl_lock);
2043         spin_lock_irq(&tu->qlock);
2044         while ((long)count - result >= unit) {
2045                 while (!tu->qused) {
2046                         wait_queue_t wait;
2047
2048                         if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
2049                                 err = -EAGAIN;
2050                                 goto _error;
2051                         }
2052
2053                         set_current_state(TASK_INTERRUPTIBLE);
2054                         init_waitqueue_entry(&wait, current);
2055                         add_wait_queue(&tu->qchange_sleep, &wait);
2056
2057                         spin_unlock_irq(&tu->qlock);
2058                         mutex_unlock(&tu->ioctl_lock);
2059                         schedule();
2060                         mutex_lock(&tu->ioctl_lock);
2061                         spin_lock_irq(&tu->qlock);
2062
2063                         remove_wait_queue(&tu->qchange_sleep, &wait);
2064
2065                         if (tu->disconnected) {
2066                                 err = -ENODEV;
2067                                 goto _error;
2068                         }
2069                         if (signal_pending(current)) {
2070                                 err = -ERESTARTSYS;
2071                                 goto _error;
2072                         }
2073                 }
2074
2075                 qhead = tu->qhead++;
2076                 tu->qhead %= tu->queue_size;
2077                 tu->qused--;
2078                 spin_unlock_irq(&tu->qlock);
2079
2080                 if (tu->tread) {
2081                         if (copy_to_user(buffer, &tu->tqueue[qhead],
2082                                          sizeof(struct snd_timer_tread)))
2083                                 err = -EFAULT;
2084                 } else {
2085                         if (copy_to_user(buffer, &tu->queue[qhead],
2086                                          sizeof(struct snd_timer_read)))
2087                                 err = -EFAULT;
2088                 }
2089
2090                 spin_lock_irq(&tu->qlock);
2091                 if (err < 0)
2092                         goto _error;
2093                 result += unit;
2094                 buffer += unit;
2095         }
2096  _error:
2097         spin_unlock_irq(&tu->qlock);
2098         mutex_unlock(&tu->ioctl_lock);
2099         return result > 0 ? result : err;
2100 }
2101
2102 static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
2103 {
2104         unsigned int mask;
2105         struct snd_timer_user *tu;
2106
2107         tu = file->private_data;
2108
2109         poll_wait(file, &tu->qchange_sleep, wait);
2110
2111         mask = 0;
2112         if (tu->qused)
2113                 mask |= POLLIN | POLLRDNORM;
2114         if (tu->disconnected)
2115                 mask |= POLLERR;
2116
2117         return mask;
2118 }
2119
2120 #ifdef CONFIG_COMPAT
2121 #include "timer_compat.c"
2122 #else
2123 #define snd_timer_user_ioctl_compat     NULL
2124 #endif
2125
2126 static const struct file_operations snd_timer_f_ops =
2127 {
2128         .owner =        THIS_MODULE,
2129         .read =         snd_timer_user_read,
2130         .open =         snd_timer_user_open,
2131         .release =      snd_timer_user_release,
2132         .llseek =       no_llseek,
2133         .poll =         snd_timer_user_poll,
2134         .unlocked_ioctl =       snd_timer_user_ioctl,
2135         .compat_ioctl = snd_timer_user_ioctl_compat,
2136         .fasync =       snd_timer_user_fasync,
2137 };
2138
2139 /* unregister the system timer */
2140 static void snd_timer_free_all(void)
2141 {
2142         struct snd_timer *timer, *n;
2143
2144         list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2145                 snd_timer_free(timer);
2146 }
2147
2148 static struct device timer_dev;
2149
2150 /*
2151  *  ENTRY functions
2152  */
2153
2154 static int __init alsa_timer_init(void)
2155 {
2156         int err;
2157
2158         snd_device_initialize(&timer_dev, NULL);
2159         dev_set_name(&timer_dev, "timer");
2160
2161 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2162         snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2163                               "system timer");
2164 #endif
2165
2166         err = snd_timer_register_system();
2167         if (err < 0) {
2168                 pr_err("ALSA: unable to register system timer (%i)\n", err);
2169                 put_device(&timer_dev);
2170                 return err;
2171         }
2172
2173         err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2174                                   &snd_timer_f_ops, NULL, &timer_dev);
2175         if (err < 0) {
2176                 pr_err("ALSA: unable to register timer device (%i)\n", err);
2177                 snd_timer_free_all();
2178                 put_device(&timer_dev);
2179                 return err;
2180         }
2181
2182         snd_timer_proc_init();
2183         return 0;
2184 }
2185
2186 static void __exit alsa_timer_exit(void)
2187 {
2188         snd_unregister_device(&timer_dev);
2189         snd_timer_free_all();
2190         put_device(&timer_dev);
2191         snd_timer_proc_done();
2192 #ifdef SNDRV_OSS_INFO_DEV_TIMERS
2193         snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2194 #endif
2195 }
2196
2197 module_init(alsa_timer_init)
2198 module_exit(alsa_timer_exit)