blob: 5fc52706f7c5cb959ca5d23e4b4ed2375505f735 [file] [log] [blame]
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +09001/*
2 * bebob_stream.c - a part of driver for BeBoB based devices
3 *
4 * Copyright (c) 2013-2014 Takashi Sakamoto
5 *
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
8
9#include "./bebob.h"
10
11#define CALLBACK_TIMEOUT 1000
Takashi Sakamotob6bc8122014-04-25 22:45:16 +090012#define FW_ISO_RESOURCE_DELAY 1000
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +090013
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
26const 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 */
40static 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
50static unsigned int
51get_formation_index(unsigned int rate)
52{
53 unsigned int i;
54
55 for (i = 0; i < ARRAY_SIZE(snd_bebob_rate_table); i++) {
56 if (snd_bebob_rate_table[i] == rate)
57 return i;
58 }
59 return -EINVAL;
60}
61
62int
63snd_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);
91end:
92 return err;
93}
94
95int
96snd_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);
115end:
116 return err;
117}
118
119int
120snd_bebob_stream_check_internal_clock(struct snd_bebob *bebob, bool *internal)
121{
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900122 struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900123 u8 addr[AVC_BRIDGECO_ADDR_BYTES], input[7];
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900124 unsigned int id;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900125 int err = 0;
126
127 *internal = false;
128
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900129 /* 1.The device has its own operation to switch source of clock */
130 if (clk_spec) {
131 err = clk_spec->get(bebob, &id);
132 if (err < 0)
133 dev_err(&bebob->unit->device,
134 "fail to get clock source: %d\n", err);
135 else if (strncmp(clk_spec->labels[id], SND_BEBOB_CLOCK_INTERNAL,
136 strlen(SND_BEBOB_CLOCK_INTERNAL)) == 0)
137 *internal = true;
138 goto end;
139 }
140
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900141 /*
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900142 * 2.The device don't support to switch source of clock then assumed
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900143 * to use internal clock always
144 */
145 if (bebob->sync_input_plug < 0) {
146 *internal = true;
147 goto end;
148 }
149
150 /*
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900151 * 3.The device supports to switch source of clock by an usual way.
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900152 * Let's check input for 'Music Sub Unit Sync Input' plug.
153 */
154 avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
155 bebob->sync_input_plug);
156 err = avc_bridgeco_get_plug_input(bebob->unit, addr, input);
157 if (err < 0) {
158 dev_err(&bebob->unit->device,
159 "fail to get an input for MSU in plug %d: %d\n",
160 bebob->sync_input_plug, err);
161 goto end;
162 }
163
164 /*
165 * If there are no input plugs, all of fields are 0xff.
166 * Here check the first field. This field is used for direction.
167 */
168 if (input[0] == 0xff) {
169 *internal = true;
170 goto end;
171 }
172
173 /*
174 * If source of clock is internal CSR, Music Sub Unit Sync Input is
175 * a destination of Music Sub Unit Sync Output.
176 */
177 *internal = ((input[0] == AVC_BRIDGECO_PLUG_DIR_OUT) &&
178 (input[1] == AVC_BRIDGECO_PLUG_MODE_SUBUNIT) &&
179 (input[2] == 0x0c) &&
180 (input[3] == 0x00));
181end:
182 return err;
183}
184
185static unsigned int
186map_data_channels(struct snd_bebob *bebob, struct amdtp_stream *s)
187{
188 unsigned int sec, sections, ch, channels;
189 unsigned int pcm, midi, location;
190 unsigned int stm_pos, sec_loc, pos;
191 u8 *buf, addr[AVC_BRIDGECO_ADDR_BYTES], type;
192 enum avc_bridgeco_plug_dir dir;
193 int err;
194
195 /*
196 * The length of return value of this command cannot be expected. Here
197 * use the maximum length of FCP.
198 */
199 buf = kzalloc(256, GFP_KERNEL);
200 if (buf == NULL)
201 return -ENOMEM;
202
203 if (s == &bebob->tx_stream)
204 dir = AVC_BRIDGECO_PLUG_DIR_OUT;
205 else
206 dir = AVC_BRIDGECO_PLUG_DIR_IN;
207
208 avc_bridgeco_fill_unit_addr(addr, dir, AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
209 err = avc_bridgeco_get_plug_ch_pos(bebob->unit, addr, buf, 256);
210 if (err < 0) {
211 dev_err(&bebob->unit->device,
212 "fail to get channel position for isoc %s plug 0: %d\n",
213 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" : "out",
214 err);
215 goto end;
216 }
217 pos = 0;
218
219 /* positions in I/O buffer */
220 pcm = 0;
221 midi = 0;
222
223 /* the number of sections in AMDTP packet */
224 sections = buf[pos++];
225
226 for (sec = 0; sec < sections; sec++) {
227 /* type of this section */
228 avc_bridgeco_fill_unit_addr(addr, dir,
229 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
230 err = avc_bridgeco_get_plug_section_type(bebob->unit, addr,
231 sec, &type);
232 if (err < 0) {
233 dev_err(&bebob->unit->device,
234 "fail to get section type for isoc %s plug 0: %d\n",
235 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
236 "out",
237 err);
238 goto end;
239 }
240 /* NoType */
241 if (type == 0xff) {
242 err = -ENOSYS;
243 goto end;
244 }
245
246 /* the number of channels in this section */
247 channels = buf[pos++];
248
249 for (ch = 0; ch < channels; ch++) {
250 /* position of this channel in AMDTP packet */
251 stm_pos = buf[pos++] - 1;
252 /* location of this channel in this section */
253 sec_loc = buf[pos++] - 1;
254
Takashi Sakamoto9076c222014-04-25 22:45:25 +0900255 /*
256 * Basically the number of location is within the
257 * number of channels in this section. But some models
258 * of M-Audio don't follow this. Its location for MIDI
259 * is the position of MIDI channels in AMDTP packet.
260 */
261 if (sec_loc >= channels)
262 sec_loc = ch;
263
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900264 switch (type) {
265 /* for MIDI conformant data channel */
266 case 0x0a:
267 /* AMDTP_MAX_CHANNELS_FOR_MIDI is 1. */
268 if ((midi > 0) && (stm_pos != midi)) {
269 err = -ENOSYS;
270 goto end;
271 }
272 s->midi_position = stm_pos;
273 midi = stm_pos;
274 break;
275 /* for PCM data channel */
276 case 0x01: /* Headphone */
277 case 0x02: /* Microphone */
278 case 0x03: /* Line */
279 case 0x04: /* SPDIF */
280 case 0x05: /* ADAT */
281 case 0x06: /* TDIF */
282 case 0x07: /* MADI */
283 /* for undefined/changeable signal */
284 case 0x08: /* Analog */
285 case 0x09: /* Digital */
286 default:
287 location = pcm + sec_loc;
288 if (location >= AMDTP_MAX_CHANNELS_FOR_PCM) {
289 err = -ENOSYS;
290 goto end;
291 }
292 s->pcm_positions[location] = stm_pos;
293 break;
294 }
295 }
296
297 if (type != 0x0a)
298 pcm += channels;
299 else
300 midi += channels;
301 }
302end:
303 kfree(buf);
304 return err;
305}
306
307static int
308init_both_connections(struct snd_bebob *bebob)
309{
310 int err;
311
312 err = cmp_connection_init(&bebob->in_conn,
313 bebob->unit, CMP_INPUT, 0);
314 if (err < 0)
315 goto end;
316
317 err = cmp_connection_init(&bebob->out_conn,
318 bebob->unit, CMP_OUTPUT, 0);
319 if (err < 0)
320 cmp_connection_destroy(&bebob->in_conn);
321end:
322 return err;
323}
324
325static int
326check_connection_used_by_others(struct snd_bebob *bebob, struct amdtp_stream *s)
327{
328 struct cmp_connection *conn;
329 bool used;
330 int err;
331
332 if (s == &bebob->tx_stream)
333 conn = &bebob->out_conn;
334 else
335 conn = &bebob->in_conn;
336
337 err = cmp_connection_check_used(conn, &used);
338 if ((err >= 0) && used && !amdtp_stream_running(s)) {
339 dev_err(&bebob->unit->device,
340 "Connection established by others: %cPCR[%d]\n",
341 (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
342 conn->pcr_index);
343 err = -EBUSY;
344 }
345
346 return err;
347}
348
349static int
350make_both_connections(struct snd_bebob *bebob, unsigned int rate)
351{
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900352 int index, pcm_channels, midi_channels, err = 0;
353
354 if (bebob->connected)
355 goto end;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900356
357 /* confirm params for both streams */
358 index = get_formation_index(rate);
359 pcm_channels = bebob->tx_stream_formations[index].pcm;
360 midi_channels = bebob->tx_stream_formations[index].midi;
361 amdtp_stream_set_parameters(&bebob->tx_stream,
362 rate, pcm_channels, midi_channels * 8);
363 pcm_channels = bebob->rx_stream_formations[index].pcm;
364 midi_channels = bebob->rx_stream_formations[index].midi;
365 amdtp_stream_set_parameters(&bebob->rx_stream,
366 rate, pcm_channels, midi_channels * 8);
367
368 /* establish connections for both streams */
369 err = cmp_connection_establish(&bebob->out_conn,
370 amdtp_stream_get_max_payload(&bebob->tx_stream));
371 if (err < 0)
372 goto end;
373 err = cmp_connection_establish(&bebob->in_conn,
374 amdtp_stream_get_max_payload(&bebob->rx_stream));
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900375 if (err < 0) {
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900376 cmp_connection_break(&bebob->out_conn);
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900377 goto end;
378 }
379
380 bebob->connected = true;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900381end:
382 return err;
383}
384
385static void
386break_both_connections(struct snd_bebob *bebob)
387{
388 cmp_connection_break(&bebob->in_conn);
389 cmp_connection_break(&bebob->out_conn);
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900390
391 bebob->connected = false;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900392}
393
394static void
395destroy_both_connections(struct snd_bebob *bebob)
396{
397 break_both_connections(bebob);
398
399 cmp_connection_destroy(&bebob->in_conn);
400 cmp_connection_destroy(&bebob->out_conn);
401}
402
403static int
404get_sync_mode(struct snd_bebob *bebob, enum cip_flags *sync_mode)
405{
406 /* currently this module doesn't support SYT-Match mode */
407 *sync_mode = CIP_SYNC_TO_DEVICE;
408 return 0;
409}
410
411static int
412start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream,
413 unsigned int rate)
414{
415 struct cmp_connection *conn;
416 int err = 0;
417
418 if (stream == &bebob->rx_stream)
419 conn = &bebob->in_conn;
420 else
421 conn = &bebob->out_conn;
422
423 /* channel mapping */
424 err = map_data_channels(bebob, stream);
425 if (err < 0)
426 goto end;
427
428 /* start amdtp stream */
429 err = amdtp_stream_start(stream,
430 conn->resources.channel,
431 conn->speed);
432end:
433 return err;
434}
435
436int snd_bebob_stream_init_duplex(struct snd_bebob *bebob)
437{
438 int err;
439
440 err = init_both_connections(bebob);
441 if (err < 0)
442 goto end;
443
444 err = amdtp_stream_init(&bebob->tx_stream, bebob->unit,
445 AMDTP_IN_STREAM, CIP_BLOCKING);
446 if (err < 0) {
447 amdtp_stream_destroy(&bebob->tx_stream);
448 destroy_both_connections(bebob);
449 goto end;
450 }
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900451 /* See comments in next function */
452 init_completion(&bebob->bus_reset);
453 bebob->tx_stream.flags |= CIP_SKIP_INIT_DBC_CHECK;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900454
455 err = amdtp_stream_init(&bebob->rx_stream, bebob->unit,
456 AMDTP_OUT_STREAM, CIP_BLOCKING);
457 if (err < 0) {
458 amdtp_stream_destroy(&bebob->tx_stream);
459 amdtp_stream_destroy(&bebob->rx_stream);
460 destroy_both_connections(bebob);
461 }
462end:
463 return err;
464}
465
466int snd_bebob_stream_start_duplex(struct snd_bebob *bebob, int rate)
467{
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900468 struct snd_bebob_rate_spec *rate_spec = bebob->spec->rate;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900469 struct amdtp_stream *master, *slave;
470 atomic_t *slave_substreams;
471 enum cip_flags sync_mode;
472 unsigned int curr_rate;
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900473 bool updated = false;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900474 int err = 0;
475
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900476 /*
477 * Normal BeBoB firmware has a quirk at bus reset to transmits packets
478 * with discontinuous value in dbc field.
479 *
480 * This 'struct completion' is used to call .update() at first to update
481 * connections/streams. Next following codes handle streaming error.
482 */
483 if (amdtp_streaming_error(&bebob->tx_stream)) {
484 if (completion_done(&bebob->bus_reset))
485 reinit_completion(&bebob->bus_reset);
486
487 updated = (wait_for_completion_interruptible_timeout(
488 &bebob->bus_reset,
489 msecs_to_jiffies(FW_ISO_RESOURCE_DELAY)) > 0);
490 }
491
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900492 mutex_lock(&bebob->mutex);
493
494 /* Need no substreams */
495 if (atomic_read(&bebob->playback_substreams) == 0 &&
496 atomic_read(&bebob->capture_substreams) == 0)
497 goto end;
498
499 err = get_sync_mode(bebob, &sync_mode);
500 if (err < 0)
501 goto end;
502 if (sync_mode == CIP_SYNC_TO_DEVICE) {
503 master = &bebob->tx_stream;
504 slave = &bebob->rx_stream;
505 slave_substreams = &bebob->playback_substreams;
506 } else {
507 master = &bebob->rx_stream;
508 slave = &bebob->tx_stream;
509 slave_substreams = &bebob->capture_substreams;
510 }
511
512 /*
513 * Considering JACK/FFADO streaming:
514 * TODO: This can be removed hwdep functionality becomes popular.
515 */
516 err = check_connection_used_by_others(bebob, master);
517 if (err < 0)
518 goto end;
519
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900520 /*
521 * packet queueing error or detecting discontinuity
522 *
523 * At bus reset, connections should not be broken here. So streams need
524 * to be re-started. This is a reason to use SKIP_INIT_DBC_CHECK flag.
525 */
526 if (amdtp_streaming_error(master))
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900527 amdtp_stream_stop(master);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900528 if (amdtp_streaming_error(slave))
529 amdtp_stream_stop(slave);
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900530 if (!updated &&
531 !amdtp_stream_running(master) && !amdtp_stream_running(slave))
532 break_both_connections(bebob);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900533
534 /* stop streams if rate is different */
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900535 err = rate_spec->get(bebob, &curr_rate);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900536 if (err < 0) {
537 dev_err(&bebob->unit->device,
538 "fail to get sampling rate: %d\n", err);
539 goto end;
540 }
541 if (rate == 0)
542 rate = curr_rate;
543 if (rate != curr_rate) {
544 amdtp_stream_stop(master);
545 amdtp_stream_stop(slave);
546 break_both_connections(bebob);
547 }
548
549 /* master should be always running */
550 if (!amdtp_stream_running(master)) {
551 amdtp_stream_set_sync(sync_mode, master, slave);
552 bebob->master = master;
553
554 /*
555 * NOTE:
556 * If establishing connections at first, Yamaha GO46
557 * (and maybe Terratec X24) don't generate sound.
558 */
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900559 err = rate_spec->set(bebob, rate);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900560 if (err < 0) {
561 dev_err(&bebob->unit->device,
562 "fail to set sampling rate: %d\n",
563 err);
564 goto end;
565 }
566
567 err = make_both_connections(bebob, rate);
568 if (err < 0)
569 goto end;
570
571 err = start_stream(bebob, master, rate);
572 if (err < 0) {
573 dev_err(&bebob->unit->device,
574 "fail to run AMDTP master stream:%d\n", err);
575 break_both_connections(bebob);
576 goto end;
577 }
578
579 /* wait first callback */
580 if (!amdtp_stream_wait_callback(master, CALLBACK_TIMEOUT)) {
581 amdtp_stream_stop(master);
582 break_both_connections(bebob);
583 err = -ETIMEDOUT;
584 goto end;
585 }
586 }
587
588 /* start slave if needed */
589 if (atomic_read(slave_substreams) > 0 && !amdtp_stream_running(slave)) {
590 err = start_stream(bebob, slave, rate);
591 if (err < 0) {
592 dev_err(&bebob->unit->device,
593 "fail to run AMDTP slave stream:%d\n", err);
594 amdtp_stream_stop(master);
595 break_both_connections(bebob);
596 goto end;
597 }
598
599 /* wait first callback */
600 if (!amdtp_stream_wait_callback(slave, CALLBACK_TIMEOUT)) {
601 amdtp_stream_stop(slave);
602 amdtp_stream_stop(master);
603 break_both_connections(bebob);
604 err = -ETIMEDOUT;
605 }
606 }
607end:
608 mutex_unlock(&bebob->mutex);
609 return err;
610}
611
612void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob)
613{
614 struct amdtp_stream *master, *slave;
615 atomic_t *master_substreams, *slave_substreams;
616
617 mutex_lock(&bebob->mutex);
618
619 if (bebob->master == &bebob->rx_stream) {
620 slave = &bebob->tx_stream;
621 master = &bebob->rx_stream;
622 slave_substreams = &bebob->capture_substreams;
623 master_substreams = &bebob->playback_substreams;
624 } else {
625 slave = &bebob->rx_stream;
626 master = &bebob->tx_stream;
627 slave_substreams = &bebob->playback_substreams;
628 master_substreams = &bebob->capture_substreams;
629 }
630
631 if (atomic_read(slave_substreams) == 0) {
632 amdtp_stream_pcm_abort(slave);
633 amdtp_stream_stop(slave);
634
635 if (atomic_read(master_substreams) == 0) {
636 amdtp_stream_pcm_abort(master);
637 amdtp_stream_stop(master);
638 break_both_connections(bebob);
639 }
640 }
641
642 mutex_unlock(&bebob->mutex);
643}
644
645void snd_bebob_stream_update_duplex(struct snd_bebob *bebob)
646{
647 /* vs. XRUN recovery due to discontinuity at bus reset */
648 mutex_lock(&bebob->mutex);
649
650 if ((cmp_connection_update(&bebob->in_conn) < 0) ||
651 (cmp_connection_update(&bebob->out_conn) < 0)) {
652 amdtp_stream_pcm_abort(&bebob->rx_stream);
653 amdtp_stream_pcm_abort(&bebob->tx_stream);
654 amdtp_stream_stop(&bebob->rx_stream);
655 amdtp_stream_stop(&bebob->tx_stream);
656 break_both_connections(bebob);
657 } else {
658 amdtp_stream_update(&bebob->rx_stream);
659 amdtp_stream_update(&bebob->tx_stream);
660 }
661
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900662 /* wake up stream_start_duplex() */
663 if (!completion_done(&bebob->bus_reset))
664 complete_all(&bebob->bus_reset);
665
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900666 mutex_unlock(&bebob->mutex);
667}
668
669void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob)
670{
671 mutex_lock(&bebob->mutex);
672
673 amdtp_stream_pcm_abort(&bebob->rx_stream);
674 amdtp_stream_pcm_abort(&bebob->tx_stream);
675
676 amdtp_stream_stop(&bebob->rx_stream);
677 amdtp_stream_stop(&bebob->tx_stream);
678
679 amdtp_stream_destroy(&bebob->rx_stream);
680 amdtp_stream_destroy(&bebob->tx_stream);
681
682 destroy_both_connections(bebob);
683
684 mutex_unlock(&bebob->mutex);
685}
686
687/*
688 * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’
689 * in Additional AVC commands (Nov 2003, BridgeCo)
690 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
691 */
692static int
693parse_stream_formation(u8 *buf, unsigned int len,
694 struct snd_bebob_stream_formation *formation)
695{
696 unsigned int i, e, channels, format;
697
698 /*
699 * this module can support a hierarchy combination that:
700 * Root: Audio and Music (0x90)
701 * Level 1: AM824 Compound (0x40)
702 */
703 if ((buf[0] != 0x90) || (buf[1] != 0x40))
704 return -ENOSYS;
705
706 /* check sampling rate */
707 for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) {
708 if (buf[2] == bridgeco_freq_table[i])
709 break;
710 }
711 if (i == sizeof(bridgeco_freq_table))
712 return -ENOSYS;
713
714 /* Avoid double count by different entries for the same rate. */
715 memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation));
716
717 for (e = 0; e < buf[4]; e++) {
718 channels = buf[5 + e * 2];
719 format = buf[6 + e * 2];
720
721 switch (format) {
722 /* IEC 60958-3, currently handle as MBLA */
723 case 0x00:
724 /* Multi bit linear audio */
725 case 0x06: /* Raw */
726 formation[i].pcm += channels;
727 break;
728 /* MIDI Conformant */
729 case 0x0d:
730 formation[i].midi += channels;
731 break;
732 /* IEC 61937-3 to 7 */
733 case 0x01:
734 case 0x02:
735 case 0x03:
736 case 0x04:
737 case 0x05:
738 /* Multi bit linear audio */
739 case 0x07: /* DVD-Audio */
740 case 0x0c: /* High Precision */
741 /* One Bit Audio */
742 case 0x08: /* (Plain) Raw */
743 case 0x09: /* (Plain) SACD */
744 case 0x0a: /* (Encoded) Raw */
745 case 0x0b: /* (Encoded) SACD */
746 /* Synchronization Stream (Stereo Raw audio) */
747 case 0x40:
748 /* Don't care */
749 case 0xff:
750 default:
751 return -ENOSYS; /* not supported */
752 }
753 }
754
755 if (formation[i].pcm > AMDTP_MAX_CHANNELS_FOR_PCM ||
756 formation[i].midi > AMDTP_MAX_CHANNELS_FOR_MIDI)
757 return -ENOSYS;
758
759 return 0;
760}
761
762static int
763fill_stream_formations(struct snd_bebob *bebob, enum avc_bridgeco_plug_dir dir,
764 unsigned short pid)
765{
766 u8 *buf;
767 struct snd_bebob_stream_formation *formations;
768 unsigned int len, eid;
769 u8 addr[AVC_BRIDGECO_ADDR_BYTES];
770 int err;
771
772 buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL);
773 if (buf == NULL)
774 return -ENOMEM;
775
776 if (dir == AVC_BRIDGECO_PLUG_DIR_IN)
777 formations = bebob->rx_stream_formations;
778 else
779 formations = bebob->tx_stream_formations;
780
781 for (eid = 0; eid < SND_BEBOB_STRM_FMT_ENTRIES; eid++) {
782 len = FORMAT_MAXIMUM_LENGTH;
783 avc_bridgeco_fill_unit_addr(addr, dir,
784 AVC_BRIDGECO_PLUG_UNIT_ISOC, pid);
785 err = avc_bridgeco_get_plug_strm_fmt(bebob->unit, addr, buf,
786 &len, eid);
787 /* No entries remained. */
788 if (err == -EINVAL && eid > 0) {
789 err = 0;
790 break;
791 } else if (err < 0) {
792 dev_err(&bebob->unit->device,
793 "fail to get stream format %d for isoc %s plug %d:%d\n",
794 eid,
795 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
796 "out",
797 pid, err);
798 break;
799 }
800
801 err = parse_stream_formation(buf, len, formations);
802 if (err < 0)
803 break;
804 }
805
806 kfree(buf);
807 return err;
808}
809
810static int
811seek_msu_sync_input_plug(struct snd_bebob *bebob)
812{
813 u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
814 unsigned int i, type;
815 int err;
816
817 /* Get the number of Music Sub Unit for both direction. */
818 err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs);
819 if (err < 0) {
820 dev_err(&bebob->unit->device,
821 "fail to get info for MSU in/out plugs: %d\n",
822 err);
823 goto end;
824 }
825
826 /* seek destination plugs for 'MSU sync input' */
827 bebob->sync_input_plug = -1;
828 for (i = 0; i < plugs[0]; i++) {
829 avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, i);
830 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
831 if (err < 0) {
832 dev_err(&bebob->unit->device,
833 "fail to get type for MSU in plug %d: %d\n",
834 i, err);
835 goto end;
836 }
837
838 if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
839 bebob->sync_input_plug = i;
840 break;
841 }
842 }
843end:
844 return err;
845}
846
847int snd_bebob_stream_discover(struct snd_bebob *bebob)
848{
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900849 struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900850 u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
851 enum avc_bridgeco_plug_type type;
852 unsigned int i;
853 int err;
854
855 /* the number of plugs for isoc in/out, ext in/out */
856 err = avc_general_get_plug_info(bebob->unit, 0x1f, 0x07, 0x00, plugs);
857 if (err < 0) {
858 dev_err(&bebob->unit->device,
859 "fail to get info for isoc/external in/out plugs: %d\n",
860 err);
861 goto end;
862 }
863
864 /*
865 * This module supports at least one isoc input plug and one isoc
866 * output plug.
867 */
868 if ((plugs[0] == 0) || (plugs[1] == 0)) {
869 err = -ENOSYS;
870 goto end;
871 }
872
873 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
874 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
875 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
876 if (err < 0) {
877 dev_err(&bebob->unit->device,
878 "fail to get type for isoc in plug 0: %d\n", err);
879 goto end;
880 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
881 err = -ENOSYS;
882 goto end;
883 }
884 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_IN, 0);
885 if (err < 0)
886 goto end;
887
888 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
889 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
890 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
891 if (err < 0) {
892 dev_err(&bebob->unit->device,
893 "fail to get type for isoc out plug 0: %d\n", err);
894 goto end;
895 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
896 err = -ENOSYS;
897 goto end;
898 }
899 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_OUT, 0);
900 if (err < 0)
901 goto end;
902
903 /* count external input plugs for MIDI */
904 bebob->midi_input_ports = 0;
905 for (i = 0; i < plugs[2]; i++) {
906 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
907 AVC_BRIDGECO_PLUG_UNIT_EXT, i);
908 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
909 if (err < 0) {
910 dev_err(&bebob->unit->device,
911 "fail to get type for external in plug %d: %d\n",
912 i, err);
913 goto end;
914 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
915 bebob->midi_input_ports++;
916 }
917 }
918
919 /* count external output plugs for MIDI */
920 bebob->midi_output_ports = 0;
921 for (i = 0; i < plugs[3]; i++) {
922 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
923 AVC_BRIDGECO_PLUG_UNIT_EXT, i);
924 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
925 if (err < 0) {
926 dev_err(&bebob->unit->device,
927 "fail to get type for external out plug %d: %d\n",
928 i, err);
929 goto end;
930 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
931 bebob->midi_output_ports++;
932 }
933 }
934
935 /* for check source of clock later */
Takashi Sakamoto1fc95222014-04-25 22:45:21 +0900936 if (!clk_spec)
937 err = seek_msu_sync_input_plug(bebob);
Takashi Sakamotoeb7b3a02014-04-25 22:45:15 +0900938end:
939 return err;
940}
Takashi Sakamoto618eabe2014-04-25 22:45:20 +0900941
942void snd_bebob_stream_lock_changed(struct snd_bebob *bebob)
943{
944 bebob->dev_lock_changed = true;
945 wake_up(&bebob->hwdep_wait);
946}
947
948int snd_bebob_stream_lock_try(struct snd_bebob *bebob)
949{
950 int err;
951
952 spin_lock_irq(&bebob->lock);
953
954 /* user land lock this */
955 if (bebob->dev_lock_count < 0) {
956 err = -EBUSY;
957 goto end;
958 }
959
960 /* this is the first time */
961 if (bebob->dev_lock_count++ == 0)
962 snd_bebob_stream_lock_changed(bebob);
963 err = 0;
964end:
965 spin_unlock_irq(&bebob->lock);
966 return err;
967}
968
969void snd_bebob_stream_lock_release(struct snd_bebob *bebob)
970{
971 spin_lock_irq(&bebob->lock);
972
973 if (WARN_ON(bebob->dev_lock_count <= 0))
974 goto end;
975 if (--bebob->dev_lock_count == 0)
976 snd_bebob_stream_lock_changed(bebob);
977end:
978 spin_unlock_irq(&bebob->lock);
979}