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