blob: cfcc6718aafce099f93c8c32676175cb366e8ab2 [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 Iwai9f8a7652016-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
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#else
44#define DEFAULT_TIMER_LIMIT 1
45#endif
46
47static int timer_limit = DEFAULT_TIMER_LIMIT;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +010048static int timer_tstamp_monotonic = 1;
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020049MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070050MODULE_DESCRIPTION("ALSA timer interface");
51MODULE_LICENSE("GPL");
52module_param(timer_limit, int, 0444);
53MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
Jaroslav Kyselab751eef2007-12-13 10:19:42 +010054module_param(timer_tstamp_monotonic, int, 0444);
55MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Kay Sievers03cfe6f2010-11-23 17:43:19 +010057MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
58MODULE_ALIAS("devname:snd/timer");
59
Takashi Iwai53d2f742005-11-17 13:56:05 +010060struct snd_timer_user {
61 struct snd_timer_instance *timeri;
Clemens Ladisch6b172a82005-10-12 17:20:21 +020062 int tread; /* enhanced read with timestamps and events */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 unsigned long ticks;
64 unsigned long overrun;
65 int qhead;
66 int qtail;
67 int qused;
68 int queue_size;
Takashi Iwai230323d2016-01-21 17:19:31 +010069 bool disconnected;
Takashi Iwai53d2f742005-11-17 13:56:05 +010070 struct snd_timer_read *queue;
71 struct snd_timer_tread *tqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 spinlock_t qlock;
73 unsigned long last_resolution;
74 unsigned int filter;
75 struct timespec tstamp; /* trigger tstamp */
76 wait_queue_head_t qchange_sleep;
77 struct fasync_struct *fasync;
Takashi Iwaiaf368022016-01-13 17:48:01 +010078 struct mutex ioctl_lock;
Takashi Iwai53d2f742005-11-17 13:56:05 +010079};
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/* list of timers */
82static LIST_HEAD(snd_timer_list);
83
84/* list of slave instances */
85static LIST_HEAD(snd_timer_slave_list);
86
87/* lock for slave active lists */
88static DEFINE_SPINLOCK(slave_active_lock);
89
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010090static DEFINE_MUTEX(register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Takashi Iwai53d2f742005-11-17 13:56:05 +010092static int snd_timer_free(struct snd_timer *timer);
93static int snd_timer_dev_free(struct snd_device *device);
94static int snd_timer_dev_register(struct snd_device *device);
Takashi Iwaic4614822006-06-23 14:38:23 +020095static int snd_timer_dev_disconnect(struct snd_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Takashi Iwai53d2f742005-11-17 13:56:05 +010097static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/*
100 * create a timer instance with the given owner string.
101 * when timer is not NULL, increments the module counter
102 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100103static struct snd_timer_instance *snd_timer_instance_new(char *owner,
104 struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100106 struct snd_timer_instance *timeri;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200107 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (timeri == NULL)
109 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700110 timeri->owner = kstrdup(owner, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (! timeri->owner) {
112 kfree(timeri);
113 return NULL;
114 }
115 INIT_LIST_HEAD(&timeri->open_list);
116 INIT_LIST_HEAD(&timeri->active_list);
117 INIT_LIST_HEAD(&timeri->ack_list);
118 INIT_LIST_HEAD(&timeri->slave_list_head);
119 INIT_LIST_HEAD(&timeri->slave_active_head);
120
121 timeri->timer = timer;
Clemens Ladischde242142005-10-12 17:12:31 +0200122 if (timer && !try_module_get(timer->module)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 kfree(timeri->owner);
124 kfree(timeri);
125 return NULL;
126 }
127
128 return timeri;
129}
130
131/*
132 * find a timer instance from the given timer id
133 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100134static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100136 struct snd_timer *timer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Johannes Berg9244b2c2006-10-05 16:02:22 +0200138 list_for_each_entry(timer, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (timer->tmr_class != tid->dev_class)
140 continue;
141 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
142 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
143 (timer->card == NULL ||
144 timer->card->number != tid->card))
145 continue;
146 if (timer->tmr_device != tid->device)
147 continue;
148 if (timer->tmr_subdevice != tid->subdevice)
149 continue;
150 return timer;
151 }
152 return NULL;
153}
154
Johannes Bergee2da992008-07-09 10:28:41 +0200155#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Takashi Iwai53d2f742005-11-17 13:56:05 +0100157static void snd_timer_request(struct snd_timer_id *tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 switch (tid->dev_class) {
160 case SNDRV_TIMER_CLASS_GLOBAL:
161 if (tid->device < timer_limit)
162 request_module("snd-timer-%i", tid->device);
163 break;
164 case SNDRV_TIMER_CLASS_CARD:
165 case SNDRV_TIMER_CLASS_PCM:
166 if (tid->card < snd_ecards_limit)
167 request_module("snd-card-%i", tid->card);
168 break;
169 default:
170 break;
171 }
172}
173
174#endif
175
176/*
177 * look for a master instance matching with the slave id of the given slave.
178 * when found, relink the open_link of the slave.
179 *
180 * call this with register_mutex down.
181 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100182static void snd_timer_check_slave(struct snd_timer_instance *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100184 struct snd_timer *timer;
185 struct snd_timer_instance *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 /* FIXME: it's really dumb to look up all entries.. */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200188 list_for_each_entry(timer, &snd_timer_list, device_list) {
189 list_for_each_entry(master, &timer->open_list_head, open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (slave->slave_class == master->slave_class &&
191 slave->slave_id == master->slave_id) {
Nicolas Kaiser5b7c7572011-03-16 17:10:11 +0100192 list_move_tail(&slave->open_list,
193 &master->slave_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 spin_lock_irq(&slave_active_lock);
195 slave->master = master;
196 slave->timer = master->timer;
197 spin_unlock_irq(&slave_active_lock);
198 return;
199 }
200 }
201 }
202}
203
204/*
205 * look for slave instances matching with the slave id of the given master.
206 * when found, relink the open_link of slaves.
207 *
208 * call this with register_mutex down.
209 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100210static void snd_timer_check_master(struct snd_timer_instance *master)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200212 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* check all pending slaves */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200215 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (slave->slave_class == master->slave_class &&
217 slave->slave_id == master->slave_id) {
Johannes Berg9244b2c2006-10-05 16:02:22 +0200218 list_move_tail(&slave->open_list, &master->slave_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 spin_lock_irq(&slave_active_lock);
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100220 spin_lock(&master->timer->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 slave->master = master;
222 slave->timer = master->timer;
223 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200224 list_add_tail(&slave->active_list,
225 &master->slave_active_head);
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100226 spin_unlock(&master->timer->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 spin_unlock_irq(&slave_active_lock);
228 }
229 }
230}
231
232/*
233 * open a timer instance
234 * when opening a master, the slave id must be here given.
235 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100236int snd_timer_open(struct snd_timer_instance **ti,
237 char *owner, struct snd_timer_id *tid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 unsigned int slave_id)
239{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100240 struct snd_timer *timer;
241 struct snd_timer_instance *timeri = NULL;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
244 /* open a slave instance */
245 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
246 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100247 pr_debug("ALSA: timer: invalid slave class %i\n",
248 tid->dev_sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return -EINVAL;
250 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100251 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 timeri = snd_timer_instance_new(owner, NULL);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200253 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100254 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200255 return -ENOMEM;
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 timeri->slave_class = tid->dev_sclass;
258 timeri->slave_id = tid->device;
259 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
260 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
261 snd_timer_check_slave(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100262 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 *ti = timeri;
264 return 0;
265 }
266
267 /* open a master instance */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100268 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 timer = snd_timer_find(tid);
Johannes Bergee2da992008-07-09 10:28:41 +0200270#ifdef CONFIG_MODULES
271 if (!timer) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100272 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 snd_timer_request(tid);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100274 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 timer = snd_timer_find(tid);
276 }
277#endif
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200278 if (!timer) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100279 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return -ENODEV;
281 }
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200282 if (!list_empty(&timer->open_list_head)) {
283 timeri = list_entry(timer->open_list_head.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +0100284 struct snd_timer_instance, open_list);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200285 if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100286 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200287 return -EBUSY;
288 }
289 }
290 timeri = snd_timer_instance_new(owner, timer);
291 if (!timeri) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100292 mutex_unlock(&register_mutex);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200293 return -ENOMEM;
294 }
Takashi Iwai230323d2016-01-21 17:19:31 +0100295 /* take a card refcount for safe disconnection */
296 if (timer->card)
297 get_device(&timer->card->card_dev);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200298 timeri->slave_class = tid->dev_sclass;
299 timeri->slave_id = slave_id;
Vegard Nossum8ddc0562016-08-29 00:33:51 +0200300
301 if (list_empty(&timer->open_list_head) && timer->hw.open) {
302 int err = timer->hw.open(timer);
303 if (err) {
304 kfree(timeri->owner);
305 kfree(timeri);
306
307 if (timer->card)
308 put_device(&timer->card->card_dev);
309 module_put(timer->module);
310 mutex_unlock(&register_mutex);
311 return err;
312 }
313 }
314
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200315 list_add_tail(&timeri->open_list, &timer->open_list_head);
316 snd_timer_check_master(timeri);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100317 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 *ti = timeri;
319 return 0;
320}
Takashi Iwai444dac22017-06-16 16:16:05 +0200321EXPORT_SYMBOL(snd_timer_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323/*
324 * close a timer instance
325 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100326int snd_timer_close(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100328 struct snd_timer *timer = NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200329 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Takashi Iwai00728892008-08-14 07:51:57 +0200331 if (snd_BUG_ON(!timeri))
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200332 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100334 mutex_lock(&register_mutex);
335 list_del(&timeri->open_list);
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /* force to stop the timer */
338 snd_timer_stop(timeri);
339
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100340 timer = timeri->timer;
341 if (timer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* wait, until the active callback is finished */
343 spin_lock_irq(&timer->lock);
344 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
345 spin_unlock_irq(&timer->lock);
346 udelay(10);
347 spin_lock_irq(&timer->lock);
348 }
349 spin_unlock_irq(&timer->lock);
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /* remove slave links */
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100352 spin_lock_irq(&slave_active_lock);
353 spin_lock(&timer->lock);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200354 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
355 open_list) {
Johannes Berg9244b2c2006-10-05 16:02:22 +0200356 list_move_tail(&slave->open_list, &snd_timer_slave_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 slave->master = NULL;
358 slave->timer = NULL;
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100359 list_del_init(&slave->ack_list);
360 list_del_init(&slave->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100362 spin_unlock(&timer->lock);
363 spin_unlock_irq(&slave_active_lock);
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100364
365 /* slave doesn't need to release timer resources below */
366 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
367 timer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (timeri->private_free)
371 timeri->private_free(timeri);
372 kfree(timeri->owner);
373 kfree(timeri);
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100374
375 if (timer) {
376 if (list_empty(&timer->open_list_head) && timer->hw.close)
377 timer->hw.close(timer);
378 /* release a card refcount for safe disconnection */
379 if (timer->card)
380 put_device(&timer->card->card_dev);
Clemens Ladischde242142005-10-12 17:12:31 +0200381 module_put(timer->module);
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100382 }
383
384 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return 0;
386}
Takashi Iwai444dac22017-06-16 16:16:05 +0200387EXPORT_SYMBOL(snd_timer_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Takashi Iwai53d2f742005-11-17 13:56:05 +0100389unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100391 struct snd_timer * timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 if (timeri == NULL)
394 return 0;
395 if ((timer = timeri->timer) != NULL) {
396 if (timer->hw.c_resolution)
397 return timer->hw.c_resolution(timer);
398 return timer->hw.resolution;
399 }
400 return 0;
401}
Takashi Iwai444dac22017-06-16 16:16:05 +0200402EXPORT_SYMBOL(snd_timer_resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Takashi Iwai53d2f742005-11-17 13:56:05 +0100404static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100406 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100408 struct snd_timer_instance *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct timespec tstamp;
410
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100411 if (timer_tstamp_monotonic)
Thomas Gleixner26204e02014-06-11 23:59:14 +0000412 ktime_get_ts(&tstamp);
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100413 else
414 getnstimeofday(&tstamp);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200415 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
416 event > SNDRV_TIMER_EVENT_PAUSE))
417 return;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200418 if (event == SNDRV_TIMER_EVENT_START ||
419 event == SNDRV_TIMER_EVENT_CONTINUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 resolution = snd_timer_resolution(ti);
421 if (ti->ccallback)
Jaroslav Kyselab30477d2010-03-03 11:05:55 +0100422 ti->ccallback(ti, event, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
424 return;
425 timer = ti->timer;
426 if (timer == NULL)
427 return;
428 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
429 return;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200430 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (ts->ccallback)
Takashi Iwai117159f2016-02-08 17:36:25 +0100432 ts->ccallback(ts, event + 100, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100435/* start/continue a master timer */
436static int snd_timer_start1(struct snd_timer_instance *timeri,
437 bool start, unsigned long ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100439 struct snd_timer *timer;
440 int result;
441 unsigned long flags;
442
443 timer = timeri->timer;
444 if (!timer)
445 return -EINVAL;
446
447 spin_lock_irqsave(&timer->lock, flags);
448 if (timer->card && timer->card->shutdown) {
449 result = -ENODEV;
450 goto unlock;
451 }
452 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
453 SNDRV_TIMER_IFLG_START)) {
454 result = -EBUSY;
455 goto unlock;
456 }
457
458 if (start)
459 timeri->ticks = timeri->cticks = ticks;
460 else if (!timeri->cticks)
461 timeri->cticks = 1;
462 timeri->pticks = 0;
463
Nicolas Kaiser5b7c7572011-03-16 17:10:11 +0100464 list_move_tail(&timeri->active_list, &timer->active_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (timer->running) {
466 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
467 goto __start_now;
468 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
469 timeri->flags |= SNDRV_TIMER_IFLG_START;
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100470 result = 1; /* delayed start */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 } else {
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100472 if (start)
473 timer->sticks = ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 timer->hw.start(timer);
475 __start_now:
476 timer->running++;
477 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100478 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100480 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
481 SNDRV_TIMER_EVENT_CONTINUE);
482 unlock:
483 spin_unlock_irqrestore(&timer->lock, flags);
484 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100487/* start/continue a slave timer */
488static int snd_timer_start_slave(struct snd_timer_instance *timeri,
489 bool start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 unsigned long flags;
492
493 spin_lock_irqsave(&slave_active_lock, flags);
Takashi Iwaif784beb2016-01-30 23:09:08 +0100494 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
495 spin_unlock_irqrestore(&slave_active_lock, flags);
496 return -EBUSY;
497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100499 if (timeri->master && timeri->timer) {
500 spin_lock(&timeri->timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200501 list_add_tail(&timeri->active_list,
502 &timeri->master->slave_active_head);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100503 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
504 SNDRV_TIMER_EVENT_CONTINUE);
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100505 spin_unlock(&timeri->timer->lock);
506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 spin_unlock_irqrestore(&slave_active_lock, flags);
508 return 1; /* delayed start */
509}
510
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100511/* stop/pause a master timer */
512static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100514 struct snd_timer *timer;
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100515 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 unsigned long flags;
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 timer = timeri->timer;
519 if (!timer)
520 return -EINVAL;
521 spin_lock_irqsave(&timer->lock, flags);
Takashi Iwaif784beb2016-01-30 23:09:08 +0100522 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
523 SNDRV_TIMER_IFLG_START))) {
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100524 result = -EBUSY;
525 goto unlock;
Takashi Iwaif784beb2016-01-30 23:09:08 +0100526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 list_del_init(&timeri->ack_list);
528 list_del_init(&timeri->active_list);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100529 if (timer->card && timer->card->shutdown)
530 goto unlock;
531 if (stop) {
532 timeri->cticks = timeri->ticks;
533 timeri->pticks = 0;
Takashi Iwai230323d2016-01-21 17:19:31 +0100534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
536 !(--timer->running)) {
537 timer->hw.stop(timer);
538 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
539 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
540 snd_timer_reschedule(timer, 0);
541 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
542 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
543 timer->hw.start(timer);
544 }
545 }
546 }
Takashi Iwaic3b16812016-01-14 17:01:46 +0100547 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
Takashi Iwai9f8a7652016-09-07 15:45:31 +0200548 if (stop)
549 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
550 else
551 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100552 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
Ben Hutchings416808f2018-05-17 22:34:39 +0100553 SNDRV_TIMER_EVENT_PAUSE);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100554 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 spin_unlock_irqrestore(&timer->lock, flags);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100556 return result;
557}
558
559/* stop/pause a slave timer */
560static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
561{
562 unsigned long flags;
563
564 spin_lock_irqsave(&slave_active_lock, flags);
565 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
566 spin_unlock_irqrestore(&slave_active_lock, flags);
567 return -EBUSY;
568 }
569 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
570 if (timeri->timer) {
571 spin_lock(&timeri->timer->lock);
572 list_del_init(&timeri->ack_list);
573 list_del_init(&timeri->active_list);
574 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
Ben Hutchings416808f2018-05-17 22:34:39 +0100575 SNDRV_TIMER_EVENT_PAUSE);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100576 spin_unlock(&timeri->timer->lock);
577 }
578 spin_unlock_irqrestore(&slave_active_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 0;
580}
581
582/*
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100583 * start the timer instance
584 */
585int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
586{
587 if (timeri == NULL || ticks < 1)
588 return -EINVAL;
589 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
590 return snd_timer_start_slave(timeri, true);
591 else
592 return snd_timer_start1(timeri, true, ticks);
593}
Takashi Iwai444dac22017-06-16 16:16:05 +0200594EXPORT_SYMBOL(snd_timer_start);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100595
596/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 * stop the timer instance.
598 *
599 * do not call this from the timer callback!
600 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100601int snd_timer_stop(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100603 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
604 return snd_timer_stop_slave(timeri, true);
605 else
606 return snd_timer_stop1(timeri, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
Takashi Iwai444dac22017-06-16 16:16:05 +0200608EXPORT_SYMBOL(snd_timer_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610/*
611 * start again.. the tick is kept.
612 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100613int snd_timer_continue(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Takashi Iwai9f8a7652016-09-07 15:45:31 +0200615 /* timer can continue only after pause */
616 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
617 return -EINVAL;
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100620 return snd_timer_start_slave(timeri, false);
621 else
622 return snd_timer_start1(timeri, false, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
Takashi Iwai444dac22017-06-16 16:16:05 +0200624EXPORT_SYMBOL(snd_timer_continue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626/*
627 * pause.. remember the ticks left
628 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100629int snd_timer_pause(struct snd_timer_instance * timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100631 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
632 return snd_timer_stop_slave(timeri, false);
633 else
634 return snd_timer_stop1(timeri, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
Takashi Iwai444dac22017-06-16 16:16:05 +0200636EXPORT_SYMBOL(snd_timer_pause);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638/*
639 * reschedule the timer
640 *
641 * start pending instances and check the scheduling ticks.
642 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
643 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100644static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100646 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 unsigned long ticks = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Johannes Berg9244b2c2006-10-05 16:02:22 +0200649 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (ti->flags & SNDRV_TIMER_IFLG_START) {
651 ti->flags &= ~SNDRV_TIMER_IFLG_START;
652 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
653 timer->running++;
654 }
655 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
656 if (ticks > ti->cticks)
657 ticks = ti->cticks;
658 }
659 }
660 if (ticks == ~0UL) {
661 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
662 return;
663 }
664 if (ticks > timer->hw.ticks)
665 ticks = timer->hw.ticks;
666 if (ticks_left != ticks)
667 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
668 timer->sticks = ticks;
669}
670
671/*
672 * timer tasklet
673 *
674 */
675static void snd_timer_tasklet(unsigned long arg)
676{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100677 struct snd_timer *timer = (struct snd_timer *) arg;
678 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 struct list_head *p;
680 unsigned long resolution, ticks;
Takashi Iwai2999ff52006-07-05 17:16:58 +0200681 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Takashi Iwai230323d2016-01-21 17:19:31 +0100683 if (timer->card && timer->card->shutdown)
684 return;
685
Takashi Iwai2999ff52006-07-05 17:16:58 +0200686 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* now process all callbacks */
688 while (!list_empty(&timer->sack_list_head)) {
689 p = timer->sack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100690 ti = list_entry(p, struct snd_timer_instance, ack_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 /* remove from ack_list and make empty */
693 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 ticks = ti->pticks;
696 ti->pticks = 0;
697 resolution = ti->resolution;
698
699 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
700 spin_unlock(&timer->lock);
701 if (ti->callback)
702 ti->callback(ti, resolution, ticks);
703 spin_lock(&timer->lock);
704 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
705 }
Takashi Iwai2999ff52006-07-05 17:16:58 +0200706 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709/*
710 * timer interrupt
711 *
712 * ticks_left is usually equal to timer->sticks.
713 *
714 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100715void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200717 struct snd_timer_instance *ti, *ts, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 unsigned long resolution, ticks;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200719 struct list_head *p, *ack_list_head;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100720 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 int use_tasklet = 0;
722
723 if (timer == NULL)
724 return;
725
Takashi Iwai230323d2016-01-21 17:19:31 +0100726 if (timer->card && timer->card->shutdown)
727 return;
728
Takashi Iwaib32425a2005-11-18 18:52:14 +0100729 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 /* remember the current resolution */
732 if (timer->hw.c_resolution)
733 resolution = timer->hw.c_resolution(timer);
734 else
735 resolution = timer->hw.resolution;
736
737 /* loop for all active instances
Johannes Berg9244b2c2006-10-05 16:02:22 +0200738 * Here we cannot use list_for_each_entry because the active_list of a
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200739 * processed instance is relinked to done_list_head before the callback
740 * is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200742 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
743 active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
745 continue;
746 ti->pticks += ticks_left;
747 ti->resolution = resolution;
748 if (ti->cticks < ticks_left)
749 ti->cticks = 0;
750 else
751 ti->cticks -= ticks_left;
752 if (ti->cticks) /* not expired */
753 continue;
754 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
755 ti->cticks = ti->ticks;
756 } else {
757 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwai094fd3b2016-02-04 17:06:13 +0100758 --timer->running;
759 list_del_init(&ti->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200761 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
762 (ti->flags & SNDRV_TIMER_IFLG_FAST))
763 ack_list_head = &timer->ack_list_head;
764 else
765 ack_list_head = &timer->sack_list_head;
766 if (list_empty(&ti->ack_list))
767 list_add_tail(&ti->ack_list, ack_list_head);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200768 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 ts->pticks = ti->pticks;
770 ts->resolution = resolution;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200771 if (list_empty(&ts->ack_list))
772 list_add_tail(&ts->ack_list, ack_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774 }
775 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
Clemens Ladischcd93fe42006-07-17 16:53:57 +0200776 snd_timer_reschedule(timer, timer->sticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (timer->running) {
778 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
779 timer->hw.stop(timer);
780 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
781 }
782 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
783 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
784 /* restart timer */
785 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
786 timer->hw.start(timer);
787 }
788 } else {
789 timer->hw.stop(timer);
790 }
791
792 /* now process all fast callbacks */
793 while (!list_empty(&timer->ack_list_head)) {
794 p = timer->ack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100795 ti = list_entry(p, struct snd_timer_instance, ack_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /* remove from ack_list and make empty */
798 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 ticks = ti->pticks;
801 ti->pticks = 0;
802
803 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
804 spin_unlock(&timer->lock);
805 if (ti->callback)
806 ti->callback(ti, resolution, ticks);
807 spin_lock(&timer->lock);
808 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
809 }
810
811 /* do we have any slow callbacks? */
812 use_tasklet = !list_empty(&timer->sack_list_head);
Takashi Iwaib32425a2005-11-18 18:52:14 +0100813 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 if (use_tasklet)
Takashi Iwai1f041282008-12-18 12:17:55 +0100816 tasklet_schedule(&timer->task_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
Takashi Iwai444dac22017-06-16 16:16:05 +0200818EXPORT_SYMBOL(snd_timer_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
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 Iwaiec0e9932015-03-10 15:42:14 +0100840 if (!timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return -ENOMEM;
842 timer->tmr_class = tid->dev_class;
843 timer->card = card;
844 timer->tmr_device = tid->device;
845 timer->tmr_subdevice = tid->subdevice;
846 if (id)
847 strlcpy(timer->id, id, sizeof(timer->id));
Vegard Nossum6b760bb2016-08-29 00:33:50 +0200848 timer->sticks = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 INIT_LIST_HEAD(&timer->device_list);
850 INIT_LIST_HEAD(&timer->open_list_head);
851 INIT_LIST_HEAD(&timer->active_list_head);
852 INIT_LIST_HEAD(&timer->ack_list_head);
853 INIT_LIST_HEAD(&timer->sack_list_head);
854 spin_lock_init(&timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200855 tasklet_init(&timer->task_queue, snd_timer_tasklet,
856 (unsigned long)timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (card != NULL) {
Clemens Ladischde242142005-10-12 17:12:31 +0200858 timer->module = card->module;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200859 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
860 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 snd_timer_free(timer);
862 return err;
863 }
864 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200865 if (rtimer)
866 *rtimer = timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return 0;
868}
Takashi Iwai444dac22017-06-16 16:16:05 +0200869EXPORT_SYMBOL(snd_timer_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Takashi Iwai53d2f742005-11-17 13:56:05 +0100871static int snd_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200873 if (!timer)
874 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +0200875
876 mutex_lock(&register_mutex);
877 if (! list_empty(&timer->open_list_head)) {
878 struct list_head *p, *n;
879 struct snd_timer_instance *ti;
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100880 pr_warn("ALSA: timer %p is busy?\n", timer);
Takashi Iwaic4614822006-06-23 14:38:23 +0200881 list_for_each_safe(p, n, &timer->open_list_head) {
882 list_del_init(p);
883 ti = list_entry(p, struct snd_timer_instance, open_list);
884 ti->timer = NULL;
885 }
886 }
887 list_del(&timer->device_list);
888 mutex_unlock(&register_mutex);
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (timer->private_free)
891 timer->private_free(timer);
892 kfree(timer);
893 return 0;
894}
895
Takashi Iwai53d2f742005-11-17 13:56:05 +0100896static int snd_timer_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100898 struct snd_timer *timer = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return snd_timer_free(timer);
900}
901
Takashi Iwai53d2f742005-11-17 13:56:05 +0100902static int snd_timer_dev_register(struct snd_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100904 struct snd_timer *timer = dev->device_data;
905 struct snd_timer *timer1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200907 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
908 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
910 !timer->hw.resolution && timer->hw.c_resolution == NULL)
911 return -EINVAL;
912
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100913 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200914 list_for_each_entry(timer1, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (timer1->tmr_class > timer->tmr_class)
916 break;
917 if (timer1->tmr_class < timer->tmr_class)
918 continue;
919 if (timer1->card && timer->card) {
920 if (timer1->card->number > timer->card->number)
921 break;
922 if (timer1->card->number < timer->card->number)
923 continue;
924 }
925 if (timer1->tmr_device > timer->tmr_device)
926 break;
927 if (timer1->tmr_device < timer->tmr_device)
928 continue;
929 if (timer1->tmr_subdevice > timer->tmr_subdevice)
930 break;
931 if (timer1->tmr_subdevice < timer->tmr_subdevice)
932 continue;
933 /* conflicts.. */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100934 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return -EBUSY;
936 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200937 list_add_tail(&timer->device_list, &timer1->device_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100938 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return 0;
940}
941
Takashi Iwaic4614822006-06-23 14:38:23 +0200942static int snd_timer_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100944 struct snd_timer *timer = device->device_data;
Takashi Iwai230323d2016-01-21 17:19:31 +0100945 struct snd_timer_instance *ti;
946
Takashi Iwaic4614822006-06-23 14:38:23 +0200947 mutex_lock(&register_mutex);
948 list_del_init(&timer->device_list);
Takashi Iwai230323d2016-01-21 17:19:31 +0100949 /* wake up pending sleepers */
950 list_for_each_entry(ti, &timer->open_list_head, open_list) {
Takashi Iwai40ed94442016-01-21 17:43:08 +0100951 if (ti->disconnect)
952 ti->disconnect(ti);
Takashi Iwai230323d2016-01-21 17:19:31 +0100953 }
Takashi Iwaic4614822006-06-23 14:38:23 +0200954 mutex_unlock(&register_mutex);
955 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Takashi Iwai53d2f742005-11-17 13:56:05 +0100958void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 unsigned long flags;
961 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100962 struct snd_timer_instance *ti, *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Takashi Iwai230323d2016-01-21 17:19:31 +0100964 if (timer->card && timer->card->shutdown)
965 return;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200966 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
967 return;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200968 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
969 event > SNDRV_TIMER_EVENT_MRESUME))
970 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 spin_lock_irqsave(&timer->lock, flags);
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +0200972 if (event == SNDRV_TIMER_EVENT_MSTART ||
973 event == SNDRV_TIMER_EVENT_MCONTINUE ||
974 event == SNDRV_TIMER_EVENT_MRESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (timer->hw.c_resolution)
976 resolution = timer->hw.c_resolution(timer);
977 else
978 resolution = timer->hw.resolution;
979 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200980 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (ti->ccallback)
982 ti->ccallback(ti, event, tstamp, resolution);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200983 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (ts->ccallback)
985 ts->ccallback(ts, event, tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987 spin_unlock_irqrestore(&timer->lock, flags);
988}
Takashi Iwai444dac22017-06-16 16:16:05 +0200989EXPORT_SYMBOL(snd_timer_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991/*
992 * exported functions for global timers
993 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100994int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100996 struct snd_timer_id tid;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
999 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1000 tid.card = -1;
1001 tid.device = device;
1002 tid.subdevice = 0;
1003 return snd_timer_new(NULL, id, &tid, rtimer);
1004}
Takashi Iwai444dac22017-06-16 16:16:05 +02001005EXPORT_SYMBOL(snd_timer_global_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Takashi Iwai53d2f742005-11-17 13:56:05 +01001007int snd_timer_global_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 return snd_timer_free(timer);
1010}
Takashi Iwai444dac22017-06-16 16:16:05 +02001011EXPORT_SYMBOL(snd_timer_global_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Takashi Iwai53d2f742005-11-17 13:56:05 +01001013int snd_timer_global_register(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001015 struct snd_device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 memset(&dev, 0, sizeof(dev));
1018 dev.device_data = timer;
1019 return snd_timer_dev_register(&dev);
1020}
Takashi Iwai444dac22017-06-16 16:16:05 +02001021EXPORT_SYMBOL(snd_timer_global_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001023/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 * System timer
1025 */
1026
1027struct snd_timer_system_private {
1028 struct timer_list tlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 unsigned long last_expires;
1030 unsigned long last_jiffies;
1031 unsigned long correction;
1032};
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034static void snd_timer_s_function(unsigned long data)
1035{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001036 struct snd_timer *timer = (struct snd_timer *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 struct snd_timer_system_private *priv = timer->private_data;
1038 unsigned long jiff = jiffies;
1039 if (time_after(jiff, priv->last_expires))
Clemens Ladisch6ed5eff2006-07-17 16:51:37 +02001040 priv->correction += (long)jiff - (long)priv->last_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1042}
1043
Takashi Iwai53d2f742005-11-17 13:56:05 +01001044static int snd_timer_s_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 struct snd_timer_system_private *priv;
1047 unsigned long njiff;
1048
1049 priv = (struct snd_timer_system_private *) timer->private_data;
1050 njiff = (priv->last_jiffies = jiffies);
1051 if (priv->correction > timer->sticks - 1) {
1052 priv->correction -= timer->sticks - 1;
1053 njiff++;
1054 } else {
1055 njiff += timer->sticks - priv->correction;
Clemens Ladisch17f48ec2006-07-17 16:50:56 +02001056 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
Takashi Iwai4a070832016-04-01 12:28:16 +02001058 priv->last_expires = njiff;
1059 mod_timer(&priv->tlist, njiff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return 0;
1061}
1062
Takashi Iwai53d2f742005-11-17 13:56:05 +01001063static int snd_timer_s_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 struct snd_timer_system_private *priv;
1066 unsigned long jiff;
1067
1068 priv = (struct snd_timer_system_private *) timer->private_data;
1069 del_timer(&priv->tlist);
1070 jiff = jiffies;
1071 if (time_before(jiff, priv->last_expires))
1072 timer->sticks = priv->last_expires - jiff;
1073 else
1074 timer->sticks = 1;
Clemens Ladischde2696d2006-07-17 16:52:09 +02001075 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return 0;
1077}
1078
Takashi Iwaif1463572016-02-02 14:14:10 +01001079static int snd_timer_s_close(struct snd_timer *timer)
1080{
1081 struct snd_timer_system_private *priv;
1082
1083 priv = (struct snd_timer_system_private *)timer->private_data;
1084 del_timer_sync(&priv->tlist);
1085 return 0;
1086}
1087
Takashi Iwai53d2f742005-11-17 13:56:05 +01001088static struct snd_timer_hardware snd_timer_system =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1091 .resolution = 1000000000L / HZ,
1092 .ticks = 10000000L,
Takashi Iwaif1463572016-02-02 14:14:10 +01001093 .close = snd_timer_s_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 .start = snd_timer_s_start,
1095 .stop = snd_timer_s_stop
1096};
1097
Takashi Iwai53d2f742005-11-17 13:56:05 +01001098static void snd_timer_free_system(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 kfree(timer->private_data);
1101}
1102
1103static int snd_timer_register_system(void)
1104{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001105 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 struct snd_timer_system_private *priv;
1107 int err;
1108
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001109 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1110 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return err;
1112 strcpy(timer->name, "system timer");
1113 timer->hw = snd_timer_system;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001114 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (priv == NULL) {
1116 snd_timer_free(timer);
1117 return -ENOMEM;
1118 }
Takashi Iwaif169c102015-01-19 11:26:25 +01001119 setup_timer(&priv->tlist, snd_timer_s_function, (unsigned long) timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 timer->private_data = priv;
1121 timer->private_free = snd_timer_free_system;
1122 return snd_timer_global_register(timer);
1123}
1124
Jie Yangcd6a6502015-05-27 19:45:45 +08001125#ifdef CONFIG_SND_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126/*
1127 * Info interface
1128 */
1129
Takashi Iwai53d2f742005-11-17 13:56:05 +01001130static void snd_timer_proc_read(struct snd_info_entry *entry,
1131 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001133 struct snd_timer *timer;
1134 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001136 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001137 list_for_each_entry(timer, &snd_timer_list, device_list) {
Takashi Iwai230323d2016-01-21 17:19:31 +01001138 if (timer->card && timer->card->shutdown)
1139 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 switch (timer->tmr_class) {
1141 case SNDRV_TIMER_CLASS_GLOBAL:
1142 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1143 break;
1144 case SNDRV_TIMER_CLASS_CARD:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001145 snd_iprintf(buffer, "C%i-%i: ",
1146 timer->card->number, timer->tmr_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 break;
1148 case SNDRV_TIMER_CLASS_PCM:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001149 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1150 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 break;
1152 default:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001153 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1154 timer->card ? timer->card->number : -1,
1155 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157 snd_iprintf(buffer, "%s :", timer->name);
1158 if (timer->hw.resolution)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001159 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1160 timer->hw.resolution / 1000,
1161 timer->hw.resolution % 1000,
1162 timer->hw.ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1164 snd_iprintf(buffer, " SLAVE");
1165 snd_iprintf(buffer, "\n");
Johannes Berg9244b2c2006-10-05 16:02:22 +02001166 list_for_each_entry(ti, &timer->open_list_head, open_list)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001167 snd_iprintf(buffer, " Client %s : %s\n",
1168 ti->owner ? ti->owner : "unknown",
1169 ti->flags & (SNDRV_TIMER_IFLG_START |
1170 SNDRV_TIMER_IFLG_RUNNING)
1171 ? "running" : "stopped");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001173 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001176static struct snd_info_entry *snd_timer_proc_entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001177
1178static void __init snd_timer_proc_init(void)
1179{
1180 struct snd_info_entry *entry;
1181
1182 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1183 if (entry != NULL) {
Takashi Iwaie28563c2005-12-01 10:42:42 +01001184 entry->c.text.read = snd_timer_proc_read;
1185 if (snd_info_register(entry) < 0) {
1186 snd_info_free_entry(entry);
1187 entry = NULL;
1188 }
1189 }
1190 snd_timer_proc_entry = entry;
1191}
1192
1193static void __exit snd_timer_proc_done(void)
1194{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001195 snd_info_free_entry(snd_timer_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001196}
Jie Yangcd6a6502015-05-27 19:45:45 +08001197#else /* !CONFIG_SND_PROC_FS */
Takashi Iwaie28563c2005-12-01 10:42:42 +01001198#define snd_timer_proc_init()
1199#define snd_timer_proc_done()
1200#endif
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202/*
1203 * USER SPACE interface
1204 */
1205
Takashi Iwai53d2f742005-11-17 13:56:05 +01001206static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 unsigned long resolution,
1208 unsigned long ticks)
1209{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001210 struct snd_timer_user *tu = timeri->callback_data;
1211 struct snd_timer_read *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 int prev;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 spin_lock(&tu->qlock);
1215 if (tu->qused > 0) {
1216 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1217 r = &tu->queue[prev];
1218 if (r->resolution == resolution) {
1219 r->ticks += ticks;
1220 goto __wake;
1221 }
1222 }
1223 if (tu->qused >= tu->queue_size) {
1224 tu->overrun++;
1225 } else {
1226 r = &tu->queue[tu->qtail++];
1227 tu->qtail %= tu->queue_size;
1228 r->resolution = resolution;
1229 r->ticks = ticks;
1230 tu->qused++;
1231 }
1232 __wake:
1233 spin_unlock(&tu->qlock);
1234 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1235 wake_up(&tu->qchange_sleep);
1236}
1237
Takashi Iwai53d2f742005-11-17 13:56:05 +01001238static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1239 struct snd_timer_tread *tread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 if (tu->qused >= tu->queue_size) {
1242 tu->overrun++;
1243 } else {
1244 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1245 tu->qtail %= tu->queue_size;
1246 tu->qused++;
1247 }
1248}
1249
Takashi Iwai53d2f742005-11-17 13:56:05 +01001250static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1251 int event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 struct timespec *tstamp,
1253 unsigned long resolution)
1254{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001255 struct snd_timer_user *tu = timeri->callback_data;
1256 struct snd_timer_tread r1;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001257 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001259 if (event >= SNDRV_TIMER_EVENT_START &&
1260 event <= SNDRV_TIMER_EVENT_PAUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 tu->tstamp = *tstamp;
1262 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1263 return;
Kangjie Lu9a47e9c2016-05-03 16:44:20 -04001264 memset(&r1, 0, sizeof(r1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 r1.event = event;
1266 r1.tstamp = *tstamp;
1267 r1.val = resolution;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001268 spin_lock_irqsave(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 snd_timer_user_append_to_tqueue(tu, &r1);
Dan Carpenterbfe70782010-04-28 10:29:14 +02001270 spin_unlock_irqrestore(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1272 wake_up(&tu->qchange_sleep);
1273}
1274
Takashi Iwai40ed94442016-01-21 17:43:08 +01001275static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
1276{
1277 struct snd_timer_user *tu = timeri->callback_data;
1278
1279 tu->disconnected = true;
1280 wake_up(&tu->qchange_sleep);
1281}
1282
Takashi Iwai53d2f742005-11-17 13:56:05 +01001283static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 unsigned long resolution,
1285 unsigned long ticks)
1286{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001287 struct snd_timer_user *tu = timeri->callback_data;
1288 struct snd_timer_tread *r, r1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 struct timespec tstamp;
1290 int prev, append = 0;
1291
Takashi Iwai07799e72005-10-10 11:49:49 +02001292 memset(&tstamp, 0, sizeof(tstamp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 spin_lock(&tu->qlock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001294 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1295 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 spin_unlock(&tu->qlock);
1297 return;
1298 }
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001299 if (tu->last_resolution != resolution || ticks > 0) {
1300 if (timer_tstamp_monotonic)
Thomas Gleixner26204e02014-06-11 23:59:14 +00001301 ktime_get_ts(&tstamp);
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001302 else
1303 getnstimeofday(&tstamp);
1304 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001305 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1306 tu->last_resolution != resolution) {
Kangjie Lue4ec8cc2016-05-03 16:44:32 -04001307 memset(&r1, 0, sizeof(r1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1309 r1.tstamp = tstamp;
1310 r1.val = resolution;
1311 snd_timer_user_append_to_tqueue(tu, &r1);
1312 tu->last_resolution = resolution;
1313 append++;
1314 }
1315 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1316 goto __wake;
1317 if (ticks == 0)
1318 goto __wake;
1319 if (tu->qused > 0) {
1320 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1321 r = &tu->tqueue[prev];
1322 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1323 r->tstamp = tstamp;
1324 r->val += ticks;
1325 append++;
1326 goto __wake;
1327 }
1328 }
1329 r1.event = SNDRV_TIMER_EVENT_TICK;
1330 r1.tstamp = tstamp;
1331 r1.val = ticks;
1332 snd_timer_user_append_to_tqueue(tu, &r1);
1333 append++;
1334 __wake:
1335 spin_unlock(&tu->qlock);
1336 if (append == 0)
1337 return;
1338 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1339 wake_up(&tu->qchange_sleep);
1340}
1341
1342static int snd_timer_user_open(struct inode *inode, struct file *file)
1343{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001344 struct snd_timer_user *tu;
Takashi Iwai02f48652010-04-13 11:49:04 +02001345 int err;
1346
1347 err = nonseekable_open(inode, file);
1348 if (err < 0)
1349 return err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001350
Takashi Iwaica2c0962005-09-09 14:20:23 +02001351 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (tu == NULL)
1353 return -ENOMEM;
1354 spin_lock_init(&tu->qlock);
1355 init_waitqueue_head(&tu->qchange_sleep);
Takashi Iwaiaf368022016-01-13 17:48:01 +01001356 mutex_init(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 tu->ticks = 1;
1358 tu->queue_size = 128;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001359 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001360 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (tu->queue == NULL) {
1362 kfree(tu);
1363 return -ENOMEM;
1364 }
1365 file->private_data = tu;
1366 return 0;
1367}
1368
1369static int snd_timer_user_release(struct inode *inode, struct file *file)
1370{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001371 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 if (file->private_data) {
1374 tu = file->private_data;
1375 file->private_data = NULL;
Takashi Iwaiaf368022016-01-13 17:48:01 +01001376 mutex_lock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (tu->timeri)
1378 snd_timer_close(tu->timeri);
Takashi Iwaiaf368022016-01-13 17:48:01 +01001379 mutex_unlock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 kfree(tu->queue);
1381 kfree(tu->tqueue);
1382 kfree(tu);
1383 }
1384 return 0;
1385}
1386
Takashi Iwai53d2f742005-11-17 13:56:05 +01001387static void snd_timer_user_zero_id(struct snd_timer_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
1389 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1390 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1391 id->card = -1;
1392 id->device = -1;
1393 id->subdevice = -1;
1394}
1395
Takashi Iwai53d2f742005-11-17 13:56:05 +01001396static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
1398 id->dev_class = timer->tmr_class;
1399 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1400 id->card = timer->card ? timer->card->number : -1;
1401 id->device = timer->tmr_device;
1402 id->subdevice = timer->tmr_subdevice;
1403}
1404
Takashi Iwai53d2f742005-11-17 13:56:05 +01001405static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001407 struct snd_timer_id id;
1408 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 struct list_head *p;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (copy_from_user(&id, _tid, sizeof(id)))
1412 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001413 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (id.dev_class < 0) { /* first item */
1415 if (list_empty(&snd_timer_list))
1416 snd_timer_user_zero_id(&id);
1417 else {
Clemens Ladisch9dfba382005-10-12 17:14:55 +02001418 timer = list_entry(snd_timer_list.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001419 struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 snd_timer_user_copy_id(&id, timer);
1421 }
1422 } else {
1423 switch (id.dev_class) {
1424 case SNDRV_TIMER_CLASS_GLOBAL:
1425 id.device = id.device < 0 ? 0 : id.device + 1;
1426 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001427 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1429 snd_timer_user_copy_id(&id, timer);
1430 break;
1431 }
1432 if (timer->tmr_device >= id.device) {
1433 snd_timer_user_copy_id(&id, timer);
1434 break;
1435 }
1436 }
1437 if (p == &snd_timer_list)
1438 snd_timer_user_zero_id(&id);
1439 break;
1440 case SNDRV_TIMER_CLASS_CARD:
1441 case SNDRV_TIMER_CLASS_PCM:
1442 if (id.card < 0) {
1443 id.card = 0;
1444 } else {
1445 if (id.card < 0) {
1446 id.card = 0;
1447 } else {
1448 if (id.device < 0) {
1449 id.device = 0;
1450 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001451 if (id.subdevice < 0) {
1452 id.subdevice = 0;
1453 } else {
1454 id.subdevice++;
1455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457 }
1458 }
1459 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001460 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (timer->tmr_class > id.dev_class) {
1462 snd_timer_user_copy_id(&id, timer);
1463 break;
1464 }
1465 if (timer->tmr_class < id.dev_class)
1466 continue;
1467 if (timer->card->number > id.card) {
1468 snd_timer_user_copy_id(&id, timer);
1469 break;
1470 }
1471 if (timer->card->number < id.card)
1472 continue;
1473 if (timer->tmr_device > id.device) {
1474 snd_timer_user_copy_id(&id, timer);
1475 break;
1476 }
1477 if (timer->tmr_device < id.device)
1478 continue;
1479 if (timer->tmr_subdevice > id.subdevice) {
1480 snd_timer_user_copy_id(&id, timer);
1481 break;
1482 }
1483 if (timer->tmr_subdevice < id.subdevice)
1484 continue;
1485 snd_timer_user_copy_id(&id, timer);
1486 break;
1487 }
1488 if (p == &snd_timer_list)
1489 snd_timer_user_zero_id(&id);
1490 break;
1491 default:
1492 snd_timer_user_zero_id(&id);
1493 }
1494 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001495 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1497 return -EFAULT;
1498 return 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001499}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001501static int snd_timer_user_ginfo(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001502 struct snd_timer_ginfo __user *_ginfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001504 struct snd_timer_ginfo *ginfo;
1505 struct snd_timer_id tid;
1506 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 struct list_head *p;
1508 int err = 0;
1509
Li Zefanef44a1e2009-04-10 09:43:08 +08001510 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1511 if (IS_ERR(ginfo))
1512 return PTR_ERR(ginfo);
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 tid = ginfo->tid;
1515 memset(ginfo, 0, sizeof(*ginfo));
1516 ginfo->tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001517 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 t = snd_timer_find(&tid);
1519 if (t != NULL) {
1520 ginfo->card = t->card ? t->card->number : -1;
1521 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1522 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1523 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1524 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1525 ginfo->resolution = t->hw.resolution;
1526 if (t->hw.resolution_min > 0) {
1527 ginfo->resolution_min = t->hw.resolution_min;
1528 ginfo->resolution_max = t->hw.resolution_max;
1529 }
1530 list_for_each(p, &t->open_list_head) {
1531 ginfo->clients++;
1532 }
1533 } else {
1534 err = -ENODEV;
1535 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001536 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1538 err = -EFAULT;
1539 kfree(ginfo);
1540 return err;
1541}
1542
Takashi Sakamoto91d21782016-03-23 08:03:59 +09001543static int timer_set_gparams(struct snd_timer_gparams *gparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001545 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 int err;
1547
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001548 mutex_lock(&register_mutex);
Takashi Sakamoto91d21782016-03-23 08:03:59 +09001549 t = snd_timer_find(&gparams->tid);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001550 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 err = -ENODEV;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001552 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001554 if (!list_empty(&t->open_list_head)) {
1555 err = -EBUSY;
1556 goto _error;
1557 }
1558 if (!t->hw.set_period) {
1559 err = -ENOSYS;
1560 goto _error;
1561 }
Takashi Sakamoto91d21782016-03-23 08:03:59 +09001562 err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001563_error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001564 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return err;
1566}
1567
Takashi Sakamoto91d21782016-03-23 08:03:59 +09001568static int snd_timer_user_gparams(struct file *file,
1569 struct snd_timer_gparams __user *_gparams)
1570{
1571 struct snd_timer_gparams gparams;
1572
1573 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1574 return -EFAULT;
1575 return timer_set_gparams(&gparams);
1576}
1577
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001578static int snd_timer_user_gstatus(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001579 struct snd_timer_gstatus __user *_gstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001581 struct snd_timer_gstatus gstatus;
1582 struct snd_timer_id tid;
1583 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int err = 0;
1585
1586 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1587 return -EFAULT;
1588 tid = gstatus.tid;
1589 memset(&gstatus, 0, sizeof(gstatus));
1590 gstatus.tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001591 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 t = snd_timer_find(&tid);
1593 if (t != NULL) {
1594 if (t->hw.c_resolution)
1595 gstatus.resolution = t->hw.c_resolution(t);
1596 else
1597 gstatus.resolution = t->hw.resolution;
1598 if (t->hw.precise_resolution) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001599 t->hw.precise_resolution(t, &gstatus.resolution_num,
1600 &gstatus.resolution_den);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 } else {
1602 gstatus.resolution_num = gstatus.resolution;
1603 gstatus.resolution_den = 1000000000uL;
1604 }
1605 } else {
1606 err = -ENODEV;
1607 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001608 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1610 err = -EFAULT;
1611 return err;
1612}
1613
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001614static int snd_timer_user_tselect(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001615 struct snd_timer_select __user *_tselect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001617 struct snd_timer_user *tu;
1618 struct snd_timer_select tselect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 char str[32];
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001620 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 tu = file->private_data;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001623 if (tu->timeri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 snd_timer_close(tu->timeri);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001625 tu->timeri = NULL;
1626 }
1627 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1628 err = -EFAULT;
1629 goto __err;
1630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 sprintf(str, "application %i", current->pid);
1632 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1633 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001634 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1635 if (err < 0)
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001636 goto __err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Takashi Iwai82ecd2f2017-06-02 17:26:56 +02001638 tu->qhead = tu->qtail = tu->qused = 0;
Jesper Juhl4d572772005-05-30 17:30:32 +02001639 kfree(tu->queue);
1640 tu->queue = NULL;
1641 kfree(tu->tqueue);
1642 tu->tqueue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001644 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001645 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001646 if (tu->tqueue == NULL)
1647 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001649 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001650 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001651 if (tu->queue == NULL)
1652 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001654
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001655 if (err < 0) {
1656 snd_timer_close(tu->timeri);
1657 tu->timeri = NULL;
1658 } else {
1659 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001660 tu->timeri->callback = tu->tread
1661 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001662 tu->timeri->ccallback = snd_timer_user_ccallback;
1663 tu->timeri->callback_data = (void *)tu;
Takashi Iwai40ed94442016-01-21 17:43:08 +01001664 tu->timeri->disconnect = snd_timer_user_disconnect;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001665 }
1666
1667 __err:
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001668 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001671static int snd_timer_user_info(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001672 struct snd_timer_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001674 struct snd_timer_user *tu;
1675 struct snd_timer_info *info;
1676 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 int err = 0;
1678
1679 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001680 if (!tu->timeri)
1681 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001683 if (!t)
1684 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Takashi Iwaica2c0962005-09-09 14:20:23 +02001686 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (! info)
1688 return -ENOMEM;
1689 info->card = t->card ? t->card->number : -1;
1690 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1691 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1692 strlcpy(info->id, t->id, sizeof(info->id));
1693 strlcpy(info->name, t->name, sizeof(info->name));
1694 info->resolution = t->hw.resolution;
1695 if (copy_to_user(_info, info, sizeof(*_info)))
1696 err = -EFAULT;
1697 kfree(info);
1698 return err;
1699}
1700
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001701static int snd_timer_user_params(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001702 struct snd_timer_params __user *_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001704 struct snd_timer_user *tu;
1705 struct snd_timer_params params;
1706 struct snd_timer *t;
1707 struct snd_timer_read *tr;
1708 struct snd_timer_tread *ttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001712 if (!tu->timeri)
1713 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001715 if (!t)
1716 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 if (copy_from_user(&params, _params, sizeof(params)))
1718 return -EFAULT;
Takashi Iwai5ac92762017-02-28 14:49:07 +01001719 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1720 u64 resolution;
1721
1722 if (params.ticks < 1) {
1723 err = -EINVAL;
1724 goto _end;
1725 }
1726
1727 /* Don't allow resolution less than 1ms */
1728 resolution = snd_timer_resolution(tu->timeri);
1729 resolution *= params.ticks;
1730 if (resolution < 1000000) {
1731 err = -EINVAL;
1732 goto _end;
1733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001735 if (params.queue_size > 0 &&
1736 (params.queue_size < 32 || params.queue_size > 1024)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 err = -EINVAL;
1738 goto _end;
1739 }
1740 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1741 (1<<SNDRV_TIMER_EVENT_TICK)|
1742 (1<<SNDRV_TIMER_EVENT_START)|
1743 (1<<SNDRV_TIMER_EVENT_STOP)|
1744 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1745 (1<<SNDRV_TIMER_EVENT_PAUSE)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001746 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1747 (1<<SNDRV_TIMER_EVENT_RESUME)|
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 (1<<SNDRV_TIMER_EVENT_MSTART)|
1749 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1750 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
Jaroslav Kysela65d11d92005-08-16 13:05:43 +02001751 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1752 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001753 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 err = -EINVAL;
1755 goto _end;
1756 }
1757 snd_timer_stop(tu->timeri);
1758 spin_lock_irq(&t->lock);
1759 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1760 SNDRV_TIMER_IFLG_EXCLUSIVE|
1761 SNDRV_TIMER_IFLG_EARLY_EVENT);
1762 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1763 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1764 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1765 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1766 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1767 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1768 spin_unlock_irq(&t->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001769 if (params.queue_size > 0 &&
1770 (unsigned int)tu->queue_size != params.queue_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (tu->tread) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001772 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1773 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 if (ttr) {
1775 kfree(tu->tqueue);
1776 tu->queue_size = params.queue_size;
1777 tu->tqueue = ttr;
1778 }
1779 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001780 tr = kmalloc(params.queue_size * sizeof(*tr),
1781 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (tr) {
1783 kfree(tu->queue);
1784 tu->queue_size = params.queue_size;
1785 tu->queue = tr;
1786 }
1787 }
1788 }
1789 tu->qhead = tu->qtail = tu->qused = 0;
1790 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1791 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001792 struct snd_timer_tread tread;
Kangjie Lucec8f962016-05-03 16:44:07 -04001793 memset(&tread, 0, sizeof(tread));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 tread.event = SNDRV_TIMER_EVENT_EARLY;
1795 tread.tstamp.tv_sec = 0;
1796 tread.tstamp.tv_nsec = 0;
1797 tread.val = 0;
1798 snd_timer_user_append_to_tqueue(tu, &tread);
1799 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001800 struct snd_timer_read *r = &tu->queue[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 r->resolution = 0;
1802 r->ticks = 0;
1803 tu->qused++;
1804 tu->qtail++;
1805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
1807 tu->filter = params.filter;
1808 tu->ticks = params.ticks;
1809 err = 0;
1810 _end:
1811 if (copy_to_user(_params, &params, sizeof(params)))
1812 return -EFAULT;
1813 return err;
1814}
1815
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001816static int snd_timer_user_status(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001817 struct snd_timer_status __user *_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001819 struct snd_timer_user *tu;
1820 struct snd_timer_status status;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001823 if (!tu->timeri)
1824 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 memset(&status, 0, sizeof(status));
1826 status.tstamp = tu->tstamp;
1827 status.resolution = snd_timer_resolution(tu->timeri);
1828 status.lost = tu->timeri->lost;
1829 status.overrun = tu->overrun;
1830 spin_lock_irq(&tu->qlock);
1831 status.queue = tu->qused;
1832 spin_unlock_irq(&tu->qlock);
1833 if (copy_to_user(_status, &status, sizeof(status)))
1834 return -EFAULT;
1835 return 0;
1836}
1837
1838static int snd_timer_user_start(struct file *file)
1839{
1840 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001841 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001844 if (!tu->timeri)
1845 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 snd_timer_stop(tu->timeri);
1847 tu->timeri->lost = 0;
1848 tu->last_resolution = 0;
1849 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1850}
1851
1852static int snd_timer_user_stop(struct file *file)
1853{
1854 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001855 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001858 if (!tu->timeri)
1859 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1861}
1862
1863static int snd_timer_user_continue(struct file *file)
1864{
1865 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001866 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001869 if (!tu->timeri)
1870 return -EBADFD;
Takashi Iwai9f8a7652016-09-07 15:45:31 +02001871 /* start timer instead of continue if it's not used before */
1872 if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1873 return snd_timer_user_start(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 tu->timeri->lost = 0;
1875 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1876}
1877
Takashi Iwai15790a62005-05-15 15:04:14 +02001878static int snd_timer_user_pause(struct file *file)
1879{
1880 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001881 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001882
Takashi Iwai15790a62005-05-15 15:04:14 +02001883 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001884 if (!tu->timeri)
1885 return -EBADFD;
Jaroslav Kyselad138b442005-05-16 13:51:39 +02001886 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
Takashi Iwai15790a62005-05-15 15:04:14 +02001887}
1888
Takashi Iwai8c50b372005-05-15 15:43:54 +02001889enum {
1890 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1891 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1892 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1893 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1894};
1895
Takashi Iwaiaf368022016-01-13 17:48:01 +01001896static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001897 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001899 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 void __user *argp = (void __user *)arg;
1901 int __user *p = argp;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 tu = file->private_data;
1904 switch (cmd) {
1905 case SNDRV_TIMER_IOCTL_PVERSION:
1906 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1907 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1908 return snd_timer_user_next_device(argp);
1909 case SNDRV_TIMER_IOCTL_TREAD:
1910 {
1911 int xarg;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001912
Takashi Iwaiaf368022016-01-13 17:48:01 +01001913 if (tu->timeri) /* too late */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return -EBUSY;
Takashi Iwaiaf368022016-01-13 17:48:01 +01001915 if (get_user(xarg, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return -EFAULT;
1917 tu->tread = xarg ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return 0;
1919 }
1920 case SNDRV_TIMER_IOCTL_GINFO:
1921 return snd_timer_user_ginfo(file, argp);
1922 case SNDRV_TIMER_IOCTL_GPARAMS:
1923 return snd_timer_user_gparams(file, argp);
1924 case SNDRV_TIMER_IOCTL_GSTATUS:
1925 return snd_timer_user_gstatus(file, argp);
1926 case SNDRV_TIMER_IOCTL_SELECT:
1927 return snd_timer_user_tselect(file, argp);
1928 case SNDRV_TIMER_IOCTL_INFO:
1929 return snd_timer_user_info(file, argp);
1930 case SNDRV_TIMER_IOCTL_PARAMS:
1931 return snd_timer_user_params(file, argp);
1932 case SNDRV_TIMER_IOCTL_STATUS:
1933 return snd_timer_user_status(file, argp);
1934 case SNDRV_TIMER_IOCTL_START:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001935 case SNDRV_TIMER_IOCTL_START_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return snd_timer_user_start(file);
1937 case SNDRV_TIMER_IOCTL_STOP:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001938 case SNDRV_TIMER_IOCTL_STOP_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return snd_timer_user_stop(file);
1940 case SNDRV_TIMER_IOCTL_CONTINUE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001941 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return snd_timer_user_continue(file);
Takashi Iwai15790a62005-05-15 15:04:14 +02001943 case SNDRV_TIMER_IOCTL_PAUSE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001944 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
Takashi Iwai15790a62005-05-15 15:04:14 +02001945 return snd_timer_user_pause(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
1947 return -ENOTTY;
1948}
1949
Takashi Iwaiaf368022016-01-13 17:48:01 +01001950static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
1951 unsigned long arg)
1952{
1953 struct snd_timer_user *tu = file->private_data;
1954 long ret;
1955
1956 mutex_lock(&tu->ioctl_lock);
1957 ret = __snd_timer_user_ioctl(file, cmd, arg);
1958 mutex_unlock(&tu->ioctl_lock);
1959 return ret;
1960}
1961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962static int snd_timer_user_fasync(int fd, struct file * file, int on)
1963{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001964 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 tu = file->private_data;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001967 return fasync_helper(fd, file, on, &tu->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001970static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
1971 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001973 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 long result = 0, unit;
Takashi Iwai4dff5c72016-02-08 17:26:58 +01001975 int qhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 tu = file->private_data;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001979 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
Takashi Iwai66e982d2017-06-02 15:03:38 +02001980 mutex_lock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 spin_lock_irq(&tu->qlock);
1982 while ((long)count - result >= unit) {
1983 while (!tu->qused) {
1984 wait_queue_t wait;
1985
1986 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1987 err = -EAGAIN;
Takashi Iwai4dff5c72016-02-08 17:26:58 +01001988 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 }
1990
1991 set_current_state(TASK_INTERRUPTIBLE);
1992 init_waitqueue_entry(&wait, current);
1993 add_wait_queue(&tu->qchange_sleep, &wait);
1994
1995 spin_unlock_irq(&tu->qlock);
Takashi Iwai66e982d2017-06-02 15:03:38 +02001996 mutex_unlock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 schedule();
Takashi Iwai66e982d2017-06-02 15:03:38 +02001998 mutex_lock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 spin_lock_irq(&tu->qlock);
2000
2001 remove_wait_queue(&tu->qchange_sleep, &wait);
2002
Takashi Iwai230323d2016-01-21 17:19:31 +01002003 if (tu->disconnected) {
2004 err = -ENODEV;
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002005 goto _error;
Takashi Iwai230323d2016-01-21 17:19:31 +01002006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (signal_pending(current)) {
2008 err = -ERESTARTSYS;
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002009 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
2011 }
2012
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002013 qhead = tu->qhead++;
2014 tu->qhead %= tu->queue_size;
Takashi Iwai3fa69932016-07-04 14:02:15 +02002015 tu->qused--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 spin_unlock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 if (tu->tread) {
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002019 if (copy_to_user(buffer, &tu->tqueue[qhead],
2020 sizeof(struct snd_timer_tread)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 } else {
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002023 if (copy_to_user(buffer, &tu->queue[qhead],
2024 sizeof(struct snd_timer_read)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 spin_lock_irq(&tu->qlock);
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002029 if (err < 0)
2030 goto _error;
2031 result += unit;
2032 buffer += unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 _error:
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002035 spin_unlock_irq(&tu->qlock);
Takashi Iwai66e982d2017-06-02 15:03:38 +02002036 mutex_unlock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return result > 0 ? result : err;
2038}
2039
2040static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
2041{
2042 unsigned int mask;
Takashi Iwai53d2f742005-11-17 13:56:05 +01002043 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 tu = file->private_data;
2046
2047 poll_wait(file, &tu->qchange_sleep, wait);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 mask = 0;
2050 if (tu->qused)
2051 mask |= POLLIN | POLLRDNORM;
Takashi Iwai230323d2016-01-21 17:19:31 +01002052 if (tu->disconnected)
2053 mask |= POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 return mask;
2056}
2057
2058#ifdef CONFIG_COMPAT
2059#include "timer_compat.c"
2060#else
2061#define snd_timer_user_ioctl_compat NULL
2062#endif
2063
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08002064static const struct file_operations snd_timer_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 .owner = THIS_MODULE,
2067 .read = snd_timer_user_read,
2068 .open = snd_timer_user_open,
2069 .release = snd_timer_user_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02002070 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 .poll = snd_timer_user_poll,
2072 .unlocked_ioctl = snd_timer_user_ioctl,
2073 .compat_ioctl = snd_timer_user_ioctl_compat,
2074 .fasync = snd_timer_user_fasync,
2075};
2076
Takashi Iwai7c358602015-01-29 18:09:05 +01002077/* unregister the system timer */
2078static void snd_timer_free_all(void)
2079{
2080 struct snd_timer *timer, *n;
2081
2082 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2083 snd_timer_free(timer);
2084}
2085
Takashi Iwai89da0612015-01-29 18:12:26 +01002086static struct device timer_dev;
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088/*
2089 * ENTRY functions
2090 */
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092static int __init alsa_timer_init(void)
2093{
2094 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Takashi Iwai89da0612015-01-29 18:12:26 +01002096 snd_device_initialize(&timer_dev, NULL);
2097 dev_set_name(&timer_dev, "timer");
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099#ifdef SNDRV_OSS_INFO_DEV_TIMERS
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002100 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2101 "system timer");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102#endif
Takashi Iwaie28563c2005-12-01 10:42:42 +01002103
Takashi Iwai7c358602015-01-29 18:09:05 +01002104 err = snd_timer_register_system();
2105 if (err < 0) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +01002106 pr_err("ALSA: unable to register system timer (%i)\n", err);
Takashi Iwai89da0612015-01-29 18:12:26 +01002107 put_device(&timer_dev);
Takashi Iwai7c358602015-01-29 18:09:05 +01002108 return err;
2109 }
2110
Takashi Iwai40a4b262015-01-30 08:34:58 +01002111 err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2112 &snd_timer_f_ops, NULL, &timer_dev);
Takashi Iwai7c358602015-01-29 18:09:05 +01002113 if (err < 0) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +01002114 pr_err("ALSA: unable to register timer device (%i)\n", err);
Takashi Iwai7c358602015-01-29 18:09:05 +01002115 snd_timer_free_all();
Takashi Iwai89da0612015-01-29 18:12:26 +01002116 put_device(&timer_dev);
Takashi Iwai7c358602015-01-29 18:09:05 +01002117 return err;
2118 }
2119
Takashi Iwaie28563c2005-12-01 10:42:42 +01002120 snd_timer_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return 0;
2122}
2123
2124static void __exit alsa_timer_exit(void)
2125{
Takashi Iwai40a4b262015-01-30 08:34:58 +01002126 snd_unregister_device(&timer_dev);
Takashi Iwai7c358602015-01-29 18:09:05 +01002127 snd_timer_free_all();
Takashi Iwai89da0612015-01-29 18:12:26 +01002128 put_device(&timer_dev);
Takashi Iwaie28563c2005-12-01 10:42:42 +01002129 snd_timer_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130#ifdef SNDRV_OSS_INFO_DEV_TIMERS
2131 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2132#endif
2133}
2134
2135module_init(alsa_timer_init)
2136module_exit(alsa_timer_exit)