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;
181 conn = &oxfw->in_conn;
183 s_dir = AMDTP_OUT_STREAM;
186 err = cmp_connection_init(conn, oxfw->unit, c_dir, 0);
190 err = amdtp_am824_init(stream, oxfw->unit, s_dir, flags);
192 cmp_connection_destroy(conn);
199 static int keep_resources(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
201 enum avc_general_plug_dir dir;
203 struct snd_oxfw_stream_formation formation;
204 struct cmp_connection *conn;
208 if (stream == &oxfw->rx_stream) {
209 dir = AVC_GENERAL_PLUG_DIR_IN;
210 formats = oxfw->rx_stream_formats;
211 conn = &oxfw->in_conn;
213 dir = AVC_GENERAL_PLUG_DIR_OUT;
214 formats = oxfw->tx_stream_formats;
215 conn = &oxfw->out_conn;
218 err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
222 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
223 struct snd_oxfw_stream_formation fmt;
225 if (formats[i] == NULL)
228 err = snd_oxfw_stream_parse_format(formats[i], &fmt);
232 if (fmt.rate == formation.rate && fmt.pcm == formation.pcm &&
233 fmt.midi == formation.midi)
236 if (i == SND_OXFW_STREAM_FORMAT_ENTRIES)
239 // The stream should have one pcm channels at least.
240 if (formation.pcm == 0)
243 err = amdtp_am824_set_parameters(stream, formation.rate, formation.pcm,
244 formation.midi * 8, false);
248 return cmp_connection_reserve(conn, amdtp_stream_get_max_payload(stream));
251 int snd_oxfw_stream_reserve_duplex(struct snd_oxfw *oxfw,
252 struct amdtp_stream *stream,
253 unsigned int rate, unsigned int pcm_channels,
254 unsigned int frames_per_period,
255 unsigned int frames_per_buffer)
257 struct snd_oxfw_stream_formation formation;
258 enum avc_general_plug_dir dir;
261 // Considering JACK/FFADO streaming:
262 // TODO: This can be removed hwdep functionality becomes popular.
263 err = check_connection_used_by_others(oxfw, &oxfw->rx_stream);
266 if (oxfw->has_output) {
267 err = check_connection_used_by_others(oxfw, &oxfw->tx_stream);
272 if (stream == &oxfw->tx_stream)
273 dir = AVC_GENERAL_PLUG_DIR_OUT;
275 dir = AVC_GENERAL_PLUG_DIR_IN;
277 err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
281 rate = formation.rate;
282 pcm_channels = formation.pcm;
284 if (formation.rate != rate || formation.pcm != pcm_channels) {
285 amdtp_domain_stop(&oxfw->domain);
287 cmp_connection_break(&oxfw->in_conn);
288 cmp_connection_release(&oxfw->in_conn);
290 if (oxfw->has_output) {
291 cmp_connection_break(&oxfw->out_conn);
292 cmp_connection_release(&oxfw->out_conn);
296 if (oxfw->substreams_count == 0 ||
297 formation.rate != rate || formation.pcm != pcm_channels) {
298 err = set_stream_format(oxfw, stream, rate, pcm_channels);
300 dev_err(&oxfw->unit->device,
301 "fail to set stream format: %d\n", err);
305 err = keep_resources(oxfw, &oxfw->rx_stream);
309 if (oxfw->has_output) {
310 err = keep_resources(oxfw, &oxfw->tx_stream);
312 cmp_connection_release(&oxfw->in_conn);
317 err = amdtp_domain_set_events_per_period(&oxfw->domain,
318 frames_per_period, frames_per_buffer);
320 cmp_connection_release(&oxfw->in_conn);
321 if (oxfw->has_output)
322 cmp_connection_release(&oxfw->out_conn);
330 int snd_oxfw_stream_start_duplex(struct snd_oxfw *oxfw)
334 if (oxfw->substreams_count == 0)
337 if (amdtp_streaming_error(&oxfw->rx_stream) ||
338 amdtp_streaming_error(&oxfw->tx_stream)) {
339 amdtp_domain_stop(&oxfw->domain);
341 cmp_connection_break(&oxfw->in_conn);
342 if (oxfw->has_output)
343 cmp_connection_break(&oxfw->out_conn);
346 if (!amdtp_stream_running(&oxfw->rx_stream)) {
347 unsigned int tx_init_skip_cycles = 0;
348 bool replay_seq = false;
350 err = start_stream(oxfw, &oxfw->rx_stream);
352 dev_err(&oxfw->unit->device,
353 "fail to prepare rx stream: %d\n", err);
357 if (oxfw->has_output &&
358 !amdtp_stream_running(&oxfw->tx_stream)) {
359 err = start_stream(oxfw, &oxfw->tx_stream);
361 dev_err(&oxfw->unit->device,
362 "fail to prepare tx stream: %d\n", err);
366 if (oxfw->quirks & SND_OXFW_QUIRK_JUMBO_PAYLOAD) {
367 // Just after changing sampling transfer frequency, many cycles are
368 // skipped for packet transmission.
369 tx_init_skip_cycles = 400;
370 } else if (oxfw->quirks & SND_OXFW_QUIRK_VOLUNTARY_RECOVERY) {
371 // It takes a bit time for target device to adjust event frequency
372 // according to nominal event frequency in isochronous packets from
374 tx_init_skip_cycles = 4000;
380 // NOTE: The device ignores presentation time expressed by the value of syt field
381 // of CIP header in received packets. The sequence of the number of data blocks per
382 // packet is important for media clock recovery.
383 err = amdtp_domain_start(&oxfw->domain, tx_init_skip_cycles, replay_seq, false);
387 if (!amdtp_domain_wait_ready(&oxfw->domain, READY_TIMEOUT_MS)) {
395 amdtp_domain_stop(&oxfw->domain);
397 cmp_connection_break(&oxfw->in_conn);
398 if (oxfw->has_output)
399 cmp_connection_break(&oxfw->out_conn);
404 void snd_oxfw_stream_stop_duplex(struct snd_oxfw *oxfw)
406 if (oxfw->substreams_count == 0) {
407 amdtp_domain_stop(&oxfw->domain);
409 cmp_connection_break(&oxfw->in_conn);
410 cmp_connection_release(&oxfw->in_conn);
412 if (oxfw->has_output) {
413 cmp_connection_break(&oxfw->out_conn);
414 cmp_connection_release(&oxfw->out_conn);
419 static void destroy_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
421 struct cmp_connection *conn;
423 if (stream == &oxfw->tx_stream)
424 conn = &oxfw->out_conn;
426 conn = &oxfw->in_conn;
428 amdtp_stream_destroy(stream);
429 cmp_connection_destroy(conn);
432 int snd_oxfw_stream_init_duplex(struct snd_oxfw *oxfw)
436 err = init_stream(oxfw, &oxfw->rx_stream);
440 if (oxfw->has_output) {
441 err = init_stream(oxfw, &oxfw->tx_stream);
443 destroy_stream(oxfw, &oxfw->rx_stream);
448 err = amdtp_domain_init(&oxfw->domain);
450 destroy_stream(oxfw, &oxfw->rx_stream);
451 if (oxfw->has_output)
452 destroy_stream(oxfw, &oxfw->tx_stream);
458 // This function should be called before starting the stream or after stopping
460 void snd_oxfw_stream_destroy_duplex(struct snd_oxfw *oxfw)
462 amdtp_domain_destroy(&oxfw->domain);
464 destroy_stream(oxfw, &oxfw->rx_stream);
466 if (oxfw->has_output)
467 destroy_stream(oxfw, &oxfw->tx_stream);
470 void snd_oxfw_stream_update_duplex(struct snd_oxfw *oxfw)
472 amdtp_domain_stop(&oxfw->domain);
474 cmp_connection_break(&oxfw->in_conn);
476 amdtp_stream_pcm_abort(&oxfw->rx_stream);
478 if (oxfw->has_output) {
479 cmp_connection_break(&oxfw->out_conn);
481 amdtp_stream_pcm_abort(&oxfw->tx_stream);
485 int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
486 enum avc_general_plug_dir dir,
487 struct snd_oxfw_stream_formation *formation)
493 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
494 format = kmalloc(len, GFP_KERNEL);
498 err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
506 err = snd_oxfw_stream_parse_format(format, formation);
513 * See Table 6.16 - AM824 Stream Format
514 * Figure 6.19 - format_information field for AM824 Compound
515 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
516 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
518 int snd_oxfw_stream_parse_format(u8 *format,
519 struct snd_oxfw_stream_formation *formation)
521 unsigned int i, e, channels, type;
523 memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
526 * this module can support a hierarchy combination that:
527 * Root: Audio and Music (0x90)
528 * Level 1: AM824 Compound (0x40)
530 if ((format[0] != 0x90) || (format[1] != 0x40))
533 /* check the sampling rate */
534 for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
535 if (format[2] == avc_stream_rate_table[i])
538 if (i == ARRAY_SIZE(avc_stream_rate_table))
541 formation->rate = oxfw_rate_table[i];
543 for (e = 0; e < format[4]; e++) {
544 channels = format[5 + e * 2];
545 type = format[6 + e * 2];
548 /* IEC 60958 Conformant, currently handled as MBLA */
550 /* Multi Bit Linear Audio (Raw) */
552 formation->pcm += channels;
554 /* MIDI Conformant */
556 formation->midi = channels;
558 /* IEC 61937-3 to 7 */
564 /* Multi Bit Linear Audio */
565 case 0x07: /* DVD-Audio */
566 case 0x0c: /* High Precision */
568 case 0x08: /* (Plain) Raw */
569 case 0x09: /* (Plain) SACD */
570 case 0x0a: /* (Encoded) Raw */
571 case 0x0b: /* (Encoded) SACD */
572 /* SMPTE Time-Code conformant */
578 /* Synchronization Stream (Stereo Raw audio) */
583 return -ENXIO; /* not supported */
587 if (formation->pcm > AM824_MAX_CHANNELS_FOR_PCM ||
588 formation->midi > AM824_MAX_CHANNELS_FOR_MIDI)
595 assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
596 unsigned int pid, u8 *buf, unsigned int *len,
599 struct snd_oxfw_stream_formation formation;
603 /* get format at current sampling rate */
604 err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
606 dev_err(&oxfw->unit->device,
607 "fail to get current stream format for isoc %s plug %d:%d\n",
608 (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
613 /* parse and set stream format */
615 err = snd_oxfw_stream_parse_format(buf, &formation);
619 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
626 /* apply the format for each available sampling rate */
627 for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
628 if (formation.rate == oxfw_rate_table[i])
631 err = avc_general_inquiry_sig_fmt(oxfw->unit,
638 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
640 if (formats[eid] == NULL) {
644 formats[eid][2] = avc_stream_rate_table[i];
648 oxfw->assumed = true;
653 static int fill_stream_formats(struct snd_oxfw *oxfw,
654 enum avc_general_plug_dir dir,
658 unsigned int len, eid = 0;
659 struct snd_oxfw_stream_formation dummy;
662 buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
666 if (dir == AVC_GENERAL_PLUG_DIR_OUT)
667 formats = oxfw->tx_stream_formats;
669 formats = oxfw->rx_stream_formats;
671 /* get first entry */
672 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
673 err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
675 /* LIST subfunction is not implemented */
676 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
677 err = assume_stream_formats(oxfw, dir, pid, buf, &len,
680 } else if (err < 0) {
681 dev_err(&oxfw->unit->device,
682 "fail to get stream format %d for isoc %s plug %d:%d\n",
683 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
688 /* LIST subfunction is implemented */
689 while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
690 /* The format is too short. */
696 /* parse and set stream format */
697 err = snd_oxfw_stream_parse_format(buf, &dummy);
701 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, len,
709 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
710 err = avc_stream_get_format_list(oxfw->unit, dir, 0,
712 /* No entries remained. */
713 if (err == -EINVAL) {
716 } else if (err < 0) {
717 dev_err(&oxfw->unit->device,
718 "fail to get stream format %d for isoc %s plug %d:%d\n",
719 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
730 int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
732 u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
733 struct snd_oxfw_stream_formation formation;
738 /* the number of plugs for isoc in/out, ext in/out */
739 err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
741 dev_err(&oxfw->unit->device,
742 "fail to get info for isoc/external in/out plugs: %d\n",
745 } else if ((plugs[0] == 0) && (plugs[1] == 0)) {
750 /* use oPCR[0] if exists */
752 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
757 // The oPCR is not available for isoc communication.
760 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
761 format = oxfw->tx_stream_formats[i];
764 err = snd_oxfw_stream_parse_format(format,
769 /* Add one MIDI port. */
770 if (formation.midi > 0)
771 oxfw->midi_input_ports = 1;
774 oxfw->has_output = true;
778 /* use iPCR[0] if exists */
780 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
785 // The iPCR is not available for isoc communication.
788 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
789 format = oxfw->rx_stream_formats[i];
792 err = snd_oxfw_stream_parse_format(format,
797 /* Add one MIDI port. */
798 if (formation.midi > 0)
799 oxfw->midi_output_ports = 1;
802 oxfw->has_input = true;
809 void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
811 oxfw->dev_lock_changed = true;
812 wake_up(&oxfw->hwdep_wait);
815 int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
819 spin_lock_irq(&oxfw->lock);
821 /* user land lock this */
822 if (oxfw->dev_lock_count < 0) {
827 /* this is the first time */
828 if (oxfw->dev_lock_count++ == 0)
829 snd_oxfw_stream_lock_changed(oxfw);
832 spin_unlock_irq(&oxfw->lock);
836 void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
838 spin_lock_irq(&oxfw->lock);
840 if (WARN_ON(oxfw->dev_lock_count <= 0))
842 if (--oxfw->dev_lock_count == 0)
843 snd_oxfw_stream_lock_changed(oxfw);
845 spin_unlock_irq(&oxfw->lock);