Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * amdtp-tascam.c - a part of driver for TASCAM FireWire series
0004  *
0005  * Copyright (c) 2015 Takashi Sakamoto
0006  */
0007 
0008 #include <sound/pcm.h>
0009 #include "tascam.h"
0010 
0011 #define AMDTP_FMT_TSCM_TX   0x1e
0012 #define AMDTP_FMT_TSCM_RX   0x3e
0013 
0014 struct amdtp_tscm {
0015     unsigned int pcm_channels;
0016 };
0017 
0018 int amdtp_tscm_set_parameters(struct amdtp_stream *s, unsigned int rate)
0019 {
0020     struct amdtp_tscm *p = s->protocol;
0021     unsigned int data_channels;
0022 
0023     if (amdtp_stream_running(s))
0024         return -EBUSY;
0025 
0026     data_channels = p->pcm_channels;
0027 
0028     /* Packets in in-stream have extra 2 data channels. */
0029     if (s->direction == AMDTP_IN_STREAM)
0030         data_channels += 2;
0031 
0032     return amdtp_stream_set_parameters(s, rate, data_channels);
0033 }
0034 
0035 static void write_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
0036               __be32 *buffer, unsigned int frames,
0037               unsigned int pcm_frames)
0038 {
0039     struct amdtp_tscm *p = s->protocol;
0040     unsigned int channels = p->pcm_channels;
0041     struct snd_pcm_runtime *runtime = pcm->runtime;
0042     unsigned int pcm_buffer_pointer;
0043     int remaining_frames;
0044     const u32 *src;
0045     int i, c;
0046 
0047     pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames;
0048     pcm_buffer_pointer %= runtime->buffer_size;
0049 
0050     src = (void *)runtime->dma_area +
0051                 frames_to_bytes(runtime, pcm_buffer_pointer);
0052     remaining_frames = runtime->buffer_size - pcm_buffer_pointer;
0053 
0054     for (i = 0; i < frames; ++i) {
0055         for (c = 0; c < channels; ++c) {
0056             buffer[c] = cpu_to_be32(*src);
0057             src++;
0058         }
0059         buffer += s->data_block_quadlets;
0060         if (--remaining_frames == 0)
0061             src = (void *)runtime->dma_area;
0062     }
0063 }
0064 
0065 static void read_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
0066              __be32 *buffer, unsigned int frames,
0067              unsigned int pcm_frames)
0068 {
0069     struct amdtp_tscm *p = s->protocol;
0070     unsigned int channels = p->pcm_channels;
0071     struct snd_pcm_runtime *runtime = pcm->runtime;
0072     unsigned int pcm_buffer_pointer;
0073     int remaining_frames;
0074     u32 *dst;
0075     int i, c;
0076 
0077     pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames;
0078     pcm_buffer_pointer %= runtime->buffer_size;
0079 
0080     dst  = (void *)runtime->dma_area +
0081                 frames_to_bytes(runtime, pcm_buffer_pointer);
0082     remaining_frames = runtime->buffer_size - pcm_buffer_pointer;
0083 
0084     /* The first data channel is for event counter. */
0085     buffer += 1;
0086 
0087     for (i = 0; i < frames; ++i) {
0088         for (c = 0; c < channels; ++c) {
0089             *dst = be32_to_cpu(buffer[c]);
0090             dst++;
0091         }
0092         buffer += s->data_block_quadlets;
0093         if (--remaining_frames == 0)
0094             dst = (void *)runtime->dma_area;
0095     }
0096 }
0097 
0098 static void write_pcm_silence(struct amdtp_stream *s, __be32 *buffer,
0099                   unsigned int data_blocks)
0100 {
0101     struct amdtp_tscm *p = s->protocol;
0102     unsigned int channels, i, c;
0103 
0104     channels = p->pcm_channels;
0105 
0106     for (i = 0; i < data_blocks; ++i) {
0107         for (c = 0; c < channels; ++c)
0108             buffer[c] = 0x00000000;
0109         buffer += s->data_block_quadlets;
0110     }
0111 }
0112 
0113 int amdtp_tscm_add_pcm_hw_constraints(struct amdtp_stream *s,
0114                       struct snd_pcm_runtime *runtime)
0115 {
0116     int err;
0117 
0118     /*
0119      * Our implementation allows this protocol to deliver 24 bit sample in
0120      * 32bit data channel.
0121      */
0122     err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
0123     if (err < 0)
0124         return err;
0125 
0126     return amdtp_stream_add_pcm_hw_constraints(s, runtime);
0127 }
0128 
0129 static void read_status_messages(struct amdtp_stream *s,
0130                  __be32 *buffer, unsigned int data_blocks)
0131 {
0132     struct snd_tscm *tscm = container_of(s, struct snd_tscm, tx_stream);
0133     bool used = READ_ONCE(tscm->hwdep->used);
0134     int i;
0135 
0136     for (i = 0; i < data_blocks; i++) {
0137         unsigned int index;
0138         __be32 before;
0139         __be32 after;
0140 
0141         index = be32_to_cpu(buffer[0]) % SNDRV_FIREWIRE_TASCAM_STATE_COUNT;
0142         before = tscm->state[index];
0143         after = buffer[s->data_block_quadlets - 1];
0144 
0145         if (used && index > 4 && index < 16) {
0146             __be32 mask;
0147 
0148             if (index == 5)
0149                 mask = cpu_to_be32(~0x0000ffff);
0150             else if (index == 6)
0151                 mask = cpu_to_be32(~0x0000ffff);
0152             else if (index == 8)
0153                 mask = cpu_to_be32(~0x000f0f00);
0154             else
0155                 mask = cpu_to_be32(~0x00000000);
0156 
0157             if ((before ^ after) & mask) {
0158                 struct snd_firewire_tascam_change *entry =
0159                         &tscm->queue[tscm->push_pos];
0160                 unsigned long flag;
0161 
0162                 spin_lock_irqsave(&tscm->lock, flag);
0163                 entry->index = index;
0164                 entry->before = before;
0165                 entry->after = after;
0166                 if (++tscm->push_pos >= SND_TSCM_QUEUE_COUNT)
0167                     tscm->push_pos = 0;
0168                 spin_unlock_irqrestore(&tscm->lock, flag);
0169 
0170                 wake_up(&tscm->hwdep_wait);
0171             }
0172         }
0173 
0174         tscm->state[index] = after;
0175         buffer += s->data_block_quadlets;
0176     }
0177 }
0178 
0179 static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s,
0180                         const struct pkt_desc *descs,
0181                         unsigned int packets,
0182                         struct snd_pcm_substream *pcm)
0183 {
0184     unsigned int pcm_frames = 0;
0185     int i;
0186 
0187     for (i = 0; i < packets; ++i) {
0188         const struct pkt_desc *desc = descs + i;
0189         __be32 *buf = desc->ctx_payload;
0190         unsigned int data_blocks = desc->data_blocks;
0191 
0192         if (pcm) {
0193             read_pcm_s32(s, pcm, buf, data_blocks, pcm_frames);
0194             pcm_frames += data_blocks;
0195         }
0196 
0197         read_status_messages(s, buf, data_blocks);
0198     }
0199 
0200     return pcm_frames;
0201 }
0202 
0203 static unsigned int process_it_ctx_payloads(struct amdtp_stream *s,
0204                         const struct pkt_desc *descs,
0205                         unsigned int packets,
0206                         struct snd_pcm_substream *pcm)
0207 {
0208     unsigned int pcm_frames = 0;
0209     int i;
0210 
0211     for (i = 0; i < packets; ++i) {
0212         const struct pkt_desc *desc = descs + i;
0213         __be32 *buf = desc->ctx_payload;
0214         unsigned int data_blocks = desc->data_blocks;
0215 
0216         if (pcm) {
0217             write_pcm_s32(s, pcm, buf, data_blocks, pcm_frames);
0218             pcm_frames += data_blocks;
0219         } else {
0220             write_pcm_silence(s, buf, data_blocks);
0221         }
0222     }
0223 
0224     return pcm_frames;
0225 }
0226 
0227 int amdtp_tscm_init(struct amdtp_stream *s, struct fw_unit *unit,
0228             enum amdtp_stream_direction dir, unsigned int pcm_channels)
0229 {
0230     amdtp_stream_process_ctx_payloads_t process_ctx_payloads;
0231     unsigned int flags = CIP_NONBLOCKING | CIP_SKIP_DBC_ZERO_CHECK | CIP_UNAWARE_SYT;
0232     struct amdtp_tscm *p;
0233     unsigned int fmt;
0234     int err;
0235 
0236     if (dir == AMDTP_IN_STREAM) {
0237         fmt = AMDTP_FMT_TSCM_TX;
0238         process_ctx_payloads = process_ir_ctx_payloads;
0239     } else {
0240         fmt = AMDTP_FMT_TSCM_RX;
0241         process_ctx_payloads = process_it_ctx_payloads;
0242     }
0243 
0244     err = amdtp_stream_init(s, unit, dir, flags, fmt,
0245             process_ctx_payloads, sizeof(struct amdtp_tscm));
0246     if (err < 0)
0247         return 0;
0248 
0249     if (dir == AMDTP_OUT_STREAM) {
0250         // Use fixed value for FDF field.
0251         s->ctx_data.rx.fdf = 0x00;
0252     }
0253 
0254     /* This protocol uses fixed number of data channels for PCM samples. */
0255     p = s->protocol;
0256     p->pcm_channels = pcm_channels;
0257 
0258     return 0;
0259 }