Commit | Line | Data |
---|---|---|
da607e19 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
e2786ca6 TS |
2 | /* |
3 | * oxfw_stream.c - a part of driver for OXFW970/971 based devices | |
4 | * | |
5 | * Copyright (c) 2014 Takashi Sakamoto | |
e2786ca6 TS |
6 | */ |
7 | ||
8 | #include "oxfw.h" | |
f3699e2c | 9 | #include <linux/delay.h> |
e2786ca6 | 10 | |
5cd1d3f4 | 11 | #define AVC_GENERIC_FRAME_MAXIMUM_BYTES 512 |
f3699e2c | 12 | #define CALLBACK_TIMEOUT 200 |
5cd1d3f4 TS |
13 | |
14 | /* | |
15 | * According to datasheet of Oxford Semiconductor: | |
16 | * OXFW970: 32.0/44.1/48.0/96.0 Khz, 8 audio channels I/O | |
17 | * OXFW971: 32.0/44.1/48.0/88.2/96.0/192.0 kHz, 16 audio channels I/O, MIDI I/O | |
18 | */ | |
19 | static const unsigned int oxfw_rate_table[] = { | |
20 | [0] = 32000, | |
21 | [1] = 44100, | |
22 | [2] = 48000, | |
23 | [3] = 88200, | |
24 | [4] = 96000, | |
25 | [5] = 192000, | |
26 | }; | |
27 | ||
28 | /* | |
29 | * See Table 5.7 – Sampling frequency for Multi-bit Audio | |
30 | * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA) | |
31 | */ | |
32 | static const unsigned int avc_stream_rate_table[] = { | |
33 | [0] = 0x02, | |
34 | [1] = 0x03, | |
35 | [2] = 0x04, | |
36 | [3] = 0x0a, | |
37 | [4] = 0x05, | |
38 | [5] = 0x07, | |
39 | }; | |
40 | ||
b0ac0009 TS |
41 | static int set_rate(struct snd_oxfw *oxfw, unsigned int rate) |
42 | { | |
43 | int err; | |
44 | ||
45 | err = avc_general_set_sig_fmt(oxfw->unit, rate, | |
46 | AVC_GENERAL_PLUG_DIR_IN, 0); | |
47 | if (err < 0) | |
48 | goto end; | |
49 | ||
50 | if (oxfw->has_output) | |
51 | err = avc_general_set_sig_fmt(oxfw->unit, rate, | |
52 | AVC_GENERAL_PLUG_DIR_OUT, 0); | |
53 | end: | |
54 | return err; | |
55 | } | |
56 | ||
f3699e2c TS |
57 | static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s, |
58 | unsigned int rate, unsigned int pcm_channels) | |
59 | { | |
60 | u8 **formats; | |
61 | struct snd_oxfw_stream_formation formation; | |
62 | enum avc_general_plug_dir dir; | |
5580ba7b DC |
63 | unsigned int len; |
64 | int i, err; | |
f3699e2c | 65 | |
b0ac0009 TS |
66 | if (s == &oxfw->tx_stream) { |
67 | formats = oxfw->tx_stream_formats; | |
68 | dir = AVC_GENERAL_PLUG_DIR_OUT; | |
69 | } else { | |
70 | formats = oxfw->rx_stream_formats; | |
71 | dir = AVC_GENERAL_PLUG_DIR_IN; | |
72 | } | |
f3699e2c TS |
73 | |
74 | /* Seek stream format for requirements. */ | |
75 | for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { | |
76 | err = snd_oxfw_stream_parse_format(formats[i], &formation); | |
77 | if (err < 0) | |
78 | return err; | |
79 | ||
80 | if ((formation.rate == rate) && (formation.pcm == pcm_channels)) | |
81 | break; | |
82 | } | |
83 | if (i == SND_OXFW_STREAM_FORMAT_ENTRIES) | |
84 | return -EINVAL; | |
85 | ||
86 | /* If assumed, just change rate. */ | |
87 | if (oxfw->assumed) | |
b0ac0009 | 88 | return set_rate(oxfw, rate); |
f3699e2c TS |
89 | |
90 | /* Calculate format length. */ | |
91 | len = 5 + formats[i][4] * 2; | |
92 | ||
93 | err = avc_stream_set_format(oxfw->unit, dir, 0, formats[i], len); | |
94 | if (err < 0) | |
95 | return err; | |
96 | ||
97 | /* Some requests just after changing format causes freezing. */ | |
98 | msleep(100); | |
99 | ||
100 | return 0; | |
101 | } | |
102 | ||
521b2e11 | 103 | static int start_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream) |
e2786ca6 | 104 | { |
f3699e2c | 105 | struct cmp_connection *conn; |
f3699e2c | 106 | int err; |
e2786ca6 | 107 | |
0356ce3a | 108 | if (stream == &oxfw->rx_stream) |
b0ac0009 | 109 | conn = &oxfw->in_conn; |
0356ce3a | 110 | else |
b0ac0009 | 111 | conn = &oxfw->out_conn; |
e2786ca6 | 112 | |
7bc93821 | 113 | err = cmp_connection_establish(conn); |
e2786ca6 | 114 | if (err < 0) |
e34244dd | 115 | return err; |
e2786ca6 | 116 | |
ac5d7786 TS |
117 | err = amdtp_domain_add_stream(&oxfw->domain, stream, |
118 | conn->resources.channel, conn->speed); | |
f3699e2c TS |
119 | if (err < 0) { |
120 | cmp_connection_break(conn); | |
e34244dd | 121 | return err; |
f3699e2c TS |
122 | } |
123 | ||
e34244dd | 124 | return 0; |
f3699e2c TS |
125 | } |
126 | ||
b0ac0009 TS |
127 | static int check_connection_used_by_others(struct snd_oxfw *oxfw, |
128 | struct amdtp_stream *stream) | |
f3699e2c | 129 | { |
b0ac0009 TS |
130 | struct cmp_connection *conn; |
131 | bool used; | |
132 | int err; | |
133 | ||
134 | if (stream == &oxfw->tx_stream) | |
135 | conn = &oxfw->out_conn; | |
136 | else | |
137 | conn = &oxfw->in_conn; | |
138 | ||
139 | err = cmp_connection_check_used(conn, &used); | |
140 | if ((err >= 0) && used && !amdtp_stream_running(stream)) { | |
141 | dev_err(&oxfw->unit->device, | |
142 | "Connection established by others: %cPCR[%d]\n", | |
143 | (conn->direction == CMP_OUTPUT) ? 'o' : 'i', | |
144 | conn->pcr_index); | |
145 | err = -EBUSY; | |
146 | } | |
147 | ||
148 | return err; | |
149 | } | |
150 | ||
779f0dba | 151 | static int init_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream) |
b0ac0009 TS |
152 | { |
153 | struct cmp_connection *conn; | |
154 | enum cmp_direction c_dir; | |
155 | enum amdtp_stream_direction s_dir; | |
156 | int err; | |
157 | ||
158 | if (stream == &oxfw->tx_stream) { | |
159 | conn = &oxfw->out_conn; | |
160 | c_dir = CMP_OUTPUT; | |
161 | s_dir = AMDTP_IN_STREAM; | |
162 | } else { | |
163 | conn = &oxfw->in_conn; | |
164 | c_dir = CMP_INPUT; | |
165 | s_dir = AMDTP_OUT_STREAM; | |
166 | } | |
167 | ||
168 | err = cmp_connection_init(conn, oxfw->unit, c_dir, 0); | |
169 | if (err < 0) | |
779f0dba | 170 | return err; |
b0ac0009 | 171 | |
5955815e | 172 | err = amdtp_am824_init(stream, oxfw->unit, s_dir, CIP_NONBLOCKING); |
b0ac0009 | 173 | if (err < 0) { |
b0ac0009 | 174 | cmp_connection_destroy(conn); |
779f0dba | 175 | return err; |
b0ac0009 TS |
176 | } |
177 | ||
a2064710 TS |
178 | /* |
179 | * OXFW starts to transmit packets with non-zero dbc. | |
180 | * OXFW postpone transferring packets till handling any asynchronous | |
181 | * packets. As a result, next isochronous packet includes more data | |
182 | * blocks than IEC 61883-6 defines. | |
183 | */ | |
13f3a46d | 184 | if (stream == &oxfw->tx_stream) { |
62f00e40 | 185 | oxfw->tx_stream.flags |= CIP_JUMBO_PAYLOAD; |
13f3a46d TS |
186 | if (oxfw->wrong_dbs) |
187 | oxfw->tx_stream.flags |= CIP_WRONG_DBS; | |
188 | } | |
779f0dba TS |
189 | |
190 | return 0; | |
b0ac0009 TS |
191 | } |
192 | ||
0356ce3a TS |
193 | static int keep_resources(struct snd_oxfw *oxfw, struct amdtp_stream *stream) |
194 | { | |
195 | enum avc_general_plug_dir dir; | |
196 | u8 **formats; | |
197 | struct snd_oxfw_stream_formation formation; | |
7bc93821 | 198 | struct cmp_connection *conn; |
0356ce3a TS |
199 | int i; |
200 | int err; | |
201 | ||
202 | if (stream == &oxfw->rx_stream) { | |
203 | dir = AVC_GENERAL_PLUG_DIR_IN; | |
204 | formats = oxfw->rx_stream_formats; | |
7bc93821 | 205 | conn = &oxfw->in_conn; |
0356ce3a TS |
206 | } else { |
207 | dir = AVC_GENERAL_PLUG_DIR_OUT; | |
208 | formats = oxfw->tx_stream_formats; | |
7bc93821 | 209 | conn = &oxfw->out_conn; |
0356ce3a TS |
210 | } |
211 | ||
212 | err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation); | |
213 | if (err < 0) | |
214 | return err; | |
215 | ||
216 | for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { | |
217 | struct snd_oxfw_stream_formation fmt; | |
218 | ||
219 | if (formats[i] == NULL) | |
220 | break; | |
221 | ||
222 | err = snd_oxfw_stream_parse_format(formats[i], &fmt); | |
223 | if (err < 0) | |
224 | return err; | |
225 | ||
226 | if (fmt.rate == formation.rate && fmt.pcm == formation.pcm && | |
227 | fmt.midi == formation.midi) | |
228 | break; | |
229 | } | |
230 | if (i == SND_OXFW_STREAM_FORMAT_ENTRIES) | |
231 | return -EINVAL; | |
232 | ||
233 | // The stream should have one pcm channels at least. | |
234 | if (formation.pcm == 0) | |
235 | return -EINVAL; | |
236 | ||
7bc93821 | 237 | err = amdtp_am824_set_parameters(stream, formation.rate, formation.pcm, |
0356ce3a | 238 | formation.midi * 8, false); |
7bc93821 TS |
239 | if (err < 0) |
240 | return err; | |
241 | ||
242 | return cmp_connection_reserve(conn, amdtp_stream_get_max_payload(stream)); | |
0356ce3a TS |
243 | } |
244 | ||
4f380d00 TS |
245 | int snd_oxfw_stream_reserve_duplex(struct snd_oxfw *oxfw, |
246 | struct amdtp_stream *stream, | |
1d6a722c | 247 | unsigned int rate, unsigned int pcm_channels, |
3299d2a0 TS |
248 | unsigned int frames_per_period, |
249 | unsigned int frames_per_buffer) | |
b0ac0009 | 250 | { |
f3699e2c | 251 | struct snd_oxfw_stream_formation formation; |
b0ac0009 | 252 | enum avc_general_plug_dir dir; |
4f380d00 | 253 | int err; |
b0ac0009 | 254 | |
20358d44 TS |
255 | // Considering JACK/FFADO streaming: |
256 | // TODO: This can be removed hwdep functionality becomes popular. | |
257 | err = check_connection_used_by_others(oxfw, &oxfw->rx_stream); | |
258 | if (err < 0) | |
259 | return err; | |
260 | if (oxfw->has_output) { | |
261 | err = check_connection_used_by_others(oxfw, &oxfw->tx_stream); | |
262 | if (err < 0) | |
263 | return err; | |
b0ac0009 TS |
264 | } |
265 | ||
20358d44 TS |
266 | if (stream == &oxfw->tx_stream) |
267 | dir = AVC_GENERAL_PLUG_DIR_OUT; | |
268 | else | |
269 | dir = AVC_GENERAL_PLUG_DIR_IN; | |
b0ac0009 | 270 | |
b0ac0009 | 271 | err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation); |
e2786ca6 | 272 | if (err < 0) |
20358d44 | 273 | return err; |
4f380d00 | 274 | if (rate == 0) { |
05588d34 | 275 | rate = formation.rate; |
05588d34 | 276 | pcm_channels = formation.pcm; |
4f380d00 TS |
277 | } |
278 | if (formation.rate != rate || formation.pcm != pcm_channels) { | |
ac5d7786 TS |
279 | amdtp_domain_stop(&oxfw->domain); |
280 | ||
e34244dd | 281 | cmp_connection_break(&oxfw->in_conn); |
3f2ce83d | 282 | cmp_connection_release(&oxfw->in_conn); |
e34244dd TS |
283 | |
284 | if (oxfw->has_output) { | |
e34244dd | 285 | cmp_connection_break(&oxfw->out_conn); |
3f2ce83d | 286 | cmp_connection_release(&oxfw->out_conn); |
e34244dd | 287 | } |
4f380d00 | 288 | } |
f3699e2c | 289 | |
4f380d00 TS |
290 | if (oxfw->substreams_count == 0 || |
291 | formation.rate != rate || formation.pcm != pcm_channels) { | |
b0ac0009 | 292 | err = set_stream_format(oxfw, stream, rate, pcm_channels); |
f3699e2c TS |
293 | if (err < 0) { |
294 | dev_err(&oxfw->unit->device, | |
295 | "fail to set stream format: %d\n", err); | |
20358d44 | 296 | return err; |
f3699e2c | 297 | } |
0356ce3a TS |
298 | |
299 | err = keep_resources(oxfw, &oxfw->rx_stream); | |
300 | if (err < 0) | |
301 | return err; | |
302 | ||
303 | if (oxfw->has_output) { | |
304 | err = keep_resources(oxfw, &oxfw->tx_stream); | |
7bc93821 TS |
305 | if (err < 0) { |
306 | cmp_connection_release(&oxfw->in_conn); | |
0356ce3a | 307 | return err; |
7bc93821 | 308 | } |
0356ce3a | 309 | } |
1d6a722c TS |
310 | |
311 | err = amdtp_domain_set_events_per_period(&oxfw->domain, | |
3299d2a0 | 312 | frames_per_period, frames_per_buffer); |
1d6a722c TS |
313 | if (err < 0) { |
314 | cmp_connection_release(&oxfw->in_conn); | |
315 | if (oxfw->has_output) | |
316 | cmp_connection_release(&oxfw->out_conn); | |
317 | return err; | |
318 | } | |
20358d44 | 319 | } |
b0ac0009 | 320 | |
4f380d00 TS |
321 | return 0; |
322 | } | |
323 | ||
324 | int snd_oxfw_stream_start_duplex(struct snd_oxfw *oxfw) | |
325 | { | |
326 | int err; | |
327 | ||
328 | if (oxfw->substreams_count == 0) | |
329 | return -EIO; | |
330 | ||
331 | if (amdtp_streaming_error(&oxfw->rx_stream) || | |
332 | amdtp_streaming_error(&oxfw->tx_stream)) { | |
ac5d7786 | 333 | amdtp_domain_stop(&oxfw->domain); |
4f380d00 | 334 | |
ac5d7786 TS |
335 | cmp_connection_break(&oxfw->in_conn); |
336 | if (oxfw->has_output) | |
4f380d00 | 337 | cmp_connection_break(&oxfw->out_conn); |
4f380d00 TS |
338 | } |
339 | ||
20358d44 TS |
340 | if (!amdtp_stream_running(&oxfw->rx_stream)) { |
341 | err = start_stream(oxfw, &oxfw->rx_stream); | |
342 | if (err < 0) { | |
343 | dev_err(&oxfw->unit->device, | |
ac5d7786 | 344 | "fail to prepare rx stream: %d\n", err); |
20358d44 TS |
345 | goto error; |
346 | } | |
20358d44 | 347 | |
ac5d7786 TS |
348 | if (oxfw->has_output && |
349 | !amdtp_stream_running(&oxfw->tx_stream)) { | |
20358d44 | 350 | err = start_stream(oxfw, &oxfw->tx_stream); |
b0ac0009 TS |
351 | if (err < 0) { |
352 | dev_err(&oxfw->unit->device, | |
ac5d7786 | 353 | "fail to prepare tx stream: %d\n", err); |
20358d44 | 354 | goto error; |
b0ac0009 TS |
355 | } |
356 | } | |
ac5d7786 | 357 | |
acfedcbe | 358 | err = amdtp_domain_start(&oxfw->domain, 0); |
ac5d7786 TS |
359 | if (err < 0) |
360 | goto error; | |
361 | ||
362 | // Wait first packet. | |
363 | if (!amdtp_stream_wait_callback(&oxfw->rx_stream, | |
ac5d7786 TS |
364 | CALLBACK_TIMEOUT)) { |
365 | err = -ETIMEDOUT; | |
366 | goto error; | |
367 | } | |
368 | ||
4c098dab TS |
369 | if (oxfw->has_output) { |
370 | if (!amdtp_stream_wait_callback(&oxfw->tx_stream, | |
371 | CALLBACK_TIMEOUT)) { | |
372 | err = -ETIMEDOUT; | |
373 | goto error; | |
374 | } | |
375 | } | |
f3699e2c TS |
376 | } |
377 | ||
20358d44 TS |
378 | return 0; |
379 | error: | |
ac5d7786 TS |
380 | amdtp_domain_stop(&oxfw->domain); |
381 | ||
20358d44 | 382 | cmp_connection_break(&oxfw->in_conn); |
ac5d7786 | 383 | if (oxfw->has_output) |
20358d44 | 384 | cmp_connection_break(&oxfw->out_conn); |
ac5d7786 | 385 | |
e2786ca6 TS |
386 | return err; |
387 | } | |
388 | ||
779f0dba | 389 | void snd_oxfw_stream_stop_duplex(struct snd_oxfw *oxfw) |
e2786ca6 | 390 | { |
4a0a0472 | 391 | if (oxfw->substreams_count == 0) { |
ac5d7786 TS |
392 | amdtp_domain_stop(&oxfw->domain); |
393 | ||
e34244dd | 394 | cmp_connection_break(&oxfw->in_conn); |
7bc93821 | 395 | cmp_connection_release(&oxfw->in_conn); |
b0ac0009 | 396 | |
e34244dd | 397 | if (oxfw->has_output) { |
e34244dd | 398 | cmp_connection_break(&oxfw->out_conn); |
7bc93821 | 399 | cmp_connection_release(&oxfw->out_conn); |
e34244dd | 400 | } |
20358d44 | 401 | } |
e2786ca6 TS |
402 | } |
403 | ||
779f0dba | 404 | static void destroy_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream) |
e2786ca6 | 405 | { |
b0ac0009 TS |
406 | struct cmp_connection *conn; |
407 | ||
408 | if (stream == &oxfw->tx_stream) | |
409 | conn = &oxfw->out_conn; | |
410 | else | |
411 | conn = &oxfw->in_conn; | |
e2786ca6 | 412 | |
b0ac0009 TS |
413 | amdtp_stream_destroy(stream); |
414 | cmp_connection_destroy(conn); | |
e2786ca6 TS |
415 | } |
416 | ||
779f0dba TS |
417 | int snd_oxfw_stream_init_duplex(struct snd_oxfw *oxfw) |
418 | { | |
419 | int err; | |
420 | ||
421 | err = init_stream(oxfw, &oxfw->rx_stream); | |
422 | if (err < 0) | |
423 | return err; | |
424 | ||
425 | if (oxfw->has_output) { | |
426 | err = init_stream(oxfw, &oxfw->tx_stream); | |
427 | if (err < 0) { | |
428 | destroy_stream(oxfw, &oxfw->rx_stream); | |
429 | return err; | |
430 | } | |
431 | } | |
432 | ||
ac5d7786 TS |
433 | err = amdtp_domain_init(&oxfw->domain); |
434 | if (err < 0) { | |
435 | destroy_stream(oxfw, &oxfw->rx_stream); | |
436 | if (oxfw->has_output) | |
437 | destroy_stream(oxfw, &oxfw->tx_stream); | |
438 | } | |
439 | ||
440 | return err; | |
779f0dba TS |
441 | } |
442 | ||
443 | // This function should be called before starting the stream or after stopping | |
444 | // the streams. | |
445 | void snd_oxfw_stream_destroy_duplex(struct snd_oxfw *oxfw) | |
446 | { | |
ac5d7786 TS |
447 | amdtp_domain_destroy(&oxfw->domain); |
448 | ||
779f0dba TS |
449 | destroy_stream(oxfw, &oxfw->rx_stream); |
450 | ||
451 | if (oxfw->has_output) | |
452 | destroy_stream(oxfw, &oxfw->tx_stream); | |
453 | } | |
454 | ||
455 | void snd_oxfw_stream_update_duplex(struct snd_oxfw *oxfw) | |
e2786ca6 | 456 | { |
ac5d7786 TS |
457 | amdtp_domain_stop(&oxfw->domain); |
458 | ||
e34244dd | 459 | cmp_connection_break(&oxfw->in_conn); |
b0ac0009 | 460 | |
e34244dd TS |
461 | amdtp_stream_pcm_abort(&oxfw->rx_stream); |
462 | ||
463 | if (oxfw->has_output) { | |
e34244dd TS |
464 | cmp_connection_break(&oxfw->out_conn); |
465 | ||
466 | amdtp_stream_pcm_abort(&oxfw->tx_stream); | |
467 | } | |
e2786ca6 | 468 | } |
5cd1d3f4 | 469 | |
3c96101f TS |
470 | int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw, |
471 | enum avc_general_plug_dir dir, | |
472 | struct snd_oxfw_stream_formation *formation) | |
473 | { | |
474 | u8 *format; | |
475 | unsigned int len; | |
476 | int err; | |
477 | ||
478 | len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; | |
479 | format = kmalloc(len, GFP_KERNEL); | |
480 | if (format == NULL) | |
481 | return -ENOMEM; | |
482 | ||
483 | err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len); | |
484 | if (err < 0) | |
485 | goto end; | |
486 | if (len < 3) { | |
487 | err = -EIO; | |
488 | goto end; | |
489 | } | |
490 | ||
491 | err = snd_oxfw_stream_parse_format(format, formation); | |
492 | end: | |
493 | kfree(format); | |
494 | return err; | |
495 | } | |
496 | ||
5cd1d3f4 TS |
497 | /* |
498 | * See Table 6.16 - AM824 Stream Format | |
499 | * Figure 6.19 - format_information field for AM824 Compound | |
500 | * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA) | |
501 | * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005 | |
502 | */ | |
503 | int snd_oxfw_stream_parse_format(u8 *format, | |
504 | struct snd_oxfw_stream_formation *formation) | |
505 | { | |
506 | unsigned int i, e, channels, type; | |
507 | ||
508 | memset(formation, 0, sizeof(struct snd_oxfw_stream_formation)); | |
509 | ||
510 | /* | |
511 | * this module can support a hierarchy combination that: | |
512 | * Root: Audio and Music (0x90) | |
513 | * Level 1: AM824 Compound (0x40) | |
514 | */ | |
515 | if ((format[0] != 0x90) || (format[1] != 0x40)) | |
516 | return -ENOSYS; | |
517 | ||
518 | /* check the sampling rate */ | |
519 | for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) { | |
520 | if (format[2] == avc_stream_rate_table[i]) | |
521 | break; | |
522 | } | |
523 | if (i == ARRAY_SIZE(avc_stream_rate_table)) | |
524 | return -ENOSYS; | |
525 | ||
526 | formation->rate = oxfw_rate_table[i]; | |
527 | ||
528 | for (e = 0; e < format[4]; e++) { | |
529 | channels = format[5 + e * 2]; | |
530 | type = format[6 + e * 2]; | |
531 | ||
532 | switch (type) { | |
533 | /* IEC 60958 Conformant, currently handled as MBLA */ | |
534 | case 0x00: | |
535 | /* Multi Bit Linear Audio (Raw) */ | |
536 | case 0x06: | |
537 | formation->pcm += channels; | |
538 | break; | |
539 | /* MIDI Conformant */ | |
540 | case 0x0d: | |
541 | formation->midi = channels; | |
542 | break; | |
543 | /* IEC 61937-3 to 7 */ | |
544 | case 0x01: | |
545 | case 0x02: | |
546 | case 0x03: | |
547 | case 0x04: | |
548 | case 0x05: | |
549 | /* Multi Bit Linear Audio */ | |
550 | case 0x07: /* DVD-Audio */ | |
551 | case 0x0c: /* High Precision */ | |
552 | /* One Bit Audio */ | |
553 | case 0x08: /* (Plain) Raw */ | |
554 | case 0x09: /* (Plain) SACD */ | |
555 | case 0x0a: /* (Encoded) Raw */ | |
556 | case 0x0b: /* (Encoded) SACD */ | |
557 | /* SMPTE Time-Code conformant */ | |
558 | case 0x0e: | |
559 | /* Sample Count */ | |
560 | case 0x0f: | |
561 | /* Anciliary Data */ | |
562 | case 0x10: | |
563 | /* Synchronization Stream (Stereo Raw audio) */ | |
564 | case 0x40: | |
565 | /* Don't care */ | |
566 | case 0xff: | |
567 | default: | |
568 | return -ENOSYS; /* not supported */ | |
569 | } | |
570 | } | |
571 | ||
49c7b3fc TS |
572 | if (formation->pcm > AM824_MAX_CHANNELS_FOR_PCM || |
573 | formation->midi > AM824_MAX_CHANNELS_FOR_MIDI) | |
5cd1d3f4 TS |
574 | return -ENOSYS; |
575 | ||
576 | return 0; | |
577 | } | |
578 | ||
579 | static int | |
580 | assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir, | |
581 | unsigned int pid, u8 *buf, unsigned int *len, | |
582 | u8 **formats) | |
583 | { | |
584 | struct snd_oxfw_stream_formation formation; | |
585 | unsigned int i, eid; | |
586 | int err; | |
587 | ||
588 | /* get format at current sampling rate */ | |
589 | err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len); | |
590 | if (err < 0) { | |
591 | dev_err(&oxfw->unit->device, | |
592 | "fail to get current stream format for isoc %s plug %d:%d\n", | |
593 | (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out", | |
594 | pid, err); | |
595 | goto end; | |
596 | } | |
597 | ||
598 | /* parse and set stream format */ | |
599 | eid = 0; | |
600 | err = snd_oxfw_stream_parse_format(buf, &formation); | |
601 | if (err < 0) | |
602 | goto end; | |
603 | ||
cd3b7116 TS |
604 | formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len, |
605 | GFP_KERNEL); | |
606 | if (!formats[eid]) { | |
5cd1d3f4 TS |
607 | err = -ENOMEM; |
608 | goto end; | |
609 | } | |
5cd1d3f4 TS |
610 | |
611 | /* apply the format for each available sampling rate */ | |
612 | for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) { | |
613 | if (formation.rate == oxfw_rate_table[i]) | |
614 | continue; | |
615 | ||
616 | err = avc_general_inquiry_sig_fmt(oxfw->unit, | |
617 | oxfw_rate_table[i], | |
618 | dir, pid); | |
619 | if (err < 0) | |
620 | continue; | |
621 | ||
622 | eid++; | |
cd3b7116 TS |
623 | formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len, |
624 | GFP_KERNEL); | |
5cd1d3f4 TS |
625 | if (formats[eid] == NULL) { |
626 | err = -ENOMEM; | |
627 | goto end; | |
628 | } | |
5cd1d3f4 TS |
629 | formats[eid][2] = avc_stream_rate_table[i]; |
630 | } | |
631 | ||
632 | err = 0; | |
633 | oxfw->assumed = true; | |
634 | end: | |
635 | return err; | |
636 | } | |
637 | ||
638 | static int fill_stream_formats(struct snd_oxfw *oxfw, | |
639 | enum avc_general_plug_dir dir, | |
640 | unsigned short pid) | |
641 | { | |
642 | u8 *buf, **formats; | |
643 | unsigned int len, eid = 0; | |
644 | struct snd_oxfw_stream_formation dummy; | |
645 | int err; | |
646 | ||
647 | buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL); | |
648 | if (buf == NULL) | |
649 | return -ENOMEM; | |
650 | ||
b0ac0009 TS |
651 | if (dir == AVC_GENERAL_PLUG_DIR_OUT) |
652 | formats = oxfw->tx_stream_formats; | |
653 | else | |
654 | formats = oxfw->rx_stream_formats; | |
5cd1d3f4 TS |
655 | |
656 | /* get first entry */ | |
657 | len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; | |
658 | err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0); | |
659 | if (err == -ENOSYS) { | |
660 | /* LIST subfunction is not implemented */ | |
661 | len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; | |
662 | err = assume_stream_formats(oxfw, dir, pid, buf, &len, | |
663 | formats); | |
664 | goto end; | |
665 | } else if (err < 0) { | |
666 | dev_err(&oxfw->unit->device, | |
667 | "fail to get stream format %d for isoc %s plug %d:%d\n", | |
668 | eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out", | |
669 | pid, err); | |
670 | goto end; | |
671 | } | |
672 | ||
673 | /* LIST subfunction is implemented */ | |
674 | while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) { | |
675 | /* The format is too short. */ | |
676 | if (len < 3) { | |
677 | err = -EIO; | |
678 | break; | |
679 | } | |
680 | ||
681 | /* parse and set stream format */ | |
682 | err = snd_oxfw_stream_parse_format(buf, &dummy); | |
683 | if (err < 0) | |
684 | break; | |
685 | ||
cd3b7116 TS |
686 | formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, len, |
687 | GFP_KERNEL); | |
688 | if (!formats[eid]) { | |
5cd1d3f4 TS |
689 | err = -ENOMEM; |
690 | break; | |
691 | } | |
5cd1d3f4 TS |
692 | |
693 | /* get next entry */ | |
694 | len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; | |
695 | err = avc_stream_get_format_list(oxfw->unit, dir, 0, | |
696 | buf, &len, ++eid); | |
697 | /* No entries remained. */ | |
698 | if (err == -EINVAL) { | |
699 | err = 0; | |
700 | break; | |
701 | } else if (err < 0) { | |
702 | dev_err(&oxfw->unit->device, | |
703 | "fail to get stream format %d for isoc %s plug %d:%d\n", | |
704 | eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : | |
705 | "out", | |
706 | pid, err); | |
707 | break; | |
708 | } | |
709 | } | |
710 | end: | |
711 | kfree(buf); | |
712 | return err; | |
713 | } | |
714 | ||
715 | int snd_oxfw_stream_discover(struct snd_oxfw *oxfw) | |
716 | { | |
717 | u8 plugs[AVC_PLUG_INFO_BUF_BYTES]; | |
32056041 TS |
718 | struct snd_oxfw_stream_formation formation; |
719 | u8 *format; | |
720 | unsigned int i; | |
5cd1d3f4 TS |
721 | int err; |
722 | ||
723 | /* the number of plugs for isoc in/out, ext in/out */ | |
724 | err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs); | |
725 | if (err < 0) { | |
726 | dev_err(&oxfw->unit->device, | |
727 | "fail to get info for isoc/external in/out plugs: %d\n", | |
728 | err); | |
729 | goto end; | |
b0ac0009 | 730 | } else if ((plugs[0] == 0) && (plugs[1] == 0)) { |
5cd1d3f4 TS |
731 | err = -ENOSYS; |
732 | goto end; | |
733 | } | |
734 | ||
b0ac0009 TS |
735 | /* use oPCR[0] if exists */ |
736 | if (plugs[1] > 0) { | |
737 | err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0); | |
738 | if (err < 0) | |
739 | goto end; | |
32056041 TS |
740 | |
741 | for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { | |
742 | format = oxfw->tx_stream_formats[i]; | |
743 | if (format == NULL) | |
744 | continue; | |
745 | err = snd_oxfw_stream_parse_format(format, &formation); | |
746 | if (err < 0) | |
747 | continue; | |
748 | ||
749 | /* Add one MIDI port. */ | |
750 | if (formation.midi > 0) | |
751 | oxfw->midi_input_ports = 1; | |
752 | } | |
753 | ||
b0ac0009 TS |
754 | oxfw->has_output = true; |
755 | } | |
756 | ||
5cd1d3f4 | 757 | /* use iPCR[0] if exists */ |
32056041 | 758 | if (plugs[0] > 0) { |
5cd1d3f4 | 759 | err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0); |
32056041 TS |
760 | if (err < 0) |
761 | goto end; | |
762 | ||
763 | for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { | |
764 | format = oxfw->rx_stream_formats[i]; | |
765 | if (format == NULL) | |
766 | continue; | |
767 | err = snd_oxfw_stream_parse_format(format, &formation); | |
768 | if (err < 0) | |
769 | continue; | |
770 | ||
771 | /* Add one MIDI port. */ | |
772 | if (formation.midi > 0) | |
773 | oxfw->midi_output_ports = 1; | |
774 | } | |
775 | } | |
5cd1d3f4 TS |
776 | end: |
777 | return err; | |
778 | } | |
8985f4ac TS |
779 | |
780 | void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw) | |
781 | { | |
782 | oxfw->dev_lock_changed = true; | |
783 | wake_up(&oxfw->hwdep_wait); | |
784 | } | |
785 | ||
786 | int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw) | |
787 | { | |
788 | int err; | |
789 | ||
790 | spin_lock_irq(&oxfw->lock); | |
791 | ||
792 | /* user land lock this */ | |
793 | if (oxfw->dev_lock_count < 0) { | |
794 | err = -EBUSY; | |
795 | goto end; | |
796 | } | |
797 | ||
798 | /* this is the first time */ | |
799 | if (oxfw->dev_lock_count++ == 0) | |
800 | snd_oxfw_stream_lock_changed(oxfw); | |
801 | err = 0; | |
802 | end: | |
803 | spin_unlock_irq(&oxfw->lock); | |
804 | return err; | |
805 | } | |
806 | ||
807 | void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw) | |
808 | { | |
809 | spin_lock_irq(&oxfw->lock); | |
810 | ||
811 | if (WARN_ON(oxfw->dev_lock_count <= 0)) | |
812 | goto end; | |
813 | if (--oxfw->dev_lock_count == 0) | |
814 | snd_oxfw_stream_lock_changed(oxfw); | |
815 | end: | |
816 | spin_unlock_irq(&oxfw->lock); | |
817 | } |