blob: e944d27f79c3469b32fa708b820e9422077feb75 [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 Iwaicdf8ae72017-11-05 10:07:43 +0100182static int 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) {
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100192 if (master->timer->num_instances >=
193 master->timer->max_instances)
194 return -EBUSY;
Nicolas Kaiser5b7c7572011-03-16 17:10:11 +0100195 list_move_tail(&slave->open_list,
196 &master->slave_list_head);
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100197 master->timer->num_instances++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 spin_lock_irq(&slave_active_lock);
199 slave->master = master;
200 slave->timer = master->timer;
201 spin_unlock_irq(&slave_active_lock);
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204 }
205 }
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209/*
210 * look for slave instances matching with the slave id of the given master.
211 * when found, relink the open_link of slaves.
212 *
213 * call this with register_mutex down.
214 */
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100215static int snd_timer_check_master(struct snd_timer_instance *master)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200217 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /* check all pending slaves */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200220 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (slave->slave_class == master->slave_class &&
222 slave->slave_id == master->slave_id) {
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100223 if (master->timer->num_instances >=
224 master->timer->max_instances)
225 return -EBUSY;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200226 list_move_tail(&slave->open_list, &master->slave_list_head);
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100227 master->timer->num_instances++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 spin_lock_irq(&slave_active_lock);
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100229 spin_lock(&master->timer->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 slave->master = master;
231 slave->timer = master->timer;
232 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200233 list_add_tail(&slave->active_list,
234 &master->slave_active_head);
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100235 spin_unlock(&master->timer->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 spin_unlock_irq(&slave_active_lock);
237 }
238 }
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Takashi Iwai3a3385a2019-10-30 22:42:57 +0100242static int snd_timer_close_locked(struct snd_timer_instance *timeri,
243 struct device **card_devp_to_put);
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/*
246 * open a timer instance
247 * when opening a master, the slave id must be here given.
248 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100249int snd_timer_open(struct snd_timer_instance **ti,
250 char *owner, struct snd_timer_id *tid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 unsigned int slave_id)
252{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100253 struct snd_timer *timer;
254 struct snd_timer_instance *timeri = NULL;
Takashi Iwai3a3385a2019-10-30 22:42:57 +0100255 struct device *card_dev_to_put = NULL;
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100256 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200257
Takashi Iwai681789d2019-03-28 17:11:10 +0100258 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
260 /* open a slave instance */
261 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
262 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100263 pr_debug("ALSA: timer: invalid slave class %i\n",
264 tid->dev_sclass);
Takashi Iwai681789d2019-03-28 17:11:10 +0100265 err = -EINVAL;
266 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 timeri = snd_timer_instance_new(owner, NULL);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200269 if (!timeri) {
Takashi Iwai681789d2019-03-28 17:11:10 +0100270 err = -ENOMEM;
271 goto unlock;
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 timeri->slave_class = tid->dev_sclass;
274 timeri->slave_id = tid->device;
275 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
276 list_add_tail(&timeri->open_list, &snd_timer_slave_list);
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100277 err = snd_timer_check_slave(timeri);
278 if (err < 0) {
Takashi Iwai3a3385a2019-10-30 22:42:57 +0100279 snd_timer_close_locked(timeri, &card_dev_to_put);
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100280 timeri = NULL;
281 }
Takashi Iwai681789d2019-03-28 17:11:10 +0100282 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284
285 /* open a master instance */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 timer = snd_timer_find(tid);
Johannes Bergee2da992008-07-09 10:28:41 +0200287#ifdef CONFIG_MODULES
288 if (!timer) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100289 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 snd_timer_request(tid);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100291 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 timer = snd_timer_find(tid);
293 }
294#endif
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200295 if (!timer) {
Takashi Iwai681789d2019-03-28 17:11:10 +0100296 err = -ENODEV;
297 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200299 if (!list_empty(&timer->open_list_head)) {
Takashi Iwai8bdaac02019-11-06 17:55:47 +0100300 struct snd_timer_instance *t =
301 list_entry(timer->open_list_head.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +0100302 struct snd_timer_instance, open_list);
Takashi Iwai8bdaac02019-11-06 17:55:47 +0100303 if (t->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
Takashi Iwai681789d2019-03-28 17:11:10 +0100304 err = -EBUSY;
Takashi Iwai681789d2019-03-28 17:11:10 +0100305 goto unlock;
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200306 }
307 }
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100308 if (timer->num_instances >= timer->max_instances) {
Takashi Iwai681789d2019-03-28 17:11:10 +0100309 err = -EBUSY;
310 goto unlock;
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100311 }
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200312 timeri = snd_timer_instance_new(owner, timer);
313 if (!timeri) {
Takashi Iwai681789d2019-03-28 17:11:10 +0100314 err = -ENOMEM;
315 goto unlock;
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200316 }
Takashi Iwai230323d2016-01-21 17:19:31 +0100317 /* take a card refcount for safe disconnection */
318 if (timer->card)
319 get_device(&timer->card->card_dev);
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200320 timeri->slave_class = tid->dev_sclass;
321 timeri->slave_id = slave_id;
Vegard Nossum8ddc0562016-08-29 00:33:51 +0200322
323 if (list_empty(&timer->open_list_head) && timer->hw.open) {
Takashi Iwai681789d2019-03-28 17:11:10 +0100324 err = timer->hw.open(timer);
Vegard Nossum8ddc0562016-08-29 00:33:51 +0200325 if (err) {
326 kfree(timeri->owner);
327 kfree(timeri);
Takashi Iwai681789d2019-03-28 17:11:10 +0100328 timeri = NULL;
Vegard Nossum8ddc0562016-08-29 00:33:51 +0200329
330 if (timer->card)
Takashi Iwai3a3385a2019-10-30 22:42:57 +0100331 card_dev_to_put = &timer->card->card_dev;
Vegard Nossum8ddc0562016-08-29 00:33:51 +0200332 module_put(timer->module);
Takashi Iwai681789d2019-03-28 17:11:10 +0100333 goto unlock;
Vegard Nossum8ddc0562016-08-29 00:33:51 +0200334 }
335 }
336
Clemens Ladisch2fd43d12005-10-12 17:10:35 +0200337 list_add_tail(&timeri->open_list, &timer->open_list_head);
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100338 timer->num_instances++;
339 err = snd_timer_check_master(timeri);
340 if (err < 0) {
Takashi Iwai3a3385a2019-10-30 22:42:57 +0100341 snd_timer_close_locked(timeri, &card_dev_to_put);
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100342 timeri = NULL;
343 }
Takashi Iwai681789d2019-03-28 17:11:10 +0100344
345 unlock:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100346 mutex_unlock(&register_mutex);
Takashi Iwai3a3385a2019-10-30 22:42:57 +0100347 /* put_device() is called after unlock for avoiding deadlock */
348 if (card_dev_to_put)
349 put_device(card_dev_to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 *ti = timeri;
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100351 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
Takashi Iwai444dac22017-06-16 16:16:05 +0200353EXPORT_SYMBOL(snd_timer_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/*
356 * close a timer instance
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100357 * call this with register_mutex down.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 */
Takashi Iwai3a3385a2019-10-30 22:42:57 +0100359static int snd_timer_close_locked(struct snd_timer_instance *timeri,
360 struct device **card_devp_to_put)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100362 struct snd_timer *timer = NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200363 struct snd_timer_instance *slave, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100365 list_del(&timeri->open_list);
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /* force to stop the timer */
368 snd_timer_stop(timeri);
369
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100370 timer = timeri->timer;
371 if (timer) {
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100372 timer->num_instances--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* wait, until the active callback is finished */
374 spin_lock_irq(&timer->lock);
375 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
376 spin_unlock_irq(&timer->lock);
377 udelay(10);
378 spin_lock_irq(&timer->lock);
379 }
380 spin_unlock_irq(&timer->lock);
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /* remove slave links */
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100383 spin_lock_irq(&slave_active_lock);
384 spin_lock(&timer->lock);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200385 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
386 open_list) {
Johannes Berg9244b2c2006-10-05 16:02:22 +0200387 list_move_tail(&slave->open_list, &snd_timer_slave_list);
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100388 timer->num_instances--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 slave->master = NULL;
390 slave->timer = NULL;
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100391 list_del_init(&slave->ack_list);
392 list_del_init(&slave->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100394 spin_unlock(&timer->lock);
395 spin_unlock_irq(&slave_active_lock);
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100396
397 /* slave doesn't need to release timer resources below */
398 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
399 timer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (timeri->private_free)
403 timeri->private_free(timeri);
404 kfree(timeri->owner);
405 kfree(timeri);
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100406
407 if (timer) {
408 if (list_empty(&timer->open_list_head) && timer->hw.close)
409 timer->hw.close(timer);
410 /* release a card refcount for safe disconnection */
411 if (timer->card)
Takashi Iwai3a3385a2019-10-30 22:42:57 +0100412 *card_devp_to_put = &timer->card->card_dev;
Clemens Ladischde242142005-10-12 17:12:31 +0200413 module_put(timer->module);
Takashi Iwai9984d1b2016-02-10 11:53:30 +0100414 }
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return 0;
417}
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100418
419/*
420 * close a timer instance
421 */
422int snd_timer_close(struct snd_timer_instance *timeri)
423{
Takashi Iwai3a3385a2019-10-30 22:42:57 +0100424 struct device *card_dev_to_put = NULL;
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100425 int err;
426
427 if (snd_BUG_ON(!timeri))
428 return -ENXIO;
429
430 mutex_lock(&register_mutex);
Takashi Iwai3a3385a2019-10-30 22:42:57 +0100431 err = snd_timer_close_locked(timeri, &card_dev_to_put);
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100432 mutex_unlock(&register_mutex);
Takashi Iwai3a3385a2019-10-30 22:42:57 +0100433 /* put_device() is called after unlock for avoiding deadlock */
434 if (card_dev_to_put)
435 put_device(card_dev_to_put);
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100436 return err;
437}
Takashi Iwai444dac22017-06-16 16:16:05 +0200438EXPORT_SYMBOL(snd_timer_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Takashi Iwai53d2f742005-11-17 13:56:05 +0100440unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100442 struct snd_timer * timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (timeri == NULL)
445 return 0;
446 if ((timer = timeri->timer) != NULL) {
447 if (timer->hw.c_resolution)
448 return timer->hw.c_resolution(timer);
449 return timer->hw.resolution;
450 }
451 return 0;
452}
Takashi Iwai444dac22017-06-16 16:16:05 +0200453EXPORT_SYMBOL(snd_timer_resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Takashi Iwai53d2f742005-11-17 13:56:05 +0100455static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100457 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100459 struct snd_timer_instance *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct timespec tstamp;
461
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100462 if (timer_tstamp_monotonic)
Thomas Gleixner26204e02014-06-11 23:59:14 +0000463 ktime_get_ts(&tstamp);
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100464 else
465 getnstimeofday(&tstamp);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200466 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
467 event > SNDRV_TIMER_EVENT_PAUSE))
468 return;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200469 if (event == SNDRV_TIMER_EVENT_START ||
470 event == SNDRV_TIMER_EVENT_CONTINUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 resolution = snd_timer_resolution(ti);
472 if (ti->ccallback)
Jaroslav Kyselab30477d2010-03-03 11:05:55 +0100473 ti->ccallback(ti, event, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
475 return;
476 timer = ti->timer;
477 if (timer == NULL)
478 return;
479 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
480 return;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200481 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (ts->ccallback)
Takashi Iwai117159f2016-02-08 17:36:25 +0100483 ts->ccallback(ts, event + 100, &tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100486/* start/continue a master timer */
487static int snd_timer_start1(struct snd_timer_instance *timeri,
488 bool start, unsigned long ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100490 struct snd_timer *timer;
491 int result;
492 unsigned long flags;
493
494 timer = timeri->timer;
495 if (!timer)
496 return -EINVAL;
497
498 spin_lock_irqsave(&timer->lock, flags);
499 if (timer->card && timer->card->shutdown) {
500 result = -ENODEV;
501 goto unlock;
502 }
503 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
504 SNDRV_TIMER_IFLG_START)) {
505 result = -EBUSY;
506 goto unlock;
507 }
508
509 if (start)
510 timeri->ticks = timeri->cticks = ticks;
511 else if (!timeri->cticks)
512 timeri->cticks = 1;
513 timeri->pticks = 0;
514
Nicolas Kaiser5b7c7572011-03-16 17:10:11 +0100515 list_move_tail(&timeri->active_list, &timer->active_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (timer->running) {
517 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
518 goto __start_now;
519 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
520 timeri->flags |= SNDRV_TIMER_IFLG_START;
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100521 result = 1; /* delayed start */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 } else {
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100523 if (start)
524 timer->sticks = ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 timer->hw.start(timer);
526 __start_now:
527 timer->running++;
528 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100529 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100531 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
532 SNDRV_TIMER_EVENT_CONTINUE);
533 unlock:
534 spin_unlock_irqrestore(&timer->lock, flags);
535 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100538/* start/continue a slave timer */
539static int snd_timer_start_slave(struct snd_timer_instance *timeri,
540 bool start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 unsigned long flags;
543
544 spin_lock_irqsave(&slave_active_lock, flags);
Takashi Iwaif784beb2016-01-30 23:09:08 +0100545 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
546 spin_unlock_irqrestore(&slave_active_lock, flags);
547 return -EBUSY;
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100550 if (timeri->master && timeri->timer) {
551 spin_lock(&timeri->timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200552 list_add_tail(&timeri->active_list,
553 &timeri->master->slave_active_head);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100554 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
555 SNDRV_TIMER_EVENT_CONTINUE);
Takashi Iwaib5a663a2016-01-14 16:30:58 +0100556 spin_unlock(&timeri->timer->lock);
557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 spin_unlock_irqrestore(&slave_active_lock, flags);
559 return 1; /* delayed start */
560}
561
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100562/* stop/pause a master timer */
563static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100565 struct snd_timer *timer;
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100566 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 unsigned long flags;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 timer = timeri->timer;
570 if (!timer)
571 return -EINVAL;
572 spin_lock_irqsave(&timer->lock, flags);
Takashi Iwaif784beb2016-01-30 23:09:08 +0100573 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
574 SNDRV_TIMER_IFLG_START))) {
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100575 result = -EBUSY;
576 goto unlock;
Takashi Iwaif784beb2016-01-30 23:09:08 +0100577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 list_del_init(&timeri->ack_list);
579 list_del_init(&timeri->active_list);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100580 if (timer->card && timer->card->shutdown)
581 goto unlock;
582 if (stop) {
583 timeri->cticks = timeri->ticks;
584 timeri->pticks = 0;
Takashi Iwai230323d2016-01-21 17:19:31 +0100585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
587 !(--timer->running)) {
588 timer->hw.stop(timer);
589 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
590 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
591 snd_timer_reschedule(timer, 0);
592 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
593 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
594 timer->hw.start(timer);
595 }
596 }
597 }
Takashi Iwaic3b16812016-01-14 17:01:46 +0100598 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
Takashi Iwai9f8a7652016-09-07 15:45:31 +0200599 if (stop)
600 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
601 else
602 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100603 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
Ben Hutchings416808f2018-05-17 22:34:39 +0100604 SNDRV_TIMER_EVENT_PAUSE);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100605 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 spin_unlock_irqrestore(&timer->lock, flags);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100607 return result;
608}
609
610/* stop/pause a slave timer */
611static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
612{
613 unsigned long flags;
614
615 spin_lock_irqsave(&slave_active_lock, flags);
616 if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
617 spin_unlock_irqrestore(&slave_active_lock, flags);
618 return -EBUSY;
619 }
620 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
621 if (timeri->timer) {
622 spin_lock(&timeri->timer->lock);
623 list_del_init(&timeri->ack_list);
624 list_del_init(&timeri->active_list);
625 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
Ben Hutchings416808f2018-05-17 22:34:39 +0100626 SNDRV_TIMER_EVENT_PAUSE);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100627 spin_unlock(&timeri->timer->lock);
628 }
629 spin_unlock_irqrestore(&slave_active_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return 0;
631}
632
633/*
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100634 * start the timer instance
635 */
636int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
637{
638 if (timeri == NULL || ticks < 1)
639 return -EINVAL;
640 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
641 return snd_timer_start_slave(timeri, true);
642 else
643 return snd_timer_start1(timeri, true, ticks);
644}
Takashi Iwai444dac22017-06-16 16:16:05 +0200645EXPORT_SYMBOL(snd_timer_start);
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100646
647/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 * stop the timer instance.
649 *
650 * do not call this from the timer callback!
651 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100652int snd_timer_stop(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100654 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
655 return snd_timer_stop_slave(timeri, true);
656 else
657 return snd_timer_stop1(timeri, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
Takashi Iwai444dac22017-06-16 16:16:05 +0200659EXPORT_SYMBOL(snd_timer_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661/*
662 * start again.. the tick is kept.
663 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100664int snd_timer_continue(struct snd_timer_instance *timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Takashi Iwai9f8a7652016-09-07 15:45:31 +0200666 /* timer can continue only after pause */
667 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
668 return -EINVAL;
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100671 return snd_timer_start_slave(timeri, false);
672 else
673 return snd_timer_start1(timeri, false, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
Takashi Iwai444dac22017-06-16 16:16:05 +0200675EXPORT_SYMBOL(snd_timer_continue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677/*
678 * pause.. remember the ticks left
679 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100680int snd_timer_pause(struct snd_timer_instance * timeri)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Takashi Iwaif65e0d22016-02-10 12:47:03 +0100682 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
683 return snd_timer_stop_slave(timeri, false);
684 else
685 return snd_timer_stop1(timeri, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
Takashi Iwai444dac22017-06-16 16:16:05 +0200687EXPORT_SYMBOL(snd_timer_pause);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689/*
690 * reschedule the timer
691 *
692 * start pending instances and check the scheduling ticks.
693 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
694 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100695static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100697 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 unsigned long ticks = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Johannes Berg9244b2c2006-10-05 16:02:22 +0200700 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (ti->flags & SNDRV_TIMER_IFLG_START) {
702 ti->flags &= ~SNDRV_TIMER_IFLG_START;
703 ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
704 timer->running++;
705 }
706 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
707 if (ticks > ti->cticks)
708 ticks = ti->cticks;
709 }
710 }
711 if (ticks == ~0UL) {
712 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
713 return;
714 }
715 if (ticks > timer->hw.ticks)
716 ticks = timer->hw.ticks;
717 if (ticks_left != ticks)
718 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
719 timer->sticks = ticks;
720}
721
722/*
723 * timer tasklet
724 *
725 */
726static void snd_timer_tasklet(unsigned long arg)
727{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100728 struct snd_timer *timer = (struct snd_timer *) arg;
729 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct list_head *p;
731 unsigned long resolution, ticks;
Takashi Iwai2999ff52006-07-05 17:16:58 +0200732 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Takashi Iwai230323d2016-01-21 17:19:31 +0100734 if (timer->card && timer->card->shutdown)
735 return;
736
Takashi Iwai2999ff52006-07-05 17:16:58 +0200737 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /* now process all callbacks */
739 while (!list_empty(&timer->sack_list_head)) {
740 p = timer->sack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100741 ti = list_entry(p, struct snd_timer_instance, ack_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 /* remove from ack_list and make empty */
744 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 ticks = ti->pticks;
747 ti->pticks = 0;
748 resolution = ti->resolution;
749
750 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
751 spin_unlock(&timer->lock);
752 if (ti->callback)
753 ti->callback(ti, resolution, ticks);
754 spin_lock(&timer->lock);
755 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
756 }
Takashi Iwai2999ff52006-07-05 17:16:58 +0200757 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
760/*
761 * timer interrupt
762 *
763 * ticks_left is usually equal to timer->sticks.
764 *
765 */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100766void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Johannes Berg9244b2c2006-10-05 16:02:22 +0200768 struct snd_timer_instance *ti, *ts, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 unsigned long resolution, ticks;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200770 struct list_head *p, *ack_list_head;
Takashi Iwaib32425a2005-11-18 18:52:14 +0100771 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 int use_tasklet = 0;
773
774 if (timer == NULL)
775 return;
776
Takashi Iwai230323d2016-01-21 17:19:31 +0100777 if (timer->card && timer->card->shutdown)
778 return;
779
Takashi Iwaib32425a2005-11-18 18:52:14 +0100780 spin_lock_irqsave(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 /* remember the current resolution */
783 if (timer->hw.c_resolution)
784 resolution = timer->hw.c_resolution(timer);
785 else
786 resolution = timer->hw.resolution;
787
788 /* loop for all active instances
Johannes Berg9244b2c2006-10-05 16:02:22 +0200789 * Here we cannot use list_for_each_entry because the active_list of a
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200790 * processed instance is relinked to done_list_head before the callback
791 * is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
Johannes Berg9244b2c2006-10-05 16:02:22 +0200793 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
794 active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
796 continue;
797 ti->pticks += ticks_left;
798 ti->resolution = resolution;
799 if (ti->cticks < ticks_left)
800 ti->cticks = 0;
801 else
802 ti->cticks -= ticks_left;
803 if (ti->cticks) /* not expired */
804 continue;
805 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
806 ti->cticks = ti->ticks;
807 } else {
808 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
Takashi Iwai094fd3b2016-02-04 17:06:13 +0100809 --timer->running;
810 list_del_init(&ti->active_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200812 if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
813 (ti->flags & SNDRV_TIMER_IFLG_FAST))
814 ack_list_head = &timer->ack_list_head;
815 else
816 ack_list_head = &timer->sack_list_head;
817 if (list_empty(&ti->ack_list))
818 list_add_tail(&ti->ack_list, ack_list_head);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200819 list_for_each_entry(ts, &ti->slave_active_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 ts->pticks = ti->pticks;
821 ts->resolution = resolution;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200822 if (list_empty(&ts->ack_list))
823 list_add_tail(&ts->ack_list, ack_list_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825 }
826 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
Clemens Ladischcd93fe42006-07-17 16:53:57 +0200827 snd_timer_reschedule(timer, timer->sticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (timer->running) {
829 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
830 timer->hw.stop(timer);
831 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
832 }
833 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
834 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
835 /* restart timer */
836 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
837 timer->hw.start(timer);
838 }
839 } else {
840 timer->hw.stop(timer);
841 }
842
843 /* now process all fast callbacks */
844 while (!list_empty(&timer->ack_list_head)) {
845 p = timer->ack_list_head.next; /* get first item */
Takashi Iwai53d2f742005-11-17 13:56:05 +0100846 ti = list_entry(p, struct snd_timer_instance, ack_list);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /* remove from ack_list and make empty */
849 list_del_init(p);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 ticks = ti->pticks;
852 ti->pticks = 0;
853
854 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
855 spin_unlock(&timer->lock);
856 if (ti->callback)
857 ti->callback(ti, resolution, ticks);
858 spin_lock(&timer->lock);
859 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
860 }
861
862 /* do we have any slow callbacks? */
863 use_tasklet = !list_empty(&timer->sack_list_head);
Takashi Iwaib32425a2005-11-18 18:52:14 +0100864 spin_unlock_irqrestore(&timer->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 if (use_tasklet)
Takashi Iwai1f041282008-12-18 12:17:55 +0100867 tasklet_schedule(&timer->task_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
Takashi Iwai444dac22017-06-16 16:16:05 +0200869EXPORT_SYMBOL(snd_timer_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871/*
872
873 */
874
Takashi Iwai53d2f742005-11-17 13:56:05 +0100875int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
876 struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100878 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +0100880 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 .dev_free = snd_timer_dev_free,
882 .dev_register = snd_timer_dev_register,
Takashi Iwaic4614822006-06-23 14:38:23 +0200883 .dev_disconnect = snd_timer_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 };
885
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200886 if (snd_BUG_ON(!tid))
887 return -EINVAL;
888 if (rtimer)
889 *rtimer = NULL;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200890 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
Takashi Iwaiec0e9932015-03-10 15:42:14 +0100891 if (!timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return -ENOMEM;
893 timer->tmr_class = tid->dev_class;
894 timer->card = card;
895 timer->tmr_device = tid->device;
896 timer->tmr_subdevice = tid->subdevice;
897 if (id)
898 strlcpy(timer->id, id, sizeof(timer->id));
Vegard Nossum6b760bb2016-08-29 00:33:50 +0200899 timer->sticks = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 INIT_LIST_HEAD(&timer->device_list);
901 INIT_LIST_HEAD(&timer->open_list_head);
902 INIT_LIST_HEAD(&timer->active_list_head);
903 INIT_LIST_HEAD(&timer->ack_list_head);
904 INIT_LIST_HEAD(&timer->sack_list_head);
905 spin_lock_init(&timer->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200906 tasklet_init(&timer->task_queue, snd_timer_tasklet,
907 (unsigned long)timer);
Takashi Iwaicdf8ae72017-11-05 10:07:43 +0100908 timer->max_instances = 1000; /* default limit per timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (card != NULL) {
Clemens Ladischde242142005-10-12 17:12:31 +0200910 timer->module = card->module;
Clemens Ladisch6b172a82005-10-12 17:20:21 +0200911 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
912 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 snd_timer_free(timer);
914 return err;
915 }
916 }
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200917 if (rtimer)
918 *rtimer = timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return 0;
920}
Takashi Iwai444dac22017-06-16 16:16:05 +0200921EXPORT_SYMBOL(snd_timer_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Takashi Iwai53d2f742005-11-17 13:56:05 +0100923static int snd_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200925 if (!timer)
926 return 0;
Takashi Iwaic4614822006-06-23 14:38:23 +0200927
928 mutex_lock(&register_mutex);
929 if (! list_empty(&timer->open_list_head)) {
930 struct list_head *p, *n;
931 struct snd_timer_instance *ti;
Takashi Iwaicf74dcf2014-02-04 18:22:39 +0100932 pr_warn("ALSA: timer %p is busy?\n", timer);
Takashi Iwaic4614822006-06-23 14:38:23 +0200933 list_for_each_safe(p, n, &timer->open_list_head) {
934 list_del_init(p);
935 ti = list_entry(p, struct snd_timer_instance, open_list);
936 ti->timer = NULL;
937 }
938 }
939 list_del(&timer->device_list);
940 mutex_unlock(&register_mutex);
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (timer->private_free)
943 timer->private_free(timer);
944 kfree(timer);
945 return 0;
946}
947
Takashi Iwai53d2f742005-11-17 13:56:05 +0100948static int snd_timer_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100950 struct snd_timer *timer = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return snd_timer_free(timer);
952}
953
Takashi Iwai53d2f742005-11-17 13:56:05 +0100954static int snd_timer_dev_register(struct snd_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100956 struct snd_timer *timer = dev->device_data;
957 struct snd_timer *timer1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200959 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
960 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
962 !timer->hw.resolution && timer->hw.c_resolution == NULL)
963 return -EINVAL;
964
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100965 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200966 list_for_each_entry(timer1, &snd_timer_list, device_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (timer1->tmr_class > timer->tmr_class)
968 break;
969 if (timer1->tmr_class < timer->tmr_class)
970 continue;
971 if (timer1->card && timer->card) {
972 if (timer1->card->number > timer->card->number)
973 break;
974 if (timer1->card->number < timer->card->number)
975 continue;
976 }
977 if (timer1->tmr_device > timer->tmr_device)
978 break;
979 if (timer1->tmr_device < timer->tmr_device)
980 continue;
981 if (timer1->tmr_subdevice > timer->tmr_subdevice)
982 break;
983 if (timer1->tmr_subdevice < timer->tmr_subdevice)
984 continue;
985 /* conflicts.. */
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100986 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return -EBUSY;
988 }
Johannes Berg9244b2c2006-10-05 16:02:22 +0200989 list_add_tail(&timer->device_list, &timer1->device_list);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100990 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return 0;
992}
993
Takashi Iwaic4614822006-06-23 14:38:23 +0200994static int snd_timer_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Takashi Iwai53d2f742005-11-17 13:56:05 +0100996 struct snd_timer *timer = device->device_data;
Takashi Iwai230323d2016-01-21 17:19:31 +0100997 struct snd_timer_instance *ti;
998
Takashi Iwaic4614822006-06-23 14:38:23 +0200999 mutex_lock(&register_mutex);
1000 list_del_init(&timer->device_list);
Takashi Iwai230323d2016-01-21 17:19:31 +01001001 /* wake up pending sleepers */
1002 list_for_each_entry(ti, &timer->open_list_head, open_list) {
Takashi Iwai40ed94442016-01-21 17:43:08 +01001003 if (ti->disconnect)
1004 ti->disconnect(ti);
Takashi Iwai230323d2016-01-21 17:19:31 +01001005 }
Takashi Iwaic4614822006-06-23 14:38:23 +02001006 mutex_unlock(&register_mutex);
1007 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Takashi Iwai53d2f742005-11-17 13:56:05 +01001010void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
1012 unsigned long flags;
1013 unsigned long resolution = 0;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001014 struct snd_timer_instance *ti, *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Takashi Iwai230323d2016-01-21 17:19:31 +01001016 if (timer->card && timer->card->shutdown)
1017 return;
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02001018 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
1019 return;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001020 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
1021 event > SNDRV_TIMER_EVENT_MRESUME))
1022 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 spin_lock_irqsave(&timer->lock, flags);
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001024 if (event == SNDRV_TIMER_EVENT_MSTART ||
1025 event == SNDRV_TIMER_EVENT_MCONTINUE ||
1026 event == SNDRV_TIMER_EVENT_MRESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (timer->hw.c_resolution)
1028 resolution = timer->hw.c_resolution(timer);
1029 else
1030 resolution = timer->hw.resolution;
1031 }
Johannes Berg9244b2c2006-10-05 16:02:22 +02001032 list_for_each_entry(ti, &timer->active_list_head, active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (ti->ccallback)
1034 ti->ccallback(ti, event, tstamp, resolution);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001035 list_for_each_entry(ts, &ti->slave_active_head, active_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (ts->ccallback)
1037 ts->ccallback(ts, event, tstamp, resolution);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039 spin_unlock_irqrestore(&timer->lock, flags);
1040}
Takashi Iwai444dac22017-06-16 16:16:05 +02001041EXPORT_SYMBOL(snd_timer_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043/*
1044 * exported functions for global timers
1045 */
Takashi Iwai53d2f742005-11-17 13:56:05 +01001046int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001048 struct snd_timer_id tid;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
1051 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1052 tid.card = -1;
1053 tid.device = device;
1054 tid.subdevice = 0;
1055 return snd_timer_new(NULL, id, &tid, rtimer);
1056}
Takashi Iwai444dac22017-06-16 16:16:05 +02001057EXPORT_SYMBOL(snd_timer_global_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Takashi Iwai53d2f742005-11-17 13:56:05 +01001059int snd_timer_global_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 return snd_timer_free(timer);
1062}
Takashi Iwai444dac22017-06-16 16:16:05 +02001063EXPORT_SYMBOL(snd_timer_global_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Takashi Iwai53d2f742005-11-17 13:56:05 +01001065int snd_timer_global_register(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001067 struct snd_device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 memset(&dev, 0, sizeof(dev));
1070 dev.device_data = timer;
1071 return snd_timer_dev_register(&dev);
1072}
Takashi Iwai444dac22017-06-16 16:16:05 +02001073EXPORT_SYMBOL(snd_timer_global_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001075/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 * System timer
1077 */
1078
1079struct snd_timer_system_private {
1080 struct timer_list tlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 unsigned long last_expires;
1082 unsigned long last_jiffies;
1083 unsigned long correction;
1084};
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086static void snd_timer_s_function(unsigned long data)
1087{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001088 struct snd_timer *timer = (struct snd_timer *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 struct snd_timer_system_private *priv = timer->private_data;
1090 unsigned long jiff = jiffies;
1091 if (time_after(jiff, priv->last_expires))
Clemens Ladisch6ed5eff2006-07-17 16:51:37 +02001092 priv->correction += (long)jiff - (long)priv->last_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1094}
1095
Takashi Iwai53d2f742005-11-17 13:56:05 +01001096static int snd_timer_s_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 struct snd_timer_system_private *priv;
1099 unsigned long njiff;
1100
1101 priv = (struct snd_timer_system_private *) timer->private_data;
1102 njiff = (priv->last_jiffies = jiffies);
1103 if (priv->correction > timer->sticks - 1) {
1104 priv->correction -= timer->sticks - 1;
1105 njiff++;
1106 } else {
1107 njiff += timer->sticks - priv->correction;
Clemens Ladisch17f48ec2006-07-17 16:50:56 +02001108 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
Takashi Iwai4a070832016-04-01 12:28:16 +02001110 priv->last_expires = njiff;
1111 mod_timer(&priv->tlist, njiff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return 0;
1113}
1114
Takashi Iwai53d2f742005-11-17 13:56:05 +01001115static int snd_timer_s_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117 struct snd_timer_system_private *priv;
1118 unsigned long jiff;
1119
1120 priv = (struct snd_timer_system_private *) timer->private_data;
1121 del_timer(&priv->tlist);
1122 jiff = jiffies;
1123 if (time_before(jiff, priv->last_expires))
1124 timer->sticks = priv->last_expires - jiff;
1125 else
1126 timer->sticks = 1;
Clemens Ladischde2696d2006-07-17 16:52:09 +02001127 priv->correction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return 0;
1129}
1130
Takashi Iwaif1463572016-02-02 14:14:10 +01001131static int snd_timer_s_close(struct snd_timer *timer)
1132{
1133 struct snd_timer_system_private *priv;
1134
1135 priv = (struct snd_timer_system_private *)timer->private_data;
1136 del_timer_sync(&priv->tlist);
1137 return 0;
1138}
1139
Takashi Iwai53d2f742005-11-17 13:56:05 +01001140static struct snd_timer_hardware snd_timer_system =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
1143 .resolution = 1000000000L / HZ,
1144 .ticks = 10000000L,
Takashi Iwaif1463572016-02-02 14:14:10 +01001145 .close = snd_timer_s_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 .start = snd_timer_s_start,
1147 .stop = snd_timer_s_stop
1148};
1149
Takashi Iwai53d2f742005-11-17 13:56:05 +01001150static void snd_timer_free_system(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 kfree(timer->private_data);
1153}
1154
1155static int snd_timer_register_system(void)
1156{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001157 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 struct snd_timer_system_private *priv;
1159 int err;
1160
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001161 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1162 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return err;
1164 strcpy(timer->name, "system timer");
1165 timer->hw = snd_timer_system;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001166 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (priv == NULL) {
1168 snd_timer_free(timer);
1169 return -ENOMEM;
1170 }
Takashi Iwaif169c102015-01-19 11:26:25 +01001171 setup_timer(&priv->tlist, snd_timer_s_function, (unsigned long) timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 timer->private_data = priv;
1173 timer->private_free = snd_timer_free_system;
1174 return snd_timer_global_register(timer);
1175}
1176
Jie Yangcd6a6502015-05-27 19:45:45 +08001177#ifdef CONFIG_SND_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178/*
1179 * Info interface
1180 */
1181
Takashi Iwai53d2f742005-11-17 13:56:05 +01001182static void snd_timer_proc_read(struct snd_info_entry *entry,
1183 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001185 struct snd_timer *timer;
1186 struct snd_timer_instance *ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001188 mutex_lock(&register_mutex);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001189 list_for_each_entry(timer, &snd_timer_list, device_list) {
Takashi Iwai230323d2016-01-21 17:19:31 +01001190 if (timer->card && timer->card->shutdown)
1191 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 switch (timer->tmr_class) {
1193 case SNDRV_TIMER_CLASS_GLOBAL:
1194 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1195 break;
1196 case SNDRV_TIMER_CLASS_CARD:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001197 snd_iprintf(buffer, "C%i-%i: ",
1198 timer->card->number, timer->tmr_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 break;
1200 case SNDRV_TIMER_CLASS_PCM:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001201 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1202 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 break;
1204 default:
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001205 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1206 timer->card ? timer->card->number : -1,
1207 timer->tmr_device, timer->tmr_subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209 snd_iprintf(buffer, "%s :", timer->name);
1210 if (timer->hw.resolution)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001211 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
1212 timer->hw.resolution / 1000,
1213 timer->hw.resolution % 1000,
1214 timer->hw.ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1216 snd_iprintf(buffer, " SLAVE");
1217 snd_iprintf(buffer, "\n");
Johannes Berg9244b2c2006-10-05 16:02:22 +02001218 list_for_each_entry(ti, &timer->open_list_head, open_list)
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001219 snd_iprintf(buffer, " Client %s : %s\n",
1220 ti->owner ? ti->owner : "unknown",
1221 ti->flags & (SNDRV_TIMER_IFLG_START |
1222 SNDRV_TIMER_IFLG_RUNNING)
1223 ? "running" : "stopped");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001225 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Takashi Iwai6581f4e2006-05-17 17:14:51 +02001228static struct snd_info_entry *snd_timer_proc_entry;
Takashi Iwaie28563c2005-12-01 10:42:42 +01001229
1230static void __init snd_timer_proc_init(void)
1231{
1232 struct snd_info_entry *entry;
1233
1234 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
1235 if (entry != NULL) {
Takashi Iwaie28563c2005-12-01 10:42:42 +01001236 entry->c.text.read = snd_timer_proc_read;
1237 if (snd_info_register(entry) < 0) {
1238 snd_info_free_entry(entry);
1239 entry = NULL;
1240 }
1241 }
1242 snd_timer_proc_entry = entry;
1243}
1244
1245static void __exit snd_timer_proc_done(void)
1246{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001247 snd_info_free_entry(snd_timer_proc_entry);
Takashi Iwaie28563c2005-12-01 10:42:42 +01001248}
Jie Yangcd6a6502015-05-27 19:45:45 +08001249#else /* !CONFIG_SND_PROC_FS */
Takashi Iwaie28563c2005-12-01 10:42:42 +01001250#define snd_timer_proc_init()
1251#define snd_timer_proc_done()
1252#endif
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254/*
1255 * USER SPACE interface
1256 */
1257
Takashi Iwai53d2f742005-11-17 13:56:05 +01001258static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 unsigned long resolution,
1260 unsigned long ticks)
1261{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001262 struct snd_timer_user *tu = timeri->callback_data;
1263 struct snd_timer_read *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int prev;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 spin_lock(&tu->qlock);
1267 if (tu->qused > 0) {
1268 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1269 r = &tu->queue[prev];
1270 if (r->resolution == resolution) {
1271 r->ticks += ticks;
1272 goto __wake;
1273 }
1274 }
1275 if (tu->qused >= tu->queue_size) {
1276 tu->overrun++;
1277 } else {
1278 r = &tu->queue[tu->qtail++];
1279 tu->qtail %= tu->queue_size;
1280 r->resolution = resolution;
1281 r->ticks = ticks;
1282 tu->qused++;
1283 }
1284 __wake:
1285 spin_unlock(&tu->qlock);
1286 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1287 wake_up(&tu->qchange_sleep);
1288}
1289
Takashi Iwai53d2f742005-11-17 13:56:05 +01001290static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
1291 struct snd_timer_tread *tread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 if (tu->qused >= tu->queue_size) {
1294 tu->overrun++;
1295 } else {
1296 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
1297 tu->qtail %= tu->queue_size;
1298 tu->qused++;
1299 }
1300}
1301
Takashi Iwai53d2f742005-11-17 13:56:05 +01001302static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
1303 int event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 struct timespec *tstamp,
1305 unsigned long resolution)
1306{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001307 struct snd_timer_user *tu = timeri->callback_data;
1308 struct snd_timer_tread r1;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001309 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001311 if (event >= SNDRV_TIMER_EVENT_START &&
1312 event <= SNDRV_TIMER_EVENT_PAUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 tu->tstamp = *tstamp;
1314 if ((tu->filter & (1 << event)) == 0 || !tu->tread)
1315 return;
Kangjie Lu9a47e9c2016-05-03 16:44:20 -04001316 memset(&r1, 0, sizeof(r1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 r1.event = event;
1318 r1.tstamp = *tstamp;
1319 r1.val = resolution;
Dan Carpenterbfe70782010-04-28 10:29:14 +02001320 spin_lock_irqsave(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 snd_timer_user_append_to_tqueue(tu, &r1);
Dan Carpenterbfe70782010-04-28 10:29:14 +02001322 spin_unlock_irqrestore(&tu->qlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1324 wake_up(&tu->qchange_sleep);
1325}
1326
Takashi Iwai40ed94442016-01-21 17:43:08 +01001327static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
1328{
1329 struct snd_timer_user *tu = timeri->callback_data;
1330
1331 tu->disconnected = true;
1332 wake_up(&tu->qchange_sleep);
1333}
1334
Takashi Iwai53d2f742005-11-17 13:56:05 +01001335static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 unsigned long resolution,
1337 unsigned long ticks)
1338{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001339 struct snd_timer_user *tu = timeri->callback_data;
1340 struct snd_timer_tread *r, r1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 struct timespec tstamp;
1342 int prev, append = 0;
1343
Takashi Iwai07799e72005-10-10 11:49:49 +02001344 memset(&tstamp, 0, sizeof(tstamp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 spin_lock(&tu->qlock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001346 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
1347 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 spin_unlock(&tu->qlock);
1349 return;
1350 }
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001351 if (tu->last_resolution != resolution || ticks > 0) {
1352 if (timer_tstamp_monotonic)
Thomas Gleixner26204e02014-06-11 23:59:14 +00001353 ktime_get_ts(&tstamp);
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01001354 else
1355 getnstimeofday(&tstamp);
1356 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001357 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
1358 tu->last_resolution != resolution) {
Kangjie Lue4ec8cc2016-05-03 16:44:32 -04001359 memset(&r1, 0, sizeof(r1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
1361 r1.tstamp = tstamp;
1362 r1.val = resolution;
1363 snd_timer_user_append_to_tqueue(tu, &r1);
1364 tu->last_resolution = resolution;
1365 append++;
1366 }
1367 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
1368 goto __wake;
1369 if (ticks == 0)
1370 goto __wake;
1371 if (tu->qused > 0) {
1372 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
1373 r = &tu->tqueue[prev];
1374 if (r->event == SNDRV_TIMER_EVENT_TICK) {
1375 r->tstamp = tstamp;
1376 r->val += ticks;
1377 append++;
1378 goto __wake;
1379 }
1380 }
1381 r1.event = SNDRV_TIMER_EVENT_TICK;
1382 r1.tstamp = tstamp;
1383 r1.val = ticks;
1384 snd_timer_user_append_to_tqueue(tu, &r1);
1385 append++;
1386 __wake:
1387 spin_unlock(&tu->qlock);
1388 if (append == 0)
1389 return;
1390 kill_fasync(&tu->fasync, SIGIO, POLL_IN);
1391 wake_up(&tu->qchange_sleep);
1392}
1393
1394static int snd_timer_user_open(struct inode *inode, struct file *file)
1395{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001396 struct snd_timer_user *tu;
Takashi Iwai02f48652010-04-13 11:49:04 +02001397 int err;
1398
1399 err = nonseekable_open(inode, file);
1400 if (err < 0)
1401 return err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001402
Takashi Iwaica2c0962005-09-09 14:20:23 +02001403 tu = kzalloc(sizeof(*tu), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (tu == NULL)
1405 return -ENOMEM;
1406 spin_lock_init(&tu->qlock);
1407 init_waitqueue_head(&tu->qchange_sleep);
Takashi Iwaiaf368022016-01-13 17:48:01 +01001408 mutex_init(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 tu->ticks = 1;
1410 tu->queue_size = 128;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001411 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001412 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (tu->queue == NULL) {
1414 kfree(tu);
1415 return -ENOMEM;
1416 }
1417 file->private_data = tu;
1418 return 0;
1419}
1420
1421static int snd_timer_user_release(struct inode *inode, struct file *file)
1422{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001423 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 if (file->private_data) {
1426 tu = file->private_data;
1427 file->private_data = NULL;
Takashi Iwaiaf368022016-01-13 17:48:01 +01001428 mutex_lock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (tu->timeri)
1430 snd_timer_close(tu->timeri);
Takashi Iwaiaf368022016-01-13 17:48:01 +01001431 mutex_unlock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 kfree(tu->queue);
1433 kfree(tu->tqueue);
1434 kfree(tu);
1435 }
1436 return 0;
1437}
1438
Takashi Iwai53d2f742005-11-17 13:56:05 +01001439static void snd_timer_user_zero_id(struct snd_timer_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 id->dev_class = SNDRV_TIMER_CLASS_NONE;
1442 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1443 id->card = -1;
1444 id->device = -1;
1445 id->subdevice = -1;
1446}
1447
Takashi Iwai53d2f742005-11-17 13:56:05 +01001448static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 id->dev_class = timer->tmr_class;
1451 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1452 id->card = timer->card ? timer->card->number : -1;
1453 id->device = timer->tmr_device;
1454 id->subdevice = timer->tmr_subdevice;
1455}
1456
Takashi Iwai53d2f742005-11-17 13:56:05 +01001457static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001459 struct snd_timer_id id;
1460 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 struct list_head *p;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (copy_from_user(&id, _tid, sizeof(id)))
1464 return -EFAULT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001465 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (id.dev_class < 0) { /* first item */
1467 if (list_empty(&snd_timer_list))
1468 snd_timer_user_zero_id(&id);
1469 else {
Clemens Ladisch9dfba382005-10-12 17:14:55 +02001470 timer = list_entry(snd_timer_list.next,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001471 struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 snd_timer_user_copy_id(&id, timer);
1473 }
1474 } else {
1475 switch (id.dev_class) {
1476 case SNDRV_TIMER_CLASS_GLOBAL:
1477 id.device = id.device < 0 ? 0 : id.device + 1;
1478 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001479 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1481 snd_timer_user_copy_id(&id, timer);
1482 break;
1483 }
1484 if (timer->tmr_device >= id.device) {
1485 snd_timer_user_copy_id(&id, timer);
1486 break;
1487 }
1488 }
1489 if (p == &snd_timer_list)
1490 snd_timer_user_zero_id(&id);
1491 break;
1492 case SNDRV_TIMER_CLASS_CARD:
1493 case SNDRV_TIMER_CLASS_PCM:
1494 if (id.card < 0) {
1495 id.card = 0;
1496 } else {
1497 if (id.card < 0) {
1498 id.card = 0;
1499 } else {
1500 if (id.device < 0) {
1501 id.device = 0;
1502 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001503 if (id.subdevice < 0) {
1504 id.subdevice = 0;
1505 } else {
1506 id.subdevice++;
1507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509 }
1510 }
1511 list_for_each(p, &snd_timer_list) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001512 timer = list_entry(p, struct snd_timer, device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (timer->tmr_class > id.dev_class) {
1514 snd_timer_user_copy_id(&id, timer);
1515 break;
1516 }
1517 if (timer->tmr_class < id.dev_class)
1518 continue;
1519 if (timer->card->number > id.card) {
1520 snd_timer_user_copy_id(&id, timer);
1521 break;
1522 }
1523 if (timer->card->number < id.card)
1524 continue;
1525 if (timer->tmr_device > id.device) {
1526 snd_timer_user_copy_id(&id, timer);
1527 break;
1528 }
1529 if (timer->tmr_device < id.device)
1530 continue;
1531 if (timer->tmr_subdevice > id.subdevice) {
1532 snd_timer_user_copy_id(&id, timer);
1533 break;
1534 }
1535 if (timer->tmr_subdevice < id.subdevice)
1536 continue;
1537 snd_timer_user_copy_id(&id, timer);
1538 break;
1539 }
1540 if (p == &snd_timer_list)
1541 snd_timer_user_zero_id(&id);
1542 break;
1543 default:
1544 snd_timer_user_zero_id(&id);
1545 }
1546 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001547 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (copy_to_user(_tid, &id, sizeof(*_tid)))
1549 return -EFAULT;
1550 return 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001551}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001553static int snd_timer_user_ginfo(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001554 struct snd_timer_ginfo __user *_ginfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001556 struct snd_timer_ginfo *ginfo;
1557 struct snd_timer_id tid;
1558 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 struct list_head *p;
1560 int err = 0;
1561
Li Zefanef44a1e2009-04-10 09:43:08 +08001562 ginfo = memdup_user(_ginfo, sizeof(*ginfo));
1563 if (IS_ERR(ginfo))
1564 return PTR_ERR(ginfo);
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 tid = ginfo->tid;
1567 memset(ginfo, 0, sizeof(*ginfo));
1568 ginfo->tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001569 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 t = snd_timer_find(&tid);
1571 if (t != NULL) {
1572 ginfo->card = t->card ? t->card->number : -1;
1573 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1574 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
1575 strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
1576 strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
1577 ginfo->resolution = t->hw.resolution;
1578 if (t->hw.resolution_min > 0) {
1579 ginfo->resolution_min = t->hw.resolution_min;
1580 ginfo->resolution_max = t->hw.resolution_max;
1581 }
1582 list_for_each(p, &t->open_list_head) {
1583 ginfo->clients++;
1584 }
1585 } else {
1586 err = -ENODEV;
1587 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001588 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
1590 err = -EFAULT;
1591 kfree(ginfo);
1592 return err;
1593}
1594
Takashi Sakamoto91d21782016-03-23 08:03:59 +09001595static int timer_set_gparams(struct snd_timer_gparams *gparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001597 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 int err;
1599
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001600 mutex_lock(&register_mutex);
Takashi Sakamoto91d21782016-03-23 08:03:59 +09001601 t = snd_timer_find(&gparams->tid);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001602 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 err = -ENODEV;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001604 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001606 if (!list_empty(&t->open_list_head)) {
1607 err = -EBUSY;
1608 goto _error;
1609 }
1610 if (!t->hw.set_period) {
1611 err = -ENOSYS;
1612 goto _error;
1613 }
Takashi Sakamoto91d21782016-03-23 08:03:59 +09001614 err = t->hw.set_period(t, gparams->period_num, gparams->period_den);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001615_error:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001616 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return err;
1618}
1619
Takashi Sakamoto91d21782016-03-23 08:03:59 +09001620static int snd_timer_user_gparams(struct file *file,
1621 struct snd_timer_gparams __user *_gparams)
1622{
1623 struct snd_timer_gparams gparams;
1624
1625 if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
1626 return -EFAULT;
1627 return timer_set_gparams(&gparams);
1628}
1629
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001630static int snd_timer_user_gstatus(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001631 struct snd_timer_gstatus __user *_gstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001633 struct snd_timer_gstatus gstatus;
1634 struct snd_timer_id tid;
1635 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 int err = 0;
1637
1638 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
1639 return -EFAULT;
1640 tid = gstatus.tid;
1641 memset(&gstatus, 0, sizeof(gstatus));
1642 gstatus.tid = tid;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001643 mutex_lock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 t = snd_timer_find(&tid);
1645 if (t != NULL) {
1646 if (t->hw.c_resolution)
1647 gstatus.resolution = t->hw.c_resolution(t);
1648 else
1649 gstatus.resolution = t->hw.resolution;
1650 if (t->hw.precise_resolution) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001651 t->hw.precise_resolution(t, &gstatus.resolution_num,
1652 &gstatus.resolution_den);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 } else {
1654 gstatus.resolution_num = gstatus.resolution;
1655 gstatus.resolution_den = 1000000000uL;
1656 }
1657 } else {
1658 err = -ENODEV;
1659 }
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01001660 mutex_unlock(&register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
1662 err = -EFAULT;
1663 return err;
1664}
1665
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001666static int snd_timer_user_tselect(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001667 struct snd_timer_select __user *_tselect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001669 struct snd_timer_user *tu;
1670 struct snd_timer_select tselect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 char str[32];
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001672 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 tu = file->private_data;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001675 if (tu->timeri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 snd_timer_close(tu->timeri);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001677 tu->timeri = NULL;
1678 }
1679 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
1680 err = -EFAULT;
1681 goto __err;
1682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 sprintf(str, "application %i", current->pid);
1684 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
1685 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001686 err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1687 if (err < 0)
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001688 goto __err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Takashi Iwai82ecd2f2017-06-02 17:26:56 +02001690 tu->qhead = tu->qtail = tu->qused = 0;
Jesper Juhl4d572772005-05-30 17:30:32 +02001691 kfree(tu->queue);
1692 tu->queue = NULL;
1693 kfree(tu->tqueue);
1694 tu->tqueue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001696 tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001697 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001698 if (tu->tqueue == NULL)
1699 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001701 tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001702 GFP_KERNEL);
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001703 if (tu->queue == NULL)
1704 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001706
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001707 if (err < 0) {
1708 snd_timer_close(tu->timeri);
1709 tu->timeri = NULL;
1710 } else {
1711 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001712 tu->timeri->callback = tu->tread
1713 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001714 tu->timeri->ccallback = snd_timer_user_ccallback;
1715 tu->timeri->callback_data = (void *)tu;
Takashi Iwai40ed94442016-01-21 17:43:08 +01001716 tu->timeri->disconnect = snd_timer_user_disconnect;
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001717 }
1718
1719 __err:
Jaroslav Kyselac1935b42005-04-04 16:44:58 +02001720 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001723static int snd_timer_user_info(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001724 struct snd_timer_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001726 struct snd_timer_user *tu;
1727 struct snd_timer_info *info;
1728 struct snd_timer *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 int err = 0;
1730
1731 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001732 if (!tu->timeri)
1733 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001735 if (!t)
1736 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Takashi Iwaica2c0962005-09-09 14:20:23 +02001738 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (! info)
1740 return -ENOMEM;
1741 info->card = t->card ? t->card->number : -1;
1742 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
1743 info->flags |= SNDRV_TIMER_FLG_SLAVE;
1744 strlcpy(info->id, t->id, sizeof(info->id));
1745 strlcpy(info->name, t->name, sizeof(info->name));
1746 info->resolution = t->hw.resolution;
1747 if (copy_to_user(_info, info, sizeof(*_info)))
1748 err = -EFAULT;
1749 kfree(info);
1750 return err;
1751}
1752
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001753static int snd_timer_user_params(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001754 struct snd_timer_params __user *_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001756 struct snd_timer_user *tu;
1757 struct snd_timer_params params;
1758 struct snd_timer *t;
1759 struct snd_timer_read *tr;
1760 struct snd_timer_tread *ttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 int err;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001764 if (!tu->timeri)
1765 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 t = tu->timeri->timer;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001767 if (!t)
1768 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if (copy_from_user(&params, _params, sizeof(params)))
1770 return -EFAULT;
Takashi Iwai5ac92762017-02-28 14:49:07 +01001771 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
1772 u64 resolution;
1773
1774 if (params.ticks < 1) {
1775 err = -EINVAL;
1776 goto _end;
1777 }
1778
1779 /* Don't allow resolution less than 1ms */
1780 resolution = snd_timer_resolution(tu->timeri);
1781 resolution *= params.ticks;
1782 if (resolution < 1000000) {
1783 err = -EINVAL;
1784 goto _end;
1785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001787 if (params.queue_size > 0 &&
1788 (params.queue_size < 32 || params.queue_size > 1024)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 err = -EINVAL;
1790 goto _end;
1791 }
1792 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
1793 (1<<SNDRV_TIMER_EVENT_TICK)|
1794 (1<<SNDRV_TIMER_EVENT_START)|
1795 (1<<SNDRV_TIMER_EVENT_STOP)|
1796 (1<<SNDRV_TIMER_EVENT_CONTINUE)|
1797 (1<<SNDRV_TIMER_EVENT_PAUSE)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001798 (1<<SNDRV_TIMER_EVENT_SUSPEND)|
1799 (1<<SNDRV_TIMER_EVENT_RESUME)|
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 (1<<SNDRV_TIMER_EVENT_MSTART)|
1801 (1<<SNDRV_TIMER_EVENT_MSTOP)|
1802 (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
Jaroslav Kysela65d11d92005-08-16 13:05:43 +02001803 (1<<SNDRV_TIMER_EVENT_MPAUSE)|
1804 (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
Jaroslav Kyselaa501dfa2005-08-16 11:09:05 +02001805 (1<<SNDRV_TIMER_EVENT_MRESUME))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 err = -EINVAL;
1807 goto _end;
1808 }
1809 snd_timer_stop(tu->timeri);
1810 spin_lock_irq(&t->lock);
1811 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
1812 SNDRV_TIMER_IFLG_EXCLUSIVE|
1813 SNDRV_TIMER_IFLG_EARLY_EVENT);
1814 if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
1815 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1816 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
1817 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
1818 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
1819 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
1820 spin_unlock_irq(&t->lock);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001821 if (params.queue_size > 0 &&
1822 (unsigned int)tu->queue_size != params.queue_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (tu->tread) {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001824 ttr = kmalloc(params.queue_size * sizeof(*ttr),
1825 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (ttr) {
1827 kfree(tu->tqueue);
1828 tu->queue_size = params.queue_size;
1829 tu->tqueue = ttr;
1830 }
1831 } else {
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001832 tr = kmalloc(params.queue_size * sizeof(*tr),
1833 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (tr) {
1835 kfree(tu->queue);
1836 tu->queue_size = params.queue_size;
1837 tu->queue = tr;
1838 }
1839 }
1840 }
1841 tu->qhead = tu->qtail = tu->qused = 0;
1842 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
1843 if (tu->tread) {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001844 struct snd_timer_tread tread;
Kangjie Lucec8f962016-05-03 16:44:07 -04001845 memset(&tread, 0, sizeof(tread));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 tread.event = SNDRV_TIMER_EVENT_EARLY;
1847 tread.tstamp.tv_sec = 0;
1848 tread.tstamp.tv_nsec = 0;
1849 tread.val = 0;
1850 snd_timer_user_append_to_tqueue(tu, &tread);
1851 } else {
Takashi Iwai53d2f742005-11-17 13:56:05 +01001852 struct snd_timer_read *r = &tu->queue[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 r->resolution = 0;
1854 r->ticks = 0;
1855 tu->qused++;
1856 tu->qtail++;
1857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
1859 tu->filter = params.filter;
1860 tu->ticks = params.ticks;
1861 err = 0;
1862 _end:
1863 if (copy_to_user(_params, &params, sizeof(params)))
1864 return -EFAULT;
1865 return err;
1866}
1867
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001868static int snd_timer_user_status(struct file *file,
Takashi Iwai53d2f742005-11-17 13:56:05 +01001869 struct snd_timer_status __user *_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001871 struct snd_timer_user *tu;
1872 struct snd_timer_status status;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001875 if (!tu->timeri)
1876 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 memset(&status, 0, sizeof(status));
1878 status.tstamp = tu->tstamp;
1879 status.resolution = snd_timer_resolution(tu->timeri);
1880 status.lost = tu->timeri->lost;
1881 status.overrun = tu->overrun;
1882 spin_lock_irq(&tu->qlock);
1883 status.queue = tu->qused;
1884 spin_unlock_irq(&tu->qlock);
1885 if (copy_to_user(_status, &status, sizeof(status)))
1886 return -EFAULT;
1887 return 0;
1888}
1889
1890static int snd_timer_user_start(struct file *file)
1891{
1892 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001893 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001896 if (!tu->timeri)
1897 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 snd_timer_stop(tu->timeri);
1899 tu->timeri->lost = 0;
1900 tu->last_resolution = 0;
1901 return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
1902}
1903
1904static int snd_timer_user_stop(struct file *file)
1905{
1906 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001907 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001910 if (!tu->timeri)
1911 return -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
1913}
1914
1915static int snd_timer_user_continue(struct file *file)
1916{
1917 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001918 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001921 if (!tu->timeri)
1922 return -EBADFD;
Takashi Iwai9f8a7652016-09-07 15:45:31 +02001923 /* start timer instead of continue if it's not used before */
1924 if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
1925 return snd_timer_user_start(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 tu->timeri->lost = 0;
1927 return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
1928}
1929
Takashi Iwai15790a62005-05-15 15:04:14 +02001930static int snd_timer_user_pause(struct file *file)
1931{
1932 int err;
Takashi Iwai53d2f742005-11-17 13:56:05 +01001933 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001934
Takashi Iwai15790a62005-05-15 15:04:14 +02001935 tu = file->private_data;
Clemens Ladisch7c64ec32007-07-16 11:01:30 +02001936 if (!tu->timeri)
1937 return -EBADFD;
Jaroslav Kyselad138b442005-05-16 13:51:39 +02001938 return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
Takashi Iwai15790a62005-05-15 15:04:14 +02001939}
1940
Takashi Iwai8c50b372005-05-15 15:43:54 +02001941enum {
1942 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
1943 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
1944 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
1945 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
1946};
1947
Takashi Iwaiaf368022016-01-13 17:48:01 +01001948static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001949 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Takashi Iwai53d2f742005-11-17 13:56:05 +01001951 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 void __user *argp = (void __user *)arg;
1953 int __user *p = argp;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 tu = file->private_data;
1956 switch (cmd) {
1957 case SNDRV_TIMER_IOCTL_PVERSION:
1958 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
1959 case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
1960 return snd_timer_user_next_device(argp);
1961 case SNDRV_TIMER_IOCTL_TREAD:
1962 {
1963 int xarg;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02001964
Takashi Iwaiaf368022016-01-13 17:48:01 +01001965 if (tu->timeri) /* too late */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return -EBUSY;
Takashi Iwaiaf368022016-01-13 17:48:01 +01001967 if (get_user(xarg, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return -EFAULT;
1969 tu->tread = xarg ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return 0;
1971 }
1972 case SNDRV_TIMER_IOCTL_GINFO:
1973 return snd_timer_user_ginfo(file, argp);
1974 case SNDRV_TIMER_IOCTL_GPARAMS:
1975 return snd_timer_user_gparams(file, argp);
1976 case SNDRV_TIMER_IOCTL_GSTATUS:
1977 return snd_timer_user_gstatus(file, argp);
1978 case SNDRV_TIMER_IOCTL_SELECT:
1979 return snd_timer_user_tselect(file, argp);
1980 case SNDRV_TIMER_IOCTL_INFO:
1981 return snd_timer_user_info(file, argp);
1982 case SNDRV_TIMER_IOCTL_PARAMS:
1983 return snd_timer_user_params(file, argp);
1984 case SNDRV_TIMER_IOCTL_STATUS:
1985 return snd_timer_user_status(file, argp);
1986 case SNDRV_TIMER_IOCTL_START:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001987 case SNDRV_TIMER_IOCTL_START_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return snd_timer_user_start(file);
1989 case SNDRV_TIMER_IOCTL_STOP:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001990 case SNDRV_TIMER_IOCTL_STOP_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return snd_timer_user_stop(file);
1992 case SNDRV_TIMER_IOCTL_CONTINUE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001993 case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return snd_timer_user_continue(file);
Takashi Iwai15790a62005-05-15 15:04:14 +02001995 case SNDRV_TIMER_IOCTL_PAUSE:
Takashi Iwai8c50b372005-05-15 15:43:54 +02001996 case SNDRV_TIMER_IOCTL_PAUSE_OLD:
Takashi Iwai15790a62005-05-15 15:04:14 +02001997 return snd_timer_user_pause(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
1999 return -ENOTTY;
2000}
2001
Takashi Iwaiaf368022016-01-13 17:48:01 +01002002static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
2003 unsigned long arg)
2004{
2005 struct snd_timer_user *tu = file->private_data;
2006 long ret;
2007
2008 mutex_lock(&tu->ioctl_lock);
2009 ret = __snd_timer_user_ioctl(file, cmd, arg);
2010 mutex_unlock(&tu->ioctl_lock);
2011 return ret;
2012}
2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014static int snd_timer_user_fasync(int fd, struct file * file, int on)
2015{
Takashi Iwai53d2f742005-11-17 13:56:05 +01002016 struct snd_timer_user *tu;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 tu = file->private_data;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07002019 return fasync_helper(fd, file, on, &tu->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002022static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
2023 size_t count, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
Takashi Iwai53d2f742005-11-17 13:56:05 +01002025 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 long result = 0, unit;
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002027 int qhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 int err = 0;
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 tu = file->private_data;
Takashi Iwai53d2f742005-11-17 13:56:05 +01002031 unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
Takashi Iwai66e982d2017-06-02 15:03:38 +02002032 mutex_lock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 spin_lock_irq(&tu->qlock);
2034 while ((long)count - result >= unit) {
2035 while (!tu->qused) {
2036 wait_queue_t wait;
2037
2038 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
2039 err = -EAGAIN;
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002040 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 }
2042
2043 set_current_state(TASK_INTERRUPTIBLE);
2044 init_waitqueue_entry(&wait, current);
2045 add_wait_queue(&tu->qchange_sleep, &wait);
2046
2047 spin_unlock_irq(&tu->qlock);
Takashi Iwai66e982d2017-06-02 15:03:38 +02002048 mutex_unlock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 schedule();
Takashi Iwai66e982d2017-06-02 15:03:38 +02002050 mutex_lock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 spin_lock_irq(&tu->qlock);
2052
2053 remove_wait_queue(&tu->qchange_sleep, &wait);
2054
Takashi Iwai230323d2016-01-21 17:19:31 +01002055 if (tu->disconnected) {
2056 err = -ENODEV;
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002057 goto _error;
Takashi Iwai230323d2016-01-21 17:19:31 +01002058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (signal_pending(current)) {
2060 err = -ERESTARTSYS;
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002061 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
2063 }
2064
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002065 qhead = tu->qhead++;
2066 tu->qhead %= tu->queue_size;
Takashi Iwai3fa69932016-07-04 14:02:15 +02002067 tu->qused--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 spin_unlock_irq(&tu->qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 if (tu->tread) {
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002071 if (copy_to_user(buffer, &tu->tqueue[qhead],
2072 sizeof(struct snd_timer_tread)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 } else {
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002075 if (copy_to_user(buffer, &tu->queue[qhead],
2076 sizeof(struct snd_timer_read)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 spin_lock_irq(&tu->qlock);
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002081 if (err < 0)
2082 goto _error;
2083 result += unit;
2084 buffer += unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 _error:
Takashi Iwai4dff5c72016-02-08 17:26:58 +01002087 spin_unlock_irq(&tu->qlock);
Takashi Iwai66e982d2017-06-02 15:03:38 +02002088 mutex_unlock(&tu->ioctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return result > 0 ? result : err;
2090}
2091
2092static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
2093{
2094 unsigned int mask;
Takashi Iwai53d2f742005-11-17 13:56:05 +01002095 struct snd_timer_user *tu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 tu = file->private_data;
2098
2099 poll_wait(file, &tu->qchange_sleep, wait);
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 mask = 0;
2102 if (tu->qused)
2103 mask |= POLLIN | POLLRDNORM;
Takashi Iwai230323d2016-01-21 17:19:31 +01002104 if (tu->disconnected)
2105 mask |= POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 return mask;
2108}
2109
2110#ifdef CONFIG_COMPAT
2111#include "timer_compat.c"
2112#else
2113#define snd_timer_user_ioctl_compat NULL
2114#endif
2115
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08002116static const struct file_operations snd_timer_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
2118 .owner = THIS_MODULE,
2119 .read = snd_timer_user_read,
2120 .open = snd_timer_user_open,
2121 .release = snd_timer_user_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02002122 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 .poll = snd_timer_user_poll,
2124 .unlocked_ioctl = snd_timer_user_ioctl,
2125 .compat_ioctl = snd_timer_user_ioctl_compat,
2126 .fasync = snd_timer_user_fasync,
2127};
2128
Takashi Iwai7c358602015-01-29 18:09:05 +01002129/* unregister the system timer */
2130static void snd_timer_free_all(void)
2131{
2132 struct snd_timer *timer, *n;
2133
2134 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2135 snd_timer_free(timer);
2136}
2137
Takashi Iwai89da0612015-01-29 18:12:26 +01002138static struct device timer_dev;
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140/*
2141 * ENTRY functions
2142 */
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144static int __init alsa_timer_init(void)
2145{
2146 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Takashi Iwai89da0612015-01-29 18:12:26 +01002148 snd_device_initialize(&timer_dev, NULL);
2149 dev_set_name(&timer_dev, "timer");
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151#ifdef SNDRV_OSS_INFO_DEV_TIMERS
Clemens Ladisch6b172a82005-10-12 17:20:21 +02002152 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
2153 "system timer");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154#endif
Takashi Iwaie28563c2005-12-01 10:42:42 +01002155
Takashi Iwai7c358602015-01-29 18:09:05 +01002156 err = snd_timer_register_system();
2157 if (err < 0) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +01002158 pr_err("ALSA: unable to register system timer (%i)\n", err);
Takashi Iwai89da0612015-01-29 18:12:26 +01002159 put_device(&timer_dev);
Takashi Iwai7c358602015-01-29 18:09:05 +01002160 return err;
2161 }
2162
Takashi Iwai40a4b262015-01-30 08:34:58 +01002163 err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
2164 &snd_timer_f_ops, NULL, &timer_dev);
Takashi Iwai7c358602015-01-29 18:09:05 +01002165 if (err < 0) {
Takashi Iwaicf74dcf2014-02-04 18:22:39 +01002166 pr_err("ALSA: unable to register timer device (%i)\n", err);
Takashi Iwai7c358602015-01-29 18:09:05 +01002167 snd_timer_free_all();
Takashi Iwai89da0612015-01-29 18:12:26 +01002168 put_device(&timer_dev);
Takashi Iwai7c358602015-01-29 18:09:05 +01002169 return err;
2170 }
2171
Takashi Iwaie28563c2005-12-01 10:42:42 +01002172 snd_timer_proc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return 0;
2174}
2175
2176static void __exit alsa_timer_exit(void)
2177{
Takashi Iwai40a4b262015-01-30 08:34:58 +01002178 snd_unregister_device(&timer_dev);
Takashi Iwai7c358602015-01-29 18:09:05 +01002179 snd_timer_free_all();
Takashi Iwai89da0612015-01-29 18:12:26 +01002180 put_device(&timer_dev);
Takashi Iwaie28563c2005-12-01 10:42:42 +01002181 snd_timer_proc_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182#ifdef SNDRV_OSS_INFO_DEV_TIMERS
2183 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
2184#endif
2185}
2186
2187module_init(alsa_timer_init)
2188module_exit(alsa_timer_exit)