xref: /linux/sound/firewire/oxfw/oxfw-stream.c (revision 51c29fd21389d9995a8a18a91eeb8dd1220a2119)
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 	}
158*51c29fd2STakashi Sakamoto 	err = amdtp_am824_set_parameters(stream, rate, pcm_channels, midi_ports,
159*51c29fd2STakashi 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 	 */
244b0ac0009STakashi Sakamoto 	if (stream == &oxfw->tx_stream)
245a2064710STakashi Sakamoto 		oxfw->tx_stream.flags |= CIP_SKIP_INIT_DBC_CHECK |
246a2064710STakashi Sakamoto 					 CIP_JUMBO_PAYLOAD;
247b0ac0009STakashi Sakamoto end:
248b0ac0009STakashi Sakamoto 	return err;
249b0ac0009STakashi Sakamoto }
250b0ac0009STakashi Sakamoto 
251b0ac0009STakashi Sakamoto int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw,
252b0ac0009STakashi Sakamoto 				  struct amdtp_stream *stream,
253b0ac0009STakashi Sakamoto 				  unsigned int rate, unsigned int pcm_channels)
254b0ac0009STakashi Sakamoto {
255b0ac0009STakashi Sakamoto 	struct amdtp_stream *opposite;
256f3699e2cSTakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
257b0ac0009STakashi Sakamoto 	enum avc_general_plug_dir dir;
258b0ac0009STakashi Sakamoto 	unsigned int substreams, opposite_substreams;
259e2786ca6STakashi Sakamoto 	int err = 0;
260e2786ca6STakashi Sakamoto 
261b0ac0009STakashi Sakamoto 	if (stream == &oxfw->tx_stream) {
262b0ac0009STakashi Sakamoto 		substreams = oxfw->capture_substreams;
263b0ac0009STakashi Sakamoto 		opposite = &oxfw->rx_stream;
264b0ac0009STakashi Sakamoto 		opposite_substreams = oxfw->playback_substreams;
265b0ac0009STakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_OUT;
266b0ac0009STakashi Sakamoto 	} else {
267b0ac0009STakashi Sakamoto 		substreams = oxfw->playback_substreams;
268b0ac0009STakashi Sakamoto 		opposite_substreams = oxfw->capture_substreams;
269e2786ca6STakashi Sakamoto 
270b0ac0009STakashi Sakamoto 		if (oxfw->has_output)
271b0ac0009STakashi Sakamoto 			opposite = &oxfw->rx_stream;
272b0ac0009STakashi Sakamoto 		else
273b0ac0009STakashi Sakamoto 			opposite = NULL;
274b0ac0009STakashi Sakamoto 
275b0ac0009STakashi Sakamoto 		dir = AVC_GENERAL_PLUG_DIR_IN;
276b0ac0009STakashi Sakamoto 	}
277b0ac0009STakashi Sakamoto 
278b0ac0009STakashi Sakamoto 	if (substreams == 0)
279b0ac0009STakashi Sakamoto 		goto end;
280b0ac0009STakashi Sakamoto 
281b0ac0009STakashi Sakamoto 	/*
282b0ac0009STakashi Sakamoto 	 * Considering JACK/FFADO streaming:
283b0ac0009STakashi Sakamoto 	 * TODO: This can be removed hwdep functionality becomes popular.
284b0ac0009STakashi Sakamoto 	 */
285b0ac0009STakashi Sakamoto 	err = check_connection_used_by_others(oxfw, stream);
286b0ac0009STakashi Sakamoto 	if (err < 0)
287b0ac0009STakashi Sakamoto 		goto end;
288b0ac0009STakashi Sakamoto 
289b0ac0009STakashi Sakamoto 	/* packet queueing error */
290b0ac0009STakashi Sakamoto 	if (amdtp_streaming_error(stream))
291b0ac0009STakashi Sakamoto 		stop_stream(oxfw, stream);
292b0ac0009STakashi Sakamoto 
293b0ac0009STakashi Sakamoto 	err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
294e2786ca6STakashi Sakamoto 	if (err < 0)
295e2786ca6STakashi Sakamoto 		goto end;
29605588d34STakashi Sakamoto 	if (rate == 0)
29705588d34STakashi Sakamoto 		rate = formation.rate;
29805588d34STakashi Sakamoto 	if (pcm_channels == 0)
29905588d34STakashi Sakamoto 		pcm_channels = formation.pcm;
300e2786ca6STakashi Sakamoto 
301f3699e2cSTakashi Sakamoto 	if ((formation.rate != rate) || (formation.pcm != pcm_channels)) {
302b0ac0009STakashi Sakamoto 		if (opposite != NULL) {
303b0ac0009STakashi Sakamoto 			err = check_connection_used_by_others(oxfw, opposite);
304b0ac0009STakashi Sakamoto 			if (err < 0)
305b0ac0009STakashi Sakamoto 				goto end;
306b0ac0009STakashi Sakamoto 			stop_stream(oxfw, opposite);
307b0ac0009STakashi Sakamoto 		}
308b0ac0009STakashi Sakamoto 		stop_stream(oxfw, stream);
309f3699e2cSTakashi Sakamoto 
310b0ac0009STakashi Sakamoto 		err = set_stream_format(oxfw, stream, rate, pcm_channels);
311f3699e2cSTakashi Sakamoto 		if (err < 0) {
312f3699e2cSTakashi Sakamoto 			dev_err(&oxfw->unit->device,
313f3699e2cSTakashi Sakamoto 				"fail to set stream format: %d\n", err);
314f3699e2cSTakashi Sakamoto 			goto end;
315f3699e2cSTakashi Sakamoto 		}
316b0ac0009STakashi Sakamoto 
317b0ac0009STakashi Sakamoto 		/* Start opposite stream if needed. */
318b0ac0009STakashi Sakamoto 		if (opposite && !amdtp_stream_running(opposite) &&
319b0ac0009STakashi Sakamoto 		    (opposite_substreams > 0)) {
320b0ac0009STakashi Sakamoto 			err = start_stream(oxfw, opposite, rate, 0);
321b0ac0009STakashi Sakamoto 			if (err < 0) {
322b0ac0009STakashi Sakamoto 				dev_err(&oxfw->unit->device,
323b0ac0009STakashi Sakamoto 					"fail to restart stream: %d\n", err);
324b0ac0009STakashi Sakamoto 				goto end;
325b0ac0009STakashi Sakamoto 			}
326b0ac0009STakashi Sakamoto 		}
327f3699e2cSTakashi Sakamoto 	}
328f3699e2cSTakashi Sakamoto 
329b0ac0009STakashi Sakamoto 	/* Start requested stream. */
330b0ac0009STakashi Sakamoto 	if (!amdtp_stream_running(stream)) {
331b0ac0009STakashi Sakamoto 		err = start_stream(oxfw, stream, rate, pcm_channels);
332f3699e2cSTakashi Sakamoto 		if (err < 0)
333f3699e2cSTakashi Sakamoto 			dev_err(&oxfw->unit->device,
334f3699e2cSTakashi Sakamoto 				"fail to start stream: %d\n", err);
335b0ac0009STakashi Sakamoto 	}
336e2786ca6STakashi Sakamoto end:
337e2786ca6STakashi Sakamoto 	return err;
338e2786ca6STakashi Sakamoto }
339e2786ca6STakashi Sakamoto 
340b0ac0009STakashi Sakamoto void snd_oxfw_stream_stop_simplex(struct snd_oxfw *oxfw,
341b0ac0009STakashi Sakamoto 				  struct amdtp_stream *stream)
342e2786ca6STakashi Sakamoto {
343b0ac0009STakashi Sakamoto 	if (((stream == &oxfw->tx_stream) && (oxfw->capture_substreams > 0)) ||
344b0ac0009STakashi Sakamoto 	    ((stream == &oxfw->rx_stream) && (oxfw->playback_substreams > 0)))
345b0ac0009STakashi Sakamoto 		return;
346b0ac0009STakashi Sakamoto 
347b0ac0009STakashi Sakamoto 	stop_stream(oxfw, stream);
348e2786ca6STakashi Sakamoto }
349e2786ca6STakashi Sakamoto 
350d23c2cc4STakashi Sakamoto /*
351d23c2cc4STakashi Sakamoto  * This function should be called before starting the stream or after stopping
352d23c2cc4STakashi Sakamoto  * the streams.
353d23c2cc4STakashi Sakamoto  */
354b0ac0009STakashi Sakamoto void snd_oxfw_stream_destroy_simplex(struct snd_oxfw *oxfw,
355b0ac0009STakashi Sakamoto 				     struct amdtp_stream *stream)
356e2786ca6STakashi Sakamoto {
357b0ac0009STakashi Sakamoto 	struct cmp_connection *conn;
358e2786ca6STakashi Sakamoto 
359b0ac0009STakashi Sakamoto 	if (stream == &oxfw->tx_stream)
360b0ac0009STakashi Sakamoto 		conn = &oxfw->out_conn;
361e2786ca6STakashi Sakamoto 	else
362b0ac0009STakashi Sakamoto 		conn = &oxfw->in_conn;
363b0ac0009STakashi Sakamoto 
364b0ac0009STakashi Sakamoto 	amdtp_stream_destroy(stream);
365b0ac0009STakashi Sakamoto 	cmp_connection_destroy(conn);
366b0ac0009STakashi Sakamoto }
367b0ac0009STakashi Sakamoto 
368b0ac0009STakashi Sakamoto void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw,
369b0ac0009STakashi Sakamoto 				    struct amdtp_stream *stream)
370b0ac0009STakashi Sakamoto {
371b0ac0009STakashi Sakamoto 	struct cmp_connection *conn;
372b0ac0009STakashi Sakamoto 
373b0ac0009STakashi Sakamoto 	if (stream == &oxfw->tx_stream)
374b0ac0009STakashi Sakamoto 		conn = &oxfw->out_conn;
375b0ac0009STakashi Sakamoto 	else
376b0ac0009STakashi Sakamoto 		conn = &oxfw->in_conn;
377b0ac0009STakashi Sakamoto 
378b0ac0009STakashi Sakamoto 	if (cmp_connection_update(conn) < 0)
379b0ac0009STakashi Sakamoto 		stop_stream(oxfw, stream);
380b0ac0009STakashi Sakamoto 	else
381b0ac0009STakashi Sakamoto 		amdtp_stream_update(stream);
382e2786ca6STakashi Sakamoto }
3835cd1d3f4STakashi Sakamoto 
3843c96101fSTakashi Sakamoto int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
3853c96101fSTakashi Sakamoto 				enum avc_general_plug_dir dir,
3863c96101fSTakashi Sakamoto 				struct snd_oxfw_stream_formation *formation)
3873c96101fSTakashi Sakamoto {
3883c96101fSTakashi Sakamoto 	u8 *format;
3893c96101fSTakashi Sakamoto 	unsigned int len;
3903c96101fSTakashi Sakamoto 	int err;
3913c96101fSTakashi Sakamoto 
3923c96101fSTakashi Sakamoto 	len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
3933c96101fSTakashi Sakamoto 	format = kmalloc(len, GFP_KERNEL);
3943c96101fSTakashi Sakamoto 	if (format == NULL)
3953c96101fSTakashi Sakamoto 		return -ENOMEM;
3963c96101fSTakashi Sakamoto 
3973c96101fSTakashi Sakamoto 	err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
3983c96101fSTakashi Sakamoto 	if (err < 0)
3993c96101fSTakashi Sakamoto 		goto end;
4003c96101fSTakashi Sakamoto 	if (len < 3) {
4013c96101fSTakashi Sakamoto 		err = -EIO;
4023c96101fSTakashi Sakamoto 		goto end;
4033c96101fSTakashi Sakamoto 	}
4043c96101fSTakashi Sakamoto 
4053c96101fSTakashi Sakamoto 	err = snd_oxfw_stream_parse_format(format, formation);
4063c96101fSTakashi Sakamoto end:
4073c96101fSTakashi Sakamoto 	kfree(format);
4083c96101fSTakashi Sakamoto 	return err;
4093c96101fSTakashi Sakamoto }
4103c96101fSTakashi Sakamoto 
4115cd1d3f4STakashi Sakamoto /*
4125cd1d3f4STakashi Sakamoto  * See Table 6.16 - AM824 Stream Format
4135cd1d3f4STakashi Sakamoto  *     Figure 6.19 - format_information field for AM824 Compound
4145cd1d3f4STakashi Sakamoto  * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
4155cd1d3f4STakashi Sakamoto  * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
4165cd1d3f4STakashi Sakamoto  */
4175cd1d3f4STakashi Sakamoto int snd_oxfw_stream_parse_format(u8 *format,
4185cd1d3f4STakashi Sakamoto 				 struct snd_oxfw_stream_formation *formation)
4195cd1d3f4STakashi Sakamoto {
4205cd1d3f4STakashi Sakamoto 	unsigned int i, e, channels, type;
4215cd1d3f4STakashi Sakamoto 
4225cd1d3f4STakashi Sakamoto 	memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
4235cd1d3f4STakashi Sakamoto 
4245cd1d3f4STakashi Sakamoto 	/*
4255cd1d3f4STakashi Sakamoto 	 * this module can support a hierarchy combination that:
4265cd1d3f4STakashi Sakamoto 	 *  Root:	Audio and Music (0x90)
4275cd1d3f4STakashi Sakamoto 	 *  Level 1:	AM824 Compound  (0x40)
4285cd1d3f4STakashi Sakamoto 	 */
4295cd1d3f4STakashi Sakamoto 	if ((format[0] != 0x90) || (format[1] != 0x40))
4305cd1d3f4STakashi Sakamoto 		return -ENOSYS;
4315cd1d3f4STakashi Sakamoto 
4325cd1d3f4STakashi Sakamoto 	/* check the sampling rate */
4335cd1d3f4STakashi Sakamoto 	for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
4345cd1d3f4STakashi Sakamoto 		if (format[2] == avc_stream_rate_table[i])
4355cd1d3f4STakashi Sakamoto 			break;
4365cd1d3f4STakashi Sakamoto 	}
4375cd1d3f4STakashi Sakamoto 	if (i == ARRAY_SIZE(avc_stream_rate_table))
4385cd1d3f4STakashi Sakamoto 		return -ENOSYS;
4395cd1d3f4STakashi Sakamoto 
4405cd1d3f4STakashi Sakamoto 	formation->rate = oxfw_rate_table[i];
4415cd1d3f4STakashi Sakamoto 
4425cd1d3f4STakashi Sakamoto 	for (e = 0; e < format[4]; e++) {
4435cd1d3f4STakashi Sakamoto 		channels = format[5 + e * 2];
4445cd1d3f4STakashi Sakamoto 		type = format[6 + e * 2];
4455cd1d3f4STakashi Sakamoto 
4465cd1d3f4STakashi Sakamoto 		switch (type) {
4475cd1d3f4STakashi Sakamoto 		/* IEC 60958 Conformant, currently handled as MBLA */
4485cd1d3f4STakashi Sakamoto 		case 0x00:
4495cd1d3f4STakashi Sakamoto 		/* Multi Bit Linear Audio (Raw) */
4505cd1d3f4STakashi Sakamoto 		case 0x06:
4515cd1d3f4STakashi Sakamoto 			formation->pcm += channels;
4525cd1d3f4STakashi Sakamoto 			break;
4535cd1d3f4STakashi Sakamoto 		/* MIDI Conformant */
4545cd1d3f4STakashi Sakamoto 		case 0x0d:
4555cd1d3f4STakashi Sakamoto 			formation->midi = channels;
4565cd1d3f4STakashi Sakamoto 			break;
4575cd1d3f4STakashi Sakamoto 		/* IEC 61937-3 to 7 */
4585cd1d3f4STakashi Sakamoto 		case 0x01:
4595cd1d3f4STakashi Sakamoto 		case 0x02:
4605cd1d3f4STakashi Sakamoto 		case 0x03:
4615cd1d3f4STakashi Sakamoto 		case 0x04:
4625cd1d3f4STakashi Sakamoto 		case 0x05:
4635cd1d3f4STakashi Sakamoto 		/* Multi Bit Linear Audio */
4645cd1d3f4STakashi Sakamoto 		case 0x07:	/* DVD-Audio */
4655cd1d3f4STakashi Sakamoto 		case 0x0c:	/* High Precision */
4665cd1d3f4STakashi Sakamoto 		/* One Bit Audio */
4675cd1d3f4STakashi Sakamoto 		case 0x08:	/* (Plain) Raw */
4685cd1d3f4STakashi Sakamoto 		case 0x09:	/* (Plain) SACD */
4695cd1d3f4STakashi Sakamoto 		case 0x0a:	/* (Encoded) Raw */
4705cd1d3f4STakashi Sakamoto 		case 0x0b:	/* (Encoded) SACD */
4715cd1d3f4STakashi Sakamoto 		/* SMPTE Time-Code conformant */
4725cd1d3f4STakashi Sakamoto 		case 0x0e:
4735cd1d3f4STakashi Sakamoto 		/* Sample Count */
4745cd1d3f4STakashi Sakamoto 		case 0x0f:
4755cd1d3f4STakashi Sakamoto 		/* Anciliary Data */
4765cd1d3f4STakashi Sakamoto 		case 0x10:
4775cd1d3f4STakashi Sakamoto 		/* Synchronization Stream (Stereo Raw audio) */
4785cd1d3f4STakashi Sakamoto 		case 0x40:
4795cd1d3f4STakashi Sakamoto 		/* Don't care */
4805cd1d3f4STakashi Sakamoto 		case 0xff:
4815cd1d3f4STakashi Sakamoto 		default:
4825cd1d3f4STakashi Sakamoto 			return -ENOSYS;	/* not supported */
4835cd1d3f4STakashi Sakamoto 		}
4845cd1d3f4STakashi Sakamoto 	}
4855cd1d3f4STakashi Sakamoto 
4865cd1d3f4STakashi Sakamoto 	if (formation->pcm  > AMDTP_MAX_CHANNELS_FOR_PCM ||
4875cd1d3f4STakashi Sakamoto 	    formation->midi > AMDTP_MAX_CHANNELS_FOR_MIDI)
4885cd1d3f4STakashi Sakamoto 		return -ENOSYS;
4895cd1d3f4STakashi Sakamoto 
4905cd1d3f4STakashi Sakamoto 	return 0;
4915cd1d3f4STakashi Sakamoto }
4925cd1d3f4STakashi Sakamoto 
4935cd1d3f4STakashi Sakamoto static int
4945cd1d3f4STakashi Sakamoto assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
4955cd1d3f4STakashi Sakamoto 		      unsigned int pid, u8 *buf, unsigned int *len,
4965cd1d3f4STakashi Sakamoto 		      u8 **formats)
4975cd1d3f4STakashi Sakamoto {
4985cd1d3f4STakashi Sakamoto 	struct snd_oxfw_stream_formation formation;
4995cd1d3f4STakashi Sakamoto 	unsigned int i, eid;
5005cd1d3f4STakashi Sakamoto 	int err;
5015cd1d3f4STakashi Sakamoto 
5025cd1d3f4STakashi Sakamoto 	/* get format at current sampling rate */
5035cd1d3f4STakashi Sakamoto 	err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
5045cd1d3f4STakashi Sakamoto 	if (err < 0) {
5055cd1d3f4STakashi Sakamoto 		dev_err(&oxfw->unit->device,
5065cd1d3f4STakashi Sakamoto 		"fail to get current stream format for isoc %s plug %d:%d\n",
5075cd1d3f4STakashi Sakamoto 			(dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
5085cd1d3f4STakashi Sakamoto 			pid, err);
5095cd1d3f4STakashi Sakamoto 		goto end;
5105cd1d3f4STakashi Sakamoto 	}
5115cd1d3f4STakashi Sakamoto 
5125cd1d3f4STakashi Sakamoto 	/* parse and set stream format */
5135cd1d3f4STakashi Sakamoto 	eid = 0;
5145cd1d3f4STakashi Sakamoto 	err = snd_oxfw_stream_parse_format(buf, &formation);
5155cd1d3f4STakashi Sakamoto 	if (err < 0)
5165cd1d3f4STakashi Sakamoto 		goto end;
5175cd1d3f4STakashi Sakamoto 
51896bab82fSAndrzej Hajda 	formats[eid] = kmemdup(buf, *len, GFP_KERNEL);
5195cd1d3f4STakashi Sakamoto 	if (formats[eid] == NULL) {
5205cd1d3f4STakashi Sakamoto 		err = -ENOMEM;
5215cd1d3f4STakashi Sakamoto 		goto end;
5225cd1d3f4STakashi Sakamoto 	}
5235cd1d3f4STakashi Sakamoto 
5245cd1d3f4STakashi Sakamoto 	/* apply the format for each available sampling rate */
5255cd1d3f4STakashi Sakamoto 	for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
5265cd1d3f4STakashi Sakamoto 		if (formation.rate == oxfw_rate_table[i])
5275cd1d3f4STakashi Sakamoto 			continue;
5285cd1d3f4STakashi Sakamoto 
5295cd1d3f4STakashi Sakamoto 		err = avc_general_inquiry_sig_fmt(oxfw->unit,
5305cd1d3f4STakashi Sakamoto 						  oxfw_rate_table[i],
5315cd1d3f4STakashi Sakamoto 						  dir, pid);
5325cd1d3f4STakashi Sakamoto 		if (err < 0)
5335cd1d3f4STakashi Sakamoto 			continue;
5345cd1d3f4STakashi Sakamoto 
5355cd1d3f4STakashi Sakamoto 		eid++;
53696bab82fSAndrzej Hajda 		formats[eid] = kmemdup(buf, *len, GFP_KERNEL);
5375cd1d3f4STakashi Sakamoto 		if (formats[eid] == NULL) {
5385cd1d3f4STakashi Sakamoto 			err = -ENOMEM;
5395cd1d3f4STakashi Sakamoto 			goto end;
5405cd1d3f4STakashi Sakamoto 		}
5415cd1d3f4STakashi Sakamoto 		formats[eid][2] = avc_stream_rate_table[i];
5425cd1d3f4STakashi Sakamoto 	}
5435cd1d3f4STakashi Sakamoto 
5445cd1d3f4STakashi Sakamoto 	err = 0;
5455cd1d3f4STakashi Sakamoto 	oxfw->assumed = true;
5465cd1d3f4STakashi Sakamoto end:
5475cd1d3f4STakashi Sakamoto 	return err;
5485cd1d3f4STakashi Sakamoto }
5495cd1d3f4STakashi Sakamoto 
5505cd1d3f4STakashi Sakamoto static int fill_stream_formats(struct snd_oxfw *oxfw,
5515cd1d3f4STakashi Sakamoto 			       enum avc_general_plug_dir dir,
5525cd1d3f4STakashi Sakamoto 			       unsigned short pid)
5535cd1d3f4STakashi Sakamoto {
5545cd1d3f4STakashi Sakamoto 	u8 *buf, **formats;
5555cd1d3f4STakashi Sakamoto 	unsigned int len, eid = 0;
5565cd1d3f4STakashi Sakamoto 	struct snd_oxfw_stream_formation dummy;
5575cd1d3f4STakashi Sakamoto 	int err;
5585cd1d3f4STakashi Sakamoto 
5595cd1d3f4STakashi Sakamoto 	buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
5605cd1d3f4STakashi Sakamoto 	if (buf == NULL)
5615cd1d3f4STakashi Sakamoto 		return -ENOMEM;
5625cd1d3f4STakashi Sakamoto 
563b0ac0009STakashi Sakamoto 	if (dir == AVC_GENERAL_PLUG_DIR_OUT)
564b0ac0009STakashi Sakamoto 		formats = oxfw->tx_stream_formats;
565b0ac0009STakashi Sakamoto 	else
5665cd1d3f4STakashi Sakamoto 		formats = oxfw->rx_stream_formats;
5675cd1d3f4STakashi Sakamoto 
5685cd1d3f4STakashi Sakamoto 	/* get first entry */
5695cd1d3f4STakashi Sakamoto 	len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
5705cd1d3f4STakashi Sakamoto 	err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
5715cd1d3f4STakashi Sakamoto 	if (err == -ENOSYS) {
5725cd1d3f4STakashi Sakamoto 		/* LIST subfunction is not implemented */
5735cd1d3f4STakashi Sakamoto 		len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
5745cd1d3f4STakashi Sakamoto 		err = assume_stream_formats(oxfw, dir, pid, buf, &len,
5755cd1d3f4STakashi Sakamoto 					    formats);
5765cd1d3f4STakashi Sakamoto 		goto end;
5775cd1d3f4STakashi Sakamoto 	} else if (err < 0) {
5785cd1d3f4STakashi Sakamoto 		dev_err(&oxfw->unit->device,
5795cd1d3f4STakashi Sakamoto 			"fail to get stream format %d for isoc %s plug %d:%d\n",
5805cd1d3f4STakashi Sakamoto 			eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
5815cd1d3f4STakashi Sakamoto 			pid, err);
5825cd1d3f4STakashi Sakamoto 		goto end;
5835cd1d3f4STakashi Sakamoto 	}
5845cd1d3f4STakashi Sakamoto 
5855cd1d3f4STakashi Sakamoto 	/* LIST subfunction is implemented */
5865cd1d3f4STakashi Sakamoto 	while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
5875cd1d3f4STakashi Sakamoto 		/* The format is too short. */
5885cd1d3f4STakashi Sakamoto 		if (len < 3) {
5895cd1d3f4STakashi Sakamoto 			err = -EIO;
5905cd1d3f4STakashi Sakamoto 			break;
5915cd1d3f4STakashi Sakamoto 		}
5925cd1d3f4STakashi Sakamoto 
5935cd1d3f4STakashi Sakamoto 		/* parse and set stream format */
5945cd1d3f4STakashi Sakamoto 		err = snd_oxfw_stream_parse_format(buf, &dummy);
5955cd1d3f4STakashi Sakamoto 		if (err < 0)
5965cd1d3f4STakashi Sakamoto 			break;
5975cd1d3f4STakashi Sakamoto 
59896bab82fSAndrzej Hajda 		formats[eid] = kmemdup(buf, len, GFP_KERNEL);
5995cd1d3f4STakashi Sakamoto 		if (formats[eid] == NULL) {
6005cd1d3f4STakashi Sakamoto 			err = -ENOMEM;
6015cd1d3f4STakashi Sakamoto 			break;
6025cd1d3f4STakashi Sakamoto 		}
6035cd1d3f4STakashi Sakamoto 
6045cd1d3f4STakashi Sakamoto 		/* get next entry */
6055cd1d3f4STakashi Sakamoto 		len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
6065cd1d3f4STakashi Sakamoto 		err = avc_stream_get_format_list(oxfw->unit, dir, 0,
6075cd1d3f4STakashi Sakamoto 						 buf, &len, ++eid);
6085cd1d3f4STakashi Sakamoto 		/* No entries remained. */
6095cd1d3f4STakashi Sakamoto 		if (err == -EINVAL) {
6105cd1d3f4STakashi Sakamoto 			err = 0;
6115cd1d3f4STakashi Sakamoto 			break;
6125cd1d3f4STakashi Sakamoto 		} else if (err < 0) {
6135cd1d3f4STakashi Sakamoto 			dev_err(&oxfw->unit->device,
6145cd1d3f4STakashi Sakamoto 			"fail to get stream format %d for isoc %s plug %d:%d\n",
6155cd1d3f4STakashi Sakamoto 				eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
6165cd1d3f4STakashi Sakamoto 									"out",
6175cd1d3f4STakashi Sakamoto 				pid, err);
6185cd1d3f4STakashi Sakamoto 			break;
6195cd1d3f4STakashi Sakamoto 		}
6205cd1d3f4STakashi Sakamoto 	}
6215cd1d3f4STakashi Sakamoto end:
6225cd1d3f4STakashi Sakamoto 	kfree(buf);
6235cd1d3f4STakashi Sakamoto 	return err;
6245cd1d3f4STakashi Sakamoto }
6255cd1d3f4STakashi Sakamoto 
6265cd1d3f4STakashi Sakamoto int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
6275cd1d3f4STakashi Sakamoto {
6285cd1d3f4STakashi Sakamoto 	u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
6295cd1d3f4STakashi Sakamoto 	int err;
6305cd1d3f4STakashi Sakamoto 
6315cd1d3f4STakashi Sakamoto 	/* the number of plugs for isoc in/out, ext in/out  */
6325cd1d3f4STakashi Sakamoto 	err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
6335cd1d3f4STakashi Sakamoto 	if (err < 0) {
6345cd1d3f4STakashi Sakamoto 		dev_err(&oxfw->unit->device,
6355cd1d3f4STakashi Sakamoto 		"fail to get info for isoc/external in/out plugs: %d\n",
6365cd1d3f4STakashi Sakamoto 			err);
6375cd1d3f4STakashi Sakamoto 		goto end;
638b0ac0009STakashi Sakamoto 	} else if ((plugs[0] == 0) && (plugs[1] == 0)) {
6395cd1d3f4STakashi Sakamoto 		err = -ENOSYS;
6405cd1d3f4STakashi Sakamoto 		goto end;
6415cd1d3f4STakashi Sakamoto 	}
6425cd1d3f4STakashi Sakamoto 
643b0ac0009STakashi Sakamoto 	/* use oPCR[0] if exists */
644b0ac0009STakashi Sakamoto 	if (plugs[1] > 0) {
645b0ac0009STakashi Sakamoto 		err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
646b0ac0009STakashi Sakamoto 		if (err < 0)
647b0ac0009STakashi Sakamoto 			goto end;
648b0ac0009STakashi Sakamoto 		oxfw->has_output = true;
649b0ac0009STakashi Sakamoto 	}
650b0ac0009STakashi Sakamoto 
6515cd1d3f4STakashi Sakamoto 	/* use iPCR[0] if exists */
6525cd1d3f4STakashi Sakamoto 	if (plugs[0] > 0)
6535cd1d3f4STakashi Sakamoto 		err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
6545cd1d3f4STakashi Sakamoto end:
6555cd1d3f4STakashi Sakamoto 	return err;
6565cd1d3f4STakashi Sakamoto }
6578985f4acSTakashi Sakamoto 
6588985f4acSTakashi Sakamoto void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
6598985f4acSTakashi Sakamoto {
6608985f4acSTakashi Sakamoto 	oxfw->dev_lock_changed = true;
6618985f4acSTakashi Sakamoto 	wake_up(&oxfw->hwdep_wait);
6628985f4acSTakashi Sakamoto }
6638985f4acSTakashi Sakamoto 
6648985f4acSTakashi Sakamoto int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
6658985f4acSTakashi Sakamoto {
6668985f4acSTakashi Sakamoto 	int err;
6678985f4acSTakashi Sakamoto 
6688985f4acSTakashi Sakamoto 	spin_lock_irq(&oxfw->lock);
6698985f4acSTakashi Sakamoto 
6708985f4acSTakashi Sakamoto 	/* user land lock this */
6718985f4acSTakashi Sakamoto 	if (oxfw->dev_lock_count < 0) {
6728985f4acSTakashi Sakamoto 		err = -EBUSY;
6738985f4acSTakashi Sakamoto 		goto end;
6748985f4acSTakashi Sakamoto 	}
6758985f4acSTakashi Sakamoto 
6768985f4acSTakashi Sakamoto 	/* this is the first time */
6778985f4acSTakashi Sakamoto 	if (oxfw->dev_lock_count++ == 0)
6788985f4acSTakashi Sakamoto 		snd_oxfw_stream_lock_changed(oxfw);
6798985f4acSTakashi Sakamoto 	err = 0;
6808985f4acSTakashi Sakamoto end:
6818985f4acSTakashi Sakamoto 	spin_unlock_irq(&oxfw->lock);
6828985f4acSTakashi Sakamoto 	return err;
6838985f4acSTakashi Sakamoto }
6848985f4acSTakashi Sakamoto 
6858985f4acSTakashi Sakamoto void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
6868985f4acSTakashi Sakamoto {
6878985f4acSTakashi Sakamoto 	spin_lock_irq(&oxfw->lock);
6888985f4acSTakashi Sakamoto 
6898985f4acSTakashi Sakamoto 	if (WARN_ON(oxfw->dev_lock_count <= 0))
6908985f4acSTakashi Sakamoto 		goto end;
6918985f4acSTakashi Sakamoto 	if (--oxfw->dev_lock_count == 0)
6928985f4acSTakashi Sakamoto 		snd_oxfw_stream_lock_changed(oxfw);
6938985f4acSTakashi Sakamoto end:
6948985f4acSTakashi Sakamoto 	spin_unlock_irq(&oxfw->lock);
6958985f4acSTakashi Sakamoto }
696