blob: c9da76e05b3f561f1e0a627a7a28994fb765e9b8 [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 Iwai1a30ab32016-09-07 15:45:31 +020038/* internal flags */
39#define SNDRV_TIMER_IFLG_PAUSED 0x00010000
40
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +010041#if IS_ENABLED(CONFIG_SND_HRTIMER)
Clemens Ladisch109fef92010-11-18 09:53:54 +010042#define DEFAULT_TIMER_LIMIT 4
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +010043#elif IS_ENABLED(CONFIG_SND_RTCTIMER)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define DEFAULT_TIMER_LIMIT 2
45#else
46#define DEFAULT_TIMER_LIMIT 1
47#endif
48
49static int timer_limit = DEFAULT_TIMER_LIMIT;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +010050static int timer_tstamp_monotonic = 1;
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020051MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070052MODULE_DESCRIPTION("ALSA timer interface");
53MODULE_LICENSE("GPL");
54module_param(timer_limit, int, 0444);
55MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
Jaroslav Kyselab751eef2007-12-13 10:19:42 +010056module_param(timer_tstamp_monotonic, int, 0444);
57MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Kay Sievers03cfe6f2010-11-23 17:43:19 +010059MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
60MODULE_ALIAS("devname:snd/timer");
61
Takashi Iwai53d2f742005-11-17 13:56:05 +010062struct snd_timer_user {
63 struct snd_timer_instance *timeri;
Clemens Ladisch6b172a82005-10-12 17:20:21 +020064 int tread; /* enhanced read with timestamps and events */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 unsigned long ticks;
66 unsigned long overrun;
67 int qhead;
68 int qtail;
69 int qused;
70 int queue_size;
Takashi Iwai8d155be2016-01-21 17:19:31 +010071 bool disconnected;
Takashi Iwai53d2f742005-11-17 13:56:05 +010072 struct snd_timer_read *queue;
73 struct snd_timer_tread *tqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 spinlock_t qlock;
75 unsigned long last_resolution;
76 unsigned int filter;
77 struct timespec tstamp; /* trigger tstamp */
78 wait_queue_head_t qchange_sleep;
79 struct fasync_struct *fasync;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010080 struct mutex tread_sem;
Takashi Iwai53d2f742005-11-17 13:56:05 +010081};
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/* list of timers */
84static LIST_HEAD(snd_timer_list);
85
86/* list of slave instances */
87static LIST_HEAD(snd_timer_slave_list);
88
89/* lock for slave active lists */
90static DEFINE_SPINLOCK(slave_active_lock);
91
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010092static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Takashi Iwai53d2f742005-11-17 13:56:05 +010094static int snd_timer_free(struct snd_timer *timer);
95static int snd_timer_dev_free(struct snd_device *device);
96static int snd_timer_dev_register(struct snd_device *device);
Takashi Iwaic4614822006-06-23 14:38:23 +020097static int snd_timer_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Takashi Iwai53d2f742005-11-17 13:56:05 +010099static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/*
102 * create a timer instance with the given owner string.
103 * when timer is not NULL, increments the module counter
104 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100105static struct snd_timer_instance *snd_timer_instance_new(char *owner,
106 struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100108 struct snd_timer_instance *timeri;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200109 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (timeri == NULL)
111 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700112 timeri->owner = kstrdup(owner, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (! timeri->owner) {
114 kfree(timeri);
115 return NULL;
116 }
117 INIT_LIST_HEAD(&timeri->open_list);
118 INIT_LIST_HEAD(&timeri->active_list);
119 INIT_LIST_HEAD(&timeri->ack_list);
120 INIT_LIST_HEAD(&timeri->slave_list_head);
121 INIT_LIST_HEAD(&timeri->slave_active_head);
122
123 timeri->timer = timer;
Clemens Ladischde242142005-10-12 17:12:31 +0200124 if (timer && !try_module_get(timer->module)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 kfree(timeri->owner);
126 kfree(timeri);
127 return NULL;
128 }
129
130 return timeri;
131}
132
133/*
134 * find a timer instance from the given timer id
135 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100136static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100138 struct snd_timer *timer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Johannes Berg9244b2c2006-10-05 16:02:22 +0200140 list_for_each_entry(timer, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (timer->tmr_class != tid->dev_class)
142 continue;
143 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
144 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
145 (timer->card == NULL ||
146 timer->card->number != tid->card))
147 continue;
148 if (timer->tmr_device != tid->device)
149 continue;
150 if (timer->tmr_subdevice != tid->subdevice)
151 continue;
152 return timer;
153 }
154 return NULL;
155}
156
Johannes Bergee2da992008-07-09 10:28:41 +0200157#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Takashi Iwai53d2f742005-11-17 13:56:05 +0100159static void snd_timer_request(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 switch (tid->dev_class) {
162 case SNDRV_TIMER_CLASS_GLOBAL:
163 if (tid->device < timer_limit)
164 request_module("snd-timer-%i", tid->device);
165 break;
166 case SNDRV_TIMER_CLASS_CARD:
167 case SNDRV_TIMER_CLASS_PCM:
168 if (tid->card < snd_ecards_limit)
169 request_module("snd-card-%i", tid->card);
170 break;
171 default:
172 break;
173 }
174}
175
176#endif
177
178/*
179 * look for a master instance matching with the slave id of the given slave.
180 * when found, relink the open_link of the slave.
181 *
182 * call this with register_mutex down.
183 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100184static void snd_timer_check_slave(struct snd_timer_instance *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100186 struct snd_timer *timer;
187 struct snd_timer_instance *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 /* FIXME: it's really dumb to look up all entries.. */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200190 list_for_each_entry(timer, &snd_timer_list, device_list) {
191 list_for_each_entry(master, &timer->open_list_head, open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (slave->slave_class == master->slave_class &&
193 slave->slave_id == master->slave_id) {
Nicolas Kaiser5b7c7572011-03-16 17:10:11 +0100194 list_move_tail(&slave->open_list,
195 &master->slave_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 spin_lock_irq(&slave_active_lock);
197 slave->master = master;
198 slave->timer = master->timer;
199 spin_unlock_irq(&slave_active_lock);
200 return;
201 }
202 }
203 }
204}
205
206/*
207 * look for slave instances matching with the slave id of the given master.
208 * when found, relink the open_link of slaves.
209 *
210 * call this with register_mutex down.
211 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100212static void snd_timer_check_master(struct snd_timer_instance *master)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200214 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 /* check all pending slaves */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200217 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (slave->slave_class == master->slave_class &&
219 slave->slave_id == master->slave_id) {
Johannes Berg9244b2c2006-10-05 16:02:22 +0200220 list_move_tail(&slave->open_list, &master->slave_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 spin_lock_irq(&slave_active_lock);
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100222 spin_lock(&master->timer->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 slave->master = master;
224 slave->timer = master->timer;
225 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200226 list_add_tail(&slave->active_list,
227 &master->slave_active_head);
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100228 spin_unlock(&master->timer->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 spin_unlock_irq(&slave_active_lock);
230 }
231 }
232}
233
234/*
235 * open a timer instance
236 * when opening a master, the slave id must be here given.
237 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100238int snd_timer_open(struct snd_timer_instance **ti,
239 char *owner, struct snd_timer_id *tid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 unsigned int slave_id)
241{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100242 struct snd_timer *timer;
243 struct snd_timer_instance *timeri = NULL;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
246 /* open a slave instance */
247 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
248 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100249 pr_debug("ALSA: timer: invalid slave class %i\n",
250 tid->dev_sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return -EINVAL;
252 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100253 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 timeri = snd_timer_instance_new(owner, NULL);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200255 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100256 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200257 return -ENOMEM;
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 timeri->slave_class = tid->dev_sclass;
260 timeri->slave_id = tid->device;
261 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
262 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
263 snd_timer_check_slave(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100264 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 *ti = timeri;
266 return 0;
267 }
268
269 /* open a master instance */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100270 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 timer = snd_timer_find(tid);
Johannes Bergee2da992008-07-09 10:28:41 +0200272#ifdef CONFIG_MODULES
273 if (!timer) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100274 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 snd_timer_request(tid);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100276 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 timer = snd_timer_find(tid);
278 }
279#endif
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200280 if (!timer) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100281 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return -ENODEV;
283 }
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200284 if (!list_empty(&timer->open_list_head)) {
285 timeri = list_entry(timer->open_list_head.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +0100286 struct snd_timer_instance, open_list);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200287 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100288 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200289 return -EBUSY;
290 }
291 }
292 timeri = snd_timer_instance_new(owner, timer);
293 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100294 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200295 return -ENOMEM;
296 }
Takashi Iwai8d155be2016-01-21 17:19:31 +0100297 /* take a card refcount for safe disconnection */
298 if (timer->card)
299 get_device(&timer->card->card_dev);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200300 timeri->slave_class = tid->dev_sclass;
301 timeri->slave_id = slave_id;
Vegard Nossum14e3a782016-08-29 00:33:51 +0200302
303 if (list_empty(&timer->open_list_head) && timer->hw.open) {
304 int err = timer->hw.open(timer);
305 if (err) {
306 kfree(timeri->owner);
307 kfree(timeri);
308
309 if (timer->card)
310 put_device(&timer->card->card_dev);
311 module_put(timer->module);
312 mutex_unlock(&register_mutex);
313 return err;
314 }
315 }
316
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200317 list_add_tail(&timeri->open_list, &timer->open_list_head);
318 snd_timer_check_master(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100319 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 *ti = timeri;
321 return 0;
322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/*
325 * close a timer instance
326 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100327int snd_timer_close(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100329 struct snd_timer *timer = NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200330 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Takashi Iwai00728892008-08-14 07:51:57 +0200332 if (snd_BUG_ON(!timeri))
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200333 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /* force to stop the timer */
336 snd_timer_stop(timeri);
337
338 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
339 /* wait, until the active callback is finished */
340 spin_lock_irq(&slave_active_lock);
341 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
342 spin_unlock_irq(&slave_active_lock);
343 udelay(10);
344 spin_lock_irq(&slave_active_lock);
345 }
346 spin_unlock_irq(&slave_active_lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100347 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 list_del(&timeri->open_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100349 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 } else {
351 timer = timeri->timer;
Takashi Iwai94094c82011-08-08 12:28:22 +0200352 if (snd_BUG_ON(!timer))
353 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 /* wait, until the active callback is finished */
355 spin_lock_irq(&timer->lock);
356 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
357 spin_unlock_irq(&timer->lock);
358 udelay(10);
359 spin_lock_irq(&timer->lock);
360 }
361 spin_unlock_irq(&timer->lock);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100362 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 list_del(&timeri->open_list);
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100364 if (list_empty(&timer->open_list_head) &&
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200365 timer->hw.close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 timer->hw.close(timer);
367 /* remove slave links */
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100368 spin_lock_irq(&slave_active_lock);
369 spin_lock(&timer->lock);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200370 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
371 open_list) {
Johannes Berg9244b2c2006-10-05 16:02:22 +0200372 list_move_tail(&slave->open_list, &snd_timer_slave_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 slave->master = NULL;
374 slave->timer = NULL;
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100375 list_del_init(&slave->ack_list);
376 list_del_init(&slave->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100378 spin_unlock(&timer->lock);
379 spin_unlock_irq(&slave_active_lock);
Takashi Iwai8d155be2016-01-21 17:19:31 +0100380 /* release a card refcount for safe disconnection */
381 if (timer->card)
382 put_device(&timer->card->card_dev);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100383 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Takashi Iwai94094c82011-08-08 12:28:22 +0200385 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (timeri->private_free)
387 timeri->private_free(timeri);
388 kfree(timeri->owner);
389 kfree(timeri);
Clemens Ladischde242142005-10-12 17:12:31 +0200390 if (timer)
391 module_put(timer->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return 0;
393}
394
Takashi Iwai53d2f742005-11-17 13:56:05 +0100395unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100397 struct snd_timer * timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 if (timeri == NULL)
400 return 0;
401 if ((timer = timeri->timer) != NULL) {
402 if (timer->hw.c_resolution)
403 return timer->hw.c_resolution(timer);
404 return timer->hw.resolution;
405 }
406 return 0;
407}
408
Takashi Iwai53d2f742005-11-17 13:56:05 +0100409static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100411 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100413 struct snd_timer_instance *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct timespec tstamp;
415
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100416 if (timer_tstamp_monotonic)
Thomas Gleixner26204e02014-06-11 23:59:14 +0000417 ktime_get_ts(&tstamp);
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100418 else
419 getnstimeofday(&tstamp);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200420 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
421 event > SNDRV_TIMER_EVENT_PAUSE))
422 return;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200423 if (event == SNDRV_TIMER_EVENT_START ||
424 event == SNDRV_TIMER_EVENT_CONTINUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 resolution = snd_timer_resolution(ti);
426 if (ti->ccallback)
Jaroslav Kyselab30477d2010-03-03 11:05:55 +0100427 ti->ccallback(ti, event, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
429 return;
430 timer = ti->timer;
431 if (timer == NULL)
432 return;
433 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
434 return;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200435 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (ts->ccallback)
Takashi Iwai77d0a942016-02-08 17:36:25 +0100437 ts->ccallback(ts, event + 100, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Takashi Iwai11740142016-02-10 12:47:03 +0100440/* start/continue a master timer */
441static int snd_timer_start1(struct snd_timer_instance *timeri,
442 bool start, unsigned long ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Takashi Iwai11740142016-02-10 12:47:03 +0100444 struct snd_timer *timer;
445 int result;
446 unsigned long flags;
447
448 timer = timeri->timer;
449 if (!timer)
450 return -EINVAL;
451
452 spin_lock_irqsave(&timer->lock, flags);
453 if (timer->card && timer->card->shutdown) {
454 result = -ENODEV;
455 goto unlock;
456 }
457 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
458 SNDRV_TIMER_IFLG_START)) {
459 result = -EBUSY;
460 goto unlock;
461 }
462
463 if (start)
464 timeri->ticks = timeri->cticks = ticks;
465 else if (!timeri->cticks)
466 timeri->cticks = 1;
467 timeri->pticks = 0;
468
Nicolas Kaiser5b7c7572011-03-16 17:10:11 +0100469 list_move_tail(&timeri->active_list, &timer->active_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (timer->running) {
471 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
472 goto __start_now;
473 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
474 timeri->flags |= SNDRV_TIMER_IFLG_START;
Takashi Iwai11740142016-02-10 12:47:03 +0100475 result = 1; /* delayed start */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 } else {
Takashi Iwai11740142016-02-10 12:47:03 +0100477 if (start)
478 timer->sticks = ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 timer->hw.start(timer);
480 __start_now:
481 timer->running++;
482 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwai11740142016-02-10 12:47:03 +0100483 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Takashi Iwai11740142016-02-10 12:47:03 +0100485 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
486 SNDRV_TIMER_EVENT_CONTINUE);
487 unlock:
488 spin_unlock_irqrestore(&timer->lock, flags);
489 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Takashi Iwai11740142016-02-10 12:47:03 +0100492/* start/continue a slave timer */
493static int snd_timer_start_slave(struct snd_timer_instance *timeri,
494 bool start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 unsigned long flags;
497
498 spin_lock_irqsave(&slave_active_lock, flags);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100499 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
500 spin_unlock_irqrestore(&slave_active_lock, flags);
501 return -EBUSY;
502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100504 if (timeri->master && timeri->timer) {
505 spin_lock(&timeri->timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200506 list_add_tail(&timeri->active_list,
507 &timeri->master->slave_active_head);
Takashi Iwai11740142016-02-10 12:47:03 +0100508 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
509 SNDRV_TIMER_EVENT_CONTINUE);
Takashi Iwaif40ee9c2016-01-14 16:30:58 +0100510 spin_unlock(&timeri->timer->lock);
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 spin_unlock_irqrestore(&slave_active_lock, flags);
513 return 1; /* delayed start */
514}
515
Takashi Iwai11740142016-02-10 12:47:03 +0100516/* stop/pause a master timer */
517static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100519 struct snd_timer *timer;
Takashi Iwai11740142016-02-10 12:47:03 +0100520 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 unsigned long flags;
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 timer = timeri->timer;
524 if (!timer)
525 return -EINVAL;
526 spin_lock_irqsave(&timer->lock, flags);
Takashi Iwai9f750af2016-01-30 23:09:08 +0100527 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
528 SNDRV_TIMER_IFLG_START))) {
Takashi Iwai11740142016-02-10 12:47:03 +0100529 result = -EBUSY;
530 goto unlock;
Takashi Iwai9f750af2016-01-30 23:09:08 +0100531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 list_del_init(&timeri->ack_list);
533 list_del_init(&timeri->active_list);
Takashi Iwai11740142016-02-10 12:47:03 +0100534 if (timer->card && timer->card->shutdown)
535 goto unlock;
536 if (stop) {
537 timeri->cticks = timeri->ticks;
538 timeri->pticks = 0;
Takashi Iwai8d155be2016-01-21 17:19:31 +0100539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
541 !(--timer->running)) {
542 timer->hw.stop(timer);
543 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
544 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
545 snd_timer_reschedule(timer, 0);
546 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
547 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
548 timer->hw.start(timer);
549 }
550 }
551 }
Takashi Iwaid7a258c2016-01-14 17:01:46 +0100552 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
Takashi Iwai1a30ab32016-09-07 15:45:31 +0200553 if (stop)
554 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
555 else
556 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
Takashi Iwai11740142016-02-10 12:47:03 +0100557 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
558 SNDRV_TIMER_EVENT_CONTINUE);
559 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 spin_unlock_irqrestore(&timer->lock, flags);
Takashi Iwai11740142016-02-10 12:47:03 +0100561 return result;
562}
563
564/* stop/pause a slave timer */
565static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
566{
567 unsigned long flags;
568
569 spin_lock_irqsave(&slave_active_lock, flags);
570 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
571 spin_unlock_irqrestore(&slave_active_lock, flags);
572 return -EBUSY;
573 }
574 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
575 if (timeri->timer) {
576 spin_lock(&timeri->timer->lock);
577 list_del_init(&timeri->ack_list);
578 list_del_init(&timeri->active_list);
579 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
580 SNDRV_TIMER_EVENT_CONTINUE);
581 spin_unlock(&timeri->timer->lock);
582 }
583 spin_unlock_irqrestore(&slave_active_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return 0;
585}
586
587/*
Takashi Iwai11740142016-02-10 12:47:03 +0100588 * start the timer instance
589 */
590int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
591{
592 if (timeri == NULL || ticks < 1)
593 return -EINVAL;
594 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
595 return snd_timer_start_slave(timeri, true);
596 else
597 return snd_timer_start1(timeri, true, ticks);
598}
599
600/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 * stop the timer instance.
602 *
603 * do not call this from the timer callback!
604 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100605int snd_timer_stop(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Takashi Iwai11740142016-02-10 12:47:03 +0100607 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
608 return snd_timer_stop_slave(timeri, true);
609 else
610 return snd_timer_stop1(timeri, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613/*
614 * start again.. the tick is kept.
615 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100616int snd_timer_continue(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Takashi Iwai1a30ab32016-09-07 15:45:31 +0200618 /* timer can continue only after pause */
619 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
620 return -EINVAL;
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
Takashi Iwai11740142016-02-10 12:47:03 +0100623 return snd_timer_start_slave(timeri, false);
624 else
625 return snd_timer_start1(timeri, false, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628/*
629 * pause.. remember the ticks left
630 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100631int snd_timer_pause(struct snd_timer_instance * timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Takashi Iwai11740142016-02-10 12:47:03 +0100633 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
634 return snd_timer_stop_slave(timeri, false);
635 else
636 return snd_timer_stop1(timeri, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639/*
640 * reschedule the timer
641 *
642 * start pending instances and check the scheduling ticks.
643 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
644 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100645static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100647 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 unsigned long ticks = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Johannes Berg9244b2c2006-10-05 16:02:22 +0200650 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (ti->flags & SNDRV_TIMER_IFLG_START) {
652 ti->flags &= ~SNDRV_TIMER_IFLG_START;
653 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
654 timer->running++;
655 }
656 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
657 if (ticks > ti->cticks)
658 ticks = ti->cticks;
659 }
660 }
661 if (ticks == ~0UL) {
662 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
663 return;
664 }
665 if (ticks > timer->hw.ticks)
666 ticks = timer->hw.ticks;
667 if (ticks_left != ticks)
668 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
669 timer->sticks = ticks;
670}
671
672/*
673 * timer tasklet
674 *
675 */
676static void snd_timer_tasklet(unsigned long arg)
677{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100678 struct snd_timer *timer = (struct snd_timer *) arg;
679 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 struct list_head *p;
681 unsigned long resolution, ticks;
Takashi Iwai2999ff52006-07-05 17:16:58 +0200682 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Takashi Iwai8d155be2016-01-21 17:19:31 +0100684 if (timer->card && timer->card->shutdown)
685 return;
686
Takashi Iwai2999ff52006-07-05 17:16:58 +0200687 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* now process all callbacks */
689 while (!list_empty(&timer->sack_list_head)) {
690 p = timer->sack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100691 ti = list_entry(p, struct snd_timer_instance, ack_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /* remove from ack_list and make empty */
694 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 ticks = ti->pticks;
697 ti->pticks = 0;
698 resolution = ti->resolution;
699
700 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
701 spin_unlock(&timer->lock);
702 if (ti->callback)
703 ti->callback(ti, resolution, ticks);
704 spin_lock(&timer->lock);
705 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
706 }
Takashi Iwai2999ff52006-07-05 17:16:58 +0200707 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710/*
711 * timer interrupt
712 *
713 * ticks_left is usually equal to timer->sticks.
714 *
715 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100716void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200718 struct snd_timer_instance *ti, *ts, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 unsigned long resolution, ticks;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200720 struct list_head *p, *ack_list_head;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100721 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 int use_tasklet = 0;
723
724 if (timer == NULL)
725 return;
726
Takashi Iwai8d155be2016-01-21 17:19:31 +0100727 if (timer->card && timer->card->shutdown)
728 return;
729
Takashi Iwaib32425a2005-11-18 18:52:14 +0100730 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 /* remember the current resolution */
733 if (timer->hw.c_resolution)
734 resolution = timer->hw.c_resolution(timer);
735 else
736 resolution = timer->hw.resolution;
737
738 /* loop for all active instances
Johannes Berg9244b2c2006-10-05 16:02:22 +0200739 * Here we cannot use list_for_each_entry because the active_list of a
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200740 * processed instance is relinked to done_list_head before the callback
741 * is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200743 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
744 active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
746 continue;
747 ti->pticks += ticks_left;
748 ti->resolution = resolution;
749 if (ti->cticks < ticks_left)
750 ti->cticks = 0;
751 else
752 ti->cticks -= ticks_left;
753 if (ti->cticks) /* not expired */
754 continue;
755 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
756 ti->cticks = ti->ticks;
757 } else {
758 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwai2562c292016-02-04 17:06:13 +0100759 --timer->running;
760 list_del_init(&ti->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200762 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
763 (ti->flags & SNDRV_TIMER_IFLG_FAST))
764 ack_list_head = &timer->ack_list_head;
765 else
766 ack_list_head = &timer->sack_list_head;
767 if (list_empty(&ti->ack_list))
768 list_add_tail(&ti->ack_list, ack_list_head);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200769 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 ts->pticks = ti->pticks;
771 ts->resolution = resolution;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200772 if (list_empty(&ts->ack_list))
773 list_add_tail(&ts->ack_list, ack_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775 }
776 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
Clemens Ladischcd93fe42006-07-17 16:53:57 +0200777 snd_timer_reschedule(timer, timer->sticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (timer->running) {
779 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
780 timer->hw.stop(timer);
781 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
782 }
783 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
784 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
785 /* restart timer */
786 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
787 timer->hw.start(timer);
788 }
789 } else {
790 timer->hw.stop(timer);
791 }
792
793 /* now process all fast callbacks */
794 while (!list_empty(&timer->ack_list_head)) {
795 p = timer->ack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100796 ti = list_entry(p, struct snd_timer_instance, ack_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 /* remove from ack_list and make empty */
799 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 ticks = ti->pticks;
802 ti->pticks = 0;
803
804 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
805 spin_unlock(&timer->lock);
806 if (ti->callback)
807 ti->callback(ti, resolution, ticks);
808 spin_lock(&timer->lock);
809 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
810 }
811
812 /* do we have any slow callbacks? */
813 use_tasklet = !list_empty(&timer->sack_list_head);
Takashi Iwaib32425a2005-11-18 18:52:14 +0100814 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (use_tasklet)
Takashi Iwai1f041282008-12-18 12:17:55 +0100817 tasklet_schedule(&timer->task_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
820/*
821
822 */
823
Takashi Iwai53d2f742005-11-17 13:56:05 +0100824int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
825 struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100827 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100829 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 .dev_free = snd_timer_dev_free,
831 .dev_register = snd_timer_dev_register,
Takashi Iwaic4614822006-06-23 14:38:23 +0200832 .dev_disconnect = snd_timer_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 };
834
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200835 if (snd_BUG_ON(!tid))
836 return -EINVAL;
837 if (rtimer)
838 *rtimer = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200839 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100840 if (timer == NULL) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100841 pr_err("ALSA: timer: cannot allocate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return -ENOMEM;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 timer->tmr_class = tid->dev_class;
845 timer->card = card;
846 timer->tmr_device = tid->device;
847 timer->tmr_subdevice = tid->subdevice;
848 if (id)
849 strlcpy(timer->id, id, sizeof(timer->id));
Vegard Nossuma987c622016-08-29 00:33:50 +0200850 timer->sticks = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 INIT_LIST_HEAD(&timer->device_list);
852 INIT_LIST_HEAD(&timer->open_list_head);
853 INIT_LIST_HEAD(&timer->active_list_head);
854 INIT_LIST_HEAD(&timer->ack_list_head);
855 INIT_LIST_HEAD(&timer->sack_list_head);
856 spin_lock_init(&timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200857 tasklet_init(&timer->task_queue, snd_timer_tasklet,
858 (unsigned long)timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (card != NULL) {
Clemens Ladischde242142005-10-12 17:12:31 +0200860 timer->module = card->module;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200861 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
862 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 snd_timer_free(timer);
864 return err;
865 }
866 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200867 if (rtimer)
868 *rtimer = timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return 0;
870}
871
Takashi Iwai53d2f742005-11-17 13:56:05 +0100872static int snd_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200874 if (!timer)
875 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +0200876
877 mutex_lock(&register_mutex);
878 if (! list_empty(&timer->open_list_head)) {
879 struct list_head *p, *n;
880 struct snd_timer_instance *ti;
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100881 pr_warn("ALSA: timer %p is busy?\n", timer);
Takashi Iwaic4614822006-06-23 14:38:23 +0200882 list_for_each_safe(p, n, &timer->open_list_head) {
883 list_del_init(p);
884 ti = list_entry(p, struct snd_timer_instance, open_list);
885 ti->timer = NULL;
886 }
887 }
888 list_del(&timer->device_list);
889 mutex_unlock(&register_mutex);
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (timer->private_free)
892 timer->private_free(timer);
893 kfree(timer);
894 return 0;
895}
896
Takashi Iwai53d2f742005-11-17 13:56:05 +0100897static int snd_timer_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100899 struct snd_timer *timer = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return snd_timer_free(timer);
901}
902
Takashi Iwai53d2f742005-11-17 13:56:05 +0100903static int snd_timer_dev_register(struct snd_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100905 struct snd_timer *timer = dev->device_data;
906 struct snd_timer *timer1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200908 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
909 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
911 !timer->hw.resolution && timer->hw.c_resolution == NULL)
912 return -EINVAL;
913
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100914 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200915 list_for_each_entry(timer1, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (timer1->tmr_class > timer->tmr_class)
917 break;
918 if (timer1->tmr_class < timer->tmr_class)
919 continue;
920 if (timer1->card && timer->card) {
921 if (timer1->card->number > timer->card->number)
922 break;
923 if (timer1->card->number < timer->card->number)
924 continue;
925 }
926 if (timer1->tmr_device > timer->tmr_device)
927 break;
928 if (timer1->tmr_device < timer->tmr_device)
929 continue;
930 if (timer1->tmr_subdevice > timer->tmr_subdevice)
931 break;
932 if (timer1->tmr_subdevice < timer->tmr_subdevice)
933 continue;
934 /* conflicts.. */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100935 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return -EBUSY;
937 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200938 list_add_tail(&timer->device_list, &timer1->device_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100939 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return 0;
941}
942
Takashi Iwai8d155be2016-01-21 17:19:31 +0100943/* just for reference in snd_timer_dev_disconnect() below */
944static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
945 int event, struct timespec *tstamp,
946 unsigned long resolution);
947
Takashi Iwaic4614822006-06-23 14:38:23 +0200948static int snd_timer_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100950 struct snd_timer *timer = device->device_data;
Takashi Iwai8d155be2016-01-21 17:19:31 +0100951 struct snd_timer_instance *ti;
952
Takashi Iwaic4614822006-06-23 14:38:23 +0200953 mutex_lock(&register_mutex);
954 list_del_init(&timer->device_list);
Takashi Iwai8d155be2016-01-21 17:19:31 +0100955 /* wake up pending sleepers */
956 list_for_each_entry(ti, &timer->open_list_head, open_list) {
957 /* FIXME: better to have a ti.disconnect() op */
958 if (ti->ccallback == snd_timer_user_ccallback) {
959 struct snd_timer_user *tu = ti->callback_data;
960
961 tu->disconnected = true;
962 wake_up(&tu->qchange_sleep);
963 }
964 }
Takashi Iwaic4614822006-06-23 14:38:23 +0200965 mutex_unlock(&register_mutex);
966 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
Takashi Iwai53d2f742005-11-17 13:56:05 +0100969void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 unsigned long flags;
972 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100973 struct snd_timer_instance *ti, *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Takashi Iwai8d155be2016-01-21 17:19:31 +0100975 if (timer->card && timer->card->shutdown)
976 return;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200977 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
978 return;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200979 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
980 event > SNDRV_TIMER_EVENT_MRESUME))
981 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 spin_lock_irqsave(&timer->lock, flags);
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +0200983 if (event == SNDRV_TIMER_EVENT_MSTART ||
984 event == SNDRV_TIMER_EVENT_MCONTINUE ||
985 event == SNDRV_TIMER_EVENT_MRESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (timer->hw.c_resolution)
987 resolution = timer->hw.c_resolution(timer);
988 else
989 resolution = timer->hw.resolution;
990 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200991 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (ti->ccallback)
993 ti->ccallback(ti, event, tstamp, resolution);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200994 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (ts->ccallback)
996 ts->ccallback(ts, event, tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998 spin_unlock_irqrestore(&timer->lock, flags);
999}
1000
1001/*
1002 * exported functions for global timers
1003 */
Takashi Iwai53d2f742005-11-17 13:56:05 +01001004int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001006 struct snd_timer_id tid;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1009 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1010 tid.card = -1;
1011 tid.device = device;
1012 tid.subdevice = 0;
1013 return snd_timer_new(NULL, id, &tid, rtimer);
1014}
1015
Takashi Iwai53d2f742005-11-17 13:56:05 +01001016int snd_timer_global_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
1018 return snd_timer_free(timer);
1019}
1020
Takashi Iwai53d2f742005-11-17 13:56:05 +01001021int snd_timer_global_register(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001023 struct snd_device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 memset(&dev, 0, sizeof(dev));
1026 dev.device_data = timer;
1027 return snd_timer_dev_register(&dev);
1028}
1029
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001030/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 * System timer
1032 */
1033
1034struct snd_timer_system_private {
1035 struct timer_list tlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 unsigned long last_expires;
1037 unsigned long last_jiffies;
1038 unsigned long correction;
1039};
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041static void snd_timer_s_function(unsigned long data)
1042{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001043 struct snd_timer *timer = (struct snd_timer *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 struct snd_timer_system_private *priv = timer->private_data;
1045 unsigned long jiff = jiffies;
1046 if (time_after(jiff, priv->last_expires))
Clemens Ladisch6ed5eff2006-07-17 16:51:37 +02001047 priv->correction += (long)jiff - (long)priv->last_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1049}
1050
Takashi Iwai53d2f742005-11-17 13:56:05 +01001051static int snd_timer_s_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
1053 struct snd_timer_system_private *priv;
1054 unsigned long njiff;
1055
1056 priv = (struct snd_timer_system_private *) timer->private_data;
1057 njiff = (priv->last_jiffies = jiffies);
1058 if (priv->correction > timer->sticks - 1) {
1059 priv->correction -= timer->sticks - 1;
1060 njiff++;
1061 } else {
1062 njiff += timer->sticks - priv->correction;
Clemens Ladisch17f48ec2006-07-17 16:50:56 +02001063 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
Takashi Iwaid4f95a02016-04-01 12:28:16 +02001065 priv->last_expires = njiff;
1066 mod_timer(&priv->tlist, njiff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return 0;
1068}
1069
Takashi Iwai53d2f742005-11-17 13:56:05 +01001070static int snd_timer_s_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 struct snd_timer_system_private *priv;
1073 unsigned long jiff;
1074
1075 priv = (struct snd_timer_system_private *) timer->private_data;
1076 del_timer(&priv->tlist);
1077 jiff = jiffies;
1078 if (time_before(jiff, priv->last_expires))
1079 timer->sticks = priv->last_expires - jiff;
1080 else
1081 timer->sticks = 1;
Clemens Ladischde2696d2006-07-17 16:52:09 +02001082 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return 0;
1084}
1085
Takashi Iwai53d2f742005-11-17 13:56:05 +01001086static struct snd_timer_hardware snd_timer_system =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1089 .resolution = 1000000000L / HZ,
1090 .ticks = 10000000L,
1091 .start = snd_timer_s_start,
1092 .stop = snd_timer_s_stop
1093};
1094
Takashi Iwai53d2f742005-11-17 13:56:05 +01001095static void snd_timer_free_system(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 kfree(timer->private_data);
1098}
1099
1100static int snd_timer_register_system(void)
1101{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001102 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 struct snd_timer_system_private *priv;
1104 int err;
1105
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001106 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1107 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return err;
1109 strcpy(timer->name, "system timer");
1110 timer->hw = snd_timer_system;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001111 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (priv == NULL) {
1113 snd_timer_free(timer);
1114 return -ENOMEM;
1115 }
1116 init_timer(&priv->tlist);
1117 priv->tlist.function = snd_timer_s_function;
1118 priv->tlist.data = (unsigned long) timer;
1119 timer->private_data = priv;
1120 timer->private_free = snd_timer_free_system;
1121 return snd_timer_global_register(timer);
1122}
1123
Takashi Iwaie28563c2005-12-01 10:42:42 +01001124#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125/*
1126 * Info interface
1127 */
1128
Takashi Iwai53d2f742005-11-17 13:56:05 +01001129static void snd_timer_proc_read(struct snd_info_entry *entry,
1130 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001132 struct snd_timer *timer;
1133 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001135 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001136 list_for_each_entry(timer, &snd_timer_list, device_list) {
Takashi Iwai8d155be2016-01-21 17:19:31 +01001137 if (timer->card && timer->card->shutdown)
1138 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 switch (timer->tmr_class) {
1140 case SNDRV_TIMER_CLASS_GLOBAL:
1141 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1142 break;
1143 case SNDRV_TIMER_CLASS_CARD:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001144 snd_iprintf(buffer, "C%i-%i: ",
1145 timer->card->number, timer->tmr_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 break;
1147 case SNDRV_TIMER_CLASS_PCM:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001148 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1149 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 break;
1151 default:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001152 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1153 timer->card ? timer->card->number : -1,
1154 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156 snd_iprintf(buffer, "%s :", timer->name);
1157 if (timer->hw.resolution)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001158 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1159 timer->hw.resolution / 1000,
1160 timer->hw.resolution % 1000,
1161 timer->hw.ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1163 snd_iprintf(buffer, " SLAVE");
1164 snd_iprintf(buffer, "\n");
Johannes Berg9244b2c2006-10-05 16:02:22 +02001165 list_for_each_entry(ti, &timer->open_list_head, open_list)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001166 snd_iprintf(buffer, " Client %s : %s\n",
1167 ti->owner ? ti->owner : "unknown",
1168 ti->flags & (SNDRV_TIMER_IFLG_START |
1169 SNDRV_TIMER_IFLG_RUNNING)
1170 ? "running" : "stopped");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001172 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001175static struct snd_info_entry *snd_timer_proc_entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001176
1177static void __init snd_timer_proc_init(void)
1178{
1179 struct snd_info_entry *entry;
1180
1181 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1182 if (entry != NULL) {
Takashi Iwaie28563c2005-12-01 10:42:42 +01001183 entry->c.text.read = snd_timer_proc_read;
1184 if (snd_info_register(entry) < 0) {
1185 snd_info_free_entry(entry);
1186 entry = NULL;
1187 }
1188 }
1189 snd_timer_proc_entry = entry;
1190}
1191
1192static void __exit snd_timer_proc_done(void)
1193{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001194 snd_info_free_entry(snd_timer_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001195}
1196#else /* !CONFIG_PROC_FS */
1197#define snd_timer_proc_init()
1198#define snd_timer_proc_done()
1199#endif
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201/*
1202 * USER SPACE interface
1203 */
1204
Takashi Iwai53d2f742005-11-17 13:56:05 +01001205static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 unsigned long resolution,
1207 unsigned long ticks)
1208{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001209 struct snd_timer_user *tu = timeri->callback_data;
1210 struct snd_timer_read *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 int prev;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 spin_lock(&tu->qlock);
1214 if (tu->qused > 0) {
1215 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1216 r = &tu->queue[prev];
1217 if (r->resolution == resolution) {
1218 r->ticks += ticks;
1219 goto __wake;
1220 }
1221 }
1222 if (tu->qused >= tu->queue_size) {
1223 tu->overrun++;
1224 } else {
1225 r = &tu->queue[tu->qtail++];
1226 tu->qtail %= tu->queue_size;
1227 r->resolution = resolution;
1228 r->ticks = ticks;
1229 tu->qused++;
1230 }
1231 __wake:
1232 spin_unlock(&tu->qlock);
1233 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1234 wake_up(&tu->qchange_sleep);
1235}
1236
Takashi Iwai53d2f742005-11-17 13:56:05 +01001237static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1238 struct snd_timer_tread *tread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 if (tu->qused >= tu->queue_size) {
1241 tu->overrun++;
1242 } else {
1243 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1244 tu->qtail %= tu->queue_size;
1245 tu->qused++;
1246 }
1247}
1248
Takashi Iwai53d2f742005-11-17 13:56:05 +01001249static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1250 int event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 struct timespec *tstamp,
1252 unsigned long resolution)
1253{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001254 struct snd_timer_user *tu = timeri->callback_data;
1255 struct snd_timer_tread r1;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001256 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001258 if (event >= SNDRV_TIMER_EVENT_START &&
1259 event <= SNDRV_TIMER_EVENT_PAUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 tu->tstamp = *tstamp;
1261 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1262 return;
1263 r1.event = event;
1264 r1.tstamp = *tstamp;
1265 r1.val = resolution;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001266 spin_lock_irqsave(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 snd_timer_user_append_to_tqueue(tu, &r1);
Dan Carpenterbfe70782010-04-28 10:29:14 +02001268 spin_unlock_irqrestore(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1270 wake_up(&tu->qchange_sleep);
1271}
1272
Takashi Iwai53d2f742005-11-17 13:56:05 +01001273static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 unsigned long resolution,
1275 unsigned long ticks)
1276{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001277 struct snd_timer_user *tu = timeri->callback_data;
1278 struct snd_timer_tread *r, r1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 struct timespec tstamp;
1280 int prev, append = 0;
1281
Takashi Iwai07799e72005-10-10 11:49:49 +02001282 memset(&tstamp, 0, sizeof(tstamp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 spin_lock(&tu->qlock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001284 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1285 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 spin_unlock(&tu->qlock);
1287 return;
1288 }
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001289 if (tu->last_resolution != resolution || ticks > 0) {
1290 if (timer_tstamp_monotonic)
Thomas Gleixner26204e02014-06-11 23:59:14 +00001291 ktime_get_ts(&tstamp);
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001292 else
1293 getnstimeofday(&tstamp);
1294 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001295 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1296 tu->last_resolution != resolution) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1298 r1.tstamp = tstamp;
1299 r1.val = resolution;
1300 snd_timer_user_append_to_tqueue(tu, &r1);
1301 tu->last_resolution = resolution;
1302 append++;
1303 }
1304 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1305 goto __wake;
1306 if (ticks == 0)
1307 goto __wake;
1308 if (tu->qused > 0) {
1309 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1310 r = &tu->tqueue[prev];
1311 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1312 r->tstamp = tstamp;
1313 r->val += ticks;
1314 append++;
1315 goto __wake;
1316 }
1317 }
1318 r1.event = SNDRV_TIMER_EVENT_TICK;
1319 r1.tstamp = tstamp;
1320 r1.val = ticks;
1321 snd_timer_user_append_to_tqueue(tu, &r1);
1322 append++;
1323 __wake:
1324 spin_unlock(&tu->qlock);
1325 if (append == 0)
1326 return;
1327 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1328 wake_up(&tu->qchange_sleep);
1329}
1330
1331static int snd_timer_user_open(struct inode *inode, struct file *file)
1332{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001333 struct snd_timer_user *tu;
Takashi Iwai02f48652010-04-13 11:49:04 +02001334 int err;
1335
1336 err = nonseekable_open(inode, file);
1337 if (err < 0)
1338 return err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001339
Takashi Iwaica2c0962005-09-09 14:20:23 +02001340 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (tu == NULL)
1342 return -ENOMEM;
1343 spin_lock_init(&tu->qlock);
1344 init_waitqueue_head(&tu->qchange_sleep);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001345 mutex_init(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 tu->ticks = 1;
1347 tu->queue_size = 128;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001348 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001349 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (tu->queue == NULL) {
1351 kfree(tu);
1352 return -ENOMEM;
1353 }
1354 file->private_data = tu;
1355 return 0;
1356}
1357
1358static int snd_timer_user_release(struct inode *inode, struct file *file)
1359{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001360 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 if (file->private_data) {
1363 tu = file->private_data;
1364 file->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (tu->timeri)
1366 snd_timer_close(tu->timeri);
1367 kfree(tu->queue);
1368 kfree(tu->tqueue);
1369 kfree(tu);
1370 }
1371 return 0;
1372}
1373
Takashi Iwai53d2f742005-11-17 13:56:05 +01001374static void snd_timer_user_zero_id(struct snd_timer_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1377 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1378 id->card = -1;
1379 id->device = -1;
1380 id->subdevice = -1;
1381}
1382
Takashi Iwai53d2f742005-11-17 13:56:05 +01001383static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
1385 id->dev_class = timer->tmr_class;
1386 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1387 id->card = timer->card ? timer->card->number : -1;
1388 id->device = timer->tmr_device;
1389 id->subdevice = timer->tmr_subdevice;
1390}
1391
Takashi Iwai53d2f742005-11-17 13:56:05 +01001392static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001394 struct snd_timer_id id;
1395 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 struct list_head *p;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (copy_from_user(&id, _tid, sizeof(id)))
1399 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001400 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (id.dev_class < 0) { /* first item */
1402 if (list_empty(&snd_timer_list))
1403 snd_timer_user_zero_id(&id);
1404 else {
Clemens Ladisch9dfba382005-10-12 17:14:55 +02001405 timer = list_entry(snd_timer_list.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001406 struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 snd_timer_user_copy_id(&id, timer);
1408 }
1409 } else {
1410 switch (id.dev_class) {
1411 case SNDRV_TIMER_CLASS_GLOBAL:
1412 id.device = id.device < 0 ? 0 : id.device + 1;
1413 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001414 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1416 snd_timer_user_copy_id(&id, timer);
1417 break;
1418 }
1419 if (timer->tmr_device >= id.device) {
1420 snd_timer_user_copy_id(&id, timer);
1421 break;
1422 }
1423 }
1424 if (p == &snd_timer_list)
1425 snd_timer_user_zero_id(&id);
1426 break;
1427 case SNDRV_TIMER_CLASS_CARD:
1428 case SNDRV_TIMER_CLASS_PCM:
1429 if (id.card < 0) {
1430 id.card = 0;
1431 } else {
1432 if (id.card < 0) {
1433 id.card = 0;
1434 } else {
1435 if (id.device < 0) {
1436 id.device = 0;
1437 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001438 if (id.subdevice < 0) {
1439 id.subdevice = 0;
1440 } else {
1441 id.subdevice++;
1442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444 }
1445 }
1446 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001447 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (timer->tmr_class > id.dev_class) {
1449 snd_timer_user_copy_id(&id, timer);
1450 break;
1451 }
1452 if (timer->tmr_class < id.dev_class)
1453 continue;
1454 if (timer->card->number > id.card) {
1455 snd_timer_user_copy_id(&id, timer);
1456 break;
1457 }
1458 if (timer->card->number < id.card)
1459 continue;
1460 if (timer->tmr_device > id.device) {
1461 snd_timer_user_copy_id(&id, timer);
1462 break;
1463 }
1464 if (timer->tmr_device < id.device)
1465 continue;
1466 if (timer->tmr_subdevice > id.subdevice) {
1467 snd_timer_user_copy_id(&id, timer);
1468 break;
1469 }
1470 if (timer->tmr_subdevice < id.subdevice)
1471 continue;
1472 snd_timer_user_copy_id(&id, timer);
1473 break;
1474 }
1475 if (p == &snd_timer_list)
1476 snd_timer_user_zero_id(&id);
1477 break;
1478 default:
1479 snd_timer_user_zero_id(&id);
1480 }
1481 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001482 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1484 return -EFAULT;
1485 return 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001486}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001488static int snd_timer_user_ginfo(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001489 struct snd_timer_ginfo __user *_ginfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001491 struct snd_timer_ginfo *ginfo;
1492 struct snd_timer_id tid;
1493 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 struct list_head *p;
1495 int err = 0;
1496
Li Zefanef44a1e2009-04-10 09:43:08 +08001497 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1498 if (IS_ERR(ginfo))
1499 return PTR_ERR(ginfo);
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 tid = ginfo->tid;
1502 memset(ginfo, 0, sizeof(*ginfo));
1503 ginfo->tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001504 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 t = snd_timer_find(&tid);
1506 if (t != NULL) {
1507 ginfo->card = t->card ? t->card->number : -1;
1508 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1509 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1510 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1511 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1512 ginfo->resolution = t->hw.resolution;
1513 if (t->hw.resolution_min > 0) {
1514 ginfo->resolution_min = t->hw.resolution_min;
1515 ginfo->resolution_max = t->hw.resolution_max;
1516 }
1517 list_for_each(p, &t->open_list_head) {
1518 ginfo->clients++;
1519 }
1520 } else {
1521 err = -ENODEV;
1522 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001523 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1525 err = -EFAULT;
1526 kfree(ginfo);
1527 return err;
1528}
1529
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001530static int snd_timer_user_gparams(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001531 struct snd_timer_gparams __user *_gparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001533 struct snd_timer_gparams gparams;
1534 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 int err;
1536
1537 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1538 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001539 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 t = snd_timer_find(&gparams.tid);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001541 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 err = -ENODEV;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001543 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001545 if (!list_empty(&t->open_list_head)) {
1546 err = -EBUSY;
1547 goto _error;
1548 }
1549 if (!t->hw.set_period) {
1550 err = -ENOSYS;
1551 goto _error;
1552 }
1553 err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
1554_error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001555 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return err;
1557}
1558
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001559static int snd_timer_user_gstatus(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001560 struct snd_timer_gstatus __user *_gstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001562 struct snd_timer_gstatus gstatus;
1563 struct snd_timer_id tid;
1564 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 int err = 0;
1566
1567 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1568 return -EFAULT;
1569 tid = gstatus.tid;
1570 memset(&gstatus, 0, sizeof(gstatus));
1571 gstatus.tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001572 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 t = snd_timer_find(&tid);
1574 if (t != NULL) {
1575 if (t->hw.c_resolution)
1576 gstatus.resolution = t->hw.c_resolution(t);
1577 else
1578 gstatus.resolution = t->hw.resolution;
1579 if (t->hw.precise_resolution) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001580 t->hw.precise_resolution(t, &gstatus.resolution_num,
1581 &gstatus.resolution_den);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 } else {
1583 gstatus.resolution_num = gstatus.resolution;
1584 gstatus.resolution_den = 1000000000uL;
1585 }
1586 } else {
1587 err = -ENODEV;
1588 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001589 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1591 err = -EFAULT;
1592 return err;
1593}
1594
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001595static int snd_timer_user_tselect(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001596 struct snd_timer_select __user *_tselect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001598 struct snd_timer_user *tu;
1599 struct snd_timer_select tselect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 char str[32];
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001601 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 tu = file->private_data;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001604 mutex_lock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001605 if (tu->timeri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 snd_timer_close(tu->timeri);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001607 tu->timeri = NULL;
1608 }
1609 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1610 err = -EFAULT;
1611 goto __err;
1612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 sprintf(str, "application %i", current->pid);
1614 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1615 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001616 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1617 if (err < 0)
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001618 goto __err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Jesper Juhl4d572772005-05-30 17:30:32 +02001620 kfree(tu->queue);
1621 tu->queue = NULL;
1622 kfree(tu->tqueue);
1623 tu->tqueue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001625 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001626 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001627 if (tu->tqueue == NULL)
1628 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001630 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001631 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001632 if (tu->queue == NULL)
1633 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001635
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001636 if (err < 0) {
1637 snd_timer_close(tu->timeri);
1638 tu->timeri = NULL;
1639 } else {
1640 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001641 tu->timeri->callback = tu->tread
1642 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001643 tu->timeri->ccallback = snd_timer_user_ccallback;
1644 tu->timeri->callback_data = (void *)tu;
1645 }
1646
1647 __err:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001648 mutex_unlock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001649 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001652static int snd_timer_user_info(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001653 struct snd_timer_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001655 struct snd_timer_user *tu;
1656 struct snd_timer_info *info;
1657 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 int err = 0;
1659
1660 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001661 if (!tu->timeri)
1662 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001664 if (!t)
1665 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Takashi Iwaica2c0962005-09-09 14:20:23 +02001667 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if (! info)
1669 return -ENOMEM;
1670 info->card = t->card ? t->card->number : -1;
1671 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1672 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1673 strlcpy(info->id, t->id, sizeof(info->id));
1674 strlcpy(info->name, t->name, sizeof(info->name));
1675 info->resolution = t->hw.resolution;
1676 if (copy_to_user(_info, info, sizeof(*_info)))
1677 err = -EFAULT;
1678 kfree(info);
1679 return err;
1680}
1681
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001682static int snd_timer_user_params(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001683 struct snd_timer_params __user *_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001685 struct snd_timer_user *tu;
1686 struct snd_timer_params params;
1687 struct snd_timer *t;
1688 struct snd_timer_read *tr;
1689 struct snd_timer_tread *ttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001693 if (!tu->timeri)
1694 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001696 if (!t)
1697 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (copy_from_user(&params, _params, sizeof(params)))
1699 return -EFAULT;
1700 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
1701 err = -EINVAL;
1702 goto _end;
1703 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001704 if (params.queue_size > 0 &&
1705 (params.queue_size < 32 || params.queue_size > 1024)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 err = -EINVAL;
1707 goto _end;
1708 }
1709 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1710 (1<<SNDRV_TIMER_EVENT_TICK)|
1711 (1<<SNDRV_TIMER_EVENT_START)|
1712 (1<<SNDRV_TIMER_EVENT_STOP)|
1713 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1714 (1<<SNDRV_TIMER_EVENT_PAUSE)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001715 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1716 (1<<SNDRV_TIMER_EVENT_RESUME)|
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 (1<<SNDRV_TIMER_EVENT_MSTART)|
1718 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1719 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
Jaroslav Kysela65d11d92005-08-16 13:05:43 +02001720 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1721 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001722 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 err = -EINVAL;
1724 goto _end;
1725 }
1726 snd_timer_stop(tu->timeri);
1727 spin_lock_irq(&t->lock);
1728 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1729 SNDRV_TIMER_IFLG_EXCLUSIVE|
1730 SNDRV_TIMER_IFLG_EARLY_EVENT);
1731 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1732 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1733 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1734 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1735 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1736 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1737 spin_unlock_irq(&t->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001738 if (params.queue_size > 0 &&
1739 (unsigned int)tu->queue_size != params.queue_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (tu->tread) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001741 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1742 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 if (ttr) {
1744 kfree(tu->tqueue);
1745 tu->queue_size = params.queue_size;
1746 tu->tqueue = ttr;
1747 }
1748 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001749 tr = kmalloc(params.queue_size * sizeof(*tr),
1750 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (tr) {
1752 kfree(tu->queue);
1753 tu->queue_size = params.queue_size;
1754 tu->queue = tr;
1755 }
1756 }
1757 }
1758 tu->qhead = tu->qtail = tu->qused = 0;
1759 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1760 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001761 struct snd_timer_tread tread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 tread.event = SNDRV_TIMER_EVENT_EARLY;
1763 tread.tstamp.tv_sec = 0;
1764 tread.tstamp.tv_nsec = 0;
1765 tread.val = 0;
1766 snd_timer_user_append_to_tqueue(tu, &tread);
1767 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001768 struct snd_timer_read *r = &tu->queue[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 r->resolution = 0;
1770 r->ticks = 0;
1771 tu->qused++;
1772 tu->qtail++;
1773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775 tu->filter = params.filter;
1776 tu->ticks = params.ticks;
1777 err = 0;
1778 _end:
1779 if (copy_to_user(_params, &params, sizeof(params)))
1780 return -EFAULT;
1781 return err;
1782}
1783
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001784static int snd_timer_user_status(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001785 struct snd_timer_status __user *_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001787 struct snd_timer_user *tu;
1788 struct snd_timer_status status;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001791 if (!tu->timeri)
1792 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 memset(&status, 0, sizeof(status));
1794 status.tstamp = tu->tstamp;
1795 status.resolution = snd_timer_resolution(tu->timeri);
1796 status.lost = tu->timeri->lost;
1797 status.overrun = tu->overrun;
1798 spin_lock_irq(&tu->qlock);
1799 status.queue = tu->qused;
1800 spin_unlock_irq(&tu->qlock);
1801 if (copy_to_user(_status, &status, sizeof(status)))
1802 return -EFAULT;
1803 return 0;
1804}
1805
1806static int snd_timer_user_start(struct file *file)
1807{
1808 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001809 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001812 if (!tu->timeri)
1813 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 snd_timer_stop(tu->timeri);
1815 tu->timeri->lost = 0;
1816 tu->last_resolution = 0;
1817 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1818}
1819
1820static int snd_timer_user_stop(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 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1829}
1830
1831static int snd_timer_user_continue(struct file *file)
1832{
1833 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001834 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001837 if (!tu->timeri)
1838 return -EBADFD;
Takashi Iwai1a30ab32016-09-07 15:45:31 +02001839 /* start timer instead of continue if it's not used before */
1840 if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1841 return snd_timer_user_start(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 tu->timeri->lost = 0;
1843 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1844}
1845
Takashi Iwai15790a62005-05-15 15:04:14 +02001846static int snd_timer_user_pause(struct file *file)
1847{
1848 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001849 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001850
Takashi Iwai15790a62005-05-15 15:04:14 +02001851 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001852 if (!tu->timeri)
1853 return -EBADFD;
Jaroslav Kyselad138b442005-05-16 13:51:39 +02001854 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
Takashi Iwai15790a62005-05-15 15:04:14 +02001855}
1856
Takashi Iwai8c50b372005-05-15 15:43:54 +02001857enum {
1858 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1859 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1860 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1861 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1862};
1863
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001864static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1865 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001867 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 void __user *argp = (void __user *)arg;
1869 int __user *p = argp;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 tu = file->private_data;
1872 switch (cmd) {
1873 case SNDRV_TIMER_IOCTL_PVERSION:
1874 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1875 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1876 return snd_timer_user_next_device(argp);
1877 case SNDRV_TIMER_IOCTL_TREAD:
1878 {
1879 int xarg;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001880
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001881 mutex_lock(&tu->tread_sem);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001882 if (tu->timeri) { /* too late */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001883 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return -EBUSY;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001885 }
1886 if (get_user(xarg, p)) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001887 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return -EFAULT;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 tu->tread = xarg ? 1 : 0;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001891 mutex_unlock(&tu->tread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return 0;
1893 }
1894 case SNDRV_TIMER_IOCTL_GINFO:
1895 return snd_timer_user_ginfo(file, argp);
1896 case SNDRV_TIMER_IOCTL_GPARAMS:
1897 return snd_timer_user_gparams(file, argp);
1898 case SNDRV_TIMER_IOCTL_GSTATUS:
1899 return snd_timer_user_gstatus(file, argp);
1900 case SNDRV_TIMER_IOCTL_SELECT:
1901 return snd_timer_user_tselect(file, argp);
1902 case SNDRV_TIMER_IOCTL_INFO:
1903 return snd_timer_user_info(file, argp);
1904 case SNDRV_TIMER_IOCTL_PARAMS:
1905 return snd_timer_user_params(file, argp);
1906 case SNDRV_TIMER_IOCTL_STATUS:
1907 return snd_timer_user_status(file, argp);
1908 case SNDRV_TIMER_IOCTL_START:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001909 case SNDRV_TIMER_IOCTL_START_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 return snd_timer_user_start(file);
1911 case SNDRV_TIMER_IOCTL_STOP:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001912 case SNDRV_TIMER_IOCTL_STOP_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return snd_timer_user_stop(file);
1914 case SNDRV_TIMER_IOCTL_CONTINUE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001915 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return snd_timer_user_continue(file);
Takashi Iwai15790a62005-05-15 15:04:14 +02001917 case SNDRV_TIMER_IOCTL_PAUSE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001918 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
Takashi Iwai15790a62005-05-15 15:04:14 +02001919 return snd_timer_user_pause(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921 return -ENOTTY;
1922}
1923
1924static int snd_timer_user_fasync(int fd, struct file * file, int on)
1925{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001926 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 tu = file->private_data;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001929 return fasync_helper(fd, file, on, &tu->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001932static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1933 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001935 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 long result = 0, unit;
Takashi Iwai0f97e402016-02-08 17:26:58 +01001937 int qhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 tu = file->private_data;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001941 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 spin_lock_irq(&tu->qlock);
1943 while ((long)count - result >= unit) {
1944 while (!tu->qused) {
1945 wait_queue_t wait;
1946
1947 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1948 err = -EAGAIN;
Takashi Iwai0f97e402016-02-08 17:26:58 +01001949 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951
1952 set_current_state(TASK_INTERRUPTIBLE);
1953 init_waitqueue_entry(&wait, current);
1954 add_wait_queue(&tu->qchange_sleep, &wait);
1955
1956 spin_unlock_irq(&tu->qlock);
1957 schedule();
1958 spin_lock_irq(&tu->qlock);
1959
1960 remove_wait_queue(&tu->qchange_sleep, &wait);
1961
Takashi Iwai8d155be2016-01-21 17:19:31 +01001962 if (tu->disconnected) {
1963 err = -ENODEV;
Takashi Iwai0f97e402016-02-08 17:26:58 +01001964 goto _error;
Takashi Iwai8d155be2016-01-21 17:19:31 +01001965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 if (signal_pending(current)) {
1967 err = -ERESTARTSYS;
Takashi Iwai0f97e402016-02-08 17:26:58 +01001968 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
1970 }
1971
Takashi Iwai0f97e402016-02-08 17:26:58 +01001972 qhead = tu->qhead++;
1973 tu->qhead %= tu->queue_size;
Takashi Iwaia851e562016-07-04 14:02:15 +02001974 tu->qused--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 spin_unlock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 if (tu->tread) {
Takashi Iwai0f97e402016-02-08 17:26:58 +01001978 if (copy_to_user(buffer, &tu->tqueue[qhead],
1979 sizeof(struct snd_timer_tread)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 } else {
Takashi Iwai0f97e402016-02-08 17:26:58 +01001982 if (copy_to_user(buffer, &tu->queue[qhead],
1983 sizeof(struct snd_timer_read)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
1986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 spin_lock_irq(&tu->qlock);
Takashi Iwai0f97e402016-02-08 17:26:58 +01001988 if (err < 0)
1989 goto _error;
1990 result += unit;
1991 buffer += unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 _error:
Takashi Iwai0f97e402016-02-08 17:26:58 +01001994 spin_unlock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return result > 0 ? result : err;
1996}
1997
1998static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
1999{
2000 unsigned int mask;
Takashi Iwai53d2f742005-11-17 13:56:05 +01002001 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 tu = file->private_data;
2004
2005 poll_wait(file, &tu->qchange_sleep, wait);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 mask = 0;
2008 if (tu->qused)
2009 mask |= POLLIN | POLLRDNORM;
Takashi Iwai8d155be2016-01-21 17:19:31 +01002010 if (tu->disconnected)
2011 mask |= POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 return mask;
2014}
2015
2016#ifdef CONFIG_COMPAT
2017#include "timer_compat.c"
2018#else
2019#define snd_timer_user_ioctl_compat NULL
2020#endif
2021
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08002022static const struct file_operations snd_timer_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
2024 .owner = THIS_MODULE,
2025 .read = snd_timer_user_read,
2026 .open = snd_timer_user_open,
2027 .release = snd_timer_user_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02002028 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 .poll = snd_timer_user_poll,
2030 .unlocked_ioctl = snd_timer_user_ioctl,
2031 .compat_ioctl = snd_timer_user_ioctl_compat,
2032 .fasync = snd_timer_user_fasync,
2033};
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035/*
2036 * ENTRY functions
2037 */
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039static int __init alsa_timer_init(void)
2040{
2041 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043#ifdef SNDRV_OSS_INFO_DEV_TIMERS
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002044 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2045 "system timer");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046#endif
Takashi Iwaie28563c2005-12-01 10:42:42 +01002047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if ((err = snd_timer_register_system()) < 0)
Takashi Iwaicf74dcf2014-02-04 18:22:39 +01002049 pr_err("ALSA: unable to register system timer (%i)\n", err);
Clemens Ladischf87135f2005-11-20 14:06:59 +01002050 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2051 &snd_timer_f_ops, NULL, "timer")) < 0)
Takashi Iwaicf74dcf2014-02-04 18:22:39 +01002052 pr_err("ALSA: unable to register timer device (%i)\n", err);
Takashi Iwaie28563c2005-12-01 10:42:42 +01002053 snd_timer_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return 0;
2055}
2056
2057static void __exit alsa_timer_exit(void)
2058{
2059 struct list_head *p, *n;
2060
2061 snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
2062 /* unregister the system timer */
2063 list_for_each_safe(p, n, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01002064 struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
Takashi Iwaic4614822006-06-23 14:38:23 +02002065 snd_timer_free(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
Takashi Iwaie28563c2005-12-01 10:42:42 +01002067 snd_timer_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068#ifdef SNDRV_OSS_INFO_DEV_TIMERS
2069 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2070#endif
2071}
2072
2073module_init(alsa_timer_init)
2074module_exit(alsa_timer_exit)
2075
2076EXPORT_SYMBOL(snd_timer_open);
2077EXPORT_SYMBOL(snd_timer_close);
2078EXPORT_SYMBOL(snd_timer_resolution);
2079EXPORT_SYMBOL(snd_timer_start);
2080EXPORT_SYMBOL(snd_timer_stop);
2081EXPORT_SYMBOL(snd_timer_continue);
2082EXPORT_SYMBOL(snd_timer_pause);
2083EXPORT_SYMBOL(snd_timer_new);
2084EXPORT_SYMBOL(snd_timer_notify);
2085EXPORT_SYMBOL(snd_timer_global_new);
2086EXPORT_SYMBOL(snd_timer_global_free);
2087EXPORT_SYMBOL(snd_timer_global_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088EXPORT_SYMBOL(snd_timer_interrupt);