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