1 // SPDX-License-Identifier: GPL-2.0-only
3 * tascam-pcm.c - a part of driver for TASCAM FireWire series
5 * Copyright (c) 2015 Takashi Sakamoto
10 static int pcm_init_hw_params(struct snd_tscm *tscm,
11 struct snd_pcm_substream *substream)
13 struct snd_pcm_runtime *runtime = substream->runtime;
14 struct snd_pcm_hardware *hw = &runtime->hw;
15 struct amdtp_stream *stream;
16 unsigned int pcm_channels;
18 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
19 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
20 stream = &tscm->tx_stream;
21 pcm_channels = tscm->spec->pcm_capture_analog_channels;
23 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
24 stream = &tscm->rx_stream;
25 pcm_channels = tscm->spec->pcm_playback_analog_channels;
28 if (tscm->spec->has_adat)
30 if (tscm->spec->has_spdif)
32 runtime->hw.channels_min = runtime->hw.channels_max = pcm_channels;
34 hw->rates = SNDRV_PCM_RATE_44100 |
35 SNDRV_PCM_RATE_48000 |
36 SNDRV_PCM_RATE_88200 |
38 snd_pcm_limit_hw_rates(runtime);
40 return amdtp_tscm_add_pcm_hw_constraints(stream, runtime);
43 static int pcm_open(struct snd_pcm_substream *substream)
45 struct snd_tscm *tscm = substream->private_data;
46 enum snd_tscm_clock clock;
50 err = snd_tscm_stream_lock_try(tscm);
54 err = pcm_init_hw_params(tscm, substream);
58 err = snd_tscm_stream_get_clock(tscm, &clock);
62 if (clock != SND_TSCM_CLOCK_INTERNAL ||
63 amdtp_stream_pcm_running(&tscm->rx_stream) ||
64 amdtp_stream_pcm_running(&tscm->tx_stream)) {
65 err = snd_tscm_stream_get_rate(tscm, &rate);
68 substream->runtime->hw.rate_min = rate;
69 substream->runtime->hw.rate_max = rate;
72 snd_pcm_set_sync(substream);
76 snd_tscm_stream_lock_release(tscm);
80 static int pcm_close(struct snd_pcm_substream *substream)
82 struct snd_tscm *tscm = substream->private_data;
84 snd_tscm_stream_lock_release(tscm);
89 static int pcm_hw_params(struct snd_pcm_substream *substream,
90 struct snd_pcm_hw_params *hw_params)
92 struct snd_tscm *tscm = substream->private_data;
95 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
96 params_buffer_bytes(hw_params));
100 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
101 unsigned int rate = params_rate(hw_params);
102 unsigned int frames_per_period = params_period_size(hw_params);
104 mutex_lock(&tscm->mutex);
105 err = snd_tscm_stream_reserve_duplex(tscm, rate,
108 ++tscm->substreams_counter;
109 mutex_unlock(&tscm->mutex);
115 static int pcm_hw_free(struct snd_pcm_substream *substream)
117 struct snd_tscm *tscm = substream->private_data;
119 mutex_lock(&tscm->mutex);
121 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
122 --tscm->substreams_counter;
124 snd_tscm_stream_stop_duplex(tscm);
126 mutex_unlock(&tscm->mutex);
128 return snd_pcm_lib_free_vmalloc_buffer(substream);
131 static int pcm_capture_prepare(struct snd_pcm_substream *substream)
133 struct snd_tscm *tscm = substream->private_data;
134 struct snd_pcm_runtime *runtime = substream->runtime;
137 mutex_lock(&tscm->mutex);
139 err = snd_tscm_stream_start_duplex(tscm, runtime->rate);
141 amdtp_stream_pcm_prepare(&tscm->tx_stream);
143 mutex_unlock(&tscm->mutex);
148 static int pcm_playback_prepare(struct snd_pcm_substream *substream)
150 struct snd_tscm *tscm = substream->private_data;
151 struct snd_pcm_runtime *runtime = substream->runtime;
154 mutex_lock(&tscm->mutex);
156 err = snd_tscm_stream_start_duplex(tscm, runtime->rate);
158 amdtp_stream_pcm_prepare(&tscm->rx_stream);
160 mutex_unlock(&tscm->mutex);
165 static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
167 struct snd_tscm *tscm = substream->private_data;
170 case SNDRV_PCM_TRIGGER_START:
171 amdtp_stream_pcm_trigger(&tscm->tx_stream, substream);
173 case SNDRV_PCM_TRIGGER_STOP:
174 amdtp_stream_pcm_trigger(&tscm->tx_stream, NULL);
183 static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
185 struct snd_tscm *tscm = substream->private_data;
188 case SNDRV_PCM_TRIGGER_START:
189 amdtp_stream_pcm_trigger(&tscm->rx_stream, substream);
191 case SNDRV_PCM_TRIGGER_STOP:
192 amdtp_stream_pcm_trigger(&tscm->rx_stream, NULL);
201 static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
203 struct snd_tscm *tscm = sbstrm->private_data;
205 return amdtp_stream_pcm_pointer(&tscm->tx_stream);
208 static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
210 struct snd_tscm *tscm = sbstrm->private_data;
212 return amdtp_stream_pcm_pointer(&tscm->rx_stream);
215 static int pcm_capture_ack(struct snd_pcm_substream *substream)
217 struct snd_tscm *tscm = substream->private_data;
219 return amdtp_stream_pcm_ack(&tscm->tx_stream);
222 static int pcm_playback_ack(struct snd_pcm_substream *substream)
224 struct snd_tscm *tscm = substream->private_data;
226 return amdtp_stream_pcm_ack(&tscm->rx_stream);
229 int snd_tscm_create_pcm_devices(struct snd_tscm *tscm)
231 static const struct snd_pcm_ops capture_ops = {
234 .ioctl = snd_pcm_lib_ioctl,
235 .hw_params = pcm_hw_params,
236 .hw_free = pcm_hw_free,
237 .prepare = pcm_capture_prepare,
238 .trigger = pcm_capture_trigger,
239 .pointer = pcm_capture_pointer,
240 .ack = pcm_capture_ack,
241 .page = snd_pcm_lib_get_vmalloc_page,
243 static const struct snd_pcm_ops playback_ops = {
246 .ioctl = snd_pcm_lib_ioctl,
247 .hw_params = pcm_hw_params,
248 .hw_free = pcm_hw_free,
249 .prepare = pcm_playback_prepare,
250 .trigger = pcm_playback_trigger,
251 .pointer = pcm_playback_pointer,
252 .ack = pcm_playback_ack,
253 .page = snd_pcm_lib_get_vmalloc_page,
258 err = snd_pcm_new(tscm->card, tscm->card->driver, 0, 1, 1, &pcm);
262 pcm->private_data = tscm;
263 snprintf(pcm->name, sizeof(pcm->name),
264 "%s PCM", tscm->card->shortname);
265 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
266 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);