xref: /linux/sound/firewire/oxfw/oxfw-stream.c (revision ac5d77864cfcb0f034221c2b3dac828ae24c722e)
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 
117*ac5d7786STakashi Sakamoto 	err = amdtp_domain_add_stream(&oxfw->domain, stream,
118*ac5d7786STakashi 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,
247b0ac0009STakashi Sakamoto 				   unsigned int rate, unsigned int pcm_channels)
248b0ac0009STakashi Sakamoto {
249f3699e2cSTakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
250b0ac0009STakashi Sakamoto 	enum avc_general_plug_dir dir;
2514f380d00STakashi Sakamoto 	int err;
252e2786ca6STakashi Sakamoto 
25320358d44STakashi Sakamoto 	// Considering JACK/FFADO streaming:
25420358d44STakashi Sakamoto 	// TODO: This can be removed hwdep functionality becomes popular.
25520358d44STakashi Sakamoto 	err = check_connection_used_by_others(oxfw, &oxfw->rx_stream);
25620358d44STakashi Sakamoto 	if (err < 0)
25720358d44STakashi Sakamoto 		return err;
25820358d44STakashi Sakamoto 	if (oxfw->has_output) {
25920358d44STakashi Sakamoto 		err = check_connection_used_by_others(oxfw, &oxfw->tx_stream);
26020358d44STakashi Sakamoto 		if (err < 0)
26120358d44STakashi Sakamoto 			return err;
262b0ac0009STakashi Sakamoto 	}
263b0ac0009STakashi Sakamoto 
26420358d44STakashi Sakamoto 	if (stream == &oxfw->tx_stream)
26520358d44STakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_OUT;
26620358d44STakashi Sakamoto 	else
26720358d44STakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_IN;
268b0ac0009STakashi Sakamoto 
269b0ac0009STakashi Sakamoto 	err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
270e2786ca6STakashi Sakamoto 	if (err < 0)
27120358d44STakashi Sakamoto 		return err;
2724f380d00STakashi Sakamoto 	if (rate == 0) {
27305588d34STakashi Sakamoto 		rate = formation.rate;
27405588d34STakashi Sakamoto 		pcm_channels = formation.pcm;
2754f380d00STakashi Sakamoto 	}
2764f380d00STakashi Sakamoto 	if (formation.rate != rate || formation.pcm != pcm_channels) {
277*ac5d7786STakashi Sakamoto 		amdtp_domain_stop(&oxfw->domain);
278*ac5d7786STakashi Sakamoto 
279e34244ddSTakashi Sakamoto 		cmp_connection_break(&oxfw->in_conn);
2803f2ce83dSTakashi Sakamoto 		cmp_connection_release(&oxfw->in_conn);
281e34244ddSTakashi Sakamoto 
282e34244ddSTakashi Sakamoto 		if (oxfw->has_output) {
283e34244ddSTakashi Sakamoto 			cmp_connection_break(&oxfw->out_conn);
2843f2ce83dSTakashi Sakamoto 			cmp_connection_release(&oxfw->out_conn);
285e34244ddSTakashi Sakamoto 		}
2864f380d00STakashi Sakamoto 	}
287f3699e2cSTakashi Sakamoto 
2884f380d00STakashi Sakamoto 	if (oxfw->substreams_count == 0 ||
2894f380d00STakashi Sakamoto 	    formation.rate != rate || formation.pcm != pcm_channels) {
290b0ac0009STakashi Sakamoto 		err = set_stream_format(oxfw, stream, rate, pcm_channels);
291f3699e2cSTakashi Sakamoto 		if (err < 0) {
292f3699e2cSTakashi Sakamoto 			dev_err(&oxfw->unit->device,
293f3699e2cSTakashi Sakamoto 				"fail to set stream format: %d\n", err);
29420358d44STakashi Sakamoto 			return err;
29520358d44STakashi Sakamoto 		}
2960356ce3aSTakashi Sakamoto 
2970356ce3aSTakashi Sakamoto 		err = keep_resources(oxfw, &oxfw->rx_stream);
2980356ce3aSTakashi Sakamoto 		if (err < 0)
2990356ce3aSTakashi Sakamoto 			return err;
3000356ce3aSTakashi Sakamoto 
3010356ce3aSTakashi Sakamoto 		if (oxfw->has_output) {
3020356ce3aSTakashi Sakamoto 			err = keep_resources(oxfw, &oxfw->tx_stream);
3037bc93821STakashi Sakamoto 			if (err < 0) {
3047bc93821STakashi Sakamoto 				cmp_connection_release(&oxfw->in_conn);
3050356ce3aSTakashi Sakamoto 				return err;
3060356ce3aSTakashi Sakamoto 			}
307f3699e2cSTakashi Sakamoto 		}
3087bc93821STakashi Sakamoto 	}
309b0ac0009STakashi Sakamoto 
3104f380d00STakashi Sakamoto 	return 0;
3114f380d00STakashi Sakamoto }
3124f380d00STakashi Sakamoto 
3134f380d00STakashi Sakamoto int snd_oxfw_stream_start_duplex(struct snd_oxfw *oxfw)
3144f380d00STakashi Sakamoto {
3154f380d00STakashi Sakamoto 	int err;
3164f380d00STakashi Sakamoto 
3174f380d00STakashi Sakamoto 	if (oxfw->substreams_count == 0)
3184f380d00STakashi Sakamoto 		return -EIO;
3194f380d00STakashi Sakamoto 
3204f380d00STakashi Sakamoto 	if (amdtp_streaming_error(&oxfw->rx_stream) ||
3214f380d00STakashi Sakamoto 	    amdtp_streaming_error(&oxfw->tx_stream)) {
322*ac5d7786STakashi Sakamoto 		amdtp_domain_stop(&oxfw->domain);
3234f380d00STakashi Sakamoto 
324*ac5d7786STakashi Sakamoto 		cmp_connection_break(&oxfw->in_conn);
325*ac5d7786STakashi Sakamoto 		if (oxfw->has_output)
3264f380d00STakashi Sakamoto 			cmp_connection_break(&oxfw->out_conn);
3274f380d00STakashi Sakamoto 	}
3284f380d00STakashi Sakamoto 
32920358d44STakashi Sakamoto 	if (!amdtp_stream_running(&oxfw->rx_stream)) {
33020358d44STakashi Sakamoto 		err = start_stream(oxfw, &oxfw->rx_stream);
331b0ac0009STakashi Sakamoto 		if (err < 0) {
332b0ac0009STakashi Sakamoto 			dev_err(&oxfw->unit->device,
333*ac5d7786STakashi Sakamoto 				"fail to prepare rx stream: %d\n", err);
334*ac5d7786STakashi Sakamoto 			goto error;
335*ac5d7786STakashi Sakamoto 		}
336*ac5d7786STakashi Sakamoto 
337*ac5d7786STakashi Sakamoto 		if (oxfw->has_output &&
338*ac5d7786STakashi Sakamoto 		    !amdtp_stream_running(&oxfw->tx_stream)) {
339*ac5d7786STakashi Sakamoto 			err = start_stream(oxfw, &oxfw->tx_stream);
340*ac5d7786STakashi Sakamoto 			if (err < 0) {
341*ac5d7786STakashi Sakamoto 				dev_err(&oxfw->unit->device,
342*ac5d7786STakashi Sakamoto 					"fail to prepare tx stream: %d\n", err);
34320358d44STakashi Sakamoto 				goto error;
34420358d44STakashi Sakamoto 			}
34520358d44STakashi Sakamoto 		}
34620358d44STakashi Sakamoto 
347*ac5d7786STakashi Sakamoto 		err = amdtp_domain_start(&oxfw->domain);
348*ac5d7786STakashi Sakamoto 		if (err < 0)
349*ac5d7786STakashi Sakamoto 			goto error;
350*ac5d7786STakashi Sakamoto 
351*ac5d7786STakashi Sakamoto 		// Wait first packet.
352*ac5d7786STakashi Sakamoto 		if (!amdtp_stream_wait_callback(&oxfw->rx_stream,
353*ac5d7786STakashi Sakamoto 						CALLBACK_TIMEOUT) ||
354*ac5d7786STakashi Sakamoto 		    !amdtp_stream_wait_callback(&oxfw->tx_stream,
355*ac5d7786STakashi Sakamoto 						CALLBACK_TIMEOUT)) {
356*ac5d7786STakashi Sakamoto 			err = -ETIMEDOUT;
35720358d44STakashi Sakamoto 			goto error;
358b0ac0009STakashi Sakamoto 		}
359*ac5d7786STakashi Sakamoto 
360f3699e2cSTakashi Sakamoto 	}
361f3699e2cSTakashi Sakamoto 
36220358d44STakashi Sakamoto 	return 0;
36320358d44STakashi Sakamoto error:
364*ac5d7786STakashi Sakamoto 	amdtp_domain_stop(&oxfw->domain);
365*ac5d7786STakashi Sakamoto 
36620358d44STakashi Sakamoto 	cmp_connection_break(&oxfw->in_conn);
367*ac5d7786STakashi Sakamoto 	if (oxfw->has_output)
36820358d44STakashi Sakamoto 		cmp_connection_break(&oxfw->out_conn);
369*ac5d7786STakashi Sakamoto 
370e2786ca6STakashi Sakamoto 	return err;
371e2786ca6STakashi Sakamoto }
372e2786ca6STakashi Sakamoto 
373779f0dbaSTakashi Sakamoto void snd_oxfw_stream_stop_duplex(struct snd_oxfw *oxfw)
374e2786ca6STakashi Sakamoto {
3754a0a0472STakashi Sakamoto 	if (oxfw->substreams_count == 0) {
376*ac5d7786STakashi Sakamoto 		amdtp_domain_stop(&oxfw->domain);
377*ac5d7786STakashi Sakamoto 
378e34244ddSTakashi Sakamoto 		cmp_connection_break(&oxfw->in_conn);
3797bc93821STakashi Sakamoto 		cmp_connection_release(&oxfw->in_conn);
380b0ac0009STakashi Sakamoto 
381e34244ddSTakashi Sakamoto 		if (oxfw->has_output) {
382e34244ddSTakashi Sakamoto 			cmp_connection_break(&oxfw->out_conn);
3837bc93821STakashi Sakamoto 			cmp_connection_release(&oxfw->out_conn);
384e34244ddSTakashi Sakamoto 		}
38520358d44STakashi Sakamoto 	}
386e2786ca6STakashi Sakamoto }
387e2786ca6STakashi Sakamoto 
388779f0dbaSTakashi Sakamoto static void destroy_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
389e2786ca6STakashi Sakamoto {
390b0ac0009STakashi Sakamoto 	struct cmp_connection *conn;
391e2786ca6STakashi Sakamoto 
392b0ac0009STakashi Sakamoto 	if (stream == &oxfw->tx_stream)
393b0ac0009STakashi Sakamoto 		conn = &oxfw->out_conn;
394e2786ca6STakashi Sakamoto 	else
395b0ac0009STakashi Sakamoto 		conn = &oxfw->in_conn;
396b0ac0009STakashi Sakamoto 
397b0ac0009STakashi Sakamoto 	amdtp_stream_destroy(stream);
398b0ac0009STakashi Sakamoto 	cmp_connection_destroy(conn);
399b0ac0009STakashi Sakamoto }
400b0ac0009STakashi Sakamoto 
401779f0dbaSTakashi Sakamoto int snd_oxfw_stream_init_duplex(struct snd_oxfw *oxfw)
402779f0dbaSTakashi Sakamoto {
403779f0dbaSTakashi Sakamoto 	int err;
404779f0dbaSTakashi Sakamoto 
405779f0dbaSTakashi Sakamoto 	err = init_stream(oxfw, &oxfw->rx_stream);
406779f0dbaSTakashi Sakamoto 	if (err < 0)
407779f0dbaSTakashi Sakamoto 		return err;
408779f0dbaSTakashi Sakamoto 
409779f0dbaSTakashi Sakamoto 	if (oxfw->has_output) {
410779f0dbaSTakashi Sakamoto 		err = init_stream(oxfw, &oxfw->tx_stream);
411779f0dbaSTakashi Sakamoto 		if (err < 0) {
412779f0dbaSTakashi Sakamoto 			destroy_stream(oxfw, &oxfw->rx_stream);
413779f0dbaSTakashi Sakamoto 			return err;
414779f0dbaSTakashi Sakamoto 		}
415779f0dbaSTakashi Sakamoto 	}
416779f0dbaSTakashi Sakamoto 
417*ac5d7786STakashi Sakamoto 	err = amdtp_domain_init(&oxfw->domain);
418*ac5d7786STakashi Sakamoto 	if (err < 0) {
419*ac5d7786STakashi Sakamoto 		destroy_stream(oxfw, &oxfw->rx_stream);
420*ac5d7786STakashi Sakamoto 		if (oxfw->has_output)
421*ac5d7786STakashi Sakamoto 			destroy_stream(oxfw, &oxfw->tx_stream);
422*ac5d7786STakashi Sakamoto 	}
423*ac5d7786STakashi Sakamoto 
424*ac5d7786STakashi Sakamoto 	return err;
425779f0dbaSTakashi Sakamoto }
426779f0dbaSTakashi Sakamoto 
427779f0dbaSTakashi Sakamoto // This function should be called before starting the stream or after stopping
428779f0dbaSTakashi Sakamoto // the streams.
429779f0dbaSTakashi Sakamoto void snd_oxfw_stream_destroy_duplex(struct snd_oxfw *oxfw)
430779f0dbaSTakashi Sakamoto {
431*ac5d7786STakashi Sakamoto 	amdtp_domain_destroy(&oxfw->domain);
432*ac5d7786STakashi Sakamoto 
433779f0dbaSTakashi Sakamoto 	destroy_stream(oxfw, &oxfw->rx_stream);
434779f0dbaSTakashi Sakamoto 
435779f0dbaSTakashi Sakamoto 	if (oxfw->has_output)
436779f0dbaSTakashi Sakamoto 		destroy_stream(oxfw, &oxfw->tx_stream);
437779f0dbaSTakashi Sakamoto }
438779f0dbaSTakashi Sakamoto 
439779f0dbaSTakashi Sakamoto void snd_oxfw_stream_update_duplex(struct snd_oxfw *oxfw)
440b0ac0009STakashi Sakamoto {
441*ac5d7786STakashi Sakamoto 	amdtp_domain_stop(&oxfw->domain);
442*ac5d7786STakashi Sakamoto 
443e34244ddSTakashi Sakamoto 	cmp_connection_break(&oxfw->in_conn);
444b0ac0009STakashi Sakamoto 
445e34244ddSTakashi Sakamoto 	amdtp_stream_pcm_abort(&oxfw->rx_stream);
446e34244ddSTakashi Sakamoto 
447e34244ddSTakashi Sakamoto 	if (oxfw->has_output) {
448e34244ddSTakashi Sakamoto 		cmp_connection_break(&oxfw->out_conn);
449e34244ddSTakashi Sakamoto 
450e34244ddSTakashi Sakamoto 		amdtp_stream_pcm_abort(&oxfw->tx_stream);
451e34244ddSTakashi Sakamoto 	}
452e2786ca6STakashi Sakamoto }
4535cd1d3f4STakashi Sakamoto 
4543c96101fSTakashi Sakamoto int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
4553c96101fSTakashi Sakamoto 				enum avc_general_plug_dir dir,
4563c96101fSTakashi Sakamoto 				struct snd_oxfw_stream_formation *formation)
4573c96101fSTakashi Sakamoto {
4583c96101fSTakashi Sakamoto 	u8 *format;
4593c96101fSTakashi Sakamoto 	unsigned int len;
4603c96101fSTakashi Sakamoto 	int err;
4613c96101fSTakashi Sakamoto 
4623c96101fSTakashi Sakamoto 	len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
4633c96101fSTakashi Sakamoto 	format = kmalloc(len, GFP_KERNEL);
4643c96101fSTakashi Sakamoto 	if (format == NULL)
4653c96101fSTakashi Sakamoto 		return -ENOMEM;
4663c96101fSTakashi Sakamoto 
4673c96101fSTakashi Sakamoto 	err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
4683c96101fSTakashi Sakamoto 	if (err < 0)
4693c96101fSTakashi Sakamoto 		goto end;
4703c96101fSTakashi Sakamoto 	if (len < 3) {
4713c96101fSTakashi Sakamoto 		err = -EIO;
4723c96101fSTakashi Sakamoto 		goto end;
4733c96101fSTakashi Sakamoto 	}
4743c96101fSTakashi Sakamoto 
4753c96101fSTakashi Sakamoto 	err = snd_oxfw_stream_parse_format(format, formation);
4763c96101fSTakashi Sakamoto end:
4773c96101fSTakashi Sakamoto 	kfree(format);
4783c96101fSTakashi Sakamoto 	return err;
4793c96101fSTakashi Sakamoto }
4803c96101fSTakashi Sakamoto 
4815cd1d3f4STakashi Sakamoto /*
4825cd1d3f4STakashi Sakamoto  * See Table 6.16 - AM824 Stream Format
4835cd1d3f4STakashi Sakamoto  *     Figure 6.19 - format_information field for AM824 Compound
4845cd1d3f4STakashi Sakamoto  * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
4855cd1d3f4STakashi Sakamoto  * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
4865cd1d3f4STakashi Sakamoto  */
4875cd1d3f4STakashi Sakamoto int snd_oxfw_stream_parse_format(u8 *format,
4885cd1d3f4STakashi Sakamoto 				 struct snd_oxfw_stream_formation *formation)
4895cd1d3f4STakashi Sakamoto {
4905cd1d3f4STakashi Sakamoto 	unsigned int i, e, channels, type;
4915cd1d3f4STakashi Sakamoto 
4925cd1d3f4STakashi Sakamoto 	memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
4935cd1d3f4STakashi Sakamoto 
4945cd1d3f4STakashi Sakamoto 	/*
4955cd1d3f4STakashi Sakamoto 	 * this module can support a hierarchy combination that:
4965cd1d3f4STakashi Sakamoto 	 *  Root:	Audio and Music (0x90)
4975cd1d3f4STakashi Sakamoto 	 *  Level 1:	AM824 Compound  (0x40)
4985cd1d3f4STakashi Sakamoto 	 */
4995cd1d3f4STakashi Sakamoto 	if ((format[0] != 0x90) || (format[1] != 0x40))
5005cd1d3f4STakashi Sakamoto 		return -ENOSYS;
5015cd1d3f4STakashi Sakamoto 
5025cd1d3f4STakashi Sakamoto 	/* check the sampling rate */
5035cd1d3f4STakashi Sakamoto 	for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
5045cd1d3f4STakashi Sakamoto 		if (format[2] == avc_stream_rate_table[i])
5055cd1d3f4STakashi Sakamoto 			break;
5065cd1d3f4STakashi Sakamoto 	}
5075cd1d3f4STakashi Sakamoto 	if (i == ARRAY_SIZE(avc_stream_rate_table))
5085cd1d3f4STakashi Sakamoto 		return -ENOSYS;
5095cd1d3f4STakashi Sakamoto 
5105cd1d3f4STakashi Sakamoto 	formation->rate = oxfw_rate_table[i];
5115cd1d3f4STakashi Sakamoto 
5125cd1d3f4STakashi Sakamoto 	for (e = 0; e < format[4]; e++) {
5135cd1d3f4STakashi Sakamoto 		channels = format[5 + e * 2];
5145cd1d3f4STakashi Sakamoto 		type = format[6 + e * 2];
5155cd1d3f4STakashi Sakamoto 
5165cd1d3f4STakashi Sakamoto 		switch (type) {
5175cd1d3f4STakashi Sakamoto 		/* IEC 60958 Conformant, currently handled as MBLA */
5185cd1d3f4STakashi Sakamoto 		case 0x00:
5195cd1d3f4STakashi Sakamoto 		/* Multi Bit Linear Audio (Raw) */
5205cd1d3f4STakashi Sakamoto 		case 0x06:
5215cd1d3f4STakashi Sakamoto 			formation->pcm += channels;
5225cd1d3f4STakashi Sakamoto 			break;
5235cd1d3f4STakashi Sakamoto 		/* MIDI Conformant */
5245cd1d3f4STakashi Sakamoto 		case 0x0d:
5255cd1d3f4STakashi Sakamoto 			formation->midi = channels;
5265cd1d3f4STakashi Sakamoto 			break;
5275cd1d3f4STakashi Sakamoto 		/* IEC 61937-3 to 7 */
5285cd1d3f4STakashi Sakamoto 		case 0x01:
5295cd1d3f4STakashi Sakamoto 		case 0x02:
5305cd1d3f4STakashi Sakamoto 		case 0x03:
5315cd1d3f4STakashi Sakamoto 		case 0x04:
5325cd1d3f4STakashi Sakamoto 		case 0x05:
5335cd1d3f4STakashi Sakamoto 		/* Multi Bit Linear Audio */
5345cd1d3f4STakashi Sakamoto 		case 0x07:	/* DVD-Audio */
5355cd1d3f4STakashi Sakamoto 		case 0x0c:	/* High Precision */
5365cd1d3f4STakashi Sakamoto 		/* One Bit Audio */
5375cd1d3f4STakashi Sakamoto 		case 0x08:	/* (Plain) Raw */
5385cd1d3f4STakashi Sakamoto 		case 0x09:	/* (Plain) SACD */
5395cd1d3f4STakashi Sakamoto 		case 0x0a:	/* (Encoded) Raw */
5405cd1d3f4STakashi Sakamoto 		case 0x0b:	/* (Encoded) SACD */
5415cd1d3f4STakashi Sakamoto 		/* SMPTE Time-Code conformant */
5425cd1d3f4STakashi Sakamoto 		case 0x0e:
5435cd1d3f4STakashi Sakamoto 		/* Sample Count */
5445cd1d3f4STakashi Sakamoto 		case 0x0f:
5455cd1d3f4STakashi Sakamoto 		/* Anciliary Data */
5465cd1d3f4STakashi Sakamoto 		case 0x10:
5475cd1d3f4STakashi Sakamoto 		/* Synchronization Stream (Stereo Raw audio) */
5485cd1d3f4STakashi Sakamoto 		case 0x40:
5495cd1d3f4STakashi Sakamoto 		/* Don't care */
5505cd1d3f4STakashi Sakamoto 		case 0xff:
5515cd1d3f4STakashi Sakamoto 		default:
5525cd1d3f4STakashi Sakamoto 			return -ENOSYS;	/* not supported */
5535cd1d3f4STakashi Sakamoto 		}
5545cd1d3f4STakashi Sakamoto 	}
5555cd1d3f4STakashi Sakamoto 
55649c7b3fcSTakashi Sakamoto 	if (formation->pcm  > AM824_MAX_CHANNELS_FOR_PCM ||
55749c7b3fcSTakashi Sakamoto 	    formation->midi > AM824_MAX_CHANNELS_FOR_MIDI)
5585cd1d3f4STakashi Sakamoto 		return -ENOSYS;
5595cd1d3f4STakashi Sakamoto 
5605cd1d3f4STakashi Sakamoto 	return 0;
5615cd1d3f4STakashi Sakamoto }
5625cd1d3f4STakashi Sakamoto 
5635cd1d3f4STakashi Sakamoto static int
5645cd1d3f4STakashi Sakamoto assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
5655cd1d3f4STakashi Sakamoto 		      unsigned int pid, u8 *buf, unsigned int *len,
5665cd1d3f4STakashi Sakamoto 		      u8 **formats)
5675cd1d3f4STakashi Sakamoto {
5685cd1d3f4STakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
5695cd1d3f4STakashi Sakamoto 	unsigned int i, eid;
5705cd1d3f4STakashi Sakamoto 	int err;
5715cd1d3f4STakashi Sakamoto 
5725cd1d3f4STakashi Sakamoto 	/* get format at current sampling rate */
5735cd1d3f4STakashi Sakamoto 	err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
5745cd1d3f4STakashi Sakamoto 	if (err < 0) {
5755cd1d3f4STakashi Sakamoto 		dev_err(&oxfw->unit->device,
5765cd1d3f4STakashi Sakamoto 		"fail to get current stream format for isoc %s plug %d:%d\n",
5775cd1d3f4STakashi Sakamoto 			(dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
5785cd1d3f4STakashi Sakamoto 			pid, err);
5795cd1d3f4STakashi Sakamoto 		goto end;
5805cd1d3f4STakashi Sakamoto 	}
5815cd1d3f4STakashi Sakamoto 
5825cd1d3f4STakashi Sakamoto 	/* parse and set stream format */
5835cd1d3f4STakashi Sakamoto 	eid = 0;
5845cd1d3f4STakashi Sakamoto 	err = snd_oxfw_stream_parse_format(buf, &formation);
5855cd1d3f4STakashi Sakamoto 	if (err < 0)
5865cd1d3f4STakashi Sakamoto 		goto end;
5875cd1d3f4STakashi Sakamoto 
588cd3b7116STakashi Sakamoto 	formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
589cd3b7116STakashi Sakamoto 				    GFP_KERNEL);
590cd3b7116STakashi Sakamoto 	if (!formats[eid]) {
5915cd1d3f4STakashi Sakamoto 		err = -ENOMEM;
5925cd1d3f4STakashi Sakamoto 		goto end;
5935cd1d3f4STakashi Sakamoto 	}
5945cd1d3f4STakashi Sakamoto 
5955cd1d3f4STakashi Sakamoto 	/* apply the format for each available sampling rate */
5965cd1d3f4STakashi Sakamoto 	for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
5975cd1d3f4STakashi Sakamoto 		if (formation.rate == oxfw_rate_table[i])
5985cd1d3f4STakashi Sakamoto 			continue;
5995cd1d3f4STakashi Sakamoto 
6005cd1d3f4STakashi Sakamoto 		err = avc_general_inquiry_sig_fmt(oxfw->unit,
6015cd1d3f4STakashi Sakamoto 						  oxfw_rate_table[i],
6025cd1d3f4STakashi Sakamoto 						  dir, pid);
6035cd1d3f4STakashi Sakamoto 		if (err < 0)
6045cd1d3f4STakashi Sakamoto 			continue;
6055cd1d3f4STakashi Sakamoto 
6065cd1d3f4STakashi Sakamoto 		eid++;
607cd3b7116STakashi Sakamoto 		formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
608cd3b7116STakashi Sakamoto 					    GFP_KERNEL);
6095cd1d3f4STakashi Sakamoto 		if (formats[eid] == NULL) {
6105cd1d3f4STakashi Sakamoto 			err = -ENOMEM;
6115cd1d3f4STakashi Sakamoto 			goto end;
6125cd1d3f4STakashi Sakamoto 		}
6135cd1d3f4STakashi Sakamoto 		formats[eid][2] = avc_stream_rate_table[i];
6145cd1d3f4STakashi Sakamoto 	}
6155cd1d3f4STakashi Sakamoto 
6165cd1d3f4STakashi Sakamoto 	err = 0;
6175cd1d3f4STakashi Sakamoto 	oxfw->assumed = true;
6185cd1d3f4STakashi Sakamoto end:
6195cd1d3f4STakashi Sakamoto 	return err;
6205cd1d3f4STakashi Sakamoto }
6215cd1d3f4STakashi Sakamoto 
6225cd1d3f4STakashi Sakamoto static int fill_stream_formats(struct snd_oxfw *oxfw,
6235cd1d3f4STakashi Sakamoto 			       enum avc_general_plug_dir dir,
6245cd1d3f4STakashi Sakamoto 			       unsigned short pid)
6255cd1d3f4STakashi Sakamoto {
6265cd1d3f4STakashi Sakamoto 	u8 *buf, **formats;
6275cd1d3f4STakashi Sakamoto 	unsigned int len, eid = 0;
6285cd1d3f4STakashi Sakamoto 	struct snd_oxfw_stream_formation dummy;
6295cd1d3f4STakashi Sakamoto 	int err;
6305cd1d3f4STakashi Sakamoto 
6315cd1d3f4STakashi Sakamoto 	buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
6325cd1d3f4STakashi Sakamoto 	if (buf == NULL)
6335cd1d3f4STakashi Sakamoto 		return -ENOMEM;
6345cd1d3f4STakashi Sakamoto 
635b0ac0009STakashi Sakamoto 	if (dir == AVC_GENERAL_PLUG_DIR_OUT)
636b0ac0009STakashi Sakamoto 		formats = oxfw->tx_stream_formats;
637b0ac0009STakashi Sakamoto 	else
6385cd1d3f4STakashi Sakamoto 		formats = oxfw->rx_stream_formats;
6395cd1d3f4STakashi Sakamoto 
6405cd1d3f4STakashi Sakamoto 	/* get first entry */
6415cd1d3f4STakashi Sakamoto 	len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
6425cd1d3f4STakashi Sakamoto 	err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
6435cd1d3f4STakashi Sakamoto 	if (err == -ENOSYS) {
6445cd1d3f4STakashi Sakamoto 		/* LIST subfunction is not implemented */
6455cd1d3f4STakashi Sakamoto 		len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
6465cd1d3f4STakashi Sakamoto 		err = assume_stream_formats(oxfw, dir, pid, buf, &len,
6475cd1d3f4STakashi Sakamoto 					    formats);
6485cd1d3f4STakashi Sakamoto 		goto end;
6495cd1d3f4STakashi Sakamoto 	} else if (err < 0) {
6505cd1d3f4STakashi Sakamoto 		dev_err(&oxfw->unit->device,
6515cd1d3f4STakashi Sakamoto 			"fail to get stream format %d for isoc %s plug %d:%d\n",
6525cd1d3f4STakashi Sakamoto 			eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
6535cd1d3f4STakashi Sakamoto 			pid, err);
6545cd1d3f4STakashi Sakamoto 		goto end;
6555cd1d3f4STakashi Sakamoto 	}
6565cd1d3f4STakashi Sakamoto 
6575cd1d3f4STakashi Sakamoto 	/* LIST subfunction is implemented */
6585cd1d3f4STakashi Sakamoto 	while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
6595cd1d3f4STakashi Sakamoto 		/* The format is too short. */
6605cd1d3f4STakashi Sakamoto 		if (len < 3) {
6615cd1d3f4STakashi Sakamoto 			err = -EIO;
6625cd1d3f4STakashi Sakamoto 			break;
6635cd1d3f4STakashi Sakamoto 		}
6645cd1d3f4STakashi Sakamoto 
6655cd1d3f4STakashi Sakamoto 		/* parse and set stream format */
6665cd1d3f4STakashi Sakamoto 		err = snd_oxfw_stream_parse_format(buf, &dummy);
6675cd1d3f4STakashi Sakamoto 		if (err < 0)
6685cd1d3f4STakashi Sakamoto 			break;
6695cd1d3f4STakashi Sakamoto 
670cd3b7116STakashi Sakamoto 		formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, len,
671cd3b7116STakashi Sakamoto 					    GFP_KERNEL);
672cd3b7116STakashi Sakamoto 		if (!formats[eid]) {
6735cd1d3f4STakashi Sakamoto 			err = -ENOMEM;
6745cd1d3f4STakashi Sakamoto 			break;
6755cd1d3f4STakashi Sakamoto 		}
6765cd1d3f4STakashi Sakamoto 
6775cd1d3f4STakashi Sakamoto 		/* get next entry */
6785cd1d3f4STakashi Sakamoto 		len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
6795cd1d3f4STakashi Sakamoto 		err = avc_stream_get_format_list(oxfw->unit, dir, 0,
6805cd1d3f4STakashi Sakamoto 						 buf, &len, ++eid);
6815cd1d3f4STakashi Sakamoto 		/* No entries remained. */
6825cd1d3f4STakashi Sakamoto 		if (err == -EINVAL) {
6835cd1d3f4STakashi Sakamoto 			err = 0;
6845cd1d3f4STakashi Sakamoto 			break;
6855cd1d3f4STakashi Sakamoto 		} else if (err < 0) {
6865cd1d3f4STakashi Sakamoto 			dev_err(&oxfw->unit->device,
6875cd1d3f4STakashi Sakamoto 			"fail to get stream format %d for isoc %s plug %d:%d\n",
6885cd1d3f4STakashi Sakamoto 				eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
6895cd1d3f4STakashi Sakamoto 									"out",
6905cd1d3f4STakashi Sakamoto 				pid, err);
6915cd1d3f4STakashi Sakamoto 			break;
6925cd1d3f4STakashi Sakamoto 		}
6935cd1d3f4STakashi Sakamoto 	}
6945cd1d3f4STakashi Sakamoto end:
6955cd1d3f4STakashi Sakamoto 	kfree(buf);
6965cd1d3f4STakashi Sakamoto 	return err;
6975cd1d3f4STakashi Sakamoto }
6985cd1d3f4STakashi Sakamoto 
6995cd1d3f4STakashi Sakamoto int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
7005cd1d3f4STakashi Sakamoto {
7015cd1d3f4STakashi Sakamoto 	u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
70232056041STakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
70332056041STakashi Sakamoto 	u8 *format;
70432056041STakashi Sakamoto 	unsigned int i;
7055cd1d3f4STakashi Sakamoto 	int err;
7065cd1d3f4STakashi Sakamoto 
7075cd1d3f4STakashi Sakamoto 	/* the number of plugs for isoc in/out, ext in/out  */
7085cd1d3f4STakashi Sakamoto 	err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
7095cd1d3f4STakashi Sakamoto 	if (err < 0) {
7105cd1d3f4STakashi Sakamoto 		dev_err(&oxfw->unit->device,
7115cd1d3f4STakashi Sakamoto 		"fail to get info for isoc/external in/out plugs: %d\n",
7125cd1d3f4STakashi Sakamoto 			err);
7135cd1d3f4STakashi Sakamoto 		goto end;
714b0ac0009STakashi Sakamoto 	} else if ((plugs[0] == 0) && (plugs[1] == 0)) {
7155cd1d3f4STakashi Sakamoto 		err = -ENOSYS;
7165cd1d3f4STakashi Sakamoto 		goto end;
7175cd1d3f4STakashi Sakamoto 	}
7185cd1d3f4STakashi Sakamoto 
719b0ac0009STakashi Sakamoto 	/* use oPCR[0] if exists */
720b0ac0009STakashi Sakamoto 	if (plugs[1] > 0) {
721b0ac0009STakashi Sakamoto 		err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
722b0ac0009STakashi Sakamoto 		if (err < 0)
723b0ac0009STakashi Sakamoto 			goto end;
72432056041STakashi Sakamoto 
72532056041STakashi Sakamoto 		for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
72632056041STakashi Sakamoto 			format = oxfw->tx_stream_formats[i];
72732056041STakashi Sakamoto 			if (format == NULL)
72832056041STakashi Sakamoto 				continue;
72932056041STakashi Sakamoto 			err = snd_oxfw_stream_parse_format(format, &formation);
73032056041STakashi Sakamoto 			if (err < 0)
73132056041STakashi Sakamoto 				continue;
73232056041STakashi Sakamoto 
73332056041STakashi Sakamoto 			/* Add one MIDI port. */
73432056041STakashi Sakamoto 			if (formation.midi > 0)
73532056041STakashi Sakamoto 				oxfw->midi_input_ports = 1;
73632056041STakashi Sakamoto 		}
73732056041STakashi Sakamoto 
738b0ac0009STakashi Sakamoto 		oxfw->has_output = true;
739b0ac0009STakashi Sakamoto 	}
740b0ac0009STakashi Sakamoto 
7415cd1d3f4STakashi Sakamoto 	/* use iPCR[0] if exists */
74232056041STakashi Sakamoto 	if (plugs[0] > 0) {
7435cd1d3f4STakashi Sakamoto 		err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
74432056041STakashi Sakamoto 		if (err < 0)
74532056041STakashi Sakamoto 			goto end;
74632056041STakashi Sakamoto 
74732056041STakashi Sakamoto 		for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
74832056041STakashi Sakamoto 			format = oxfw->rx_stream_formats[i];
74932056041STakashi Sakamoto 			if (format == NULL)
75032056041STakashi Sakamoto 				continue;
75132056041STakashi Sakamoto 			err = snd_oxfw_stream_parse_format(format, &formation);
75232056041STakashi Sakamoto 			if (err < 0)
75332056041STakashi Sakamoto 				continue;
75432056041STakashi Sakamoto 
75532056041STakashi Sakamoto 			/* Add one MIDI port. */
75632056041STakashi Sakamoto 			if (formation.midi > 0)
75732056041STakashi Sakamoto 				oxfw->midi_output_ports = 1;
75832056041STakashi Sakamoto 		}
75932056041STakashi Sakamoto 	}
7605cd1d3f4STakashi Sakamoto end:
7615cd1d3f4STakashi Sakamoto 	return err;
7625cd1d3f4STakashi Sakamoto }
7638985f4acSTakashi Sakamoto 
7648985f4acSTakashi Sakamoto void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
7658985f4acSTakashi Sakamoto {
7668985f4acSTakashi Sakamoto 	oxfw->dev_lock_changed = true;
7678985f4acSTakashi Sakamoto 	wake_up(&oxfw->hwdep_wait);
7688985f4acSTakashi Sakamoto }
7698985f4acSTakashi Sakamoto 
7708985f4acSTakashi Sakamoto int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
7718985f4acSTakashi Sakamoto {
7728985f4acSTakashi Sakamoto 	int err;
7738985f4acSTakashi Sakamoto 
7748985f4acSTakashi Sakamoto 	spin_lock_irq(&oxfw->lock);
7758985f4acSTakashi Sakamoto 
7768985f4acSTakashi Sakamoto 	/* user land lock this */
7778985f4acSTakashi Sakamoto 	if (oxfw->dev_lock_count < 0) {
7788985f4acSTakashi Sakamoto 		err = -EBUSY;
7798985f4acSTakashi Sakamoto 		goto end;
7808985f4acSTakashi Sakamoto 	}
7818985f4acSTakashi Sakamoto 
7828985f4acSTakashi Sakamoto 	/* this is the first time */
7838985f4acSTakashi Sakamoto 	if (oxfw->dev_lock_count++ == 0)
7848985f4acSTakashi Sakamoto 		snd_oxfw_stream_lock_changed(oxfw);
7858985f4acSTakashi Sakamoto 	err = 0;
7868985f4acSTakashi Sakamoto end:
7878985f4acSTakashi Sakamoto 	spin_unlock_irq(&oxfw->lock);
7888985f4acSTakashi Sakamoto 	return err;
7898985f4acSTakashi Sakamoto }
7908985f4acSTakashi Sakamoto 
7918985f4acSTakashi Sakamoto void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
7928985f4acSTakashi Sakamoto {
7938985f4acSTakashi Sakamoto 	spin_lock_irq(&oxfw->lock);
7948985f4acSTakashi Sakamoto 
7958985f4acSTakashi Sakamoto 	if (WARN_ON(oxfw->dev_lock_count <= 0))
7968985f4acSTakashi Sakamoto 		goto end;
7978985f4acSTakashi Sakamoto 	if (--oxfw->dev_lock_count == 0)
7988985f4acSTakashi Sakamoto 		snd_oxfw_stream_lock_changed(oxfw);
7998985f4acSTakashi Sakamoto end:
8008985f4acSTakashi Sakamoto 	spin_unlock_irq(&oxfw->lock);
8018985f4acSTakashi Sakamoto }
802