blob: 0141813815b3a37451afc5cbc92f0a65af3da10f [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
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
Takashi Sakamoto9076c222014-04-25 22:45:25 +0900326 /*
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
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900335 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 }
Takashi Sakamotof65be912015-09-19 11:21:58 +0900343 amdtp_am824_set_midi_position(s, stm_pos);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900344 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;
Takashi Sakamoto49c7b3f2015-09-19 11:22:01 +0900359 if (location >= AM824_MAX_CHANNELS_FOR_PCM) {
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900360 err = -ENOSYS;
361 goto end;
362 }
Takashi Sakamotof65be912015-09-19 11:21:58 +0900363 amdtp_am824_set_pcm_position(s, location,
364 stm_pos);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900365 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{
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900424 int index, pcm_channels, midi_channels, err = 0;
425
426 if (bebob->connected)
427 goto end;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900428
429 /* confirm params for both streams */
Lucas Tanure07905292016-01-25 19:30:23 -0200430 err = get_formation_index(rate, &index);
431 if (err < 0)
432 goto end;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900433 pcm_channels = bebob->tx_stream_formations[index].pcm;
434 midi_channels = bebob->tx_stream_formations[index].midi;
Takashi Sakamoto51c29fd2015-09-19 11:21:56 +0900435 err = amdtp_am824_set_parameters(&bebob->tx_stream, rate,
436 pcm_channels, midi_channels * 8,
437 false);
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900438 if (err < 0)
439 goto end;
440
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900441 pcm_channels = bebob->rx_stream_formations[index].pcm;
442 midi_channels = bebob->rx_stream_formations[index].midi;
Takashi Sakamoto51c29fd2015-09-19 11:21:56 +0900443 err = amdtp_am824_set_parameters(&bebob->rx_stream, rate,
444 pcm_channels, midi_channels * 8,
445 false);
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900446 if (err < 0)
447 goto end;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900448
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));
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900456 if (err < 0) {
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900457 cmp_connection_break(&bebob->out_conn);
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900458 goto end;
459 }
460
461 bebob->connected = true;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900462end:
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);
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900471
472 bebob->connected = false;
Takashi Sakamoto3149ac42014-04-25 22:45:26 +0900473
474 /* These models seems to be in transition state for a longer time. */
475 if (bebob->maudio_special_quirk != NULL)
476 msleep(200);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900477}
478
479static void
480destroy_both_connections(struct snd_bebob *bebob)
481{
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900482 cmp_connection_destroy(&bebob->in_conn);
483 cmp_connection_destroy(&bebob->out_conn);
484}
485
486static int
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900487start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream,
488 unsigned int rate)
489{
490 struct cmp_connection *conn;
491 int err = 0;
492
493 if (stream == &bebob->rx_stream)
494 conn = &bebob->in_conn;
495 else
496 conn = &bebob->out_conn;
497
498 /* channel mapping */
Takashi Sakamoto3149ac42014-04-25 22:45:26 +0900499 if (bebob->maudio_special_quirk == NULL) {
500 err = map_data_channels(bebob, stream);
501 if (err < 0)
502 goto end;
503 }
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900504
505 /* start amdtp stream */
506 err = amdtp_stream_start(stream,
507 conn->resources.channel,
508 conn->speed);
509end:
510 return err;
511}
512
513int snd_bebob_stream_init_duplex(struct snd_bebob *bebob)
514{
515 int err;
516
517 err = init_both_connections(bebob);
518 if (err < 0)
519 goto end;
520
Takashi Sakamoto59558152015-09-19 11:21:55 +0900521 err = amdtp_am824_init(&bebob->tx_stream, bebob->unit,
522 AMDTP_IN_STREAM, CIP_BLOCKING);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900523 if (err < 0) {
524 amdtp_stream_destroy(&bebob->tx_stream);
525 destroy_both_connections(bebob);
526 goto end;
527 }
Takashi Sakamoto14a37ac2016-02-20 16:18:56 +0900528
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900529 bebob->tx_stream.flags |= CIP_SKIP_INIT_DBC_CHECK;
Takashi Sakamotoc4d860a2015-06-14 12:49:35 +0900530
531 /*
532 * BeBoB v3 transfers packets with these qurks:
533 * - In the beginning of streaming, the value of dbc is incremented
534 * even if no data blocks are transferred.
535 * - The value of dbc is reset suddenly.
536 */
537 if (bebob->version > 2)
538 bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC |
539 CIP_SKIP_DBC_ZERO_CHECK;
540
Takashi Sakamoto9d591242014-04-25 22:45:27 +0900541 /*
542 * At high sampling rate, M-Audio special firmware transmits empty
543 * packet with the value of dbc incremented by 8 but the others are
544 * valid to IEC 61883-1.
545 */
546 if (bebob->maudio_special_quirk)
547 bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900548
Takashi Sakamoto59558152015-09-19 11:21:55 +0900549 err = amdtp_am824_init(&bebob->rx_stream, bebob->unit,
550 AMDTP_OUT_STREAM, CIP_BLOCKING);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900551 if (err < 0) {
552 amdtp_stream_destroy(&bebob->tx_stream);
553 amdtp_stream_destroy(&bebob->rx_stream);
554 destroy_both_connections(bebob);
555 }
556end:
557 return err;
558}
559
Takashi Sakamoto4a286d52014-05-28 00:14:40 +0900560int snd_bebob_stream_start_duplex(struct snd_bebob *bebob, unsigned int rate)
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900561{
Julia Lawall6b9866c2015-10-11 08:10:55 +0200562 const struct snd_bebob_rate_spec *rate_spec = bebob->spec->rate;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900563 unsigned int curr_rate;
564 int err = 0;
565
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900566 /* Need no substreams */
Takashi Sakamoto4fd6c6c2016-02-20 16:18:58 +0900567 if (bebob->substreams_counter == 0)
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900568 goto end;
569
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900570 /*
571 * Considering JACK/FFADO streaming:
572 * TODO: This can be removed hwdep functionality becomes popular.
573 */
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900574 err = check_connection_used_by_others(bebob, &bebob->rx_stream);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900575 if (err < 0)
576 goto end;
577
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900578 /*
579 * packet queueing error or detecting discontinuity
580 *
581 * At bus reset, connections should not be broken here. So streams need
582 * to be re-started. This is a reason to use SKIP_INIT_DBC_CHECK flag.
583 */
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900584 if (amdtp_streaming_error(&bebob->rx_stream))
585 amdtp_stream_stop(&bebob->rx_stream);
586 if (amdtp_streaming_error(&bebob->tx_stream))
587 amdtp_stream_stop(&bebob->tx_stream);
588 if (!amdtp_stream_running(&bebob->rx_stream) &&
589 !amdtp_stream_running(&bebob->tx_stream))
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900590 break_both_connections(bebob);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900591
592 /* stop streams if rate is different */
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900593 err = rate_spec->get(bebob, &curr_rate);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900594 if (err < 0) {
595 dev_err(&bebob->unit->device,
596 "fail to get sampling rate: %d\n", err);
597 goto end;
598 }
599 if (rate == 0)
600 rate = curr_rate;
601 if (rate != curr_rate) {
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900602 amdtp_stream_stop(&bebob->rx_stream);
603 amdtp_stream_stop(&bebob->tx_stream);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900604 break_both_connections(bebob);
605 }
606
607 /* master should be always running */
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900608 if (!amdtp_stream_running(&bebob->rx_stream)) {
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900609 /*
610 * NOTE:
611 * If establishing connections at first, Yamaha GO46
612 * (and maybe Terratec X24) don't generate sound.
Takashi Sakamoto3149ac42014-04-25 22:45:26 +0900613 *
614 * For firmware customized by M-Audio, refer to next NOTE.
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900615 */
Takashi Sakamoto3149ac42014-04-25 22:45:26 +0900616 if (bebob->maudio_special_quirk == NULL) {
617 err = rate_spec->set(bebob, rate);
618 if (err < 0) {
619 dev_err(&bebob->unit->device,
620 "fail to set sampling rate: %d\n",
621 err);
622 goto end;
623 }
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900624 }
625
626 err = make_both_connections(bebob, rate);
627 if (err < 0)
628 goto end;
629
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900630 err = start_stream(bebob, &bebob->rx_stream, rate);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900631 if (err < 0) {
632 dev_err(&bebob->unit->device,
633 "fail to run AMDTP master stream:%d\n", err);
634 break_both_connections(bebob);
635 goto end;
636 }
637
Takashi Sakamoto3149ac42014-04-25 22:45:26 +0900638 /*
639 * NOTE:
640 * The firmware customized by M-Audio uses these commands to
641 * start transmitting stream. This is not usual way.
642 */
643 if (bebob->maudio_special_quirk != NULL) {
644 err = rate_spec->set(bebob, rate);
645 if (err < 0) {
646 dev_err(&bebob->unit->device,
647 "fail to ensure sampling rate: %d\n",
648 err);
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900649 amdtp_stream_stop(&bebob->rx_stream);
Takashi Sakamoto3149ac42014-04-25 22:45:26 +0900650 break_both_connections(bebob);
651 goto end;
652 }
653 }
654
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900655 /* wait first callback */
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900656 if (!amdtp_stream_wait_callback(&bebob->rx_stream,
657 CALLBACK_TIMEOUT)) {
658 amdtp_stream_stop(&bebob->rx_stream);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900659 break_both_connections(bebob);
660 err = -ETIMEDOUT;
661 goto end;
662 }
663 }
664
665 /* start slave if needed */
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900666 if (!amdtp_stream_running(&bebob->tx_stream)) {
667 err = start_stream(bebob, &bebob->tx_stream, rate);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900668 if (err < 0) {
669 dev_err(&bebob->unit->device,
670 "fail to run AMDTP slave stream:%d\n", err);
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900671 amdtp_stream_stop(&bebob->rx_stream);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900672 break_both_connections(bebob);
673 goto end;
674 }
675
676 /* wait first callback */
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900677 if (!amdtp_stream_wait_callback(&bebob->tx_stream,
678 CALLBACK_TIMEOUT)) {
679 amdtp_stream_stop(&bebob->tx_stream);
680 amdtp_stream_stop(&bebob->rx_stream);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900681 break_both_connections(bebob);
682 err = -ETIMEDOUT;
683 }
684 }
685end:
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900686 return err;
687}
688
689void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob)
690{
Takashi Sakamoto4fd6c6c2016-02-20 16:18:58 +0900691 if (bebob->substreams_counter == 0) {
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900692 amdtp_stream_pcm_abort(&bebob->rx_stream);
693 amdtp_stream_stop(&bebob->rx_stream);
Takashi Sakamoto8d1c2692015-06-14 12:49:36 +0900694
Takashi Sakamotoc71283c2016-05-09 23:15:50 +0900695 amdtp_stream_pcm_abort(&bebob->tx_stream);
696 amdtp_stream_stop(&bebob->tx_stream);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900697
Takashi Sakamoto8d1c2692015-06-14 12:49:36 +0900698 break_both_connections(bebob);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900699 }
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900700}
701
Takashi Sakamotod23c2cc2015-02-21 23:54:59 +0900702/*
703 * This function should be called before starting streams or after stopping
704 * streams.
705 */
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900706void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob)
707{
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900708 amdtp_stream_destroy(&bebob->rx_stream);
709 amdtp_stream_destroy(&bebob->tx_stream);
710
711 destroy_both_connections(bebob);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900712}
713
714/*
715 * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’
716 * in Additional AVC commands (Nov 2003, BridgeCo)
717 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
718 */
719static int
720parse_stream_formation(u8 *buf, unsigned int len,
721 struct snd_bebob_stream_formation *formation)
722{
723 unsigned int i, e, channels, format;
724
725 /*
726 * this module can support a hierarchy combination that:
727 * Root: Audio and Music (0x90)
728 * Level 1: AM824 Compound (0x40)
729 */
730 if ((buf[0] != 0x90) || (buf[1] != 0x40))
731 return -ENOSYS;
732
733 /* check sampling rate */
734 for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) {
735 if (buf[2] == bridgeco_freq_table[i])
736 break;
737 }
Dan Carpenter33a5f982014-05-28 19:43:30 +0300738 if (i == ARRAY_SIZE(bridgeco_freq_table))
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900739 return -ENOSYS;
740
741 /* Avoid double count by different entries for the same rate. */
742 memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation));
743
744 for (e = 0; e < buf[4]; e++) {
745 channels = buf[5 + e * 2];
746 format = buf[6 + e * 2];
747
748 switch (format) {
Takashi Sakamoto51fa31d2014-05-28 00:14:47 +0900749 /* IEC 60958 Conformant, currently handled as MBLA */
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900750 case 0x00:
751 /* Multi bit linear audio */
752 case 0x06: /* Raw */
753 formation[i].pcm += channels;
754 break;
755 /* MIDI Conformant */
756 case 0x0d:
757 formation[i].midi += channels;
758 break;
759 /* IEC 61937-3 to 7 */
760 case 0x01:
761 case 0x02:
762 case 0x03:
763 case 0x04:
764 case 0x05:
765 /* Multi bit linear audio */
766 case 0x07: /* DVD-Audio */
767 case 0x0c: /* High Precision */
768 /* One Bit Audio */
769 case 0x08: /* (Plain) Raw */
770 case 0x09: /* (Plain) SACD */
771 case 0x0a: /* (Encoded) Raw */
772 case 0x0b: /* (Encoded) SACD */
773 /* Synchronization Stream (Stereo Raw audio) */
774 case 0x40:
775 /* Don't care */
776 case 0xff:
777 default:
778 return -ENOSYS; /* not supported */
779 }
780 }
781
Takashi Sakamoto49c7b3f2015-09-19 11:22:01 +0900782 if (formation[i].pcm > AM824_MAX_CHANNELS_FOR_PCM ||
783 formation[i].midi > AM824_MAX_CHANNELS_FOR_MIDI)
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900784 return -ENOSYS;
785
786 return 0;
787}
788
789static int
790fill_stream_formations(struct snd_bebob *bebob, enum avc_bridgeco_plug_dir dir,
791 unsigned short pid)
792{
793 u8 *buf;
794 struct snd_bebob_stream_formation *formations;
795 unsigned int len, eid;
796 u8 addr[AVC_BRIDGECO_ADDR_BYTES];
797 int err;
798
799 buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL);
800 if (buf == NULL)
801 return -ENOMEM;
802
803 if (dir == AVC_BRIDGECO_PLUG_DIR_IN)
804 formations = bebob->rx_stream_formations;
805 else
806 formations = bebob->tx_stream_formations;
807
808 for (eid = 0; eid < SND_BEBOB_STRM_FMT_ENTRIES; eid++) {
809 len = FORMAT_MAXIMUM_LENGTH;
810 avc_bridgeco_fill_unit_addr(addr, dir,
811 AVC_BRIDGECO_PLUG_UNIT_ISOC, pid);
812 err = avc_bridgeco_get_plug_strm_fmt(bebob->unit, addr, buf,
813 &len, eid);
814 /* No entries remained. */
815 if (err == -EINVAL && eid > 0) {
816 err = 0;
817 break;
818 } else if (err < 0) {
819 dev_err(&bebob->unit->device,
820 "fail to get stream format %d for isoc %s plug %d:%d\n",
821 eid,
822 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
823 "out",
824 pid, err);
825 break;
826 }
827
828 err = parse_stream_formation(buf, len, formations);
829 if (err < 0)
830 break;
831 }
832
833 kfree(buf);
834 return err;
835}
836
837static int
838seek_msu_sync_input_plug(struct snd_bebob *bebob)
839{
840 u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
Takashi Sakamotoa6b598b2014-05-28 00:14:41 +0900841 unsigned int i;
842 enum avc_bridgeco_plug_type type;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900843 int err;
844
845 /* Get the number of Music Sub Unit for both direction. */
846 err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs);
847 if (err < 0) {
848 dev_err(&bebob->unit->device,
849 "fail to get info for MSU in/out plugs: %d\n",
850 err);
851 goto end;
852 }
853
854 /* seek destination plugs for 'MSU sync input' */
855 bebob->sync_input_plug = -1;
856 for (i = 0; i < plugs[0]; i++) {
857 avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, i);
858 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
859 if (err < 0) {
860 dev_err(&bebob->unit->device,
861 "fail to get type for MSU in plug %d: %d\n",
862 i, err);
863 goto end;
864 }
865
866 if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
867 bebob->sync_input_plug = i;
868 break;
869 }
870 }
871end:
872 return err;
873}
874
875int snd_bebob_stream_discover(struct snd_bebob *bebob)
876{
Julia Lawall6b9866c2015-10-11 08:10:55 +0200877 const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900878 u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
879 enum avc_bridgeco_plug_type type;
880 unsigned int i;
881 int err;
882
883 /* the number of plugs for isoc in/out, ext in/out */
884 err = avc_general_get_plug_info(bebob->unit, 0x1f, 0x07, 0x00, plugs);
885 if (err < 0) {
886 dev_err(&bebob->unit->device,
887 "fail to get info for isoc/external in/out plugs: %d\n",
888 err);
889 goto end;
890 }
891
892 /*
893 * This module supports at least one isoc input plug and one isoc
894 * output plug.
895 */
896 if ((plugs[0] == 0) || (plugs[1] == 0)) {
897 err = -ENOSYS;
898 goto end;
899 }
900
901 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
902 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
903 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
904 if (err < 0) {
905 dev_err(&bebob->unit->device,
906 "fail to get type for isoc in plug 0: %d\n", err);
907 goto end;
908 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
909 err = -ENOSYS;
910 goto end;
911 }
912 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_IN, 0);
913 if (err < 0)
914 goto end;
915
916 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
917 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
918 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
919 if (err < 0) {
920 dev_err(&bebob->unit->device,
921 "fail to get type for isoc out plug 0: %d\n", err);
922 goto end;
923 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
924 err = -ENOSYS;
925 goto end;
926 }
927 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_OUT, 0);
928 if (err < 0)
929 goto end;
930
931 /* count external input plugs for MIDI */
932 bebob->midi_input_ports = 0;
933 for (i = 0; i < plugs[2]; i++) {
934 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
935 AVC_BRIDGECO_PLUG_UNIT_EXT, i);
936 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
937 if (err < 0) {
938 dev_err(&bebob->unit->device,
939 "fail to get type for external in plug %d: %d\n",
940 i, err);
941 goto end;
942 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
943 bebob->midi_input_ports++;
944 }
945 }
946
947 /* count external output plugs for MIDI */
948 bebob->midi_output_ports = 0;
949 for (i = 0; i < plugs[3]; i++) {
950 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
951 AVC_BRIDGECO_PLUG_UNIT_EXT, i);
952 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
953 if (err < 0) {
954 dev_err(&bebob->unit->device,
955 "fail to get type for external out plug %d: %d\n",
956 i, err);
957 goto end;
958 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
959 bebob->midi_output_ports++;
960 }
961 }
962
963 /* for check source of clock later */
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900964 if (!clk_spec)
965 err = seek_msu_sync_input_plug(bebob);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900966end:
967 return err;
968}
Takashi Sakamoto618eabe2014-04-25 22:45:20 +0900969
970void snd_bebob_stream_lock_changed(struct snd_bebob *bebob)
971{
972 bebob->dev_lock_changed = true;
973 wake_up(&bebob->hwdep_wait);
974}
975
976int snd_bebob_stream_lock_try(struct snd_bebob *bebob)
977{
978 int err;
979
980 spin_lock_irq(&bebob->lock);
981
982 /* user land lock this */
983 if (bebob->dev_lock_count < 0) {
984 err = -EBUSY;
985 goto end;
986 }
987
988 /* this is the first time */
989 if (bebob->dev_lock_count++ == 0)
990 snd_bebob_stream_lock_changed(bebob);
991 err = 0;
992end:
993 spin_unlock_irq(&bebob->lock);
994 return err;
995}
996
997void snd_bebob_stream_lock_release(struct snd_bebob *bebob)
998{
999 spin_lock_irq(&bebob->lock);
1000
1001 if (WARN_ON(bebob->dev_lock_count <= 0))
1002 goto end;
1003 if (--bebob->dev_lock_count == 0)
1004 snd_bebob_stream_lock_changed(bebob);
1005end:
1006 spin_unlock_irq(&bebob->lock);
1007}