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