Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Digital Audio (PCM) abstract layer |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #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 */ |
| 32 | static 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 Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 47 | void snd_pcm_timer_resolution_change(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
| 49 | unsigned long rate, mult, fsize, l, post; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 50 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | mult = 1000000000; |
| 53 | rate = runtime->rate; |
| 54 | snd_assert(rate != 0, return); |
| 55 | l = gcd(mult, rate); |
| 56 | mult /= l; |
| 57 | rate /= l; |
| 58 | fsize = runtime->period_size; |
| 59 | snd_assert(fsize != 0, return); |
| 60 | l = gcd(rate, fsize); |
| 61 | rate /= l; |
| 62 | fsize /= l; |
| 63 | post = 1; |
| 64 | while ((mult * fsize) / fsize != mult) { |
| 65 | mult /= 2; |
| 66 | post *= 2; |
| 67 | } |
| 68 | if (rate == 0) { |
| 69 | snd_printk(KERN_ERR "pcm timer resolution out of range (rate = %u, period_size = %lu)\n", runtime->rate, runtime->period_size); |
| 70 | runtime->timer_resolution = -1; |
| 71 | return; |
| 72 | } |
| 73 | runtime->timer_resolution = (mult * fsize / rate) * post; |
| 74 | } |
| 75 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 76 | static unsigned long snd_pcm_timer_resolution(struct snd_timer * timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 78 | struct snd_pcm_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | substream = timer->private_data; |
| 81 | return substream->runtime ? substream->runtime->timer_resolution : 0; |
| 82 | } |
| 83 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 84 | static int snd_pcm_timer_start(struct snd_timer * timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | unsigned long flags; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 87 | struct snd_pcm_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | substream = snd_timer_chip(timer); |
| 90 | spin_lock_irqsave(&substream->timer_lock, flags); |
| 91 | substream->timer_running = 1; |
| 92 | spin_unlock_irqrestore(&substream->timer_lock, flags); |
| 93 | return 0; |
| 94 | } |
| 95 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 96 | static int snd_pcm_timer_stop(struct snd_timer * timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
| 98 | unsigned long flags; |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 99 | struct snd_pcm_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | substream = snd_timer_chip(timer); |
| 102 | spin_lock_irqsave(&substream->timer_lock, flags); |
| 103 | substream->timer_running = 0; |
| 104 | spin_unlock_irqrestore(&substream->timer_lock, flags); |
| 105 | return 0; |
| 106 | } |
| 107 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 108 | static struct snd_timer_hardware snd_pcm_timer = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
| 110 | .flags = SNDRV_TIMER_HW_AUTO | SNDRV_TIMER_HW_SLAVE, |
| 111 | .resolution = 0, |
| 112 | .ticks = 1, |
| 113 | .c_resolution = snd_pcm_timer_resolution, |
| 114 | .start = snd_pcm_timer_start, |
| 115 | .stop = snd_pcm_timer_stop, |
| 116 | }; |
| 117 | |
| 118 | /* |
| 119 | * Init functions |
| 120 | */ |
| 121 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 122 | static void snd_pcm_timer_free(struct snd_timer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 124 | struct snd_pcm_substream *substream = timer->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | substream->timer = NULL; |
| 126 | } |
| 127 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 128 | void snd_pcm_timer_init(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 130 | struct snd_timer_id tid; |
| 131 | struct snd_timer *timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE; |
| 134 | tid.dev_class = SNDRV_TIMER_CLASS_PCM; |
| 135 | tid.card = substream->pcm->card->number; |
| 136 | tid.device = substream->pcm->device; |
| 137 | tid.subdevice = (substream->number << 1) | (substream->stream & 1); |
| 138 | if (snd_timer_new(substream->pcm->card, "PCM", &tid, &timer) < 0) |
| 139 | return; |
| 140 | sprintf(timer->name, "PCM %s %i-%i-%i", |
| 141 | substream->stream == SNDRV_PCM_STREAM_CAPTURE ? |
| 142 | "capture" : "playback", |
| 143 | tid.card, tid.device, tid.subdevice); |
| 144 | timer->hw = snd_pcm_timer; |
| 145 | if (snd_device_register(timer->card, timer) < 0) { |
| 146 | snd_device_free(timer->card, timer); |
| 147 | return; |
| 148 | } |
| 149 | timer->private_data = substream; |
| 150 | timer->private_free = snd_pcm_timer_free; |
| 151 | substream->timer = timer; |
| 152 | } |
| 153 | |
Takashi Iwai | 877211f | 2005-11-17 13:59:38 +0100 | [diff] [blame] | 154 | void snd_pcm_timer_done(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | { |
| 156 | if (substream->timer) { |
| 157 | snd_device_free(substream->pcm->card, substream->timer); |
| 158 | substream->timer = NULL; |
| 159 | } |
| 160 | } |