blob: 5040c7b862fe6f571fb9a231e00819758ca2a632 [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
Johannes Bergee2da992008-07-09 10:28:41 +0200149#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
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);
Johannes Bergee2da992008-07-09 10:28:41 +0200262#ifdef CONFIG_MODULES
263 if (!timer) {
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
Takashi Iwai00728892008-08-14 07:51:57 +0200309 if (snd_BUG_ON(!timeri))
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200310 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 /* force to stop the timer */
313 snd_timer_stop(timeri);
314
315 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
316 /* wait, until the active callback is finished */
317 spin_lock_irq(&slave_active_lock);
318 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
319 spin_unlock_irq(&slave_active_lock);
320 udelay(10);
321 spin_lock_irq(&slave_active_lock);
322 }
323 spin_unlock_irq(&slave_active_lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100324 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 list_del(&timeri->open_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100326 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 } else {
328 timer = timeri->timer;
329 /* wait, until the active callback is finished */
330 spin_lock_irq(&timer->lock);
331 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
332 spin_unlock_irq(&timer->lock);
333 udelay(10);
334 spin_lock_irq(&timer->lock);
335 }
336 spin_unlock_irq(&timer->lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100337 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 list_del(&timeri->open_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200339 if (timer && list_empty(&timer->open_list_head) &&
340 timer->hw.close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 timer->hw.close(timer);
342 /* remove slave links */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200343 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
344 open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 spin_lock_irq(&slave_active_lock);
346 _snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200347 list_move_tail(&slave->open_list, &snd_timer_slave_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 slave->master = NULL;
349 slave->timer = NULL;
350 spin_unlock_irq(&slave_active_lock);
351 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100352 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 if (timeri->private_free)
355 timeri->private_free(timeri);
356 kfree(timeri->owner);
357 kfree(timeri);
Clemens Ladischde242142005-10-12 17:12:31 +0200358 if (timer)
359 module_put(timer->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361}
362
Takashi Iwai53d2f742005-11-17 13:56:05 +0100363unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100365 struct snd_timer * timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 if (timeri == NULL)
368 return 0;
369 if ((timer = timeri->timer) != NULL) {
370 if (timer->hw.c_resolution)
371 return timer->hw.c_resolution(timer);
372 return timer->hw.resolution;
373 }
374 return 0;
375}
376
Takashi Iwai53d2f742005-11-17 13:56:05 +0100377static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100379 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 unsigned long flags;
381 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100382 struct snd_timer_instance *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct timespec tstamp;
384
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100385 if (timer_tstamp_monotonic)
386 do_posix_clock_monotonic_gettime(&tstamp);
387 else
388 getnstimeofday(&tstamp);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200389 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
390 event > SNDRV_TIMER_EVENT_PAUSE))
391 return;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200392 if (event == SNDRV_TIMER_EVENT_START ||
393 event == SNDRV_TIMER_EVENT_CONTINUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 resolution = snd_timer_resolution(ti);
395 if (ti->ccallback)
Jaroslav Kyselab30477d2010-03-03 11:05:55 +0100396 ti->ccallback(ti, event, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
398 return;
399 timer = ti->timer;
400 if (timer == NULL)
401 return;
402 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
403 return;
404 spin_lock_irqsave(&timer->lock, flags);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200405 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (ts->ccallback)
407 ts->ccallback(ti, event + 100, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 spin_unlock_irqrestore(&timer->lock, flags);
409}
410
Takashi Iwai53d2f742005-11-17 13:56:05 +0100411static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200412 unsigned long sticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 list_del(&timeri->active_list);
415 list_add_tail(&timeri->active_list, &timer->active_list_head);
416 if (timer->running) {
417 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
418 goto __start_now;
419 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
420 timeri->flags |= SNDRV_TIMER_IFLG_START;
421 return 1; /* delayed start */
422 } else {
423 timer->sticks = sticks;
424 timer->hw.start(timer);
425 __start_now:
426 timer->running++;
427 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
428 return 0;
429 }
430}
431
Takashi Iwai53d2f742005-11-17 13:56:05 +0100432static int snd_timer_start_slave(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 unsigned long flags;
435
436 spin_lock_irqsave(&slave_active_lock, flags);
437 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
438 if (timeri->master)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200439 list_add_tail(&timeri->active_list,
440 &timeri->master->slave_active_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 spin_unlock_irqrestore(&slave_active_lock, flags);
442 return 1; /* delayed start */
443}
444
445/*
446 * start the timer instance
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200447 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100448int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100450 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 int result = -EINVAL;
452 unsigned long flags;
453
454 if (timeri == NULL || ticks < 1)
455 return -EINVAL;
456 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
457 result = snd_timer_start_slave(timeri);
458 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
459 return result;
460 }
461 timer = timeri->timer;
462 if (timer == NULL)
463 return -EINVAL;
464 spin_lock_irqsave(&timer->lock, flags);
465 timeri->ticks = timeri->cticks = ticks;
466 timeri->pticks = 0;
467 result = snd_timer_start1(timer, timeri, ticks);
468 spin_unlock_irqrestore(&timer->lock, flags);
469 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
470 return result;
471}
472
Takashi Iwai53d2f742005-11-17 13:56:05 +0100473static int _snd_timer_stop(struct snd_timer_instance * timeri,
474 int keep_flag, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100476 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 unsigned long flags;
478
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200479 if (snd_BUG_ON(!timeri))
480 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
483 if (!keep_flag) {
484 spin_lock_irqsave(&slave_active_lock, flags);
485 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
486 spin_unlock_irqrestore(&slave_active_lock, flags);
487 }
488 goto __end;
489 }
490 timer = timeri->timer;
491 if (!timer)
492 return -EINVAL;
493 spin_lock_irqsave(&timer->lock, flags);
494 list_del_init(&timeri->ack_list);
495 list_del_init(&timeri->active_list);
496 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
497 !(--timer->running)) {
498 timer->hw.stop(timer);
499 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
500 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
501 snd_timer_reschedule(timer, 0);
502 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
503 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
504 timer->hw.start(timer);
505 }
506 }
507 }
508 if (!keep_flag)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200509 timeri->flags &=
510 ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 spin_unlock_irqrestore(&timer->lock, flags);
512 __end:
513 if (event != SNDRV_TIMER_EVENT_RESOLUTION)
514 snd_timer_notify1(timeri, event);
515 return 0;
516}
517
518/*
519 * stop the timer instance.
520 *
521 * do not call this from the timer callback!
522 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100523int snd_timer_stop(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100525 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 unsigned long flags;
527 int err;
528
529 err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
530 if (err < 0)
531 return err;
532 timer = timeri->timer;
533 spin_lock_irqsave(&timer->lock, flags);
534 timeri->cticks = timeri->ticks;
535 timeri->pticks = 0;
536 spin_unlock_irqrestore(&timer->lock, flags);
537 return 0;
538}
539
540/*
541 * start again.. the tick is kept.
542 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100543int snd_timer_continue(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100545 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int result = -EINVAL;
547 unsigned long flags;
548
549 if (timeri == NULL)
550 return result;
551 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
552 return snd_timer_start_slave(timeri);
553 timer = timeri->timer;
554 if (! timer)
555 return -EINVAL;
556 spin_lock_irqsave(&timer->lock, flags);
557 if (!timeri->cticks)
558 timeri->cticks = 1;
559 timeri->pticks = 0;
560 result = snd_timer_start1(timer, timeri, timer->sticks);
561 spin_unlock_irqrestore(&timer->lock, flags);
562 snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
563 return result;
564}
565
566/*
567 * pause.. remember the ticks left
568 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100569int snd_timer_pause(struct snd_timer_instance * timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
572}
573
574/*
575 * reschedule the timer
576 *
577 * start pending instances and check the scheduling ticks.
578 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
579 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100580static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100582 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 unsigned long ticks = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Johannes Berg9244b2c2006-10-05 16:02:22 +0200585 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (ti->flags & SNDRV_TIMER_IFLG_START) {
587 ti->flags &= ~SNDRV_TIMER_IFLG_START;
588 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
589 timer->running++;
590 }
591 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
592 if (ticks > ti->cticks)
593 ticks = ti->cticks;
594 }
595 }
596 if (ticks == ~0UL) {
597 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
598 return;
599 }
600 if (ticks > timer->hw.ticks)
601 ticks = timer->hw.ticks;
602 if (ticks_left != ticks)
603 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
604 timer->sticks = ticks;
605}
606
607/*
608 * timer tasklet
609 *
610 */
611static void snd_timer_tasklet(unsigned long arg)
612{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100613 struct snd_timer *timer = (struct snd_timer *) arg;
614 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 struct list_head *p;
616 unsigned long resolution, ticks;
Takashi Iwai2999ff52006-07-05 17:16:58 +0200617 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Takashi Iwai2999ff52006-07-05 17:16:58 +0200619 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /* now process all callbacks */
621 while (!list_empty(&timer->sack_list_head)) {
622 p = timer->sack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100623 ti = list_entry(p, struct snd_timer_instance, ack_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /* remove from ack_list and make empty */
626 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 ticks = ti->pticks;
629 ti->pticks = 0;
630 resolution = ti->resolution;
631
632 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
633 spin_unlock(&timer->lock);
634 if (ti->callback)
635 ti->callback(ti, resolution, ticks);
636 spin_lock(&timer->lock);
637 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
638 }
Takashi Iwai2999ff52006-07-05 17:16:58 +0200639 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642/*
643 * timer interrupt
644 *
645 * ticks_left is usually equal to timer->sticks.
646 *
647 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100648void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200650 struct snd_timer_instance *ti, *ts, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 unsigned long resolution, ticks;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200652 struct list_head *p, *ack_list_head;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100653 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 int use_tasklet = 0;
655
656 if (timer == NULL)
657 return;
658
Takashi Iwaib32425a2005-11-18 18:52:14 +0100659 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 /* remember the current resolution */
662 if (timer->hw.c_resolution)
663 resolution = timer->hw.c_resolution(timer);
664 else
665 resolution = timer->hw.resolution;
666
667 /* loop for all active instances
Johannes Berg9244b2c2006-10-05 16:02:22 +0200668 * Here we cannot use list_for_each_entry because the active_list of a
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200669 * processed instance is relinked to done_list_head before the callback
670 * is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200672 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
673 active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
675 continue;
676 ti->pticks += ticks_left;
677 ti->resolution = resolution;
678 if (ti->cticks < ticks_left)
679 ti->cticks = 0;
680 else
681 ti->cticks -= ticks_left;
682 if (ti->cticks) /* not expired */
683 continue;
684 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
685 ti->cticks = ti->ticks;
686 } else {
687 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
688 if (--timer->running)
Johannes Berg9244b2c2006-10-05 16:02:22 +0200689 list_del(&ti->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200691 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
692 (ti->flags & SNDRV_TIMER_IFLG_FAST))
693 ack_list_head = &timer->ack_list_head;
694 else
695 ack_list_head = &timer->sack_list_head;
696 if (list_empty(&ti->ack_list))
697 list_add_tail(&ti->ack_list, ack_list_head);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200698 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 ts->pticks = ti->pticks;
700 ts->resolution = resolution;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200701 if (list_empty(&ts->ack_list))
702 list_add_tail(&ts->ack_list, ack_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704 }
705 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
Clemens Ladischcd93fe42006-07-17 16:53:57 +0200706 snd_timer_reschedule(timer, timer->sticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (timer->running) {
708 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
709 timer->hw.stop(timer);
710 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
711 }
712 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
713 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
714 /* restart timer */
715 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
716 timer->hw.start(timer);
717 }
718 } else {
719 timer->hw.stop(timer);
720 }
721
722 /* now process all fast callbacks */
723 while (!list_empty(&timer->ack_list_head)) {
724 p = timer->ack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100725 ti = list_entry(p, struct snd_timer_instance, ack_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* remove from ack_list and make empty */
728 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 ticks = ti->pticks;
731 ti->pticks = 0;
732
733 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
734 spin_unlock(&timer->lock);
735 if (ti->callback)
736 ti->callback(ti, resolution, ticks);
737 spin_lock(&timer->lock);
738 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
739 }
740
741 /* do we have any slow callbacks? */
742 use_tasklet = !list_empty(&timer->sack_list_head);
Takashi Iwaib32425a2005-11-18 18:52:14 +0100743 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 if (use_tasklet)
Takashi Iwai1f041282008-12-18 12:17:55 +0100746 tasklet_schedule(&timer->task_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749/*
750
751 */
752
Takashi Iwai53d2f742005-11-17 13:56:05 +0100753int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
754 struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100756 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100758 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 .dev_free = snd_timer_dev_free,
760 .dev_register = snd_timer_dev_register,
Takashi Iwaic4614822006-06-23 14:38:23 +0200761 .dev_disconnect = snd_timer_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 };
763
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200764 if (snd_BUG_ON(!tid))
765 return -EINVAL;
766 if (rtimer)
767 *rtimer = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200768 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100769 if (timer == NULL) {
770 snd_printk(KERN_ERR "timer: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 timer->tmr_class = tid->dev_class;
774 timer->card = card;
775 timer->tmr_device = tid->device;
776 timer->tmr_subdevice = tid->subdevice;
777 if (id)
778 strlcpy(timer->id, id, sizeof(timer->id));
779 INIT_LIST_HEAD(&timer->device_list);
780 INIT_LIST_HEAD(&timer->open_list_head);
781 INIT_LIST_HEAD(&timer->active_list_head);
782 INIT_LIST_HEAD(&timer->ack_list_head);
783 INIT_LIST_HEAD(&timer->sack_list_head);
784 spin_lock_init(&timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200785 tasklet_init(&timer->task_queue, snd_timer_tasklet,
786 (unsigned long)timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (card != NULL) {
Clemens Ladischde242142005-10-12 17:12:31 +0200788 timer->module = card->module;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200789 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
790 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 snd_timer_free(timer);
792 return err;
793 }
794 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200795 if (rtimer)
796 *rtimer = timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return 0;
798}
799
Takashi Iwai53d2f742005-11-17 13:56:05 +0100800static int snd_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200802 if (!timer)
803 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +0200804
805 mutex_lock(&register_mutex);
806 if (! list_empty(&timer->open_list_head)) {
807 struct list_head *p, *n;
808 struct snd_timer_instance *ti;
809 snd_printk(KERN_WARNING "timer %p is busy?\n", timer);
810 list_for_each_safe(p, n, &timer->open_list_head) {
811 list_del_init(p);
812 ti = list_entry(p, struct snd_timer_instance, open_list);
813 ti->timer = NULL;
814 }
815 }
816 list_del(&timer->device_list);
817 mutex_unlock(&register_mutex);
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (timer->private_free)
820 timer->private_free(timer);
821 kfree(timer);
822 return 0;
823}
824
Takashi Iwai53d2f742005-11-17 13:56:05 +0100825static int snd_timer_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100827 struct snd_timer *timer = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return snd_timer_free(timer);
829}
830
Takashi Iwai53d2f742005-11-17 13:56:05 +0100831static int snd_timer_dev_register(struct snd_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100833 struct snd_timer *timer = dev->device_data;
834 struct snd_timer *timer1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200836 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
837 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
839 !timer->hw.resolution && timer->hw.c_resolution == NULL)
840 return -EINVAL;
841
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100842 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200843 list_for_each_entry(timer1, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (timer1->tmr_class > timer->tmr_class)
845 break;
846 if (timer1->tmr_class < timer->tmr_class)
847 continue;
848 if (timer1->card && timer->card) {
849 if (timer1->card->number > timer->card->number)
850 break;
851 if (timer1->card->number < timer->card->number)
852 continue;
853 }
854 if (timer1->tmr_device > timer->tmr_device)
855 break;
856 if (timer1->tmr_device < timer->tmr_device)
857 continue;
858 if (timer1->tmr_subdevice > timer->tmr_subdevice)
859 break;
860 if (timer1->tmr_subdevice < timer->tmr_subdevice)
861 continue;
862 /* conflicts.. */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100863 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return -EBUSY;
865 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200866 list_add_tail(&timer->device_list, &timer1->device_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100867 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 0;
869}
870
Takashi Iwaic4614822006-06-23 14:38:23 +0200871static int snd_timer_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100873 struct snd_timer *timer = device->device_data;
Takashi Iwaic4614822006-06-23 14:38:23 +0200874 mutex_lock(&register_mutex);
875 list_del_init(&timer->device_list);
876 mutex_unlock(&register_mutex);
877 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Takashi Iwai53d2f742005-11-17 13:56:05 +0100880void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 unsigned long flags;
883 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100884 struct snd_timer_instance *ti, *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200886 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
887 return;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200888 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
889 event > SNDRV_TIMER_EVENT_MRESUME))
890 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 spin_lock_irqsave(&timer->lock, flags);
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +0200892 if (event == SNDRV_TIMER_EVENT_MSTART ||
893 event == SNDRV_TIMER_EVENT_MCONTINUE ||
894 event == SNDRV_TIMER_EVENT_MRESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (timer->hw.c_resolution)
896 resolution = timer->hw.c_resolution(timer);
897 else
898 resolution = timer->hw.resolution;
899 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200900 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (ti->ccallback)
902 ti->ccallback(ti, event, tstamp, resolution);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200903 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (ts->ccallback)
905 ts->ccallback(ts, event, tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907 spin_unlock_irqrestore(&timer->lock, flags);
908}
909
910/*
911 * exported functions for global timers
912 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100913int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100915 struct snd_timer_id tid;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
918 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
919 tid.card = -1;
920 tid.device = device;
921 tid.subdevice = 0;
922 return snd_timer_new(NULL, id, &tid, rtimer);
923}
924
Takashi Iwai53d2f742005-11-17 13:56:05 +0100925int snd_timer_global_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 return snd_timer_free(timer);
928}
929
Takashi Iwai53d2f742005-11-17 13:56:05 +0100930int snd_timer_global_register(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100932 struct snd_device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 memset(&dev, 0, sizeof(dev));
935 dev.device_data = timer;
936 return snd_timer_dev_register(&dev);
937}
938
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200939/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 * System timer
941 */
942
943struct snd_timer_system_private {
944 struct timer_list tlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 unsigned long last_expires;
946 unsigned long last_jiffies;
947 unsigned long correction;
948};
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950static void snd_timer_s_function(unsigned long data)
951{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100952 struct snd_timer *timer = (struct snd_timer *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 struct snd_timer_system_private *priv = timer->private_data;
954 unsigned long jiff = jiffies;
955 if (time_after(jiff, priv->last_expires))
Clemens Ladisch6ed5eff2006-07-17 16:51:37 +0200956 priv->correction += (long)jiff - (long)priv->last_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
958}
959
Takashi Iwai53d2f742005-11-17 13:56:05 +0100960static int snd_timer_s_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 struct snd_timer_system_private *priv;
963 unsigned long njiff;
964
965 priv = (struct snd_timer_system_private *) timer->private_data;
966 njiff = (priv->last_jiffies = jiffies);
967 if (priv->correction > timer->sticks - 1) {
968 priv->correction -= timer->sticks - 1;
969 njiff++;
970 } else {
971 njiff += timer->sticks - priv->correction;
Clemens Ladisch17f48ec2006-07-17 16:50:56 +0200972 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974 priv->last_expires = priv->tlist.expires = njiff;
975 add_timer(&priv->tlist);
976 return 0;
977}
978
Takashi Iwai53d2f742005-11-17 13:56:05 +0100979static int snd_timer_s_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 struct snd_timer_system_private *priv;
982 unsigned long jiff;
983
984 priv = (struct snd_timer_system_private *) timer->private_data;
985 del_timer(&priv->tlist);
986 jiff = jiffies;
987 if (time_before(jiff, priv->last_expires))
988 timer->sticks = priv->last_expires - jiff;
989 else
990 timer->sticks = 1;
Clemens Ladischde2696d2006-07-17 16:52:09 +0200991 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return 0;
993}
994
Takashi Iwai53d2f742005-11-17 13:56:05 +0100995static struct snd_timer_hardware snd_timer_system =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
998 .resolution = 1000000000L / HZ,
999 .ticks = 10000000L,
1000 .start = snd_timer_s_start,
1001 .stop = snd_timer_s_stop
1002};
1003
Takashi Iwai53d2f742005-11-17 13:56:05 +01001004static void snd_timer_free_system(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 kfree(timer->private_data);
1007}
1008
1009static int snd_timer_register_system(void)
1010{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001011 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 struct snd_timer_system_private *priv;
1013 int err;
1014
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001015 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1016 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return err;
1018 strcpy(timer->name, "system timer");
1019 timer->hw = snd_timer_system;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001020 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (priv == NULL) {
1022 snd_timer_free(timer);
1023 return -ENOMEM;
1024 }
1025 init_timer(&priv->tlist);
1026 priv->tlist.function = snd_timer_s_function;
1027 priv->tlist.data = (unsigned long) timer;
1028 timer->private_data = priv;
1029 timer->private_free = snd_timer_free_system;
1030 return snd_timer_global_register(timer);
1031}
1032
Takashi Iwaie28563c2005-12-01 10:42:42 +01001033#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034/*
1035 * Info interface
1036 */
1037
Takashi Iwai53d2f742005-11-17 13:56:05 +01001038static void snd_timer_proc_read(struct snd_info_entry *entry,
1039 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001041 struct snd_timer *timer;
1042 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001044 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001045 list_for_each_entry(timer, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 switch (timer->tmr_class) {
1047 case SNDRV_TIMER_CLASS_GLOBAL:
1048 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1049 break;
1050 case SNDRV_TIMER_CLASS_CARD:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001051 snd_iprintf(buffer, "C%i-%i: ",
1052 timer->card->number, timer->tmr_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 break;
1054 case SNDRV_TIMER_CLASS_PCM:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001055 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1056 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 break;
1058 default:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001059 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1060 timer->card ? timer->card->number : -1,
1061 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063 snd_iprintf(buffer, "%s :", timer->name);
1064 if (timer->hw.resolution)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001065 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1066 timer->hw.resolution / 1000,
1067 timer->hw.resolution % 1000,
1068 timer->hw.ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1070 snd_iprintf(buffer, " SLAVE");
1071 snd_iprintf(buffer, "\n");
Johannes Berg9244b2c2006-10-05 16:02:22 +02001072 list_for_each_entry(ti, &timer->open_list_head, open_list)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001073 snd_iprintf(buffer, " Client %s : %s\n",
1074 ti->owner ? ti->owner : "unknown",
1075 ti->flags & (SNDRV_TIMER_IFLG_START |
1076 SNDRV_TIMER_IFLG_RUNNING)
1077 ? "running" : "stopped");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001079 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001082static struct snd_info_entry *snd_timer_proc_entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001083
1084static void __init snd_timer_proc_init(void)
1085{
1086 struct snd_info_entry *entry;
1087
1088 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1089 if (entry != NULL) {
Takashi Iwaie28563c2005-12-01 10:42:42 +01001090 entry->c.text.read = snd_timer_proc_read;
1091 if (snd_info_register(entry) < 0) {
1092 snd_info_free_entry(entry);
1093 entry = NULL;
1094 }
1095 }
1096 snd_timer_proc_entry = entry;
1097}
1098
1099static void __exit snd_timer_proc_done(void)
1100{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001101 snd_info_free_entry(snd_timer_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001102}
1103#else /* !CONFIG_PROC_FS */
1104#define snd_timer_proc_init()
1105#define snd_timer_proc_done()
1106#endif
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108/*
1109 * USER SPACE interface
1110 */
1111
Takashi Iwai53d2f742005-11-17 13:56:05 +01001112static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 unsigned long resolution,
1114 unsigned long ticks)
1115{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001116 struct snd_timer_user *tu = timeri->callback_data;
1117 struct snd_timer_read *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int prev;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 spin_lock(&tu->qlock);
1121 if (tu->qused > 0) {
1122 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1123 r = &tu->queue[prev];
1124 if (r->resolution == resolution) {
1125 r->ticks += ticks;
1126 goto __wake;
1127 }
1128 }
1129 if (tu->qused >= tu->queue_size) {
1130 tu->overrun++;
1131 } else {
1132 r = &tu->queue[tu->qtail++];
1133 tu->qtail %= tu->queue_size;
1134 r->resolution = resolution;
1135 r->ticks = ticks;
1136 tu->qused++;
1137 }
1138 __wake:
1139 spin_unlock(&tu->qlock);
1140 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1141 wake_up(&tu->qchange_sleep);
1142}
1143
Takashi Iwai53d2f742005-11-17 13:56:05 +01001144static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1145 struct snd_timer_tread *tread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 if (tu->qused >= tu->queue_size) {
1148 tu->overrun++;
1149 } else {
1150 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1151 tu->qtail %= tu->queue_size;
1152 tu->qused++;
1153 }
1154}
1155
Takashi Iwai53d2f742005-11-17 13:56:05 +01001156static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1157 int event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 struct timespec *tstamp,
1159 unsigned long resolution)
1160{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001161 struct snd_timer_user *tu = timeri->callback_data;
1162 struct snd_timer_tread r1;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001163 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001165 if (event >= SNDRV_TIMER_EVENT_START &&
1166 event <= SNDRV_TIMER_EVENT_PAUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 tu->tstamp = *tstamp;
1168 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1169 return;
1170 r1.event = event;
1171 r1.tstamp = *tstamp;
1172 r1.val = resolution;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001173 spin_lock_irqsave(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 snd_timer_user_append_to_tqueue(tu, &r1);
Dan Carpenterbfe70782010-04-28 10:29:14 +02001175 spin_unlock_irqrestore(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1177 wake_up(&tu->qchange_sleep);
1178}
1179
Takashi Iwai53d2f742005-11-17 13:56:05 +01001180static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 unsigned long resolution,
1182 unsigned long ticks)
1183{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001184 struct snd_timer_user *tu = timeri->callback_data;
1185 struct snd_timer_tread *r, r1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 struct timespec tstamp;
1187 int prev, append = 0;
1188
Takashi Iwai07799e72005-10-10 11:49:49 +02001189 memset(&tstamp, 0, sizeof(tstamp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 spin_lock(&tu->qlock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001191 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1192 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 spin_unlock(&tu->qlock);
1194 return;
1195 }
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001196 if (tu->last_resolution != resolution || ticks > 0) {
1197 if (timer_tstamp_monotonic)
1198 do_posix_clock_monotonic_gettime(&tstamp);
1199 else
1200 getnstimeofday(&tstamp);
1201 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001202 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1203 tu->last_resolution != resolution) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1205 r1.tstamp = tstamp;
1206 r1.val = resolution;
1207 snd_timer_user_append_to_tqueue(tu, &r1);
1208 tu->last_resolution = resolution;
1209 append++;
1210 }
1211 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1212 goto __wake;
1213 if (ticks == 0)
1214 goto __wake;
1215 if (tu->qused > 0) {
1216 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1217 r = &tu->tqueue[prev];
1218 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1219 r->tstamp = tstamp;
1220 r->val += ticks;
1221 append++;
1222 goto __wake;
1223 }
1224 }
1225 r1.event = SNDRV_TIMER_EVENT_TICK;
1226 r1.tstamp = tstamp;
1227 r1.val = ticks;
1228 snd_timer_user_append_to_tqueue(tu, &r1);
1229 append++;
1230 __wake:
1231 spin_unlock(&tu->qlock);
1232 if (append == 0)
1233 return;
1234 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1235 wake_up(&tu->qchange_sleep);
1236}
1237
1238static int snd_timer_user_open(struct inode *inode, struct file *file)
1239{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001240 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001241
Takashi Iwaica2c0962005-09-09 14:20:23 +02001242 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (tu == NULL)
1244 return -ENOMEM;
1245 spin_lock_init(&tu->qlock);
1246 init_waitqueue_head(&tu->qchange_sleep);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001247 mutex_init(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 tu->ticks = 1;
1249 tu->queue_size = 128;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001250 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001251 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (tu->queue == NULL) {
1253 kfree(tu);
1254 return -ENOMEM;
1255 }
1256 file->private_data = tu;
1257 return 0;
1258}
1259
1260static int snd_timer_user_release(struct inode *inode, struct file *file)
1261{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001262 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 if (file->private_data) {
1265 tu = file->private_data;
1266 file->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (tu->timeri)
1268 snd_timer_close(tu->timeri);
1269 kfree(tu->queue);
1270 kfree(tu->tqueue);
1271 kfree(tu);
1272 }
1273 return 0;
1274}
1275
Takashi Iwai53d2f742005-11-17 13:56:05 +01001276static void snd_timer_user_zero_id(struct snd_timer_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1279 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1280 id->card = -1;
1281 id->device = -1;
1282 id->subdevice = -1;
1283}
1284
Takashi Iwai53d2f742005-11-17 13:56:05 +01001285static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 id->dev_class = timer->tmr_class;
1288 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1289 id->card = timer->card ? timer->card->number : -1;
1290 id->device = timer->tmr_device;
1291 id->subdevice = timer->tmr_subdevice;
1292}
1293
Takashi Iwai53d2f742005-11-17 13:56:05 +01001294static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001296 struct snd_timer_id id;
1297 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 struct list_head *p;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (copy_from_user(&id, _tid, sizeof(id)))
1301 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001302 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (id.dev_class < 0) { /* first item */
1304 if (list_empty(&snd_timer_list))
1305 snd_timer_user_zero_id(&id);
1306 else {
Clemens Ladisch9dfba382005-10-12 17:14:55 +02001307 timer = list_entry(snd_timer_list.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001308 struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 snd_timer_user_copy_id(&id, timer);
1310 }
1311 } else {
1312 switch (id.dev_class) {
1313 case SNDRV_TIMER_CLASS_GLOBAL:
1314 id.device = id.device < 0 ? 0 : id.device + 1;
1315 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001316 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1318 snd_timer_user_copy_id(&id, timer);
1319 break;
1320 }
1321 if (timer->tmr_device >= id.device) {
1322 snd_timer_user_copy_id(&id, timer);
1323 break;
1324 }
1325 }
1326 if (p == &snd_timer_list)
1327 snd_timer_user_zero_id(&id);
1328 break;
1329 case SNDRV_TIMER_CLASS_CARD:
1330 case SNDRV_TIMER_CLASS_PCM:
1331 if (id.card < 0) {
1332 id.card = 0;
1333 } else {
1334 if (id.card < 0) {
1335 id.card = 0;
1336 } else {
1337 if (id.device < 0) {
1338 id.device = 0;
1339 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001340 if (id.subdevice < 0) {
1341 id.subdevice = 0;
1342 } else {
1343 id.subdevice++;
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346 }
1347 }
1348 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001349 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (timer->tmr_class > id.dev_class) {
1351 snd_timer_user_copy_id(&id, timer);
1352 break;
1353 }
1354 if (timer->tmr_class < id.dev_class)
1355 continue;
1356 if (timer->card->number > id.card) {
1357 snd_timer_user_copy_id(&id, timer);
1358 break;
1359 }
1360 if (timer->card->number < id.card)
1361 continue;
1362 if (timer->tmr_device > id.device) {
1363 snd_timer_user_copy_id(&id, timer);
1364 break;
1365 }
1366 if (timer->tmr_device < id.device)
1367 continue;
1368 if (timer->tmr_subdevice > id.subdevice) {
1369 snd_timer_user_copy_id(&id, timer);
1370 break;
1371 }
1372 if (timer->tmr_subdevice < id.subdevice)
1373 continue;
1374 snd_timer_user_copy_id(&id, timer);
1375 break;
1376 }
1377 if (p == &snd_timer_list)
1378 snd_timer_user_zero_id(&id);
1379 break;
1380 default:
1381 snd_timer_user_zero_id(&id);
1382 }
1383 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001384 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1386 return -EFAULT;
1387 return 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001388}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001390static int snd_timer_user_ginfo(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001391 struct snd_timer_ginfo __user *_ginfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001393 struct snd_timer_ginfo *ginfo;
1394 struct snd_timer_id tid;
1395 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 struct list_head *p;
1397 int err = 0;
1398
Li Zefanef44a1e2009-04-10 09:43:08 +08001399 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1400 if (IS_ERR(ginfo))
1401 return PTR_ERR(ginfo);
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 tid = ginfo->tid;
1404 memset(ginfo, 0, sizeof(*ginfo));
1405 ginfo->tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001406 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 t = snd_timer_find(&tid);
1408 if (t != NULL) {
1409 ginfo->card = t->card ? t->card->number : -1;
1410 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1411 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1412 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1413 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1414 ginfo->resolution = t->hw.resolution;
1415 if (t->hw.resolution_min > 0) {
1416 ginfo->resolution_min = t->hw.resolution_min;
1417 ginfo->resolution_max = t->hw.resolution_max;
1418 }
1419 list_for_each(p, &t->open_list_head) {
1420 ginfo->clients++;
1421 }
1422 } else {
1423 err = -ENODEV;
1424 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001425 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1427 err = -EFAULT;
1428 kfree(ginfo);
1429 return err;
1430}
1431
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001432static int snd_timer_user_gparams(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001433 struct snd_timer_gparams __user *_gparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001435 struct snd_timer_gparams gparams;
1436 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 int err;
1438
1439 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1440 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001441 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 t = snd_timer_find(&gparams.tid);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001443 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 err = -ENODEV;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001445 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001447 if (!list_empty(&t->open_list_head)) {
1448 err = -EBUSY;
1449 goto _error;
1450 }
1451 if (!t->hw.set_period) {
1452 err = -ENOSYS;
1453 goto _error;
1454 }
1455 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1456_error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001457 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return err;
1459}
1460
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001461static int snd_timer_user_gstatus(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001462 struct snd_timer_gstatus __user *_gstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001464 struct snd_timer_gstatus gstatus;
1465 struct snd_timer_id tid;
1466 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 int err = 0;
1468
1469 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1470 return -EFAULT;
1471 tid = gstatus.tid;
1472 memset(&gstatus, 0, sizeof(gstatus));
1473 gstatus.tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001474 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 t = snd_timer_find(&tid);
1476 if (t != NULL) {
1477 if (t->hw.c_resolution)
1478 gstatus.resolution = t->hw.c_resolution(t);
1479 else
1480 gstatus.resolution = t->hw.resolution;
1481 if (t->hw.precise_resolution) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001482 t->hw.precise_resolution(t, &gstatus.resolution_num,
1483 &gstatus.resolution_den);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 } else {
1485 gstatus.resolution_num = gstatus.resolution;
1486 gstatus.resolution_den = 1000000000uL;
1487 }
1488 } else {
1489 err = -ENODEV;
1490 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001491 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1493 err = -EFAULT;
1494 return err;
1495}
1496
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001497static int snd_timer_user_tselect(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001498 struct snd_timer_select __user *_tselect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001500 struct snd_timer_user *tu;
1501 struct snd_timer_select tselect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 char str[32];
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001503 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 tu = file->private_data;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001506 mutex_lock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001507 if (tu->timeri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 snd_timer_close(tu->timeri);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001509 tu->timeri = NULL;
1510 }
1511 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1512 err = -EFAULT;
1513 goto __err;
1514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 sprintf(str, "application %i", current->pid);
1516 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1517 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001518 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1519 if (err < 0)
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001520 goto __err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Jesper Juhl4d572772005-05-30 17:30:32 +02001522 kfree(tu->queue);
1523 tu->queue = NULL;
1524 kfree(tu->tqueue);
1525 tu->tqueue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001527 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001528 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001529 if (tu->tqueue == NULL)
1530 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001532 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001533 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001534 if (tu->queue == NULL)
1535 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001537
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001538 if (err < 0) {
1539 snd_timer_close(tu->timeri);
1540 tu->timeri = NULL;
1541 } else {
1542 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001543 tu->timeri->callback = tu->tread
1544 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001545 tu->timeri->ccallback = snd_timer_user_ccallback;
1546 tu->timeri->callback_data = (void *)tu;
1547 }
1548
1549 __err:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001550 mutex_unlock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001551 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001554static int snd_timer_user_info(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001555 struct snd_timer_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001557 struct snd_timer_user *tu;
1558 struct snd_timer_info *info;
1559 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 int err = 0;
1561
1562 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001563 if (!tu->timeri)
1564 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001566 if (!t)
1567 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Takashi Iwaica2c0962005-09-09 14:20:23 +02001569 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (! info)
1571 return -ENOMEM;
1572 info->card = t->card ? t->card->number : -1;
1573 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1574 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1575 strlcpy(info->id, t->id, sizeof(info->id));
1576 strlcpy(info->name, t->name, sizeof(info->name));
1577 info->resolution = t->hw.resolution;
1578 if (copy_to_user(_info, info, sizeof(*_info)))
1579 err = -EFAULT;
1580 kfree(info);
1581 return err;
1582}
1583
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001584static int snd_timer_user_params(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001585 struct snd_timer_params __user *_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001587 struct snd_timer_user *tu;
1588 struct snd_timer_params params;
1589 struct snd_timer *t;
1590 struct snd_timer_read *tr;
1591 struct snd_timer_tread *ttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001595 if (!tu->timeri)
1596 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001598 if (!t)
1599 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (copy_from_user(&params, _params, sizeof(params)))
1601 return -EFAULT;
1602 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1603 err = -EINVAL;
1604 goto _end;
1605 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001606 if (params.queue_size > 0 &&
1607 (params.queue_size < 32 || params.queue_size > 1024)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 err = -EINVAL;
1609 goto _end;
1610 }
1611 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1612 (1<<SNDRV_TIMER_EVENT_TICK)|
1613 (1<<SNDRV_TIMER_EVENT_START)|
1614 (1<<SNDRV_TIMER_EVENT_STOP)|
1615 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1616 (1<<SNDRV_TIMER_EVENT_PAUSE)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001617 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1618 (1<<SNDRV_TIMER_EVENT_RESUME)|
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 (1<<SNDRV_TIMER_EVENT_MSTART)|
1620 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1621 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
Jaroslav Kysela65d11d92005-08-16 13:05:43 +02001622 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1623 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001624 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 err = -EINVAL;
1626 goto _end;
1627 }
1628 snd_timer_stop(tu->timeri);
1629 spin_lock_irq(&t->lock);
1630 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1631 SNDRV_TIMER_IFLG_EXCLUSIVE|
1632 SNDRV_TIMER_IFLG_EARLY_EVENT);
1633 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1634 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1635 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1636 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1637 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1638 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1639 spin_unlock_irq(&t->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001640 if (params.queue_size > 0 &&
1641 (unsigned int)tu->queue_size != params.queue_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (tu->tread) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001643 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1644 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (ttr) {
1646 kfree(tu->tqueue);
1647 tu->queue_size = params.queue_size;
1648 tu->tqueue = ttr;
1649 }
1650 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001651 tr = kmalloc(params.queue_size * sizeof(*tr),
1652 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (tr) {
1654 kfree(tu->queue);
1655 tu->queue_size = params.queue_size;
1656 tu->queue = tr;
1657 }
1658 }
1659 }
1660 tu->qhead = tu->qtail = tu->qused = 0;
1661 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1662 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001663 struct snd_timer_tread tread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 tread.event = SNDRV_TIMER_EVENT_EARLY;
1665 tread.tstamp.tv_sec = 0;
1666 tread.tstamp.tv_nsec = 0;
1667 tread.val = 0;
1668 snd_timer_user_append_to_tqueue(tu, &tread);
1669 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001670 struct snd_timer_read *r = &tu->queue[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 r->resolution = 0;
1672 r->ticks = 0;
1673 tu->qused++;
1674 tu->qtail++;
1675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
1677 tu->filter = params.filter;
1678 tu->ticks = params.ticks;
1679 err = 0;
1680 _end:
1681 if (copy_to_user(_params, &params, sizeof(params)))
1682 return -EFAULT;
1683 return err;
1684}
1685
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001686static int snd_timer_user_status(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001687 struct snd_timer_status __user *_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001689 struct snd_timer_user *tu;
1690 struct snd_timer_status status;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001693 if (!tu->timeri)
1694 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 memset(&status, 0, sizeof(status));
1696 status.tstamp = tu->tstamp;
1697 status.resolution = snd_timer_resolution(tu->timeri);
1698 status.lost = tu->timeri->lost;
1699 status.overrun = tu->overrun;
1700 spin_lock_irq(&tu->qlock);
1701 status.queue = tu->qused;
1702 spin_unlock_irq(&tu->qlock);
1703 if (copy_to_user(_status, &status, sizeof(status)))
1704 return -EFAULT;
1705 return 0;
1706}
1707
1708static int snd_timer_user_start(struct file *file)
1709{
1710 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001711 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001714 if (!tu->timeri)
1715 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 snd_timer_stop(tu->timeri);
1717 tu->timeri->lost = 0;
1718 tu->last_resolution = 0;
1719 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1720}
1721
1722static int snd_timer_user_stop(struct file *file)
1723{
1724 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001725 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001728 if (!tu->timeri)
1729 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1731}
1732
1733static int snd_timer_user_continue(struct file *file)
1734{
1735 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001736 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001739 if (!tu->timeri)
1740 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 tu->timeri->lost = 0;
1742 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1743}
1744
Takashi Iwai15790a62005-05-15 15:04:14 +02001745static int snd_timer_user_pause(struct file *file)
1746{
1747 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001748 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001749
Takashi Iwai15790a62005-05-15 15:04:14 +02001750 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001751 if (!tu->timeri)
1752 return -EBADFD;
Jaroslav Kyselad138b442005-05-16 13:51:39 +02001753 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
Takashi Iwai15790a62005-05-15 15:04:14 +02001754}
1755
Takashi Iwai8c50b372005-05-15 15:43:54 +02001756enum {
1757 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1758 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1759 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1760 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1761};
1762
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001763static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1764 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001766 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 void __user *argp = (void __user *)arg;
1768 int __user *p = argp;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 tu = file->private_data;
1771 switch (cmd) {
1772 case SNDRV_TIMER_IOCTL_PVERSION:
1773 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1774 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1775 return snd_timer_user_next_device(argp);
1776 case SNDRV_TIMER_IOCTL_TREAD:
1777 {
1778 int xarg;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001779
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001780 mutex_lock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001781 if (tu->timeri) { /* too late */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001782 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return -EBUSY;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001784 }
1785 if (get_user(xarg, p)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001786 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return -EFAULT;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 tu->tread = xarg ? 1 : 0;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001790 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return 0;
1792 }
1793 case SNDRV_TIMER_IOCTL_GINFO:
1794 return snd_timer_user_ginfo(file, argp);
1795 case SNDRV_TIMER_IOCTL_GPARAMS:
1796 return snd_timer_user_gparams(file, argp);
1797 case SNDRV_TIMER_IOCTL_GSTATUS:
1798 return snd_timer_user_gstatus(file, argp);
1799 case SNDRV_TIMER_IOCTL_SELECT:
1800 return snd_timer_user_tselect(file, argp);
1801 case SNDRV_TIMER_IOCTL_INFO:
1802 return snd_timer_user_info(file, argp);
1803 case SNDRV_TIMER_IOCTL_PARAMS:
1804 return snd_timer_user_params(file, argp);
1805 case SNDRV_TIMER_IOCTL_STATUS:
1806 return snd_timer_user_status(file, argp);
1807 case SNDRV_TIMER_IOCTL_START:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001808 case SNDRV_TIMER_IOCTL_START_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return snd_timer_user_start(file);
1810 case SNDRV_TIMER_IOCTL_STOP:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001811 case SNDRV_TIMER_IOCTL_STOP_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return snd_timer_user_stop(file);
1813 case SNDRV_TIMER_IOCTL_CONTINUE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001814 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return snd_timer_user_continue(file);
Takashi Iwai15790a62005-05-15 15:04:14 +02001816 case SNDRV_TIMER_IOCTL_PAUSE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001817 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
Takashi Iwai15790a62005-05-15 15:04:14 +02001818 return snd_timer_user_pause(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
1820 return -ENOTTY;
1821}
1822
1823static int snd_timer_user_fasync(int fd, struct file * file, int on)
1824{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001825 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 tu = file->private_data;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001828 return fasync_helper(fd, file, on, &tu->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
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);