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 CALLBACK_TIMEOUT 200
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
;
158 if (stream
== &oxfw
->tx_stream
) {
159 conn
= &oxfw
->out_conn
;
161 s_dir
= AMDTP_IN_STREAM
;
163 conn
= &oxfw
->in_conn
;
165 s_dir
= AMDTP_OUT_STREAM
;
168 err
= cmp_connection_init(conn
, oxfw
->unit
, c_dir
, 0);
172 err
= amdtp_am824_init(stream
, oxfw
->unit
, s_dir
, CIP_NONBLOCKING
);
174 cmp_connection_destroy(conn
);
179 * OXFW starts to transmit packets with non-zero dbc.
180 * OXFW postpone transferring packets till handling any asynchronous
181 * packets. As a result, next isochronous packet includes more data
182 * blocks than IEC 61883-6 defines.
184 if (stream
== &oxfw
->tx_stream
) {
185 oxfw
->tx_stream
.flags
|= CIP_JUMBO_PAYLOAD
;
187 oxfw
->tx_stream
.flags
|= CIP_WRONG_DBS
;
193 static int keep_resources(struct snd_oxfw
*oxfw
, struct amdtp_stream
*stream
)
195 enum avc_general_plug_dir dir
;
197 struct snd_oxfw_stream_formation formation
;
198 struct cmp_connection
*conn
;
202 if (stream
== &oxfw
->rx_stream
) {
203 dir
= AVC_GENERAL_PLUG_DIR_IN
;
204 formats
= oxfw
->rx_stream_formats
;
205 conn
= &oxfw
->in_conn
;
207 dir
= AVC_GENERAL_PLUG_DIR_OUT
;
208 formats
= oxfw
->tx_stream_formats
;
209 conn
= &oxfw
->out_conn
;
212 err
= snd_oxfw_stream_get_current_formation(oxfw
, dir
, &formation
);
216 for (i
= 0; i
< SND_OXFW_STREAM_FORMAT_ENTRIES
; i
++) {
217 struct snd_oxfw_stream_formation fmt
;
219 if (formats
[i
] == NULL
)
222 err
= snd_oxfw_stream_parse_format(formats
[i
], &fmt
);
226 if (fmt
.rate
== formation
.rate
&& fmt
.pcm
== formation
.pcm
&&
227 fmt
.midi
== formation
.midi
)
230 if (i
== SND_OXFW_STREAM_FORMAT_ENTRIES
)
233 // The stream should have one pcm channels at least.
234 if (formation
.pcm
== 0)
237 err
= amdtp_am824_set_parameters(stream
, formation
.rate
, formation
.pcm
,
238 formation
.midi
* 8, false);
242 return cmp_connection_reserve(conn
, amdtp_stream_get_max_payload(stream
));
245 int snd_oxfw_stream_reserve_duplex(struct snd_oxfw
*oxfw
,
246 struct amdtp_stream
*stream
,
247 unsigned int rate
, unsigned int pcm_channels
,
248 unsigned int frames_per_period
,
249 unsigned int frames_per_buffer
)
251 struct snd_oxfw_stream_formation formation
;
252 enum avc_general_plug_dir dir
;
255 // Considering JACK/FFADO streaming:
256 // TODO: This can be removed hwdep functionality becomes popular.
257 err
= check_connection_used_by_others(oxfw
, &oxfw
->rx_stream
);
260 if (oxfw
->has_output
) {
261 err
= check_connection_used_by_others(oxfw
, &oxfw
->tx_stream
);
266 if (stream
== &oxfw
->tx_stream
)
267 dir
= AVC_GENERAL_PLUG_DIR_OUT
;
269 dir
= AVC_GENERAL_PLUG_DIR_IN
;
271 err
= snd_oxfw_stream_get_current_formation(oxfw
, dir
, &formation
);
275 rate
= formation
.rate
;
276 pcm_channels
= formation
.pcm
;
278 if (formation
.rate
!= rate
|| formation
.pcm
!= pcm_channels
) {
279 amdtp_domain_stop(&oxfw
->domain
);
281 cmp_connection_break(&oxfw
->in_conn
);
282 cmp_connection_release(&oxfw
->in_conn
);
284 if (oxfw
->has_output
) {
285 cmp_connection_break(&oxfw
->out_conn
);
286 cmp_connection_release(&oxfw
->out_conn
);
290 if (oxfw
->substreams_count
== 0 ||
291 formation
.rate
!= rate
|| formation
.pcm
!= pcm_channels
) {
292 err
= set_stream_format(oxfw
, stream
, rate
, pcm_channels
);
294 dev_err(&oxfw
->unit
->device
,
295 "fail to set stream format: %d\n", err
);
299 err
= keep_resources(oxfw
, &oxfw
->rx_stream
);
303 if (oxfw
->has_output
) {
304 err
= keep_resources(oxfw
, &oxfw
->tx_stream
);
306 cmp_connection_release(&oxfw
->in_conn
);
311 err
= amdtp_domain_set_events_per_period(&oxfw
->domain
,
312 frames_per_period
, frames_per_buffer
);
314 cmp_connection_release(&oxfw
->in_conn
);
315 if (oxfw
->has_output
)
316 cmp_connection_release(&oxfw
->out_conn
);
324 int snd_oxfw_stream_start_duplex(struct snd_oxfw
*oxfw
)
328 if (oxfw
->substreams_count
== 0)
331 if (amdtp_streaming_error(&oxfw
->rx_stream
) ||
332 amdtp_streaming_error(&oxfw
->tx_stream
)) {
333 amdtp_domain_stop(&oxfw
->domain
);
335 cmp_connection_break(&oxfw
->in_conn
);
336 if (oxfw
->has_output
)
337 cmp_connection_break(&oxfw
->out_conn
);
340 if (!amdtp_stream_running(&oxfw
->rx_stream
)) {
341 err
= start_stream(oxfw
, &oxfw
->rx_stream
);
343 dev_err(&oxfw
->unit
->device
,
344 "fail to prepare rx stream: %d\n", err
);
348 if (oxfw
->has_output
&&
349 !amdtp_stream_running(&oxfw
->tx_stream
)) {
350 err
= start_stream(oxfw
, &oxfw
->tx_stream
);
352 dev_err(&oxfw
->unit
->device
,
353 "fail to prepare tx stream: %d\n", err
);
358 err
= amdtp_domain_start(&oxfw
->domain
, 0);
362 // Wait first packet.
363 if (!amdtp_stream_wait_callback(&oxfw
->rx_stream
,
369 if (oxfw
->has_output
) {
370 if (!amdtp_stream_wait_callback(&oxfw
->tx_stream
,
380 amdtp_domain_stop(&oxfw
->domain
);
382 cmp_connection_break(&oxfw
->in_conn
);
383 if (oxfw
->has_output
)
384 cmp_connection_break(&oxfw
->out_conn
);
389 void snd_oxfw_stream_stop_duplex(struct snd_oxfw
*oxfw
)
391 if (oxfw
->substreams_count
== 0) {
392 amdtp_domain_stop(&oxfw
->domain
);
394 cmp_connection_break(&oxfw
->in_conn
);
395 cmp_connection_release(&oxfw
->in_conn
);
397 if (oxfw
->has_output
) {
398 cmp_connection_break(&oxfw
->out_conn
);
399 cmp_connection_release(&oxfw
->out_conn
);
404 static void destroy_stream(struct snd_oxfw
*oxfw
, struct amdtp_stream
*stream
)
406 struct cmp_connection
*conn
;
408 if (stream
== &oxfw
->tx_stream
)
409 conn
= &oxfw
->out_conn
;
411 conn
= &oxfw
->in_conn
;
413 amdtp_stream_destroy(stream
);
414 cmp_connection_destroy(conn
);
417 int snd_oxfw_stream_init_duplex(struct snd_oxfw
*oxfw
)
421 err
= init_stream(oxfw
, &oxfw
->rx_stream
);
425 if (oxfw
->has_output
) {
426 err
= init_stream(oxfw
, &oxfw
->tx_stream
);
428 destroy_stream(oxfw
, &oxfw
->rx_stream
);
433 err
= amdtp_domain_init(&oxfw
->domain
);
435 destroy_stream(oxfw
, &oxfw
->rx_stream
);
436 if (oxfw
->has_output
)
437 destroy_stream(oxfw
, &oxfw
->tx_stream
);
443 // This function should be called before starting the stream or after stopping
445 void snd_oxfw_stream_destroy_duplex(struct snd_oxfw
*oxfw
)
447 amdtp_domain_destroy(&oxfw
->domain
);
449 destroy_stream(oxfw
, &oxfw
->rx_stream
);
451 if (oxfw
->has_output
)
452 destroy_stream(oxfw
, &oxfw
->tx_stream
);
455 void snd_oxfw_stream_update_duplex(struct snd_oxfw
*oxfw
)
457 amdtp_domain_stop(&oxfw
->domain
);
459 cmp_connection_break(&oxfw
->in_conn
);
461 amdtp_stream_pcm_abort(&oxfw
->rx_stream
);
463 if (oxfw
->has_output
) {
464 cmp_connection_break(&oxfw
->out_conn
);
466 amdtp_stream_pcm_abort(&oxfw
->tx_stream
);
470 int snd_oxfw_stream_get_current_formation(struct snd_oxfw
*oxfw
,
471 enum avc_general_plug_dir dir
,
472 struct snd_oxfw_stream_formation
*formation
)
478 len
= AVC_GENERIC_FRAME_MAXIMUM_BYTES
;
479 format
= kmalloc(len
, GFP_KERNEL
);
483 err
= avc_stream_get_format_single(oxfw
->unit
, dir
, 0, format
, &len
);
491 err
= snd_oxfw_stream_parse_format(format
, formation
);
498 * See Table 6.16 - AM824 Stream Format
499 * Figure 6.19 - format_information field for AM824 Compound
500 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
501 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
503 int snd_oxfw_stream_parse_format(u8
*format
,
504 struct snd_oxfw_stream_formation
*formation
)
506 unsigned int i
, e
, channels
, type
;
508 memset(formation
, 0, sizeof(struct snd_oxfw_stream_formation
));
511 * this module can support a hierarchy combination that:
512 * Root: Audio and Music (0x90)
513 * Level 1: AM824 Compound (0x40)
515 if ((format
[0] != 0x90) || (format
[1] != 0x40))
518 /* check the sampling rate */
519 for (i
= 0; i
< ARRAY_SIZE(avc_stream_rate_table
); i
++) {
520 if (format
[2] == avc_stream_rate_table
[i
])
523 if (i
== ARRAY_SIZE(avc_stream_rate_table
))
526 formation
->rate
= oxfw_rate_table
[i
];
528 for (e
= 0; e
< format
[4]; e
++) {
529 channels
= format
[5 + e
* 2];
530 type
= format
[6 + e
* 2];
533 /* IEC 60958 Conformant, currently handled as MBLA */
535 /* Multi Bit Linear Audio (Raw) */
537 formation
->pcm
+= channels
;
539 /* MIDI Conformant */
541 formation
->midi
= channels
;
543 /* IEC 61937-3 to 7 */
549 /* Multi Bit Linear Audio */
550 case 0x07: /* DVD-Audio */
551 case 0x0c: /* High Precision */
553 case 0x08: /* (Plain) Raw */
554 case 0x09: /* (Plain) SACD */
555 case 0x0a: /* (Encoded) Raw */
556 case 0x0b: /* (Encoded) SACD */
557 /* SMPTE Time-Code conformant */
563 /* Synchronization Stream (Stereo Raw audio) */
568 return -ENXIO
; /* not supported */
572 if (formation
->pcm
> AM824_MAX_CHANNELS_FOR_PCM
||
573 formation
->midi
> AM824_MAX_CHANNELS_FOR_MIDI
)
580 assume_stream_formats(struct snd_oxfw
*oxfw
, enum avc_general_plug_dir dir
,
581 unsigned int pid
, u8
*buf
, unsigned int *len
,
584 struct snd_oxfw_stream_formation formation
;
588 /* get format at current sampling rate */
589 err
= avc_stream_get_format_single(oxfw
->unit
, dir
, pid
, buf
, len
);
591 dev_err(&oxfw
->unit
->device
,
592 "fail to get current stream format for isoc %s plug %d:%d\n",
593 (dir
== AVC_GENERAL_PLUG_DIR_IN
) ? "in" : "out",
598 /* parse and set stream format */
600 err
= snd_oxfw_stream_parse_format(buf
, &formation
);
604 formats
[eid
] = devm_kmemdup(&oxfw
->card
->card_dev
, buf
, *len
,
611 /* apply the format for each available sampling rate */
612 for (i
= 0; i
< ARRAY_SIZE(oxfw_rate_table
); i
++) {
613 if (formation
.rate
== oxfw_rate_table
[i
])
616 err
= avc_general_inquiry_sig_fmt(oxfw
->unit
,
623 formats
[eid
] = devm_kmemdup(&oxfw
->card
->card_dev
, buf
, *len
,
625 if (formats
[eid
] == NULL
) {
629 formats
[eid
][2] = avc_stream_rate_table
[i
];
633 oxfw
->assumed
= true;
638 static int fill_stream_formats(struct snd_oxfw
*oxfw
,
639 enum avc_general_plug_dir dir
,
643 unsigned int len
, eid
= 0;
644 struct snd_oxfw_stream_formation dummy
;
647 buf
= kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES
, GFP_KERNEL
);
651 if (dir
== AVC_GENERAL_PLUG_DIR_OUT
)
652 formats
= oxfw
->tx_stream_formats
;
654 formats
= oxfw
->rx_stream_formats
;
656 /* get first entry */
657 len
= AVC_GENERIC_FRAME_MAXIMUM_BYTES
;
658 err
= avc_stream_get_format_list(oxfw
->unit
, dir
, 0, buf
, &len
, 0);
660 /* LIST subfunction is not implemented */
661 len
= AVC_GENERIC_FRAME_MAXIMUM_BYTES
;
662 err
= assume_stream_formats(oxfw
, dir
, pid
, buf
, &len
,
665 } else if (err
< 0) {
666 dev_err(&oxfw
->unit
->device
,
667 "fail to get stream format %d for isoc %s plug %d:%d\n",
668 eid
, (dir
== AVC_GENERAL_PLUG_DIR_IN
) ? "in" : "out",
673 /* LIST subfunction is implemented */
674 while (eid
< SND_OXFW_STREAM_FORMAT_ENTRIES
) {
675 /* The format is too short. */
681 /* parse and set stream format */
682 err
= snd_oxfw_stream_parse_format(buf
, &dummy
);
686 formats
[eid
] = devm_kmemdup(&oxfw
->card
->card_dev
, buf
, len
,
694 len
= AVC_GENERIC_FRAME_MAXIMUM_BYTES
;
695 err
= avc_stream_get_format_list(oxfw
->unit
, dir
, 0,
697 /* No entries remained. */
698 if (err
== -EINVAL
) {
701 } else if (err
< 0) {
702 dev_err(&oxfw
->unit
->device
,
703 "fail to get stream format %d for isoc %s plug %d:%d\n",
704 eid
, (dir
== AVC_GENERAL_PLUG_DIR_IN
) ? "in" :
715 int snd_oxfw_stream_discover(struct snd_oxfw
*oxfw
)
717 u8 plugs
[AVC_PLUG_INFO_BUF_BYTES
];
718 struct snd_oxfw_stream_formation formation
;
723 /* the number of plugs for isoc in/out, ext in/out */
724 err
= avc_general_get_plug_info(oxfw
->unit
, 0x1f, 0x07, 0x00, plugs
);
726 dev_err(&oxfw
->unit
->device
,
727 "fail to get info for isoc/external in/out plugs: %d\n",
730 } else if ((plugs
[0] == 0) && (plugs
[1] == 0)) {
735 /* use oPCR[0] if exists */
737 err
= fill_stream_formats(oxfw
, AVC_GENERAL_PLUG_DIR_OUT
, 0);
742 // The oPCR is not available for isoc communication.
745 for (i
= 0; i
< SND_OXFW_STREAM_FORMAT_ENTRIES
; i
++) {
746 format
= oxfw
->tx_stream_formats
[i
];
749 err
= snd_oxfw_stream_parse_format(format
,
754 /* Add one MIDI port. */
755 if (formation
.midi
> 0)
756 oxfw
->midi_input_ports
= 1;
759 oxfw
->has_output
= true;
763 /* use iPCR[0] if exists */
765 err
= fill_stream_formats(oxfw
, AVC_GENERAL_PLUG_DIR_IN
, 0);
770 // The iPCR is not available for isoc communication.
773 for (i
= 0; i
< SND_OXFW_STREAM_FORMAT_ENTRIES
; i
++) {
774 format
= oxfw
->rx_stream_formats
[i
];
777 err
= snd_oxfw_stream_parse_format(format
,
782 /* Add one MIDI port. */
783 if (formation
.midi
> 0)
784 oxfw
->midi_output_ports
= 1;
787 oxfw
->has_input
= true;
794 void snd_oxfw_stream_lock_changed(struct snd_oxfw
*oxfw
)
796 oxfw
->dev_lock_changed
= true;
797 wake_up(&oxfw
->hwdep_wait
);
800 int snd_oxfw_stream_lock_try(struct snd_oxfw
*oxfw
)
804 spin_lock_irq(&oxfw
->lock
);
806 /* user land lock this */
807 if (oxfw
->dev_lock_count
< 0) {
812 /* this is the first time */
813 if (oxfw
->dev_lock_count
++ == 0)
814 snd_oxfw_stream_lock_changed(oxfw
);
817 spin_unlock_irq(&oxfw
->lock
);
821 void snd_oxfw_stream_lock_release(struct snd_oxfw
*oxfw
)
823 spin_lock_irq(&oxfw
->lock
);
825 if (WARN_ON(oxfw
->dev_lock_count
<= 0))
827 if (--oxfw
->dev_lock_count
== 0)
828 snd_oxfw_stream_lock_changed(oxfw
);
830 spin_unlock_irq(&oxfw
->lock
);