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