1 /*
2 * tascam-pcm.c - a part of driver for TASCAM FireWire series
3 *
4 * Copyright (c) 2015 Takashi Sakamoto
5 *
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
8
9 #include "tascam.h"
10
set_buffer_params(struct snd_pcm_hardware * hw)11 static void set_buffer_params(struct snd_pcm_hardware *hw)
12 {
13 hw->period_bytes_min = 4 * hw->channels_min;
14 hw->period_bytes_max = hw->period_bytes_min * 2048;
15 hw->buffer_bytes_max = hw->period_bytes_max * 2;
16
17 hw->periods_min = 2;
18 hw->periods_max = UINT_MAX;
19 }
20
pcm_init_hw_params(struct snd_tscm * tscm,struct snd_pcm_substream * substream)21 static int pcm_init_hw_params(struct snd_tscm *tscm,
22 struct snd_pcm_substream *substream)
23 {
24 static const struct snd_pcm_hardware hardware = {
25 .info = SNDRV_PCM_INFO_BATCH |
26 SNDRV_PCM_INFO_BLOCK_TRANSFER |
27 SNDRV_PCM_INFO_INTERLEAVED |
28 SNDRV_PCM_INFO_JOINT_DUPLEX |
29 SNDRV_PCM_INFO_MMAP |
30 SNDRV_PCM_INFO_MMAP_VALID,
31 .rates = SNDRV_PCM_RATE_44100 |
32 SNDRV_PCM_RATE_48000 |
33 SNDRV_PCM_RATE_88200 |
34 SNDRV_PCM_RATE_96000,
35 .rate_min = 44100,
36 .rate_max = 96000,
37 .channels_min = 10,
38 .channels_max = 18,
39 };
40 struct snd_pcm_runtime *runtime = substream->runtime;
41 struct amdtp_stream *stream;
42 unsigned int pcm_channels;
43
44 runtime->hw = hardware;
45
46 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
47 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
48 stream = &tscm->tx_stream;
49 pcm_channels = tscm->spec->pcm_capture_analog_channels;
50 } else {
51 runtime->hw.formats =
52 SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_S32;
53 stream = &tscm->rx_stream;
54 pcm_channels = tscm->spec->pcm_playback_analog_channels;
55 }
56
57 if (tscm->spec->has_adat)
58 pcm_channels += 8;
59 if (tscm->spec->has_spdif)
60 pcm_channels += 2;
61 runtime->hw.channels_min = runtime->hw.channels_max = pcm_channels;
62
63 set_buffer_params(&runtime->hw);
64
65 return amdtp_tscm_add_pcm_hw_constraints(stream, runtime);
66 }
67
pcm_open(struct snd_pcm_substream * substream)68 static int pcm_open(struct snd_pcm_substream *substream)
69 {
70 struct snd_tscm *tscm = substream->private_data;
71 enum snd_tscm_clock clock;
72 unsigned int rate;
73 int err;
74
75 err = snd_tscm_stream_lock_try(tscm);
76 if (err < 0)
77 goto end;
78
79 err = pcm_init_hw_params(tscm, substream);
80 if (err < 0)
81 goto err_locked;
82
83 err = snd_tscm_stream_get_clock(tscm, &clock);
84 if (err < 0)
85 goto err_locked;
86
87 if (clock != SND_TSCM_CLOCK_INTERNAL ||
88 amdtp_stream_pcm_running(&tscm->rx_stream) ||
89 amdtp_stream_pcm_running(&tscm->tx_stream)) {
90 err = snd_tscm_stream_get_rate(tscm, &rate);
91 if (err < 0)
92 goto err_locked;
93 substream->runtime->hw.rate_min = rate;
94 substream->runtime->hw.rate_max = rate;
95 }
96
97 snd_pcm_set_sync(substream);
98 end:
99 return err;
100 err_locked:
101 snd_tscm_stream_lock_release(tscm);
102 return err;
103 }
104
pcm_close(struct snd_pcm_substream * substream)105 static int pcm_close(struct snd_pcm_substream *substream)
106 {
107 struct snd_tscm *tscm = substream->private_data;
108
109 snd_tscm_stream_lock_release(tscm);
110
111 return 0;
112 }
113
pcm_capture_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)114 static int pcm_capture_hw_params(struct snd_pcm_substream *substream,
115 struct snd_pcm_hw_params *hw_params)
116 {
117 struct snd_tscm *tscm = substream->private_data;
118 int err;
119
120 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
121 params_buffer_bytes(hw_params));
122 if (err < 0)
123 return err;
124
125 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
126 mutex_lock(&tscm->mutex);
127 tscm->substreams_counter++;
128 mutex_unlock(&tscm->mutex);
129 }
130
131 amdtp_tscm_set_pcm_format(&tscm->tx_stream, params_format(hw_params));
132
133 return 0;
134 }
135
pcm_playback_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)136 static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
137 struct snd_pcm_hw_params *hw_params)
138 {
139 struct snd_tscm *tscm = substream->private_data;
140 int err;
141
142 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
143 params_buffer_bytes(hw_params));
144 if (err < 0)
145 return err;
146
147 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
148 mutex_lock(&tscm->mutex);
149 tscm->substreams_counter++;
150 mutex_unlock(&tscm->mutex);
151 }
152
153 amdtp_tscm_set_pcm_format(&tscm->rx_stream, params_format(hw_params));
154
155 return 0;
156 }
157
pcm_capture_hw_free(struct snd_pcm_substream * substream)158 static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
159 {
160 struct snd_tscm *tscm = substream->private_data;
161
162 mutex_lock(&tscm->mutex);
163
164 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
165 tscm->substreams_counter--;
166
167 snd_tscm_stream_stop_duplex(tscm);
168
169 mutex_unlock(&tscm->mutex);
170
171 return snd_pcm_lib_free_vmalloc_buffer(substream);
172 }
173
pcm_playback_hw_free(struct snd_pcm_substream * substream)174 static int pcm_playback_hw_free(struct snd_pcm_substream *substream)
175 {
176 struct snd_tscm *tscm = substream->private_data;
177
178 mutex_lock(&tscm->mutex);
179
180 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
181 tscm->substreams_counter--;
182
183 snd_tscm_stream_stop_duplex(tscm);
184
185 mutex_unlock(&tscm->mutex);
186
187 return snd_pcm_lib_free_vmalloc_buffer(substream);
188 }
189
pcm_capture_prepare(struct snd_pcm_substream * substream)190 static int pcm_capture_prepare(struct snd_pcm_substream *substream)
191 {
192 struct snd_tscm *tscm = substream->private_data;
193 struct snd_pcm_runtime *runtime = substream->runtime;
194 int err;
195
196 mutex_lock(&tscm->mutex);
197
198 err = snd_tscm_stream_start_duplex(tscm, runtime->rate);
199 if (err >= 0)
200 amdtp_stream_pcm_prepare(&tscm->tx_stream);
201
202 mutex_unlock(&tscm->mutex);
203
204 return err;
205 }
206
pcm_playback_prepare(struct snd_pcm_substream * substream)207 static int pcm_playback_prepare(struct snd_pcm_substream *substream)
208 {
209 struct snd_tscm *tscm = substream->private_data;
210 struct snd_pcm_runtime *runtime = substream->runtime;
211 int err;
212
213 mutex_lock(&tscm->mutex);
214
215 err = snd_tscm_stream_start_duplex(tscm, runtime->rate);
216 if (err >= 0)
217 amdtp_stream_pcm_prepare(&tscm->rx_stream);
218
219 mutex_unlock(&tscm->mutex);
220
221 return err;
222 }
223
pcm_capture_trigger(struct snd_pcm_substream * substream,int cmd)224 static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
225 {
226 struct snd_tscm *tscm = substream->private_data;
227
228 switch (cmd) {
229 case SNDRV_PCM_TRIGGER_START:
230 amdtp_stream_pcm_trigger(&tscm->tx_stream, substream);
231 break;
232 case SNDRV_PCM_TRIGGER_STOP:
233 amdtp_stream_pcm_trigger(&tscm->tx_stream, NULL);
234 break;
235 default:
236 return -EINVAL;
237 }
238
239 return 0;
240 }
241
pcm_playback_trigger(struct snd_pcm_substream * substream,int cmd)242 static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
243 {
244 struct snd_tscm *tscm = substream->private_data;
245
246 switch (cmd) {
247 case SNDRV_PCM_TRIGGER_START:
248 amdtp_stream_pcm_trigger(&tscm->rx_stream, substream);
249 break;
250 case SNDRV_PCM_TRIGGER_STOP:
251 amdtp_stream_pcm_trigger(&tscm->rx_stream, NULL);
252 break;
253 default:
254 return -EINVAL;
255 }
256
257 return 0;
258 }
259
pcm_capture_pointer(struct snd_pcm_substream * sbstrm)260 static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
261 {
262 struct snd_tscm *tscm = sbstrm->private_data;
263
264 return amdtp_stream_pcm_pointer(&tscm->tx_stream);
265 }
266
pcm_playback_pointer(struct snd_pcm_substream * sbstrm)267 static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
268 {
269 struct snd_tscm *tscm = sbstrm->private_data;
270
271 return amdtp_stream_pcm_pointer(&tscm->rx_stream);
272 }
273
274 static struct snd_pcm_ops pcm_capture_ops = {
275 .open = pcm_open,
276 .close = pcm_close,
277 .ioctl = snd_pcm_lib_ioctl,
278 .hw_params = pcm_capture_hw_params,
279 .hw_free = pcm_capture_hw_free,
280 .prepare = pcm_capture_prepare,
281 .trigger = pcm_capture_trigger,
282 .pointer = pcm_capture_pointer,
283 .page = snd_pcm_lib_get_vmalloc_page,
284 };
285
286 static struct snd_pcm_ops pcm_playback_ops = {
287 .open = pcm_open,
288 .close = pcm_close,
289 .ioctl = snd_pcm_lib_ioctl,
290 .hw_params = pcm_playback_hw_params,
291 .hw_free = pcm_playback_hw_free,
292 .prepare = pcm_playback_prepare,
293 .trigger = pcm_playback_trigger,
294 .pointer = pcm_playback_pointer,
295 .page = snd_pcm_lib_get_vmalloc_page,
296 .mmap = snd_pcm_lib_mmap_vmalloc,
297 };
298
snd_tscm_create_pcm_devices(struct snd_tscm * tscm)299 int snd_tscm_create_pcm_devices(struct snd_tscm *tscm)
300 {
301 struct snd_pcm *pcm;
302 int err;
303
304 err = snd_pcm_new(tscm->card, tscm->card->driver, 0, 1, 1, &pcm);
305 if (err < 0)
306 return err;
307
308 pcm->private_data = tscm;
309 snprintf(pcm->name, sizeof(pcm->name),
310 "%s PCM", tscm->card->shortname);
311 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
312 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);
313
314 return 0;
315 }
316