Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 1 | /* |
| 2 | * dice_stream.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 | |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 12 | #define CALLBACK_TIMEOUT 200 |
| 13 | |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 14 | const unsigned int snd_dice_rates[SND_DICE_RATES_COUNT] = { |
| 15 | /* mode 0 */ |
| 16 | [0] = 32000, |
| 17 | [1] = 44100, |
| 18 | [2] = 48000, |
| 19 | /* mode 1 */ |
| 20 | [3] = 88200, |
| 21 | [4] = 96000, |
| 22 | /* mode 2 */ |
| 23 | [5] = 176400, |
| 24 | [6] = 192000, |
| 25 | }; |
| 26 | |
| 27 | int snd_dice_stream_get_rate_mode(struct snd_dice *dice, unsigned int rate, |
| 28 | unsigned int *mode) |
| 29 | { |
| 30 | int i; |
| 31 | |
| 32 | for (i = 0; i < ARRAY_SIZE(snd_dice_rates); i++) { |
| 33 | if (!(dice->clock_caps & BIT(i))) |
| 34 | continue; |
| 35 | if (snd_dice_rates[i] != rate) |
| 36 | continue; |
| 37 | |
| 38 | *mode = (i - 1) / 2; |
| 39 | return 0; |
| 40 | } |
| 41 | return -EINVAL; |
| 42 | } |
| 43 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 44 | static void release_resources(struct snd_dice *dice, |
| 45 | struct fw_iso_resources *resources) |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 46 | { |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 47 | unsigned int channel; |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 48 | |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 49 | /* Reset channel number */ |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 50 | channel = cpu_to_be32((u32)-1); |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 51 | if (resources == &dice->tx_resources) |
| 52 | snd_dice_transaction_write_tx(dice, TX_ISOCHRONOUS, |
| 53 | &channel, 4); |
| 54 | else |
| 55 | snd_dice_transaction_write_rx(dice, RX_ISOCHRONOUS, |
| 56 | &channel, 4); |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 57 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 58 | fw_iso_resources_free(resources); |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 59 | } |
| 60 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 61 | static int keep_resources(struct snd_dice *dice, |
| 62 | struct fw_iso_resources *resources, |
| 63 | unsigned int max_payload_bytes) |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 64 | { |
| 65 | unsigned int channel; |
| 66 | int err; |
| 67 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 68 | err = fw_iso_resources_allocate(resources, max_payload_bytes, |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 69 | fw_parent_device(dice->unit)->max_speed); |
| 70 | if (err < 0) |
| 71 | goto end; |
| 72 | |
| 73 | /* Set channel number */ |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 74 | channel = cpu_to_be32(resources->channel); |
| 75 | if (resources == &dice->tx_resources) |
| 76 | err = snd_dice_transaction_write_tx(dice, TX_ISOCHRONOUS, |
| 77 | &channel, 4); |
| 78 | else |
| 79 | err = snd_dice_transaction_write_rx(dice, RX_ISOCHRONOUS, |
| 80 | &channel, 4); |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 81 | if (err < 0) |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 82 | release_resources(dice, resources); |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 83 | end: |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 84 | return err; |
| 85 | } |
| 86 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 87 | static void stop_stream(struct snd_dice *dice, struct amdtp_stream *stream) |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 88 | { |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 89 | amdtp_stream_pcm_abort(stream); |
| 90 | amdtp_stream_stop(stream); |
Takashi Sakamoto | c50fb91 | 2014-11-29 00:59:15 +0900 | [diff] [blame] | 91 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 92 | if (stream == &dice->tx_stream) |
| 93 | release_resources(dice, &dice->tx_resources); |
| 94 | else |
| 95 | release_resources(dice, &dice->rx_resources); |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 96 | } |
| 97 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 98 | static int start_stream(struct snd_dice *dice, struct amdtp_stream *stream, |
| 99 | unsigned int rate) |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 100 | { |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 101 | struct fw_iso_resources *resources; |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 102 | unsigned int i, mode, pcm_chs, midi_ports; |
Takashi Sakamoto | 27ec83b | 2015-09-19 11:21:50 +0900 | [diff] [blame] | 103 | bool double_pcm_frames; |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 104 | int err; |
| 105 | |
| 106 | err = snd_dice_stream_get_rate_mode(dice, rate, &mode); |
| 107 | if (err < 0) |
| 108 | goto end; |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 109 | if (stream == &dice->tx_stream) { |
| 110 | resources = &dice->tx_resources; |
| 111 | pcm_chs = dice->tx_channels[mode]; |
| 112 | midi_ports = dice->tx_midi_ports[mode]; |
| 113 | } else { |
| 114 | resources = &dice->rx_resources; |
| 115 | pcm_chs = dice->rx_channels[mode]; |
| 116 | midi_ports = dice->rx_midi_ports[mode]; |
| 117 | } |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * At 176.4/192.0 kHz, Dice has a quirk to transfer two PCM frames in |
| 121 | * one data block of AMDTP packet. Thus sampling transfer frequency is |
| 122 | * a half of PCM sampling frequency, i.e. PCM frames at 192.0 kHz are |
| 123 | * transferred on AMDTP packets at 96 kHz. Two successive samples of a |
| 124 | * channel are stored consecutively in the packet. This quirk is called |
| 125 | * as 'Dual Wire'. |
| 126 | * For this quirk, blocking mode is required and PCM buffer size should |
| 127 | * be aligned to SYT_INTERVAL. |
| 128 | */ |
Takashi Sakamoto | 27ec83b | 2015-09-19 11:21:50 +0900 | [diff] [blame] | 129 | double_pcm_frames = mode > 1; |
| 130 | if (double_pcm_frames) { |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 131 | rate /= 2; |
| 132 | pcm_chs *= 2; |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 133 | } |
| 134 | |
Takashi Sakamoto | 51c29fd | 2015-09-19 11:21:56 +0900 | [diff] [blame^] | 135 | err = amdtp_am824_set_parameters(stream, rate, pcm_chs, midi_ports, |
| 136 | double_pcm_frames); |
Takashi Sakamoto | 547e631 | 2015-09-19 11:21:49 +0900 | [diff] [blame] | 137 | if (err < 0) |
| 138 | goto end; |
| 139 | |
Takashi Sakamoto | 27ec83b | 2015-09-19 11:21:50 +0900 | [diff] [blame] | 140 | if (double_pcm_frames) { |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 141 | pcm_chs /= 2; |
| 142 | |
| 143 | for (i = 0; i < pcm_chs; i++) { |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 144 | stream->pcm_positions[i] = i * 2; |
| 145 | stream->pcm_positions[i + pcm_chs] = i * 2 + 1; |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 149 | err = keep_resources(dice, resources, |
| 150 | amdtp_stream_get_max_payload(stream)); |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 151 | if (err < 0) { |
| 152 | dev_err(&dice->unit->device, |
| 153 | "fail to keep isochronous resources\n"); |
| 154 | goto end; |
| 155 | } |
| 156 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 157 | err = amdtp_stream_start(stream, resources->channel, |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 158 | fw_parent_device(dice->unit)->max_speed); |
| 159 | if (err < 0) |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 160 | release_resources(dice, resources); |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 161 | end: |
| 162 | return err; |
| 163 | } |
| 164 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 165 | static int get_sync_mode(struct snd_dice *dice, enum cip_flags *sync_mode) |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 166 | { |
Takashi Sakamoto | 8fc01fc | 2014-12-09 00:10:37 +0900 | [diff] [blame] | 167 | u32 source; |
| 168 | int err; |
| 169 | |
| 170 | err = snd_dice_transaction_get_clock_source(dice, &source); |
| 171 | if (err < 0) |
| 172 | goto end; |
| 173 | |
| 174 | switch (source) { |
| 175 | /* So-called 'SYT Match' modes, sync_to_syt value of packets received */ |
| 176 | case CLOCK_SOURCE_ARX4: /* in 4th stream */ |
| 177 | case CLOCK_SOURCE_ARX3: /* in 3rd stream */ |
| 178 | case CLOCK_SOURCE_ARX2: /* in 2nd stream */ |
| 179 | err = -ENOSYS; |
| 180 | break; |
| 181 | case CLOCK_SOURCE_ARX1: /* in 1st stream, which this driver uses */ |
| 182 | *sync_mode = 0; |
| 183 | break; |
| 184 | default: |
| 185 | *sync_mode = CIP_SYNC_TO_DEVICE; |
| 186 | break; |
| 187 | } |
| 188 | end: |
| 189 | return err; |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | int snd_dice_stream_start_duplex(struct snd_dice *dice, unsigned int rate) |
| 193 | { |
| 194 | struct amdtp_stream *master, *slave; |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 195 | unsigned int curr_rate; |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 196 | enum cip_flags sync_mode; |
| 197 | int err = 0; |
| 198 | |
| 199 | if (dice->substreams_counter == 0) |
| 200 | goto end; |
| 201 | |
| 202 | err = get_sync_mode(dice, &sync_mode); |
| 203 | if (err < 0) |
| 204 | goto end; |
| 205 | if (sync_mode == CIP_SYNC_TO_DEVICE) { |
| 206 | master = &dice->tx_stream; |
| 207 | slave = &dice->rx_stream; |
| 208 | } else { |
| 209 | master = &dice->rx_stream; |
| 210 | slave = &dice->tx_stream; |
| 211 | } |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 212 | |
| 213 | /* Some packet queueing errors. */ |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 214 | if (amdtp_streaming_error(master) || amdtp_streaming_error(slave)) |
| 215 | stop_stream(dice, master); |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 216 | |
| 217 | /* Stop stream if rate is different. */ |
| 218 | err = snd_dice_transaction_get_rate(dice, &curr_rate); |
| 219 | if (err < 0) { |
| 220 | dev_err(&dice->unit->device, |
| 221 | "fail to get sampling rate\n"); |
| 222 | goto end; |
| 223 | } |
Takashi Sakamoto | a113ff8 | 2014-12-09 00:10:39 +0900 | [diff] [blame] | 224 | if (rate == 0) |
| 225 | rate = curr_rate; |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 226 | if (rate != curr_rate) |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 227 | stop_stream(dice, master); |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 228 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 229 | if (!amdtp_stream_running(master)) { |
| 230 | stop_stream(dice, slave); |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 231 | snd_dice_transaction_clear_enable(dice); |
| 232 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 233 | amdtp_stream_set_sync(sync_mode, master, slave); |
| 234 | |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 235 | err = snd_dice_transaction_set_rate(dice, rate); |
| 236 | if (err < 0) { |
| 237 | dev_err(&dice->unit->device, |
| 238 | "fail to set sampling rate\n"); |
| 239 | goto end; |
| 240 | } |
| 241 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 242 | /* Start both streams. */ |
| 243 | err = start_stream(dice, master, rate); |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 244 | if (err < 0) { |
| 245 | dev_err(&dice->unit->device, |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 246 | "fail to start AMDTP master stream\n"); |
| 247 | goto end; |
| 248 | } |
| 249 | err = start_stream(dice, slave, rate); |
| 250 | if (err < 0) { |
| 251 | dev_err(&dice->unit->device, |
| 252 | "fail to start AMDTP slave stream\n"); |
| 253 | stop_stream(dice, master); |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 254 | goto end; |
| 255 | } |
| 256 | err = snd_dice_transaction_set_enable(dice); |
| 257 | if (err < 0) { |
| 258 | dev_err(&dice->unit->device, |
| 259 | "fail to enable interface\n"); |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 260 | stop_stream(dice, master); |
| 261 | stop_stream(dice, slave); |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 262 | goto end; |
| 263 | } |
| 264 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 265 | /* Wait first callbacks */ |
| 266 | if (!amdtp_stream_wait_callback(master, CALLBACK_TIMEOUT) || |
| 267 | !amdtp_stream_wait_callback(slave, CALLBACK_TIMEOUT)) { |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 268 | snd_dice_transaction_clear_enable(dice); |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 269 | stop_stream(dice, master); |
| 270 | stop_stream(dice, slave); |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 271 | err = -ETIMEDOUT; |
| 272 | } |
| 273 | } |
| 274 | end: |
| 275 | return err; |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 276 | } |
| 277 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 278 | void snd_dice_stream_stop_duplex(struct snd_dice *dice) |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 279 | { |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 280 | if (dice->substreams_counter > 0) |
| 281 | return; |
| 282 | |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 283 | snd_dice_transaction_clear_enable(dice); |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 284 | |
| 285 | stop_stream(dice, &dice->tx_stream); |
| 286 | stop_stream(dice, &dice->rx_stream); |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 287 | } |
| 288 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 289 | static int init_stream(struct snd_dice *dice, struct amdtp_stream *stream) |
| 290 | { |
| 291 | int err; |
| 292 | struct fw_iso_resources *resources; |
| 293 | enum amdtp_stream_direction dir; |
| 294 | |
| 295 | if (stream == &dice->tx_stream) { |
| 296 | resources = &dice->tx_resources; |
| 297 | dir = AMDTP_IN_STREAM; |
| 298 | } else { |
| 299 | resources = &dice->rx_resources; |
| 300 | dir = AMDTP_OUT_STREAM; |
| 301 | } |
| 302 | |
| 303 | err = fw_iso_resources_init(resources, dice->unit); |
| 304 | if (err < 0) |
| 305 | goto end; |
| 306 | resources->channels_mask = 0x00000000ffffffffuLL; |
| 307 | |
Takashi Sakamoto | 5955815 | 2015-09-19 11:21:55 +0900 | [diff] [blame] | 308 | err = amdtp_am824_init(stream, dice->unit, dir, CIP_BLOCKING); |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 309 | if (err < 0) { |
| 310 | amdtp_stream_destroy(stream); |
| 311 | fw_iso_resources_destroy(resources); |
| 312 | } |
| 313 | end: |
| 314 | return err; |
| 315 | } |
| 316 | |
Takashi Sakamoto | d23c2cc | 2015-02-21 23:54:59 +0900 | [diff] [blame] | 317 | /* |
| 318 | * This function should be called before starting streams or after stopping |
| 319 | * streams. |
| 320 | */ |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 321 | static void destroy_stream(struct snd_dice *dice, struct amdtp_stream *stream) |
| 322 | { |
Takashi Sakamoto | d23c2cc | 2015-02-21 23:54:59 +0900 | [diff] [blame] | 323 | struct fw_iso_resources *resources; |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 324 | |
| 325 | if (stream == &dice->tx_stream) |
Takashi Sakamoto | d23c2cc | 2015-02-21 23:54:59 +0900 | [diff] [blame] | 326 | resources = &dice->tx_resources; |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 327 | else |
Takashi Sakamoto | d23c2cc | 2015-02-21 23:54:59 +0900 | [diff] [blame] | 328 | resources = &dice->rx_resources; |
| 329 | |
| 330 | amdtp_stream_destroy(stream); |
| 331 | fw_iso_resources_destroy(resources); |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | int snd_dice_stream_init_duplex(struct snd_dice *dice) |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 335 | { |
| 336 | int err; |
| 337 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 338 | dice->substreams_counter = 0; |
| 339 | |
| 340 | err = init_stream(dice, &dice->tx_stream); |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 341 | if (err < 0) |
| 342 | goto end; |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 343 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 344 | err = init_stream(dice, &dice->rx_stream); |
Takashi Sakamoto | d23c2cc | 2015-02-21 23:54:59 +0900 | [diff] [blame] | 345 | if (err < 0) |
| 346 | destroy_stream(dice, &dice->tx_stream); |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 347 | end: |
| 348 | return err; |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 349 | } |
| 350 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 351 | void snd_dice_stream_destroy_duplex(struct snd_dice *dice) |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 352 | { |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 353 | snd_dice_transaction_clear_enable(dice); |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 354 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 355 | destroy_stream(dice, &dice->tx_stream); |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 356 | destroy_stream(dice, &dice->rx_stream); |
| 357 | |
| 358 | dice->substreams_counter = 0; |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 359 | } |
| 360 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 361 | void snd_dice_stream_update_duplex(struct snd_dice *dice) |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 362 | { |
| 363 | /* |
| 364 | * On a bus reset, the DICE firmware disables streaming and then goes |
| 365 | * off contemplating its own navel for hundreds of milliseconds before |
| 366 | * it can react to any of our attempts to reenable streaming. This |
| 367 | * means that we lose synchronization anyway, so we force our streams |
| 368 | * to stop so that the application can restart them in an orderly |
| 369 | * manner. |
| 370 | */ |
| 371 | dice->global_enabled = false; |
| 372 | |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 373 | stop_stream(dice, &dice->rx_stream); |
| 374 | stop_stream(dice, &dice->tx_stream); |
Takashi Sakamoto | 288a8d0 | 2014-12-09 00:10:35 +0900 | [diff] [blame] | 375 | |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 376 | fw_iso_resources_update(&dice->rx_resources); |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 377 | fw_iso_resources_update(&dice->tx_resources); |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static void dice_lock_changed(struct snd_dice *dice) |
| 381 | { |
| 382 | dice->dev_lock_changed = true; |
| 383 | wake_up(&dice->hwdep_wait); |
| 384 | } |
| 385 | |
| 386 | int snd_dice_stream_lock_try(struct snd_dice *dice) |
| 387 | { |
| 388 | int err; |
| 389 | |
| 390 | spin_lock_irq(&dice->lock); |
| 391 | |
| 392 | if (dice->dev_lock_count < 0) { |
| 393 | err = -EBUSY; |
| 394 | goto out; |
| 395 | } |
| 396 | |
| 397 | if (dice->dev_lock_count++ == 0) |
| 398 | dice_lock_changed(dice); |
| 399 | err = 0; |
| 400 | out: |
| 401 | spin_unlock_irq(&dice->lock); |
| 402 | return err; |
| 403 | } |
| 404 | |
| 405 | void snd_dice_stream_lock_release(struct snd_dice *dice) |
| 406 | { |
| 407 | spin_lock_irq(&dice->lock); |
| 408 | |
| 409 | if (WARN_ON(dice->dev_lock_count <= 0)) |
| 410 | goto out; |
| 411 | |
| 412 | if (--dice->dev_lock_count == 0) |
| 413 | dice_lock_changed(dice); |
| 414 | out: |
| 415 | spin_unlock_irq(&dice->lock); |
| 416 | } |