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