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