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