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