forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/sound/firewire/bebob/bebob_pcm.c
....@@ -1,9 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * bebob_pcm.c - a part of driver for BeBoB based devices
34 *
45 * Copyright (c) 2013-2014 Takashi Sakamoto
5
- *
6
- * Licensed under the terms of the GNU General Public License, version 2.
76 */
87
98 #include "./bebob.h"
....@@ -130,18 +129,17 @@
130129 return err;
131130 }
132131
133
-static int
134
-pcm_open(struct snd_pcm_substream *substream)
132
+static int pcm_open(struct snd_pcm_substream *substream)
135133 {
136134 struct snd_bebob *bebob = substream->private_data;
137135 const struct snd_bebob_rate_spec *spec = bebob->spec->rate;
138
- unsigned int sampling_rate;
136
+ struct amdtp_domain *d = &bebob->domain;
139137 enum snd_bebob_clock_type src;
140138 int err;
141139
142140 err = snd_bebob_stream_lock_try(bebob);
143141 if (err < 0)
144
- goto end;
142
+ return err;
145143
146144 err = pcm_init_hw_params(bebob, substream);
147145 if (err < 0)
....@@ -151,15 +149,20 @@
151149 if (err < 0)
152150 goto err_locked;
153151
154
- /*
155
- * When source of clock is internal or any PCM stream are running,
156
- * the available sampling rate is limited at current sampling rate.
157
- */
152
+ mutex_lock(&bebob->mutex);
153
+
154
+ // When source of clock is not internal or any stream is reserved for
155
+ // transmission of PCM frames, the available sampling rate is limited
156
+ // at current one.
158157 if (src == SND_BEBOB_CLOCK_TYPE_EXTERNAL ||
159
- amdtp_stream_pcm_running(&bebob->tx_stream) ||
160
- amdtp_stream_pcm_running(&bebob->rx_stream)) {
158
+ (bebob->substreams_counter > 0 && d->events_per_period > 0)) {
159
+ unsigned int frames_per_period = d->events_per_period;
160
+ unsigned int frames_per_buffer = d->events_per_buffer;
161
+ unsigned int sampling_rate;
162
+
161163 err = spec->get(bebob, &sampling_rate);
162164 if (err < 0) {
165
+ mutex_unlock(&bebob->mutex);
163166 dev_err(&bebob->unit->device,
164167 "fail to get sampling rate: %d\n", err);
165168 goto err_locked;
....@@ -167,11 +170,31 @@
167170
168171 substream->runtime->hw.rate_min = sampling_rate;
169172 substream->runtime->hw.rate_max = sampling_rate;
173
+
174
+ if (frames_per_period > 0) {
175
+ err = snd_pcm_hw_constraint_minmax(substream->runtime,
176
+ SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
177
+ frames_per_period, frames_per_period);
178
+ if (err < 0) {
179
+ mutex_unlock(&bebob->mutex);
180
+ goto err_locked;
181
+ }
182
+
183
+ err = snd_pcm_hw_constraint_minmax(substream->runtime,
184
+ SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
185
+ frames_per_buffer, frames_per_buffer);
186
+ if (err < 0) {
187
+ mutex_unlock(&bebob->mutex);
188
+ goto err_locked;
189
+ }
190
+ }
170191 }
171192
193
+ mutex_unlock(&bebob->mutex);
194
+
172195 snd_pcm_set_sync(substream);
173
-end:
174
- return err;
196
+
197
+ return 0;
175198 err_locked:
176199 snd_bebob_stream_lock_release(bebob);
177200 return err;
....@@ -185,86 +208,51 @@
185208 return 0;
186209 }
187210
188
-static int
189
-pcm_capture_hw_params(struct snd_pcm_substream *substream,
190
- struct snd_pcm_hw_params *hw_params)
211
+static int pcm_hw_params(struct snd_pcm_substream *substream,
212
+ struct snd_pcm_hw_params *hw_params)
191213 {
192214 struct snd_bebob *bebob = substream->private_data;
193
- int err;
194
-
195
- err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
196
- params_buffer_bytes(hw_params));
197
- if (err < 0)
198
- return err;
215
+ int err = 0;
199216
200217 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
218
+ unsigned int rate = params_rate(hw_params);
219
+ unsigned int frames_per_period = params_period_size(hw_params);
220
+ unsigned int frames_per_buffer = params_buffer_size(hw_params);
221
+
201222 mutex_lock(&bebob->mutex);
202
- bebob->substreams_counter++;
223
+ err = snd_bebob_stream_reserve_duplex(bebob, rate,
224
+ frames_per_period, frames_per_buffer);
225
+ if (err >= 0)
226
+ ++bebob->substreams_counter;
203227 mutex_unlock(&bebob->mutex);
204228 }
205229
206
- return 0;
207
-}
208
-static int
209
-pcm_playback_hw_params(struct snd_pcm_substream *substream,
210
- struct snd_pcm_hw_params *hw_params)
211
-{
212
- struct snd_bebob *bebob = substream->private_data;
213
- int err;
214
-
215
- err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
216
- params_buffer_bytes(hw_params));
217
- if (err < 0)
218
- return err;
219
-
220
- if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
221
- mutex_lock(&bebob->mutex);
222
- bebob->substreams_counter++;
223
- mutex_unlock(&bebob->mutex);
224
- }
225
-
226
- return 0;
230
+ return err;
227231 }
228232
229
-static int
230
-pcm_capture_hw_free(struct snd_pcm_substream *substream)
233
+static int pcm_hw_free(struct snd_pcm_substream *substream)
231234 {
232235 struct snd_bebob *bebob = substream->private_data;
233236
234
- if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) {
235
- mutex_lock(&bebob->mutex);
237
+ mutex_lock(&bebob->mutex);
238
+
239
+ if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
236240 bebob->substreams_counter--;
237
- mutex_unlock(&bebob->mutex);
238
- }
239241
240242 snd_bebob_stream_stop_duplex(bebob);
241243
242
- return snd_pcm_lib_free_vmalloc_buffer(substream);
243
-}
244
-static int
245
-pcm_playback_hw_free(struct snd_pcm_substream *substream)
246
-{
247
- struct snd_bebob *bebob = substream->private_data;
244
+ mutex_unlock(&bebob->mutex);
248245
249
- if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) {
250
- mutex_lock(&bebob->mutex);
251
- bebob->substreams_counter--;
252
- mutex_unlock(&bebob->mutex);
253
- }
254
-
255
- snd_bebob_stream_stop_duplex(bebob);
256
-
257
- return snd_pcm_lib_free_vmalloc_buffer(substream);
246
+ return 0;
258247 }
259248
260249 static int
261250 pcm_capture_prepare(struct snd_pcm_substream *substream)
262251 {
263252 struct snd_bebob *bebob = substream->private_data;
264
- struct snd_pcm_runtime *runtime = substream->runtime;
265253 int err;
266254
267
- err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
255
+ err = snd_bebob_stream_start_duplex(bebob);
268256 if (err >= 0)
269257 amdtp_stream_pcm_prepare(&bebob->tx_stream);
270258
....@@ -274,10 +262,9 @@
274262 pcm_playback_prepare(struct snd_pcm_substream *substream)
275263 {
276264 struct snd_bebob *bebob = substream->private_data;
277
- struct snd_pcm_runtime *runtime = substream->runtime;
278265 int err;
279266
280
- err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
267
+ err = snd_bebob_stream_start_duplex(bebob);
281268 if (err >= 0)
282269 amdtp_stream_pcm_prepare(&bebob->rx_stream);
283270
....@@ -321,31 +308,33 @@
321308 return 0;
322309 }
323310
324
-static snd_pcm_uframes_t
325
-pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
311
+static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
326312 {
327313 struct snd_bebob *bebob = sbstrm->private_data;
328
- return amdtp_stream_pcm_pointer(&bebob->tx_stream);
314
+
315
+ return amdtp_domain_stream_pcm_pointer(&bebob->domain,
316
+ &bebob->tx_stream);
329317 }
330
-static snd_pcm_uframes_t
331
-pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
318
+static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
332319 {
333320 struct snd_bebob *bebob = sbstrm->private_data;
334
- return amdtp_stream_pcm_pointer(&bebob->rx_stream);
321
+
322
+ return amdtp_domain_stream_pcm_pointer(&bebob->domain,
323
+ &bebob->rx_stream);
335324 }
336325
337326 static int pcm_capture_ack(struct snd_pcm_substream *substream)
338327 {
339328 struct snd_bebob *bebob = substream->private_data;
340329
341
- return amdtp_stream_pcm_ack(&bebob->tx_stream);
330
+ return amdtp_domain_stream_pcm_ack(&bebob->domain, &bebob->tx_stream);
342331 }
343332
344333 static int pcm_playback_ack(struct snd_pcm_substream *substream)
345334 {
346335 struct snd_bebob *bebob = substream->private_data;
347336
348
- return amdtp_stream_pcm_ack(&bebob->rx_stream);
337
+ return amdtp_domain_stream_pcm_ack(&bebob->domain, &bebob->rx_stream);
349338 }
350339
351340 int snd_bebob_create_pcm_devices(struct snd_bebob *bebob)
....@@ -353,26 +342,22 @@
353342 static const struct snd_pcm_ops capture_ops = {
354343 .open = pcm_open,
355344 .close = pcm_close,
356
- .ioctl = snd_pcm_lib_ioctl,
357
- .hw_params = pcm_capture_hw_params,
358
- .hw_free = pcm_capture_hw_free,
345
+ .hw_params = pcm_hw_params,
346
+ .hw_free = pcm_hw_free,
359347 .prepare = pcm_capture_prepare,
360348 .trigger = pcm_capture_trigger,
361349 .pointer = pcm_capture_pointer,
362350 .ack = pcm_capture_ack,
363
- .page = snd_pcm_lib_get_vmalloc_page,
364351 };
365352 static const struct snd_pcm_ops playback_ops = {
366353 .open = pcm_open,
367354 .close = pcm_close,
368
- .ioctl = snd_pcm_lib_ioctl,
369
- .hw_params = pcm_playback_hw_params,
370
- .hw_free = pcm_playback_hw_free,
355
+ .hw_params = pcm_hw_params,
356
+ .hw_free = pcm_hw_free,
371357 .prepare = pcm_playback_prepare,
372358 .trigger = pcm_playback_trigger,
373359 .pointer = pcm_playback_pointer,
374360 .ack = pcm_playback_ack,
375
- .page = snd_pcm_lib_get_vmalloc_page,
376361 };
377362 struct snd_pcm *pcm;
378363 int err;
....@@ -386,6 +371,7 @@
386371 "%s PCM", bebob->card->shortname);
387372 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
388373 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
374
+ snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
389375 end:
390376 return err;
391377 }