|
| 1 | +/* |
| 2 | + * tascam-pcm.c - a part of driver for TASCAM FireWire series |
| 3 | + * |
| 4 | + * Copyright (c) 2015 Takashi Sakamoto |
| 5 | + * |
| 6 | + * Licensed under the terms of the GNU General Public License, version 2. |
| 7 | + */ |
| 8 | + |
| 9 | +#include "tascam.h" |
| 10 | + |
| 11 | +static void set_buffer_params(struct snd_pcm_hardware *hw) |
| 12 | +{ |
| 13 | + hw->period_bytes_min = 4 * hw->channels_min; |
| 14 | + hw->period_bytes_max = hw->period_bytes_min * 2048; |
| 15 | + hw->buffer_bytes_max = hw->period_bytes_max * 2; |
| 16 | + |
| 17 | + hw->periods_min = 2; |
| 18 | + hw->periods_max = UINT_MAX; |
| 19 | +} |
| 20 | + |
| 21 | +static int pcm_init_hw_params(struct snd_tscm *tscm, |
| 22 | + struct snd_pcm_substream *substream) |
| 23 | +{ |
| 24 | + static const struct snd_pcm_hardware hardware = { |
| 25 | + .info = SNDRV_PCM_INFO_BATCH | |
| 26 | + SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 27 | + SNDRV_PCM_INFO_INTERLEAVED | |
| 28 | + SNDRV_PCM_INFO_JOINT_DUPLEX | |
| 29 | + SNDRV_PCM_INFO_MMAP | |
| 30 | + SNDRV_PCM_INFO_MMAP_VALID, |
| 31 | + .rates = SNDRV_PCM_RATE_44100 | |
| 32 | + SNDRV_PCM_RATE_48000 | |
| 33 | + SNDRV_PCM_RATE_88200 | |
| 34 | + SNDRV_PCM_RATE_96000, |
| 35 | + .rate_min = 44100, |
| 36 | + .rate_max = 96000, |
| 37 | + .channels_min = 10, |
| 38 | + .channels_max = 18, |
| 39 | + }; |
| 40 | + struct snd_pcm_runtime *runtime = substream->runtime; |
| 41 | + struct amdtp_stream *stream; |
| 42 | + unsigned int pcm_channels; |
| 43 | + |
| 44 | + runtime->hw = hardware; |
| 45 | + |
| 46 | + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 47 | + runtime->hw.formats = SNDRV_PCM_FMTBIT_S32; |
| 48 | + stream = &tscm->tx_stream; |
| 49 | + pcm_channels = tscm->spec->pcm_capture_analog_channels; |
| 50 | + } else { |
| 51 | + runtime->hw.formats = |
| 52 | + SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_S32; |
| 53 | + stream = &tscm->rx_stream; |
| 54 | + pcm_channels = tscm->spec->pcm_playback_analog_channels; |
| 55 | + } |
| 56 | + |
| 57 | + if (tscm->spec->has_adat) |
| 58 | + pcm_channels += 8; |
| 59 | + if (tscm->spec->has_spdif) |
| 60 | + pcm_channels += 2; |
| 61 | + runtime->hw.channels_min = runtime->hw.channels_max = pcm_channels; |
| 62 | + |
| 63 | + set_buffer_params(&runtime->hw); |
| 64 | + |
| 65 | + return amdtp_tscm_add_pcm_hw_constraints(stream, runtime); |
| 66 | +} |
| 67 | + |
| 68 | +static int pcm_open(struct snd_pcm_substream *substream) |
| 69 | +{ |
| 70 | + struct snd_tscm *tscm = substream->private_data; |
| 71 | + enum snd_tscm_clock clock; |
| 72 | + unsigned int rate; |
| 73 | + int err; |
| 74 | + |
| 75 | + err = pcm_init_hw_params(tscm, substream); |
| 76 | + if (err < 0) |
| 77 | + return err; |
| 78 | + |
| 79 | + err = snd_tscm_stream_get_clock(tscm, &clock); |
| 80 | + if (clock != SND_TSCM_CLOCK_INTERNAL || |
| 81 | + amdtp_stream_pcm_running(&tscm->rx_stream) || |
| 82 | + amdtp_stream_pcm_running(&tscm->tx_stream)) { |
| 83 | + err = snd_tscm_stream_get_rate(tscm, &rate); |
| 84 | + if (err < 0) |
| 85 | + return err; |
| 86 | + substream->runtime->hw.rate_min = rate; |
| 87 | + substream->runtime->hw.rate_max = rate; |
| 88 | + } |
| 89 | + |
| 90 | + snd_pcm_set_sync(substream); |
| 91 | + |
| 92 | + return err; |
| 93 | +} |
| 94 | + |
| 95 | +static int pcm_close(struct snd_pcm_substream *substream) |
| 96 | +{ |
| 97 | + return 0; |
| 98 | +} |
| 99 | + |
| 100 | +static int pcm_capture_hw_params(struct snd_pcm_substream *substream, |
| 101 | + struct snd_pcm_hw_params *hw_params) |
| 102 | +{ |
| 103 | + struct snd_tscm *tscm = substream->private_data; |
| 104 | + int err; |
| 105 | + |
| 106 | + err = snd_pcm_lib_alloc_vmalloc_buffer(substream, |
| 107 | + params_buffer_bytes(hw_params)); |
| 108 | + if (err < 0) |
| 109 | + return err; |
| 110 | + |
| 111 | + if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) { |
| 112 | + mutex_lock(&tscm->mutex); |
| 113 | + tscm->substreams_counter++; |
| 114 | + mutex_unlock(&tscm->mutex); |
| 115 | + } |
| 116 | + |
| 117 | + amdtp_tscm_set_pcm_format(&tscm->tx_stream, params_format(hw_params)); |
| 118 | + |
| 119 | + return 0; |
| 120 | +} |
| 121 | + |
| 122 | +static int pcm_playback_hw_params(struct snd_pcm_substream *substream, |
| 123 | + struct snd_pcm_hw_params *hw_params) |
| 124 | +{ |
| 125 | + struct snd_tscm *tscm = substream->private_data; |
| 126 | + int err; |
| 127 | + |
| 128 | + err = snd_pcm_lib_alloc_vmalloc_buffer(substream, |
| 129 | + params_buffer_bytes(hw_params)); |
| 130 | + if (err < 0) |
| 131 | + return err; |
| 132 | + |
| 133 | + if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) { |
| 134 | + mutex_lock(&tscm->mutex); |
| 135 | + tscm->substreams_counter++; |
| 136 | + mutex_unlock(&tscm->mutex); |
| 137 | + } |
| 138 | + |
| 139 | + amdtp_tscm_set_pcm_format(&tscm->rx_stream, params_format(hw_params)); |
| 140 | + |
| 141 | + return 0; |
| 142 | +} |
| 143 | + |
| 144 | +static int pcm_capture_hw_free(struct snd_pcm_substream *substream) |
| 145 | +{ |
| 146 | + struct snd_tscm *tscm = substream->private_data; |
| 147 | + |
| 148 | + mutex_lock(&tscm->mutex); |
| 149 | + |
| 150 | + if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) |
| 151 | + tscm->substreams_counter--; |
| 152 | + |
| 153 | + snd_tscm_stream_stop_duplex(tscm); |
| 154 | + |
| 155 | + mutex_unlock(&tscm->mutex); |
| 156 | + |
| 157 | + return snd_pcm_lib_free_vmalloc_buffer(substream); |
| 158 | +} |
| 159 | + |
| 160 | +static int pcm_playback_hw_free(struct snd_pcm_substream *substream) |
| 161 | +{ |
| 162 | + struct snd_tscm *tscm = substream->private_data; |
| 163 | + |
| 164 | + mutex_lock(&tscm->mutex); |
| 165 | + |
| 166 | + if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) |
| 167 | + tscm->substreams_counter--; |
| 168 | + |
| 169 | + snd_tscm_stream_stop_duplex(tscm); |
| 170 | + |
| 171 | + mutex_unlock(&tscm->mutex); |
| 172 | + |
| 173 | + return snd_pcm_lib_free_vmalloc_buffer(substream); |
| 174 | +} |
| 175 | + |
| 176 | +static int pcm_capture_prepare(struct snd_pcm_substream *substream) |
| 177 | +{ |
| 178 | + struct snd_tscm *tscm = substream->private_data; |
| 179 | + struct snd_pcm_runtime *runtime = substream->runtime; |
| 180 | + int err; |
| 181 | + |
| 182 | + mutex_lock(&tscm->mutex); |
| 183 | + |
| 184 | + err = snd_tscm_stream_start_duplex(tscm, runtime->rate); |
| 185 | + if (err >= 0) |
| 186 | + amdtp_stream_pcm_prepare(&tscm->tx_stream); |
| 187 | + |
| 188 | + mutex_unlock(&tscm->mutex); |
| 189 | + |
| 190 | + return err; |
| 191 | +} |
| 192 | + |
| 193 | +static int pcm_playback_prepare(struct snd_pcm_substream *substream) |
| 194 | +{ |
| 195 | + struct snd_tscm *tscm = substream->private_data; |
| 196 | + struct snd_pcm_runtime *runtime = substream->runtime; |
| 197 | + int err; |
| 198 | + |
| 199 | + mutex_lock(&tscm->mutex); |
| 200 | + |
| 201 | + err = snd_tscm_stream_start_duplex(tscm, runtime->rate); |
| 202 | + if (err >= 0) |
| 203 | + amdtp_stream_pcm_prepare(&tscm->rx_stream); |
| 204 | + |
| 205 | + mutex_unlock(&tscm->mutex); |
| 206 | + |
| 207 | + return err; |
| 208 | +} |
| 209 | + |
| 210 | +static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd) |
| 211 | +{ |
| 212 | + struct snd_tscm *tscm = substream->private_data; |
| 213 | + |
| 214 | + switch (cmd) { |
| 215 | + case SNDRV_PCM_TRIGGER_START: |
| 216 | + amdtp_stream_pcm_trigger(&tscm->tx_stream, substream); |
| 217 | + break; |
| 218 | + case SNDRV_PCM_TRIGGER_STOP: |
| 219 | + amdtp_stream_pcm_trigger(&tscm->tx_stream, NULL); |
| 220 | + break; |
| 221 | + default: |
| 222 | + return -EINVAL; |
| 223 | + } |
| 224 | + |
| 225 | + return 0; |
| 226 | +} |
| 227 | + |
| 228 | +static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd) |
| 229 | +{ |
| 230 | + struct snd_tscm *tscm = substream->private_data; |
| 231 | + |
| 232 | + switch (cmd) { |
| 233 | + case SNDRV_PCM_TRIGGER_START: |
| 234 | + amdtp_stream_pcm_trigger(&tscm->rx_stream, substream); |
| 235 | + break; |
| 236 | + case SNDRV_PCM_TRIGGER_STOP: |
| 237 | + amdtp_stream_pcm_trigger(&tscm->rx_stream, NULL); |
| 238 | + break; |
| 239 | + default: |
| 240 | + return -EINVAL; |
| 241 | + } |
| 242 | + |
| 243 | + return 0; |
| 244 | +} |
| 245 | + |
| 246 | +static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm) |
| 247 | +{ |
| 248 | + struct snd_tscm *tscm = sbstrm->private_data; |
| 249 | + |
| 250 | + return amdtp_stream_pcm_pointer(&tscm->tx_stream); |
| 251 | +} |
| 252 | + |
| 253 | +static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm) |
| 254 | +{ |
| 255 | + struct snd_tscm *tscm = sbstrm->private_data; |
| 256 | + |
| 257 | + return amdtp_stream_pcm_pointer(&tscm->rx_stream); |
| 258 | +} |
| 259 | + |
| 260 | +static struct snd_pcm_ops pcm_capture_ops = { |
| 261 | + .open = pcm_open, |
| 262 | + .close = pcm_close, |
| 263 | + .ioctl = snd_pcm_lib_ioctl, |
| 264 | + .hw_params = pcm_capture_hw_params, |
| 265 | + .hw_free = pcm_capture_hw_free, |
| 266 | + .prepare = pcm_capture_prepare, |
| 267 | + .trigger = pcm_capture_trigger, |
| 268 | + .pointer = pcm_capture_pointer, |
| 269 | + .page = snd_pcm_lib_get_vmalloc_page, |
| 270 | +}; |
| 271 | + |
| 272 | +static struct snd_pcm_ops pcm_playback_ops = { |
| 273 | + .open = pcm_open, |
| 274 | + .close = pcm_close, |
| 275 | + .ioctl = snd_pcm_lib_ioctl, |
| 276 | + .hw_params = pcm_playback_hw_params, |
| 277 | + .hw_free = pcm_playback_hw_free, |
| 278 | + .prepare = pcm_playback_prepare, |
| 279 | + .trigger = pcm_playback_trigger, |
| 280 | + .pointer = pcm_playback_pointer, |
| 281 | + .page = snd_pcm_lib_get_vmalloc_page, |
| 282 | + .mmap = snd_pcm_lib_mmap_vmalloc, |
| 283 | +}; |
| 284 | + |
| 285 | +int snd_tscm_create_pcm_devices(struct snd_tscm *tscm) |
| 286 | +{ |
| 287 | + struct snd_pcm *pcm; |
| 288 | + int err; |
| 289 | + |
| 290 | + err = snd_pcm_new(tscm->card, tscm->card->driver, 0, 1, 1, &pcm); |
| 291 | + if (err < 0) |
| 292 | + return err; |
| 293 | + |
| 294 | + pcm->private_data = tscm; |
| 295 | + snprintf(pcm->name, sizeof(pcm->name), |
| 296 | + "%s PCM", tscm->card->shortname); |
| 297 | + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops); |
| 298 | + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops); |
| 299 | + |
| 300 | + return 0; |
| 301 | +} |
0 commit comments