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