xref: /linux/sound/firewire/oxfw/oxfw-stream.c (revision 32056041019aa91c2555cc4c280f9fbca8a1be99)
1e2786ca6STakashi Sakamoto /*
2e2786ca6STakashi Sakamoto  * oxfw_stream.c - a part of driver for OXFW970/971 based devices
3e2786ca6STakashi Sakamoto  *
4e2786ca6STakashi Sakamoto  * Copyright (c) 2014 Takashi Sakamoto
5e2786ca6STakashi Sakamoto  *
6e2786ca6STakashi Sakamoto  * Licensed under the terms of the GNU General Public License, version 2.
7e2786ca6STakashi Sakamoto  */
8e2786ca6STakashi Sakamoto 
9e2786ca6STakashi Sakamoto #include "oxfw.h"
10f3699e2cSTakashi Sakamoto #include <linux/delay.h>
11e2786ca6STakashi Sakamoto 
125cd1d3f4STakashi Sakamoto #define AVC_GENERIC_FRAME_MAXIMUM_BYTES	512
13f3699e2cSTakashi Sakamoto #define CALLBACK_TIMEOUT	200
145cd1d3f4STakashi Sakamoto 
155cd1d3f4STakashi Sakamoto /*
165cd1d3f4STakashi Sakamoto  * According to datasheet of Oxford Semiconductor:
175cd1d3f4STakashi Sakamoto  *  OXFW970: 32.0/44.1/48.0/96.0 Khz, 8 audio channels I/O
185cd1d3f4STakashi Sakamoto  *  OXFW971: 32.0/44.1/48.0/88.2/96.0/192.0 kHz, 16 audio channels I/O, MIDI I/O
195cd1d3f4STakashi Sakamoto  */
205cd1d3f4STakashi Sakamoto static const unsigned int oxfw_rate_table[] = {
215cd1d3f4STakashi Sakamoto 	[0] = 32000,
225cd1d3f4STakashi Sakamoto 	[1] = 44100,
235cd1d3f4STakashi Sakamoto 	[2] = 48000,
245cd1d3f4STakashi Sakamoto 	[3] = 88200,
255cd1d3f4STakashi Sakamoto 	[4] = 96000,
265cd1d3f4STakashi Sakamoto 	[5] = 192000,
275cd1d3f4STakashi Sakamoto };
285cd1d3f4STakashi Sakamoto 
295cd1d3f4STakashi Sakamoto /*
305cd1d3f4STakashi Sakamoto  * See Table 5.7 – Sampling frequency for Multi-bit Audio
315cd1d3f4STakashi Sakamoto  * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
325cd1d3f4STakashi Sakamoto  */
335cd1d3f4STakashi Sakamoto static const unsigned int avc_stream_rate_table[] = {
345cd1d3f4STakashi Sakamoto 	[0] = 0x02,
355cd1d3f4STakashi Sakamoto 	[1] = 0x03,
365cd1d3f4STakashi Sakamoto 	[2] = 0x04,
375cd1d3f4STakashi Sakamoto 	[3] = 0x0a,
385cd1d3f4STakashi Sakamoto 	[4] = 0x05,
395cd1d3f4STakashi Sakamoto 	[5] = 0x07,
405cd1d3f4STakashi Sakamoto };
415cd1d3f4STakashi Sakamoto 
42b0ac0009STakashi Sakamoto static int set_rate(struct snd_oxfw *oxfw, unsigned int rate)
43b0ac0009STakashi Sakamoto {
44b0ac0009STakashi Sakamoto 	int err;
45b0ac0009STakashi Sakamoto 
46b0ac0009STakashi Sakamoto 	err = avc_general_set_sig_fmt(oxfw->unit, rate,
47b0ac0009STakashi Sakamoto 				      AVC_GENERAL_PLUG_DIR_IN, 0);
48b0ac0009STakashi Sakamoto 	if (err < 0)
49b0ac0009STakashi Sakamoto 		goto end;
50b0ac0009STakashi Sakamoto 
51b0ac0009STakashi Sakamoto 	if (oxfw->has_output)
52b0ac0009STakashi Sakamoto 		err = avc_general_set_sig_fmt(oxfw->unit, rate,
53b0ac0009STakashi Sakamoto 					      AVC_GENERAL_PLUG_DIR_OUT, 0);
54b0ac0009STakashi Sakamoto end:
55b0ac0009STakashi Sakamoto 	return err;
56b0ac0009STakashi Sakamoto }
57b0ac0009STakashi Sakamoto 
58f3699e2cSTakashi Sakamoto static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s,
59f3699e2cSTakashi Sakamoto 			     unsigned int rate, unsigned int pcm_channels)
60f3699e2cSTakashi Sakamoto {
61f3699e2cSTakashi Sakamoto 	u8 **formats;
62f3699e2cSTakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
63f3699e2cSTakashi Sakamoto 	enum avc_general_plug_dir dir;
645580ba7bSDan Carpenter 	unsigned int len;
655580ba7bSDan Carpenter 	int i, err;
66f3699e2cSTakashi Sakamoto 
67b0ac0009STakashi Sakamoto 	if (s == &oxfw->tx_stream) {
68b0ac0009STakashi Sakamoto 		formats = oxfw->tx_stream_formats;
69b0ac0009STakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_OUT;
70b0ac0009STakashi Sakamoto 	} else {
71f3699e2cSTakashi Sakamoto 		formats = oxfw->rx_stream_formats;
72f3699e2cSTakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_IN;
73b0ac0009STakashi Sakamoto 	}
74f3699e2cSTakashi Sakamoto 
75f3699e2cSTakashi Sakamoto 	/* Seek stream format for requirements. */
76f3699e2cSTakashi Sakamoto 	for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
77f3699e2cSTakashi Sakamoto 		err = snd_oxfw_stream_parse_format(formats[i], &formation);
78f3699e2cSTakashi Sakamoto 		if (err < 0)
79f3699e2cSTakashi Sakamoto 			return err;
80f3699e2cSTakashi Sakamoto 
81f3699e2cSTakashi Sakamoto 		if ((formation.rate == rate) && (formation.pcm == pcm_channels))
82f3699e2cSTakashi Sakamoto 			break;
83f3699e2cSTakashi Sakamoto 	}
84f3699e2cSTakashi Sakamoto 	if (i == SND_OXFW_STREAM_FORMAT_ENTRIES)
85f3699e2cSTakashi Sakamoto 		return -EINVAL;
86f3699e2cSTakashi Sakamoto 
87f3699e2cSTakashi Sakamoto 	/* If assumed, just change rate. */
88f3699e2cSTakashi Sakamoto 	if (oxfw->assumed)
89b0ac0009STakashi Sakamoto 		return set_rate(oxfw, rate);
90f3699e2cSTakashi Sakamoto 
91f3699e2cSTakashi Sakamoto 	/* Calculate format length. */
92f3699e2cSTakashi Sakamoto 	len = 5 + formats[i][4] * 2;
93f3699e2cSTakashi Sakamoto 
94f3699e2cSTakashi Sakamoto 	err = avc_stream_set_format(oxfw->unit, dir, 0, formats[i], len);
95f3699e2cSTakashi Sakamoto 	if (err < 0)
96f3699e2cSTakashi Sakamoto 		return err;
97f3699e2cSTakashi Sakamoto 
98f3699e2cSTakashi Sakamoto 	/* Some requests just after changing format causes freezing. */
99f3699e2cSTakashi Sakamoto 	msleep(100);
100f3699e2cSTakashi Sakamoto 
101f3699e2cSTakashi Sakamoto 	return 0;
102f3699e2cSTakashi Sakamoto }
103f3699e2cSTakashi Sakamoto 
104b0ac0009STakashi Sakamoto static void stop_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
105e2786ca6STakashi Sakamoto {
106b0ac0009STakashi Sakamoto 	amdtp_stream_pcm_abort(stream);
107b0ac0009STakashi Sakamoto 	amdtp_stream_stop(stream);
108e2786ca6STakashi Sakamoto 
109b0ac0009STakashi Sakamoto 	if (stream == &oxfw->tx_stream)
110b0ac0009STakashi Sakamoto 		cmp_connection_break(&oxfw->out_conn);
111b0ac0009STakashi Sakamoto 	else
112e2786ca6STakashi Sakamoto 		cmp_connection_break(&oxfw->in_conn);
113e2786ca6STakashi Sakamoto }
114e2786ca6STakashi Sakamoto 
115b0ac0009STakashi Sakamoto static int start_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream,
116b0ac0009STakashi Sakamoto 			unsigned int rate, unsigned int pcm_channels)
117e2786ca6STakashi Sakamoto {
118f3699e2cSTakashi Sakamoto 	u8 **formats;
119f3699e2cSTakashi Sakamoto 	struct cmp_connection *conn;
120f3699e2cSTakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
121f3699e2cSTakashi Sakamoto 	unsigned int i, midi_ports;
122f3699e2cSTakashi Sakamoto 	int err;
123f3699e2cSTakashi Sakamoto 
124b0ac0009STakashi Sakamoto 	if (stream == &oxfw->rx_stream) {
125f3699e2cSTakashi Sakamoto 		formats = oxfw->rx_stream_formats;
126f3699e2cSTakashi Sakamoto 		conn = &oxfw->in_conn;
127b0ac0009STakashi Sakamoto 	} else {
128b0ac0009STakashi Sakamoto 		formats = oxfw->tx_stream_formats;
129b0ac0009STakashi Sakamoto 		conn = &oxfw->out_conn;
130b0ac0009STakashi Sakamoto 	}
131f3699e2cSTakashi Sakamoto 
132b0ac0009STakashi Sakamoto 	/* Get stream format */
133f3699e2cSTakashi Sakamoto 	for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
134f3699e2cSTakashi Sakamoto 		if (formats[i] == NULL)
135f3699e2cSTakashi Sakamoto 			break;
136f3699e2cSTakashi Sakamoto 
137f3699e2cSTakashi Sakamoto 		err = snd_oxfw_stream_parse_format(formats[i], &formation);
138f3699e2cSTakashi Sakamoto 		if (err < 0)
139f3699e2cSTakashi Sakamoto 			goto end;
140f3699e2cSTakashi Sakamoto 		if (rate != formation.rate)
141f3699e2cSTakashi Sakamoto 			continue;
142f3699e2cSTakashi Sakamoto 		if (pcm_channels == 0 ||  pcm_channels == formation.pcm)
143f3699e2cSTakashi Sakamoto 			break;
144f3699e2cSTakashi Sakamoto 	}
145f3699e2cSTakashi Sakamoto 	if (i == SND_OXFW_STREAM_FORMAT_ENTRIES) {
146f3699e2cSTakashi Sakamoto 		err = -EINVAL;
147f3699e2cSTakashi Sakamoto 		goto end;
148f3699e2cSTakashi Sakamoto 	}
149f3699e2cSTakashi Sakamoto 
150f3699e2cSTakashi Sakamoto 	pcm_channels = formation.pcm;
151f3699e2cSTakashi Sakamoto 	midi_ports = DIV_ROUND_UP(formation.midi, 8);
152f3699e2cSTakashi Sakamoto 
153f3699e2cSTakashi Sakamoto 	/* The stream should have one pcm channels at least */
154f3699e2cSTakashi Sakamoto 	if (pcm_channels == 0) {
155f3699e2cSTakashi Sakamoto 		err = -EINVAL;
156f3699e2cSTakashi Sakamoto 		goto end;
157f3699e2cSTakashi Sakamoto 	}
15851c29fd2STakashi Sakamoto 	err = amdtp_am824_set_parameters(stream, rate, pcm_channels, midi_ports,
15951c29fd2STakashi Sakamoto 					 false);
160547e631cSTakashi Sakamoto 	if (err < 0)
161547e631cSTakashi Sakamoto 		goto end;
162f3699e2cSTakashi Sakamoto 
163f3699e2cSTakashi Sakamoto 	err = cmp_connection_establish(conn,
164f3699e2cSTakashi Sakamoto 				       amdtp_stream_get_max_payload(stream));
165f3699e2cSTakashi Sakamoto 	if (err < 0)
166f3699e2cSTakashi Sakamoto 		goto end;
167f3699e2cSTakashi Sakamoto 
168f3699e2cSTakashi Sakamoto 	err = amdtp_stream_start(stream,
169f3699e2cSTakashi Sakamoto 				 conn->resources.channel,
170f3699e2cSTakashi Sakamoto 				 conn->speed);
171f3699e2cSTakashi Sakamoto 	if (err < 0) {
172f3699e2cSTakashi Sakamoto 		cmp_connection_break(conn);
173f3699e2cSTakashi Sakamoto 		goto end;
174f3699e2cSTakashi Sakamoto 	}
175f3699e2cSTakashi Sakamoto 
176f3699e2cSTakashi Sakamoto 	/* Wait first packet */
177f2b14c0bSTakashi Sakamoto 	if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) {
178b0ac0009STakashi Sakamoto 		stop_stream(oxfw, stream);
179f2b14c0bSTakashi Sakamoto 		err = -ETIMEDOUT;
180f2b14c0bSTakashi Sakamoto 	}
181f3699e2cSTakashi Sakamoto end:
182f3699e2cSTakashi Sakamoto 	return err;
183f3699e2cSTakashi Sakamoto }
184f3699e2cSTakashi Sakamoto 
185b0ac0009STakashi Sakamoto static int check_connection_used_by_others(struct snd_oxfw *oxfw,
186b0ac0009STakashi Sakamoto 					   struct amdtp_stream *stream)
187f3699e2cSTakashi Sakamoto {
188b0ac0009STakashi Sakamoto 	struct cmp_connection *conn;
189b0ac0009STakashi Sakamoto 	bool used;
190b0ac0009STakashi Sakamoto 	int err;
191b0ac0009STakashi Sakamoto 
192b0ac0009STakashi Sakamoto 	if (stream == &oxfw->tx_stream)
193b0ac0009STakashi Sakamoto 		conn = &oxfw->out_conn;
194b0ac0009STakashi Sakamoto 	else
195b0ac0009STakashi Sakamoto 		conn = &oxfw->in_conn;
196b0ac0009STakashi Sakamoto 
197b0ac0009STakashi Sakamoto 	err = cmp_connection_check_used(conn, &used);
198b0ac0009STakashi Sakamoto 	if ((err >= 0) && used && !amdtp_stream_running(stream)) {
199b0ac0009STakashi Sakamoto 		dev_err(&oxfw->unit->device,
200b0ac0009STakashi Sakamoto 			"Connection established by others: %cPCR[%d]\n",
201b0ac0009STakashi Sakamoto 			(conn->direction == CMP_OUTPUT) ? 'o' : 'i',
202b0ac0009STakashi Sakamoto 			conn->pcr_index);
203b0ac0009STakashi Sakamoto 		err = -EBUSY;
204b0ac0009STakashi Sakamoto 	}
205b0ac0009STakashi Sakamoto 
206b0ac0009STakashi Sakamoto 	return err;
207b0ac0009STakashi Sakamoto }
208b0ac0009STakashi Sakamoto 
209b0ac0009STakashi Sakamoto int snd_oxfw_stream_init_simplex(struct snd_oxfw *oxfw,
210b0ac0009STakashi Sakamoto 				 struct amdtp_stream *stream)
211b0ac0009STakashi Sakamoto {
212b0ac0009STakashi Sakamoto 	struct cmp_connection *conn;
213b0ac0009STakashi Sakamoto 	enum cmp_direction c_dir;
214b0ac0009STakashi Sakamoto 	enum amdtp_stream_direction s_dir;
215b0ac0009STakashi Sakamoto 	int err;
216b0ac0009STakashi Sakamoto 
217b0ac0009STakashi Sakamoto 	if (stream == &oxfw->tx_stream) {
218b0ac0009STakashi Sakamoto 		conn = &oxfw->out_conn;
219b0ac0009STakashi Sakamoto 		c_dir = CMP_OUTPUT;
220b0ac0009STakashi Sakamoto 		s_dir = AMDTP_IN_STREAM;
221b0ac0009STakashi Sakamoto 	} else {
222b0ac0009STakashi Sakamoto 		conn = &oxfw->in_conn;
223b0ac0009STakashi Sakamoto 		c_dir = CMP_INPUT;
224b0ac0009STakashi Sakamoto 		s_dir = AMDTP_OUT_STREAM;
225b0ac0009STakashi Sakamoto 	}
226b0ac0009STakashi Sakamoto 
227b0ac0009STakashi Sakamoto 	err = cmp_connection_init(conn, oxfw->unit, c_dir, 0);
228b0ac0009STakashi Sakamoto 	if (err < 0)
229b0ac0009STakashi Sakamoto 		goto end;
230b0ac0009STakashi Sakamoto 
2315955815eSTakashi Sakamoto 	err = amdtp_am824_init(stream, oxfw->unit, s_dir, CIP_NONBLOCKING);
232b0ac0009STakashi Sakamoto 	if (err < 0) {
233b0ac0009STakashi Sakamoto 		amdtp_stream_destroy(stream);
234b0ac0009STakashi Sakamoto 		cmp_connection_destroy(conn);
235b0ac0009STakashi Sakamoto 		goto end;
236b0ac0009STakashi Sakamoto 	}
237b0ac0009STakashi Sakamoto 
238a2064710STakashi Sakamoto 	/*
239a2064710STakashi Sakamoto 	 * OXFW starts to transmit packets with non-zero dbc.
240a2064710STakashi Sakamoto 	 * OXFW postpone transferring packets till handling any asynchronous
241a2064710STakashi Sakamoto 	 * packets. As a result, next isochronous packet includes more data
242a2064710STakashi Sakamoto 	 * blocks than IEC 61883-6 defines.
243a2064710STakashi Sakamoto 	 */
24413f3a46dSTakashi Sakamoto 	if (stream == &oxfw->tx_stream) {
245a2064710STakashi Sakamoto 		oxfw->tx_stream.flags |= CIP_SKIP_INIT_DBC_CHECK |
246a2064710STakashi Sakamoto 					 CIP_JUMBO_PAYLOAD;
24713f3a46dSTakashi Sakamoto 		if (oxfw->wrong_dbs)
24813f3a46dSTakashi Sakamoto 			oxfw->tx_stream.flags |= CIP_WRONG_DBS;
24913f3a46dSTakashi Sakamoto 	}
250b0ac0009STakashi Sakamoto end:
251b0ac0009STakashi Sakamoto 	return err;
252b0ac0009STakashi Sakamoto }
253b0ac0009STakashi Sakamoto 
254b0ac0009STakashi Sakamoto int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw,
255b0ac0009STakashi Sakamoto 				  struct amdtp_stream *stream,
256b0ac0009STakashi Sakamoto 				  unsigned int rate, unsigned int pcm_channels)
257b0ac0009STakashi Sakamoto {
258b0ac0009STakashi Sakamoto 	struct amdtp_stream *opposite;
259f3699e2cSTakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
260b0ac0009STakashi Sakamoto 	enum avc_general_plug_dir dir;
261b0ac0009STakashi Sakamoto 	unsigned int substreams, opposite_substreams;
262e2786ca6STakashi Sakamoto 	int err = 0;
263e2786ca6STakashi Sakamoto 
264b0ac0009STakashi Sakamoto 	if (stream == &oxfw->tx_stream) {
265b0ac0009STakashi Sakamoto 		substreams = oxfw->capture_substreams;
266b0ac0009STakashi Sakamoto 		opposite = &oxfw->rx_stream;
267b0ac0009STakashi Sakamoto 		opposite_substreams = oxfw->playback_substreams;
268b0ac0009STakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_OUT;
269b0ac0009STakashi Sakamoto 	} else {
270b0ac0009STakashi Sakamoto 		substreams = oxfw->playback_substreams;
271b0ac0009STakashi Sakamoto 		opposite_substreams = oxfw->capture_substreams;
272e2786ca6STakashi Sakamoto 
273b0ac0009STakashi Sakamoto 		if (oxfw->has_output)
274b0ac0009STakashi Sakamoto 			opposite = &oxfw->rx_stream;
275b0ac0009STakashi Sakamoto 		else
276b0ac0009STakashi Sakamoto 			opposite = NULL;
277b0ac0009STakashi Sakamoto 
278b0ac0009STakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_IN;
279b0ac0009STakashi Sakamoto 	}
280b0ac0009STakashi Sakamoto 
281b0ac0009STakashi Sakamoto 	if (substreams == 0)
282b0ac0009STakashi Sakamoto 		goto end;
283b0ac0009STakashi Sakamoto 
284b0ac0009STakashi Sakamoto 	/*
285b0ac0009STakashi Sakamoto 	 * Considering JACK/FFADO streaming:
286b0ac0009STakashi Sakamoto 	 * TODO: This can be removed hwdep functionality becomes popular.
287b0ac0009STakashi Sakamoto 	 */
288b0ac0009STakashi Sakamoto 	err = check_connection_used_by_others(oxfw, stream);
289b0ac0009STakashi Sakamoto 	if (err < 0)
290b0ac0009STakashi Sakamoto 		goto end;
291b0ac0009STakashi Sakamoto 
292b0ac0009STakashi Sakamoto 	/* packet queueing error */
293b0ac0009STakashi Sakamoto 	if (amdtp_streaming_error(stream))
294b0ac0009STakashi Sakamoto 		stop_stream(oxfw, stream);
295b0ac0009STakashi Sakamoto 
296b0ac0009STakashi Sakamoto 	err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
297e2786ca6STakashi Sakamoto 	if (err < 0)
298e2786ca6STakashi Sakamoto 		goto end;
29905588d34STakashi Sakamoto 	if (rate == 0)
30005588d34STakashi Sakamoto 		rate = formation.rate;
30105588d34STakashi Sakamoto 	if (pcm_channels == 0)
30205588d34STakashi Sakamoto 		pcm_channels = formation.pcm;
303e2786ca6STakashi Sakamoto 
304f3699e2cSTakashi Sakamoto 	if ((formation.rate != rate) || (formation.pcm != pcm_channels)) {
305b0ac0009STakashi Sakamoto 		if (opposite != NULL) {
306b0ac0009STakashi Sakamoto 			err = check_connection_used_by_others(oxfw, opposite);
307b0ac0009STakashi Sakamoto 			if (err < 0)
308b0ac0009STakashi Sakamoto 				goto end;
309b0ac0009STakashi Sakamoto 			stop_stream(oxfw, opposite);
310b0ac0009STakashi Sakamoto 		}
311b0ac0009STakashi Sakamoto 		stop_stream(oxfw, stream);
312f3699e2cSTakashi Sakamoto 
313b0ac0009STakashi Sakamoto 		err = set_stream_format(oxfw, stream, rate, pcm_channels);
314f3699e2cSTakashi Sakamoto 		if (err < 0) {
315f3699e2cSTakashi Sakamoto 			dev_err(&oxfw->unit->device,
316f3699e2cSTakashi Sakamoto 				"fail to set stream format: %d\n", err);
317f3699e2cSTakashi Sakamoto 			goto end;
318f3699e2cSTakashi Sakamoto 		}
319b0ac0009STakashi Sakamoto 
320b0ac0009STakashi Sakamoto 		/* Start opposite stream if needed. */
321b0ac0009STakashi Sakamoto 		if (opposite && !amdtp_stream_running(opposite) &&
322b0ac0009STakashi Sakamoto 		    (opposite_substreams > 0)) {
323b0ac0009STakashi Sakamoto 			err = start_stream(oxfw, opposite, rate, 0);
324b0ac0009STakashi Sakamoto 			if (err < 0) {
325b0ac0009STakashi Sakamoto 				dev_err(&oxfw->unit->device,
326b0ac0009STakashi Sakamoto 					"fail to restart stream: %d\n", err);
327b0ac0009STakashi Sakamoto 				goto end;
328b0ac0009STakashi Sakamoto 			}
329b0ac0009STakashi Sakamoto 		}
330f3699e2cSTakashi Sakamoto 	}
331f3699e2cSTakashi Sakamoto 
332b0ac0009STakashi Sakamoto 	/* Start requested stream. */
333b0ac0009STakashi Sakamoto 	if (!amdtp_stream_running(stream)) {
334b0ac0009STakashi Sakamoto 		err = start_stream(oxfw, stream, rate, pcm_channels);
335f3699e2cSTakashi Sakamoto 		if (err < 0)
336f3699e2cSTakashi Sakamoto 			dev_err(&oxfw->unit->device,
337f3699e2cSTakashi Sakamoto 				"fail to start stream: %d\n", err);
338b0ac0009STakashi Sakamoto 	}
339e2786ca6STakashi Sakamoto end:
340e2786ca6STakashi Sakamoto 	return err;
341e2786ca6STakashi Sakamoto }
342e2786ca6STakashi Sakamoto 
343b0ac0009STakashi Sakamoto void snd_oxfw_stream_stop_simplex(struct snd_oxfw *oxfw,
344b0ac0009STakashi Sakamoto 				  struct amdtp_stream *stream)
345e2786ca6STakashi Sakamoto {
346b0ac0009STakashi Sakamoto 	if (((stream == &oxfw->tx_stream) && (oxfw->capture_substreams > 0)) ||
347b0ac0009STakashi Sakamoto 	    ((stream == &oxfw->rx_stream) && (oxfw->playback_substreams > 0)))
348b0ac0009STakashi Sakamoto 		return;
349b0ac0009STakashi Sakamoto 
350b0ac0009STakashi Sakamoto 	stop_stream(oxfw, stream);
351e2786ca6STakashi Sakamoto }
352e2786ca6STakashi Sakamoto 
353d23c2cc4STakashi Sakamoto /*
354d23c2cc4STakashi Sakamoto  * This function should be called before starting the stream or after stopping
355d23c2cc4STakashi Sakamoto  * the streams.
356d23c2cc4STakashi Sakamoto  */
357b0ac0009STakashi Sakamoto void snd_oxfw_stream_destroy_simplex(struct snd_oxfw *oxfw,
358b0ac0009STakashi Sakamoto 				     struct amdtp_stream *stream)
359e2786ca6STakashi Sakamoto {
360b0ac0009STakashi Sakamoto 	struct cmp_connection *conn;
361e2786ca6STakashi Sakamoto 
362b0ac0009STakashi Sakamoto 	if (stream == &oxfw->tx_stream)
363b0ac0009STakashi Sakamoto 		conn = &oxfw->out_conn;
364e2786ca6STakashi Sakamoto 	else
365b0ac0009STakashi Sakamoto 		conn = &oxfw->in_conn;
366b0ac0009STakashi Sakamoto 
367b0ac0009STakashi Sakamoto 	amdtp_stream_destroy(stream);
368b0ac0009STakashi Sakamoto 	cmp_connection_destroy(conn);
369b0ac0009STakashi Sakamoto }
370b0ac0009STakashi Sakamoto 
371b0ac0009STakashi Sakamoto void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw,
372b0ac0009STakashi Sakamoto 				    struct amdtp_stream *stream)
373b0ac0009STakashi Sakamoto {
374b0ac0009STakashi Sakamoto 	struct cmp_connection *conn;
375b0ac0009STakashi Sakamoto 
376b0ac0009STakashi Sakamoto 	if (stream == &oxfw->tx_stream)
377b0ac0009STakashi Sakamoto 		conn = &oxfw->out_conn;
378b0ac0009STakashi Sakamoto 	else
379b0ac0009STakashi Sakamoto 		conn = &oxfw->in_conn;
380b0ac0009STakashi Sakamoto 
381b0ac0009STakashi Sakamoto 	if (cmp_connection_update(conn) < 0)
382b0ac0009STakashi Sakamoto 		stop_stream(oxfw, stream);
383b0ac0009STakashi Sakamoto 	else
384b0ac0009STakashi Sakamoto 		amdtp_stream_update(stream);
385e2786ca6STakashi Sakamoto }
3865cd1d3f4STakashi Sakamoto 
3873c96101fSTakashi Sakamoto int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
3883c96101fSTakashi Sakamoto 				enum avc_general_plug_dir dir,
3893c96101fSTakashi Sakamoto 				struct snd_oxfw_stream_formation *formation)
3903c96101fSTakashi Sakamoto {
3913c96101fSTakashi Sakamoto 	u8 *format;
3923c96101fSTakashi Sakamoto 	unsigned int len;
3933c96101fSTakashi Sakamoto 	int err;
3943c96101fSTakashi Sakamoto 
3953c96101fSTakashi Sakamoto 	len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
3963c96101fSTakashi Sakamoto 	format = kmalloc(len, GFP_KERNEL);
3973c96101fSTakashi Sakamoto 	if (format == NULL)
3983c96101fSTakashi Sakamoto 		return -ENOMEM;
3993c96101fSTakashi Sakamoto 
4003c96101fSTakashi Sakamoto 	err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
4013c96101fSTakashi Sakamoto 	if (err < 0)
4023c96101fSTakashi Sakamoto 		goto end;
4033c96101fSTakashi Sakamoto 	if (len < 3) {
4043c96101fSTakashi Sakamoto 		err = -EIO;
4053c96101fSTakashi Sakamoto 		goto end;
4063c96101fSTakashi Sakamoto 	}
4073c96101fSTakashi Sakamoto 
4083c96101fSTakashi Sakamoto 	err = snd_oxfw_stream_parse_format(format, formation);
4093c96101fSTakashi Sakamoto end:
4103c96101fSTakashi Sakamoto 	kfree(format);
4113c96101fSTakashi Sakamoto 	return err;
4123c96101fSTakashi Sakamoto }
4133c96101fSTakashi Sakamoto 
4145cd1d3f4STakashi Sakamoto /*
4155cd1d3f4STakashi Sakamoto  * See Table 6.16 - AM824 Stream Format
4165cd1d3f4STakashi Sakamoto  *     Figure 6.19 - format_information field for AM824 Compound
4175cd1d3f4STakashi Sakamoto  * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
4185cd1d3f4STakashi Sakamoto  * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
4195cd1d3f4STakashi Sakamoto  */
4205cd1d3f4STakashi Sakamoto int snd_oxfw_stream_parse_format(u8 *format,
4215cd1d3f4STakashi Sakamoto 				 struct snd_oxfw_stream_formation *formation)
4225cd1d3f4STakashi Sakamoto {
4235cd1d3f4STakashi Sakamoto 	unsigned int i, e, channels, type;
4245cd1d3f4STakashi Sakamoto 
4255cd1d3f4STakashi Sakamoto 	memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
4265cd1d3f4STakashi Sakamoto 
4275cd1d3f4STakashi Sakamoto 	/*
4285cd1d3f4STakashi Sakamoto 	 * this module can support a hierarchy combination that:
4295cd1d3f4STakashi Sakamoto 	 *  Root:	Audio and Music (0x90)
4305cd1d3f4STakashi Sakamoto 	 *  Level 1:	AM824 Compound  (0x40)
4315cd1d3f4STakashi Sakamoto 	 */
4325cd1d3f4STakashi Sakamoto 	if ((format[0] != 0x90) || (format[1] != 0x40))
4335cd1d3f4STakashi Sakamoto 		return -ENOSYS;
4345cd1d3f4STakashi Sakamoto 
4355cd1d3f4STakashi Sakamoto 	/* check the sampling rate */
4365cd1d3f4STakashi Sakamoto 	for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
4375cd1d3f4STakashi Sakamoto 		if (format[2] == avc_stream_rate_table[i])
4385cd1d3f4STakashi Sakamoto 			break;
4395cd1d3f4STakashi Sakamoto 	}
4405cd1d3f4STakashi Sakamoto 	if (i == ARRAY_SIZE(avc_stream_rate_table))
4415cd1d3f4STakashi Sakamoto 		return -ENOSYS;
4425cd1d3f4STakashi Sakamoto 
4435cd1d3f4STakashi Sakamoto 	formation->rate = oxfw_rate_table[i];
4445cd1d3f4STakashi Sakamoto 
4455cd1d3f4STakashi Sakamoto 	for (e = 0; e < format[4]; e++) {
4465cd1d3f4STakashi Sakamoto 		channels = format[5 + e * 2];
4475cd1d3f4STakashi Sakamoto 		type = format[6 + e * 2];
4485cd1d3f4STakashi Sakamoto 
4495cd1d3f4STakashi Sakamoto 		switch (type) {
4505cd1d3f4STakashi Sakamoto 		/* IEC 60958 Conformant, currently handled as MBLA */
4515cd1d3f4STakashi Sakamoto 		case 0x00:
4525cd1d3f4STakashi Sakamoto 		/* Multi Bit Linear Audio (Raw) */
4535cd1d3f4STakashi Sakamoto 		case 0x06:
4545cd1d3f4STakashi Sakamoto 			formation->pcm += channels;
4555cd1d3f4STakashi Sakamoto 			break;
4565cd1d3f4STakashi Sakamoto 		/* MIDI Conformant */
4575cd1d3f4STakashi Sakamoto 		case 0x0d:
4585cd1d3f4STakashi Sakamoto 			formation->midi = channels;
4595cd1d3f4STakashi Sakamoto 			break;
4605cd1d3f4STakashi Sakamoto 		/* IEC 61937-3 to 7 */
4615cd1d3f4STakashi Sakamoto 		case 0x01:
4625cd1d3f4STakashi Sakamoto 		case 0x02:
4635cd1d3f4STakashi Sakamoto 		case 0x03:
4645cd1d3f4STakashi Sakamoto 		case 0x04:
4655cd1d3f4STakashi Sakamoto 		case 0x05:
4665cd1d3f4STakashi Sakamoto 		/* Multi Bit Linear Audio */
4675cd1d3f4STakashi Sakamoto 		case 0x07:	/* DVD-Audio */
4685cd1d3f4STakashi Sakamoto 		case 0x0c:	/* High Precision */
4695cd1d3f4STakashi Sakamoto 		/* One Bit Audio */
4705cd1d3f4STakashi Sakamoto 		case 0x08:	/* (Plain) Raw */
4715cd1d3f4STakashi Sakamoto 		case 0x09:	/* (Plain) SACD */
4725cd1d3f4STakashi Sakamoto 		case 0x0a:	/* (Encoded) Raw */
4735cd1d3f4STakashi Sakamoto 		case 0x0b:	/* (Encoded) SACD */
4745cd1d3f4STakashi Sakamoto 		/* SMPTE Time-Code conformant */
4755cd1d3f4STakashi Sakamoto 		case 0x0e:
4765cd1d3f4STakashi Sakamoto 		/* Sample Count */
4775cd1d3f4STakashi Sakamoto 		case 0x0f:
4785cd1d3f4STakashi Sakamoto 		/* Anciliary Data */
4795cd1d3f4STakashi Sakamoto 		case 0x10:
4805cd1d3f4STakashi Sakamoto 		/* Synchronization Stream (Stereo Raw audio) */
4815cd1d3f4STakashi Sakamoto 		case 0x40:
4825cd1d3f4STakashi Sakamoto 		/* Don't care */
4835cd1d3f4STakashi Sakamoto 		case 0xff:
4845cd1d3f4STakashi Sakamoto 		default:
4855cd1d3f4STakashi Sakamoto 			return -ENOSYS;	/* not supported */
4865cd1d3f4STakashi Sakamoto 		}
4875cd1d3f4STakashi Sakamoto 	}
4885cd1d3f4STakashi Sakamoto 
48949c7b3fcSTakashi Sakamoto 	if (formation->pcm  > AM824_MAX_CHANNELS_FOR_PCM ||
49049c7b3fcSTakashi Sakamoto 	    formation->midi > AM824_MAX_CHANNELS_FOR_MIDI)
4915cd1d3f4STakashi Sakamoto 		return -ENOSYS;
4925cd1d3f4STakashi Sakamoto 
4935cd1d3f4STakashi Sakamoto 	return 0;
4945cd1d3f4STakashi Sakamoto }
4955cd1d3f4STakashi Sakamoto 
4965cd1d3f4STakashi Sakamoto static int
4975cd1d3f4STakashi Sakamoto assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
4985cd1d3f4STakashi Sakamoto 		      unsigned int pid, u8 *buf, unsigned int *len,
4995cd1d3f4STakashi Sakamoto 		      u8 **formats)
5005cd1d3f4STakashi Sakamoto {
5015cd1d3f4STakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
5025cd1d3f4STakashi Sakamoto 	unsigned int i, eid;
5035cd1d3f4STakashi Sakamoto 	int err;
5045cd1d3f4STakashi Sakamoto 
5055cd1d3f4STakashi Sakamoto 	/* get format at current sampling rate */
5065cd1d3f4STakashi Sakamoto 	err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
5075cd1d3f4STakashi Sakamoto 	if (err < 0) {
5085cd1d3f4STakashi Sakamoto 		dev_err(&oxfw->unit->device,
5095cd1d3f4STakashi Sakamoto 		"fail to get current stream format for isoc %s plug %d:%d\n",
5105cd1d3f4STakashi Sakamoto 			(dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
5115cd1d3f4STakashi Sakamoto 			pid, err);
5125cd1d3f4STakashi Sakamoto 		goto end;
5135cd1d3f4STakashi Sakamoto 	}
5145cd1d3f4STakashi Sakamoto 
5155cd1d3f4STakashi Sakamoto 	/* parse and set stream format */
5165cd1d3f4STakashi Sakamoto 	eid = 0;
5175cd1d3f4STakashi Sakamoto 	err = snd_oxfw_stream_parse_format(buf, &formation);
5185cd1d3f4STakashi Sakamoto 	if (err < 0)
5195cd1d3f4STakashi Sakamoto 		goto end;
5205cd1d3f4STakashi Sakamoto 
52196bab82fSAndrzej Hajda 	formats[eid] = kmemdup(buf, *len, GFP_KERNEL);
5225cd1d3f4STakashi Sakamoto 	if (formats[eid] == NULL) {
5235cd1d3f4STakashi Sakamoto 		err = -ENOMEM;
5245cd1d3f4STakashi Sakamoto 		goto end;
5255cd1d3f4STakashi Sakamoto 	}
5265cd1d3f4STakashi Sakamoto 
5275cd1d3f4STakashi Sakamoto 	/* apply the format for each available sampling rate */
5285cd1d3f4STakashi Sakamoto 	for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
5295cd1d3f4STakashi Sakamoto 		if (formation.rate == oxfw_rate_table[i])
5305cd1d3f4STakashi Sakamoto 			continue;
5315cd1d3f4STakashi Sakamoto 
5325cd1d3f4STakashi Sakamoto 		err = avc_general_inquiry_sig_fmt(oxfw->unit,
5335cd1d3f4STakashi Sakamoto 						  oxfw_rate_table[i],
5345cd1d3f4STakashi Sakamoto 						  dir, pid);
5355cd1d3f4STakashi Sakamoto 		if (err < 0)
5365cd1d3f4STakashi Sakamoto 			continue;
5375cd1d3f4STakashi Sakamoto 
5385cd1d3f4STakashi Sakamoto 		eid++;
53996bab82fSAndrzej Hajda 		formats[eid] = kmemdup(buf, *len, GFP_KERNEL);
5405cd1d3f4STakashi Sakamoto 		if (formats[eid] == NULL) {
5415cd1d3f4STakashi Sakamoto 			err = -ENOMEM;
5425cd1d3f4STakashi Sakamoto 			goto end;
5435cd1d3f4STakashi Sakamoto 		}
5445cd1d3f4STakashi Sakamoto 		formats[eid][2] = avc_stream_rate_table[i];
5455cd1d3f4STakashi Sakamoto 	}
5465cd1d3f4STakashi Sakamoto 
5475cd1d3f4STakashi Sakamoto 	err = 0;
5485cd1d3f4STakashi Sakamoto 	oxfw->assumed = true;
5495cd1d3f4STakashi Sakamoto end:
5505cd1d3f4STakashi Sakamoto 	return err;
5515cd1d3f4STakashi Sakamoto }
5525cd1d3f4STakashi Sakamoto 
5535cd1d3f4STakashi Sakamoto static int fill_stream_formats(struct snd_oxfw *oxfw,
5545cd1d3f4STakashi Sakamoto 			       enum avc_general_plug_dir dir,
5555cd1d3f4STakashi Sakamoto 			       unsigned short pid)
5565cd1d3f4STakashi Sakamoto {
5575cd1d3f4STakashi Sakamoto 	u8 *buf, **formats;
5585cd1d3f4STakashi Sakamoto 	unsigned int len, eid = 0;
5595cd1d3f4STakashi Sakamoto 	struct snd_oxfw_stream_formation dummy;
5605cd1d3f4STakashi Sakamoto 	int err;
5615cd1d3f4STakashi Sakamoto 
5625cd1d3f4STakashi Sakamoto 	buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
5635cd1d3f4STakashi Sakamoto 	if (buf == NULL)
5645cd1d3f4STakashi Sakamoto 		return -ENOMEM;
5655cd1d3f4STakashi Sakamoto 
566b0ac0009STakashi Sakamoto 	if (dir == AVC_GENERAL_PLUG_DIR_OUT)
567b0ac0009STakashi Sakamoto 		formats = oxfw->tx_stream_formats;
568b0ac0009STakashi Sakamoto 	else
5695cd1d3f4STakashi Sakamoto 		formats = oxfw->rx_stream_formats;
5705cd1d3f4STakashi Sakamoto 
5715cd1d3f4STakashi Sakamoto 	/* get first entry */
5725cd1d3f4STakashi Sakamoto 	len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
5735cd1d3f4STakashi Sakamoto 	err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
5745cd1d3f4STakashi Sakamoto 	if (err == -ENOSYS) {
5755cd1d3f4STakashi Sakamoto 		/* LIST subfunction is not implemented */
5765cd1d3f4STakashi Sakamoto 		len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
5775cd1d3f4STakashi Sakamoto 		err = assume_stream_formats(oxfw, dir, pid, buf, &len,
5785cd1d3f4STakashi Sakamoto 					    formats);
5795cd1d3f4STakashi Sakamoto 		goto end;
5805cd1d3f4STakashi Sakamoto 	} else if (err < 0) {
5815cd1d3f4STakashi Sakamoto 		dev_err(&oxfw->unit->device,
5825cd1d3f4STakashi Sakamoto 			"fail to get stream format %d for isoc %s plug %d:%d\n",
5835cd1d3f4STakashi Sakamoto 			eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
5845cd1d3f4STakashi Sakamoto 			pid, err);
5855cd1d3f4STakashi Sakamoto 		goto end;
5865cd1d3f4STakashi Sakamoto 	}
5875cd1d3f4STakashi Sakamoto 
5885cd1d3f4STakashi Sakamoto 	/* LIST subfunction is implemented */
5895cd1d3f4STakashi Sakamoto 	while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
5905cd1d3f4STakashi Sakamoto 		/* The format is too short. */
5915cd1d3f4STakashi Sakamoto 		if (len < 3) {
5925cd1d3f4STakashi Sakamoto 			err = -EIO;
5935cd1d3f4STakashi Sakamoto 			break;
5945cd1d3f4STakashi Sakamoto 		}
5955cd1d3f4STakashi Sakamoto 
5965cd1d3f4STakashi Sakamoto 		/* parse and set stream format */
5975cd1d3f4STakashi Sakamoto 		err = snd_oxfw_stream_parse_format(buf, &dummy);
5985cd1d3f4STakashi Sakamoto 		if (err < 0)
5995cd1d3f4STakashi Sakamoto 			break;
6005cd1d3f4STakashi Sakamoto 
60196bab82fSAndrzej Hajda 		formats[eid] = kmemdup(buf, len, GFP_KERNEL);
6025cd1d3f4STakashi Sakamoto 		if (formats[eid] == NULL) {
6035cd1d3f4STakashi Sakamoto 			err = -ENOMEM;
6045cd1d3f4STakashi Sakamoto 			break;
6055cd1d3f4STakashi Sakamoto 		}
6065cd1d3f4STakashi Sakamoto 
6075cd1d3f4STakashi Sakamoto 		/* get next entry */
6085cd1d3f4STakashi Sakamoto 		len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
6095cd1d3f4STakashi Sakamoto 		err = avc_stream_get_format_list(oxfw->unit, dir, 0,
6105cd1d3f4STakashi Sakamoto 						 buf, &len, ++eid);
6115cd1d3f4STakashi Sakamoto 		/* No entries remained. */
6125cd1d3f4STakashi Sakamoto 		if (err == -EINVAL) {
6135cd1d3f4STakashi Sakamoto 			err = 0;
6145cd1d3f4STakashi Sakamoto 			break;
6155cd1d3f4STakashi Sakamoto 		} else if (err < 0) {
6165cd1d3f4STakashi Sakamoto 			dev_err(&oxfw->unit->device,
6175cd1d3f4STakashi Sakamoto 			"fail to get stream format %d for isoc %s plug %d:%d\n",
6185cd1d3f4STakashi Sakamoto 				eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
6195cd1d3f4STakashi Sakamoto 									"out",
6205cd1d3f4STakashi Sakamoto 				pid, err);
6215cd1d3f4STakashi Sakamoto 			break;
6225cd1d3f4STakashi Sakamoto 		}
6235cd1d3f4STakashi Sakamoto 	}
6245cd1d3f4STakashi Sakamoto end:
6255cd1d3f4STakashi Sakamoto 	kfree(buf);
6265cd1d3f4STakashi Sakamoto 	return err;
6275cd1d3f4STakashi Sakamoto }
6285cd1d3f4STakashi Sakamoto 
6295cd1d3f4STakashi Sakamoto int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
6305cd1d3f4STakashi Sakamoto {
6315cd1d3f4STakashi Sakamoto 	u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
632*32056041STakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
633*32056041STakashi Sakamoto 	u8 *format;
634*32056041STakashi Sakamoto 	unsigned int i;
6355cd1d3f4STakashi Sakamoto 	int err;
6365cd1d3f4STakashi Sakamoto 
6375cd1d3f4STakashi Sakamoto 	/* the number of plugs for isoc in/out, ext in/out  */
6385cd1d3f4STakashi Sakamoto 	err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
6395cd1d3f4STakashi Sakamoto 	if (err < 0) {
6405cd1d3f4STakashi Sakamoto 		dev_err(&oxfw->unit->device,
6415cd1d3f4STakashi Sakamoto 		"fail to get info for isoc/external in/out plugs: %d\n",
6425cd1d3f4STakashi Sakamoto 			err);
6435cd1d3f4STakashi Sakamoto 		goto end;
644b0ac0009STakashi Sakamoto 	} else if ((plugs[0] == 0) && (plugs[1] == 0)) {
6455cd1d3f4STakashi Sakamoto 		err = -ENOSYS;
6465cd1d3f4STakashi Sakamoto 		goto end;
6475cd1d3f4STakashi Sakamoto 	}
6485cd1d3f4STakashi Sakamoto 
649b0ac0009STakashi Sakamoto 	/* use oPCR[0] if exists */
650b0ac0009STakashi Sakamoto 	if (plugs[1] > 0) {
651b0ac0009STakashi Sakamoto 		err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
652b0ac0009STakashi Sakamoto 		if (err < 0)
653b0ac0009STakashi Sakamoto 			goto end;
654*32056041STakashi Sakamoto 
655*32056041STakashi Sakamoto 		for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
656*32056041STakashi Sakamoto 			format = oxfw->tx_stream_formats[i];
657*32056041STakashi Sakamoto 			if (format == NULL)
658*32056041STakashi Sakamoto 				continue;
659*32056041STakashi Sakamoto 			err = snd_oxfw_stream_parse_format(format, &formation);
660*32056041STakashi Sakamoto 			if (err < 0)
661*32056041STakashi Sakamoto 				continue;
662*32056041STakashi Sakamoto 
663*32056041STakashi Sakamoto 			/* Add one MIDI port. */
664*32056041STakashi Sakamoto 			if (formation.midi > 0)
665*32056041STakashi Sakamoto 				oxfw->midi_input_ports = 1;
666*32056041STakashi Sakamoto 		}
667*32056041STakashi Sakamoto 
668b0ac0009STakashi Sakamoto 		oxfw->has_output = true;
669b0ac0009STakashi Sakamoto 	}
670b0ac0009STakashi Sakamoto 
6715cd1d3f4STakashi Sakamoto 	/* use iPCR[0] if exists */
672*32056041STakashi Sakamoto 	if (plugs[0] > 0) {
6735cd1d3f4STakashi Sakamoto 		err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
674*32056041STakashi Sakamoto 		if (err < 0)
675*32056041STakashi Sakamoto 			goto end;
676*32056041STakashi Sakamoto 
677*32056041STakashi Sakamoto 		for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
678*32056041STakashi Sakamoto 			format = oxfw->rx_stream_formats[i];
679*32056041STakashi Sakamoto 			if (format == NULL)
680*32056041STakashi Sakamoto 				continue;
681*32056041STakashi Sakamoto 			err = snd_oxfw_stream_parse_format(format, &formation);
682*32056041STakashi Sakamoto 			if (err < 0)
683*32056041STakashi Sakamoto 				continue;
684*32056041STakashi Sakamoto 
685*32056041STakashi Sakamoto 			/* Add one MIDI port. */
686*32056041STakashi Sakamoto 			if (formation.midi > 0)
687*32056041STakashi Sakamoto 				oxfw->midi_output_ports = 1;
688*32056041STakashi Sakamoto 		}
689*32056041STakashi Sakamoto 	}
6905cd1d3f4STakashi Sakamoto end:
6915cd1d3f4STakashi Sakamoto 	return err;
6925cd1d3f4STakashi Sakamoto }
6938985f4acSTakashi Sakamoto 
6948985f4acSTakashi Sakamoto void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
6958985f4acSTakashi Sakamoto {
6968985f4acSTakashi Sakamoto 	oxfw->dev_lock_changed = true;
6978985f4acSTakashi Sakamoto 	wake_up(&oxfw->hwdep_wait);
6988985f4acSTakashi Sakamoto }
6998985f4acSTakashi Sakamoto 
7008985f4acSTakashi Sakamoto int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
7018985f4acSTakashi Sakamoto {
7028985f4acSTakashi Sakamoto 	int err;
7038985f4acSTakashi Sakamoto 
7048985f4acSTakashi Sakamoto 	spin_lock_irq(&oxfw->lock);
7058985f4acSTakashi Sakamoto 
7068985f4acSTakashi Sakamoto 	/* user land lock this */
7078985f4acSTakashi Sakamoto 	if (oxfw->dev_lock_count < 0) {
7088985f4acSTakashi Sakamoto 		err = -EBUSY;
7098985f4acSTakashi Sakamoto 		goto end;
7108985f4acSTakashi Sakamoto 	}
7118985f4acSTakashi Sakamoto 
7128985f4acSTakashi Sakamoto 	/* this is the first time */
7138985f4acSTakashi Sakamoto 	if (oxfw->dev_lock_count++ == 0)
7148985f4acSTakashi Sakamoto 		snd_oxfw_stream_lock_changed(oxfw);
7158985f4acSTakashi Sakamoto 	err = 0;
7168985f4acSTakashi Sakamoto end:
7178985f4acSTakashi Sakamoto 	spin_unlock_irq(&oxfw->lock);
7188985f4acSTakashi Sakamoto 	return err;
7198985f4acSTakashi Sakamoto }
7208985f4acSTakashi Sakamoto 
7218985f4acSTakashi Sakamoto void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
7228985f4acSTakashi Sakamoto {
7238985f4acSTakashi Sakamoto 	spin_lock_irq(&oxfw->lock);
7248985f4acSTakashi Sakamoto 
7258985f4acSTakashi Sakamoto 	if (WARN_ON(oxfw->dev_lock_count <= 0))
7268985f4acSTakashi Sakamoto 		goto end;
7278985f4acSTakashi Sakamoto 	if (--oxfw->dev_lock_count == 0)
7288985f4acSTakashi Sakamoto 		snd_oxfw_stream_lock_changed(oxfw);
7298985f4acSTakashi Sakamoto end:
7308985f4acSTakashi Sakamoto 	spin_unlock_irq(&oxfw->lock);
7318985f4acSTakashi Sakamoto }
732