2 * tascam-pcm.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.
11 static int pcm_init_hw_params(struct snd_tscm *tscm,
12 struct snd_pcm_substream *substream)
14 struct snd_pcm_runtime *runtime = substream->runtime;
15 struct snd_pcm_hardware *hw = &runtime->hw;
16 struct amdtp_stream *stream;
17 unsigned int pcm_channels;
19 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
20 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
21 stream = &tscm->tx_stream;
22 pcm_channels = tscm->spec->pcm_capture_analog_channels;
24 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
25 stream = &tscm->rx_stream;
26 pcm_channels = tscm->spec->pcm_playback_analog_channels;
29 if (tscm->spec->has_adat)
31 if (tscm->spec->has_spdif)
33 runtime->hw.channels_min = runtime->hw.channels_max = pcm_channels;
35 hw->rates = SNDRV_PCM_RATE_44100 |
36 SNDRV_PCM_RATE_48000 |
37 SNDRV_PCM_RATE_88200 |
39 snd_pcm_limit_hw_rates(runtime);
41 return amdtp_tscm_add_pcm_hw_constraints(stream, runtime);
44 static int pcm_open(struct snd_pcm_substream *substream)
46 struct snd_tscm *tscm = substream->private_data;
47 enum snd_tscm_clock clock;
51 err = snd_tscm_stream_lock_try(tscm);
55 err = pcm_init_hw_params(tscm, substream);
59 err = snd_tscm_stream_get_clock(tscm, &clock);
63 if (clock != SND_TSCM_CLOCK_INTERNAL ||
64 amdtp_stream_pcm_running(&tscm->rx_stream) ||
65 amdtp_stream_pcm_running(&tscm->tx_stream)) {
66 err = snd_tscm_stream_get_rate(tscm, &rate);
69 substream->runtime->hw.rate_min = rate;
70 substream->runtime->hw.rate_max = rate;
73 snd_pcm_set_sync(substream);
77 snd_tscm_stream_lock_release(tscm);
81 static int pcm_close(struct snd_pcm_substream *substream)
83 struct snd_tscm *tscm = substream->private_data;
85 snd_tscm_stream_lock_release(tscm);
90 static int pcm_capture_hw_params(struct snd_pcm_substream *substream,
91 struct snd_pcm_hw_params *hw_params)
93 struct snd_tscm *tscm = substream->private_data;
96 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
97 params_buffer_bytes(hw_params));
101 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
102 mutex_lock(&tscm->mutex);
103 tscm->substreams_counter++;
104 mutex_unlock(&tscm->mutex);
110 static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
111 struct snd_pcm_hw_params *hw_params)
113 struct snd_tscm *tscm = substream->private_data;
116 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
117 params_buffer_bytes(hw_params));
121 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
122 mutex_lock(&tscm->mutex);
123 tscm->substreams_counter++;
124 mutex_unlock(&tscm->mutex);
130 static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
132 struct snd_tscm *tscm = substream->private_data;
134 mutex_lock(&tscm->mutex);
136 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
137 tscm->substreams_counter--;
139 snd_tscm_stream_stop_duplex(tscm);
141 mutex_unlock(&tscm->mutex);
143 return snd_pcm_lib_free_vmalloc_buffer(substream);
146 static int pcm_playback_hw_free(struct snd_pcm_substream *substream)
148 struct snd_tscm *tscm = substream->private_data;
150 mutex_lock(&tscm->mutex);
152 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
153 tscm->substreams_counter--;
155 snd_tscm_stream_stop_duplex(tscm);
157 mutex_unlock(&tscm->mutex);
159 return snd_pcm_lib_free_vmalloc_buffer(substream);
162 static int pcm_capture_prepare(struct snd_pcm_substream *substream)
164 struct snd_tscm *tscm = substream->private_data;
165 struct snd_pcm_runtime *runtime = substream->runtime;
168 mutex_lock(&tscm->mutex);
170 err = snd_tscm_stream_start_duplex(tscm, runtime->rate);
172 amdtp_stream_pcm_prepare(&tscm->tx_stream);
174 mutex_unlock(&tscm->mutex);
179 static int pcm_playback_prepare(struct snd_pcm_substream *substream)
181 struct snd_tscm *tscm = substream->private_data;
182 struct snd_pcm_runtime *runtime = substream->runtime;
185 mutex_lock(&tscm->mutex);
187 err = snd_tscm_stream_start_duplex(tscm, runtime->rate);
189 amdtp_stream_pcm_prepare(&tscm->rx_stream);
191 mutex_unlock(&tscm->mutex);
196 static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
198 struct snd_tscm *tscm = substream->private_data;
201 case SNDRV_PCM_TRIGGER_START:
202 amdtp_stream_pcm_trigger(&tscm->tx_stream, substream);
204 case SNDRV_PCM_TRIGGER_STOP:
205 amdtp_stream_pcm_trigger(&tscm->tx_stream, NULL);
214 static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
216 struct snd_tscm *tscm = substream->private_data;
219 case SNDRV_PCM_TRIGGER_START:
220 amdtp_stream_pcm_trigger(&tscm->rx_stream, substream);
222 case SNDRV_PCM_TRIGGER_STOP:
223 amdtp_stream_pcm_trigger(&tscm->rx_stream, NULL);
232 static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
234 struct snd_tscm *tscm = sbstrm->private_data;
236 return amdtp_stream_pcm_pointer(&tscm->tx_stream);
239 static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
241 struct snd_tscm *tscm = sbstrm->private_data;
243 return amdtp_stream_pcm_pointer(&tscm->rx_stream);
246 static int pcm_capture_ack(struct snd_pcm_substream *substream)
248 struct snd_tscm *tscm = substream->private_data;
250 return amdtp_stream_pcm_ack(&tscm->tx_stream);
253 static int pcm_playback_ack(struct snd_pcm_substream *substream)
255 struct snd_tscm *tscm = substream->private_data;
257 return amdtp_stream_pcm_ack(&tscm->rx_stream);
260 int snd_tscm_create_pcm_devices(struct snd_tscm *tscm)
262 static const struct snd_pcm_ops capture_ops = {
265 .ioctl = snd_pcm_lib_ioctl,
266 .hw_params = pcm_capture_hw_params,
267 .hw_free = pcm_capture_hw_free,
268 .prepare = pcm_capture_prepare,
269 .trigger = pcm_capture_trigger,
270 .pointer = pcm_capture_pointer,
271 .ack = pcm_capture_ack,
272 .page = snd_pcm_lib_get_vmalloc_page,
274 static const struct snd_pcm_ops playback_ops = {
277 .ioctl = snd_pcm_lib_ioctl,
278 .hw_params = pcm_playback_hw_params,
279 .hw_free = pcm_playback_hw_free,
280 .prepare = pcm_playback_prepare,
281 .trigger = pcm_playback_trigger,
282 .pointer = pcm_playback_pointer,
283 .ack = pcm_playback_ack,
284 .page = snd_pcm_lib_get_vmalloc_page,
289 err = snd_pcm_new(tscm->card, tscm->card->driver, 0, 1, 1, &pcm);
293 pcm->private_data = tscm;
294 snprintf(pcm->name, sizeof(pcm->name),
295 "%s PCM", tscm->card->shortname);
296 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
297 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);