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