linux/sound/firewire/fireface/amdtp-ff.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * amdtp-ff.c - a part of driver for RME Fireface series
   4 *
   5 * Copyright (c) 2015-2017 Takashi Sakamoto
   6 */
   7
   8#include <sound/pcm.h>
   9#include "ff.h"
  10
  11struct amdtp_ff {
  12        unsigned int pcm_channels;
  13};
  14
  15int amdtp_ff_set_parameters(struct amdtp_stream *s, unsigned int rate,
  16                            unsigned int pcm_channels)
  17{
  18        struct amdtp_ff *p = s->protocol;
  19        unsigned int data_channels;
  20
  21        if (amdtp_stream_running(s))
  22                return -EBUSY;
  23
  24        p->pcm_channels = pcm_channels;
  25        data_channels = pcm_channels;
  26
  27        return amdtp_stream_set_parameters(s, rate, data_channels);
  28}
  29
  30static void write_pcm_s32(struct amdtp_stream *s,
  31                          struct snd_pcm_substream *pcm,
  32                          __le32 *buffer, unsigned int frames)
  33{
  34        struct amdtp_ff *p = s->protocol;
  35        struct snd_pcm_runtime *runtime = pcm->runtime;
  36        unsigned int channels, remaining_frames, i, c;
  37        const u32 *src;
  38
  39        channels = p->pcm_channels;
  40        src = (void *)runtime->dma_area +
  41                        frames_to_bytes(runtime, s->pcm_buffer_pointer);
  42        remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  43
  44        for (i = 0; i < frames; ++i) {
  45                for (c = 0; c < channels; ++c) {
  46                        buffer[c] = cpu_to_le32(*src);
  47                        src++;
  48                }
  49                buffer += s->data_block_quadlets;
  50                if (--remaining_frames == 0)
  51                        src = (void *)runtime->dma_area;
  52        }
  53}
  54
  55static void read_pcm_s32(struct amdtp_stream *s,
  56                         struct snd_pcm_substream *pcm,
  57                         __le32 *buffer, unsigned int frames)
  58{
  59        struct amdtp_ff *p = s->protocol;
  60        struct snd_pcm_runtime *runtime = pcm->runtime;
  61        unsigned int channels, remaining_frames, i, c;
  62        u32 *dst;
  63
  64        channels = p->pcm_channels;
  65        dst  = (void *)runtime->dma_area +
  66                        frames_to_bytes(runtime, s->pcm_buffer_pointer);
  67        remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  68
  69        for (i = 0; i < frames; ++i) {
  70                for (c = 0; c < channels; ++c) {
  71                        *dst = le32_to_cpu(buffer[c]) & 0xffffff00;
  72                        dst++;
  73                }
  74                buffer += s->data_block_quadlets;
  75                if (--remaining_frames == 0)
  76                        dst = (void *)runtime->dma_area;
  77        }
  78}
  79
  80static void write_pcm_silence(struct amdtp_stream *s,
  81                              __le32 *buffer, unsigned int frames)
  82{
  83        struct amdtp_ff *p = s->protocol;
  84        unsigned int i, c, channels = p->pcm_channels;
  85
  86        for (i = 0; i < frames; ++i) {
  87                for (c = 0; c < channels; ++c)
  88                        buffer[c] = cpu_to_le32(0x00000000);
  89                buffer += s->data_block_quadlets;
  90        }
  91}
  92
  93int amdtp_ff_add_pcm_hw_constraints(struct amdtp_stream *s,
  94                                    struct snd_pcm_runtime *runtime)
  95{
  96        int err;
  97
  98        err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  99        if (err < 0)
 100                return err;
 101
 102        return amdtp_stream_add_pcm_hw_constraints(s, runtime);
 103}
 104
 105static unsigned int process_rx_data_blocks(struct amdtp_stream *s,
 106                                           __be32 *buffer,
 107                                           unsigned int data_blocks,
 108                                           unsigned int *syt)
 109{
 110        struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
 111        unsigned int pcm_frames;
 112
 113        if (pcm) {
 114                write_pcm_s32(s, pcm, (__le32 *)buffer, data_blocks);
 115                pcm_frames = data_blocks;
 116        } else {
 117                write_pcm_silence(s, (__le32 *)buffer, data_blocks);
 118                pcm_frames = 0;
 119        }
 120
 121        return pcm_frames;
 122}
 123
 124static unsigned int process_tx_data_blocks(struct amdtp_stream *s,
 125                                           __be32 *buffer,
 126                                           unsigned int data_blocks,
 127                                           unsigned int *syt)
 128{
 129        struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
 130        unsigned int pcm_frames;
 131
 132        if (pcm) {
 133                read_pcm_s32(s, pcm, (__le32 *)buffer, data_blocks);
 134                pcm_frames = data_blocks;
 135        } else {
 136                pcm_frames = 0;
 137        }
 138
 139        return pcm_frames;
 140}
 141
 142int amdtp_ff_init(struct amdtp_stream *s, struct fw_unit *unit,
 143                  enum amdtp_stream_direction dir)
 144{
 145        amdtp_stream_process_data_blocks_t process_data_blocks;
 146
 147        if (dir == AMDTP_IN_STREAM)
 148                process_data_blocks = process_tx_data_blocks;
 149        else
 150                process_data_blocks = process_rx_data_blocks;
 151
 152        return amdtp_stream_init(s, unit, dir, CIP_NO_HEADER, 0,
 153                                 process_data_blocks, sizeof(struct amdtp_ff));
 154}
 155