Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Timers abstract layer |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/delay.h> |
| 23 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/slab.h> |
| 25 | #include <linux/time.h> |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 26 | #include <linux/mutex.h> |
Paul Gortmaker | 51990e8 | 2012-01-22 11:23:42 -0500 | [diff] [blame] | 27 | #include <linux/device.h> |
Paul Gortmaker | 65a7721 | 2011-07-15 13:13:37 -0400 | [diff] [blame] | 28 | #include <linux/module.h> |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 29 | #include <linux/string.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 30 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <sound/core.h> |
| 32 | #include <sound/timer.h> |
| 33 | #include <sound/control.h> |
| 34 | #include <sound/info.h> |
| 35 | #include <sound/minors.h> |
| 36 | #include <sound/initval.h> |
| 37 | #include <linux/kmod.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Takashi Iwai | 9f8a765 | 2016-09-07 15:45:31 +0200 | [diff] [blame] | 39 | /* internal flags */ |
| 40 | #define SNDRV_TIMER_IFLG_PAUSED 0x00010000 |
| 41 | |
Takashi Iwai | 8eeaa2f | 2014-02-10 09:48:47 +0100 | [diff] [blame] | 42 | #if IS_ENABLED(CONFIG_SND_HRTIMER) |
Clemens Ladisch | 109fef9 | 2010-11-18 09:53:54 +0100 | [diff] [blame] | 43 | #define DEFAULT_TIMER_LIMIT 4 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #else |
| 45 | #define DEFAULT_TIMER_LIMIT 1 |
| 46 | #endif |
| 47 | |
| 48 | static int timer_limit = DEFAULT_TIMER_LIMIT; |
Jaroslav Kysela | b751eef | 2007-12-13 10:19:42 +0100 | [diff] [blame] | 49 | static int timer_tstamp_monotonic = 1; |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 50 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | MODULE_DESCRIPTION("ALSA timer interface"); |
| 52 | MODULE_LICENSE("GPL"); |
| 53 | module_param(timer_limit, int, 0444); |
| 54 | MODULE_PARM_DESC(timer_limit, "Maximum global timers in system."); |
Jaroslav Kysela | b751eef | 2007-12-13 10:19:42 +0100 | [diff] [blame] | 55 | module_param(timer_tstamp_monotonic, int, 0444); |
| 56 | MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default)."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Kay Sievers | 03cfe6f | 2010-11-23 17:43:19 +0100 | [diff] [blame] | 58 | MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER); |
| 59 | MODULE_ALIAS("devname:snd/timer"); |
| 60 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 61 | struct snd_timer_user { |
| 62 | struct snd_timer_instance *timeri; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 63 | int tread; /* enhanced read with timestamps and events */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | unsigned long ticks; |
| 65 | unsigned long overrun; |
| 66 | int qhead; |
| 67 | int qtail; |
| 68 | int qused; |
| 69 | int queue_size; |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 70 | bool disconnected; |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 71 | struct snd_timer_read *queue; |
| 72 | struct snd_timer_tread *tqueue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | spinlock_t qlock; |
| 74 | unsigned long last_resolution; |
| 75 | unsigned int filter; |
| 76 | struct timespec tstamp; /* trigger tstamp */ |
| 77 | wait_queue_head_t qchange_sleep; |
| 78 | struct fasync_struct *fasync; |
Takashi Iwai | af36802 | 2016-01-13 17:48:01 +0100 | [diff] [blame] | 79 | struct mutex ioctl_lock; |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 80 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | /* list of timers */ |
| 83 | static LIST_HEAD(snd_timer_list); |
| 84 | |
| 85 | /* list of slave instances */ |
| 86 | static LIST_HEAD(snd_timer_slave_list); |
| 87 | |
| 88 | /* lock for slave active lists */ |
| 89 | static DEFINE_SPINLOCK(slave_active_lock); |
| 90 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 91 | static DEFINE_MUTEX(register_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 93 | static int snd_timer_free(struct snd_timer *timer); |
| 94 | static int snd_timer_dev_free(struct snd_device *device); |
| 95 | static int snd_timer_dev_register(struct snd_device *device); |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 96 | static int snd_timer_dev_disconnect(struct snd_device *device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 98 | static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * create a timer instance with the given owner string. |
| 102 | * when timer is not NULL, increments the module counter |
| 103 | */ |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 104 | static struct snd_timer_instance *snd_timer_instance_new(char *owner, |
| 105 | struct snd_timer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 107 | struct snd_timer_instance *timeri; |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 108 | timeri = kzalloc(sizeof(*timeri), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | if (timeri == NULL) |
| 110 | return NULL; |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 111 | timeri->owner = kstrdup(owner, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | if (! timeri->owner) { |
| 113 | kfree(timeri); |
| 114 | return NULL; |
| 115 | } |
| 116 | INIT_LIST_HEAD(&timeri->open_list); |
| 117 | INIT_LIST_HEAD(&timeri->active_list); |
| 118 | INIT_LIST_HEAD(&timeri->ack_list); |
| 119 | INIT_LIST_HEAD(&timeri->slave_list_head); |
| 120 | INIT_LIST_HEAD(&timeri->slave_active_head); |
| 121 | |
| 122 | timeri->timer = timer; |
Clemens Ladisch | de24214 | 2005-10-12 17:12:31 +0200 | [diff] [blame] | 123 | if (timer && !try_module_get(timer->module)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | kfree(timeri->owner); |
| 125 | kfree(timeri); |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | return timeri; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * find a timer instance from the given timer id |
| 134 | */ |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 135 | static struct snd_timer *snd_timer_find(struct snd_timer_id *tid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 137 | struct snd_timer *timer = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 139 | list_for_each_entry(timer, &snd_timer_list, device_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | if (timer->tmr_class != tid->dev_class) |
| 141 | continue; |
| 142 | if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD || |
| 143 | timer->tmr_class == SNDRV_TIMER_CLASS_PCM) && |
| 144 | (timer->card == NULL || |
| 145 | timer->card->number != tid->card)) |
| 146 | continue; |
| 147 | if (timer->tmr_device != tid->device) |
| 148 | continue; |
| 149 | if (timer->tmr_subdevice != tid->subdevice) |
| 150 | continue; |
| 151 | return timer; |
| 152 | } |
| 153 | return NULL; |
| 154 | } |
| 155 | |
Johannes Berg | ee2da99 | 2008-07-09 10:28:41 +0200 | [diff] [blame] | 156 | #ifdef CONFIG_MODULES |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 158 | static void snd_timer_request(struct snd_timer_id *tid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | switch (tid->dev_class) { |
| 161 | case SNDRV_TIMER_CLASS_GLOBAL: |
| 162 | if (tid->device < timer_limit) |
| 163 | request_module("snd-timer-%i", tid->device); |
| 164 | break; |
| 165 | case SNDRV_TIMER_CLASS_CARD: |
| 166 | case SNDRV_TIMER_CLASS_PCM: |
| 167 | if (tid->card < snd_ecards_limit) |
| 168 | request_module("snd-card-%i", tid->card); |
| 169 | break; |
| 170 | default: |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | #endif |
| 176 | |
| 177 | /* |
| 178 | * look for a master instance matching with the slave id of the given slave. |
| 179 | * when found, relink the open_link of the slave. |
| 180 | * |
| 181 | * call this with register_mutex down. |
| 182 | */ |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 183 | static void snd_timer_check_slave(struct snd_timer_instance *slave) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 185 | struct snd_timer *timer; |
| 186 | struct snd_timer_instance *master; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
| 188 | /* FIXME: it's really dumb to look up all entries.. */ |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 189 | list_for_each_entry(timer, &snd_timer_list, device_list) { |
| 190 | list_for_each_entry(master, &timer->open_list_head, open_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | if (slave->slave_class == master->slave_class && |
| 192 | slave->slave_id == master->slave_id) { |
Nicolas Kaiser | 5b7c757 | 2011-03-16 17:10:11 +0100 | [diff] [blame] | 193 | list_move_tail(&slave->open_list, |
| 194 | &master->slave_list_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | spin_lock_irq(&slave_active_lock); |
| 196 | slave->master = master; |
| 197 | slave->timer = master->timer; |
| 198 | spin_unlock_irq(&slave_active_lock); |
| 199 | return; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * look for slave instances matching with the slave id of the given master. |
| 207 | * when found, relink the open_link of slaves. |
| 208 | * |
| 209 | * call this with register_mutex down. |
| 210 | */ |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 211 | static void snd_timer_check_master(struct snd_timer_instance *master) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 213 | struct snd_timer_instance *slave, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | /* check all pending slaves */ |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 216 | list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | if (slave->slave_class == master->slave_class && |
| 218 | slave->slave_id == master->slave_id) { |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 219 | list_move_tail(&slave->open_list, &master->slave_list_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | spin_lock_irq(&slave_active_lock); |
Takashi Iwai | b5a663a | 2016-01-14 16:30:58 +0100 | [diff] [blame] | 221 | spin_lock(&master->timer->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | slave->master = master; |
| 223 | slave->timer = master->timer; |
| 224 | if (slave->flags & SNDRV_TIMER_IFLG_RUNNING) |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 225 | list_add_tail(&slave->active_list, |
| 226 | &master->slave_active_head); |
Takashi Iwai | b5a663a | 2016-01-14 16:30:58 +0100 | [diff] [blame] | 227 | spin_unlock(&master->timer->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | spin_unlock_irq(&slave_active_lock); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * open a timer instance |
| 235 | * when opening a master, the slave id must be here given. |
| 236 | */ |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 237 | int snd_timer_open(struct snd_timer_instance **ti, |
| 238 | char *owner, struct snd_timer_id *tid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | unsigned int slave_id) |
| 240 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 241 | struct snd_timer *timer; |
| 242 | struct snd_timer_instance *timeri = NULL; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) { |
| 245 | /* open a slave instance */ |
| 246 | if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE || |
| 247 | tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) { |
Takashi Iwai | cf74dcf | 2014-02-04 18:22:39 +0100 | [diff] [blame] | 248 | pr_debug("ALSA: timer: invalid slave class %i\n", |
| 249 | tid->dev_sclass); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | return -EINVAL; |
| 251 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 252 | mutex_lock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | timeri = snd_timer_instance_new(owner, NULL); |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 254 | if (!timeri) { |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 255 | mutex_unlock(®ister_mutex); |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 256 | return -ENOMEM; |
| 257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | timeri->slave_class = tid->dev_sclass; |
| 259 | timeri->slave_id = tid->device; |
| 260 | timeri->flags |= SNDRV_TIMER_IFLG_SLAVE; |
| 261 | list_add_tail(&timeri->open_list, &snd_timer_slave_list); |
| 262 | snd_timer_check_slave(timeri); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 263 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | *ti = timeri; |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | /* open a master instance */ |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 269 | mutex_lock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | timer = snd_timer_find(tid); |
Johannes Berg | ee2da99 | 2008-07-09 10:28:41 +0200 | [diff] [blame] | 271 | #ifdef CONFIG_MODULES |
| 272 | if (!timer) { |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 273 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | snd_timer_request(tid); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 275 | mutex_lock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | timer = snd_timer_find(tid); |
| 277 | } |
| 278 | #endif |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 279 | if (!timer) { |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 280 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | return -ENODEV; |
| 282 | } |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 283 | if (!list_empty(&timer->open_list_head)) { |
| 284 | timeri = list_entry(timer->open_list_head.next, |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 285 | struct snd_timer_instance, open_list); |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 286 | if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) { |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 287 | mutex_unlock(®ister_mutex); |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 288 | return -EBUSY; |
| 289 | } |
| 290 | } |
| 291 | timeri = snd_timer_instance_new(owner, timer); |
| 292 | if (!timeri) { |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 293 | mutex_unlock(®ister_mutex); |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 294 | return -ENOMEM; |
| 295 | } |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 296 | /* take a card refcount for safe disconnection */ |
| 297 | if (timer->card) |
| 298 | get_device(&timer->card->card_dev); |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 299 | timeri->slave_class = tid->dev_sclass; |
| 300 | timeri->slave_id = slave_id; |
Vegard Nossum | 8ddc056 | 2016-08-29 00:33:51 +0200 | [diff] [blame] | 301 | |
| 302 | if (list_empty(&timer->open_list_head) && timer->hw.open) { |
| 303 | int err = timer->hw.open(timer); |
| 304 | if (err) { |
| 305 | kfree(timeri->owner); |
| 306 | kfree(timeri); |
| 307 | |
| 308 | if (timer->card) |
| 309 | put_device(&timer->card->card_dev); |
| 310 | module_put(timer->module); |
| 311 | mutex_unlock(®ister_mutex); |
| 312 | return err; |
| 313 | } |
| 314 | } |
| 315 | |
Clemens Ladisch | 2fd43d1 | 2005-10-12 17:10:35 +0200 | [diff] [blame] | 316 | list_add_tail(&timeri->open_list, &timer->open_list_head); |
| 317 | snd_timer_check_master(timeri); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 318 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | *ti = timeri; |
| 320 | return 0; |
| 321 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 322 | EXPORT_SYMBOL(snd_timer_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | /* |
| 325 | * close a timer instance |
| 326 | */ |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 327 | int snd_timer_close(struct snd_timer_instance *timeri) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 329 | struct snd_timer *timer = NULL; |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 330 | struct snd_timer_instance *slave, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
Takashi Iwai | 0072889 | 2008-08-14 07:51:57 +0200 | [diff] [blame] | 332 | if (snd_BUG_ON(!timeri)) |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 333 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Takashi Iwai | 9984d1b | 2016-02-10 11:53:30 +0100 | [diff] [blame] | 335 | mutex_lock(®ister_mutex); |
| 336 | list_del(&timeri->open_list); |
| 337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | /* force to stop the timer */ |
| 339 | snd_timer_stop(timeri); |
| 340 | |
Takashi Iwai | 9984d1b | 2016-02-10 11:53:30 +0100 | [diff] [blame] | 341 | timer = timeri->timer; |
| 342 | if (timer) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | /* wait, until the active callback is finished */ |
| 344 | spin_lock_irq(&timer->lock); |
| 345 | while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) { |
| 346 | spin_unlock_irq(&timer->lock); |
| 347 | udelay(10); |
| 348 | spin_lock_irq(&timer->lock); |
| 349 | } |
| 350 | spin_unlock_irq(&timer->lock); |
Takashi Iwai | 9984d1b | 2016-02-10 11:53:30 +0100 | [diff] [blame] | 351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | /* remove slave links */ |
Takashi Iwai | b5a663a | 2016-01-14 16:30:58 +0100 | [diff] [blame] | 353 | spin_lock_irq(&slave_active_lock); |
| 354 | spin_lock(&timer->lock); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 355 | list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head, |
| 356 | open_list) { |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 357 | list_move_tail(&slave->open_list, &snd_timer_slave_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | slave->master = NULL; |
| 359 | slave->timer = NULL; |
Takashi Iwai | b5a663a | 2016-01-14 16:30:58 +0100 | [diff] [blame] | 360 | list_del_init(&slave->ack_list); |
| 361 | list_del_init(&slave->active_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } |
Takashi Iwai | b5a663a | 2016-01-14 16:30:58 +0100 | [diff] [blame] | 363 | spin_unlock(&timer->lock); |
| 364 | spin_unlock_irq(&slave_active_lock); |
Takashi Iwai | 9984d1b | 2016-02-10 11:53:30 +0100 | [diff] [blame] | 365 | |
| 366 | /* slave doesn't need to release timer resources below */ |
| 367 | if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) |
| 368 | timer = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
Takashi Iwai | 9984d1b | 2016-02-10 11:53:30 +0100 | [diff] [blame] | 370 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | if (timeri->private_free) |
| 372 | timeri->private_free(timeri); |
| 373 | kfree(timeri->owner); |
| 374 | kfree(timeri); |
Takashi Iwai | 9984d1b | 2016-02-10 11:53:30 +0100 | [diff] [blame] | 375 | |
| 376 | if (timer) { |
| 377 | if (list_empty(&timer->open_list_head) && timer->hw.close) |
| 378 | timer->hw.close(timer); |
| 379 | /* release a card refcount for safe disconnection */ |
| 380 | if (timer->card) |
| 381 | put_device(&timer->card->card_dev); |
Clemens Ladisch | de24214 | 2005-10-12 17:12:31 +0200 | [diff] [blame] | 382 | module_put(timer->module); |
Takashi Iwai | 9984d1b | 2016-02-10 11:53:30 +0100 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | return 0; |
| 387 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 388 | EXPORT_SYMBOL(snd_timer_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 390 | unsigned long snd_timer_resolution(struct snd_timer_instance *timeri) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 392 | struct snd_timer * timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
| 394 | if (timeri == NULL) |
| 395 | return 0; |
Markus Elfring | dd1f7ab | 2017-08-23 09:45:06 +0200 | [diff] [blame] | 396 | timer = timeri->timer; |
| 397 | if (timer) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | if (timer->hw.c_resolution) |
| 399 | return timer->hw.c_resolution(timer); |
| 400 | return timer->hw.resolution; |
| 401 | } |
| 402 | return 0; |
| 403 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 404 | EXPORT_SYMBOL(snd_timer_resolution); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 406 | static void snd_timer_notify1(struct snd_timer_instance *ti, int event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 408 | struct snd_timer *timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | unsigned long resolution = 0; |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 410 | struct snd_timer_instance *ts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | struct timespec tstamp; |
| 412 | |
Jaroslav Kysela | b751eef | 2007-12-13 10:19:42 +0100 | [diff] [blame] | 413 | if (timer_tstamp_monotonic) |
Thomas Gleixner | 26204e0 | 2014-06-11 23:59:14 +0000 | [diff] [blame] | 414 | ktime_get_ts(&tstamp); |
Jaroslav Kysela | b751eef | 2007-12-13 10:19:42 +0100 | [diff] [blame] | 415 | else |
| 416 | getnstimeofday(&tstamp); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 417 | if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START || |
| 418 | event > SNDRV_TIMER_EVENT_PAUSE)) |
| 419 | return; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 420 | if (event == SNDRV_TIMER_EVENT_START || |
| 421 | event == SNDRV_TIMER_EVENT_CONTINUE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | resolution = snd_timer_resolution(ti); |
| 423 | if (ti->ccallback) |
Jaroslav Kysela | b30477d5 | 2010-03-03 11:05:55 +0100 | [diff] [blame] | 424 | ti->ccallback(ti, event, &tstamp, resolution); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | if (ti->flags & SNDRV_TIMER_IFLG_SLAVE) |
| 426 | return; |
| 427 | timer = ti->timer; |
| 428 | if (timer == NULL) |
| 429 | return; |
| 430 | if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE) |
| 431 | return; |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 432 | list_for_each_entry(ts, &ti->slave_active_head, active_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | if (ts->ccallback) |
Takashi Iwai | 117159f | 2016-02-08 17:36:25 +0100 | [diff] [blame] | 434 | ts->ccallback(ts, event + 100, &tstamp, resolution); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 437 | /* start/continue a master timer */ |
| 438 | static int snd_timer_start1(struct snd_timer_instance *timeri, |
| 439 | bool start, unsigned long ticks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 441 | struct snd_timer *timer; |
| 442 | int result; |
| 443 | unsigned long flags; |
| 444 | |
| 445 | timer = timeri->timer; |
| 446 | if (!timer) |
| 447 | return -EINVAL; |
| 448 | |
| 449 | spin_lock_irqsave(&timer->lock, flags); |
| 450 | if (timer->card && timer->card->shutdown) { |
| 451 | result = -ENODEV; |
| 452 | goto unlock; |
| 453 | } |
| 454 | if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING | |
| 455 | SNDRV_TIMER_IFLG_START)) { |
| 456 | result = -EBUSY; |
| 457 | goto unlock; |
| 458 | } |
| 459 | |
| 460 | if (start) |
| 461 | timeri->ticks = timeri->cticks = ticks; |
| 462 | else if (!timeri->cticks) |
| 463 | timeri->cticks = 1; |
| 464 | timeri->pticks = 0; |
| 465 | |
Nicolas Kaiser | 5b7c757 | 2011-03-16 17:10:11 +0100 | [diff] [blame] | 466 | list_move_tail(&timeri->active_list, &timer->active_list_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | if (timer->running) { |
| 468 | if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE) |
| 469 | goto __start_now; |
| 470 | timer->flags |= SNDRV_TIMER_FLG_RESCHED; |
| 471 | timeri->flags |= SNDRV_TIMER_IFLG_START; |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 472 | result = 1; /* delayed start */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } else { |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 474 | if (start) |
| 475 | timer->sticks = ticks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | timer->hw.start(timer); |
| 477 | __start_now: |
| 478 | timer->running++; |
| 479 | timeri->flags |= SNDRV_TIMER_IFLG_RUNNING; |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 480 | result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 482 | snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START : |
| 483 | SNDRV_TIMER_EVENT_CONTINUE); |
| 484 | unlock: |
| 485 | spin_unlock_irqrestore(&timer->lock, flags); |
| 486 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 489 | /* start/continue a slave timer */ |
| 490 | static int snd_timer_start_slave(struct snd_timer_instance *timeri, |
| 491 | bool start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | { |
| 493 | unsigned long flags; |
| 494 | |
| 495 | spin_lock_irqsave(&slave_active_lock, flags); |
Takashi Iwai | f784beb | 2016-01-30 23:09:08 +0100 | [diff] [blame] | 496 | if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) { |
| 497 | spin_unlock_irqrestore(&slave_active_lock, flags); |
| 498 | return -EBUSY; |
| 499 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | timeri->flags |= SNDRV_TIMER_IFLG_RUNNING; |
Takashi Iwai | b5a663a | 2016-01-14 16:30:58 +0100 | [diff] [blame] | 501 | if (timeri->master && timeri->timer) { |
| 502 | spin_lock(&timeri->timer->lock); |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 503 | list_add_tail(&timeri->active_list, |
| 504 | &timeri->master->slave_active_head); |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 505 | snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START : |
| 506 | SNDRV_TIMER_EVENT_CONTINUE); |
Takashi Iwai | b5a663a | 2016-01-14 16:30:58 +0100 | [diff] [blame] | 507 | spin_unlock(&timeri->timer->lock); |
| 508 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | spin_unlock_irqrestore(&slave_active_lock, flags); |
| 510 | return 1; /* delayed start */ |
| 511 | } |
| 512 | |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 513 | /* stop/pause a master timer */ |
| 514 | static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 516 | struct snd_timer *timer; |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 517 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | unsigned long flags; |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | timer = timeri->timer; |
| 521 | if (!timer) |
| 522 | return -EINVAL; |
| 523 | spin_lock_irqsave(&timer->lock, flags); |
Takashi Iwai | f784beb | 2016-01-30 23:09:08 +0100 | [diff] [blame] | 524 | if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING | |
| 525 | SNDRV_TIMER_IFLG_START))) { |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 526 | result = -EBUSY; |
| 527 | goto unlock; |
Takashi Iwai | f784beb | 2016-01-30 23:09:08 +0100 | [diff] [blame] | 528 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | list_del_init(&timeri->ack_list); |
| 530 | list_del_init(&timeri->active_list); |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 531 | if (timer->card && timer->card->shutdown) |
| 532 | goto unlock; |
| 533 | if (stop) { |
| 534 | timeri->cticks = timeri->ticks; |
| 535 | timeri->pticks = 0; |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 536 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) && |
| 538 | !(--timer->running)) { |
| 539 | timer->hw.stop(timer); |
| 540 | if (timer->flags & SNDRV_TIMER_FLG_RESCHED) { |
| 541 | timer->flags &= ~SNDRV_TIMER_FLG_RESCHED; |
| 542 | snd_timer_reschedule(timer, 0); |
| 543 | if (timer->flags & SNDRV_TIMER_FLG_CHANGE) { |
| 544 | timer->flags &= ~SNDRV_TIMER_FLG_CHANGE; |
| 545 | timer->hw.start(timer); |
| 546 | } |
| 547 | } |
| 548 | } |
Takashi Iwai | c3b1681 | 2016-01-14 17:01:46 +0100 | [diff] [blame] | 549 | timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START); |
Takashi Iwai | 9f8a765 | 2016-09-07 15:45:31 +0200 | [diff] [blame] | 550 | if (stop) |
| 551 | timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED; |
| 552 | else |
| 553 | timeri->flags |= SNDRV_TIMER_IFLG_PAUSED; |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 554 | snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP : |
| 555 | SNDRV_TIMER_EVENT_CONTINUE); |
| 556 | unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | spin_unlock_irqrestore(&timer->lock, flags); |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 558 | return result; |
| 559 | } |
| 560 | |
| 561 | /* stop/pause a slave timer */ |
| 562 | static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop) |
| 563 | { |
| 564 | unsigned long flags; |
| 565 | |
| 566 | spin_lock_irqsave(&slave_active_lock, flags); |
| 567 | if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) { |
| 568 | spin_unlock_irqrestore(&slave_active_lock, flags); |
| 569 | return -EBUSY; |
| 570 | } |
| 571 | timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING; |
| 572 | if (timeri->timer) { |
| 573 | spin_lock(&timeri->timer->lock); |
| 574 | list_del_init(&timeri->ack_list); |
| 575 | list_del_init(&timeri->active_list); |
| 576 | snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP : |
| 577 | SNDRV_TIMER_EVENT_CONTINUE); |
| 578 | spin_unlock(&timeri->timer->lock); |
| 579 | } |
| 580 | spin_unlock_irqrestore(&slave_active_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | /* |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 585 | * start the timer instance |
| 586 | */ |
| 587 | int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks) |
| 588 | { |
| 589 | if (timeri == NULL || ticks < 1) |
| 590 | return -EINVAL; |
| 591 | if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) |
| 592 | return snd_timer_start_slave(timeri, true); |
| 593 | else |
| 594 | return snd_timer_start1(timeri, true, ticks); |
| 595 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 596 | EXPORT_SYMBOL(snd_timer_start); |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 597 | |
| 598 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | * stop the timer instance. |
| 600 | * |
| 601 | * do not call this from the timer callback! |
| 602 | */ |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 603 | int snd_timer_stop(struct snd_timer_instance *timeri) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | { |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 605 | if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) |
| 606 | return snd_timer_stop_slave(timeri, true); |
| 607 | else |
| 608 | return snd_timer_stop1(timeri, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 610 | EXPORT_SYMBOL(snd_timer_stop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
| 612 | /* |
| 613 | * start again.. the tick is kept. |
| 614 | */ |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 615 | int snd_timer_continue(struct snd_timer_instance *timeri) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | { |
Takashi Iwai | 9f8a765 | 2016-09-07 15:45:31 +0200 | [diff] [blame] | 617 | /* timer can continue only after pause */ |
| 618 | if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED)) |
| 619 | return -EINVAL; |
| 620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 622 | return snd_timer_start_slave(timeri, false); |
| 623 | else |
| 624 | return snd_timer_start1(timeri, false, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 626 | EXPORT_SYMBOL(snd_timer_continue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
| 628 | /* |
| 629 | * pause.. remember the ticks left |
| 630 | */ |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 631 | int snd_timer_pause(struct snd_timer_instance * timeri) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | { |
Takashi Iwai | f65e0d2 | 2016-02-10 12:47:03 +0100 | [diff] [blame] | 633 | if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) |
| 634 | return snd_timer_stop_slave(timeri, false); |
| 635 | else |
| 636 | return snd_timer_stop1(timeri, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 638 | EXPORT_SYMBOL(snd_timer_pause); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
| 640 | /* |
| 641 | * reschedule the timer |
| 642 | * |
| 643 | * start pending instances and check the scheduling ticks. |
| 644 | * when the scheduling ticks is changed set CHANGE flag to reprogram the timer. |
| 645 | */ |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 646 | static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 648 | struct snd_timer_instance *ti; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | unsigned long ticks = ~0UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 651 | list_for_each_entry(ti, &timer->active_list_head, active_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | if (ti->flags & SNDRV_TIMER_IFLG_START) { |
| 653 | ti->flags &= ~SNDRV_TIMER_IFLG_START; |
| 654 | ti->flags |= SNDRV_TIMER_IFLG_RUNNING; |
| 655 | timer->running++; |
| 656 | } |
| 657 | if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) { |
| 658 | if (ticks > ti->cticks) |
| 659 | ticks = ti->cticks; |
| 660 | } |
| 661 | } |
| 662 | if (ticks == ~0UL) { |
| 663 | timer->flags &= ~SNDRV_TIMER_FLG_RESCHED; |
| 664 | return; |
| 665 | } |
| 666 | if (ticks > timer->hw.ticks) |
| 667 | ticks = timer->hw.ticks; |
| 668 | if (ticks_left != ticks) |
| 669 | timer->flags |= SNDRV_TIMER_FLG_CHANGE; |
| 670 | timer->sticks = ticks; |
| 671 | } |
| 672 | |
| 673 | /* |
| 674 | * timer tasklet |
| 675 | * |
| 676 | */ |
| 677 | static void snd_timer_tasklet(unsigned long arg) |
| 678 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 679 | struct snd_timer *timer = (struct snd_timer *) arg; |
| 680 | struct snd_timer_instance *ti; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | struct list_head *p; |
| 682 | unsigned long resolution, ticks; |
Takashi Iwai | 2999ff5 | 2006-07-05 17:16:58 +0200 | [diff] [blame] | 683 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 685 | if (timer->card && timer->card->shutdown) |
| 686 | return; |
| 687 | |
Takashi Iwai | 2999ff5 | 2006-07-05 17:16:58 +0200 | [diff] [blame] | 688 | spin_lock_irqsave(&timer->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | /* now process all callbacks */ |
| 690 | while (!list_empty(&timer->sack_list_head)) { |
| 691 | p = timer->sack_list_head.next; /* get first item */ |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 692 | ti = list_entry(p, struct snd_timer_instance, ack_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
| 694 | /* remove from ack_list and make empty */ |
| 695 | list_del_init(p); |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | ticks = ti->pticks; |
| 698 | ti->pticks = 0; |
| 699 | resolution = ti->resolution; |
| 700 | |
| 701 | ti->flags |= SNDRV_TIMER_IFLG_CALLBACK; |
| 702 | spin_unlock(&timer->lock); |
| 703 | if (ti->callback) |
| 704 | ti->callback(ti, resolution, ticks); |
| 705 | spin_lock(&timer->lock); |
| 706 | ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK; |
| 707 | } |
Takashi Iwai | 2999ff5 | 2006-07-05 17:16:58 +0200 | [diff] [blame] | 708 | spin_unlock_irqrestore(&timer->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | /* |
| 712 | * timer interrupt |
| 713 | * |
| 714 | * ticks_left is usually equal to timer->sticks. |
| 715 | * |
| 716 | */ |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 717 | void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | { |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 719 | struct snd_timer_instance *ti, *ts, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | unsigned long resolution, ticks; |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 721 | struct list_head *p, *ack_list_head; |
Takashi Iwai | b32425a | 2005-11-18 18:52:14 +0100 | [diff] [blame] | 722 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | int use_tasklet = 0; |
| 724 | |
| 725 | if (timer == NULL) |
| 726 | return; |
| 727 | |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 728 | if (timer->card && timer->card->shutdown) |
| 729 | return; |
| 730 | |
Takashi Iwai | b32425a | 2005-11-18 18:52:14 +0100 | [diff] [blame] | 731 | spin_lock_irqsave(&timer->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
| 733 | /* remember the current resolution */ |
| 734 | if (timer->hw.c_resolution) |
| 735 | resolution = timer->hw.c_resolution(timer); |
| 736 | else |
| 737 | resolution = timer->hw.resolution; |
| 738 | |
| 739 | /* loop for all active instances |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 740 | * Here we cannot use list_for_each_entry because the active_list of a |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 741 | * processed instance is relinked to done_list_head before the callback |
| 742 | * is called. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | */ |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 744 | list_for_each_entry_safe(ti, tmp, &timer->active_list_head, |
| 745 | active_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING)) |
| 747 | continue; |
| 748 | ti->pticks += ticks_left; |
| 749 | ti->resolution = resolution; |
| 750 | if (ti->cticks < ticks_left) |
| 751 | ti->cticks = 0; |
| 752 | else |
| 753 | ti->cticks -= ticks_left; |
| 754 | if (ti->cticks) /* not expired */ |
| 755 | continue; |
| 756 | if (ti->flags & SNDRV_TIMER_IFLG_AUTO) { |
| 757 | ti->cticks = ti->ticks; |
| 758 | } else { |
| 759 | ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING; |
Takashi Iwai | 094fd3b | 2016-02-04 17:06:13 +0100 | [diff] [blame] | 760 | --timer->running; |
| 761 | list_del_init(&ti->active_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | } |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 763 | if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) || |
| 764 | (ti->flags & SNDRV_TIMER_IFLG_FAST)) |
| 765 | ack_list_head = &timer->ack_list_head; |
| 766 | else |
| 767 | ack_list_head = &timer->sack_list_head; |
| 768 | if (list_empty(&ti->ack_list)) |
| 769 | list_add_tail(&ti->ack_list, ack_list_head); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 770 | list_for_each_entry(ts, &ti->slave_active_head, active_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | ts->pticks = ti->pticks; |
| 772 | ts->resolution = resolution; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 773 | if (list_empty(&ts->ack_list)) |
| 774 | list_add_tail(&ts->ack_list, ack_list_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | if (timer->flags & SNDRV_TIMER_FLG_RESCHED) |
Clemens Ladisch | cd93fe4 | 2006-07-17 16:53:57 +0200 | [diff] [blame] | 778 | snd_timer_reschedule(timer, timer->sticks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | if (timer->running) { |
| 780 | if (timer->hw.flags & SNDRV_TIMER_HW_STOP) { |
| 781 | timer->hw.stop(timer); |
| 782 | timer->flags |= SNDRV_TIMER_FLG_CHANGE; |
| 783 | } |
| 784 | if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) || |
| 785 | (timer->flags & SNDRV_TIMER_FLG_CHANGE)) { |
| 786 | /* restart timer */ |
| 787 | timer->flags &= ~SNDRV_TIMER_FLG_CHANGE; |
| 788 | timer->hw.start(timer); |
| 789 | } |
| 790 | } else { |
| 791 | timer->hw.stop(timer); |
| 792 | } |
| 793 | |
| 794 | /* now process all fast callbacks */ |
| 795 | while (!list_empty(&timer->ack_list_head)) { |
| 796 | p = timer->ack_list_head.next; /* get first item */ |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 797 | ti = list_entry(p, struct snd_timer_instance, ack_list); |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | /* remove from ack_list and make empty */ |
| 800 | list_del_init(p); |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 801 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | ticks = ti->pticks; |
| 803 | ti->pticks = 0; |
| 804 | |
| 805 | ti->flags |= SNDRV_TIMER_IFLG_CALLBACK; |
| 806 | spin_unlock(&timer->lock); |
| 807 | if (ti->callback) |
| 808 | ti->callback(ti, resolution, ticks); |
| 809 | spin_lock(&timer->lock); |
| 810 | ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK; |
| 811 | } |
| 812 | |
| 813 | /* do we have any slow callbacks? */ |
| 814 | use_tasklet = !list_empty(&timer->sack_list_head); |
Takashi Iwai | b32425a | 2005-11-18 18:52:14 +0100 | [diff] [blame] | 815 | spin_unlock_irqrestore(&timer->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | |
| 817 | if (use_tasklet) |
Takashi Iwai | 1f04128 | 2008-12-18 12:17:55 +0100 | [diff] [blame] | 818 | tasklet_schedule(&timer->task_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 820 | EXPORT_SYMBOL(snd_timer_interrupt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | |
| 822 | /* |
| 823 | |
| 824 | */ |
| 825 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 826 | int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid, |
| 827 | struct snd_timer **rtimer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 829 | struct snd_timer *timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | int err; |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 831 | static struct snd_device_ops ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | .dev_free = snd_timer_dev_free, |
| 833 | .dev_register = snd_timer_dev_register, |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 834 | .dev_disconnect = snd_timer_dev_disconnect, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | }; |
| 836 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 837 | if (snd_BUG_ON(!tid)) |
| 838 | return -EINVAL; |
| 839 | if (rtimer) |
| 840 | *rtimer = NULL; |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 841 | timer = kzalloc(sizeof(*timer), GFP_KERNEL); |
Takashi Iwai | ec0e993 | 2015-03-10 15:42:14 +0100 | [diff] [blame] | 842 | if (!timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | return -ENOMEM; |
| 844 | timer->tmr_class = tid->dev_class; |
| 845 | timer->card = card; |
| 846 | timer->tmr_device = tid->device; |
| 847 | timer->tmr_subdevice = tid->subdevice; |
| 848 | if (id) |
| 849 | strlcpy(timer->id, id, sizeof(timer->id)); |
Vegard Nossum | 6b760bb | 2016-08-29 00:33:50 +0200 | [diff] [blame] | 850 | timer->sticks = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | INIT_LIST_HEAD(&timer->device_list); |
| 852 | INIT_LIST_HEAD(&timer->open_list_head); |
| 853 | INIT_LIST_HEAD(&timer->active_list_head); |
| 854 | INIT_LIST_HEAD(&timer->ack_list_head); |
| 855 | INIT_LIST_HEAD(&timer->sack_list_head); |
| 856 | spin_lock_init(&timer->lock); |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 857 | tasklet_init(&timer->task_queue, snd_timer_tasklet, |
| 858 | (unsigned long)timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | if (card != NULL) { |
Clemens Ladisch | de24214 | 2005-10-12 17:12:31 +0200 | [diff] [blame] | 860 | timer->module = card->module; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 861 | err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops); |
| 862 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | snd_timer_free(timer); |
| 864 | return err; |
| 865 | } |
| 866 | } |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 867 | if (rtimer) |
| 868 | *rtimer = timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | return 0; |
| 870 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 871 | EXPORT_SYMBOL(snd_timer_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 873 | static int snd_timer_free(struct snd_timer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 875 | if (!timer) |
| 876 | return 0; |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 877 | |
| 878 | mutex_lock(®ister_mutex); |
| 879 | if (! list_empty(&timer->open_list_head)) { |
| 880 | struct list_head *p, *n; |
| 881 | struct snd_timer_instance *ti; |
Takashi Iwai | cf74dcf | 2014-02-04 18:22:39 +0100 | [diff] [blame] | 882 | pr_warn("ALSA: timer %p is busy?\n", timer); |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 883 | list_for_each_safe(p, n, &timer->open_list_head) { |
| 884 | list_del_init(p); |
| 885 | ti = list_entry(p, struct snd_timer_instance, open_list); |
| 886 | ti->timer = NULL; |
| 887 | } |
| 888 | } |
| 889 | list_del(&timer->device_list); |
| 890 | mutex_unlock(®ister_mutex); |
| 891 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | if (timer->private_free) |
| 893 | timer->private_free(timer); |
| 894 | kfree(timer); |
| 895 | return 0; |
| 896 | } |
| 897 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 898 | static int snd_timer_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 900 | struct snd_timer *timer = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | return snd_timer_free(timer); |
| 902 | } |
| 903 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 904 | static int snd_timer_dev_register(struct snd_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 906 | struct snd_timer *timer = dev->device_data; |
| 907 | struct snd_timer *timer1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 909 | if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop)) |
| 910 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) && |
| 912 | !timer->hw.resolution && timer->hw.c_resolution == NULL) |
| 913 | return -EINVAL; |
| 914 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 915 | mutex_lock(®ister_mutex); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 916 | list_for_each_entry(timer1, &snd_timer_list, device_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | if (timer1->tmr_class > timer->tmr_class) |
| 918 | break; |
| 919 | if (timer1->tmr_class < timer->tmr_class) |
| 920 | continue; |
| 921 | if (timer1->card && timer->card) { |
| 922 | if (timer1->card->number > timer->card->number) |
| 923 | break; |
| 924 | if (timer1->card->number < timer->card->number) |
| 925 | continue; |
| 926 | } |
| 927 | if (timer1->tmr_device > timer->tmr_device) |
| 928 | break; |
| 929 | if (timer1->tmr_device < timer->tmr_device) |
| 930 | continue; |
| 931 | if (timer1->tmr_subdevice > timer->tmr_subdevice) |
| 932 | break; |
| 933 | if (timer1->tmr_subdevice < timer->tmr_subdevice) |
| 934 | continue; |
| 935 | /* conflicts.. */ |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 936 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | return -EBUSY; |
| 938 | } |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 939 | list_add_tail(&timer->device_list, &timer1->device_list); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 940 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | return 0; |
| 942 | } |
| 943 | |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 944 | static int snd_timer_dev_disconnect(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 946 | struct snd_timer *timer = device->device_data; |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 947 | struct snd_timer_instance *ti; |
| 948 | |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 949 | mutex_lock(®ister_mutex); |
| 950 | list_del_init(&timer->device_list); |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 951 | /* wake up pending sleepers */ |
| 952 | list_for_each_entry(ti, &timer->open_list_head, open_list) { |
Takashi Iwai | 40ed944 | 2016-01-21 17:43:08 +0100 | [diff] [blame] | 953 | if (ti->disconnect) |
| 954 | ti->disconnect(ti); |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 955 | } |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 956 | mutex_unlock(®ister_mutex); |
| 957 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 960 | void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | { |
| 962 | unsigned long flags; |
| 963 | unsigned long resolution = 0; |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 964 | struct snd_timer_instance *ti, *ts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 966 | if (timer->card && timer->card->shutdown) |
| 967 | return; |
Takashi Iwai | 7c22f1a | 2005-10-10 11:46:31 +0200 | [diff] [blame] | 968 | if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)) |
| 969 | return; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 970 | if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART || |
| 971 | event > SNDRV_TIMER_EVENT_MRESUME)) |
| 972 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | spin_lock_irqsave(&timer->lock, flags); |
Jaroslav Kysela | a501dfa | 2005-08-16 11:09:05 +0200 | [diff] [blame] | 974 | if (event == SNDRV_TIMER_EVENT_MSTART || |
| 975 | event == SNDRV_TIMER_EVENT_MCONTINUE || |
| 976 | event == SNDRV_TIMER_EVENT_MRESUME) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | if (timer->hw.c_resolution) |
| 978 | resolution = timer->hw.c_resolution(timer); |
| 979 | else |
| 980 | resolution = timer->hw.resolution; |
| 981 | } |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 982 | list_for_each_entry(ti, &timer->active_list_head, active_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | if (ti->ccallback) |
| 984 | ti->ccallback(ti, event, tstamp, resolution); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 985 | list_for_each_entry(ts, &ti->slave_active_head, active_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | if (ts->ccallback) |
| 987 | ts->ccallback(ts, event, tstamp, resolution); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | } |
| 989 | spin_unlock_irqrestore(&timer->lock, flags); |
| 990 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 991 | EXPORT_SYMBOL(snd_timer_notify); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | |
| 993 | /* |
| 994 | * exported functions for global timers |
| 995 | */ |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 996 | int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 998 | struct snd_timer_id tid; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL; |
| 1001 | tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE; |
| 1002 | tid.card = -1; |
| 1003 | tid.device = device; |
| 1004 | tid.subdevice = 0; |
| 1005 | return snd_timer_new(NULL, id, &tid, rtimer); |
| 1006 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 1007 | EXPORT_SYMBOL(snd_timer_global_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1009 | int snd_timer_global_free(struct snd_timer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | { |
| 1011 | return snd_timer_free(timer); |
| 1012 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 1013 | EXPORT_SYMBOL(snd_timer_global_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1015 | int snd_timer_global_register(struct snd_timer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1017 | struct snd_device dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | |
| 1019 | memset(&dev, 0, sizeof(dev)); |
| 1020 | dev.device_data = timer; |
| 1021 | return snd_timer_dev_register(&dev); |
| 1022 | } |
Takashi Iwai | 9885639 | 2017-06-16 16:16:05 +0200 | [diff] [blame] | 1023 | EXPORT_SYMBOL(snd_timer_global_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1025 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | * System timer |
| 1027 | */ |
| 1028 | |
| 1029 | struct snd_timer_system_private { |
| 1030 | struct timer_list tlist; |
Kees Cook | 38e9a80 | 2017-10-04 17:53:33 -0700 | [diff] [blame^] | 1031 | struct snd_timer *snd_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | unsigned long last_expires; |
| 1033 | unsigned long last_jiffies; |
| 1034 | unsigned long correction; |
| 1035 | }; |
| 1036 | |
Kees Cook | 38e9a80 | 2017-10-04 17:53:33 -0700 | [diff] [blame^] | 1037 | static void snd_timer_s_function(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | { |
Kees Cook | 38e9a80 | 2017-10-04 17:53:33 -0700 | [diff] [blame^] | 1039 | struct snd_timer_system_private *priv = from_timer(priv, t, |
| 1040 | tlist); |
| 1041 | struct snd_timer *timer = priv->snd_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | unsigned long jiff = jiffies; |
| 1043 | if (time_after(jiff, priv->last_expires)) |
Clemens Ladisch | 6ed5eff | 2006-07-17 16:51:37 +0200 | [diff] [blame] | 1044 | priv->correction += (long)jiff - (long)priv->last_expires; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies); |
| 1046 | } |
| 1047 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1048 | static int snd_timer_s_start(struct snd_timer * timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | { |
| 1050 | struct snd_timer_system_private *priv; |
| 1051 | unsigned long njiff; |
| 1052 | |
| 1053 | priv = (struct snd_timer_system_private *) timer->private_data; |
| 1054 | njiff = (priv->last_jiffies = jiffies); |
| 1055 | if (priv->correction > timer->sticks - 1) { |
| 1056 | priv->correction -= timer->sticks - 1; |
| 1057 | njiff++; |
| 1058 | } else { |
| 1059 | njiff += timer->sticks - priv->correction; |
Clemens Ladisch | 17f48ec | 2006-07-17 16:50:56 +0200 | [diff] [blame] | 1060 | priv->correction = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | } |
Takashi Iwai | 4a07083 | 2016-04-01 12:28:16 +0200 | [diff] [blame] | 1062 | priv->last_expires = njiff; |
| 1063 | mod_timer(&priv->tlist, njiff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | return 0; |
| 1065 | } |
| 1066 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1067 | static int snd_timer_s_stop(struct snd_timer * timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | { |
| 1069 | struct snd_timer_system_private *priv; |
| 1070 | unsigned long jiff; |
| 1071 | |
| 1072 | priv = (struct snd_timer_system_private *) timer->private_data; |
| 1073 | del_timer(&priv->tlist); |
| 1074 | jiff = jiffies; |
| 1075 | if (time_before(jiff, priv->last_expires)) |
| 1076 | timer->sticks = priv->last_expires - jiff; |
| 1077 | else |
| 1078 | timer->sticks = 1; |
Clemens Ladisch | de2696d | 2006-07-17 16:52:09 +0200 | [diff] [blame] | 1079 | priv->correction = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | return 0; |
| 1081 | } |
| 1082 | |
Takashi Iwai | f146357 | 2016-02-02 14:14:10 +0100 | [diff] [blame] | 1083 | static int snd_timer_s_close(struct snd_timer *timer) |
| 1084 | { |
| 1085 | struct snd_timer_system_private *priv; |
| 1086 | |
| 1087 | priv = (struct snd_timer_system_private *)timer->private_data; |
| 1088 | del_timer_sync(&priv->tlist); |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1092 | static struct snd_timer_hardware snd_timer_system = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | { |
| 1094 | .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET, |
| 1095 | .resolution = 1000000000L / HZ, |
| 1096 | .ticks = 10000000L, |
Takashi Iwai | f146357 | 2016-02-02 14:14:10 +0100 | [diff] [blame] | 1097 | .close = snd_timer_s_close, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | .start = snd_timer_s_start, |
| 1099 | .stop = snd_timer_s_stop |
| 1100 | }; |
| 1101 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1102 | static void snd_timer_free_system(struct snd_timer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | { |
| 1104 | kfree(timer->private_data); |
| 1105 | } |
| 1106 | |
| 1107 | static int snd_timer_register_system(void) |
| 1108 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1109 | struct snd_timer *timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | struct snd_timer_system_private *priv; |
| 1111 | int err; |
| 1112 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1113 | err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer); |
| 1114 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | return err; |
| 1116 | strcpy(timer->name, "system timer"); |
| 1117 | timer->hw = snd_timer_system; |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 1118 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | if (priv == NULL) { |
| 1120 | snd_timer_free(timer); |
| 1121 | return -ENOMEM; |
| 1122 | } |
Kees Cook | 38e9a80 | 2017-10-04 17:53:33 -0700 | [diff] [blame^] | 1123 | priv->snd_timer = timer; |
| 1124 | timer_setup(&priv->tlist, snd_timer_s_function, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | timer->private_data = priv; |
| 1126 | timer->private_free = snd_timer_free_system; |
| 1127 | return snd_timer_global_register(timer); |
| 1128 | } |
| 1129 | |
Jie Yang | cd6a650 | 2015-05-27 19:45:45 +0800 | [diff] [blame] | 1130 | #ifdef CONFIG_SND_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | /* |
| 1132 | * Info interface |
| 1133 | */ |
| 1134 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1135 | static void snd_timer_proc_read(struct snd_info_entry *entry, |
| 1136 | struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1138 | struct snd_timer *timer; |
| 1139 | struct snd_timer_instance *ti; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1141 | mutex_lock(®ister_mutex); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 1142 | list_for_each_entry(timer, &snd_timer_list, device_list) { |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 1143 | if (timer->card && timer->card->shutdown) |
| 1144 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | switch (timer->tmr_class) { |
| 1146 | case SNDRV_TIMER_CLASS_GLOBAL: |
| 1147 | snd_iprintf(buffer, "G%i: ", timer->tmr_device); |
| 1148 | break; |
| 1149 | case SNDRV_TIMER_CLASS_CARD: |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1150 | snd_iprintf(buffer, "C%i-%i: ", |
| 1151 | timer->card->number, timer->tmr_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | break; |
| 1153 | case SNDRV_TIMER_CLASS_PCM: |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1154 | snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number, |
| 1155 | timer->tmr_device, timer->tmr_subdevice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | break; |
| 1157 | default: |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1158 | snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class, |
| 1159 | timer->card ? timer->card->number : -1, |
| 1160 | timer->tmr_device, timer->tmr_subdevice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | } |
| 1162 | snd_iprintf(buffer, "%s :", timer->name); |
| 1163 | if (timer->hw.resolution) |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1164 | snd_iprintf(buffer, " %lu.%03luus (%lu ticks)", |
| 1165 | timer->hw.resolution / 1000, |
| 1166 | timer->hw.resolution % 1000, |
| 1167 | timer->hw.ticks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE) |
| 1169 | snd_iprintf(buffer, " SLAVE"); |
| 1170 | snd_iprintf(buffer, "\n"); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 1171 | list_for_each_entry(ti, &timer->open_list_head, open_list) |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1172 | snd_iprintf(buffer, " Client %s : %s\n", |
| 1173 | ti->owner ? ti->owner : "unknown", |
| 1174 | ti->flags & (SNDRV_TIMER_IFLG_START | |
| 1175 | SNDRV_TIMER_IFLG_RUNNING) |
| 1176 | ? "running" : "stopped"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1178 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
Takashi Iwai | 6581f4e | 2006-05-17 17:14:51 +0200 | [diff] [blame] | 1181 | static struct snd_info_entry *snd_timer_proc_entry; |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 1182 | |
| 1183 | static void __init snd_timer_proc_init(void) |
| 1184 | { |
| 1185 | struct snd_info_entry *entry; |
| 1186 | |
| 1187 | entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL); |
| 1188 | if (entry != NULL) { |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 1189 | entry->c.text.read = snd_timer_proc_read; |
| 1190 | if (snd_info_register(entry) < 0) { |
| 1191 | snd_info_free_entry(entry); |
| 1192 | entry = NULL; |
| 1193 | } |
| 1194 | } |
| 1195 | snd_timer_proc_entry = entry; |
| 1196 | } |
| 1197 | |
| 1198 | static void __exit snd_timer_proc_done(void) |
| 1199 | { |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 1200 | snd_info_free_entry(snd_timer_proc_entry); |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 1201 | } |
Jie Yang | cd6a650 | 2015-05-27 19:45:45 +0800 | [diff] [blame] | 1202 | #else /* !CONFIG_SND_PROC_FS */ |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 1203 | #define snd_timer_proc_init() |
| 1204 | #define snd_timer_proc_done() |
| 1205 | #endif |
| 1206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | /* |
| 1208 | * USER SPACE interface |
| 1209 | */ |
| 1210 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1211 | static void snd_timer_user_interrupt(struct snd_timer_instance *timeri, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | unsigned long resolution, |
| 1213 | unsigned long ticks) |
| 1214 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1215 | struct snd_timer_user *tu = timeri->callback_data; |
| 1216 | struct snd_timer_read *r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | int prev; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | spin_lock(&tu->qlock); |
| 1220 | if (tu->qused > 0) { |
| 1221 | prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1; |
| 1222 | r = &tu->queue[prev]; |
| 1223 | if (r->resolution == resolution) { |
| 1224 | r->ticks += ticks; |
| 1225 | goto __wake; |
| 1226 | } |
| 1227 | } |
| 1228 | if (tu->qused >= tu->queue_size) { |
| 1229 | tu->overrun++; |
| 1230 | } else { |
| 1231 | r = &tu->queue[tu->qtail++]; |
| 1232 | tu->qtail %= tu->queue_size; |
| 1233 | r->resolution = resolution; |
| 1234 | r->ticks = ticks; |
| 1235 | tu->qused++; |
| 1236 | } |
| 1237 | __wake: |
| 1238 | spin_unlock(&tu->qlock); |
| 1239 | kill_fasync(&tu->fasync, SIGIO, POLL_IN); |
| 1240 | wake_up(&tu->qchange_sleep); |
| 1241 | } |
| 1242 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1243 | static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu, |
| 1244 | struct snd_timer_tread *tread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | { |
| 1246 | if (tu->qused >= tu->queue_size) { |
| 1247 | tu->overrun++; |
| 1248 | } else { |
| 1249 | memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread)); |
| 1250 | tu->qtail %= tu->queue_size; |
| 1251 | tu->qused++; |
| 1252 | } |
| 1253 | } |
| 1254 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1255 | static void snd_timer_user_ccallback(struct snd_timer_instance *timeri, |
| 1256 | int event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | struct timespec *tstamp, |
| 1258 | unsigned long resolution) |
| 1259 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1260 | struct snd_timer_user *tu = timeri->callback_data; |
| 1261 | struct snd_timer_tread r1; |
Dan Carpenter | bfe7078 | 2010-04-28 10:29:14 +0200 | [diff] [blame] | 1262 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1264 | if (event >= SNDRV_TIMER_EVENT_START && |
| 1265 | event <= SNDRV_TIMER_EVENT_PAUSE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | tu->tstamp = *tstamp; |
| 1267 | if ((tu->filter & (1 << event)) == 0 || !tu->tread) |
| 1268 | return; |
Kangjie Lu | 9a47e9c | 2016-05-03 16:44:20 -0400 | [diff] [blame] | 1269 | memset(&r1, 0, sizeof(r1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | r1.event = event; |
| 1271 | r1.tstamp = *tstamp; |
| 1272 | r1.val = resolution; |
Dan Carpenter | bfe7078 | 2010-04-28 10:29:14 +0200 | [diff] [blame] | 1273 | spin_lock_irqsave(&tu->qlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | snd_timer_user_append_to_tqueue(tu, &r1); |
Dan Carpenter | bfe7078 | 2010-04-28 10:29:14 +0200 | [diff] [blame] | 1275 | spin_unlock_irqrestore(&tu->qlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | kill_fasync(&tu->fasync, SIGIO, POLL_IN); |
| 1277 | wake_up(&tu->qchange_sleep); |
| 1278 | } |
| 1279 | |
Takashi Iwai | 40ed944 | 2016-01-21 17:43:08 +0100 | [diff] [blame] | 1280 | static void snd_timer_user_disconnect(struct snd_timer_instance *timeri) |
| 1281 | { |
| 1282 | struct snd_timer_user *tu = timeri->callback_data; |
| 1283 | |
| 1284 | tu->disconnected = true; |
| 1285 | wake_up(&tu->qchange_sleep); |
| 1286 | } |
| 1287 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1288 | static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | unsigned long resolution, |
| 1290 | unsigned long ticks) |
| 1291 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1292 | struct snd_timer_user *tu = timeri->callback_data; |
| 1293 | struct snd_timer_tread *r, r1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | struct timespec tstamp; |
| 1295 | int prev, append = 0; |
| 1296 | |
Dan Carpenter | a8c006a | 2017-03-31 18:22:23 +0300 | [diff] [blame] | 1297 | memset(&r1, 0, sizeof(r1)); |
Takashi Iwai | 07799e7 | 2005-10-10 11:49:49 +0200 | [diff] [blame] | 1298 | memset(&tstamp, 0, sizeof(tstamp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | spin_lock(&tu->qlock); |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1300 | if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) | |
| 1301 | (1 << SNDRV_TIMER_EVENT_TICK))) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | spin_unlock(&tu->qlock); |
| 1303 | return; |
| 1304 | } |
Jaroslav Kysela | b751eef | 2007-12-13 10:19:42 +0100 | [diff] [blame] | 1305 | if (tu->last_resolution != resolution || ticks > 0) { |
| 1306 | if (timer_tstamp_monotonic) |
Thomas Gleixner | 26204e0 | 2014-06-11 23:59:14 +0000 | [diff] [blame] | 1307 | ktime_get_ts(&tstamp); |
Jaroslav Kysela | b751eef | 2007-12-13 10:19:42 +0100 | [diff] [blame] | 1308 | else |
| 1309 | getnstimeofday(&tstamp); |
| 1310 | } |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1311 | if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) && |
| 1312 | tu->last_resolution != resolution) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | r1.event = SNDRV_TIMER_EVENT_RESOLUTION; |
| 1314 | r1.tstamp = tstamp; |
| 1315 | r1.val = resolution; |
| 1316 | snd_timer_user_append_to_tqueue(tu, &r1); |
| 1317 | tu->last_resolution = resolution; |
| 1318 | append++; |
| 1319 | } |
| 1320 | if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0) |
| 1321 | goto __wake; |
| 1322 | if (ticks == 0) |
| 1323 | goto __wake; |
| 1324 | if (tu->qused > 0) { |
| 1325 | prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1; |
| 1326 | r = &tu->tqueue[prev]; |
| 1327 | if (r->event == SNDRV_TIMER_EVENT_TICK) { |
| 1328 | r->tstamp = tstamp; |
| 1329 | r->val += ticks; |
| 1330 | append++; |
| 1331 | goto __wake; |
| 1332 | } |
| 1333 | } |
| 1334 | r1.event = SNDRV_TIMER_EVENT_TICK; |
| 1335 | r1.tstamp = tstamp; |
| 1336 | r1.val = ticks; |
| 1337 | snd_timer_user_append_to_tqueue(tu, &r1); |
| 1338 | append++; |
| 1339 | __wake: |
| 1340 | spin_unlock(&tu->qlock); |
| 1341 | if (append == 0) |
| 1342 | return; |
| 1343 | kill_fasync(&tu->fasync, SIGIO, POLL_IN); |
| 1344 | wake_up(&tu->qchange_sleep); |
| 1345 | } |
| 1346 | |
Takashi Iwai | 890e2cb | 2017-06-02 17:16:59 +0200 | [diff] [blame] | 1347 | static int realloc_user_queue(struct snd_timer_user *tu, int size) |
| 1348 | { |
| 1349 | struct snd_timer_read *queue = NULL; |
| 1350 | struct snd_timer_tread *tqueue = NULL; |
| 1351 | |
| 1352 | if (tu->tread) { |
| 1353 | tqueue = kcalloc(size, sizeof(*tqueue), GFP_KERNEL); |
| 1354 | if (!tqueue) |
| 1355 | return -ENOMEM; |
| 1356 | } else { |
| 1357 | queue = kcalloc(size, sizeof(*queue), GFP_KERNEL); |
| 1358 | if (!queue) |
| 1359 | return -ENOMEM; |
| 1360 | } |
| 1361 | |
| 1362 | spin_lock_irq(&tu->qlock); |
| 1363 | kfree(tu->queue); |
| 1364 | kfree(tu->tqueue); |
| 1365 | tu->queue_size = size; |
| 1366 | tu->queue = queue; |
| 1367 | tu->tqueue = tqueue; |
| 1368 | tu->qhead = tu->qtail = tu->qused = 0; |
| 1369 | spin_unlock_irq(&tu->qlock); |
| 1370 | |
| 1371 | return 0; |
| 1372 | } |
| 1373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | static int snd_timer_user_open(struct inode *inode, struct file *file) |
| 1375 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1376 | struct snd_timer_user *tu; |
Takashi Iwai | 02f4865 | 2010-04-13 11:49:04 +0200 | [diff] [blame] | 1377 | int err; |
| 1378 | |
| 1379 | err = nonseekable_open(inode, file); |
| 1380 | if (err < 0) |
| 1381 | return err; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1382 | |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 1383 | tu = kzalloc(sizeof(*tu), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | if (tu == NULL) |
| 1385 | return -ENOMEM; |
| 1386 | spin_lock_init(&tu->qlock); |
| 1387 | init_waitqueue_head(&tu->qchange_sleep); |
Takashi Iwai | af36802 | 2016-01-13 17:48:01 +0100 | [diff] [blame] | 1388 | mutex_init(&tu->ioctl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | tu->ticks = 1; |
Takashi Iwai | 890e2cb | 2017-06-02 17:16:59 +0200 | [diff] [blame] | 1390 | if (realloc_user_queue(tu, 128) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | kfree(tu); |
| 1392 | return -ENOMEM; |
| 1393 | } |
| 1394 | file->private_data = tu; |
| 1395 | return 0; |
| 1396 | } |
| 1397 | |
| 1398 | static int snd_timer_user_release(struct inode *inode, struct file *file) |
| 1399 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1400 | struct snd_timer_user *tu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | |
| 1402 | if (file->private_data) { |
| 1403 | tu = file->private_data; |
| 1404 | file->private_data = NULL; |
Takashi Iwai | af36802 | 2016-01-13 17:48:01 +0100 | [diff] [blame] | 1405 | mutex_lock(&tu->ioctl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | if (tu->timeri) |
| 1407 | snd_timer_close(tu->timeri); |
Takashi Iwai | af36802 | 2016-01-13 17:48:01 +0100 | [diff] [blame] | 1408 | mutex_unlock(&tu->ioctl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | kfree(tu->queue); |
| 1410 | kfree(tu->tqueue); |
| 1411 | kfree(tu); |
| 1412 | } |
| 1413 | return 0; |
| 1414 | } |
| 1415 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1416 | static void snd_timer_user_zero_id(struct snd_timer_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | { |
| 1418 | id->dev_class = SNDRV_TIMER_CLASS_NONE; |
| 1419 | id->dev_sclass = SNDRV_TIMER_SCLASS_NONE; |
| 1420 | id->card = -1; |
| 1421 | id->device = -1; |
| 1422 | id->subdevice = -1; |
| 1423 | } |
| 1424 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1425 | static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | { |
| 1427 | id->dev_class = timer->tmr_class; |
| 1428 | id->dev_sclass = SNDRV_TIMER_SCLASS_NONE; |
| 1429 | id->card = timer->card ? timer->card->number : -1; |
| 1430 | id->device = timer->tmr_device; |
| 1431 | id->subdevice = timer->tmr_subdevice; |
| 1432 | } |
| 1433 | |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1434 | static int snd_timer_user_next_device(struct snd_timer_id __user *_tid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1436 | struct snd_timer_id id; |
| 1437 | struct snd_timer *timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | struct list_head *p; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | if (copy_from_user(&id, _tid, sizeof(id))) |
| 1441 | return -EFAULT; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1442 | mutex_lock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | if (id.dev_class < 0) { /* first item */ |
| 1444 | if (list_empty(&snd_timer_list)) |
| 1445 | snd_timer_user_zero_id(&id); |
| 1446 | else { |
Clemens Ladisch | 9dfba38 | 2005-10-12 17:14:55 +0200 | [diff] [blame] | 1447 | timer = list_entry(snd_timer_list.next, |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1448 | struct snd_timer, device_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | snd_timer_user_copy_id(&id, timer); |
| 1450 | } |
| 1451 | } else { |
| 1452 | switch (id.dev_class) { |
| 1453 | case SNDRV_TIMER_CLASS_GLOBAL: |
| 1454 | id.device = id.device < 0 ? 0 : id.device + 1; |
| 1455 | list_for_each(p, &snd_timer_list) { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1456 | timer = list_entry(p, struct snd_timer, device_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) { |
| 1458 | snd_timer_user_copy_id(&id, timer); |
| 1459 | break; |
| 1460 | } |
| 1461 | if (timer->tmr_device >= id.device) { |
| 1462 | snd_timer_user_copy_id(&id, timer); |
| 1463 | break; |
| 1464 | } |
| 1465 | } |
| 1466 | if (p == &snd_timer_list) |
| 1467 | snd_timer_user_zero_id(&id); |
| 1468 | break; |
| 1469 | case SNDRV_TIMER_CLASS_CARD: |
| 1470 | case SNDRV_TIMER_CLASS_PCM: |
| 1471 | if (id.card < 0) { |
| 1472 | id.card = 0; |
| 1473 | } else { |
Dan Carpenter | e8ed682 | 2017-03-31 18:21:41 +0300 | [diff] [blame] | 1474 | if (id.device < 0) { |
| 1475 | id.device = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | } else { |
Dan Carpenter | e8ed682 | 2017-03-31 18:21:41 +0300 | [diff] [blame] | 1477 | if (id.subdevice < 0) |
| 1478 | id.subdevice = 0; |
| 1479 | else |
| 1480 | id.subdevice++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | } |
| 1482 | } |
| 1483 | list_for_each(p, &snd_timer_list) { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1484 | timer = list_entry(p, struct snd_timer, device_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | if (timer->tmr_class > id.dev_class) { |
| 1486 | snd_timer_user_copy_id(&id, timer); |
| 1487 | break; |
| 1488 | } |
| 1489 | if (timer->tmr_class < id.dev_class) |
| 1490 | continue; |
| 1491 | if (timer->card->number > id.card) { |
| 1492 | snd_timer_user_copy_id(&id, timer); |
| 1493 | break; |
| 1494 | } |
| 1495 | if (timer->card->number < id.card) |
| 1496 | continue; |
| 1497 | if (timer->tmr_device > id.device) { |
| 1498 | snd_timer_user_copy_id(&id, timer); |
| 1499 | break; |
| 1500 | } |
| 1501 | if (timer->tmr_device < id.device) |
| 1502 | continue; |
| 1503 | if (timer->tmr_subdevice > id.subdevice) { |
| 1504 | snd_timer_user_copy_id(&id, timer); |
| 1505 | break; |
| 1506 | } |
| 1507 | if (timer->tmr_subdevice < id.subdevice) |
| 1508 | continue; |
| 1509 | snd_timer_user_copy_id(&id, timer); |
| 1510 | break; |
| 1511 | } |
| 1512 | if (p == &snd_timer_list) |
| 1513 | snd_timer_user_zero_id(&id); |
| 1514 | break; |
| 1515 | default: |
| 1516 | snd_timer_user_zero_id(&id); |
| 1517 | } |
| 1518 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1519 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | if (copy_to_user(_tid, &id, sizeof(*_tid))) |
| 1521 | return -EFAULT; |
| 1522 | return 0; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1525 | static int snd_timer_user_ginfo(struct file *file, |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1526 | struct snd_timer_ginfo __user *_ginfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1528 | struct snd_timer_ginfo *ginfo; |
| 1529 | struct snd_timer_id tid; |
| 1530 | struct snd_timer *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | struct list_head *p; |
| 1532 | int err = 0; |
| 1533 | |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 1534 | ginfo = memdup_user(_ginfo, sizeof(*ginfo)); |
| 1535 | if (IS_ERR(ginfo)) |
| 1536 | return PTR_ERR(ginfo); |
| 1537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | tid = ginfo->tid; |
| 1539 | memset(ginfo, 0, sizeof(*ginfo)); |
| 1540 | ginfo->tid = tid; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1541 | mutex_lock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | t = snd_timer_find(&tid); |
| 1543 | if (t != NULL) { |
| 1544 | ginfo->card = t->card ? t->card->number : -1; |
| 1545 | if (t->hw.flags & SNDRV_TIMER_HW_SLAVE) |
| 1546 | ginfo->flags |= SNDRV_TIMER_FLG_SLAVE; |
| 1547 | strlcpy(ginfo->id, t->id, sizeof(ginfo->id)); |
| 1548 | strlcpy(ginfo->name, t->name, sizeof(ginfo->name)); |
| 1549 | ginfo->resolution = t->hw.resolution; |
| 1550 | if (t->hw.resolution_min > 0) { |
| 1551 | ginfo->resolution_min = t->hw.resolution_min; |
| 1552 | ginfo->resolution_max = t->hw.resolution_max; |
| 1553 | } |
| 1554 | list_for_each(p, &t->open_list_head) { |
| 1555 | ginfo->clients++; |
| 1556 | } |
| 1557 | } else { |
| 1558 | err = -ENODEV; |
| 1559 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1560 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo))) |
| 1562 | err = -EFAULT; |
| 1563 | kfree(ginfo); |
| 1564 | return err; |
| 1565 | } |
| 1566 | |
Takashi Sakamoto | 91d2178 | 2016-03-23 08:03:59 +0900 | [diff] [blame] | 1567 | static int timer_set_gparams(struct snd_timer_gparams *gparams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1569 | struct snd_timer *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | int err; |
| 1571 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1572 | mutex_lock(®ister_mutex); |
Takashi Sakamoto | 91d2178 | 2016-03-23 08:03:59 +0900 | [diff] [blame] | 1573 | t = snd_timer_find(&gparams->tid); |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1574 | if (!t) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | err = -ENODEV; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1576 | goto _error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | } |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1578 | if (!list_empty(&t->open_list_head)) { |
| 1579 | err = -EBUSY; |
| 1580 | goto _error; |
| 1581 | } |
| 1582 | if (!t->hw.set_period) { |
| 1583 | err = -ENOSYS; |
| 1584 | goto _error; |
| 1585 | } |
Takashi Sakamoto | 91d2178 | 2016-03-23 08:03:59 +0900 | [diff] [blame] | 1586 | err = t->hw.set_period(t, gparams->period_num, gparams->period_den); |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1587 | _error: |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1588 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | return err; |
| 1590 | } |
| 1591 | |
Takashi Sakamoto | 91d2178 | 2016-03-23 08:03:59 +0900 | [diff] [blame] | 1592 | static int snd_timer_user_gparams(struct file *file, |
| 1593 | struct snd_timer_gparams __user *_gparams) |
| 1594 | { |
| 1595 | struct snd_timer_gparams gparams; |
| 1596 | |
| 1597 | if (copy_from_user(&gparams, _gparams, sizeof(gparams))) |
| 1598 | return -EFAULT; |
| 1599 | return timer_set_gparams(&gparams); |
| 1600 | } |
| 1601 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1602 | static int snd_timer_user_gstatus(struct file *file, |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1603 | struct snd_timer_gstatus __user *_gstatus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1605 | struct snd_timer_gstatus gstatus; |
| 1606 | struct snd_timer_id tid; |
| 1607 | struct snd_timer *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | int err = 0; |
| 1609 | |
| 1610 | if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus))) |
| 1611 | return -EFAULT; |
| 1612 | tid = gstatus.tid; |
| 1613 | memset(&gstatus, 0, sizeof(gstatus)); |
| 1614 | gstatus.tid = tid; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1615 | mutex_lock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | t = snd_timer_find(&tid); |
| 1617 | if (t != NULL) { |
| 1618 | if (t->hw.c_resolution) |
| 1619 | gstatus.resolution = t->hw.c_resolution(t); |
| 1620 | else |
| 1621 | gstatus.resolution = t->hw.resolution; |
| 1622 | if (t->hw.precise_resolution) { |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1623 | t->hw.precise_resolution(t, &gstatus.resolution_num, |
| 1624 | &gstatus.resolution_den); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | } else { |
| 1626 | gstatus.resolution_num = gstatus.resolution; |
| 1627 | gstatus.resolution_den = 1000000000uL; |
| 1628 | } |
| 1629 | } else { |
| 1630 | err = -ENODEV; |
| 1631 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1632 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus))) |
| 1634 | err = -EFAULT; |
| 1635 | return err; |
| 1636 | } |
| 1637 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1638 | static int snd_timer_user_tselect(struct file *file, |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1639 | struct snd_timer_select __user *_tselect) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1641 | struct snd_timer_user *tu; |
| 1642 | struct snd_timer_select tselect; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | char str[32]; |
Jaroslav Kysela | c1935b4 | 2005-04-04 16:44:58 +0200 | [diff] [blame] | 1644 | int err = 0; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | tu = file->private_data; |
Jaroslav Kysela | c1935b4 | 2005-04-04 16:44:58 +0200 | [diff] [blame] | 1647 | if (tu->timeri) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | snd_timer_close(tu->timeri); |
Jaroslav Kysela | c1935b4 | 2005-04-04 16:44:58 +0200 | [diff] [blame] | 1649 | tu->timeri = NULL; |
| 1650 | } |
| 1651 | if (copy_from_user(&tselect, _tselect, sizeof(tselect))) { |
| 1652 | err = -EFAULT; |
| 1653 | goto __err; |
| 1654 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | sprintf(str, "application %i", current->pid); |
| 1656 | if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE) |
| 1657 | tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1658 | err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid); |
| 1659 | if (err < 0) |
Jaroslav Kysela | c1935b4 | 2005-04-04 16:44:58 +0200 | [diff] [blame] | 1660 | goto __err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | |
Takashi Iwai | 890e2cb | 2017-06-02 17:16:59 +0200 | [diff] [blame] | 1662 | tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST; |
| 1663 | tu->timeri->callback = tu->tread |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1664 | ? snd_timer_user_tinterrupt : snd_timer_user_interrupt; |
Takashi Iwai | 890e2cb | 2017-06-02 17:16:59 +0200 | [diff] [blame] | 1665 | tu->timeri->ccallback = snd_timer_user_ccallback; |
| 1666 | tu->timeri->callback_data = (void *)tu; |
| 1667 | tu->timeri->disconnect = snd_timer_user_disconnect; |
Jaroslav Kysela | c1935b4 | 2005-04-04 16:44:58 +0200 | [diff] [blame] | 1668 | |
| 1669 | __err: |
Jaroslav Kysela | c1935b4 | 2005-04-04 16:44:58 +0200 | [diff] [blame] | 1670 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | } |
| 1672 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1673 | static int snd_timer_user_info(struct file *file, |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1674 | struct snd_timer_info __user *_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1676 | struct snd_timer_user *tu; |
| 1677 | struct snd_timer_info *info; |
| 1678 | struct snd_timer *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | int err = 0; |
| 1680 | |
| 1681 | tu = file->private_data; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1682 | if (!tu->timeri) |
| 1683 | return -EBADFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | t = tu->timeri->timer; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1685 | if (!t) |
| 1686 | return -EBADFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 1688 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | if (! info) |
| 1690 | return -ENOMEM; |
| 1691 | info->card = t->card ? t->card->number : -1; |
| 1692 | if (t->hw.flags & SNDRV_TIMER_HW_SLAVE) |
| 1693 | info->flags |= SNDRV_TIMER_FLG_SLAVE; |
| 1694 | strlcpy(info->id, t->id, sizeof(info->id)); |
| 1695 | strlcpy(info->name, t->name, sizeof(info->name)); |
| 1696 | info->resolution = t->hw.resolution; |
| 1697 | if (copy_to_user(_info, info, sizeof(*_info))) |
| 1698 | err = -EFAULT; |
| 1699 | kfree(info); |
| 1700 | return err; |
| 1701 | } |
| 1702 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1703 | static int snd_timer_user_params(struct file *file, |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1704 | struct snd_timer_params __user *_params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1706 | struct snd_timer_user *tu; |
| 1707 | struct snd_timer_params params; |
| 1708 | struct snd_timer *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | int err; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | tu = file->private_data; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1712 | if (!tu->timeri) |
| 1713 | return -EBADFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | t = tu->timeri->timer; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1715 | if (!t) |
| 1716 | return -EBADFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | if (copy_from_user(¶ms, _params, sizeof(params))) |
| 1718 | return -EFAULT; |
Takashi Iwai | 71321eb | 2017-02-28 14:49:07 +0100 | [diff] [blame] | 1719 | if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) { |
| 1720 | u64 resolution; |
| 1721 | |
| 1722 | if (params.ticks < 1) { |
| 1723 | err = -EINVAL; |
| 1724 | goto _end; |
| 1725 | } |
| 1726 | |
| 1727 | /* Don't allow resolution less than 1ms */ |
| 1728 | resolution = snd_timer_resolution(tu->timeri); |
| 1729 | resolution *= params.ticks; |
| 1730 | if (resolution < 1000000) { |
| 1731 | err = -EINVAL; |
| 1732 | goto _end; |
| 1733 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | } |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1735 | if (params.queue_size > 0 && |
| 1736 | (params.queue_size < 32 || params.queue_size > 1024)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | err = -EINVAL; |
| 1738 | goto _end; |
| 1739 | } |
| 1740 | if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)| |
| 1741 | (1<<SNDRV_TIMER_EVENT_TICK)| |
| 1742 | (1<<SNDRV_TIMER_EVENT_START)| |
| 1743 | (1<<SNDRV_TIMER_EVENT_STOP)| |
| 1744 | (1<<SNDRV_TIMER_EVENT_CONTINUE)| |
| 1745 | (1<<SNDRV_TIMER_EVENT_PAUSE)| |
Jaroslav Kysela | a501dfa | 2005-08-16 11:09:05 +0200 | [diff] [blame] | 1746 | (1<<SNDRV_TIMER_EVENT_SUSPEND)| |
| 1747 | (1<<SNDRV_TIMER_EVENT_RESUME)| |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | (1<<SNDRV_TIMER_EVENT_MSTART)| |
| 1749 | (1<<SNDRV_TIMER_EVENT_MSTOP)| |
| 1750 | (1<<SNDRV_TIMER_EVENT_MCONTINUE)| |
Jaroslav Kysela | 65d11d9 | 2005-08-16 13:05:43 +0200 | [diff] [blame] | 1751 | (1<<SNDRV_TIMER_EVENT_MPAUSE)| |
| 1752 | (1<<SNDRV_TIMER_EVENT_MSUSPEND)| |
Jaroslav Kysela | a501dfa | 2005-08-16 11:09:05 +0200 | [diff] [blame] | 1753 | (1<<SNDRV_TIMER_EVENT_MRESUME))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | err = -EINVAL; |
| 1755 | goto _end; |
| 1756 | } |
| 1757 | snd_timer_stop(tu->timeri); |
| 1758 | spin_lock_irq(&t->lock); |
| 1759 | tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO| |
| 1760 | SNDRV_TIMER_IFLG_EXCLUSIVE| |
| 1761 | SNDRV_TIMER_IFLG_EARLY_EVENT); |
| 1762 | if (params.flags & SNDRV_TIMER_PSFLG_AUTO) |
| 1763 | tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO; |
| 1764 | if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE) |
| 1765 | tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE; |
| 1766 | if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT) |
| 1767 | tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT; |
| 1768 | spin_unlock_irq(&t->lock); |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1769 | if (params.queue_size > 0 && |
| 1770 | (unsigned int)tu->queue_size != params.queue_size) { |
Takashi Iwai | 890e2cb | 2017-06-02 17:16:59 +0200 | [diff] [blame] | 1771 | err = realloc_user_queue(tu, params.queue_size); |
| 1772 | if (err < 0) |
| 1773 | goto _end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | } |
Takashi Iwai | d7f910b | 2017-06-02 17:35:30 +0200 | [diff] [blame] | 1775 | spin_lock_irq(&tu->qlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | tu->qhead = tu->qtail = tu->qused = 0; |
| 1777 | if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) { |
| 1778 | if (tu->tread) { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1779 | struct snd_timer_tread tread; |
Kangjie Lu | cec8f96 | 2016-05-03 16:44:07 -0400 | [diff] [blame] | 1780 | memset(&tread, 0, sizeof(tread)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | tread.event = SNDRV_TIMER_EVENT_EARLY; |
| 1782 | tread.tstamp.tv_sec = 0; |
| 1783 | tread.tstamp.tv_nsec = 0; |
| 1784 | tread.val = 0; |
| 1785 | snd_timer_user_append_to_tqueue(tu, &tread); |
| 1786 | } else { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1787 | struct snd_timer_read *r = &tu->queue[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | r->resolution = 0; |
| 1789 | r->ticks = 0; |
| 1790 | tu->qused++; |
| 1791 | tu->qtail++; |
| 1792 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | } |
| 1794 | tu->filter = params.filter; |
| 1795 | tu->ticks = params.ticks; |
Takashi Iwai | d7f910b | 2017-06-02 17:35:30 +0200 | [diff] [blame] | 1796 | spin_unlock_irq(&tu->qlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | err = 0; |
| 1798 | _end: |
| 1799 | if (copy_to_user(_params, ¶ms, sizeof(params))) |
| 1800 | return -EFAULT; |
| 1801 | return err; |
| 1802 | } |
| 1803 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1804 | static int snd_timer_user_status(struct file *file, |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1805 | struct snd_timer_status __user *_status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1807 | struct snd_timer_user *tu; |
| 1808 | struct snd_timer_status status; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | tu = file->private_data; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1811 | if (!tu->timeri) |
| 1812 | return -EBADFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | memset(&status, 0, sizeof(status)); |
| 1814 | status.tstamp = tu->tstamp; |
| 1815 | status.resolution = snd_timer_resolution(tu->timeri); |
| 1816 | status.lost = tu->timeri->lost; |
| 1817 | status.overrun = tu->overrun; |
| 1818 | spin_lock_irq(&tu->qlock); |
| 1819 | status.queue = tu->qused; |
| 1820 | spin_unlock_irq(&tu->qlock); |
| 1821 | if (copy_to_user(_status, &status, sizeof(status))) |
| 1822 | return -EFAULT; |
| 1823 | return 0; |
| 1824 | } |
| 1825 | |
| 1826 | static int snd_timer_user_start(struct file *file) |
| 1827 | { |
| 1828 | int err; |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1829 | struct snd_timer_user *tu; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | tu = file->private_data; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1832 | if (!tu->timeri) |
| 1833 | return -EBADFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | snd_timer_stop(tu->timeri); |
| 1835 | tu->timeri->lost = 0; |
| 1836 | tu->last_resolution = 0; |
| 1837 | return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0; |
| 1838 | } |
| 1839 | |
| 1840 | static int snd_timer_user_stop(struct file *file) |
| 1841 | { |
| 1842 | int err; |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1843 | struct snd_timer_user *tu; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | tu = file->private_data; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1846 | if (!tu->timeri) |
| 1847 | return -EBADFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0; |
| 1849 | } |
| 1850 | |
| 1851 | static int snd_timer_user_continue(struct file *file) |
| 1852 | { |
| 1853 | int err; |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1854 | struct snd_timer_user *tu; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | tu = file->private_data; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1857 | if (!tu->timeri) |
| 1858 | return -EBADFD; |
Takashi Iwai | 9f8a765 | 2016-09-07 15:45:31 +0200 | [diff] [blame] | 1859 | /* start timer instead of continue if it's not used before */ |
| 1860 | if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED)) |
| 1861 | return snd_timer_user_start(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | tu->timeri->lost = 0; |
| 1863 | return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0; |
| 1864 | } |
| 1865 | |
Takashi Iwai | 15790a6 | 2005-05-15 15:04:14 +0200 | [diff] [blame] | 1866 | static int snd_timer_user_pause(struct file *file) |
| 1867 | { |
| 1868 | int err; |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1869 | struct snd_timer_user *tu; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1870 | |
Takashi Iwai | 15790a6 | 2005-05-15 15:04:14 +0200 | [diff] [blame] | 1871 | tu = file->private_data; |
Clemens Ladisch | 7c64ec3 | 2007-07-16 11:01:30 +0200 | [diff] [blame] | 1872 | if (!tu->timeri) |
| 1873 | return -EBADFD; |
Jaroslav Kysela | d138b44 | 2005-05-16 13:51:39 +0200 | [diff] [blame] | 1874 | return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0; |
Takashi Iwai | 15790a6 | 2005-05-15 15:04:14 +0200 | [diff] [blame] | 1875 | } |
| 1876 | |
Takashi Iwai | 8c50b37 | 2005-05-15 15:43:54 +0200 | [diff] [blame] | 1877 | enum { |
| 1878 | SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20), |
| 1879 | SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21), |
| 1880 | SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22), |
| 1881 | SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23), |
| 1882 | }; |
| 1883 | |
Takashi Iwai | af36802 | 2016-01-13 17:48:01 +0100 | [diff] [blame] | 1884 | static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd, |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1885 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1887 | struct snd_timer_user *tu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | void __user *argp = (void __user *)arg; |
| 1889 | int __user *p = argp; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | tu = file->private_data; |
| 1892 | switch (cmd) { |
| 1893 | case SNDRV_TIMER_IOCTL_PVERSION: |
| 1894 | return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0; |
| 1895 | case SNDRV_TIMER_IOCTL_NEXT_DEVICE: |
| 1896 | return snd_timer_user_next_device(argp); |
| 1897 | case SNDRV_TIMER_IOCTL_TREAD: |
| 1898 | { |
Takashi Iwai | 890e2cb | 2017-06-02 17:16:59 +0200 | [diff] [blame] | 1899 | int xarg, old_tread; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1900 | |
Takashi Iwai | af36802 | 2016-01-13 17:48:01 +0100 | [diff] [blame] | 1901 | if (tu->timeri) /* too late */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | return -EBUSY; |
Takashi Iwai | af36802 | 2016-01-13 17:48:01 +0100 | [diff] [blame] | 1903 | if (get_user(xarg, p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | return -EFAULT; |
Takashi Iwai | 890e2cb | 2017-06-02 17:16:59 +0200 | [diff] [blame] | 1905 | old_tread = tu->tread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | tu->tread = xarg ? 1 : 0; |
Takashi Iwai | 890e2cb | 2017-06-02 17:16:59 +0200 | [diff] [blame] | 1907 | if (tu->tread != old_tread && |
| 1908 | realloc_user_queue(tu, tu->queue_size) < 0) { |
| 1909 | tu->tread = old_tread; |
| 1910 | return -ENOMEM; |
| 1911 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | return 0; |
| 1913 | } |
| 1914 | case SNDRV_TIMER_IOCTL_GINFO: |
| 1915 | return snd_timer_user_ginfo(file, argp); |
| 1916 | case SNDRV_TIMER_IOCTL_GPARAMS: |
| 1917 | return snd_timer_user_gparams(file, argp); |
| 1918 | case SNDRV_TIMER_IOCTL_GSTATUS: |
| 1919 | return snd_timer_user_gstatus(file, argp); |
| 1920 | case SNDRV_TIMER_IOCTL_SELECT: |
| 1921 | return snd_timer_user_tselect(file, argp); |
| 1922 | case SNDRV_TIMER_IOCTL_INFO: |
| 1923 | return snd_timer_user_info(file, argp); |
| 1924 | case SNDRV_TIMER_IOCTL_PARAMS: |
| 1925 | return snd_timer_user_params(file, argp); |
| 1926 | case SNDRV_TIMER_IOCTL_STATUS: |
| 1927 | return snd_timer_user_status(file, argp); |
| 1928 | case SNDRV_TIMER_IOCTL_START: |
Takashi Iwai | 8c50b37 | 2005-05-15 15:43:54 +0200 | [diff] [blame] | 1929 | case SNDRV_TIMER_IOCTL_START_OLD: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | return snd_timer_user_start(file); |
| 1931 | case SNDRV_TIMER_IOCTL_STOP: |
Takashi Iwai | 8c50b37 | 2005-05-15 15:43:54 +0200 | [diff] [blame] | 1932 | case SNDRV_TIMER_IOCTL_STOP_OLD: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | return snd_timer_user_stop(file); |
| 1934 | case SNDRV_TIMER_IOCTL_CONTINUE: |
Takashi Iwai | 8c50b37 | 2005-05-15 15:43:54 +0200 | [diff] [blame] | 1935 | case SNDRV_TIMER_IOCTL_CONTINUE_OLD: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | return snd_timer_user_continue(file); |
Takashi Iwai | 15790a6 | 2005-05-15 15:04:14 +0200 | [diff] [blame] | 1937 | case SNDRV_TIMER_IOCTL_PAUSE: |
Takashi Iwai | 8c50b37 | 2005-05-15 15:43:54 +0200 | [diff] [blame] | 1938 | case SNDRV_TIMER_IOCTL_PAUSE_OLD: |
Takashi Iwai | 15790a6 | 2005-05-15 15:04:14 +0200 | [diff] [blame] | 1939 | return snd_timer_user_pause(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | } |
| 1941 | return -ENOTTY; |
| 1942 | } |
| 1943 | |
Takashi Iwai | af36802 | 2016-01-13 17:48:01 +0100 | [diff] [blame] | 1944 | static long snd_timer_user_ioctl(struct file *file, unsigned int cmd, |
| 1945 | unsigned long arg) |
| 1946 | { |
| 1947 | struct snd_timer_user *tu = file->private_data; |
| 1948 | long ret; |
| 1949 | |
| 1950 | mutex_lock(&tu->ioctl_lock); |
| 1951 | ret = __snd_timer_user_ioctl(file, cmd, arg); |
| 1952 | mutex_unlock(&tu->ioctl_lock); |
| 1953 | return ret; |
| 1954 | } |
| 1955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | static int snd_timer_user_fasync(int fd, struct file * file, int on) |
| 1957 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1958 | struct snd_timer_user *tu; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | tu = file->private_data; |
Jonathan Corbet | 60aa492 | 2009-02-01 14:52:56 -0700 | [diff] [blame] | 1961 | return fasync_helper(fd, file, on, &tu->fasync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | } |
| 1963 | |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1964 | static ssize_t snd_timer_user_read(struct file *file, char __user *buffer, |
| 1965 | size_t count, loff_t *offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | { |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1967 | struct snd_timer_user *tu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | long result = 0, unit; |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 1969 | int qhead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | int err = 0; |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 1971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | tu = file->private_data; |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 1973 | unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read); |
Takashi Iwai | d11662f | 2017-06-02 15:03:38 +0200 | [diff] [blame] | 1974 | mutex_lock(&tu->ioctl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | spin_lock_irq(&tu->qlock); |
| 1976 | while ((long)count - result >= unit) { |
| 1977 | while (!tu->qused) { |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 1978 | wait_queue_entry_t wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | |
| 1980 | if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) { |
| 1981 | err = -EAGAIN; |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 1982 | goto _error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | set_current_state(TASK_INTERRUPTIBLE); |
| 1986 | init_waitqueue_entry(&wait, current); |
| 1987 | add_wait_queue(&tu->qchange_sleep, &wait); |
| 1988 | |
| 1989 | spin_unlock_irq(&tu->qlock); |
Takashi Iwai | d11662f | 2017-06-02 15:03:38 +0200 | [diff] [blame] | 1990 | mutex_unlock(&tu->ioctl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | schedule(); |
Takashi Iwai | d11662f | 2017-06-02 15:03:38 +0200 | [diff] [blame] | 1992 | mutex_lock(&tu->ioctl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | spin_lock_irq(&tu->qlock); |
| 1994 | |
| 1995 | remove_wait_queue(&tu->qchange_sleep, &wait); |
| 1996 | |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 1997 | if (tu->disconnected) { |
| 1998 | err = -ENODEV; |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 1999 | goto _error; |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 2000 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | if (signal_pending(current)) { |
| 2002 | err = -ERESTARTSYS; |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 2003 | goto _error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | } |
| 2005 | } |
| 2006 | |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 2007 | qhead = tu->qhead++; |
| 2008 | tu->qhead %= tu->queue_size; |
Takashi Iwai | 3fa6993 | 2016-07-04 14:02:15 +0200 | [diff] [blame] | 2009 | tu->qused--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | spin_unlock_irq(&tu->qlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | |
| 2012 | if (tu->tread) { |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 2013 | if (copy_to_user(buffer, &tu->tqueue[qhead], |
| 2014 | sizeof(struct snd_timer_tread))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | } else { |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 2017 | if (copy_to_user(buffer, &tu->queue[qhead], |
| 2018 | sizeof(struct snd_timer_read))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | spin_lock_irq(&tu->qlock); |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 2023 | if (err < 0) |
| 2024 | goto _error; |
| 2025 | result += unit; |
| 2026 | buffer += unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | _error: |
Takashi Iwai | 4dff5c7 | 2016-02-08 17:26:58 +0100 | [diff] [blame] | 2029 | spin_unlock_irq(&tu->qlock); |
Takashi Iwai | d11662f | 2017-06-02 15:03:38 +0200 | [diff] [blame] | 2030 | mutex_unlock(&tu->ioctl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | return result > 0 ? result : err; |
| 2032 | } |
| 2033 | |
| 2034 | static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait) |
| 2035 | { |
| 2036 | unsigned int mask; |
Takashi Iwai | 53d2f744 | 2005-11-17 13:56:05 +0100 | [diff] [blame] | 2037 | struct snd_timer_user *tu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | |
| 2039 | tu = file->private_data; |
| 2040 | |
| 2041 | poll_wait(file, &tu->qchange_sleep, wait); |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 2042 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | mask = 0; |
Takashi Iwai | d7f910b | 2017-06-02 17:35:30 +0200 | [diff] [blame] | 2044 | spin_lock_irq(&tu->qlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | if (tu->qused) |
| 2046 | mask |= POLLIN | POLLRDNORM; |
Takashi Iwai | 230323d | 2016-01-21 17:19:31 +0100 | [diff] [blame] | 2047 | if (tu->disconnected) |
| 2048 | mask |= POLLERR; |
Takashi Iwai | d7f910b | 2017-06-02 17:35:30 +0200 | [diff] [blame] | 2049 | spin_unlock_irq(&tu->qlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | |
| 2051 | return mask; |
| 2052 | } |
| 2053 | |
| 2054 | #ifdef CONFIG_COMPAT |
| 2055 | #include "timer_compat.c" |
| 2056 | #else |
| 2057 | #define snd_timer_user_ioctl_compat NULL |
| 2058 | #endif |
| 2059 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 2060 | static const struct file_operations snd_timer_f_ops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | { |
| 2062 | .owner = THIS_MODULE, |
| 2063 | .read = snd_timer_user_read, |
| 2064 | .open = snd_timer_user_open, |
| 2065 | .release = snd_timer_user_release, |
Takashi Iwai | 02f4865 | 2010-04-13 11:49:04 +0200 | [diff] [blame] | 2066 | .llseek = no_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | .poll = snd_timer_user_poll, |
| 2068 | .unlocked_ioctl = snd_timer_user_ioctl, |
| 2069 | .compat_ioctl = snd_timer_user_ioctl_compat, |
| 2070 | .fasync = snd_timer_user_fasync, |
| 2071 | }; |
| 2072 | |
Takashi Iwai | 7c35860 | 2015-01-29 18:09:05 +0100 | [diff] [blame] | 2073 | /* unregister the system timer */ |
| 2074 | static void snd_timer_free_all(void) |
| 2075 | { |
| 2076 | struct snd_timer *timer, *n; |
| 2077 | |
| 2078 | list_for_each_entry_safe(timer, n, &snd_timer_list, device_list) |
| 2079 | snd_timer_free(timer); |
| 2080 | } |
| 2081 | |
Takashi Iwai | 89da061 | 2015-01-29 18:12:26 +0100 | [diff] [blame] | 2082 | static struct device timer_dev; |
| 2083 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | /* |
| 2085 | * ENTRY functions |
| 2086 | */ |
| 2087 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | static int __init alsa_timer_init(void) |
| 2089 | { |
| 2090 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | |
Takashi Iwai | 89da061 | 2015-01-29 18:12:26 +0100 | [diff] [blame] | 2092 | snd_device_initialize(&timer_dev, NULL); |
| 2093 | dev_set_name(&timer_dev, "timer"); |
| 2094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | #ifdef SNDRV_OSS_INFO_DEV_TIMERS |
Clemens Ladisch | 6b172a8 | 2005-10-12 17:20:21 +0200 | [diff] [blame] | 2096 | snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1, |
| 2097 | "system timer"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | #endif |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 2099 | |
Takashi Iwai | 7c35860 | 2015-01-29 18:09:05 +0100 | [diff] [blame] | 2100 | err = snd_timer_register_system(); |
| 2101 | if (err < 0) { |
Takashi Iwai | cf74dcf | 2014-02-04 18:22:39 +0100 | [diff] [blame] | 2102 | pr_err("ALSA: unable to register system timer (%i)\n", err); |
Markus Elfring | 1ae0e4c | 2017-08-23 09:30:41 +0200 | [diff] [blame] | 2103 | goto put_timer; |
Takashi Iwai | 7c35860 | 2015-01-29 18:09:05 +0100 | [diff] [blame] | 2104 | } |
| 2105 | |
Takashi Iwai | 40a4b26 | 2015-01-30 08:34:58 +0100 | [diff] [blame] | 2106 | err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0, |
| 2107 | &snd_timer_f_ops, NULL, &timer_dev); |
Takashi Iwai | 7c35860 | 2015-01-29 18:09:05 +0100 | [diff] [blame] | 2108 | if (err < 0) { |
Takashi Iwai | cf74dcf | 2014-02-04 18:22:39 +0100 | [diff] [blame] | 2109 | pr_err("ALSA: unable to register timer device (%i)\n", err); |
Takashi Iwai | 7c35860 | 2015-01-29 18:09:05 +0100 | [diff] [blame] | 2110 | snd_timer_free_all(); |
Markus Elfring | 1ae0e4c | 2017-08-23 09:30:41 +0200 | [diff] [blame] | 2111 | goto put_timer; |
Takashi Iwai | 7c35860 | 2015-01-29 18:09:05 +0100 | [diff] [blame] | 2112 | } |
| 2113 | |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 2114 | snd_timer_proc_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | return 0; |
Markus Elfring | 1ae0e4c | 2017-08-23 09:30:41 +0200 | [diff] [blame] | 2116 | |
| 2117 | put_timer: |
| 2118 | put_device(&timer_dev); |
| 2119 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | static void __exit alsa_timer_exit(void) |
| 2123 | { |
Takashi Iwai | 40a4b26 | 2015-01-30 08:34:58 +0100 | [diff] [blame] | 2124 | snd_unregister_device(&timer_dev); |
Takashi Iwai | 7c35860 | 2015-01-29 18:09:05 +0100 | [diff] [blame] | 2125 | snd_timer_free_all(); |
Takashi Iwai | 89da061 | 2015-01-29 18:12:26 +0100 | [diff] [blame] | 2126 | put_device(&timer_dev); |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 2127 | snd_timer_proc_done(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | #ifdef SNDRV_OSS_INFO_DEV_TIMERS |
| 2129 | snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1); |
| 2130 | #endif |
| 2131 | } |
| 2132 | |
| 2133 | module_init(alsa_timer_init) |
| 2134 | module_exit(alsa_timer_exit) |