~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/core/timer.c

Version: ~ [ linux-5.9-rc6 ] ~ [ linux-5.8.10 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.66 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.146 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.198 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.236 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.236 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp