1 /*
2  * bebob_stream.c - a part of driver for BeBoB based devices
3  *
4  * Copyright (c) 2013-2014 Takashi Sakamoto
5  *
6  * Licensed under the terms of the GNU General Public License, version 2.
7  */
8 
9 #include "./bebob.h"
10 
11 #define CALLBACK_TIMEOUT	2000
12 #define FW_ISO_RESOURCE_DELAY	1000
13 
14 /*
15  * NOTE;
16  * For BeBoB streams, Both of input and output CMP connection are important.
17  *
18  * For most devices, each CMP connection starts to transmit/receive a
19  * corresponding stream. But for a few devices, both of CMP connection needs
20  * to start transmitting stream. An example is 'M-Audio Firewire 410'.
21  */
22 
23 /* 128 is an arbitrary length but it seems to be enough */
24 #define FORMAT_MAXIMUM_LENGTH 128
25 
26 const unsigned int snd_bebob_rate_table[SND_BEBOB_STRM_FMT_ENTRIES] = {
27 	[0] = 32000,
28 	[1] = 44100,
29 	[2] = 48000,
30 	[3] = 88200,
31 	[4] = 96000,
32 	[5] = 176400,
33 	[6] = 192000,
34 };
35 
36 /*
37  * See: Table 51: Extended Stream Format Info ‘Sampling Frequency’
38  * in Additional AVC commands (Nov 2003, BridgeCo)
39  */
40 static const unsigned int bridgeco_freq_table[] = {
41 	[0] = 0x02,
42 	[1] = 0x03,
43 	[2] = 0x04,
44 	[3] = 0x0a,
45 	[4] = 0x05,
46 	[5] = 0x06,
47 	[6] = 0x07,
48 };
49 
50 static int
51 get_formation_index(unsigned int rate, unsigned int *index)
52 {
53 	unsigned int i;
54 
55 	for (i = 0; i < ARRAY_SIZE(snd_bebob_rate_table); i++) {
56 		if (snd_bebob_rate_table[i] == rate) {
57 			*index = i;
58 			return 0;
59 		}
60 	}
61 	return -EINVAL;
62 }
63 
64 int
65 snd_bebob_stream_get_rate(struct snd_bebob *bebob, unsigned int *curr_rate)
66 {
67 	unsigned int tx_rate, rx_rate, trials;
68 	int err;
69 
70 	trials = 0;
71 	do {
72 		err = avc_general_get_sig_fmt(bebob->unit, &tx_rate,
73 					      AVC_GENERAL_PLUG_DIR_OUT, 0);
74 	} while (err == -EAGAIN && ++trials < 3);
75 	if (err < 0)
76 		goto end;
77 
78 	trials = 0;
79 	do {
80 		err = avc_general_get_sig_fmt(bebob->unit, &rx_rate,
81 					      AVC_GENERAL_PLUG_DIR_IN, 0);
82 	} while (err == -EAGAIN && ++trials < 3);
83 	if (err < 0)
84 		goto end;
85 
86 	*curr_rate = rx_rate;
87 	if (rx_rate == tx_rate)
88 		goto end;
89 
90 	/* synchronize receive stream rate to transmit stream rate */
91 	err = avc_general_set_sig_fmt(bebob->unit, rx_rate,
92 				      AVC_GENERAL_PLUG_DIR_IN, 0);
93 end:
94 	return err;
95 }
96 
97 int
98 snd_bebob_stream_set_rate(struct snd_bebob *bebob, unsigned int rate)
99 {
100 	int err;
101 
102 	err = avc_general_set_sig_fmt(bebob->unit, rate,
103 				      AVC_GENERAL_PLUG_DIR_OUT, 0);
104 	if (err < 0)
105 		goto end;
106 
107 	err = avc_general_set_sig_fmt(bebob->unit, rate,
108 				      AVC_GENERAL_PLUG_DIR_IN, 0);
109 	if (err < 0)
110 		goto end;
111 
112 	/*
113 	 * Some devices need a bit time for transition.
114 	 * 300msec is got by some experiments.
115 	 */
116 	msleep(300);
117 end:
118 	return err;
119 }
120 
121 int snd_bebob_stream_get_clock_src(struct snd_bebob *bebob,
122 				   enum snd_bebob_clock_type *src)
123 {
124 	const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
125 	u8 addr[AVC_BRIDGECO_ADDR_BYTES], input[7];
126 	unsigned int id;
127 	enum avc_bridgeco_plug_type type;
128 	int err = 0;
129 
130 	/* 1.The device has its own operation to switch source of clock */
131 	if (clk_spec) {
132 		err = clk_spec->get(bebob, &id);
133 		if (err < 0) {
134 			dev_err(&bebob->unit->device,
135 				"fail to get clock source: %d\n", err);
136 			goto end;
137 		}
138 
139 		if (id >= clk_spec->num) {
140 			dev_err(&bebob->unit->device,
141 				"clock source %d out of range 0..%d\n",
142 				id, clk_spec->num - 1);
143 			err = -EIO;
144 			goto end;
145 		}
146 
147 		*src = clk_spec->types[id];
148 		goto end;
149 	}
150 
151 	/*
152 	 * 2.The device don't support to switch source of clock then assumed
153 	 *   to use internal clock always
154 	 */
155 	if (bebob->sync_input_plug < 0) {
156 		*src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
157 		goto end;
158 	}
159 
160 	/*
161 	 * 3.The device supports to switch source of clock by an usual way.
162 	 *   Let's check input for 'Music Sub Unit Sync Input' plug.
163 	 */
164 	avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
165 				   bebob->sync_input_plug);
166 	err = avc_bridgeco_get_plug_input(bebob->unit, addr, input);
167 	if (err < 0) {
168 		dev_err(&bebob->unit->device,
169 			"fail to get an input for MSU in plug %d: %d\n",
170 			bebob->sync_input_plug, err);
171 		goto end;
172 	}
173 
174 	/*
175 	 * If there are no input plugs, all of fields are 0xff.
176 	 * Here check the first field. This field is used for direction.
177 	 */
178 	if (input[0] == 0xff) {
179 		*src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
180 		goto end;
181 	}
182 
183 	/* The source from any output plugs is for one purpose only. */
184 	if (input[0] == AVC_BRIDGECO_PLUG_DIR_OUT) {
185 		/*
186 		 * In BeBoB architecture, the source from music subunit may
187 		 * bypass from oPCR[0]. This means that this source gives
188 		 * synchronization to IEEE 1394 cycle start packet.
189 		 */
190 		if (input[1] == AVC_BRIDGECO_PLUG_MODE_SUBUNIT &&
191 		    input[2] == 0x0c) {
192 			*src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
193 			goto end;
194 		}
195 	/* The source from any input units is for several purposes. */
196 	} else if (input[1] == AVC_BRIDGECO_PLUG_MODE_UNIT) {
197 		if (input[2] == AVC_BRIDGECO_PLUG_UNIT_ISOC) {
198 			if (input[3] == 0x00) {
199 				/*
200 				 * This source comes from iPCR[0]. This means
201 				 * that presentation timestamp calculated by
202 				 * SYT series of the received packets. In
203 				 * short, this driver is the master of
204 				 * synchronization.
205 				 */
206 				*src = SND_BEBOB_CLOCK_TYPE_SYT;
207 				goto end;
208 			} else {
209 				/*
210 				 * This source comes from iPCR[1-29]. This
211 				 * means that the synchronization stream is not
212 				 * the Audio/MIDI compound stream.
213 				 */
214 				*src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
215 				goto end;
216 			}
217 		} else if (input[2] == AVC_BRIDGECO_PLUG_UNIT_EXT) {
218 			/* Check type of this plug.  */
219 			avc_bridgeco_fill_unit_addr(addr,
220 						    AVC_BRIDGECO_PLUG_DIR_IN,
221 						    AVC_BRIDGECO_PLUG_UNIT_EXT,
222 						    input[3]);
223 			err = avc_bridgeco_get_plug_type(bebob->unit, addr,
224 							 &type);
225 			if (err < 0)
226 				goto end;
227 
228 			if (type == AVC_BRIDGECO_PLUG_TYPE_DIG) {
229 				/*
230 				 * SPDIF/ADAT or sometimes (not always) word
231 				 * clock.
232 				 */
233 				*src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
234 				goto end;
235 			} else if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
236 				/* Often word clock. */
237 				*src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
238 				goto end;
239 			} else if (type == AVC_BRIDGECO_PLUG_TYPE_ADDITION) {
240 				/*
241 				 * Not standard.
242 				 * Mostly, additional internal clock.
243 				 */
244 				*src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
245 				goto end;
246 			}
247 		}
248 	}
249 
250 	/* Not supported. */
251 	err = -EIO;
252 end:
253 	return err;
254 }
255 
256 static unsigned int
257 map_data_channels(struct snd_bebob *bebob, struct amdtp_stream *s)
258 {
259 	unsigned int sec, sections, ch, channels;
260 	unsigned int pcm, midi, location;
261 	unsigned int stm_pos, sec_loc, pos;
262 	u8 *buf, addr[AVC_BRIDGECO_ADDR_BYTES], type;
263 	enum avc_bridgeco_plug_dir dir;
264 	int err;
265 
266 	/*
267 	 * The length of return value of this command cannot be expected. Here
268 	 * use the maximum length of FCP.
269 	 */
270 	buf = kzalloc(256, GFP_KERNEL);
271 	if (buf == NULL)
272 		return -ENOMEM;
273 
274 	if (s == &bebob->tx_stream)
275 		dir = AVC_BRIDGECO_PLUG_DIR_OUT;
276 	else
277 		dir = AVC_BRIDGECO_PLUG_DIR_IN;
278 
279 	avc_bridgeco_fill_unit_addr(addr, dir, AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
280 	err = avc_bridgeco_get_plug_ch_pos(bebob->unit, addr, buf, 256);
281 	if (err < 0) {
282 		dev_err(&bebob->unit->device,
283 			"fail to get channel position for isoc %s plug 0: %d\n",
284 			(dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" : "out",
285 			err);
286 		goto end;
287 	}
288 	pos = 0;
289 
290 	/* positions in I/O buffer */
291 	pcm = 0;
292 	midi = 0;
293 
294 	/* the number of sections in AMDTP packet */
295 	sections = buf[pos++];
296 
297 	for (sec = 0; sec < sections; sec++) {
298 		/* type of this section */
299 		avc_bridgeco_fill_unit_addr(addr, dir,
300 					    AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
301 		err = avc_bridgeco_get_plug_section_type(bebob->unit, addr,
302 							 sec, &type);
303 		if (err < 0) {
304 			dev_err(&bebob->unit->device,
305 			"fail to get section type for isoc %s plug 0: %d\n",
306 				(dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
307 								    "out",
308 				err);
309 			goto end;
310 		}
311 		/* NoType */
312 		if (type == 0xff) {
313 			err = -ENOSYS;
314 			goto end;
315 		}
316 
317 		/* the number of channels in this section */
318 		channels = buf[pos++];
319 
320 		for (ch = 0; ch < channels; ch++) {
321 			/* position of this channel in AMDTP packet */
322 			stm_pos = buf[pos++] - 1;
323 			/* location of this channel in this section */
324 			sec_loc = buf[pos++] - 1;
325 
326 			/*
327 			 * Basically the number of location is within the
328 			 * number of channels in this section. But some models
329 			 * of M-Audio don't follow this. Its location for MIDI
330 			 * is the position of MIDI channels in AMDTP packet.
331 			 */
332 			if (sec_loc >= channels)
333 				sec_loc = ch;
334 
335 			switch (type) {
336 			/* for MIDI conformant data channel */
337 			case 0x0a:
338 				/* AMDTP_MAX_CHANNELS_FOR_MIDI is 1. */
339 				if ((midi > 0) && (stm_pos != midi)) {
340 					err = -ENOSYS;
341 					goto end;
342 				}
343 				amdtp_am824_set_midi_position(s, stm_pos);
344 				midi = stm_pos;
345 				break;
346 			/* for PCM data channel */
347 			case 0x01:	/* Headphone */
348 			case 0x02:	/* Microphone */
349 			case 0x03:	/* Line */
350 			case 0x04:	/* SPDIF */
351 			case 0x05:	/* ADAT */
352 			case 0x06:	/* TDIF */
353 			case 0x07:	/* MADI */
354 			/* for undefined/changeable signal  */
355 			case 0x08:	/* Analog */
356 			case 0x09:	/* Digital */
357 			default:
358 				location = pcm + sec_loc;
359 				if (location >= AM824_MAX_CHANNELS_FOR_PCM) {
360 					err = -ENOSYS;
361 					goto end;
362 				}
363 				amdtp_am824_set_pcm_position(s, location,
364 							     stm_pos);
365 				break;
366 			}
367 		}
368 
369 		if (type != 0x0a)
370 			pcm += channels;
371 		else
372 			midi += channels;
373 	}
374 end:
375 	kfree(buf);
376 	return err;
377 }
378 
379 static int
380 check_connection_used_by_others(struct snd_bebob *bebob, struct amdtp_stream *s)
381 {
382 	struct cmp_connection *conn;
383 	bool used;
384 	int err;
385 
386 	if (s == &bebob->tx_stream)
387 		conn = &bebob->out_conn;
388 	else
389 		conn = &bebob->in_conn;
390 
391 	err = cmp_connection_check_used(conn, &used);
392 	if ((err >= 0) && used && !amdtp_stream_running(s)) {
393 		dev_err(&bebob->unit->device,
394 			"Connection established by others: %cPCR[%d]\n",
395 			(conn->direction == CMP_OUTPUT) ? 'o' : 'i',
396 			conn->pcr_index);
397 		err = -EBUSY;
398 	}
399 
400 	return err;
401 }
402 
403 static int make_both_connections(struct snd_bebob *bebob)
404 {
405 	int err = 0;
406 
407 	err = cmp_connection_establish(&bebob->out_conn);
408 	if (err < 0)
409 		return err;
410 
411 	err = cmp_connection_establish(&bebob->in_conn);
412 	if (err < 0) {
413 		cmp_connection_break(&bebob->out_conn);
414 		return err;
415 	}
416 
417 	return 0;
418 }
419 
420 static void
421 break_both_connections(struct snd_bebob *bebob)
422 {
423 	cmp_connection_break(&bebob->in_conn);
424 	cmp_connection_break(&bebob->out_conn);
425 
426 	/* These models seems to be in transition state for a longer time. */
427 	if (bebob->maudio_special_quirk != NULL)
428 		msleep(200);
429 }
430 
431 static int
432 start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream)
433 {
434 	struct cmp_connection *conn;
435 	int err = 0;
436 
437 	if (stream == &bebob->rx_stream)
438 		conn = &bebob->in_conn;
439 	else
440 		conn = &bebob->out_conn;
441 
442 	/* channel mapping */
443 	if (bebob->maudio_special_quirk == NULL) {
444 		err = map_data_channels(bebob, stream);
445 		if (err < 0)
446 			goto end;
447 	}
448 
449 	/* start amdtp stream */
450 	err = amdtp_stream_start(stream,
451 				 conn->resources.channel,
452 				 conn->speed);
453 end:
454 	return err;
455 }
456 
457 static int init_stream(struct snd_bebob *bebob, struct amdtp_stream *stream)
458 {
459 	enum amdtp_stream_direction dir_stream;
460 	struct cmp_connection *conn;
461 	enum cmp_direction dir_conn;
462 	int err;
463 
464 	if (stream == &bebob->tx_stream) {
465 		dir_stream = AMDTP_IN_STREAM;
466 		conn = &bebob->out_conn;
467 		dir_conn = CMP_OUTPUT;
468 	} else {
469 		dir_stream = AMDTP_OUT_STREAM;
470 		conn = &bebob->in_conn;
471 		dir_conn = CMP_INPUT;
472 	}
473 
474 	err = cmp_connection_init(conn, bebob->unit, dir_conn, 0);
475 	if (err < 0)
476 		return err;
477 
478 	err = amdtp_am824_init(stream, bebob->unit, dir_stream, CIP_BLOCKING);
479 	if (err < 0) {
480 		cmp_connection_destroy(conn);
481 		return err;
482 	}
483 
484 	if (stream == &bebob->tx_stream) {
485 		// BeBoB v3 transfers packets with these qurks:
486 		//  - In the beginning of streaming, the value of dbc is
487 		//    incremented even if no data blocks are transferred.
488 		//  - The value of dbc is reset suddenly.
489 		if (bebob->version > 2)
490 			bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC |
491 						  CIP_SKIP_DBC_ZERO_CHECK;
492 
493 		// At high sampling rate, M-Audio special firmware transmits
494 		// empty packet with the value of dbc incremented by 8 but the
495 		// others are valid to IEC 61883-1.
496 		if (bebob->maudio_special_quirk)
497 			bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC;
498 	}
499 
500 	return 0;
501 }
502 
503 static void destroy_stream(struct snd_bebob *bebob, struct amdtp_stream *stream)
504 {
505 	amdtp_stream_destroy(stream);
506 
507 	if (stream == &bebob->tx_stream)
508 		cmp_connection_destroy(&bebob->out_conn);
509 	else
510 		cmp_connection_destroy(&bebob->in_conn);
511 }
512 
513 int snd_bebob_stream_init_duplex(struct snd_bebob *bebob)
514 {
515 	int err;
516 
517 	err = init_stream(bebob, &bebob->tx_stream);
518 	if (err < 0)
519 		return err;
520 
521 	err = init_stream(bebob, &bebob->rx_stream);
522 	if (err < 0) {
523 		destroy_stream(bebob, &bebob->tx_stream);
524 		return err;
525 	}
526 
527 	return 0;
528 }
529 
530 static int keep_resources(struct snd_bebob *bebob, struct amdtp_stream *stream,
531 			  unsigned int rate, unsigned int index)
532 {
533 	struct snd_bebob_stream_formation *formation;
534 	struct cmp_connection *conn;
535 	int err;
536 
537 	if (stream == &bebob->tx_stream) {
538 		formation = bebob->tx_stream_formations + index;
539 		conn = &bebob->out_conn;
540 	} else {
541 		formation = bebob->rx_stream_formations + index;
542 		conn = &bebob->in_conn;
543 	}
544 
545 	err = amdtp_am824_set_parameters(stream, rate, formation->pcm,
546 					 formation->midi, false);
547 	if (err < 0)
548 		return err;
549 
550 	return cmp_connection_reserve(conn, amdtp_stream_get_max_payload(stream));
551 }
552 
553 int snd_bebob_stream_reserve_duplex(struct snd_bebob *bebob, unsigned int rate)
554 {
555 	unsigned int curr_rate;
556 	int err;
557 
558 	// Considering JACK/FFADO streaming:
559 	// TODO: This can be removed hwdep functionality becomes popular.
560 	err = check_connection_used_by_others(bebob, &bebob->rx_stream);
561 	if (err < 0)
562 		return err;
563 
564 	err = bebob->spec->rate->get(bebob, &curr_rate);
565 	if (err < 0)
566 		return err;
567 	if (rate == 0)
568 		rate = curr_rate;
569 	if (curr_rate != rate) {
570 		amdtp_stream_stop(&bebob->tx_stream);
571 		amdtp_stream_stop(&bebob->rx_stream);
572 
573 		break_both_connections(bebob);
574 
575 		cmp_connection_release(&bebob->out_conn);
576 		cmp_connection_release(&bebob->in_conn);
577 	}
578 
579 	if (bebob->substreams_counter == 0 || curr_rate != rate) {
580 		unsigned int index;
581 
582 		// NOTE:
583 		// If establishing connections at first, Yamaha GO46
584 		// (and maybe Terratec X24) don't generate sound.
585 		//
586 		// For firmware customized by M-Audio, refer to next NOTE.
587 		err = bebob->spec->rate->set(bebob, rate);
588 		if (err < 0) {
589 			dev_err(&bebob->unit->device,
590 				"fail to set sampling rate: %d\n",
591 				err);
592 			return err;
593 		}
594 
595 		err = get_formation_index(rate, &index);
596 		if (err < 0)
597 			return err;
598 
599 		err = keep_resources(bebob, &bebob->tx_stream, rate, index);
600 		if (err < 0)
601 			return err;
602 
603 		err = keep_resources(bebob, &bebob->rx_stream, rate, index);
604 		if (err < 0) {
605 			cmp_connection_release(&bebob->out_conn);
606 			return err;
607 		}
608 	}
609 
610 	return 0;
611 }
612 
613 int snd_bebob_stream_start_duplex(struct snd_bebob *bebob)
614 {
615 	int err;
616 
617 	// Need no substreams.
618 	if (bebob->substreams_counter == 0)
619 		return -EIO;
620 
621 	// packet queueing error or detecting discontinuity
622 	if (amdtp_streaming_error(&bebob->rx_stream) ||
623 	    amdtp_streaming_error(&bebob->tx_stream)) {
624 		amdtp_stream_stop(&bebob->rx_stream);
625 		amdtp_stream_stop(&bebob->tx_stream);
626 
627 		break_both_connections(bebob);
628 	}
629 
630 	if (!amdtp_stream_running(&bebob->rx_stream)) {
631 		unsigned int curr_rate;
632 
633 		if (bebob->maudio_special_quirk) {
634 			err = bebob->spec->rate->get(bebob, &curr_rate);
635 			if (err < 0)
636 				return err;
637 		}
638 
639 		err = make_both_connections(bebob);
640 		if (err < 0)
641 			return err;
642 
643 		err = start_stream(bebob, &bebob->rx_stream);
644 		if (err < 0) {
645 			dev_err(&bebob->unit->device,
646 				"fail to run AMDTP master stream:%d\n", err);
647 			goto error;
648 		}
649 
650 		// NOTE:
651 		// The firmware customized by M-Audio uses these commands to
652 		// start transmitting stream. This is not usual way.
653 		if (bebob->maudio_special_quirk) {
654 			err = bebob->spec->rate->set(bebob, curr_rate);
655 			if (err < 0) {
656 				dev_err(&bebob->unit->device,
657 					"fail to ensure sampling rate: %d\n",
658 					err);
659 				goto error;
660 			}
661 		}
662 
663 		if (!amdtp_stream_wait_callback(&bebob->rx_stream,
664 						CALLBACK_TIMEOUT)) {
665 			err = -ETIMEDOUT;
666 			goto error;
667 		}
668 	}
669 
670 	if (!amdtp_stream_running(&bebob->tx_stream)) {
671 		err = start_stream(bebob, &bebob->tx_stream);
672 		if (err < 0) {
673 			dev_err(&bebob->unit->device,
674 				"fail to run AMDTP slave stream:%d\n", err);
675 			goto error;
676 		}
677 
678 		if (!amdtp_stream_wait_callback(&bebob->tx_stream,
679 						CALLBACK_TIMEOUT)) {
680 			err = -ETIMEDOUT;
681 			goto error;
682 		}
683 	}
684 
685 	return 0;
686 error:
687 	amdtp_stream_stop(&bebob->tx_stream);
688 	amdtp_stream_stop(&bebob->rx_stream);
689 	break_both_connections(bebob);
690 	return err;
691 }
692 
693 void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob)
694 {
695 	if (bebob->substreams_counter == 0) {
696 		amdtp_stream_stop(&bebob->rx_stream);
697 		amdtp_stream_stop(&bebob->tx_stream);
698 
699 		break_both_connections(bebob);
700 
701 		cmp_connection_release(&bebob->out_conn);
702 		cmp_connection_release(&bebob->in_conn);
703 	}
704 }
705 
706 /*
707  * This function should be called before starting streams or after stopping
708  * streams.
709  */
710 void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob)
711 {
712 	destroy_stream(bebob, &bebob->tx_stream);
713 	destroy_stream(bebob, &bebob->rx_stream);
714 }
715 
716 /*
717  * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’
718  * in Additional AVC commands (Nov 2003, BridgeCo)
719  * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
720  */
721 static int
722 parse_stream_formation(u8 *buf, unsigned int len,
723 		       struct snd_bebob_stream_formation *formation)
724 {
725 	unsigned int i, e, channels, format;
726 
727 	/*
728 	 * this module can support a hierarchy combination that:
729 	 *  Root:	Audio and Music (0x90)
730 	 *  Level 1:	AM824 Compound  (0x40)
731 	 */
732 	if ((buf[0] != 0x90) || (buf[1] != 0x40))
733 		return -ENOSYS;
734 
735 	/* check sampling rate */
736 	for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) {
737 		if (buf[2] == bridgeco_freq_table[i])
738 			break;
739 	}
740 	if (i == ARRAY_SIZE(bridgeco_freq_table))
741 		return -ENOSYS;
742 
743 	/* Avoid double count by different entries for the same rate. */
744 	memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation));
745 
746 	for (e = 0; e < buf[4]; e++) {
747 		channels = buf[5 + e * 2];
748 		format = buf[6 + e * 2];
749 
750 		switch (format) {
751 		/* IEC 60958 Conformant, currently handled as MBLA */
752 		case 0x00:
753 		/* Multi bit linear audio */
754 		case 0x06:	/* Raw */
755 			formation[i].pcm += channels;
756 			break;
757 		/* MIDI Conformant */
758 		case 0x0d:
759 			formation[i].midi += channels;
760 			break;
761 		/* IEC 61937-3 to 7 */
762 		case 0x01:
763 		case 0x02:
764 		case 0x03:
765 		case 0x04:
766 		case 0x05:
767 		/* Multi bit linear audio */
768 		case 0x07:	/* DVD-Audio */
769 		case 0x0c:	/* High Precision */
770 		/* One Bit Audio */
771 		case 0x08:	/* (Plain) Raw */
772 		case 0x09:	/* (Plain) SACD */
773 		case 0x0a:	/* (Encoded) Raw */
774 		case 0x0b:	/* (Encoded) SACD */
775 		/* Synchronization Stream (Stereo Raw audio) */
776 		case 0x40:
777 		/* Don't care */
778 		case 0xff:
779 		default:
780 			return -ENOSYS;	/* not supported */
781 		}
782 	}
783 
784 	if (formation[i].pcm  > AM824_MAX_CHANNELS_FOR_PCM ||
785 	    formation[i].midi > AM824_MAX_CHANNELS_FOR_MIDI)
786 		return -ENOSYS;
787 
788 	return 0;
789 }
790 
791 static int
792 fill_stream_formations(struct snd_bebob *bebob, enum avc_bridgeco_plug_dir dir,
793 		       unsigned short pid)
794 {
795 	u8 *buf;
796 	struct snd_bebob_stream_formation *formations;
797 	unsigned int len, eid;
798 	u8 addr[AVC_BRIDGECO_ADDR_BYTES];
799 	int err;
800 
801 	buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL);
802 	if (buf == NULL)
803 		return -ENOMEM;
804 
805 	if (dir == AVC_BRIDGECO_PLUG_DIR_IN)
806 		formations = bebob->rx_stream_formations;
807 	else
808 		formations = bebob->tx_stream_formations;
809 
810 	for (eid = 0; eid < SND_BEBOB_STRM_FMT_ENTRIES; eid++) {
811 		len = FORMAT_MAXIMUM_LENGTH;
812 		avc_bridgeco_fill_unit_addr(addr, dir,
813 					    AVC_BRIDGECO_PLUG_UNIT_ISOC, pid);
814 		err = avc_bridgeco_get_plug_strm_fmt(bebob->unit, addr, buf,
815 						     &len, eid);
816 		/* No entries remained. */
817 		if (err == -EINVAL && eid > 0) {
818 			err = 0;
819 			break;
820 		} else if (err < 0) {
821 			dev_err(&bebob->unit->device,
822 			"fail to get stream format %d for isoc %s plug %d:%d\n",
823 				eid,
824 				(dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
825 								    "out",
826 				pid, err);
827 			break;
828 		}
829 
830 		err = parse_stream_formation(buf, len, formations);
831 		if (err < 0)
832 			break;
833 	}
834 
835 	kfree(buf);
836 	return err;
837 }
838 
839 static int
840 seek_msu_sync_input_plug(struct snd_bebob *bebob)
841 {
842 	u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
843 	unsigned int i;
844 	enum avc_bridgeco_plug_type type;
845 	int err;
846 
847 	/* Get the number of Music Sub Unit for both direction. */
848 	err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs);
849 	if (err < 0) {
850 		dev_err(&bebob->unit->device,
851 			"fail to get info for MSU in/out plugs: %d\n",
852 			err);
853 		goto end;
854 	}
855 
856 	/* seek destination plugs for 'MSU sync input' */
857 	bebob->sync_input_plug = -1;
858 	for (i = 0; i < plugs[0]; i++) {
859 		avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, i);
860 		err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
861 		if (err < 0) {
862 			dev_err(&bebob->unit->device,
863 				"fail to get type for MSU in plug %d: %d\n",
864 				i, err);
865 			goto end;
866 		}
867 
868 		if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
869 			bebob->sync_input_plug = i;
870 			break;
871 		}
872 	}
873 end:
874 	return err;
875 }
876 
877 int snd_bebob_stream_discover(struct snd_bebob *bebob)
878 {
879 	const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
880 	u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
881 	enum avc_bridgeco_plug_type type;
882 	unsigned int i;
883 	int err;
884 
885 	/* the number of plugs for isoc in/out, ext in/out  */
886 	err = avc_general_get_plug_info(bebob->unit, 0x1f, 0x07, 0x00, plugs);
887 	if (err < 0) {
888 		dev_err(&bebob->unit->device,
889 		"fail to get info for isoc/external in/out plugs: %d\n",
890 			err);
891 		goto end;
892 	}
893 
894 	/*
895 	 * This module supports at least one isoc input plug and one isoc
896 	 * output plug.
897 	 */
898 	if ((plugs[0] == 0) || (plugs[1] == 0)) {
899 		err = -ENOSYS;
900 		goto end;
901 	}
902 
903 	avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
904 				    AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
905 	err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
906 	if (err < 0) {
907 		dev_err(&bebob->unit->device,
908 			"fail to get type for isoc in plug 0: %d\n", err);
909 		goto end;
910 	} else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
911 		err = -ENOSYS;
912 		goto end;
913 	}
914 	err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_IN, 0);
915 	if (err < 0)
916 		goto end;
917 
918 	avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
919 				    AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
920 	err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
921 	if (err < 0) {
922 		dev_err(&bebob->unit->device,
923 			"fail to get type for isoc out plug 0: %d\n", err);
924 		goto end;
925 	} else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
926 		err = -ENOSYS;
927 		goto end;
928 	}
929 	err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_OUT, 0);
930 	if (err < 0)
931 		goto end;
932 
933 	/* count external input plugs for MIDI */
934 	bebob->midi_input_ports = 0;
935 	for (i = 0; i < plugs[2]; i++) {
936 		avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
937 					    AVC_BRIDGECO_PLUG_UNIT_EXT, i);
938 		err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
939 		if (err < 0) {
940 			dev_err(&bebob->unit->device,
941 			"fail to get type for external in plug %d: %d\n",
942 				i, err);
943 			goto end;
944 		} else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
945 			bebob->midi_input_ports++;
946 		}
947 	}
948 
949 	/* count external output plugs for MIDI */
950 	bebob->midi_output_ports = 0;
951 	for (i = 0; i < plugs[3]; i++) {
952 		avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
953 					    AVC_BRIDGECO_PLUG_UNIT_EXT, i);
954 		err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
955 		if (err < 0) {
956 			dev_err(&bebob->unit->device,
957 			"fail to get type for external out plug %d: %d\n",
958 				i, err);
959 			goto end;
960 		} else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
961 			bebob->midi_output_ports++;
962 		}
963 	}
964 
965 	/* for check source of clock later */
966 	if (!clk_spec)
967 		err = seek_msu_sync_input_plug(bebob);
968 end:
969 	return err;
970 }
971 
972 void snd_bebob_stream_lock_changed(struct snd_bebob *bebob)
973 {
974 	bebob->dev_lock_changed = true;
975 	wake_up(&bebob->hwdep_wait);
976 }
977 
978 int snd_bebob_stream_lock_try(struct snd_bebob *bebob)
979 {
980 	int err;
981 
982 	spin_lock_irq(&bebob->lock);
983 
984 	/* user land lock this */
985 	if (bebob->dev_lock_count < 0) {
986 		err = -EBUSY;
987 		goto end;
988 	}
989 
990 	/* this is the first time */
991 	if (bebob->dev_lock_count++ == 0)
992 		snd_bebob_stream_lock_changed(bebob);
993 	err = 0;
994 end:
995 	spin_unlock_irq(&bebob->lock);
996 	return err;
997 }
998 
999 void snd_bebob_stream_lock_release(struct snd_bebob *bebob)
1000 {
1001 	spin_lock_irq(&bebob->lock);
1002 
1003 	if (WARN_ON(bebob->dev_lock_count <= 0))
1004 		goto end;
1005 	if (--bebob->dev_lock_count == 0)
1006 		snd_bebob_stream_lock_changed(bebob);
1007 end:
1008 	spin_unlock_irq(&bebob->lock);
1009 }
1010