blob: ad65cd9f29aff4f7ccfad41e8377c370ac7b0fe1 [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>
Paul Gortmaker51990e82012-01-22 11:23:42 -050027#include <linux/device.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040028#include <linux/module.h>
Paulo Marques543537b2005-06-23 00:09:02 -070029#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/core.h>
31#include <sound/timer.h>
32#include <sound/control.h>
33#include <sound/info.h>
34#include <sound/minors.h>
35#include <sound/initval.h>
36#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +010038#if IS_ENABLED(CONFIG_SND_HRTIMER)
Clemens Ladisch109fef92010-11-18 09:53:54 +010039#define DEFAULT_TIMER_LIMIT 4
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +010040#elif IS_ENABLED(CONFIG_SND_RTCTIMER)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define DEFAULT_TIMER_LIMIT 2
42#else
43#define DEFAULT_TIMER_LIMIT 1
44#endif
45
46static int timer_limit = DEFAULT_TIMER_LIMIT;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +010047static int timer_tstamp_monotonic = 1;
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020048MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070049MODULE_DESCRIPTION("ALSA timer interface");
50MODULE_LICENSE("GPL");
51module_param(timer_limit, int, 0444);
52MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
Jaroslav Kyselab751eef2007-12-13 10:19:42 +010053module_param(timer_tstamp_monotonic, int, 0444);
54MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Kay Sievers03cfe6f2010-11-23 17:43:19 +010056MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
57MODULE_ALIAS("devname:snd/timer");
58
Takashi Iwai53d2f742005-11-17 13:56:05 +010059struct snd_timer_user {
60 struct snd_timer_instance *timeri;
Clemens Ladisch6b172a82005-10-12 17:20:21 +020061 int tread; /* enhanced read with timestamps and events */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 unsigned long ticks;
63 unsigned long overrun;
64 int qhead;
65 int qtail;
66 int qused;
67 int queue_size;
Takashi Iwai8d155be2016-01-21 17:19:31 +010068 bool disconnected;
Takashi Iwai53d2f742005-11-17 13:56:05 +010069 struct snd_timer_read *queue;
70 struct snd_timer_tread *tqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 spinlock_t qlock;
72 unsigned long last_resolution;
73 unsigned int filter;
74 struct timespec tstamp; /* trigger tstamp */
75 wait_queue_head_t qchange_sleep;
76 struct fasync_struct *fasync;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010077 struct mutex tread_sem;
Takashi Iwai53d2f742005-11-17 13:56:05 +010078};
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* list of timers */
81static LIST_HEAD(snd_timer_list);
82
83/* list of slave instances */
84static LIST_HEAD(snd_timer_slave_list);
85
86/* lock for slave active lists */
87static DEFINE_SPINLOCK(slave_active_lock);
88
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010089static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Takashi Iwai53d2f742005-11-17 13:56:05 +010091static int snd_timer_free(struct snd_timer *timer);
92static int snd_timer_dev_free(struct snd_device *device);
93static int snd_timer_dev_register(struct snd_device *device);
Takashi Iwaic4614822006-06-23 14:38:23 +020094static int snd_timer_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Takashi Iwai53d2f742005-11-17 13:56:05 +010096static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/*
99 * create a timer instance with the given owner string.
100 * when timer is not NULL, increments the module counter
101 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100102static struct snd_timer_instance *snd_timer_instance_new(char *owner,
103 struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100105 struct snd_timer_instance *timeri;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200106 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (timeri == NULL)
108 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700109 timeri->owner = kstrdup(owner, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (! timeri->owner) {
111 kfree(timeri);
112 return NULL;
113 }
114 INIT_LIST_HEAD(&timeri->open_list);
115 INIT_LIST_HEAD(&timeri->active_list);
116 INIT_LIST_HEAD(&timeri->ack_list);
117 INIT_LIST_HEAD(&timeri->slave_list_head);
118 INIT_LIST_HEAD(&timeri->slave_active_head);
119
120 timeri->timer = timer;
Clemens Ladischde242142005-10-12 17:12:31 +0200121 if (timer && !try_module_get(timer->module)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 kfree(timeri->owner);
123 kfree(timeri);
124 return NULL;
125 }
126
127 return timeri;
128}
129
130/*
131 * find a timer instance from the given timer id
132 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100133static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100135 struct snd_timer *timer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Johannes Berg9244b2c2006-10-05 16:02:22 +0200137 list_for_each_entry(timer, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (timer->tmr_class != tid->dev_class)
139 continue;
140 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
141 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
142 (timer->card == NULL ||
143 timer->card->number != tid->card))
144 continue;
145 if (timer->tmr_device != tid->device)
146 continue;
147 if (timer->tmr_subdevice != tid->subdevice)
148 continue;
149 return timer;
150 }
151 return NULL;
152}
153
Johannes Bergee2da992008-07-09 10:28:41 +0200154#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Takashi Iwai53d2f742005-11-17 13:56:05 +0100156static void snd_timer_request(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 switch (tid->dev_class) {
159 case SNDRV_TIMER_CLASS_GLOBAL:
160 if (tid->device < timer_limit)
161 request_module("snd-timer-%i", tid->device);
162 break;
163 case SNDRV_TIMER_CLASS_CARD:
164 case SNDRV_TIMER_CLASS_PCM:
165 if (tid->card < snd_ecards_limit)
166 request_module("snd-card-%i", tid->card);
167 break;
168 default:
169 break;
170 }
171}
172
173#endif
174
175/*
176 * look for a master instance matching with the slave id of the given slave.
177 * when found, relink the open_link of the slave.
178 *
179 * call this with register_mutex down.
180 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100181static void snd_timer_check_slave(struct snd_timer_instance *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100183 struct snd_timer *timer;
184 struct snd_timer_instance *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /* FIXME: it's really dumb to look up all entries.. */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200187 list_for_each_entry(timer, &snd_timer_list, device_list) {
188 list_for_each_entry(master, &timer->open_list_head, open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (slave->slave_class == master->slave_class &&
190 slave->slave_id == master->slave_id) {
Nicolas Kaiser5b7c7572011-03-16 17:10:11 +0100191 list_move_tail(&slave->open_list,
192 &master->slave_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 spin_lock_irq(&slave_active_lock);
194 slave->master = master;
195 slave->timer = master->timer;
196 spin_unlock_irq(&slave_active_lock);
197 return;
198 }
199 }
200 }
201}
202
203/*
204 * look for slave instances matching with the slave id of the given master.
205 * when found, relink the open_link of slaves.
206 *
207 * call this with register_mutex down.
208 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100209static void snd_timer_check_master(struct snd_timer_instance *master)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200211 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /* check all pending slaves */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200214 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (slave->slave_class == master->slave_class &&
216 slave->slave_id == master->slave_id) {
Johannes Berg9244b2c2006-10-05 16:02:22 +0200217 list_move_tail(&slave->open_list, &master->slave_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 spin_lock_irq(&slave_active_lock);
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100219 spin_lock(&master->timer->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 slave->master = master;
221 slave->timer = master->timer;
222 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200223 list_add_tail(&slave->active_list,
224 &master->slave_active_head);
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100225 spin_unlock(&master->timer->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 spin_unlock_irq(&slave_active_lock);
227 }
228 }
229}
230
231/*
232 * open a timer instance
233 * when opening a master, the slave id must be here given.
234 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100235int snd_timer_open(struct snd_timer_instance **ti,
236 char *owner, struct snd_timer_id *tid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 unsigned int slave_id)
238{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100239 struct snd_timer *timer;
240 struct snd_timer_instance *timeri = NULL;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
243 /* open a slave instance */
244 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
245 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100246 pr_debug("ALSA: timer: invalid slave class %i\n",
247 tid->dev_sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return -EINVAL;
249 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100250 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 timeri = snd_timer_instance_new(owner, NULL);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200252 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100253 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200254 return -ENOMEM;
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 timeri->slave_class = tid->dev_sclass;
257 timeri->slave_id = tid->device;
258 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
259 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
260 snd_timer_check_slave(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100261 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 *ti = timeri;
263 return 0;
264 }
265
266 /* open a master instance */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100267 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 timer = snd_timer_find(tid);
Johannes Bergee2da992008-07-09 10:28:41 +0200269#ifdef CONFIG_MODULES
270 if (!timer) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100271 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 snd_timer_request(tid);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100273 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 timer = snd_timer_find(tid);
275 }
276#endif
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200277 if (!timer) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100278 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return -ENODEV;
280 }
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200281 if (!list_empty(&timer->open_list_head)) {
282 timeri = list_entry(timer->open_list_head.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +0100283 struct snd_timer_instance, open_list);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200284 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100285 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200286 return -EBUSY;
287 }
288 }
289 timeri = snd_timer_instance_new(owner, timer);
290 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100291 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200292 return -ENOMEM;
293 }
Takashi Iwai8d155be2016-01-21 17:19:31 +0100294 /* take a card refcount for safe disconnection */
295 if (timer->card)
296 get_device(&timer->card->card_dev);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200297 timeri->slave_class = tid->dev_sclass;
298 timeri->slave_id = slave_id;
Vegard Nossum14e3a782016-08-29 00:33:51 +0200299
300 if (list_empty(&timer->open_list_head) && timer->hw.open) {
301 int err = timer->hw.open(timer);
302 if (err) {
303 kfree(timeri->owner);
304 kfree(timeri);
305
306 if (timer->card)
307 put_device(&timer->card->card_dev);
308 module_put(timer->module);
309 mutex_unlock(&register_mutex);
310 return err;
311 }
312 }
313
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200314 list_add_tail(&timeri->open_list, &timer->open_list_head);
315 snd_timer_check_master(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100316 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 *ti = timeri;
318 return 0;
319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321/*
322 * close a timer instance
323 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100324int snd_timer_close(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100326 struct snd_timer *timer = NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200327 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Takashi Iwai00728892008-08-14 07:51:57 +0200329 if (snd_BUG_ON(!timeri))
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200330 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* force to stop the timer */
333 snd_timer_stop(timeri);
334
335 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
336 /* wait, until the active callback is finished */
337 spin_lock_irq(&slave_active_lock);
338 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
339 spin_unlock_irq(&slave_active_lock);
340 udelay(10);
341 spin_lock_irq(&slave_active_lock);
342 }
343 spin_unlock_irq(&slave_active_lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100344 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 list_del(&timeri->open_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100346 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 } else {
348 timer = timeri->timer;
Takashi Iwai94094c82011-08-08 12:28:22 +0200349 if (snd_BUG_ON(!timer))
350 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /* wait, until the active callback is finished */
352 spin_lock_irq(&timer->lock);
353 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
354 spin_unlock_irq(&timer->lock);
355 udelay(10);
356 spin_lock_irq(&timer->lock);
357 }
358 spin_unlock_irq(&timer->lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100359 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 list_del(&timeri->open_list);
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100361 if (list_empty(&timer->open_list_head) &&
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200362 timer->hw.close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 timer->hw.close(timer);
364 /* remove slave links */
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100365 spin_lock_irq(&slave_active_lock);
366 spin_lock(&timer->lock);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200367 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
368 open_list) {
Johannes Berg9244b2c2006-10-05 16:02:22 +0200369 list_move_tail(&slave->open_list, &snd_timer_slave_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 slave->master = NULL;
371 slave->timer = NULL;
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100372 list_del_init(&slave->ack_list);
373 list_del_init(&slave->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100375 spin_unlock(&timer->lock);
376 spin_unlock_irq(&slave_active_lock);
Takashi Iwai8d155be2016-01-21 17:19:31 +0100377 /* release a card refcount for safe disconnection */
378 if (timer->card)
379 put_device(&timer->card->card_dev);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100380 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
Takashi Iwai94094c82011-08-08 12:28:22 +0200382 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (timeri->private_free)
384 timeri->private_free(timeri);
385 kfree(timeri->owner);
386 kfree(timeri);
Clemens Ladischde242142005-10-12 17:12:31 +0200387 if (timer)
388 module_put(timer->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 0;
390}
391
Takashi Iwai53d2f742005-11-17 13:56:05 +0100392unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100394 struct snd_timer * timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 if (timeri == NULL)
397 return 0;
398 if ((timer = timeri->timer) != NULL) {
399 if (timer->hw.c_resolution)
400 return timer->hw.c_resolution(timer);
401 return timer->hw.resolution;
402 }
403 return 0;
404}
405
Takashi Iwai53d2f742005-11-17 13:56:05 +0100406static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100408 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100410 struct snd_timer_instance *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 struct timespec tstamp;
412
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100413 if (timer_tstamp_monotonic)
Thomas Gleixner26204e02014-06-11 23:59:14 +0000414 ktime_get_ts(&tstamp);
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100415 else
416 getnstimeofday(&tstamp);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200417 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
418 event > SNDRV_TIMER_EVENT_PAUSE))
419 return;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200420 if (event == SNDRV_TIMER_EVENT_START ||
421 event == SNDRV_TIMER_EVENT_CONTINUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 resolution = snd_timer_resolution(ti);
423 if (ti->ccallback)
Jaroslav Kyselab30477d2010-03-03 11:05:55 +0100424 ti->ccallback(ti, event, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
426 return;
427 timer = ti->timer;
428 if (timer == NULL)
429 return;
430 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
431 return;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200432 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (ts->ccallback)
Takashi Iwai77d0a942016-02-08 17:36:25 +0100434 ts->ccallback(ts, event + 100, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Takashi Iwai11740142016-02-10 12:47:03 +0100437/* start/continue a master timer */
438static int snd_timer_start1(struct snd_timer_instance *timeri,
439 bool start, unsigned long ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Takashi Iwai11740142016-02-10 12:47:03 +0100441 struct snd_timer *timer;
442 int result;
443 unsigned long flags;
444
445 timer = timeri->timer;
446 if (!timer)
447 return -EINVAL;
448
449 spin_lock_irqsave(&timer->lock, flags);
450 if (timer->card && timer->card->shutdown) {
451 result = -ENODEV;
452 goto unlock;
453 }
454 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
455 SNDRV_TIMER_IFLG_START)) {
456 result = -EBUSY;
457 goto unlock;
458 }
459
460 if (start)
461 timeri->ticks = timeri->cticks = ticks;
462 else if (!timeri->cticks)
463 timeri->cticks = 1;
464 timeri->pticks = 0;
465
Nicolas Kaiser5b7c7572011-03-16 17:10:11 +0100466 list_move_tail(&timeri->active_list, &timer->active_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (timer->running) {
468 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
469 goto __start_now;
470 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
471 timeri->flags |= SNDRV_TIMER_IFLG_START;
Takashi Iwai11740142016-02-10 12:47:03 +0100472 result = 1; /* delayed start */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 } else {
Takashi Iwai11740142016-02-10 12:47:03 +0100474 if (start)
475 timer->sticks = ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 timer->hw.start(timer);
477 __start_now:
478 timer->running++;
479 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwai11740142016-02-10 12:47:03 +0100480 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Takashi Iwai11740142016-02-10 12:47:03 +0100482 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
483 SNDRV_TIMER_EVENT_CONTINUE);
484 unlock:
485 spin_unlock_irqrestore(&timer->lock, flags);
486 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Takashi Iwai11740142016-02-10 12:47:03 +0100489/* start/continue a slave timer */
490static int snd_timer_start_slave(struct snd_timer_instance *timeri,
491 bool start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 unsigned long flags;
494
495 spin_lock_irqsave(&slave_active_lock, flags);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100496 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
497 spin_unlock_irqrestore(&slave_active_lock, flags);
498 return -EBUSY;
499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100501 if (timeri->master && timeri->timer) {
502 spin_lock(&timeri->timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200503 list_add_tail(&timeri->active_list,
504 &timeri->master->slave_active_head);
Takashi Iwai11740142016-02-10 12:47:03 +0100505 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
506 SNDRV_TIMER_EVENT_CONTINUE);
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100507 spin_unlock(&timeri->timer->lock);
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 spin_unlock_irqrestore(&slave_active_lock, flags);
510 return 1; /* delayed start */
511}
512
Takashi Iwai11740142016-02-10 12:47:03 +0100513/* stop/pause a master timer */
514static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100516 struct snd_timer *timer;
Takashi Iwai11740142016-02-10 12:47:03 +0100517 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 unsigned long flags;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 timer = timeri->timer;
521 if (!timer)
522 return -EINVAL;
523 spin_lock_irqsave(&timer->lock, flags);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100524 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
525 SNDRV_TIMER_IFLG_START))) {
Takashi Iwai11740142016-02-10 12:47:03 +0100526 result = -EBUSY;
527 goto unlock;
Takashi Iwai9f750af2016-01-30 23:09:08 +0100528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 list_del_init(&timeri->ack_list);
530 list_del_init(&timeri->active_list);
Takashi Iwai11740142016-02-10 12:47:03 +0100531 if (timer->card && timer->card->shutdown)
532 goto unlock;
533 if (stop) {
534 timeri->cticks = timeri->ticks;
535 timeri->pticks = 0;
Takashi Iwai8d155be2016-01-21 17:19:31 +0100536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
538 !(--timer->running)) {
539 timer->hw.stop(timer);
540 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
541 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
542 snd_timer_reschedule(timer, 0);
543 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
544 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
545 timer->hw.start(timer);
546 }
547 }
548 }
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100549 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
Takashi Iwai11740142016-02-10 12:47:03 +0100550 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
551 SNDRV_TIMER_EVENT_CONTINUE);
552 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 spin_unlock_irqrestore(&timer->lock, flags);
Takashi Iwai11740142016-02-10 12:47:03 +0100554 return result;
555}
556
557/* stop/pause a slave timer */
558static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
559{
560 unsigned long flags;
561
562 spin_lock_irqsave(&slave_active_lock, flags);
563 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
564 spin_unlock_irqrestore(&slave_active_lock, flags);
565 return -EBUSY;
566 }
567 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
568 if (timeri->timer) {
569 spin_lock(&timeri->timer->lock);
570 list_del_init(&timeri->ack_list);
571 list_del_init(&timeri->active_list);
572 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
573 SNDRV_TIMER_EVENT_CONTINUE);
574 spin_unlock(&timeri->timer->lock);
575 }
576 spin_unlock_irqrestore(&slave_active_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return 0;
578}
579
580/*
Takashi Iwai11740142016-02-10 12:47:03 +0100581 * start the timer instance
582 */
583int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
584{
585 if (timeri == NULL || ticks < 1)
586 return -EINVAL;
587 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
588 return snd_timer_start_slave(timeri, true);
589 else
590 return snd_timer_start1(timeri, true, ticks);
591}
592
593/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * stop the timer instance.
595 *
596 * do not call this from the timer callback!
597 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100598int snd_timer_stop(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Takashi Iwai11740142016-02-10 12:47:03 +0100600 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
601 return snd_timer_stop_slave(timeri, true);
602 else
603 return snd_timer_stop1(timeri, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606/*
607 * start again.. the tick is kept.
608 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100609int snd_timer_continue(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
Takashi Iwai11740142016-02-10 12:47:03 +0100612 return snd_timer_start_slave(timeri, false);
613 else
614 return snd_timer_start1(timeri, false, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617/*
618 * pause.. remember the ticks left
619 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100620int snd_timer_pause(struct snd_timer_instance * timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Takashi Iwai11740142016-02-10 12:47:03 +0100622 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
623 return snd_timer_stop_slave(timeri, false);
624 else
625 return snd_timer_stop1(timeri, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628/*
629 * reschedule the timer
630 *
631 * start pending instances and check the scheduling ticks.
632 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
633 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100634static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100636 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 unsigned long ticks = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Johannes Berg9244b2c2006-10-05 16:02:22 +0200639 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (ti->flags & SNDRV_TIMER_IFLG_START) {
641 ti->flags &= ~SNDRV_TIMER_IFLG_START;
642 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
643 timer->running++;
644 }
645 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
646 if (ticks > ti->cticks)
647 ticks = ti->cticks;
648 }
649 }
650 if (ticks == ~0UL) {
651 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
652 return;
653 }
654 if (ticks > timer->hw.ticks)
655 ticks = timer->hw.ticks;
656 if (ticks_left != ticks)
657 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
658 timer->sticks = ticks;
659}
660
661/*
662 * timer tasklet
663 *
664 */
665static void snd_timer_tasklet(unsigned long arg)
666{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100667 struct snd_timer *timer = (struct snd_timer *) arg;
668 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 struct list_head *p;
670 unsigned long resolution, ticks;
Takashi Iwai2999ff52006-07-05 17:16:58 +0200671 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Takashi Iwai8d155be2016-01-21 17:19:31 +0100673 if (timer->card && timer->card->shutdown)
674 return;
675
Takashi Iwai2999ff52006-07-05 17:16:58 +0200676 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /* now process all callbacks */
678 while (!list_empty(&timer->sack_list_head)) {
679 p = timer->sack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100680 ti = list_entry(p, struct snd_timer_instance, ack_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 /* remove from ack_list and make empty */
683 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 ticks = ti->pticks;
686 ti->pticks = 0;
687 resolution = ti->resolution;
688
689 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
690 spin_unlock(&timer->lock);
691 if (ti->callback)
692 ti->callback(ti, resolution, ticks);
693 spin_lock(&timer->lock);
694 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
695 }
Takashi Iwai2999ff52006-07-05 17:16:58 +0200696 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699/*
700 * timer interrupt
701 *
702 * ticks_left is usually equal to timer->sticks.
703 *
704 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100705void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200707 struct snd_timer_instance *ti, *ts, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 unsigned long resolution, ticks;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200709 struct list_head *p, *ack_list_head;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100710 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 int use_tasklet = 0;
712
713 if (timer == NULL)
714 return;
715
Takashi Iwai8d155be2016-01-21 17:19:31 +0100716 if (timer->card && timer->card->shutdown)
717 return;
718
Takashi Iwaib32425a2005-11-18 18:52:14 +0100719 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 /* remember the current resolution */
722 if (timer->hw.c_resolution)
723 resolution = timer->hw.c_resolution(timer);
724 else
725 resolution = timer->hw.resolution;
726
727 /* loop for all active instances
Johannes Berg9244b2c2006-10-05 16:02:22 +0200728 * Here we cannot use list_for_each_entry because the active_list of a
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200729 * processed instance is relinked to done_list_head before the callback
730 * is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200732 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
733 active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
735 continue;
736 ti->pticks += ticks_left;
737 ti->resolution = resolution;
738 if (ti->cticks < ticks_left)
739 ti->cticks = 0;
740 else
741 ti->cticks -= ticks_left;
742 if (ti->cticks) /* not expired */
743 continue;
744 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
745 ti->cticks = ti->ticks;
746 } else {
747 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwai2562c292016-02-04 17:06:13 +0100748 --timer->running;
749 list_del_init(&ti->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200751 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
752 (ti->flags & SNDRV_TIMER_IFLG_FAST))
753 ack_list_head = &timer->ack_list_head;
754 else
755 ack_list_head = &timer->sack_list_head;
756 if (list_empty(&ti->ack_list))
757 list_add_tail(&ti->ack_list, ack_list_head);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200758 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 ts->pticks = ti->pticks;
760 ts->resolution = resolution;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200761 if (list_empty(&ts->ack_list))
762 list_add_tail(&ts->ack_list, ack_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764 }
765 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
Clemens Ladischcd93fe42006-07-17 16:53:57 +0200766 snd_timer_reschedule(timer, timer->sticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (timer->running) {
768 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
769 timer->hw.stop(timer);
770 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
771 }
772 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
773 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
774 /* restart timer */
775 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
776 timer->hw.start(timer);
777 }
778 } else {
779 timer->hw.stop(timer);
780 }
781
782 /* now process all fast callbacks */
783 while (!list_empty(&timer->ack_list_head)) {
784 p = timer->ack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100785 ti = list_entry(p, struct snd_timer_instance, ack_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /* remove from ack_list and make empty */
788 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 ticks = ti->pticks;
791 ti->pticks = 0;
792
793 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
794 spin_unlock(&timer->lock);
795 if (ti->callback)
796 ti->callback(ti, resolution, ticks);
797 spin_lock(&timer->lock);
798 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
799 }
800
801 /* do we have any slow callbacks? */
802 use_tasklet = !list_empty(&timer->sack_list_head);
Takashi Iwaib32425a2005-11-18 18:52:14 +0100803 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 if (use_tasklet)
Takashi Iwai1f041282008-12-18 12:17:55 +0100806 tasklet_schedule(&timer->task_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
809/*
810
811 */
812
Takashi Iwai53d2f742005-11-17 13:56:05 +0100813int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
814 struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100816 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100818 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 .dev_free = snd_timer_dev_free,
820 .dev_register = snd_timer_dev_register,
Takashi Iwaic4614822006-06-23 14:38:23 +0200821 .dev_disconnect = snd_timer_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 };
823
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200824 if (snd_BUG_ON(!tid))
825 return -EINVAL;
826 if (rtimer)
827 *rtimer = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200828 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100829 if (timer == NULL) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100830 pr_err("ALSA: timer: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 timer->tmr_class = tid->dev_class;
834 timer->card = card;
835 timer->tmr_device = tid->device;
836 timer->tmr_subdevice = tid->subdevice;
837 if (id)
838 strlcpy(timer->id, id, sizeof(timer->id));
Vegard Nossuma987c622016-08-29 00:33:50 +0200839 timer->sticks = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 INIT_LIST_HEAD(&timer->device_list);
841 INIT_LIST_HEAD(&timer->open_list_head);
842 INIT_LIST_HEAD(&timer->active_list_head);
843 INIT_LIST_HEAD(&timer->ack_list_head);
844 INIT_LIST_HEAD(&timer->sack_list_head);
845 spin_lock_init(&timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200846 tasklet_init(&timer->task_queue, snd_timer_tasklet,
847 (unsigned long)timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (card != NULL) {
Clemens Ladischde242142005-10-12 17:12:31 +0200849 timer->module = card->module;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200850 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
851 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 snd_timer_free(timer);
853 return err;
854 }
855 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200856 if (rtimer)
857 *rtimer = timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return 0;
859}
860
Takashi Iwai53d2f742005-11-17 13:56:05 +0100861static int snd_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200863 if (!timer)
864 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +0200865
866 mutex_lock(&register_mutex);
867 if (! list_empty(&timer->open_list_head)) {
868 struct list_head *p, *n;
869 struct snd_timer_instance *ti;
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100870 pr_warn("ALSA: timer %p is busy?\n", timer);
Takashi Iwaic4614822006-06-23 14:38:23 +0200871 list_for_each_safe(p, n, &timer->open_list_head) {
872 list_del_init(p);
873 ti = list_entry(p, struct snd_timer_instance, open_list);
874 ti->timer = NULL;
875 }
876 }
877 list_del(&timer->device_list);
878 mutex_unlock(&register_mutex);
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (timer->private_free)
881 timer->private_free(timer);
882 kfree(timer);
883 return 0;
884}
885
Takashi Iwai53d2f742005-11-17 13:56:05 +0100886static int snd_timer_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100888 struct snd_timer *timer = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return snd_timer_free(timer);
890}
891
Takashi Iwai53d2f742005-11-17 13:56:05 +0100892static int snd_timer_dev_register(struct snd_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100894 struct snd_timer *timer = dev->device_data;
895 struct snd_timer *timer1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200897 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
898 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
900 !timer->hw.resolution && timer->hw.c_resolution == NULL)
901 return -EINVAL;
902
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100903 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200904 list_for_each_entry(timer1, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (timer1->tmr_class > timer->tmr_class)
906 break;
907 if (timer1->tmr_class < timer->tmr_class)
908 continue;
909 if (timer1->card && timer->card) {
910 if (timer1->card->number > timer->card->number)
911 break;
912 if (timer1->card->number < timer->card->number)
913 continue;
914 }
915 if (timer1->tmr_device > timer->tmr_device)
916 break;
917 if (timer1->tmr_device < timer->tmr_device)
918 continue;
919 if (timer1->tmr_subdevice > timer->tmr_subdevice)
920 break;
921 if (timer1->tmr_subdevice < timer->tmr_subdevice)
922 continue;
923 /* conflicts.. */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100924 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return -EBUSY;
926 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200927 list_add_tail(&timer->device_list, &timer1->device_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100928 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return 0;
930}
931
Takashi Iwai8d155be2016-01-21 17:19:31 +0100932/* just for reference in snd_timer_dev_disconnect() below */
933static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
934 int event, struct timespec *tstamp,
935 unsigned long resolution);
936
Takashi Iwaic4614822006-06-23 14:38:23 +0200937static int snd_timer_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100939 struct snd_timer *timer = device->device_data;
Takashi Iwai8d155be2016-01-21 17:19:31 +0100940 struct snd_timer_instance *ti;
941
Takashi Iwaic4614822006-06-23 14:38:23 +0200942 mutex_lock(&register_mutex);
943 list_del_init(&timer->device_list);
Takashi Iwai8d155be2016-01-21 17:19:31 +0100944 /* wake up pending sleepers */
945 list_for_each_entry(ti, &timer->open_list_head, open_list) {
946 /* FIXME: better to have a ti.disconnect() op */
947 if (ti->ccallback == snd_timer_user_ccallback) {
948 struct snd_timer_user *tu = ti->callback_data;
949
950 tu->disconnected = true;
951 wake_up(&tu->qchange_sleep);
952 }
953 }
Takashi Iwaic4614822006-06-23 14:38:23 +0200954 mutex_unlock(&register_mutex);
955 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Takashi Iwai53d2f742005-11-17 13:56:05 +0100958void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 unsigned long flags;
961 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100962 struct snd_timer_instance *ti, *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Takashi Iwai8d155be2016-01-21 17:19:31 +0100964 if (timer->card && timer->card->shutdown)
965 return;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200966 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
967 return;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200968 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
969 event > SNDRV_TIMER_EVENT_MRESUME))
970 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 spin_lock_irqsave(&timer->lock, flags);
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +0200972 if (event == SNDRV_TIMER_EVENT_MSTART ||
973 event == SNDRV_TIMER_EVENT_MCONTINUE ||
974 event == SNDRV_TIMER_EVENT_MRESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (timer->hw.c_resolution)
976 resolution = timer->hw.c_resolution(timer);
977 else
978 resolution = timer->hw.resolution;
979 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200980 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (ti->ccallback)
982 ti->ccallback(ti, event, tstamp, resolution);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200983 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (ts->ccallback)
985 ts->ccallback(ts, event, tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987 spin_unlock_irqrestore(&timer->lock, flags);
988}
989
990/*
991 * exported functions for global timers
992 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100993int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100995 struct snd_timer_id tid;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
998 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
999 tid.card = -1;
1000 tid.device = device;
1001 tid.subdevice = 0;
1002 return snd_timer_new(NULL, id, &tid, rtimer);
1003}
1004
Takashi Iwai53d2f742005-11-17 13:56:05 +01001005int snd_timer_global_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 return snd_timer_free(timer);
1008}
1009
Takashi Iwai53d2f742005-11-17 13:56:05 +01001010int snd_timer_global_register(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001012 struct snd_device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 memset(&dev, 0, sizeof(dev));
1015 dev.device_data = timer;
1016 return snd_timer_dev_register(&dev);
1017}
1018
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001019/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 * System timer
1021 */
1022
1023struct snd_timer_system_private {
1024 struct timer_list tlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 unsigned long last_expires;
1026 unsigned long last_jiffies;
1027 unsigned long correction;
1028};
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030static void snd_timer_s_function(unsigned long data)
1031{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001032 struct snd_timer *timer = (struct snd_timer *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 struct snd_timer_system_private *priv = timer->private_data;
1034 unsigned long jiff = jiffies;
1035 if (time_after(jiff, priv->last_expires))
Clemens Ladisch6ed5eff2006-07-17 16:51:37 +02001036 priv->correction += (long)jiff - (long)priv->last_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1038}
1039
Takashi Iwai53d2f742005-11-17 13:56:05 +01001040static int snd_timer_s_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 struct snd_timer_system_private *priv;
1043 unsigned long njiff;
1044
1045 priv = (struct snd_timer_system_private *) timer->private_data;
1046 njiff = (priv->last_jiffies = jiffies);
1047 if (priv->correction > timer->sticks - 1) {
1048 priv->correction -= timer->sticks - 1;
1049 njiff++;
1050 } else {
1051 njiff += timer->sticks - priv->correction;
Clemens Ladisch17f48ec2006-07-17 16:50:56 +02001052 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
Takashi Iwaid4f95a02016-04-01 12:28:16 +02001054 priv->last_expires = njiff;
1055 mod_timer(&priv->tlist, njiff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return 0;
1057}
1058
Takashi Iwai53d2f742005-11-17 13:56:05 +01001059static int snd_timer_s_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 struct snd_timer_system_private *priv;
1062 unsigned long jiff;
1063
1064 priv = (struct snd_timer_system_private *) timer->private_data;
1065 del_timer(&priv->tlist);
1066 jiff = jiffies;
1067 if (time_before(jiff, priv->last_expires))
1068 timer->sticks = priv->last_expires - jiff;
1069 else
1070 timer->sticks = 1;
Clemens Ladischde2696d2006-07-17 16:52:09 +02001071 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return 0;
1073}
1074
Takashi Iwai53d2f742005-11-17 13:56:05 +01001075static struct snd_timer_hardware snd_timer_system =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1078 .resolution = 1000000000L / HZ,
1079 .ticks = 10000000L,
1080 .start = snd_timer_s_start,
1081 .stop = snd_timer_s_stop
1082};
1083
Takashi Iwai53d2f742005-11-17 13:56:05 +01001084static void snd_timer_free_system(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
1086 kfree(timer->private_data);
1087}
1088
1089static int snd_timer_register_system(void)
1090{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001091 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 struct snd_timer_system_private *priv;
1093 int err;
1094
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001095 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1096 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return err;
1098 strcpy(timer->name, "system timer");
1099 timer->hw = snd_timer_system;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001100 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (priv == NULL) {
1102 snd_timer_free(timer);
1103 return -ENOMEM;
1104 }
1105 init_timer(&priv->tlist);
1106 priv->tlist.function = snd_timer_s_function;
1107 priv->tlist.data = (unsigned long) timer;
1108 timer->private_data = priv;
1109 timer->private_free = snd_timer_free_system;
1110 return snd_timer_global_register(timer);
1111}
1112
Takashi Iwaie28563c2005-12-01 10:42:42 +01001113#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114/*
1115 * Info interface
1116 */
1117
Takashi Iwai53d2f742005-11-17 13:56:05 +01001118static void snd_timer_proc_read(struct snd_info_entry *entry,
1119 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001121 struct snd_timer *timer;
1122 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001124 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001125 list_for_each_entry(timer, &snd_timer_list, device_list) {
Takashi Iwai8d155be2016-01-21 17:19:31 +01001126 if (timer->card && timer->card->shutdown)
1127 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 switch (timer->tmr_class) {
1129 case SNDRV_TIMER_CLASS_GLOBAL:
1130 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1131 break;
1132 case SNDRV_TIMER_CLASS_CARD:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001133 snd_iprintf(buffer, "C%i-%i: ",
1134 timer->card->number, timer->tmr_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 break;
1136 case SNDRV_TIMER_CLASS_PCM:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001137 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1138 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 break;
1140 default:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001141 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1142 timer->card ? timer->card->number : -1,
1143 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
1145 snd_iprintf(buffer, "%s :", timer->name);
1146 if (timer->hw.resolution)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001147 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1148 timer->hw.resolution / 1000,
1149 timer->hw.resolution % 1000,
1150 timer->hw.ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1152 snd_iprintf(buffer, " SLAVE");
1153 snd_iprintf(buffer, "\n");
Johannes Berg9244b2c2006-10-05 16:02:22 +02001154 list_for_each_entry(ti, &timer->open_list_head, open_list)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001155 snd_iprintf(buffer, " Client %s : %s\n",
1156 ti->owner ? ti->owner : "unknown",
1157 ti->flags & (SNDRV_TIMER_IFLG_START |
1158 SNDRV_TIMER_IFLG_RUNNING)
1159 ? "running" : "stopped");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001161 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001164static struct snd_info_entry *snd_timer_proc_entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001165
1166static void __init snd_timer_proc_init(void)
1167{
1168 struct snd_info_entry *entry;
1169
1170 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1171 if (entry != NULL) {
Takashi Iwaie28563c2005-12-01 10:42:42 +01001172 entry->c.text.read = snd_timer_proc_read;
1173 if (snd_info_register(entry) < 0) {
1174 snd_info_free_entry(entry);
1175 entry = NULL;
1176 }
1177 }
1178 snd_timer_proc_entry = entry;
1179}
1180
1181static void __exit snd_timer_proc_done(void)
1182{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001183 snd_info_free_entry(snd_timer_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001184}
1185#else /* !CONFIG_PROC_FS */
1186#define snd_timer_proc_init()
1187#define snd_timer_proc_done()
1188#endif
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190/*
1191 * USER SPACE interface
1192 */
1193
Takashi Iwai53d2f742005-11-17 13:56:05 +01001194static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 unsigned long resolution,
1196 unsigned long ticks)
1197{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001198 struct snd_timer_user *tu = timeri->callback_data;
1199 struct snd_timer_read *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 int prev;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 spin_lock(&tu->qlock);
1203 if (tu->qused > 0) {
1204 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1205 r = &tu->queue[prev];
1206 if (r->resolution == resolution) {
1207 r->ticks += ticks;
1208 goto __wake;
1209 }
1210 }
1211 if (tu->qused >= tu->queue_size) {
1212 tu->overrun++;
1213 } else {
1214 r = &tu->queue[tu->qtail++];
1215 tu->qtail %= tu->queue_size;
1216 r->resolution = resolution;
1217 r->ticks = ticks;
1218 tu->qused++;
1219 }
1220 __wake:
1221 spin_unlock(&tu->qlock);
1222 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1223 wake_up(&tu->qchange_sleep);
1224}
1225
Takashi Iwai53d2f742005-11-17 13:56:05 +01001226static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1227 struct snd_timer_tread *tread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 if (tu->qused >= tu->queue_size) {
1230 tu->overrun++;
1231 } else {
1232 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1233 tu->qtail %= tu->queue_size;
1234 tu->qused++;
1235 }
1236}
1237
Takashi Iwai53d2f742005-11-17 13:56:05 +01001238static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1239 int event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 struct timespec *tstamp,
1241 unsigned long resolution)
1242{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001243 struct snd_timer_user *tu = timeri->callback_data;
1244 struct snd_timer_tread r1;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001245 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001247 if (event >= SNDRV_TIMER_EVENT_START &&
1248 event <= SNDRV_TIMER_EVENT_PAUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 tu->tstamp = *tstamp;
1250 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1251 return;
1252 r1.event = event;
1253 r1.tstamp = *tstamp;
1254 r1.val = resolution;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001255 spin_lock_irqsave(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 snd_timer_user_append_to_tqueue(tu, &r1);
Dan Carpenterbfe70782010-04-28 10:29:14 +02001257 spin_unlock_irqrestore(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1259 wake_up(&tu->qchange_sleep);
1260}
1261
Takashi Iwai53d2f742005-11-17 13:56:05 +01001262static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 unsigned long resolution,
1264 unsigned long ticks)
1265{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001266 struct snd_timer_user *tu = timeri->callback_data;
1267 struct snd_timer_tread *r, r1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 struct timespec tstamp;
1269 int prev, append = 0;
1270
Takashi Iwai07799e72005-10-10 11:49:49 +02001271 memset(&tstamp, 0, sizeof(tstamp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 spin_lock(&tu->qlock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001273 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1274 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 spin_unlock(&tu->qlock);
1276 return;
1277 }
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001278 if (tu->last_resolution != resolution || ticks > 0) {
1279 if (timer_tstamp_monotonic)
Thomas Gleixner26204e02014-06-11 23:59:14 +00001280 ktime_get_ts(&tstamp);
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001281 else
1282 getnstimeofday(&tstamp);
1283 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001284 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1285 tu->last_resolution != resolution) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1287 r1.tstamp = tstamp;
1288 r1.val = resolution;
1289 snd_timer_user_append_to_tqueue(tu, &r1);
1290 tu->last_resolution = resolution;
1291 append++;
1292 }
1293 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1294 goto __wake;
1295 if (ticks == 0)
1296 goto __wake;
1297 if (tu->qused > 0) {
1298 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1299 r = &tu->tqueue[prev];
1300 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1301 r->tstamp = tstamp;
1302 r->val += ticks;
1303 append++;
1304 goto __wake;
1305 }
1306 }
1307 r1.event = SNDRV_TIMER_EVENT_TICK;
1308 r1.tstamp = tstamp;
1309 r1.val = ticks;
1310 snd_timer_user_append_to_tqueue(tu, &r1);
1311 append++;
1312 __wake:
1313 spin_unlock(&tu->qlock);
1314 if (append == 0)
1315 return;
1316 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1317 wake_up(&tu->qchange_sleep);
1318}
1319
1320static int snd_timer_user_open(struct inode *inode, struct file *file)
1321{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001322 struct snd_timer_user *tu;
Takashi Iwai02f48652010-04-13 11:49:04 +02001323 int err;
1324
1325 err = nonseekable_open(inode, file);
1326 if (err < 0)
1327 return err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001328
Takashi Iwaica2c0962005-09-09 14:20:23 +02001329 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (tu == NULL)
1331 return -ENOMEM;
1332 spin_lock_init(&tu->qlock);
1333 init_waitqueue_head(&tu->qchange_sleep);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001334 mutex_init(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 tu->ticks = 1;
1336 tu->queue_size = 128;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001337 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001338 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (tu->queue == NULL) {
1340 kfree(tu);
1341 return -ENOMEM;
1342 }
1343 file->private_data = tu;
1344 return 0;
1345}
1346
1347static int snd_timer_user_release(struct inode *inode, struct file *file)
1348{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001349 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 if (file->private_data) {
1352 tu = file->private_data;
1353 file->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (tu->timeri)
1355 snd_timer_close(tu->timeri);
1356 kfree(tu->queue);
1357 kfree(tu->tqueue);
1358 kfree(tu);
1359 }
1360 return 0;
1361}
1362
Takashi Iwai53d2f742005-11-17 13:56:05 +01001363static void snd_timer_user_zero_id(struct snd_timer_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1366 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1367 id->card = -1;
1368 id->device = -1;
1369 id->subdevice = -1;
1370}
1371
Takashi Iwai53d2f742005-11-17 13:56:05 +01001372static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374 id->dev_class = timer->tmr_class;
1375 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1376 id->card = timer->card ? timer->card->number : -1;
1377 id->device = timer->tmr_device;
1378 id->subdevice = timer->tmr_subdevice;
1379}
1380
Takashi Iwai53d2f742005-11-17 13:56:05 +01001381static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001383 struct snd_timer_id id;
1384 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 struct list_head *p;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (copy_from_user(&id, _tid, sizeof(id)))
1388 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001389 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (id.dev_class < 0) { /* first item */
1391 if (list_empty(&snd_timer_list))
1392 snd_timer_user_zero_id(&id);
1393 else {
Clemens Ladisch9dfba382005-10-12 17:14:55 +02001394 timer = list_entry(snd_timer_list.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001395 struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 snd_timer_user_copy_id(&id, timer);
1397 }
1398 } else {
1399 switch (id.dev_class) {
1400 case SNDRV_TIMER_CLASS_GLOBAL:
1401 id.device = id.device < 0 ? 0 : id.device + 1;
1402 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001403 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1405 snd_timer_user_copy_id(&id, timer);
1406 break;
1407 }
1408 if (timer->tmr_device >= id.device) {
1409 snd_timer_user_copy_id(&id, timer);
1410 break;
1411 }
1412 }
1413 if (p == &snd_timer_list)
1414 snd_timer_user_zero_id(&id);
1415 break;
1416 case SNDRV_TIMER_CLASS_CARD:
1417 case SNDRV_TIMER_CLASS_PCM:
1418 if (id.card < 0) {
1419 id.card = 0;
1420 } else {
1421 if (id.card < 0) {
1422 id.card = 0;
1423 } else {
1424 if (id.device < 0) {
1425 id.device = 0;
1426 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001427 if (id.subdevice < 0) {
1428 id.subdevice = 0;
1429 } else {
1430 id.subdevice++;
1431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433 }
1434 }
1435 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001436 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (timer->tmr_class > id.dev_class) {
1438 snd_timer_user_copy_id(&id, timer);
1439 break;
1440 }
1441 if (timer->tmr_class < id.dev_class)
1442 continue;
1443 if (timer->card->number > id.card) {
1444 snd_timer_user_copy_id(&id, timer);
1445 break;
1446 }
1447 if (timer->card->number < id.card)
1448 continue;
1449 if (timer->tmr_device > id.device) {
1450 snd_timer_user_copy_id(&id, timer);
1451 break;
1452 }
1453 if (timer->tmr_device < id.device)
1454 continue;
1455 if (timer->tmr_subdevice > id.subdevice) {
1456 snd_timer_user_copy_id(&id, timer);
1457 break;
1458 }
1459 if (timer->tmr_subdevice < id.subdevice)
1460 continue;
1461 snd_timer_user_copy_id(&id, timer);
1462 break;
1463 }
1464 if (p == &snd_timer_list)
1465 snd_timer_user_zero_id(&id);
1466 break;
1467 default:
1468 snd_timer_user_zero_id(&id);
1469 }
1470 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001471 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1473 return -EFAULT;
1474 return 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001475}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001477static int snd_timer_user_ginfo(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001478 struct snd_timer_ginfo __user *_ginfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001480 struct snd_timer_ginfo *ginfo;
1481 struct snd_timer_id tid;
1482 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 struct list_head *p;
1484 int err = 0;
1485
Li Zefanef44a1e2009-04-10 09:43:08 +08001486 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1487 if (IS_ERR(ginfo))
1488 return PTR_ERR(ginfo);
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 tid = ginfo->tid;
1491 memset(ginfo, 0, sizeof(*ginfo));
1492 ginfo->tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001493 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 t = snd_timer_find(&tid);
1495 if (t != NULL) {
1496 ginfo->card = t->card ? t->card->number : -1;
1497 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1498 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1499 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1500 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1501 ginfo->resolution = t->hw.resolution;
1502 if (t->hw.resolution_min > 0) {
1503 ginfo->resolution_min = t->hw.resolution_min;
1504 ginfo->resolution_max = t->hw.resolution_max;
1505 }
1506 list_for_each(p, &t->open_list_head) {
1507 ginfo->clients++;
1508 }
1509 } else {
1510 err = -ENODEV;
1511 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001512 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1514 err = -EFAULT;
1515 kfree(ginfo);
1516 return err;
1517}
1518
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001519static int snd_timer_user_gparams(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001520 struct snd_timer_gparams __user *_gparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001522 struct snd_timer_gparams gparams;
1523 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 int err;
1525
1526 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1527 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001528 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 t = snd_timer_find(&gparams.tid);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001530 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 err = -ENODEV;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001532 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001534 if (!list_empty(&t->open_list_head)) {
1535 err = -EBUSY;
1536 goto _error;
1537 }
1538 if (!t->hw.set_period) {
1539 err = -ENOSYS;
1540 goto _error;
1541 }
1542 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1543_error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001544 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return err;
1546}
1547
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001548static int snd_timer_user_gstatus(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001549 struct snd_timer_gstatus __user *_gstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001551 struct snd_timer_gstatus gstatus;
1552 struct snd_timer_id tid;
1553 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 int err = 0;
1555
1556 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1557 return -EFAULT;
1558 tid = gstatus.tid;
1559 memset(&gstatus, 0, sizeof(gstatus));
1560 gstatus.tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001561 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 t = snd_timer_find(&tid);
1563 if (t != NULL) {
1564 if (t->hw.c_resolution)
1565 gstatus.resolution = t->hw.c_resolution(t);
1566 else
1567 gstatus.resolution = t->hw.resolution;
1568 if (t->hw.precise_resolution) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001569 t->hw.precise_resolution(t, &gstatus.resolution_num,
1570 &gstatus.resolution_den);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 } else {
1572 gstatus.resolution_num = gstatus.resolution;
1573 gstatus.resolution_den = 1000000000uL;
1574 }
1575 } else {
1576 err = -ENODEV;
1577 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001578 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1580 err = -EFAULT;
1581 return err;
1582}
1583
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001584static int snd_timer_user_tselect(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001585 struct snd_timer_select __user *_tselect)
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_select tselect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 char str[32];
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001590 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 tu = file->private_data;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001593 mutex_lock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001594 if (tu->timeri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 snd_timer_close(tu->timeri);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001596 tu->timeri = NULL;
1597 }
1598 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1599 err = -EFAULT;
1600 goto __err;
1601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 sprintf(str, "application %i", current->pid);
1603 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1604 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001605 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1606 if (err < 0)
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001607 goto __err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Jesper Juhl4d572772005-05-30 17:30:32 +02001609 kfree(tu->queue);
1610 tu->queue = NULL;
1611 kfree(tu->tqueue);
1612 tu->tqueue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001614 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001615 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001616 if (tu->tqueue == NULL)
1617 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001619 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001620 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001621 if (tu->queue == NULL)
1622 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001624
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001625 if (err < 0) {
1626 snd_timer_close(tu->timeri);
1627 tu->timeri = NULL;
1628 } else {
1629 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001630 tu->timeri->callback = tu->tread
1631 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001632 tu->timeri->ccallback = snd_timer_user_ccallback;
1633 tu->timeri->callback_data = (void *)tu;
1634 }
1635
1636 __err:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001637 mutex_unlock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001638 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001641static int snd_timer_user_info(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001642 struct snd_timer_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001644 struct snd_timer_user *tu;
1645 struct snd_timer_info *info;
1646 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 int err = 0;
1648
1649 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001650 if (!tu->timeri)
1651 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001653 if (!t)
1654 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Takashi Iwaica2c0962005-09-09 14:20:23 +02001656 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (! info)
1658 return -ENOMEM;
1659 info->card = t->card ? t->card->number : -1;
1660 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1661 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1662 strlcpy(info->id, t->id, sizeof(info->id));
1663 strlcpy(info->name, t->name, sizeof(info->name));
1664 info->resolution = t->hw.resolution;
1665 if (copy_to_user(_info, info, sizeof(*_info)))
1666 err = -EFAULT;
1667 kfree(info);
1668 return err;
1669}
1670
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001671static int snd_timer_user_params(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001672 struct snd_timer_params __user *_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001674 struct snd_timer_user *tu;
1675 struct snd_timer_params params;
1676 struct snd_timer *t;
1677 struct snd_timer_read *tr;
1678 struct snd_timer_tread *ttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001682 if (!tu->timeri)
1683 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001685 if (!t)
1686 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (copy_from_user(&params, _params, sizeof(params)))
1688 return -EFAULT;
1689 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1690 err = -EINVAL;
1691 goto _end;
1692 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001693 if (params.queue_size > 0 &&
1694 (params.queue_size < 32 || params.queue_size > 1024)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 err = -EINVAL;
1696 goto _end;
1697 }
1698 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1699 (1<<SNDRV_TIMER_EVENT_TICK)|
1700 (1<<SNDRV_TIMER_EVENT_START)|
1701 (1<<SNDRV_TIMER_EVENT_STOP)|
1702 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1703 (1<<SNDRV_TIMER_EVENT_PAUSE)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001704 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1705 (1<<SNDRV_TIMER_EVENT_RESUME)|
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 (1<<SNDRV_TIMER_EVENT_MSTART)|
1707 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1708 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
Jaroslav Kysela65d11d92005-08-16 13:05:43 +02001709 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1710 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001711 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 err = -EINVAL;
1713 goto _end;
1714 }
1715 snd_timer_stop(tu->timeri);
1716 spin_lock_irq(&t->lock);
1717 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1718 SNDRV_TIMER_IFLG_EXCLUSIVE|
1719 SNDRV_TIMER_IFLG_EARLY_EVENT);
1720 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1721 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1722 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1723 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1724 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1725 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1726 spin_unlock_irq(&t->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001727 if (params.queue_size > 0 &&
1728 (unsigned int)tu->queue_size != params.queue_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 if (tu->tread) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001730 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1731 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (ttr) {
1733 kfree(tu->tqueue);
1734 tu->queue_size = params.queue_size;
1735 tu->tqueue = ttr;
1736 }
1737 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001738 tr = kmalloc(params.queue_size * sizeof(*tr),
1739 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (tr) {
1741 kfree(tu->queue);
1742 tu->queue_size = params.queue_size;
1743 tu->queue = tr;
1744 }
1745 }
1746 }
1747 tu->qhead = tu->qtail = tu->qused = 0;
1748 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1749 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001750 struct snd_timer_tread tread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 tread.event = SNDRV_TIMER_EVENT_EARLY;
1752 tread.tstamp.tv_sec = 0;
1753 tread.tstamp.tv_nsec = 0;
1754 tread.val = 0;
1755 snd_timer_user_append_to_tqueue(tu, &tread);
1756 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001757 struct snd_timer_read *r = &tu->queue[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 r->resolution = 0;
1759 r->ticks = 0;
1760 tu->qused++;
1761 tu->qtail++;
1762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
1764 tu->filter = params.filter;
1765 tu->ticks = params.ticks;
1766 err = 0;
1767 _end:
1768 if (copy_to_user(_params, &params, sizeof(params)))
1769 return -EFAULT;
1770 return err;
1771}
1772
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001773static int snd_timer_user_status(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001774 struct snd_timer_status __user *_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001776 struct snd_timer_user *tu;
1777 struct snd_timer_status status;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001780 if (!tu->timeri)
1781 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 memset(&status, 0, sizeof(status));
1783 status.tstamp = tu->tstamp;
1784 status.resolution = snd_timer_resolution(tu->timeri);
1785 status.lost = tu->timeri->lost;
1786 status.overrun = tu->overrun;
1787 spin_lock_irq(&tu->qlock);
1788 status.queue = tu->qused;
1789 spin_unlock_irq(&tu->qlock);
1790 if (copy_to_user(_status, &status, sizeof(status)))
1791 return -EFAULT;
1792 return 0;
1793}
1794
1795static int snd_timer_user_start(struct file *file)
1796{
1797 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001798 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001801 if (!tu->timeri)
1802 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 snd_timer_stop(tu->timeri);
1804 tu->timeri->lost = 0;
1805 tu->last_resolution = 0;
1806 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1807}
1808
1809static int snd_timer_user_stop(struct file *file)
1810{
1811 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001812 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001815 if (!tu->timeri)
1816 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1818}
1819
1820static int snd_timer_user_continue(struct file *file)
1821{
1822 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001823 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001826 if (!tu->timeri)
1827 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 tu->timeri->lost = 0;
1829 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1830}
1831
Takashi Iwai15790a62005-05-15 15:04:14 +02001832static int snd_timer_user_pause(struct file *file)
1833{
1834 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001835 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001836
Takashi Iwai15790a62005-05-15 15:04:14 +02001837 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001838 if (!tu->timeri)
1839 return -EBADFD;
Jaroslav Kyselad138b442005-05-16 13:51:39 +02001840 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
Takashi Iwai15790a62005-05-15 15:04:14 +02001841}
1842
Takashi Iwai8c50b372005-05-15 15:43:54 +02001843enum {
1844 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1845 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1846 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1847 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1848};
1849
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001850static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1851 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001853 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 void __user *argp = (void __user *)arg;
1855 int __user *p = argp;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 tu = file->private_data;
1858 switch (cmd) {
1859 case SNDRV_TIMER_IOCTL_PVERSION:
1860 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1861 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1862 return snd_timer_user_next_device(argp);
1863 case SNDRV_TIMER_IOCTL_TREAD:
1864 {
1865 int xarg;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001866
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001867 mutex_lock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001868 if (tu->timeri) { /* too late */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001869 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 return -EBUSY;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001871 }
1872 if (get_user(xarg, p)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001873 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return -EFAULT;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 tu->tread = xarg ? 1 : 0;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001877 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return 0;
1879 }
1880 case SNDRV_TIMER_IOCTL_GINFO:
1881 return snd_timer_user_ginfo(file, argp);
1882 case SNDRV_TIMER_IOCTL_GPARAMS:
1883 return snd_timer_user_gparams(file, argp);
1884 case SNDRV_TIMER_IOCTL_GSTATUS:
1885 return snd_timer_user_gstatus(file, argp);
1886 case SNDRV_TIMER_IOCTL_SELECT:
1887 return snd_timer_user_tselect(file, argp);
1888 case SNDRV_TIMER_IOCTL_INFO:
1889 return snd_timer_user_info(file, argp);
1890 case SNDRV_TIMER_IOCTL_PARAMS:
1891 return snd_timer_user_params(file, argp);
1892 case SNDRV_TIMER_IOCTL_STATUS:
1893 return snd_timer_user_status(file, argp);
1894 case SNDRV_TIMER_IOCTL_START:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001895 case SNDRV_TIMER_IOCTL_START_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return snd_timer_user_start(file);
1897 case SNDRV_TIMER_IOCTL_STOP:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001898 case SNDRV_TIMER_IOCTL_STOP_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return snd_timer_user_stop(file);
1900 case SNDRV_TIMER_IOCTL_CONTINUE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001901 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return snd_timer_user_continue(file);
Takashi Iwai15790a62005-05-15 15:04:14 +02001903 case SNDRV_TIMER_IOCTL_PAUSE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001904 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
Takashi Iwai15790a62005-05-15 15:04:14 +02001905 return snd_timer_user_pause(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
1907 return -ENOTTY;
1908}
1909
1910static int snd_timer_user_fasync(int fd, struct file * file, int on)
1911{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001912 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 tu = file->private_data;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001915 return fasync_helper(fd, file, on, &tu->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
1917
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001918static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1919 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001921 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 long result = 0, unit;
Takashi Iwai0f97e402016-02-08 17:26:58 +01001923 int qhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 tu = file->private_data;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001927 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 spin_lock_irq(&tu->qlock);
1929 while ((long)count - result >= unit) {
1930 while (!tu->qused) {
1931 wait_queue_t wait;
1932
1933 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1934 err = -EAGAIN;
Takashi Iwai0f97e402016-02-08 17:26:58 +01001935 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
1937
1938 set_current_state(TASK_INTERRUPTIBLE);
1939 init_waitqueue_entry(&wait, current);
1940 add_wait_queue(&tu->qchange_sleep, &wait);
1941
1942 spin_unlock_irq(&tu->qlock);
1943 schedule();
1944 spin_lock_irq(&tu->qlock);
1945
1946 remove_wait_queue(&tu->qchange_sleep, &wait);
1947
Takashi Iwai8d155be2016-01-21 17:19:31 +01001948 if (tu->disconnected) {
1949 err = -ENODEV;
Takashi Iwai0f97e402016-02-08 17:26:58 +01001950 goto _error;
Takashi Iwai8d155be2016-01-21 17:19:31 +01001951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (signal_pending(current)) {
1953 err = -ERESTARTSYS;
Takashi Iwai0f97e402016-02-08 17:26:58 +01001954 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
1956 }
1957
Takashi Iwai0f97e402016-02-08 17:26:58 +01001958 qhead = tu->qhead++;
1959 tu->qhead %= tu->queue_size;
Takashi Iwaia851e562016-07-04 14:02:15 +02001960 tu->qused--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 spin_unlock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 if (tu->tread) {
Takashi Iwai0f97e402016-02-08 17:26:58 +01001964 if (copy_to_user(buffer, &tu->tqueue[qhead],
1965 sizeof(struct snd_timer_tread)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 } else {
Takashi Iwai0f97e402016-02-08 17:26:58 +01001968 if (copy_to_user(buffer, &tu->queue[qhead],
1969 sizeof(struct snd_timer_read)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 spin_lock_irq(&tu->qlock);
Takashi Iwai0f97e402016-02-08 17:26:58 +01001974 if (err < 0)
1975 goto _error;
1976 result += unit;
1977 buffer += unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 _error:
Takashi Iwai0f97e402016-02-08 17:26:58 +01001980 spin_unlock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 return result > 0 ? result : err;
1982}
1983
1984static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1985{
1986 unsigned int mask;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001987 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 tu = file->private_data;
1990
1991 poll_wait(file, &tu->qchange_sleep, wait);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 mask = 0;
1994 if (tu->qused)
1995 mask |= POLLIN | POLLRDNORM;
Takashi Iwai8d155be2016-01-21 17:19:31 +01001996 if (tu->disconnected)
1997 mask |= POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 return mask;
2000}
2001
2002#ifdef CONFIG_COMPAT
2003#include "timer_compat.c"
2004#else
2005#define snd_timer_user_ioctl_compat NULL
2006#endif
2007
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08002008static const struct file_operations snd_timer_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 .owner = THIS_MODULE,
2011 .read = snd_timer_user_read,
2012 .open = snd_timer_user_open,
2013 .release = snd_timer_user_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02002014 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 .poll = snd_timer_user_poll,
2016 .unlocked_ioctl = snd_timer_user_ioctl,
2017 .compat_ioctl = snd_timer_user_ioctl_compat,
2018 .fasync = snd_timer_user_fasync,
2019};
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021/*
2022 * ENTRY functions
2023 */
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025static int __init alsa_timer_init(void)
2026{
2027 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029#ifdef SNDRV_OSS_INFO_DEV_TIMERS
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002030 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2031 "system timer");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032#endif
Takashi Iwaie28563c2005-12-01 10:42:42 +01002033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if ((err = snd_timer_register_system()) < 0)
Takashi Iwaicf74dcf2014-02-04 18:22:39 +01002035 pr_err("ALSA: unable to register system timer (%i)\n", err);
Clemens Ladischf87135f2005-11-20 14:06:59 +01002036 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2037 &snd_timer_f_ops, NULL, "timer")) < 0)
Takashi Iwaicf74dcf2014-02-04 18:22:39 +01002038 pr_err("ALSA: unable to register timer device (%i)\n", err);
Takashi Iwaie28563c2005-12-01 10:42:42 +01002039 snd_timer_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return 0;
2041}
2042
2043static void __exit alsa_timer_exit(void)
2044{
2045 struct list_head *p, *n;
2046
2047 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
2048 /* unregister the system timer */
2049 list_for_each_safe(p, n, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01002050 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
Takashi Iwaic4614822006-06-23 14:38:23 +02002051 snd_timer_free(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
Takashi Iwaie28563c2005-12-01 10:42:42 +01002053 snd_timer_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054#ifdef SNDRV_OSS_INFO_DEV_TIMERS
2055 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2056#endif
2057}
2058
2059module_init(alsa_timer_init)
2060module_exit(alsa_timer_exit)
2061
2062EXPORT_SYMBOL(snd_timer_open);
2063EXPORT_SYMBOL(snd_timer_close);
2064EXPORT_SYMBOL(snd_timer_resolution);
2065EXPORT_SYMBOL(snd_timer_start);
2066EXPORT_SYMBOL(snd_timer_stop);
2067EXPORT_SYMBOL(snd_timer_continue);
2068EXPORT_SYMBOL(snd_timer_pause);
2069EXPORT_SYMBOL(snd_timer_new);
2070EXPORT_SYMBOL(snd_timer_notify);
2071EXPORT_SYMBOL(snd_timer_global_new);
2072EXPORT_SYMBOL(snd_timer_global_free);
2073EXPORT_SYMBOL(snd_timer_global_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074EXPORT_SYMBOL(snd_timer_interrupt);