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