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