timer.c 50.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 *  Timers abstract layer
 *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
 *
 *
 *   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 <sound/driver.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/smp_lock.h>
#include <linux/slab.h>
#include <linux/time.h>
#include <linux/moduleparam.h>
29
#include <linux/string.h>
L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#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>
#ifdef CONFIG_KERNELD
#include <linux/kerneld.h>
#endif

#if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
#define DEFAULT_TIMER_LIMIT 3
#elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
#define DEFAULT_TIMER_LIMIT 2
#else
#define DEFAULT_TIMER_LIMIT 1
#endif

static int timer_limit = DEFAULT_TIMER_LIMIT;
MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Takashi Iwai <tiwai@suse.de>");
MODULE_DESCRIPTION("ALSA timer interface");
MODULE_LICENSE("GPL");
module_param(timer_limit, int, 0444);
MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");

typedef struct {
	snd_timer_instance_t *timeri;
C
Clemens Ladisch 已提交
58
	int tread;		/* enhanced read with timestamps and events */
L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72
	unsigned long ticks;
	unsigned long overrun;
	int qhead;
	int qtail;
	int qused;
	int queue_size;
	snd_timer_read_t *queue;
	snd_timer_tread_t *tqueue;
	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;
73
	struct semaphore tread_sem;
L
Linus Torvalds 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
} snd_timer_user_t;

/* 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);

static DECLARE_MUTEX(register_mutex);

static int snd_timer_free(snd_timer_t *timer);
static int snd_timer_dev_free(snd_device_t *device);
static int snd_timer_dev_register(snd_device_t *device);
static int snd_timer_dev_unregister(snd_device_t *device);

static void snd_timer_reschedule(snd_timer_t * timer, unsigned long ticks_left);

/*
 * create a timer instance with the given owner string.
 * when timer is not NULL, increments the module counter
 */
C
Clemens Ladisch 已提交
98 99
static snd_timer_instance_t *snd_timer_instance_new(char *owner,
						    snd_timer_t *timer)
L
Linus Torvalds 已提交
100 101
{
	snd_timer_instance_t *timeri;
102
	timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
L
Linus Torvalds 已提交
103 104
	if (timeri == NULL)
		return NULL;
105
	timeri->owner = kstrdup(owner, GFP_KERNEL);
L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113 114 115 116
	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;
117
	if (timer && !try_module_get(timer->module)) {
L
Linus Torvalds 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
		kfree(timeri->owner);
		kfree(timeri);
		return NULL;
	}

	return timeri;
}

/*
 * find a timer instance from the given timer id
 */
static snd_timer_t *snd_timer_find(snd_timer_id_t *tid)
{
	snd_timer_t *timer = NULL;
	struct list_head *p;

	list_for_each(p, &snd_timer_list) {
135
		timer = list_entry(p, snd_timer_t, device_list);
L
Linus Torvalds 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

		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;
}

#ifdef CONFIG_KMOD

static void snd_timer_request(snd_timer_id_t *tid)
{
	if (! current->fs->root)
		return;
	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.
 */
static void snd_timer_check_slave(snd_timer_instance_t *slave)
{
	snd_timer_t *timer;
	snd_timer_instance_t *master;
	struct list_head *p, *q;

	/* FIXME: it's really dumb to look up all entries.. */
	list_for_each(p, &snd_timer_list) {
190
		timer = list_entry(p, snd_timer_t, device_list);
L
Linus Torvalds 已提交
191
		list_for_each(q, &timer->open_list_head) {
192
			master = list_entry(q, snd_timer_instance_t, open_list);
L
Linus Torvalds 已提交
193 194 195
			if (slave->slave_class == master->slave_class &&
			    slave->slave_id == master->slave_id) {
				list_del(&slave->open_list);
C
Clemens Ladisch 已提交
196 197
				list_add_tail(&slave->open_list,
					      &master->slave_list_head);
L
Linus Torvalds 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
				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.
 */
static void snd_timer_check_master(snd_timer_instance_t *master)
{
	snd_timer_instance_t *slave;
	struct list_head *p, *n;

	/* check all pending slaves */
	list_for_each_safe(p, n, &snd_timer_slave_list) {
221
		slave = list_entry(p, snd_timer_instance_t, open_list);
L
Linus Torvalds 已提交
222 223 224 225 226 227 228 229
		if (slave->slave_class == master->slave_class &&
		    slave->slave_id == master->slave_id) {
			list_del(p);
			list_add_tail(p, &master->slave_list_head);
			spin_lock_irq(&slave_active_lock);
			slave->master = master;
			slave->timer = master->timer;
			if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
C
Clemens Ladisch 已提交
230 231
				list_add_tail(&slave->active_list,
					      &master->slave_active_head);
L
Linus Torvalds 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
			spin_unlock_irq(&slave_active_lock);
		}
	}
}

/*
 * open a timer instance
 * when opening a master, the slave id must be here given.
 */
int snd_timer_open(snd_timer_instance_t **ti,
		   char *owner, snd_timer_id_t *tid,
		   unsigned int slave_id)
{
	snd_timer_t *timer;
	snd_timer_instance_t *timeri = NULL;
C
Clemens Ladisch 已提交
247

L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255 256
	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) {
			snd_printd("invalid slave class %i\n", tid->dev_sclass);
			return -EINVAL;
		}
		down(&register_mutex);
		timeri = snd_timer_instance_new(owner, NULL);
257 258 259 260
		if (!timeri) {
			up(&register_mutex);
			return -ENOMEM;
		}
L
Linus Torvalds 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
		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);
		up(&register_mutex);
		*ti = timeri;
		return 0;
	}

	/* open a master instance */
	down(&register_mutex);
	timer = snd_timer_find(tid);
#ifdef CONFIG_KMOD
	if (timer == NULL) {
		up(&register_mutex);
		snd_timer_request(tid);
		down(&register_mutex);
		timer = snd_timer_find(tid);
	}
#endif
282
	if (!timer) {
L
Linus Torvalds 已提交
283 284 285
		up(&register_mutex);
		return -ENODEV;
	}
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	if (!list_empty(&timer->open_list_head)) {
		timeri = list_entry(timer->open_list_head.next,
				    snd_timer_instance_t, open_list);
		if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
			up(&register_mutex);
			return -EBUSY;
		}
	}
	timeri = snd_timer_instance_new(owner, timer);
	if (!timeri) {
		up(&register_mutex);
		return -ENOMEM;
	}
	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);
L
Linus Torvalds 已提交
305 306 307 308 309
	up(&register_mutex);
	*ti = timeri;
	return 0;
}

C
Clemens Ladisch 已提交
310 311
static int _snd_timer_stop(snd_timer_instance_t * timeri,
			   int keep_flag, enum sndrv_timer_event event);
L
Linus Torvalds 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350

/*
 * close a timer instance
 */
int snd_timer_close(snd_timer_instance_t * timeri)
{
	snd_timer_t *timer = NULL;
	struct list_head *p, *n;
	snd_timer_instance_t *slave;

	snd_assert(timeri != NULL, return -ENXIO);

	/* force to stop the timer */
	snd_timer_stop(timeri);

	if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
		/* wait, until the active callback is finished */
		spin_lock_irq(&slave_active_lock);
		while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
			spin_unlock_irq(&slave_active_lock);
			udelay(10);
			spin_lock_irq(&slave_active_lock);
		}
		spin_unlock_irq(&slave_active_lock);
		down(&register_mutex);
		list_del(&timeri->open_list);
		up(&register_mutex);
	} else {
		timer = timeri->timer;
		/* 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);
		down(&register_mutex);
		list_del(&timeri->open_list);
C
Clemens Ladisch 已提交
351 352
		if (timer && list_empty(&timer->open_list_head) &&
		    timer->hw.close)
L
Linus Torvalds 已提交
353 354 355
			timer->hw.close(timer);
		/* remove slave links */
		list_for_each_safe(p, n, &timeri->slave_list_head) {
356
			slave = list_entry(p, snd_timer_instance_t, open_list);
L
Linus Torvalds 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370
			spin_lock_irq(&slave_active_lock);
			_snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
			list_del(p);
			list_add_tail(p, &snd_timer_slave_list);
			slave->master = NULL;
			slave->timer = NULL;
			spin_unlock_irq(&slave_active_lock);
		}
		up(&register_mutex);
	}
	if (timeri->private_free)
		timeri->private_free(timeri);
	kfree(timeri->owner);
	kfree(timeri);
371 372
	if (timer)
		module_put(timer->module);
L
Linus Torvalds 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	return 0;
}

unsigned long snd_timer_resolution(snd_timer_instance_t * timeri)
{
	snd_timer_t * timer;

	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;
}

C
Clemens Ladisch 已提交
390 391
static void snd_timer_notify1(snd_timer_instance_t *ti,
			      enum sndrv_timer_event event)
L
Linus Torvalds 已提交
392 393 394 395 396 397 398 399
{
	snd_timer_t *timer;
	unsigned long flags;
	unsigned long resolution = 0;
	snd_timer_instance_t *ts;
	struct list_head *n;
	struct timespec tstamp;

T
Takashi Iwai 已提交
400
	getnstimeofday(&tstamp);
C
Clemens Ladisch 已提交
401 402 403 404
	snd_assert(event >= SNDRV_TIMER_EVENT_START &&
		   event <= SNDRV_TIMER_EVENT_PAUSE, return);
	if (event == SNDRV_TIMER_EVENT_START ||
	    event == SNDRV_TIMER_EVENT_CONTINUE)
L
Linus Torvalds 已提交
405 406 407 408 409 410 411 412 413 414 415 416
		resolution = snd_timer_resolution(ti);
	if (ti->ccallback)
		ti->ccallback(ti, SNDRV_TIMER_EVENT_START, &tstamp, resolution);
	if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
		return;
	timer = ti->timer;
	if (timer == NULL)
		return;
	if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
		return;
	spin_lock_irqsave(&timer->lock, flags);
	list_for_each(n, &ti->slave_active_head) {
417
		ts = list_entry(n, snd_timer_instance_t, active_list);
L
Linus Torvalds 已提交
418 419 420 421 422 423
		if (ts->ccallback)
			ts->ccallback(ti, event + 100, &tstamp, resolution);
	}
	spin_unlock_irqrestore(&timer->lock, flags);
}

C
Clemens Ladisch 已提交
424 425
static int snd_timer_start1(snd_timer_t *timer, snd_timer_instance_t *timeri,
			    unsigned long sticks)
L
Linus Torvalds 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
{
	list_del(&timeri->active_list);
	list_add_tail(&timeri->active_list, &timer->active_list_head);
	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;
		return 1;	/* delayed start */
	} else {
		timer->sticks = sticks;
		timer->hw.start(timer);
	      __start_now:
		timer->running++;
		timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
		return 0;
	}
}

static int snd_timer_start_slave(snd_timer_instance_t *timeri)
{
	unsigned long flags;

	spin_lock_irqsave(&slave_active_lock, flags);
	timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
	if (timeri->master)
C
Clemens Ladisch 已提交
452 453
		list_add_tail(&timeri->active_list,
			      &timeri->master->slave_active_head);
L
Linus Torvalds 已提交
454 455 456 457 458 459
	spin_unlock_irqrestore(&slave_active_lock, flags);
	return 1; /* delayed start */
}

/*
 *  start the timer instance
C
Clemens Ladisch 已提交
460
 */
L
Linus Torvalds 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
int snd_timer_start(snd_timer_instance_t * timeri, unsigned int ticks)
{
	snd_timer_t *timer;
	int result = -EINVAL;
	unsigned long flags;

	if (timeri == NULL || ticks < 1)
		return -EINVAL;
	if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
		result = snd_timer_start_slave(timeri);
		snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
		return result;
	}
	timer = timeri->timer;
	if (timer == NULL)
		return -EINVAL;
	spin_lock_irqsave(&timer->lock, flags);
	timeri->ticks = timeri->cticks = ticks;
	timeri->pticks = 0;
	result = snd_timer_start1(timer, timeri, ticks);
	spin_unlock_irqrestore(&timer->lock, flags);
	snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
	return result;
}

C
Clemens Ladisch 已提交
486 487
static int _snd_timer_stop(snd_timer_instance_t * timeri,
			   int keep_flag, enum sndrv_timer_event event)
L
Linus Torvalds 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
{
	snd_timer_t *timer;
	unsigned long flags;

	snd_assert(timeri != NULL, return -ENXIO);

	if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
		if (!keep_flag) {
			spin_lock_irqsave(&slave_active_lock, flags);
			timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
			spin_unlock_irqrestore(&slave_active_lock, flags);
		}
		goto __end;
	}
	timer = timeri->timer;
	if (!timer)
		return -EINVAL;
	spin_lock_irqsave(&timer->lock, flags);
	list_del_init(&timeri->ack_list);
	list_del_init(&timeri->active_list);
	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);
			}
		}
	}
	if (!keep_flag)
C
Clemens Ladisch 已提交
521 522
		timeri->flags &=
			~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
L
Linus Torvalds 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
	spin_unlock_irqrestore(&timer->lock, flags);
      __end:
	if (event != SNDRV_TIMER_EVENT_RESOLUTION)
		snd_timer_notify1(timeri, event);
	return 0;
}

/*
 * stop the timer instance.
 *
 * do not call this from the timer callback!
 */
int snd_timer_stop(snd_timer_instance_t * timeri)
{
	snd_timer_t *timer;
	unsigned long flags;
	int err;

	err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
	if (err < 0)
		return err;
	timer = timeri->timer;
	spin_lock_irqsave(&timer->lock, flags);
	timeri->cticks = timeri->ticks;
	timeri->pticks = 0;
	spin_unlock_irqrestore(&timer->lock, flags);
	return 0;
}

/*
 * start again..  the tick is kept.
 */
int snd_timer_continue(snd_timer_instance_t * timeri)
{
	snd_timer_t *timer;
	int result = -EINVAL;
	unsigned long flags;

	if (timeri == NULL)
		return result;
	if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
		return snd_timer_start_slave(timeri);
	timer = timeri->timer;
	if (! timer)
		return -EINVAL;
	spin_lock_irqsave(&timer->lock, flags);
	if (!timeri->cticks)
		timeri->cticks = 1;
	timeri->pticks = 0;
	result = snd_timer_start1(timer, timeri, timer->sticks);
	spin_unlock_irqrestore(&timer->lock, flags);
	snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
	return result;
}

/*
 * pause.. remember the ticks left
 */
int snd_timer_pause(snd_timer_instance_t * timeri)
{
	return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
}

/*
 * reschedule the timer
 *
 * start pending instances and check the scheduling ticks.
 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
 */
static void snd_timer_reschedule(snd_timer_t * timer, unsigned long ticks_left)
{
	snd_timer_instance_t *ti;
	unsigned long ticks = ~0UL;
	struct list_head *p;

	list_for_each(p, &timer->active_list_head) {
599
		ti = list_entry(p, snd_timer_instance_t, active_list);
L
Linus Torvalds 已提交
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
		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)
{
	snd_timer_t *timer = (snd_timer_t *) arg;
	snd_timer_instance_t *ti;
	struct list_head *p;
	unsigned long resolution, ticks;

	spin_lock(&timer->lock);
	/* now process all callbacks */
	while (!list_empty(&timer->sack_list_head)) {
		p = timer->sack_list_head.next;		/* get first item */
636
		ti = list_entry(p, snd_timer_instance_t, ack_list);
L
Linus Torvalds 已提交
637 638 639

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

L
Linus Torvalds 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
		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;
	}
	spin_unlock(&timer->lock);
}

/*
 * timer interrupt
 *
 * ticks_left is usually equal to timer->sticks.
 *
 */
void snd_timer_interrupt(snd_timer_t * timer, unsigned long ticks_left)
{
	snd_timer_instance_t *ti, *ts;
	unsigned long resolution, ticks;
C
Clemens Ladisch 已提交
665
	struct list_head *p, *q, *n, *ack_list_head;
L
Linus Torvalds 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679
	int use_tasklet = 0;

	if (timer == NULL)
		return;

	spin_lock(&timer->lock);

	/* 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
C
Clemens Ladisch 已提交
680 681 682
	 * Here we cannot use list_for_each because the active_list of a
	 * processed instance is relinked to done_list_head before the callback
	 * is called.
L
Linus Torvalds 已提交
683 684
	 */
	list_for_each_safe(p, n, &timer->active_list_head) {
685
		ti = list_entry(p, snd_timer_instance_t, active_list);
L
Linus Torvalds 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
		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;
			if (--timer->running)
				list_del(p);
		}
C
Clemens Ladisch 已提交
703 704 705 706 707 708 709
		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);
L
Linus Torvalds 已提交
710
		list_for_each(q, &ti->slave_active_head) {
711
			ts = list_entry(q, snd_timer_instance_t, active_list);
L
Linus Torvalds 已提交
712 713
			ts->pticks = ti->pticks;
			ts->resolution = resolution;
C
Clemens Ladisch 已提交
714 715
			if (list_empty(&ts->ack_list))
				list_add_tail(&ts->ack_list, ack_list_head);
L
Linus Torvalds 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
		}
	}
	if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
		snd_timer_reschedule(timer, ticks_left);
	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 */
738
		ti = list_entry(p, snd_timer_instance_t, ack_list);
C
Clemens Ladisch 已提交
739

L
Linus Torvalds 已提交
740 741
		/* remove from ack_list and make empty */
		list_del_init(p);
C
Clemens Ladisch 已提交
742

L
Linus Torvalds 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
		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);
	spin_unlock(&timer->lock);

	if (use_tasklet)
		tasklet_hi_schedule(&timer->task_queue);
}

/*

 */

C
Clemens Ladisch 已提交
766 767
int snd_timer_new(snd_card_t *card, char *id, snd_timer_id_t *tid,
		  snd_timer_t **rtimer)
L
Linus Torvalds 已提交
768 769 770 771 772 773 774 775 776 777 778 779
{
	snd_timer_t *timer;
	int err;
	static snd_device_ops_t ops = {
		.dev_free = snd_timer_dev_free,
		.dev_register = snd_timer_dev_register,
		.dev_unregister = snd_timer_dev_unregister
	};

	snd_assert(tid != NULL, return -EINVAL);
	snd_assert(rtimer != NULL, return -EINVAL);
	*rtimer = NULL;
780
	timer = kzalloc(sizeof(*timer), GFP_KERNEL);
L
Linus Torvalds 已提交
781 782 783 784 785 786 787 788 789 790 791 792 793 794
	if (timer == NULL)
		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 已提交
795 796
	tasklet_init(&timer->task_queue, snd_timer_tasklet,
		     (unsigned long)timer);
L
Linus Torvalds 已提交
797
	if (card != NULL) {
798
		timer->module = card->module;
C
Clemens Ladisch 已提交
799 800
		err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
		if (err < 0) {
L
Linus Torvalds 已提交
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
			snd_timer_free(timer);
			return err;
		}
	}
	*rtimer = timer;
	return 0;
}

static int snd_timer_free(snd_timer_t *timer)
{
	snd_assert(timer != NULL, return -ENXIO);
	if (timer->private_free)
		timer->private_free(timer);
	kfree(timer);
	return 0;
}

818
static int snd_timer_dev_free(snd_device_t *device)
L
Linus Torvalds 已提交
819 820 821 822 823
{
	snd_timer_t *timer = device->device_data;
	return snd_timer_free(timer);
}

824
static int snd_timer_dev_register(snd_device_t *dev)
L
Linus Torvalds 已提交
825 826 827 828 829
{
	snd_timer_t *timer = dev->device_data;
	snd_timer_t *timer1;
	struct list_head *p;

C
Clemens Ladisch 已提交
830 831
	snd_assert(timer != NULL && timer->hw.start != NULL &&
		   timer->hw.stop != NULL, return -ENXIO);
L
Linus Torvalds 已提交
832 833 834 835 836 837
	if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
	    !timer->hw.resolution && timer->hw.c_resolution == NULL)
	    	return -EINVAL;

	down(&register_mutex);
	list_for_each(p, &snd_timer_list) {
838
		timer1 = list_entry(p, snd_timer_t, device_list);
L
Linus Torvalds 已提交
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
		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.. */
		up(&register_mutex);
		return -EBUSY;
	}
	list_add_tail(&timer->device_list, p);
	up(&register_mutex);
	return 0;
}

866
static int snd_timer_unregister(snd_timer_t *timer)
L
Linus Torvalds 已提交
867 868 869 870 871 872 873 874 875 876
{
	struct list_head *p, *n;
	snd_timer_instance_t *ti;

	snd_assert(timer != NULL, return -ENXIO);
	down(&register_mutex);
	if (! list_empty(&timer->open_list_head)) {
		snd_printk(KERN_WARNING "timer 0x%lx is busy?\n", (long)timer);
		list_for_each_safe(p, n, &timer->open_list_head) {
			list_del_init(p);
877
			ti = list_entry(p, snd_timer_instance_t, open_list);
L
Linus Torvalds 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891
			ti->timer = NULL;
		}
	}
	list_del(&timer->device_list);
	up(&register_mutex);
	return snd_timer_free(timer);
}

static int snd_timer_dev_unregister(snd_device_t *device)
{
	snd_timer_t *timer = device->device_data;
	return snd_timer_unregister(timer);
}

C
Clemens Ladisch 已提交
892 893
void snd_timer_notify(snd_timer_t *timer, enum sndrv_timer_event event,
		      struct timespec *tstamp)
L
Linus Torvalds 已提交
894 895 896 897 898 899
{
	unsigned long flags;
	unsigned long resolution = 0;
	snd_timer_instance_t *ti, *ts;
	struct list_head *p, *n;

900 901
	if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
		return;
C
Clemens Ladisch 已提交
902 903
	snd_assert(event >= SNDRV_TIMER_EVENT_MSTART &&
		   event <= SNDRV_TIMER_EVENT_MRESUME, return);
L
Linus Torvalds 已提交
904
	spin_lock_irqsave(&timer->lock, flags);
905 906 907
	if (event == SNDRV_TIMER_EVENT_MSTART ||
	    event == SNDRV_TIMER_EVENT_MCONTINUE ||
	    event == SNDRV_TIMER_EVENT_MRESUME) {
L
Linus Torvalds 已提交
908 909 910 911 912 913
		if (timer->hw.c_resolution)
			resolution = timer->hw.c_resolution(timer);
		else
			resolution = timer->hw.resolution;
	}
	list_for_each(p, &timer->active_list_head) {
914
		ti = list_entry(p, snd_timer_instance_t, active_list);
L
Linus Torvalds 已提交
915 916 917
		if (ti->ccallback)
			ti->ccallback(ti, event, tstamp, resolution);
		list_for_each(n, &ti->slave_active_head) {
918
			ts = list_entry(n, snd_timer_instance_t, active_list);
L
Linus Torvalds 已提交
919 920 921 922 923 924 925 926 927 928 929 930 931
			if (ts->ccallback)
				ts->ccallback(ts, event, tstamp, resolution);
		}
	}
	spin_unlock_irqrestore(&timer->lock, flags);
}

/*
 * exported functions for global timers
 */
int snd_timer_global_new(char *id, int device, snd_timer_t **rtimer)
{
	snd_timer_id_t tid;
C
Clemens Ladisch 已提交
932

L
Linus Torvalds 已提交
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
	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);
}

int snd_timer_global_free(snd_timer_t *timer)
{
	return snd_timer_free(timer);
}

int snd_timer_global_register(snd_timer_t *timer)
{
	snd_device_t dev;

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

int snd_timer_global_unregister(snd_timer_t *timer)
{
	return snd_timer_unregister(timer);
}

C
Clemens Ladisch 已提交
960
/*
L
Linus Torvalds 已提交
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
 *  System timer
 */

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

static void snd_timer_s_function(unsigned long data)
{
	snd_timer_t *timer = (snd_timer_t *)data;
	struct snd_timer_system_private *priv = timer->private_data;
	unsigned long jiff = jiffies;
	if (time_after(jiff, priv->last_expires))
		priv->correction = (long)jiff - (long)priv->last_expires;
	snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
}

static int snd_timer_s_start(snd_timer_t * timer)
{
	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;
		priv->correction -= timer->sticks;
	}
	priv->last_expires = priv->tlist.expires = njiff;
	add_timer(&priv->tlist);
	return 0;
}

static int snd_timer_s_stop(snd_timer_t * timer)
{
	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;
	return 0;
}

static struct _snd_timer_hardware snd_timer_system =
{
	.flags =	SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
	.resolution =	1000000000L / HZ,
	.ticks =	10000000L,
	.start =	snd_timer_s_start,
	.stop =		snd_timer_s_stop
};

static void snd_timer_free_system(snd_timer_t *timer)
{
	kfree(timer->private_data);
}

static int snd_timer_register_system(void)
{
	snd_timer_t *timer;
	struct snd_timer_system_private *priv;
	int err;

C
Clemens Ladisch 已提交
1036 1037
	err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
	if (err < 0)
L
Linus Torvalds 已提交
1038 1039 1040
		return err;
	strcpy(timer->name, "system timer");
	timer->hw = snd_timer_system;
1041
	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
L
Linus Torvalds 已提交
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
	if (priv == NULL) {
		snd_timer_free(timer);
		return -ENOMEM;
	}
	init_timer(&priv->tlist);
	priv->tlist.function = snd_timer_s_function;
	priv->tlist.data = (unsigned long) timer;
	timer->private_data = priv;
	timer->private_free = snd_timer_free_system;
	return snd_timer_global_register(timer);
}

/*
 *  Info interface
 */

static void snd_timer_proc_read(snd_info_entry_t *entry,
				snd_info_buffer_t * buffer)
{
	unsigned long flags;
	snd_timer_t *timer;
	snd_timer_instance_t *ti;
	struct list_head *p, *q;

	down(&register_mutex);
	list_for_each(p, &snd_timer_list) {
1068
		timer = list_entry(p, snd_timer_t, device_list);
L
Linus Torvalds 已提交
1069 1070 1071 1072 1073
		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 已提交
1074 1075
			snd_iprintf(buffer, "C%i-%i: ",
				    timer->card->number, timer->tmr_device);
L
Linus Torvalds 已提交
1076 1077
			break;
		case SNDRV_TIMER_CLASS_PCM:
C
Clemens Ladisch 已提交
1078 1079
			snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
				    timer->tmr_device, timer->tmr_subdevice);
L
Linus Torvalds 已提交
1080 1081
			break;
		default:
C
Clemens Ladisch 已提交
1082 1083 1084
			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 已提交
1085 1086 1087
		}
		snd_iprintf(buffer, "%s :", timer->name);
		if (timer->hw.resolution)
C
Clemens Ladisch 已提交
1088 1089 1090 1091
			snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
				    timer->hw.resolution / 1000,
				    timer->hw.resolution % 1000,
				    timer->hw.ticks);
L
Linus Torvalds 已提交
1092 1093 1094 1095 1096
		if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
			snd_iprintf(buffer, " SLAVE");
		snd_iprintf(buffer, "\n");
		spin_lock_irqsave(&timer->lock, flags);
		list_for_each(q, &timer->open_list_head) {
1097
			ti = list_entry(q, snd_timer_instance_t, open_list);
C
Clemens Ladisch 已提交
1098 1099 1100 1101 1102
			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 已提交
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
		}
		spin_unlock_irqrestore(&timer->lock, flags);
	}
	up(&register_mutex);
}

/*
 *  USER SPACE interface
 */

static void snd_timer_user_interrupt(snd_timer_instance_t *timeri,
				     unsigned long resolution,
				     unsigned long ticks)
{
	snd_timer_user_t *tu = timeri->callback_data;
	snd_timer_read_t *r;
	int prev;
C
Clemens Ladisch 已提交
1120

L
Linus Torvalds 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
	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);
}

C
Clemens Ladisch 已提交
1145 1146
static void snd_timer_user_append_to_tqueue(snd_timer_user_t *tu,
					    snd_timer_tread_t *tread)
L
Linus Torvalds 已提交
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
{
	if (tu->qused >= tu->queue_size) {
		tu->overrun++;
	} else {
		memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
		tu->qtail %= tu->queue_size;
		tu->qused++;
	}
}

static void snd_timer_user_ccallback(snd_timer_instance_t *timeri,
				     enum sndrv_timer_event event,
				     struct timespec *tstamp,
				     unsigned long resolution)
{
	snd_timer_user_t *tu = timeri->callback_data;
	snd_timer_tread_t r1;

C
Clemens Ladisch 已提交
1165 1166
	if (event >= SNDRV_TIMER_EVENT_START &&
	    event <= SNDRV_TIMER_EVENT_PAUSE)
L
Linus Torvalds 已提交
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
		tu->tstamp = *tstamp;
	if ((tu->filter & (1 << event)) == 0 || !tu->tread)
		return;
	r1.event = event;
	r1.tstamp = *tstamp;
	r1.val = resolution;
	spin_lock(&tu->qlock);
	snd_timer_user_append_to_tqueue(tu, &r1);
	spin_unlock(&tu->qlock);
	kill_fasync(&tu->fasync, SIGIO, POLL_IN);
	wake_up(&tu->qchange_sleep);
}

static void snd_timer_user_tinterrupt(snd_timer_instance_t *timeri,
				      unsigned long resolution,
				      unsigned long ticks)
{
	snd_timer_user_t *tu = timeri->callback_data;
	snd_timer_tread_t *r, r1;
	struct timespec tstamp;
	int prev, append = 0;

T
Takashi Iwai 已提交
1189
	memset(&tstamp, 0, sizeof(tstamp));
L
Linus Torvalds 已提交
1190
	spin_lock(&tu->qlock);
C
Clemens Ladisch 已提交
1191 1192
	if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
			   (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
L
Linus Torvalds 已提交
1193 1194 1195 1196
		spin_unlock(&tu->qlock);
		return;
	}
	if (tu->last_resolution != resolution || ticks > 0)
T
Takashi Iwai 已提交
1197
		getnstimeofday(&tstamp);
C
Clemens Ladisch 已提交
1198 1199
	if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
	    tu->last_resolution != resolution) {
L
Linus Torvalds 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
		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)
{
	snd_timer_user_t *tu;
C
Clemens Ladisch 已提交
1237

1238
	tu = kzalloc(sizeof(*tu), GFP_KERNEL);
L
Linus Torvalds 已提交
1239 1240 1241 1242
	if (tu == NULL)
		return -ENOMEM;
	spin_lock_init(&tu->qlock);
	init_waitqueue_head(&tu->qchange_sleep);
1243
	init_MUTEX(&tu->tread_sem);
L
Linus Torvalds 已提交
1244 1245
	tu->ticks = 1;
	tu->queue_size = 128;
C
Clemens Ladisch 已提交
1246 1247
	tu->queue = kmalloc(tu->queue_size * sizeof(snd_timer_read_t),
			    GFP_KERNEL);
L
Linus Torvalds 已提交
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 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
	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)
{
	snd_timer_user_t *tu;

	if (file->private_data) {
		tu = file->private_data;
		file->private_data = NULL;
		fasync_helper(-1, file, 0, &tu->fasync);
		if (tu->timeri)
			snd_timer_close(tu->timeri);
		kfree(tu->queue);
		kfree(tu->tqueue);
		kfree(tu);
	}
	return 0;
}

static void snd_timer_user_zero_id(snd_timer_id_t *id)
{
	id->dev_class = SNDRV_TIMER_CLASS_NONE;
	id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
	id->card = -1;
	id->device = -1;
	id->subdevice = -1;
}

static void snd_timer_user_copy_id(snd_timer_id_t *id, snd_timer_t *timer)
{
	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;
}

static int snd_timer_user_next_device(snd_timer_id_t __user *_tid)
{
	snd_timer_id_t id;
	snd_timer_t *timer;
	struct list_head *p;
C
Clemens Ladisch 已提交
1296

L
Linus Torvalds 已提交
1297 1298 1299 1300 1301 1302 1303
	if (copy_from_user(&id, _tid, sizeof(id)))
		return -EFAULT;
	down(&register_mutex);
	if (id.dev_class < 0) {		/* first item */
		if (list_empty(&snd_timer_list))
			snd_timer_user_zero_id(&id);
		else {
1304 1305
			timer = list_entry(snd_timer_list.next,
					   snd_timer_t, device_list);
L
Linus Torvalds 已提交
1306 1307 1308 1309 1310 1311 1312
			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) {
1313
				timer = list_entry(p, snd_timer_t, device_list);
L
Linus Torvalds 已提交
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
				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 已提交
1337 1338 1339 1340 1341
						if (id.subdevice < 0) {
							id.subdevice = 0;
						} else {
							id.subdevice++;
						}
L
Linus Torvalds 已提交
1342 1343 1344 1345
					}
				}
			}
			list_for_each(p, &snd_timer_list) {
1346
				timer = list_entry(p, snd_timer_t, device_list);
L
Linus Torvalds 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
				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);
		}
	}
	up(&register_mutex);
	if (copy_to_user(_tid, &id, sizeof(*_tid)))
		return -EFAULT;
	return 0;
C
Clemens Ladisch 已提交
1385
}
L
Linus Torvalds 已提交
1386

C
Clemens Ladisch 已提交
1387 1388
static int snd_timer_user_ginfo(struct file *file,
				snd_timer_ginfo_t __user *_ginfo)
L
Linus Torvalds 已提交
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
{
	snd_timer_ginfo_t *ginfo;
	snd_timer_id_t tid;
	snd_timer_t *t;
	struct list_head *p;
	int err = 0;

	ginfo = kmalloc(sizeof(*ginfo), GFP_KERNEL);
	if (! ginfo)
		return -ENOMEM;
	if (copy_from_user(ginfo, _ginfo, sizeof(*ginfo))) {
		kfree(ginfo);
		return -EFAULT;
	}
	tid = ginfo->tid;
	memset(ginfo, 0, sizeof(*ginfo));
	ginfo->tid = tid;
	down(&register_mutex);
	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;
	}
	up(&register_mutex);
	if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
		err = -EFAULT;
	kfree(ginfo);
	return err;
}

C
Clemens Ladisch 已提交
1432 1433
static int snd_timer_user_gparams(struct file *file,
				  snd_timer_gparams_t __user *_gparams)
L
Linus Torvalds 已提交
1434 1435 1436 1437 1438 1439 1440 1441 1442
{
	snd_timer_gparams_t gparams;
	snd_timer_t *t;
	int err;

	if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
		return -EFAULT;
	down(&register_mutex);
	t = snd_timer_find(&gparams.tid);
C
Clemens Ladisch 已提交
1443
	if (!t) {
L
Linus Torvalds 已提交
1444
		err = -ENODEV;
C
Clemens Ladisch 已提交
1445 1446 1447 1448 1449 1450 1451 1452 1453
		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 已提交
1454
	}
C
Clemens Ladisch 已提交
1455 1456
	err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
_error:
L
Linus Torvalds 已提交
1457 1458 1459 1460
	up(&register_mutex);
	return err;
}

C
Clemens Ladisch 已提交
1461 1462
static int snd_timer_user_gstatus(struct file *file,
				  snd_timer_gstatus_t __user *_gstatus)
L
Linus Torvalds 已提交
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
{
	snd_timer_gstatus_t gstatus;
	snd_timer_id_t tid;
	snd_timer_t *t;
	int err = 0;

	if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
		return -EFAULT;
	tid = gstatus.tid;
	memset(&gstatus, 0, sizeof(gstatus));
	gstatus.tid = tid;
	down(&register_mutex);
	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 已提交
1482 1483
			t->hw.precise_resolution(t, &gstatus.resolution_num,
						 &gstatus.resolution_den);
L
Linus Torvalds 已提交
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
		} else {
			gstatus.resolution_num = gstatus.resolution;
			gstatus.resolution_den = 1000000000uL;
		}
	} else {
		err = -ENODEV;
	}
	up(&register_mutex);
	if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
		err = -EFAULT;
	return err;
}

C
Clemens Ladisch 已提交
1497 1498
static int snd_timer_user_tselect(struct file *file,
				  snd_timer_select_t __user *_tselect)
L
Linus Torvalds 已提交
1499 1500 1501 1502
{
	snd_timer_user_t *tu;
	snd_timer_select_t tselect;
	char str[32];
1503
	int err = 0;
C
Clemens Ladisch 已提交
1504

L
Linus Torvalds 已提交
1505
	tu = file->private_data;
1506 1507
	down(&tu->tread_sem);
	if (tu->timeri) {
L
Linus Torvalds 已提交
1508
		snd_timer_close(tu->timeri);
1509 1510 1511 1512 1513 1514
		tu->timeri = NULL;
	}
	if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
		err = -EFAULT;
		goto __err;
	}
L
Linus Torvalds 已提交
1515 1516 1517
	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 已提交
1518 1519
	err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
	if (err < 0)
1520
		goto __err;
L
Linus Torvalds 已提交
1521

1522 1523 1524 1525
	kfree(tu->queue);
	tu->queue = NULL;
	kfree(tu->tqueue);
	tu->tqueue = NULL;
L
Linus Torvalds 已提交
1526
	if (tu->tread) {
C
Clemens Ladisch 已提交
1527 1528
		tu->tqueue = kmalloc(tu->queue_size * sizeof(snd_timer_tread_t),
				     GFP_KERNEL);
1529 1530
		if (tu->tqueue == NULL)
			err = -ENOMEM;
L
Linus Torvalds 已提交
1531
	} else {
C
Clemens Ladisch 已提交
1532 1533
		tu->queue = kmalloc(tu->queue_size * sizeof(snd_timer_read_t),
				    GFP_KERNEL);
1534 1535
		if (tu->queue == NULL)
			err = -ENOMEM;
L
Linus Torvalds 已提交
1536
	}
C
Clemens Ladisch 已提交
1537

1538 1539 1540 1541 1542
      	if (err < 0) {
		snd_timer_close(tu->timeri);
      		tu->timeri = NULL;
      	} else {
		tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
C
Clemens Ladisch 已提交
1543 1544
		tu->timeri->callback = tu->tread
			? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
1545 1546 1547 1548 1549 1550 1551
		tu->timeri->ccallback = snd_timer_user_ccallback;
		tu->timeri->callback_data = (void *)tu;
	}

      __err:
      	up(&tu->tread_sem);
	return err;
L
Linus Torvalds 已提交
1552 1553
}

C
Clemens Ladisch 已提交
1554 1555
static int snd_timer_user_info(struct file *file,
			       snd_timer_info_t __user *_info)
L
Linus Torvalds 已提交
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
{
	snd_timer_user_t *tu;
	snd_timer_info_t *info;
	snd_timer_t *t;
	int err = 0;

	tu = file->private_data;
	snd_assert(tu->timeri != NULL, return -ENXIO);
	t = tu->timeri->timer;
	snd_assert(t != NULL, return -ENXIO);

1567
	info = kzalloc(sizeof(*info), GFP_KERNEL);
L
Linus Torvalds 已提交
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
	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 已提交
1582 1583
static int snd_timer_user_params(struct file *file,
				 snd_timer_params_t __user *_params)
L
Linus Torvalds 已提交
1584 1585 1586 1587 1588 1589 1590
{
	snd_timer_user_t *tu;
	snd_timer_params_t params;
	snd_timer_t *t;
	snd_timer_read_t *tr;
	snd_timer_tread_t *ttr;
	int err;
C
Clemens Ladisch 已提交
1591

L
Linus Torvalds 已提交
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
	tu = file->private_data;
	snd_assert(tu->timeri != NULL, return -ENXIO);
	t = tu->timeri->timer;
	snd_assert(t != NULL, return -ENXIO);
	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 已提交
1602 1603
	if (params.queue_size > 0 &&
	    (params.queue_size < 32 || params.queue_size > 1024)) {
L
Linus Torvalds 已提交
1604 1605 1606 1607 1608 1609 1610 1611 1612
		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)|
1613 1614
			      (1<<SNDRV_TIMER_EVENT_SUSPEND)|
			      (1<<SNDRV_TIMER_EVENT_RESUME)|
L
Linus Torvalds 已提交
1615 1616 1617
			      (1<<SNDRV_TIMER_EVENT_MSTART)|
			      (1<<SNDRV_TIMER_EVENT_MSTOP)|
			      (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
1618 1619
			      (1<<SNDRV_TIMER_EVENT_MPAUSE)|
			      (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
1620
			      (1<<SNDRV_TIMER_EVENT_MRESUME))) {
L
Linus Torvalds 已提交
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
		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 已提交
1636 1637
	if (params.queue_size > 0 &&
	    (unsigned int)tu->queue_size != params.queue_size) {
L
Linus Torvalds 已提交
1638
		if (tu->tread) {
C
Clemens Ladisch 已提交
1639 1640
			ttr = kmalloc(params.queue_size * sizeof(*ttr),
				      GFP_KERNEL);
L
Linus Torvalds 已提交
1641 1642 1643 1644 1645 1646
			if (ttr) {
				kfree(tu->tqueue);
				tu->queue_size = params.queue_size;
				tu->tqueue = ttr;
			}
		} else {
C
Clemens Ladisch 已提交
1647 1648
			tr = kmalloc(params.queue_size * sizeof(*tr),
				     GFP_KERNEL);
L
Linus Torvalds 已提交
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
			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) {
			snd_timer_tread_t tread;
			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 {
			snd_timer_read_t *r = &tu->queue[0];
			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 已提交
1682 1683
static int snd_timer_user_status(struct file *file,
				 snd_timer_status_t __user *_status)
L
Linus Torvalds 已提交
1684 1685 1686
{
	snd_timer_user_t *tu;
	snd_timer_status_t status;
C
Clemens Ladisch 已提交
1687

L
Linus Torvalds 已提交
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
	tu = file->private_data;
	snd_assert(tu->timeri != NULL, return -ENXIO);
	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;
	snd_timer_user_t *tu;
C
Clemens Ladisch 已提交
1707

L
Linus Torvalds 已提交
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
	tu = file->private_data;
	snd_assert(tu->timeri != NULL, return -ENXIO);
	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;
	snd_timer_user_t *tu;
C
Clemens Ladisch 已提交
1720

L
Linus Torvalds 已提交
1721 1722 1723 1724 1725 1726 1727 1728 1729
	tu = file->private_data;
	snd_assert(tu->timeri != NULL, return -ENXIO);
	return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
}

static int snd_timer_user_continue(struct file *file)
{
	int err;
	snd_timer_user_t *tu;
C
Clemens Ladisch 已提交
1730

L
Linus Torvalds 已提交
1731 1732 1733 1734 1735 1736
	tu = file->private_data;
	snd_assert(tu->timeri != NULL, return -ENXIO);
	tu->timeri->lost = 0;
	return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
}

T
Takashi Iwai 已提交
1737 1738 1739 1740
static int snd_timer_user_pause(struct file *file)
{
	int err;
	snd_timer_user_t *tu;
C
Clemens Ladisch 已提交
1741

T
Takashi Iwai 已提交
1742 1743
	tu = file->private_data;
	snd_assert(tu->timeri != NULL, return -ENXIO);
1744
	return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
T
Takashi Iwai 已提交
1745 1746
}

1747 1748 1749 1750 1751 1752 1753
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),
};

C
Clemens Ladisch 已提交
1754 1755
static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
				 unsigned long arg)
L
Linus Torvalds 已提交
1756 1757 1758 1759
{
	snd_timer_user_t *tu;
	void __user *argp = (void __user *)arg;
	int __user *p = argp;
C
Clemens Ladisch 已提交
1760

L
Linus Torvalds 已提交
1761 1762 1763 1764 1765 1766 1767 1768 1769
	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 已提交
1770

1771 1772 1773
		down(&tu->tread_sem);
		if (tu->timeri)	{	/* too late */
			up(&tu->tread_sem);
L
Linus Torvalds 已提交
1774
			return -EBUSY;
1775 1776 1777
		}
		if (get_user(xarg, p)) {
			up(&tu->tread_sem);
L
Linus Torvalds 已提交
1778
			return -EFAULT;
1779
		}
L
Linus Torvalds 已提交
1780
		tu->tread = xarg ? 1 : 0;
1781
		up(&tu->tread_sem);
L
Linus Torvalds 已提交
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
		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:
1799
	case SNDRV_TIMER_IOCTL_START_OLD:
L
Linus Torvalds 已提交
1800 1801
		return snd_timer_user_start(file);
	case SNDRV_TIMER_IOCTL_STOP:
1802
	case SNDRV_TIMER_IOCTL_STOP_OLD:
L
Linus Torvalds 已提交
1803 1804
		return snd_timer_user_stop(file);
	case SNDRV_TIMER_IOCTL_CONTINUE:
1805
	case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
L
Linus Torvalds 已提交
1806
		return snd_timer_user_continue(file);
T
Takashi Iwai 已提交
1807
	case SNDRV_TIMER_IOCTL_PAUSE:
1808
	case SNDRV_TIMER_IOCTL_PAUSE_OLD:
T
Takashi Iwai 已提交
1809
		return snd_timer_user_pause(file);
L
Linus Torvalds 已提交
1810 1811 1812 1813 1814 1815 1816 1817
	}
	return -ENOTTY;
}

static int snd_timer_user_fasync(int fd, struct file * file, int on)
{
	snd_timer_user_t *tu;
	int err;
C
Clemens Ladisch 已提交
1818

L
Linus Torvalds 已提交
1819 1820 1821 1822 1823 1824 1825
	tu = file->private_data;
	err = fasync_helper(fd, file, on, &tu->fasync);
        if (err < 0)
		return err;
	return 0;
}

C
Clemens Ladisch 已提交
1826 1827
static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
				   size_t count, loff_t *offset)
L
Linus Torvalds 已提交
1828 1829 1830 1831
{
	snd_timer_user_t *tu;
	long result = 0, unit;
	int err = 0;
C
Clemens Ladisch 已提交
1832

L
Linus Torvalds 已提交
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
	tu = file->private_data;
	unit = tu->tread ? sizeof(snd_timer_tread_t) : sizeof(snd_timer_read_t);
	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;
				break;
			}

			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);

			if (signal_pending(current)) {
				err = -ERESTARTSYS;
				break;
			}
		}

		spin_unlock_irq(&tu->qlock);
		if (err < 0)
			goto _error;

		if (tu->tread) {
C
Clemens Ladisch 已提交
1866 1867
			if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
					 sizeof(snd_timer_tread_t))) {
L
Linus Torvalds 已提交
1868 1869 1870 1871
				err = -EFAULT;
				goto _error;
			}
		} else {
C
Clemens Ladisch 已提交
1872 1873
			if (copy_to_user(buffer, &tu->queue[tu->qhead++],
					 sizeof(snd_timer_read_t))) {
L
Linus Torvalds 已提交
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
				err = -EFAULT;
				goto _error;
			}
		}

		tu->qhead %= tu->queue_size;

		result += unit;
		buffer += unit;

		spin_lock_irq(&tu->qlock);
		tu->qused--;
	}
	spin_unlock_irq(&tu->qlock);
 _error:
	return result > 0 ? result : err;
}

static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
{
        unsigned int mask;
        snd_timer_user_t *tu;

        tu = file->private_data;

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

L
Linus Torvalds 已提交
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
	mask = 0;
	if (tu->qused)
		mask |= POLLIN | POLLRDNORM;

	return mask;
}

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

static struct file_operations snd_timer_f_ops =
{
	.owner =	THIS_MODULE,
	.read =		snd_timer_user_read,
	.open =		snd_timer_user_open,
	.release =	snd_timer_user_release,
	.poll =		snd_timer_user_poll,
	.unlocked_ioctl =	snd_timer_user_ioctl,
	.compat_ioctl =	snd_timer_user_ioctl_compat,
	.fasync = 	snd_timer_user_fasync,
};

static snd_minor_t snd_timer_reg =
{
	.comment =	"timer",
	.f_ops =	&snd_timer_f_ops,
};

/*
 *  ENTRY functions
 */

static snd_info_entry_t *snd_timer_proc_entry = NULL;

static int __init alsa_timer_init(void)
{
	int err;
	snd_info_entry_t *entry;

#ifdef SNDRV_OSS_INFO_DEV_TIMERS
C
Clemens Ladisch 已提交
1944 1945
	snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
			      "system timer");
L
Linus Torvalds 已提交
1946
#endif
C
Clemens Ladisch 已提交
1947 1948
	entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
	if (entry != NULL) {
L
Linus Torvalds 已提交
1949 1950 1951 1952 1953 1954 1955 1956 1957
		entry->c.text.read_size = SNDRV_TIMER_DEVICES * 128;
		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;
	if ((err = snd_timer_register_system()) < 0)
C
Clemens Ladisch 已提交
1958 1959
		snd_printk(KERN_ERR "unable to register system timer (%i)\n",
			   err);
L
Linus Torvalds 已提交
1960 1961
	if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER,
					NULL, 0, &snd_timer_reg, "timer"))<0)
C
Clemens Ladisch 已提交
1962 1963
		snd_printk(KERN_ERR "unable to register timer device (%i)\n",
			   err);
L
Linus Torvalds 已提交
1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
	return 0;
}

static void __exit alsa_timer_exit(void)
{
	struct list_head *p, *n;

	snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
	/* unregister the system timer */
	list_for_each_safe(p, n, &snd_timer_list) {
1974
		snd_timer_t *timer = list_entry(p, snd_timer_t, device_list);
L
Linus Torvalds 已提交
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
		snd_timer_unregister(timer);
	}
	if (snd_timer_proc_entry) {
		snd_info_unregister(snd_timer_proc_entry);
		snd_timer_proc_entry = NULL;
	}
#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_global_unregister);
EXPORT_SYMBOL(snd_timer_interrupt);