Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Digital Audio (PCM) abstract layer
0004  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
0005  */
0006 
0007 #include <linux/time.h>
0008 #include <linux/gcd.h>
0009 #include <sound/core.h>
0010 #include <sound/pcm.h>
0011 #include <sound/timer.h>
0012 
0013 #include "pcm_local.h"
0014 
0015 /*
0016  *  Timer functions
0017  */
0018 
0019 void snd_pcm_timer_resolution_change(struct snd_pcm_substream *substream)
0020 {
0021     unsigned long rate, mult, fsize, l, post;
0022     struct snd_pcm_runtime *runtime = substream->runtime;
0023 
0024     mult = 1000000000;
0025     rate = runtime->rate;
0026     if (snd_BUG_ON(!rate))
0027         return;
0028     l = gcd(mult, rate);
0029     mult /= l;
0030     rate /= l;
0031     fsize = runtime->period_size;
0032     if (snd_BUG_ON(!fsize))
0033         return;
0034     l = gcd(rate, fsize);
0035     rate /= l;
0036     fsize /= l;
0037     post = 1;
0038     while ((mult * fsize) / fsize != mult) {
0039         mult /= 2;
0040         post *= 2;
0041     }
0042     if (rate == 0) {
0043         pcm_err(substream->pcm,
0044             "pcm timer resolution out of range (rate = %u, period_size = %lu)\n",
0045             runtime->rate, runtime->period_size);
0046         runtime->timer_resolution = -1;
0047         return;
0048     }
0049     runtime->timer_resolution = (mult * fsize / rate) * post;
0050 }
0051 
0052 static unsigned long snd_pcm_timer_resolution(struct snd_timer * timer)
0053 {
0054     struct snd_pcm_substream *substream;
0055 
0056     substream = timer->private_data;
0057     return substream->runtime ? substream->runtime->timer_resolution : 0;
0058 }
0059 
0060 static int snd_pcm_timer_start(struct snd_timer * timer)
0061 {
0062     struct snd_pcm_substream *substream;
0063 
0064     substream = snd_timer_chip(timer);
0065     substream->timer_running = 1;
0066     return 0;
0067 }
0068 
0069 static int snd_pcm_timer_stop(struct snd_timer * timer)
0070 {
0071     struct snd_pcm_substream *substream;
0072 
0073     substream = snd_timer_chip(timer);
0074     substream->timer_running = 0;
0075     return 0;
0076 }
0077 
0078 static const struct snd_timer_hardware snd_pcm_timer =
0079 {
0080     .flags =    SNDRV_TIMER_HW_AUTO | SNDRV_TIMER_HW_SLAVE,
0081     .resolution =   0,
0082     .ticks =    1,
0083     .c_resolution = snd_pcm_timer_resolution,
0084     .start =    snd_pcm_timer_start,
0085     .stop =     snd_pcm_timer_stop,
0086 };
0087 
0088 /*
0089  *  Init functions
0090  */
0091 
0092 static void snd_pcm_timer_free(struct snd_timer *timer)
0093 {
0094     struct snd_pcm_substream *substream = timer->private_data;
0095     substream->timer = NULL;
0096 }
0097 
0098 void snd_pcm_timer_init(struct snd_pcm_substream *substream)
0099 {
0100     struct snd_timer_id tid;
0101     struct snd_timer *timer;
0102 
0103     tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
0104     tid.dev_class = SNDRV_TIMER_CLASS_PCM;
0105     tid.card = substream->pcm->card->number;
0106     tid.device = substream->pcm->device;
0107     tid.subdevice = (substream->number << 1) | (substream->stream & 1);
0108     if (snd_timer_new(substream->pcm->card, "PCM", &tid, &timer) < 0)
0109         return;
0110     sprintf(timer->name, "PCM %s %i-%i-%i",
0111             substream->stream == SNDRV_PCM_STREAM_CAPTURE ?
0112                 "capture" : "playback",
0113             tid.card, tid.device, tid.subdevice);
0114     timer->hw = snd_pcm_timer;
0115     if (snd_device_register(timer->card, timer) < 0) {
0116         snd_device_free(timer->card, timer);
0117         return;
0118     }
0119     timer->private_data = substream;
0120     timer->private_free = snd_pcm_timer_free;
0121     substream->timer = timer;
0122 }
0123 
0124 void snd_pcm_timer_done(struct snd_pcm_substream *substream)
0125 {
0126     if (substream->timer) {
0127         snd_device_free(substream->pcm->card, substream->timer);
0128         substream->timer = NULL;
0129     }
0130 }