ALSA: bebob: Use a signed return type for get_formation_index
[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
9a73195e 11#define CALLBACK_TIMEOUT 2000
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
07905298
LT
50static int
51get_formation_index(unsigned int rate, unsigned int *index)
eb7b3a05
TS
52{
53 unsigned int i;
54
55 for (i = 0; i < ARRAY_SIZE(snd_bebob_rate_table); i++) {
07905298
LT
56 if (snd_bebob_rate_table[i] == rate) {
57 *index = i;
58 return 0;
59 }
eb7b3a05
TS
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
3e254b16
TS
121int snd_bebob_stream_get_clock_src(struct snd_bebob *bebob,
122 enum snd_bebob_clock_type *src)
eb7b3a05 123{
6b9866c8 124 const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
eb7b3a05 125 u8 addr[AVC_BRIDGECO_ADDR_BYTES], input[7];
1fc9522a 126 unsigned int id;
5a668812 127 enum avc_bridgeco_plug_type type;
eb7b3a05
TS
128 int err = 0;
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
3e254b16 147 *src = clk_spec->types[id];
1fc9522a
TS
148 goto end;
149 }
150
eb7b3a05 151 /*
1fc9522a 152 * 2.The device don't support to switch source of clock then assumed
eb7b3a05
TS
153 * to use internal clock always
154 */
155 if (bebob->sync_input_plug < 0) {
3e254b16 156 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
eb7b3a05
TS
157 goto end;
158 }
159
160 /*
1fc9522a 161 * 3.The device supports to switch source of clock by an usual way.
eb7b3a05
TS
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) {
3e254b16 179 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
eb7b3a05
TS
180 goto end;
181 }
182
5a668812
TS
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) {
3e254b16 192 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
5a668812
TS
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 */
3e254b16 206 *src = SND_BEBOB_CLOCK_TYPE_SYT;
5a668812
TS
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 */
3e254b16 214 *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
5a668812
TS
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 */
3e254b16 233 *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
5a668812
TS
234 goto end;
235 } else if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
236 /* Often word clock. */
3e254b16 237 *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
5a668812
TS
238 goto end;
239 } else if (type == AVC_BRIDGECO_PLUG_TYPE_ADDITION) {
240 /*
241 * Not standard.
242 * Mostly, additional internal clock.
243 */
3e254b16 244 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
5a668812
TS
245 goto end;
246 }
247 }
248 }
249
250 /* Not supported. */
251 err = -EIO;
eb7b3a05
TS
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
9076c22d
TS
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
eb7b3a05
TS
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 }
f65be911 343 amdtp_am824_set_midi_position(s, stm_pos);
eb7b3a05
TS
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;
49c7b3fc 359 if (location >= AM824_MAX_CHANNELS_FOR_PCM) {
eb7b3a05
TS
360 err = -ENOSYS;
361 goto end;
362 }
f65be911
TS
363 amdtp_am824_set_pcm_position(s, location,
364 stm_pos);
eb7b3a05
TS
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{
b6bc8123
TS
424 int index, pcm_channels, midi_channels, err = 0;
425
426 if (bebob->connected)
427 goto end;
eb7b3a05
TS
428
429 /* confirm params for both streams */
07905298
LT
430 err = get_formation_index(rate, &index);
431 if (err < 0)
432 goto end;
eb7b3a05
TS
433 pcm_channels = bebob->tx_stream_formations[index].pcm;
434 midi_channels = bebob->tx_stream_formations[index].midi;
51c29fd2
TS
435 err = amdtp_am824_set_parameters(&bebob->tx_stream, rate,
436 pcm_channels, midi_channels * 8,
437 false);
547e631c
TS
438 if (err < 0)
439 goto end;
440
eb7b3a05
TS
441 pcm_channels = bebob->rx_stream_formations[index].pcm;
442 midi_channels = bebob->rx_stream_formations[index].midi;
51c29fd2
TS
443 err = amdtp_am824_set_parameters(&bebob->rx_stream, rate,
444 pcm_channels, midi_channels * 8,
445 false);
547e631c
TS
446 if (err < 0)
447 goto end;
eb7b3a05
TS
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));
b6bc8123 456 if (err < 0) {
eb7b3a05 457 cmp_connection_break(&bebob->out_conn);
b6bc8123
TS
458 goto end;
459 }
460
461 bebob->connected = true;
eb7b3a05
TS
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);
b6bc8123
TS
471
472 bebob->connected = false;
3149ac48
TS
473
474 /* These models seems to be in transition state for a longer time. */
475 if (bebob->maudio_special_quirk != NULL)
476 msleep(200);
eb7b3a05
TS
477}
478
479static void
480destroy_both_connections(struct snd_bebob *bebob)
481{
eb7b3a05
TS
482 cmp_connection_destroy(&bebob->in_conn);
483 cmp_connection_destroy(&bebob->out_conn);
484}
485
486static int
487get_sync_mode(struct snd_bebob *bebob, enum cip_flags *sync_mode)
488{
05773792
TS
489 enum snd_bebob_clock_type src;
490 int err;
491
492 err = snd_bebob_stream_get_clock_src(bebob, &src);
493 if (err < 0)
494 return err;
495
496 switch (src) {
497 case SND_BEBOB_CLOCK_TYPE_INTERNAL:
498 case SND_BEBOB_CLOCK_TYPE_EXTERNAL:
499 *sync_mode = CIP_SYNC_TO_DEVICE;
500 break;
501 default:
502 case SND_BEBOB_CLOCK_TYPE_SYT:
503 *sync_mode = 0;
504 break;
505 }
506
eb7b3a05
TS
507 return 0;
508}
509
510static int
511start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream,
512 unsigned int rate)
513{
514 struct cmp_connection *conn;
515 int err = 0;
516
517 if (stream == &bebob->rx_stream)
518 conn = &bebob->in_conn;
519 else
520 conn = &bebob->out_conn;
521
522 /* channel mapping */
3149ac48
TS
523 if (bebob->maudio_special_quirk == NULL) {
524 err = map_data_channels(bebob, stream);
525 if (err < 0)
526 goto end;
527 }
eb7b3a05
TS
528
529 /* start amdtp stream */
530 err = amdtp_stream_start(stream,
531 conn->resources.channel,
532 conn->speed);
533end:
534 return err;
535}
536
537int snd_bebob_stream_init_duplex(struct snd_bebob *bebob)
538{
539 int err;
540
541 err = init_both_connections(bebob);
542 if (err < 0)
543 goto end;
544
5955815e
TS
545 err = amdtp_am824_init(&bebob->tx_stream, bebob->unit,
546 AMDTP_IN_STREAM, CIP_BLOCKING);
eb7b3a05
TS
547 if (err < 0) {
548 amdtp_stream_destroy(&bebob->tx_stream);
549 destroy_both_connections(bebob);
550 goto end;
551 }
b6bc8123
TS
552 /* See comments in next function */
553 init_completion(&bebob->bus_reset);
554 bebob->tx_stream.flags |= CIP_SKIP_INIT_DBC_CHECK;
c4d860a0
TS
555
556 /*
557 * BeBoB v3 transfers packets with these qurks:
558 * - In the beginning of streaming, the value of dbc is incremented
559 * even if no data blocks are transferred.
560 * - The value of dbc is reset suddenly.
561 */
562 if (bebob->version > 2)
563 bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC |
564 CIP_SKIP_DBC_ZERO_CHECK;
565
9d59124c
TS
566 /*
567 * At high sampling rate, M-Audio special firmware transmits empty
568 * packet with the value of dbc incremented by 8 but the others are
569 * valid to IEC 61883-1.
570 */
571 if (bebob->maudio_special_quirk)
572 bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC;
eb7b3a05 573
5955815e
TS
574 err = amdtp_am824_init(&bebob->rx_stream, bebob->unit,
575 AMDTP_OUT_STREAM, CIP_BLOCKING);
eb7b3a05
TS
576 if (err < 0) {
577 amdtp_stream_destroy(&bebob->tx_stream);
578 amdtp_stream_destroy(&bebob->rx_stream);
579 destroy_both_connections(bebob);
580 }
581end:
582 return err;
583}
584
4a286d55 585int snd_bebob_stream_start_duplex(struct snd_bebob *bebob, unsigned int rate)
eb7b3a05 586{
6b9866c8 587 const struct snd_bebob_rate_spec *rate_spec = bebob->spec->rate;
eb7b3a05 588 struct amdtp_stream *master, *slave;
eb7b3a05
TS
589 enum cip_flags sync_mode;
590 unsigned int curr_rate;
b6bc8123 591 bool updated = false;
eb7b3a05
TS
592 int err = 0;
593
b6bc8123
TS
594 /*
595 * Normal BeBoB firmware has a quirk at bus reset to transmits packets
596 * with discontinuous value in dbc field.
597 *
598 * This 'struct completion' is used to call .update() at first to update
599 * connections/streams. Next following codes handle streaming error.
600 */
601 if (amdtp_streaming_error(&bebob->tx_stream)) {
602 if (completion_done(&bebob->bus_reset))
603 reinit_completion(&bebob->bus_reset);
604
605 updated = (wait_for_completion_interruptible_timeout(
606 &bebob->bus_reset,
607 msecs_to_jiffies(FW_ISO_RESOURCE_DELAY)) > 0);
608 }
609
eb7b3a05
TS
610 mutex_lock(&bebob->mutex);
611
612 /* Need no substreams */
8d1c2694 613 if (atomic_read(&bebob->substreams_counter) == 0)
eb7b3a05
TS
614 goto end;
615
616 err = get_sync_mode(bebob, &sync_mode);
617 if (err < 0)
618 goto end;
619 if (sync_mode == CIP_SYNC_TO_DEVICE) {
620 master = &bebob->tx_stream;
621 slave = &bebob->rx_stream;
eb7b3a05
TS
622 } else {
623 master = &bebob->rx_stream;
624 slave = &bebob->tx_stream;
eb7b3a05
TS
625 }
626
627 /*
628 * Considering JACK/FFADO streaming:
629 * TODO: This can be removed hwdep functionality becomes popular.
630 */
631 err = check_connection_used_by_others(bebob, master);
632 if (err < 0)
633 goto end;
634
b6bc8123
TS
635 /*
636 * packet queueing error or detecting discontinuity
637 *
638 * At bus reset, connections should not be broken here. So streams need
639 * to be re-started. This is a reason to use SKIP_INIT_DBC_CHECK flag.
640 */
641 if (amdtp_streaming_error(master))
eb7b3a05 642 amdtp_stream_stop(master);
eb7b3a05
TS
643 if (amdtp_streaming_error(slave))
644 amdtp_stream_stop(slave);
b6bc8123
TS
645 if (!updated &&
646 !amdtp_stream_running(master) && !amdtp_stream_running(slave))
647 break_both_connections(bebob);
eb7b3a05
TS
648
649 /* stop streams if rate is different */
1fc9522a 650 err = rate_spec->get(bebob, &curr_rate);
eb7b3a05
TS
651 if (err < 0) {
652 dev_err(&bebob->unit->device,
653 "fail to get sampling rate: %d\n", err);
654 goto end;
655 }
656 if (rate == 0)
657 rate = curr_rate;
658 if (rate != curr_rate) {
659 amdtp_stream_stop(master);
660 amdtp_stream_stop(slave);
661 break_both_connections(bebob);
662 }
663
664 /* master should be always running */
665 if (!amdtp_stream_running(master)) {
666 amdtp_stream_set_sync(sync_mode, master, slave);
667 bebob->master = master;
668
669 /*
670 * NOTE:
671 * If establishing connections at first, Yamaha GO46
672 * (and maybe Terratec X24) don't generate sound.
3149ac48
TS
673 *
674 * For firmware customized by M-Audio, refer to next NOTE.
eb7b3a05 675 */
3149ac48
TS
676 if (bebob->maudio_special_quirk == NULL) {
677 err = rate_spec->set(bebob, rate);
678 if (err < 0) {
679 dev_err(&bebob->unit->device,
680 "fail to set sampling rate: %d\n",
681 err);
682 goto end;
683 }
eb7b3a05
TS
684 }
685
686 err = make_both_connections(bebob, rate);
687 if (err < 0)
688 goto end;
689
690 err = start_stream(bebob, master, rate);
691 if (err < 0) {
692 dev_err(&bebob->unit->device,
693 "fail to run AMDTP master stream:%d\n", err);
694 break_both_connections(bebob);
695 goto end;
696 }
697
3149ac48
TS
698 /*
699 * NOTE:
700 * The firmware customized by M-Audio uses these commands to
701 * start transmitting stream. This is not usual way.
702 */
703 if (bebob->maudio_special_quirk != NULL) {
704 err = rate_spec->set(bebob, rate);
705 if (err < 0) {
706 dev_err(&bebob->unit->device,
707 "fail to ensure sampling rate: %d\n",
708 err);
709 amdtp_stream_stop(master);
710 break_both_connections(bebob);
711 goto end;
712 }
713 }
714
eb7b3a05
TS
715 /* wait first callback */
716 if (!amdtp_stream_wait_callback(master, CALLBACK_TIMEOUT)) {
717 amdtp_stream_stop(master);
718 break_both_connections(bebob);
719 err = -ETIMEDOUT;
720 goto end;
721 }
722 }
723
724 /* start slave if needed */
8d1c2694 725 if (!amdtp_stream_running(slave)) {
eb7b3a05
TS
726 err = start_stream(bebob, slave, rate);
727 if (err < 0) {
728 dev_err(&bebob->unit->device,
729 "fail to run AMDTP slave stream:%d\n", err);
730 amdtp_stream_stop(master);
731 break_both_connections(bebob);
732 goto end;
733 }
734
735 /* wait first callback */
736 if (!amdtp_stream_wait_callback(slave, CALLBACK_TIMEOUT)) {
737 amdtp_stream_stop(slave);
738 amdtp_stream_stop(master);
739 break_both_connections(bebob);
740 err = -ETIMEDOUT;
741 }
742 }
743end:
744 mutex_unlock(&bebob->mutex);
745 return err;
746}
747
748void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob)
749{
750 struct amdtp_stream *master, *slave;
eb7b3a05 751
eb7b3a05
TS
752 if (bebob->master == &bebob->rx_stream) {
753 slave = &bebob->tx_stream;
754 master = &bebob->rx_stream;
eb7b3a05
TS
755 } else {
756 slave = &bebob->rx_stream;
757 master = &bebob->tx_stream;
eb7b3a05
TS
758 }
759
c6e5e741
TS
760 mutex_lock(&bebob->mutex);
761
8d1c2694
TS
762 if (atomic_read(&bebob->substreams_counter) == 0) {
763 amdtp_stream_pcm_abort(master);
764 amdtp_stream_stop(master);
765
eb7b3a05
TS
766 amdtp_stream_pcm_abort(slave);
767 amdtp_stream_stop(slave);
768
8d1c2694 769 break_both_connections(bebob);
eb7b3a05
TS
770 }
771
772 mutex_unlock(&bebob->mutex);
773}
774
775void snd_bebob_stream_update_duplex(struct snd_bebob *bebob)
776{
777 /* vs. XRUN recovery due to discontinuity at bus reset */
778 mutex_lock(&bebob->mutex);
779
780 if ((cmp_connection_update(&bebob->in_conn) < 0) ||
781 (cmp_connection_update(&bebob->out_conn) < 0)) {
782 amdtp_stream_pcm_abort(&bebob->rx_stream);
783 amdtp_stream_pcm_abort(&bebob->tx_stream);
784 amdtp_stream_stop(&bebob->rx_stream);
785 amdtp_stream_stop(&bebob->tx_stream);
786 break_both_connections(bebob);
787 } else {
788 amdtp_stream_update(&bebob->rx_stream);
789 amdtp_stream_update(&bebob->tx_stream);
790 }
791
b6bc8123
TS
792 /* wake up stream_start_duplex() */
793 if (!completion_done(&bebob->bus_reset))
794 complete_all(&bebob->bus_reset);
795
eb7b3a05
TS
796 mutex_unlock(&bebob->mutex);
797}
798
d23c2cc4
TS
799/*
800 * This function should be called before starting streams or after stopping
801 * streams.
802 */
eb7b3a05
TS
803void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob)
804{
eb7b3a05
TS
805 amdtp_stream_destroy(&bebob->rx_stream);
806 amdtp_stream_destroy(&bebob->tx_stream);
807
808 destroy_both_connections(bebob);
eb7b3a05
TS
809}
810
811/*
812 * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’
813 * in Additional AVC commands (Nov 2003, BridgeCo)
814 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
815 */
816static int
817parse_stream_formation(u8 *buf, unsigned int len,
818 struct snd_bebob_stream_formation *formation)
819{
820 unsigned int i, e, channels, format;
821
822 /*
823 * this module can support a hierarchy combination that:
824 * Root: Audio and Music (0x90)
825 * Level 1: AM824 Compound (0x40)
826 */
827 if ((buf[0] != 0x90) || (buf[1] != 0x40))
828 return -ENOSYS;
829
830 /* check sampling rate */
831 for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) {
832 if (buf[2] == bridgeco_freq_table[i])
833 break;
834 }
33a5f989 835 if (i == ARRAY_SIZE(bridgeco_freq_table))
eb7b3a05
TS
836 return -ENOSYS;
837
838 /* Avoid double count by different entries for the same rate. */
839 memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation));
840
841 for (e = 0; e < buf[4]; e++) {
842 channels = buf[5 + e * 2];
843 format = buf[6 + e * 2];
844
845 switch (format) {
51fa31d4 846 /* IEC 60958 Conformant, currently handled as MBLA */
eb7b3a05
TS
847 case 0x00:
848 /* Multi bit linear audio */
849 case 0x06: /* Raw */
850 formation[i].pcm += channels;
851 break;
852 /* MIDI Conformant */
853 case 0x0d:
854 formation[i].midi += channels;
855 break;
856 /* IEC 61937-3 to 7 */
857 case 0x01:
858 case 0x02:
859 case 0x03:
860 case 0x04:
861 case 0x05:
862 /* Multi bit linear audio */
863 case 0x07: /* DVD-Audio */
864 case 0x0c: /* High Precision */
865 /* One Bit Audio */
866 case 0x08: /* (Plain) Raw */
867 case 0x09: /* (Plain) SACD */
868 case 0x0a: /* (Encoded) Raw */
869 case 0x0b: /* (Encoded) SACD */
870 /* Synchronization Stream (Stereo Raw audio) */
871 case 0x40:
872 /* Don't care */
873 case 0xff:
874 default:
875 return -ENOSYS; /* not supported */
876 }
877 }
878
49c7b3fc
TS
879 if (formation[i].pcm > AM824_MAX_CHANNELS_FOR_PCM ||
880 formation[i].midi > AM824_MAX_CHANNELS_FOR_MIDI)
eb7b3a05
TS
881 return -ENOSYS;
882
883 return 0;
884}
885
886static int
887fill_stream_formations(struct snd_bebob *bebob, enum avc_bridgeco_plug_dir dir,
888 unsigned short pid)
889{
890 u8 *buf;
891 struct snd_bebob_stream_formation *formations;
892 unsigned int len, eid;
893 u8 addr[AVC_BRIDGECO_ADDR_BYTES];
894 int err;
895
896 buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL);
897 if (buf == NULL)
898 return -ENOMEM;
899
900 if (dir == AVC_BRIDGECO_PLUG_DIR_IN)
901 formations = bebob->rx_stream_formations;
902 else
903 formations = bebob->tx_stream_formations;
904
905 for (eid = 0; eid < SND_BEBOB_STRM_FMT_ENTRIES; eid++) {
906 len = FORMAT_MAXIMUM_LENGTH;
907 avc_bridgeco_fill_unit_addr(addr, dir,
908 AVC_BRIDGECO_PLUG_UNIT_ISOC, pid);
909 err = avc_bridgeco_get_plug_strm_fmt(bebob->unit, addr, buf,
910 &len, eid);
911 /* No entries remained. */
912 if (err == -EINVAL && eid > 0) {
913 err = 0;
914 break;
915 } else if (err < 0) {
916 dev_err(&bebob->unit->device,
917 "fail to get stream format %d for isoc %s plug %d:%d\n",
918 eid,
919 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
920 "out",
921 pid, err);
922 break;
923 }
924
925 err = parse_stream_formation(buf, len, formations);
926 if (err < 0)
927 break;
928 }
929
930 kfree(buf);
931 return err;
932}
933
934static int
935seek_msu_sync_input_plug(struct snd_bebob *bebob)
936{
937 u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
a6b598bf
TS
938 unsigned int i;
939 enum avc_bridgeco_plug_type type;
eb7b3a05
TS
940 int err;
941
942 /* Get the number of Music Sub Unit for both direction. */
943 err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs);
944 if (err < 0) {
945 dev_err(&bebob->unit->device,
946 "fail to get info for MSU in/out plugs: %d\n",
947 err);
948 goto end;
949 }
950
951 /* seek destination plugs for 'MSU sync input' */
952 bebob->sync_input_plug = -1;
953 for (i = 0; i < plugs[0]; i++) {
954 avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, 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 MSU in plug %d: %d\n",
959 i, err);
960 goto end;
961 }
962
963 if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
964 bebob->sync_input_plug = i;
965 break;
966 }
967 }
968end:
969 return err;
970}
971
972int snd_bebob_stream_discover(struct snd_bebob *bebob)
973{
6b9866c8 974 const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
eb7b3a05
TS
975 u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
976 enum avc_bridgeco_plug_type type;
977 unsigned int i;
978 int err;
979
980 /* the number of plugs for isoc in/out, ext in/out */
981 err = avc_general_get_plug_info(bebob->unit, 0x1f, 0x07, 0x00, plugs);
982 if (err < 0) {
983 dev_err(&bebob->unit->device,
984 "fail to get info for isoc/external in/out plugs: %d\n",
985 err);
986 goto end;
987 }
988
989 /*
990 * This module supports at least one isoc input plug and one isoc
991 * output plug.
992 */
993 if ((plugs[0] == 0) || (plugs[1] == 0)) {
994 err = -ENOSYS;
995 goto end;
996 }
997
998 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
999 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
1000 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
1001 if (err < 0) {
1002 dev_err(&bebob->unit->device,
1003 "fail to get type for isoc in plug 0: %d\n", err);
1004 goto end;
1005 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
1006 err = -ENOSYS;
1007 goto end;
1008 }
1009 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_IN, 0);
1010 if (err < 0)
1011 goto end;
1012
1013 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
1014 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
1015 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
1016 if (err < 0) {
1017 dev_err(&bebob->unit->device,
1018 "fail to get type for isoc out plug 0: %d\n", err);
1019 goto end;
1020 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
1021 err = -ENOSYS;
1022 goto end;
1023 }
1024 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_OUT, 0);
1025 if (err < 0)
1026 goto end;
1027
1028 /* count external input plugs for MIDI */
1029 bebob->midi_input_ports = 0;
1030 for (i = 0; i < plugs[2]; i++) {
1031 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
1032 AVC_BRIDGECO_PLUG_UNIT_EXT, i);
1033 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
1034 if (err < 0) {
1035 dev_err(&bebob->unit->device,
1036 "fail to get type for external in plug %d: %d\n",
1037 i, err);
1038 goto end;
1039 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
1040 bebob->midi_input_ports++;
1041 }
1042 }
1043
1044 /* count external output plugs for MIDI */
1045 bebob->midi_output_ports = 0;
1046 for (i = 0; i < plugs[3]; i++) {
1047 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
1048 AVC_BRIDGECO_PLUG_UNIT_EXT, i);
1049 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
1050 if (err < 0) {
1051 dev_err(&bebob->unit->device,
1052 "fail to get type for external out plug %d: %d\n",
1053 i, err);
1054 goto end;
1055 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
1056 bebob->midi_output_ports++;
1057 }
1058 }
1059
1060 /* for check source of clock later */
1fc9522a
TS
1061 if (!clk_spec)
1062 err = seek_msu_sync_input_plug(bebob);
eb7b3a05
TS
1063end:
1064 return err;
1065}
618eabea
TS
1066
1067void snd_bebob_stream_lock_changed(struct snd_bebob *bebob)
1068{
1069 bebob->dev_lock_changed = true;
1070 wake_up(&bebob->hwdep_wait);
1071}
1072
1073int snd_bebob_stream_lock_try(struct snd_bebob *bebob)
1074{
1075 int err;
1076
1077 spin_lock_irq(&bebob->lock);
1078
1079 /* user land lock this */
1080 if (bebob->dev_lock_count < 0) {
1081 err = -EBUSY;
1082 goto end;
1083 }
1084
1085 /* this is the first time */
1086 if (bebob->dev_lock_count++ == 0)
1087 snd_bebob_stream_lock_changed(bebob);
1088 err = 0;
1089end:
1090 spin_unlock_irq(&bebob->lock);
1091 return err;
1092}
1093
1094void snd_bebob_stream_lock_release(struct snd_bebob *bebob)
1095{
1096 spin_lock_irq(&bebob->lock);
1097
1098 if (WARN_ON(bebob->dev_lock_count <= 0))
1099 goto end;
1100 if (--bebob->dev_lock_count == 0)
1101 snd_bebob_stream_lock_changed(bebob);
1102end:
1103 spin_unlock_irq(&bebob->lock);
1104}
This page took 0.142745 seconds and 5 git commands to generate.