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
;
18 void (*transfer_samples
)(struct amdtp_stream
*s
,
19 struct snd_pcm_substream
*pcm
,
20 __be32
*buffer
, unsigned int frames
);
23 int amdtp_tscm_set_parameters(struct amdtp_stream
*s
, unsigned int rate
)
25 struct amdtp_tscm
*p
= s
->protocol
;
26 unsigned int data_channels
;
28 if (amdtp_stream_running(s
))
31 data_channels
= p
->pcm_channels
;
33 /* Packets in in-stream have extra 2 data channels. */
34 if (s
->direction
== AMDTP_IN_STREAM
)
37 return amdtp_stream_set_parameters(s
, rate
, data_channels
);
40 static void write_pcm_s32(struct amdtp_stream
*s
,
41 struct snd_pcm_substream
*pcm
,
42 __be32
*buffer
, unsigned int frames
)
44 struct amdtp_tscm
*p
= s
->protocol
;
45 struct snd_pcm_runtime
*runtime
= pcm
->runtime
;
46 unsigned int channels
, remaining_frames
, i
, c
;
49 channels
= p
->pcm_channels
;
50 src
= (void *)runtime
->dma_area
+
51 frames_to_bytes(runtime
, s
->pcm_buffer_pointer
);
52 remaining_frames
= runtime
->buffer_size
- s
->pcm_buffer_pointer
;
54 for (i
= 0; i
< frames
; ++i
) {
55 for (c
= 0; c
< channels
; ++c
) {
56 buffer
[c
] = cpu_to_be32(*src
);
59 buffer
+= s
->data_block_quadlets
;
60 if (--remaining_frames
== 0)
61 src
= (void *)runtime
->dma_area
;
65 static void write_pcm_s16(struct amdtp_stream
*s
,
66 struct snd_pcm_substream
*pcm
,
67 __be32
*buffer
, unsigned int frames
)
69 struct amdtp_tscm
*p
= s
->protocol
;
70 struct snd_pcm_runtime
*runtime
= pcm
->runtime
;
71 unsigned int channels
, remaining_frames
, i
, c
;
74 channels
= p
->pcm_channels
;
75 src
= (void *)runtime
->dma_area
+
76 frames_to_bytes(runtime
, s
->pcm_buffer_pointer
);
77 remaining_frames
= runtime
->buffer_size
- s
->pcm_buffer_pointer
;
79 for (i
= 0; i
< frames
; ++i
) {
80 for (c
= 0; c
< channels
; ++c
) {
81 buffer
[c
] = cpu_to_be32(*src
<< 16);
84 buffer
+= s
->data_block_quadlets
;
85 if (--remaining_frames
== 0)
86 src
= (void *)runtime
->dma_area
;
90 static void read_pcm_s32(struct amdtp_stream
*s
,
91 struct snd_pcm_substream
*pcm
,
92 __be32
*buffer
, unsigned int frames
)
94 struct amdtp_tscm
*p
= s
->protocol
;
95 struct snd_pcm_runtime
*runtime
= pcm
->runtime
;
96 unsigned int channels
, remaining_frames
, i
, c
;
99 channels
= p
->pcm_channels
;
100 dst
= (void *)runtime
->dma_area
+
101 frames_to_bytes(runtime
, s
->pcm_buffer_pointer
);
102 remaining_frames
= runtime
->buffer_size
- s
->pcm_buffer_pointer
;
104 /* The first data channel is for event counter. */
107 for (i
= 0; i
< frames
; ++i
) {
108 for (c
= 0; c
< channels
; ++c
) {
109 *dst
= be32_to_cpu(buffer
[c
]);
112 buffer
+= s
->data_block_quadlets
;
113 if (--remaining_frames
== 0)
114 dst
= (void *)runtime
->dma_area
;
118 static void write_pcm_silence(struct amdtp_stream
*s
, __be32
*buffer
,
119 unsigned int data_blocks
)
121 struct amdtp_tscm
*p
= s
->protocol
;
122 unsigned int channels
, i
, c
;
124 channels
= p
->pcm_channels
;
126 for (i
= 0; i
< data_blocks
; ++i
) {
127 for (c
= 0; c
< channels
; ++c
)
128 buffer
[c
] = 0x00000000;
129 buffer
+= s
->data_block_quadlets
;
133 int amdtp_tscm_add_pcm_hw_constraints(struct amdtp_stream
*s
,
134 struct snd_pcm_runtime
*runtime
)
139 * Our implementation allows this protocol to deliver 24 bit sample in
140 * 32bit data channel.
142 err
= snd_pcm_hw_constraint_msbits(runtime
, 0, 32, 24);
146 return amdtp_stream_add_pcm_hw_constraints(s
, runtime
);
149 void amdtp_tscm_set_pcm_format(struct amdtp_stream
*s
, snd_pcm_format_t format
)
151 struct amdtp_tscm
*p
= s
->protocol
;
153 if (WARN_ON(amdtp_stream_pcm_running(s
)))
160 case SNDRV_PCM_FORMAT_S16
:
161 if (s
->direction
== AMDTP_OUT_STREAM
) {
162 p
->transfer_samples
= write_pcm_s16
;
167 case SNDRV_PCM_FORMAT_S32
:
168 if (s
->direction
== AMDTP_OUT_STREAM
)
169 p
->transfer_samples
= write_pcm_s32
;
171 p
->transfer_samples
= read_pcm_s32
;
176 static unsigned int process_tx_data_blocks(struct amdtp_stream
*s
,
178 unsigned int data_blocks
,
181 struct amdtp_tscm
*p
= (struct amdtp_tscm
*)s
->protocol
;
182 struct snd_pcm_substream
*pcm
;
184 pcm
= ACCESS_ONCE(s
->pcm
);
185 if (data_blocks
> 0 && pcm
)
186 p
->transfer_samples(s
, pcm
, buffer
, data_blocks
);
188 /* A place holder for control messages. */
193 static unsigned int process_rx_data_blocks(struct amdtp_stream
*s
,
195 unsigned int data_blocks
,
198 struct amdtp_tscm
*p
= (struct amdtp_tscm
*)s
->protocol
;
199 struct snd_pcm_substream
*pcm
;
201 /* This field is not used. */
204 pcm
= ACCESS_ONCE(s
->pcm
);
206 p
->transfer_samples(s
, pcm
, buffer
, data_blocks
);
208 write_pcm_silence(s
, buffer
, data_blocks
);
213 int amdtp_tscm_init(struct amdtp_stream
*s
, struct fw_unit
*unit
,
214 enum amdtp_stream_direction dir
, unsigned int pcm_channels
)
216 amdtp_stream_process_data_blocks_t process_data_blocks
;
217 struct amdtp_tscm
*p
;
221 if (dir
== AMDTP_IN_STREAM
) {
222 fmt
= AMDTP_FMT_TSCM_TX
;
223 process_data_blocks
= process_tx_data_blocks
;
225 fmt
= AMDTP_FMT_TSCM_RX
;
226 process_data_blocks
= process_rx_data_blocks
;
229 err
= amdtp_stream_init(s
, unit
, dir
,
230 CIP_NONBLOCKING
| CIP_SKIP_DBC_ZERO_CHECK
, fmt
,
231 process_data_blocks
, sizeof(struct amdtp_tscm
));
235 /* Use fixed value for FDF field. */
238 /* This protocol uses fixed number of data channels for PCM samples. */
240 p
->pcm_channels
= pcm_channels
;