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