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