xref: /linux/sound/firewire/oxfw/oxfw-stream.c (revision 03be63b2953c1b33ac77b7c21345495acc47e84b)
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
12f3699e2cSTakashi Sakamoto #define CALLBACK_TIMEOUT	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;
156b0ac0009STakashi Sakamoto 	int err;
157b0ac0009STakashi Sakamoto 
158b0ac0009STakashi Sakamoto 	if (stream == &oxfw->tx_stream) {
159b0ac0009STakashi Sakamoto 		conn = &oxfw->out_conn;
160b0ac0009STakashi Sakamoto 		c_dir = CMP_OUTPUT;
161b0ac0009STakashi Sakamoto 		s_dir = AMDTP_IN_STREAM;
162b0ac0009STakashi Sakamoto 	} else {
163b0ac0009STakashi Sakamoto 		conn = &oxfw->in_conn;
164b0ac0009STakashi Sakamoto 		c_dir = CMP_INPUT;
165b0ac0009STakashi Sakamoto 		s_dir = AMDTP_OUT_STREAM;
166b0ac0009STakashi Sakamoto 	}
167b0ac0009STakashi Sakamoto 
168b0ac0009STakashi Sakamoto 	err = cmp_connection_init(conn, oxfw->unit, c_dir, 0);
169b0ac0009STakashi Sakamoto 	if (err < 0)
170779f0dbaSTakashi Sakamoto 		return err;
171b0ac0009STakashi Sakamoto 
1725955815eSTakashi Sakamoto 	err = amdtp_am824_init(stream, oxfw->unit, s_dir, CIP_NONBLOCKING);
173b0ac0009STakashi Sakamoto 	if (err < 0) {
174b0ac0009STakashi Sakamoto 		cmp_connection_destroy(conn);
175779f0dbaSTakashi Sakamoto 		return err;
176b0ac0009STakashi Sakamoto 	}
177b0ac0009STakashi Sakamoto 
178a2064710STakashi Sakamoto 	/*
179a2064710STakashi Sakamoto 	 * OXFW starts to transmit packets with non-zero dbc.
180a2064710STakashi Sakamoto 	 * OXFW postpone transferring packets till handling any asynchronous
181a2064710STakashi Sakamoto 	 * packets. As a result, next isochronous packet includes more data
182a2064710STakashi Sakamoto 	 * blocks than IEC 61883-6 defines.
183a2064710STakashi Sakamoto 	 */
18413f3a46dSTakashi Sakamoto 	if (stream == &oxfw->tx_stream) {
18562f00e40STakashi Sakamoto 		oxfw->tx_stream.flags |= CIP_JUMBO_PAYLOAD;
18613f3a46dSTakashi Sakamoto 		if (oxfw->wrong_dbs)
18713f3a46dSTakashi Sakamoto 			oxfw->tx_stream.flags |= CIP_WRONG_DBS;
18813f3a46dSTakashi Sakamoto 	}
189779f0dbaSTakashi Sakamoto 
190779f0dbaSTakashi Sakamoto 	return 0;
191b0ac0009STakashi Sakamoto }
192b0ac0009STakashi Sakamoto 
1930356ce3aSTakashi Sakamoto static int keep_resources(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
1940356ce3aSTakashi Sakamoto {
1950356ce3aSTakashi Sakamoto 	enum avc_general_plug_dir dir;
1960356ce3aSTakashi Sakamoto 	u8 **formats;
1970356ce3aSTakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
1987bc93821STakashi Sakamoto 	struct cmp_connection *conn;
1990356ce3aSTakashi Sakamoto 	int i;
2000356ce3aSTakashi Sakamoto 	int err;
2010356ce3aSTakashi Sakamoto 
2020356ce3aSTakashi Sakamoto 	if (stream == &oxfw->rx_stream) {
2030356ce3aSTakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_IN;
2040356ce3aSTakashi Sakamoto 		formats = oxfw->rx_stream_formats;
2057bc93821STakashi Sakamoto 		conn = &oxfw->in_conn;
2060356ce3aSTakashi Sakamoto 	} else {
2070356ce3aSTakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_OUT;
2080356ce3aSTakashi Sakamoto 		formats = oxfw->tx_stream_formats;
2097bc93821STakashi Sakamoto 		conn = &oxfw->out_conn;
2100356ce3aSTakashi Sakamoto 	}
2110356ce3aSTakashi Sakamoto 
2120356ce3aSTakashi Sakamoto 	err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
2130356ce3aSTakashi Sakamoto 	if (err < 0)
2140356ce3aSTakashi Sakamoto 		return err;
2150356ce3aSTakashi Sakamoto 
2160356ce3aSTakashi Sakamoto 	for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
2170356ce3aSTakashi Sakamoto 		struct snd_oxfw_stream_formation fmt;
2180356ce3aSTakashi Sakamoto 
2190356ce3aSTakashi Sakamoto 		if (formats[i] == NULL)
2200356ce3aSTakashi Sakamoto 			break;
2210356ce3aSTakashi Sakamoto 
2220356ce3aSTakashi Sakamoto 		err = snd_oxfw_stream_parse_format(formats[i], &fmt);
2230356ce3aSTakashi Sakamoto 		if (err < 0)
2240356ce3aSTakashi Sakamoto 			return err;
2250356ce3aSTakashi Sakamoto 
2260356ce3aSTakashi Sakamoto 		if (fmt.rate == formation.rate && fmt.pcm == formation.pcm &&
2270356ce3aSTakashi Sakamoto 		    fmt.midi == formation.midi)
2280356ce3aSTakashi Sakamoto 			break;
2290356ce3aSTakashi Sakamoto 	}
2300356ce3aSTakashi Sakamoto 	if (i == SND_OXFW_STREAM_FORMAT_ENTRIES)
2310356ce3aSTakashi Sakamoto 		return -EINVAL;
2320356ce3aSTakashi Sakamoto 
2330356ce3aSTakashi Sakamoto 	// The stream should have one pcm channels at least.
2340356ce3aSTakashi Sakamoto 	if (formation.pcm == 0)
2350356ce3aSTakashi Sakamoto 		return -EINVAL;
2360356ce3aSTakashi Sakamoto 
2377bc93821STakashi Sakamoto 	err = amdtp_am824_set_parameters(stream, formation.rate, formation.pcm,
2380356ce3aSTakashi Sakamoto 					 formation.midi * 8, false);
2397bc93821STakashi Sakamoto 	if (err < 0)
2407bc93821STakashi Sakamoto 		return err;
2417bc93821STakashi Sakamoto 
2427bc93821STakashi Sakamoto 	return cmp_connection_reserve(conn, amdtp_stream_get_max_payload(stream));
2430356ce3aSTakashi Sakamoto }
2440356ce3aSTakashi Sakamoto 
2454f380d00STakashi Sakamoto int snd_oxfw_stream_reserve_duplex(struct snd_oxfw *oxfw,
246b0ac0009STakashi Sakamoto 				   struct amdtp_stream *stream,
2471d6a722cSTakashi Sakamoto 				   unsigned int rate, unsigned int pcm_channels,
2483299d2a0STakashi Sakamoto 				   unsigned int frames_per_period,
2493299d2a0STakashi Sakamoto 				   unsigned int frames_per_buffer)
250b0ac0009STakashi Sakamoto {
251f3699e2cSTakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
252b0ac0009STakashi Sakamoto 	enum avc_general_plug_dir dir;
2534f380d00STakashi Sakamoto 	int err;
254e2786ca6STakashi Sakamoto 
25520358d44STakashi Sakamoto 	// Considering JACK/FFADO streaming:
25620358d44STakashi Sakamoto 	// TODO: This can be removed hwdep functionality becomes popular.
25720358d44STakashi Sakamoto 	err = check_connection_used_by_others(oxfw, &oxfw->rx_stream);
25820358d44STakashi Sakamoto 	if (err < 0)
25920358d44STakashi Sakamoto 		return err;
26020358d44STakashi Sakamoto 	if (oxfw->has_output) {
26120358d44STakashi Sakamoto 		err = check_connection_used_by_others(oxfw, &oxfw->tx_stream);
26220358d44STakashi Sakamoto 		if (err < 0)
26320358d44STakashi Sakamoto 			return err;
264b0ac0009STakashi Sakamoto 	}
265b0ac0009STakashi Sakamoto 
26620358d44STakashi Sakamoto 	if (stream == &oxfw->tx_stream)
26720358d44STakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_OUT;
26820358d44STakashi Sakamoto 	else
26920358d44STakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_IN;
270b0ac0009STakashi Sakamoto 
271b0ac0009STakashi Sakamoto 	err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
272e2786ca6STakashi Sakamoto 	if (err < 0)
27320358d44STakashi Sakamoto 		return err;
2744f380d00STakashi Sakamoto 	if (rate == 0) {
27505588d34STakashi Sakamoto 		rate = formation.rate;
27605588d34STakashi Sakamoto 		pcm_channels = formation.pcm;
2774f380d00STakashi Sakamoto 	}
2784f380d00STakashi Sakamoto 	if (formation.rate != rate || formation.pcm != pcm_channels) {
279ac5d7786STakashi Sakamoto 		amdtp_domain_stop(&oxfw->domain);
280ac5d7786STakashi Sakamoto 
281e34244ddSTakashi Sakamoto 		cmp_connection_break(&oxfw->in_conn);
2823f2ce83dSTakashi Sakamoto 		cmp_connection_release(&oxfw->in_conn);
283e34244ddSTakashi Sakamoto 
284e34244ddSTakashi Sakamoto 		if (oxfw->has_output) {
285e34244ddSTakashi Sakamoto 			cmp_connection_break(&oxfw->out_conn);
2863f2ce83dSTakashi Sakamoto 			cmp_connection_release(&oxfw->out_conn);
287e34244ddSTakashi Sakamoto 		}
2884f380d00STakashi Sakamoto 	}
289f3699e2cSTakashi Sakamoto 
2904f380d00STakashi Sakamoto 	if (oxfw->substreams_count == 0 ||
2914f380d00STakashi Sakamoto 	    formation.rate != rate || formation.pcm != pcm_channels) {
292b0ac0009STakashi Sakamoto 		err = set_stream_format(oxfw, stream, rate, pcm_channels);
293f3699e2cSTakashi Sakamoto 		if (err < 0) {
294f3699e2cSTakashi Sakamoto 			dev_err(&oxfw->unit->device,
295f3699e2cSTakashi Sakamoto 				"fail to set stream format: %d\n", err);
29620358d44STakashi Sakamoto 			return err;
29720358d44STakashi Sakamoto 		}
2980356ce3aSTakashi Sakamoto 
2990356ce3aSTakashi Sakamoto 		err = keep_resources(oxfw, &oxfw->rx_stream);
3000356ce3aSTakashi Sakamoto 		if (err < 0)
3010356ce3aSTakashi Sakamoto 			return err;
3020356ce3aSTakashi Sakamoto 
3030356ce3aSTakashi Sakamoto 		if (oxfw->has_output) {
3040356ce3aSTakashi Sakamoto 			err = keep_resources(oxfw, &oxfw->tx_stream);
3057bc93821STakashi Sakamoto 			if (err < 0) {
3067bc93821STakashi Sakamoto 				cmp_connection_release(&oxfw->in_conn);
3070356ce3aSTakashi Sakamoto 				return err;
3080356ce3aSTakashi Sakamoto 			}
309f3699e2cSTakashi Sakamoto 		}
3101d6a722cSTakashi Sakamoto 
3111d6a722cSTakashi Sakamoto 		err = amdtp_domain_set_events_per_period(&oxfw->domain,
3123299d2a0STakashi Sakamoto 					frames_per_period, frames_per_buffer);
3131d6a722cSTakashi Sakamoto 		if (err < 0) {
3141d6a722cSTakashi Sakamoto 			cmp_connection_release(&oxfw->in_conn);
3151d6a722cSTakashi Sakamoto 			if (oxfw->has_output)
3161d6a722cSTakashi Sakamoto 				cmp_connection_release(&oxfw->out_conn);
3171d6a722cSTakashi Sakamoto 			return err;
3181d6a722cSTakashi Sakamoto 		}
3197bc93821STakashi Sakamoto 	}
320b0ac0009STakashi Sakamoto 
3214f380d00STakashi Sakamoto 	return 0;
3224f380d00STakashi Sakamoto }
3234f380d00STakashi Sakamoto 
3244f380d00STakashi Sakamoto int snd_oxfw_stream_start_duplex(struct snd_oxfw *oxfw)
3254f380d00STakashi Sakamoto {
3264f380d00STakashi Sakamoto 	int err;
3274f380d00STakashi Sakamoto 
3284f380d00STakashi Sakamoto 	if (oxfw->substreams_count == 0)
3294f380d00STakashi Sakamoto 		return -EIO;
3304f380d00STakashi Sakamoto 
3314f380d00STakashi Sakamoto 	if (amdtp_streaming_error(&oxfw->rx_stream) ||
3324f380d00STakashi Sakamoto 	    amdtp_streaming_error(&oxfw->tx_stream)) {
333ac5d7786STakashi Sakamoto 		amdtp_domain_stop(&oxfw->domain);
3344f380d00STakashi Sakamoto 
335ac5d7786STakashi Sakamoto 		cmp_connection_break(&oxfw->in_conn);
336ac5d7786STakashi Sakamoto 		if (oxfw->has_output)
3374f380d00STakashi Sakamoto 			cmp_connection_break(&oxfw->out_conn);
3384f380d00STakashi Sakamoto 	}
3394f380d00STakashi Sakamoto 
34020358d44STakashi Sakamoto 	if (!amdtp_stream_running(&oxfw->rx_stream)) {
34120358d44STakashi Sakamoto 		err = start_stream(oxfw, &oxfw->rx_stream);
342b0ac0009STakashi Sakamoto 		if (err < 0) {
343b0ac0009STakashi Sakamoto 			dev_err(&oxfw->unit->device,
344ac5d7786STakashi Sakamoto 				"fail to prepare rx stream: %d\n", err);
345ac5d7786STakashi Sakamoto 			goto error;
346ac5d7786STakashi Sakamoto 		}
347ac5d7786STakashi Sakamoto 
348ac5d7786STakashi Sakamoto 		if (oxfw->has_output &&
349ac5d7786STakashi Sakamoto 		    !amdtp_stream_running(&oxfw->tx_stream)) {
350ac5d7786STakashi Sakamoto 			err = start_stream(oxfw, &oxfw->tx_stream);
351ac5d7786STakashi Sakamoto 			if (err < 0) {
352ac5d7786STakashi Sakamoto 				dev_err(&oxfw->unit->device,
353ac5d7786STakashi Sakamoto 					"fail to prepare tx stream: %d\n", err);
35420358d44STakashi Sakamoto 				goto error;
35520358d44STakashi Sakamoto 			}
35620358d44STakashi Sakamoto 		}
35720358d44STakashi Sakamoto 
358acfedcbeSTakashi Sakamoto 		err = amdtp_domain_start(&oxfw->domain, 0);
359ac5d7786STakashi Sakamoto 		if (err < 0)
360ac5d7786STakashi Sakamoto 			goto error;
361ac5d7786STakashi Sakamoto 
362ac5d7786STakashi Sakamoto 		// Wait first packet.
363ac5d7786STakashi Sakamoto 		if (!amdtp_stream_wait_callback(&oxfw->rx_stream,
364ac5d7786STakashi Sakamoto 						CALLBACK_TIMEOUT)) {
365ac5d7786STakashi Sakamoto 			err = -ETIMEDOUT;
36620358d44STakashi Sakamoto 			goto error;
367b0ac0009STakashi Sakamoto 		}
368ac5d7786STakashi Sakamoto 
3694c098dabSTakashi Sakamoto 		if (oxfw->has_output) {
3704c098dabSTakashi Sakamoto 			if (!amdtp_stream_wait_callback(&oxfw->tx_stream,
3714c098dabSTakashi Sakamoto 							CALLBACK_TIMEOUT)) {
3724c098dabSTakashi Sakamoto 				err = -ETIMEDOUT;
3734c098dabSTakashi Sakamoto 				goto error;
3744c098dabSTakashi Sakamoto 			}
3754c098dabSTakashi Sakamoto 		}
376f3699e2cSTakashi Sakamoto 	}
377f3699e2cSTakashi Sakamoto 
37820358d44STakashi Sakamoto 	return 0;
37920358d44STakashi Sakamoto error:
380ac5d7786STakashi Sakamoto 	amdtp_domain_stop(&oxfw->domain);
381ac5d7786STakashi Sakamoto 
38220358d44STakashi Sakamoto 	cmp_connection_break(&oxfw->in_conn);
383ac5d7786STakashi Sakamoto 	if (oxfw->has_output)
38420358d44STakashi Sakamoto 		cmp_connection_break(&oxfw->out_conn);
385ac5d7786STakashi Sakamoto 
386e2786ca6STakashi Sakamoto 	return err;
387e2786ca6STakashi Sakamoto }
388e2786ca6STakashi Sakamoto 
389779f0dbaSTakashi Sakamoto void snd_oxfw_stream_stop_duplex(struct snd_oxfw *oxfw)
390e2786ca6STakashi Sakamoto {
3914a0a0472STakashi Sakamoto 	if (oxfw->substreams_count == 0) {
392ac5d7786STakashi Sakamoto 		amdtp_domain_stop(&oxfw->domain);
393ac5d7786STakashi Sakamoto 
394e34244ddSTakashi Sakamoto 		cmp_connection_break(&oxfw->in_conn);
3957bc93821STakashi Sakamoto 		cmp_connection_release(&oxfw->in_conn);
396b0ac0009STakashi Sakamoto 
397e34244ddSTakashi Sakamoto 		if (oxfw->has_output) {
398e34244ddSTakashi Sakamoto 			cmp_connection_break(&oxfw->out_conn);
3997bc93821STakashi Sakamoto 			cmp_connection_release(&oxfw->out_conn);
400e34244ddSTakashi Sakamoto 		}
40120358d44STakashi Sakamoto 	}
402e2786ca6STakashi Sakamoto }
403e2786ca6STakashi Sakamoto 
404779f0dbaSTakashi Sakamoto static void destroy_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
405e2786ca6STakashi Sakamoto {
406b0ac0009STakashi Sakamoto 	struct cmp_connection *conn;
407e2786ca6STakashi Sakamoto 
408b0ac0009STakashi Sakamoto 	if (stream == &oxfw->tx_stream)
409b0ac0009STakashi Sakamoto 		conn = &oxfw->out_conn;
410e2786ca6STakashi Sakamoto 	else
411b0ac0009STakashi Sakamoto 		conn = &oxfw->in_conn;
412b0ac0009STakashi Sakamoto 
413b0ac0009STakashi Sakamoto 	amdtp_stream_destroy(stream);
414b0ac0009STakashi Sakamoto 	cmp_connection_destroy(conn);
415b0ac0009STakashi Sakamoto }
416b0ac0009STakashi Sakamoto 
417779f0dbaSTakashi Sakamoto int snd_oxfw_stream_init_duplex(struct snd_oxfw *oxfw)
418779f0dbaSTakashi Sakamoto {
419779f0dbaSTakashi Sakamoto 	int err;
420779f0dbaSTakashi Sakamoto 
421779f0dbaSTakashi Sakamoto 	err = init_stream(oxfw, &oxfw->rx_stream);
422779f0dbaSTakashi Sakamoto 	if (err < 0)
423779f0dbaSTakashi Sakamoto 		return err;
424779f0dbaSTakashi Sakamoto 
425779f0dbaSTakashi Sakamoto 	if (oxfw->has_output) {
426779f0dbaSTakashi Sakamoto 		err = init_stream(oxfw, &oxfw->tx_stream);
427779f0dbaSTakashi Sakamoto 		if (err < 0) {
428779f0dbaSTakashi Sakamoto 			destroy_stream(oxfw, &oxfw->rx_stream);
429779f0dbaSTakashi Sakamoto 			return err;
430779f0dbaSTakashi Sakamoto 		}
431779f0dbaSTakashi Sakamoto 	}
432779f0dbaSTakashi Sakamoto 
433ac5d7786STakashi Sakamoto 	err = amdtp_domain_init(&oxfw->domain);
434ac5d7786STakashi Sakamoto 	if (err < 0) {
435ac5d7786STakashi Sakamoto 		destroy_stream(oxfw, &oxfw->rx_stream);
436ac5d7786STakashi Sakamoto 		if (oxfw->has_output)
437ac5d7786STakashi Sakamoto 			destroy_stream(oxfw, &oxfw->tx_stream);
438ac5d7786STakashi Sakamoto 	}
439ac5d7786STakashi Sakamoto 
440ac5d7786STakashi Sakamoto 	return err;
441779f0dbaSTakashi Sakamoto }
442779f0dbaSTakashi Sakamoto 
443779f0dbaSTakashi Sakamoto // This function should be called before starting the stream or after stopping
444779f0dbaSTakashi Sakamoto // the streams.
445779f0dbaSTakashi Sakamoto void snd_oxfw_stream_destroy_duplex(struct snd_oxfw *oxfw)
446779f0dbaSTakashi Sakamoto {
447ac5d7786STakashi Sakamoto 	amdtp_domain_destroy(&oxfw->domain);
448ac5d7786STakashi Sakamoto 
449779f0dbaSTakashi Sakamoto 	destroy_stream(oxfw, &oxfw->rx_stream);
450779f0dbaSTakashi Sakamoto 
451779f0dbaSTakashi Sakamoto 	if (oxfw->has_output)
452779f0dbaSTakashi Sakamoto 		destroy_stream(oxfw, &oxfw->tx_stream);
453779f0dbaSTakashi Sakamoto }
454779f0dbaSTakashi Sakamoto 
455779f0dbaSTakashi Sakamoto void snd_oxfw_stream_update_duplex(struct snd_oxfw *oxfw)
456b0ac0009STakashi Sakamoto {
457ac5d7786STakashi Sakamoto 	amdtp_domain_stop(&oxfw->domain);
458ac5d7786STakashi Sakamoto 
459e34244ddSTakashi Sakamoto 	cmp_connection_break(&oxfw->in_conn);
460b0ac0009STakashi Sakamoto 
461e34244ddSTakashi Sakamoto 	amdtp_stream_pcm_abort(&oxfw->rx_stream);
462e34244ddSTakashi Sakamoto 
463e34244ddSTakashi Sakamoto 	if (oxfw->has_output) {
464e34244ddSTakashi Sakamoto 		cmp_connection_break(&oxfw->out_conn);
465e34244ddSTakashi Sakamoto 
466e34244ddSTakashi Sakamoto 		amdtp_stream_pcm_abort(&oxfw->tx_stream);
467e34244ddSTakashi Sakamoto 	}
468e2786ca6STakashi Sakamoto }
4695cd1d3f4STakashi Sakamoto 
4703c96101fSTakashi Sakamoto int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
4713c96101fSTakashi Sakamoto 				enum avc_general_plug_dir dir,
4723c96101fSTakashi Sakamoto 				struct snd_oxfw_stream_formation *formation)
4733c96101fSTakashi Sakamoto {
4743c96101fSTakashi Sakamoto 	u8 *format;
4753c96101fSTakashi Sakamoto 	unsigned int len;
4763c96101fSTakashi Sakamoto 	int err;
4773c96101fSTakashi Sakamoto 
4783c96101fSTakashi Sakamoto 	len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
4793c96101fSTakashi Sakamoto 	format = kmalloc(len, GFP_KERNEL);
4803c96101fSTakashi Sakamoto 	if (format == NULL)
4813c96101fSTakashi Sakamoto 		return -ENOMEM;
4823c96101fSTakashi Sakamoto 
4833c96101fSTakashi Sakamoto 	err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
4843c96101fSTakashi Sakamoto 	if (err < 0)
4853c96101fSTakashi Sakamoto 		goto end;
4863c96101fSTakashi Sakamoto 	if (len < 3) {
4873c96101fSTakashi Sakamoto 		err = -EIO;
4883c96101fSTakashi Sakamoto 		goto end;
4893c96101fSTakashi Sakamoto 	}
4903c96101fSTakashi Sakamoto 
4913c96101fSTakashi Sakamoto 	err = snd_oxfw_stream_parse_format(format, formation);
4923c96101fSTakashi Sakamoto end:
4933c96101fSTakashi Sakamoto 	kfree(format);
4943c96101fSTakashi Sakamoto 	return err;
4953c96101fSTakashi Sakamoto }
4963c96101fSTakashi Sakamoto 
4975cd1d3f4STakashi Sakamoto /*
4985cd1d3f4STakashi Sakamoto  * See Table 6.16 - AM824 Stream Format
4995cd1d3f4STakashi Sakamoto  *     Figure 6.19 - format_information field for AM824 Compound
5005cd1d3f4STakashi Sakamoto  * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
5015cd1d3f4STakashi Sakamoto  * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
5025cd1d3f4STakashi Sakamoto  */
5035cd1d3f4STakashi Sakamoto int snd_oxfw_stream_parse_format(u8 *format,
5045cd1d3f4STakashi Sakamoto 				 struct snd_oxfw_stream_formation *formation)
5055cd1d3f4STakashi Sakamoto {
5065cd1d3f4STakashi Sakamoto 	unsigned int i, e, channels, type;
5075cd1d3f4STakashi Sakamoto 
5085cd1d3f4STakashi Sakamoto 	memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
5095cd1d3f4STakashi Sakamoto 
5105cd1d3f4STakashi Sakamoto 	/*
5115cd1d3f4STakashi Sakamoto 	 * this module can support a hierarchy combination that:
5125cd1d3f4STakashi Sakamoto 	 *  Root:	Audio and Music (0x90)
5135cd1d3f4STakashi Sakamoto 	 *  Level 1:	AM824 Compound  (0x40)
5145cd1d3f4STakashi Sakamoto 	 */
5155cd1d3f4STakashi Sakamoto 	if ((format[0] != 0x90) || (format[1] != 0x40))
516*03be63b2STakashi Sakamoto 		return -ENXIO;
5175cd1d3f4STakashi Sakamoto 
5185cd1d3f4STakashi Sakamoto 	/* check the sampling rate */
5195cd1d3f4STakashi Sakamoto 	for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
5205cd1d3f4STakashi Sakamoto 		if (format[2] == avc_stream_rate_table[i])
5215cd1d3f4STakashi Sakamoto 			break;
5225cd1d3f4STakashi Sakamoto 	}
5235cd1d3f4STakashi Sakamoto 	if (i == ARRAY_SIZE(avc_stream_rate_table))
524*03be63b2STakashi Sakamoto 		return -ENXIO;
5255cd1d3f4STakashi Sakamoto 
5265cd1d3f4STakashi Sakamoto 	formation->rate = oxfw_rate_table[i];
5275cd1d3f4STakashi Sakamoto 
5285cd1d3f4STakashi Sakamoto 	for (e = 0; e < format[4]; e++) {
5295cd1d3f4STakashi Sakamoto 		channels = format[5 + e * 2];
5305cd1d3f4STakashi Sakamoto 		type = format[6 + e * 2];
5315cd1d3f4STakashi Sakamoto 
5325cd1d3f4STakashi Sakamoto 		switch (type) {
5335cd1d3f4STakashi Sakamoto 		/* IEC 60958 Conformant, currently handled as MBLA */
5345cd1d3f4STakashi Sakamoto 		case 0x00:
5355cd1d3f4STakashi Sakamoto 		/* Multi Bit Linear Audio (Raw) */
5365cd1d3f4STakashi Sakamoto 		case 0x06:
5375cd1d3f4STakashi Sakamoto 			formation->pcm += channels;
5385cd1d3f4STakashi Sakamoto 			break;
5395cd1d3f4STakashi Sakamoto 		/* MIDI Conformant */
5405cd1d3f4STakashi Sakamoto 		case 0x0d:
5415cd1d3f4STakashi Sakamoto 			formation->midi = channels;
5425cd1d3f4STakashi Sakamoto 			break;
5435cd1d3f4STakashi Sakamoto 		/* IEC 61937-3 to 7 */
5445cd1d3f4STakashi Sakamoto 		case 0x01:
5455cd1d3f4STakashi Sakamoto 		case 0x02:
5465cd1d3f4STakashi Sakamoto 		case 0x03:
5475cd1d3f4STakashi Sakamoto 		case 0x04:
5485cd1d3f4STakashi Sakamoto 		case 0x05:
5495cd1d3f4STakashi Sakamoto 		/* Multi Bit Linear Audio */
5505cd1d3f4STakashi Sakamoto 		case 0x07:	/* DVD-Audio */
5515cd1d3f4STakashi Sakamoto 		case 0x0c:	/* High Precision */
5525cd1d3f4STakashi Sakamoto 		/* One Bit Audio */
5535cd1d3f4STakashi Sakamoto 		case 0x08:	/* (Plain) Raw */
5545cd1d3f4STakashi Sakamoto 		case 0x09:	/* (Plain) SACD */
5555cd1d3f4STakashi Sakamoto 		case 0x0a:	/* (Encoded) Raw */
5565cd1d3f4STakashi Sakamoto 		case 0x0b:	/* (Encoded) SACD */
5575cd1d3f4STakashi Sakamoto 		/* SMPTE Time-Code conformant */
5585cd1d3f4STakashi Sakamoto 		case 0x0e:
5595cd1d3f4STakashi Sakamoto 		/* Sample Count */
5605cd1d3f4STakashi Sakamoto 		case 0x0f:
5615cd1d3f4STakashi Sakamoto 		/* Anciliary Data */
5625cd1d3f4STakashi Sakamoto 		case 0x10:
5635cd1d3f4STakashi Sakamoto 		/* Synchronization Stream (Stereo Raw audio) */
5645cd1d3f4STakashi Sakamoto 		case 0x40:
5655cd1d3f4STakashi Sakamoto 		/* Don't care */
5665cd1d3f4STakashi Sakamoto 		case 0xff:
5675cd1d3f4STakashi Sakamoto 		default:
568*03be63b2STakashi Sakamoto 			return -ENXIO;	/* not supported */
5695cd1d3f4STakashi Sakamoto 		}
5705cd1d3f4STakashi Sakamoto 	}
5715cd1d3f4STakashi Sakamoto 
57249c7b3fcSTakashi Sakamoto 	if (formation->pcm  > AM824_MAX_CHANNELS_FOR_PCM ||
57349c7b3fcSTakashi Sakamoto 	    formation->midi > AM824_MAX_CHANNELS_FOR_MIDI)
574*03be63b2STakashi Sakamoto 		return -ENXIO;
5755cd1d3f4STakashi Sakamoto 
5765cd1d3f4STakashi Sakamoto 	return 0;
5775cd1d3f4STakashi Sakamoto }
5785cd1d3f4STakashi Sakamoto 
5795cd1d3f4STakashi Sakamoto static int
5805cd1d3f4STakashi Sakamoto assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
5815cd1d3f4STakashi Sakamoto 		      unsigned int pid, u8 *buf, unsigned int *len,
5825cd1d3f4STakashi Sakamoto 		      u8 **formats)
5835cd1d3f4STakashi Sakamoto {
5845cd1d3f4STakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
5855cd1d3f4STakashi Sakamoto 	unsigned int i, eid;
5865cd1d3f4STakashi Sakamoto 	int err;
5875cd1d3f4STakashi Sakamoto 
5885cd1d3f4STakashi Sakamoto 	/* get format at current sampling rate */
5895cd1d3f4STakashi Sakamoto 	err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
5905cd1d3f4STakashi Sakamoto 	if (err < 0) {
5915cd1d3f4STakashi Sakamoto 		dev_err(&oxfw->unit->device,
5925cd1d3f4STakashi Sakamoto 		"fail to get current stream format for isoc %s plug %d:%d\n",
5935cd1d3f4STakashi Sakamoto 			(dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
5945cd1d3f4STakashi Sakamoto 			pid, err);
5955cd1d3f4STakashi Sakamoto 		goto end;
5965cd1d3f4STakashi Sakamoto 	}
5975cd1d3f4STakashi Sakamoto 
5985cd1d3f4STakashi Sakamoto 	/* parse and set stream format */
5995cd1d3f4STakashi Sakamoto 	eid = 0;
6005cd1d3f4STakashi Sakamoto 	err = snd_oxfw_stream_parse_format(buf, &formation);
6015cd1d3f4STakashi Sakamoto 	if (err < 0)
6025cd1d3f4STakashi Sakamoto 		goto end;
6035cd1d3f4STakashi Sakamoto 
604cd3b7116STakashi Sakamoto 	formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
605cd3b7116STakashi Sakamoto 				    GFP_KERNEL);
606cd3b7116STakashi Sakamoto 	if (!formats[eid]) {
6075cd1d3f4STakashi Sakamoto 		err = -ENOMEM;
6085cd1d3f4STakashi Sakamoto 		goto end;
6095cd1d3f4STakashi Sakamoto 	}
6105cd1d3f4STakashi Sakamoto 
6115cd1d3f4STakashi Sakamoto 	/* apply the format for each available sampling rate */
6125cd1d3f4STakashi Sakamoto 	for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
6135cd1d3f4STakashi Sakamoto 		if (formation.rate == oxfw_rate_table[i])
6145cd1d3f4STakashi Sakamoto 			continue;
6155cd1d3f4STakashi Sakamoto 
6165cd1d3f4STakashi Sakamoto 		err = avc_general_inquiry_sig_fmt(oxfw->unit,
6175cd1d3f4STakashi Sakamoto 						  oxfw_rate_table[i],
6185cd1d3f4STakashi Sakamoto 						  dir, pid);
6195cd1d3f4STakashi Sakamoto 		if (err < 0)
6205cd1d3f4STakashi Sakamoto 			continue;
6215cd1d3f4STakashi Sakamoto 
6225cd1d3f4STakashi Sakamoto 		eid++;
623cd3b7116STakashi Sakamoto 		formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
624cd3b7116STakashi Sakamoto 					    GFP_KERNEL);
6255cd1d3f4STakashi Sakamoto 		if (formats[eid] == NULL) {
6265cd1d3f4STakashi Sakamoto 			err = -ENOMEM;
6275cd1d3f4STakashi Sakamoto 			goto end;
6285cd1d3f4STakashi Sakamoto 		}
6295cd1d3f4STakashi Sakamoto 		formats[eid][2] = avc_stream_rate_table[i];
6305cd1d3f4STakashi Sakamoto 	}
6315cd1d3f4STakashi Sakamoto 
6325cd1d3f4STakashi Sakamoto 	err = 0;
6335cd1d3f4STakashi Sakamoto 	oxfw->assumed = true;
6345cd1d3f4STakashi Sakamoto end:
6355cd1d3f4STakashi Sakamoto 	return err;
6365cd1d3f4STakashi Sakamoto }
6375cd1d3f4STakashi Sakamoto 
6385cd1d3f4STakashi Sakamoto static int fill_stream_formats(struct snd_oxfw *oxfw,
6395cd1d3f4STakashi Sakamoto 			       enum avc_general_plug_dir dir,
6405cd1d3f4STakashi Sakamoto 			       unsigned short pid)
6415cd1d3f4STakashi Sakamoto {
6425cd1d3f4STakashi Sakamoto 	u8 *buf, **formats;
6435cd1d3f4STakashi Sakamoto 	unsigned int len, eid = 0;
6445cd1d3f4STakashi Sakamoto 	struct snd_oxfw_stream_formation dummy;
6455cd1d3f4STakashi Sakamoto 	int err;
6465cd1d3f4STakashi Sakamoto 
6475cd1d3f4STakashi Sakamoto 	buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
6485cd1d3f4STakashi Sakamoto 	if (buf == NULL)
6495cd1d3f4STakashi Sakamoto 		return -ENOMEM;
6505cd1d3f4STakashi Sakamoto 
651b0ac0009STakashi Sakamoto 	if (dir == AVC_GENERAL_PLUG_DIR_OUT)
652b0ac0009STakashi Sakamoto 		formats = oxfw->tx_stream_formats;
653b0ac0009STakashi Sakamoto 	else
6545cd1d3f4STakashi Sakamoto 		formats = oxfw->rx_stream_formats;
6555cd1d3f4STakashi Sakamoto 
6565cd1d3f4STakashi Sakamoto 	/* get first entry */
6575cd1d3f4STakashi Sakamoto 	len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
6585cd1d3f4STakashi Sakamoto 	err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
659*03be63b2STakashi Sakamoto 	if (err == -ENXIO) {
6605cd1d3f4STakashi Sakamoto 		/* LIST subfunction is not implemented */
6615cd1d3f4STakashi Sakamoto 		len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
6625cd1d3f4STakashi Sakamoto 		err = assume_stream_formats(oxfw, dir, pid, buf, &len,
6635cd1d3f4STakashi Sakamoto 					    formats);
6645cd1d3f4STakashi Sakamoto 		goto end;
6655cd1d3f4STakashi Sakamoto 	} else if (err < 0) {
6665cd1d3f4STakashi Sakamoto 		dev_err(&oxfw->unit->device,
6675cd1d3f4STakashi Sakamoto 			"fail to get stream format %d for isoc %s plug %d:%d\n",
6685cd1d3f4STakashi Sakamoto 			eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
6695cd1d3f4STakashi Sakamoto 			pid, err);
6705cd1d3f4STakashi Sakamoto 		goto end;
6715cd1d3f4STakashi Sakamoto 	}
6725cd1d3f4STakashi Sakamoto 
6735cd1d3f4STakashi Sakamoto 	/* LIST subfunction is implemented */
6745cd1d3f4STakashi Sakamoto 	while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
6755cd1d3f4STakashi Sakamoto 		/* The format is too short. */
6765cd1d3f4STakashi Sakamoto 		if (len < 3) {
6775cd1d3f4STakashi Sakamoto 			err = -EIO;
6785cd1d3f4STakashi Sakamoto 			break;
6795cd1d3f4STakashi Sakamoto 		}
6805cd1d3f4STakashi Sakamoto 
6815cd1d3f4STakashi Sakamoto 		/* parse and set stream format */
6825cd1d3f4STakashi Sakamoto 		err = snd_oxfw_stream_parse_format(buf, &dummy);
6835cd1d3f4STakashi Sakamoto 		if (err < 0)
6845cd1d3f4STakashi Sakamoto 			break;
6855cd1d3f4STakashi Sakamoto 
686cd3b7116STakashi Sakamoto 		formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, len,
687cd3b7116STakashi Sakamoto 					    GFP_KERNEL);
688cd3b7116STakashi Sakamoto 		if (!formats[eid]) {
6895cd1d3f4STakashi Sakamoto 			err = -ENOMEM;
6905cd1d3f4STakashi Sakamoto 			break;
6915cd1d3f4STakashi Sakamoto 		}
6925cd1d3f4STakashi Sakamoto 
6935cd1d3f4STakashi Sakamoto 		/* get next entry */
6945cd1d3f4STakashi Sakamoto 		len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
6955cd1d3f4STakashi Sakamoto 		err = avc_stream_get_format_list(oxfw->unit, dir, 0,
6965cd1d3f4STakashi Sakamoto 						 buf, &len, ++eid);
6975cd1d3f4STakashi Sakamoto 		/* No entries remained. */
6985cd1d3f4STakashi Sakamoto 		if (err == -EINVAL) {
6995cd1d3f4STakashi Sakamoto 			err = 0;
7005cd1d3f4STakashi Sakamoto 			break;
7015cd1d3f4STakashi Sakamoto 		} else if (err < 0) {
7025cd1d3f4STakashi Sakamoto 			dev_err(&oxfw->unit->device,
7035cd1d3f4STakashi Sakamoto 			"fail to get stream format %d for isoc %s plug %d:%d\n",
7045cd1d3f4STakashi Sakamoto 				eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
7055cd1d3f4STakashi Sakamoto 									"out",
7065cd1d3f4STakashi Sakamoto 				pid, err);
7075cd1d3f4STakashi Sakamoto 			break;
7085cd1d3f4STakashi Sakamoto 		}
7095cd1d3f4STakashi Sakamoto 	}
7105cd1d3f4STakashi Sakamoto end:
7115cd1d3f4STakashi Sakamoto 	kfree(buf);
7125cd1d3f4STakashi Sakamoto 	return err;
7135cd1d3f4STakashi Sakamoto }
7145cd1d3f4STakashi Sakamoto 
7155cd1d3f4STakashi Sakamoto int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
7165cd1d3f4STakashi Sakamoto {
7175cd1d3f4STakashi Sakamoto 	u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
71832056041STakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
71932056041STakashi Sakamoto 	u8 *format;
72032056041STakashi Sakamoto 	unsigned int i;
7215cd1d3f4STakashi Sakamoto 	int err;
7225cd1d3f4STakashi Sakamoto 
7235cd1d3f4STakashi Sakamoto 	/* the number of plugs for isoc in/out, ext in/out  */
7245cd1d3f4STakashi Sakamoto 	err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
7255cd1d3f4STakashi Sakamoto 	if (err < 0) {
7265cd1d3f4STakashi Sakamoto 		dev_err(&oxfw->unit->device,
7275cd1d3f4STakashi Sakamoto 		"fail to get info for isoc/external in/out plugs: %d\n",
7285cd1d3f4STakashi Sakamoto 			err);
7295cd1d3f4STakashi Sakamoto 		goto end;
730b0ac0009STakashi Sakamoto 	} else if ((plugs[0] == 0) && (plugs[1] == 0)) {
731*03be63b2STakashi Sakamoto 		err = -ENXIO;
7325cd1d3f4STakashi Sakamoto 		goto end;
7335cd1d3f4STakashi Sakamoto 	}
7345cd1d3f4STakashi Sakamoto 
735b0ac0009STakashi Sakamoto 	/* use oPCR[0] if exists */
736b0ac0009STakashi Sakamoto 	if (plugs[1] > 0) {
737b0ac0009STakashi Sakamoto 		err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
738b0ac0009STakashi Sakamoto 		if (err < 0)
739b0ac0009STakashi Sakamoto 			goto end;
74032056041STakashi Sakamoto 
74132056041STakashi Sakamoto 		for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
74232056041STakashi Sakamoto 			format = oxfw->tx_stream_formats[i];
74332056041STakashi Sakamoto 			if (format == NULL)
74432056041STakashi Sakamoto 				continue;
74532056041STakashi Sakamoto 			err = snd_oxfw_stream_parse_format(format, &formation);
74632056041STakashi Sakamoto 			if (err < 0)
74732056041STakashi Sakamoto 				continue;
74832056041STakashi Sakamoto 
74932056041STakashi Sakamoto 			/* Add one MIDI port. */
75032056041STakashi Sakamoto 			if (formation.midi > 0)
75132056041STakashi Sakamoto 				oxfw->midi_input_ports = 1;
75232056041STakashi Sakamoto 		}
75332056041STakashi Sakamoto 
754b0ac0009STakashi Sakamoto 		oxfw->has_output = true;
755b0ac0009STakashi Sakamoto 	}
756b0ac0009STakashi Sakamoto 
7575cd1d3f4STakashi Sakamoto 	/* use iPCR[0] if exists */
75832056041STakashi Sakamoto 	if (plugs[0] > 0) {
7595cd1d3f4STakashi Sakamoto 		err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
76032056041STakashi Sakamoto 		if (err < 0)
76132056041STakashi Sakamoto 			goto end;
76232056041STakashi Sakamoto 
76332056041STakashi Sakamoto 		for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
76432056041STakashi Sakamoto 			format = oxfw->rx_stream_formats[i];
76532056041STakashi Sakamoto 			if (format == NULL)
76632056041STakashi Sakamoto 				continue;
76732056041STakashi Sakamoto 			err = snd_oxfw_stream_parse_format(format, &formation);
76832056041STakashi Sakamoto 			if (err < 0)
76932056041STakashi Sakamoto 				continue;
77032056041STakashi Sakamoto 
77132056041STakashi Sakamoto 			/* Add one MIDI port. */
77232056041STakashi Sakamoto 			if (formation.midi > 0)
77332056041STakashi Sakamoto 				oxfw->midi_output_ports = 1;
77432056041STakashi Sakamoto 		}
77532056041STakashi Sakamoto 	}
7765cd1d3f4STakashi Sakamoto end:
7775cd1d3f4STakashi Sakamoto 	return err;
7785cd1d3f4STakashi Sakamoto }
7798985f4acSTakashi Sakamoto 
7808985f4acSTakashi Sakamoto void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
7818985f4acSTakashi Sakamoto {
7828985f4acSTakashi Sakamoto 	oxfw->dev_lock_changed = true;
7838985f4acSTakashi Sakamoto 	wake_up(&oxfw->hwdep_wait);
7848985f4acSTakashi Sakamoto }
7858985f4acSTakashi Sakamoto 
7868985f4acSTakashi Sakamoto int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
7878985f4acSTakashi Sakamoto {
7888985f4acSTakashi Sakamoto 	int err;
7898985f4acSTakashi Sakamoto 
7908985f4acSTakashi Sakamoto 	spin_lock_irq(&oxfw->lock);
7918985f4acSTakashi Sakamoto 
7928985f4acSTakashi Sakamoto 	/* user land lock this */
7938985f4acSTakashi Sakamoto 	if (oxfw->dev_lock_count < 0) {
7948985f4acSTakashi Sakamoto 		err = -EBUSY;
7958985f4acSTakashi Sakamoto 		goto end;
7968985f4acSTakashi Sakamoto 	}
7978985f4acSTakashi Sakamoto 
7988985f4acSTakashi Sakamoto 	/* this is the first time */
7998985f4acSTakashi Sakamoto 	if (oxfw->dev_lock_count++ == 0)
8008985f4acSTakashi Sakamoto 		snd_oxfw_stream_lock_changed(oxfw);
8018985f4acSTakashi Sakamoto 	err = 0;
8028985f4acSTakashi Sakamoto end:
8038985f4acSTakashi Sakamoto 	spin_unlock_irq(&oxfw->lock);
8048985f4acSTakashi Sakamoto 	return err;
8058985f4acSTakashi Sakamoto }
8068985f4acSTakashi Sakamoto 
8078985f4acSTakashi Sakamoto void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
8088985f4acSTakashi Sakamoto {
8098985f4acSTakashi Sakamoto 	spin_lock_irq(&oxfw->lock);
8108985f4acSTakashi Sakamoto 
8118985f4acSTakashi Sakamoto 	if (WARN_ON(oxfw->dev_lock_count <= 0))
8128985f4acSTakashi Sakamoto 		goto end;
8138985f4acSTakashi Sakamoto 	if (--oxfw->dev_lock_count == 0)
8148985f4acSTakashi Sakamoto 		snd_oxfw_stream_lock_changed(oxfw);
8158985f4acSTakashi Sakamoto end:
8168985f4acSTakashi Sakamoto 	spin_unlock_irq(&oxfw->lock);
8178985f4acSTakashi Sakamoto }
818