2 * bebob_stream.c - a part of driver for BeBoB based devices
4 * Copyright (c) 2013-2014 Takashi Sakamoto
6 * Licensed under the terms of the GNU General Public License, version 2.
11 #define CALLBACK_TIMEOUT 1000
12 #define FW_ISO_RESOURCE_DELAY 1000
16 * For BeBoB streams, Both of input and output CMP connection are important.
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'.
23 /* 128 is an arbitrary length but it seems to be enough */
24 #define FORMAT_MAXIMUM_LENGTH 128
26 const unsigned int snd_bebob_rate_table[SND_BEBOB_STRM_FMT_ENTRIES] = {
37 * See: Table 51: Extended Stream Format Info ‘Sampling Frequency’
38 * in Additional AVC commands (Nov 2003, BridgeCo)
40 static const unsigned int bridgeco_freq_table[] = {
51 get_formation_index(unsigned int rate)
55 for (i = 0; i < ARRAY_SIZE(snd_bebob_rate_table); i++) {
56 if (snd_bebob_rate_table[i] == rate)
63 snd_bebob_stream_get_rate(struct snd_bebob *bebob, unsigned int *curr_rate)
65 unsigned int tx_rate, rx_rate, trials;
70 err = avc_general_get_sig_fmt(bebob->unit, &tx_rate,
71 AVC_GENERAL_PLUG_DIR_OUT, 0);
72 } while (err == -EAGAIN && ++trials < 3);
78 err = avc_general_get_sig_fmt(bebob->unit, &rx_rate,
79 AVC_GENERAL_PLUG_DIR_IN, 0);
80 } while (err == -EAGAIN && ++trials < 3);
85 if (rx_rate == tx_rate)
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);
96 snd_bebob_stream_set_rate(struct snd_bebob *bebob, unsigned int rate)
100 err = avc_general_set_sig_fmt(bebob->unit, rate,
101 AVC_GENERAL_PLUG_DIR_OUT, 0);
105 err = avc_general_set_sig_fmt(bebob->unit, rate,
106 AVC_GENERAL_PLUG_DIR_IN, 0);
111 * Some devices need a bit time for transition.
112 * 300msec is got by some experiments.
120 snd_bebob_stream_check_internal_clock(struct snd_bebob *bebob, bool *internal)
122 struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
123 u8 addr[AVC_BRIDGECO_ADDR_BYTES], input[7];
129 /* 1.The device has its own operation to switch source of clock */
131 err = clk_spec->get(bebob, &id);
133 dev_err(&bebob->unit->device,
134 "fail to get clock source: %d\n", err);
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);
146 if (strncmp(clk_spec->labels[id], SND_BEBOB_CLOCK_INTERNAL,
147 strlen(SND_BEBOB_CLOCK_INTERNAL)) == 0)
154 * 2.The device don't support to switch source of clock then assumed
155 * to use internal clock always
157 if (bebob->sync_input_plug < 0) {
163 * 3.The device supports to switch source of clock by an usual way.
164 * Let's check input for 'Music Sub Unit Sync Input' plug.
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);
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);
177 * If there are no input plugs, all of fields are 0xff.
178 * Here check the first field. This field is used for direction.
180 if (input[0] == 0xff) {
186 * If source of clock is internal CSR, Music Sub Unit Sync Input is
187 * a destination of Music Sub Unit Sync Output.
189 *internal = ((input[0] == AVC_BRIDGECO_PLUG_DIR_OUT) &&
190 (input[1] == AVC_BRIDGECO_PLUG_MODE_SUBUNIT) &&
191 (input[2] == 0x0c) &&
198 map_data_channels(struct snd_bebob *bebob, struct amdtp_stream *s)
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;
208 * The length of return value of this command cannot be expected. Here
209 * use the maximum length of FCP.
211 buf = kzalloc(256, GFP_KERNEL);
215 if (s == &bebob->tx_stream)
216 dir = AVC_BRIDGECO_PLUG_DIR_OUT;
218 dir = AVC_BRIDGECO_PLUG_DIR_IN;
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);
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",
231 /* positions in I/O buffer */
235 /* the number of sections in AMDTP packet */
236 sections = buf[pos++];
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,
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" :
258 /* the number of channels in this section */
259 channels = buf[pos++];
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;
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.
273 if (sec_loc >= channels)
277 /* for MIDI conformant data channel */
279 /* AMDTP_MAX_CHANNELS_FOR_MIDI is 1. */
280 if ((midi > 0) && (stm_pos != midi)) {
284 s->midi_position = stm_pos;
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 */
299 location = pcm + sec_loc;
300 if (location >= AMDTP_MAX_CHANNELS_FOR_PCM) {
304 s->pcm_positions[location] = stm_pos;
320 init_both_connections(struct snd_bebob *bebob)
324 err = cmp_connection_init(&bebob->in_conn,
325 bebob->unit, CMP_INPUT, 0);
329 err = cmp_connection_init(&bebob->out_conn,
330 bebob->unit, CMP_OUTPUT, 0);
332 cmp_connection_destroy(&bebob->in_conn);
338 check_connection_used_by_others(struct snd_bebob *bebob, struct amdtp_stream *s)
340 struct cmp_connection *conn;
344 if (s == &bebob->tx_stream)
345 conn = &bebob->out_conn;
347 conn = &bebob->in_conn;
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',
362 make_both_connections(struct snd_bebob *bebob, unsigned int rate)
364 int index, pcm_channels, midi_channels, err = 0;
366 if (bebob->connected)
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);
380 /* establish connections for both streams */
381 err = cmp_connection_establish(&bebob->out_conn,
382 amdtp_stream_get_max_payload(&bebob->tx_stream));
385 err = cmp_connection_establish(&bebob->in_conn,
386 amdtp_stream_get_max_payload(&bebob->rx_stream));
388 cmp_connection_break(&bebob->out_conn);
392 bebob->connected = true;
398 break_both_connections(struct snd_bebob *bebob)
400 cmp_connection_break(&bebob->in_conn);
401 cmp_connection_break(&bebob->out_conn);
403 bebob->connected = false;
405 /* These models seems to be in transition state for a longer time. */
406 if (bebob->maudio_special_quirk != NULL)
411 destroy_both_connections(struct snd_bebob *bebob)
413 cmp_connection_destroy(&bebob->in_conn);
414 cmp_connection_destroy(&bebob->out_conn);
418 get_sync_mode(struct snd_bebob *bebob, enum cip_flags *sync_mode)
420 /* currently this module doesn't support SYT-Match mode */
421 *sync_mode = CIP_SYNC_TO_DEVICE;
426 start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream,
429 struct cmp_connection *conn;
432 if (stream == &bebob->rx_stream)
433 conn = &bebob->in_conn;
435 conn = &bebob->out_conn;
437 /* channel mapping */
438 if (bebob->maudio_special_quirk == NULL) {
439 err = map_data_channels(bebob, stream);
444 /* start amdtp stream */
445 err = amdtp_stream_start(stream,
446 conn->resources.channel,
452 int snd_bebob_stream_init_duplex(struct snd_bebob *bebob)
456 err = init_both_connections(bebob);
460 err = amdtp_stream_init(&bebob->tx_stream, bebob->unit,
461 AMDTP_IN_STREAM, CIP_BLOCKING);
463 amdtp_stream_destroy(&bebob->tx_stream);
464 destroy_both_connections(bebob);
467 /* See comments in next function */
468 init_completion(&bebob->bus_reset);
469 bebob->tx_stream.flags |= CIP_SKIP_INIT_DBC_CHECK;
471 * At high sampling rate, M-Audio special firmware transmits empty
472 * packet with the value of dbc incremented by 8 but the others are
473 * valid to IEC 61883-1.
475 if (bebob->maudio_special_quirk)
476 bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC;
478 err = amdtp_stream_init(&bebob->rx_stream, bebob->unit,
479 AMDTP_OUT_STREAM, CIP_BLOCKING);
481 amdtp_stream_destroy(&bebob->tx_stream);
482 amdtp_stream_destroy(&bebob->rx_stream);
483 destroy_both_connections(bebob);
489 int snd_bebob_stream_start_duplex(struct snd_bebob *bebob, unsigned int rate)
491 struct snd_bebob_rate_spec *rate_spec = bebob->spec->rate;
492 struct amdtp_stream *master, *slave;
493 atomic_t *slave_substreams;
494 enum cip_flags sync_mode;
495 unsigned int curr_rate;
496 bool updated = false;
500 * Normal BeBoB firmware has a quirk at bus reset to transmits packets
501 * with discontinuous value in dbc field.
503 * This 'struct completion' is used to call .update() at first to update
504 * connections/streams. Next following codes handle streaming error.
506 if (amdtp_streaming_error(&bebob->tx_stream)) {
507 if (completion_done(&bebob->bus_reset))
508 reinit_completion(&bebob->bus_reset);
510 updated = (wait_for_completion_interruptible_timeout(
512 msecs_to_jiffies(FW_ISO_RESOURCE_DELAY)) > 0);
515 mutex_lock(&bebob->mutex);
517 /* Need no substreams */
518 if (atomic_read(&bebob->playback_substreams) == 0 &&
519 atomic_read(&bebob->capture_substreams) == 0)
522 err = get_sync_mode(bebob, &sync_mode);
525 if (sync_mode == CIP_SYNC_TO_DEVICE) {
526 master = &bebob->tx_stream;
527 slave = &bebob->rx_stream;
528 slave_substreams = &bebob->playback_substreams;
530 master = &bebob->rx_stream;
531 slave = &bebob->tx_stream;
532 slave_substreams = &bebob->capture_substreams;
536 * Considering JACK/FFADO streaming:
537 * TODO: This can be removed hwdep functionality becomes popular.
539 err = check_connection_used_by_others(bebob, master);
544 * packet queueing error or detecting discontinuity
546 * At bus reset, connections should not be broken here. So streams need
547 * to be re-started. This is a reason to use SKIP_INIT_DBC_CHECK flag.
549 if (amdtp_streaming_error(master))
550 amdtp_stream_stop(master);
551 if (amdtp_streaming_error(slave))
552 amdtp_stream_stop(slave);
554 !amdtp_stream_running(master) && !amdtp_stream_running(slave))
555 break_both_connections(bebob);
557 /* stop streams if rate is different */
558 err = rate_spec->get(bebob, &curr_rate);
560 dev_err(&bebob->unit->device,
561 "fail to get sampling rate: %d\n", err);
566 if (rate != curr_rate) {
567 amdtp_stream_stop(master);
568 amdtp_stream_stop(slave);
569 break_both_connections(bebob);
572 /* master should be always running */
573 if (!amdtp_stream_running(master)) {
574 amdtp_stream_set_sync(sync_mode, master, slave);
575 bebob->master = master;
579 * If establishing connections at first, Yamaha GO46
580 * (and maybe Terratec X24) don't generate sound.
582 * For firmware customized by M-Audio, refer to next NOTE.
584 if (bebob->maudio_special_quirk == NULL) {
585 err = rate_spec->set(bebob, rate);
587 dev_err(&bebob->unit->device,
588 "fail to set sampling rate: %d\n",
594 err = make_both_connections(bebob, rate);
598 err = start_stream(bebob, master, rate);
600 dev_err(&bebob->unit->device,
601 "fail to run AMDTP master stream:%d\n", err);
602 break_both_connections(bebob);
608 * The firmware customized by M-Audio uses these commands to
609 * start transmitting stream. This is not usual way.
611 if (bebob->maudio_special_quirk != NULL) {
612 err = rate_spec->set(bebob, rate);
614 dev_err(&bebob->unit->device,
615 "fail to ensure sampling rate: %d\n",
617 amdtp_stream_stop(master);
618 break_both_connections(bebob);
623 /* wait first callback */
624 if (!amdtp_stream_wait_callback(master, CALLBACK_TIMEOUT)) {
625 amdtp_stream_stop(master);
626 break_both_connections(bebob);
632 /* start slave if needed */
633 if (atomic_read(slave_substreams) > 0 && !amdtp_stream_running(slave)) {
634 err = start_stream(bebob, slave, rate);
636 dev_err(&bebob->unit->device,
637 "fail to run AMDTP slave stream:%d\n", err);
638 amdtp_stream_stop(master);
639 break_both_connections(bebob);
643 /* wait first callback */
644 if (!amdtp_stream_wait_callback(slave, CALLBACK_TIMEOUT)) {
645 amdtp_stream_stop(slave);
646 amdtp_stream_stop(master);
647 break_both_connections(bebob);
652 mutex_unlock(&bebob->mutex);
656 void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob)
658 struct amdtp_stream *master, *slave;
659 atomic_t *master_substreams, *slave_substreams;
661 if (bebob->master == &bebob->rx_stream) {
662 slave = &bebob->tx_stream;
663 master = &bebob->rx_stream;
664 slave_substreams = &bebob->capture_substreams;
665 master_substreams = &bebob->playback_substreams;
667 slave = &bebob->rx_stream;
668 master = &bebob->tx_stream;
669 slave_substreams = &bebob->playback_substreams;
670 master_substreams = &bebob->capture_substreams;
673 mutex_lock(&bebob->mutex);
675 if (atomic_read(slave_substreams) == 0) {
676 amdtp_stream_pcm_abort(slave);
677 amdtp_stream_stop(slave);
679 if (atomic_read(master_substreams) == 0) {
680 amdtp_stream_pcm_abort(master);
681 amdtp_stream_stop(master);
682 break_both_connections(bebob);
686 mutex_unlock(&bebob->mutex);
689 void snd_bebob_stream_update_duplex(struct snd_bebob *bebob)
691 /* vs. XRUN recovery due to discontinuity at bus reset */
692 mutex_lock(&bebob->mutex);
694 if ((cmp_connection_update(&bebob->in_conn) < 0) ||
695 (cmp_connection_update(&bebob->out_conn) < 0)) {
696 amdtp_stream_pcm_abort(&bebob->rx_stream);
697 amdtp_stream_pcm_abort(&bebob->tx_stream);
698 amdtp_stream_stop(&bebob->rx_stream);
699 amdtp_stream_stop(&bebob->tx_stream);
700 break_both_connections(bebob);
702 amdtp_stream_update(&bebob->rx_stream);
703 amdtp_stream_update(&bebob->tx_stream);
706 /* wake up stream_start_duplex() */
707 if (!completion_done(&bebob->bus_reset))
708 complete_all(&bebob->bus_reset);
710 mutex_unlock(&bebob->mutex);
714 * This function should be called before starting streams or after stopping
717 void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob)
719 amdtp_stream_destroy(&bebob->rx_stream);
720 amdtp_stream_destroy(&bebob->tx_stream);
722 destroy_both_connections(bebob);
726 * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’
727 * in Additional AVC commands (Nov 2003, BridgeCo)
728 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
731 parse_stream_formation(u8 *buf, unsigned int len,
732 struct snd_bebob_stream_formation *formation)
734 unsigned int i, e, channels, format;
737 * this module can support a hierarchy combination that:
738 * Root: Audio and Music (0x90)
739 * Level 1: AM824 Compound (0x40)
741 if ((buf[0] != 0x90) || (buf[1] != 0x40))
744 /* check sampling rate */
745 for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) {
746 if (buf[2] == bridgeco_freq_table[i])
749 if (i == ARRAY_SIZE(bridgeco_freq_table))
752 /* Avoid double count by different entries for the same rate. */
753 memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation));
755 for (e = 0; e < buf[4]; e++) {
756 channels = buf[5 + e * 2];
757 format = buf[6 + e * 2];
760 /* IEC 60958 Conformant, currently handled as MBLA */
762 /* Multi bit linear audio */
764 formation[i].pcm += channels;
766 /* MIDI Conformant */
768 formation[i].midi += channels;
770 /* IEC 61937-3 to 7 */
776 /* Multi bit linear audio */
777 case 0x07: /* DVD-Audio */
778 case 0x0c: /* High Precision */
780 case 0x08: /* (Plain) Raw */
781 case 0x09: /* (Plain) SACD */
782 case 0x0a: /* (Encoded) Raw */
783 case 0x0b: /* (Encoded) SACD */
784 /* Synchronization Stream (Stereo Raw audio) */
789 return -ENOSYS; /* not supported */
793 if (formation[i].pcm > AMDTP_MAX_CHANNELS_FOR_PCM ||
794 formation[i].midi > AMDTP_MAX_CHANNELS_FOR_MIDI)
801 fill_stream_formations(struct snd_bebob *bebob, enum avc_bridgeco_plug_dir dir,
805 struct snd_bebob_stream_formation *formations;
806 unsigned int len, eid;
807 u8 addr[AVC_BRIDGECO_ADDR_BYTES];
810 buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL);
814 if (dir == AVC_BRIDGECO_PLUG_DIR_IN)
815 formations = bebob->rx_stream_formations;
817 formations = bebob->tx_stream_formations;
819 for (eid = 0; eid < SND_BEBOB_STRM_FMT_ENTRIES; eid++) {
820 len = FORMAT_MAXIMUM_LENGTH;
821 avc_bridgeco_fill_unit_addr(addr, dir,
822 AVC_BRIDGECO_PLUG_UNIT_ISOC, pid);
823 err = avc_bridgeco_get_plug_strm_fmt(bebob->unit, addr, buf,
825 /* No entries remained. */
826 if (err == -EINVAL && eid > 0) {
829 } else if (err < 0) {
830 dev_err(&bebob->unit->device,
831 "fail to get stream format %d for isoc %s plug %d:%d\n",
833 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
839 err = parse_stream_formation(buf, len, formations);
849 seek_msu_sync_input_plug(struct snd_bebob *bebob)
851 u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
853 enum avc_bridgeco_plug_type type;
856 /* Get the number of Music Sub Unit for both direction. */
857 err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs);
859 dev_err(&bebob->unit->device,
860 "fail to get info for MSU in/out plugs: %d\n",
865 /* seek destination plugs for 'MSU sync input' */
866 bebob->sync_input_plug = -1;
867 for (i = 0; i < plugs[0]; i++) {
868 avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, i);
869 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
871 dev_err(&bebob->unit->device,
872 "fail to get type for MSU in plug %d: %d\n",
877 if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
878 bebob->sync_input_plug = i;
886 int snd_bebob_stream_discover(struct snd_bebob *bebob)
888 struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
889 u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
890 enum avc_bridgeco_plug_type type;
894 /* the number of plugs for isoc in/out, ext in/out */
895 err = avc_general_get_plug_info(bebob->unit, 0x1f, 0x07, 0x00, plugs);
897 dev_err(&bebob->unit->device,
898 "fail to get info for isoc/external in/out plugs: %d\n",
904 * This module supports at least one isoc input plug and one isoc
907 if ((plugs[0] == 0) || (plugs[1] == 0)) {
912 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
913 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
914 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
916 dev_err(&bebob->unit->device,
917 "fail to get type for isoc in plug 0: %d\n", err);
919 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
923 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_IN, 0);
927 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
928 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
929 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
931 dev_err(&bebob->unit->device,
932 "fail to get type for isoc out plug 0: %d\n", err);
934 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
938 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_OUT, 0);
942 /* count external input plugs for MIDI */
943 bebob->midi_input_ports = 0;
944 for (i = 0; i < plugs[2]; i++) {
945 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
946 AVC_BRIDGECO_PLUG_UNIT_EXT, i);
947 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
949 dev_err(&bebob->unit->device,
950 "fail to get type for external in plug %d: %d\n",
953 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
954 bebob->midi_input_ports++;
958 /* count external output plugs for MIDI */
959 bebob->midi_output_ports = 0;
960 for (i = 0; i < plugs[3]; i++) {
961 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
962 AVC_BRIDGECO_PLUG_UNIT_EXT, i);
963 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
965 dev_err(&bebob->unit->device,
966 "fail to get type for external out plug %d: %d\n",
969 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
970 bebob->midi_output_ports++;
974 /* for check source of clock later */
976 err = seek_msu_sync_input_plug(bebob);
981 void snd_bebob_stream_lock_changed(struct snd_bebob *bebob)
983 bebob->dev_lock_changed = true;
984 wake_up(&bebob->hwdep_wait);
987 int snd_bebob_stream_lock_try(struct snd_bebob *bebob)
991 spin_lock_irq(&bebob->lock);
993 /* user land lock this */
994 if (bebob->dev_lock_count < 0) {
999 /* this is the first time */
1000 if (bebob->dev_lock_count++ == 0)
1001 snd_bebob_stream_lock_changed(bebob);
1004 spin_unlock_irq(&bebob->lock);
1008 void snd_bebob_stream_lock_release(struct snd_bebob *bebob)
1010 spin_lock_irq(&bebob->lock);
1012 if (WARN_ON(bebob->dev_lock_count <= 0))
1014 if (--bebob->dev_lock_count == 0)
1015 snd_bebob_stream_lock_changed(bebob);
1017 spin_unlock_irq(&bebob->lock);