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