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_stream_start(stream, conn->resources.channel, conn->speed);
119 cmp_connection_break(conn);
123 // Wait first packet.
124 if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) {
125 amdtp_stream_stop(stream);
126 cmp_connection_break(conn);
133 static int check_connection_used_by_others(struct snd_oxfw *oxfw,
134 struct amdtp_stream *stream)
136 struct cmp_connection *conn;
140 if (stream == &oxfw->tx_stream)
141 conn = &oxfw->out_conn;
143 conn = &oxfw->in_conn;
145 err = cmp_connection_check_used(conn, &used);
146 if ((err >= 0) && used && !amdtp_stream_running(stream)) {
147 dev_err(&oxfw->unit->device,
148 "Connection established by others: %cPCR[%d]\n",
149 (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
157 static int init_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
159 struct cmp_connection *conn;
160 enum cmp_direction c_dir;
161 enum amdtp_stream_direction s_dir;
164 if (stream == &oxfw->tx_stream) {
165 conn = &oxfw->out_conn;
167 s_dir = AMDTP_IN_STREAM;
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, CIP_NONBLOCKING);
180 cmp_connection_destroy(conn);
185 * OXFW starts to transmit packets with non-zero dbc.
186 * OXFW postpone transferring packets till handling any asynchronous
187 * packets. As a result, next isochronous packet includes more data
188 * blocks than IEC 61883-6 defines.
190 if (stream == &oxfw->tx_stream) {
191 oxfw->tx_stream.flags |= CIP_JUMBO_PAYLOAD;
193 oxfw->tx_stream.flags |= CIP_WRONG_DBS;
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)
255 struct snd_oxfw_stream_formation formation;
256 enum avc_general_plug_dir dir;
259 // Considering JACK/FFADO streaming:
260 // TODO: This can be removed hwdep functionality becomes popular.
261 err = check_connection_used_by_others(oxfw, &oxfw->rx_stream);
264 if (oxfw->has_output) {
265 err = check_connection_used_by_others(oxfw, &oxfw->tx_stream);
270 if (stream == &oxfw->tx_stream)
271 dir = AVC_GENERAL_PLUG_DIR_OUT;
273 dir = AVC_GENERAL_PLUG_DIR_IN;
275 err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
279 rate = formation.rate;
280 pcm_channels = formation.pcm;
282 if (formation.rate != rate || formation.pcm != pcm_channels) {
283 amdtp_stream_stop(&oxfw->rx_stream);
284 cmp_connection_break(&oxfw->in_conn);
285 cmp_connection_release(&oxfw->in_conn);
287 if (oxfw->has_output) {
288 amdtp_stream_stop(&oxfw->tx_stream);
289 cmp_connection_break(&oxfw->out_conn);
290 cmp_connection_release(&oxfw->out_conn);
294 if (oxfw->substreams_count == 0 ||
295 formation.rate != rate || formation.pcm != pcm_channels) {
296 err = set_stream_format(oxfw, stream, rate, pcm_channels);
298 dev_err(&oxfw->unit->device,
299 "fail to set stream format: %d\n", err);
303 err = keep_resources(oxfw, &oxfw->rx_stream);
307 if (oxfw->has_output) {
308 err = keep_resources(oxfw, &oxfw->tx_stream);
310 cmp_connection_release(&oxfw->in_conn);
319 int snd_oxfw_stream_start_duplex(struct snd_oxfw *oxfw)
323 if (oxfw->substreams_count == 0)
326 if (amdtp_streaming_error(&oxfw->rx_stream) ||
327 amdtp_streaming_error(&oxfw->tx_stream)) {
328 amdtp_stream_stop(&oxfw->rx_stream);
329 cmp_connection_break(&oxfw->in_conn);
331 if (oxfw->has_output) {
332 amdtp_stream_stop(&oxfw->tx_stream);
333 cmp_connection_break(&oxfw->out_conn);
337 if (!amdtp_stream_running(&oxfw->rx_stream)) {
338 err = start_stream(oxfw, &oxfw->rx_stream);
340 dev_err(&oxfw->unit->device,
341 "fail to start rx stream: %d\n", err);
346 if (oxfw->has_output) {
347 if (!amdtp_stream_running(&oxfw->tx_stream)) {
348 err = start_stream(oxfw, &oxfw->tx_stream);
350 dev_err(&oxfw->unit->device,
351 "fail to start tx stream: %d\n", err);
359 amdtp_stream_stop(&oxfw->rx_stream);
360 cmp_connection_break(&oxfw->in_conn);
361 if (oxfw->has_output) {
362 amdtp_stream_stop(&oxfw->tx_stream);
363 cmp_connection_break(&oxfw->out_conn);
368 void snd_oxfw_stream_stop_duplex(struct snd_oxfw *oxfw)
370 if (oxfw->substreams_count == 0) {
371 amdtp_stream_stop(&oxfw->rx_stream);
372 cmp_connection_break(&oxfw->in_conn);
373 cmp_connection_release(&oxfw->in_conn);
375 if (oxfw->has_output) {
376 amdtp_stream_stop(&oxfw->tx_stream);
377 cmp_connection_break(&oxfw->out_conn);
378 cmp_connection_release(&oxfw->out_conn);
383 static void destroy_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
385 struct cmp_connection *conn;
387 if (stream == &oxfw->tx_stream)
388 conn = &oxfw->out_conn;
390 conn = &oxfw->in_conn;
392 amdtp_stream_destroy(stream);
393 cmp_connection_destroy(conn);
396 int snd_oxfw_stream_init_duplex(struct snd_oxfw *oxfw)
400 err = init_stream(oxfw, &oxfw->rx_stream);
404 if (oxfw->has_output) {
405 err = init_stream(oxfw, &oxfw->tx_stream);
407 destroy_stream(oxfw, &oxfw->rx_stream);
415 // This function should be called before starting the stream or after stopping
417 void snd_oxfw_stream_destroy_duplex(struct snd_oxfw *oxfw)
419 destroy_stream(oxfw, &oxfw->rx_stream);
421 if (oxfw->has_output)
422 destroy_stream(oxfw, &oxfw->tx_stream);
425 void snd_oxfw_stream_update_duplex(struct snd_oxfw *oxfw)
427 amdtp_stream_stop(&oxfw->rx_stream);
428 cmp_connection_break(&oxfw->in_conn);
430 amdtp_stream_pcm_abort(&oxfw->rx_stream);
432 if (oxfw->has_output) {
433 amdtp_stream_stop(&oxfw->tx_stream);
434 cmp_connection_break(&oxfw->out_conn);
436 amdtp_stream_pcm_abort(&oxfw->tx_stream);
440 int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
441 enum avc_general_plug_dir dir,
442 struct snd_oxfw_stream_formation *formation)
448 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
449 format = kmalloc(len, GFP_KERNEL);
453 err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
461 err = snd_oxfw_stream_parse_format(format, formation);
468 * See Table 6.16 - AM824 Stream Format
469 * Figure 6.19 - format_information field for AM824 Compound
470 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
471 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
473 int snd_oxfw_stream_parse_format(u8 *format,
474 struct snd_oxfw_stream_formation *formation)
476 unsigned int i, e, channels, type;
478 memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
481 * this module can support a hierarchy combination that:
482 * Root: Audio and Music (0x90)
483 * Level 1: AM824 Compound (0x40)
485 if ((format[0] != 0x90) || (format[1] != 0x40))
488 /* check the sampling rate */
489 for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
490 if (format[2] == avc_stream_rate_table[i])
493 if (i == ARRAY_SIZE(avc_stream_rate_table))
496 formation->rate = oxfw_rate_table[i];
498 for (e = 0; e < format[4]; e++) {
499 channels = format[5 + e * 2];
500 type = format[6 + e * 2];
503 /* IEC 60958 Conformant, currently handled as MBLA */
505 /* Multi Bit Linear Audio (Raw) */
507 formation->pcm += channels;
509 /* MIDI Conformant */
511 formation->midi = channels;
513 /* IEC 61937-3 to 7 */
519 /* Multi Bit Linear Audio */
520 case 0x07: /* DVD-Audio */
521 case 0x0c: /* High Precision */
523 case 0x08: /* (Plain) Raw */
524 case 0x09: /* (Plain) SACD */
525 case 0x0a: /* (Encoded) Raw */
526 case 0x0b: /* (Encoded) SACD */
527 /* SMPTE Time-Code conformant */
533 /* Synchronization Stream (Stereo Raw audio) */
538 return -ENOSYS; /* not supported */
542 if (formation->pcm > AM824_MAX_CHANNELS_FOR_PCM ||
543 formation->midi > AM824_MAX_CHANNELS_FOR_MIDI)
550 assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
551 unsigned int pid, u8 *buf, unsigned int *len,
554 struct snd_oxfw_stream_formation formation;
558 /* get format at current sampling rate */
559 err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
561 dev_err(&oxfw->unit->device,
562 "fail to get current stream format for isoc %s plug %d:%d\n",
563 (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
568 /* parse and set stream format */
570 err = snd_oxfw_stream_parse_format(buf, &formation);
574 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
581 /* apply the format for each available sampling rate */
582 for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
583 if (formation.rate == oxfw_rate_table[i])
586 err = avc_general_inquiry_sig_fmt(oxfw->unit,
593 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
595 if (formats[eid] == NULL) {
599 formats[eid][2] = avc_stream_rate_table[i];
603 oxfw->assumed = true;
608 static int fill_stream_formats(struct snd_oxfw *oxfw,
609 enum avc_general_plug_dir dir,
613 unsigned int len, eid = 0;
614 struct snd_oxfw_stream_formation dummy;
617 buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
621 if (dir == AVC_GENERAL_PLUG_DIR_OUT)
622 formats = oxfw->tx_stream_formats;
624 formats = oxfw->rx_stream_formats;
626 /* get first entry */
627 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
628 err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
629 if (err == -ENOSYS) {
630 /* LIST subfunction is not implemented */
631 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
632 err = assume_stream_formats(oxfw, dir, pid, buf, &len,
635 } else if (err < 0) {
636 dev_err(&oxfw->unit->device,
637 "fail to get stream format %d for isoc %s plug %d:%d\n",
638 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
643 /* LIST subfunction is implemented */
644 while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
645 /* The format is too short. */
651 /* parse and set stream format */
652 err = snd_oxfw_stream_parse_format(buf, &dummy);
656 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, len,
664 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
665 err = avc_stream_get_format_list(oxfw->unit, dir, 0,
667 /* No entries remained. */
668 if (err == -EINVAL) {
671 } else if (err < 0) {
672 dev_err(&oxfw->unit->device,
673 "fail to get stream format %d for isoc %s plug %d:%d\n",
674 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
685 int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
687 u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
688 struct snd_oxfw_stream_formation formation;
693 /* the number of plugs for isoc in/out, ext in/out */
694 err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
696 dev_err(&oxfw->unit->device,
697 "fail to get info for isoc/external in/out plugs: %d\n",
700 } else if ((plugs[0] == 0) && (plugs[1] == 0)) {
705 /* use oPCR[0] if exists */
707 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
711 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
712 format = oxfw->tx_stream_formats[i];
715 err = snd_oxfw_stream_parse_format(format, &formation);
719 /* Add one MIDI port. */
720 if (formation.midi > 0)
721 oxfw->midi_input_ports = 1;
724 oxfw->has_output = true;
727 /* use iPCR[0] if exists */
729 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
733 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
734 format = oxfw->rx_stream_formats[i];
737 err = snd_oxfw_stream_parse_format(format, &formation);
741 /* Add one MIDI port. */
742 if (formation.midi > 0)
743 oxfw->midi_output_ports = 1;
750 void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
752 oxfw->dev_lock_changed = true;
753 wake_up(&oxfw->hwdep_wait);
756 int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
760 spin_lock_irq(&oxfw->lock);
762 /* user land lock this */
763 if (oxfw->dev_lock_count < 0) {
768 /* this is the first time */
769 if (oxfw->dev_lock_count++ == 0)
770 snd_oxfw_stream_lock_changed(oxfw);
773 spin_unlock_irq(&oxfw->lock);
777 void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
779 spin_lock_irq(&oxfw->lock);
781 if (WARN_ON(oxfw->dev_lock_count <= 0))
783 if (--oxfw->dev_lock_count == 0)
784 snd_oxfw_stream_lock_changed(oxfw);
786 spin_unlock_irq(&oxfw->lock);