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