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