1 // SPDX-License-Identifier: GPL-2.0-only
3 * tascam-stream.c - a part of driver for TASCAM FireWire series
5 * Copyright (c) 2015 Takashi Sakamoto
8 #include <linux/delay.h>
11 #define CLOCK_STATUS_MASK 0xffff0000
12 #define CLOCK_CONFIG_MASK 0x0000ffff
14 #define CALLBACK_TIMEOUT 500
16 static int get_clock(struct snd_tscm
*tscm
, u32
*data
)
23 err
= snd_fw_transaction(tscm
->unit
, TCODE_READ_QUADLET_REQUEST
,
24 TSCM_ADDR_BASE
+ TSCM_OFFSET_CLOCK_STATUS
,
25 ®
, sizeof(reg
), 0);
29 *data
= be32_to_cpu(reg
);
30 if (*data
& CLOCK_STATUS_MASK
)
33 // In intermediate state after changing clock status.
37 // Still in the intermediate state.
44 static int set_clock(struct snd_tscm
*tscm
, unsigned int rate
,
45 enum snd_tscm_clock clock
)
51 err
= get_clock(tscm
, &data
);
54 data
&= CLOCK_CONFIG_MASK
;
59 if ((rate
% 44100) == 0) {
62 if (rate
/ 44100 == 2)
64 } else if ((rate
% 48000) == 0) {
67 if (rate
/ 48000 == 2)
74 if (clock
!= INT_MAX
) {
79 reg
= cpu_to_be32(data
);
81 err
= snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
82 TSCM_ADDR_BASE
+ TSCM_OFFSET_CLOCK_STATUS
,
83 ®
, sizeof(reg
), 0);
87 if (data
& 0x00008000)
88 reg
= cpu_to_be32(0x0000001a);
90 reg
= cpu_to_be32(0x0000000d);
92 return snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
93 TSCM_ADDR_BASE
+ TSCM_OFFSET_MULTIPLEX_MODE
,
94 ®
, sizeof(reg
), 0);
97 int snd_tscm_stream_get_rate(struct snd_tscm
*tscm
, unsigned int *rate
)
102 err
= get_clock(tscm
, &data
);
106 data
= (data
& 0xff000000) >> 24;
108 /* Check base rate. */
109 if ((data
& 0x0f) == 0x01)
111 else if ((data
& 0x0f) == 0x02)
116 /* Check multiplier. */
117 if ((data
& 0xf0) == 0x80)
119 else if ((data
& 0xf0) != 0x00)
125 int snd_tscm_stream_get_clock(struct snd_tscm
*tscm
, enum snd_tscm_clock
*clock
)
130 err
= get_clock(tscm
, &data
);
134 *clock
= ((data
& 0x00ff0000) >> 16) - 1;
135 if (*clock
< 0 || *clock
> SND_TSCM_CLOCK_ADAT
)
141 static int enable_data_channels(struct snd_tscm
*tscm
)
149 for (i
= 0; i
< tscm
->spec
->pcm_capture_analog_channels
; ++i
)
151 if (tscm
->spec
->has_adat
)
153 if (tscm
->spec
->has_spdif
)
156 reg
= cpu_to_be32(data
);
157 err
= snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
158 TSCM_ADDR_BASE
+ TSCM_OFFSET_TX_PCM_CHANNELS
,
159 ®
, sizeof(reg
), 0);
164 for (i
= 0; i
< tscm
->spec
->pcm_playback_analog_channels
; ++i
)
166 if (tscm
->spec
->has_adat
)
168 if (tscm
->spec
->has_spdif
)
171 reg
= cpu_to_be32(data
);
172 return snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
173 TSCM_ADDR_BASE
+ TSCM_OFFSET_RX_PCM_CHANNELS
,
174 ®
, sizeof(reg
), 0);
177 static int set_stream_formats(struct snd_tscm
*tscm
, unsigned int rate
)
182 // Set an option for unknown purpose.
183 reg
= cpu_to_be32(0x00200000);
184 err
= snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
185 TSCM_ADDR_BASE
+ TSCM_OFFSET_SET_OPTION
,
186 ®
, sizeof(reg
), 0);
190 return enable_data_channels(tscm
);
193 static void finish_session(struct snd_tscm
*tscm
)
198 snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
199 TSCM_ADDR_BASE
+ TSCM_OFFSET_START_STREAMING
,
200 ®
, sizeof(reg
), 0);
203 snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
204 TSCM_ADDR_BASE
+ TSCM_OFFSET_ISOC_RX_ON
,
205 ®
, sizeof(reg
), 0);
207 // Unregister channels.
208 reg
= cpu_to_be32(0x00000000);
209 snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
210 TSCM_ADDR_BASE
+ TSCM_OFFSET_ISOC_TX_CH
,
211 ®
, sizeof(reg
), 0);
212 reg
= cpu_to_be32(0x00000000);
213 snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
214 TSCM_ADDR_BASE
+ TSCM_OFFSET_UNKNOWN
,
215 ®
, sizeof(reg
), 0);
216 reg
= cpu_to_be32(0x00000000);
217 snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
218 TSCM_ADDR_BASE
+ TSCM_OFFSET_ISOC_RX_CH
,
219 ®
, sizeof(reg
), 0);
222 static int begin_session(struct snd_tscm
*tscm
)
227 // Register the isochronous channel for transmitting stream.
228 reg
= cpu_to_be32(tscm
->tx_resources
.channel
);
229 err
= snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
230 TSCM_ADDR_BASE
+ TSCM_OFFSET_ISOC_TX_CH
,
231 ®
, sizeof(reg
), 0);
236 reg
= cpu_to_be32(0x00000002);
237 err
= snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
238 TSCM_ADDR_BASE
+ TSCM_OFFSET_UNKNOWN
,
239 ®
, sizeof(reg
), 0);
243 // Register the isochronous channel for receiving stream.
244 reg
= cpu_to_be32(tscm
->rx_resources
.channel
);
245 err
= snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
246 TSCM_ADDR_BASE
+ TSCM_OFFSET_ISOC_RX_CH
,
247 ®
, sizeof(reg
), 0);
251 reg
= cpu_to_be32(0x00000001);
252 err
= snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
253 TSCM_ADDR_BASE
+ TSCM_OFFSET_START_STREAMING
,
254 ®
, sizeof(reg
), 0);
258 reg
= cpu_to_be32(0x00000001);
259 err
= snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
260 TSCM_ADDR_BASE
+ TSCM_OFFSET_ISOC_RX_ON
,
261 ®
, sizeof(reg
), 0);
265 // Set an option for unknown purpose.
266 reg
= cpu_to_be32(0x00002000);
267 err
= snd_fw_transaction(tscm
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
268 TSCM_ADDR_BASE
+ TSCM_OFFSET_SET_OPTION
,
269 ®
, sizeof(reg
), 0);
273 // Start multiplexing PCM samples on packets.
274 reg
= cpu_to_be32(0x00000001);
275 return snd_fw_transaction(tscm
->unit
,
276 TCODE_WRITE_QUADLET_REQUEST
,
277 TSCM_ADDR_BASE
+ TSCM_OFFSET_ISOC_TX_ON
,
278 ®
, sizeof(reg
), 0);
281 static int keep_resources(struct snd_tscm
*tscm
, unsigned int rate
,
282 struct amdtp_stream
*stream
)
284 struct fw_iso_resources
*resources
;
287 if (stream
== &tscm
->tx_stream
)
288 resources
= &tscm
->tx_resources
;
290 resources
= &tscm
->rx_resources
;
292 err
= amdtp_tscm_set_parameters(stream
, rate
);
296 return fw_iso_resources_allocate(resources
,
297 amdtp_stream_get_max_payload(stream
),
298 fw_parent_device(tscm
->unit
)->max_speed
);
301 static int init_stream(struct snd_tscm
*tscm
, struct amdtp_stream
*s
)
303 struct fw_iso_resources
*resources
;
304 enum amdtp_stream_direction dir
;
305 unsigned int pcm_channels
;
308 if (s
== &tscm
->tx_stream
) {
309 resources
= &tscm
->tx_resources
;
310 dir
= AMDTP_IN_STREAM
;
311 pcm_channels
= tscm
->spec
->pcm_capture_analog_channels
;
313 resources
= &tscm
->rx_resources
;
314 dir
= AMDTP_OUT_STREAM
;
315 pcm_channels
= tscm
->spec
->pcm_playback_analog_channels
;
318 if (tscm
->spec
->has_adat
)
320 if (tscm
->spec
->has_spdif
)
323 err
= fw_iso_resources_init(resources
, tscm
->unit
);
327 err
= amdtp_tscm_init(s
, tscm
->unit
, dir
, pcm_channels
);
329 fw_iso_resources_free(resources
);
334 static void destroy_stream(struct snd_tscm
*tscm
, struct amdtp_stream
*s
)
336 amdtp_stream_destroy(s
);
338 if (s
== &tscm
->tx_stream
)
339 fw_iso_resources_destroy(&tscm
->tx_resources
);
341 fw_iso_resources_destroy(&tscm
->rx_resources
);
344 int snd_tscm_stream_init_duplex(struct snd_tscm
*tscm
)
348 err
= init_stream(tscm
, &tscm
->tx_stream
);
352 err
= init_stream(tscm
, &tscm
->rx_stream
);
354 destroy_stream(tscm
, &tscm
->tx_stream
);
358 err
= amdtp_domain_init(&tscm
->domain
);
360 destroy_stream(tscm
, &tscm
->tx_stream
);
361 destroy_stream(tscm
, &tscm
->rx_stream
);
367 // At bus reset, streaming is stopped and some registers are clear.
368 void snd_tscm_stream_update_duplex(struct snd_tscm
*tscm
)
370 amdtp_domain_stop(&tscm
->domain
);
372 amdtp_stream_pcm_abort(&tscm
->tx_stream
);
373 amdtp_stream_pcm_abort(&tscm
->rx_stream
);
376 // This function should be called before starting streams or after stopping
378 void snd_tscm_stream_destroy_duplex(struct snd_tscm
*tscm
)
380 amdtp_domain_destroy(&tscm
->domain
);
382 destroy_stream(tscm
, &tscm
->rx_stream
);
383 destroy_stream(tscm
, &tscm
->tx_stream
);
386 int snd_tscm_stream_reserve_duplex(struct snd_tscm
*tscm
, unsigned int rate
,
387 unsigned int frames_per_period
,
388 unsigned int frames_per_buffer
)
390 unsigned int curr_rate
;
393 err
= snd_tscm_stream_get_rate(tscm
, &curr_rate
);
397 if (tscm
->substreams_counter
== 0 || rate
!= curr_rate
) {
398 amdtp_domain_stop(&tscm
->domain
);
400 finish_session(tscm
);
402 fw_iso_resources_free(&tscm
->tx_resources
);
403 fw_iso_resources_free(&tscm
->rx_resources
);
405 err
= set_clock(tscm
, rate
, INT_MAX
);
409 err
= keep_resources(tscm
, rate
, &tscm
->tx_stream
);
413 err
= keep_resources(tscm
, rate
, &tscm
->rx_stream
);
415 fw_iso_resources_free(&tscm
->tx_resources
);
419 err
= amdtp_domain_set_events_per_period(&tscm
->domain
,
420 frames_per_period
, frames_per_buffer
);
422 fw_iso_resources_free(&tscm
->tx_resources
);
423 fw_iso_resources_free(&tscm
->rx_resources
);
431 int snd_tscm_stream_start_duplex(struct snd_tscm
*tscm
, unsigned int rate
)
433 unsigned int generation
= tscm
->rx_resources
.generation
;
436 if (tscm
->substreams_counter
== 0)
439 if (amdtp_streaming_error(&tscm
->rx_stream
) ||
440 amdtp_streaming_error(&tscm
->tx_stream
)) {
441 amdtp_domain_stop(&tscm
->domain
);
442 finish_session(tscm
);
445 if (generation
!= fw_parent_device(tscm
->unit
)->card
->generation
) {
446 err
= fw_iso_resources_update(&tscm
->tx_resources
);
450 err
= fw_iso_resources_update(&tscm
->rx_resources
);
455 if (!amdtp_stream_running(&tscm
->rx_stream
)) {
456 int spd
= fw_parent_device(tscm
->unit
)->max_speed
;
458 err
= set_stream_formats(tscm
, rate
);
462 err
= begin_session(tscm
);
466 err
= amdtp_domain_add_stream(&tscm
->domain
, &tscm
->rx_stream
,
467 tscm
->rx_resources
.channel
, spd
);
471 err
= amdtp_domain_add_stream(&tscm
->domain
, &tscm
->tx_stream
,
472 tscm
->tx_resources
.channel
, spd
);
476 err
= amdtp_domain_start(&tscm
->domain
, 0);
480 if (!amdtp_stream_wait_callback(&tscm
->rx_stream
,
482 !amdtp_stream_wait_callback(&tscm
->tx_stream
,
491 amdtp_domain_stop(&tscm
->domain
);
492 finish_session(tscm
);
497 void snd_tscm_stream_stop_duplex(struct snd_tscm
*tscm
)
499 if (tscm
->substreams_counter
== 0) {
500 amdtp_domain_stop(&tscm
->domain
);
501 finish_session(tscm
);
503 fw_iso_resources_free(&tscm
->tx_resources
);
504 fw_iso_resources_free(&tscm
->rx_resources
);
508 void snd_tscm_stream_lock_changed(struct snd_tscm
*tscm
)
510 tscm
->dev_lock_changed
= true;
511 wake_up(&tscm
->hwdep_wait
);
514 int snd_tscm_stream_lock_try(struct snd_tscm
*tscm
)
518 spin_lock_irq(&tscm
->lock
);
520 /* user land lock this */
521 if (tscm
->dev_lock_count
< 0) {
526 /* this is the first time */
527 if (tscm
->dev_lock_count
++ == 0)
528 snd_tscm_stream_lock_changed(tscm
);
531 spin_unlock_irq(&tscm
->lock
);
535 void snd_tscm_stream_lock_release(struct snd_tscm
*tscm
)
537 spin_lock_irq(&tscm
->lock
);
539 if (WARN_ON(tscm
->dev_lock_count
<= 0))
541 if (--tscm
->dev_lock_count
== 0)
542 snd_tscm_stream_lock_changed(tscm
);
544 spin_unlock_irq(&tscm
->lock
);