ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / core / pcm_timer.c
1 /*
2  *  Digital Audio (PCM) 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/time.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/timer.h>
27
28 #define chip_t snd_pcm_substream_t
29
30 /*
31  *  Timer functions
32  */
33
34 /* Greatest common divisor */
35 static unsigned long gcd(unsigned long a, unsigned long b)
36 {
37         unsigned long r;
38         if (a < b) {
39                 r = a;
40                 a = b;
41                 b = r;
42         }
43         while ((r = a % b) != 0) {
44                 a = b;
45                 b = r;
46         }
47         return b;
48 }
49
50 void snd_pcm_timer_resolution_change(snd_pcm_substream_t *substream)
51 {
52         unsigned long rate, mult, fsize, l;
53         snd_pcm_runtime_t *runtime = substream->runtime;
54         
55         mult = 1000000000;
56         rate = runtime->rate;
57         snd_assert(rate != 0, return);
58         l = gcd(mult, rate);
59         mult /= l;
60         rate /= l;
61         fsize = runtime->period_size;
62         snd_assert(fsize != 0, return);
63         l = gcd(rate, fsize);
64         rate /= l;
65         fsize /= l;
66         while ((mult * fsize) / fsize != mult) {
67                 mult /= 2;
68                 rate /= 2;
69         }
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;
74         }
75         runtime->timer_resolution = mult * fsize / rate;
76 }
77
78 static unsigned long snd_pcm_timer_resolution(snd_timer_t * timer)
79 {
80         snd_pcm_substream_t * substream;
81         
82         substream = snd_magic_cast(snd_pcm_substream_t, timer->private_data, return -ENXIO);
83         return substream->runtime ? substream->runtime->timer_resolution : 0;
84 }
85
86 static int snd_pcm_timer_start(snd_timer_t * timer)
87 {
88         unsigned long flags;
89         snd_pcm_substream_t * substream;
90         
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;
96 }
97
98 static int snd_pcm_timer_stop(snd_timer_t * timer)
99 {
100         unsigned long flags;
101         snd_pcm_substream_t * substream;
102         
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;
108 }
109
110 static struct _snd_timer_hardware snd_pcm_timer =
111 {
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,
118 };
119
120 /*
121  *  Init functions
122  */
123
124 static void snd_pcm_timer_free(snd_timer_t *timer)
125 {
126         snd_pcm_substream_t *substream = snd_magic_cast(snd_pcm_substream_t, timer->private_data, return);
127         substream->timer = NULL;
128 }
129
130 void snd_pcm_timer_init(snd_pcm_substream_t *substream)
131 {
132         snd_timer_id_t tid;
133         snd_timer_t *timer;
134         
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;
150         }
151         timer->private_data = substream;
152         timer->private_free = snd_pcm_timer_free;
153         substream->timer = timer;
154 }
155
156 void snd_pcm_timer_done(snd_pcm_substream_t *substream)
157 {
158         if (substream->timer) {
159                 snd_device_free(substream->pcm->card, substream->timer);
160                 substream->timer = NULL;
161         }
162 }