blob: be2c056eb62df1b824de2af5995d5b6271908027 [file] [log] [blame]
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +09001/*
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
Takashi Sakamoto9a731952015-06-14 12:49:34 +090011#define CALLBACK_TIMEOUT 2000
Takashi Sakamotob6bc8122014-04-25 22:45:16 +090012#define FW_ISO_RESOURCE_DELAY 1000
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +090013
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
Lucas Tanure07905292016-01-25 19:30:23 -020050static int
51get_formation_index(unsigned int rate, unsigned int *index)
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +090052{
53 unsigned int i;
54
55 for (i = 0; i < ARRAY_SIZE(snd_bebob_rate_table); i++) {
Lucas Tanure07905292016-01-25 19:30:23 -020056 if (snd_bebob_rate_table[i] == rate) {
57 *index = i;
58 return 0;
59 }
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +090060 }
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
Takashi Sakamoto3e254b12015-06-14 12:49:30 +0900121int snd_bebob_stream_get_clock_src(struct snd_bebob *bebob,
122 enum snd_bebob_clock_type *src)
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900123{
Julia Lawall6b9866c2015-10-11 08:10:55 +0200124 const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900125 u8 addr[AVC_BRIDGECO_ADDR_BYTES], input[7];
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900126 unsigned int id;
Takashi Sakamoto5a668812015-06-14 12:49:27 +0900127 enum avc_bridgeco_plug_type type;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900128 int err = 0;
129
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900130 /* 1.The device has its own operation to switch source of clock */
131 if (clk_spec) {
132 err = clk_spec->get(bebob, &id);
Christian Vogeld1d0b6b2014-10-25 13:40:41 +0200133 if (err < 0) {
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900134 dev_err(&bebob->unit->device,
135 "fail to get clock source: %d\n", err);
Christian Vogeld1d0b6b2014-10-25 13:40:41 +0200136 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
Takashi Sakamoto3e254b12015-06-14 12:49:30 +0900147 *src = clk_spec->types[id];
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900148 goto end;
149 }
150
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900151 /*
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900152 * 2.The device don't support to switch source of clock then assumed
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900153 * to use internal clock always
154 */
155 if (bebob->sync_input_plug < 0) {
Takashi Sakamoto3e254b12015-06-14 12:49:30 +0900156 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900157 goto end;
158 }
159
160 /*
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900161 * 3.The device supports to switch source of clock by an usual way.
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900162 * 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) {
Takashi Sakamoto3e254b12015-06-14 12:49:30 +0900179 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900180 goto end;
181 }
182
Takashi Sakamoto5a668812015-06-14 12:49:27 +0900183 /* 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) {
Takashi Sakamoto3e254b12015-06-14 12:49:30 +0900192 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
Takashi Sakamoto5a668812015-06-14 12:49:27 +0900193 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 */
Takashi Sakamoto3e254b12015-06-14 12:49:30 +0900206 *src = SND_BEBOB_CLOCK_TYPE_SYT;
Takashi Sakamoto5a668812015-06-14 12:49:27 +0900207 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 */
Takashi Sakamoto3e254b12015-06-14 12:49:30 +0900214 *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
Takashi Sakamoto5a668812015-06-14 12:49:27 +0900215 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 */
Takashi Sakamoto3e254b12015-06-14 12:49:30 +0900233 *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
Takashi Sakamoto5a668812015-06-14 12:49:27 +0900234 goto end;
235 } else if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
236 /* Often word clock. */
Takashi Sakamoto3e254b12015-06-14 12:49:30 +0900237 *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
Takashi Sakamoto5a668812015-06-14 12:49:27 +0900238 goto end;
239 } else if (type == AVC_BRIDGECO_PLUG_TYPE_ADDITION) {
240 /*
241 * Not standard.
242 * Mostly, additional internal clock.
243 */
Takashi Sakamoto3e254b12015-06-14 12:49:30 +0900244 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
Takashi Sakamoto5a668812015-06-14 12:49:27 +0900245 goto end;
246 }
247 }
248 }
249
250 /* Not supported. */
251 err = -EIO;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900252end:
253 return err;
254}
255
Takashi Sakamoto9bb93ca2019-10-26 12:06:20 +0900256static int map_data_channels(struct snd_bebob *bebob, struct amdtp_stream *s)
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900257{
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
Takashi Sakamoto9076c222014-04-25 22:45:25 +0900325 /*
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
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900334 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 }
Takashi Sakamotof65be912015-09-19 11:21:58 +0900342 amdtp_am824_set_midi_position(s, stm_pos);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900343 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;
Takashi Sakamoto49c7b3f2015-09-19 11:22:01 +0900358 if (location >= AM824_MAX_CHANNELS_FOR_PCM) {
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900359 err = -ENOSYS;
360 goto end;
361 }
Takashi Sakamotof65be912015-09-19 11:21:58 +0900362 amdtp_am824_set_pcm_position(s, location,
363 stm_pos);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900364 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
378static int
379init_both_connections(struct snd_bebob *bebob)
380{
381 int err;
382
383 err = cmp_connection_init(&bebob->in_conn,
384 bebob->unit, CMP_INPUT, 0);
385 if (err < 0)
386 goto end;
387
388 err = cmp_connection_init(&bebob->out_conn,
389 bebob->unit, CMP_OUTPUT, 0);
390 if (err < 0)
391 cmp_connection_destroy(&bebob->in_conn);
392end:
393 return err;
394}
395
396static int
397check_connection_used_by_others(struct snd_bebob *bebob, struct amdtp_stream *s)
398{
399 struct cmp_connection *conn;
400 bool used;
401 int err;
402
403 if (s == &bebob->tx_stream)
404 conn = &bebob->out_conn;
405 else
406 conn = &bebob->in_conn;
407
408 err = cmp_connection_check_used(conn, &used);
409 if ((err >= 0) && used && !amdtp_stream_running(s)) {
410 dev_err(&bebob->unit->device,
411 "Connection established by others: %cPCR[%d]\n",
412 (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
413 conn->pcr_index);
414 err = -EBUSY;
415 }
416
417 return err;
418}
419
420static int
421make_both_connections(struct snd_bebob *bebob, unsigned int rate)
422{
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900423 int index, pcm_channels, midi_channels, err = 0;
424
425 if (bebob->connected)
426 goto end;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900427
428 /* confirm params for both streams */
Lucas Tanure07905292016-01-25 19:30:23 -0200429 err = get_formation_index(rate, &index);
430 if (err < 0)
431 goto end;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900432 pcm_channels = bebob->tx_stream_formations[index].pcm;
433 midi_channels = bebob->tx_stream_formations[index].midi;
Takashi Sakamoto51c29fd2015-09-19 11:21:56 +0900434 err = amdtp_am824_set_parameters(&bebob->tx_stream, rate,
435 pcm_channels, midi_channels * 8,
436 false);
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900437 if (err < 0)
438 goto end;
439
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900440 pcm_channels = bebob->rx_stream_formations[index].pcm;
441 midi_channels = bebob->rx_stream_formations[index].midi;
Takashi Sakamoto51c29fd2015-09-19 11:21:56 +0900442 err = amdtp_am824_set_parameters(&bebob->rx_stream, rate,
443 pcm_channels, midi_channels * 8,
444 false);
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900445 if (err < 0)
446 goto end;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900447
448 /* establish connections for both streams */
449 err = cmp_connection_establish(&bebob->out_conn,
450 amdtp_stream_get_max_payload(&bebob->tx_stream));
451 if (err < 0)
452 goto end;
453 err = cmp_connection_establish(&bebob->in_conn,
454 amdtp_stream_get_max_payload(&bebob->rx_stream));
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900455 if (err < 0) {
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900456 cmp_connection_break(&bebob->out_conn);
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900457 goto end;
458 }
459
460 bebob->connected = true;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900461end:
462 return err;
463}
464
465static void
466break_both_connections(struct snd_bebob *bebob)
467{
468 cmp_connection_break(&bebob->in_conn);
469 cmp_connection_break(&bebob->out_conn);
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900470
471 bebob->connected = false;
Takashi Sakamoto3149ac42014-04-25 22:45:26 +0900472
473 /* These models seems to be in transition state for a longer time. */
474 if (bebob->maudio_special_quirk != NULL)
475 msleep(200);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900476}
477
478static void
479destroy_both_connections(struct snd_bebob *bebob)
480{
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900481 cmp_connection_destroy(&bebob->in_conn);
482 cmp_connection_destroy(&bebob->out_conn);
483}
484
485static int
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900486start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream,
487 unsigned int rate)
488{
489 struct cmp_connection *conn;
490 int err = 0;
491
492 if (stream == &bebob->rx_stream)
493 conn = &bebob->in_conn;
494 else
495 conn = &bebob->out_conn;
496
497 /* channel mapping */
Takashi Sakamoto3149ac42014-04-25 22:45:26 +0900498 if (bebob->maudio_special_quirk == NULL) {
499 err = map_data_channels(bebob, stream);
500 if (err < 0)
501 goto end;
502 }
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900503
504 /* start amdtp stream */
505 err = amdtp_stream_start(stream,
506 conn->resources.channel,
507 conn->speed);
508end:
509 return err;
510}
511
512int snd_bebob_stream_init_duplex(struct snd_bebob *bebob)
513{
514 int err;
515
516 err = init_both_connections(bebob);
517 if (err < 0)
518 goto end;
519
Takashi Sakamoto59558152015-09-19 11:21:55 +0900520 err = amdtp_am824_init(&bebob->tx_stream, bebob->unit,
521 AMDTP_IN_STREAM, CIP_BLOCKING);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900522 if (err < 0) {
523 amdtp_stream_destroy(&bebob->tx_stream);
524 destroy_both_connections(bebob);
525 goto end;
526 }
Takashi Sakamoto14a37ac2016-02-20 16:18:56 +0900527
Takashi Sakamotoc4d860a2015-06-14 12:49:35 +0900528 /*
529 * BeBoB v3 transfers packets with these qurks:
530 * - In the beginning of streaming, the value of dbc is incremented
531 * even if no data blocks are transferred.
532 * - The value of dbc is reset suddenly.
533 */
534 if (bebob->version > 2)
535 bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC |
536 CIP_SKIP_DBC_ZERO_CHECK;
537
Takashi Sakamoto9d591242014-04-25 22:45:27 +0900538 /*
539 * At high sampling rate, M-Audio special firmware transmits empty
540 * packet with the value of dbc incremented by 8 but the others are
541 * valid to IEC 61883-1.
542 */
543 if (bebob->maudio_special_quirk)
544 bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900545
Takashi Sakamoto59558152015-09-19 11:21:55 +0900546 err = amdtp_am824_init(&bebob->rx_stream, bebob->unit,
547 AMDTP_OUT_STREAM, CIP_BLOCKING);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900548 if (err < 0) {
549 amdtp_stream_destroy(&bebob->tx_stream);
550 amdtp_stream_destroy(&bebob->rx_stream);
551 destroy_both_connections(bebob);
552 }
553end:
554 return err;
555}
556
Takashi Sakamoto4a286d52014-05-28 00:14:40 +0900557int snd_bebob_stream_start_duplex(struct snd_bebob *bebob, unsigned int rate)
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900558{
Julia Lawall6b9866c2015-10-11 08:10:55 +0200559 const struct snd_bebob_rate_spec *rate_spec = bebob->spec->rate;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900560 unsigned int curr_rate;
561 int err = 0;
562
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900563 /* Need no substreams */
Takashi Sakamoto4fd6c6c2016-02-20 16:18:58 +0900564 if (bebob->substreams_counter == 0)
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900565 goto end;
566
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900567 /*
568 * Considering JACK/FFADO streaming:
569 * TODO: This can be removed hwdep functionality becomes popular.
570 */
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900571 err = check_connection_used_by_others(bebob, &bebob->rx_stream);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900572 if (err < 0)
573 goto end;
574
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900575 /*
576 * packet queueing error or detecting discontinuity
577 *
578 * At bus reset, connections should not be broken here. So streams need
579 * to be re-started. This is a reason to use SKIP_INIT_DBC_CHECK flag.
580 */
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900581 if (amdtp_streaming_error(&bebob->rx_stream))
582 amdtp_stream_stop(&bebob->rx_stream);
583 if (amdtp_streaming_error(&bebob->tx_stream))
584 amdtp_stream_stop(&bebob->tx_stream);
585 if (!amdtp_stream_running(&bebob->rx_stream) &&
586 !amdtp_stream_running(&bebob->tx_stream))
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900587 break_both_connections(bebob);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900588
589 /* stop streams if rate is different */
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900590 err = rate_spec->get(bebob, &curr_rate);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900591 if (err < 0) {
592 dev_err(&bebob->unit->device,
593 "fail to get sampling rate: %d\n", err);
594 goto end;
595 }
596 if (rate == 0)
597 rate = curr_rate;
598 if (rate != curr_rate) {
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900599 amdtp_stream_stop(&bebob->rx_stream);
600 amdtp_stream_stop(&bebob->tx_stream);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900601 break_both_connections(bebob);
602 }
603
604 /* master should be always running */
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900605 if (!amdtp_stream_running(&bebob->rx_stream)) {
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900606 /*
607 * NOTE:
608 * If establishing connections at first, Yamaha GO46
609 * (and maybe Terratec X24) don't generate sound.
Takashi Sakamoto3149ac42014-04-25 22:45:26 +0900610 *
611 * For firmware customized by M-Audio, refer to next NOTE.
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900612 */
Takashi Sakamoto3149ac42014-04-25 22:45:26 +0900613 if (bebob->maudio_special_quirk == NULL) {
614 err = rate_spec->set(bebob, rate);
615 if (err < 0) {
616 dev_err(&bebob->unit->device,
617 "fail to set sampling rate: %d\n",
618 err);
619 goto end;
620 }
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900621 }
622
623 err = make_both_connections(bebob, rate);
624 if (err < 0)
625 goto end;
626
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900627 err = start_stream(bebob, &bebob->rx_stream, rate);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900628 if (err < 0) {
629 dev_err(&bebob->unit->device,
630 "fail to run AMDTP master stream:%d\n", err);
631 break_both_connections(bebob);
632 goto end;
633 }
634
Takashi Sakamoto3149ac42014-04-25 22:45:26 +0900635 /*
636 * NOTE:
637 * The firmware customized by M-Audio uses these commands to
638 * start transmitting stream. This is not usual way.
639 */
640 if (bebob->maudio_special_quirk != NULL) {
641 err = rate_spec->set(bebob, rate);
642 if (err < 0) {
643 dev_err(&bebob->unit->device,
644 "fail to ensure sampling rate: %d\n",
645 err);
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900646 amdtp_stream_stop(&bebob->rx_stream);
Takashi Sakamoto3149ac42014-04-25 22:45:26 +0900647 break_both_connections(bebob);
648 goto end;
649 }
650 }
651
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900652 /* wait first callback */
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900653 if (!amdtp_stream_wait_callback(&bebob->rx_stream,
654 CALLBACK_TIMEOUT)) {
655 amdtp_stream_stop(&bebob->rx_stream);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900656 break_both_connections(bebob);
657 err = -ETIMEDOUT;
658 goto end;
659 }
660 }
661
662 /* start slave if needed */
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900663 if (!amdtp_stream_running(&bebob->tx_stream)) {
664 err = start_stream(bebob, &bebob->tx_stream, rate);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900665 if (err < 0) {
666 dev_err(&bebob->unit->device,
667 "fail to run AMDTP slave stream:%d\n", err);
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900668 amdtp_stream_stop(&bebob->rx_stream);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900669 break_both_connections(bebob);
670 goto end;
671 }
672
673 /* wait first callback */
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900674 if (!amdtp_stream_wait_callback(&bebob->tx_stream,
675 CALLBACK_TIMEOUT)) {
676 amdtp_stream_stop(&bebob->tx_stream);
677 amdtp_stream_stop(&bebob->rx_stream);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900678 break_both_connections(bebob);
679 err = -ETIMEDOUT;
680 }
681 }
682end:
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900683 return err;
684}
685
686void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob)
687{
Takashi Sakamoto4fd6c6c2016-02-20 16:18:58 +0900688 if (bebob->substreams_counter == 0) {
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900689 amdtp_stream_pcm_abort(&bebob->rx_stream);
690 amdtp_stream_stop(&bebob->rx_stream);
Takashi Sakamoto8d1c2692015-06-14 12:49:36 +0900691
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900692 amdtp_stream_pcm_abort(&bebob->tx_stream);
693 amdtp_stream_stop(&bebob->tx_stream);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900694
Takashi Sakamoto8d1c2692015-06-14 12:49:36 +0900695 break_both_connections(bebob);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900696 }
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900697}
698
Takashi Sakamotod23c2cc2015-02-21 23:54:59 +0900699/*
700 * This function should be called before starting streams or after stopping
701 * streams.
702 */
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900703void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob)
704{
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900705 amdtp_stream_destroy(&bebob->rx_stream);
706 amdtp_stream_destroy(&bebob->tx_stream);
707
708 destroy_both_connections(bebob);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900709}
710
711/*
712 * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’
713 * in Additional AVC commands (Nov 2003, BridgeCo)
714 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
715 */
716static int
717parse_stream_formation(u8 *buf, unsigned int len,
718 struct snd_bebob_stream_formation *formation)
719{
720 unsigned int i, e, channels, format;
721
722 /*
723 * this module can support a hierarchy combination that:
724 * Root: Audio and Music (0x90)
725 * Level 1: AM824 Compound (0x40)
726 */
727 if ((buf[0] != 0x90) || (buf[1] != 0x40))
728 return -ENOSYS;
729
730 /* check sampling rate */
731 for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) {
732 if (buf[2] == bridgeco_freq_table[i])
733 break;
734 }
Dan Carpenter33a5f982014-05-28 19:43:30 +0300735 if (i == ARRAY_SIZE(bridgeco_freq_table))
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900736 return -ENOSYS;
737
738 /* Avoid double count by different entries for the same rate. */
739 memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation));
740
741 for (e = 0; e < buf[4]; e++) {
742 channels = buf[5 + e * 2];
743 format = buf[6 + e * 2];
744
745 switch (format) {
Takashi Sakamoto51fa31d2014-05-28 00:14:47 +0900746 /* IEC 60958 Conformant, currently handled as MBLA */
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900747 case 0x00:
748 /* Multi bit linear audio */
749 case 0x06: /* Raw */
750 formation[i].pcm += channels;
751 break;
752 /* MIDI Conformant */
753 case 0x0d:
754 formation[i].midi += channels;
755 break;
756 /* IEC 61937-3 to 7 */
757 case 0x01:
758 case 0x02:
759 case 0x03:
760 case 0x04:
761 case 0x05:
762 /* Multi bit linear audio */
763 case 0x07: /* DVD-Audio */
764 case 0x0c: /* High Precision */
765 /* One Bit Audio */
766 case 0x08: /* (Plain) Raw */
767 case 0x09: /* (Plain) SACD */
768 case 0x0a: /* (Encoded) Raw */
769 case 0x0b: /* (Encoded) SACD */
770 /* Synchronization Stream (Stereo Raw audio) */
771 case 0x40:
772 /* Don't care */
773 case 0xff:
774 default:
775 return -ENOSYS; /* not supported */
776 }
777 }
778
Takashi Sakamoto49c7b3f2015-09-19 11:22:01 +0900779 if (formation[i].pcm > AM824_MAX_CHANNELS_FOR_PCM ||
780 formation[i].midi > AM824_MAX_CHANNELS_FOR_MIDI)
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900781 return -ENOSYS;
782
783 return 0;
784}
785
786static int
787fill_stream_formations(struct snd_bebob *bebob, enum avc_bridgeco_plug_dir dir,
788 unsigned short pid)
789{
790 u8 *buf;
791 struct snd_bebob_stream_formation *formations;
792 unsigned int len, eid;
793 u8 addr[AVC_BRIDGECO_ADDR_BYTES];
794 int err;
795
796 buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL);
797 if (buf == NULL)
798 return -ENOMEM;
799
800 if (dir == AVC_BRIDGECO_PLUG_DIR_IN)
801 formations = bebob->rx_stream_formations;
802 else
803 formations = bebob->tx_stream_formations;
804
805 for (eid = 0; eid < SND_BEBOB_STRM_FMT_ENTRIES; eid++) {
806 len = FORMAT_MAXIMUM_LENGTH;
807 avc_bridgeco_fill_unit_addr(addr, dir,
808 AVC_BRIDGECO_PLUG_UNIT_ISOC, pid);
809 err = avc_bridgeco_get_plug_strm_fmt(bebob->unit, addr, buf,
810 &len, eid);
811 /* No entries remained. */
812 if (err == -EINVAL && eid > 0) {
813 err = 0;
814 break;
815 } else if (err < 0) {
816 dev_err(&bebob->unit->device,
817 "fail to get stream format %d for isoc %s plug %d:%d\n",
818 eid,
819 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
820 "out",
821 pid, err);
822 break;
823 }
824
825 err = parse_stream_formation(buf, len, formations);
826 if (err < 0)
827 break;
828 }
829
830 kfree(buf);
831 return err;
832}
833
834static int
835seek_msu_sync_input_plug(struct snd_bebob *bebob)
836{
837 u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
Takashi Sakamotoa6b598b2014-05-28 00:14:41 +0900838 unsigned int i;
839 enum avc_bridgeco_plug_type type;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900840 int err;
841
842 /* Get the number of Music Sub Unit for both direction. */
843 err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs);
844 if (err < 0) {
845 dev_err(&bebob->unit->device,
846 "fail to get info for MSU in/out plugs: %d\n",
847 err);
848 goto end;
849 }
850
851 /* seek destination plugs for 'MSU sync input' */
852 bebob->sync_input_plug = -1;
853 for (i = 0; i < plugs[0]; i++) {
854 avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, i);
855 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
856 if (err < 0) {
857 dev_err(&bebob->unit->device,
858 "fail to get type for MSU in plug %d: %d\n",
859 i, err);
860 goto end;
861 }
862
863 if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
864 bebob->sync_input_plug = i;
865 break;
866 }
867 }
868end:
869 return err;
870}
871
872int snd_bebob_stream_discover(struct snd_bebob *bebob)
873{
Julia Lawall6b9866c2015-10-11 08:10:55 +0200874 const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900875 u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
876 enum avc_bridgeco_plug_type type;
877 unsigned int i;
878 int err;
879
880 /* the number of plugs for isoc in/out, ext in/out */
881 err = avc_general_get_plug_info(bebob->unit, 0x1f, 0x07, 0x00, plugs);
882 if (err < 0) {
883 dev_err(&bebob->unit->device,
884 "fail to get info for isoc/external in/out plugs: %d\n",
885 err);
886 goto end;
887 }
888
889 /*
890 * This module supports at least one isoc input plug and one isoc
891 * output plug.
892 */
893 if ((plugs[0] == 0) || (plugs[1] == 0)) {
894 err = -ENOSYS;
895 goto end;
896 }
897
898 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
899 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
900 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
901 if (err < 0) {
902 dev_err(&bebob->unit->device,
903 "fail to get type for isoc in plug 0: %d\n", err);
904 goto end;
905 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
906 err = -ENOSYS;
907 goto end;
908 }
909 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_IN, 0);
910 if (err < 0)
911 goto end;
912
913 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
914 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
915 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
916 if (err < 0) {
917 dev_err(&bebob->unit->device,
918 "fail to get type for isoc out plug 0: %d\n", err);
919 goto end;
920 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
921 err = -ENOSYS;
922 goto end;
923 }
924 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_OUT, 0);
925 if (err < 0)
926 goto end;
927
928 /* count external input plugs for MIDI */
929 bebob->midi_input_ports = 0;
930 for (i = 0; i < plugs[2]; i++) {
931 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
932 AVC_BRIDGECO_PLUG_UNIT_EXT, i);
933 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
934 if (err < 0) {
935 dev_err(&bebob->unit->device,
936 "fail to get type for external in plug %d: %d\n",
937 i, err);
938 goto end;
939 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
940 bebob->midi_input_ports++;
941 }
942 }
943
944 /* count external output plugs for MIDI */
945 bebob->midi_output_ports = 0;
946 for (i = 0; i < plugs[3]; i++) {
947 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
948 AVC_BRIDGECO_PLUG_UNIT_EXT, i);
949 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
950 if (err < 0) {
951 dev_err(&bebob->unit->device,
952 "fail to get type for external out plug %d: %d\n",
953 i, err);
954 goto end;
955 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
956 bebob->midi_output_ports++;
957 }
958 }
959
960 /* for check source of clock later */
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900961 if (!clk_spec)
962 err = seek_msu_sync_input_plug(bebob);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900963end:
964 return err;
965}
Takashi Sakamoto618eabe2014-04-25 22:45:20 +0900966
967void snd_bebob_stream_lock_changed(struct snd_bebob *bebob)
968{
969 bebob->dev_lock_changed = true;
970 wake_up(&bebob->hwdep_wait);
971}
972
973int snd_bebob_stream_lock_try(struct snd_bebob *bebob)
974{
975 int err;
976
977 spin_lock_irq(&bebob->lock);
978
979 /* user land lock this */
980 if (bebob->dev_lock_count < 0) {
981 err = -EBUSY;
982 goto end;
983 }
984
985 /* this is the first time */
986 if (bebob->dev_lock_count++ == 0)
987 snd_bebob_stream_lock_changed(bebob);
988 err = 0;
989end:
990 spin_unlock_irq(&bebob->lock);
991 return err;
992}
993
994void snd_bebob_stream_lock_release(struct snd_bebob *bebob)
995{
996 spin_lock_irq(&bebob->lock);
997
998 if (WARN_ON(bebob->dev_lock_count <= 0))
999 goto end;
1000 if (--bebob->dev_lock_count == 0)
1001 snd_bebob_stream_lock_changed(bebob);
1002end:
1003 spin_unlock_irq(&bebob->lock);
1004}