.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * fireworks_pcm.c - a part of driver for Fireworks based devices |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (c) 2009-2010 Clemens Ladisch |
---|
5 | 6 | * Copyright (c) 2013-2014 Takashi Sakamoto |
---|
6 | | - * |
---|
7 | | - * Licensed under the terms of the GNU General Public License, version 2. |
---|
8 | 7 | */ |
---|
9 | 8 | #include "./fireworks.h" |
---|
10 | 9 | |
---|
.. | .. |
---|
149 | 148 | } |
---|
150 | 149 | |
---|
151 | 150 | /* limit rates */ |
---|
152 | | - runtime->hw.rates = efw->supported_sampling_rate, |
---|
| 151 | + runtime->hw.rates = efw->supported_sampling_rate; |
---|
153 | 152 | snd_pcm_limit_hw_rates(runtime); |
---|
154 | 153 | |
---|
155 | 154 | limit_channels(&runtime->hw, pcm_channels); |
---|
.. | .. |
---|
174 | 173 | static int pcm_open(struct snd_pcm_substream *substream) |
---|
175 | 174 | { |
---|
176 | 175 | struct snd_efw *efw = substream->private_data; |
---|
177 | | - unsigned int sampling_rate; |
---|
| 176 | + struct amdtp_domain *d = &efw->domain; |
---|
178 | 177 | enum snd_efw_clock_source clock_source; |
---|
179 | 178 | int err; |
---|
180 | 179 | |
---|
181 | 180 | err = snd_efw_stream_lock_try(efw); |
---|
182 | 181 | if (err < 0) |
---|
183 | | - goto end; |
---|
| 182 | + return err; |
---|
184 | 183 | |
---|
185 | 184 | err = pcm_init_hw_params(efw, substream); |
---|
186 | 185 | if (err < 0) |
---|
.. | .. |
---|
190 | 189 | if (err < 0) |
---|
191 | 190 | goto err_locked; |
---|
192 | 191 | |
---|
193 | | - /* |
---|
194 | | - * When source of clock is not internal or any PCM streams are running, |
---|
195 | | - * available sampling rate is limited at current sampling rate. |
---|
196 | | - */ |
---|
| 192 | + mutex_lock(&efw->mutex); |
---|
| 193 | + |
---|
| 194 | + // When source of clock is not internal or any stream is reserved for |
---|
| 195 | + // transmission of PCM frames, the available sampling rate is limited |
---|
| 196 | + // at current one. |
---|
197 | 197 | if ((clock_source != SND_EFW_CLOCK_SOURCE_INTERNAL) || |
---|
198 | | - amdtp_stream_pcm_running(&efw->tx_stream) || |
---|
199 | | - amdtp_stream_pcm_running(&efw->rx_stream)) { |
---|
| 198 | + (efw->substreams_counter > 0 && d->events_per_period > 0)) { |
---|
| 199 | + unsigned int frames_per_period = d->events_per_period; |
---|
| 200 | + unsigned int frames_per_buffer = d->events_per_buffer; |
---|
| 201 | + unsigned int sampling_rate; |
---|
| 202 | + |
---|
200 | 203 | err = snd_efw_command_get_sampling_rate(efw, &sampling_rate); |
---|
201 | | - if (err < 0) |
---|
| 204 | + if (err < 0) { |
---|
| 205 | + mutex_unlock(&efw->mutex); |
---|
202 | 206 | goto err_locked; |
---|
| 207 | + } |
---|
203 | 208 | substream->runtime->hw.rate_min = sampling_rate; |
---|
204 | 209 | substream->runtime->hw.rate_max = sampling_rate; |
---|
| 210 | + |
---|
| 211 | + if (frames_per_period > 0) { |
---|
| 212 | + err = snd_pcm_hw_constraint_minmax(substream->runtime, |
---|
| 213 | + SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
---|
| 214 | + frames_per_period, frames_per_period); |
---|
| 215 | + if (err < 0) { |
---|
| 216 | + mutex_unlock(&efw->mutex); |
---|
| 217 | + goto err_locked; |
---|
| 218 | + } |
---|
| 219 | + |
---|
| 220 | + err = snd_pcm_hw_constraint_minmax(substream->runtime, |
---|
| 221 | + SNDRV_PCM_HW_PARAM_BUFFER_SIZE, |
---|
| 222 | + frames_per_buffer, frames_per_buffer); |
---|
| 223 | + if (err < 0) { |
---|
| 224 | + mutex_unlock(&efw->mutex); |
---|
| 225 | + goto err_locked; |
---|
| 226 | + } |
---|
| 227 | + } |
---|
205 | 228 | } |
---|
206 | 229 | |
---|
| 230 | + mutex_unlock(&efw->mutex); |
---|
| 231 | + |
---|
207 | 232 | snd_pcm_set_sync(substream); |
---|
208 | | -end: |
---|
209 | | - return err; |
---|
| 233 | + |
---|
| 234 | + return 0; |
---|
210 | 235 | err_locked: |
---|
211 | 236 | snd_efw_stream_lock_release(efw); |
---|
212 | 237 | return err; |
---|
.. | .. |
---|
219 | 244 | return 0; |
---|
220 | 245 | } |
---|
221 | 246 | |
---|
222 | | -static int pcm_capture_hw_params(struct snd_pcm_substream *substream, |
---|
| 247 | +static int pcm_hw_params(struct snd_pcm_substream *substream, |
---|
223 | 248 | struct snd_pcm_hw_params *hw_params) |
---|
224 | 249 | { |
---|
225 | 250 | struct snd_efw *efw = substream->private_data; |
---|
226 | | - int err; |
---|
227 | | - |
---|
228 | | - err = snd_pcm_lib_alloc_vmalloc_buffer(substream, |
---|
229 | | - params_buffer_bytes(hw_params)); |
---|
230 | | - if (err < 0) |
---|
231 | | - return err; |
---|
| 251 | + int err = 0; |
---|
232 | 252 | |
---|
233 | 253 | if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) { |
---|
| 254 | + unsigned int rate = params_rate(hw_params); |
---|
| 255 | + unsigned int frames_per_period = params_period_size(hw_params); |
---|
| 256 | + unsigned int frames_per_buffer = params_buffer_size(hw_params); |
---|
| 257 | + |
---|
234 | 258 | mutex_lock(&efw->mutex); |
---|
235 | | - efw->capture_substreams++; |
---|
| 259 | + err = snd_efw_stream_reserve_duplex(efw, rate, |
---|
| 260 | + frames_per_period, frames_per_buffer); |
---|
| 261 | + if (err >= 0) |
---|
| 262 | + ++efw->substreams_counter; |
---|
236 | 263 | mutex_unlock(&efw->mutex); |
---|
237 | 264 | } |
---|
238 | 265 | |
---|
239 | | - return 0; |
---|
240 | | -} |
---|
241 | | -static int pcm_playback_hw_params(struct snd_pcm_substream *substream, |
---|
242 | | - struct snd_pcm_hw_params *hw_params) |
---|
243 | | -{ |
---|
244 | | - struct snd_efw *efw = substream->private_data; |
---|
245 | | - int err; |
---|
246 | | - |
---|
247 | | - err = snd_pcm_lib_alloc_vmalloc_buffer(substream, |
---|
248 | | - params_buffer_bytes(hw_params)); |
---|
249 | | - if (err < 0) |
---|
250 | | - return err; |
---|
251 | | - |
---|
252 | | - if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) { |
---|
253 | | - mutex_lock(&efw->mutex); |
---|
254 | | - efw->playback_substreams++; |
---|
255 | | - mutex_unlock(&efw->mutex); |
---|
256 | | - } |
---|
257 | | - |
---|
258 | | - return 0; |
---|
| 266 | + return err; |
---|
259 | 267 | } |
---|
260 | 268 | |
---|
261 | | -static int pcm_capture_hw_free(struct snd_pcm_substream *substream) |
---|
| 269 | +static int pcm_hw_free(struct snd_pcm_substream *substream) |
---|
262 | 270 | { |
---|
263 | 271 | struct snd_efw *efw = substream->private_data; |
---|
264 | 272 | |
---|
265 | | - if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) { |
---|
266 | | - mutex_lock(&efw->mutex); |
---|
267 | | - efw->capture_substreams--; |
---|
268 | | - mutex_unlock(&efw->mutex); |
---|
269 | | - } |
---|
| 273 | + mutex_lock(&efw->mutex); |
---|
| 274 | + |
---|
| 275 | + if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) |
---|
| 276 | + --efw->substreams_counter; |
---|
270 | 277 | |
---|
271 | 278 | snd_efw_stream_stop_duplex(efw); |
---|
272 | 279 | |
---|
273 | | - return snd_pcm_lib_free_vmalloc_buffer(substream); |
---|
274 | | -} |
---|
275 | | -static int pcm_playback_hw_free(struct snd_pcm_substream *substream) |
---|
276 | | -{ |
---|
277 | | - struct snd_efw *efw = substream->private_data; |
---|
| 280 | + mutex_unlock(&efw->mutex); |
---|
278 | 281 | |
---|
279 | | - if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) { |
---|
280 | | - mutex_lock(&efw->mutex); |
---|
281 | | - efw->playback_substreams--; |
---|
282 | | - mutex_unlock(&efw->mutex); |
---|
283 | | - } |
---|
284 | | - |
---|
285 | | - snd_efw_stream_stop_duplex(efw); |
---|
286 | | - |
---|
287 | | - return snd_pcm_lib_free_vmalloc_buffer(substream); |
---|
| 282 | + return 0; |
---|
288 | 283 | } |
---|
289 | 284 | |
---|
290 | 285 | static int pcm_capture_prepare(struct snd_pcm_substream *substream) |
---|
291 | 286 | { |
---|
292 | 287 | struct snd_efw *efw = substream->private_data; |
---|
293 | | - struct snd_pcm_runtime *runtime = substream->runtime; |
---|
294 | 288 | int err; |
---|
295 | 289 | |
---|
296 | | - err = snd_efw_stream_start_duplex(efw, runtime->rate); |
---|
| 290 | + err = snd_efw_stream_start_duplex(efw); |
---|
297 | 291 | if (err >= 0) |
---|
298 | 292 | amdtp_stream_pcm_prepare(&efw->tx_stream); |
---|
299 | 293 | |
---|
.. | .. |
---|
302 | 296 | static int pcm_playback_prepare(struct snd_pcm_substream *substream) |
---|
303 | 297 | { |
---|
304 | 298 | struct snd_efw *efw = substream->private_data; |
---|
305 | | - struct snd_pcm_runtime *runtime = substream->runtime; |
---|
306 | 299 | int err; |
---|
307 | 300 | |
---|
308 | | - err = snd_efw_stream_start_duplex(efw, runtime->rate); |
---|
| 301 | + err = snd_efw_stream_start_duplex(efw); |
---|
309 | 302 | if (err >= 0) |
---|
310 | 303 | amdtp_stream_pcm_prepare(&efw->rx_stream); |
---|
311 | 304 | |
---|
.. | .. |
---|
350 | 343 | static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm) |
---|
351 | 344 | { |
---|
352 | 345 | struct snd_efw *efw = sbstrm->private_data; |
---|
353 | | - return amdtp_stream_pcm_pointer(&efw->tx_stream); |
---|
| 346 | + |
---|
| 347 | + return amdtp_domain_stream_pcm_pointer(&efw->domain, &efw->tx_stream); |
---|
354 | 348 | } |
---|
355 | 349 | static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm) |
---|
356 | 350 | { |
---|
357 | 351 | struct snd_efw *efw = sbstrm->private_data; |
---|
358 | | - return amdtp_stream_pcm_pointer(&efw->rx_stream); |
---|
| 352 | + |
---|
| 353 | + return amdtp_domain_stream_pcm_pointer(&efw->domain, &efw->rx_stream); |
---|
359 | 354 | } |
---|
360 | 355 | |
---|
361 | 356 | static int pcm_capture_ack(struct snd_pcm_substream *substream) |
---|
362 | 357 | { |
---|
363 | 358 | struct snd_efw *efw = substream->private_data; |
---|
364 | 359 | |
---|
365 | | - return amdtp_stream_pcm_ack(&efw->tx_stream); |
---|
| 360 | + return amdtp_domain_stream_pcm_ack(&efw->domain, &efw->tx_stream); |
---|
366 | 361 | } |
---|
367 | 362 | |
---|
368 | 363 | static int pcm_playback_ack(struct snd_pcm_substream *substream) |
---|
369 | 364 | { |
---|
370 | 365 | struct snd_efw *efw = substream->private_data; |
---|
371 | 366 | |
---|
372 | | - return amdtp_stream_pcm_ack(&efw->rx_stream); |
---|
| 367 | + return amdtp_domain_stream_pcm_ack(&efw->domain, &efw->rx_stream); |
---|
373 | 368 | } |
---|
374 | 369 | |
---|
375 | 370 | int snd_efw_create_pcm_devices(struct snd_efw *efw) |
---|
.. | .. |
---|
377 | 372 | static const struct snd_pcm_ops capture_ops = { |
---|
378 | 373 | .open = pcm_open, |
---|
379 | 374 | .close = pcm_close, |
---|
380 | | - .ioctl = snd_pcm_lib_ioctl, |
---|
381 | | - .hw_params = pcm_capture_hw_params, |
---|
382 | | - .hw_free = pcm_capture_hw_free, |
---|
| 375 | + .hw_params = pcm_hw_params, |
---|
| 376 | + .hw_free = pcm_hw_free, |
---|
383 | 377 | .prepare = pcm_capture_prepare, |
---|
384 | 378 | .trigger = pcm_capture_trigger, |
---|
385 | 379 | .pointer = pcm_capture_pointer, |
---|
386 | 380 | .ack = pcm_capture_ack, |
---|
387 | | - .page = snd_pcm_lib_get_vmalloc_page, |
---|
388 | 381 | }; |
---|
389 | 382 | static const struct snd_pcm_ops playback_ops = { |
---|
390 | 383 | .open = pcm_open, |
---|
391 | 384 | .close = pcm_close, |
---|
392 | | - .ioctl = snd_pcm_lib_ioctl, |
---|
393 | | - .hw_params = pcm_playback_hw_params, |
---|
394 | | - .hw_free = pcm_playback_hw_free, |
---|
| 385 | + .hw_params = pcm_hw_params, |
---|
| 386 | + .hw_free = pcm_hw_free, |
---|
395 | 387 | .prepare = pcm_playback_prepare, |
---|
396 | 388 | .trigger = pcm_playback_trigger, |
---|
397 | 389 | .pointer = pcm_playback_pointer, |
---|
398 | 390 | .ack = pcm_playback_ack, |
---|
399 | | - .page = snd_pcm_lib_get_vmalloc_page, |
---|
400 | 391 | }; |
---|
401 | 392 | struct snd_pcm *pcm; |
---|
402 | 393 | int err; |
---|
.. | .. |
---|
409 | 400 | snprintf(pcm->name, sizeof(pcm->name), "%s PCM", efw->card->shortname); |
---|
410 | 401 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops); |
---|
411 | 402 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops); |
---|
| 403 | + snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0); |
---|
412 | 404 | end: |
---|
413 | 405 | return err; |
---|
414 | 406 | } |
---|