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