1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * digi00x-pcm.c - a part of driver for Digidesign Digi 002/003 family 4 * 5 * Copyright (c) 2014-2015 Takashi Sakamoto 6 */ 7 8 #include "digi00x.h" 9 10 static int hw_rule_rate(struct snd_pcm_hw_params *params, 11 struct snd_pcm_hw_rule *rule) 12 { 13 struct snd_interval *r = 14 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 15 const struct snd_interval *c = 16 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS); 17 struct snd_interval t = { 18 .min = UINT_MAX, .max = 0, .integer = 1, 19 }; 20 unsigned int i; 21 22 for (i = 0; i < SND_DG00X_RATE_COUNT; i++) { 23 if (!snd_interval_test(c, 24 snd_dg00x_stream_pcm_channels[i])) 25 continue; 26 27 t.min = min(t.min, snd_dg00x_stream_rates[i]); 28 t.max = max(t.max, snd_dg00x_stream_rates[i]); 29 } 30 31 return snd_interval_refine(r, &t); 32 } 33 34 static int hw_rule_channels(struct snd_pcm_hw_params *params, 35 struct snd_pcm_hw_rule *rule) 36 { 37 struct snd_interval *c = 38 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 39 const struct snd_interval *r = 40 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE); 41 struct snd_interval t = { 42 .min = UINT_MAX, .max = 0, .integer = 1, 43 }; 44 unsigned int i; 45 46 for (i = 0; i < SND_DG00X_RATE_COUNT; i++) { 47 if (!snd_interval_test(r, snd_dg00x_stream_rates[i])) 48 continue; 49 50 t.min = min(t.min, snd_dg00x_stream_pcm_channels[i]); 51 t.max = max(t.max, snd_dg00x_stream_pcm_channels[i]); 52 } 53 54 return snd_interval_refine(c, &t); 55 } 56 57 static int pcm_init_hw_params(struct snd_dg00x *dg00x, 58 struct snd_pcm_substream *substream) 59 { 60 struct snd_pcm_runtime *runtime = substream->runtime; 61 struct snd_pcm_hardware *hw = &runtime->hw; 62 struct amdtp_stream *s; 63 int err; 64 65 66 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { 67 substream->runtime->hw.formats = SNDRV_PCM_FMTBIT_S32; 68 s = &dg00x->tx_stream; 69 } else { 70 substream->runtime->hw.formats = SNDRV_PCM_FMTBIT_S32; 71 s = &dg00x->rx_stream; 72 } 73 74 hw->channels_min = 10; 75 hw->channels_max = 18; 76 77 hw->rates = SNDRV_PCM_RATE_44100 | 78 SNDRV_PCM_RATE_48000 | 79 SNDRV_PCM_RATE_88200 | 80 SNDRV_PCM_RATE_96000; 81 snd_pcm_limit_hw_rates(runtime); 82 83 err = snd_pcm_hw_rule_add(substream->runtime, 0, 84 SNDRV_PCM_HW_PARAM_CHANNELS, 85 hw_rule_channels, NULL, 86 SNDRV_PCM_HW_PARAM_RATE, -1); 87 if (err < 0) 88 return err; 89 90 err = snd_pcm_hw_rule_add(substream->runtime, 0, 91 SNDRV_PCM_HW_PARAM_RATE, 92 hw_rule_rate, NULL, 93 SNDRV_PCM_HW_PARAM_CHANNELS, -1); 94 if (err < 0) 95 return err; 96 97 return amdtp_dot_add_pcm_hw_constraints(s, substream->runtime); 98 } 99 100 static int pcm_open(struct snd_pcm_substream *substream) 101 { 102 struct snd_dg00x *dg00x = substream->private_data; 103 enum snd_dg00x_clock clock; 104 bool detect; 105 unsigned int rate; 106 int err; 107 108 err = snd_dg00x_stream_lock_try(dg00x); 109 if (err < 0) 110 goto end; 111 112 err = pcm_init_hw_params(dg00x, substream); 113 if (err < 0) 114 goto err_locked; 115 116 /* Check current clock source. */ 117 err = snd_dg00x_stream_get_clock(dg00x, &clock); 118 if (err < 0) 119 goto err_locked; 120 if (clock != SND_DG00X_CLOCK_INTERNAL) { 121 err = snd_dg00x_stream_check_external_clock(dg00x, &detect); 122 if (err < 0) 123 goto err_locked; 124 if (!detect) { 125 err = -EBUSY; 126 goto err_locked; 127 } 128 } 129 130 if ((clock != SND_DG00X_CLOCK_INTERNAL) || 131 amdtp_stream_pcm_running(&dg00x->rx_stream) || 132 amdtp_stream_pcm_running(&dg00x->tx_stream)) { 133 err = snd_dg00x_stream_get_external_rate(dg00x, &rate); 134 if (err < 0) 135 goto err_locked; 136 substream->runtime->hw.rate_min = rate; 137 substream->runtime->hw.rate_max = rate; 138 } 139 140 snd_pcm_set_sync(substream); 141 end: 142 return err; 143 err_locked: 144 snd_dg00x_stream_lock_release(dg00x); 145 return err; 146 } 147 148 static int pcm_close(struct snd_pcm_substream *substream) 149 { 150 struct snd_dg00x *dg00x = substream->private_data; 151 152 snd_dg00x_stream_lock_release(dg00x); 153 154 return 0; 155 } 156 157 static int pcm_capture_hw_params(struct snd_pcm_substream *substream, 158 struct snd_pcm_hw_params *hw_params) 159 { 160 struct snd_dg00x *dg00x = substream->private_data; 161 int err; 162 163 err = snd_pcm_lib_alloc_vmalloc_buffer(substream, 164 params_buffer_bytes(hw_params)); 165 if (err < 0) 166 return err; 167 168 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) { 169 mutex_lock(&dg00x->mutex); 170 dg00x->substreams_counter++; 171 mutex_unlock(&dg00x->mutex); 172 } 173 174 return 0; 175 } 176 177 static int pcm_playback_hw_params(struct snd_pcm_substream *substream, 178 struct snd_pcm_hw_params *hw_params) 179 { 180 struct snd_dg00x *dg00x = substream->private_data; 181 int err; 182 183 err = snd_pcm_lib_alloc_vmalloc_buffer(substream, 184 params_buffer_bytes(hw_params)); 185 if (err < 0) 186 return err; 187 188 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) { 189 mutex_lock(&dg00x->mutex); 190 dg00x->substreams_counter++; 191 mutex_unlock(&dg00x->mutex); 192 } 193 194 return 0; 195 } 196 197 static int pcm_capture_hw_free(struct snd_pcm_substream *substream) 198 { 199 struct snd_dg00x *dg00x = substream->private_data; 200 201 mutex_lock(&dg00x->mutex); 202 203 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) 204 dg00x->substreams_counter--; 205 206 snd_dg00x_stream_stop_duplex(dg00x); 207 208 mutex_unlock(&dg00x->mutex); 209 210 return snd_pcm_lib_free_vmalloc_buffer(substream); 211 } 212 213 static int pcm_playback_hw_free(struct snd_pcm_substream *substream) 214 { 215 struct snd_dg00x *dg00x = substream->private_data; 216 217 mutex_lock(&dg00x->mutex); 218 219 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) 220 dg00x->substreams_counter--; 221 222 snd_dg00x_stream_stop_duplex(dg00x); 223 224 mutex_unlock(&dg00x->mutex); 225 226 return snd_pcm_lib_free_vmalloc_buffer(substream); 227 } 228 229 static int pcm_capture_prepare(struct snd_pcm_substream *substream) 230 { 231 struct snd_dg00x *dg00x = substream->private_data; 232 struct snd_pcm_runtime *runtime = substream->runtime; 233 int err; 234 235 mutex_lock(&dg00x->mutex); 236 237 err = snd_dg00x_stream_start_duplex(dg00x, runtime->rate); 238 if (err >= 0) 239 amdtp_stream_pcm_prepare(&dg00x->tx_stream); 240 241 mutex_unlock(&dg00x->mutex); 242 243 return err; 244 } 245 246 static int pcm_playback_prepare(struct snd_pcm_substream *substream) 247 { 248 struct snd_dg00x *dg00x = substream->private_data; 249 struct snd_pcm_runtime *runtime = substream->runtime; 250 int err; 251 252 mutex_lock(&dg00x->mutex); 253 254 err = snd_dg00x_stream_start_duplex(dg00x, runtime->rate); 255 if (err >= 0) { 256 amdtp_stream_pcm_prepare(&dg00x->rx_stream); 257 amdtp_dot_reset(&dg00x->rx_stream); 258 } 259 260 mutex_unlock(&dg00x->mutex); 261 262 return err; 263 } 264 265 static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd) 266 { 267 struct snd_dg00x *dg00x = substream->private_data; 268 269 switch (cmd) { 270 case SNDRV_PCM_TRIGGER_START: 271 amdtp_stream_pcm_trigger(&dg00x->tx_stream, substream); 272 break; 273 case SNDRV_PCM_TRIGGER_STOP: 274 amdtp_stream_pcm_trigger(&dg00x->tx_stream, NULL); 275 break; 276 default: 277 return -EINVAL; 278 } 279 280 return 0; 281 } 282 283 static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd) 284 { 285 struct snd_dg00x *dg00x = substream->private_data; 286 287 switch (cmd) { 288 case SNDRV_PCM_TRIGGER_START: 289 amdtp_stream_pcm_trigger(&dg00x->rx_stream, substream); 290 break; 291 case SNDRV_PCM_TRIGGER_STOP: 292 amdtp_stream_pcm_trigger(&dg00x->rx_stream, NULL); 293 break; 294 default: 295 return -EINVAL; 296 } 297 298 return 0; 299 } 300 301 static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm) 302 { 303 struct snd_dg00x *dg00x = sbstrm->private_data; 304 305 return amdtp_stream_pcm_pointer(&dg00x->tx_stream); 306 } 307 308 static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm) 309 { 310 struct snd_dg00x *dg00x = sbstrm->private_data; 311 312 return amdtp_stream_pcm_pointer(&dg00x->rx_stream); 313 } 314 315 static int pcm_capture_ack(struct snd_pcm_substream *substream) 316 { 317 struct snd_dg00x *dg00x = substream->private_data; 318 319 return amdtp_stream_pcm_ack(&dg00x->tx_stream); 320 } 321 322 static int pcm_playback_ack(struct snd_pcm_substream *substream) 323 { 324 struct snd_dg00x *dg00x = substream->private_data; 325 326 return amdtp_stream_pcm_ack(&dg00x->rx_stream); 327 } 328 329 int snd_dg00x_create_pcm_devices(struct snd_dg00x *dg00x) 330 { 331 static const struct snd_pcm_ops capture_ops = { 332 .open = pcm_open, 333 .close = pcm_close, 334 .ioctl = snd_pcm_lib_ioctl, 335 .hw_params = pcm_capture_hw_params, 336 .hw_free = pcm_capture_hw_free, 337 .prepare = pcm_capture_prepare, 338 .trigger = pcm_capture_trigger, 339 .pointer = pcm_capture_pointer, 340 .ack = pcm_capture_ack, 341 .page = snd_pcm_lib_get_vmalloc_page, 342 }; 343 static const struct snd_pcm_ops playback_ops = { 344 .open = pcm_open, 345 .close = pcm_close, 346 .ioctl = snd_pcm_lib_ioctl, 347 .hw_params = pcm_playback_hw_params, 348 .hw_free = pcm_playback_hw_free, 349 .prepare = pcm_playback_prepare, 350 .trigger = pcm_playback_trigger, 351 .pointer = pcm_playback_pointer, 352 .ack = pcm_playback_ack, 353 .page = snd_pcm_lib_get_vmalloc_page, 354 }; 355 struct snd_pcm *pcm; 356 int err; 357 358 err = snd_pcm_new(dg00x->card, dg00x->card->driver, 0, 1, 1, &pcm); 359 if (err < 0) 360 return err; 361 362 pcm->private_data = dg00x; 363 snprintf(pcm->name, sizeof(pcm->name), 364 "%s PCM", dg00x->card->shortname); 365 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops); 366 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops); 367 368 return 0; 369 } 370