blob: 9d8184a2c2d06350f44d294fdabd3d4797c6b638 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Timers abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/delay.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/slab.h>
25#include <linux/time.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010026#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/moduleparam.h>
Paulo Marques543537b2005-06-23 00:09:02 -070028#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include <sound/timer.h>
31#include <sound/control.h>
32#include <sound/info.h>
33#include <sound/minors.h>
34#include <sound/initval.h>
35#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#if defined(CONFIG_SND_HPET) || defined(CONFIG_SND_HPET_MODULE)
38#define DEFAULT_TIMER_LIMIT 3
39#elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
40#define DEFAULT_TIMER_LIMIT 2
41#else
42#define DEFAULT_TIMER_LIMIT 1
43#endif
44
45static int timer_limit = DEFAULT_TIMER_LIMIT;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +010046static int timer_tstamp_monotonic = 1;
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020047MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070048MODULE_DESCRIPTION("ALSA timer interface");
49MODULE_LICENSE("GPL");
50module_param(timer_limit, int, 0444);
51MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
Jaroslav Kyselab751eef2007-12-13 10:19:42 +010052module_param(timer_tstamp_monotonic, int, 0444);
53MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Takashi Iwai53d2f742005-11-17 13:56:05 +010055struct snd_timer_user {
56 struct snd_timer_instance *timeri;
Clemens Ladisch6b172a82005-10-12 17:20:21 +020057 int tread; /* enhanced read with timestamps and events */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 unsigned long ticks;
59 unsigned long overrun;
60 int qhead;
61 int qtail;
62 int qused;
63 int queue_size;
Takashi Iwai53d2f742005-11-17 13:56:05 +010064 struct snd_timer_read *queue;
65 struct snd_timer_tread *tqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 spinlock_t qlock;
67 unsigned long last_resolution;
68 unsigned int filter;
69 struct timespec tstamp; /* trigger tstamp */
70 wait_queue_head_t qchange_sleep;
71 struct fasync_struct *fasync;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010072 struct mutex tread_sem;
Takashi Iwai53d2f742005-11-17 13:56:05 +010073};
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/* list of timers */
76static LIST_HEAD(snd_timer_list);
77
78/* list of slave instances */
79static LIST_HEAD(snd_timer_slave_list);
80
81/* lock for slave active lists */
82static DEFINE_SPINLOCK(slave_active_lock);
83
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010084static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Takashi Iwai53d2f742005-11-17 13:56:05 +010086static int snd_timer_free(struct snd_timer *timer);
87static int snd_timer_dev_free(struct snd_device *device);
88static int snd_timer_dev_register(struct snd_device *device);
Takashi Iwaic4614822006-06-23 14:38:23 +020089static int snd_timer_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Takashi Iwai53d2f742005-11-17 13:56:05 +010091static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93/*
94 * create a timer instance with the given owner string.
95 * when timer is not NULL, increments the module counter
96 */
Takashi Iwai53d2f742005-11-17 13:56:05 +010097static struct snd_timer_instance *snd_timer_instance_new(char *owner,
98 struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100100 struct snd_timer_instance *timeri;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200101 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (timeri == NULL)
103 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700104 timeri->owner = kstrdup(owner, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (! timeri->owner) {
106 kfree(timeri);
107 return NULL;
108 }
109 INIT_LIST_HEAD(&timeri->open_list);
110 INIT_LIST_HEAD(&timeri->active_list);
111 INIT_LIST_HEAD(&timeri->ack_list);
112 INIT_LIST_HEAD(&timeri->slave_list_head);
113 INIT_LIST_HEAD(&timeri->slave_active_head);
114
115 timeri->timer = timer;
Clemens Ladischde242142005-10-12 17:12:31 +0200116 if (timer && !try_module_get(timer->module)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 kfree(timeri->owner);
118 kfree(timeri);
119 return NULL;
120 }
121
122 return timeri;
123}
124
125/*
126 * find a timer instance from the given timer id
127 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100128static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100130 struct snd_timer *timer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Johannes Berg9244b2c2006-10-05 16:02:22 +0200132 list_for_each_entry(timer, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (timer->tmr_class != tid->dev_class)
134 continue;
135 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
136 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
137 (timer->card == NULL ||
138 timer->card->number != tid->card))
139 continue;
140 if (timer->tmr_device != tid->device)
141 continue;
142 if (timer->tmr_subdevice != tid->subdevice)
143 continue;
144 return timer;
145 }
146 return NULL;
147}
148
149#ifdef CONFIG_KMOD
150
Takashi Iwai53d2f742005-11-17 13:56:05 +0100151static void snd_timer_request(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 switch (tid->dev_class) {
154 case SNDRV_TIMER_CLASS_GLOBAL:
155 if (tid->device < timer_limit)
156 request_module("snd-timer-%i", tid->device);
157 break;
158 case SNDRV_TIMER_CLASS_CARD:
159 case SNDRV_TIMER_CLASS_PCM:
160 if (tid->card < snd_ecards_limit)
161 request_module("snd-card-%i", tid->card);
162 break;
163 default:
164 break;
165 }
166}
167
168#endif
169
170/*
171 * look for a master instance matching with the slave id of the given slave.
172 * when found, relink the open_link of the slave.
173 *
174 * call this with register_mutex down.
175 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100176static void snd_timer_check_slave(struct snd_timer_instance *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100178 struct snd_timer *timer;
179 struct snd_timer_instance *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /* FIXME: it's really dumb to look up all entries.. */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200182 list_for_each_entry(timer, &snd_timer_list, device_list) {
183 list_for_each_entry(master, &timer->open_list_head, open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (slave->slave_class == master->slave_class &&
185 slave->slave_id == master->slave_id) {
186 list_del(&slave->open_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200187 list_add_tail(&slave->open_list,
188 &master->slave_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 spin_lock_irq(&slave_active_lock);
190 slave->master = master;
191 slave->timer = master->timer;
192 spin_unlock_irq(&slave_active_lock);
193 return;
194 }
195 }
196 }
197}
198
199/*
200 * look for slave instances matching with the slave id of the given master.
201 * when found, relink the open_link of slaves.
202 *
203 * call this with register_mutex down.
204 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100205static void snd_timer_check_master(struct snd_timer_instance *master)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200207 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* check all pending slaves */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200210 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (slave->slave_class == master->slave_class &&
212 slave->slave_id == master->slave_id) {
Johannes Berg9244b2c2006-10-05 16:02:22 +0200213 list_move_tail(&slave->open_list, &master->slave_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 spin_lock_irq(&slave_active_lock);
215 slave->master = master;
216 slave->timer = master->timer;
217 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200218 list_add_tail(&slave->active_list,
219 &master->slave_active_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 spin_unlock_irq(&slave_active_lock);
221 }
222 }
223}
224
225/*
226 * open a timer instance
227 * when opening a master, the slave id must be here given.
228 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100229int snd_timer_open(struct snd_timer_instance **ti,
230 char *owner, struct snd_timer_id *tid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 unsigned int slave_id)
232{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100233 struct snd_timer *timer;
234 struct snd_timer_instance *timeri = NULL;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
237 /* open a slave instance */
238 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
239 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
240 snd_printd("invalid slave class %i\n", tid->dev_sclass);
241 return -EINVAL;
242 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100243 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 timeri = snd_timer_instance_new(owner, NULL);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200245 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100246 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200247 return -ENOMEM;
248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 timeri->slave_class = tid->dev_sclass;
250 timeri->slave_id = tid->device;
251 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
252 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
253 snd_timer_check_slave(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100254 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 *ti = timeri;
256 return 0;
257 }
258
259 /* open a master instance */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100260 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 timer = snd_timer_find(tid);
262#ifdef CONFIG_KMOD
263 if (timer == NULL) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100264 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 snd_timer_request(tid);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100266 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 timer = snd_timer_find(tid);
268 }
269#endif
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200270 if (!timer) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100271 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return -ENODEV;
273 }
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200274 if (!list_empty(&timer->open_list_head)) {
275 timeri = list_entry(timer->open_list_head.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +0100276 struct snd_timer_instance, open_list);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200277 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100278 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200279 return -EBUSY;
280 }
281 }
282 timeri = snd_timer_instance_new(owner, timer);
283 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100284 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200285 return -ENOMEM;
286 }
287 timeri->slave_class = tid->dev_sclass;
288 timeri->slave_id = slave_id;
289 if (list_empty(&timer->open_list_head) && timer->hw.open)
290 timer->hw.open(timer);
291 list_add_tail(&timeri->open_list, &timer->open_list_head);
292 snd_timer_check_master(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100293 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 *ti = timeri;
295 return 0;
296}
297
Takashi Iwai53d2f742005-11-17 13:56:05 +0100298static int _snd_timer_stop(struct snd_timer_instance *timeri,
299 int keep_flag, int event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301/*
302 * close a timer instance
303 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100304int snd_timer_close(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100306 struct snd_timer *timer = NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200307 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 snd_assert(timeri != NULL, return -ENXIO);
310
311 /* force to stop the timer */
312 snd_timer_stop(timeri);
313
314 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
315 /* wait, until the active callback is finished */
316 spin_lock_irq(&slave_active_lock);
317 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
318 spin_unlock_irq(&slave_active_lock);
319 udelay(10);
320 spin_lock_irq(&slave_active_lock);
321 }
322 spin_unlock_irq(&slave_active_lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100323 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 list_del(&timeri->open_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100325 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 } else {
327 timer = timeri->timer;
328 /* wait, until the active callback is finished */
329 spin_lock_irq(&timer->lock);
330 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
331 spin_unlock_irq(&timer->lock);
332 udelay(10);
333 spin_lock_irq(&timer->lock);
334 }
335 spin_unlock_irq(&timer->lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100336 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 list_del(&timeri->open_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200338 if (timer && list_empty(&timer->open_list_head) &&
339 timer->hw.close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 timer->hw.close(timer);
341 /* remove slave links */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200342 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
343 open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 spin_lock_irq(&slave_active_lock);
345 _snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200346 list_move_tail(&slave->open_list, &snd_timer_slave_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 slave->master = NULL;
348 slave->timer = NULL;
349 spin_unlock_irq(&slave_active_lock);
350 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100351 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353 if (timeri->private_free)
354 timeri->private_free(timeri);
355 kfree(timeri->owner);
356 kfree(timeri);
Clemens Ladischde242142005-10-12 17:12:31 +0200357 if (timer)
358 module_put(timer->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return 0;
360}
361
Takashi Iwai53d2f742005-11-17 13:56:05 +0100362unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100364 struct snd_timer * timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 if (timeri == NULL)
367 return 0;
368 if ((timer = timeri->timer) != NULL) {
369 if (timer->hw.c_resolution)
370 return timer->hw.c_resolution(timer);
371 return timer->hw.resolution;
372 }
373 return 0;
374}
375
Takashi Iwai53d2f742005-11-17 13:56:05 +0100376static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100378 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 unsigned long flags;
380 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100381 struct snd_timer_instance *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct timespec tstamp;
383
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100384 if (timer_tstamp_monotonic)
385 do_posix_clock_monotonic_gettime(&tstamp);
386 else
387 getnstimeofday(&tstamp);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200388 snd_assert(event >= SNDRV_TIMER_EVENT_START &&
389 event <= SNDRV_TIMER_EVENT_PAUSE, return);
390 if (event == SNDRV_TIMER_EVENT_START ||
391 event == SNDRV_TIMER_EVENT_CONTINUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 resolution = snd_timer_resolution(ti);
393 if (ti->ccallback)
394 ti->ccallback(ti, SNDRV_TIMER_EVENT_START, &tstamp, resolution);
395 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
396 return;
397 timer = ti->timer;
398 if (timer == NULL)
399 return;
400 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
401 return;
402 spin_lock_irqsave(&timer->lock, flags);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200403 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (ts->ccallback)
405 ts->ccallback(ti, event + 100, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 spin_unlock_irqrestore(&timer->lock, flags);
407}
408
Takashi Iwai53d2f742005-11-17 13:56:05 +0100409static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200410 unsigned long sticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 list_del(&timeri->active_list);
413 list_add_tail(&timeri->active_list, &timer->active_list_head);
414 if (timer->running) {
415 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
416 goto __start_now;
417 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
418 timeri->flags |= SNDRV_TIMER_IFLG_START;
419 return 1; /* delayed start */
420 } else {
421 timer->sticks = sticks;
422 timer->hw.start(timer);
423 __start_now:
424 timer->running++;
425 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
426 return 0;
427 }
428}
429
Takashi Iwai53d2f742005-11-17 13:56:05 +0100430static int snd_timer_start_slave(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 unsigned long flags;
433
434 spin_lock_irqsave(&slave_active_lock, flags);
435 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
436 if (timeri->master)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200437 list_add_tail(&timeri->active_list,
438 &timeri->master->slave_active_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 spin_unlock_irqrestore(&slave_active_lock, flags);
440 return 1; /* delayed start */
441}
442
443/*
444 * start the timer instance
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200445 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100446int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100448 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 int result = -EINVAL;
450 unsigned long flags;
451
452 if (timeri == NULL || ticks < 1)
453 return -EINVAL;
454 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
455 result = snd_timer_start_slave(timeri);
456 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
457 return result;
458 }
459 timer = timeri->timer;
460 if (timer == NULL)
461 return -EINVAL;
462 spin_lock_irqsave(&timer->lock, flags);
463 timeri->ticks = timeri->cticks = ticks;
464 timeri->pticks = 0;
465 result = snd_timer_start1(timer, timeri, ticks);
466 spin_unlock_irqrestore(&timer->lock, flags);
467 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
468 return result;
469}
470
Takashi Iwai53d2f742005-11-17 13:56:05 +0100471static int _snd_timer_stop(struct snd_timer_instance * timeri,
472 int keep_flag, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100474 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 unsigned long flags;
476
477 snd_assert(timeri != NULL, return -ENXIO);
478
479 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
480 if (!keep_flag) {
481 spin_lock_irqsave(&slave_active_lock, flags);
482 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
483 spin_unlock_irqrestore(&slave_active_lock, flags);
484 }
485 goto __end;
486 }
487 timer = timeri->timer;
488 if (!timer)
489 return -EINVAL;
490 spin_lock_irqsave(&timer->lock, flags);
491 list_del_init(&timeri->ack_list);
492 list_del_init(&timeri->active_list);
493 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
494 !(--timer->running)) {
495 timer->hw.stop(timer);
496 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
497 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
498 snd_timer_reschedule(timer, 0);
499 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
500 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
501 timer->hw.start(timer);
502 }
503 }
504 }
505 if (!keep_flag)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200506 timeri->flags &=
507 ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 spin_unlock_irqrestore(&timer->lock, flags);
509 __end:
510 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
511 snd_timer_notify1(timeri, event);
512 return 0;
513}
514
515/*
516 * stop the timer instance.
517 *
518 * do not call this from the timer callback!
519 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100520int snd_timer_stop(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100522 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 unsigned long flags;
524 int err;
525
526 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
527 if (err < 0)
528 return err;
529 timer = timeri->timer;
530 spin_lock_irqsave(&timer->lock, flags);
531 timeri->cticks = timeri->ticks;
532 timeri->pticks = 0;
533 spin_unlock_irqrestore(&timer->lock, flags);
534 return 0;
535}
536
537/*
538 * start again.. the tick is kept.
539 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100540int snd_timer_continue(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100542 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 int result = -EINVAL;
544 unsigned long flags;
545
546 if (timeri == NULL)
547 return result;
548 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
549 return snd_timer_start_slave(timeri);
550 timer = timeri->timer;
551 if (! timer)
552 return -EINVAL;
553 spin_lock_irqsave(&timer->lock, flags);
554 if (!timeri->cticks)
555 timeri->cticks = 1;
556 timeri->pticks = 0;
557 result = snd_timer_start1(timer, timeri, timer->sticks);
558 spin_unlock_irqrestore(&timer->lock, flags);
559 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
560 return result;
561}
562
563/*
564 * pause.. remember the ticks left
565 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100566int snd_timer_pause(struct snd_timer_instance * timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
569}
570
571/*
572 * reschedule the timer
573 *
574 * start pending instances and check the scheduling ticks.
575 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
576 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100577static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100579 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 unsigned long ticks = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Johannes Berg9244b2c2006-10-05 16:02:22 +0200582 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (ti->flags & SNDRV_TIMER_IFLG_START) {
584 ti->flags &= ~SNDRV_TIMER_IFLG_START;
585 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
586 timer->running++;
587 }
588 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
589 if (ticks > ti->cticks)
590 ticks = ti->cticks;
591 }
592 }
593 if (ticks == ~0UL) {
594 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
595 return;
596 }
597 if (ticks > timer->hw.ticks)
598 ticks = timer->hw.ticks;
599 if (ticks_left != ticks)
600 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
601 timer->sticks = ticks;
602}
603
604/*
605 * timer tasklet
606 *
607 */
608static void snd_timer_tasklet(unsigned long arg)
609{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100610 struct snd_timer *timer = (struct snd_timer *) arg;
611 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct list_head *p;
613 unsigned long resolution, ticks;
Takashi Iwai2999ff52006-07-05 17:16:58 +0200614 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Takashi Iwai2999ff52006-07-05 17:16:58 +0200616 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* now process all callbacks */
618 while (!list_empty(&timer->sack_list_head)) {
619 p = timer->sack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100620 ti = list_entry(p, struct snd_timer_instance, ack_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 /* remove from ack_list and make empty */
623 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 ticks = ti->pticks;
626 ti->pticks = 0;
627 resolution = ti->resolution;
628
629 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
630 spin_unlock(&timer->lock);
631 if (ti->callback)
632 ti->callback(ti, resolution, ticks);
633 spin_lock(&timer->lock);
634 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
635 }
Takashi Iwai2999ff52006-07-05 17:16:58 +0200636 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639/*
640 * timer interrupt
641 *
642 * ticks_left is usually equal to timer->sticks.
643 *
644 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100645void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200647 struct snd_timer_instance *ti, *ts, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 unsigned long resolution, ticks;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200649 struct list_head *p, *ack_list_head;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100650 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 int use_tasklet = 0;
652
653 if (timer == NULL)
654 return;
655
Takashi Iwaib32425a2005-11-18 18:52:14 +0100656 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /* remember the current resolution */
659 if (timer->hw.c_resolution)
660 resolution = timer->hw.c_resolution(timer);
661 else
662 resolution = timer->hw.resolution;
663
664 /* loop for all active instances
Johannes Berg9244b2c2006-10-05 16:02:22 +0200665 * Here we cannot use list_for_each_entry because the active_list of a
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200666 * processed instance is relinked to done_list_head before the callback
667 * is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200669 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
670 active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
672 continue;
673 ti->pticks += ticks_left;
674 ti->resolution = resolution;
675 if (ti->cticks < ticks_left)
676 ti->cticks = 0;
677 else
678 ti->cticks -= ticks_left;
679 if (ti->cticks) /* not expired */
680 continue;
681 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
682 ti->cticks = ti->ticks;
683 } else {
684 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
685 if (--timer->running)
Johannes Berg9244b2c2006-10-05 16:02:22 +0200686 list_del(&ti->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200688 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
689 (ti->flags & SNDRV_TIMER_IFLG_FAST))
690 ack_list_head = &timer->ack_list_head;
691 else
692 ack_list_head = &timer->sack_list_head;
693 if (list_empty(&ti->ack_list))
694 list_add_tail(&ti->ack_list, ack_list_head);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200695 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 ts->pticks = ti->pticks;
697 ts->resolution = resolution;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200698 if (list_empty(&ts->ack_list))
699 list_add_tail(&ts->ack_list, ack_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701 }
702 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
Clemens Ladischcd93fe42006-07-17 16:53:57 +0200703 snd_timer_reschedule(timer, timer->sticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (timer->running) {
705 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
706 timer->hw.stop(timer);
707 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
708 }
709 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
710 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
711 /* restart timer */
712 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
713 timer->hw.start(timer);
714 }
715 } else {
716 timer->hw.stop(timer);
717 }
718
719 /* now process all fast callbacks */
720 while (!list_empty(&timer->ack_list_head)) {
721 p = timer->ack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100722 ti = list_entry(p, struct snd_timer_instance, ack_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /* remove from ack_list and make empty */
725 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 ticks = ti->pticks;
728 ti->pticks = 0;
729
730 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
731 spin_unlock(&timer->lock);
732 if (ti->callback)
733 ti->callback(ti, resolution, ticks);
734 spin_lock(&timer->lock);
735 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
736 }
737
738 /* do we have any slow callbacks? */
739 use_tasklet = !list_empty(&timer->sack_list_head);
Takashi Iwaib32425a2005-11-18 18:52:14 +0100740 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 if (use_tasklet)
743 tasklet_hi_schedule(&timer->task_queue);
744}
745
746/*
747
748 */
749
Takashi Iwai53d2f742005-11-17 13:56:05 +0100750int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
751 struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100753 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100755 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 .dev_free = snd_timer_dev_free,
757 .dev_register = snd_timer_dev_register,
Takashi Iwaic4614822006-06-23 14:38:23 +0200758 .dev_disconnect = snd_timer_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 };
760
761 snd_assert(tid != NULL, return -EINVAL);
762 snd_assert(rtimer != NULL, return -EINVAL);
763 *rtimer = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200764 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100765 if (timer == NULL) {
766 snd_printk(KERN_ERR "timer: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 timer->tmr_class = tid->dev_class;
770 timer->card = card;
771 timer->tmr_device = tid->device;
772 timer->tmr_subdevice = tid->subdevice;
773 if (id)
774 strlcpy(timer->id, id, sizeof(timer->id));
775 INIT_LIST_HEAD(&timer->device_list);
776 INIT_LIST_HEAD(&timer->open_list_head);
777 INIT_LIST_HEAD(&timer->active_list_head);
778 INIT_LIST_HEAD(&timer->ack_list_head);
779 INIT_LIST_HEAD(&timer->sack_list_head);
780 spin_lock_init(&timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200781 tasklet_init(&timer->task_queue, snd_timer_tasklet,
782 (unsigned long)timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (card != NULL) {
Clemens Ladischde242142005-10-12 17:12:31 +0200784 timer->module = card->module;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200785 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
786 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 snd_timer_free(timer);
788 return err;
789 }
790 }
791 *rtimer = timer;
792 return 0;
793}
794
Takashi Iwai53d2f742005-11-17 13:56:05 +0100795static int snd_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 snd_assert(timer != NULL, return -ENXIO);
Takashi Iwaic4614822006-06-23 14:38:23 +0200798
799 mutex_lock(&register_mutex);
800 if (! list_empty(&timer->open_list_head)) {
801 struct list_head *p, *n;
802 struct snd_timer_instance *ti;
803 snd_printk(KERN_WARNING "timer %p is busy?\n", timer);
804 list_for_each_safe(p, n, &timer->open_list_head) {
805 list_del_init(p);
806 ti = list_entry(p, struct snd_timer_instance, open_list);
807 ti->timer = NULL;
808 }
809 }
810 list_del(&timer->device_list);
811 mutex_unlock(&register_mutex);
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (timer->private_free)
814 timer->private_free(timer);
815 kfree(timer);
816 return 0;
817}
818
Takashi Iwai53d2f742005-11-17 13:56:05 +0100819static int snd_timer_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100821 struct snd_timer *timer = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return snd_timer_free(timer);
823}
824
Takashi Iwai53d2f742005-11-17 13:56:05 +0100825static int snd_timer_dev_register(struct snd_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100827 struct snd_timer *timer = dev->device_data;
828 struct snd_timer *timer1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200830 snd_assert(timer != NULL && timer->hw.start != NULL &&
831 timer->hw.stop != NULL, return -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
833 !timer->hw.resolution && timer->hw.c_resolution == NULL)
834 return -EINVAL;
835
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100836 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200837 list_for_each_entry(timer1, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (timer1->tmr_class > timer->tmr_class)
839 break;
840 if (timer1->tmr_class < timer->tmr_class)
841 continue;
842 if (timer1->card && timer->card) {
843 if (timer1->card->number > timer->card->number)
844 break;
845 if (timer1->card->number < timer->card->number)
846 continue;
847 }
848 if (timer1->tmr_device > timer->tmr_device)
849 break;
850 if (timer1->tmr_device < timer->tmr_device)
851 continue;
852 if (timer1->tmr_subdevice > timer->tmr_subdevice)
853 break;
854 if (timer1->tmr_subdevice < timer->tmr_subdevice)
855 continue;
856 /* conflicts.. */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100857 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return -EBUSY;
859 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200860 list_add_tail(&timer->device_list, &timer1->device_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100861 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return 0;
863}
864
Takashi Iwaic4614822006-06-23 14:38:23 +0200865static int snd_timer_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100867 struct snd_timer *timer = device->device_data;
Takashi Iwaic4614822006-06-23 14:38:23 +0200868 mutex_lock(&register_mutex);
869 list_del_init(&timer->device_list);
870 mutex_unlock(&register_mutex);
871 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Takashi Iwai53d2f742005-11-17 13:56:05 +0100874void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 unsigned long flags;
877 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100878 struct snd_timer_instance *ti, *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200880 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
881 return;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200882 snd_assert(event >= SNDRV_TIMER_EVENT_MSTART &&
883 event <= SNDRV_TIMER_EVENT_MRESUME, return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 spin_lock_irqsave(&timer->lock, flags);
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +0200885 if (event == SNDRV_TIMER_EVENT_MSTART ||
886 event == SNDRV_TIMER_EVENT_MCONTINUE ||
887 event == SNDRV_TIMER_EVENT_MRESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (timer->hw.c_resolution)
889 resolution = timer->hw.c_resolution(timer);
890 else
891 resolution = timer->hw.resolution;
892 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200893 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (ti->ccallback)
895 ti->ccallback(ti, event, tstamp, resolution);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200896 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (ts->ccallback)
898 ts->ccallback(ts, event, tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900 spin_unlock_irqrestore(&timer->lock, flags);
901}
902
903/*
904 * exported functions for global timers
905 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100906int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100908 struct snd_timer_id tid;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
911 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
912 tid.card = -1;
913 tid.device = device;
914 tid.subdevice = 0;
915 return snd_timer_new(NULL, id, &tid, rtimer);
916}
917
Takashi Iwai53d2f742005-11-17 13:56:05 +0100918int snd_timer_global_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 return snd_timer_free(timer);
921}
922
Takashi Iwai53d2f742005-11-17 13:56:05 +0100923int snd_timer_global_register(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100925 struct snd_device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 memset(&dev, 0, sizeof(dev));
928 dev.device_data = timer;
929 return snd_timer_dev_register(&dev);
930}
931
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200932/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 * System timer
934 */
935
936struct snd_timer_system_private {
937 struct timer_list tlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 unsigned long last_expires;
939 unsigned long last_jiffies;
940 unsigned long correction;
941};
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943static void snd_timer_s_function(unsigned long data)
944{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100945 struct snd_timer *timer = (struct snd_timer *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 struct snd_timer_system_private *priv = timer->private_data;
947 unsigned long jiff = jiffies;
948 if (time_after(jiff, priv->last_expires))
Clemens Ladisch6ed5eff2006-07-17 16:51:37 +0200949 priv->correction += (long)jiff - (long)priv->last_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
951}
952
Takashi Iwai53d2f742005-11-17 13:56:05 +0100953static int snd_timer_s_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 struct snd_timer_system_private *priv;
956 unsigned long njiff;
957
958 priv = (struct snd_timer_system_private *) timer->private_data;
959 njiff = (priv->last_jiffies = jiffies);
960 if (priv->correction > timer->sticks - 1) {
961 priv->correction -= timer->sticks - 1;
962 njiff++;
963 } else {
964 njiff += timer->sticks - priv->correction;
Clemens Ladisch17f48ec2006-07-17 16:50:56 +0200965 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967 priv->last_expires = priv->tlist.expires = njiff;
968 add_timer(&priv->tlist);
969 return 0;
970}
971
Takashi Iwai53d2f742005-11-17 13:56:05 +0100972static int snd_timer_s_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 struct snd_timer_system_private *priv;
975 unsigned long jiff;
976
977 priv = (struct snd_timer_system_private *) timer->private_data;
978 del_timer(&priv->tlist);
979 jiff = jiffies;
980 if (time_before(jiff, priv->last_expires))
981 timer->sticks = priv->last_expires - jiff;
982 else
983 timer->sticks = 1;
Clemens Ladischde2696d2006-07-17 16:52:09 +0200984 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return 0;
986}
987
Takashi Iwai53d2f742005-11-17 13:56:05 +0100988static struct snd_timer_hardware snd_timer_system =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
991 .resolution = 1000000000L / HZ,
992 .ticks = 10000000L,
993 .start = snd_timer_s_start,
994 .stop = snd_timer_s_stop
995};
996
Takashi Iwai53d2f742005-11-17 13:56:05 +0100997static void snd_timer_free_system(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 kfree(timer->private_data);
1000}
1001
1002static int snd_timer_register_system(void)
1003{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001004 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 struct snd_timer_system_private *priv;
1006 int err;
1007
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001008 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1009 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return err;
1011 strcpy(timer->name, "system timer");
1012 timer->hw = snd_timer_system;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001013 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (priv == NULL) {
1015 snd_timer_free(timer);
1016 return -ENOMEM;
1017 }
1018 init_timer(&priv->tlist);
1019 priv->tlist.function = snd_timer_s_function;
1020 priv->tlist.data = (unsigned long) timer;
1021 timer->private_data = priv;
1022 timer->private_free = snd_timer_free_system;
1023 return snd_timer_global_register(timer);
1024}
1025
Takashi Iwaie28563c2005-12-01 10:42:42 +01001026#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027/*
1028 * Info interface
1029 */
1030
Takashi Iwai53d2f742005-11-17 13:56:05 +01001031static void snd_timer_proc_read(struct snd_info_entry *entry,
1032 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001034 struct snd_timer *timer;
1035 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001037 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001038 list_for_each_entry(timer, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 switch (timer->tmr_class) {
1040 case SNDRV_TIMER_CLASS_GLOBAL:
1041 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1042 break;
1043 case SNDRV_TIMER_CLASS_CARD:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001044 snd_iprintf(buffer, "C%i-%i: ",
1045 timer->card->number, timer->tmr_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 break;
1047 case SNDRV_TIMER_CLASS_PCM:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001048 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1049 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 break;
1051 default:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001052 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1053 timer->card ? timer->card->number : -1,
1054 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056 snd_iprintf(buffer, "%s :", timer->name);
1057 if (timer->hw.resolution)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001058 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1059 timer->hw.resolution / 1000,
1060 timer->hw.resolution % 1000,
1061 timer->hw.ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1063 snd_iprintf(buffer, " SLAVE");
1064 snd_iprintf(buffer, "\n");
Johannes Berg9244b2c2006-10-05 16:02:22 +02001065 list_for_each_entry(ti, &timer->open_list_head, open_list)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001066 snd_iprintf(buffer, " Client %s : %s\n",
1067 ti->owner ? ti->owner : "unknown",
1068 ti->flags & (SNDRV_TIMER_IFLG_START |
1069 SNDRV_TIMER_IFLG_RUNNING)
1070 ? "running" : "stopped");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001072 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001075static struct snd_info_entry *snd_timer_proc_entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001076
1077static void __init snd_timer_proc_init(void)
1078{
1079 struct snd_info_entry *entry;
1080
1081 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1082 if (entry != NULL) {
Takashi Iwaie28563c2005-12-01 10:42:42 +01001083 entry->c.text.read = snd_timer_proc_read;
1084 if (snd_info_register(entry) < 0) {
1085 snd_info_free_entry(entry);
1086 entry = NULL;
1087 }
1088 }
1089 snd_timer_proc_entry = entry;
1090}
1091
1092static void __exit snd_timer_proc_done(void)
1093{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001094 snd_info_free_entry(snd_timer_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001095}
1096#else /* !CONFIG_PROC_FS */
1097#define snd_timer_proc_init()
1098#define snd_timer_proc_done()
1099#endif
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101/*
1102 * USER SPACE interface
1103 */
1104
Takashi Iwai53d2f742005-11-17 13:56:05 +01001105static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 unsigned long resolution,
1107 unsigned long ticks)
1108{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001109 struct snd_timer_user *tu = timeri->callback_data;
1110 struct snd_timer_read *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 int prev;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 spin_lock(&tu->qlock);
1114 if (tu->qused > 0) {
1115 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1116 r = &tu->queue[prev];
1117 if (r->resolution == resolution) {
1118 r->ticks += ticks;
1119 goto __wake;
1120 }
1121 }
1122 if (tu->qused >= tu->queue_size) {
1123 tu->overrun++;
1124 } else {
1125 r = &tu->queue[tu->qtail++];
1126 tu->qtail %= tu->queue_size;
1127 r->resolution = resolution;
1128 r->ticks = ticks;
1129 tu->qused++;
1130 }
1131 __wake:
1132 spin_unlock(&tu->qlock);
1133 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1134 wake_up(&tu->qchange_sleep);
1135}
1136
Takashi Iwai53d2f742005-11-17 13:56:05 +01001137static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1138 struct snd_timer_tread *tread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
1140 if (tu->qused >= tu->queue_size) {
1141 tu->overrun++;
1142 } else {
1143 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1144 tu->qtail %= tu->queue_size;
1145 tu->qused++;
1146 }
1147}
1148
Takashi Iwai53d2f742005-11-17 13:56:05 +01001149static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1150 int event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 struct timespec *tstamp,
1152 unsigned long resolution)
1153{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001154 struct snd_timer_user *tu = timeri->callback_data;
1155 struct snd_timer_tread r1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001157 if (event >= SNDRV_TIMER_EVENT_START &&
1158 event <= SNDRV_TIMER_EVENT_PAUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 tu->tstamp = *tstamp;
1160 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1161 return;
1162 r1.event = event;
1163 r1.tstamp = *tstamp;
1164 r1.val = resolution;
1165 spin_lock(&tu->qlock);
1166 snd_timer_user_append_to_tqueue(tu, &r1);
1167 spin_unlock(&tu->qlock);
1168 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1169 wake_up(&tu->qchange_sleep);
1170}
1171
Takashi Iwai53d2f742005-11-17 13:56:05 +01001172static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 unsigned long resolution,
1174 unsigned long ticks)
1175{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001176 struct snd_timer_user *tu = timeri->callback_data;
1177 struct snd_timer_tread *r, r1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 struct timespec tstamp;
1179 int prev, append = 0;
1180
Takashi Iwai07799e72005-10-10 11:49:49 +02001181 memset(&tstamp, 0, sizeof(tstamp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 spin_lock(&tu->qlock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001183 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1184 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 spin_unlock(&tu->qlock);
1186 return;
1187 }
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001188 if (tu->last_resolution != resolution || ticks > 0) {
1189 if (timer_tstamp_monotonic)
1190 do_posix_clock_monotonic_gettime(&tstamp);
1191 else
1192 getnstimeofday(&tstamp);
1193 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001194 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1195 tu->last_resolution != resolution) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1197 r1.tstamp = tstamp;
1198 r1.val = resolution;
1199 snd_timer_user_append_to_tqueue(tu, &r1);
1200 tu->last_resolution = resolution;
1201 append++;
1202 }
1203 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1204 goto __wake;
1205 if (ticks == 0)
1206 goto __wake;
1207 if (tu->qused > 0) {
1208 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1209 r = &tu->tqueue[prev];
1210 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1211 r->tstamp = tstamp;
1212 r->val += ticks;
1213 append++;
1214 goto __wake;
1215 }
1216 }
1217 r1.event = SNDRV_TIMER_EVENT_TICK;
1218 r1.tstamp = tstamp;
1219 r1.val = ticks;
1220 snd_timer_user_append_to_tqueue(tu, &r1);
1221 append++;
1222 __wake:
1223 spin_unlock(&tu->qlock);
1224 if (append == 0)
1225 return;
1226 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1227 wake_up(&tu->qchange_sleep);
1228}
1229
1230static int snd_timer_user_open(struct inode *inode, struct file *file)
1231{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001232 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001233
Takashi Iwaica2c0962005-09-09 14:20:23 +02001234 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (tu == NULL)
1236 return -ENOMEM;
1237 spin_lock_init(&tu->qlock);
1238 init_waitqueue_head(&tu->qchange_sleep);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001239 mutex_init(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 tu->ticks = 1;
1241 tu->queue_size = 128;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001242 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001243 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (tu->queue == NULL) {
1245 kfree(tu);
1246 return -ENOMEM;
1247 }
1248 file->private_data = tu;
1249 return 0;
1250}
1251
1252static int snd_timer_user_release(struct inode *inode, struct file *file)
1253{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001254 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 if (file->private_data) {
1257 tu = file->private_data;
1258 file->private_data = NULL;
1259 fasync_helper(-1, file, 0, &tu->fasync);
1260 if (tu->timeri)
1261 snd_timer_close(tu->timeri);
1262 kfree(tu->queue);
1263 kfree(tu->tqueue);
1264 kfree(tu);
1265 }
1266 return 0;
1267}
1268
Takashi Iwai53d2f742005-11-17 13:56:05 +01001269static void snd_timer_user_zero_id(struct snd_timer_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1272 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1273 id->card = -1;
1274 id->device = -1;
1275 id->subdevice = -1;
1276}
1277
Takashi Iwai53d2f742005-11-17 13:56:05 +01001278static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 id->dev_class = timer->tmr_class;
1281 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1282 id->card = timer->card ? timer->card->number : -1;
1283 id->device = timer->tmr_device;
1284 id->subdevice = timer->tmr_subdevice;
1285}
1286
Takashi Iwai53d2f742005-11-17 13:56:05 +01001287static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001289 struct snd_timer_id id;
1290 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 struct list_head *p;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (copy_from_user(&id, _tid, sizeof(id)))
1294 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001295 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (id.dev_class < 0) { /* first item */
1297 if (list_empty(&snd_timer_list))
1298 snd_timer_user_zero_id(&id);
1299 else {
Clemens Ladisch9dfba382005-10-12 17:14:55 +02001300 timer = list_entry(snd_timer_list.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001301 struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 snd_timer_user_copy_id(&id, timer);
1303 }
1304 } else {
1305 switch (id.dev_class) {
1306 case SNDRV_TIMER_CLASS_GLOBAL:
1307 id.device = id.device < 0 ? 0 : id.device + 1;
1308 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001309 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1311 snd_timer_user_copy_id(&id, timer);
1312 break;
1313 }
1314 if (timer->tmr_device >= id.device) {
1315 snd_timer_user_copy_id(&id, timer);
1316 break;
1317 }
1318 }
1319 if (p == &snd_timer_list)
1320 snd_timer_user_zero_id(&id);
1321 break;
1322 case SNDRV_TIMER_CLASS_CARD:
1323 case SNDRV_TIMER_CLASS_PCM:
1324 if (id.card < 0) {
1325 id.card = 0;
1326 } else {
1327 if (id.card < 0) {
1328 id.card = 0;
1329 } else {
1330 if (id.device < 0) {
1331 id.device = 0;
1332 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001333 if (id.subdevice < 0) {
1334 id.subdevice = 0;
1335 } else {
1336 id.subdevice++;
1337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339 }
1340 }
1341 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001342 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (timer->tmr_class > id.dev_class) {
1344 snd_timer_user_copy_id(&id, timer);
1345 break;
1346 }
1347 if (timer->tmr_class < id.dev_class)
1348 continue;
1349 if (timer->card->number > id.card) {
1350 snd_timer_user_copy_id(&id, timer);
1351 break;
1352 }
1353 if (timer->card->number < id.card)
1354 continue;
1355 if (timer->tmr_device > id.device) {
1356 snd_timer_user_copy_id(&id, timer);
1357 break;
1358 }
1359 if (timer->tmr_device < id.device)
1360 continue;
1361 if (timer->tmr_subdevice > id.subdevice) {
1362 snd_timer_user_copy_id(&id, timer);
1363 break;
1364 }
1365 if (timer->tmr_subdevice < id.subdevice)
1366 continue;
1367 snd_timer_user_copy_id(&id, timer);
1368 break;
1369 }
1370 if (p == &snd_timer_list)
1371 snd_timer_user_zero_id(&id);
1372 break;
1373 default:
1374 snd_timer_user_zero_id(&id);
1375 }
1376 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001377 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1379 return -EFAULT;
1380 return 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001381}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001383static int snd_timer_user_ginfo(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001384 struct snd_timer_ginfo __user *_ginfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001386 struct snd_timer_ginfo *ginfo;
1387 struct snd_timer_id tid;
1388 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 struct list_head *p;
1390 int err = 0;
1391
1392 ginfo = kmalloc(sizeof(*ginfo), GFP_KERNEL);
1393 if (! ginfo)
1394 return -ENOMEM;
1395 if (copy_from_user(ginfo, _ginfo, sizeof(*ginfo))) {
1396 kfree(ginfo);
1397 return -EFAULT;
1398 }
1399 tid = ginfo->tid;
1400 memset(ginfo, 0, sizeof(*ginfo));
1401 ginfo->tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001402 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 t = snd_timer_find(&tid);
1404 if (t != NULL) {
1405 ginfo->card = t->card ? t->card->number : -1;
1406 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1407 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1408 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1409 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1410 ginfo->resolution = t->hw.resolution;
1411 if (t->hw.resolution_min > 0) {
1412 ginfo->resolution_min = t->hw.resolution_min;
1413 ginfo->resolution_max = t->hw.resolution_max;
1414 }
1415 list_for_each(p, &t->open_list_head) {
1416 ginfo->clients++;
1417 }
1418 } else {
1419 err = -ENODEV;
1420 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001421 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1423 err = -EFAULT;
1424 kfree(ginfo);
1425 return err;
1426}
1427
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001428static int snd_timer_user_gparams(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001429 struct snd_timer_gparams __user *_gparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001431 struct snd_timer_gparams gparams;
1432 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 int err;
1434
1435 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1436 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001437 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 t = snd_timer_find(&gparams.tid);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001439 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 err = -ENODEV;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001441 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001443 if (!list_empty(&t->open_list_head)) {
1444 err = -EBUSY;
1445 goto _error;
1446 }
1447 if (!t->hw.set_period) {
1448 err = -ENOSYS;
1449 goto _error;
1450 }
1451 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1452_error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001453 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return err;
1455}
1456
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001457static int snd_timer_user_gstatus(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001458 struct snd_timer_gstatus __user *_gstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001460 struct snd_timer_gstatus gstatus;
1461 struct snd_timer_id tid;
1462 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 int err = 0;
1464
1465 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1466 return -EFAULT;
1467 tid = gstatus.tid;
1468 memset(&gstatus, 0, sizeof(gstatus));
1469 gstatus.tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001470 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 t = snd_timer_find(&tid);
1472 if (t != NULL) {
1473 if (t->hw.c_resolution)
1474 gstatus.resolution = t->hw.c_resolution(t);
1475 else
1476 gstatus.resolution = t->hw.resolution;
1477 if (t->hw.precise_resolution) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001478 t->hw.precise_resolution(t, &gstatus.resolution_num,
1479 &gstatus.resolution_den);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 } else {
1481 gstatus.resolution_num = gstatus.resolution;
1482 gstatus.resolution_den = 1000000000uL;
1483 }
1484 } else {
1485 err = -ENODEV;
1486 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001487 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1489 err = -EFAULT;
1490 return err;
1491}
1492
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001493static int snd_timer_user_tselect(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001494 struct snd_timer_select __user *_tselect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001496 struct snd_timer_user *tu;
1497 struct snd_timer_select tselect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 char str[32];
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001499 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 tu = file->private_data;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001502 mutex_lock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001503 if (tu->timeri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 snd_timer_close(tu->timeri);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001505 tu->timeri = NULL;
1506 }
1507 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1508 err = -EFAULT;
1509 goto __err;
1510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 sprintf(str, "application %i", current->pid);
1512 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1513 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001514 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1515 if (err < 0)
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001516 goto __err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Jesper Juhl4d572772005-05-30 17:30:32 +02001518 kfree(tu->queue);
1519 tu->queue = NULL;
1520 kfree(tu->tqueue);
1521 tu->tqueue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001523 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001524 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001525 if (tu->tqueue == NULL)
1526 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001528 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001529 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001530 if (tu->queue == NULL)
1531 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001533
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001534 if (err < 0) {
1535 snd_timer_close(tu->timeri);
1536 tu->timeri = NULL;
1537 } else {
1538 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001539 tu->timeri->callback = tu->tread
1540 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001541 tu->timeri->ccallback = snd_timer_user_ccallback;
1542 tu->timeri->callback_data = (void *)tu;
1543 }
1544
1545 __err:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001546 mutex_unlock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001547 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001550static int snd_timer_user_info(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001551 struct snd_timer_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001553 struct snd_timer_user *tu;
1554 struct snd_timer_info *info;
1555 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 int err = 0;
1557
1558 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001559 if (!tu->timeri)
1560 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001562 if (!t)
1563 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Takashi Iwaica2c0962005-09-09 14:20:23 +02001565 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (! info)
1567 return -ENOMEM;
1568 info->card = t->card ? t->card->number : -1;
1569 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1570 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1571 strlcpy(info->id, t->id, sizeof(info->id));
1572 strlcpy(info->name, t->name, sizeof(info->name));
1573 info->resolution = t->hw.resolution;
1574 if (copy_to_user(_info, info, sizeof(*_info)))
1575 err = -EFAULT;
1576 kfree(info);
1577 return err;
1578}
1579
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001580static int snd_timer_user_params(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001581 struct snd_timer_params __user *_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001583 struct snd_timer_user *tu;
1584 struct snd_timer_params params;
1585 struct snd_timer *t;
1586 struct snd_timer_read *tr;
1587 struct snd_timer_tread *ttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001591 if (!tu->timeri)
1592 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001594 if (!t)
1595 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (copy_from_user(&params, _params, sizeof(params)))
1597 return -EFAULT;
1598 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1599 err = -EINVAL;
1600 goto _end;
1601 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001602 if (params.queue_size > 0 &&
1603 (params.queue_size < 32 || params.queue_size > 1024)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 err = -EINVAL;
1605 goto _end;
1606 }
1607 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1608 (1<<SNDRV_TIMER_EVENT_TICK)|
1609 (1<<SNDRV_TIMER_EVENT_START)|
1610 (1<<SNDRV_TIMER_EVENT_STOP)|
1611 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1612 (1<<SNDRV_TIMER_EVENT_PAUSE)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001613 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1614 (1<<SNDRV_TIMER_EVENT_RESUME)|
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 (1<<SNDRV_TIMER_EVENT_MSTART)|
1616 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1617 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
Jaroslav Kysela65d11d92005-08-16 13:05:43 +02001618 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1619 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001620 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 err = -EINVAL;
1622 goto _end;
1623 }
1624 snd_timer_stop(tu->timeri);
1625 spin_lock_irq(&t->lock);
1626 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1627 SNDRV_TIMER_IFLG_EXCLUSIVE|
1628 SNDRV_TIMER_IFLG_EARLY_EVENT);
1629 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1630 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1631 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1632 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1633 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1634 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1635 spin_unlock_irq(&t->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001636 if (params.queue_size > 0 &&
1637 (unsigned int)tu->queue_size != params.queue_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (tu->tread) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001639 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1640 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (ttr) {
1642 kfree(tu->tqueue);
1643 tu->queue_size = params.queue_size;
1644 tu->tqueue = ttr;
1645 }
1646 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001647 tr = kmalloc(params.queue_size * sizeof(*tr),
1648 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 if (tr) {
1650 kfree(tu->queue);
1651 tu->queue_size = params.queue_size;
1652 tu->queue = tr;
1653 }
1654 }
1655 }
1656 tu->qhead = tu->qtail = tu->qused = 0;
1657 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1658 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001659 struct snd_timer_tread tread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 tread.event = SNDRV_TIMER_EVENT_EARLY;
1661 tread.tstamp.tv_sec = 0;
1662 tread.tstamp.tv_nsec = 0;
1663 tread.val = 0;
1664 snd_timer_user_append_to_tqueue(tu, &tread);
1665 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001666 struct snd_timer_read *r = &tu->queue[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 r->resolution = 0;
1668 r->ticks = 0;
1669 tu->qused++;
1670 tu->qtail++;
1671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673 tu->filter = params.filter;
1674 tu->ticks = params.ticks;
1675 err = 0;
1676 _end:
1677 if (copy_to_user(_params, &params, sizeof(params)))
1678 return -EFAULT;
1679 return err;
1680}
1681
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001682static int snd_timer_user_status(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001683 struct snd_timer_status __user *_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001685 struct snd_timer_user *tu;
1686 struct snd_timer_status status;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001689 if (!tu->timeri)
1690 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 memset(&status, 0, sizeof(status));
1692 status.tstamp = tu->tstamp;
1693 status.resolution = snd_timer_resolution(tu->timeri);
1694 status.lost = tu->timeri->lost;
1695 status.overrun = tu->overrun;
1696 spin_lock_irq(&tu->qlock);
1697 status.queue = tu->qused;
1698 spin_unlock_irq(&tu->qlock);
1699 if (copy_to_user(_status, &status, sizeof(status)))
1700 return -EFAULT;
1701 return 0;
1702}
1703
1704static int snd_timer_user_start(struct file *file)
1705{
1706 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001707 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001710 if (!tu->timeri)
1711 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 snd_timer_stop(tu->timeri);
1713 tu->timeri->lost = 0;
1714 tu->last_resolution = 0;
1715 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1716}
1717
1718static int snd_timer_user_stop(struct file *file)
1719{
1720 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001721 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001724 if (!tu->timeri)
1725 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1727}
1728
1729static int snd_timer_user_continue(struct file *file)
1730{
1731 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001732 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001735 if (!tu->timeri)
1736 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 tu->timeri->lost = 0;
1738 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1739}
1740
Takashi Iwai15790a62005-05-15 15:04:14 +02001741static int snd_timer_user_pause(struct file *file)
1742{
1743 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001744 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001745
Takashi Iwai15790a62005-05-15 15:04:14 +02001746 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001747 if (!tu->timeri)
1748 return -EBADFD;
Jaroslav Kyselad138b442005-05-16 13:51:39 +02001749 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
Takashi Iwai15790a62005-05-15 15:04:14 +02001750}
1751
Takashi Iwai8c50b372005-05-15 15:43:54 +02001752enum {
1753 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1754 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1755 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1756 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1757};
1758
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001759static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1760 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001762 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 void __user *argp = (void __user *)arg;
1764 int __user *p = argp;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 tu = file->private_data;
1767 switch (cmd) {
1768 case SNDRV_TIMER_IOCTL_PVERSION:
1769 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1770 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1771 return snd_timer_user_next_device(argp);
1772 case SNDRV_TIMER_IOCTL_TREAD:
1773 {
1774 int xarg;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001775
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001776 mutex_lock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001777 if (tu->timeri) { /* too late */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001778 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return -EBUSY;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001780 }
1781 if (get_user(xarg, p)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001782 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return -EFAULT;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 tu->tread = xarg ? 1 : 0;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001786 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return 0;
1788 }
1789 case SNDRV_TIMER_IOCTL_GINFO:
1790 return snd_timer_user_ginfo(file, argp);
1791 case SNDRV_TIMER_IOCTL_GPARAMS:
1792 return snd_timer_user_gparams(file, argp);
1793 case SNDRV_TIMER_IOCTL_GSTATUS:
1794 return snd_timer_user_gstatus(file, argp);
1795 case SNDRV_TIMER_IOCTL_SELECT:
1796 return snd_timer_user_tselect(file, argp);
1797 case SNDRV_TIMER_IOCTL_INFO:
1798 return snd_timer_user_info(file, argp);
1799 case SNDRV_TIMER_IOCTL_PARAMS:
1800 return snd_timer_user_params(file, argp);
1801 case SNDRV_TIMER_IOCTL_STATUS:
1802 return snd_timer_user_status(file, argp);
1803 case SNDRV_TIMER_IOCTL_START:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001804 case SNDRV_TIMER_IOCTL_START_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return snd_timer_user_start(file);
1806 case SNDRV_TIMER_IOCTL_STOP:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001807 case SNDRV_TIMER_IOCTL_STOP_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return snd_timer_user_stop(file);
1809 case SNDRV_TIMER_IOCTL_CONTINUE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001810 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return snd_timer_user_continue(file);
Takashi Iwai15790a62005-05-15 15:04:14 +02001812 case SNDRV_TIMER_IOCTL_PAUSE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001813 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
Takashi Iwai15790a62005-05-15 15:04:14 +02001814 return snd_timer_user_pause(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
1816 return -ENOTTY;
1817}
1818
1819static int snd_timer_user_fasync(int fd, struct file * file, int on)
1820{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001821 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 tu = file->private_data;
1825 err = fasync_helper(fd, file, on, &tu->fasync);
1826 if (err < 0)
1827 return err;
1828 return 0;
1829}
1830
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001831static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1832 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001834 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 long result = 0, unit;
1836 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 tu = file->private_data;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001839 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 spin_lock_irq(&tu->qlock);
1841 while ((long)count - result >= unit) {
1842 while (!tu->qused) {
1843 wait_queue_t wait;
1844
1845 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1846 err = -EAGAIN;
1847 break;
1848 }
1849
1850 set_current_state(TASK_INTERRUPTIBLE);
1851 init_waitqueue_entry(&wait, current);
1852 add_wait_queue(&tu->qchange_sleep, &wait);
1853
1854 spin_unlock_irq(&tu->qlock);
1855 schedule();
1856 spin_lock_irq(&tu->qlock);
1857
1858 remove_wait_queue(&tu->qchange_sleep, &wait);
1859
1860 if (signal_pending(current)) {
1861 err = -ERESTARTSYS;
1862 break;
1863 }
1864 }
1865
1866 spin_unlock_irq(&tu->qlock);
1867 if (err < 0)
1868 goto _error;
1869
1870 if (tu->tread) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001871 if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
Takashi Iwai53d2f742005-11-17 13:56:05 +01001872 sizeof(struct snd_timer_tread))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 err = -EFAULT;
1874 goto _error;
1875 }
1876 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001877 if (copy_to_user(buffer, &tu->queue[tu->qhead++],
Takashi Iwai53d2f742005-11-17 13:56:05 +01001878 sizeof(struct snd_timer_read))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 err = -EFAULT;
1880 goto _error;
1881 }
1882 }
1883
1884 tu->qhead %= tu->queue_size;
1885
1886 result += unit;
1887 buffer += unit;
1888
1889 spin_lock_irq(&tu->qlock);
1890 tu->qused--;
1891 }
1892 spin_unlock_irq(&tu->qlock);
1893 _error:
1894 return result > 0 ? result : err;
1895}
1896
1897static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1898{
1899 unsigned int mask;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001900 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 tu = file->private_data;
1903
1904 poll_wait(file, &tu->qchange_sleep, wait);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 mask = 0;
1907 if (tu->qused)
1908 mask |= POLLIN | POLLRDNORM;
1909
1910 return mask;
1911}
1912
1913#ifdef CONFIG_COMPAT
1914#include "timer_compat.c"
1915#else
1916#define snd_timer_user_ioctl_compat NULL
1917#endif
1918
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001919static const struct file_operations snd_timer_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
1921 .owner = THIS_MODULE,
1922 .read = snd_timer_user_read,
1923 .open = snd_timer_user_open,
1924 .release = snd_timer_user_release,
1925 .poll = snd_timer_user_poll,
1926 .unlocked_ioctl = snd_timer_user_ioctl,
1927 .compat_ioctl = snd_timer_user_ioctl_compat,
1928 .fasync = snd_timer_user_fasync,
1929};
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931/*
1932 * ENTRY functions
1933 */
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935static int __init alsa_timer_init(void)
1936{
1937 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939#ifdef SNDRV_OSS_INFO_DEV_TIMERS
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001940 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
1941 "system timer");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942#endif
Takashi Iwaie28563c2005-12-01 10:42:42 +01001943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if ((err = snd_timer_register_system()) < 0)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001945 snd_printk(KERN_ERR "unable to register system timer (%i)\n",
1946 err);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001947 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
1948 &snd_timer_f_ops, NULL, "timer")) < 0)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001949 snd_printk(KERN_ERR "unable to register timer device (%i)\n",
1950 err);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001951 snd_timer_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return 0;
1953}
1954
1955static void __exit alsa_timer_exit(void)
1956{
1957 struct list_head *p, *n;
1958
1959 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
1960 /* unregister the system timer */
1961 list_for_each_safe(p, n, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001962 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
Takashi Iwaic4614822006-06-23 14:38:23 +02001963 snd_timer_free(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
Takashi Iwaie28563c2005-12-01 10:42:42 +01001965 snd_timer_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966#ifdef SNDRV_OSS_INFO_DEV_TIMERS
1967 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
1968#endif
1969}
1970
1971module_init(alsa_timer_init)
1972module_exit(alsa_timer_exit)
1973
1974EXPORT_SYMBOL(snd_timer_open);
1975EXPORT_SYMBOL(snd_timer_close);
1976EXPORT_SYMBOL(snd_timer_resolution);
1977EXPORT_SYMBOL(snd_timer_start);
1978EXPORT_SYMBOL(snd_timer_stop);
1979EXPORT_SYMBOL(snd_timer_continue);
1980EXPORT_SYMBOL(snd_timer_pause);
1981EXPORT_SYMBOL(snd_timer_new);
1982EXPORT_SYMBOL(snd_timer_notify);
1983EXPORT_SYMBOL(snd_timer_global_new);
1984EXPORT_SYMBOL(snd_timer_global_free);
1985EXPORT_SYMBOL(snd_timer_global_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986EXPORT_SYMBOL(snd_timer_interrupt);