ASoC: SOF: fix an Oops, caused by invalid topology
[sfrench/cifs-2.6.git] / sound / soc / sof / pcm.c
index 549238a98b2ab5bda27d411ecd2de67bd427c24c..29435ba2d32927ea6cd4fd2154d5ac59d6eb3483 100644 (file)
 #include <sound/pcm_params.h>
 #include <sound/sof.h>
 #include "sof-priv.h"
+#include "sof-audio.h"
 #include "ops.h"
 
-#define DRV_NAME       "sof-audio-component"
-
 /* Create DMA buffer page table for DSP */
 static int create_page_table(struct snd_soc_component *component,
                             struct snd_pcm_substream *substream,
                             unsigned char *dma_area, size_t size)
 {
        struct snd_soc_pcm_runtime *rtd = substream->private_data;
-       struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
        struct snd_sof_pcm *spcm;
        struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
        int stream = substream->stream;
 
-       spcm = snd_sof_find_spcm_dai(sdev, rtd);
+       spcm = snd_sof_find_spcm_dai(component, rtd);
        if (!spcm)
                return -EINVAL;
 
-       return snd_sof_create_page_table(sdev, dmab,
+       return snd_sof_create_page_table(component->dev, dmab,
                spcm->stream[stream].page_table.area, size);
 }
 
 static int sof_pcm_dsp_params(struct snd_sof_pcm *spcm, struct snd_pcm_substream *substream,
                              const struct sof_ipc_pcm_params_reply *reply)
 {
-       struct snd_sof_dev *sdev = spcm->sdev;
+       struct snd_soc_component *scomp = spcm->scomp;
+       struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
+
        /* validate offset */
        int ret = snd_sof_ipc_pcm_params(sdev, substream, reply);
 
        if (ret < 0)
-               dev_err(sdev->dev, "error: got wrong reply for PCM %d\n",
+               dev_err(scomp->dev, "error: got wrong reply for PCM %d\n",
                        spcm->pcm.pcm_id);
 
        return ret;
@@ -70,13 +70,12 @@ void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream)
 {
        struct snd_soc_pcm_runtime *rtd = substream->private_data;
        struct snd_soc_component *component =
-               snd_soc_rtdcom_lookup(rtd, DRV_NAME);
-       struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
+               snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
        struct snd_sof_pcm *spcm;
 
-       spcm = snd_sof_find_spcm_dai(sdev, rtd);
+       spcm = snd_sof_find_spcm_dai(component, rtd);
        if (!spcm) {
-               dev_err(sdev->dev,
+               dev_err(component->dev,
                        "error: period elapsed for unknown stream!\n");
                return;
        }
@@ -93,7 +92,27 @@ void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream)
 }
 EXPORT_SYMBOL(snd_sof_pcm_period_elapsed);
 
-/* this may get called several times by oss emulation */
+static int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream,
+                               struct snd_sof_dev *sdev,
+                               struct snd_sof_pcm *spcm)
+{
+       struct sof_ipc_stream stream;
+       struct sof_ipc_reply reply;
+       int ret;
+
+       stream.hdr.size = sizeof(stream);
+       stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
+       stream.comp_id = spcm->stream[substream->stream].comp_id;
+
+       /* send IPC to the DSP */
+       ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
+                                sizeof(stream), &reply, sizeof(reply));
+       if (!ret)
+               spcm->prepared[substream->stream] = false;
+
+       return ret;
+}
+
 static int sof_pcm_hw_params(struct snd_soc_component *component,
                             struct snd_pcm_substream *substream,
                             struct snd_pcm_hw_params *params)
@@ -110,29 +129,27 @@ static int sof_pcm_hw_params(struct snd_soc_component *component,
        if (rtd->dai_link->no_pcm)
                return 0;
 
-       spcm = snd_sof_find_spcm_dai(sdev, rtd);
+       spcm = snd_sof_find_spcm_dai(component, rtd);
        if (!spcm)
                return -EINVAL;
 
-       dev_dbg(sdev->dev, "pcm: hw params stream %d dir %d\n",
+       /*
+        * Handle repeated calls to hw_params() without free_pcm() in
+        * between. At least ALSA OSS emulation depends on this.
+        */
+       if (spcm->prepared[substream->stream]) {
+               ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
+               if (ret < 0)
+                       return ret;
+       }
+
+       dev_dbg(component->dev, "pcm: hw params stream %d dir %d\n",
                spcm->pcm.pcm_id, substream->stream);
 
        memset(&pcm, 0, sizeof(pcm));
 
-       /* allocate audio buffer pages */
-       ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
-       if (ret < 0) {
-               dev_err(sdev->dev, "error: could not allocate %d bytes for PCM %d\n",
-                       params_buffer_bytes(params), spcm->pcm.pcm_id);
-               return ret;
-       }
-       if (ret) {
-               /*
-                * ret == 1 means the buffer is changed
-                * create compressed page table for audio firmware
-                * ret == 0 means the buffer is not changed
-                * so no need to regenerate the page table
-                */
+       /* create compressed page table for audio firmware */
+       if (runtime->buffer_changed) {
                ret = create_page_table(component, substream, runtime->dma_area,
                                        runtime->dma_bytes);
                if (ret < 0)
@@ -187,17 +204,17 @@ static int sof_pcm_hw_params(struct snd_soc_component *component,
                                             params,
                                             &pcm.params);
        if (ret < 0) {
-               dev_err(sdev->dev, "error: platform hw params failed\n");
+               dev_err(component->dev, "error: platform hw params failed\n");
                return ret;
        }
 
-       dev_dbg(sdev->dev, "stream_tag %d", pcm.params.stream_tag);
+       dev_dbg(component->dev, "stream_tag %d", pcm.params.stream_tag);
 
        /* send IPC to the DSP */
        ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
                                 &ipc_params_reply, sizeof(ipc_params_reply));
        if (ret < 0) {
-               dev_err(sdev->dev, "error: hw params ipc failed for stream %d\n",
+               dev_err(component->dev, "error: hw params ipc failed for stream %d\n",
                        pcm.params.stream_tag);
                return ret;
        }
@@ -214,27 +231,6 @@ static int sof_pcm_hw_params(struct snd_soc_component *component,
        return ret;
 }
 
-static int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream,
-                               struct snd_sof_dev *sdev,
-                               struct snd_sof_pcm *spcm)
-{
-       struct sof_ipc_stream stream;
-       struct sof_ipc_reply reply;
-       int ret;
-
-       stream.hdr.size = sizeof(stream);
-       stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
-       stream.comp_id = spcm->stream[substream->stream].comp_id;
-
-       /* send IPC to the DSP */
-       ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
-                                sizeof(stream), &reply, sizeof(reply));
-       if (!ret)
-               spcm->prepared[substream->stream] = false;
-
-       return ret;
-}
-
 static int sof_pcm_hw_free(struct snd_soc_component *component,
                           struct snd_pcm_substream *substream)
 {
@@ -247,12 +243,12 @@ static int sof_pcm_hw_free(struct snd_soc_component *component,
        if (rtd->dai_link->no_pcm)
                return 0;
 
-       spcm = snd_sof_find_spcm_dai(sdev, rtd);
+       spcm = snd_sof_find_spcm_dai(component, rtd);
        if (!spcm)
                return -EINVAL;
 
-       dev_dbg(sdev->dev, "pcm: free stream %d dir %d\n", spcm->pcm.pcm_id,
-               substream->stream);
+       dev_dbg(component->dev, "pcm: free stream %d dir %d\n",
+               spcm->pcm.pcm_id, substream->stream);
 
        if (spcm->prepared[substream->stream]) {
                ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
@@ -260,13 +256,11 @@ static int sof_pcm_hw_free(struct snd_soc_component *component,
                        err = ret;
        }
 
-       snd_pcm_lib_free_pages(substream);
-
        cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
 
        ret = snd_sof_pcm_platform_hw_free(sdev, substream);
        if (ret < 0) {
-               dev_err(sdev->dev, "error: platform hw free failed\n");
+               dev_err(component->dev, "error: platform hw free failed\n");
                err = ret;
        }
 
@@ -277,7 +271,6 @@ static int sof_pcm_prepare(struct snd_soc_component *component,
                           struct snd_pcm_substream *substream)
 {
        struct snd_soc_pcm_runtime *rtd = substream->private_data;
-       struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
        struct snd_sof_pcm *spcm;
        int ret;
 
@@ -285,21 +278,22 @@ static int sof_pcm_prepare(struct snd_soc_component *component,
        if (rtd->dai_link->no_pcm)
                return 0;
 
-       spcm = snd_sof_find_spcm_dai(sdev, rtd);
+       spcm = snd_sof_find_spcm_dai(component, rtd);
        if (!spcm)
                return -EINVAL;
 
        if (spcm->prepared[substream->stream])
                return 0;
 
-       dev_dbg(sdev->dev, "pcm: prepare stream %d dir %d\n", spcm->pcm.pcm_id,
-               substream->stream);
+       dev_dbg(component->dev, "pcm: prepare stream %d dir %d\n",
+               spcm->pcm.pcm_id, substream->stream);
 
        /* set hw_params */
        ret = sof_pcm_hw_params(component,
                                substream, &spcm->params[substream->stream]);
        if (ret < 0) {
-               dev_err(sdev->dev, "error: set pcm hw_params after resume\n");
+               dev_err(component->dev,
+                       "error: set pcm hw_params after resume\n");
                return ret;
        }
 
@@ -326,11 +320,11 @@ static int sof_pcm_trigger(struct snd_soc_component *component,
        if (rtd->dai_link->no_pcm)
                return 0;
 
-       spcm = snd_sof_find_spcm_dai(sdev, rtd);
+       spcm = snd_sof_find_spcm_dai(component, rtd);
        if (!spcm)
                return -EINVAL;
 
-       dev_dbg(sdev->dev, "pcm: trigger stream %d dir %d cmd %d\n",
+       dev_dbg(component->dev, "pcm: trigger stream %d dir %d cmd %d\n",
                spcm->pcm.pcm_id, substream->stream, cmd);
 
        stream.hdr.size = sizeof(stream);
@@ -359,7 +353,7 @@ static int sof_pcm_trigger(struct snd_soc_component *component,
                /* set up hw_params */
                ret = sof_pcm_prepare(component, substream);
                if (ret < 0) {
-                       dev_err(sdev->dev,
+                       dev_err(component->dev,
                                "error: failed to set up hw_params upon resume\n");
                        return ret;
                }
@@ -396,7 +390,8 @@ static int sof_pcm_trigger(struct snd_soc_component *component,
                reset_hw_params = true;
                break;
        default:
-               dev_err(sdev->dev, "error: unhandled trigger cmd %d\n", cmd);
+               dev_err(component->dev, "error: unhandled trigger cmd %d\n",
+                       cmd);
                return -EINVAL;
        }
 
@@ -438,7 +433,7 @@ static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component,
        if (sof_ops(sdev)->pcm_pointer)
                return sof_ops(sdev)->pcm_pointer(sdev, substream);
 
-       spcm = snd_sof_find_spcm_dai(sdev, rtd);
+       spcm = snd_sof_find_spcm_dai(component, rtd);
        if (!spcm)
                return -EINVAL;
 
@@ -448,7 +443,8 @@ static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component,
        dai = bytes_to_frames(substream->runtime,
                              spcm->stream[substream->stream].posn.dai_posn);
 
-       dev_dbg(sdev->dev, "PCM: stream %d dir %d DMA position %lu DAI position %lu\n",
+       dev_dbg(component->dev,
+               "PCM: stream %d dir %d DMA position %lu DAI position %lu\n",
                spcm->pcm.pcm_id, substream->stream, host, dai);
 
        return host;
@@ -469,12 +465,12 @@ static int sof_pcm_open(struct snd_soc_component *component,
        if (rtd->dai_link->no_pcm)
                return 0;
 
-       spcm = snd_sof_find_spcm_dai(sdev, rtd);
+       spcm = snd_sof_find_spcm_dai(component, rtd);
        if (!spcm)
                return -EINVAL;
 
-       dev_dbg(sdev->dev, "pcm: open stream %d dir %d\n", spcm->pcm.pcm_id,
-               substream->stream);
+       dev_dbg(component->dev, "pcm: open stream %d dir %d\n",
+               spcm->pcm.pcm_id, substream->stream);
 
        INIT_WORK(&spcm->stream[substream->stream].period_elapsed_work,
                  sof_pcm_period_elapsed_work);
@@ -504,13 +500,13 @@ static int sof_pcm_open(struct snd_soc_component *component,
         */
        runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
 
-       dev_dbg(sdev->dev, "period min %zd max %zd bytes\n",
+       dev_dbg(component->dev, "period min %zd max %zd bytes\n",
                runtime->hw.period_bytes_min,
                runtime->hw.period_bytes_max);
-       dev_dbg(sdev->dev, "period count %d max %d\n",
+       dev_dbg(component->dev, "period count %d max %d\n",
                runtime->hw.periods_min,
                runtime->hw.periods_max);
-       dev_dbg(sdev->dev, "buffer max %zd bytes\n",
+       dev_dbg(component->dev, "buffer max %zd bytes\n",
                runtime->hw.buffer_bytes_max);
 
        /* set wait time - TODO: come from topology */
@@ -523,7 +519,7 @@ static int sof_pcm_open(struct snd_soc_component *component,
 
        ret = snd_sof_pcm_platform_open(sdev, substream);
        if (ret < 0)
-               dev_err(sdev->dev, "error: pcm open failed %d\n", ret);
+               dev_err(component->dev, "error: pcm open failed %d\n", ret);
 
        return ret;
 }
@@ -540,16 +536,16 @@ static int sof_pcm_close(struct snd_soc_component *component,
        if (rtd->dai_link->no_pcm)
                return 0;
 
-       spcm = snd_sof_find_spcm_dai(sdev, rtd);
+       spcm = snd_sof_find_spcm_dai(component, rtd);
        if (!spcm)
                return -EINVAL;
 
-       dev_dbg(sdev->dev, "pcm: close stream %d dir %d\n", spcm->pcm.pcm_id,
-               substream->stream);
+       dev_dbg(component->dev, "pcm: close stream %d dir %d\n",
+               spcm->pcm.pcm_id, substream->stream);
 
        err = snd_sof_pcm_platform_close(sdev, substream);
        if (err < 0) {
-               dev_err(sdev->dev, "error: pcm close failed %d\n",
+               dev_err(component->dev, "error: pcm close failed %d\n",
                        err);
                /*
                 * keep going, no point in preventing the close
@@ -575,14 +571,14 @@ static int sof_pcm_new(struct snd_soc_component *component,
        int stream = SNDRV_PCM_STREAM_PLAYBACK;
 
        /* find SOF PCM for this RTD */
-       spcm = snd_sof_find_spcm_dai(sdev, rtd);
+       spcm = snd_sof_find_spcm_dai(component, rtd);
        if (!spcm) {
-               dev_warn(sdev->dev, "warn: can't find PCM with DAI ID %d\n",
+               dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n",
                         rtd->dai_link->id);
                return 0;
        }
 
-       dev_dbg(sdev->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
+       dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
 
        /* do we need to pre-allocate playback audio buffer pages */
        if (!spcm->pcm.playback)
@@ -591,13 +587,19 @@ static int sof_pcm_new(struct snd_soc_component *component,
        caps = &spcm->pcm.caps[stream];
 
        /* pre-allocate playback audio buffer pages */
-       dev_dbg(sdev->dev, "spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
+       dev_dbg(component->dev,
+               "spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
                caps->name, caps->buffer_size_min, caps->buffer_size_max);
 
-       snd_pcm_lib_preallocate_pages(pcm->streams[stream].substream,
-                                     SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
-                                     le32_to_cpu(caps->buffer_size_min),
-                                     le32_to_cpu(caps->buffer_size_max));
+       if (!pcm->streams[stream].substream) {
+               dev_err(component->dev, "error: NULL playback substream!\n");
+               return -EINVAL;
+       }
+
+       snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
+                                  SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
+                                  le32_to_cpu(caps->buffer_size_min),
+                                  le32_to_cpu(caps->buffer_size_max));
 capture:
        stream = SNDRV_PCM_STREAM_CAPTURE;
 
@@ -608,13 +610,19 @@ capture:
        caps = &spcm->pcm.caps[stream];
 
        /* pre-allocate capture audio buffer pages */
-       dev_dbg(sdev->dev, "spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
+       dev_dbg(component->dev,
+               "spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
                caps->name, caps->buffer_size_min, caps->buffer_size_max);
 
-       snd_pcm_lib_preallocate_pages(pcm->streams[stream].substream,
-                                     SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
-                                     le32_to_cpu(caps->buffer_size_min),
-                                     le32_to_cpu(caps->buffer_size_max));
+       if (!pcm->streams[stream].substream) {
+               dev_err(component->dev, "error: NULL capture substream!\n");
+               return -EINVAL;
+       }
+
+       snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
+                                  SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
+                                  le32_to_cpu(caps->buffer_size_min),
+                                  le32_to_cpu(caps->buffer_size_max));
 
        return 0;
 }
@@ -629,14 +637,14 @@ static int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
                                                SNDRV_PCM_HW_PARAM_CHANNELS);
        struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
        struct snd_soc_component *component =
-               snd_soc_rtdcom_lookup(rtd, DRV_NAME);
-       struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
+               snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
        struct snd_sof_dai *dai =
-               snd_sof_find_dai(sdev, (char *)rtd->dai_link->name);
+               snd_sof_find_dai(component, (char *)rtd->dai_link->name);
 
        /* no topology exists for this BE, try a common configuration */
        if (!dai) {
-               dev_warn(sdev->dev, "warning: no topology found for BE DAI %s config\n",
+               dev_warn(component->dev,
+                        "warning: no topology found for BE DAI %s config\n",
                         rtd->dai_link->name);
 
                /*  set 48k, stereo, 16bits by default */
@@ -666,7 +674,7 @@ static int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
                snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
                break;
        default:
-               dev_err(sdev->dev, "error: No available DAI format!\n");
+               dev_err(component->dev, "error: No available DAI format!\n");
                return -EINVAL;
        }
 
@@ -678,9 +686,9 @@ static int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
                channels->min = dai->dai_config->ssp.tdm_slots;
                channels->max = dai->dai_config->ssp.tdm_slots;
 
-               dev_dbg(sdev->dev,
+               dev_dbg(component->dev,
                        "rate_min: %d rate_max: %d\n", rate->min, rate->max);
-               dev_dbg(sdev->dev,
+               dev_dbg(component->dev,
                        "channels_min: %d channels_max: %d\n",
                        channels->min, channels->max);
 
@@ -688,7 +696,7 @@ static int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
        case SOF_DAI_INTEL_DMIC:
                /* DMIC only supports 16 or 32 bit formats */
                if (dai->comp_dai.config.frame_fmt == SOF_IPC_FRAME_S24_4LE) {
-                       dev_err(sdev->dev,
+                       dev_err(component->dev,
                                "error: invalid fmt %d for DAI type %d\n",
                                dai->comp_dai.config.frame_fmt,
                                dai->dai_config->type);
@@ -704,12 +712,20 @@ static int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
                channels->min = dai->dai_config->esai.tdm_slots;
                channels->max = dai->dai_config->esai.tdm_slots;
 
-               dev_dbg(sdev->dev,
+               dev_dbg(component->dev,
+                       "channels_min: %d channels_max: %d\n",
+                       channels->min, channels->max);
+               break;
+       case SOF_DAI_IMX_SAI:
+               channels->min = dai->dai_config->sai.tdm_slots;
+               channels->max = dai->dai_config->sai.tdm_slots;
+
+               dev_dbg(component->dev,
                        "channels_min: %d channels_max: %d\n",
                        channels->min, channels->max);
                break;
        default:
-               dev_err(sdev->dev, "error: invalid DAI type %d\n",
+               dev_err(component->dev, "error: invalid DAI type %d\n",
                        dai->dai_config->type);
                break;
        }
@@ -734,21 +750,13 @@ static int sof_pcm_probe(struct snd_soc_component *component)
        if (!tplg_filename)
                return -ENOMEM;
 
-       ret = snd_sof_load_topology(sdev, tplg_filename);
+       ret = snd_sof_load_topology(component, tplg_filename);
        if (ret < 0) {
-               dev_err(sdev->dev, "error: failed to load DSP topology %d\n",
+               dev_err(component->dev, "error: failed to load DSP topology %d\n",
                        ret);
                return ret;
        }
 
-       /*
-        * Some platforms in SOF, ex: BYT, may not have their platform PM
-        * callbacks set. Increment the usage count so as to
-        * prevent the device from entering runtime suspend.
-        */
-       if (!sof_ops(sdev)->runtime_suspend || !sof_ops(sdev)->runtime_resume)
-               pm_runtime_get_noresume(sdev->dev);
-
        return ret;
 }
 
@@ -771,7 +779,6 @@ void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
        pd->remove = sof_pcm_remove;
        pd->open = sof_pcm_open;
        pd->close = sof_pcm_close;
-       pd->ioctl = snd_soc_pcm_lib_ioctl;
        pd->hw_params = sof_pcm_hw_params;
        pd->prepare = sof_pcm_prepare;
        pd->hw_free = sof_pcm_hw_free;