1 // SPDX-License-Identifier: GPL-2.0-only
3 * oxfw_stream.c - a part of driver for OXFW970/971 based devices
5 * Copyright (c) 2014 Takashi Sakamoto
9 #include <linux/delay.h>
11 #define AVC_GENERIC_FRAME_MAXIMUM_BYTES 512
12 #define READY_TIMEOUT_MS 600
15 * According to datasheet of Oxford Semiconductor:
16 * OXFW970: 32.0/44.1/48.0/96.0 Khz, 8 audio channels I/O
17 * OXFW971: 32.0/44.1/48.0/88.2/96.0/192.0 kHz, 16 audio channels I/O, MIDI I/O
19 static const unsigned int oxfw_rate_table
[] = {
29 * See Table 5.7 – Sampling frequency for Multi-bit Audio
30 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
32 static const unsigned int avc_stream_rate_table
[] = {
41 static int set_rate(struct snd_oxfw
*oxfw
, unsigned int rate
)
45 err
= avc_general_set_sig_fmt(oxfw
->unit
, rate
,
46 AVC_GENERAL_PLUG_DIR_IN
, 0);
51 err
= avc_general_set_sig_fmt(oxfw
->unit
, rate
,
52 AVC_GENERAL_PLUG_DIR_OUT
, 0);
57 static int set_stream_format(struct snd_oxfw
*oxfw
, struct amdtp_stream
*s
,
58 unsigned int rate
, unsigned int pcm_channels
)
61 struct snd_oxfw_stream_formation formation
;
62 enum avc_general_plug_dir dir
;
66 if (s
== &oxfw
->tx_stream
) {
67 formats
= oxfw
->tx_stream_formats
;
68 dir
= AVC_GENERAL_PLUG_DIR_OUT
;
70 formats
= oxfw
->rx_stream_formats
;
71 dir
= AVC_GENERAL_PLUG_DIR_IN
;
74 /* Seek stream format for requirements. */
75 for (i
= 0; i
< SND_OXFW_STREAM_FORMAT_ENTRIES
; i
++) {
76 err
= snd_oxfw_stream_parse_format(formats
[i
], &formation
);
80 if ((formation
.rate
== rate
) && (formation
.pcm
== pcm_channels
))
83 if (i
== SND_OXFW_STREAM_FORMAT_ENTRIES
)
86 /* If assumed, just change rate. */
88 return set_rate(oxfw
, rate
);
90 /* Calculate format length. */
91 len
= 5 + formats
[i
][4] * 2;
93 err
= avc_stream_set_format(oxfw
->unit
, dir
, 0, formats
[i
], len
);
97 /* Some requests just after changing format causes freezing. */
103 static int start_stream(struct snd_oxfw
*oxfw
, struct amdtp_stream
*stream
)
105 struct cmp_connection
*conn
;
108 if (stream
== &oxfw
->rx_stream
)
109 conn
= &oxfw
->in_conn
;
111 conn
= &oxfw
->out_conn
;
113 err
= cmp_connection_establish(conn
);
117 err
= amdtp_domain_add_stream(&oxfw
->domain
, stream
,
118 conn
->resources
.channel
, conn
->speed
);
120 cmp_connection_break(conn
);
127 static int check_connection_used_by_others(struct snd_oxfw
*oxfw
,
128 struct amdtp_stream
*stream
)
130 struct cmp_connection
*conn
;
134 if (stream
== &oxfw
->tx_stream
)
135 conn
= &oxfw
->out_conn
;
137 conn
= &oxfw
->in_conn
;
139 err
= cmp_connection_check_used(conn
, &used
);
140 if ((err
>= 0) && used
&& !amdtp_stream_running(stream
)) {
141 dev_err(&oxfw
->unit
->device
,
142 "Connection established by others: %cPCR[%d]\n",
143 (conn
->direction
== CMP_OUTPUT
) ? 'o' : 'i',
151 static int init_stream(struct snd_oxfw
*oxfw
, struct amdtp_stream
*stream
)
153 struct cmp_connection
*conn
;
154 enum cmp_direction c_dir
;
155 enum amdtp_stream_direction s_dir
;
156 unsigned int flags
= 0;
159 if (!(oxfw
->quirks
& SND_OXFW_QUIRK_BLOCKING_TRANSMISSION
))
160 flags
|= CIP_NONBLOCKING
;
162 flags
|= CIP_BLOCKING
;
164 // OXFW 970/971 has no function to generate playback timing according to the sequence
165 // of value in syt field, thus the packet should include NO_INFO value in the field.
166 // However, some models just ignore data blocks in packet with NO_INFO for audio data
168 if (!(oxfw
->quirks
& SND_OXFW_QUIRK_IGNORE_NO_INFO_PACKET
))
169 flags
|= CIP_UNAWARE_SYT
;
171 if (stream
== &oxfw
->tx_stream
) {
172 conn
= &oxfw
->out_conn
;
174 s_dir
= AMDTP_IN_STREAM
;
176 if (oxfw
->quirks
& SND_OXFW_QUIRK_JUMBO_PAYLOAD
)
177 flags
|= CIP_JUMBO_PAYLOAD
;
178 if (oxfw
->quirks
& SND_OXFW_QUIRK_WRONG_DBS
)
179 flags
|= CIP_WRONG_DBS
;
180 if (oxfw
->quirks
& SND_OXFW_QUIRK_DBC_IS_TOTAL_PAYLOAD_QUADLETS
)
181 flags
|= CIP_DBC_IS_END_EVENT
| CIP_DBC_IS_PAYLOAD_QUADLETS
;
183 conn
= &oxfw
->in_conn
;
185 s_dir
= AMDTP_OUT_STREAM
;
188 err
= cmp_connection_init(conn
, oxfw
->unit
, c_dir
, 0);
192 err
= amdtp_am824_init(stream
, oxfw
->unit
, s_dir
, flags
);
194 cmp_connection_destroy(conn
);
201 static int keep_resources(struct snd_oxfw
*oxfw
, struct amdtp_stream
*stream
)
203 enum avc_general_plug_dir dir
;
205 struct snd_oxfw_stream_formation formation
;
206 struct cmp_connection
*conn
;
210 if (stream
== &oxfw
->rx_stream
) {
211 dir
= AVC_GENERAL_PLUG_DIR_IN
;
212 formats
= oxfw
->rx_stream_formats
;
213 conn
= &oxfw
->in_conn
;
215 dir
= AVC_GENERAL_PLUG_DIR_OUT
;
216 formats
= oxfw
->tx_stream_formats
;
217 conn
= &oxfw
->out_conn
;
220 err
= snd_oxfw_stream_get_current_formation(oxfw
, dir
, &formation
);
224 for (i
= 0; i
< SND_OXFW_STREAM_FORMAT_ENTRIES
; i
++) {
225 struct snd_oxfw_stream_formation fmt
;
227 if (formats
[i
] == NULL
)
230 err
= snd_oxfw_stream_parse_format(formats
[i
], &fmt
);
234 if (fmt
.rate
== formation
.rate
&& fmt
.pcm
== formation
.pcm
&&
235 fmt
.midi
== formation
.midi
)
238 if (i
== SND_OXFW_STREAM_FORMAT_ENTRIES
)
241 // The stream should have one pcm channels at least.
242 if (formation
.pcm
== 0)
245 err
= amdtp_am824_set_parameters(stream
, formation
.rate
, formation
.pcm
,
246 formation
.midi
* 8, false);
250 return cmp_connection_reserve(conn
, amdtp_stream_get_max_payload(stream
));
253 int snd_oxfw_stream_reserve_duplex(struct snd_oxfw
*oxfw
,
254 struct amdtp_stream
*stream
,
255 unsigned int rate
, unsigned int pcm_channels
,
256 unsigned int frames_per_period
,
257 unsigned int frames_per_buffer
)
259 struct snd_oxfw_stream_formation formation
;
260 enum avc_general_plug_dir dir
;
263 // Considering JACK/FFADO streaming:
264 // TODO: This can be removed hwdep functionality becomes popular.
265 err
= check_connection_used_by_others(oxfw
, &oxfw
->rx_stream
);
268 if (oxfw
->has_output
) {
269 err
= check_connection_used_by_others(oxfw
, &oxfw
->tx_stream
);
274 if (stream
== &oxfw
->tx_stream
)
275 dir
= AVC_GENERAL_PLUG_DIR_OUT
;
277 dir
= AVC_GENERAL_PLUG_DIR_IN
;
279 err
= snd_oxfw_stream_get_current_formation(oxfw
, dir
, &formation
);
283 rate
= formation
.rate
;
284 pcm_channels
= formation
.pcm
;
286 if (formation
.rate
!= rate
|| formation
.pcm
!= pcm_channels
) {
287 amdtp_domain_stop(&oxfw
->domain
);
289 cmp_connection_break(&oxfw
->in_conn
);
290 cmp_connection_release(&oxfw
->in_conn
);
292 if (oxfw
->has_output
) {
293 cmp_connection_break(&oxfw
->out_conn
);
294 cmp_connection_release(&oxfw
->out_conn
);
298 if (oxfw
->substreams_count
== 0 ||
299 formation
.rate
!= rate
|| formation
.pcm
!= pcm_channels
) {
300 err
= set_stream_format(oxfw
, stream
, rate
, pcm_channels
);
302 dev_err(&oxfw
->unit
->device
,
303 "fail to set stream format: %d\n", err
);
307 err
= keep_resources(oxfw
, &oxfw
->rx_stream
);
311 if (oxfw
->has_output
) {
312 err
= keep_resources(oxfw
, &oxfw
->tx_stream
);
314 cmp_connection_release(&oxfw
->in_conn
);
319 err
= amdtp_domain_set_events_per_period(&oxfw
->domain
,
320 frames_per_period
, frames_per_buffer
);
322 cmp_connection_release(&oxfw
->in_conn
);
323 if (oxfw
->has_output
)
324 cmp_connection_release(&oxfw
->out_conn
);
332 int snd_oxfw_stream_start_duplex(struct snd_oxfw
*oxfw
)
336 if (oxfw
->substreams_count
== 0)
339 if (amdtp_streaming_error(&oxfw
->rx_stream
) ||
340 amdtp_streaming_error(&oxfw
->tx_stream
)) {
341 amdtp_domain_stop(&oxfw
->domain
);
343 cmp_connection_break(&oxfw
->in_conn
);
344 if (oxfw
->has_output
)
345 cmp_connection_break(&oxfw
->out_conn
);
348 if (!amdtp_stream_running(&oxfw
->rx_stream
)) {
349 unsigned int tx_init_skip_cycles
= 0;
350 bool replay_seq
= false;
352 err
= start_stream(oxfw
, &oxfw
->rx_stream
);
354 dev_err(&oxfw
->unit
->device
,
355 "fail to prepare rx stream: %d\n", err
);
359 if (oxfw
->has_output
&&
360 !amdtp_stream_running(&oxfw
->tx_stream
)) {
361 err
= start_stream(oxfw
, &oxfw
->tx_stream
);
363 dev_err(&oxfw
->unit
->device
,
364 "fail to prepare tx stream: %d\n", err
);
368 if (oxfw
->quirks
& SND_OXFW_QUIRK_JUMBO_PAYLOAD
) {
369 // Just after changing sampling transfer frequency, many cycles are
370 // skipped for packet transmission.
371 tx_init_skip_cycles
= 400;
372 } else if (oxfw
->quirks
& SND_OXFW_QUIRK_VOLUNTARY_RECOVERY
) {
373 // It takes a bit time for target device to adjust event frequency
374 // according to nominal event frequency in isochronous packets from
376 tx_init_skip_cycles
= 4000;
382 // NOTE: The device ignores presentation time expressed by the value of syt field
383 // of CIP header in received packets. The sequence of the number of data blocks per
384 // packet is important for media clock recovery.
385 err
= amdtp_domain_start(&oxfw
->domain
, tx_init_skip_cycles
, replay_seq
, false);
389 if (!amdtp_domain_wait_ready(&oxfw
->domain
, READY_TIMEOUT_MS
)) {
397 amdtp_domain_stop(&oxfw
->domain
);
399 cmp_connection_break(&oxfw
->in_conn
);
400 if (oxfw
->has_output
)
401 cmp_connection_break(&oxfw
->out_conn
);
406 void snd_oxfw_stream_stop_duplex(struct snd_oxfw
*oxfw
)
408 if (oxfw
->substreams_count
== 0) {
409 amdtp_domain_stop(&oxfw
->domain
);
411 cmp_connection_break(&oxfw
->in_conn
);
412 cmp_connection_release(&oxfw
->in_conn
);
414 if (oxfw
->has_output
) {
415 cmp_connection_break(&oxfw
->out_conn
);
416 cmp_connection_release(&oxfw
->out_conn
);
421 static void destroy_stream(struct snd_oxfw
*oxfw
, struct amdtp_stream
*stream
)
423 struct cmp_connection
*conn
;
425 if (stream
== &oxfw
->tx_stream
)
426 conn
= &oxfw
->out_conn
;
428 conn
= &oxfw
->in_conn
;
430 amdtp_stream_destroy(stream
);
431 cmp_connection_destroy(conn
);
434 int snd_oxfw_stream_init_duplex(struct snd_oxfw
*oxfw
)
438 err
= init_stream(oxfw
, &oxfw
->rx_stream
);
442 if (oxfw
->has_output
) {
443 err
= init_stream(oxfw
, &oxfw
->tx_stream
);
445 destroy_stream(oxfw
, &oxfw
->rx_stream
);
450 err
= amdtp_domain_init(&oxfw
->domain
);
452 destroy_stream(oxfw
, &oxfw
->rx_stream
);
453 if (oxfw
->has_output
)
454 destroy_stream(oxfw
, &oxfw
->tx_stream
);
460 // This function should be called before starting the stream or after stopping
462 void snd_oxfw_stream_destroy_duplex(struct snd_oxfw
*oxfw
)
464 amdtp_domain_destroy(&oxfw
->domain
);
466 destroy_stream(oxfw
, &oxfw
->rx_stream
);
468 if (oxfw
->has_output
)
469 destroy_stream(oxfw
, &oxfw
->tx_stream
);
472 void snd_oxfw_stream_update_duplex(struct snd_oxfw
*oxfw
)
474 amdtp_domain_stop(&oxfw
->domain
);
476 cmp_connection_break(&oxfw
->in_conn
);
478 amdtp_stream_pcm_abort(&oxfw
->rx_stream
);
480 if (oxfw
->has_output
) {
481 cmp_connection_break(&oxfw
->out_conn
);
483 amdtp_stream_pcm_abort(&oxfw
->tx_stream
);
487 int snd_oxfw_stream_get_current_formation(struct snd_oxfw
*oxfw
,
488 enum avc_general_plug_dir dir
,
489 struct snd_oxfw_stream_formation
*formation
)
493 if (!(oxfw
->quirks
& SND_OXFW_QUIRK_STREAM_FORMAT_INFO_UNSUPPORTED
)) {
497 len
= AVC_GENERIC_FRAME_MAXIMUM_BYTES
;
498 format
= kmalloc(len
, GFP_KERNEL
);
502 err
= avc_stream_get_format_single(oxfw
->unit
, dir
, 0, format
, &len
);
507 err
= snd_oxfw_stream_parse_format(format
, formation
);
512 // Miglia Harmony Audio does not support Extended Stream Format Information
513 // command. Use the duplicated hard-coded format, instead.
518 err
= avc_general_get_sig_fmt(oxfw
->unit
, &rate
, dir
, 0);
522 if (dir
== AVC_GENERAL_PLUG_DIR_IN
)
523 formats
= oxfw
->rx_stream_formats
;
525 formats
= oxfw
->tx_stream_formats
;
527 for (i
= 0; (i
< SND_OXFW_STREAM_FORMAT_ENTRIES
); ++i
) {
531 err
= snd_oxfw_stream_parse_format(formats
[i
], formation
);
535 if (formation
->rate
== rate
)
538 if (i
== SND_OXFW_STREAM_FORMAT_ENTRIES
)
546 * See Table 6.16 - AM824 Stream Format
547 * Figure 6.19 - format_information field for AM824 Compound
548 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
549 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
551 int snd_oxfw_stream_parse_format(const u8
*format
,
552 struct snd_oxfw_stream_formation
*formation
)
554 unsigned int i
, e
, channels
, type
;
556 memset(formation
, 0, sizeof(struct snd_oxfw_stream_formation
));
559 * this module can support a hierarchy combination that:
560 * Root: Audio and Music (0x90)
561 * Level 1: AM824 Compound (0x40)
563 if ((format
[0] != 0x90) || (format
[1] != 0x40))
566 /* check the sampling rate */
567 for (i
= 0; i
< ARRAY_SIZE(avc_stream_rate_table
); i
++) {
568 if (format
[2] == avc_stream_rate_table
[i
])
571 if (i
== ARRAY_SIZE(avc_stream_rate_table
))
574 formation
->rate
= oxfw_rate_table
[i
];
576 for (e
= 0; e
< format
[4]; e
++) {
577 channels
= format
[5 + e
* 2];
578 type
= format
[6 + e
* 2];
581 /* IEC 60958 Conformant, currently handled as MBLA */
583 /* Multi Bit Linear Audio (Raw) */
585 formation
->pcm
+= channels
;
587 /* MIDI Conformant */
589 formation
->midi
= channels
;
591 /* IEC 61937-3 to 7 */
597 /* Multi Bit Linear Audio */
598 case 0x07: /* DVD-Audio */
599 case 0x0c: /* High Precision */
601 case 0x08: /* (Plain) Raw */
602 case 0x09: /* (Plain) SACD */
603 case 0x0a: /* (Encoded) Raw */
604 case 0x0b: /* (Encoded) SACD */
605 /* SMPTE Time-Code conformant */
611 /* Synchronization Stream (Stereo Raw audio) */
616 return -ENXIO
; /* not supported */
620 if (formation
->pcm
> AM824_MAX_CHANNELS_FOR_PCM
||
621 formation
->midi
> AM824_MAX_CHANNELS_FOR_MIDI
)
628 assume_stream_formats(struct snd_oxfw
*oxfw
, enum avc_general_plug_dir dir
,
629 unsigned int pid
, u8
*buf
, unsigned int *len
,
632 struct snd_oxfw_stream_formation formation
;
636 // get format at current sampling rate.
637 if (!(oxfw
->quirks
& SND_OXFW_QUIRK_STREAM_FORMAT_INFO_UNSUPPORTED
)) {
638 err
= avc_stream_get_format_single(oxfw
->unit
, dir
, pid
, buf
, len
);
640 dev_err(&oxfw
->unit
->device
,
641 "fail to get current stream format for isoc %s plug %d:%d\n",
642 (dir
== AVC_GENERAL_PLUG_DIR_IN
) ? "in" : "out",
647 // Miglia Harmony Audio does not support Extended Stream Format Information
648 // command. Use the hard-coded format, instead.
651 buf
[2] = avc_stream_rate_table
[0];
655 if (dir
== AVC_GENERAL_PLUG_DIR_IN
)
665 /* parse and set stream format */
667 err
= snd_oxfw_stream_parse_format(buf
, &formation
);
671 formats
[eid
] = devm_kmemdup(&oxfw
->card
->card_dev
, buf
, *len
,
678 /* apply the format for each available sampling rate */
679 for (i
= 0; i
< ARRAY_SIZE(oxfw_rate_table
); i
++) {
680 if (formation
.rate
== oxfw_rate_table
[i
])
683 err
= avc_general_inquiry_sig_fmt(oxfw
->unit
,
690 formats
[eid
] = devm_kmemdup(&oxfw
->card
->card_dev
, buf
, *len
,
692 if (formats
[eid
] == NULL
) {
696 formats
[eid
][2] = avc_stream_rate_table
[i
];
700 oxfw
->assumed
= true;
705 static int fill_stream_formats(struct snd_oxfw
*oxfw
,
706 enum avc_general_plug_dir dir
,
710 unsigned int len
, eid
= 0;
711 struct snd_oxfw_stream_formation dummy
;
714 buf
= kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES
, GFP_KERNEL
);
718 if (dir
== AVC_GENERAL_PLUG_DIR_OUT
)
719 formats
= oxfw
->tx_stream_formats
;
721 formats
= oxfw
->rx_stream_formats
;
723 /* get first entry */
724 len
= AVC_GENERIC_FRAME_MAXIMUM_BYTES
;
725 err
= avc_stream_get_format_list(oxfw
->unit
, dir
, 0, buf
, &len
, 0);
727 /* LIST subfunction is not implemented */
728 len
= AVC_GENERIC_FRAME_MAXIMUM_BYTES
;
729 err
= assume_stream_formats(oxfw
, dir
, pid
, buf
, &len
,
732 } else if (err
< 0) {
733 dev_err(&oxfw
->unit
->device
,
734 "fail to get stream format %d for isoc %s plug %d:%d\n",
735 eid
, (dir
== AVC_GENERAL_PLUG_DIR_IN
) ? "in" : "out",
740 /* LIST subfunction is implemented */
741 while (eid
< SND_OXFW_STREAM_FORMAT_ENTRIES
) {
742 /* The format is too short. */
748 /* parse and set stream format */
749 err
= snd_oxfw_stream_parse_format(buf
, &dummy
);
753 formats
[eid
] = devm_kmemdup(&oxfw
->card
->card_dev
, buf
, len
,
761 len
= AVC_GENERIC_FRAME_MAXIMUM_BYTES
;
762 err
= avc_stream_get_format_list(oxfw
->unit
, dir
, 0,
764 /* No entries remained. */
765 if (err
== -EINVAL
) {
768 } else if (err
< 0) {
769 dev_err(&oxfw
->unit
->device
,
770 "fail to get stream format %d for isoc %s plug %d:%d\n",
771 eid
, (dir
== AVC_GENERAL_PLUG_DIR_IN
) ? "in" :
782 int snd_oxfw_stream_discover(struct snd_oxfw
*oxfw
)
784 u8 plugs
[AVC_PLUG_INFO_BUF_BYTES
];
785 struct snd_oxfw_stream_formation formation
;
790 /* the number of plugs for isoc in/out, ext in/out */
791 err
= avc_general_get_plug_info(oxfw
->unit
, 0x1f, 0x07, 0x00, plugs
);
793 dev_err(&oxfw
->unit
->device
,
794 "fail to get info for isoc/external in/out plugs: %d\n",
797 } else if ((plugs
[0] == 0) && (plugs
[1] == 0)) {
802 /* use oPCR[0] if exists */
804 err
= fill_stream_formats(oxfw
, AVC_GENERAL_PLUG_DIR_OUT
, 0);
809 // The oPCR is not available for isoc communication.
812 for (i
= 0; i
< SND_OXFW_STREAM_FORMAT_ENTRIES
; i
++) {
813 format
= oxfw
->tx_stream_formats
[i
];
816 err
= snd_oxfw_stream_parse_format(format
,
821 /* Add one MIDI port. */
822 if (formation
.midi
> 0)
823 oxfw
->midi_input_ports
= 1;
826 oxfw
->has_output
= true;
830 /* use iPCR[0] if exists */
832 err
= fill_stream_formats(oxfw
, AVC_GENERAL_PLUG_DIR_IN
, 0);
837 // The iPCR is not available for isoc communication.
840 for (i
= 0; i
< SND_OXFW_STREAM_FORMAT_ENTRIES
; i
++) {
841 format
= oxfw
->rx_stream_formats
[i
];
844 err
= snd_oxfw_stream_parse_format(format
,
849 /* Add one MIDI port. */
850 if (formation
.midi
> 0)
851 oxfw
->midi_output_ports
= 1;
854 oxfw
->has_input
= true;
861 void snd_oxfw_stream_lock_changed(struct snd_oxfw
*oxfw
)
863 oxfw
->dev_lock_changed
= true;
864 wake_up(&oxfw
->hwdep_wait
);
867 int snd_oxfw_stream_lock_try(struct snd_oxfw
*oxfw
)
871 spin_lock_irq(&oxfw
->lock
);
873 /* user land lock this */
874 if (oxfw
->dev_lock_count
< 0) {
879 /* this is the first time */
880 if (oxfw
->dev_lock_count
++ == 0)
881 snd_oxfw_stream_lock_changed(oxfw
);
884 spin_unlock_irq(&oxfw
->lock
);
888 void snd_oxfw_stream_lock_release(struct snd_oxfw
*oxfw
)
890 spin_lock_irq(&oxfw
->lock
);
892 if (WARN_ON(oxfw
->dev_lock_count
<= 0))
894 if (--oxfw
->dev_lock_count
== 0)
895 snd_oxfw_stream_lock_changed(oxfw
);
897 spin_unlock_irq(&oxfw
->lock
);