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