.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * oxfw_pcm.c - a part of driver for OXFW970/971 based devices |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (c) Clemens Ladisch <clemens@ladisch.de> |
---|
5 | | - * Licensed under the terms of the GNU General Public License, version 2. |
---|
6 | 6 | */ |
---|
7 | 7 | |
---|
8 | 8 | #include "oxfw.h" |
---|
.. | .. |
---|
170 | 170 | static int pcm_open(struct snd_pcm_substream *substream) |
---|
171 | 171 | { |
---|
172 | 172 | struct snd_oxfw *oxfw = substream->private_data; |
---|
| 173 | + struct amdtp_domain *d = &oxfw->domain; |
---|
173 | 174 | int err; |
---|
174 | 175 | |
---|
175 | 176 | err = snd_oxfw_stream_lock_try(oxfw); |
---|
176 | 177 | if (err < 0) |
---|
177 | | - goto end; |
---|
| 178 | + return err; |
---|
178 | 179 | |
---|
179 | 180 | err = init_hw_params(oxfw, substream); |
---|
180 | 181 | if (err < 0) |
---|
181 | 182 | goto err_locked; |
---|
182 | 183 | |
---|
183 | | - /* |
---|
184 | | - * When any PCM streams are already running, the available sampling |
---|
185 | | - * rate is limited at current value. |
---|
186 | | - */ |
---|
187 | | - if (amdtp_stream_pcm_running(&oxfw->tx_stream) || |
---|
188 | | - amdtp_stream_pcm_running(&oxfw->rx_stream)) { |
---|
| 184 | + mutex_lock(&oxfw->mutex); |
---|
| 185 | + |
---|
| 186 | + // When source of clock is not internal or any stream is reserved for |
---|
| 187 | + // transmission of PCM frames, the available sampling rate is limited |
---|
| 188 | + // at current one. |
---|
| 189 | + if (oxfw->substreams_count > 0 && d->events_per_period > 0) { |
---|
| 190 | + unsigned int frames_per_period = d->events_per_period; |
---|
| 191 | + unsigned int frames_per_buffer = d->events_per_buffer; |
---|
| 192 | + |
---|
189 | 193 | err = limit_to_current_params(substream); |
---|
190 | | - if (err < 0) |
---|
191 | | - goto end; |
---|
| 194 | + if (err < 0) { |
---|
| 195 | + mutex_unlock(&oxfw->mutex); |
---|
| 196 | + goto err_locked; |
---|
| 197 | + } |
---|
| 198 | + |
---|
| 199 | + if (frames_per_period > 0) { |
---|
| 200 | + err = snd_pcm_hw_constraint_minmax(substream->runtime, |
---|
| 201 | + SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
---|
| 202 | + frames_per_period, frames_per_period); |
---|
| 203 | + if (err < 0) { |
---|
| 204 | + mutex_unlock(&oxfw->mutex); |
---|
| 205 | + goto err_locked; |
---|
| 206 | + } |
---|
| 207 | + |
---|
| 208 | + err = snd_pcm_hw_constraint_minmax(substream->runtime, |
---|
| 209 | + SNDRV_PCM_HW_PARAM_BUFFER_SIZE, |
---|
| 210 | + frames_per_buffer, frames_per_buffer); |
---|
| 211 | + if (err < 0) { |
---|
| 212 | + mutex_unlock(&oxfw->mutex); |
---|
| 213 | + goto err_locked; |
---|
| 214 | + } |
---|
| 215 | + } |
---|
192 | 216 | } |
---|
193 | 217 | |
---|
| 218 | + mutex_unlock(&oxfw->mutex); |
---|
| 219 | + |
---|
194 | 220 | snd_pcm_set_sync(substream); |
---|
195 | | -end: |
---|
196 | | - return err; |
---|
| 221 | + |
---|
| 222 | + return 0; |
---|
197 | 223 | err_locked: |
---|
198 | 224 | snd_oxfw_stream_lock_release(oxfw); |
---|
199 | 225 | return err; |
---|
.. | .. |
---|
211 | 237 | struct snd_pcm_hw_params *hw_params) |
---|
212 | 238 | { |
---|
213 | 239 | struct snd_oxfw *oxfw = substream->private_data; |
---|
214 | | - int err; |
---|
215 | | - |
---|
216 | | - err = snd_pcm_lib_alloc_vmalloc_buffer(substream, |
---|
217 | | - params_buffer_bytes(hw_params)); |
---|
218 | | - if (err < 0) |
---|
219 | | - return err; |
---|
| 240 | + int err = 0; |
---|
220 | 241 | |
---|
221 | 242 | if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) { |
---|
| 243 | + unsigned int rate = params_rate(hw_params); |
---|
| 244 | + unsigned int channels = params_channels(hw_params); |
---|
| 245 | + unsigned int frames_per_period = params_period_size(hw_params); |
---|
| 246 | + unsigned int frames_per_buffer = params_buffer_size(hw_params); |
---|
| 247 | + |
---|
222 | 248 | mutex_lock(&oxfw->mutex); |
---|
223 | | - oxfw->capture_substreams++; |
---|
| 249 | + err = snd_oxfw_stream_reserve_duplex(oxfw, &oxfw->tx_stream, |
---|
| 250 | + rate, channels, frames_per_period, |
---|
| 251 | + frames_per_buffer); |
---|
| 252 | + if (err >= 0) |
---|
| 253 | + ++oxfw->substreams_count; |
---|
224 | 254 | mutex_unlock(&oxfw->mutex); |
---|
225 | 255 | } |
---|
226 | 256 | |
---|
227 | | - return 0; |
---|
| 257 | + return err; |
---|
228 | 258 | } |
---|
229 | 259 | static int pcm_playback_hw_params(struct snd_pcm_substream *substream, |
---|
230 | 260 | struct snd_pcm_hw_params *hw_params) |
---|
231 | 261 | { |
---|
232 | 262 | struct snd_oxfw *oxfw = substream->private_data; |
---|
233 | | - int err; |
---|
234 | | - |
---|
235 | | - err = snd_pcm_lib_alloc_vmalloc_buffer(substream, |
---|
236 | | - params_buffer_bytes(hw_params)); |
---|
237 | | - if (err < 0) |
---|
238 | | - return err; |
---|
| 263 | + int err = 0; |
---|
239 | 264 | |
---|
240 | 265 | if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) { |
---|
| 266 | + unsigned int rate = params_rate(hw_params); |
---|
| 267 | + unsigned int channels = params_channels(hw_params); |
---|
| 268 | + unsigned int frames_per_period = params_period_size(hw_params); |
---|
| 269 | + unsigned int frames_per_buffer = params_buffer_size(hw_params); |
---|
| 270 | + |
---|
241 | 271 | mutex_lock(&oxfw->mutex); |
---|
242 | | - oxfw->playback_substreams++; |
---|
| 272 | + err = snd_oxfw_stream_reserve_duplex(oxfw, &oxfw->rx_stream, |
---|
| 273 | + rate, channels, frames_per_period, |
---|
| 274 | + frames_per_buffer); |
---|
| 275 | + if (err >= 0) |
---|
| 276 | + ++oxfw->substreams_count; |
---|
243 | 277 | mutex_unlock(&oxfw->mutex); |
---|
244 | 278 | } |
---|
245 | 279 | |
---|
246 | | - return 0; |
---|
| 280 | + return err; |
---|
247 | 281 | } |
---|
248 | 282 | |
---|
249 | 283 | static int pcm_capture_hw_free(struct snd_pcm_substream *substream) |
---|
.. | .. |
---|
253 | 287 | mutex_lock(&oxfw->mutex); |
---|
254 | 288 | |
---|
255 | 289 | if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) |
---|
256 | | - oxfw->capture_substreams--; |
---|
| 290 | + --oxfw->substreams_count; |
---|
257 | 291 | |
---|
258 | | - snd_oxfw_stream_stop_simplex(oxfw, &oxfw->tx_stream); |
---|
| 292 | + snd_oxfw_stream_stop_duplex(oxfw); |
---|
259 | 293 | |
---|
260 | 294 | mutex_unlock(&oxfw->mutex); |
---|
261 | 295 | |
---|
262 | | - return snd_pcm_lib_free_vmalloc_buffer(substream); |
---|
| 296 | + return 0; |
---|
263 | 297 | } |
---|
264 | 298 | static int pcm_playback_hw_free(struct snd_pcm_substream *substream) |
---|
265 | 299 | { |
---|
.. | .. |
---|
268 | 302 | mutex_lock(&oxfw->mutex); |
---|
269 | 303 | |
---|
270 | 304 | if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) |
---|
271 | | - oxfw->playback_substreams--; |
---|
| 305 | + --oxfw->substreams_count; |
---|
272 | 306 | |
---|
273 | | - snd_oxfw_stream_stop_simplex(oxfw, &oxfw->rx_stream); |
---|
| 307 | + snd_oxfw_stream_stop_duplex(oxfw); |
---|
274 | 308 | |
---|
275 | 309 | mutex_unlock(&oxfw->mutex); |
---|
276 | 310 | |
---|
277 | | - return snd_pcm_lib_free_vmalloc_buffer(substream); |
---|
| 311 | + return 0; |
---|
278 | 312 | } |
---|
279 | 313 | |
---|
280 | 314 | static int pcm_capture_prepare(struct snd_pcm_substream *substream) |
---|
281 | 315 | { |
---|
282 | 316 | struct snd_oxfw *oxfw = substream->private_data; |
---|
283 | | - struct snd_pcm_runtime *runtime = substream->runtime; |
---|
284 | 317 | int err; |
---|
285 | 318 | |
---|
286 | 319 | mutex_lock(&oxfw->mutex); |
---|
287 | | - err = snd_oxfw_stream_start_simplex(oxfw, &oxfw->tx_stream, |
---|
288 | | - runtime->rate, runtime->channels); |
---|
| 320 | + err = snd_oxfw_stream_start_duplex(oxfw); |
---|
289 | 321 | mutex_unlock(&oxfw->mutex); |
---|
290 | 322 | if (err < 0) |
---|
291 | 323 | goto end; |
---|
.. | .. |
---|
297 | 329 | static int pcm_playback_prepare(struct snd_pcm_substream *substream) |
---|
298 | 330 | { |
---|
299 | 331 | struct snd_oxfw *oxfw = substream->private_data; |
---|
300 | | - struct snd_pcm_runtime *runtime = substream->runtime; |
---|
301 | 332 | int err; |
---|
302 | 333 | |
---|
303 | 334 | mutex_lock(&oxfw->mutex); |
---|
304 | | - err = snd_oxfw_stream_start_simplex(oxfw, &oxfw->rx_stream, |
---|
305 | | - runtime->rate, runtime->channels); |
---|
| 335 | + err = snd_oxfw_stream_start_duplex(oxfw); |
---|
306 | 336 | mutex_unlock(&oxfw->mutex); |
---|
307 | 337 | if (err < 0) |
---|
308 | 338 | goto end; |
---|
.. | .. |
---|
353 | 383 | { |
---|
354 | 384 | struct snd_oxfw *oxfw = sbstm->private_data; |
---|
355 | 385 | |
---|
356 | | - return amdtp_stream_pcm_pointer(&oxfw->tx_stream); |
---|
| 386 | + return amdtp_domain_stream_pcm_pointer(&oxfw->domain, &oxfw->tx_stream); |
---|
357 | 387 | } |
---|
358 | 388 | static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstm) |
---|
359 | 389 | { |
---|
360 | 390 | struct snd_oxfw *oxfw = sbstm->private_data; |
---|
361 | 391 | |
---|
362 | | - return amdtp_stream_pcm_pointer(&oxfw->rx_stream); |
---|
| 392 | + return amdtp_domain_stream_pcm_pointer(&oxfw->domain, &oxfw->rx_stream); |
---|
363 | 393 | } |
---|
364 | 394 | |
---|
365 | 395 | static int pcm_capture_ack(struct snd_pcm_substream *substream) |
---|
366 | 396 | { |
---|
367 | 397 | struct snd_oxfw *oxfw = substream->private_data; |
---|
368 | 398 | |
---|
369 | | - return amdtp_stream_pcm_ack(&oxfw->tx_stream); |
---|
| 399 | + return amdtp_domain_stream_pcm_ack(&oxfw->domain, &oxfw->tx_stream); |
---|
370 | 400 | } |
---|
371 | 401 | |
---|
372 | 402 | static int pcm_playback_ack(struct snd_pcm_substream *substream) |
---|
373 | 403 | { |
---|
374 | 404 | struct snd_oxfw *oxfw = substream->private_data; |
---|
375 | 405 | |
---|
376 | | - return amdtp_stream_pcm_ack(&oxfw->rx_stream); |
---|
| 406 | + return amdtp_domain_stream_pcm_ack(&oxfw->domain, &oxfw->rx_stream); |
---|
377 | 407 | } |
---|
378 | 408 | |
---|
379 | 409 | int snd_oxfw_create_pcm(struct snd_oxfw *oxfw) |
---|
.. | .. |
---|
381 | 411 | static const struct snd_pcm_ops capture_ops = { |
---|
382 | 412 | .open = pcm_open, |
---|
383 | 413 | .close = pcm_close, |
---|
384 | | - .ioctl = snd_pcm_lib_ioctl, |
---|
385 | 414 | .hw_params = pcm_capture_hw_params, |
---|
386 | 415 | .hw_free = pcm_capture_hw_free, |
---|
387 | 416 | .prepare = pcm_capture_prepare, |
---|
388 | 417 | .trigger = pcm_capture_trigger, |
---|
389 | 418 | .pointer = pcm_capture_pointer, |
---|
390 | 419 | .ack = pcm_capture_ack, |
---|
391 | | - .page = snd_pcm_lib_get_vmalloc_page, |
---|
392 | 420 | }; |
---|
393 | 421 | static const struct snd_pcm_ops playback_ops = { |
---|
394 | 422 | .open = pcm_open, |
---|
395 | 423 | .close = pcm_close, |
---|
396 | | - .ioctl = snd_pcm_lib_ioctl, |
---|
397 | 424 | .hw_params = pcm_playback_hw_params, |
---|
398 | 425 | .hw_free = pcm_playback_hw_free, |
---|
399 | 426 | .prepare = pcm_playback_prepare, |
---|
400 | 427 | .trigger = pcm_playback_trigger, |
---|
401 | 428 | .pointer = pcm_playback_pointer, |
---|
402 | 429 | .ack = pcm_playback_ack, |
---|
403 | | - .page = snd_pcm_lib_get_vmalloc_page, |
---|
404 | 430 | }; |
---|
405 | 431 | struct snd_pcm *pcm; |
---|
406 | 432 | unsigned int cap = 0; |
---|
.. | .. |
---|
418 | 444 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops); |
---|
419 | 445 | if (cap > 0) |
---|
420 | 446 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops); |
---|
| 447 | + snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0); |
---|
421 | 448 | |
---|
422 | 449 | return 0; |
---|
423 | 450 | } |
---|