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