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