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