1 // SPDX-License-Identifier: GPL-2.0-only
3 * dice_stream.c - a part of driver for DICE based devices
5 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6 * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp>
11 #define READY_TIMEOUT_MS 200
12 #define NOTIFICATION_TIMEOUT_MS 100
19 const unsigned int snd_dice_rates
[SND_DICE_RATES_COUNT
] = {
32 int snd_dice_stream_get_rate_mode(struct snd_dice
*dice
, unsigned int rate
,
33 enum snd_dice_rate_mode
*mode
)
35 /* Corresponding to each entry in snd_dice_rates. */
36 static const enum snd_dice_rate_mode modes
[] = {
37 [0] = SND_DICE_RATE_MODE_LOW
,
38 [1] = SND_DICE_RATE_MODE_LOW
,
39 [2] = SND_DICE_RATE_MODE_LOW
,
40 [3] = SND_DICE_RATE_MODE_MIDDLE
,
41 [4] = SND_DICE_RATE_MODE_MIDDLE
,
42 [5] = SND_DICE_RATE_MODE_HIGH
,
43 [6] = SND_DICE_RATE_MODE_HIGH
,
47 for (i
= 0; i
< ARRAY_SIZE(snd_dice_rates
); i
++) {
48 if (!(dice
->clock_caps
& BIT(i
)))
50 if (snd_dice_rates
[i
] != rate
)
60 static int select_clock(struct snd_dice
*dice
, unsigned int rate
)
67 err
= snd_dice_transaction_read_global(dice
, GLOBAL_CLOCK_SELECT
,
72 data
= be32_to_cpu(reg
);
74 data
&= ~CLOCK_RATE_MASK
;
75 for (i
= 0; i
< ARRAY_SIZE(snd_dice_rates
); ++i
) {
76 if (snd_dice_rates
[i
] == rate
)
79 if (i
== ARRAY_SIZE(snd_dice_rates
))
81 data
|= i
<< CLOCK_RATE_SHIFT
;
83 if (completion_done(&dice
->clock_accepted
))
84 reinit_completion(&dice
->clock_accepted
);
86 new = cpu_to_be32(data
);
87 err
= snd_dice_transaction_write_global(dice
, GLOBAL_CLOCK_SELECT
,
92 if (wait_for_completion_timeout(&dice
->clock_accepted
,
93 msecs_to_jiffies(NOTIFICATION_TIMEOUT_MS
)) == 0) {
101 static int get_register_params(struct snd_dice
*dice
,
102 struct reg_params
*tx_params
,
103 struct reg_params
*rx_params
)
108 err
= snd_dice_transaction_read_tx(dice
, TX_NUMBER
, reg
, sizeof(reg
));
112 min_t(unsigned int, be32_to_cpu(reg
[0]), MAX_STREAMS
);
113 tx_params
->size
= be32_to_cpu(reg
[1]) * 4;
115 err
= snd_dice_transaction_read_rx(dice
, RX_NUMBER
, reg
, sizeof(reg
));
119 min_t(unsigned int, be32_to_cpu(reg
[0]), MAX_STREAMS
);
120 rx_params
->size
= be32_to_cpu(reg
[1]) * 4;
125 static void release_resources(struct snd_dice
*dice
)
129 for (i
= 0; i
< MAX_STREAMS
; ++i
) {
130 fw_iso_resources_free(&dice
->tx_resources
[i
]);
131 fw_iso_resources_free(&dice
->rx_resources
[i
]);
135 static void stop_streams(struct snd_dice
*dice
, enum amdtp_stream_direction dir
,
136 struct reg_params
*params
)
141 for (i
= 0; i
< params
->count
; i
++) {
142 reg
= cpu_to_be32((u32
)-1);
143 if (dir
== AMDTP_IN_STREAM
) {
144 snd_dice_transaction_write_tx(dice
,
145 params
->size
* i
+ TX_ISOCHRONOUS
,
148 snd_dice_transaction_write_rx(dice
,
149 params
->size
* i
+ RX_ISOCHRONOUS
,
155 static int keep_resources(struct snd_dice
*dice
, struct amdtp_stream
*stream
,
156 struct fw_iso_resources
*resources
, unsigned int rate
,
157 unsigned int pcm_chs
, unsigned int midi_ports
)
159 bool double_pcm_frames
;
163 // At 176.4/192.0 kHz, Dice has a quirk to transfer two PCM frames in
164 // one data block of AMDTP packet. Thus sampling transfer frequency is
165 // a half of PCM sampling frequency, i.e. PCM frames at 192.0 kHz are
166 // transferred on AMDTP packets at 96 kHz. Two successive samples of a
167 // channel are stored consecutively in the packet. This quirk is called
169 // For this quirk, blocking mode is required and PCM buffer size should
170 // be aligned to SYT_INTERVAL.
171 double_pcm_frames
= (rate
> 96000 && !dice
->disable_double_pcm_frames
);
172 if (double_pcm_frames
) {
177 err
= amdtp_am824_set_parameters(stream
, rate
, pcm_chs
, midi_ports
,
182 if (double_pcm_frames
) {
185 for (i
= 0; i
< pcm_chs
; i
++) {
186 amdtp_am824_set_pcm_position(stream
, i
, i
* 2);
187 amdtp_am824_set_pcm_position(stream
, i
+ pcm_chs
,
192 return fw_iso_resources_allocate(resources
,
193 amdtp_stream_get_max_payload(stream
),
194 fw_parent_device(dice
->unit
)->max_speed
);
197 static int keep_dual_resources(struct snd_dice
*dice
, unsigned int rate
,
198 enum amdtp_stream_direction dir
,
199 struct reg_params
*params
)
201 enum snd_dice_rate_mode mode
;
205 err
= snd_dice_stream_get_rate_mode(dice
, rate
, &mode
);
209 for (i
= 0; i
< params
->count
; ++i
) {
211 struct amdtp_stream
*stream
;
212 struct fw_iso_resources
*resources
;
213 unsigned int pcm_cache
;
214 unsigned int pcm_chs
;
215 unsigned int midi_ports
;
217 if (dir
== AMDTP_IN_STREAM
) {
218 stream
= &dice
->tx_stream
[i
];
219 resources
= &dice
->tx_resources
[i
];
221 pcm_cache
= dice
->tx_pcm_chs
[i
][mode
];
222 err
= snd_dice_transaction_read_tx(dice
,
223 params
->size
* i
+ TX_NUMBER_AUDIO
,
226 stream
= &dice
->rx_stream
[i
];
227 resources
= &dice
->rx_resources
[i
];
229 pcm_cache
= dice
->rx_pcm_chs
[i
][mode
];
230 err
= snd_dice_transaction_read_rx(dice
,
231 params
->size
* i
+ RX_NUMBER_AUDIO
,
236 pcm_chs
= be32_to_cpu(reg
[0]);
237 midi_ports
= be32_to_cpu(reg
[1]);
239 // These are important for developer of this driver.
240 if (pcm_chs
!= pcm_cache
) {
241 dev_info(&dice
->unit
->device
,
242 "cache mismatch: pcm: %u:%u, midi: %u\n",
243 pcm_chs
, pcm_cache
, midi_ports
);
247 err
= keep_resources(dice
, stream
, resources
, rate
, pcm_chs
,
256 static void finish_session(struct snd_dice
*dice
, struct reg_params
*tx_params
,
257 struct reg_params
*rx_params
)
259 stop_streams(dice
, AMDTP_IN_STREAM
, tx_params
);
260 stop_streams(dice
, AMDTP_OUT_STREAM
, rx_params
);
262 snd_dice_transaction_clear_enable(dice
);
265 int snd_dice_stream_reserve_duplex(struct snd_dice
*dice
, unsigned int rate
,
266 unsigned int events_per_period
,
267 unsigned int events_per_buffer
)
269 unsigned int curr_rate
;
272 // Check sampling transmission frequency.
273 err
= snd_dice_transaction_get_rate(dice
, &curr_rate
);
279 if (dice
->substreams_counter
== 0 || curr_rate
!= rate
) {
280 struct reg_params tx_params
, rx_params
;
282 amdtp_domain_stop(&dice
->domain
);
284 err
= get_register_params(dice
, &tx_params
, &rx_params
);
287 finish_session(dice
, &tx_params
, &rx_params
);
289 release_resources(dice
);
291 // Just after owning the unit (GLOBAL_OWNER), the unit can
292 // return invalid stream formats. Selecting clock parameters
293 // have an effect for the unit to refine it.
294 err
= select_clock(dice
, rate
);
298 // After changing sampling transfer frequency, the value of
299 // register can be changed.
300 err
= get_register_params(dice
, &tx_params
, &rx_params
);
304 err
= keep_dual_resources(dice
, rate
, AMDTP_IN_STREAM
,
309 err
= keep_dual_resources(dice
, rate
, AMDTP_OUT_STREAM
,
314 err
= amdtp_domain_set_events_per_period(&dice
->domain
,
315 events_per_period
, events_per_buffer
);
322 release_resources(dice
);
326 static int start_streams(struct snd_dice
*dice
, enum amdtp_stream_direction dir
,
327 unsigned int rate
, struct reg_params
*params
)
329 unsigned int max_speed
= fw_parent_device(dice
->unit
)->max_speed
;
333 for (i
= 0; i
< params
->count
; i
++) {
334 struct amdtp_stream
*stream
;
335 struct fw_iso_resources
*resources
;
338 if (dir
== AMDTP_IN_STREAM
) {
339 stream
= dice
->tx_stream
+ i
;
340 resources
= dice
->tx_resources
+ i
;
342 stream
= dice
->rx_stream
+ i
;
343 resources
= dice
->rx_resources
+ i
;
346 reg
= cpu_to_be32(resources
->channel
);
347 if (dir
== AMDTP_IN_STREAM
) {
348 err
= snd_dice_transaction_write_tx(dice
,
349 params
->size
* i
+ TX_ISOCHRONOUS
,
352 err
= snd_dice_transaction_write_rx(dice
,
353 params
->size
* i
+ RX_ISOCHRONOUS
,
359 if (dir
== AMDTP_IN_STREAM
) {
360 reg
= cpu_to_be32(max_speed
);
361 err
= snd_dice_transaction_write_tx(dice
,
362 params
->size
* i
+ TX_SPEED
,
368 err
= amdtp_domain_add_stream(&dice
->domain
, stream
,
369 resources
->channel
, max_speed
);
378 * MEMO: After this function, there're two states of streams:
379 * - None streams are running.
380 * - All streams are running.
382 int snd_dice_stream_start_duplex(struct snd_dice
*dice
)
384 unsigned int generation
= dice
->rx_resources
[0].generation
;
385 struct reg_params tx_params
, rx_params
;
388 enum snd_dice_rate_mode mode
;
391 if (dice
->substreams_counter
== 0)
394 err
= get_register_params(dice
, &tx_params
, &rx_params
);
398 // Check error of packet streaming.
399 for (i
= 0; i
< MAX_STREAMS
; ++i
) {
400 if (amdtp_streaming_error(&dice
->tx_stream
[i
]) ||
401 amdtp_streaming_error(&dice
->rx_stream
[i
])) {
402 amdtp_domain_stop(&dice
->domain
);
403 finish_session(dice
, &tx_params
, &rx_params
);
408 if (generation
!= fw_parent_device(dice
->unit
)->card
->generation
) {
409 for (i
= 0; i
< MAX_STREAMS
; ++i
) {
410 if (i
< tx_params
.count
)
411 fw_iso_resources_update(dice
->tx_resources
+ i
);
412 if (i
< rx_params
.count
)
413 fw_iso_resources_update(dice
->rx_resources
+ i
);
417 // Check required streams are running or not.
418 err
= snd_dice_transaction_get_rate(dice
, &rate
);
421 err
= snd_dice_stream_get_rate_mode(dice
, rate
, &mode
);
424 for (i
= 0; i
< MAX_STREAMS
; ++i
) {
425 if (dice
->tx_pcm_chs
[i
][mode
] > 0 &&
426 !amdtp_stream_running(&dice
->tx_stream
[i
]))
428 if (dice
->rx_pcm_chs
[i
][mode
] > 0 &&
429 !amdtp_stream_running(&dice
->rx_stream
[i
]))
432 if (i
< MAX_STREAMS
) {
433 // Start both streams.
434 err
= start_streams(dice
, AMDTP_IN_STREAM
, rate
, &tx_params
);
438 err
= start_streams(dice
, AMDTP_OUT_STREAM
, rate
, &rx_params
);
442 err
= snd_dice_transaction_set_enable(dice
);
444 dev_err(&dice
->unit
->device
,
445 "fail to enable interface\n");
449 // MEMO: The device immediately starts packet transmission when enabled. Some
450 // devices are strictly to generate any discontinuity in the sequence of tx packet
451 // when they receives invalid sequence of presentation time in CIP header. The
452 // sequence replay for media clock recovery can suppress the behaviour.
453 err
= amdtp_domain_start(&dice
->domain
, 0, true, false);
457 if (!amdtp_domain_wait_ready(&dice
->domain
, READY_TIMEOUT_MS
)) {
465 amdtp_domain_stop(&dice
->domain
);
466 finish_session(dice
, &tx_params
, &rx_params
);
471 * MEMO: After this function, there're two states of streams:
472 * - None streams are running.
473 * - All streams are running.
475 void snd_dice_stream_stop_duplex(struct snd_dice
*dice
)
477 struct reg_params tx_params
, rx_params
;
479 if (dice
->substreams_counter
== 0) {
480 if (get_register_params(dice
, &tx_params
, &rx_params
) >= 0)
481 finish_session(dice
, &tx_params
, &rx_params
);
483 amdtp_domain_stop(&dice
->domain
);
484 release_resources(dice
);
488 static int init_stream(struct snd_dice
*dice
, enum amdtp_stream_direction dir
,
491 struct amdtp_stream
*stream
;
492 struct fw_iso_resources
*resources
;
495 if (dir
== AMDTP_IN_STREAM
) {
496 stream
= &dice
->tx_stream
[index
];
497 resources
= &dice
->tx_resources
[index
];
499 stream
= &dice
->rx_stream
[index
];
500 resources
= &dice
->rx_resources
[index
];
503 err
= fw_iso_resources_init(resources
, dice
->unit
);
506 resources
->channels_mask
= 0x00000000ffffffffuLL
;
508 err
= amdtp_am824_init(stream
, dice
->unit
, dir
, CIP_BLOCKING
);
510 amdtp_stream_destroy(stream
);
511 fw_iso_resources_destroy(resources
);
518 * This function should be called before starting streams or after stopping
521 static void destroy_stream(struct snd_dice
*dice
,
522 enum amdtp_stream_direction dir
,
525 struct amdtp_stream
*stream
;
526 struct fw_iso_resources
*resources
;
528 if (dir
== AMDTP_IN_STREAM
) {
529 stream
= &dice
->tx_stream
[index
];
530 resources
= &dice
->tx_resources
[index
];
532 stream
= &dice
->rx_stream
[index
];
533 resources
= &dice
->rx_resources
[index
];
536 amdtp_stream_destroy(stream
);
537 fw_iso_resources_destroy(resources
);
540 int snd_dice_stream_init_duplex(struct snd_dice
*dice
)
544 for (i
= 0; i
< MAX_STREAMS
; i
++) {
545 err
= init_stream(dice
, AMDTP_IN_STREAM
, i
);
548 destroy_stream(dice
, AMDTP_IN_STREAM
, i
);
553 for (i
= 0; i
< MAX_STREAMS
; i
++) {
554 err
= init_stream(dice
, AMDTP_OUT_STREAM
, i
);
557 destroy_stream(dice
, AMDTP_OUT_STREAM
, i
);
558 for (i
= 0; i
< MAX_STREAMS
; i
++)
559 destroy_stream(dice
, AMDTP_IN_STREAM
, i
);
564 err
= amdtp_domain_init(&dice
->domain
);
566 for (i
= 0; i
< MAX_STREAMS
; ++i
) {
567 destroy_stream(dice
, AMDTP_OUT_STREAM
, i
);
568 destroy_stream(dice
, AMDTP_IN_STREAM
, i
);
575 void snd_dice_stream_destroy_duplex(struct snd_dice
*dice
)
579 for (i
= 0; i
< MAX_STREAMS
; i
++) {
580 destroy_stream(dice
, AMDTP_IN_STREAM
, i
);
581 destroy_stream(dice
, AMDTP_OUT_STREAM
, i
);
584 amdtp_domain_destroy(&dice
->domain
);
587 void snd_dice_stream_update_duplex(struct snd_dice
*dice
)
589 struct reg_params tx_params
, rx_params
;
592 * On a bus reset, the DICE firmware disables streaming and then goes
593 * off contemplating its own navel for hundreds of milliseconds before
594 * it can react to any of our attempts to reenable streaming. This
595 * means that we lose synchronization anyway, so we force our streams
596 * to stop so that the application can restart them in an orderly
599 dice
->global_enabled
= false;
601 if (get_register_params(dice
, &tx_params
, &rx_params
) == 0) {
602 amdtp_domain_stop(&dice
->domain
);
604 stop_streams(dice
, AMDTP_IN_STREAM
, &tx_params
);
605 stop_streams(dice
, AMDTP_OUT_STREAM
, &rx_params
);
609 int snd_dice_stream_detect_current_formats(struct snd_dice
*dice
)
612 enum snd_dice_rate_mode mode
;
614 struct reg_params tx_params
, rx_params
;
618 /* If extended protocol is available, detect detail spec. */
619 err
= snd_dice_detect_extension_formats(dice
);
624 * Available stream format is restricted at current mode of sampling
627 err
= snd_dice_transaction_get_rate(dice
, &rate
);
631 err
= snd_dice_stream_get_rate_mode(dice
, rate
, &mode
);
636 * Just after owning the unit (GLOBAL_OWNER), the unit can return
637 * invalid stream formats. Selecting clock parameters have an effect
638 * for the unit to refine it.
640 err
= select_clock(dice
, rate
);
644 err
= get_register_params(dice
, &tx_params
, &rx_params
);
648 for (i
= 0; i
< tx_params
.count
; ++i
) {
649 err
= snd_dice_transaction_read_tx(dice
,
650 tx_params
.size
* i
+ TX_NUMBER_AUDIO
,
654 dice
->tx_pcm_chs
[i
][mode
] = be32_to_cpu(reg
[0]);
655 dice
->tx_midi_ports
[i
] = max_t(unsigned int,
656 be32_to_cpu(reg
[1]), dice
->tx_midi_ports
[i
]);
658 for (i
= 0; i
< rx_params
.count
; ++i
) {
659 err
= snd_dice_transaction_read_rx(dice
,
660 rx_params
.size
* i
+ RX_NUMBER_AUDIO
,
664 dice
->rx_pcm_chs
[i
][mode
] = be32_to_cpu(reg
[0]);
665 dice
->rx_midi_ports
[i
] = max_t(unsigned int,
666 be32_to_cpu(reg
[1]), dice
->rx_midi_ports
[i
]);
672 static void dice_lock_changed(struct snd_dice
*dice
)
674 dice
->dev_lock_changed
= true;
675 wake_up(&dice
->hwdep_wait
);
678 int snd_dice_stream_lock_try(struct snd_dice
*dice
)
682 spin_lock_irq(&dice
->lock
);
684 if (dice
->dev_lock_count
< 0) {
689 if (dice
->dev_lock_count
++ == 0)
690 dice_lock_changed(dice
);
693 spin_unlock_irq(&dice
->lock
);
697 void snd_dice_stream_lock_release(struct snd_dice
*dice
)
699 spin_lock_irq(&dice
->lock
);
701 if (WARN_ON(dice
->dev_lock_count
<= 0))
704 if (--dice
->dev_lock_count
== 0)
705 dice_lock_changed(dice
);
707 spin_unlock_irq(&dice
->lock
);