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;
156 unsigned int flags = CIP_NONBLOCKING;
159 if (stream == &oxfw->tx_stream) {
160 conn = &oxfw->out_conn;
162 s_dir = AMDTP_IN_STREAM;
164 if (oxfw->quirks & SND_OXFW_QUIRK_JUMBO_PAYLOAD)
165 flags |= CIP_JUMBO_PAYLOAD;
167 flags |= CIP_WRONG_DBS;
169 conn = &oxfw->in_conn;
171 s_dir = AMDTP_OUT_STREAM;
174 err = cmp_connection_init(conn, oxfw->unit, c_dir, 0);
178 err = amdtp_am824_init(stream, oxfw->unit, s_dir, flags);
180 cmp_connection_destroy(conn);
187 static int keep_resources(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
189 enum avc_general_plug_dir dir;
191 struct snd_oxfw_stream_formation formation;
192 struct cmp_connection *conn;
196 if (stream == &oxfw->rx_stream) {
197 dir = AVC_GENERAL_PLUG_DIR_IN;
198 formats = oxfw->rx_stream_formats;
199 conn = &oxfw->in_conn;
201 dir = AVC_GENERAL_PLUG_DIR_OUT;
202 formats = oxfw->tx_stream_formats;
203 conn = &oxfw->out_conn;
206 err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
210 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
211 struct snd_oxfw_stream_formation fmt;
213 if (formats[i] == NULL)
216 err = snd_oxfw_stream_parse_format(formats[i], &fmt);
220 if (fmt.rate == formation.rate && fmt.pcm == formation.pcm &&
221 fmt.midi == formation.midi)
224 if (i == SND_OXFW_STREAM_FORMAT_ENTRIES)
227 // The stream should have one pcm channels at least.
228 if (formation.pcm == 0)
231 err = amdtp_am824_set_parameters(stream, formation.rate, formation.pcm,
232 formation.midi * 8, false);
236 return cmp_connection_reserve(conn, amdtp_stream_get_max_payload(stream));
239 int snd_oxfw_stream_reserve_duplex(struct snd_oxfw *oxfw,
240 struct amdtp_stream *stream,
241 unsigned int rate, unsigned int pcm_channels,
242 unsigned int frames_per_period,
243 unsigned int frames_per_buffer)
245 struct snd_oxfw_stream_formation formation;
246 enum avc_general_plug_dir dir;
249 // Considering JACK/FFADO streaming:
250 // TODO: This can be removed hwdep functionality becomes popular.
251 err = check_connection_used_by_others(oxfw, &oxfw->rx_stream);
254 if (oxfw->has_output) {
255 err = check_connection_used_by_others(oxfw, &oxfw->tx_stream);
260 if (stream == &oxfw->tx_stream)
261 dir = AVC_GENERAL_PLUG_DIR_OUT;
263 dir = AVC_GENERAL_PLUG_DIR_IN;
265 err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
269 rate = formation.rate;
270 pcm_channels = formation.pcm;
272 if (formation.rate != rate || formation.pcm != pcm_channels) {
273 amdtp_domain_stop(&oxfw->domain);
275 cmp_connection_break(&oxfw->in_conn);
276 cmp_connection_release(&oxfw->in_conn);
278 if (oxfw->has_output) {
279 cmp_connection_break(&oxfw->out_conn);
280 cmp_connection_release(&oxfw->out_conn);
284 if (oxfw->substreams_count == 0 ||
285 formation.rate != rate || formation.pcm != pcm_channels) {
286 err = set_stream_format(oxfw, stream, rate, pcm_channels);
288 dev_err(&oxfw->unit->device,
289 "fail to set stream format: %d\n", err);
293 err = keep_resources(oxfw, &oxfw->rx_stream);
297 if (oxfw->has_output) {
298 err = keep_resources(oxfw, &oxfw->tx_stream);
300 cmp_connection_release(&oxfw->in_conn);
305 err = amdtp_domain_set_events_per_period(&oxfw->domain,
306 frames_per_period, frames_per_buffer);
308 cmp_connection_release(&oxfw->in_conn);
309 if (oxfw->has_output)
310 cmp_connection_release(&oxfw->out_conn);
318 int snd_oxfw_stream_start_duplex(struct snd_oxfw *oxfw)
322 if (oxfw->substreams_count == 0)
325 if (amdtp_streaming_error(&oxfw->rx_stream) ||
326 amdtp_streaming_error(&oxfw->tx_stream)) {
327 amdtp_domain_stop(&oxfw->domain);
329 cmp_connection_break(&oxfw->in_conn);
330 if (oxfw->has_output)
331 cmp_connection_break(&oxfw->out_conn);
334 if (!amdtp_stream_running(&oxfw->rx_stream)) {
335 err = start_stream(oxfw, &oxfw->rx_stream);
337 dev_err(&oxfw->unit->device,
338 "fail to prepare rx stream: %d\n", err);
342 if (oxfw->has_output &&
343 !amdtp_stream_running(&oxfw->tx_stream)) {
344 err = start_stream(oxfw, &oxfw->tx_stream);
346 dev_err(&oxfw->unit->device,
347 "fail to prepare tx stream: %d\n", err);
352 err = amdtp_domain_start(&oxfw->domain, 0);
356 // Wait first packet.
357 if (!amdtp_stream_wait_callback(&oxfw->rx_stream,
363 if (oxfw->has_output) {
364 if (!amdtp_stream_wait_callback(&oxfw->tx_stream,
374 amdtp_domain_stop(&oxfw->domain);
376 cmp_connection_break(&oxfw->in_conn);
377 if (oxfw->has_output)
378 cmp_connection_break(&oxfw->out_conn);
383 void snd_oxfw_stream_stop_duplex(struct snd_oxfw *oxfw)
385 if (oxfw->substreams_count == 0) {
386 amdtp_domain_stop(&oxfw->domain);
388 cmp_connection_break(&oxfw->in_conn);
389 cmp_connection_release(&oxfw->in_conn);
391 if (oxfw->has_output) {
392 cmp_connection_break(&oxfw->out_conn);
393 cmp_connection_release(&oxfw->out_conn);
398 static void destroy_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
400 struct cmp_connection *conn;
402 if (stream == &oxfw->tx_stream)
403 conn = &oxfw->out_conn;
405 conn = &oxfw->in_conn;
407 amdtp_stream_destroy(stream);
408 cmp_connection_destroy(conn);
411 int snd_oxfw_stream_init_duplex(struct snd_oxfw *oxfw)
415 err = init_stream(oxfw, &oxfw->rx_stream);
419 if (oxfw->has_output) {
420 err = init_stream(oxfw, &oxfw->tx_stream);
422 destroy_stream(oxfw, &oxfw->rx_stream);
427 err = amdtp_domain_init(&oxfw->domain);
429 destroy_stream(oxfw, &oxfw->rx_stream);
430 if (oxfw->has_output)
431 destroy_stream(oxfw, &oxfw->tx_stream);
437 // This function should be called before starting the stream or after stopping
439 void snd_oxfw_stream_destroy_duplex(struct snd_oxfw *oxfw)
441 amdtp_domain_destroy(&oxfw->domain);
443 destroy_stream(oxfw, &oxfw->rx_stream);
445 if (oxfw->has_output)
446 destroy_stream(oxfw, &oxfw->tx_stream);
449 void snd_oxfw_stream_update_duplex(struct snd_oxfw *oxfw)
451 amdtp_domain_stop(&oxfw->domain);
453 cmp_connection_break(&oxfw->in_conn);
455 amdtp_stream_pcm_abort(&oxfw->rx_stream);
457 if (oxfw->has_output) {
458 cmp_connection_break(&oxfw->out_conn);
460 amdtp_stream_pcm_abort(&oxfw->tx_stream);
464 int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
465 enum avc_general_plug_dir dir,
466 struct snd_oxfw_stream_formation *formation)
472 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
473 format = kmalloc(len, GFP_KERNEL);
477 err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
485 err = snd_oxfw_stream_parse_format(format, formation);
492 * See Table 6.16 - AM824 Stream Format
493 * Figure 6.19 - format_information field for AM824 Compound
494 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
495 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
497 int snd_oxfw_stream_parse_format(u8 *format,
498 struct snd_oxfw_stream_formation *formation)
500 unsigned int i, e, channels, type;
502 memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
505 * this module can support a hierarchy combination that:
506 * Root: Audio and Music (0x90)
507 * Level 1: AM824 Compound (0x40)
509 if ((format[0] != 0x90) || (format[1] != 0x40))
512 /* check the sampling rate */
513 for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
514 if (format[2] == avc_stream_rate_table[i])
517 if (i == ARRAY_SIZE(avc_stream_rate_table))
520 formation->rate = oxfw_rate_table[i];
522 for (e = 0; e < format[4]; e++) {
523 channels = format[5 + e * 2];
524 type = format[6 + e * 2];
527 /* IEC 60958 Conformant, currently handled as MBLA */
529 /* Multi Bit Linear Audio (Raw) */
531 formation->pcm += channels;
533 /* MIDI Conformant */
535 formation->midi = channels;
537 /* IEC 61937-3 to 7 */
543 /* Multi Bit Linear Audio */
544 case 0x07: /* DVD-Audio */
545 case 0x0c: /* High Precision */
547 case 0x08: /* (Plain) Raw */
548 case 0x09: /* (Plain) SACD */
549 case 0x0a: /* (Encoded) Raw */
550 case 0x0b: /* (Encoded) SACD */
551 /* SMPTE Time-Code conformant */
557 /* Synchronization Stream (Stereo Raw audio) */
562 return -ENXIO; /* not supported */
566 if (formation->pcm > AM824_MAX_CHANNELS_FOR_PCM ||
567 formation->midi > AM824_MAX_CHANNELS_FOR_MIDI)
574 assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
575 unsigned int pid, u8 *buf, unsigned int *len,
578 struct snd_oxfw_stream_formation formation;
582 /* get format at current sampling rate */
583 err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
585 dev_err(&oxfw->unit->device,
586 "fail to get current stream format for isoc %s plug %d:%d\n",
587 (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
592 /* parse and set stream format */
594 err = snd_oxfw_stream_parse_format(buf, &formation);
598 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
605 /* apply the format for each available sampling rate */
606 for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
607 if (formation.rate == oxfw_rate_table[i])
610 err = avc_general_inquiry_sig_fmt(oxfw->unit,
617 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
619 if (formats[eid] == NULL) {
623 formats[eid][2] = avc_stream_rate_table[i];
627 oxfw->assumed = true;
632 static int fill_stream_formats(struct snd_oxfw *oxfw,
633 enum avc_general_plug_dir dir,
637 unsigned int len, eid = 0;
638 struct snd_oxfw_stream_formation dummy;
641 buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
645 if (dir == AVC_GENERAL_PLUG_DIR_OUT)
646 formats = oxfw->tx_stream_formats;
648 formats = oxfw->rx_stream_formats;
650 /* get first entry */
651 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
652 err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
654 /* LIST subfunction is not implemented */
655 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
656 err = assume_stream_formats(oxfw, dir, pid, buf, &len,
659 } else if (err < 0) {
660 dev_err(&oxfw->unit->device,
661 "fail to get stream format %d for isoc %s plug %d:%d\n",
662 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
667 /* LIST subfunction is implemented */
668 while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
669 /* The format is too short. */
675 /* parse and set stream format */
676 err = snd_oxfw_stream_parse_format(buf, &dummy);
680 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, len,
688 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
689 err = avc_stream_get_format_list(oxfw->unit, dir, 0,
691 /* No entries remained. */
692 if (err == -EINVAL) {
695 } else if (err < 0) {
696 dev_err(&oxfw->unit->device,
697 "fail to get stream format %d for isoc %s plug %d:%d\n",
698 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
709 int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
711 u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
712 struct snd_oxfw_stream_formation formation;
717 /* the number of plugs for isoc in/out, ext in/out */
718 err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
720 dev_err(&oxfw->unit->device,
721 "fail to get info for isoc/external in/out plugs: %d\n",
724 } else if ((plugs[0] == 0) && (plugs[1] == 0)) {
729 /* use oPCR[0] if exists */
731 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
736 // The oPCR is not available for isoc communication.
739 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
740 format = oxfw->tx_stream_formats[i];
743 err = snd_oxfw_stream_parse_format(format,
748 /* Add one MIDI port. */
749 if (formation.midi > 0)
750 oxfw->midi_input_ports = 1;
753 oxfw->has_output = true;
757 /* use iPCR[0] if exists */
759 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
764 // The iPCR is not available for isoc communication.
767 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
768 format = oxfw->rx_stream_formats[i];
771 err = snd_oxfw_stream_parse_format(format,
776 /* Add one MIDI port. */
777 if (formation.midi > 0)
778 oxfw->midi_output_ports = 1;
781 oxfw->has_input = true;
788 void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
790 oxfw->dev_lock_changed = true;
791 wake_up(&oxfw->hwdep_wait);
794 int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
798 spin_lock_irq(&oxfw->lock);
800 /* user land lock this */
801 if (oxfw->dev_lock_count < 0) {
806 /* this is the first time */
807 if (oxfw->dev_lock_count++ == 0)
808 snd_oxfw_stream_lock_changed(oxfw);
811 spin_unlock_irq(&oxfw->lock);
815 void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
817 spin_lock_irq(&oxfw->lock);
819 if (WARN_ON(oxfw->dev_lock_count <= 0))
821 if (--oxfw->dev_lock_count == 0)
822 snd_oxfw_stream_lock_changed(oxfw);
824 spin_unlock_irq(&oxfw->lock);