ALSA: firewire-tascam: register the size of PCM period to AMDTP domain
[linux-2.6-block.git] / sound / firewire / tascam / tascam-pcm.c
CommitLineData
da607e19 1// SPDX-License-Identifier: GPL-2.0-only
e453df44
TS
2/*
3 * tascam-pcm.c - a part of driver for TASCAM FireWire series
4 *
5 * Copyright (c) 2015 Takashi Sakamoto
e453df44
TS
6 */
7
8#include "tascam.h"
9
e453df44
TS
10static int pcm_init_hw_params(struct snd_tscm *tscm,
11 struct snd_pcm_substream *substream)
12{
e453df44 13 struct snd_pcm_runtime *runtime = substream->runtime;
55799c5a 14 struct snd_pcm_hardware *hw = &runtime->hw;
e453df44
TS
15 struct amdtp_stream *stream;
16 unsigned int pcm_channels;
17
e453df44
TS
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;
22 } else {
a02cb8f8 23 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
e453df44
TS
24 stream = &tscm->rx_stream;
25 pcm_channels = tscm->spec->pcm_playback_analog_channels;
26 }
27
28 if (tscm->spec->has_adat)
29 pcm_channels += 8;
30 if (tscm->spec->has_spdif)
31 pcm_channels += 2;
32 runtime->hw.channels_min = runtime->hw.channels_max = pcm_channels;
33
55799c5a
TS
34 hw->rates = SNDRV_PCM_RATE_44100 |
35 SNDRV_PCM_RATE_48000 |
36 SNDRV_PCM_RATE_88200 |
37 SNDRV_PCM_RATE_96000;
38 snd_pcm_limit_hw_rates(runtime);
e453df44
TS
39
40 return amdtp_tscm_add_pcm_hw_constraints(stream, runtime);
41}
42
43static int pcm_open(struct snd_pcm_substream *substream)
44{
45 struct snd_tscm *tscm = substream->private_data;
46 enum snd_tscm_clock clock;
47 unsigned int rate;
48 int err;
49
e5e0c3dd
TS
50 err = snd_tscm_stream_lock_try(tscm);
51 if (err < 0)
52 goto end;
53
e453df44
TS
54 err = pcm_init_hw_params(tscm, substream);
55 if (err < 0)
e5e0c3dd 56 goto err_locked;
e453df44
TS
57
58 err = snd_tscm_stream_get_clock(tscm, &clock);
2617120f
TS
59 if (err < 0)
60 goto err_locked;
61
e453df44
TS
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);
66 if (err < 0)
e5e0c3dd 67 goto err_locked;
e453df44
TS
68 substream->runtime->hw.rate_min = rate;
69 substream->runtime->hw.rate_max = rate;
70 }
71
72 snd_pcm_set_sync(substream);
e5e0c3dd
TS
73end:
74 return err;
75err_locked:
76 snd_tscm_stream_lock_release(tscm);
e453df44
TS
77 return err;
78}
79
80static int pcm_close(struct snd_pcm_substream *substream)
81{
e5e0c3dd
TS
82 struct snd_tscm *tscm = substream->private_data;
83
84 snd_tscm_stream_lock_release(tscm);
85
e453df44
TS
86 return 0;
87}
88
d8f291b7
TS
89static int pcm_hw_params(struct snd_pcm_substream *substream,
90 struct snd_pcm_hw_params *hw_params)
e453df44
TS
91{
92 struct snd_tscm *tscm = substream->private_data;
93 int err;
94
95 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
96 params_buffer_bytes(hw_params));
97 if (err < 0)
98 return err;
99
100 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
07b26642 101 unsigned int rate = params_rate(hw_params);
262542ed 102 unsigned int frames_per_period = params_period_size(hw_params);
07b26642 103
e453df44 104 mutex_lock(&tscm->mutex);
262542ed
TS
105 err = snd_tscm_stream_reserve_duplex(tscm, rate,
106 frames_per_period);
07b26642
TS
107 if (err >= 0)
108 ++tscm->substreams_counter;
e453df44
TS
109 mutex_unlock(&tscm->mutex);
110 }
111
07b26642 112 return err;
e453df44
TS
113}
114
d8f291b7 115static int pcm_hw_free(struct snd_pcm_substream *substream)
e453df44
TS
116{
117 struct snd_tscm *tscm = substream->private_data;
118
119 mutex_lock(&tscm->mutex);
120
121 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
07b26642 122 --tscm->substreams_counter;
e453df44
TS
123
124 snd_tscm_stream_stop_duplex(tscm);
125
126 mutex_unlock(&tscm->mutex);
127
128 return snd_pcm_lib_free_vmalloc_buffer(substream);
129}
130
131static int pcm_capture_prepare(struct snd_pcm_substream *substream)
132{
133 struct snd_tscm *tscm = substream->private_data;
134 struct snd_pcm_runtime *runtime = substream->runtime;
135 int err;
136
137 mutex_lock(&tscm->mutex);
138
139 err = snd_tscm_stream_start_duplex(tscm, runtime->rate);
140 if (err >= 0)
141 amdtp_stream_pcm_prepare(&tscm->tx_stream);
142
143 mutex_unlock(&tscm->mutex);
144
145 return err;
146}
147
148static int pcm_playback_prepare(struct snd_pcm_substream *substream)
149{
150 struct snd_tscm *tscm = substream->private_data;
151 struct snd_pcm_runtime *runtime = substream->runtime;
152 int err;
153
154 mutex_lock(&tscm->mutex);
155
156 err = snd_tscm_stream_start_duplex(tscm, runtime->rate);
157 if (err >= 0)
158 amdtp_stream_pcm_prepare(&tscm->rx_stream);
159
160 mutex_unlock(&tscm->mutex);
161
162 return err;
163}
164
165static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
166{
167 struct snd_tscm *tscm = substream->private_data;
168
169 switch (cmd) {
170 case SNDRV_PCM_TRIGGER_START:
171 amdtp_stream_pcm_trigger(&tscm->tx_stream, substream);
172 break;
173 case SNDRV_PCM_TRIGGER_STOP:
174 amdtp_stream_pcm_trigger(&tscm->tx_stream, NULL);
175 break;
176 default:
177 return -EINVAL;
178 }
179
180 return 0;
181}
182
183static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
184{
185 struct snd_tscm *tscm = substream->private_data;
186
187 switch (cmd) {
188 case SNDRV_PCM_TRIGGER_START:
189 amdtp_stream_pcm_trigger(&tscm->rx_stream, substream);
190 break;
191 case SNDRV_PCM_TRIGGER_STOP:
192 amdtp_stream_pcm_trigger(&tscm->rx_stream, NULL);
193 break;
194 default:
195 return -EINVAL;
196 }
197
198 return 0;
199}
200
201static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
202{
203 struct snd_tscm *tscm = sbstrm->private_data;
204
205 return amdtp_stream_pcm_pointer(&tscm->tx_stream);
206}
207
208static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
209{
210 struct snd_tscm *tscm = sbstrm->private_data;
211
212 return amdtp_stream_pcm_pointer(&tscm->rx_stream);
213}
214
875becf8
TS
215static int pcm_capture_ack(struct snd_pcm_substream *substream)
216{
217 struct snd_tscm *tscm = substream->private_data;
218
219 return amdtp_stream_pcm_ack(&tscm->tx_stream);
220}
221
222static int pcm_playback_ack(struct snd_pcm_substream *substream)
223{
224 struct snd_tscm *tscm = substream->private_data;
225
226 return amdtp_stream_pcm_ack(&tscm->rx_stream);
227}
228
e453df44
TS
229int snd_tscm_create_pcm_devices(struct snd_tscm *tscm)
230{
92128236
TS
231 static const struct snd_pcm_ops capture_ops = {
232 .open = pcm_open,
233 .close = pcm_close,
234 .ioctl = snd_pcm_lib_ioctl,
d8f291b7
TS
235 .hw_params = pcm_hw_params,
236 .hw_free = pcm_hw_free,
92128236
TS
237 .prepare = pcm_capture_prepare,
238 .trigger = pcm_capture_trigger,
239 .pointer = pcm_capture_pointer,
875becf8 240 .ack = pcm_capture_ack,
92128236
TS
241 .page = snd_pcm_lib_get_vmalloc_page,
242 };
243 static const struct snd_pcm_ops playback_ops = {
244 .open = pcm_open,
245 .close = pcm_close,
246 .ioctl = snd_pcm_lib_ioctl,
d8f291b7
TS
247 .hw_params = pcm_hw_params,
248 .hw_free = pcm_hw_free,
92128236
TS
249 .prepare = pcm_playback_prepare,
250 .trigger = pcm_playback_trigger,
251 .pointer = pcm_playback_pointer,
875becf8 252 .ack = pcm_playback_ack,
92128236 253 .page = snd_pcm_lib_get_vmalloc_page,
92128236 254 };
e453df44
TS
255 struct snd_pcm *pcm;
256 int err;
257
258 err = snd_pcm_new(tscm->card, tscm->card->driver, 0, 1, 1, &pcm);
259 if (err < 0)
260 return err;
261
262 pcm->private_data = tscm;
263 snprintf(pcm->name, sizeof(pcm->name),
264 "%s PCM", tscm->card->shortname);
92128236
TS
265 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
266 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
e453df44
TS
267
268 return 0;
269}