added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / sound / core / pcm_timer.c
blob2c89c04f29163766dde1c6b9f6f7a296310343f3
1 /*
2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
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.
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.
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
22 #include <linux/time.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/timer.h>
28 * Timer functions
31 /* Greatest common divisor */
32 static unsigned long gcd(unsigned long a, unsigned long b)
34 unsigned long r;
35 if (a < b) {
36 r = a;
37 a = b;
38 b = r;
40 while ((r = a % b) != 0) {
41 a = b;
42 b = r;
44 return b;
47 void snd_pcm_timer_resolution_change(struct snd_pcm_substream *substream)
49 unsigned long rate, mult, fsize, l, post;
50 struct snd_pcm_runtime *runtime = substream->runtime;
52 mult = 1000000000;
53 rate = runtime->rate;
54 if (snd_BUG_ON(!rate))
55 return;
56 l = gcd(mult, rate);
57 mult /= l;
58 rate /= l;
59 fsize = runtime->period_size;
60 if (snd_BUG_ON(!fsize))
61 return;
62 l = gcd(rate, fsize);
63 rate /= l;
64 fsize /= l;
65 post = 1;
66 while ((mult * fsize) / fsize != mult) {
67 mult /= 2;
68 post *= 2;
70 if (rate == 0) {
71 snd_printk(KERN_ERR "pcm timer resolution out of range (rate = %u, period_size = %lu)\n", runtime->rate, runtime->period_size);
72 runtime->timer_resolution = -1;
73 return;
75 runtime->timer_resolution = (mult * fsize / rate) * post;
78 static unsigned long snd_pcm_timer_resolution(struct snd_timer * timer)
80 struct snd_pcm_substream *substream;
82 substream = timer->private_data;
83 return substream->runtime ? substream->runtime->timer_resolution : 0;
86 static int snd_pcm_timer_start(struct snd_timer * timer)
88 unsigned long flags;
89 struct snd_pcm_substream *substream;
91 substream = snd_timer_chip(timer);
92 spin_lock_irqsave(&substream->timer_lock, flags);
93 substream->timer_running = 1;
94 spin_unlock_irqrestore(&substream->timer_lock, flags);
95 return 0;
98 static int snd_pcm_timer_stop(struct snd_timer * timer)
100 unsigned long flags;
101 struct snd_pcm_substream *substream;
103 substream = snd_timer_chip(timer);
104 spin_lock_irqsave(&substream->timer_lock, flags);
105 substream->timer_running = 0;
106 spin_unlock_irqrestore(&substream->timer_lock, flags);
107 return 0;
110 static struct snd_timer_hardware snd_pcm_timer =
112 .flags = SNDRV_TIMER_HW_AUTO | SNDRV_TIMER_HW_SLAVE,
113 .resolution = 0,
114 .ticks = 1,
115 .c_resolution = snd_pcm_timer_resolution,
116 .start = snd_pcm_timer_start,
117 .stop = snd_pcm_timer_stop,
121 * Init functions
124 static void snd_pcm_timer_free(struct snd_timer *timer)
126 struct snd_pcm_substream *substream = timer->private_data;
127 substream->timer = NULL;
130 void snd_pcm_timer_init(struct snd_pcm_substream *substream)
132 struct snd_timer_id tid;
133 struct snd_timer *timer;
135 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
136 tid.dev_class = SNDRV_TIMER_CLASS_PCM;
137 tid.card = substream->pcm->card->number;
138 tid.device = substream->pcm->device;
139 tid.subdevice = (substream->number << 1) | (substream->stream & 1);
140 if (snd_timer_new(substream->pcm->card, "PCM", &tid, &timer) < 0)
141 return;
142 sprintf(timer->name, "PCM %s %i-%i-%i",
143 substream->stream == SNDRV_PCM_STREAM_CAPTURE ?
144 "capture" : "playback",
145 tid.card, tid.device, tid.subdevice);
146 timer->hw = snd_pcm_timer;
147 if (snd_device_register(timer->card, timer) < 0) {
148 snd_device_free(timer->card, timer);
149 return;
151 timer->private_data = substream;
152 timer->private_free = snd_pcm_timer_free;
153 substream->timer = timer;
156 void snd_pcm_timer_done(struct snd_pcm_substream *substream)
158 if (substream->timer) {
159 snd_device_free(substream->pcm->card, substream->timer);
160 substream->timer = NULL;