xref: /linux/sound/firewire/bebob/bebob_stream.c (revision 52338415)
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 int map_data_channels(struct snd_bebob *bebob, struct amdtp_stream *s)
256 {
257 	unsigned int sec, sections, ch, channels;
258 	unsigned int pcm, midi, location;
259 	unsigned int stm_pos, sec_loc, pos;
260 	u8 *buf, addr[AVC_BRIDGECO_ADDR_BYTES], type;
261 	enum avc_bridgeco_plug_dir dir;
262 	int err;
263 
264 	/*
265 	 * The length of return value of this command cannot be expected. Here
266 	 * use the maximum length of FCP.
267 	 */
268 	buf = kzalloc(256, GFP_KERNEL);
269 	if (buf == NULL)
270 		return -ENOMEM;
271 
272 	if (s == &bebob->tx_stream)
273 		dir = AVC_BRIDGECO_PLUG_DIR_OUT;
274 	else
275 		dir = AVC_BRIDGECO_PLUG_DIR_IN;
276 
277 	avc_bridgeco_fill_unit_addr(addr, dir, AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
278 	err = avc_bridgeco_get_plug_ch_pos(bebob->unit, addr, buf, 256);
279 	if (err < 0) {
280 		dev_err(&bebob->unit->device,
281 			"fail to get channel position for isoc %s plug 0: %d\n",
282 			(dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" : "out",
283 			err);
284 		goto end;
285 	}
286 	pos = 0;
287 
288 	/* positions in I/O buffer */
289 	pcm = 0;
290 	midi = 0;
291 
292 	/* the number of sections in AMDTP packet */
293 	sections = buf[pos++];
294 
295 	for (sec = 0; sec < sections; sec++) {
296 		/* type of this section */
297 		avc_bridgeco_fill_unit_addr(addr, dir,
298 					    AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
299 		err = avc_bridgeco_get_plug_section_type(bebob->unit, addr,
300 							 sec, &type);
301 		if (err < 0) {
302 			dev_err(&bebob->unit->device,
303 			"fail to get section type for isoc %s plug 0: %d\n",
304 				(dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
305 								    "out",
306 				err);
307 			goto end;
308 		}
309 		/* NoType */
310 		if (type == 0xff) {
311 			err = -ENOSYS;
312 			goto end;
313 		}
314 
315 		/* the number of channels in this section */
316 		channels = buf[pos++];
317 
318 		for (ch = 0; ch < channels; ch++) {
319 			/* position of this channel in AMDTP packet */
320 			stm_pos = buf[pos++] - 1;
321 			/* location of this channel in this section */
322 			sec_loc = buf[pos++] - 1;
323 
324 			/*
325 			 * Basically the number of location is within the
326 			 * number of channels in this section. But some models
327 			 * of M-Audio don't follow this. Its location for MIDI
328 			 * is the position of MIDI channels in AMDTP packet.
329 			 */
330 			if (sec_loc >= channels)
331 				sec_loc = ch;
332 
333 			switch (type) {
334 			/* for MIDI conformant data channel */
335 			case 0x0a:
336 				/* AMDTP_MAX_CHANNELS_FOR_MIDI is 1. */
337 				if ((midi > 0) && (stm_pos != midi)) {
338 					err = -ENOSYS;
339 					goto end;
340 				}
341 				amdtp_am824_set_midi_position(s, stm_pos);
342 				midi = stm_pos;
343 				break;
344 			/* for PCM data channel */
345 			case 0x01:	/* Headphone */
346 			case 0x02:	/* Microphone */
347 			case 0x03:	/* Line */
348 			case 0x04:	/* SPDIF */
349 			case 0x05:	/* ADAT */
350 			case 0x06:	/* TDIF */
351 			case 0x07:	/* MADI */
352 			/* for undefined/changeable signal  */
353 			case 0x08:	/* Analog */
354 			case 0x09:	/* Digital */
355 			default:
356 				location = pcm + sec_loc;
357 				if (location >= AM824_MAX_CHANNELS_FOR_PCM) {
358 					err = -ENOSYS;
359 					goto end;
360 				}
361 				amdtp_am824_set_pcm_position(s, location,
362 							     stm_pos);
363 				break;
364 			}
365 		}
366 
367 		if (type != 0x0a)
368 			pcm += channels;
369 		else
370 			midi += channels;
371 	}
372 end:
373 	kfree(buf);
374 	return err;
375 }
376 
377 static int
378 check_connection_used_by_others(struct snd_bebob *bebob, struct amdtp_stream *s)
379 {
380 	struct cmp_connection *conn;
381 	bool used;
382 	int err;
383 
384 	if (s == &bebob->tx_stream)
385 		conn = &bebob->out_conn;
386 	else
387 		conn = &bebob->in_conn;
388 
389 	err = cmp_connection_check_used(conn, &used);
390 	if ((err >= 0) && used && !amdtp_stream_running(s)) {
391 		dev_err(&bebob->unit->device,
392 			"Connection established by others: %cPCR[%d]\n",
393 			(conn->direction == CMP_OUTPUT) ? 'o' : 'i',
394 			conn->pcr_index);
395 		err = -EBUSY;
396 	}
397 
398 	return err;
399 }
400 
401 static int make_both_connections(struct snd_bebob *bebob)
402 {
403 	int err = 0;
404 
405 	err = cmp_connection_establish(&bebob->out_conn);
406 	if (err < 0)
407 		return err;
408 
409 	err = cmp_connection_establish(&bebob->in_conn);
410 	if (err < 0) {
411 		cmp_connection_break(&bebob->out_conn);
412 		return err;
413 	}
414 
415 	return 0;
416 }
417 
418 static void
419 break_both_connections(struct snd_bebob *bebob)
420 {
421 	cmp_connection_break(&bebob->in_conn);
422 	cmp_connection_break(&bebob->out_conn);
423 
424 	/* These models seems to be in transition state for a longer time. */
425 	if (bebob->maudio_special_quirk != NULL)
426 		msleep(200);
427 }
428 
429 static int
430 start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream)
431 {
432 	struct cmp_connection *conn;
433 	int err = 0;
434 
435 	if (stream == &bebob->rx_stream)
436 		conn = &bebob->in_conn;
437 	else
438 		conn = &bebob->out_conn;
439 
440 	/* channel mapping */
441 	if (bebob->maudio_special_quirk == NULL) {
442 		err = map_data_channels(bebob, stream);
443 		if (err < 0)
444 			goto end;
445 	}
446 
447 	// start amdtp stream.
448 	err = amdtp_domain_add_stream(&bebob->domain, stream,
449 				      conn->resources.channel, conn->speed);
450 end:
451 	return err;
452 }
453 
454 static int init_stream(struct snd_bebob *bebob, struct amdtp_stream *stream)
455 {
456 	enum amdtp_stream_direction dir_stream;
457 	struct cmp_connection *conn;
458 	enum cmp_direction dir_conn;
459 	int err;
460 
461 	if (stream == &bebob->tx_stream) {
462 		dir_stream = AMDTP_IN_STREAM;
463 		conn = &bebob->out_conn;
464 		dir_conn = CMP_OUTPUT;
465 	} else {
466 		dir_stream = AMDTP_OUT_STREAM;
467 		conn = &bebob->in_conn;
468 		dir_conn = CMP_INPUT;
469 	}
470 
471 	err = cmp_connection_init(conn, bebob->unit, dir_conn, 0);
472 	if (err < 0)
473 		return err;
474 
475 	err = amdtp_am824_init(stream, bebob->unit, dir_stream, CIP_BLOCKING);
476 	if (err < 0) {
477 		cmp_connection_destroy(conn);
478 		return err;
479 	}
480 
481 	if (stream == &bebob->tx_stream) {
482 		// BeBoB v3 transfers packets with these qurks:
483 		//  - In the beginning of streaming, the value of dbc is
484 		//    incremented even if no data blocks are transferred.
485 		//  - The value of dbc is reset suddenly.
486 		if (bebob->version > 2)
487 			bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC |
488 						  CIP_SKIP_DBC_ZERO_CHECK;
489 
490 		// At high sampling rate, M-Audio special firmware transmits
491 		// empty packet with the value of dbc incremented by 8 but the
492 		// others are valid to IEC 61883-1.
493 		if (bebob->maudio_special_quirk)
494 			bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC;
495 	}
496 
497 	return 0;
498 }
499 
500 static void destroy_stream(struct snd_bebob *bebob, struct amdtp_stream *stream)
501 {
502 	amdtp_stream_destroy(stream);
503 
504 	if (stream == &bebob->tx_stream)
505 		cmp_connection_destroy(&bebob->out_conn);
506 	else
507 		cmp_connection_destroy(&bebob->in_conn);
508 }
509 
510 int snd_bebob_stream_init_duplex(struct snd_bebob *bebob)
511 {
512 	int err;
513 
514 	err = init_stream(bebob, &bebob->tx_stream);
515 	if (err < 0)
516 		return err;
517 
518 	err = init_stream(bebob, &bebob->rx_stream);
519 	if (err < 0) {
520 		destroy_stream(bebob, &bebob->tx_stream);
521 		return err;
522 	}
523 
524 	err = amdtp_domain_init(&bebob->domain);
525 	if (err < 0) {
526 		destroy_stream(bebob, &bebob->tx_stream);
527 		destroy_stream(bebob, &bebob->rx_stream);
528 	}
529 
530 	return err;
531 }
532 
533 static int keep_resources(struct snd_bebob *bebob, struct amdtp_stream *stream,
534 			  unsigned int rate, unsigned int index)
535 {
536 	struct snd_bebob_stream_formation *formation;
537 	struct cmp_connection *conn;
538 	int err;
539 
540 	if (stream == &bebob->tx_stream) {
541 		formation = bebob->tx_stream_formations + index;
542 		conn = &bebob->out_conn;
543 	} else {
544 		formation = bebob->rx_stream_formations + index;
545 		conn = &bebob->in_conn;
546 	}
547 
548 	err = amdtp_am824_set_parameters(stream, rate, formation->pcm,
549 					 formation->midi, false);
550 	if (err < 0)
551 		return err;
552 
553 	return cmp_connection_reserve(conn, amdtp_stream_get_max_payload(stream));
554 }
555 
556 int snd_bebob_stream_reserve_duplex(struct snd_bebob *bebob, unsigned int rate)
557 {
558 	unsigned int curr_rate;
559 	int err;
560 
561 	// Considering JACK/FFADO streaming:
562 	// TODO: This can be removed hwdep functionality becomes popular.
563 	err = check_connection_used_by_others(bebob, &bebob->rx_stream);
564 	if (err < 0)
565 		return err;
566 
567 	err = bebob->spec->rate->get(bebob, &curr_rate);
568 	if (err < 0)
569 		return err;
570 	if (rate == 0)
571 		rate = curr_rate;
572 	if (curr_rate != rate) {
573 		amdtp_domain_stop(&bebob->domain);
574 		break_both_connections(bebob);
575 
576 		cmp_connection_release(&bebob->out_conn);
577 		cmp_connection_release(&bebob->in_conn);
578 	}
579 
580 	if (bebob->substreams_counter == 0 || curr_rate != rate) {
581 		unsigned int index;
582 
583 		// NOTE:
584 		// If establishing connections at first, Yamaha GO46
585 		// (and maybe Terratec X24) don't generate sound.
586 		//
587 		// For firmware customized by M-Audio, refer to next NOTE.
588 		err = bebob->spec->rate->set(bebob, rate);
589 		if (err < 0) {
590 			dev_err(&bebob->unit->device,
591 				"fail to set sampling rate: %d\n",
592 				err);
593 			return err;
594 		}
595 
596 		err = get_formation_index(rate, &index);
597 		if (err < 0)
598 			return err;
599 
600 		err = keep_resources(bebob, &bebob->tx_stream, rate, index);
601 		if (err < 0)
602 			return err;
603 
604 		err = keep_resources(bebob, &bebob->rx_stream, rate, index);
605 		if (err < 0) {
606 			cmp_connection_release(&bebob->out_conn);
607 			return err;
608 		}
609 	}
610 
611 	return 0;
612 }
613 
614 int snd_bebob_stream_start_duplex(struct snd_bebob *bebob)
615 {
616 	int err;
617 
618 	// Need no substreams.
619 	if (bebob->substreams_counter == 0)
620 		return -EIO;
621 
622 	// packet queueing error or detecting discontinuity
623 	if (amdtp_streaming_error(&bebob->rx_stream) ||
624 	    amdtp_streaming_error(&bebob->tx_stream)) {
625 		amdtp_domain_stop(&bebob->domain);
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 			goto error;
645 
646 		err = start_stream(bebob, &bebob->tx_stream);
647 		if (err < 0)
648 			goto error;
649 
650 		err = amdtp_domain_start(&bebob->domain);
651 		if (err < 0)
652 			goto error;
653 
654 		// NOTE:
655 		// The firmware customized by M-Audio uses these commands to
656 		// start transmitting stream. This is not usual way.
657 		if (bebob->maudio_special_quirk) {
658 			err = bebob->spec->rate->set(bebob, curr_rate);
659 			if (err < 0) {
660 				dev_err(&bebob->unit->device,
661 					"fail to ensure sampling rate: %d\n",
662 					err);
663 				goto error;
664 			}
665 		}
666 
667 		if (!amdtp_stream_wait_callback(&bebob->rx_stream,
668 						CALLBACK_TIMEOUT) ||
669 		    !amdtp_stream_wait_callback(&bebob->tx_stream,
670 						CALLBACK_TIMEOUT)) {
671 			err = -ETIMEDOUT;
672 			goto error;
673 		}
674 	}
675 
676 	return 0;
677 error:
678 	amdtp_domain_stop(&bebob->domain);
679 	break_both_connections(bebob);
680 	return err;
681 }
682 
683 void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob)
684 {
685 	if (bebob->substreams_counter == 0) {
686 		amdtp_domain_stop(&bebob->domain);
687 		break_both_connections(bebob);
688 
689 		cmp_connection_release(&bebob->out_conn);
690 		cmp_connection_release(&bebob->in_conn);
691 	}
692 }
693 
694 /*
695  * This function should be called before starting streams or after stopping
696  * streams.
697  */
698 void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob)
699 {
700 	amdtp_domain_destroy(&bebob->domain);
701 
702 	destroy_stream(bebob, &bebob->tx_stream);
703 	destroy_stream(bebob, &bebob->rx_stream);
704 }
705 
706 /*
707  * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’
708  * in Additional AVC commands (Nov 2003, BridgeCo)
709  * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
710  */
711 static int
712 parse_stream_formation(u8 *buf, unsigned int len,
713 		       struct snd_bebob_stream_formation *formation)
714 {
715 	unsigned int i, e, channels, format;
716 
717 	/*
718 	 * this module can support a hierarchy combination that:
719 	 *  Root:	Audio and Music (0x90)
720 	 *  Level 1:	AM824 Compound  (0x40)
721 	 */
722 	if ((buf[0] != 0x90) || (buf[1] != 0x40))
723 		return -ENOSYS;
724 
725 	/* check sampling rate */
726 	for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) {
727 		if (buf[2] == bridgeco_freq_table[i])
728 			break;
729 	}
730 	if (i == ARRAY_SIZE(bridgeco_freq_table))
731 		return -ENOSYS;
732 
733 	/* Avoid double count by different entries for the same rate. */
734 	memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation));
735 
736 	for (e = 0; e < buf[4]; e++) {
737 		channels = buf[5 + e * 2];
738 		format = buf[6 + e * 2];
739 
740 		switch (format) {
741 		/* IEC 60958 Conformant, currently handled as MBLA */
742 		case 0x00:
743 		/* Multi bit linear audio */
744 		case 0x06:	/* Raw */
745 			formation[i].pcm += channels;
746 			break;
747 		/* MIDI Conformant */
748 		case 0x0d:
749 			formation[i].midi += channels;
750 			break;
751 		/* IEC 61937-3 to 7 */
752 		case 0x01:
753 		case 0x02:
754 		case 0x03:
755 		case 0x04:
756 		case 0x05:
757 		/* Multi bit linear audio */
758 		case 0x07:	/* DVD-Audio */
759 		case 0x0c:	/* High Precision */
760 		/* One Bit Audio */
761 		case 0x08:	/* (Plain) Raw */
762 		case 0x09:	/* (Plain) SACD */
763 		case 0x0a:	/* (Encoded) Raw */
764 		case 0x0b:	/* (Encoded) SACD */
765 		/* Synchronization Stream (Stereo Raw audio) */
766 		case 0x40:
767 		/* Don't care */
768 		case 0xff:
769 		default:
770 			return -ENOSYS;	/* not supported */
771 		}
772 	}
773 
774 	if (formation[i].pcm  > AM824_MAX_CHANNELS_FOR_PCM ||
775 	    formation[i].midi > AM824_MAX_CHANNELS_FOR_MIDI)
776 		return -ENOSYS;
777 
778 	return 0;
779 }
780 
781 static int
782 fill_stream_formations(struct snd_bebob *bebob, enum avc_bridgeco_plug_dir dir,
783 		       unsigned short pid)
784 {
785 	u8 *buf;
786 	struct snd_bebob_stream_formation *formations;
787 	unsigned int len, eid;
788 	u8 addr[AVC_BRIDGECO_ADDR_BYTES];
789 	int err;
790 
791 	buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL);
792 	if (buf == NULL)
793 		return -ENOMEM;
794 
795 	if (dir == AVC_BRIDGECO_PLUG_DIR_IN)
796 		formations = bebob->rx_stream_formations;
797 	else
798 		formations = bebob->tx_stream_formations;
799 
800 	for (eid = 0; eid < SND_BEBOB_STRM_FMT_ENTRIES; eid++) {
801 		len = FORMAT_MAXIMUM_LENGTH;
802 		avc_bridgeco_fill_unit_addr(addr, dir,
803 					    AVC_BRIDGECO_PLUG_UNIT_ISOC, pid);
804 		err = avc_bridgeco_get_plug_strm_fmt(bebob->unit, addr, buf,
805 						     &len, eid);
806 		/* No entries remained. */
807 		if (err == -EINVAL && eid > 0) {
808 			err = 0;
809 			break;
810 		} else if (err < 0) {
811 			dev_err(&bebob->unit->device,
812 			"fail to get stream format %d for isoc %s plug %d:%d\n",
813 				eid,
814 				(dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
815 								    "out",
816 				pid, err);
817 			break;
818 		}
819 
820 		err = parse_stream_formation(buf, len, formations);
821 		if (err < 0)
822 			break;
823 	}
824 
825 	kfree(buf);
826 	return err;
827 }
828 
829 static int
830 seek_msu_sync_input_plug(struct snd_bebob *bebob)
831 {
832 	u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
833 	unsigned int i;
834 	enum avc_bridgeco_plug_type type;
835 	int err;
836 
837 	/* Get the number of Music Sub Unit for both direction. */
838 	err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs);
839 	if (err < 0) {
840 		dev_err(&bebob->unit->device,
841 			"fail to get info for MSU in/out plugs: %d\n",
842 			err);
843 		goto end;
844 	}
845 
846 	/* seek destination plugs for 'MSU sync input' */
847 	bebob->sync_input_plug = -1;
848 	for (i = 0; i < plugs[0]; i++) {
849 		avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, i);
850 		err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
851 		if (err < 0) {
852 			dev_err(&bebob->unit->device,
853 				"fail to get type for MSU in plug %d: %d\n",
854 				i, err);
855 			goto end;
856 		}
857 
858 		if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
859 			bebob->sync_input_plug = i;
860 			break;
861 		}
862 	}
863 end:
864 	return err;
865 }
866 
867 int snd_bebob_stream_discover(struct snd_bebob *bebob)
868 {
869 	const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
870 	u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
871 	enum avc_bridgeco_plug_type type;
872 	unsigned int i;
873 	int err;
874 
875 	/* the number of plugs for isoc in/out, ext in/out  */
876 	err = avc_general_get_plug_info(bebob->unit, 0x1f, 0x07, 0x00, plugs);
877 	if (err < 0) {
878 		dev_err(&bebob->unit->device,
879 		"fail to get info for isoc/external in/out plugs: %d\n",
880 			err);
881 		goto end;
882 	}
883 
884 	/*
885 	 * This module supports at least one isoc input plug and one isoc
886 	 * output plug.
887 	 */
888 	if ((plugs[0] == 0) || (plugs[1] == 0)) {
889 		err = -ENOSYS;
890 		goto end;
891 	}
892 
893 	avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
894 				    AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
895 	err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
896 	if (err < 0) {
897 		dev_err(&bebob->unit->device,
898 			"fail to get type for isoc in plug 0: %d\n", err);
899 		goto end;
900 	} else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
901 		err = -ENOSYS;
902 		goto end;
903 	}
904 	err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_IN, 0);
905 	if (err < 0)
906 		goto end;
907 
908 	avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
909 				    AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
910 	err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
911 	if (err < 0) {
912 		dev_err(&bebob->unit->device,
913 			"fail to get type for isoc out plug 0: %d\n", err);
914 		goto end;
915 	} else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
916 		err = -ENOSYS;
917 		goto end;
918 	}
919 	err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_OUT, 0);
920 	if (err < 0)
921 		goto end;
922 
923 	/* count external input plugs for MIDI */
924 	bebob->midi_input_ports = 0;
925 	for (i = 0; i < plugs[2]; i++) {
926 		avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
927 					    AVC_BRIDGECO_PLUG_UNIT_EXT, i);
928 		err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
929 		if (err < 0) {
930 			dev_err(&bebob->unit->device,
931 			"fail to get type for external in plug %d: %d\n",
932 				i, err);
933 			goto end;
934 		} else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
935 			bebob->midi_input_ports++;
936 		}
937 	}
938 
939 	/* count external output plugs for MIDI */
940 	bebob->midi_output_ports = 0;
941 	for (i = 0; i < plugs[3]; i++) {
942 		avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
943 					    AVC_BRIDGECO_PLUG_UNIT_EXT, i);
944 		err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
945 		if (err < 0) {
946 			dev_err(&bebob->unit->device,
947 			"fail to get type for external out plug %d: %d\n",
948 				i, err);
949 			goto end;
950 		} else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
951 			bebob->midi_output_ports++;
952 		}
953 	}
954 
955 	/* for check source of clock later */
956 	if (!clk_spec)
957 		err = seek_msu_sync_input_plug(bebob);
958 end:
959 	return err;
960 }
961 
962 void snd_bebob_stream_lock_changed(struct snd_bebob *bebob)
963 {
964 	bebob->dev_lock_changed = true;
965 	wake_up(&bebob->hwdep_wait);
966 }
967 
968 int snd_bebob_stream_lock_try(struct snd_bebob *bebob)
969 {
970 	int err;
971 
972 	spin_lock_irq(&bebob->lock);
973 
974 	/* user land lock this */
975 	if (bebob->dev_lock_count < 0) {
976 		err = -EBUSY;
977 		goto end;
978 	}
979 
980 	/* this is the first time */
981 	if (bebob->dev_lock_count++ == 0)
982 		snd_bebob_stream_lock_changed(bebob);
983 	err = 0;
984 end:
985 	spin_unlock_irq(&bebob->lock);
986 	return err;
987 }
988 
989 void snd_bebob_stream_lock_release(struct snd_bebob *bebob)
990 {
991 	spin_lock_irq(&bebob->lock);
992 
993 	if (WARN_ON(bebob->dev_lock_count <= 0))
994 		goto end;
995 	if (--bebob->dev_lock_count == 0)
996 		snd_bebob_stream_lock_changed(bebob);
997 end:
998 	spin_unlock_irq(&bebob->lock);
999 }
1000