2 * dice_stream.c - a part of driver for DICE based devices
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp>
7 * Licensed under the terms of the GNU General Public License, version 2.
12 #define CALLBACK_TIMEOUT 200
14 const unsigned int snd_dice_rates[SND_DICE_RATES_COUNT] = {
27 int snd_dice_stream_get_rate_mode(struct snd_dice *dice, unsigned int rate,
32 for (i = 0; i < ARRAY_SIZE(snd_dice_rates); i++) {
33 if (!(dice->clock_caps & BIT(i)))
35 if (snd_dice_rates[i] != rate)
44 static void release_resources(struct snd_dice *dice,
45 struct fw_iso_resources *resources)
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, sizeof(channel));
55 snd_dice_transaction_write_rx(dice, RX_ISOCHRONOUS,
56 &channel, sizeof(channel));
58 fw_iso_resources_free(resources);
61 static int keep_resources(struct snd_dice *dice,
62 struct fw_iso_resources *resources,
63 unsigned int max_payload_bytes)
68 err = fw_iso_resources_allocate(resources, max_payload_bytes,
69 fw_parent_device(dice->unit)->max_speed);
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, sizeof(channel));
79 err = snd_dice_transaction_write_rx(dice, RX_ISOCHRONOUS,
80 &channel, sizeof(channel));
82 release_resources(dice, resources);
87 static void stop_stream(struct snd_dice *dice, struct amdtp_stream *stream)
89 amdtp_stream_pcm_abort(stream);
90 amdtp_stream_stop(stream);
92 if (stream == &dice->tx_stream)
93 release_resources(dice, &dice->tx_resources);
95 release_resources(dice, &dice->rx_resources);
98 static int start_stream(struct snd_dice *dice, struct amdtp_stream *stream,
101 struct fw_iso_resources *resources;
102 unsigned int i, mode, pcm_chs, midi_ports;
103 bool double_pcm_frames;
106 err = snd_dice_stream_get_rate_mode(dice, rate, &mode);
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];
114 resources = &dice->rx_resources;
115 pcm_chs = dice->rx_channels[mode];
116 midi_ports = dice->rx_midi_ports[mode];
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
126 * For this quirk, blocking mode is required and PCM buffer size should
127 * be aligned to SYT_INTERVAL.
129 double_pcm_frames = mode > 1;
130 if (double_pcm_frames) {
135 err = amdtp_am824_set_parameters(stream, rate, pcm_chs, midi_ports,
140 if (double_pcm_frames) {
143 for (i = 0; i < pcm_chs; i++) {
144 amdtp_am824_set_pcm_position(stream, i, i * 2);
145 amdtp_am824_set_pcm_position(stream, i + pcm_chs,
150 err = keep_resources(dice, resources,
151 amdtp_stream_get_max_payload(stream));
153 dev_err(&dice->unit->device,
154 "fail to keep isochronous resources\n");
158 err = amdtp_stream_start(stream, resources->channel,
159 fw_parent_device(dice->unit)->max_speed);
161 release_resources(dice, resources);
166 static int get_sync_mode(struct snd_dice *dice, enum cip_flags *sync_mode)
171 err = snd_dice_transaction_get_clock_source(dice, &source);
176 /* So-called 'SYT Match' modes, sync_to_syt value of packets received */
177 case CLOCK_SOURCE_ARX4: /* in 4th stream */
178 case CLOCK_SOURCE_ARX3: /* in 3rd stream */
179 case CLOCK_SOURCE_ARX2: /* in 2nd stream */
182 case CLOCK_SOURCE_ARX1: /* in 1st stream, which this driver uses */
186 *sync_mode = CIP_SYNC_TO_DEVICE;
193 int snd_dice_stream_start_duplex(struct snd_dice *dice, unsigned int rate)
195 struct amdtp_stream *master, *slave;
196 unsigned int curr_rate;
197 enum cip_flags sync_mode;
200 if (dice->substreams_counter == 0)
203 err = get_sync_mode(dice, &sync_mode);
206 if (sync_mode == CIP_SYNC_TO_DEVICE) {
207 master = &dice->tx_stream;
208 slave = &dice->rx_stream;
210 master = &dice->rx_stream;
211 slave = &dice->tx_stream;
214 /* Some packet queueing errors. */
215 if (amdtp_streaming_error(master) || amdtp_streaming_error(slave))
216 stop_stream(dice, master);
218 /* Stop stream if rate is different. */
219 err = snd_dice_transaction_get_rate(dice, &curr_rate);
221 dev_err(&dice->unit->device,
222 "fail to get sampling rate\n");
227 if (rate != curr_rate)
228 stop_stream(dice, master);
230 if (!amdtp_stream_running(master)) {
231 stop_stream(dice, slave);
232 snd_dice_transaction_clear_enable(dice);
234 amdtp_stream_set_sync(sync_mode, master, slave);
236 err = snd_dice_transaction_set_rate(dice, rate);
238 dev_err(&dice->unit->device,
239 "fail to set sampling rate\n");
243 /* Start both streams. */
244 err = start_stream(dice, master, rate);
246 dev_err(&dice->unit->device,
247 "fail to start AMDTP master stream\n");
250 err = start_stream(dice, slave, rate);
252 dev_err(&dice->unit->device,
253 "fail to start AMDTP slave stream\n");
254 stop_stream(dice, master);
257 err = snd_dice_transaction_set_enable(dice);
259 dev_err(&dice->unit->device,
260 "fail to enable interface\n");
261 stop_stream(dice, master);
262 stop_stream(dice, slave);
266 /* Wait first callbacks */
267 if (!amdtp_stream_wait_callback(master, CALLBACK_TIMEOUT) ||
268 !amdtp_stream_wait_callback(slave, CALLBACK_TIMEOUT)) {
269 snd_dice_transaction_clear_enable(dice);
270 stop_stream(dice, master);
271 stop_stream(dice, slave);
279 void snd_dice_stream_stop_duplex(struct snd_dice *dice)
281 if (dice->substreams_counter > 0)
284 snd_dice_transaction_clear_enable(dice);
286 stop_stream(dice, &dice->tx_stream);
287 stop_stream(dice, &dice->rx_stream);
290 static int init_stream(struct snd_dice *dice, struct amdtp_stream *stream)
293 struct fw_iso_resources *resources;
294 enum amdtp_stream_direction dir;
296 if (stream == &dice->tx_stream) {
297 resources = &dice->tx_resources;
298 dir = AMDTP_IN_STREAM;
300 resources = &dice->rx_resources;
301 dir = AMDTP_OUT_STREAM;
304 err = fw_iso_resources_init(resources, dice->unit);
307 resources->channels_mask = 0x00000000ffffffffuLL;
309 err = amdtp_am824_init(stream, dice->unit, dir, CIP_BLOCKING);
311 amdtp_stream_destroy(stream);
312 fw_iso_resources_destroy(resources);
319 * This function should be called before starting streams or after stopping
322 static void destroy_stream(struct snd_dice *dice, struct amdtp_stream *stream)
324 struct fw_iso_resources *resources;
326 if (stream == &dice->tx_stream)
327 resources = &dice->tx_resources;
329 resources = &dice->rx_resources;
331 amdtp_stream_destroy(stream);
332 fw_iso_resources_destroy(resources);
335 int snd_dice_stream_init_duplex(struct snd_dice *dice)
339 dice->substreams_counter = 0;
341 err = init_stream(dice, &dice->tx_stream);
345 err = init_stream(dice, &dice->rx_stream);
347 destroy_stream(dice, &dice->tx_stream);
352 void snd_dice_stream_destroy_duplex(struct snd_dice *dice)
354 snd_dice_transaction_clear_enable(dice);
356 destroy_stream(dice, &dice->tx_stream);
357 destroy_stream(dice, &dice->rx_stream);
359 dice->substreams_counter = 0;
362 void snd_dice_stream_update_duplex(struct snd_dice *dice)
365 * On a bus reset, the DICE firmware disables streaming and then goes
366 * off contemplating its own navel for hundreds of milliseconds before
367 * it can react to any of our attempts to reenable streaming. This
368 * means that we lose synchronization anyway, so we force our streams
369 * to stop so that the application can restart them in an orderly
372 dice->global_enabled = false;
374 stop_stream(dice, &dice->rx_stream);
375 stop_stream(dice, &dice->tx_stream);
377 fw_iso_resources_update(&dice->rx_resources);
378 fw_iso_resources_update(&dice->tx_resources);
381 static void dice_lock_changed(struct snd_dice *dice)
383 dice->dev_lock_changed = true;
384 wake_up(&dice->hwdep_wait);
387 int snd_dice_stream_lock_try(struct snd_dice *dice)
391 spin_lock_irq(&dice->lock);
393 if (dice->dev_lock_count < 0) {
398 if (dice->dev_lock_count++ == 0)
399 dice_lock_changed(dice);
402 spin_unlock_irq(&dice->lock);
406 void snd_dice_stream_lock_release(struct snd_dice *dice)
408 spin_lock_irq(&dice->lock);
410 if (WARN_ON(dice->dev_lock_count <= 0))
413 if (--dice->dev_lock_count == 0)
414 dice_lock_changed(dice);
416 spin_unlock_irq(&dice->lock);