blob: ca8068b63d6c1bc413d16a2309d7d33fbe378893 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/time.h>
23#include <sound/core.h>
24#include <sound/pcm.h>
25#include <sound/timer.h>
26
27/*
28 * Timer functions
29 */
30
31/* Greatest common divisor */
32static unsigned long gcd(unsigned long a, unsigned long b)
33{
34 unsigned long r;
35 if (a < b) {
36 r = a;
37 a = b;
38 b = r;
39 }
40 while ((r = a % b) != 0) {
41 a = b;
42 b = r;
43 }
44 return b;
45}
46
Takashi Iwai877211f2005-11-17 13:59:38 +010047void snd_pcm_timer_resolution_change(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 unsigned long rate, mult, fsize, l, post;
Takashi Iwai877211f2005-11-17 13:59:38 +010050 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 mult = 1000000000;
53 rate = runtime->rate;
Takashi Iwai7eaa9432008-08-08 17:09:09 +020054 if (snd_BUG_ON(!rate))
55 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 l = gcd(mult, rate);
57 mult /= l;
58 rate /= l;
59 fsize = runtime->period_size;
Takashi Iwai7eaa9432008-08-08 17:09:09 +020060 if (snd_BUG_ON(!fsize))
61 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 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;
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) * post;
76}
77
Takashi Iwai877211f2005-11-17 13:59:38 +010078static unsigned long snd_pcm_timer_resolution(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Takashi Iwai877211f2005-11-17 13:59:38 +010080 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 substream = timer->private_data;
83 return substream->runtime ? substream->runtime->timer_resolution : 0;
84}
85
Takashi Iwai877211f2005-11-17 13:59:38 +010086static int snd_pcm_timer_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Takashi Iwai877211f2005-11-17 13:59:38 +010088 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 substream = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 substream->timer_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return 0;
93}
94
Takashi Iwai877211f2005-11-17 13:59:38 +010095static int snd_pcm_timer_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Takashi Iwai877211f2005-11-17 13:59:38 +010097 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 substream = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 substream->timer_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return 0;
102}
103
Takashi Iwai877211f2005-11-17 13:59:38 +0100104static struct snd_timer_hardware snd_pcm_timer =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 .flags = SNDRV_TIMER_HW_AUTO | SNDRV_TIMER_HW_SLAVE,
107 .resolution = 0,
108 .ticks = 1,
109 .c_resolution = snd_pcm_timer_resolution,
110 .start = snd_pcm_timer_start,
111 .stop = snd_pcm_timer_stop,
112};
113
114/*
115 * Init functions
116 */
117
Takashi Iwai877211f2005-11-17 13:59:38 +0100118static void snd_pcm_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Takashi Iwai877211f2005-11-17 13:59:38 +0100120 struct snd_pcm_substream *substream = timer->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 substream->timer = NULL;
122}
123
Takashi Iwai877211f2005-11-17 13:59:38 +0100124void snd_pcm_timer_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Takashi Iwai877211f2005-11-17 13:59:38 +0100126 struct snd_timer_id tid;
127 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
130 tid.dev_class = SNDRV_TIMER_CLASS_PCM;
131 tid.card = substream->pcm->card->number;
132 tid.device = substream->pcm->device;
133 tid.subdevice = (substream->number << 1) | (substream->stream & 1);
134 if (snd_timer_new(substream->pcm->card, "PCM", &tid, &timer) < 0)
135 return;
136 sprintf(timer->name, "PCM %s %i-%i-%i",
137 substream->stream == SNDRV_PCM_STREAM_CAPTURE ?
138 "capture" : "playback",
139 tid.card, tid.device, tid.subdevice);
140 timer->hw = snd_pcm_timer;
141 if (snd_device_register(timer->card, timer) < 0) {
142 snd_device_free(timer->card, timer);
143 return;
144 }
145 timer->private_data = substream;
146 timer->private_free = snd_pcm_timer_free;
147 substream->timer = timer;
148}
149
Takashi Iwai877211f2005-11-17 13:59:38 +0100150void snd_pcm_timer_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 if (substream->timer) {
153 snd_device_free(substream->pcm->card, substream->timer);
154 substream->timer = NULL;
155 }
156}