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