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)
249 struct snd_oxfw_stream_formation formation;
250 enum avc_general_plug_dir dir;
253 // Considering JACK/FFADO streaming:
254 // TODO: This can be removed hwdep functionality becomes popular.
255 err = check_connection_used_by_others(oxfw, &oxfw->rx_stream);
258 if (oxfw->has_output) {
259 err = check_connection_used_by_others(oxfw, &oxfw->tx_stream);
264 if (stream == &oxfw->tx_stream)
265 dir = AVC_GENERAL_PLUG_DIR_OUT;
267 dir = AVC_GENERAL_PLUG_DIR_IN;
269 err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
273 rate = formation.rate;
274 pcm_channels = formation.pcm;
276 if (formation.rate != rate || formation.pcm != pcm_channels) {
277 amdtp_domain_stop(&oxfw->domain);
279 cmp_connection_break(&oxfw->in_conn);
280 cmp_connection_release(&oxfw->in_conn);
282 if (oxfw->has_output) {
283 cmp_connection_break(&oxfw->out_conn);
284 cmp_connection_release(&oxfw->out_conn);
288 if (oxfw->substreams_count == 0 ||
289 formation.rate != rate || formation.pcm != pcm_channels) {
290 err = set_stream_format(oxfw, stream, rate, pcm_channels);
292 dev_err(&oxfw->unit->device,
293 "fail to set stream format: %d\n", err);
297 err = keep_resources(oxfw, &oxfw->rx_stream);
301 if (oxfw->has_output) {
302 err = keep_resources(oxfw, &oxfw->tx_stream);
304 cmp_connection_release(&oxfw->in_conn);
313 int snd_oxfw_stream_start_duplex(struct snd_oxfw *oxfw)
317 if (oxfw->substreams_count == 0)
320 if (amdtp_streaming_error(&oxfw->rx_stream) ||
321 amdtp_streaming_error(&oxfw->tx_stream)) {
322 amdtp_domain_stop(&oxfw->domain);
324 cmp_connection_break(&oxfw->in_conn);
325 if (oxfw->has_output)
326 cmp_connection_break(&oxfw->out_conn);
329 if (!amdtp_stream_running(&oxfw->rx_stream)) {
330 err = start_stream(oxfw, &oxfw->rx_stream);
332 dev_err(&oxfw->unit->device,
333 "fail to prepare rx stream: %d\n", err);
337 if (oxfw->has_output &&
338 !amdtp_stream_running(&oxfw->tx_stream)) {
339 err = start_stream(oxfw, &oxfw->tx_stream);
341 dev_err(&oxfw->unit->device,
342 "fail to prepare tx stream: %d\n", err);
347 err = amdtp_domain_start(&oxfw->domain);
351 // Wait first packet.
352 if (!amdtp_stream_wait_callback(&oxfw->rx_stream,
358 if (oxfw->has_output) {
359 if (!amdtp_stream_wait_callback(&oxfw->tx_stream,
369 amdtp_domain_stop(&oxfw->domain);
371 cmp_connection_break(&oxfw->in_conn);
372 if (oxfw->has_output)
373 cmp_connection_break(&oxfw->out_conn);
378 void snd_oxfw_stream_stop_duplex(struct snd_oxfw *oxfw)
380 if (oxfw->substreams_count == 0) {
381 amdtp_domain_stop(&oxfw->domain);
383 cmp_connection_break(&oxfw->in_conn);
384 cmp_connection_release(&oxfw->in_conn);
386 if (oxfw->has_output) {
387 cmp_connection_break(&oxfw->out_conn);
388 cmp_connection_release(&oxfw->out_conn);
393 static void destroy_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
395 struct cmp_connection *conn;
397 if (stream == &oxfw->tx_stream)
398 conn = &oxfw->out_conn;
400 conn = &oxfw->in_conn;
402 amdtp_stream_destroy(stream);
403 cmp_connection_destroy(conn);
406 int snd_oxfw_stream_init_duplex(struct snd_oxfw *oxfw)
410 err = init_stream(oxfw, &oxfw->rx_stream);
414 if (oxfw->has_output) {
415 err = init_stream(oxfw, &oxfw->tx_stream);
417 destroy_stream(oxfw, &oxfw->rx_stream);
422 err = amdtp_domain_init(&oxfw->domain);
424 destroy_stream(oxfw, &oxfw->rx_stream);
425 if (oxfw->has_output)
426 destroy_stream(oxfw, &oxfw->tx_stream);
432 // This function should be called before starting the stream or after stopping
434 void snd_oxfw_stream_destroy_duplex(struct snd_oxfw *oxfw)
436 amdtp_domain_destroy(&oxfw->domain);
438 destroy_stream(oxfw, &oxfw->rx_stream);
440 if (oxfw->has_output)
441 destroy_stream(oxfw, &oxfw->tx_stream);
444 void snd_oxfw_stream_update_duplex(struct snd_oxfw *oxfw)
446 amdtp_domain_stop(&oxfw->domain);
448 cmp_connection_break(&oxfw->in_conn);
450 amdtp_stream_pcm_abort(&oxfw->rx_stream);
452 if (oxfw->has_output) {
453 cmp_connection_break(&oxfw->out_conn);
455 amdtp_stream_pcm_abort(&oxfw->tx_stream);
459 int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
460 enum avc_general_plug_dir dir,
461 struct snd_oxfw_stream_formation *formation)
467 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
468 format = kmalloc(len, GFP_KERNEL);
472 err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
480 err = snd_oxfw_stream_parse_format(format, formation);
487 * See Table 6.16 - AM824 Stream Format
488 * Figure 6.19 - format_information field for AM824 Compound
489 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
490 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
492 int snd_oxfw_stream_parse_format(u8 *format,
493 struct snd_oxfw_stream_formation *formation)
495 unsigned int i, e, channels, type;
497 memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
500 * this module can support a hierarchy combination that:
501 * Root: Audio and Music (0x90)
502 * Level 1: AM824 Compound (0x40)
504 if ((format[0] != 0x90) || (format[1] != 0x40))
507 /* check the sampling rate */
508 for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
509 if (format[2] == avc_stream_rate_table[i])
512 if (i == ARRAY_SIZE(avc_stream_rate_table))
515 formation->rate = oxfw_rate_table[i];
517 for (e = 0; e < format[4]; e++) {
518 channels = format[5 + e * 2];
519 type = format[6 + e * 2];
522 /* IEC 60958 Conformant, currently handled as MBLA */
524 /* Multi Bit Linear Audio (Raw) */
526 formation->pcm += channels;
528 /* MIDI Conformant */
530 formation->midi = channels;
532 /* IEC 61937-3 to 7 */
538 /* Multi Bit Linear Audio */
539 case 0x07: /* DVD-Audio */
540 case 0x0c: /* High Precision */
542 case 0x08: /* (Plain) Raw */
543 case 0x09: /* (Plain) SACD */
544 case 0x0a: /* (Encoded) Raw */
545 case 0x0b: /* (Encoded) SACD */
546 /* SMPTE Time-Code conformant */
552 /* Synchronization Stream (Stereo Raw audio) */
557 return -ENOSYS; /* not supported */
561 if (formation->pcm > AM824_MAX_CHANNELS_FOR_PCM ||
562 formation->midi > AM824_MAX_CHANNELS_FOR_MIDI)
569 assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
570 unsigned int pid, u8 *buf, unsigned int *len,
573 struct snd_oxfw_stream_formation formation;
577 /* get format at current sampling rate */
578 err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
580 dev_err(&oxfw->unit->device,
581 "fail to get current stream format for isoc %s plug %d:%d\n",
582 (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
587 /* parse and set stream format */
589 err = snd_oxfw_stream_parse_format(buf, &formation);
593 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
600 /* apply the format for each available sampling rate */
601 for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
602 if (formation.rate == oxfw_rate_table[i])
605 err = avc_general_inquiry_sig_fmt(oxfw->unit,
612 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
614 if (formats[eid] == NULL) {
618 formats[eid][2] = avc_stream_rate_table[i];
622 oxfw->assumed = true;
627 static int fill_stream_formats(struct snd_oxfw *oxfw,
628 enum avc_general_plug_dir dir,
632 unsigned int len, eid = 0;
633 struct snd_oxfw_stream_formation dummy;
636 buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
640 if (dir == AVC_GENERAL_PLUG_DIR_OUT)
641 formats = oxfw->tx_stream_formats;
643 formats = oxfw->rx_stream_formats;
645 /* get first entry */
646 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
647 err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
648 if (err == -ENOSYS) {
649 /* LIST subfunction is not implemented */
650 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
651 err = assume_stream_formats(oxfw, dir, pid, buf, &len,
654 } else if (err < 0) {
655 dev_err(&oxfw->unit->device,
656 "fail to get stream format %d for isoc %s plug %d:%d\n",
657 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
662 /* LIST subfunction is implemented */
663 while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
664 /* The format is too short. */
670 /* parse and set stream format */
671 err = snd_oxfw_stream_parse_format(buf, &dummy);
675 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, len,
683 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
684 err = avc_stream_get_format_list(oxfw->unit, dir, 0,
686 /* No entries remained. */
687 if (err == -EINVAL) {
690 } else if (err < 0) {
691 dev_err(&oxfw->unit->device,
692 "fail to get stream format %d for isoc %s plug %d:%d\n",
693 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
704 int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
706 u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
707 struct snd_oxfw_stream_formation formation;
712 /* the number of plugs for isoc in/out, ext in/out */
713 err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
715 dev_err(&oxfw->unit->device,
716 "fail to get info for isoc/external in/out plugs: %d\n",
719 } else if ((plugs[0] == 0) && (plugs[1] == 0)) {
724 /* use oPCR[0] if exists */
726 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
730 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
731 format = oxfw->tx_stream_formats[i];
734 err = snd_oxfw_stream_parse_format(format, &formation);
738 /* Add one MIDI port. */
739 if (formation.midi > 0)
740 oxfw->midi_input_ports = 1;
743 oxfw->has_output = true;
746 /* use iPCR[0] if exists */
748 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
752 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
753 format = oxfw->rx_stream_formats[i];
756 err = snd_oxfw_stream_parse_format(format, &formation);
760 /* Add one MIDI port. */
761 if (formation.midi > 0)
762 oxfw->midi_output_ports = 1;
769 void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
771 oxfw->dev_lock_changed = true;
772 wake_up(&oxfw->hwdep_wait);
775 int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
779 spin_lock_irq(&oxfw->lock);
781 /* user land lock this */
782 if (oxfw->dev_lock_count < 0) {
787 /* this is the first time */
788 if (oxfw->dev_lock_count++ == 0)
789 snd_oxfw_stream_lock_changed(oxfw);
792 spin_unlock_irq(&oxfw->lock);
796 void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
798 spin_lock_irq(&oxfw->lock);
800 if (WARN_ON(oxfw->dev_lock_count <= 0))
802 if (--oxfw->dev_lock_count == 0)
803 snd_oxfw_stream_lock_changed(oxfw);
805 spin_unlock_irq(&oxfw->lock);