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