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