| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * ff-pcm.c - a part of driver for RME Fireface series |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (c) 2015-2017 Takashi Sakamoto |
|---|
| 5 | | - * |
|---|
| 6 | | - * Licensed under the terms of the GNU General Public License, version 2. |
|---|
| 7 | 6 | */ |
|---|
| 8 | 7 | |
|---|
| 9 | 8 | #include "ff.h" |
|---|
| 10 | | - |
|---|
| 11 | | -static inline unsigned int get_multiplier_mode_with_index(unsigned int index) |
|---|
| 12 | | -{ |
|---|
| 13 | | - return ((int)index - 1) / 2; |
|---|
| 14 | | -} |
|---|
| 15 | 9 | |
|---|
| 16 | 10 | static int hw_rule_rate(struct snd_pcm_hw_params *params, |
|---|
| 17 | 11 | struct snd_pcm_hw_rule *rule) |
|---|
| .. | .. |
|---|
| 24 | 18 | struct snd_interval t = { |
|---|
| 25 | 19 | .min = UINT_MAX, .max = 0, .integer = 1 |
|---|
| 26 | 20 | }; |
|---|
| 27 | | - unsigned int i, mode; |
|---|
| 21 | + unsigned int i; |
|---|
| 28 | 22 | |
|---|
| 29 | 23 | for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); i++) { |
|---|
| 30 | | - mode = get_multiplier_mode_with_index(i); |
|---|
| 24 | + enum snd_ff_stream_mode mode; |
|---|
| 25 | + int err; |
|---|
| 26 | + |
|---|
| 27 | + err = snd_ff_stream_get_multiplier_mode(i, &mode); |
|---|
| 28 | + if (err < 0) |
|---|
| 29 | + continue; |
|---|
| 30 | + |
|---|
| 31 | 31 | if (!snd_interval_test(c, pcm_channels[mode])) |
|---|
| 32 | 32 | continue; |
|---|
| 33 | 33 | |
|---|
| .. | .. |
|---|
| 49 | 49 | struct snd_interval t = { |
|---|
| 50 | 50 | .min = UINT_MAX, .max = 0, .integer = 1 |
|---|
| 51 | 51 | }; |
|---|
| 52 | | - unsigned int i, mode; |
|---|
| 52 | + unsigned int i; |
|---|
| 53 | 53 | |
|---|
| 54 | 54 | for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); i++) { |
|---|
| 55 | | - mode = get_multiplier_mode_with_index(i); |
|---|
| 55 | + enum snd_ff_stream_mode mode; |
|---|
| 56 | + int err; |
|---|
| 57 | + |
|---|
| 58 | + err = snd_ff_stream_get_multiplier_mode(i, &mode); |
|---|
| 59 | + if (err < 0) |
|---|
| 60 | + continue; |
|---|
| 61 | + |
|---|
| 56 | 62 | if (!snd_interval_test(r, amdtp_rate_table[i])) |
|---|
| 57 | 63 | continue; |
|---|
| 58 | 64 | |
|---|
| .. | .. |
|---|
| 66 | 72 | static void limit_channels_and_rates(struct snd_pcm_hardware *hw, |
|---|
| 67 | 73 | const unsigned int *pcm_channels) |
|---|
| 68 | 74 | { |
|---|
| 69 | | - unsigned int mode; |
|---|
| 70 | 75 | unsigned int rate, channels; |
|---|
| 71 | 76 | int i; |
|---|
| 72 | 77 | |
|---|
| .. | .. |
|---|
| 76 | 81 | hw->rate_max = 0; |
|---|
| 77 | 82 | |
|---|
| 78 | 83 | for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); i++) { |
|---|
| 79 | | - mode = get_multiplier_mode_with_index(i); |
|---|
| 84 | + enum snd_ff_stream_mode mode; |
|---|
| 85 | + int err; |
|---|
| 86 | + |
|---|
| 87 | + err = snd_ff_stream_get_multiplier_mode(i, &mode); |
|---|
| 88 | + if (err < 0) |
|---|
| 89 | + continue; |
|---|
| 80 | 90 | |
|---|
| 81 | 91 | channels = pcm_channels[mode]; |
|---|
| 82 | 92 | if (pcm_channels[mode] == 0) |
|---|
| .. | .. |
|---|
| 129 | 139 | static int pcm_open(struct snd_pcm_substream *substream) |
|---|
| 130 | 140 | { |
|---|
| 131 | 141 | struct snd_ff *ff = substream->private_data; |
|---|
| 142 | + struct amdtp_domain *d = &ff->domain; |
|---|
| 132 | 143 | unsigned int rate; |
|---|
| 133 | 144 | enum snd_ff_clock_src src; |
|---|
| 134 | 145 | int i, err; |
|---|
| .. | .. |
|---|
| 145 | 156 | if (err < 0) |
|---|
| 146 | 157 | goto release_lock; |
|---|
| 147 | 158 | |
|---|
| 159 | + mutex_lock(&ff->mutex); |
|---|
| 160 | + |
|---|
| 161 | + // When source of clock is not internal or any stream is reserved for |
|---|
| 162 | + // transmission of PCM frames, the available sampling rate is limited |
|---|
| 163 | + // at current one. |
|---|
| 148 | 164 | if (src != SND_FF_CLOCK_SRC_INTERNAL) { |
|---|
| 149 | 165 | for (i = 0; i < CIP_SFC_COUNT; ++i) { |
|---|
| 150 | 166 | if (amdtp_rate_table[i] == rate) |
|---|
| 151 | 167 | break; |
|---|
| 152 | 168 | } |
|---|
| 153 | | - /* |
|---|
| 154 | | - * The unit is configured at sampling frequency which packet |
|---|
| 155 | | - * streaming engine can't support. |
|---|
| 156 | | - */ |
|---|
| 169 | + |
|---|
| 170 | + // The unit is configured at sampling frequency which packet |
|---|
| 171 | + // streaming engine can't support. |
|---|
| 157 | 172 | if (i >= CIP_SFC_COUNT) { |
|---|
| 173 | + mutex_unlock(&ff->mutex); |
|---|
| 158 | 174 | err = -EIO; |
|---|
| 159 | 175 | goto release_lock; |
|---|
| 160 | 176 | } |
|---|
| .. | .. |
|---|
| 162 | 178 | substream->runtime->hw.rate_min = rate; |
|---|
| 163 | 179 | substream->runtime->hw.rate_max = rate; |
|---|
| 164 | 180 | } else { |
|---|
| 165 | | - if (amdtp_stream_pcm_running(&ff->rx_stream) || |
|---|
| 166 | | - amdtp_stream_pcm_running(&ff->tx_stream)) { |
|---|
| 181 | + if (ff->substreams_counter > 0) { |
|---|
| 182 | + unsigned int frames_per_period = d->events_per_period; |
|---|
| 183 | + unsigned int frames_per_buffer = d->events_per_buffer; |
|---|
| 184 | + |
|---|
| 167 | 185 | rate = amdtp_rate_table[ff->rx_stream.sfc]; |
|---|
| 168 | 186 | substream->runtime->hw.rate_min = rate; |
|---|
| 169 | 187 | substream->runtime->hw.rate_max = rate; |
|---|
| 188 | + |
|---|
| 189 | + err = snd_pcm_hw_constraint_minmax(substream->runtime, |
|---|
| 190 | + SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
|---|
| 191 | + frames_per_period, frames_per_period); |
|---|
| 192 | + if (err < 0) { |
|---|
| 193 | + mutex_unlock(&ff->mutex); |
|---|
| 194 | + goto release_lock; |
|---|
| 195 | + } |
|---|
| 196 | + |
|---|
| 197 | + err = snd_pcm_hw_constraint_minmax(substream->runtime, |
|---|
| 198 | + SNDRV_PCM_HW_PARAM_BUFFER_SIZE, |
|---|
| 199 | + frames_per_buffer, frames_per_buffer); |
|---|
| 200 | + if (err < 0) { |
|---|
| 201 | + mutex_unlock(&ff->mutex); |
|---|
| 202 | + goto release_lock; |
|---|
| 203 | + } |
|---|
| 170 | 204 | } |
|---|
| 171 | 205 | } |
|---|
| 206 | + |
|---|
| 207 | + mutex_unlock(&ff->mutex); |
|---|
| 172 | 208 | |
|---|
| 173 | 209 | snd_pcm_set_sync(substream); |
|---|
| 174 | 210 | |
|---|
| .. | .. |
|---|
| 188 | 224 | return 0; |
|---|
| 189 | 225 | } |
|---|
| 190 | 226 | |
|---|
| 191 | | -static int pcm_capture_hw_params(struct snd_pcm_substream *substream, |
|---|
| 192 | | - struct snd_pcm_hw_params *hw_params) |
|---|
| 227 | +static int pcm_hw_params(struct snd_pcm_substream *substream, |
|---|
| 228 | + struct snd_pcm_hw_params *hw_params) |
|---|
| 193 | 229 | { |
|---|
| 194 | 230 | struct snd_ff *ff = substream->private_data; |
|---|
| 195 | | - int err; |
|---|
| 196 | | - |
|---|
| 197 | | - err = snd_pcm_lib_alloc_vmalloc_buffer(substream, |
|---|
| 198 | | - params_buffer_bytes(hw_params)); |
|---|
| 199 | | - if (err < 0) |
|---|
| 200 | | - return err; |
|---|
| 231 | + int err = 0; |
|---|
| 201 | 232 | |
|---|
| 202 | 233 | if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) { |
|---|
| 234 | + unsigned int rate = params_rate(hw_params); |
|---|
| 235 | + unsigned int frames_per_period = params_period_size(hw_params); |
|---|
| 236 | + unsigned int frames_per_buffer = params_buffer_size(hw_params); |
|---|
| 237 | + |
|---|
| 203 | 238 | mutex_lock(&ff->mutex); |
|---|
| 204 | | - ff->substreams_counter++; |
|---|
| 239 | + err = snd_ff_stream_reserve_duplex(ff, rate, frames_per_period, |
|---|
| 240 | + frames_per_buffer); |
|---|
| 241 | + if (err >= 0) |
|---|
| 242 | + ++ff->substreams_counter; |
|---|
| 205 | 243 | mutex_unlock(&ff->mutex); |
|---|
| 206 | 244 | } |
|---|
| 207 | 245 | |
|---|
| 208 | | - return 0; |
|---|
| 246 | + return err; |
|---|
| 209 | 247 | } |
|---|
| 210 | 248 | |
|---|
| 211 | | -static int pcm_playback_hw_params(struct snd_pcm_substream *substream, |
|---|
| 212 | | - struct snd_pcm_hw_params *hw_params) |
|---|
| 213 | | -{ |
|---|
| 214 | | - struct snd_ff *ff = substream->private_data; |
|---|
| 215 | | - int err; |
|---|
| 216 | | - |
|---|
| 217 | | - err = snd_pcm_lib_alloc_vmalloc_buffer(substream, |
|---|
| 218 | | - params_buffer_bytes(hw_params)); |
|---|
| 219 | | - if (err < 0) |
|---|
| 220 | | - return err; |
|---|
| 221 | | - |
|---|
| 222 | | - if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) { |
|---|
| 223 | | - mutex_lock(&ff->mutex); |
|---|
| 224 | | - ff->substreams_counter++; |
|---|
| 225 | | - mutex_unlock(&ff->mutex); |
|---|
| 226 | | - } |
|---|
| 227 | | - |
|---|
| 228 | | - return 0; |
|---|
| 229 | | -} |
|---|
| 230 | | - |
|---|
| 231 | | -static int pcm_capture_hw_free(struct snd_pcm_substream *substream) |
|---|
| 249 | +static int pcm_hw_free(struct snd_pcm_substream *substream) |
|---|
| 232 | 250 | { |
|---|
| 233 | 251 | struct snd_ff *ff = substream->private_data; |
|---|
| 234 | 252 | |
|---|
| 235 | 253 | mutex_lock(&ff->mutex); |
|---|
| 236 | 254 | |
|---|
| 237 | 255 | if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) |
|---|
| 238 | | - ff->substreams_counter--; |
|---|
| 256 | + --ff->substreams_counter; |
|---|
| 239 | 257 | |
|---|
| 240 | 258 | snd_ff_stream_stop_duplex(ff); |
|---|
| 241 | 259 | |
|---|
| 242 | 260 | mutex_unlock(&ff->mutex); |
|---|
| 243 | 261 | |
|---|
| 244 | | - return snd_pcm_lib_free_vmalloc_buffer(substream); |
|---|
| 245 | | -} |
|---|
| 246 | | - |
|---|
| 247 | | -static int pcm_playback_hw_free(struct snd_pcm_substream *substream) |
|---|
| 248 | | -{ |
|---|
| 249 | | - struct snd_ff *ff = substream->private_data; |
|---|
| 250 | | - |
|---|
| 251 | | - mutex_lock(&ff->mutex); |
|---|
| 252 | | - |
|---|
| 253 | | - if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) |
|---|
| 254 | | - ff->substreams_counter--; |
|---|
| 255 | | - |
|---|
| 256 | | - snd_ff_stream_stop_duplex(ff); |
|---|
| 257 | | - |
|---|
| 258 | | - mutex_unlock(&ff->mutex); |
|---|
| 259 | | - |
|---|
| 260 | | - return snd_pcm_lib_free_vmalloc_buffer(substream); |
|---|
| 262 | + return 0; |
|---|
| 261 | 263 | } |
|---|
| 262 | 264 | |
|---|
| 263 | 265 | static int pcm_capture_prepare(struct snd_pcm_substream *substream) |
|---|
| .. | .. |
|---|
| 334 | 336 | { |
|---|
| 335 | 337 | struct snd_ff *ff = sbstrm->private_data; |
|---|
| 336 | 338 | |
|---|
| 337 | | - return amdtp_stream_pcm_pointer(&ff->tx_stream); |
|---|
| 339 | + return amdtp_domain_stream_pcm_pointer(&ff->domain, &ff->tx_stream); |
|---|
| 338 | 340 | } |
|---|
| 339 | 341 | |
|---|
| 340 | 342 | static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm) |
|---|
| 341 | 343 | { |
|---|
| 342 | 344 | struct snd_ff *ff = sbstrm->private_data; |
|---|
| 343 | 345 | |
|---|
| 344 | | - return amdtp_stream_pcm_pointer(&ff->rx_stream); |
|---|
| 346 | + return amdtp_domain_stream_pcm_pointer(&ff->domain, &ff->rx_stream); |
|---|
| 345 | 347 | } |
|---|
| 346 | 348 | |
|---|
| 347 | 349 | static int pcm_capture_ack(struct snd_pcm_substream *substream) |
|---|
| 348 | 350 | { |
|---|
| 349 | 351 | struct snd_ff *ff = substream->private_data; |
|---|
| 350 | 352 | |
|---|
| 351 | | - return amdtp_stream_pcm_ack(&ff->tx_stream); |
|---|
| 353 | + return amdtp_domain_stream_pcm_ack(&ff->domain, &ff->tx_stream); |
|---|
| 352 | 354 | } |
|---|
| 353 | 355 | |
|---|
| 354 | 356 | static int pcm_playback_ack(struct snd_pcm_substream *substream) |
|---|
| 355 | 357 | { |
|---|
| 356 | 358 | struct snd_ff *ff = substream->private_data; |
|---|
| 357 | 359 | |
|---|
| 358 | | - return amdtp_stream_pcm_ack(&ff->rx_stream); |
|---|
| 360 | + return amdtp_domain_stream_pcm_ack(&ff->domain, &ff->rx_stream); |
|---|
| 359 | 361 | } |
|---|
| 360 | 362 | |
|---|
| 361 | 363 | int snd_ff_create_pcm_devices(struct snd_ff *ff) |
|---|
| .. | .. |
|---|
| 363 | 365 | static const struct snd_pcm_ops pcm_capture_ops = { |
|---|
| 364 | 366 | .open = pcm_open, |
|---|
| 365 | 367 | .close = pcm_close, |
|---|
| 366 | | - .ioctl = snd_pcm_lib_ioctl, |
|---|
| 367 | | - .hw_params = pcm_capture_hw_params, |
|---|
| 368 | | - .hw_free = pcm_capture_hw_free, |
|---|
| 368 | + .hw_params = pcm_hw_params, |
|---|
| 369 | + .hw_free = pcm_hw_free, |
|---|
| 369 | 370 | .prepare = pcm_capture_prepare, |
|---|
| 370 | 371 | .trigger = pcm_capture_trigger, |
|---|
| 371 | 372 | .pointer = pcm_capture_pointer, |
|---|
| 372 | 373 | .ack = pcm_capture_ack, |
|---|
| 373 | | - .page = snd_pcm_lib_get_vmalloc_page, |
|---|
| 374 | 374 | }; |
|---|
| 375 | 375 | static const struct snd_pcm_ops pcm_playback_ops = { |
|---|
| 376 | 376 | .open = pcm_open, |
|---|
| 377 | 377 | .close = pcm_close, |
|---|
| 378 | | - .ioctl = snd_pcm_lib_ioctl, |
|---|
| 379 | | - .hw_params = pcm_playback_hw_params, |
|---|
| 380 | | - .hw_free = pcm_playback_hw_free, |
|---|
| 378 | + .hw_params = pcm_hw_params, |
|---|
| 379 | + .hw_free = pcm_hw_free, |
|---|
| 381 | 380 | .prepare = pcm_playback_prepare, |
|---|
| 382 | 381 | .trigger = pcm_playback_trigger, |
|---|
| 383 | 382 | .pointer = pcm_playback_pointer, |
|---|
| 384 | 383 | .ack = pcm_playback_ack, |
|---|
| 385 | | - .page = snd_pcm_lib_get_vmalloc_page, |
|---|
| 386 | 384 | }; |
|---|
| 387 | 385 | struct snd_pcm *pcm; |
|---|
| 388 | 386 | int err; |
|---|
| .. | .. |
|---|
| 396 | 394 | "%s PCM", ff->card->shortname); |
|---|
| 397 | 395 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops); |
|---|
| 398 | 396 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops); |
|---|
| 397 | + snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0); |
|---|
| 399 | 398 | |
|---|
| 400 | 399 | return 0; |
|---|
| 401 | 400 | } |
|---|