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