2 * amdtp-tascam.c - a part of driver for TASCAM FireWire series
4 * Copyright (c) 2015 Takashi Sakamoto
6 * Licensed under the terms of the GNU General Public License, version 2.
12 #define AMDTP_FMT_TSCM_TX 0x1e
13 #define AMDTP_FMT_TSCM_RX 0x3e
16 unsigned int pcm_channels;
19 int amdtp_tscm_set_parameters(struct amdtp_stream *s, unsigned int rate)
21 struct amdtp_tscm *p = s->protocol;
22 unsigned int data_channels;
24 if (amdtp_stream_running(s))
27 data_channels = p->pcm_channels;
29 /* Packets in in-stream have extra 2 data channels. */
30 if (s->direction == AMDTP_IN_STREAM)
33 return amdtp_stream_set_parameters(s, rate, data_channels);
36 static void write_pcm_s32(struct amdtp_stream *s,
37 struct snd_pcm_substream *pcm,
38 __be32 *buffer, unsigned int frames)
40 struct amdtp_tscm *p = s->protocol;
41 struct snd_pcm_runtime *runtime = pcm->runtime;
42 unsigned int channels, remaining_frames, i, c;
45 channels = p->pcm_channels;
46 src = (void *)runtime->dma_area +
47 frames_to_bytes(runtime, s->pcm_buffer_pointer);
48 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
50 for (i = 0; i < frames; ++i) {
51 for (c = 0; c < channels; ++c) {
52 buffer[c] = cpu_to_be32(*src);
55 buffer += s->data_block_quadlets;
56 if (--remaining_frames == 0)
57 src = (void *)runtime->dma_area;
61 static void read_pcm_s32(struct amdtp_stream *s,
62 struct snd_pcm_substream *pcm,
63 __be32 *buffer, unsigned int frames)
65 struct amdtp_tscm *p = s->protocol;
66 struct snd_pcm_runtime *runtime = pcm->runtime;
67 unsigned int channels, remaining_frames, i, c;
70 channels = p->pcm_channels;
71 dst = (void *)runtime->dma_area +
72 frames_to_bytes(runtime, s->pcm_buffer_pointer);
73 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
75 /* The first data channel is for event counter. */
78 for (i = 0; i < frames; ++i) {
79 for (c = 0; c < channels; ++c) {
80 *dst = be32_to_cpu(buffer[c]);
83 buffer += s->data_block_quadlets;
84 if (--remaining_frames == 0)
85 dst = (void *)runtime->dma_area;
89 static void write_pcm_silence(struct amdtp_stream *s, __be32 *buffer,
90 unsigned int data_blocks)
92 struct amdtp_tscm *p = s->protocol;
93 unsigned int channels, i, c;
95 channels = p->pcm_channels;
97 for (i = 0; i < data_blocks; ++i) {
98 for (c = 0; c < channels; ++c)
99 buffer[c] = 0x00000000;
100 buffer += s->data_block_quadlets;
104 int amdtp_tscm_add_pcm_hw_constraints(struct amdtp_stream *s,
105 struct snd_pcm_runtime *runtime)
110 * Our implementation allows this protocol to deliver 24 bit sample in
111 * 32bit data channel.
113 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
117 return amdtp_stream_add_pcm_hw_constraints(s, runtime);
120 static void read_status_messages(struct amdtp_stream *s,
121 __be32 *buffer, unsigned int data_blocks)
123 struct snd_tscm *tscm = container_of(s, struct snd_tscm, tx_stream);
124 bool used = READ_ONCE(tscm->hwdep->used);
127 for (i = 0; i < data_blocks; i++) {
132 index = be32_to_cpu(buffer[0]) % SNDRV_FIREWIRE_TASCAM_STATE_COUNT;
133 before = tscm->state[index];
134 after = buffer[s->data_block_quadlets - 1];
136 if (used && index > 4 && index < 16) {
140 mask = cpu_to_be32(~0x0000ffff);
142 mask = cpu_to_be32(~0x0000ffff);
144 mask = cpu_to_be32(~0x000f0f00);
146 mask = cpu_to_be32(~0x00000000);
148 if ((before ^ after) & mask) {
149 struct snd_firewire_tascam_change *entry =
150 &tscm->queue[tscm->push_pos];
152 spin_lock_irq(&tscm->lock);
153 entry->index = index;
154 entry->before = before;
155 entry->after = after;
156 if (++tscm->push_pos >= SND_TSCM_QUEUE_COUNT)
158 spin_unlock_irq(&tscm->lock);
160 wake_up(&tscm->hwdep_wait);
164 tscm->state[index] = after;
165 buffer += s->data_block_quadlets;
169 static unsigned int process_tx_data_blocks(struct amdtp_stream *s,
171 unsigned int data_blocks,
174 struct snd_pcm_substream *pcm;
176 pcm = READ_ONCE(s->pcm);
177 if (data_blocks > 0 && pcm)
178 read_pcm_s32(s, pcm, buffer, data_blocks);
180 read_status_messages(s, buffer, data_blocks);
185 static unsigned int process_rx_data_blocks(struct amdtp_stream *s,
187 unsigned int data_blocks,
190 struct snd_pcm_substream *pcm;
192 /* This field is not used. */
195 pcm = READ_ONCE(s->pcm);
197 write_pcm_s32(s, pcm, buffer, data_blocks);
199 write_pcm_silence(s, buffer, data_blocks);
204 int amdtp_tscm_init(struct amdtp_stream *s, struct fw_unit *unit,
205 enum amdtp_stream_direction dir, unsigned int pcm_channels)
207 amdtp_stream_process_data_blocks_t process_data_blocks;
208 struct amdtp_tscm *p;
212 if (dir == AMDTP_IN_STREAM) {
213 fmt = AMDTP_FMT_TSCM_TX;
214 process_data_blocks = process_tx_data_blocks;
216 fmt = AMDTP_FMT_TSCM_RX;
217 process_data_blocks = process_rx_data_blocks;
220 err = amdtp_stream_init(s, unit, dir,
221 CIP_NONBLOCKING | CIP_SKIP_DBC_ZERO_CHECK, fmt,
222 process_data_blocks, sizeof(struct amdtp_tscm));
226 /* Use fixed value for FDF field. */
229 /* This protocol uses fixed number of data channels for PCM samples. */
231 p->pcm_channels = pcm_channels;