1 /* 2 * dice_pcm.c - a part of driver for DICE based devices 3 * 4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de> 5 * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp> 6 * 7 * Licensed under the terms of the GNU General Public License, version 2. 8 */ 9 10 #include "dice.h" 11 12 static int dice_rate_constraint(struct snd_pcm_hw_params *params, 13 struct snd_pcm_hw_rule *rule) 14 { 15 struct snd_pcm_substream *substream = rule->private; 16 struct snd_dice *dice = substream->private_data; 17 unsigned int index = substream->pcm->device; 18 19 const struct snd_interval *c = 20 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS); 21 struct snd_interval *r = 22 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 23 struct snd_interval rates = { 24 .min = UINT_MAX, .max = 0, .integer = 1 25 }; 26 unsigned int *pcm_channels; 27 enum snd_dice_rate_mode mode; 28 unsigned int i, rate; 29 30 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) 31 pcm_channels = dice->tx_pcm_chs[index]; 32 else 33 pcm_channels = dice->rx_pcm_chs[index]; 34 35 for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) { 36 rate = snd_dice_rates[i]; 37 if (snd_dice_stream_get_rate_mode(dice, rate, &mode) < 0) 38 continue; 39 40 if (!snd_interval_test(c, pcm_channels[mode])) 41 continue; 42 43 rates.min = min(rates.min, rate); 44 rates.max = max(rates.max, rate); 45 } 46 47 return snd_interval_refine(r, &rates); 48 } 49 50 static int dice_channels_constraint(struct snd_pcm_hw_params *params, 51 struct snd_pcm_hw_rule *rule) 52 { 53 struct snd_pcm_substream *substream = rule->private; 54 struct snd_dice *dice = substream->private_data; 55 unsigned int index = substream->pcm->device; 56 57 const struct snd_interval *r = 58 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE); 59 struct snd_interval *c = 60 hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 61 struct snd_interval channels = { 62 .min = UINT_MAX, .max = 0, .integer = 1 63 }; 64 unsigned int *pcm_channels; 65 enum snd_dice_rate_mode mode; 66 unsigned int i, rate; 67 68 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) 69 pcm_channels = dice->tx_pcm_chs[index]; 70 else 71 pcm_channels = dice->rx_pcm_chs[index]; 72 73 for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) { 74 rate = snd_dice_rates[i]; 75 if (snd_dice_stream_get_rate_mode(dice, rate, &mode) < 0) 76 continue; 77 78 if (!snd_interval_test(r, rate)) 79 continue; 80 81 channels.min = min(channels.min, pcm_channels[mode]); 82 channels.max = max(channels.max, pcm_channels[mode]); 83 } 84 85 return snd_interval_refine(c, &channels); 86 } 87 88 static int limit_channels_and_rates(struct snd_dice *dice, 89 struct snd_pcm_runtime *runtime, 90 enum amdtp_stream_direction dir, 91 unsigned int index) 92 { 93 struct snd_pcm_hardware *hw = &runtime->hw; 94 unsigned int *pcm_channels; 95 unsigned int i; 96 97 if (dir == AMDTP_IN_STREAM) 98 pcm_channels = dice->tx_pcm_chs[index]; 99 else 100 pcm_channels = dice->rx_pcm_chs[index]; 101 102 hw->channels_min = UINT_MAX; 103 hw->channels_max = 0; 104 105 for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) { 106 enum snd_dice_rate_mode mode; 107 unsigned int rate, channels; 108 109 rate = snd_dice_rates[i]; 110 if (snd_dice_stream_get_rate_mode(dice, rate, &mode) < 0) 111 continue; 112 hw->rates |= snd_pcm_rate_to_rate_bit(rate); 113 114 channels = pcm_channels[mode]; 115 if (channels == 0) 116 continue; 117 hw->channels_min = min(hw->channels_min, channels); 118 hw->channels_max = max(hw->channels_max, channels); 119 } 120 121 snd_pcm_limit_hw_rates(runtime); 122 123 return 0; 124 } 125 126 static int init_hw_info(struct snd_dice *dice, 127 struct snd_pcm_substream *substream) 128 { 129 struct snd_pcm_runtime *runtime = substream->runtime; 130 struct snd_pcm_hardware *hw = &runtime->hw; 131 unsigned int index = substream->pcm->device; 132 enum amdtp_stream_direction dir; 133 struct amdtp_stream *stream; 134 int err; 135 136 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { 137 hw->formats = AM824_IN_PCM_FORMAT_BITS; 138 dir = AMDTP_IN_STREAM; 139 stream = &dice->tx_stream[index]; 140 } else { 141 hw->formats = AM824_OUT_PCM_FORMAT_BITS; 142 dir = AMDTP_OUT_STREAM; 143 stream = &dice->rx_stream[index]; 144 } 145 146 err = limit_channels_and_rates(dice, substream->runtime, dir, 147 index); 148 if (err < 0) 149 return err; 150 151 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 152 dice_rate_constraint, substream, 153 SNDRV_PCM_HW_PARAM_CHANNELS, -1); 154 if (err < 0) 155 return err; 156 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 157 dice_channels_constraint, substream, 158 SNDRV_PCM_HW_PARAM_RATE, -1); 159 if (err < 0) 160 return err; 161 162 return amdtp_am824_add_pcm_hw_constraints(stream, runtime); 163 } 164 165 static int pcm_open(struct snd_pcm_substream *substream) 166 { 167 struct snd_dice *dice = substream->private_data; 168 unsigned int source; 169 bool internal; 170 int err; 171 172 err = snd_dice_stream_lock_try(dice); 173 if (err < 0) 174 goto end; 175 176 err = init_hw_info(dice, substream); 177 if (err < 0) 178 goto err_locked; 179 180 err = snd_dice_transaction_get_clock_source(dice, &source); 181 if (err < 0) 182 goto err_locked; 183 switch (source) { 184 case CLOCK_SOURCE_AES1: 185 case CLOCK_SOURCE_AES2: 186 case CLOCK_SOURCE_AES3: 187 case CLOCK_SOURCE_AES4: 188 case CLOCK_SOURCE_AES_ANY: 189 case CLOCK_SOURCE_ADAT: 190 case CLOCK_SOURCE_TDIF: 191 case CLOCK_SOURCE_WC: 192 internal = false; 193 break; 194 default: 195 internal = true; 196 break; 197 } 198 199 /* 200 * When source of clock is not internal or any PCM streams are running, 201 * available sampling rate is limited at current sampling rate. 202 */ 203 if (!internal || 204 amdtp_stream_pcm_running(&dice->tx_stream[0]) || 205 amdtp_stream_pcm_running(&dice->tx_stream[1]) || 206 amdtp_stream_pcm_running(&dice->rx_stream[0]) || 207 amdtp_stream_pcm_running(&dice->rx_stream[1])) { 208 unsigned int rate; 209 210 err = snd_dice_transaction_get_rate(dice, &rate); 211 if (err < 0) 212 goto err_locked; 213 substream->runtime->hw.rate_min = rate; 214 substream->runtime->hw.rate_max = rate; 215 } 216 217 snd_pcm_set_sync(substream); 218 end: 219 return err; 220 err_locked: 221 snd_dice_stream_lock_release(dice); 222 return err; 223 } 224 225 static int pcm_close(struct snd_pcm_substream *substream) 226 { 227 struct snd_dice *dice = substream->private_data; 228 229 snd_dice_stream_lock_release(dice); 230 231 return 0; 232 } 233 234 static int pcm_hw_params(struct snd_pcm_substream *substream, 235 struct snd_pcm_hw_params *hw_params) 236 { 237 struct snd_dice *dice = substream->private_data; 238 int err; 239 240 err = snd_pcm_lib_alloc_vmalloc_buffer(substream, 241 params_buffer_bytes(hw_params)); 242 if (err < 0) 243 return err; 244 245 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) { 246 unsigned int rate = params_rate(hw_params); 247 248 mutex_lock(&dice->mutex); 249 err = snd_dice_stream_reserve_duplex(dice, rate); 250 if (err >= 0) 251 ++dice->substreams_counter; 252 mutex_unlock(&dice->mutex); 253 } 254 255 return err; 256 } 257 258 static int pcm_hw_free(struct snd_pcm_substream *substream) 259 { 260 struct snd_dice *dice = substream->private_data; 261 262 mutex_lock(&dice->mutex); 263 264 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) 265 --dice->substreams_counter; 266 267 snd_dice_stream_stop_duplex(dice); 268 269 mutex_unlock(&dice->mutex); 270 271 return snd_pcm_lib_free_vmalloc_buffer(substream); 272 } 273 274 static int capture_prepare(struct snd_pcm_substream *substream) 275 { 276 struct snd_dice *dice = substream->private_data; 277 struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device]; 278 int err; 279 280 mutex_lock(&dice->mutex); 281 err = snd_dice_stream_start_duplex(dice); 282 mutex_unlock(&dice->mutex); 283 if (err >= 0) 284 amdtp_stream_pcm_prepare(stream); 285 286 return 0; 287 } 288 static int playback_prepare(struct snd_pcm_substream *substream) 289 { 290 struct snd_dice *dice = substream->private_data; 291 struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device]; 292 int err; 293 294 mutex_lock(&dice->mutex); 295 err = snd_dice_stream_start_duplex(dice); 296 mutex_unlock(&dice->mutex); 297 if (err >= 0) 298 amdtp_stream_pcm_prepare(stream); 299 300 return err; 301 } 302 303 static int capture_trigger(struct snd_pcm_substream *substream, int cmd) 304 { 305 struct snd_dice *dice = substream->private_data; 306 struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device]; 307 308 switch (cmd) { 309 case SNDRV_PCM_TRIGGER_START: 310 amdtp_stream_pcm_trigger(stream, substream); 311 break; 312 case SNDRV_PCM_TRIGGER_STOP: 313 amdtp_stream_pcm_trigger(stream, NULL); 314 break; 315 default: 316 return -EINVAL; 317 } 318 319 return 0; 320 } 321 static int playback_trigger(struct snd_pcm_substream *substream, int cmd) 322 { 323 struct snd_dice *dice = substream->private_data; 324 struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device]; 325 326 switch (cmd) { 327 case SNDRV_PCM_TRIGGER_START: 328 amdtp_stream_pcm_trigger(stream, substream); 329 break; 330 case SNDRV_PCM_TRIGGER_STOP: 331 amdtp_stream_pcm_trigger(stream, NULL); 332 break; 333 default: 334 return -EINVAL; 335 } 336 337 return 0; 338 } 339 340 static snd_pcm_uframes_t capture_pointer(struct snd_pcm_substream *substream) 341 { 342 struct snd_dice *dice = substream->private_data; 343 struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device]; 344 345 return amdtp_stream_pcm_pointer(stream); 346 } 347 static snd_pcm_uframes_t playback_pointer(struct snd_pcm_substream *substream) 348 { 349 struct snd_dice *dice = substream->private_data; 350 struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device]; 351 352 return amdtp_stream_pcm_pointer(stream); 353 } 354 355 static int capture_ack(struct snd_pcm_substream *substream) 356 { 357 struct snd_dice *dice = substream->private_data; 358 struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device]; 359 360 return amdtp_stream_pcm_ack(stream); 361 } 362 363 static int playback_ack(struct snd_pcm_substream *substream) 364 { 365 struct snd_dice *dice = substream->private_data; 366 struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device]; 367 368 return amdtp_stream_pcm_ack(stream); 369 } 370 371 int snd_dice_create_pcm(struct snd_dice *dice) 372 { 373 static const struct snd_pcm_ops capture_ops = { 374 .open = pcm_open, 375 .close = pcm_close, 376 .ioctl = snd_pcm_lib_ioctl, 377 .hw_params = pcm_hw_params, 378 .hw_free = pcm_hw_free, 379 .prepare = capture_prepare, 380 .trigger = capture_trigger, 381 .pointer = capture_pointer, 382 .ack = capture_ack, 383 .page = snd_pcm_lib_get_vmalloc_page, 384 }; 385 static const struct snd_pcm_ops playback_ops = { 386 .open = pcm_open, 387 .close = pcm_close, 388 .ioctl = snd_pcm_lib_ioctl, 389 .hw_params = pcm_hw_params, 390 .hw_free = pcm_hw_free, 391 .prepare = playback_prepare, 392 .trigger = playback_trigger, 393 .pointer = playback_pointer, 394 .ack = playback_ack, 395 .page = snd_pcm_lib_get_vmalloc_page, 396 }; 397 struct snd_pcm *pcm; 398 unsigned int capture, playback; 399 int i, j; 400 int err; 401 402 for (i = 0; i < MAX_STREAMS; i++) { 403 capture = playback = 0; 404 for (j = 0; j < SND_DICE_RATE_MODE_COUNT; ++j) { 405 if (dice->tx_pcm_chs[i][j] > 0) 406 capture = 1; 407 if (dice->rx_pcm_chs[i][j] > 0) 408 playback = 1; 409 } 410 411 err = snd_pcm_new(dice->card, "DICE", i, playback, capture, 412 &pcm); 413 if (err < 0) 414 return err; 415 pcm->private_data = dice; 416 strcpy(pcm->name, dice->card->shortname); 417 418 if (capture > 0) 419 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, 420 &capture_ops); 421 422 if (playback > 0) 423 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, 424 &playback_ops); 425 } 426 427 return 0; 428 } 429