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