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 void stop_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
105 amdtp_stream_pcm_abort(stream);
106 amdtp_stream_stop(stream);
108 if (stream == &oxfw->tx_stream)
109 cmp_connection_break(&oxfw->out_conn);
111 cmp_connection_break(&oxfw->in_conn);
114 static int start_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream,
115 unsigned int rate, unsigned int pcm_channels)
118 struct cmp_connection *conn;
119 struct snd_oxfw_stream_formation formation;
120 unsigned int i, midi_ports;
123 if (stream == &oxfw->rx_stream) {
124 formats = oxfw->rx_stream_formats;
125 conn = &oxfw->in_conn;
127 formats = oxfw->tx_stream_formats;
128 conn = &oxfw->out_conn;
131 /* Get stream format */
132 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
133 if (formats[i] == NULL)
136 err = snd_oxfw_stream_parse_format(formats[i], &formation);
139 if (rate != formation.rate)
141 if (pcm_channels == 0 || pcm_channels == formation.pcm)
144 if (i == SND_OXFW_STREAM_FORMAT_ENTRIES) {
149 pcm_channels = formation.pcm;
150 midi_ports = formation.midi * 8;
152 /* The stream should have one pcm channels at least */
153 if (pcm_channels == 0) {
157 err = amdtp_am824_set_parameters(stream, rate, pcm_channels, midi_ports,
162 err = cmp_connection_establish(conn,
163 amdtp_stream_get_max_payload(stream));
167 err = amdtp_stream_start(stream,
168 conn->resources.channel,
171 cmp_connection_break(conn);
175 /* Wait first packet */
176 if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) {
177 stop_stream(oxfw, stream);
184 static int check_connection_used_by_others(struct snd_oxfw *oxfw,
185 struct amdtp_stream *stream)
187 struct cmp_connection *conn;
191 if (stream == &oxfw->tx_stream)
192 conn = &oxfw->out_conn;
194 conn = &oxfw->in_conn;
196 err = cmp_connection_check_used(conn, &used);
197 if ((err >= 0) && used && !amdtp_stream_running(stream)) {
198 dev_err(&oxfw->unit->device,
199 "Connection established by others: %cPCR[%d]\n",
200 (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
208 int snd_oxfw_stream_init_simplex(struct snd_oxfw *oxfw,
209 struct amdtp_stream *stream)
211 struct cmp_connection *conn;
212 enum cmp_direction c_dir;
213 enum amdtp_stream_direction s_dir;
216 if (stream == &oxfw->tx_stream) {
217 conn = &oxfw->out_conn;
219 s_dir = AMDTP_IN_STREAM;
221 conn = &oxfw->in_conn;
223 s_dir = AMDTP_OUT_STREAM;
226 err = cmp_connection_init(conn, oxfw->unit, c_dir, 0);
230 err = amdtp_am824_init(stream, oxfw->unit, s_dir, CIP_NONBLOCKING);
232 amdtp_stream_destroy(stream);
233 cmp_connection_destroy(conn);
238 * OXFW starts to transmit packets with non-zero dbc.
239 * OXFW postpone transferring packets till handling any asynchronous
240 * packets. As a result, next isochronous packet includes more data
241 * blocks than IEC 61883-6 defines.
243 if (stream == &oxfw->tx_stream) {
244 oxfw->tx_stream.flags |= CIP_JUMBO_PAYLOAD;
246 oxfw->tx_stream.flags |= CIP_WRONG_DBS;
252 int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw,
253 struct amdtp_stream *stream,
254 unsigned int rate, unsigned int pcm_channels)
256 struct amdtp_stream *opposite;
257 struct snd_oxfw_stream_formation formation;
258 enum avc_general_plug_dir dir;
259 unsigned int substreams, opposite_substreams;
262 if (stream == &oxfw->tx_stream) {
263 substreams = oxfw->capture_substreams;
264 opposite = &oxfw->rx_stream;
265 opposite_substreams = oxfw->playback_substreams;
266 dir = AVC_GENERAL_PLUG_DIR_OUT;
268 substreams = oxfw->playback_substreams;
269 opposite_substreams = oxfw->capture_substreams;
271 if (oxfw->has_output)
272 opposite = &oxfw->rx_stream;
276 dir = AVC_GENERAL_PLUG_DIR_IN;
283 * Considering JACK/FFADO streaming:
284 * TODO: This can be removed hwdep functionality becomes popular.
286 err = check_connection_used_by_others(oxfw, stream);
290 /* packet queueing error */
291 if (amdtp_streaming_error(stream))
292 stop_stream(oxfw, stream);
294 err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
298 rate = formation.rate;
299 if (pcm_channels == 0)
300 pcm_channels = formation.pcm;
302 if ((formation.rate != rate) || (formation.pcm != pcm_channels)) {
303 if (opposite != NULL) {
304 err = check_connection_used_by_others(oxfw, opposite);
307 stop_stream(oxfw, opposite);
309 stop_stream(oxfw, stream);
311 err = set_stream_format(oxfw, stream, rate, pcm_channels);
313 dev_err(&oxfw->unit->device,
314 "fail to set stream format: %d\n", err);
318 /* Start opposite stream if needed. */
319 if (opposite && !amdtp_stream_running(opposite) &&
320 (opposite_substreams > 0)) {
321 err = start_stream(oxfw, opposite, rate, 0);
323 dev_err(&oxfw->unit->device,
324 "fail to restart stream: %d\n", err);
330 /* Start requested stream. */
331 if (!amdtp_stream_running(stream)) {
332 err = start_stream(oxfw, stream, rate, pcm_channels);
334 dev_err(&oxfw->unit->device,
335 "fail to start stream: %d\n", err);
341 void snd_oxfw_stream_stop_simplex(struct snd_oxfw *oxfw,
342 struct amdtp_stream *stream)
344 if (((stream == &oxfw->tx_stream) && (oxfw->capture_substreams > 0)) ||
345 ((stream == &oxfw->rx_stream) && (oxfw->playback_substreams > 0)))
348 stop_stream(oxfw, stream);
352 * This function should be called before starting the stream or after stopping
355 void snd_oxfw_stream_destroy_simplex(struct snd_oxfw *oxfw,
356 struct amdtp_stream *stream)
358 struct cmp_connection *conn;
360 if (stream == &oxfw->tx_stream)
361 conn = &oxfw->out_conn;
363 conn = &oxfw->in_conn;
365 amdtp_stream_destroy(stream);
366 cmp_connection_destroy(conn);
369 void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw,
370 struct amdtp_stream *stream)
372 struct cmp_connection *conn;
374 if (stream == &oxfw->tx_stream)
375 conn = &oxfw->out_conn;
377 conn = &oxfw->in_conn;
379 if (cmp_connection_update(conn) < 0)
380 stop_stream(oxfw, stream);
382 amdtp_stream_update(stream);
385 int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
386 enum avc_general_plug_dir dir,
387 struct snd_oxfw_stream_formation *formation)
393 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
394 format = kmalloc(len, GFP_KERNEL);
398 err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
406 err = snd_oxfw_stream_parse_format(format, formation);
413 * See Table 6.16 - AM824 Stream Format
414 * Figure 6.19 - format_information field for AM824 Compound
415 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
416 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
418 int snd_oxfw_stream_parse_format(u8 *format,
419 struct snd_oxfw_stream_formation *formation)
421 unsigned int i, e, channels, type;
423 memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
426 * this module can support a hierarchy combination that:
427 * Root: Audio and Music (0x90)
428 * Level 1: AM824 Compound (0x40)
430 if ((format[0] != 0x90) || (format[1] != 0x40))
433 /* check the sampling rate */
434 for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
435 if (format[2] == avc_stream_rate_table[i])
438 if (i == ARRAY_SIZE(avc_stream_rate_table))
441 formation->rate = oxfw_rate_table[i];
443 for (e = 0; e < format[4]; e++) {
444 channels = format[5 + e * 2];
445 type = format[6 + e * 2];
448 /* IEC 60958 Conformant, currently handled as MBLA */
450 /* Multi Bit Linear Audio (Raw) */
452 formation->pcm += channels;
454 /* MIDI Conformant */
456 formation->midi = channels;
458 /* IEC 61937-3 to 7 */
464 /* Multi Bit Linear Audio */
465 case 0x07: /* DVD-Audio */
466 case 0x0c: /* High Precision */
468 case 0x08: /* (Plain) Raw */
469 case 0x09: /* (Plain) SACD */
470 case 0x0a: /* (Encoded) Raw */
471 case 0x0b: /* (Encoded) SACD */
472 /* SMPTE Time-Code conformant */
478 /* Synchronization Stream (Stereo Raw audio) */
483 return -ENOSYS; /* not supported */
487 if (formation->pcm > AM824_MAX_CHANNELS_FOR_PCM ||
488 formation->midi > AM824_MAX_CHANNELS_FOR_MIDI)
495 assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
496 unsigned int pid, u8 *buf, unsigned int *len,
499 struct snd_oxfw_stream_formation formation;
503 /* get format at current sampling rate */
504 err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
506 dev_err(&oxfw->unit->device,
507 "fail to get current stream format for isoc %s plug %d:%d\n",
508 (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
513 /* parse and set stream format */
515 err = snd_oxfw_stream_parse_format(buf, &formation);
519 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
526 /* apply the format for each available sampling rate */
527 for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
528 if (formation.rate == oxfw_rate_table[i])
531 err = avc_general_inquiry_sig_fmt(oxfw->unit,
538 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
540 if (formats[eid] == NULL) {
544 formats[eid][2] = avc_stream_rate_table[i];
548 oxfw->assumed = true;
553 static int fill_stream_formats(struct snd_oxfw *oxfw,
554 enum avc_general_plug_dir dir,
558 unsigned int len, eid = 0;
559 struct snd_oxfw_stream_formation dummy;
562 buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
566 if (dir == AVC_GENERAL_PLUG_DIR_OUT)
567 formats = oxfw->tx_stream_formats;
569 formats = oxfw->rx_stream_formats;
571 /* get first entry */
572 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
573 err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
574 if (err == -ENOSYS) {
575 /* LIST subfunction is not implemented */
576 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
577 err = assume_stream_formats(oxfw, dir, pid, buf, &len,
580 } else if (err < 0) {
581 dev_err(&oxfw->unit->device,
582 "fail to get stream format %d for isoc %s plug %d:%d\n",
583 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
588 /* LIST subfunction is implemented */
589 while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
590 /* The format is too short. */
596 /* parse and set stream format */
597 err = snd_oxfw_stream_parse_format(buf, &dummy);
601 formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, len,
609 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
610 err = avc_stream_get_format_list(oxfw->unit, dir, 0,
612 /* No entries remained. */
613 if (err == -EINVAL) {
616 } else if (err < 0) {
617 dev_err(&oxfw->unit->device,
618 "fail to get stream format %d for isoc %s plug %d:%d\n",
619 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
630 int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
632 u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
633 struct snd_oxfw_stream_formation formation;
638 /* the number of plugs for isoc in/out, ext in/out */
639 err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
641 dev_err(&oxfw->unit->device,
642 "fail to get info for isoc/external in/out plugs: %d\n",
645 } else if ((plugs[0] == 0) && (plugs[1] == 0)) {
650 /* use oPCR[0] if exists */
652 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
656 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
657 format = oxfw->tx_stream_formats[i];
660 err = snd_oxfw_stream_parse_format(format, &formation);
664 /* Add one MIDI port. */
665 if (formation.midi > 0)
666 oxfw->midi_input_ports = 1;
669 oxfw->has_output = true;
672 /* use iPCR[0] if exists */
674 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
678 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
679 format = oxfw->rx_stream_formats[i];
682 err = snd_oxfw_stream_parse_format(format, &formation);
686 /* Add one MIDI port. */
687 if (formation.midi > 0)
688 oxfw->midi_output_ports = 1;
695 void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
697 oxfw->dev_lock_changed = true;
698 wake_up(&oxfw->hwdep_wait);
701 int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
705 spin_lock_irq(&oxfw->lock);
707 /* user land lock this */
708 if (oxfw->dev_lock_count < 0) {
713 /* this is the first time */
714 if (oxfw->dev_lock_count++ == 0)
715 snd_oxfw_stream_lock_changed(oxfw);
718 spin_unlock_irq(&oxfw->lock);
722 void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
724 spin_lock_irq(&oxfw->lock);
726 if (WARN_ON(oxfw->dev_lock_count <= 0))
728 if (--oxfw->dev_lock_count == 0)
729 snd_oxfw_stream_lock_changed(oxfw);
731 spin_unlock_irq(&oxfw->lock);