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