Commit | Line | Data |
---|---|---|
da607e19 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
eb7b3a05 TS |
2 | /* |
3 | * bebob_stream.c - a part of driver for BeBoB based devices | |
4 | * | |
5 | * Copyright (c) 2013-2014 Takashi Sakamoto | |
eb7b3a05 TS |
6 | */ |
7 | ||
8 | #include "./bebob.h" | |
9 | ||
5ec85c19 | 10 | #define READY_TIMEOUT_MS 4000 |
eb7b3a05 TS |
11 | |
12 | /* | |
13 | * NOTE; | |
14 | * For BeBoB streams, Both of input and output CMP connection are important. | |
15 | * | |
16 | * For most devices, each CMP connection starts to transmit/receive a | |
17 | * corresponding stream. But for a few devices, both of CMP connection needs | |
18 | * to start transmitting stream. An example is 'M-Audio Firewire 410'. | |
19 | */ | |
20 | ||
21 | /* 128 is an arbitrary length but it seems to be enough */ | |
22 | #define FORMAT_MAXIMUM_LENGTH 128 | |
23 | ||
24 | const unsigned int snd_bebob_rate_table[SND_BEBOB_STRM_FMT_ENTRIES] = { | |
25 | [0] = 32000, | |
26 | [1] = 44100, | |
27 | [2] = 48000, | |
28 | [3] = 88200, | |
29 | [4] = 96000, | |
30 | [5] = 176400, | |
31 | [6] = 192000, | |
32 | }; | |
33 | ||
34 | /* | |
35 | * See: Table 51: Extended Stream Format Info ‘Sampling Frequency’ | |
36 | * in Additional AVC commands (Nov 2003, BridgeCo) | |
37 | */ | |
38 | static const unsigned int bridgeco_freq_table[] = { | |
39 | [0] = 0x02, | |
40 | [1] = 0x03, | |
41 | [2] = 0x04, | |
42 | [3] = 0x0a, | |
43 | [4] = 0x05, | |
44 | [5] = 0x06, | |
45 | [6] = 0x07, | |
46 | }; | |
47 | ||
07905298 LT |
48 | static int |
49 | get_formation_index(unsigned int rate, unsigned int *index) | |
eb7b3a05 TS |
50 | { |
51 | unsigned int i; | |
52 | ||
53 | for (i = 0; i < ARRAY_SIZE(snd_bebob_rate_table); i++) { | |
07905298 LT |
54 | if (snd_bebob_rate_table[i] == rate) { |
55 | *index = i; | |
56 | return 0; | |
57 | } | |
eb7b3a05 TS |
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 | { |
6b9866c8 | 122 | const 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 | ||
f2bbdbcb | 254 | static int map_data_channels(struct snd_bebob *bebob, struct amdtp_stream *s) |
eb7b3a05 TS |
255 | { |
256 | unsigned int sec, sections, ch, channels; | |
257 | unsigned int pcm, midi, location; | |
258 | unsigned int stm_pos, sec_loc, pos; | |
259 | u8 *buf, addr[AVC_BRIDGECO_ADDR_BYTES], type; | |
260 | enum avc_bridgeco_plug_dir dir; | |
261 | int err; | |
262 | ||
263 | /* | |
264 | * The length of return value of this command cannot be expected. Here | |
265 | * use the maximum length of FCP. | |
266 | */ | |
267 | buf = kzalloc(256, GFP_KERNEL); | |
268 | if (buf == NULL) | |
269 | return -ENOMEM; | |
270 | ||
271 | if (s == &bebob->tx_stream) | |
272 | dir = AVC_BRIDGECO_PLUG_DIR_OUT; | |
273 | else | |
274 | dir = AVC_BRIDGECO_PLUG_DIR_IN; | |
275 | ||
276 | avc_bridgeco_fill_unit_addr(addr, dir, AVC_BRIDGECO_PLUG_UNIT_ISOC, 0); | |
277 | err = avc_bridgeco_get_plug_ch_pos(bebob->unit, addr, buf, 256); | |
278 | if (err < 0) { | |
279 | dev_err(&bebob->unit->device, | |
280 | "fail to get channel position for isoc %s plug 0: %d\n", | |
281 | (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" : "out", | |
282 | err); | |
283 | goto end; | |
284 | } | |
285 | pos = 0; | |
286 | ||
287 | /* positions in I/O buffer */ | |
288 | pcm = 0; | |
289 | midi = 0; | |
290 | ||
291 | /* the number of sections in AMDTP packet */ | |
292 | sections = buf[pos++]; | |
293 | ||
294 | for (sec = 0; sec < sections; sec++) { | |
295 | /* type of this section */ | |
296 | avc_bridgeco_fill_unit_addr(addr, dir, | |
297 | AVC_BRIDGECO_PLUG_UNIT_ISOC, 0); | |
298 | err = avc_bridgeco_get_plug_section_type(bebob->unit, addr, | |
299 | sec, &type); | |
300 | if (err < 0) { | |
301 | dev_err(&bebob->unit->device, | |
302 | "fail to get section type for isoc %s plug 0: %d\n", | |
303 | (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" : | |
304 | "out", | |
305 | err); | |
306 | goto end; | |
307 | } | |
308 | /* NoType */ | |
309 | if (type == 0xff) { | |
310 | err = -ENOSYS; | |
311 | goto end; | |
312 | } | |
313 | ||
314 | /* the number of channels in this section */ | |
315 | channels = buf[pos++]; | |
316 | ||
317 | for (ch = 0; ch < channels; ch++) { | |
318 | /* position of this channel in AMDTP packet */ | |
319 | stm_pos = buf[pos++] - 1; | |
320 | /* location of this channel in this section */ | |
321 | sec_loc = buf[pos++] - 1; | |
322 | ||
9076c22d TS |
323 | /* |
324 | * Basically the number of location is within the | |
325 | * number of channels in this section. But some models | |
326 | * of M-Audio don't follow this. Its location for MIDI | |
327 | * is the position of MIDI channels in AMDTP packet. | |
328 | */ | |
329 | if (sec_loc >= channels) | |
330 | sec_loc = ch; | |
331 | ||
eb7b3a05 TS |
332 | switch (type) { |
333 | /* for MIDI conformant data channel */ | |
334 | case 0x0a: | |
335 | /* AMDTP_MAX_CHANNELS_FOR_MIDI is 1. */ | |
336 | if ((midi > 0) && (stm_pos != midi)) { | |
337 | err = -ENOSYS; | |
338 | goto end; | |
339 | } | |
f65be911 | 340 | amdtp_am824_set_midi_position(s, stm_pos); |
eb7b3a05 TS |
341 | midi = stm_pos; |
342 | break; | |
343 | /* for PCM data channel */ | |
344 | case 0x01: /* Headphone */ | |
345 | case 0x02: /* Microphone */ | |
346 | case 0x03: /* Line */ | |
347 | case 0x04: /* SPDIF */ | |
348 | case 0x05: /* ADAT */ | |
349 | case 0x06: /* TDIF */ | |
350 | case 0x07: /* MADI */ | |
351 | /* for undefined/changeable signal */ | |
352 | case 0x08: /* Analog */ | |
353 | case 0x09: /* Digital */ | |
354 | default: | |
355 | location = pcm + sec_loc; | |
49c7b3fc | 356 | if (location >= AM824_MAX_CHANNELS_FOR_PCM) { |
eb7b3a05 TS |
357 | err = -ENOSYS; |
358 | goto end; | |
359 | } | |
f65be911 TS |
360 | amdtp_am824_set_pcm_position(s, location, |
361 | stm_pos); | |
eb7b3a05 TS |
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 | ||
eb7b3a05 TS |
376 | static int |
377 | check_connection_used_by_others(struct snd_bebob *bebob, struct amdtp_stream *s) | |
378 | { | |
379 | struct cmp_connection *conn; | |
380 | bool used; | |
381 | int err; | |
382 | ||
383 | if (s == &bebob->tx_stream) | |
384 | conn = &bebob->out_conn; | |
385 | else | |
386 | conn = &bebob->in_conn; | |
387 | ||
388 | err = cmp_connection_check_used(conn, &used); | |
389 | if ((err >= 0) && used && !amdtp_stream_running(s)) { | |
390 | dev_err(&bebob->unit->device, | |
391 | "Connection established by others: %cPCR[%d]\n", | |
392 | (conn->direction == CMP_OUTPUT) ? 'o' : 'i', | |
393 | conn->pcr_index); | |
394 | err = -EBUSY; | |
395 | } | |
396 | ||
397 | return err; | |
398 | } | |
399 | ||
d3eabe93 | 400 | static void break_both_connections(struct snd_bebob *bebob) |
eb7b3a05 TS |
401 | { |
402 | cmp_connection_break(&bebob->in_conn); | |
403 | cmp_connection_break(&bebob->out_conn); | |
b6bc8123 | 404 | |
d3eabe93 TS |
405 | // These models seem to be in transition state for a longer time. When |
406 | // accessing in the state, any transactions is corrupted. In the worst | |
407 | // case, the device is going to reboot. | |
408 | if (bebob->version < 2) | |
409 | msleep(600); | |
eb7b3a05 TS |
410 | } |
411 | ||
6c94f380 | 412 | static int start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream) |
eb7b3a05 TS |
413 | { |
414 | struct cmp_connection *conn; | |
415 | int err = 0; | |
416 | ||
417 | if (stream == &bebob->rx_stream) | |
418 | conn = &bebob->in_conn; | |
419 | else | |
420 | conn = &bebob->out_conn; | |
421 | ||
6c94f380 | 422 | // channel mapping. |
3149ac48 TS |
423 | if (bebob->maudio_special_quirk == NULL) { |
424 | err = map_data_channels(bebob, stream); | |
425 | if (err < 0) | |
6c94f380 | 426 | return err; |
3149ac48 | 427 | } |
eb7b3a05 | 428 | |
6c94f380 TS |
429 | err = cmp_connection_establish(conn); |
430 | if (err < 0) | |
431 | return err; | |
432 | ||
433 | return amdtp_domain_add_stream(&bebob->domain, stream, | |
434 | conn->resources.channel, conn->speed); | |
eb7b3a05 TS |
435 | } |
436 | ||
33e41a5c | 437 | static int init_stream(struct snd_bebob *bebob, struct amdtp_stream *stream) |
eb7b3a05 | 438 | { |
33e41a5c TS |
439 | enum amdtp_stream_direction dir_stream; |
440 | struct cmp_connection *conn; | |
441 | enum cmp_direction dir_conn; | |
eb7b3a05 TS |
442 | int err; |
443 | ||
33e41a5c TS |
444 | if (stream == &bebob->tx_stream) { |
445 | dir_stream = AMDTP_IN_STREAM; | |
446 | conn = &bebob->out_conn; | |
447 | dir_conn = CMP_OUTPUT; | |
448 | } else { | |
449 | dir_stream = AMDTP_OUT_STREAM; | |
450 | conn = &bebob->in_conn; | |
451 | dir_conn = CMP_INPUT; | |
452 | } | |
453 | ||
454 | err = cmp_connection_init(conn, bebob->unit, dir_conn, 0); | |
eb7b3a05 | 455 | if (err < 0) |
33e41a5c | 456 | return err; |
eb7b3a05 | 457 | |
33e41a5c | 458 | err = amdtp_am824_init(stream, bebob->unit, dir_stream, CIP_BLOCKING); |
eb7b3a05 | 459 | if (err < 0) { |
33e41a5c TS |
460 | cmp_connection_destroy(conn); |
461 | return err; | |
eb7b3a05 | 462 | } |
14a37ac1 | 463 | |
33e41a5c TS |
464 | if (stream == &bebob->tx_stream) { |
465 | // BeBoB v3 transfers packets with these qurks: | |
466 | // - In the beginning of streaming, the value of dbc is | |
467 | // incremented even if no data blocks are transferred. | |
468 | // - The value of dbc is reset suddenly. | |
469 | if (bebob->version > 2) | |
470 | bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC | | |
471 | CIP_SKIP_DBC_ZERO_CHECK; | |
472 | ||
473 | // At high sampling rate, M-Audio special firmware transmits | |
474 | // empty packet with the value of dbc incremented by 8 but the | |
475 | // others are valid to IEC 61883-1. | |
476 | if (bebob->maudio_special_quirk) | |
477 | bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC; | |
478 | } | |
c4d860a0 | 479 | |
33e41a5c TS |
480 | return 0; |
481 | } | |
482 | ||
483 | static void destroy_stream(struct snd_bebob *bebob, struct amdtp_stream *stream) | |
484 | { | |
485 | amdtp_stream_destroy(stream); | |
486 | ||
487 | if (stream == &bebob->tx_stream) | |
488 | cmp_connection_destroy(&bebob->out_conn); | |
489 | else | |
490 | cmp_connection_destroy(&bebob->in_conn); | |
491 | } | |
eb7b3a05 | 492 | |
33e41a5c TS |
493 | int snd_bebob_stream_init_duplex(struct snd_bebob *bebob) |
494 | { | |
495 | int err; | |
496 | ||
497 | err = init_stream(bebob, &bebob->tx_stream); | |
498 | if (err < 0) | |
499 | return err; | |
500 | ||
501 | err = init_stream(bebob, &bebob->rx_stream); | |
eb7b3a05 | 502 | if (err < 0) { |
33e41a5c TS |
503 | destroy_stream(bebob, &bebob->tx_stream); |
504 | return err; | |
eb7b3a05 | 505 | } |
33e41a5c | 506 | |
b0db4d51 TS |
507 | err = amdtp_domain_init(&bebob->domain); |
508 | if (err < 0) { | |
509 | destroy_stream(bebob, &bebob->tx_stream); | |
510 | destroy_stream(bebob, &bebob->rx_stream); | |
511 | } | |
512 | ||
513 | return err; | |
eb7b3a05 TS |
514 | } |
515 | ||
ac2888b9 TS |
516 | static int keep_resources(struct snd_bebob *bebob, struct amdtp_stream *stream, |
517 | unsigned int rate, unsigned int index) | |
eb7b3a05 | 518 | { |
d2b6f15b TS |
519 | unsigned int pcm_channels; |
520 | unsigned int midi_ports; | |
7bc93821 TS |
521 | struct cmp_connection *conn; |
522 | int err; | |
eb7b3a05 | 523 | |
7bc93821 | 524 | if (stream == &bebob->tx_stream) { |
d2b6f15b TS |
525 | pcm_channels = bebob->tx_stream_formations[index].pcm; |
526 | midi_ports = bebob->midi_input_ports; | |
7bc93821 TS |
527 | conn = &bebob->out_conn; |
528 | } else { | |
d2b6f15b TS |
529 | pcm_channels = bebob->rx_stream_formations[index].pcm; |
530 | midi_ports = bebob->midi_output_ports; | |
7bc93821 TS |
531 | conn = &bebob->in_conn; |
532 | } | |
533 | ||
d2b6f15b | 534 | err = amdtp_am824_set_parameters(stream, rate, pcm_channels, midi_ports, false); |
7bc93821 TS |
535 | if (err < 0) |
536 | return err; | |
eb7b3a05 | 537 | |
7bc93821 | 538 | return cmp_connection_reserve(conn, amdtp_stream_get_max_payload(stream)); |
ac2888b9 TS |
539 | } |
540 | ||
8737209f | 541 | int snd_bebob_stream_reserve_duplex(struct snd_bebob *bebob, unsigned int rate, |
1fde7a44 TS |
542 | unsigned int frames_per_period, |
543 | unsigned int frames_per_buffer) | |
ac2888b9 TS |
544 | { |
545 | unsigned int curr_rate; | |
546 | int err; | |
547 | ||
548 | // Considering JACK/FFADO streaming: | |
549 | // TODO: This can be removed hwdep functionality becomes popular. | |
c71283cb | 550 | err = check_connection_used_by_others(bebob, &bebob->rx_stream); |
eb7b3a05 | 551 | if (err < 0) |
ac2888b9 | 552 | return err; |
eb7b3a05 | 553 | |
ac2888b9 TS |
554 | err = bebob->spec->rate->get(bebob, &curr_rate); |
555 | if (err < 0) | |
556 | return err; | |
557 | if (rate == 0) | |
558 | rate = curr_rate; | |
559 | if (curr_rate != rate) { | |
b0db4d51 | 560 | amdtp_domain_stop(&bebob->domain); |
b6bc8123 | 561 | break_both_connections(bebob); |
7599e279 TS |
562 | |
563 | cmp_connection_release(&bebob->out_conn); | |
564 | cmp_connection_release(&bebob->in_conn); | |
ac2888b9 | 565 | } |
eb7b3a05 | 566 | |
ac2888b9 TS |
567 | if (bebob->substreams_counter == 0 || curr_rate != rate) { |
568 | unsigned int index; | |
569 | ||
570 | // NOTE: | |
571 | // If establishing connections at first, Yamaha GO46 | |
572 | // (and maybe Terratec X24) don't generate sound. | |
573 | // | |
574 | // For firmware customized by M-Audio, refer to next NOTE. | |
575 | err = bebob->spec->rate->set(bebob, rate); | |
576 | if (err < 0) { | |
577 | dev_err(&bebob->unit->device, | |
578 | "fail to set sampling rate: %d\n", | |
579 | err); | |
580 | return err; | |
581 | } | |
582 | ||
583 | err = get_formation_index(rate, &index); | |
584 | if (err < 0) | |
585 | return err; | |
586 | ||
587 | err = keep_resources(bebob, &bebob->tx_stream, rate, index); | |
588 | if (err < 0) | |
589 | return err; | |
590 | ||
591 | err = keep_resources(bebob, &bebob->rx_stream, rate, index); | |
7bc93821 TS |
592 | if (err < 0) { |
593 | cmp_connection_release(&bebob->out_conn); | |
ac2888b9 | 594 | return err; |
7bc93821 | 595 | } |
8737209f TS |
596 | |
597 | err = amdtp_domain_set_events_per_period(&bebob->domain, | |
1fde7a44 | 598 | frames_per_period, frames_per_buffer); |
8737209f TS |
599 | if (err < 0) { |
600 | cmp_connection_release(&bebob->out_conn); | |
601 | cmp_connection_release(&bebob->in_conn); | |
602 | return err; | |
603 | } | |
eb7b3a05 | 604 | } |
ac2888b9 TS |
605 | |
606 | return 0; | |
607 | } | |
608 | ||
609 | int snd_bebob_stream_start_duplex(struct snd_bebob *bebob) | |
610 | { | |
611 | int err; | |
612 | ||
613 | // Need no substreams. | |
614 | if (bebob->substreams_counter == 0) | |
615 | return -EIO; | |
616 | ||
617 | // packet queueing error or detecting discontinuity | |
618 | if (amdtp_streaming_error(&bebob->rx_stream) || | |
619 | amdtp_streaming_error(&bebob->tx_stream)) { | |
b0db4d51 | 620 | amdtp_domain_stop(&bebob->domain); |
eb7b3a05 TS |
621 | break_both_connections(bebob); |
622 | } | |
623 | ||
c71283cb | 624 | if (!amdtp_stream_running(&bebob->rx_stream)) { |
6c94f380 | 625 | enum snd_bebob_clock_type src; |
ac2888b9 | 626 | unsigned int curr_rate; |
26541cb1 | 627 | unsigned int tx_init_skip_cycles; |
ac2888b9 TS |
628 | |
629 | if (bebob->maudio_special_quirk) { | |
630 | err = bebob->spec->rate->get(bebob, &curr_rate); | |
631 | if (err < 0) | |
632 | return err; | |
eb7b3a05 TS |
633 | } |
634 | ||
6c94f380 | 635 | err = snd_bebob_stream_get_clock_src(bebob, &src); |
eb7b3a05 | 636 | if (err < 0) |
ac2888b9 | 637 | return err; |
eb7b3a05 | 638 | |
266807f9 | 639 | err = start_stream(bebob, &bebob->rx_stream); |
b0db4d51 TS |
640 | if (err < 0) |
641 | goto error; | |
642 | ||
266807f9 | 643 | err = start_stream(bebob, &bebob->tx_stream); |
b0db4d51 TS |
644 | if (err < 0) |
645 | goto error; | |
646 | ||
5ec85c19 TS |
647 | if (!bebob->discontinuity_quirk) |
648 | tx_init_skip_cycles = 0; | |
da5e8337 | 649 | else |
26541cb1 | 650 | tx_init_skip_cycles = 16000; |
5ec85c19 TS |
651 | |
652 | // MEMO: In the early stage of packet streaming, the device transfers NODATA packets. | |
653 | // After several hundred cycles, it begins to multiplex event into the packet with | |
654 | // syt information. | |
26541cb1 | 655 | err = amdtp_domain_start(&bebob->domain, tx_init_skip_cycles); |
b0db4d51 | 656 | if (err < 0) |
ac2888b9 | 657 | goto error; |
eb7b3a05 | 658 | |
ac2888b9 TS |
659 | // NOTE: |
660 | // The firmware customized by M-Audio uses these commands to | |
661 | // start transmitting stream. This is not usual way. | |
662 | if (bebob->maudio_special_quirk) { | |
663 | err = bebob->spec->rate->set(bebob, curr_rate); | |
3149ac48 TS |
664 | if (err < 0) { |
665 | dev_err(&bebob->unit->device, | |
666 | "fail to ensure sampling rate: %d\n", | |
667 | err); | |
ac2888b9 | 668 | goto error; |
3149ac48 TS |
669 | } |
670 | } | |
671 | ||
26541cb1 TS |
672 | // Some devices postpone start of transmission mostly for 1 sec after receives |
673 | // packets firstly. | |
bdaedca7 | 674 | if (!amdtp_domain_wait_ready(&bebob->domain, READY_TIMEOUT_MS)) { |
eb7b3a05 | 675 | err = -ETIMEDOUT; |
ac2888b9 | 676 | goto error; |
eb7b3a05 TS |
677 | } |
678 | } | |
ac2888b9 TS |
679 | |
680 | return 0; | |
681 | error: | |
b0db4d51 | 682 | amdtp_domain_stop(&bebob->domain); |
ac2888b9 | 683 | break_both_connections(bebob); |
eb7b3a05 TS |
684 | return err; |
685 | } | |
686 | ||
687 | void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob) | |
688 | { | |
4fd6c6c7 | 689 | if (bebob->substreams_counter == 0) { |
b0db4d51 | 690 | amdtp_domain_stop(&bebob->domain); |
8d1c2694 | 691 | break_both_connections(bebob); |
7bc93821 TS |
692 | |
693 | cmp_connection_release(&bebob->out_conn); | |
694 | cmp_connection_release(&bebob->in_conn); | |
eb7b3a05 | 695 | } |
eb7b3a05 TS |
696 | } |
697 | ||
d23c2cc4 TS |
698 | /* |
699 | * This function should be called before starting streams or after stopping | |
700 | * streams. | |
701 | */ | |
eb7b3a05 TS |
702 | void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob) |
703 | { | |
b0db4d51 TS |
704 | amdtp_domain_destroy(&bebob->domain); |
705 | ||
33e41a5c TS |
706 | destroy_stream(bebob, &bebob->tx_stream); |
707 | destroy_stream(bebob, &bebob->rx_stream); | |
eb7b3a05 TS |
708 | } |
709 | ||
710 | /* | |
711 | * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’ | |
712 | * in Additional AVC commands (Nov 2003, BridgeCo) | |
713 | * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005 | |
714 | */ | |
715 | static int | |
716 | parse_stream_formation(u8 *buf, unsigned int len, | |
717 | struct snd_bebob_stream_formation *formation) | |
718 | { | |
719 | unsigned int i, e, channels, format; | |
720 | ||
721 | /* | |
722 | * this module can support a hierarchy combination that: | |
723 | * Root: Audio and Music (0x90) | |
724 | * Level 1: AM824 Compound (0x40) | |
725 | */ | |
726 | if ((buf[0] != 0x90) || (buf[1] != 0x40)) | |
727 | return -ENOSYS; | |
728 | ||
729 | /* check sampling rate */ | |
730 | for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) { | |
731 | if (buf[2] == bridgeco_freq_table[i]) | |
732 | break; | |
733 | } | |
33a5f989 | 734 | if (i == ARRAY_SIZE(bridgeco_freq_table)) |
eb7b3a05 TS |
735 | return -ENOSYS; |
736 | ||
737 | /* Avoid double count by different entries for the same rate. */ | |
738 | memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation)); | |
739 | ||
740 | for (e = 0; e < buf[4]; e++) { | |
741 | channels = buf[5 + e * 2]; | |
742 | format = buf[6 + e * 2]; | |
743 | ||
744 | switch (format) { | |
51fa31d4 | 745 | /* IEC 60958 Conformant, currently handled as MBLA */ |
eb7b3a05 TS |
746 | case 0x00: |
747 | /* Multi bit linear audio */ | |
748 | case 0x06: /* Raw */ | |
749 | formation[i].pcm += channels; | |
750 | break; | |
751 | /* MIDI Conformant */ | |
752 | case 0x0d: | |
753 | formation[i].midi += channels; | |
754 | break; | |
755 | /* IEC 61937-3 to 7 */ | |
756 | case 0x01: | |
757 | case 0x02: | |
758 | case 0x03: | |
759 | case 0x04: | |
760 | case 0x05: | |
761 | /* Multi bit linear audio */ | |
762 | case 0x07: /* DVD-Audio */ | |
763 | case 0x0c: /* High Precision */ | |
764 | /* One Bit Audio */ | |
765 | case 0x08: /* (Plain) Raw */ | |
766 | case 0x09: /* (Plain) SACD */ | |
767 | case 0x0a: /* (Encoded) Raw */ | |
768 | case 0x0b: /* (Encoded) SACD */ | |
769 | /* Synchronization Stream (Stereo Raw audio) */ | |
770 | case 0x40: | |
771 | /* Don't care */ | |
772 | case 0xff: | |
773 | default: | |
774 | return -ENOSYS; /* not supported */ | |
775 | } | |
776 | } | |
777 | ||
49c7b3fc TS |
778 | if (formation[i].pcm > AM824_MAX_CHANNELS_FOR_PCM || |
779 | formation[i].midi > AM824_MAX_CHANNELS_FOR_MIDI) | |
eb7b3a05 TS |
780 | return -ENOSYS; |
781 | ||
782 | return 0; | |
783 | } | |
784 | ||
caa27151 TS |
785 | static int fill_stream_formations(struct snd_bebob *bebob, u8 addr[AVC_BRIDGECO_ADDR_BYTES], |
786 | enum avc_bridgeco_plug_dir plug_dir, unsigned int plug_id, | |
787 | struct snd_bebob_stream_formation *formations) | |
eb7b3a05 | 788 | { |
caa27151 | 789 | enum avc_bridgeco_plug_type plug_type; |
eb7b3a05 | 790 | u8 *buf; |
eb7b3a05 | 791 | unsigned int len, eid; |
eb7b3a05 TS |
792 | int err; |
793 | ||
caa27151 TS |
794 | avc_bridgeco_fill_unit_addr(addr, plug_dir, AVC_BRIDGECO_PLUG_UNIT_ISOC, plug_id); |
795 | ||
796 | err = avc_bridgeco_get_plug_type(bebob->unit, addr, &plug_type); | |
797 | if (err < 0) { | |
798 | dev_err(&bebob->unit->device, | |
799 | "Fail to get type for isoc %d plug 0: %d\n", plug_dir, err); | |
800 | return err; | |
801 | } else if (plug_type != AVC_BRIDGECO_PLUG_TYPE_ISOC) | |
802 | return -ENXIO; | |
803 | ||
eb7b3a05 TS |
804 | buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL); |
805 | if (buf == NULL) | |
806 | return -ENOMEM; | |
807 | ||
caa27151 TS |
808 | for (eid = 0; eid < SND_BEBOB_STRM_FMT_ENTRIES; ++eid) { |
809 | avc_bridgeco_fill_unit_addr(addr, plug_dir, AVC_BRIDGECO_PLUG_UNIT_ISOC, plug_id); | |
eb7b3a05 | 810 | |
eb7b3a05 | 811 | len = FORMAT_MAXIMUM_LENGTH; |
caa27151 TS |
812 | err = avc_bridgeco_get_plug_strm_fmt(bebob->unit, addr, buf, &len, eid); |
813 | // No entries remained. | |
eb7b3a05 TS |
814 | if (err == -EINVAL && eid > 0) { |
815 | err = 0; | |
816 | break; | |
817 | } else if (err < 0) { | |
818 | dev_err(&bebob->unit->device, | |
caa27151 TS |
819 | "fail to get stream format %d for isoc %d plug %d:%d\n", |
820 | eid, plug_dir, plug_id, err); | |
eb7b3a05 TS |
821 | break; |
822 | } | |
823 | ||
824 | err = parse_stream_formation(buf, len, formations); | |
825 | if (err < 0) | |
826 | break; | |
827 | } | |
828 | ||
829 | kfree(buf); | |
830 | return err; | |
831 | } | |
832 | ||
5c6ea94f TS |
833 | static int detect_midi_ports(struct snd_bebob *bebob, |
834 | const struct snd_bebob_stream_formation *formats, | |
835 | u8 addr[AVC_BRIDGECO_ADDR_BYTES], enum avc_bridgeco_plug_dir plug_dir, | |
836 | unsigned int plug_count, unsigned int *midi_ports) | |
837 | { | |
838 | int i; | |
839 | int err = 0; | |
840 | ||
841 | *midi_ports = 0; | |
842 | ||
843 | /// Detect the number of available MIDI ports when packet has MIDI conformant data channel. | |
844 | for (i = 0; i < SND_BEBOB_STRM_FMT_ENTRIES; ++i) { | |
845 | if (formats[i].midi > 0) | |
846 | break; | |
847 | } | |
848 | if (i >= SND_BEBOB_STRM_FMT_ENTRIES) | |
849 | return 0; | |
850 | ||
851 | for (i = 0; i < plug_count; ++i) { | |
852 | enum avc_bridgeco_plug_type plug_type; | |
853 | unsigned int ch_count; | |
854 | ||
855 | avc_bridgeco_fill_unit_addr(addr, plug_dir, AVC_BRIDGECO_PLUG_UNIT_EXT, i); | |
856 | ||
857 | err = avc_bridgeco_get_plug_type(bebob->unit, addr, &plug_type); | |
858 | if (err < 0) { | |
859 | dev_err(&bebob->unit->device, | |
860 | "fail to get type for external %d plug %d: %d\n", | |
861 | plug_dir, i, err); | |
862 | break; | |
863 | } else if (plug_type != AVC_BRIDGECO_PLUG_TYPE_MIDI) { | |
864 | continue; | |
865 | } | |
866 | ||
867 | err = avc_bridgeco_get_plug_ch_count(bebob->unit, addr, &ch_count); | |
868 | if (err < 0) | |
869 | break; | |
870 | *midi_ports += ch_count; | |
871 | } | |
872 | ||
873 | return err; | |
874 | } | |
875 | ||
eb7b3a05 TS |
876 | static int |
877 | seek_msu_sync_input_plug(struct snd_bebob *bebob) | |
878 | { | |
879 | u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES]; | |
a6b598bf TS |
880 | unsigned int i; |
881 | enum avc_bridgeco_plug_type type; | |
eb7b3a05 TS |
882 | int err; |
883 | ||
884 | /* Get the number of Music Sub Unit for both direction. */ | |
885 | err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs); | |
886 | if (err < 0) { | |
887 | dev_err(&bebob->unit->device, | |
888 | "fail to get info for MSU in/out plugs: %d\n", | |
889 | err); | |
890 | goto end; | |
891 | } | |
892 | ||
893 | /* seek destination plugs for 'MSU sync input' */ | |
894 | bebob->sync_input_plug = -1; | |
895 | for (i = 0; i < plugs[0]; i++) { | |
896 | avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, i); | |
897 | err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type); | |
898 | if (err < 0) { | |
899 | dev_err(&bebob->unit->device, | |
900 | "fail to get type for MSU in plug %d: %d\n", | |
901 | i, err); | |
902 | goto end; | |
903 | } | |
904 | ||
905 | if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) { | |
906 | bebob->sync_input_plug = i; | |
907 | break; | |
908 | } | |
909 | } | |
910 | end: | |
911 | return err; | |
912 | } | |
913 | ||
914 | int snd_bebob_stream_discover(struct snd_bebob *bebob) | |
915 | { | |
6b9866c8 | 916 | const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock; |
eb7b3a05 | 917 | u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES]; |
eb7b3a05 TS |
918 | int err; |
919 | ||
920 | /* the number of plugs for isoc in/out, ext in/out */ | |
921 | err = avc_general_get_plug_info(bebob->unit, 0x1f, 0x07, 0x00, plugs); | |
922 | if (err < 0) { | |
923 | dev_err(&bebob->unit->device, | |
924 | "fail to get info for isoc/external in/out plugs: %d\n", | |
925 | err); | |
926 | goto end; | |
927 | } | |
928 | ||
929 | /* | |
930 | * This module supports at least one isoc input plug and one isoc | |
931 | * output plug. | |
932 | */ | |
933 | if ((plugs[0] == 0) || (plugs[1] == 0)) { | |
934 | err = -ENOSYS; | |
935 | goto end; | |
936 | } | |
937 | ||
caa27151 TS |
938 | err = fill_stream_formations(bebob, addr, AVC_BRIDGECO_PLUG_DIR_IN, 0, |
939 | bebob->rx_stream_formations); | |
eb7b3a05 TS |
940 | if (err < 0) |
941 | goto end; | |
942 | ||
caa27151 TS |
943 | err = fill_stream_formations(bebob, addr, AVC_BRIDGECO_PLUG_DIR_OUT, 0, |
944 | bebob->tx_stream_formations); | |
eb7b3a05 TS |
945 | if (err < 0) |
946 | goto end; | |
947 | ||
5c6ea94f TS |
948 | err = detect_midi_ports(bebob, bebob->rx_stream_formations, addr, AVC_BRIDGECO_PLUG_DIR_IN, |
949 | plugs[2], &bebob->midi_input_ports); | |
950 | if (err < 0) | |
951 | goto end; | |
eb7b3a05 | 952 | |
5c6ea94f TS |
953 | err = detect_midi_ports(bebob, bebob->tx_stream_formations, addr, AVC_BRIDGECO_PLUG_DIR_OUT, |
954 | plugs[3], &bebob->midi_output_ports); | |
955 | if (err < 0) | |
956 | goto end; | |
eb7b3a05 TS |
957 | |
958 | /* for check source of clock later */ | |
1fc9522a TS |
959 | if (!clk_spec) |
960 | err = seek_msu_sync_input_plug(bebob); | |
eb7b3a05 TS |
961 | end: |
962 | return err; | |
963 | } | |
618eabea TS |
964 | |
965 | void snd_bebob_stream_lock_changed(struct snd_bebob *bebob) | |
966 | { | |
967 | bebob->dev_lock_changed = true; | |
968 | wake_up(&bebob->hwdep_wait); | |
969 | } | |
970 | ||
971 | int snd_bebob_stream_lock_try(struct snd_bebob *bebob) | |
972 | { | |
973 | int err; | |
974 | ||
975 | spin_lock_irq(&bebob->lock); | |
976 | ||
977 | /* user land lock this */ | |
978 | if (bebob->dev_lock_count < 0) { | |
979 | err = -EBUSY; | |
980 | goto end; | |
981 | } | |
982 | ||
983 | /* this is the first time */ | |
984 | if (bebob->dev_lock_count++ == 0) | |
985 | snd_bebob_stream_lock_changed(bebob); | |
986 | err = 0; | |
987 | end: | |
988 | spin_unlock_irq(&bebob->lock); | |
989 | return err; | |
990 | } | |
991 | ||
992 | void snd_bebob_stream_lock_release(struct snd_bebob *bebob) | |
993 | { | |
994 | spin_lock_irq(&bebob->lock); | |
995 | ||
996 | if (WARN_ON(bebob->dev_lock_count <= 0)) | |
997 | goto end; | |
998 | if (--bebob->dev_lock_count == 0) | |
999 | snd_bebob_stream_lock_changed(bebob); | |
1000 | end: | |
1001 | spin_unlock_irq(&bebob->lock); | |
1002 | } |