Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Digital Audio (PCM) 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 | 22 | #include <linux/time.h> |
a9605391 | 23 | #include <linux/gcd.h> |
1da177e4 LT |
24 | #include <sound/core.h> |
25 | #include <sound/pcm.h> | |
26 | #include <sound/timer.h> | |
27 | ||
28 | /* | |
29 | * Timer functions | |
30 | */ | |
31 | ||
877211f5 | 32 | void snd_pcm_timer_resolution_change(struct snd_pcm_substream *substream) |
1da177e4 LT |
33 | { |
34 | unsigned long rate, mult, fsize, l, post; | |
877211f5 | 35 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
36 | |
37 | mult = 1000000000; | |
38 | rate = runtime->rate; | |
7eaa943c TI |
39 | if (snd_BUG_ON(!rate)) |
40 | return; | |
1da177e4 LT |
41 | l = gcd(mult, rate); |
42 | mult /= l; | |
43 | rate /= l; | |
44 | fsize = runtime->period_size; | |
7eaa943c TI |
45 | if (snd_BUG_ON(!fsize)) |
46 | return; | |
1da177e4 LT |
47 | l = gcd(rate, fsize); |
48 | rate /= l; | |
49 | fsize /= l; | |
50 | post = 1; | |
51 | while ((mult * fsize) / fsize != mult) { | |
52 | mult /= 2; | |
53 | post *= 2; | |
54 | } | |
55 | if (rate == 0) { | |
09e56df8 TI |
56 | pcm_err(substream->pcm, |
57 | "pcm timer resolution out of range (rate = %u, period_size = %lu)\n", | |
58 | runtime->rate, runtime->period_size); | |
1da177e4 LT |
59 | runtime->timer_resolution = -1; |
60 | return; | |
61 | } | |
62 | runtime->timer_resolution = (mult * fsize / rate) * post; | |
63 | } | |
64 | ||
877211f5 | 65 | static unsigned long snd_pcm_timer_resolution(struct snd_timer * timer) |
1da177e4 | 66 | { |
877211f5 | 67 | struct snd_pcm_substream *substream; |
1da177e4 LT |
68 | |
69 | substream = timer->private_data; | |
70 | return substream->runtime ? substream->runtime->timer_resolution : 0; | |
71 | } | |
72 | ||
877211f5 | 73 | static int snd_pcm_timer_start(struct snd_timer * timer) |
1da177e4 | 74 | { |
877211f5 | 75 | struct snd_pcm_substream *substream; |
1da177e4 LT |
76 | |
77 | substream = snd_timer_chip(timer); | |
1da177e4 | 78 | substream->timer_running = 1; |
1da177e4 LT |
79 | return 0; |
80 | } | |
81 | ||
877211f5 | 82 | static int snd_pcm_timer_stop(struct snd_timer * timer) |
1da177e4 | 83 | { |
877211f5 | 84 | struct snd_pcm_substream *substream; |
1da177e4 LT |
85 | |
86 | substream = snd_timer_chip(timer); | |
1da177e4 | 87 | substream->timer_running = 0; |
1da177e4 LT |
88 | return 0; |
89 | } | |
90 | ||
877211f5 | 91 | static struct snd_timer_hardware snd_pcm_timer = |
1da177e4 LT |
92 | { |
93 | .flags = SNDRV_TIMER_HW_AUTO | SNDRV_TIMER_HW_SLAVE, | |
94 | .resolution = 0, | |
95 | .ticks = 1, | |
96 | .c_resolution = snd_pcm_timer_resolution, | |
97 | .start = snd_pcm_timer_start, | |
98 | .stop = snd_pcm_timer_stop, | |
99 | }; | |
100 | ||
101 | /* | |
102 | * Init functions | |
103 | */ | |
104 | ||
877211f5 | 105 | static void snd_pcm_timer_free(struct snd_timer *timer) |
1da177e4 | 106 | { |
877211f5 | 107 | struct snd_pcm_substream *substream = timer->private_data; |
1da177e4 LT |
108 | substream->timer = NULL; |
109 | } | |
110 | ||
877211f5 | 111 | void snd_pcm_timer_init(struct snd_pcm_substream *substream) |
1da177e4 | 112 | { |
877211f5 TI |
113 | struct snd_timer_id tid; |
114 | struct snd_timer *timer; | |
1da177e4 LT |
115 | |
116 | tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE; | |
117 | tid.dev_class = SNDRV_TIMER_CLASS_PCM; | |
118 | tid.card = substream->pcm->card->number; | |
119 | tid.device = substream->pcm->device; | |
120 | tid.subdevice = (substream->number << 1) | (substream->stream & 1); | |
121 | if (snd_timer_new(substream->pcm->card, "PCM", &tid, &timer) < 0) | |
122 | return; | |
123 | sprintf(timer->name, "PCM %s %i-%i-%i", | |
124 | substream->stream == SNDRV_PCM_STREAM_CAPTURE ? | |
125 | "capture" : "playback", | |
126 | tid.card, tid.device, tid.subdevice); | |
127 | timer->hw = snd_pcm_timer; | |
128 | if (snd_device_register(timer->card, timer) < 0) { | |
129 | snd_device_free(timer->card, timer); | |
130 | return; | |
131 | } | |
132 | timer->private_data = substream; | |
133 | timer->private_free = snd_pcm_timer_free; | |
134 | substream->timer = timer; | |
135 | } | |
136 | ||
877211f5 | 137 | void snd_pcm_timer_done(struct snd_pcm_substream *substream) |
1da177e4 LT |
138 | { |
139 | if (substream->timer) { | |
140 | snd_device_free(substream->pcm->card, substream->timer); | |
141 | substream->timer = NULL; | |
142 | } | |
143 | } |