blob: 83683414793fae4e5f8759cddbf1d824dfdf196b [file] [log] [blame]
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +09001/*
2 * oxfw_stream.c - a part of driver for OXFW970/971 based devices
3 *
4 * Copyright (c) 2014 Takashi Sakamoto
5 *
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
8
9#include "oxfw.h"
Takashi Sakamotof3699e22014-12-09 00:10:44 +090010#include <linux/delay.h>
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +090011
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +090012#define AVC_GENERIC_FRAME_MAXIMUM_BYTES 512
Takashi Sakamotof3699e22014-12-09 00:10:44 +090013#define CALLBACK_TIMEOUT 200
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +090014
15/*
16 * According to datasheet of Oxford Semiconductor:
17 * OXFW970: 32.0/44.1/48.0/96.0 Khz, 8 audio channels I/O
18 * OXFW971: 32.0/44.1/48.0/88.2/96.0/192.0 kHz, 16 audio channels I/O, MIDI I/O
19 */
20static const unsigned int oxfw_rate_table[] = {
21 [0] = 32000,
22 [1] = 44100,
23 [2] = 48000,
24 [3] = 88200,
25 [4] = 96000,
26 [5] = 192000,
27};
28
29/*
30 * See Table 5.7 – Sampling frequency for Multi-bit Audio
31 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
32 */
33static const unsigned int avc_stream_rate_table[] = {
34 [0] = 0x02,
35 [1] = 0x03,
36 [2] = 0x04,
37 [3] = 0x0a,
38 [4] = 0x05,
39 [5] = 0x07,
40};
41
Takashi Sakamotob0ac0002014-12-09 00:10:46 +090042static int set_rate(struct snd_oxfw *oxfw, unsigned int rate)
43{
44 int err;
45
46 err = avc_general_set_sig_fmt(oxfw->unit, rate,
47 AVC_GENERAL_PLUG_DIR_IN, 0);
48 if (err < 0)
49 goto end;
50
51 if (oxfw->has_output)
52 err = avc_general_set_sig_fmt(oxfw->unit, rate,
53 AVC_GENERAL_PLUG_DIR_OUT, 0);
54end:
55 return err;
56}
57
Takashi Sakamotof3699e22014-12-09 00:10:44 +090058static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s,
59 unsigned int rate, unsigned int pcm_channels)
60{
61 u8 **formats;
62 struct snd_oxfw_stream_formation formation;
63 enum avc_general_plug_dir dir;
Dan Carpenter5580ba72014-12-12 22:27:03 +030064 unsigned int len;
65 int i, err;
Takashi Sakamotof3699e22014-12-09 00:10:44 +090066
Takashi Sakamotob0ac0002014-12-09 00:10:46 +090067 if (s == &oxfw->tx_stream) {
68 formats = oxfw->tx_stream_formats;
69 dir = AVC_GENERAL_PLUG_DIR_OUT;
70 } else {
71 formats = oxfw->rx_stream_formats;
72 dir = AVC_GENERAL_PLUG_DIR_IN;
73 }
Takashi Sakamotof3699e22014-12-09 00:10:44 +090074
75 /* Seek stream format for requirements. */
76 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
77 err = snd_oxfw_stream_parse_format(formats[i], &formation);
78 if (err < 0)
79 return err;
80
81 if ((formation.rate == rate) && (formation.pcm == pcm_channels))
82 break;
83 }
84 if (i == SND_OXFW_STREAM_FORMAT_ENTRIES)
85 return -EINVAL;
86
87 /* If assumed, just change rate. */
88 if (oxfw->assumed)
Takashi Sakamotob0ac0002014-12-09 00:10:46 +090089 return set_rate(oxfw, rate);
Takashi Sakamotof3699e22014-12-09 00:10:44 +090090
91 /* Calculate format length. */
92 len = 5 + formats[i][4] * 2;
93
94 err = avc_stream_set_format(oxfw->unit, dir, 0, formats[i], len);
95 if (err < 0)
96 return err;
97
98 /* Some requests just after changing format causes freezing. */
99 msleep(100);
100
101 return 0;
102}
103
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900104static void stop_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900105{
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900106 amdtp_stream_pcm_abort(stream);
107 amdtp_stream_stop(stream);
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900108
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900109 if (stream == &oxfw->tx_stream)
110 cmp_connection_break(&oxfw->out_conn);
111 else
112 cmp_connection_break(&oxfw->in_conn);
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900113}
114
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900115static int start_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream,
116 unsigned int rate, unsigned int pcm_channels)
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900117{
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900118 u8 **formats;
119 struct cmp_connection *conn;
120 struct snd_oxfw_stream_formation formation;
121 unsigned int i, midi_ports;
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900122 int err;
123
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900124 if (stream == &oxfw->rx_stream) {
125 formats = oxfw->rx_stream_formats;
126 conn = &oxfw->in_conn;
127 } else {
128 formats = oxfw->tx_stream_formats;
129 conn = &oxfw->out_conn;
130 }
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900131
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900132 /* Get stream format */
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900133 for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
134 if (formats[i] == NULL)
135 break;
136
137 err = snd_oxfw_stream_parse_format(formats[i], &formation);
138 if (err < 0)
139 goto end;
140 if (rate != formation.rate)
141 continue;
142 if (pcm_channels == 0 || pcm_channels == formation.pcm)
143 break;
144 }
145 if (i == SND_OXFW_STREAM_FORMAT_ENTRIES) {
146 err = -EINVAL;
147 goto end;
148 }
149
150 pcm_channels = formation.pcm;
151 midi_ports = DIV_ROUND_UP(formation.midi, 8);
152
153 /* The stream should have one pcm channels at least */
154 if (pcm_channels == 0) {
155 err = -EINVAL;
156 goto end;
157 }
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900158 err = amdtp_stream_set_parameters(stream, rate,
Takashi Sakamoto27ec83b2015-09-19 11:21:50 +0900159 pcm_channels, midi_ports, false);
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900160 if (err < 0)
161 goto end;
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900162
163 err = cmp_connection_establish(conn,
164 amdtp_stream_get_max_payload(stream));
165 if (err < 0)
166 goto end;
167
168 err = amdtp_stream_start(stream,
169 conn->resources.channel,
170 conn->speed);
171 if (err < 0) {
172 cmp_connection_break(conn);
173 goto end;
174 }
175
176 /* Wait first packet */
Takashi Sakamotof2b14c02015-02-27 09:39:32 +0900177 if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) {
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900178 stop_stream(oxfw, stream);
Takashi Sakamotof2b14c02015-02-27 09:39:32 +0900179 err = -ETIMEDOUT;
180 }
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900181end:
182 return err;
183}
184
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900185static int check_connection_used_by_others(struct snd_oxfw *oxfw,
186 struct amdtp_stream *stream)
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900187{
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900188 struct cmp_connection *conn;
189 bool used;
190 int err;
191
192 if (stream == &oxfw->tx_stream)
193 conn = &oxfw->out_conn;
194 else
195 conn = &oxfw->in_conn;
196
197 err = cmp_connection_check_used(conn, &used);
198 if ((err >= 0) && used && !amdtp_stream_running(stream)) {
199 dev_err(&oxfw->unit->device,
200 "Connection established by others: %cPCR[%d]\n",
201 (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
202 conn->pcr_index);
203 err = -EBUSY;
204 }
205
206 return err;
207}
208
209int snd_oxfw_stream_init_simplex(struct snd_oxfw *oxfw,
210 struct amdtp_stream *stream)
211{
212 struct cmp_connection *conn;
213 enum cmp_direction c_dir;
214 enum amdtp_stream_direction s_dir;
215 int err;
216
217 if (stream == &oxfw->tx_stream) {
218 conn = &oxfw->out_conn;
219 c_dir = CMP_OUTPUT;
220 s_dir = AMDTP_IN_STREAM;
221 } else {
222 conn = &oxfw->in_conn;
223 c_dir = CMP_INPUT;
224 s_dir = AMDTP_OUT_STREAM;
225 }
226
227 err = cmp_connection_init(conn, oxfw->unit, c_dir, 0);
228 if (err < 0)
229 goto end;
230
Takashi Sakamoto59558152015-09-19 11:21:55 +0900231 err = amdtp_am824_init(stream, oxfw->unit, s_dir, CIP_NONBLOCKING);
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900232 if (err < 0) {
233 amdtp_stream_destroy(stream);
234 cmp_connection_destroy(conn);
235 goto end;
236 }
237
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900238 /*
239 * OXFW starts to transmit packets with non-zero dbc.
240 * OXFW postpone transferring packets till handling any asynchronous
241 * packets. As a result, next isochronous packet includes more data
242 * blocks than IEC 61883-6 defines.
243 */
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900244 if (stream == &oxfw->tx_stream)
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900245 oxfw->tx_stream.flags |= CIP_SKIP_INIT_DBC_CHECK |
246 CIP_JUMBO_PAYLOAD;
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900247end:
248 return err;
249}
250
251int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw,
252 struct amdtp_stream *stream,
253 unsigned int rate, unsigned int pcm_channels)
254{
255 struct amdtp_stream *opposite;
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900256 struct snd_oxfw_stream_formation formation;
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900257 enum avc_general_plug_dir dir;
258 unsigned int substreams, opposite_substreams;
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900259 int err = 0;
260
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900261 if (stream == &oxfw->tx_stream) {
262 substreams = oxfw->capture_substreams;
263 opposite = &oxfw->rx_stream;
264 opposite_substreams = oxfw->playback_substreams;
265 dir = AVC_GENERAL_PLUG_DIR_OUT;
266 } else {
267 substreams = oxfw->playback_substreams;
268 opposite_substreams = oxfw->capture_substreams;
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900269
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900270 if (oxfw->has_output)
271 opposite = &oxfw->rx_stream;
272 else
273 opposite = NULL;
274
275 dir = AVC_GENERAL_PLUG_DIR_IN;
276 }
277
278 if (substreams == 0)
279 goto end;
280
281 /*
282 * Considering JACK/FFADO streaming:
283 * TODO: This can be removed hwdep functionality becomes popular.
284 */
285 err = check_connection_used_by_others(oxfw, stream);
286 if (err < 0)
287 goto end;
288
289 /* packet queueing error */
290 if (amdtp_streaming_error(stream))
291 stop_stream(oxfw, stream);
292
293 err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900294 if (err < 0)
295 goto end;
Takashi Sakamoto05588d32014-12-09 00:10:48 +0900296 if (rate == 0)
297 rate = formation.rate;
298 if (pcm_channels == 0)
299 pcm_channels = formation.pcm;
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900300
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900301 if ((formation.rate != rate) || (formation.pcm != pcm_channels)) {
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900302 if (opposite != NULL) {
303 err = check_connection_used_by_others(oxfw, opposite);
304 if (err < 0)
305 goto end;
306 stop_stream(oxfw, opposite);
307 }
308 stop_stream(oxfw, stream);
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900309
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900310 err = set_stream_format(oxfw, stream, rate, pcm_channels);
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900311 if (err < 0) {
312 dev_err(&oxfw->unit->device,
313 "fail to set stream format: %d\n", err);
314 goto end;
315 }
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900316
317 /* Start opposite stream if needed. */
318 if (opposite && !amdtp_stream_running(opposite) &&
319 (opposite_substreams > 0)) {
320 err = start_stream(oxfw, opposite, rate, 0);
321 if (err < 0) {
322 dev_err(&oxfw->unit->device,
323 "fail to restart stream: %d\n", err);
324 goto end;
325 }
326 }
Takashi Sakamotof3699e22014-12-09 00:10:44 +0900327 }
328
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900329 /* Start requested stream. */
330 if (!amdtp_stream_running(stream)) {
331 err = start_stream(oxfw, stream, rate, pcm_channels);
332 if (err < 0)
333 dev_err(&oxfw->unit->device,
334 "fail to start stream: %d\n", err);
335 }
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900336end:
337 return err;
338}
339
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900340void snd_oxfw_stream_stop_simplex(struct snd_oxfw *oxfw,
341 struct amdtp_stream *stream)
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900342{
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900343 if (((stream == &oxfw->tx_stream) && (oxfw->capture_substreams > 0)) ||
344 ((stream == &oxfw->rx_stream) && (oxfw->playback_substreams > 0)))
345 return;
346
347 stop_stream(oxfw, stream);
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900348}
349
Takashi Sakamotod23c2cc2015-02-21 23:54:59 +0900350/*
351 * This function should be called before starting the stream or after stopping
352 * the streams.
353 */
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900354void snd_oxfw_stream_destroy_simplex(struct snd_oxfw *oxfw,
355 struct amdtp_stream *stream)
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900356{
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900357 struct cmp_connection *conn;
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900358
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900359 if (stream == &oxfw->tx_stream)
360 conn = &oxfw->out_conn;
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900361 else
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900362 conn = &oxfw->in_conn;
363
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900364 amdtp_stream_destroy(stream);
365 cmp_connection_destroy(conn);
366}
367
368void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw,
369 struct amdtp_stream *stream)
370{
371 struct cmp_connection *conn;
372
373 if (stream == &oxfw->tx_stream)
374 conn = &oxfw->out_conn;
375 else
376 conn = &oxfw->in_conn;
377
378 if (cmp_connection_update(conn) < 0)
379 stop_stream(oxfw, stream);
380 else
381 amdtp_stream_update(stream);
Takashi Sakamotoe2786ca2014-11-29 00:59:27 +0900382}
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900383
Takashi Sakamoto3c961012014-12-09 00:10:43 +0900384int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
385 enum avc_general_plug_dir dir,
386 struct snd_oxfw_stream_formation *formation)
387{
388 u8 *format;
389 unsigned int len;
390 int err;
391
392 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
393 format = kmalloc(len, GFP_KERNEL);
394 if (format == NULL)
395 return -ENOMEM;
396
397 err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
398 if (err < 0)
399 goto end;
400 if (len < 3) {
401 err = -EIO;
402 goto end;
403 }
404
405 err = snd_oxfw_stream_parse_format(format, formation);
406end:
407 kfree(format);
408 return err;
409}
410
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900411/*
412 * See Table 6.16 - AM824 Stream Format
413 * Figure 6.19 - format_information field for AM824 Compound
414 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
415 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
416 */
417int snd_oxfw_stream_parse_format(u8 *format,
418 struct snd_oxfw_stream_formation *formation)
419{
420 unsigned int i, e, channels, type;
421
422 memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
423
424 /*
425 * this module can support a hierarchy combination that:
426 * Root: Audio and Music (0x90)
427 * Level 1: AM824 Compound (0x40)
428 */
429 if ((format[0] != 0x90) || (format[1] != 0x40))
430 return -ENOSYS;
431
432 /* check the sampling rate */
433 for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
434 if (format[2] == avc_stream_rate_table[i])
435 break;
436 }
437 if (i == ARRAY_SIZE(avc_stream_rate_table))
438 return -ENOSYS;
439
440 formation->rate = oxfw_rate_table[i];
441
442 for (e = 0; e < format[4]; e++) {
443 channels = format[5 + e * 2];
444 type = format[6 + e * 2];
445
446 switch (type) {
447 /* IEC 60958 Conformant, currently handled as MBLA */
448 case 0x00:
449 /* Multi Bit Linear Audio (Raw) */
450 case 0x06:
451 formation->pcm += channels;
452 break;
453 /* MIDI Conformant */
454 case 0x0d:
455 formation->midi = channels;
456 break;
457 /* IEC 61937-3 to 7 */
458 case 0x01:
459 case 0x02:
460 case 0x03:
461 case 0x04:
462 case 0x05:
463 /* Multi Bit Linear Audio */
464 case 0x07: /* DVD-Audio */
465 case 0x0c: /* High Precision */
466 /* One Bit Audio */
467 case 0x08: /* (Plain) Raw */
468 case 0x09: /* (Plain) SACD */
469 case 0x0a: /* (Encoded) Raw */
470 case 0x0b: /* (Encoded) SACD */
471 /* SMPTE Time-Code conformant */
472 case 0x0e:
473 /* Sample Count */
474 case 0x0f:
475 /* Anciliary Data */
476 case 0x10:
477 /* Synchronization Stream (Stereo Raw audio) */
478 case 0x40:
479 /* Don't care */
480 case 0xff:
481 default:
482 return -ENOSYS; /* not supported */
483 }
484 }
485
486 if (formation->pcm > AMDTP_MAX_CHANNELS_FOR_PCM ||
487 formation->midi > AMDTP_MAX_CHANNELS_FOR_MIDI)
488 return -ENOSYS;
489
490 return 0;
491}
492
493static int
494assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
495 unsigned int pid, u8 *buf, unsigned int *len,
496 u8 **formats)
497{
498 struct snd_oxfw_stream_formation formation;
499 unsigned int i, eid;
500 int err;
501
502 /* get format at current sampling rate */
503 err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
504 if (err < 0) {
505 dev_err(&oxfw->unit->device,
506 "fail to get current stream format for isoc %s plug %d:%d\n",
507 (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
508 pid, err);
509 goto end;
510 }
511
512 /* parse and set stream format */
513 eid = 0;
514 err = snd_oxfw_stream_parse_format(buf, &formation);
515 if (err < 0)
516 goto end;
517
Andrzej Hajda96bab822015-08-07 09:59:35 +0200518 formats[eid] = kmemdup(buf, *len, GFP_KERNEL);
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900519 if (formats[eid] == NULL) {
520 err = -ENOMEM;
521 goto end;
522 }
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900523
524 /* apply the format for each available sampling rate */
525 for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
526 if (formation.rate == oxfw_rate_table[i])
527 continue;
528
529 err = avc_general_inquiry_sig_fmt(oxfw->unit,
530 oxfw_rate_table[i],
531 dir, pid);
532 if (err < 0)
533 continue;
534
535 eid++;
Andrzej Hajda96bab822015-08-07 09:59:35 +0200536 formats[eid] = kmemdup(buf, *len, GFP_KERNEL);
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900537 if (formats[eid] == NULL) {
538 err = -ENOMEM;
539 goto end;
540 }
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900541 formats[eid][2] = avc_stream_rate_table[i];
542 }
543
544 err = 0;
545 oxfw->assumed = true;
546end:
547 return err;
548}
549
550static int fill_stream_formats(struct snd_oxfw *oxfw,
551 enum avc_general_plug_dir dir,
552 unsigned short pid)
553{
554 u8 *buf, **formats;
555 unsigned int len, eid = 0;
556 struct snd_oxfw_stream_formation dummy;
557 int err;
558
559 buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
560 if (buf == NULL)
561 return -ENOMEM;
562
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900563 if (dir == AVC_GENERAL_PLUG_DIR_OUT)
564 formats = oxfw->tx_stream_formats;
565 else
566 formats = oxfw->rx_stream_formats;
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900567
568 /* get first entry */
569 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
570 err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
571 if (err == -ENOSYS) {
572 /* LIST subfunction is not implemented */
573 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
574 err = assume_stream_formats(oxfw, dir, pid, buf, &len,
575 formats);
576 goto end;
577 } else if (err < 0) {
578 dev_err(&oxfw->unit->device,
579 "fail to get stream format %d for isoc %s plug %d:%d\n",
580 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
581 pid, err);
582 goto end;
583 }
584
585 /* LIST subfunction is implemented */
586 while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
587 /* The format is too short. */
588 if (len < 3) {
589 err = -EIO;
590 break;
591 }
592
593 /* parse and set stream format */
594 err = snd_oxfw_stream_parse_format(buf, &dummy);
595 if (err < 0)
596 break;
597
Andrzej Hajda96bab822015-08-07 09:59:35 +0200598 formats[eid] = kmemdup(buf, len, GFP_KERNEL);
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900599 if (formats[eid] == NULL) {
600 err = -ENOMEM;
601 break;
602 }
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900603
604 /* get next entry */
605 len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
606 err = avc_stream_get_format_list(oxfw->unit, dir, 0,
607 buf, &len, ++eid);
608 /* No entries remained. */
609 if (err == -EINVAL) {
610 err = 0;
611 break;
612 } else if (err < 0) {
613 dev_err(&oxfw->unit->device,
614 "fail to get stream format %d for isoc %s plug %d:%d\n",
615 eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
616 "out",
617 pid, err);
618 break;
619 }
620 }
621end:
622 kfree(buf);
623 return err;
624}
625
626int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
627{
628 u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
629 int err;
630
631 /* the number of plugs for isoc in/out, ext in/out */
632 err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
633 if (err < 0) {
634 dev_err(&oxfw->unit->device,
635 "fail to get info for isoc/external in/out plugs: %d\n",
636 err);
637 goto end;
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900638 } else if ((plugs[0] == 0) && (plugs[1] == 0)) {
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900639 err = -ENOSYS;
640 goto end;
641 }
642
Takashi Sakamotob0ac0002014-12-09 00:10:46 +0900643 /* use oPCR[0] if exists */
644 if (plugs[1] > 0) {
645 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
646 if (err < 0)
647 goto end;
648 oxfw->has_output = true;
649 }
650
Takashi Sakamoto5cd1d3f2014-12-09 00:10:42 +0900651 /* use iPCR[0] if exists */
652 if (plugs[0] > 0)
653 err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
654end:
655 return err;
656}
Takashi Sakamoto8985f4a2014-12-09 00:10:49 +0900657
658void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
659{
660 oxfw->dev_lock_changed = true;
661 wake_up(&oxfw->hwdep_wait);
662}
663
664int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
665{
666 int err;
667
668 spin_lock_irq(&oxfw->lock);
669
670 /* user land lock this */
671 if (oxfw->dev_lock_count < 0) {
672 err = -EBUSY;
673 goto end;
674 }
675
676 /* this is the first time */
677 if (oxfw->dev_lock_count++ == 0)
678 snd_oxfw_stream_lock_changed(oxfw);
679 err = 0;
680end:
681 spin_unlock_irq(&oxfw->lock);
682 return err;
683}
684
685void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
686{
687 spin_lock_irq(&oxfw->lock);
688
689 if (WARN_ON(oxfw->dev_lock_count <= 0))
690 goto end;
691 if (--oxfw->dev_lock_count == 0)
692 snd_oxfw_stream_lock_changed(oxfw);
693end:
694 spin_unlock_irq(&oxfw->lock);
695}