xref: /linux/sound/firewire/oxfw/oxfw-stream.c (revision 029ffc4294401fc7fbc45dd4728f26148ba499e4)
1da607e19SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e2786ca6STakashi Sakamoto /*
3e2786ca6STakashi Sakamoto  * oxfw_stream.c - a part of driver for OXFW970/971 based devices
4e2786ca6STakashi Sakamoto  *
5e2786ca6STakashi Sakamoto  * Copyright (c) 2014 Takashi Sakamoto
6e2786ca6STakashi Sakamoto  */
7e2786ca6STakashi Sakamoto 
8e2786ca6STakashi Sakamoto #include "oxfw.h"
9f3699e2cSTakashi Sakamoto #include <linux/delay.h>
10e2786ca6STakashi Sakamoto 
115cd1d3f4STakashi Sakamoto #define AVC_GENERIC_FRAME_MAXIMUM_BYTES	512
12bdaedca7STakashi Sakamoto #define READY_TIMEOUT_MS	200
135cd1d3f4STakashi Sakamoto 
145cd1d3f4STakashi Sakamoto /*
155cd1d3f4STakashi Sakamoto  * According to datasheet of Oxford Semiconductor:
165cd1d3f4STakashi Sakamoto  *  OXFW970: 32.0/44.1/48.0/96.0 Khz, 8 audio channels I/O
175cd1d3f4STakashi Sakamoto  *  OXFW971: 32.0/44.1/48.0/88.2/96.0/192.0 kHz, 16 audio channels I/O, MIDI I/O
185cd1d3f4STakashi Sakamoto  */
195cd1d3f4STakashi Sakamoto static const unsigned int oxfw_rate_table[] = {
205cd1d3f4STakashi Sakamoto 	[0] = 32000,
215cd1d3f4STakashi Sakamoto 	[1] = 44100,
225cd1d3f4STakashi Sakamoto 	[2] = 48000,
235cd1d3f4STakashi Sakamoto 	[3] = 88200,
245cd1d3f4STakashi Sakamoto 	[4] = 96000,
255cd1d3f4STakashi Sakamoto 	[5] = 192000,
265cd1d3f4STakashi Sakamoto };
275cd1d3f4STakashi Sakamoto 
285cd1d3f4STakashi Sakamoto /*
295cd1d3f4STakashi Sakamoto  * See Table 5.7 – Sampling frequency for Multi-bit Audio
305cd1d3f4STakashi Sakamoto  * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
315cd1d3f4STakashi Sakamoto  */
325cd1d3f4STakashi Sakamoto static const unsigned int avc_stream_rate_table[] = {
335cd1d3f4STakashi Sakamoto 	[0] = 0x02,
345cd1d3f4STakashi Sakamoto 	[1] = 0x03,
355cd1d3f4STakashi Sakamoto 	[2] = 0x04,
365cd1d3f4STakashi Sakamoto 	[3] = 0x0a,
375cd1d3f4STakashi Sakamoto 	[4] = 0x05,
385cd1d3f4STakashi Sakamoto 	[5] = 0x07,
395cd1d3f4STakashi Sakamoto };
405cd1d3f4STakashi Sakamoto 
41b0ac0009STakashi Sakamoto static int set_rate(struct snd_oxfw *oxfw, unsigned int rate)
42b0ac0009STakashi Sakamoto {
43b0ac0009STakashi Sakamoto 	int err;
44b0ac0009STakashi Sakamoto 
45b0ac0009STakashi Sakamoto 	err = avc_general_set_sig_fmt(oxfw->unit, rate,
46b0ac0009STakashi Sakamoto 				      AVC_GENERAL_PLUG_DIR_IN, 0);
47b0ac0009STakashi Sakamoto 	if (err < 0)
48b0ac0009STakashi Sakamoto 		goto end;
49b0ac0009STakashi Sakamoto 
50b0ac0009STakashi Sakamoto 	if (oxfw->has_output)
51b0ac0009STakashi Sakamoto 		err = avc_general_set_sig_fmt(oxfw->unit, rate,
52b0ac0009STakashi Sakamoto 					      AVC_GENERAL_PLUG_DIR_OUT, 0);
53b0ac0009STakashi Sakamoto end:
54b0ac0009STakashi Sakamoto 	return err;
55b0ac0009STakashi Sakamoto }
56b0ac0009STakashi Sakamoto 
57f3699e2cSTakashi Sakamoto static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s,
58f3699e2cSTakashi Sakamoto 			     unsigned int rate, unsigned int pcm_channels)
59f3699e2cSTakashi Sakamoto {
60f3699e2cSTakashi Sakamoto 	u8 **formats;
61f3699e2cSTakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
62f3699e2cSTakashi Sakamoto 	enum avc_general_plug_dir dir;
635580ba7bSDan Carpenter 	unsigned int len;
645580ba7bSDan Carpenter 	int i, err;
65f3699e2cSTakashi Sakamoto 
66b0ac0009STakashi Sakamoto 	if (s == &oxfw->tx_stream) {
67b0ac0009STakashi Sakamoto 		formats = oxfw->tx_stream_formats;
68b0ac0009STakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_OUT;
69b0ac0009STakashi Sakamoto 	} else {
70f3699e2cSTakashi Sakamoto 		formats = oxfw->rx_stream_formats;
71f3699e2cSTakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_IN;
72b0ac0009STakashi Sakamoto 	}
73f3699e2cSTakashi Sakamoto 
74f3699e2cSTakashi Sakamoto 	/* Seek stream format for requirements. */
75f3699e2cSTakashi Sakamoto 	for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
76f3699e2cSTakashi Sakamoto 		err = snd_oxfw_stream_parse_format(formats[i], &formation);
77f3699e2cSTakashi Sakamoto 		if (err < 0)
78f3699e2cSTakashi Sakamoto 			return err;
79f3699e2cSTakashi Sakamoto 
80f3699e2cSTakashi Sakamoto 		if ((formation.rate == rate) && (formation.pcm == pcm_channels))
81f3699e2cSTakashi Sakamoto 			break;
82f3699e2cSTakashi Sakamoto 	}
83f3699e2cSTakashi Sakamoto 	if (i == SND_OXFW_STREAM_FORMAT_ENTRIES)
84f3699e2cSTakashi Sakamoto 		return -EINVAL;
85f3699e2cSTakashi Sakamoto 
86f3699e2cSTakashi Sakamoto 	/* If assumed, just change rate. */
87f3699e2cSTakashi Sakamoto 	if (oxfw->assumed)
88b0ac0009STakashi Sakamoto 		return set_rate(oxfw, rate);
89f3699e2cSTakashi Sakamoto 
90f3699e2cSTakashi Sakamoto 	/* Calculate format length. */
91f3699e2cSTakashi Sakamoto 	len = 5 + formats[i][4] * 2;
92f3699e2cSTakashi Sakamoto 
93f3699e2cSTakashi Sakamoto 	err = avc_stream_set_format(oxfw->unit, dir, 0, formats[i], len);
94f3699e2cSTakashi Sakamoto 	if (err < 0)
95f3699e2cSTakashi Sakamoto 		return err;
96f3699e2cSTakashi Sakamoto 
97f3699e2cSTakashi Sakamoto 	/* Some requests just after changing format causes freezing. */
98f3699e2cSTakashi Sakamoto 	msleep(100);
99f3699e2cSTakashi Sakamoto 
100f3699e2cSTakashi Sakamoto 	return 0;
101f3699e2cSTakashi Sakamoto }
102f3699e2cSTakashi Sakamoto 
103521b2e11STakashi Sakamoto static int start_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
104e2786ca6STakashi Sakamoto {
105f3699e2cSTakashi Sakamoto 	struct cmp_connection *conn;
106f3699e2cSTakashi Sakamoto 	int err;
107f3699e2cSTakashi Sakamoto 
1080356ce3aSTakashi Sakamoto 	if (stream == &oxfw->rx_stream)
109f3699e2cSTakashi Sakamoto 		conn = &oxfw->in_conn;
1100356ce3aSTakashi Sakamoto 	else
111b0ac0009STakashi Sakamoto 		conn = &oxfw->out_conn;
112f3699e2cSTakashi Sakamoto 
1137bc93821STakashi Sakamoto 	err = cmp_connection_establish(conn);
114f3699e2cSTakashi Sakamoto 	if (err < 0)
115e34244ddSTakashi Sakamoto 		return err;
116f3699e2cSTakashi Sakamoto 
117ac5d7786STakashi Sakamoto 	err = amdtp_domain_add_stream(&oxfw->domain, stream,
118ac5d7786STakashi Sakamoto 				      conn->resources.channel, conn->speed);
119f3699e2cSTakashi Sakamoto 	if (err < 0) {
120f3699e2cSTakashi Sakamoto 		cmp_connection_break(conn);
121e34244ddSTakashi Sakamoto 		return err;
122f3699e2cSTakashi Sakamoto 	}
123f3699e2cSTakashi Sakamoto 
124e34244ddSTakashi Sakamoto 	return 0;
125f3699e2cSTakashi Sakamoto }
126f3699e2cSTakashi Sakamoto 
127b0ac0009STakashi Sakamoto static int check_connection_used_by_others(struct snd_oxfw *oxfw,
128b0ac0009STakashi Sakamoto 					   struct amdtp_stream *stream)
129f3699e2cSTakashi Sakamoto {
130b0ac0009STakashi Sakamoto 	struct cmp_connection *conn;
131b0ac0009STakashi Sakamoto 	bool used;
132b0ac0009STakashi Sakamoto 	int err;
133b0ac0009STakashi Sakamoto 
134b0ac0009STakashi Sakamoto 	if (stream == &oxfw->tx_stream)
135b0ac0009STakashi Sakamoto 		conn = &oxfw->out_conn;
136b0ac0009STakashi Sakamoto 	else
137b0ac0009STakashi Sakamoto 		conn = &oxfw->in_conn;
138b0ac0009STakashi Sakamoto 
139b0ac0009STakashi Sakamoto 	err = cmp_connection_check_used(conn, &used);
140b0ac0009STakashi Sakamoto 	if ((err >= 0) && used && !amdtp_stream_running(stream)) {
141b0ac0009STakashi Sakamoto 		dev_err(&oxfw->unit->device,
142b0ac0009STakashi Sakamoto 			"Connection established by others: %cPCR[%d]\n",
143b0ac0009STakashi Sakamoto 			(conn->direction == CMP_OUTPUT) ? 'o' : 'i',
144b0ac0009STakashi Sakamoto 			conn->pcr_index);
145b0ac0009STakashi Sakamoto 		err = -EBUSY;
146b0ac0009STakashi Sakamoto 	}
147b0ac0009STakashi Sakamoto 
148b0ac0009STakashi Sakamoto 	return err;
149b0ac0009STakashi Sakamoto }
150b0ac0009STakashi Sakamoto 
151779f0dbaSTakashi Sakamoto static int init_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
152b0ac0009STakashi Sakamoto {
153b0ac0009STakashi Sakamoto 	struct cmp_connection *conn;
154b0ac0009STakashi Sakamoto 	enum cmp_direction c_dir;
155b0ac0009STakashi Sakamoto 	enum amdtp_stream_direction s_dir;
156*029ffc42STakashi Sakamoto 	unsigned int flags = CIP_UNAWARE_SYT;
157b0ac0009STakashi Sakamoto 	int err;
158b0ac0009STakashi Sakamoto 
15907a35edcSTakashi Sakamoto 	if (!(oxfw->quirks & SND_OXFW_QUIRK_BLOCKING_TRANSMISSION))
160*029ffc42STakashi Sakamoto 		flags |= CIP_NONBLOCKING;
16107a35edcSTakashi Sakamoto 	else
162*029ffc42STakashi Sakamoto 		flags |= CIP_BLOCKING;
16307a35edcSTakashi Sakamoto 
164b0ac0009STakashi Sakamoto 	if (stream == &oxfw->tx_stream) {
165b0ac0009STakashi Sakamoto 		conn = &oxfw->out_conn;
166b0ac0009STakashi Sakamoto 		c_dir = CMP_OUTPUT;
167b0ac0009STakashi Sakamoto 		s_dir = AMDTP_IN_STREAM;
168a092f000STakashi Sakamoto 
169a092f000STakashi Sakamoto 		if (oxfw->quirks & SND_OXFW_QUIRK_JUMBO_PAYLOAD)
170a092f000STakashi Sakamoto 			flags |= CIP_JUMBO_PAYLOAD;
171a6f91693STakashi Sakamoto 		if (oxfw->quirks & SND_OXFW_QUIRK_WRONG_DBS)
172a092f000STakashi Sakamoto 			flags |= CIP_WRONG_DBS;
173b0ac0009STakashi Sakamoto 	} else {
174b0ac0009STakashi Sakamoto 		conn = &oxfw->in_conn;
175b0ac0009STakashi Sakamoto 		c_dir = CMP_INPUT;
176b0ac0009STakashi Sakamoto 		s_dir = AMDTP_OUT_STREAM;
177b0ac0009STakashi Sakamoto 	}
178b0ac0009STakashi Sakamoto 
179b0ac0009STakashi Sakamoto 	err = cmp_connection_init(conn, oxfw->unit, c_dir, 0);
180b0ac0009STakashi Sakamoto 	if (err < 0)
181779f0dbaSTakashi Sakamoto 		return err;
182b0ac0009STakashi Sakamoto 
183a092f000STakashi Sakamoto 	err = amdtp_am824_init(stream, oxfw->unit, s_dir, flags);
184b0ac0009STakashi Sakamoto 	if (err < 0) {
185b0ac0009STakashi Sakamoto 		cmp_connection_destroy(conn);
186779f0dbaSTakashi Sakamoto 		return err;
187b0ac0009STakashi Sakamoto 	}
188b0ac0009STakashi Sakamoto 
189779f0dbaSTakashi Sakamoto 	return 0;
190b0ac0009STakashi Sakamoto }
191b0ac0009STakashi Sakamoto 
1920356ce3aSTakashi Sakamoto static int keep_resources(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
1930356ce3aSTakashi Sakamoto {
1940356ce3aSTakashi Sakamoto 	enum avc_general_plug_dir dir;
1950356ce3aSTakashi Sakamoto 	u8 **formats;
1960356ce3aSTakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
1977bc93821STakashi Sakamoto 	struct cmp_connection *conn;
1980356ce3aSTakashi Sakamoto 	int i;
1990356ce3aSTakashi Sakamoto 	int err;
2000356ce3aSTakashi Sakamoto 
2010356ce3aSTakashi Sakamoto 	if (stream == &oxfw->rx_stream) {
2020356ce3aSTakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_IN;
2030356ce3aSTakashi Sakamoto 		formats = oxfw->rx_stream_formats;
2047bc93821STakashi Sakamoto 		conn = &oxfw->in_conn;
2050356ce3aSTakashi Sakamoto 	} else {
2060356ce3aSTakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_OUT;
2070356ce3aSTakashi Sakamoto 		formats = oxfw->tx_stream_formats;
2087bc93821STakashi Sakamoto 		conn = &oxfw->out_conn;
2090356ce3aSTakashi Sakamoto 	}
2100356ce3aSTakashi Sakamoto 
2110356ce3aSTakashi Sakamoto 	err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
2120356ce3aSTakashi Sakamoto 	if (err < 0)
2130356ce3aSTakashi Sakamoto 		return err;
2140356ce3aSTakashi Sakamoto 
2150356ce3aSTakashi Sakamoto 	for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
2160356ce3aSTakashi Sakamoto 		struct snd_oxfw_stream_formation fmt;
2170356ce3aSTakashi Sakamoto 
2180356ce3aSTakashi Sakamoto 		if (formats[i] == NULL)
2190356ce3aSTakashi Sakamoto 			break;
2200356ce3aSTakashi Sakamoto 
2210356ce3aSTakashi Sakamoto 		err = snd_oxfw_stream_parse_format(formats[i], &fmt);
2220356ce3aSTakashi Sakamoto 		if (err < 0)
2230356ce3aSTakashi Sakamoto 			return err;
2240356ce3aSTakashi Sakamoto 
2250356ce3aSTakashi Sakamoto 		if (fmt.rate == formation.rate && fmt.pcm == formation.pcm &&
2260356ce3aSTakashi Sakamoto 		    fmt.midi == formation.midi)
2270356ce3aSTakashi Sakamoto 			break;
2280356ce3aSTakashi Sakamoto 	}
2290356ce3aSTakashi Sakamoto 	if (i == SND_OXFW_STREAM_FORMAT_ENTRIES)
2300356ce3aSTakashi Sakamoto 		return -EINVAL;
2310356ce3aSTakashi Sakamoto 
2320356ce3aSTakashi Sakamoto 	// The stream should have one pcm channels at least.
2330356ce3aSTakashi Sakamoto 	if (formation.pcm == 0)
2340356ce3aSTakashi Sakamoto 		return -EINVAL;
2350356ce3aSTakashi Sakamoto 
2367bc93821STakashi Sakamoto 	err = amdtp_am824_set_parameters(stream, formation.rate, formation.pcm,
2370356ce3aSTakashi Sakamoto 					 formation.midi * 8, false);
2387bc93821STakashi Sakamoto 	if (err < 0)
2397bc93821STakashi Sakamoto 		return err;
2407bc93821STakashi Sakamoto 
2417bc93821STakashi Sakamoto 	return cmp_connection_reserve(conn, amdtp_stream_get_max_payload(stream));
2420356ce3aSTakashi Sakamoto }
2430356ce3aSTakashi Sakamoto 
2444f380d00STakashi Sakamoto int snd_oxfw_stream_reserve_duplex(struct snd_oxfw *oxfw,
245b0ac0009STakashi Sakamoto 				   struct amdtp_stream *stream,
2461d6a722cSTakashi Sakamoto 				   unsigned int rate, unsigned int pcm_channels,
2473299d2a0STakashi Sakamoto 				   unsigned int frames_per_period,
2483299d2a0STakashi Sakamoto 				   unsigned int frames_per_buffer)
249b0ac0009STakashi Sakamoto {
250f3699e2cSTakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
251b0ac0009STakashi Sakamoto 	enum avc_general_plug_dir dir;
2524f380d00STakashi Sakamoto 	int err;
253e2786ca6STakashi Sakamoto 
25420358d44STakashi Sakamoto 	// Considering JACK/FFADO streaming:
25520358d44STakashi Sakamoto 	// TODO: This can be removed hwdep functionality becomes popular.
25620358d44STakashi Sakamoto 	err = check_connection_used_by_others(oxfw, &oxfw->rx_stream);
25720358d44STakashi Sakamoto 	if (err < 0)
25820358d44STakashi Sakamoto 		return err;
25920358d44STakashi Sakamoto 	if (oxfw->has_output) {
26020358d44STakashi Sakamoto 		err = check_connection_used_by_others(oxfw, &oxfw->tx_stream);
26120358d44STakashi Sakamoto 		if (err < 0)
26220358d44STakashi Sakamoto 			return err;
263b0ac0009STakashi Sakamoto 	}
264b0ac0009STakashi Sakamoto 
26520358d44STakashi Sakamoto 	if (stream == &oxfw->tx_stream)
26620358d44STakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_OUT;
26720358d44STakashi Sakamoto 	else
26820358d44STakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_IN;
269b0ac0009STakashi Sakamoto 
270b0ac0009STakashi Sakamoto 	err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
271e2786ca6STakashi Sakamoto 	if (err < 0)
27220358d44STakashi Sakamoto 		return err;
2734f380d00STakashi Sakamoto 	if (rate == 0) {
27405588d34STakashi Sakamoto 		rate = formation.rate;
27505588d34STakashi Sakamoto 		pcm_channels = formation.pcm;
2764f380d00STakashi Sakamoto 	}
2774f380d00STakashi Sakamoto 	if (formation.rate != rate || formation.pcm != pcm_channels) {
278ac5d7786STakashi Sakamoto 		amdtp_domain_stop(&oxfw->domain);
279ac5d7786STakashi Sakamoto 
280e34244ddSTakashi Sakamoto 		cmp_connection_break(&oxfw->in_conn);
2813f2ce83dSTakashi Sakamoto 		cmp_connection_release(&oxfw->in_conn);
282e34244ddSTakashi Sakamoto 
283e34244ddSTakashi Sakamoto 		if (oxfw->has_output) {
284e34244ddSTakashi Sakamoto 			cmp_connection_break(&oxfw->out_conn);
2853f2ce83dSTakashi Sakamoto 			cmp_connection_release(&oxfw->out_conn);
286e34244ddSTakashi Sakamoto 		}
2874f380d00STakashi Sakamoto 	}
288f3699e2cSTakashi Sakamoto 
2894f380d00STakashi Sakamoto 	if (oxfw->substreams_count == 0 ||
2904f380d00STakashi Sakamoto 	    formation.rate != rate || formation.pcm != pcm_channels) {
291b0ac0009STakashi Sakamoto 		err = set_stream_format(oxfw, stream, rate, pcm_channels);
292f3699e2cSTakashi Sakamoto 		if (err < 0) {
293f3699e2cSTakashi Sakamoto 			dev_err(&oxfw->unit->device,
294f3699e2cSTakashi Sakamoto 				"fail to set stream format: %d\n", err);
29520358d44STakashi Sakamoto 			return err;
29620358d44STakashi Sakamoto 		}
2970356ce3aSTakashi Sakamoto 
2980356ce3aSTakashi Sakamoto 		err = keep_resources(oxfw, &oxfw->rx_stream);
2990356ce3aSTakashi Sakamoto 		if (err < 0)
3000356ce3aSTakashi Sakamoto 			return err;
3010356ce3aSTakashi Sakamoto 
3020356ce3aSTakashi Sakamoto 		if (oxfw->has_output) {
3030356ce3aSTakashi Sakamoto 			err = keep_resources(oxfw, &oxfw->tx_stream);
3047bc93821STakashi Sakamoto 			if (err < 0) {
3057bc93821STakashi Sakamoto 				cmp_connection_release(&oxfw->in_conn);
3060356ce3aSTakashi Sakamoto 				return err;
3070356ce3aSTakashi Sakamoto 			}
308f3699e2cSTakashi Sakamoto 		}
3091d6a722cSTakashi Sakamoto 
3101d6a722cSTakashi Sakamoto 		err = amdtp_domain_set_events_per_period(&oxfw->domain,
3113299d2a0STakashi Sakamoto 					frames_per_period, frames_per_buffer);
3121d6a722cSTakashi Sakamoto 		if (err < 0) {
3131d6a722cSTakashi Sakamoto 			cmp_connection_release(&oxfw->in_conn);
3141d6a722cSTakashi Sakamoto 			if (oxfw->has_output)
3151d6a722cSTakashi Sakamoto 				cmp_connection_release(&oxfw->out_conn);
3161d6a722cSTakashi Sakamoto 			return err;
3171d6a722cSTakashi Sakamoto 		}
3187bc93821STakashi Sakamoto 	}
319b0ac0009STakashi Sakamoto 
3204f380d00STakashi Sakamoto 	return 0;
3214f380d00STakashi Sakamoto }
3224f380d00STakashi Sakamoto 
3234f380d00STakashi Sakamoto int snd_oxfw_stream_start_duplex(struct snd_oxfw *oxfw)
3244f380d00STakashi Sakamoto {
3254f380d00STakashi Sakamoto 	int err;
3264f380d00STakashi Sakamoto 
3274f380d00STakashi Sakamoto 	if (oxfw->substreams_count == 0)
3284f380d00STakashi Sakamoto 		return -EIO;
3294f380d00STakashi Sakamoto 
3304f380d00STakashi Sakamoto 	if (amdtp_streaming_error(&oxfw->rx_stream) ||
3314f380d00STakashi Sakamoto 	    amdtp_streaming_error(&oxfw->tx_stream)) {
332ac5d7786STakashi Sakamoto 		amdtp_domain_stop(&oxfw->domain);
3334f380d00STakashi Sakamoto 
334ac5d7786STakashi Sakamoto 		cmp_connection_break(&oxfw->in_conn);
335ac5d7786STakashi Sakamoto 		if (oxfw->has_output)
3364f380d00STakashi Sakamoto 			cmp_connection_break(&oxfw->out_conn);
3374f380d00STakashi Sakamoto 	}
3384f380d00STakashi Sakamoto 
33920358d44STakashi Sakamoto 	if (!amdtp_stream_running(&oxfw->rx_stream)) {
340*029ffc42STakashi Sakamoto 		unsigned int tx_init_skip_cycles = 0;
341*029ffc42STakashi Sakamoto 		bool replay_seq = false;
342*029ffc42STakashi Sakamoto 
34320358d44STakashi Sakamoto 		err = start_stream(oxfw, &oxfw->rx_stream);
344b0ac0009STakashi Sakamoto 		if (err < 0) {
345b0ac0009STakashi Sakamoto 			dev_err(&oxfw->unit->device,
346ac5d7786STakashi Sakamoto 				"fail to prepare rx stream: %d\n", err);
347ac5d7786STakashi Sakamoto 			goto error;
348ac5d7786STakashi Sakamoto 		}
349ac5d7786STakashi Sakamoto 
350ac5d7786STakashi Sakamoto 		if (oxfw->has_output &&
351ac5d7786STakashi Sakamoto 		    !amdtp_stream_running(&oxfw->tx_stream)) {
352ac5d7786STakashi Sakamoto 			err = start_stream(oxfw, &oxfw->tx_stream);
353ac5d7786STakashi Sakamoto 			if (err < 0) {
354ac5d7786STakashi Sakamoto 				dev_err(&oxfw->unit->device,
355ac5d7786STakashi Sakamoto 					"fail to prepare tx stream: %d\n", err);
35620358d44STakashi Sakamoto 				goto error;
35720358d44STakashi Sakamoto 			}
358*029ffc42STakashi Sakamoto 
359*029ffc42STakashi Sakamoto 			if (oxfw->quirks & SND_OXFW_QUIRK_JUMBO_PAYLOAD) {
360*029ffc42STakashi Sakamoto 				// Just after changing sampling transfer frequency, many cycles are
361*029ffc42STakashi Sakamoto 				// skipped for packet transmission.
362*029ffc42STakashi Sakamoto 				tx_init_skip_cycles = 400;
363*029ffc42STakashi Sakamoto 			} else {
364*029ffc42STakashi Sakamoto 				replay_seq = true;
365*029ffc42STakashi Sakamoto 			}
36620358d44STakashi Sakamoto 		}
36720358d44STakashi Sakamoto 
368*029ffc42STakashi Sakamoto 		// NOTE: The device ignores presentation time expressed by the value of syt field
369*029ffc42STakashi Sakamoto 		// of CIP header in received packets. The sequence of the number of data blocks per
370*029ffc42STakashi Sakamoto 		// packet is important for media clock recovery.
371*029ffc42STakashi Sakamoto 		err = amdtp_domain_start(&oxfw->domain, tx_init_skip_cycles, replay_seq, false);
372ac5d7786STakashi Sakamoto 		if (err < 0)
373ac5d7786STakashi Sakamoto 			goto error;
374ac5d7786STakashi Sakamoto 
375bdaedca7STakashi Sakamoto 		if (!amdtp_domain_wait_ready(&oxfw->domain, READY_TIMEOUT_MS)) {
376ac5d7786STakashi Sakamoto 			err = -ETIMEDOUT;
37720358d44STakashi Sakamoto 			goto error;
378b0ac0009STakashi Sakamoto 		}
379f3699e2cSTakashi Sakamoto 	}
380f3699e2cSTakashi Sakamoto 
38120358d44STakashi Sakamoto 	return 0;
38220358d44STakashi Sakamoto error:
383ac5d7786STakashi Sakamoto 	amdtp_domain_stop(&oxfw->domain);
384ac5d7786STakashi Sakamoto 
38520358d44STakashi Sakamoto 	cmp_connection_break(&oxfw->in_conn);
386ac5d7786STakashi Sakamoto 	if (oxfw->has_output)
38720358d44STakashi Sakamoto 		cmp_connection_break(&oxfw->out_conn);
388ac5d7786STakashi Sakamoto 
389e2786ca6STakashi Sakamoto 	return err;
390e2786ca6STakashi Sakamoto }
391e2786ca6STakashi Sakamoto 
392779f0dbaSTakashi Sakamoto void snd_oxfw_stream_stop_duplex(struct snd_oxfw *oxfw)
393e2786ca6STakashi Sakamoto {
3944a0a0472STakashi Sakamoto 	if (oxfw->substreams_count == 0) {
395ac5d7786STakashi Sakamoto 		amdtp_domain_stop(&oxfw->domain);
396ac5d7786STakashi Sakamoto 
397e34244ddSTakashi Sakamoto 		cmp_connection_break(&oxfw->in_conn);
3987bc93821STakashi Sakamoto 		cmp_connection_release(&oxfw->in_conn);
399b0ac0009STakashi Sakamoto 
400e34244ddSTakashi Sakamoto 		if (oxfw->has_output) {
401e34244ddSTakashi Sakamoto 			cmp_connection_break(&oxfw->out_conn);
4027bc93821STakashi Sakamoto 			cmp_connection_release(&oxfw->out_conn);
403e34244ddSTakashi Sakamoto 		}
40420358d44STakashi Sakamoto 	}
405e2786ca6STakashi Sakamoto }
406e2786ca6STakashi Sakamoto 
407779f0dbaSTakashi Sakamoto static void destroy_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
408e2786ca6STakashi Sakamoto {
409b0ac0009STakashi Sakamoto 	struct cmp_connection *conn;
410e2786ca6STakashi Sakamoto 
411b0ac0009STakashi Sakamoto 	if (stream == &oxfw->tx_stream)
412b0ac0009STakashi Sakamoto 		conn = &oxfw->out_conn;
413e2786ca6STakashi Sakamoto 	else
414b0ac0009STakashi Sakamoto 		conn = &oxfw->in_conn;
415b0ac0009STakashi Sakamoto 
416b0ac0009STakashi Sakamoto 	amdtp_stream_destroy(stream);
417b0ac0009STakashi Sakamoto 	cmp_connection_destroy(conn);
418b0ac0009STakashi Sakamoto }
419b0ac0009STakashi Sakamoto 
420779f0dbaSTakashi Sakamoto int snd_oxfw_stream_init_duplex(struct snd_oxfw *oxfw)
421779f0dbaSTakashi Sakamoto {
422779f0dbaSTakashi Sakamoto 	int err;
423779f0dbaSTakashi Sakamoto 
424779f0dbaSTakashi Sakamoto 	err = init_stream(oxfw, &oxfw->rx_stream);
425779f0dbaSTakashi Sakamoto 	if (err < 0)
426779f0dbaSTakashi Sakamoto 		return err;
427779f0dbaSTakashi Sakamoto 
428779f0dbaSTakashi Sakamoto 	if (oxfw->has_output) {
429779f0dbaSTakashi Sakamoto 		err = init_stream(oxfw, &oxfw->tx_stream);
430779f0dbaSTakashi Sakamoto 		if (err < 0) {
431779f0dbaSTakashi Sakamoto 			destroy_stream(oxfw, &oxfw->rx_stream);
432779f0dbaSTakashi Sakamoto 			return err;
433779f0dbaSTakashi Sakamoto 		}
434779f0dbaSTakashi Sakamoto 	}
435779f0dbaSTakashi Sakamoto 
436ac5d7786STakashi Sakamoto 	err = amdtp_domain_init(&oxfw->domain);
437ac5d7786STakashi Sakamoto 	if (err < 0) {
438ac5d7786STakashi Sakamoto 		destroy_stream(oxfw, &oxfw->rx_stream);
439ac5d7786STakashi Sakamoto 		if (oxfw->has_output)
440ac5d7786STakashi Sakamoto 			destroy_stream(oxfw, &oxfw->tx_stream);
441ac5d7786STakashi Sakamoto 	}
442ac5d7786STakashi Sakamoto 
443ac5d7786STakashi Sakamoto 	return err;
444779f0dbaSTakashi Sakamoto }
445779f0dbaSTakashi Sakamoto 
446779f0dbaSTakashi Sakamoto // This function should be called before starting the stream or after stopping
447779f0dbaSTakashi Sakamoto // the streams.
448779f0dbaSTakashi Sakamoto void snd_oxfw_stream_destroy_duplex(struct snd_oxfw *oxfw)
449779f0dbaSTakashi Sakamoto {
450ac5d7786STakashi Sakamoto 	amdtp_domain_destroy(&oxfw->domain);
451ac5d7786STakashi Sakamoto 
452779f0dbaSTakashi Sakamoto 	destroy_stream(oxfw, &oxfw->rx_stream);
453779f0dbaSTakashi Sakamoto 
454779f0dbaSTakashi Sakamoto 	if (oxfw->has_output)
455779f0dbaSTakashi Sakamoto 		destroy_stream(oxfw, &oxfw->tx_stream);
456779f0dbaSTakashi Sakamoto }
457779f0dbaSTakashi Sakamoto 
458779f0dbaSTakashi Sakamoto void snd_oxfw_stream_update_duplex(struct snd_oxfw *oxfw)
459b0ac0009STakashi Sakamoto {
460ac5d7786STakashi Sakamoto 	amdtp_domain_stop(&oxfw->domain);
461ac5d7786STakashi Sakamoto 
462e34244ddSTakashi Sakamoto 	cmp_connection_break(&oxfw->in_conn);
463b0ac0009STakashi Sakamoto 
464e34244ddSTakashi Sakamoto 	amdtp_stream_pcm_abort(&oxfw->rx_stream);
465e34244ddSTakashi Sakamoto 
466e34244ddSTakashi Sakamoto 	if (oxfw->has_output) {
467e34244ddSTakashi Sakamoto 		cmp_connection_break(&oxfw->out_conn);
468e34244ddSTakashi Sakamoto 
469e34244ddSTakashi Sakamoto 		amdtp_stream_pcm_abort(&oxfw->tx_stream);
470e34244ddSTakashi Sakamoto 	}
471e2786ca6STakashi Sakamoto }
4725cd1d3f4STakashi Sakamoto 
4733c96101fSTakashi Sakamoto int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
4743c96101fSTakashi Sakamoto 				enum avc_general_plug_dir dir,
4753c96101fSTakashi Sakamoto 				struct snd_oxfw_stream_formation *formation)
4763c96101fSTakashi Sakamoto {
4773c96101fSTakashi Sakamoto 	u8 *format;
4783c96101fSTakashi Sakamoto 	unsigned int len;
4793c96101fSTakashi Sakamoto 	int err;
4803c96101fSTakashi Sakamoto 
4813c96101fSTakashi Sakamoto 	len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
4823c96101fSTakashi Sakamoto 	format = kmalloc(len, GFP_KERNEL);
4833c96101fSTakashi Sakamoto 	if (format == NULL)
4843c96101fSTakashi Sakamoto 		return -ENOMEM;
4853c96101fSTakashi Sakamoto 
4863c96101fSTakashi Sakamoto 	err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
4873c96101fSTakashi Sakamoto 	if (err < 0)
4883c96101fSTakashi Sakamoto 		goto end;
4893c96101fSTakashi Sakamoto 	if (len < 3) {
4903c96101fSTakashi Sakamoto 		err = -EIO;
4913c96101fSTakashi Sakamoto 		goto end;
4923c96101fSTakashi Sakamoto 	}
4933c96101fSTakashi Sakamoto 
4943c96101fSTakashi Sakamoto 	err = snd_oxfw_stream_parse_format(format, formation);
4953c96101fSTakashi Sakamoto end:
4963c96101fSTakashi Sakamoto 	kfree(format);
4973c96101fSTakashi Sakamoto 	return err;
4983c96101fSTakashi Sakamoto }
4993c96101fSTakashi Sakamoto 
5005cd1d3f4STakashi Sakamoto /*
5015cd1d3f4STakashi Sakamoto  * See Table 6.16 - AM824 Stream Format
5025cd1d3f4STakashi Sakamoto  *     Figure 6.19 - format_information field for AM824 Compound
5035cd1d3f4STakashi Sakamoto  * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
5045cd1d3f4STakashi Sakamoto  * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
5055cd1d3f4STakashi Sakamoto  */
5065cd1d3f4STakashi Sakamoto int snd_oxfw_stream_parse_format(u8 *format,
5075cd1d3f4STakashi Sakamoto 				 struct snd_oxfw_stream_formation *formation)
5085cd1d3f4STakashi Sakamoto {
5095cd1d3f4STakashi Sakamoto 	unsigned int i, e, channels, type;
5105cd1d3f4STakashi Sakamoto 
5115cd1d3f4STakashi Sakamoto 	memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
5125cd1d3f4STakashi Sakamoto 
5135cd1d3f4STakashi Sakamoto 	/*
5145cd1d3f4STakashi Sakamoto 	 * this module can support a hierarchy combination that:
5155cd1d3f4STakashi Sakamoto 	 *  Root:	Audio and Music (0x90)
5165cd1d3f4STakashi Sakamoto 	 *  Level 1:	AM824 Compound  (0x40)
5175cd1d3f4STakashi Sakamoto 	 */
5185cd1d3f4STakashi Sakamoto 	if ((format[0] != 0x90) || (format[1] != 0x40))
51903be63b2STakashi Sakamoto 		return -ENXIO;
5205cd1d3f4STakashi Sakamoto 
5215cd1d3f4STakashi Sakamoto 	/* check the sampling rate */
5225cd1d3f4STakashi Sakamoto 	for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
5235cd1d3f4STakashi Sakamoto 		if (format[2] == avc_stream_rate_table[i])
5245cd1d3f4STakashi Sakamoto 			break;
5255cd1d3f4STakashi Sakamoto 	}
5265cd1d3f4STakashi Sakamoto 	if (i == ARRAY_SIZE(avc_stream_rate_table))
52703be63b2STakashi Sakamoto 		return -ENXIO;
5285cd1d3f4STakashi Sakamoto 
5295cd1d3f4STakashi Sakamoto 	formation->rate = oxfw_rate_table[i];
5305cd1d3f4STakashi Sakamoto 
5315cd1d3f4STakashi Sakamoto 	for (e = 0; e < format[4]; e++) {
5325cd1d3f4STakashi Sakamoto 		channels = format[5 + e * 2];
5335cd1d3f4STakashi Sakamoto 		type = format[6 + e * 2];
5345cd1d3f4STakashi Sakamoto 
5355cd1d3f4STakashi Sakamoto 		switch (type) {
5365cd1d3f4STakashi Sakamoto 		/* IEC 60958 Conformant, currently handled as MBLA */
5375cd1d3f4STakashi Sakamoto 		case 0x00:
5385cd1d3f4STakashi Sakamoto 		/* Multi Bit Linear Audio (Raw) */
5395cd1d3f4STakashi Sakamoto 		case 0x06:
5405cd1d3f4STakashi Sakamoto 			formation->pcm += channels;
5415cd1d3f4STakashi Sakamoto 			break;
5425cd1d3f4STakashi Sakamoto 		/* MIDI Conformant */
5435cd1d3f4STakashi Sakamoto 		case 0x0d:
5445cd1d3f4STakashi Sakamoto 			formation->midi = channels;
5455cd1d3f4STakashi Sakamoto 			break;
5465cd1d3f4STakashi Sakamoto 		/* IEC 61937-3 to 7 */
5475cd1d3f4STakashi Sakamoto 		case 0x01:
5485cd1d3f4STakashi Sakamoto 		case 0x02:
5495cd1d3f4STakashi Sakamoto 		case 0x03:
5505cd1d3f4STakashi Sakamoto 		case 0x04:
5515cd1d3f4STakashi Sakamoto 		case 0x05:
5525cd1d3f4STakashi Sakamoto 		/* Multi Bit Linear Audio */
5535cd1d3f4STakashi Sakamoto 		case 0x07:	/* DVD-Audio */
5545cd1d3f4STakashi Sakamoto 		case 0x0c:	/* High Precision */
5555cd1d3f4STakashi Sakamoto 		/* One Bit Audio */
5565cd1d3f4STakashi Sakamoto 		case 0x08:	/* (Plain) Raw */
5575cd1d3f4STakashi Sakamoto 		case 0x09:	/* (Plain) SACD */
5585cd1d3f4STakashi Sakamoto 		case 0x0a:	/* (Encoded) Raw */
5595cd1d3f4STakashi Sakamoto 		case 0x0b:	/* (Encoded) SACD */
5605cd1d3f4STakashi Sakamoto 		/* SMPTE Time-Code conformant */
5615cd1d3f4STakashi Sakamoto 		case 0x0e:
5625cd1d3f4STakashi Sakamoto 		/* Sample Count */
5635cd1d3f4STakashi Sakamoto 		case 0x0f:
5645cd1d3f4STakashi Sakamoto 		/* Anciliary Data */
5655cd1d3f4STakashi Sakamoto 		case 0x10:
5665cd1d3f4STakashi Sakamoto 		/* Synchronization Stream (Stereo Raw audio) */
5675cd1d3f4STakashi Sakamoto 		case 0x40:
5685cd1d3f4STakashi Sakamoto 		/* Don't care */
5695cd1d3f4STakashi Sakamoto 		case 0xff:
5705cd1d3f4STakashi Sakamoto 		default:
57103be63b2STakashi Sakamoto 			return -ENXIO;	/* not supported */
5725cd1d3f4STakashi Sakamoto 		}
5735cd1d3f4STakashi Sakamoto 	}
5745cd1d3f4STakashi Sakamoto 
57549c7b3fcSTakashi Sakamoto 	if (formation->pcm  > AM824_MAX_CHANNELS_FOR_PCM ||
57649c7b3fcSTakashi Sakamoto 	    formation->midi > AM824_MAX_CHANNELS_FOR_MIDI)
57703be63b2STakashi Sakamoto 		return -ENXIO;
5785cd1d3f4STakashi Sakamoto 
5795cd1d3f4STakashi Sakamoto 	return 0;
5805cd1d3f4STakashi Sakamoto }
5815cd1d3f4STakashi Sakamoto 
5825cd1d3f4STakashi Sakamoto static int
5835cd1d3f4STakashi Sakamoto assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
5845cd1d3f4STakashi Sakamoto 		      unsigned int pid, u8 *buf, unsigned int *len,
5855cd1d3f4STakashi Sakamoto 		      u8 **formats)
5865cd1d3f4STakashi Sakamoto {
5875cd1d3f4STakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
5885cd1d3f4STakashi Sakamoto 	unsigned int i, eid;
5895cd1d3f4STakashi Sakamoto 	int err;
5905cd1d3f4STakashi Sakamoto 
5915cd1d3f4STakashi Sakamoto 	/* get format at current sampling rate */
5925cd1d3f4STakashi Sakamoto 	err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
5935cd1d3f4STakashi Sakamoto 	if (err < 0) {
5945cd1d3f4STakashi Sakamoto 		dev_err(&oxfw->unit->device,
5955cd1d3f4STakashi Sakamoto 		"fail to get current stream format for isoc %s plug %d:%d\n",
5965cd1d3f4STakashi Sakamoto 			(dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
5975cd1d3f4STakashi Sakamoto 			pid, err);
5985cd1d3f4STakashi Sakamoto 		goto end;
5995cd1d3f4STakashi Sakamoto 	}
6005cd1d3f4STakashi Sakamoto 
6015cd1d3f4STakashi Sakamoto 	/* parse and set stream format */
6025cd1d3f4STakashi Sakamoto 	eid = 0;
6035cd1d3f4STakashi Sakamoto 	err = snd_oxfw_stream_parse_format(buf, &formation);
6045cd1d3f4STakashi Sakamoto 	if (err < 0)
6055cd1d3f4STakashi Sakamoto 		goto end;
6065cd1d3f4STakashi Sakamoto 
607cd3b7116STakashi Sakamoto 	formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
608cd3b7116STakashi Sakamoto 				    GFP_KERNEL);
609cd3b7116STakashi Sakamoto 	if (!formats[eid]) {
6105cd1d3f4STakashi Sakamoto 		err = -ENOMEM;
6115cd1d3f4STakashi Sakamoto 		goto end;
6125cd1d3f4STakashi Sakamoto 	}
6135cd1d3f4STakashi Sakamoto 
6145cd1d3f4STakashi Sakamoto 	/* apply the format for each available sampling rate */
6155cd1d3f4STakashi Sakamoto 	for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
6165cd1d3f4STakashi Sakamoto 		if (formation.rate == oxfw_rate_table[i])
6175cd1d3f4STakashi Sakamoto 			continue;
6185cd1d3f4STakashi Sakamoto 
6195cd1d3f4STakashi Sakamoto 		err = avc_general_inquiry_sig_fmt(oxfw->unit,
6205cd1d3f4STakashi Sakamoto 						  oxfw_rate_table[i],
6215cd1d3f4STakashi Sakamoto 						  dir, pid);
6225cd1d3f4STakashi Sakamoto 		if (err < 0)
6235cd1d3f4STakashi Sakamoto 			continue;
6245cd1d3f4STakashi Sakamoto 
6255cd1d3f4STakashi Sakamoto 		eid++;
626cd3b7116STakashi Sakamoto 		formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
627cd3b7116STakashi Sakamoto 					    GFP_KERNEL);
6285cd1d3f4STakashi Sakamoto 		if (formats[eid] == NULL) {
6295cd1d3f4STakashi Sakamoto 			err = -ENOMEM;
6305cd1d3f4STakashi Sakamoto 			goto end;
6315cd1d3f4STakashi Sakamoto 		}
6325cd1d3f4STakashi Sakamoto 		formats[eid][2] = avc_stream_rate_table[i];
6335cd1d3f4STakashi Sakamoto 	}
6345cd1d3f4STakashi Sakamoto 
6355cd1d3f4STakashi Sakamoto 	err = 0;
6365cd1d3f4STakashi Sakamoto 	oxfw->assumed = true;
6375cd1d3f4STakashi Sakamoto end:
6385cd1d3f4STakashi Sakamoto 	return err;
6395cd1d3f4STakashi Sakamoto }
6405cd1d3f4STakashi Sakamoto 
6415cd1d3f4STakashi Sakamoto static int fill_stream_formats(struct snd_oxfw *oxfw,
6425cd1d3f4STakashi Sakamoto 			       enum avc_general_plug_dir dir,
6435cd1d3f4STakashi Sakamoto 			       unsigned short pid)
6445cd1d3f4STakashi Sakamoto {
6455cd1d3f4STakashi Sakamoto 	u8 *buf, **formats;
6465cd1d3f4STakashi Sakamoto 	unsigned int len, eid = 0;
6475cd1d3f4STakashi Sakamoto 	struct snd_oxfw_stream_formation dummy;
6485cd1d3f4STakashi Sakamoto 	int err;
6495cd1d3f4STakashi Sakamoto 
6505cd1d3f4STakashi Sakamoto 	buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
6515cd1d3f4STakashi Sakamoto 	if (buf == NULL)
6525cd1d3f4STakashi Sakamoto 		return -ENOMEM;
6535cd1d3f4STakashi Sakamoto 
654b0ac0009STakashi Sakamoto 	if (dir == AVC_GENERAL_PLUG_DIR_OUT)
655b0ac0009STakashi Sakamoto 		formats = oxfw->tx_stream_formats;
656b0ac0009STakashi Sakamoto 	else
6575cd1d3f4STakashi Sakamoto 		formats = oxfw->rx_stream_formats;
6585cd1d3f4STakashi Sakamoto 
6595cd1d3f4STakashi Sakamoto 	/* get first entry */
6605cd1d3f4STakashi Sakamoto 	len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
6615cd1d3f4STakashi Sakamoto 	err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
66203be63b2STakashi Sakamoto 	if (err == -ENXIO) {
6635cd1d3f4STakashi Sakamoto 		/* LIST subfunction is not implemented */
6645cd1d3f4STakashi Sakamoto 		len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
6655cd1d3f4STakashi Sakamoto 		err = assume_stream_formats(oxfw, dir, pid, buf, &len,
6665cd1d3f4STakashi Sakamoto 					    formats);
6675cd1d3f4STakashi Sakamoto 		goto end;
6685cd1d3f4STakashi Sakamoto 	} else if (err < 0) {
6695cd1d3f4STakashi Sakamoto 		dev_err(&oxfw->unit->device,
6705cd1d3f4STakashi Sakamoto 			"fail to get stream format %d for isoc %s plug %d:%d\n",
6715cd1d3f4STakashi Sakamoto 			eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
6725cd1d3f4STakashi Sakamoto 			pid, err);
6735cd1d3f4STakashi Sakamoto 		goto end;
6745cd1d3f4STakashi Sakamoto 	}
6755cd1d3f4STakashi Sakamoto 
6765cd1d3f4STakashi Sakamoto 	/* LIST subfunction is implemented */
6775cd1d3f4STakashi Sakamoto 	while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
6785cd1d3f4STakashi Sakamoto 		/* The format is too short. */
6795cd1d3f4STakashi Sakamoto 		if (len < 3) {
6805cd1d3f4STakashi Sakamoto 			err = -EIO;
6815cd1d3f4STakashi Sakamoto 			break;
6825cd1d3f4STakashi Sakamoto 		}
6835cd1d3f4STakashi Sakamoto 
6845cd1d3f4STakashi Sakamoto 		/* parse and set stream format */
6855cd1d3f4STakashi Sakamoto 		err = snd_oxfw_stream_parse_format(buf, &dummy);
6865cd1d3f4STakashi Sakamoto 		if (err < 0)
6875cd1d3f4STakashi Sakamoto 			break;
6885cd1d3f4STakashi Sakamoto 
689cd3b7116STakashi Sakamoto 		formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, len,
690cd3b7116STakashi Sakamoto 					    GFP_KERNEL);
691cd3b7116STakashi Sakamoto 		if (!formats[eid]) {
6925cd1d3f4STakashi Sakamoto 			err = -ENOMEM;
6935cd1d3f4STakashi Sakamoto 			break;
6945cd1d3f4STakashi Sakamoto 		}
6955cd1d3f4STakashi Sakamoto 
6965cd1d3f4STakashi Sakamoto 		/* get next entry */
6975cd1d3f4STakashi Sakamoto 		len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
6985cd1d3f4STakashi Sakamoto 		err = avc_stream_get_format_list(oxfw->unit, dir, 0,
6995cd1d3f4STakashi Sakamoto 						 buf, &len, ++eid);
7005cd1d3f4STakashi Sakamoto 		/* No entries remained. */
7015cd1d3f4STakashi Sakamoto 		if (err == -EINVAL) {
7025cd1d3f4STakashi Sakamoto 			err = 0;
7035cd1d3f4STakashi Sakamoto 			break;
7045cd1d3f4STakashi Sakamoto 		} else if (err < 0) {
7055cd1d3f4STakashi Sakamoto 			dev_err(&oxfw->unit->device,
7065cd1d3f4STakashi Sakamoto 			"fail to get stream format %d for isoc %s plug %d:%d\n",
7075cd1d3f4STakashi Sakamoto 				eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
7085cd1d3f4STakashi Sakamoto 									"out",
7095cd1d3f4STakashi Sakamoto 				pid, err);
7105cd1d3f4STakashi Sakamoto 			break;
7115cd1d3f4STakashi Sakamoto 		}
7125cd1d3f4STakashi Sakamoto 	}
7135cd1d3f4STakashi Sakamoto end:
7145cd1d3f4STakashi Sakamoto 	kfree(buf);
7155cd1d3f4STakashi Sakamoto 	return err;
7165cd1d3f4STakashi Sakamoto }
7175cd1d3f4STakashi Sakamoto 
7185cd1d3f4STakashi Sakamoto int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
7195cd1d3f4STakashi Sakamoto {
7205cd1d3f4STakashi Sakamoto 	u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
72132056041STakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
72232056041STakashi Sakamoto 	u8 *format;
72332056041STakashi Sakamoto 	unsigned int i;
7245cd1d3f4STakashi Sakamoto 	int err;
7255cd1d3f4STakashi Sakamoto 
7265cd1d3f4STakashi Sakamoto 	/* the number of plugs for isoc in/out, ext in/out  */
7275cd1d3f4STakashi Sakamoto 	err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
7285cd1d3f4STakashi Sakamoto 	if (err < 0) {
7295cd1d3f4STakashi Sakamoto 		dev_err(&oxfw->unit->device,
7305cd1d3f4STakashi Sakamoto 		"fail to get info for isoc/external in/out plugs: %d\n",
7315cd1d3f4STakashi Sakamoto 			err);
7325cd1d3f4STakashi Sakamoto 		goto end;
733b0ac0009STakashi Sakamoto 	} else if ((plugs[0] == 0) && (plugs[1] == 0)) {
73403be63b2STakashi Sakamoto 		err = -ENXIO;
7355cd1d3f4STakashi Sakamoto 		goto end;
7365cd1d3f4STakashi Sakamoto 	}
7375cd1d3f4STakashi Sakamoto 
738b0ac0009STakashi Sakamoto 	/* use oPCR[0] if exists */
739b0ac0009STakashi Sakamoto 	if (plugs[1] > 0) {
740b0ac0009STakashi Sakamoto 		err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
74141dbc792STakashi Sakamoto 		if (err < 0) {
74241dbc792STakashi Sakamoto 			if (err != -ENXIO)
74341dbc792STakashi Sakamoto 				return err;
74432056041STakashi Sakamoto 
74541dbc792STakashi Sakamoto 			// The oPCR is not available for isoc communication.
74641dbc792STakashi Sakamoto 			err = 0;
74741dbc792STakashi Sakamoto 		} else {
74832056041STakashi Sakamoto 			for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
74932056041STakashi Sakamoto 				format = oxfw->tx_stream_formats[i];
75032056041STakashi Sakamoto 				if (format == NULL)
75132056041STakashi Sakamoto 					continue;
75241dbc792STakashi Sakamoto 				err = snd_oxfw_stream_parse_format(format,
75341dbc792STakashi Sakamoto 								   &formation);
75432056041STakashi Sakamoto 				if (err < 0)
75532056041STakashi Sakamoto 					continue;
75632056041STakashi Sakamoto 
75732056041STakashi Sakamoto 				/* Add one MIDI port. */
75832056041STakashi Sakamoto 				if (formation.midi > 0)
75932056041STakashi Sakamoto 					oxfw->midi_input_ports = 1;
76032056041STakashi Sakamoto 			}
76132056041STakashi Sakamoto 
762b0ac0009STakashi Sakamoto 			oxfw->has_output = true;
763b0ac0009STakashi Sakamoto 		}
76441dbc792STakashi Sakamoto 	}
765b0ac0009STakashi Sakamoto 
7665cd1d3f4STakashi Sakamoto 	/* use iPCR[0] if exists */
76732056041STakashi Sakamoto 	if (plugs[0] > 0) {
7685cd1d3f4STakashi Sakamoto 		err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
76941dbc792STakashi Sakamoto 		if (err < 0) {
77041dbc792STakashi Sakamoto 			if (err != -ENXIO)
77141dbc792STakashi Sakamoto 				return err;
77232056041STakashi Sakamoto 
77341dbc792STakashi Sakamoto 			// The iPCR is not available for isoc communication.
77441dbc792STakashi Sakamoto 			err = 0;
77541dbc792STakashi Sakamoto 		} else {
77632056041STakashi Sakamoto 			for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
77732056041STakashi Sakamoto 				format = oxfw->rx_stream_formats[i];
77832056041STakashi Sakamoto 				if (format == NULL)
77932056041STakashi Sakamoto 					continue;
78041dbc792STakashi Sakamoto 				err = snd_oxfw_stream_parse_format(format,
78141dbc792STakashi Sakamoto 								   &formation);
78232056041STakashi Sakamoto 				if (err < 0)
78332056041STakashi Sakamoto 					continue;
78432056041STakashi Sakamoto 
78532056041STakashi Sakamoto 				/* Add one MIDI port. */
78632056041STakashi Sakamoto 				if (formation.midi > 0)
78732056041STakashi Sakamoto 					oxfw->midi_output_ports = 1;
78832056041STakashi Sakamoto 			}
78906a42a74STakashi Sakamoto 
79006a42a74STakashi Sakamoto 			oxfw->has_input = true;
79132056041STakashi Sakamoto 		}
79241dbc792STakashi Sakamoto 	}
7935cd1d3f4STakashi Sakamoto end:
7945cd1d3f4STakashi Sakamoto 	return err;
7955cd1d3f4STakashi Sakamoto }
7968985f4acSTakashi Sakamoto 
7978985f4acSTakashi Sakamoto void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
7988985f4acSTakashi Sakamoto {
7998985f4acSTakashi Sakamoto 	oxfw->dev_lock_changed = true;
8008985f4acSTakashi Sakamoto 	wake_up(&oxfw->hwdep_wait);
8018985f4acSTakashi Sakamoto }
8028985f4acSTakashi Sakamoto 
8038985f4acSTakashi Sakamoto int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
8048985f4acSTakashi Sakamoto {
8058985f4acSTakashi Sakamoto 	int err;
8068985f4acSTakashi Sakamoto 
8078985f4acSTakashi Sakamoto 	spin_lock_irq(&oxfw->lock);
8088985f4acSTakashi Sakamoto 
8098985f4acSTakashi Sakamoto 	/* user land lock this */
8108985f4acSTakashi Sakamoto 	if (oxfw->dev_lock_count < 0) {
8118985f4acSTakashi Sakamoto 		err = -EBUSY;
8128985f4acSTakashi Sakamoto 		goto end;
8138985f4acSTakashi Sakamoto 	}
8148985f4acSTakashi Sakamoto 
8158985f4acSTakashi Sakamoto 	/* this is the first time */
8168985f4acSTakashi Sakamoto 	if (oxfw->dev_lock_count++ == 0)
8178985f4acSTakashi Sakamoto 		snd_oxfw_stream_lock_changed(oxfw);
8188985f4acSTakashi Sakamoto 	err = 0;
8198985f4acSTakashi Sakamoto end:
8208985f4acSTakashi Sakamoto 	spin_unlock_irq(&oxfw->lock);
8218985f4acSTakashi Sakamoto 	return err;
8228985f4acSTakashi Sakamoto }
8238985f4acSTakashi Sakamoto 
8248985f4acSTakashi Sakamoto void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
8258985f4acSTakashi Sakamoto {
8268985f4acSTakashi Sakamoto 	spin_lock_irq(&oxfw->lock);
8278985f4acSTakashi Sakamoto 
8288985f4acSTakashi Sakamoto 	if (WARN_ON(oxfw->dev_lock_count <= 0))
8298985f4acSTakashi Sakamoto 		goto end;
8308985f4acSTakashi Sakamoto 	if (--oxfw->dev_lock_count == 0)
8318985f4acSTakashi Sakamoto 		snd_oxfw_stream_lock_changed(oxfw);
8328985f4acSTakashi Sakamoto end:
8338985f4acSTakashi Sakamoto 	spin_unlock_irq(&oxfw->lock);
8348985f4acSTakashi Sakamoto }
835