1 /*
2  * oxfw_stream.c - a part of driver for OXFW970/971 based devices
3  *
4  * Copyright (c) 2014 Takashi Sakamoto
5  *
6  * Licensed under the terms of the GNU General Public License, version 2.
7  */
8 
9 #include "oxfw.h"
10 #include <linux/delay.h>
11 
12 #define AVC_GENERIC_FRAME_MAXIMUM_BYTES	512
13 #define CALLBACK_TIMEOUT	200
14 
15 /*
16  * According to datasheet of Oxford Semiconductor:
17  *  OXFW970: 32.0/44.1/48.0/96.0 Khz, 8 audio channels I/O
18  *  OXFW971: 32.0/44.1/48.0/88.2/96.0/192.0 kHz, 16 audio channels I/O, MIDI I/O
19  */
20 static const unsigned int oxfw_rate_table[] = {
21 	[0] = 32000,
22 	[1] = 44100,
23 	[2] = 48000,
24 	[3] = 88200,
25 	[4] = 96000,
26 	[5] = 192000,
27 };
28 
29 /*
30  * See Table 5.7 – Sampling frequency for Multi-bit Audio
31  * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
32  */
33 static const unsigned int avc_stream_rate_table[] = {
34 	[0] = 0x02,
35 	[1] = 0x03,
36 	[2] = 0x04,
37 	[3] = 0x0a,
38 	[4] = 0x05,
39 	[5] = 0x07,
40 };
41 
42 static int set_rate(struct snd_oxfw *oxfw, unsigned int rate)
43 {
44 	int err;
45 
46 	err = avc_general_set_sig_fmt(oxfw->unit, rate,
47 				      AVC_GENERAL_PLUG_DIR_IN, 0);
48 	if (err < 0)
49 		goto end;
50 
51 	if (oxfw->has_output)
52 		err = avc_general_set_sig_fmt(oxfw->unit, rate,
53 					      AVC_GENERAL_PLUG_DIR_OUT, 0);
54 end:
55 	return err;
56 }
57 
58 static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s,
59 			     unsigned int rate, unsigned int pcm_channels)
60 {
61 	u8 **formats;
62 	struct snd_oxfw_stream_formation formation;
63 	enum avc_general_plug_dir dir;
64 	unsigned int len;
65 	int i, err;
66 
67 	if (s == &oxfw->tx_stream) {
68 		formats = oxfw->tx_stream_formats;
69 		dir = AVC_GENERAL_PLUG_DIR_OUT;
70 	} else {
71 		formats = oxfw->rx_stream_formats;
72 		dir = AVC_GENERAL_PLUG_DIR_IN;
73 	}
74 
75 	/* Seek stream format for requirements. */
76 	for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
77 		err = snd_oxfw_stream_parse_format(formats[i], &formation);
78 		if (err < 0)
79 			return err;
80 
81 		if ((formation.rate == rate) && (formation.pcm == pcm_channels))
82 			break;
83 	}
84 	if (i == SND_OXFW_STREAM_FORMAT_ENTRIES)
85 		return -EINVAL;
86 
87 	/* If assumed, just change rate. */
88 	if (oxfw->assumed)
89 		return set_rate(oxfw, rate);
90 
91 	/* Calculate format length. */
92 	len = 5 + formats[i][4] * 2;
93 
94 	err = avc_stream_set_format(oxfw->unit, dir, 0, formats[i], len);
95 	if (err < 0)
96 		return err;
97 
98 	/* Some requests just after changing format causes freezing. */
99 	msleep(100);
100 
101 	return 0;
102 }
103 
104 static int start_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
105 {
106 	struct cmp_connection *conn;
107 	int err;
108 
109 	if (stream == &oxfw->rx_stream)
110 		conn = &oxfw->in_conn;
111 	else
112 		conn = &oxfw->out_conn;
113 
114 	err = cmp_connection_establish(conn,
115 				       amdtp_stream_get_max_payload(stream));
116 	if (err < 0)
117 		return err;
118 
119 	err = amdtp_stream_start(stream, conn->resources.channel, conn->speed);
120 	if (err < 0) {
121 		cmp_connection_break(conn);
122 		return err;
123 	}
124 
125 	// Wait first packet.
126 	if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) {
127 		amdtp_stream_stop(stream);
128 		cmp_connection_break(conn);
129 		return -ETIMEDOUT;
130 	}
131 
132 	return 0;
133 }
134 
135 static int check_connection_used_by_others(struct snd_oxfw *oxfw,
136 					   struct amdtp_stream *stream)
137 {
138 	struct cmp_connection *conn;
139 	bool used;
140 	int err;
141 
142 	if (stream == &oxfw->tx_stream)
143 		conn = &oxfw->out_conn;
144 	else
145 		conn = &oxfw->in_conn;
146 
147 	err = cmp_connection_check_used(conn, &used);
148 	if ((err >= 0) && used && !amdtp_stream_running(stream)) {
149 		dev_err(&oxfw->unit->device,
150 			"Connection established by others: %cPCR[%d]\n",
151 			(conn->direction == CMP_OUTPUT) ? 'o' : 'i',
152 			conn->pcr_index);
153 		err = -EBUSY;
154 	}
155 
156 	return err;
157 }
158 
159 static int init_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
160 {
161 	struct cmp_connection *conn;
162 	enum cmp_direction c_dir;
163 	enum amdtp_stream_direction s_dir;
164 	int err;
165 
166 	if (stream == &oxfw->tx_stream) {
167 		conn = &oxfw->out_conn;
168 		c_dir = CMP_OUTPUT;
169 		s_dir = AMDTP_IN_STREAM;
170 	} else {
171 		conn = &oxfw->in_conn;
172 		c_dir = CMP_INPUT;
173 		s_dir = AMDTP_OUT_STREAM;
174 	}
175 
176 	err = cmp_connection_init(conn, oxfw->unit, c_dir, 0);
177 	if (err < 0)
178 		return err;
179 
180 	err = amdtp_am824_init(stream, oxfw->unit, s_dir, CIP_NONBLOCKING);
181 	if (err < 0) {
182 		cmp_connection_destroy(conn);
183 		return err;
184 	}
185 
186 	/*
187 	 * OXFW starts to transmit packets with non-zero dbc.
188 	 * OXFW postpone transferring packets till handling any asynchronous
189 	 * packets. As a result, next isochronous packet includes more data
190 	 * blocks than IEC 61883-6 defines.
191 	 */
192 	if (stream == &oxfw->tx_stream) {
193 		oxfw->tx_stream.flags |= CIP_JUMBO_PAYLOAD;
194 		if (oxfw->wrong_dbs)
195 			oxfw->tx_stream.flags |= CIP_WRONG_DBS;
196 	}
197 
198 	return 0;
199 }
200 
201 static int keep_resources(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
202 {
203 	enum avc_general_plug_dir dir;
204 	u8 **formats;
205 	struct snd_oxfw_stream_formation formation;
206 	int i;
207 	int err;
208 
209 	if (stream == &oxfw->rx_stream) {
210 		dir = AVC_GENERAL_PLUG_DIR_IN;
211 		formats = oxfw->rx_stream_formats;
212 	} else {
213 		dir = AVC_GENERAL_PLUG_DIR_OUT;
214 		formats = oxfw->tx_stream_formats;
215 	}
216 
217 	err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
218 	if (err < 0)
219 		return err;
220 
221 	for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
222 		struct snd_oxfw_stream_formation fmt;
223 
224 		if (formats[i] == NULL)
225 			break;
226 
227 		err = snd_oxfw_stream_parse_format(formats[i], &fmt);
228 		if (err < 0)
229 			return err;
230 
231 		if (fmt.rate == formation.rate && fmt.pcm == formation.pcm &&
232 		    fmt.midi == formation.midi)
233 			break;
234 	}
235 	if (i == SND_OXFW_STREAM_FORMAT_ENTRIES)
236 		return -EINVAL;
237 
238 	// The stream should have one pcm channels at least.
239 	if (formation.pcm == 0)
240 		return -EINVAL;
241 
242 	return amdtp_am824_set_parameters(stream, formation.rate, formation.pcm,
243 					 formation.midi * 8, false);
244 }
245 
246 int snd_oxfw_stream_reserve_duplex(struct snd_oxfw *oxfw,
247 				   struct amdtp_stream *stream,
248 				   unsigned int rate, unsigned int pcm_channels)
249 {
250 	struct snd_oxfw_stream_formation formation;
251 	enum avc_general_plug_dir dir;
252 	int err;
253 
254 	// Considering JACK/FFADO streaming:
255 	// TODO: This can be removed hwdep functionality becomes popular.
256 	err = check_connection_used_by_others(oxfw, &oxfw->rx_stream);
257 	if (err < 0)
258 		return err;
259 	if (oxfw->has_output) {
260 		err = check_connection_used_by_others(oxfw, &oxfw->tx_stream);
261 		if (err < 0)
262 			return err;
263 	}
264 
265 	if (stream == &oxfw->tx_stream)
266 		dir = AVC_GENERAL_PLUG_DIR_OUT;
267 	else
268 		dir = AVC_GENERAL_PLUG_DIR_IN;
269 
270 	err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
271 	if (err < 0)
272 		return err;
273 	if (rate == 0) {
274 		rate = formation.rate;
275 		pcm_channels = formation.pcm;
276 	}
277 	if (formation.rate != rate || formation.pcm != pcm_channels) {
278 		amdtp_stream_stop(&oxfw->rx_stream);
279 		cmp_connection_break(&oxfw->in_conn);
280 
281 		if (oxfw->has_output) {
282 			amdtp_stream_stop(&oxfw->tx_stream);
283 			cmp_connection_break(&oxfw->out_conn);
284 		}
285 	}
286 
287 	if (oxfw->substreams_count == 0 ||
288 	    formation.rate != rate || formation.pcm != pcm_channels) {
289 		err = set_stream_format(oxfw, stream, rate, pcm_channels);
290 		if (err < 0) {
291 			dev_err(&oxfw->unit->device,
292 				"fail to set stream format: %d\n", err);
293 			return err;
294 		}
295 
296 		err = keep_resources(oxfw, &oxfw->rx_stream);
297 		if (err < 0)
298 			return err;
299 
300 		if (oxfw->has_output) {
301 			err = keep_resources(oxfw, &oxfw->tx_stream);
302 			if (err < 0)
303 				return err;
304 		}
305 	}
306 
307 	return 0;
308 }
309 
310 int snd_oxfw_stream_start_duplex(struct snd_oxfw *oxfw)
311 {
312 	int err;
313 
314 	if (oxfw->substreams_count == 0)
315 		return -EIO;
316 
317 	if (amdtp_streaming_error(&oxfw->rx_stream) ||
318 	    amdtp_streaming_error(&oxfw->tx_stream)) {
319 		amdtp_stream_stop(&oxfw->rx_stream);
320 		cmp_connection_break(&oxfw->in_conn);
321 
322 		if (oxfw->has_output) {
323 			amdtp_stream_stop(&oxfw->tx_stream);
324 			cmp_connection_break(&oxfw->out_conn);
325 		}
326 	}
327 
328 	if (!amdtp_stream_running(&oxfw->rx_stream)) {
329 		err = start_stream(oxfw, &oxfw->rx_stream);
330 		if (err < 0) {
331 			dev_err(&oxfw->unit->device,
332 				"fail to start rx stream: %d\n", err);
333 			goto error;
334 		}
335 	}
336 
337 	if (oxfw->has_output) {
338 		if (!amdtp_stream_running(&oxfw->tx_stream)) {
339 			err = start_stream(oxfw, &oxfw->tx_stream);
340 			if (err < 0) {
341 				dev_err(&oxfw->unit->device,
342 					"fail to start tx stream: %d\n", err);
343 				goto error;
344 			}
345 		}
346 	}
347 
348 	return 0;
349 error:
350 	amdtp_stream_stop(&oxfw->rx_stream);
351 	cmp_connection_break(&oxfw->in_conn);
352 	if (oxfw->has_output) {
353 		amdtp_stream_stop(&oxfw->tx_stream);
354 		cmp_connection_break(&oxfw->out_conn);
355 	}
356 	return err;
357 }
358 
359 void snd_oxfw_stream_stop_duplex(struct snd_oxfw *oxfw)
360 {
361 	if (oxfw->substreams_count == 0) {
362 		amdtp_stream_stop(&oxfw->rx_stream);
363 		cmp_connection_break(&oxfw->in_conn);
364 
365 		if (oxfw->has_output) {
366 			amdtp_stream_stop(&oxfw->tx_stream);
367 			cmp_connection_break(&oxfw->out_conn);
368 		}
369 	}
370 }
371 
372 static void destroy_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
373 {
374 	struct cmp_connection *conn;
375 
376 	if (stream == &oxfw->tx_stream)
377 		conn = &oxfw->out_conn;
378 	else
379 		conn = &oxfw->in_conn;
380 
381 	amdtp_stream_destroy(stream);
382 	cmp_connection_destroy(conn);
383 }
384 
385 int snd_oxfw_stream_init_duplex(struct snd_oxfw *oxfw)
386 {
387 	int err;
388 
389 	err = init_stream(oxfw, &oxfw->rx_stream);
390 	if (err < 0)
391 		return err;
392 
393 	if (oxfw->has_output) {
394 		err = init_stream(oxfw, &oxfw->tx_stream);
395 		if (err < 0) {
396 			destroy_stream(oxfw, &oxfw->rx_stream);
397 			return err;
398 		}
399 	}
400 
401 	return 0;
402 }
403 
404 // This function should be called before starting the stream or after stopping
405 // the streams.
406 void snd_oxfw_stream_destroy_duplex(struct snd_oxfw *oxfw)
407 {
408 	destroy_stream(oxfw, &oxfw->rx_stream);
409 
410 	if (oxfw->has_output)
411 		destroy_stream(oxfw, &oxfw->tx_stream);
412 }
413 
414 void snd_oxfw_stream_update_duplex(struct snd_oxfw *oxfw)
415 {
416 	amdtp_stream_stop(&oxfw->rx_stream);
417 	cmp_connection_break(&oxfw->in_conn);
418 
419 	amdtp_stream_pcm_abort(&oxfw->rx_stream);
420 
421 	if (oxfw->has_output) {
422 		amdtp_stream_stop(&oxfw->tx_stream);
423 		cmp_connection_break(&oxfw->out_conn);
424 
425 		amdtp_stream_pcm_abort(&oxfw->tx_stream);
426 	}
427 }
428 
429 int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
430 				enum avc_general_plug_dir dir,
431 				struct snd_oxfw_stream_formation *formation)
432 {
433 	u8 *format;
434 	unsigned int len;
435 	int err;
436 
437 	len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
438 	format = kmalloc(len, GFP_KERNEL);
439 	if (format == NULL)
440 		return -ENOMEM;
441 
442 	err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
443 	if (err < 0)
444 		goto end;
445 	if (len < 3) {
446 		err = -EIO;
447 		goto end;
448 	}
449 
450 	err = snd_oxfw_stream_parse_format(format, formation);
451 end:
452 	kfree(format);
453 	return err;
454 }
455 
456 /*
457  * See Table 6.16 - AM824 Stream Format
458  *     Figure 6.19 - format_information field for AM824 Compound
459  * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
460  * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
461  */
462 int snd_oxfw_stream_parse_format(u8 *format,
463 				 struct snd_oxfw_stream_formation *formation)
464 {
465 	unsigned int i, e, channels, type;
466 
467 	memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
468 
469 	/*
470 	 * this module can support a hierarchy combination that:
471 	 *  Root:	Audio and Music (0x90)
472 	 *  Level 1:	AM824 Compound  (0x40)
473 	 */
474 	if ((format[0] != 0x90) || (format[1] != 0x40))
475 		return -ENOSYS;
476 
477 	/* check the sampling rate */
478 	for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
479 		if (format[2] == avc_stream_rate_table[i])
480 			break;
481 	}
482 	if (i == ARRAY_SIZE(avc_stream_rate_table))
483 		return -ENOSYS;
484 
485 	formation->rate = oxfw_rate_table[i];
486 
487 	for (e = 0; e < format[4]; e++) {
488 		channels = format[5 + e * 2];
489 		type = format[6 + e * 2];
490 
491 		switch (type) {
492 		/* IEC 60958 Conformant, currently handled as MBLA */
493 		case 0x00:
494 		/* Multi Bit Linear Audio (Raw) */
495 		case 0x06:
496 			formation->pcm += channels;
497 			break;
498 		/* MIDI Conformant */
499 		case 0x0d:
500 			formation->midi = channels;
501 			break;
502 		/* IEC 61937-3 to 7 */
503 		case 0x01:
504 		case 0x02:
505 		case 0x03:
506 		case 0x04:
507 		case 0x05:
508 		/* Multi Bit Linear Audio */
509 		case 0x07:	/* DVD-Audio */
510 		case 0x0c:	/* High Precision */
511 		/* One Bit Audio */
512 		case 0x08:	/* (Plain) Raw */
513 		case 0x09:	/* (Plain) SACD */
514 		case 0x0a:	/* (Encoded) Raw */
515 		case 0x0b:	/* (Encoded) SACD */
516 		/* SMPTE Time-Code conformant */
517 		case 0x0e:
518 		/* Sample Count */
519 		case 0x0f:
520 		/* Anciliary Data */
521 		case 0x10:
522 		/* Synchronization Stream (Stereo Raw audio) */
523 		case 0x40:
524 		/* Don't care */
525 		case 0xff:
526 		default:
527 			return -ENOSYS;	/* not supported */
528 		}
529 	}
530 
531 	if (formation->pcm  > AM824_MAX_CHANNELS_FOR_PCM ||
532 	    formation->midi > AM824_MAX_CHANNELS_FOR_MIDI)
533 		return -ENOSYS;
534 
535 	return 0;
536 }
537 
538 static int
539 assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
540 		      unsigned int pid, u8 *buf, unsigned int *len,
541 		      u8 **formats)
542 {
543 	struct snd_oxfw_stream_formation formation;
544 	unsigned int i, eid;
545 	int err;
546 
547 	/* get format at current sampling rate */
548 	err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
549 	if (err < 0) {
550 		dev_err(&oxfw->unit->device,
551 		"fail to get current stream format for isoc %s plug %d:%d\n",
552 			(dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
553 			pid, err);
554 		goto end;
555 	}
556 
557 	/* parse and set stream format */
558 	eid = 0;
559 	err = snd_oxfw_stream_parse_format(buf, &formation);
560 	if (err < 0)
561 		goto end;
562 
563 	formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
564 				    GFP_KERNEL);
565 	if (!formats[eid]) {
566 		err = -ENOMEM;
567 		goto end;
568 	}
569 
570 	/* apply the format for each available sampling rate */
571 	for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
572 		if (formation.rate == oxfw_rate_table[i])
573 			continue;
574 
575 		err = avc_general_inquiry_sig_fmt(oxfw->unit,
576 						  oxfw_rate_table[i],
577 						  dir, pid);
578 		if (err < 0)
579 			continue;
580 
581 		eid++;
582 		formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
583 					    GFP_KERNEL);
584 		if (formats[eid] == NULL) {
585 			err = -ENOMEM;
586 			goto end;
587 		}
588 		formats[eid][2] = avc_stream_rate_table[i];
589 	}
590 
591 	err = 0;
592 	oxfw->assumed = true;
593 end:
594 	return err;
595 }
596 
597 static int fill_stream_formats(struct snd_oxfw *oxfw,
598 			       enum avc_general_plug_dir dir,
599 			       unsigned short pid)
600 {
601 	u8 *buf, **formats;
602 	unsigned int len, eid = 0;
603 	struct snd_oxfw_stream_formation dummy;
604 	int err;
605 
606 	buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
607 	if (buf == NULL)
608 		return -ENOMEM;
609 
610 	if (dir == AVC_GENERAL_PLUG_DIR_OUT)
611 		formats = oxfw->tx_stream_formats;
612 	else
613 		formats = oxfw->rx_stream_formats;
614 
615 	/* get first entry */
616 	len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
617 	err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
618 	if (err == -ENOSYS) {
619 		/* LIST subfunction is not implemented */
620 		len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
621 		err = assume_stream_formats(oxfw, dir, pid, buf, &len,
622 					    formats);
623 		goto end;
624 	} else if (err < 0) {
625 		dev_err(&oxfw->unit->device,
626 			"fail to get stream format %d for isoc %s plug %d:%d\n",
627 			eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
628 			pid, err);
629 		goto end;
630 	}
631 
632 	/* LIST subfunction is implemented */
633 	while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
634 		/* The format is too short. */
635 		if (len < 3) {
636 			err = -EIO;
637 			break;
638 		}
639 
640 		/* parse and set stream format */
641 		err = snd_oxfw_stream_parse_format(buf, &dummy);
642 		if (err < 0)
643 			break;
644 
645 		formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, len,
646 					    GFP_KERNEL);
647 		if (!formats[eid]) {
648 			err = -ENOMEM;
649 			break;
650 		}
651 
652 		/* get next entry */
653 		len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
654 		err = avc_stream_get_format_list(oxfw->unit, dir, 0,
655 						 buf, &len, ++eid);
656 		/* No entries remained. */
657 		if (err == -EINVAL) {
658 			err = 0;
659 			break;
660 		} else if (err < 0) {
661 			dev_err(&oxfw->unit->device,
662 			"fail to get stream format %d for isoc %s plug %d:%d\n",
663 				eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
664 									"out",
665 				pid, err);
666 			break;
667 		}
668 	}
669 end:
670 	kfree(buf);
671 	return err;
672 }
673 
674 int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
675 {
676 	u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
677 	struct snd_oxfw_stream_formation formation;
678 	u8 *format;
679 	unsigned int i;
680 	int err;
681 
682 	/* the number of plugs for isoc in/out, ext in/out  */
683 	err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
684 	if (err < 0) {
685 		dev_err(&oxfw->unit->device,
686 		"fail to get info for isoc/external in/out plugs: %d\n",
687 			err);
688 		goto end;
689 	} else if ((plugs[0] == 0) && (plugs[1] == 0)) {
690 		err = -ENOSYS;
691 		goto end;
692 	}
693 
694 	/* use oPCR[0] if exists */
695 	if (plugs[1] > 0) {
696 		err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
697 		if (err < 0)
698 			goto end;
699 
700 		for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
701 			format = oxfw->tx_stream_formats[i];
702 			if (format == NULL)
703 				continue;
704 			err = snd_oxfw_stream_parse_format(format, &formation);
705 			if (err < 0)
706 				continue;
707 
708 			/* Add one MIDI port. */
709 			if (formation.midi > 0)
710 				oxfw->midi_input_ports = 1;
711 		}
712 
713 		oxfw->has_output = true;
714 	}
715 
716 	/* use iPCR[0] if exists */
717 	if (plugs[0] > 0) {
718 		err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
719 		if (err < 0)
720 			goto end;
721 
722 		for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
723 			format = oxfw->rx_stream_formats[i];
724 			if (format == NULL)
725 				continue;
726 			err = snd_oxfw_stream_parse_format(format, &formation);
727 			if (err < 0)
728 				continue;
729 
730 			/* Add one MIDI port. */
731 			if (formation.midi > 0)
732 				oxfw->midi_output_ports = 1;
733 		}
734 	}
735 end:
736 	return err;
737 }
738 
739 void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
740 {
741 	oxfw->dev_lock_changed = true;
742 	wake_up(&oxfw->hwdep_wait);
743 }
744 
745 int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
746 {
747 	int err;
748 
749 	spin_lock_irq(&oxfw->lock);
750 
751 	/* user land lock this */
752 	if (oxfw->dev_lock_count < 0) {
753 		err = -EBUSY;
754 		goto end;
755 	}
756 
757 	/* this is the first time */
758 	if (oxfw->dev_lock_count++ == 0)
759 		snd_oxfw_stream_lock_changed(oxfw);
760 	err = 0;
761 end:
762 	spin_unlock_irq(&oxfw->lock);
763 	return err;
764 }
765 
766 void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
767 {
768 	spin_lock_irq(&oxfw->lock);
769 
770 	if (WARN_ON(oxfw->dev_lock_count <= 0))
771 		goto end;
772 	if (--oxfw->dev_lock_count == 0)
773 		snd_oxfw_stream_lock_changed(oxfw);
774 end:
775 	spin_unlock_irq(&oxfw->lock);
776 }
777