timer.c 52.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  Timers abstract layer
3
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <linux/delay.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/time.h>
26
#include <linux/mutex.h>
27
#include <linux/device.h>
28
#include <linux/module.h>
29
#include <linux/string.h>
L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37
#include <sound/core.h>
#include <sound/timer.h>
#include <sound/control.h>
#include <sound/info.h>
#include <sound/minors.h>
#include <sound/initval.h>
#include <linux/kmod.h>

T
Takashi Iwai 已提交
38
#if IS_ENABLED(CONFIG_SND_HRTIMER)
39
#define DEFAULT_TIMER_LIMIT 4
L
Linus Torvalds 已提交
40 41 42 43 44
#else
#define DEFAULT_TIMER_LIMIT 1
#endif

static int timer_limit = DEFAULT_TIMER_LIMIT;
45
static int timer_tstamp_monotonic = 1;
46
MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
L
Linus Torvalds 已提交
47 48 49 50
MODULE_DESCRIPTION("ALSA timer interface");
MODULE_LICENSE("GPL");
module_param(timer_limit, int, 0444);
MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
51 52
module_param(timer_tstamp_monotonic, int, 0444);
MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
L
Linus Torvalds 已提交
53

54 55 56
MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
MODULE_ALIAS("devname:snd/timer");

57 58
struct snd_timer_user {
	struct snd_timer_instance *timeri;
C
Clemens Ladisch 已提交
59
	int tread;		/* enhanced read with timestamps and events */
L
Linus Torvalds 已提交
60 61 62 63 64 65
	unsigned long ticks;
	unsigned long overrun;
	int qhead;
	int qtail;
	int qused;
	int queue_size;
66
	bool disconnected;
67 68
	struct snd_timer_read *queue;
	struct snd_timer_tread *tqueue;
L
Linus Torvalds 已提交
69 70 71 72 73 74
	spinlock_t qlock;
	unsigned long last_resolution;
	unsigned int filter;
	struct timespec tstamp;		/* trigger tstamp */
	wait_queue_head_t qchange_sleep;
	struct fasync_struct *fasync;
75
	struct mutex ioctl_lock;
76
};
L
Linus Torvalds 已提交
77 78 79 80 81 82 83 84 85 86

/* list of timers */
static LIST_HEAD(snd_timer_list);

/* list of slave instances */
static LIST_HEAD(snd_timer_slave_list);

/* lock for slave active lists */
static DEFINE_SPINLOCK(slave_active_lock);

87
static DEFINE_MUTEX(register_mutex);
L
Linus Torvalds 已提交
88

89 90 91
static int snd_timer_free(struct snd_timer *timer);
static int snd_timer_dev_free(struct snd_device *device);
static int snd_timer_dev_register(struct snd_device *device);
92
static int snd_timer_dev_disconnect(struct snd_device *device);
L
Linus Torvalds 已提交
93

94
static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
L
Linus Torvalds 已提交
95 96 97 98 99

/*
 * create a timer instance with the given owner string.
 * when timer is not NULL, increments the module counter
 */
100 101
static struct snd_timer_instance *snd_timer_instance_new(char *owner,
							 struct snd_timer *timer)
L
Linus Torvalds 已提交
102
{
103
	struct snd_timer_instance *timeri;
104
	timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
L
Linus Torvalds 已提交
105 106
	if (timeri == NULL)
		return NULL;
107
	timeri->owner = kstrdup(owner, GFP_KERNEL);
L
Linus Torvalds 已提交
108 109 110 111 112 113 114 115 116 117 118
	if (! timeri->owner) {
		kfree(timeri);
		return NULL;
	}
	INIT_LIST_HEAD(&timeri->open_list);
	INIT_LIST_HEAD(&timeri->active_list);
	INIT_LIST_HEAD(&timeri->ack_list);
	INIT_LIST_HEAD(&timeri->slave_list_head);
	INIT_LIST_HEAD(&timeri->slave_active_head);

	timeri->timer = timer;
119
	if (timer && !try_module_get(timer->module)) {
L
Linus Torvalds 已提交
120 121 122 123 124 125 126 127 128 129 130
		kfree(timeri->owner);
		kfree(timeri);
		return NULL;
	}

	return timeri;
}

/*
 * find a timer instance from the given timer id
 */
131
static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
L
Linus Torvalds 已提交
132
{
133
	struct snd_timer *timer = NULL;
L
Linus Torvalds 已提交
134

135
	list_for_each_entry(timer, &snd_timer_list, device_list) {
L
Linus Torvalds 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
		if (timer->tmr_class != tid->dev_class)
			continue;
		if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
		     timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
		    (timer->card == NULL ||
		     timer->card->number != tid->card))
			continue;
		if (timer->tmr_device != tid->device)
			continue;
		if (timer->tmr_subdevice != tid->subdevice)
			continue;
		return timer;
	}
	return NULL;
}

152
#ifdef CONFIG_MODULES
L
Linus Torvalds 已提交
153

154
static void snd_timer_request(struct snd_timer_id *tid)
L
Linus Torvalds 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
{
	switch (tid->dev_class) {
	case SNDRV_TIMER_CLASS_GLOBAL:
		if (tid->device < timer_limit)
			request_module("snd-timer-%i", tid->device);
		break;
	case SNDRV_TIMER_CLASS_CARD:
	case SNDRV_TIMER_CLASS_PCM:
		if (tid->card < snd_ecards_limit)
			request_module("snd-card-%i", tid->card);
		break;
	default:
		break;
	}
}

#endif

/*
 * look for a master instance matching with the slave id of the given slave.
 * when found, relink the open_link of the slave.
 *
 * call this with register_mutex down.
 */
179
static void snd_timer_check_slave(struct snd_timer_instance *slave)
L
Linus Torvalds 已提交
180
{
181 182
	struct snd_timer *timer;
	struct snd_timer_instance *master;
L
Linus Torvalds 已提交
183 184

	/* FIXME: it's really dumb to look up all entries.. */
185 186
	list_for_each_entry(timer, &snd_timer_list, device_list) {
		list_for_each_entry(master, &timer->open_list_head, open_list) {
L
Linus Torvalds 已提交
187 188
			if (slave->slave_class == master->slave_class &&
			    slave->slave_id == master->slave_id) {
189 190
				list_move_tail(&slave->open_list,
					       &master->slave_list_head);
L
Linus Torvalds 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
				spin_lock_irq(&slave_active_lock);
				slave->master = master;
				slave->timer = master->timer;
				spin_unlock_irq(&slave_active_lock);
				return;
			}
		}
	}
}

/*
 * look for slave instances matching with the slave id of the given master.
 * when found, relink the open_link of slaves.
 *
 * call this with register_mutex down.
 */
207
static void snd_timer_check_master(struct snd_timer_instance *master)
L
Linus Torvalds 已提交
208
{
209
	struct snd_timer_instance *slave, *tmp;
L
Linus Torvalds 已提交
210 211

	/* check all pending slaves */
212
	list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
L
Linus Torvalds 已提交
213 214
		if (slave->slave_class == master->slave_class &&
		    slave->slave_id == master->slave_id) {
215
			list_move_tail(&slave->open_list, &master->slave_list_head);
L
Linus Torvalds 已提交
216
			spin_lock_irq(&slave_active_lock);
217
			spin_lock(&master->timer->lock);
L
Linus Torvalds 已提交
218 219 220
			slave->master = master;
			slave->timer = master->timer;
			if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
C
Clemens Ladisch 已提交
221 222
				list_add_tail(&slave->active_list,
					      &master->slave_active_head);
223
			spin_unlock(&master->timer->lock);
L
Linus Torvalds 已提交
224 225 226 227 228 229 230 231 232
			spin_unlock_irq(&slave_active_lock);
		}
	}
}

/*
 * open a timer instance
 * when opening a master, the slave id must be here given.
 */
233 234
int snd_timer_open(struct snd_timer_instance **ti,
		   char *owner, struct snd_timer_id *tid,
L
Linus Torvalds 已提交
235 236
		   unsigned int slave_id)
{
237 238
	struct snd_timer *timer;
	struct snd_timer_instance *timeri = NULL;
C
Clemens Ladisch 已提交
239

L
Linus Torvalds 已提交
240 241 242 243
	if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
		/* open a slave instance */
		if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
		    tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
244 245
			pr_debug("ALSA: timer: invalid slave class %i\n",
				 tid->dev_sclass);
L
Linus Torvalds 已提交
246 247
			return -EINVAL;
		}
248
		mutex_lock(&register_mutex);
L
Linus Torvalds 已提交
249
		timeri = snd_timer_instance_new(owner, NULL);
250
		if (!timeri) {
251
			mutex_unlock(&register_mutex);
252 253
			return -ENOMEM;
		}
L
Linus Torvalds 已提交
254 255 256 257 258
		timeri->slave_class = tid->dev_sclass;
		timeri->slave_id = tid->device;
		timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
		list_add_tail(&timeri->open_list, &snd_timer_slave_list);
		snd_timer_check_slave(timeri);
259
		mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
260 261 262 263 264
		*ti = timeri;
		return 0;
	}

	/* open a master instance */
265
	mutex_lock(&register_mutex);
L
Linus Torvalds 已提交
266
	timer = snd_timer_find(tid);
267 268
#ifdef CONFIG_MODULES
	if (!timer) {
269
		mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
270
		snd_timer_request(tid);
271
		mutex_lock(&register_mutex);
L
Linus Torvalds 已提交
272 273 274
		timer = snd_timer_find(tid);
	}
#endif
275
	if (!timer) {
276
		mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
277 278
		return -ENODEV;
	}
279 280
	if (!list_empty(&timer->open_list_head)) {
		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 285 286 287 288
			return -EBUSY;
		}
	}
	timeri = snd_timer_instance_new(owner, timer);
	if (!timeri) {
289
		mutex_unlock(&register_mutex);
290 291
		return -ENOMEM;
	}
292 293 294
	/* take a card refcount for safe disconnection */
	if (timer->card)
		get_device(&timer->card->card_dev);
295 296 297 298 299 300
	timeri->slave_class = tid->dev_sclass;
	timeri->slave_id = slave_id;
	if (list_empty(&timer->open_list_head) && timer->hw.open)
		timer->hw.open(timer);
	list_add_tail(&timeri->open_list, &timer->open_list_head);
	snd_timer_check_master(timeri);
301
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
302 303 304 305 306 307 308
	*ti = timeri;
	return 0;
}

/*
 * close a timer instance
 */
309
int snd_timer_close(struct snd_timer_instance *timeri)
L
Linus Torvalds 已提交
310
{
311
	struct snd_timer *timer = NULL;
312
	struct snd_timer_instance *slave, *tmp;
L
Linus Torvalds 已提交
313

314
	if (snd_BUG_ON(!timeri))
315
		return -ENXIO;
L
Linus Torvalds 已提交
316

317 318 319
	mutex_lock(&register_mutex);
	list_del(&timeri->open_list);

L
Linus Torvalds 已提交
320 321 322
	/* force to stop the timer */
	snd_timer_stop(timeri);

323 324
	timer = timeri->timer;
	if (timer) {
L
Linus Torvalds 已提交
325 326 327 328 329 330 331 332
		/* wait, until the active callback is finished */
		spin_lock_irq(&timer->lock);
		while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
			spin_unlock_irq(&timer->lock);
			udelay(10);
			spin_lock_irq(&timer->lock);
		}
		spin_unlock_irq(&timer->lock);
333

L
Linus Torvalds 已提交
334
		/* remove slave links */
335 336
		spin_lock_irq(&slave_active_lock);
		spin_lock(&timer->lock);
337 338 339
		list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
					 open_list) {
			list_move_tail(&slave->open_list, &snd_timer_slave_list);
L
Linus Torvalds 已提交
340 341
			slave->master = NULL;
			slave->timer = NULL;
342 343
			list_del_init(&slave->ack_list);
			list_del_init(&slave->active_list);
L
Linus Torvalds 已提交
344
		}
345 346
		spin_unlock(&timer->lock);
		spin_unlock_irq(&slave_active_lock);
347 348 349 350

		/* slave doesn't need to release timer resources below */
		if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
			timer = NULL;
L
Linus Torvalds 已提交
351
	}
352

L
Linus Torvalds 已提交
353 354 355 356
	if (timeri->private_free)
		timeri->private_free(timeri);
	kfree(timeri->owner);
	kfree(timeri);
357 358 359 360 361 362 363

	if (timer) {
		if (list_empty(&timer->open_list_head) && timer->hw.close)
			timer->hw.close(timer);
		/* release a card refcount for safe disconnection */
		if (timer->card)
			put_device(&timer->card->card_dev);
364
		module_put(timer->module);
365 366 367
	}

	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
368 369 370
	return 0;
}

371
unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
L
Linus Torvalds 已提交
372
{
373
	struct snd_timer * timer;
L
Linus Torvalds 已提交
374 375 376 377 378 379 380 381 382 383 384

	if (timeri == NULL)
		return 0;
	if ((timer = timeri->timer) != NULL) {
		if (timer->hw.c_resolution)
			return timer->hw.c_resolution(timer);
		return timer->hw.resolution;
	}
	return 0;
}

385
static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
L
Linus Torvalds 已提交
386
{
387
	struct snd_timer *timer;
L
Linus Torvalds 已提交
388
	unsigned long resolution = 0;
389
	struct snd_timer_instance *ts;
L
Linus Torvalds 已提交
390 391
	struct timespec tstamp;

392
	if (timer_tstamp_monotonic)
T
Thomas Gleixner 已提交
393
		ktime_get_ts(&tstamp);
394 395
	else
		getnstimeofday(&tstamp);
396 397 398
	if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
		       event > SNDRV_TIMER_EVENT_PAUSE))
		return;
C
Clemens Ladisch 已提交
399 400
	if (event == SNDRV_TIMER_EVENT_START ||
	    event == SNDRV_TIMER_EVENT_CONTINUE)
L
Linus Torvalds 已提交
401 402
		resolution = snd_timer_resolution(ti);
	if (ti->ccallback)
403
		ti->ccallback(ti, event, &tstamp, resolution);
L
Linus Torvalds 已提交
404 405 406 407 408 409 410
	if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
		return;
	timer = ti->timer;
	if (timer == NULL)
		return;
	if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
		return;
411
	list_for_each_entry(ts, &ti->slave_active_head, active_list)
L
Linus Torvalds 已提交
412
		if (ts->ccallback)
413
			ts->ccallback(ts, event + 100, &tstamp, resolution);
L
Linus Torvalds 已提交
414 415
}

416 417 418
/* start/continue a master timer */
static int snd_timer_start1(struct snd_timer_instance *timeri,
			    bool start, unsigned long ticks)
L
Linus Torvalds 已提交
419
{
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
	struct snd_timer *timer;
	int result;
	unsigned long flags;

	timer = timeri->timer;
	if (!timer)
		return -EINVAL;

	spin_lock_irqsave(&timer->lock, flags);
	if (timer->card && timer->card->shutdown) {
		result = -ENODEV;
		goto unlock;
	}
	if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
			     SNDRV_TIMER_IFLG_START)) {
		result = -EBUSY;
		goto unlock;
	}

	if (start)
		timeri->ticks = timeri->cticks = ticks;
	else if (!timeri->cticks)
		timeri->cticks = 1;
	timeri->pticks = 0;

445
	list_move_tail(&timeri->active_list, &timer->active_list_head);
L
Linus Torvalds 已提交
446 447 448 449 450
	if (timer->running) {
		if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
			goto __start_now;
		timer->flags |= SNDRV_TIMER_FLG_RESCHED;
		timeri->flags |= SNDRV_TIMER_IFLG_START;
451
		result = 1; /* delayed start */
L
Linus Torvalds 已提交
452
	} else {
453 454
		if (start)
			timer->sticks = ticks;
L
Linus Torvalds 已提交
455 456 457 458
		timer->hw.start(timer);
	      __start_now:
		timer->running++;
		timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
459
		result = 0;
L
Linus Torvalds 已提交
460
	}
461 462 463 464 465
	snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
			  SNDRV_TIMER_EVENT_CONTINUE);
 unlock:
	spin_unlock_irqrestore(&timer->lock, flags);
	return result;
L
Linus Torvalds 已提交
466 467
}

468 469 470
/* start/continue a slave timer */
static int snd_timer_start_slave(struct snd_timer_instance *timeri,
				 bool start)
L
Linus Torvalds 已提交
471 472 473 474
{
	unsigned long flags;

	spin_lock_irqsave(&slave_active_lock, flags);
475 476 477 478
	if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
		spin_unlock_irqrestore(&slave_active_lock, flags);
		return -EBUSY;
	}
L
Linus Torvalds 已提交
479
	timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
480 481
	if (timeri->master && timeri->timer) {
		spin_lock(&timeri->timer->lock);
C
Clemens Ladisch 已提交
482 483
		list_add_tail(&timeri->active_list,
			      &timeri->master->slave_active_head);
484 485
		snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
				  SNDRV_TIMER_EVENT_CONTINUE);
486 487
		spin_unlock(&timeri->timer->lock);
	}
L
Linus Torvalds 已提交
488 489 490 491
	spin_unlock_irqrestore(&slave_active_lock, flags);
	return 1; /* delayed start */
}

492 493
/* stop/pause a master timer */
static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
L
Linus Torvalds 已提交
494
{
495
	struct snd_timer *timer;
496
	int result = 0;
L
Linus Torvalds 已提交
497 498 499 500 501 502
	unsigned long flags;

	timer = timeri->timer;
	if (!timer)
		return -EINVAL;
	spin_lock_irqsave(&timer->lock, flags);
503 504
	if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
			       SNDRV_TIMER_IFLG_START))) {
505 506
		result = -EBUSY;
		goto unlock;
507
	}
L
Linus Torvalds 已提交
508 509
	list_del_init(&timeri->ack_list);
	list_del_init(&timeri->active_list);
510 511 512 513 514
	if (timer->card && timer->card->shutdown)
		goto unlock;
	if (stop) {
		timeri->cticks = timeri->ticks;
		timeri->pticks = 0;
515
	}
L
Linus Torvalds 已提交
516 517 518 519 520 521 522 523 524 525 526 527
	if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
	    !(--timer->running)) {
		timer->hw.stop(timer);
		if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
			timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
			snd_timer_reschedule(timer, 0);
			if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
				timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
				timer->hw.start(timer);
			}
		}
	}
T
Takashi Iwai 已提交
528
	timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
529 530 531
	snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
			  SNDRV_TIMER_EVENT_CONTINUE);
 unlock:
L
Linus Torvalds 已提交
532
	spin_unlock_irqrestore(&timer->lock, flags);
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
	return result;
}

/* stop/pause a slave timer */
static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
{
	unsigned long flags;

	spin_lock_irqsave(&slave_active_lock, flags);
	if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
		spin_unlock_irqrestore(&slave_active_lock, flags);
		return -EBUSY;
	}
	timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
	if (timeri->timer) {
		spin_lock(&timeri->timer->lock);
		list_del_init(&timeri->ack_list);
		list_del_init(&timeri->active_list);
		snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
				  SNDRV_TIMER_EVENT_CONTINUE);
		spin_unlock(&timeri->timer->lock);
	}
	spin_unlock_irqrestore(&slave_active_lock, flags);
L
Linus Torvalds 已提交
556 557 558
	return 0;
}

559 560 561 562 563 564 565 566 567 568 569 570 571
/*
 *  start the timer instance
 */
int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
{
	if (timeri == NULL || ticks < 1)
		return -EINVAL;
	if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
		return snd_timer_start_slave(timeri, true);
	else
		return snd_timer_start1(timeri, true, ticks);
}

L
Linus Torvalds 已提交
572 573 574 575 576
/*
 * stop the timer instance.
 *
 * do not call this from the timer callback!
 */
577
int snd_timer_stop(struct snd_timer_instance *timeri)
L
Linus Torvalds 已提交
578
{
579 580 581 582
	if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
		return snd_timer_stop_slave(timeri, true);
	else
		return snd_timer_stop1(timeri, true);
L
Linus Torvalds 已提交
583 584 585 586 587
}

/*
 * start again..  the tick is kept.
 */
588
int snd_timer_continue(struct snd_timer_instance *timeri)
L
Linus Torvalds 已提交
589 590
{
	if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
591 592 593
		return snd_timer_start_slave(timeri, false);
	else
		return snd_timer_start1(timeri, false, 0);
L
Linus Torvalds 已提交
594 595 596 597 598
}

/*
 * pause.. remember the ticks left
 */
599
int snd_timer_pause(struct snd_timer_instance * timeri)
L
Linus Torvalds 已提交
600
{
601 602 603 604
	if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
		return snd_timer_stop_slave(timeri, false);
	else
		return snd_timer_stop1(timeri, false);
L
Linus Torvalds 已提交
605 606 607 608 609 610 611 612
}

/*
 * reschedule the timer
 *
 * start pending instances and check the scheduling ticks.
 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
 */
613
static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
L
Linus Torvalds 已提交
614
{
615
	struct snd_timer_instance *ti;
L
Linus Torvalds 已提交
616 617
	unsigned long ticks = ~0UL;

618
	list_for_each_entry(ti, &timer->active_list_head, active_list) {
L
Linus Torvalds 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
		if (ti->flags & SNDRV_TIMER_IFLG_START) {
			ti->flags &= ~SNDRV_TIMER_IFLG_START;
			ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
			timer->running++;
		}
		if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
			if (ticks > ti->cticks)
				ticks = ti->cticks;
		}
	}
	if (ticks == ~0UL) {
		timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
		return;
	}
	if (ticks > timer->hw.ticks)
		ticks = timer->hw.ticks;
	if (ticks_left != ticks)
		timer->flags |= SNDRV_TIMER_FLG_CHANGE;
	timer->sticks = ticks;
}

/*
 * timer tasklet
 *
 */
static void snd_timer_tasklet(unsigned long arg)
{
646 647
	struct snd_timer *timer = (struct snd_timer *) arg;
	struct snd_timer_instance *ti;
L
Linus Torvalds 已提交
648 649
	struct list_head *p;
	unsigned long resolution, ticks;
650
	unsigned long flags;
L
Linus Torvalds 已提交
651

652 653 654
	if (timer->card && timer->card->shutdown)
		return;

655
	spin_lock_irqsave(&timer->lock, flags);
L
Linus Torvalds 已提交
656 657 658
	/* now process all callbacks */
	while (!list_empty(&timer->sack_list_head)) {
		p = timer->sack_list_head.next;		/* get first item */
659
		ti = list_entry(p, struct snd_timer_instance, ack_list);
L
Linus Torvalds 已提交
660 661 662

		/* remove from ack_list and make empty */
		list_del_init(p);
C
Clemens Ladisch 已提交
663

L
Linus Torvalds 已提交
664 665 666 667 668 669 670 671 672 673 674
		ticks = ti->pticks;
		ti->pticks = 0;
		resolution = ti->resolution;

		ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
		spin_unlock(&timer->lock);
		if (ti->callback)
			ti->callback(ti, resolution, ticks);
		spin_lock(&timer->lock);
		ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
	}
675
	spin_unlock_irqrestore(&timer->lock, flags);
L
Linus Torvalds 已提交
676 677 678 679 680 681 682 683
}

/*
 * timer interrupt
 *
 * ticks_left is usually equal to timer->sticks.
 *
 */
684
void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
L
Linus Torvalds 已提交
685
{
686
	struct snd_timer_instance *ti, *ts, *tmp;
L
Linus Torvalds 已提交
687
	unsigned long resolution, ticks;
688
	struct list_head *p, *ack_list_head;
689
	unsigned long flags;
L
Linus Torvalds 已提交
690 691 692 693 694
	int use_tasklet = 0;

	if (timer == NULL)
		return;

695 696 697
	if (timer->card && timer->card->shutdown)
		return;

698
	spin_lock_irqsave(&timer->lock, flags);
L
Linus Torvalds 已提交
699 700 701 702 703 704 705 706

	/* remember the current resolution */
	if (timer->hw.c_resolution)
		resolution = timer->hw.c_resolution(timer);
	else
		resolution = timer->hw.resolution;

	/* loop for all active instances
707
	 * Here we cannot use list_for_each_entry because the active_list of a
C
Clemens Ladisch 已提交
708 709
	 * processed instance is relinked to done_list_head before the callback
	 * is called.
L
Linus Torvalds 已提交
710
	 */
711 712
	list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
				 active_list) {
L
Linus Torvalds 已提交
713 714 715 716 717 718 719 720 721 722 723 724 725 726
		if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
			continue;
		ti->pticks += ticks_left;
		ti->resolution = resolution;
		if (ti->cticks < ticks_left)
			ti->cticks = 0;
		else
			ti->cticks -= ticks_left;
		if (ti->cticks) /* not expired */
			continue;
		if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
			ti->cticks = ti->ticks;
		} else {
			ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
727 728
			--timer->running;
			list_del_init(&ti->active_list);
L
Linus Torvalds 已提交
729
		}
C
Clemens Ladisch 已提交
730 731 732 733 734 735 736
		if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
		    (ti->flags & SNDRV_TIMER_IFLG_FAST))
			ack_list_head = &timer->ack_list_head;
		else
			ack_list_head = &timer->sack_list_head;
		if (list_empty(&ti->ack_list))
			list_add_tail(&ti->ack_list, ack_list_head);
737
		list_for_each_entry(ts, &ti->slave_active_head, active_list) {
L
Linus Torvalds 已提交
738 739
			ts->pticks = ti->pticks;
			ts->resolution = resolution;
C
Clemens Ladisch 已提交
740 741
			if (list_empty(&ts->ack_list))
				list_add_tail(&ts->ack_list, ack_list_head);
L
Linus Torvalds 已提交
742 743 744
		}
	}
	if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
745
		snd_timer_reschedule(timer, timer->sticks);
L
Linus Torvalds 已提交
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
	if (timer->running) {
		if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
			timer->hw.stop(timer);
			timer->flags |= SNDRV_TIMER_FLG_CHANGE;
		}
		if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
		    (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
			/* restart timer */
			timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
			timer->hw.start(timer);
		}
	} else {
		timer->hw.stop(timer);
	}

	/* now process all fast callbacks */
	while (!list_empty(&timer->ack_list_head)) {
		p = timer->ack_list_head.next;		/* get first item */
764
		ti = list_entry(p, struct snd_timer_instance, ack_list);
C
Clemens Ladisch 已提交
765

L
Linus Torvalds 已提交
766 767
		/* remove from ack_list and make empty */
		list_del_init(p);
C
Clemens Ladisch 已提交
768

L
Linus Torvalds 已提交
769 770 771 772 773 774 775 776 777 778 779 780 781
		ticks = ti->pticks;
		ti->pticks = 0;

		ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
		spin_unlock(&timer->lock);
		if (ti->callback)
			ti->callback(ti, resolution, ticks);
		spin_lock(&timer->lock);
		ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
	}

	/* do we have any slow callbacks? */
	use_tasklet = !list_empty(&timer->sack_list_head);
782
	spin_unlock_irqrestore(&timer->lock, flags);
L
Linus Torvalds 已提交
783 784

	if (use_tasklet)
785
		tasklet_schedule(&timer->task_queue);
L
Linus Torvalds 已提交
786 787 788 789 790 791
}

/*

 */

792 793
int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
		  struct snd_timer **rtimer)
L
Linus Torvalds 已提交
794
{
795
	struct snd_timer *timer;
L
Linus Torvalds 已提交
796
	int err;
797
	static struct snd_device_ops ops = {
L
Linus Torvalds 已提交
798 799
		.dev_free = snd_timer_dev_free,
		.dev_register = snd_timer_dev_register,
800
		.dev_disconnect = snd_timer_dev_disconnect,
L
Linus Torvalds 已提交
801 802
	};

803 804 805 806
	if (snd_BUG_ON(!tid))
		return -EINVAL;
	if (rtimer)
		*rtimer = NULL;
807
	timer = kzalloc(sizeof(*timer), GFP_KERNEL);
808
	if (!timer)
L
Linus Torvalds 已提交
809 810 811 812 813 814 815 816 817 818 819 820 821
		return -ENOMEM;
	timer->tmr_class = tid->dev_class;
	timer->card = card;
	timer->tmr_device = tid->device;
	timer->tmr_subdevice = tid->subdevice;
	if (id)
		strlcpy(timer->id, id, sizeof(timer->id));
	INIT_LIST_HEAD(&timer->device_list);
	INIT_LIST_HEAD(&timer->open_list_head);
	INIT_LIST_HEAD(&timer->active_list_head);
	INIT_LIST_HEAD(&timer->ack_list_head);
	INIT_LIST_HEAD(&timer->sack_list_head);
	spin_lock_init(&timer->lock);
C
Clemens Ladisch 已提交
822 823
	tasklet_init(&timer->task_queue, snd_timer_tasklet,
		     (unsigned long)timer);
L
Linus Torvalds 已提交
824
	if (card != NULL) {
825
		timer->module = card->module;
C
Clemens Ladisch 已提交
826 827
		err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
		if (err < 0) {
L
Linus Torvalds 已提交
828 829 830 831
			snd_timer_free(timer);
			return err;
		}
	}
832 833
	if (rtimer)
		*rtimer = timer;
L
Linus Torvalds 已提交
834 835 836
	return 0;
}

837
static int snd_timer_free(struct snd_timer *timer)
L
Linus Torvalds 已提交
838
{
839 840
	if (!timer)
		return 0;
841 842 843 844 845

	mutex_lock(&register_mutex);
	if (! list_empty(&timer->open_list_head)) {
		struct list_head *p, *n;
		struct snd_timer_instance *ti;
846
		pr_warn("ALSA: timer %p is busy?\n", timer);
847 848 849 850 851 852 853 854 855
		list_for_each_safe(p, n, &timer->open_list_head) {
			list_del_init(p);
			ti = list_entry(p, struct snd_timer_instance, open_list);
			ti->timer = NULL;
		}
	}
	list_del(&timer->device_list);
	mutex_unlock(&register_mutex);

L
Linus Torvalds 已提交
856 857 858 859 860 861
	if (timer->private_free)
		timer->private_free(timer);
	kfree(timer);
	return 0;
}

862
static int snd_timer_dev_free(struct snd_device *device)
L
Linus Torvalds 已提交
863
{
864
	struct snd_timer *timer = device->device_data;
L
Linus Torvalds 已提交
865 866 867
	return snd_timer_free(timer);
}

868
static int snd_timer_dev_register(struct snd_device *dev)
L
Linus Torvalds 已提交
869
{
870 871
	struct snd_timer *timer = dev->device_data;
	struct snd_timer *timer1;
L
Linus Torvalds 已提交
872

873 874
	if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
		return -ENXIO;
L
Linus Torvalds 已提交
875 876 877 878
	if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
	    !timer->hw.resolution && timer->hw.c_resolution == NULL)
	    	return -EINVAL;

879
	mutex_lock(&register_mutex);
880
	list_for_each_entry(timer1, &snd_timer_list, device_list) {
L
Linus Torvalds 已提交
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
		if (timer1->tmr_class > timer->tmr_class)
			break;
		if (timer1->tmr_class < timer->tmr_class)
			continue;
		if (timer1->card && timer->card) {
			if (timer1->card->number > timer->card->number)
				break;
			if (timer1->card->number < timer->card->number)
				continue;
		}
		if (timer1->tmr_device > timer->tmr_device)
			break;
		if (timer1->tmr_device < timer->tmr_device)
			continue;
		if (timer1->tmr_subdevice > timer->tmr_subdevice)
			break;
		if (timer1->tmr_subdevice < timer->tmr_subdevice)
			continue;
		/* conflicts.. */
900
		mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
901 902
		return -EBUSY;
	}
903
	list_add_tail(&timer->device_list, &timer1->device_list);
904
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
905 906 907
	return 0;
}

908
static int snd_timer_dev_disconnect(struct snd_device *device)
L
Linus Torvalds 已提交
909
{
910
	struct snd_timer *timer = device->device_data;
911 912
	struct snd_timer_instance *ti;

913
	mutex_lock(&register_mutex);
914
	list_del_init(&timer->device_list);
915 916
	/* wake up pending sleepers */
	list_for_each_entry(ti, &timer->open_list_head, open_list) {
917 918
		if (ti->disconnect)
			ti->disconnect(ti);
919
	}
920
	mutex_unlock(&register_mutex);
921
	return 0;
L
Linus Torvalds 已提交
922 923
}

924
void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
L
Linus Torvalds 已提交
925 926 927
{
	unsigned long flags;
	unsigned long resolution = 0;
928
	struct snd_timer_instance *ti, *ts;
L
Linus Torvalds 已提交
929

930 931
	if (timer->card && timer->card->shutdown)
		return;
932 933
	if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
		return;
934 935 936
	if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
		       event > SNDRV_TIMER_EVENT_MRESUME))
		return;
L
Linus Torvalds 已提交
937
	spin_lock_irqsave(&timer->lock, flags);
938 939 940
	if (event == SNDRV_TIMER_EVENT_MSTART ||
	    event == SNDRV_TIMER_EVENT_MCONTINUE ||
	    event == SNDRV_TIMER_EVENT_MRESUME) {
L
Linus Torvalds 已提交
941 942 943 944 945
		if (timer->hw.c_resolution)
			resolution = timer->hw.c_resolution(timer);
		else
			resolution = timer->hw.resolution;
	}
946
	list_for_each_entry(ti, &timer->active_list_head, active_list) {
L
Linus Torvalds 已提交
947 948
		if (ti->ccallback)
			ti->ccallback(ti, event, tstamp, resolution);
949
		list_for_each_entry(ts, &ti->slave_active_head, active_list)
L
Linus Torvalds 已提交
950 951 952 953 954 955 956 957 958
			if (ts->ccallback)
				ts->ccallback(ts, event, tstamp, resolution);
	}
	spin_unlock_irqrestore(&timer->lock, flags);
}

/*
 * exported functions for global timers
 */
959
int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
L
Linus Torvalds 已提交
960
{
961
	struct snd_timer_id tid;
C
Clemens Ladisch 已提交
962

L
Linus Torvalds 已提交
963 964 965 966 967 968 969 970
	tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
	tid.card = -1;
	tid.device = device;
	tid.subdevice = 0;
	return snd_timer_new(NULL, id, &tid, rtimer);
}

971
int snd_timer_global_free(struct snd_timer *timer)
L
Linus Torvalds 已提交
972 973 974 975
{
	return snd_timer_free(timer);
}

976
int snd_timer_global_register(struct snd_timer *timer)
L
Linus Torvalds 已提交
977
{
978
	struct snd_device dev;
L
Linus Torvalds 已提交
979 980 981 982 983 984

	memset(&dev, 0, sizeof(dev));
	dev.device_data = timer;
	return snd_timer_dev_register(&dev);
}

C
Clemens Ladisch 已提交
985
/*
L
Linus Torvalds 已提交
986 987 988 989 990 991 992 993 994 995 996 997
 *  System timer
 */

struct snd_timer_system_private {
	struct timer_list tlist;
	unsigned long last_expires;
	unsigned long last_jiffies;
	unsigned long correction;
};

static void snd_timer_s_function(unsigned long data)
{
998
	struct snd_timer *timer = (struct snd_timer *)data;
L
Linus Torvalds 已提交
999 1000 1001
	struct snd_timer_system_private *priv = timer->private_data;
	unsigned long jiff = jiffies;
	if (time_after(jiff, priv->last_expires))
1002
		priv->correction += (long)jiff - (long)priv->last_expires;
L
Linus Torvalds 已提交
1003 1004 1005
	snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
}

1006
static int snd_timer_s_start(struct snd_timer * timer)
L
Linus Torvalds 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
{
	struct snd_timer_system_private *priv;
	unsigned long njiff;

	priv = (struct snd_timer_system_private *) timer->private_data;
	njiff = (priv->last_jiffies = jiffies);
	if (priv->correction > timer->sticks - 1) {
		priv->correction -= timer->sticks - 1;
		njiff++;
	} else {
		njiff += timer->sticks - priv->correction;
1018
		priv->correction = 0;
L
Linus Torvalds 已提交
1019
	}
1020 1021
	priv->last_expires = njiff;
	mod_timer(&priv->tlist, njiff);
L
Linus Torvalds 已提交
1022 1023 1024
	return 0;
}

1025
static int snd_timer_s_stop(struct snd_timer * timer)
L
Linus Torvalds 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
{
	struct snd_timer_system_private *priv;
	unsigned long jiff;

	priv = (struct snd_timer_system_private *) timer->private_data;
	del_timer(&priv->tlist);
	jiff = jiffies;
	if (time_before(jiff, priv->last_expires))
		timer->sticks = priv->last_expires - jiff;
	else
		timer->sticks = 1;
1037
	priv->correction = 0;
L
Linus Torvalds 已提交
1038 1039 1040
	return 0;
}

1041 1042 1043 1044 1045 1046 1047 1048 1049
static int snd_timer_s_close(struct snd_timer *timer)
{
	struct snd_timer_system_private *priv;

	priv = (struct snd_timer_system_private *)timer->private_data;
	del_timer_sync(&priv->tlist);
	return 0;
}

1050
static struct snd_timer_hardware snd_timer_system =
L
Linus Torvalds 已提交
1051 1052 1053 1054
{
	.flags =	SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
	.resolution =	1000000000L / HZ,
	.ticks =	10000000L,
1055
	.close =	snd_timer_s_close,
L
Linus Torvalds 已提交
1056 1057 1058 1059
	.start =	snd_timer_s_start,
	.stop =		snd_timer_s_stop
};

1060
static void snd_timer_free_system(struct snd_timer *timer)
L
Linus Torvalds 已提交
1061 1062 1063 1064 1065 1066
{
	kfree(timer->private_data);
}

static int snd_timer_register_system(void)
{
1067
	struct snd_timer *timer;
L
Linus Torvalds 已提交
1068 1069 1070
	struct snd_timer_system_private *priv;
	int err;

C
Clemens Ladisch 已提交
1071 1072
	err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
	if (err < 0)
L
Linus Torvalds 已提交
1073 1074 1075
		return err;
	strcpy(timer->name, "system timer");
	timer->hw = snd_timer_system;
1076
	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
L
Linus Torvalds 已提交
1077 1078 1079 1080
	if (priv == NULL) {
		snd_timer_free(timer);
		return -ENOMEM;
	}
1081
	setup_timer(&priv->tlist, snd_timer_s_function, (unsigned long) timer);
L
Linus Torvalds 已提交
1082 1083 1084 1085 1086
	timer->private_data = priv;
	timer->private_free = snd_timer_free_system;
	return snd_timer_global_register(timer);
}

1087
#ifdef CONFIG_SND_PROC_FS
L
Linus Torvalds 已提交
1088 1089 1090 1091
/*
 *  Info interface
 */

1092 1093
static void snd_timer_proc_read(struct snd_info_entry *entry,
				struct snd_info_buffer *buffer)
L
Linus Torvalds 已提交
1094
{
1095 1096
	struct snd_timer *timer;
	struct snd_timer_instance *ti;
L
Linus Torvalds 已提交
1097

1098
	mutex_lock(&register_mutex);
1099
	list_for_each_entry(timer, &snd_timer_list, device_list) {
1100 1101
		if (timer->card && timer->card->shutdown)
			continue;
L
Linus Torvalds 已提交
1102 1103 1104 1105 1106
		switch (timer->tmr_class) {
		case SNDRV_TIMER_CLASS_GLOBAL:
			snd_iprintf(buffer, "G%i: ", timer->tmr_device);
			break;
		case SNDRV_TIMER_CLASS_CARD:
C
Clemens Ladisch 已提交
1107 1108
			snd_iprintf(buffer, "C%i-%i: ",
				    timer->card->number, timer->tmr_device);
L
Linus Torvalds 已提交
1109 1110
			break;
		case SNDRV_TIMER_CLASS_PCM:
C
Clemens Ladisch 已提交
1111 1112
			snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
				    timer->tmr_device, timer->tmr_subdevice);
L
Linus Torvalds 已提交
1113 1114
			break;
		default:
C
Clemens Ladisch 已提交
1115 1116 1117
			snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
				    timer->card ? timer->card->number : -1,
				    timer->tmr_device, timer->tmr_subdevice);
L
Linus Torvalds 已提交
1118 1119 1120
		}
		snd_iprintf(buffer, "%s :", timer->name);
		if (timer->hw.resolution)
C
Clemens Ladisch 已提交
1121 1122 1123 1124
			snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
				    timer->hw.resolution / 1000,
				    timer->hw.resolution % 1000,
				    timer->hw.ticks);
L
Linus Torvalds 已提交
1125 1126 1127
		if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
			snd_iprintf(buffer, " SLAVE");
		snd_iprintf(buffer, "\n");
1128
		list_for_each_entry(ti, &timer->open_list_head, open_list)
C
Clemens Ladisch 已提交
1129 1130 1131 1132 1133
			snd_iprintf(buffer, "  Client %s : %s\n",
				    ti->owner ? ti->owner : "unknown",
				    ti->flags & (SNDRV_TIMER_IFLG_START |
						 SNDRV_TIMER_IFLG_RUNNING)
				    ? "running" : "stopped");
L
Linus Torvalds 已提交
1134
	}
1135
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
1136 1137
}

1138
static struct snd_info_entry *snd_timer_proc_entry;
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156

static void __init snd_timer_proc_init(void)
{
	struct snd_info_entry *entry;

	entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
	if (entry != NULL) {
		entry->c.text.read = snd_timer_proc_read;
		if (snd_info_register(entry) < 0) {
			snd_info_free_entry(entry);
			entry = NULL;
		}
	}
	snd_timer_proc_entry = entry;
}

static void __exit snd_timer_proc_done(void)
{
1157
	snd_info_free_entry(snd_timer_proc_entry);
1158
}
1159
#else /* !CONFIG_SND_PROC_FS */
1160 1161 1162 1163
#define snd_timer_proc_init()
#define snd_timer_proc_done()
#endif

L
Linus Torvalds 已提交
1164 1165 1166 1167
/*
 *  USER SPACE interface
 */

1168
static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
L
Linus Torvalds 已提交
1169 1170 1171
				     unsigned long resolution,
				     unsigned long ticks)
{
1172 1173
	struct snd_timer_user *tu = timeri->callback_data;
	struct snd_timer_read *r;
L
Linus Torvalds 已提交
1174
	int prev;
C
Clemens Ladisch 已提交
1175

L
Linus Torvalds 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
	spin_lock(&tu->qlock);
	if (tu->qused > 0) {
		prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
		r = &tu->queue[prev];
		if (r->resolution == resolution) {
			r->ticks += ticks;
			goto __wake;
		}
	}
	if (tu->qused >= tu->queue_size) {
		tu->overrun++;
	} else {
		r = &tu->queue[tu->qtail++];
		tu->qtail %= tu->queue_size;
		r->resolution = resolution;
		r->ticks = ticks;
		tu->qused++;
	}
      __wake:
	spin_unlock(&tu->qlock);
	kill_fasync(&tu->fasync, SIGIO, POLL_IN);
	wake_up(&tu->qchange_sleep);
}

1200 1201
static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
					    struct snd_timer_tread *tread)
L
Linus Torvalds 已提交
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
{
	if (tu->qused >= tu->queue_size) {
		tu->overrun++;
	} else {
		memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
		tu->qtail %= tu->queue_size;
		tu->qused++;
	}
}

1212 1213
static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
				     int event,
L
Linus Torvalds 已提交
1214 1215 1216
				     struct timespec *tstamp,
				     unsigned long resolution)
{
1217 1218
	struct snd_timer_user *tu = timeri->callback_data;
	struct snd_timer_tread r1;
1219
	unsigned long flags;
L
Linus Torvalds 已提交
1220

C
Clemens Ladisch 已提交
1221 1222
	if (event >= SNDRV_TIMER_EVENT_START &&
	    event <= SNDRV_TIMER_EVENT_PAUSE)
L
Linus Torvalds 已提交
1223 1224 1225 1226 1227 1228
		tu->tstamp = *tstamp;
	if ((tu->filter & (1 << event)) == 0 || !tu->tread)
		return;
	r1.event = event;
	r1.tstamp = *tstamp;
	r1.val = resolution;
1229
	spin_lock_irqsave(&tu->qlock, flags);
L
Linus Torvalds 已提交
1230
	snd_timer_user_append_to_tqueue(tu, &r1);
1231
	spin_unlock_irqrestore(&tu->qlock, flags);
L
Linus Torvalds 已提交
1232 1233 1234 1235
	kill_fasync(&tu->fasync, SIGIO, POLL_IN);
	wake_up(&tu->qchange_sleep);
}

1236 1237 1238 1239 1240 1241 1242 1243
static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
{
	struct snd_timer_user *tu = timeri->callback_data;

	tu->disconnected = true;
	wake_up(&tu->qchange_sleep);
}

1244
static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
L
Linus Torvalds 已提交
1245 1246 1247
				      unsigned long resolution,
				      unsigned long ticks)
{
1248 1249
	struct snd_timer_user *tu = timeri->callback_data;
	struct snd_timer_tread *r, r1;
L
Linus Torvalds 已提交
1250 1251 1252
	struct timespec tstamp;
	int prev, append = 0;

T
Takashi Iwai 已提交
1253
	memset(&tstamp, 0, sizeof(tstamp));
L
Linus Torvalds 已提交
1254
	spin_lock(&tu->qlock);
C
Clemens Ladisch 已提交
1255 1256
	if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
			   (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
L
Linus Torvalds 已提交
1257 1258 1259
		spin_unlock(&tu->qlock);
		return;
	}
1260 1261
	if (tu->last_resolution != resolution || ticks > 0) {
		if (timer_tstamp_monotonic)
T
Thomas Gleixner 已提交
1262
			ktime_get_ts(&tstamp);
1263 1264 1265
		else
			getnstimeofday(&tstamp);
	}
C
Clemens Ladisch 已提交
1266 1267
	if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
	    tu->last_resolution != resolution) {
L
Linus Torvalds 已提交
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
		r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
		r1.tstamp = tstamp;
		r1.val = resolution;
		snd_timer_user_append_to_tqueue(tu, &r1);
		tu->last_resolution = resolution;
		append++;
	}
	if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
		goto __wake;
	if (ticks == 0)
		goto __wake;
	if (tu->qused > 0) {
		prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
		r = &tu->tqueue[prev];
		if (r->event == SNDRV_TIMER_EVENT_TICK) {
			r->tstamp = tstamp;
			r->val += ticks;
			append++;
			goto __wake;
		}
	}
	r1.event = SNDRV_TIMER_EVENT_TICK;
	r1.tstamp = tstamp;
	r1.val = ticks;
	snd_timer_user_append_to_tqueue(tu, &r1);
	append++;
      __wake:
	spin_unlock(&tu->qlock);
	if (append == 0)
		return;
	kill_fasync(&tu->fasync, SIGIO, POLL_IN);
	wake_up(&tu->qchange_sleep);
}

static int snd_timer_user_open(struct inode *inode, struct file *file)
{
1304
	struct snd_timer_user *tu;
T
Takashi Iwai 已提交
1305 1306 1307 1308 1309
	int err;

	err = nonseekable_open(inode, file);
	if (err < 0)
		return err;
C
Clemens Ladisch 已提交
1310

1311
	tu = kzalloc(sizeof(*tu), GFP_KERNEL);
L
Linus Torvalds 已提交
1312 1313 1314 1315
	if (tu == NULL)
		return -ENOMEM;
	spin_lock_init(&tu->qlock);
	init_waitqueue_head(&tu->qchange_sleep);
1316
	mutex_init(&tu->ioctl_lock);
L
Linus Torvalds 已提交
1317 1318
	tu->ticks = 1;
	tu->queue_size = 128;
1319
	tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
C
Clemens Ladisch 已提交
1320
			    GFP_KERNEL);
L
Linus Torvalds 已提交
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
	if (tu->queue == NULL) {
		kfree(tu);
		return -ENOMEM;
	}
	file->private_data = tu;
	return 0;
}

static int snd_timer_user_release(struct inode *inode, struct file *file)
{
1331
	struct snd_timer_user *tu;
L
Linus Torvalds 已提交
1332 1333 1334 1335

	if (file->private_data) {
		tu = file->private_data;
		file->private_data = NULL;
1336
		mutex_lock(&tu->ioctl_lock);
L
Linus Torvalds 已提交
1337 1338
		if (tu->timeri)
			snd_timer_close(tu->timeri);
1339
		mutex_unlock(&tu->ioctl_lock);
L
Linus Torvalds 已提交
1340 1341 1342 1343 1344 1345 1346
		kfree(tu->queue);
		kfree(tu->tqueue);
		kfree(tu);
	}
	return 0;
}

1347
static void snd_timer_user_zero_id(struct snd_timer_id *id)
L
Linus Torvalds 已提交
1348 1349 1350 1351 1352 1353 1354 1355
{
	id->dev_class = SNDRV_TIMER_CLASS_NONE;
	id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
	id->card = -1;
	id->device = -1;
	id->subdevice = -1;
}

1356
static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
L
Linus Torvalds 已提交
1357 1358 1359 1360 1361 1362 1363 1364
{
	id->dev_class = timer->tmr_class;
	id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
	id->card = timer->card ? timer->card->number : -1;
	id->device = timer->tmr_device;
	id->subdevice = timer->tmr_subdevice;
}

1365
static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
L
Linus Torvalds 已提交
1366
{
1367 1368
	struct snd_timer_id id;
	struct snd_timer *timer;
L
Linus Torvalds 已提交
1369
	struct list_head *p;
C
Clemens Ladisch 已提交
1370

L
Linus Torvalds 已提交
1371 1372
	if (copy_from_user(&id, _tid, sizeof(id)))
		return -EFAULT;
1373
	mutex_lock(&register_mutex);
L
Linus Torvalds 已提交
1374 1375 1376 1377
	if (id.dev_class < 0) {		/* first item */
		if (list_empty(&snd_timer_list))
			snd_timer_user_zero_id(&id);
		else {
1378
			timer = list_entry(snd_timer_list.next,
1379
					   struct snd_timer, device_list);
L
Linus Torvalds 已提交
1380 1381 1382 1383 1384 1385 1386
			snd_timer_user_copy_id(&id, timer);
		}
	} else {
		switch (id.dev_class) {
		case SNDRV_TIMER_CLASS_GLOBAL:
			id.device = id.device < 0 ? 0 : id.device + 1;
			list_for_each(p, &snd_timer_list) {
1387
				timer = list_entry(p, struct snd_timer, device_list);
L
Linus Torvalds 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
				if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
					snd_timer_user_copy_id(&id, timer);
					break;
				}
				if (timer->tmr_device >= id.device) {
					snd_timer_user_copy_id(&id, timer);
					break;
				}
			}
			if (p == &snd_timer_list)
				snd_timer_user_zero_id(&id);
			break;
		case SNDRV_TIMER_CLASS_CARD:
		case SNDRV_TIMER_CLASS_PCM:
			if (id.card < 0) {
				id.card = 0;
			} else {
				if (id.card < 0) {
					id.card = 0;
				} else {
					if (id.device < 0) {
						id.device = 0;
					} else {
C
Clemens Ladisch 已提交
1411 1412 1413 1414 1415
						if (id.subdevice < 0) {
							id.subdevice = 0;
						} else {
							id.subdevice++;
						}
L
Linus Torvalds 已提交
1416 1417 1418 1419
					}
				}
			}
			list_for_each(p, &snd_timer_list) {
1420
				timer = list_entry(p, struct snd_timer, device_list);
L
Linus Torvalds 已提交
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
				if (timer->tmr_class > id.dev_class) {
					snd_timer_user_copy_id(&id, timer);
					break;
				}
				if (timer->tmr_class < id.dev_class)
					continue;
				if (timer->card->number > id.card) {
					snd_timer_user_copy_id(&id, timer);
					break;
				}
				if (timer->card->number < id.card)
					continue;
				if (timer->tmr_device > id.device) {
					snd_timer_user_copy_id(&id, timer);
					break;
				}
				if (timer->tmr_device < id.device)
					continue;
				if (timer->tmr_subdevice > id.subdevice) {
					snd_timer_user_copy_id(&id, timer);
					break;
				}
				if (timer->tmr_subdevice < id.subdevice)
					continue;
				snd_timer_user_copy_id(&id, timer);
				break;
			}
			if (p == &snd_timer_list)
				snd_timer_user_zero_id(&id);
			break;
		default:
			snd_timer_user_zero_id(&id);
		}
	}
1455
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
1456 1457 1458
	if (copy_to_user(_tid, &id, sizeof(*_tid)))
		return -EFAULT;
	return 0;
C
Clemens Ladisch 已提交
1459
}
L
Linus Torvalds 已提交
1460

C
Clemens Ladisch 已提交
1461
static int snd_timer_user_ginfo(struct file *file,
1462
				struct snd_timer_ginfo __user *_ginfo)
L
Linus Torvalds 已提交
1463
{
1464 1465 1466
	struct snd_timer_ginfo *ginfo;
	struct snd_timer_id tid;
	struct snd_timer *t;
L
Linus Torvalds 已提交
1467 1468 1469
	struct list_head *p;
	int err = 0;

L
Li Zefan 已提交
1470 1471 1472 1473
	ginfo = memdup_user(_ginfo, sizeof(*ginfo));
	if (IS_ERR(ginfo))
		return PTR_ERR(ginfo);

L
Linus Torvalds 已提交
1474 1475 1476
	tid = ginfo->tid;
	memset(ginfo, 0, sizeof(*ginfo));
	ginfo->tid = tid;
1477
	mutex_lock(&register_mutex);
L
Linus Torvalds 已提交
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
	t = snd_timer_find(&tid);
	if (t != NULL) {
		ginfo->card = t->card ? t->card->number : -1;
		if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
			ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
		strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
		strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
		ginfo->resolution = t->hw.resolution;
		if (t->hw.resolution_min > 0) {
			ginfo->resolution_min = t->hw.resolution_min;
			ginfo->resolution_max = t->hw.resolution_max;
		}
		list_for_each(p, &t->open_list_head) {
			ginfo->clients++;
		}
	} else {
		err = -ENODEV;
	}
1496
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
1497 1498 1499 1500 1501 1502
	if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
		err = -EFAULT;
	kfree(ginfo);
	return err;
}

1503
static int timer_set_gparams(struct snd_timer_gparams *gparams)
L
Linus Torvalds 已提交
1504
{
1505
	struct snd_timer *t;
L
Linus Torvalds 已提交
1506 1507
	int err;

1508
	mutex_lock(&register_mutex);
1509
	t = snd_timer_find(&gparams->tid);
C
Clemens Ladisch 已提交
1510
	if (!t) {
L
Linus Torvalds 已提交
1511
		err = -ENODEV;
C
Clemens Ladisch 已提交
1512 1513 1514 1515 1516 1517 1518 1519 1520
		goto _error;
	}
	if (!list_empty(&t->open_list_head)) {
		err = -EBUSY;
		goto _error;
	}
	if (!t->hw.set_period) {
		err = -ENOSYS;
		goto _error;
L
Linus Torvalds 已提交
1521
	}
1522
	err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
C
Clemens Ladisch 已提交
1523
_error:
1524
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
1525 1526 1527
	return err;
}

1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
static int snd_timer_user_gparams(struct file *file,
				  struct snd_timer_gparams __user *_gparams)
{
	struct snd_timer_gparams gparams;

	if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
		return -EFAULT;
	return timer_set_gparams(&gparams);
}

C
Clemens Ladisch 已提交
1538
static int snd_timer_user_gstatus(struct file *file,
1539
				  struct snd_timer_gstatus __user *_gstatus)
L
Linus Torvalds 已提交
1540
{
1541 1542 1543
	struct snd_timer_gstatus gstatus;
	struct snd_timer_id tid;
	struct snd_timer *t;
L
Linus Torvalds 已提交
1544 1545 1546 1547 1548 1549 1550
	int err = 0;

	if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
		return -EFAULT;
	tid = gstatus.tid;
	memset(&gstatus, 0, sizeof(gstatus));
	gstatus.tid = tid;
1551
	mutex_lock(&register_mutex);
L
Linus Torvalds 已提交
1552 1553 1554 1555 1556 1557 1558
	t = snd_timer_find(&tid);
	if (t != NULL) {
		if (t->hw.c_resolution)
			gstatus.resolution = t->hw.c_resolution(t);
		else
			gstatus.resolution = t->hw.resolution;
		if (t->hw.precise_resolution) {
C
Clemens Ladisch 已提交
1559 1560
			t->hw.precise_resolution(t, &gstatus.resolution_num,
						 &gstatus.resolution_den);
L
Linus Torvalds 已提交
1561 1562 1563 1564 1565 1566 1567
		} else {
			gstatus.resolution_num = gstatus.resolution;
			gstatus.resolution_den = 1000000000uL;
		}
	} else {
		err = -ENODEV;
	}
1568
	mutex_unlock(&register_mutex);
L
Linus Torvalds 已提交
1569 1570 1571 1572 1573
	if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
		err = -EFAULT;
	return err;
}

C
Clemens Ladisch 已提交
1574
static int snd_timer_user_tselect(struct file *file,
1575
				  struct snd_timer_select __user *_tselect)
L
Linus Torvalds 已提交
1576
{
1577 1578
	struct snd_timer_user *tu;
	struct snd_timer_select tselect;
L
Linus Torvalds 已提交
1579
	char str[32];
1580
	int err = 0;
C
Clemens Ladisch 已提交
1581

L
Linus Torvalds 已提交
1582
	tu = file->private_data;
1583
	if (tu->timeri) {
L
Linus Torvalds 已提交
1584
		snd_timer_close(tu->timeri);
1585 1586 1587 1588 1589 1590
		tu->timeri = NULL;
	}
	if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
		err = -EFAULT;
		goto __err;
	}
L
Linus Torvalds 已提交
1591 1592 1593
	sprintf(str, "application %i", current->pid);
	if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
		tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
C
Clemens Ladisch 已提交
1594 1595
	err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
	if (err < 0)
1596
		goto __err;
L
Linus Torvalds 已提交
1597

1598 1599 1600 1601
	kfree(tu->queue);
	tu->queue = NULL;
	kfree(tu->tqueue);
	tu->tqueue = NULL;
L
Linus Torvalds 已提交
1602
	if (tu->tread) {
1603
		tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
C
Clemens Ladisch 已提交
1604
				     GFP_KERNEL);
1605 1606
		if (tu->tqueue == NULL)
			err = -ENOMEM;
L
Linus Torvalds 已提交
1607
	} else {
1608
		tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
C
Clemens Ladisch 已提交
1609
				    GFP_KERNEL);
1610 1611
		if (tu->queue == NULL)
			err = -ENOMEM;
L
Linus Torvalds 已提交
1612
	}
C
Clemens Ladisch 已提交
1613

1614 1615 1616 1617 1618
      	if (err < 0) {
		snd_timer_close(tu->timeri);
      		tu->timeri = NULL;
      	} else {
		tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
C
Clemens Ladisch 已提交
1619 1620
		tu->timeri->callback = tu->tread
			? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1621 1622
		tu->timeri->ccallback = snd_timer_user_ccallback;
		tu->timeri->callback_data = (void *)tu;
1623
		tu->timeri->disconnect = snd_timer_user_disconnect;
1624 1625 1626 1627
	}

      __err:
	return err;
L
Linus Torvalds 已提交
1628 1629
}

C
Clemens Ladisch 已提交
1630
static int snd_timer_user_info(struct file *file,
1631
			       struct snd_timer_info __user *_info)
L
Linus Torvalds 已提交
1632
{
1633 1634 1635
	struct snd_timer_user *tu;
	struct snd_timer_info *info;
	struct snd_timer *t;
L
Linus Torvalds 已提交
1636 1637 1638
	int err = 0;

	tu = file->private_data;
1639 1640
	if (!tu->timeri)
		return -EBADFD;
L
Linus Torvalds 已提交
1641
	t = tu->timeri->timer;
1642 1643
	if (!t)
		return -EBADFD;
L
Linus Torvalds 已提交
1644

1645
	info = kzalloc(sizeof(*info), GFP_KERNEL);
L
Linus Torvalds 已提交
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
	if (! info)
		return -ENOMEM;
	info->card = t->card ? t->card->number : -1;
	if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
		info->flags |= SNDRV_TIMER_FLG_SLAVE;
	strlcpy(info->id, t->id, sizeof(info->id));
	strlcpy(info->name, t->name, sizeof(info->name));
	info->resolution = t->hw.resolution;
	if (copy_to_user(_info, info, sizeof(*_info)))
		err = -EFAULT;
	kfree(info);
	return err;
}

C
Clemens Ladisch 已提交
1660
static int snd_timer_user_params(struct file *file,
1661
				 struct snd_timer_params __user *_params)
L
Linus Torvalds 已提交
1662
{
1663 1664 1665 1666 1667
	struct snd_timer_user *tu;
	struct snd_timer_params params;
	struct snd_timer *t;
	struct snd_timer_read *tr;
	struct snd_timer_tread *ttr;
L
Linus Torvalds 已提交
1668
	int err;
C
Clemens Ladisch 已提交
1669

L
Linus Torvalds 已提交
1670
	tu = file->private_data;
1671 1672
	if (!tu->timeri)
		return -EBADFD;
L
Linus Torvalds 已提交
1673
	t = tu->timeri->timer;
1674 1675
	if (!t)
		return -EBADFD;
L
Linus Torvalds 已提交
1676 1677 1678 1679 1680 1681
	if (copy_from_user(&params, _params, sizeof(params)))
		return -EFAULT;
	if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
		err = -EINVAL;
		goto _end;
	}
C
Clemens Ladisch 已提交
1682 1683
	if (params.queue_size > 0 &&
	    (params.queue_size < 32 || params.queue_size > 1024)) {
L
Linus Torvalds 已提交
1684 1685 1686 1687 1688 1689 1690 1691 1692
		err = -EINVAL;
		goto _end;
	}
	if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
			      (1<<SNDRV_TIMER_EVENT_TICK)|
			      (1<<SNDRV_TIMER_EVENT_START)|
			      (1<<SNDRV_TIMER_EVENT_STOP)|
			      (1<<SNDRV_TIMER_EVENT_CONTINUE)|
			      (1<<SNDRV_TIMER_EVENT_PAUSE)|
1693 1694
			      (1<<SNDRV_TIMER_EVENT_SUSPEND)|
			      (1<<SNDRV_TIMER_EVENT_RESUME)|
L
Linus Torvalds 已提交
1695 1696 1697
			      (1<<SNDRV_TIMER_EVENT_MSTART)|
			      (1<<SNDRV_TIMER_EVENT_MSTOP)|
			      (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1698 1699
			      (1<<SNDRV_TIMER_EVENT_MPAUSE)|
			      (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1700
			      (1<<SNDRV_TIMER_EVENT_MRESUME))) {
L
Linus Torvalds 已提交
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
		err = -EINVAL;
		goto _end;
	}
	snd_timer_stop(tu->timeri);
	spin_lock_irq(&t->lock);
	tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
			       SNDRV_TIMER_IFLG_EXCLUSIVE|
			       SNDRV_TIMER_IFLG_EARLY_EVENT);
	if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
		tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
	if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
		tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
	if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
		tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
	spin_unlock_irq(&t->lock);
C
Clemens Ladisch 已提交
1716 1717
	if (params.queue_size > 0 &&
	    (unsigned int)tu->queue_size != params.queue_size) {
L
Linus Torvalds 已提交
1718
		if (tu->tread) {
C
Clemens Ladisch 已提交
1719 1720
			ttr = kmalloc(params.queue_size * sizeof(*ttr),
				      GFP_KERNEL);
L
Linus Torvalds 已提交
1721 1722 1723 1724 1725 1726
			if (ttr) {
				kfree(tu->tqueue);
				tu->queue_size = params.queue_size;
				tu->tqueue = ttr;
			}
		} else {
C
Clemens Ladisch 已提交
1727 1728
			tr = kmalloc(params.queue_size * sizeof(*tr),
				     GFP_KERNEL);
L
Linus Torvalds 已提交
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
			if (tr) {
				kfree(tu->queue);
				tu->queue_size = params.queue_size;
				tu->queue = tr;
			}
		}
	}
	tu->qhead = tu->qtail = tu->qused = 0;
	if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
		if (tu->tread) {
1739
			struct snd_timer_tread tread;
1740
			memset(&tread, 0, sizeof(tread));
L
Linus Torvalds 已提交
1741 1742 1743 1744 1745 1746
			tread.event = SNDRV_TIMER_EVENT_EARLY;
			tread.tstamp.tv_sec = 0;
			tread.tstamp.tv_nsec = 0;
			tread.val = 0;
			snd_timer_user_append_to_tqueue(tu, &tread);
		} else {
1747
			struct snd_timer_read *r = &tu->queue[0];
L
Linus Torvalds 已提交
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
			r->resolution = 0;
			r->ticks = 0;
			tu->qused++;
			tu->qtail++;
		}
	}
	tu->filter = params.filter;
	tu->ticks = params.ticks;
	err = 0;
 _end:
	if (copy_to_user(_params, &params, sizeof(params)))
		return -EFAULT;
	return err;
}

C
Clemens Ladisch 已提交
1763
static int snd_timer_user_status(struct file *file,
1764
				 struct snd_timer_status __user *_status)
L
Linus Torvalds 已提交
1765
{
1766 1767
	struct snd_timer_user *tu;
	struct snd_timer_status status;
C
Clemens Ladisch 已提交
1768

L
Linus Torvalds 已提交
1769
	tu = file->private_data;
1770 1771
	if (!tu->timeri)
		return -EBADFD;
L
Linus Torvalds 已提交
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
	memset(&status, 0, sizeof(status));
	status.tstamp = tu->tstamp;
	status.resolution = snd_timer_resolution(tu->timeri);
	status.lost = tu->timeri->lost;
	status.overrun = tu->overrun;
	spin_lock_irq(&tu->qlock);
	status.queue = tu->qused;
	spin_unlock_irq(&tu->qlock);
	if (copy_to_user(_status, &status, sizeof(status)))
		return -EFAULT;
	return 0;
}

static int snd_timer_user_start(struct file *file)
{
	int err;
1788
	struct snd_timer_user *tu;
C
Clemens Ladisch 已提交
1789

L
Linus Torvalds 已提交
1790
	tu = file->private_data;
1791 1792
	if (!tu->timeri)
		return -EBADFD;
L
Linus Torvalds 已提交
1793 1794 1795 1796 1797 1798 1799 1800 1801
	snd_timer_stop(tu->timeri);
	tu->timeri->lost = 0;
	tu->last_resolution = 0;
	return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
}

static int snd_timer_user_stop(struct file *file)
{
	int err;
1802
	struct snd_timer_user *tu;
C
Clemens Ladisch 已提交
1803

L
Linus Torvalds 已提交
1804
	tu = file->private_data;
1805 1806
	if (!tu->timeri)
		return -EBADFD;
L
Linus Torvalds 已提交
1807 1808 1809 1810 1811 1812
	return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
}

static int snd_timer_user_continue(struct file *file)
{
	int err;
1813
	struct snd_timer_user *tu;
C
Clemens Ladisch 已提交
1814

L
Linus Torvalds 已提交
1815
	tu = file->private_data;
1816 1817
	if (!tu->timeri)
		return -EBADFD;
L
Linus Torvalds 已提交
1818 1819 1820 1821
	tu->timeri->lost = 0;
	return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
}

T
Takashi Iwai 已提交
1822 1823 1824
static int snd_timer_user_pause(struct file *file)
{
	int err;
1825
	struct snd_timer_user *tu;
C
Clemens Ladisch 已提交
1826

T
Takashi Iwai 已提交
1827
	tu = file->private_data;
1828 1829
	if (!tu->timeri)
		return -EBADFD;
1830
	return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
T
Takashi Iwai 已提交
1831 1832
}

1833 1834 1835 1836 1837 1838 1839
enum {
	SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
	SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
	SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
	SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
};

1840
static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
C
Clemens Ladisch 已提交
1841
				 unsigned long arg)
L
Linus Torvalds 已提交
1842
{
1843
	struct snd_timer_user *tu;
L
Linus Torvalds 已提交
1844 1845
	void __user *argp = (void __user *)arg;
	int __user *p = argp;
C
Clemens Ladisch 已提交
1846

L
Linus Torvalds 已提交
1847 1848 1849 1850 1851 1852 1853 1854 1855
	tu = file->private_data;
	switch (cmd) {
	case SNDRV_TIMER_IOCTL_PVERSION:
		return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
	case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
		return snd_timer_user_next_device(argp);
	case SNDRV_TIMER_IOCTL_TREAD:
	{
		int xarg;
C
Clemens Ladisch 已提交
1856

1857
		if (tu->timeri)	/* too late */
L
Linus Torvalds 已提交
1858
			return -EBUSY;
1859
		if (get_user(xarg, p))
L
Linus Torvalds 已提交
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
			return -EFAULT;
		tu->tread = xarg ? 1 : 0;
		return 0;
	}
	case SNDRV_TIMER_IOCTL_GINFO:
		return snd_timer_user_ginfo(file, argp);
	case SNDRV_TIMER_IOCTL_GPARAMS:
		return snd_timer_user_gparams(file, argp);
	case SNDRV_TIMER_IOCTL_GSTATUS:
		return snd_timer_user_gstatus(file, argp);
	case SNDRV_TIMER_IOCTL_SELECT:
		return snd_timer_user_tselect(file, argp);
	case SNDRV_TIMER_IOCTL_INFO:
		return snd_timer_user_info(file, argp);
	case SNDRV_TIMER_IOCTL_PARAMS:
		return snd_timer_user_params(file, argp);
	case SNDRV_TIMER_IOCTL_STATUS:
		return snd_timer_user_status(file, argp);
	case SNDRV_TIMER_IOCTL_START:
1879
	case SNDRV_TIMER_IOCTL_START_OLD:
L
Linus Torvalds 已提交
1880 1881
		return snd_timer_user_start(file);
	case SNDRV_TIMER_IOCTL_STOP:
1882
	case SNDRV_TIMER_IOCTL_STOP_OLD:
L
Linus Torvalds 已提交
1883 1884
		return snd_timer_user_stop(file);
	case SNDRV_TIMER_IOCTL_CONTINUE:
1885
	case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
L
Linus Torvalds 已提交
1886
		return snd_timer_user_continue(file);
T
Takashi Iwai 已提交
1887
	case SNDRV_TIMER_IOCTL_PAUSE:
1888
	case SNDRV_TIMER_IOCTL_PAUSE_OLD:
T
Takashi Iwai 已提交
1889
		return snd_timer_user_pause(file);
L
Linus Torvalds 已提交
1890 1891 1892 1893
	}
	return -ENOTTY;
}

1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
				 unsigned long arg)
{
	struct snd_timer_user *tu = file->private_data;
	long ret;

	mutex_lock(&tu->ioctl_lock);
	ret = __snd_timer_user_ioctl(file, cmd, arg);
	mutex_unlock(&tu->ioctl_lock);
	return ret;
}

L
Linus Torvalds 已提交
1906 1907
static int snd_timer_user_fasync(int fd, struct file * file, int on)
{
1908
	struct snd_timer_user *tu;
C
Clemens Ladisch 已提交
1909

L
Linus Torvalds 已提交
1910
	tu = file->private_data;
1911
	return fasync_helper(fd, file, on, &tu->fasync);
L
Linus Torvalds 已提交
1912 1913
}

C
Clemens Ladisch 已提交
1914 1915
static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
				   size_t count, loff_t *offset)
L
Linus Torvalds 已提交
1916
{
1917
	struct snd_timer_user *tu;
L
Linus Torvalds 已提交
1918
	long result = 0, unit;
1919
	int qhead;
L
Linus Torvalds 已提交
1920
	int err = 0;
C
Clemens Ladisch 已提交
1921

L
Linus Torvalds 已提交
1922
	tu = file->private_data;
1923
	unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
L
Linus Torvalds 已提交
1924 1925 1926 1927 1928 1929 1930
	spin_lock_irq(&tu->qlock);
	while ((long)count - result >= unit) {
		while (!tu->qused) {
			wait_queue_t wait;

			if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
				err = -EAGAIN;
1931
				goto _error;
L
Linus Torvalds 已提交
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
			}

			set_current_state(TASK_INTERRUPTIBLE);
			init_waitqueue_entry(&wait, current);
			add_wait_queue(&tu->qchange_sleep, &wait);

			spin_unlock_irq(&tu->qlock);
			schedule();
			spin_lock_irq(&tu->qlock);

			remove_wait_queue(&tu->qchange_sleep, &wait);

1944 1945
			if (tu->disconnected) {
				err = -ENODEV;
1946
				goto _error;
1947
			}
L
Linus Torvalds 已提交
1948 1949
			if (signal_pending(current)) {
				err = -ERESTARTSYS;
1950
				goto _error;
L
Linus Torvalds 已提交
1951 1952 1953
			}
		}

1954 1955
		qhead = tu->qhead++;
		tu->qhead %= tu->queue_size;
L
Linus Torvalds 已提交
1956 1957 1958
		spin_unlock_irq(&tu->qlock);

		if (tu->tread) {
1959 1960
			if (copy_to_user(buffer, &tu->tqueue[qhead],
					 sizeof(struct snd_timer_tread)))
L
Linus Torvalds 已提交
1961 1962
				err = -EFAULT;
		} else {
1963 1964
			if (copy_to_user(buffer, &tu->queue[qhead],
					 sizeof(struct snd_timer_read)))
L
Linus Torvalds 已提交
1965 1966 1967 1968 1969
				err = -EFAULT;
		}

		spin_lock_irq(&tu->qlock);
		tu->qused--;
1970 1971 1972 1973
		if (err < 0)
			goto _error;
		result += unit;
		buffer += unit;
L
Linus Torvalds 已提交
1974 1975
	}
 _error:
1976
	spin_unlock_irq(&tu->qlock);
L
Linus Torvalds 已提交
1977 1978 1979 1980 1981 1982
	return result > 0 ? result : err;
}

static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
{
        unsigned int mask;
1983
        struct snd_timer_user *tu;
L
Linus Torvalds 已提交
1984 1985 1986 1987

        tu = file->private_data;

        poll_wait(file, &tu->qchange_sleep, wait);
C
Clemens Ladisch 已提交
1988

L
Linus Torvalds 已提交
1989 1990 1991
	mask = 0;
	if (tu->qused)
		mask |= POLLIN | POLLRDNORM;
1992 1993
	if (tu->disconnected)
		mask |= POLLERR;
L
Linus Torvalds 已提交
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003

	return mask;
}

#ifdef CONFIG_COMPAT
#include "timer_compat.c"
#else
#define snd_timer_user_ioctl_compat	NULL
#endif

2004
static const struct file_operations snd_timer_f_ops =
L
Linus Torvalds 已提交
2005 2006 2007 2008 2009
{
	.owner =	THIS_MODULE,
	.read =		snd_timer_user_read,
	.open =		snd_timer_user_open,
	.release =	snd_timer_user_release,
T
Takashi Iwai 已提交
2010
	.llseek =	no_llseek,
L
Linus Torvalds 已提交
2011 2012 2013 2014 2015 2016
	.poll =		snd_timer_user_poll,
	.unlocked_ioctl =	snd_timer_user_ioctl,
	.compat_ioctl =	snd_timer_user_ioctl_compat,
	.fasync = 	snd_timer_user_fasync,
};

2017 2018 2019 2020 2021 2022 2023 2024 2025
/* unregister the system timer */
static void snd_timer_free_all(void)
{
	struct snd_timer *timer, *n;

	list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
		snd_timer_free(timer);
}

2026 2027
static struct device timer_dev;

L
Linus Torvalds 已提交
2028 2029 2030 2031 2032 2033 2034 2035
/*
 *  ENTRY functions
 */

static int __init alsa_timer_init(void)
{
	int err;

2036 2037 2038
	snd_device_initialize(&timer_dev, NULL);
	dev_set_name(&timer_dev, "timer");

L
Linus Torvalds 已提交
2039
#ifdef SNDRV_OSS_INFO_DEV_TIMERS
C
Clemens Ladisch 已提交
2040 2041
	snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
			      "system timer");
L
Linus Torvalds 已提交
2042
#endif
2043

2044 2045
	err = snd_timer_register_system();
	if (err < 0) {
2046
		pr_err("ALSA: unable to register system timer (%i)\n", err);
2047
		put_device(&timer_dev);
2048 2049 2050
		return err;
	}

2051 2052
	err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
				  &snd_timer_f_ops, NULL, &timer_dev);
2053
	if (err < 0) {
2054
		pr_err("ALSA: unable to register timer device (%i)\n", err);
2055
		snd_timer_free_all();
2056
		put_device(&timer_dev);
2057 2058 2059
		return err;
	}

2060
	snd_timer_proc_init();
L
Linus Torvalds 已提交
2061 2062 2063 2064 2065
	return 0;
}

static void __exit alsa_timer_exit(void)
{
2066
	snd_unregister_device(&timer_dev);
2067
	snd_timer_free_all();
2068
	put_device(&timer_dev);
2069
	snd_timer_proc_done();
L
Linus Torvalds 已提交
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
#ifdef SNDRV_OSS_INFO_DEV_TIMERS
	snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
#endif
}

module_init(alsa_timer_init)
module_exit(alsa_timer_exit)

EXPORT_SYMBOL(snd_timer_open);
EXPORT_SYMBOL(snd_timer_close);
EXPORT_SYMBOL(snd_timer_resolution);
EXPORT_SYMBOL(snd_timer_start);
EXPORT_SYMBOL(snd_timer_stop);
EXPORT_SYMBOL(snd_timer_continue);
EXPORT_SYMBOL(snd_timer_pause);
EXPORT_SYMBOL(snd_timer_new);
EXPORT_SYMBOL(snd_timer_notify);
EXPORT_SYMBOL(snd_timer_global_new);
EXPORT_SYMBOL(snd_timer_global_free);
EXPORT_SYMBOL(snd_timer_global_register);
EXPORT_SYMBOL(snd_timer_interrupt);