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