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 unsigned int process_tx_data_blocks(struct amdtp_stream
*s
,
122 unsigned int data_blocks
,
125 struct snd_pcm_substream
*pcm
;
127 pcm
= READ_ONCE(s
->pcm
);
128 if (data_blocks
> 0 && pcm
)
129 read_pcm_s32(s
, pcm
, buffer
, data_blocks
);
131 /* A place holder for control messages. */
136 static unsigned int process_rx_data_blocks(struct amdtp_stream
*s
,
138 unsigned int data_blocks
,
141 struct snd_pcm_substream
*pcm
;
143 /* This field is not used. */
146 pcm
= READ_ONCE(s
->pcm
);
148 write_pcm_s32(s
, pcm
, buffer
, data_blocks
);
150 write_pcm_silence(s
, buffer
, data_blocks
);
155 int amdtp_tscm_init(struct amdtp_stream
*s
, struct fw_unit
*unit
,
156 enum amdtp_stream_direction dir
, unsigned int pcm_channels
)
158 amdtp_stream_process_data_blocks_t process_data_blocks
;
159 struct amdtp_tscm
*p
;
163 if (dir
== AMDTP_IN_STREAM
) {
164 fmt
= AMDTP_FMT_TSCM_TX
;
165 process_data_blocks
= process_tx_data_blocks
;
167 fmt
= AMDTP_FMT_TSCM_RX
;
168 process_data_blocks
= process_rx_data_blocks
;
171 err
= amdtp_stream_init(s
, unit
, dir
,
172 CIP_NONBLOCKING
| CIP_SKIP_DBC_ZERO_CHECK
, fmt
,
173 process_data_blocks
, sizeof(struct amdtp_tscm
));
177 /* Use fixed value for FDF field. */
180 /* This protocol uses fixed number of data channels for PCM samples. */
182 p
->pcm_channels
= pcm_channels
;