1 // SPDX-License-Identifier: GPL-2.0-only 1 2 /* 3 * motu-pcm.c - a part of driver for MOTU Fire 4 * 5 * Copyright (c) 2015-2017 Takashi Sakamoto <o 6 */ 7 8 #include <sound/pcm_params.h> 9 #include "motu.h" 10 11 static int motu_rate_constraint(struct snd_pcm 12 struct snd_pcm 13 { 14 struct snd_motu_packet_format *formats 15 16 const struct snd_interval *c = 17 hw_param_interval_c(params, SN 18 struct snd_interval *r = 19 hw_param_interval(params, SNDR 20 struct snd_interval rates = { 21 .min = UINT_MAX, .max = 0, .in 22 }; 23 unsigned int i, pcm_channels, rate, mo 24 25 for (i = 0; i < ARRAY_SIZE(snd_motu_cl 26 rate = snd_motu_clock_rates[i] 27 mode = i / 2; 28 29 pcm_channels = formats->pcm_ch 30 if (!snd_interval_test(c, pcm_ 31 continue; 32 33 rates.min = min(rates.min, rat 34 rates.max = max(rates.max, rat 35 } 36 37 return snd_interval_refine(r, &rates); 38 } 39 40 static int motu_channels_constraint(struct snd 41 struct snd 42 { 43 struct snd_motu_packet_format *formats 44 45 const struct snd_interval *r = 46 hw_param_interval_c(params, SN 47 struct snd_interval *c = 48 hw_param_interval(params, SNDR 49 struct snd_interval channels = { 50 .min = UINT_MAX, .max = 0, .in 51 }; 52 unsigned int i, pcm_channels, rate, mo 53 54 for (i = 0; i < ARRAY_SIZE(snd_motu_cl 55 rate = snd_motu_clock_rates[i] 56 mode = i / 2; 57 58 if (!snd_interval_test(r, rate 59 continue; 60 61 pcm_channels = formats->pcm_ch 62 channels.min = min(channels.mi 63 channels.max = max(channels.ma 64 } 65 66 return snd_interval_refine(c, &channel 67 } 68 69 static void limit_channels_and_rates(struct sn 70 struct sn 71 struct sn 72 { 73 struct snd_pcm_hardware *hw = &runtime 74 unsigned int i, pcm_channels, rate, mo 75 76 hw->channels_min = UINT_MAX; 77 hw->channels_max = 0; 78 79 for (i = 0; i < ARRAY_SIZE(snd_motu_cl 80 rate = snd_motu_clock_rates[i] 81 mode = i / 2; 82 83 pcm_channels = formats->pcm_ch 84 if (pcm_channels == 0) 85 continue; 86 87 hw->rates |= snd_pcm_rate_to_r 88 hw->channels_min = min(hw->cha 89 hw->channels_max = max(hw->cha 90 } 91 92 snd_pcm_limit_hw_rates(runtime); 93 } 94 95 static int init_hw_info(struct snd_motu *motu, 96 struct snd_pcm_substre 97 { 98 struct snd_pcm_runtime *runtime = subs 99 struct snd_pcm_hardware *hw = &runtime 100 struct amdtp_stream *stream; 101 struct snd_motu_packet_format *formats 102 int err; 103 104 if (substream->stream == SNDRV_PCM_STR 105 hw->formats = SNDRV_PCM_FMTBIT 106 stream = &motu->tx_stream; 107 formats = &motu->tx_packet_for 108 } else { 109 hw->formats = SNDRV_PCM_FMTBIT 110 stream = &motu->rx_stream; 111 formats = &motu->rx_packet_for 112 } 113 114 limit_channels_and_rates(motu, runtime 115 116 err = snd_pcm_hw_rule_add(runtime, 0, 117 motu_rate_co 118 SNDRV_PCM_HW 119 if (err < 0) 120 return err; 121 err = snd_pcm_hw_rule_add(runtime, 0, 122 motu_channel 123 SNDRV_PCM_HW 124 if (err < 0) 125 return err; 126 127 return amdtp_motu_add_pcm_hw_constrain 128 } 129 130 static int pcm_open(struct snd_pcm_substream * 131 { 132 struct snd_motu *motu = substream->pri 133 struct amdtp_domain *d = &motu->domain 134 enum snd_motu_clock_source src; 135 int err; 136 137 err = snd_motu_stream_lock_try(motu); 138 if (err < 0) 139 return err; 140 141 mutex_lock(&motu->mutex); 142 143 err = snd_motu_stream_cache_packet_for 144 if (err < 0) 145 goto err_locked; 146 147 err = init_hw_info(motu, substream); 148 if (err < 0) 149 goto err_locked; 150 151 err = snd_motu_protocol_get_clock_sour 152 if (err < 0) 153 goto err_locked; 154 155 // When source of clock is not interna 156 // transmission of PCM frames, the ava 157 // at current one. 158 if ((src != SND_MOTU_CLOCK_SOURCE_INTE 159 src != SND_MOTU_CLOCK_SOURCE_SPH) 160 (motu->substreams_counter > 0 && d 161 unsigned int frames_per_period 162 unsigned int frames_per_buffer 163 unsigned int rate; 164 165 err = snd_motu_protocol_get_cl 166 if (err < 0) 167 goto err_locked; 168 169 substream->runtime->hw.rate_mi 170 substream->runtime->hw.rate_ma 171 172 if (frames_per_period > 0) { 173 err = snd_pcm_hw_const 174 SNDRV_ 175 frames 176 if (err < 0) 177 goto err_locke 178 179 err = snd_pcm_hw_const 180 SNDRV_ 181 frames 182 if (err < 0) 183 goto err_locke 184 } 185 } 186 187 snd_pcm_set_sync(substream); 188 189 mutex_unlock(&motu->mutex); 190 191 return 0; 192 err_locked: 193 mutex_unlock(&motu->mutex); 194 snd_motu_stream_lock_release(motu); 195 return err; 196 } 197 198 static int pcm_close(struct snd_pcm_substream 199 { 200 struct snd_motu *motu = substream->pri 201 202 snd_motu_stream_lock_release(motu); 203 204 return 0; 205 } 206 207 static int pcm_hw_params(struct snd_pcm_substr 208 struct snd_pcm_hw_par 209 { 210 struct snd_motu *motu = substream->pri 211 int err = 0; 212 213 if (substream->runtime->state == SNDRV 214 unsigned int rate = params_rat 215 unsigned int frames_per_period 216 unsigned int frames_per_buffer 217 218 mutex_lock(&motu->mutex); 219 err = snd_motu_stream_reserve_ 220 frames 221 if (err >= 0) 222 ++motu->substreams_cou 223 mutex_unlock(&motu->mutex); 224 } 225 226 return err; 227 } 228 229 static int pcm_hw_free(struct snd_pcm_substrea 230 { 231 struct snd_motu *motu = substream->pri 232 233 mutex_lock(&motu->mutex); 234 235 if (substream->runtime->state != SNDRV 236 --motu->substreams_counter; 237 238 snd_motu_stream_stop_duplex(motu); 239 240 mutex_unlock(&motu->mutex); 241 242 return 0; 243 } 244 245 static int capture_prepare(struct snd_pcm_subs 246 { 247 struct snd_motu *motu = substream->pri 248 int err; 249 250 mutex_lock(&motu->mutex); 251 err = snd_motu_stream_start_duplex(mot 252 mutex_unlock(&motu->mutex); 253 if (err >= 0) 254 amdtp_stream_pcm_prepare(&motu 255 256 return 0; 257 } 258 static int playback_prepare(struct snd_pcm_sub 259 { 260 struct snd_motu *motu = substream->pri 261 int err; 262 263 mutex_lock(&motu->mutex); 264 err = snd_motu_stream_start_duplex(mot 265 mutex_unlock(&motu->mutex); 266 if (err >= 0) 267 amdtp_stream_pcm_prepare(&motu 268 269 return err; 270 } 271 272 static int capture_trigger(struct snd_pcm_subs 273 { 274 struct snd_motu *motu = substream->pri 275 276 switch (cmd) { 277 case SNDRV_PCM_TRIGGER_START: 278 amdtp_stream_pcm_trigger(&motu 279 break; 280 case SNDRV_PCM_TRIGGER_STOP: 281 amdtp_stream_pcm_trigger(&motu 282 break; 283 default: 284 return -EINVAL; 285 } 286 287 return 0; 288 } 289 static int playback_trigger(struct snd_pcm_sub 290 { 291 struct snd_motu *motu = substream->pri 292 293 switch (cmd) { 294 case SNDRV_PCM_TRIGGER_START: 295 amdtp_stream_pcm_trigger(&motu 296 break; 297 case SNDRV_PCM_TRIGGER_STOP: 298 amdtp_stream_pcm_trigger(&motu 299 break; 300 default: 301 return -EINVAL; 302 } 303 304 return 0; 305 } 306 307 static snd_pcm_uframes_t capture_pointer(struc 308 { 309 struct snd_motu *motu = substream->pri 310 311 return amdtp_domain_stream_pcm_pointer 312 } 313 static snd_pcm_uframes_t playback_pointer(stru 314 { 315 struct snd_motu *motu = substream->pri 316 317 return amdtp_domain_stream_pcm_pointer 318 } 319 320 static int capture_ack(struct snd_pcm_substrea 321 { 322 struct snd_motu *motu = substream->pri 323 324 return amdtp_domain_stream_pcm_ack(&mo 325 } 326 327 static int playback_ack(struct snd_pcm_substre 328 { 329 struct snd_motu *motu = substream->pri 330 331 return amdtp_domain_stream_pcm_ack(&mo 332 } 333 334 int snd_motu_create_pcm_devices(struct snd_mot 335 { 336 static const struct snd_pcm_ops captur 337 .open = pcm_open, 338 .close = pcm_close, 339 .hw_params = pcm_hw_params, 340 .hw_free = pcm_hw_free, 341 .prepare = capture_prepare, 342 .trigger = capture_trigger, 343 .pointer = capture_pointer, 344 .ack = capture_ack, 345 }; 346 static const struct snd_pcm_ops playba 347 .open = pcm_open, 348 .close = pcm_close, 349 .hw_params = pcm_hw_params, 350 .hw_free = pcm_hw_free, 351 .prepare = playback_prepare, 352 .trigger = playback_trigger, 353 .pointer = playback_pointer, 354 .ack = playback_ack, 355 }; 356 struct snd_pcm *pcm; 357 int err; 358 359 err = snd_pcm_new(motu->card, motu->ca 360 if (err < 0) 361 return err; 362 pcm->private_data = motu; 363 strcpy(pcm->name, motu->card->shortnam 364 365 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_ 366 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_ 367 snd_pcm_set_managed_buffer_all(pcm, SN 368 369 return 0; 370 } 371
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.