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 		cmp_connection_release(&oxfw->in_conn);
287 
288 		if (oxfw->has_output) {
289 			amdtp_stream_stop(&oxfw->tx_stream);
290 			cmp_connection_break(&oxfw->out_conn);
291 			cmp_connection_release(&oxfw->out_conn);
292 		}
293 	}
294 
295 	if (oxfw->substreams_count == 0 ||
296 	    formation.rate != rate || formation.pcm != pcm_channels) {
297 		err = set_stream_format(oxfw, stream, rate, pcm_channels);
298 		if (err < 0) {
299 			dev_err(&oxfw->unit->device,
300 				"fail to set stream format: %d\n", err);
301 			return err;
302 		}
303 
304 		err = keep_resources(oxfw, &oxfw->rx_stream);
305 		if (err < 0)
306 			return err;
307 
308 		if (oxfw->has_output) {
309 			err = keep_resources(oxfw, &oxfw->tx_stream);
310 			if (err < 0) {
311 				cmp_connection_release(&oxfw->in_conn);
312 				return err;
313 			}
314 		}
315 	}
316 
317 	return 0;
318 }
319 
320 int snd_oxfw_stream_start_duplex(struct snd_oxfw *oxfw)
321 {
322 	int err;
323 
324 	if (oxfw->substreams_count == 0)
325 		return -EIO;
326 
327 	if (amdtp_streaming_error(&oxfw->rx_stream) ||
328 	    amdtp_streaming_error(&oxfw->tx_stream)) {
329 		amdtp_stream_stop(&oxfw->rx_stream);
330 		cmp_connection_break(&oxfw->in_conn);
331 
332 		if (oxfw->has_output) {
333 			amdtp_stream_stop(&oxfw->tx_stream);
334 			cmp_connection_break(&oxfw->out_conn);
335 		}
336 	}
337 
338 	if (!amdtp_stream_running(&oxfw->rx_stream)) {
339 		err = start_stream(oxfw, &oxfw->rx_stream);
340 		if (err < 0) {
341 			dev_err(&oxfw->unit->device,
342 				"fail to start rx stream: %d\n", err);
343 			goto error;
344 		}
345 	}
346 
347 	if (oxfw->has_output) {
348 		if (!amdtp_stream_running(&oxfw->tx_stream)) {
349 			err = start_stream(oxfw, &oxfw->tx_stream);
350 			if (err < 0) {
351 				dev_err(&oxfw->unit->device,
352 					"fail to start tx stream: %d\n", err);
353 				goto error;
354 			}
355 		}
356 	}
357 
358 	return 0;
359 error:
360 	amdtp_stream_stop(&oxfw->rx_stream);
361 	cmp_connection_break(&oxfw->in_conn);
362 	if (oxfw->has_output) {
363 		amdtp_stream_stop(&oxfw->tx_stream);
364 		cmp_connection_break(&oxfw->out_conn);
365 	}
366 	return err;
367 }
368 
369 void snd_oxfw_stream_stop_duplex(struct snd_oxfw *oxfw)
370 {
371 	if (oxfw->substreams_count == 0) {
372 		amdtp_stream_stop(&oxfw->rx_stream);
373 		cmp_connection_break(&oxfw->in_conn);
374 		cmp_connection_release(&oxfw->in_conn);
375 
376 		if (oxfw->has_output) {
377 			amdtp_stream_stop(&oxfw->tx_stream);
378 			cmp_connection_break(&oxfw->out_conn);
379 			cmp_connection_release(&oxfw->out_conn);
380 		}
381 	}
382 }
383 
384 static void destroy_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
385 {
386 	struct cmp_connection *conn;
387 
388 	if (stream == &oxfw->tx_stream)
389 		conn = &oxfw->out_conn;
390 	else
391 		conn = &oxfw->in_conn;
392 
393 	amdtp_stream_destroy(stream);
394 	cmp_connection_destroy(conn);
395 }
396 
397 int snd_oxfw_stream_init_duplex(struct snd_oxfw *oxfw)
398 {
399 	int err;
400 
401 	err = init_stream(oxfw, &oxfw->rx_stream);
402 	if (err < 0)
403 		return err;
404 
405 	if (oxfw->has_output) {
406 		err = init_stream(oxfw, &oxfw->tx_stream);
407 		if (err < 0) {
408 			destroy_stream(oxfw, &oxfw->rx_stream);
409 			return err;
410 		}
411 	}
412 
413 	return 0;
414 }
415 
416 // This function should be called before starting the stream or after stopping
417 // the streams.
418 void snd_oxfw_stream_destroy_duplex(struct snd_oxfw *oxfw)
419 {
420 	destroy_stream(oxfw, &oxfw->rx_stream);
421 
422 	if (oxfw->has_output)
423 		destroy_stream(oxfw, &oxfw->tx_stream);
424 }
425 
426 void snd_oxfw_stream_update_duplex(struct snd_oxfw *oxfw)
427 {
428 	amdtp_stream_stop(&oxfw->rx_stream);
429 	cmp_connection_break(&oxfw->in_conn);
430 
431 	amdtp_stream_pcm_abort(&oxfw->rx_stream);
432 
433 	if (oxfw->has_output) {
434 		amdtp_stream_stop(&oxfw->tx_stream);
435 		cmp_connection_break(&oxfw->out_conn);
436 
437 		amdtp_stream_pcm_abort(&oxfw->tx_stream);
438 	}
439 }
440 
441 int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
442 				enum avc_general_plug_dir dir,
443 				struct snd_oxfw_stream_formation *formation)
444 {
445 	u8 *format;
446 	unsigned int len;
447 	int err;
448 
449 	len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
450 	format = kmalloc(len, GFP_KERNEL);
451 	if (format == NULL)
452 		return -ENOMEM;
453 
454 	err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
455 	if (err < 0)
456 		goto end;
457 	if (len < 3) {
458 		err = -EIO;
459 		goto end;
460 	}
461 
462 	err = snd_oxfw_stream_parse_format(format, formation);
463 end:
464 	kfree(format);
465 	return err;
466 }
467 
468 /*
469  * See Table 6.16 - AM824 Stream Format
470  *     Figure 6.19 - format_information field for AM824 Compound
471  * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
472  * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
473  */
474 int snd_oxfw_stream_parse_format(u8 *format,
475 				 struct snd_oxfw_stream_formation *formation)
476 {
477 	unsigned int i, e, channels, type;
478 
479 	memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
480 
481 	/*
482 	 * this module can support a hierarchy combination that:
483 	 *  Root:	Audio and Music (0x90)
484 	 *  Level 1:	AM824 Compound  (0x40)
485 	 */
486 	if ((format[0] != 0x90) || (format[1] != 0x40))
487 		return -ENOSYS;
488 
489 	/* check the sampling rate */
490 	for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
491 		if (format[2] == avc_stream_rate_table[i])
492 			break;
493 	}
494 	if (i == ARRAY_SIZE(avc_stream_rate_table))
495 		return -ENOSYS;
496 
497 	formation->rate = oxfw_rate_table[i];
498 
499 	for (e = 0; e < format[4]; e++) {
500 		channels = format[5 + e * 2];
501 		type = format[6 + e * 2];
502 
503 		switch (type) {
504 		/* IEC 60958 Conformant, currently handled as MBLA */
505 		case 0x00:
506 		/* Multi Bit Linear Audio (Raw) */
507 		case 0x06:
508 			formation->pcm += channels;
509 			break;
510 		/* MIDI Conformant */
511 		case 0x0d:
512 			formation->midi = channels;
513 			break;
514 		/* IEC 61937-3 to 7 */
515 		case 0x01:
516 		case 0x02:
517 		case 0x03:
518 		case 0x04:
519 		case 0x05:
520 		/* Multi Bit Linear Audio */
521 		case 0x07:	/* DVD-Audio */
522 		case 0x0c:	/* High Precision */
523 		/* One Bit Audio */
524 		case 0x08:	/* (Plain) Raw */
525 		case 0x09:	/* (Plain) SACD */
526 		case 0x0a:	/* (Encoded) Raw */
527 		case 0x0b:	/* (Encoded) SACD */
528 		/* SMPTE Time-Code conformant */
529 		case 0x0e:
530 		/* Sample Count */
531 		case 0x0f:
532 		/* Anciliary Data */
533 		case 0x10:
534 		/* Synchronization Stream (Stereo Raw audio) */
535 		case 0x40:
536 		/* Don't care */
537 		case 0xff:
538 		default:
539 			return -ENOSYS;	/* not supported */
540 		}
541 	}
542 
543 	if (formation->pcm  > AM824_MAX_CHANNELS_FOR_PCM ||
544 	    formation->midi > AM824_MAX_CHANNELS_FOR_MIDI)
545 		return -ENOSYS;
546 
547 	return 0;
548 }
549 
550 static int
551 assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
552 		      unsigned int pid, u8 *buf, unsigned int *len,
553 		      u8 **formats)
554 {
555 	struct snd_oxfw_stream_formation formation;
556 	unsigned int i, eid;
557 	int err;
558 
559 	/* get format at current sampling rate */
560 	err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
561 	if (err < 0) {
562 		dev_err(&oxfw->unit->device,
563 		"fail to get current stream format for isoc %s plug %d:%d\n",
564 			(dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
565 			pid, err);
566 		goto end;
567 	}
568 
569 	/* parse and set stream format */
570 	eid = 0;
571 	err = snd_oxfw_stream_parse_format(buf, &formation);
572 	if (err < 0)
573 		goto end;
574 
575 	formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
576 				    GFP_KERNEL);
577 	if (!formats[eid]) {
578 		err = -ENOMEM;
579 		goto end;
580 	}
581 
582 	/* apply the format for each available sampling rate */
583 	for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
584 		if (formation.rate == oxfw_rate_table[i])
585 			continue;
586 
587 		err = avc_general_inquiry_sig_fmt(oxfw->unit,
588 						  oxfw_rate_table[i],
589 						  dir, pid);
590 		if (err < 0)
591 			continue;
592 
593 		eid++;
594 		formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
595 					    GFP_KERNEL);
596 		if (formats[eid] == NULL) {
597 			err = -ENOMEM;
598 			goto end;
599 		}
600 		formats[eid][2] = avc_stream_rate_table[i];
601 	}
602 
603 	err = 0;
604 	oxfw->assumed = true;
605 end:
606 	return err;
607 }
608 
609 static int fill_stream_formats(struct snd_oxfw *oxfw,
610 			       enum avc_general_plug_dir dir,
611 			       unsigned short pid)
612 {
613 	u8 *buf, **formats;
614 	unsigned int len, eid = 0;
615 	struct snd_oxfw_stream_formation dummy;
616 	int err;
617 
618 	buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
619 	if (buf == NULL)
620 		return -ENOMEM;
621 
622 	if (dir == AVC_GENERAL_PLUG_DIR_OUT)
623 		formats = oxfw->tx_stream_formats;
624 	else
625 		formats = oxfw->rx_stream_formats;
626 
627 	/* get first entry */
628 	len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
629 	err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
630 	if (err == -ENOSYS) {
631 		/* LIST subfunction is not implemented */
632 		len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
633 		err = assume_stream_formats(oxfw, dir, pid, buf, &len,
634 					    formats);
635 		goto end;
636 	} else if (err < 0) {
637 		dev_err(&oxfw->unit->device,
638 			"fail to get stream format %d for isoc %s plug %d:%d\n",
639 			eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
640 			pid, err);
641 		goto end;
642 	}
643 
644 	/* LIST subfunction is implemented */
645 	while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
646 		/* The format is too short. */
647 		if (len < 3) {
648 			err = -EIO;
649 			break;
650 		}
651 
652 		/* parse and set stream format */
653 		err = snd_oxfw_stream_parse_format(buf, &dummy);
654 		if (err < 0)
655 			break;
656 
657 		formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, len,
658 					    GFP_KERNEL);
659 		if (!formats[eid]) {
660 			err = -ENOMEM;
661 			break;
662 		}
663 
664 		/* get next entry */
665 		len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
666 		err = avc_stream_get_format_list(oxfw->unit, dir, 0,
667 						 buf, &len, ++eid);
668 		/* No entries remained. */
669 		if (err == -EINVAL) {
670 			err = 0;
671 			break;
672 		} else if (err < 0) {
673 			dev_err(&oxfw->unit->device,
674 			"fail to get stream format %d for isoc %s plug %d:%d\n",
675 				eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
676 									"out",
677 				pid, err);
678 			break;
679 		}
680 	}
681 end:
682 	kfree(buf);
683 	return err;
684 }
685 
686 int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
687 {
688 	u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
689 	struct snd_oxfw_stream_formation formation;
690 	u8 *format;
691 	unsigned int i;
692 	int err;
693 
694 	/* the number of plugs for isoc in/out, ext in/out  */
695 	err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
696 	if (err < 0) {
697 		dev_err(&oxfw->unit->device,
698 		"fail to get info for isoc/external in/out plugs: %d\n",
699 			err);
700 		goto end;
701 	} else if ((plugs[0] == 0) && (plugs[1] == 0)) {
702 		err = -ENOSYS;
703 		goto end;
704 	}
705 
706 	/* use oPCR[0] if exists */
707 	if (plugs[1] > 0) {
708 		err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
709 		if (err < 0)
710 			goto end;
711 
712 		for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
713 			format = oxfw->tx_stream_formats[i];
714 			if (format == NULL)
715 				continue;
716 			err = snd_oxfw_stream_parse_format(format, &formation);
717 			if (err < 0)
718 				continue;
719 
720 			/* Add one MIDI port. */
721 			if (formation.midi > 0)
722 				oxfw->midi_input_ports = 1;
723 		}
724 
725 		oxfw->has_output = true;
726 	}
727 
728 	/* use iPCR[0] if exists */
729 	if (plugs[0] > 0) {
730 		err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
731 		if (err < 0)
732 			goto end;
733 
734 		for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
735 			format = oxfw->rx_stream_formats[i];
736 			if (format == NULL)
737 				continue;
738 			err = snd_oxfw_stream_parse_format(format, &formation);
739 			if (err < 0)
740 				continue;
741 
742 			/* Add one MIDI port. */
743 			if (formation.midi > 0)
744 				oxfw->midi_output_ports = 1;
745 		}
746 	}
747 end:
748 	return err;
749 }
750 
751 void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
752 {
753 	oxfw->dev_lock_changed = true;
754 	wake_up(&oxfw->hwdep_wait);
755 }
756 
757 int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
758 {
759 	int err;
760 
761 	spin_lock_irq(&oxfw->lock);
762 
763 	/* user land lock this */
764 	if (oxfw->dev_lock_count < 0) {
765 		err = -EBUSY;
766 		goto end;
767 	}
768 
769 	/* this is the first time */
770 	if (oxfw->dev_lock_count++ == 0)
771 		snd_oxfw_stream_lock_changed(oxfw);
772 	err = 0;
773 end:
774 	spin_unlock_irq(&oxfw->lock);
775 	return err;
776 }
777 
778 void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
779 {
780 	spin_lock_irq(&oxfw->lock);
781 
782 	if (WARN_ON(oxfw->dev_lock_count <= 0))
783 		goto end;
784 	if (--oxfw->dev_lock_count == 0)
785 		snd_oxfw_stream_lock_changed(oxfw);
786 end:
787 	spin_unlock_irq(&oxfw->lock);
788 }
789