1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * motu-pcm.c - a part of driver for MOTU FireWire series 4 * 5 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp> 6 */ 7 8 #include <sound/pcm_params.h> 9 #include "motu.h" 10 11 static int motu_rate_constraint(struct snd_pcm_hw_params *params, 12 struct snd_pcm_hw_rule *rule) 13 { 14 struct snd_motu_packet_format *formats = rule->private; 15 16 const struct snd_interval *c = 17 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS); 18 struct snd_interval *r = 19 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 20 struct snd_interval rates = { 21 .min = UINT_MAX, .max = 0, .integer = 1 22 }; 23 unsigned int i, pcm_channels, rate, mode; 24 25 for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) { 26 rate = snd_motu_clock_rates[i]; 27 mode = i / 2; 28 29 pcm_channels = formats->fixed_part_pcm_chunks[mode] + 30 formats->differed_part_pcm_chunks[mode]; 31 if (!snd_interval_test(c, pcm_channels)) 32 continue; 33 34 rates.min = min(rates.min, rate); 35 rates.max = max(rates.max, rate); 36 } 37 38 return snd_interval_refine(r, &rates); 39 } 40 41 static int motu_channels_constraint(struct snd_pcm_hw_params *params, 42 struct snd_pcm_hw_rule *rule) 43 { 44 struct snd_motu_packet_format *formats = rule->private; 45 46 const struct snd_interval *r = 47 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE); 48 struct snd_interval *c = 49 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 50 struct snd_interval channels = { 51 .min = UINT_MAX, .max = 0, .integer = 1 52 }; 53 unsigned int i, pcm_channels, rate, mode; 54 55 for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) { 56 rate = snd_motu_clock_rates[i]; 57 mode = i / 2; 58 59 if (!snd_interval_test(r, rate)) 60 continue; 61 62 pcm_channels = formats->fixed_part_pcm_chunks[mode] + 63 formats->differed_part_pcm_chunks[mode]; 64 channels.min = min(channels.min, pcm_channels); 65 channels.max = max(channels.max, pcm_channels); 66 } 67 68 return snd_interval_refine(c, &channels); 69 } 70 71 static void limit_channels_and_rates(struct snd_motu *motu, 72 struct snd_pcm_runtime *runtime, 73 struct snd_motu_packet_format *formats) 74 { 75 struct snd_pcm_hardware *hw = &runtime->hw; 76 unsigned int i, pcm_channels, rate, mode; 77 78 hw->channels_min = UINT_MAX; 79 hw->channels_max = 0; 80 81 for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) { 82 rate = snd_motu_clock_rates[i]; 83 mode = i / 2; 84 85 pcm_channels = formats->fixed_part_pcm_chunks[mode] + 86 formats->differed_part_pcm_chunks[mode]; 87 if (pcm_channels == 0) 88 continue; 89 90 hw->rates |= snd_pcm_rate_to_rate_bit(rate); 91 hw->channels_min = min(hw->channels_min, pcm_channels); 92 hw->channels_max = max(hw->channels_max, pcm_channels); 93 } 94 95 snd_pcm_limit_hw_rates(runtime); 96 } 97 98 static int init_hw_info(struct snd_motu *motu, 99 struct snd_pcm_substream *substream) 100 { 101 struct snd_pcm_runtime *runtime = substream->runtime; 102 struct snd_pcm_hardware *hw = &runtime->hw; 103 struct amdtp_stream *stream; 104 struct snd_motu_packet_format *formats; 105 int err; 106 107 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { 108 hw->formats = SNDRV_PCM_FMTBIT_S32; 109 stream = &motu->tx_stream; 110 formats = &motu->tx_packet_formats; 111 } else { 112 hw->formats = SNDRV_PCM_FMTBIT_S32; 113 stream = &motu->rx_stream; 114 formats = &motu->rx_packet_formats; 115 } 116 117 limit_channels_and_rates(motu, runtime, formats); 118 119 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 120 motu_rate_constraint, formats, 121 SNDRV_PCM_HW_PARAM_CHANNELS, -1); 122 if (err < 0) 123 return err; 124 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 125 motu_channels_constraint, formats, 126 SNDRV_PCM_HW_PARAM_RATE, -1); 127 if (err < 0) 128 return err; 129 130 return amdtp_motu_add_pcm_hw_constraints(stream, runtime); 131 } 132 133 static int pcm_open(struct snd_pcm_substream *substream) 134 { 135 struct snd_motu *motu = substream->private_data; 136 const struct snd_motu_protocol *const protocol = motu->spec->protocol; 137 struct amdtp_domain *d = &motu->domain; 138 enum snd_motu_clock_source src; 139 int err; 140 141 err = snd_motu_stream_lock_try(motu); 142 if (err < 0) 143 return err; 144 145 mutex_lock(&motu->mutex); 146 147 err = snd_motu_stream_cache_packet_formats(motu); 148 if (err < 0) 149 goto err_locked; 150 151 err = init_hw_info(motu, substream); 152 if (err < 0) 153 goto err_locked; 154 155 err = protocol->get_clock_source(motu, &src); 156 if (err < 0) 157 goto err_locked; 158 159 // When source of clock is not internal or any stream is reserved for 160 // transmission of PCM frames, the available sampling rate is limited 161 // at current one. 162 if ((src != SND_MOTU_CLOCK_SOURCE_INTERNAL && 163 src != SND_MOTU_CLOCK_SOURCE_SPH) || 164 (motu->substreams_counter > 0 && d->events_per_period > 0)) { 165 unsigned int frames_per_period = d->events_per_period; 166 unsigned int frames_per_buffer = d->events_per_buffer; 167 unsigned int rate; 168 169 err = protocol->get_clock_rate(motu, &rate); 170 if (err < 0) 171 goto err_locked; 172 173 substream->runtime->hw.rate_min = rate; 174 substream->runtime->hw.rate_max = rate; 175 176 if (frames_per_period > 0) { 177 err = snd_pcm_hw_constraint_minmax(substream->runtime, 178 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 179 frames_per_period, frames_per_period); 180 if (err < 0) 181 goto err_locked; 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 goto err_locked; 188 } 189 } 190 191 snd_pcm_set_sync(substream); 192 193 mutex_unlock(&motu->mutex); 194 195 return 0; 196 err_locked: 197 mutex_unlock(&motu->mutex); 198 snd_motu_stream_lock_release(motu); 199 return err; 200 } 201 202 static int pcm_close(struct snd_pcm_substream *substream) 203 { 204 struct snd_motu *motu = substream->private_data; 205 206 snd_motu_stream_lock_release(motu); 207 208 return 0; 209 } 210 211 static int pcm_hw_params(struct snd_pcm_substream *substream, 212 struct snd_pcm_hw_params *hw_params) 213 { 214 struct snd_motu *motu = substream->private_data; 215 int err = 0; 216 217 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 222 mutex_lock(&motu->mutex); 223 err = snd_motu_stream_reserve_duplex(motu, rate, 224 frames_per_period, frames_per_buffer); 225 if (err >= 0) 226 ++motu->substreams_counter; 227 mutex_unlock(&motu->mutex); 228 } 229 230 return err; 231 } 232 233 static int pcm_hw_free(struct snd_pcm_substream *substream) 234 { 235 struct snd_motu *motu = substream->private_data; 236 237 mutex_lock(&motu->mutex); 238 239 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) 240 --motu->substreams_counter; 241 242 snd_motu_stream_stop_duplex(motu); 243 244 mutex_unlock(&motu->mutex); 245 246 return 0; 247 } 248 249 static int capture_prepare(struct snd_pcm_substream *substream) 250 { 251 struct snd_motu *motu = substream->private_data; 252 int err; 253 254 mutex_lock(&motu->mutex); 255 err = snd_motu_stream_start_duplex(motu); 256 mutex_unlock(&motu->mutex); 257 if (err >= 0) 258 amdtp_stream_pcm_prepare(&motu->tx_stream); 259 260 return 0; 261 } 262 static int playback_prepare(struct snd_pcm_substream *substream) 263 { 264 struct snd_motu *motu = substream->private_data; 265 int err; 266 267 mutex_lock(&motu->mutex); 268 err = snd_motu_stream_start_duplex(motu); 269 mutex_unlock(&motu->mutex); 270 if (err >= 0) 271 amdtp_stream_pcm_prepare(&motu->rx_stream); 272 273 return err; 274 } 275 276 static int capture_trigger(struct snd_pcm_substream *substream, int cmd) 277 { 278 struct snd_motu *motu = substream->private_data; 279 280 switch (cmd) { 281 case SNDRV_PCM_TRIGGER_START: 282 amdtp_stream_pcm_trigger(&motu->tx_stream, substream); 283 break; 284 case SNDRV_PCM_TRIGGER_STOP: 285 amdtp_stream_pcm_trigger(&motu->tx_stream, NULL); 286 break; 287 default: 288 return -EINVAL; 289 } 290 291 return 0; 292 } 293 static int playback_trigger(struct snd_pcm_substream *substream, int cmd) 294 { 295 struct snd_motu *motu = substream->private_data; 296 297 switch (cmd) { 298 case SNDRV_PCM_TRIGGER_START: 299 amdtp_stream_pcm_trigger(&motu->rx_stream, substream); 300 break; 301 case SNDRV_PCM_TRIGGER_STOP: 302 amdtp_stream_pcm_trigger(&motu->rx_stream, NULL); 303 break; 304 default: 305 return -EINVAL; 306 } 307 308 return 0; 309 } 310 311 static snd_pcm_uframes_t capture_pointer(struct snd_pcm_substream *substream) 312 { 313 struct snd_motu *motu = substream->private_data; 314 315 return amdtp_domain_stream_pcm_pointer(&motu->domain, &motu->tx_stream); 316 } 317 static snd_pcm_uframes_t playback_pointer(struct snd_pcm_substream *substream) 318 { 319 struct snd_motu *motu = substream->private_data; 320 321 return amdtp_domain_stream_pcm_pointer(&motu->domain, &motu->rx_stream); 322 } 323 324 static int capture_ack(struct snd_pcm_substream *substream) 325 { 326 struct snd_motu *motu = substream->private_data; 327 328 return amdtp_domain_stream_pcm_ack(&motu->domain, &motu->tx_stream); 329 } 330 331 static int playback_ack(struct snd_pcm_substream *substream) 332 { 333 struct snd_motu *motu = substream->private_data; 334 335 return amdtp_domain_stream_pcm_ack(&motu->domain, &motu->rx_stream); 336 } 337 338 int snd_motu_create_pcm_devices(struct snd_motu *motu) 339 { 340 static const struct snd_pcm_ops capture_ops = { 341 .open = pcm_open, 342 .close = pcm_close, 343 .hw_params = pcm_hw_params, 344 .hw_free = pcm_hw_free, 345 .prepare = capture_prepare, 346 .trigger = capture_trigger, 347 .pointer = capture_pointer, 348 .ack = capture_ack, 349 }; 350 static const struct snd_pcm_ops playback_ops = { 351 .open = pcm_open, 352 .close = pcm_close, 353 .hw_params = pcm_hw_params, 354 .hw_free = pcm_hw_free, 355 .prepare = playback_prepare, 356 .trigger = playback_trigger, 357 .pointer = playback_pointer, 358 .ack = playback_ack, 359 }; 360 struct snd_pcm *pcm; 361 int err; 362 363 err = snd_pcm_new(motu->card, motu->card->driver, 0, 1, 1, &pcm); 364 if (err < 0) 365 return err; 366 pcm->private_data = motu; 367 strcpy(pcm->name, motu->card->shortname); 368 369 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops); 370 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops); 371 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0); 372 373 return 0; 374 } 375