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 2000
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, unsigned int *index)
55 for (i = 0; i < ARRAY_SIZE(snd_bebob_rate_table); i++) {
56 if (snd_bebob_rate_table[i] == rate) {
65 snd_bebob_stream_get_rate(struct snd_bebob *bebob, unsigned int *curr_rate)
67 unsigned int tx_rate, rx_rate, trials;
72 err = avc_general_get_sig_fmt(bebob->unit, &tx_rate,
73 AVC_GENERAL_PLUG_DIR_OUT, 0);
74 } while (err == -EAGAIN && ++trials < 3);
80 err = avc_general_get_sig_fmt(bebob->unit, &rx_rate,
81 AVC_GENERAL_PLUG_DIR_IN, 0);
82 } while (err == -EAGAIN && ++trials < 3);
87 if (rx_rate == tx_rate)
90 /* synchronize receive stream rate to transmit stream rate */
91 err = avc_general_set_sig_fmt(bebob->unit, rx_rate,
92 AVC_GENERAL_PLUG_DIR_IN, 0);
98 snd_bebob_stream_set_rate(struct snd_bebob *bebob, unsigned int rate)
102 err = avc_general_set_sig_fmt(bebob->unit, rate,
103 AVC_GENERAL_PLUG_DIR_OUT, 0);
107 err = avc_general_set_sig_fmt(bebob->unit, rate,
108 AVC_GENERAL_PLUG_DIR_IN, 0);
113 * Some devices need a bit time for transition.
114 * 300msec is got by some experiments.
121 int snd_bebob_stream_get_clock_src(struct snd_bebob *bebob,
122 enum snd_bebob_clock_type *src)
124 const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
125 u8 addr[AVC_BRIDGECO_ADDR_BYTES], input[7];
127 enum avc_bridgeco_plug_type type;
130 /* 1.The device has its own operation to switch source of clock */
132 err = clk_spec->get(bebob, &id);
134 dev_err(&bebob->unit->device,
135 "fail to get clock source: %d\n", err);
139 if (id >= clk_spec->num) {
140 dev_err(&bebob->unit->device,
141 "clock source %d out of range 0..%d\n",
142 id, clk_spec->num - 1);
147 *src = clk_spec->types[id];
152 * 2.The device don't support to switch source of clock then assumed
153 * to use internal clock always
155 if (bebob->sync_input_plug < 0) {
156 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
161 * 3.The device supports to switch source of clock by an usual way.
162 * Let's check input for 'Music Sub Unit Sync Input' plug.
164 avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
165 bebob->sync_input_plug);
166 err = avc_bridgeco_get_plug_input(bebob->unit, addr, input);
168 dev_err(&bebob->unit->device,
169 "fail to get an input for MSU in plug %d: %d\n",
170 bebob->sync_input_plug, err);
175 * If there are no input plugs, all of fields are 0xff.
176 * Here check the first field. This field is used for direction.
178 if (input[0] == 0xff) {
179 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
183 /* The source from any output plugs is for one purpose only. */
184 if (input[0] == AVC_BRIDGECO_PLUG_DIR_OUT) {
186 * In BeBoB architecture, the source from music subunit may
187 * bypass from oPCR[0]. This means that this source gives
188 * synchronization to IEEE 1394 cycle start packet.
190 if (input[1] == AVC_BRIDGECO_PLUG_MODE_SUBUNIT &&
192 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
195 /* The source from any input units is for several purposes. */
196 } else if (input[1] == AVC_BRIDGECO_PLUG_MODE_UNIT) {
197 if (input[2] == AVC_BRIDGECO_PLUG_UNIT_ISOC) {
198 if (input[3] == 0x00) {
200 * This source comes from iPCR[0]. This means
201 * that presentation timestamp calculated by
202 * SYT series of the received packets. In
203 * short, this driver is the master of
206 *src = SND_BEBOB_CLOCK_TYPE_SYT;
210 * This source comes from iPCR[1-29]. This
211 * means that the synchronization stream is not
212 * the Audio/MIDI compound stream.
214 *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
217 } else if (input[2] == AVC_BRIDGECO_PLUG_UNIT_EXT) {
218 /* Check type of this plug. */
219 avc_bridgeco_fill_unit_addr(addr,
220 AVC_BRIDGECO_PLUG_DIR_IN,
221 AVC_BRIDGECO_PLUG_UNIT_EXT,
223 err = avc_bridgeco_get_plug_type(bebob->unit, addr,
228 if (type == AVC_BRIDGECO_PLUG_TYPE_DIG) {
230 * SPDIF/ADAT or sometimes (not always) word
233 *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
235 } else if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
236 /* Often word clock. */
237 *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
239 } else if (type == AVC_BRIDGECO_PLUG_TYPE_ADDITION) {
242 * Mostly, additional internal clock.
244 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
256 static int map_data_channels(struct snd_bebob *bebob, struct amdtp_stream *s)
258 unsigned int sec, sections, ch, channels;
259 unsigned int pcm, midi, location;
260 unsigned int stm_pos, sec_loc, pos;
261 u8 *buf, addr[AVC_BRIDGECO_ADDR_BYTES], type;
262 enum avc_bridgeco_plug_dir dir;
266 * The length of return value of this command cannot be expected. Here
267 * use the maximum length of FCP.
269 buf = kzalloc(256, GFP_KERNEL);
273 if (s == &bebob->tx_stream)
274 dir = AVC_BRIDGECO_PLUG_DIR_OUT;
276 dir = AVC_BRIDGECO_PLUG_DIR_IN;
278 avc_bridgeco_fill_unit_addr(addr, dir, AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
279 err = avc_bridgeco_get_plug_ch_pos(bebob->unit, addr, buf, 256);
281 dev_err(&bebob->unit->device,
282 "fail to get channel position for isoc %s plug 0: %d\n",
283 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" : "out",
289 /* positions in I/O buffer */
293 /* the number of sections in AMDTP packet */
294 sections = buf[pos++];
296 for (sec = 0; sec < sections; sec++) {
297 /* type of this section */
298 avc_bridgeco_fill_unit_addr(addr, dir,
299 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
300 err = avc_bridgeco_get_plug_section_type(bebob->unit, addr,
303 dev_err(&bebob->unit->device,
304 "fail to get section type for isoc %s plug 0: %d\n",
305 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
316 /* the number of channels in this section */
317 channels = buf[pos++];
319 for (ch = 0; ch < channels; ch++) {
320 /* position of this channel in AMDTP packet */
321 stm_pos = buf[pos++] - 1;
322 /* location of this channel in this section */
323 sec_loc = buf[pos++] - 1;
326 * Basically the number of location is within the
327 * number of channels in this section. But some models
328 * of M-Audio don't follow this. Its location for MIDI
329 * is the position of MIDI channels in AMDTP packet.
331 if (sec_loc >= channels)
335 /* for MIDI conformant data channel */
337 /* AMDTP_MAX_CHANNELS_FOR_MIDI is 1. */
338 if ((midi > 0) && (stm_pos != midi)) {
342 amdtp_am824_set_midi_position(s, stm_pos);
345 /* for PCM data channel */
346 case 0x01: /* Headphone */
347 case 0x02: /* Microphone */
348 case 0x03: /* Line */
349 case 0x04: /* SPDIF */
350 case 0x05: /* ADAT */
351 case 0x06: /* TDIF */
352 case 0x07: /* MADI */
353 /* for undefined/changeable signal */
354 case 0x08: /* Analog */
355 case 0x09: /* Digital */
357 location = pcm + sec_loc;
358 if (location >= AM824_MAX_CHANNELS_FOR_PCM) {
362 amdtp_am824_set_pcm_position(s, location,
379 init_both_connections(struct snd_bebob *bebob)
383 err = cmp_connection_init(&bebob->in_conn,
384 bebob->unit, CMP_INPUT, 0);
388 err = cmp_connection_init(&bebob->out_conn,
389 bebob->unit, CMP_OUTPUT, 0);
391 cmp_connection_destroy(&bebob->in_conn);
397 check_connection_used_by_others(struct snd_bebob *bebob, struct amdtp_stream *s)
399 struct cmp_connection *conn;
403 if (s == &bebob->tx_stream)
404 conn = &bebob->out_conn;
406 conn = &bebob->in_conn;
408 err = cmp_connection_check_used(conn, &used);
409 if ((err >= 0) && used && !amdtp_stream_running(s)) {
410 dev_err(&bebob->unit->device,
411 "Connection established by others: %cPCR[%d]\n",
412 (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
421 make_both_connections(struct snd_bebob *bebob, unsigned int rate)
423 int index, pcm_channels, midi_channels, err = 0;
425 if (bebob->connected)
428 /* confirm params for both streams */
429 err = get_formation_index(rate, &index);
432 pcm_channels = bebob->tx_stream_formations[index].pcm;
433 midi_channels = bebob->tx_stream_formations[index].midi;
434 err = amdtp_am824_set_parameters(&bebob->tx_stream, rate,
435 pcm_channels, midi_channels * 8,
440 pcm_channels = bebob->rx_stream_formations[index].pcm;
441 midi_channels = bebob->rx_stream_formations[index].midi;
442 err = amdtp_am824_set_parameters(&bebob->rx_stream, rate,
443 pcm_channels, midi_channels * 8,
448 /* establish connections for both streams */
449 err = cmp_connection_establish(&bebob->out_conn,
450 amdtp_stream_get_max_payload(&bebob->tx_stream));
453 err = cmp_connection_establish(&bebob->in_conn,
454 amdtp_stream_get_max_payload(&bebob->rx_stream));
456 cmp_connection_break(&bebob->out_conn);
460 bebob->connected = true;
466 break_both_connections(struct snd_bebob *bebob)
468 cmp_connection_break(&bebob->in_conn);
469 cmp_connection_break(&bebob->out_conn);
471 bebob->connected = false;
473 /* These models seems to be in transition state for a longer time. */
474 if (bebob->maudio_special_quirk != NULL)
479 destroy_both_connections(struct snd_bebob *bebob)
481 cmp_connection_destroy(&bebob->in_conn);
482 cmp_connection_destroy(&bebob->out_conn);
486 get_sync_mode(struct snd_bebob *bebob, enum cip_flags *sync_mode)
488 enum snd_bebob_clock_type src;
491 err = snd_bebob_stream_get_clock_src(bebob, &src);
496 case SND_BEBOB_CLOCK_TYPE_INTERNAL:
497 case SND_BEBOB_CLOCK_TYPE_EXTERNAL:
498 *sync_mode = CIP_SYNC_TO_DEVICE;
501 case SND_BEBOB_CLOCK_TYPE_SYT:
510 start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream,
513 struct cmp_connection *conn;
516 if (stream == &bebob->rx_stream)
517 conn = &bebob->in_conn;
519 conn = &bebob->out_conn;
521 /* channel mapping */
522 if (bebob->maudio_special_quirk == NULL) {
523 err = map_data_channels(bebob, stream);
528 /* start amdtp stream */
529 err = amdtp_stream_start(stream,
530 conn->resources.channel,
536 int snd_bebob_stream_init_duplex(struct snd_bebob *bebob)
540 err = init_both_connections(bebob);
544 err = amdtp_am824_init(&bebob->tx_stream, bebob->unit,
545 AMDTP_IN_STREAM, CIP_BLOCKING);
547 amdtp_stream_destroy(&bebob->tx_stream);
548 destroy_both_connections(bebob);
551 /* See comments in next function */
552 init_completion(&bebob->bus_reset);
553 bebob->tx_stream.flags |= CIP_SKIP_INIT_DBC_CHECK;
556 * BeBoB v3 transfers packets with these qurks:
557 * - In the beginning of streaming, the value of dbc is incremented
558 * even if no data blocks are transferred.
559 * - The value of dbc is reset suddenly.
561 if (bebob->version > 2)
562 bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC |
563 CIP_SKIP_DBC_ZERO_CHECK;
566 * At high sampling rate, M-Audio special firmware transmits empty
567 * packet with the value of dbc incremented by 8 but the others are
568 * valid to IEC 61883-1.
570 if (bebob->maudio_special_quirk)
571 bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC;
573 err = amdtp_am824_init(&bebob->rx_stream, bebob->unit,
574 AMDTP_OUT_STREAM, CIP_BLOCKING);
576 amdtp_stream_destroy(&bebob->tx_stream);
577 amdtp_stream_destroy(&bebob->rx_stream);
578 destroy_both_connections(bebob);
584 int snd_bebob_stream_start_duplex(struct snd_bebob *bebob, unsigned int rate)
586 const struct snd_bebob_rate_spec *rate_spec = bebob->spec->rate;
587 struct amdtp_stream *master, *slave;
588 enum cip_flags sync_mode;
589 unsigned int curr_rate;
590 bool updated = false;
594 * Normal BeBoB firmware has a quirk at bus reset to transmits packets
595 * with discontinuous value in dbc field.
597 * This 'struct completion' is used to call .update() at first to update
598 * connections/streams. Next following codes handle streaming error.
600 if (amdtp_streaming_error(&bebob->tx_stream)) {
601 if (completion_done(&bebob->bus_reset))
602 reinit_completion(&bebob->bus_reset);
604 updated = (wait_for_completion_interruptible_timeout(
606 msecs_to_jiffies(FW_ISO_RESOURCE_DELAY)) > 0);
609 mutex_lock(&bebob->mutex);
611 /* Need no substreams */
612 if (atomic_read(&bebob->substreams_counter) == 0)
615 err = get_sync_mode(bebob, &sync_mode);
618 if (sync_mode == CIP_SYNC_TO_DEVICE) {
619 master = &bebob->tx_stream;
620 slave = &bebob->rx_stream;
622 master = &bebob->rx_stream;
623 slave = &bebob->tx_stream;
627 * Considering JACK/FFADO streaming:
628 * TODO: This can be removed hwdep functionality becomes popular.
630 err = check_connection_used_by_others(bebob, master);
635 * packet queueing error or detecting discontinuity
637 * At bus reset, connections should not be broken here. So streams need
638 * to be re-started. This is a reason to use SKIP_INIT_DBC_CHECK flag.
640 if (amdtp_streaming_error(master))
641 amdtp_stream_stop(master);
642 if (amdtp_streaming_error(slave))
643 amdtp_stream_stop(slave);
645 !amdtp_stream_running(master) && !amdtp_stream_running(slave))
646 break_both_connections(bebob);
648 /* stop streams if rate is different */
649 err = rate_spec->get(bebob, &curr_rate);
651 dev_err(&bebob->unit->device,
652 "fail to get sampling rate: %d\n", err);
657 if (rate != curr_rate) {
658 amdtp_stream_stop(master);
659 amdtp_stream_stop(slave);
660 break_both_connections(bebob);
663 /* master should be always running */
664 if (!amdtp_stream_running(master)) {
665 amdtp_stream_set_sync(sync_mode, master, slave);
666 bebob->master = master;
670 * If establishing connections at first, Yamaha GO46
671 * (and maybe Terratec X24) don't generate sound.
673 * For firmware customized by M-Audio, refer to next NOTE.
675 if (bebob->maudio_special_quirk == NULL) {
676 err = rate_spec->set(bebob, rate);
678 dev_err(&bebob->unit->device,
679 "fail to set sampling rate: %d\n",
685 err = make_both_connections(bebob, rate);
689 err = start_stream(bebob, master, rate);
691 dev_err(&bebob->unit->device,
692 "fail to run AMDTP master stream:%d\n", err);
693 break_both_connections(bebob);
699 * The firmware customized by M-Audio uses these commands to
700 * start transmitting stream. This is not usual way.
702 if (bebob->maudio_special_quirk != NULL) {
703 err = rate_spec->set(bebob, rate);
705 dev_err(&bebob->unit->device,
706 "fail to ensure sampling rate: %d\n",
708 amdtp_stream_stop(master);
709 break_both_connections(bebob);
714 /* wait first callback */
715 if (!amdtp_stream_wait_callback(master, CALLBACK_TIMEOUT)) {
716 amdtp_stream_stop(master);
717 break_both_connections(bebob);
723 /* start slave if needed */
724 if (!amdtp_stream_running(slave)) {
725 err = start_stream(bebob, slave, rate);
727 dev_err(&bebob->unit->device,
728 "fail to run AMDTP slave stream:%d\n", err);
729 amdtp_stream_stop(master);
730 break_both_connections(bebob);
734 /* wait first callback */
735 if (!amdtp_stream_wait_callback(slave, CALLBACK_TIMEOUT)) {
736 amdtp_stream_stop(slave);
737 amdtp_stream_stop(master);
738 break_both_connections(bebob);
743 mutex_unlock(&bebob->mutex);
747 void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob)
749 struct amdtp_stream *master, *slave;
751 if (bebob->master == &bebob->rx_stream) {
752 slave = &bebob->tx_stream;
753 master = &bebob->rx_stream;
755 slave = &bebob->rx_stream;
756 master = &bebob->tx_stream;
759 mutex_lock(&bebob->mutex);
761 if (atomic_read(&bebob->substreams_counter) == 0) {
762 amdtp_stream_pcm_abort(master);
763 amdtp_stream_stop(master);
765 amdtp_stream_pcm_abort(slave);
766 amdtp_stream_stop(slave);
768 break_both_connections(bebob);
771 mutex_unlock(&bebob->mutex);
774 void snd_bebob_stream_update_duplex(struct snd_bebob *bebob)
776 /* vs. XRUN recovery due to discontinuity at bus reset */
777 mutex_lock(&bebob->mutex);
779 if ((cmp_connection_update(&bebob->in_conn) < 0) ||
780 (cmp_connection_update(&bebob->out_conn) < 0)) {
781 amdtp_stream_pcm_abort(&bebob->rx_stream);
782 amdtp_stream_pcm_abort(&bebob->tx_stream);
783 amdtp_stream_stop(&bebob->rx_stream);
784 amdtp_stream_stop(&bebob->tx_stream);
785 break_both_connections(bebob);
787 amdtp_stream_update(&bebob->rx_stream);
788 amdtp_stream_update(&bebob->tx_stream);
791 /* wake up stream_start_duplex() */
792 if (!completion_done(&bebob->bus_reset))
793 complete_all(&bebob->bus_reset);
795 mutex_unlock(&bebob->mutex);
799 * This function should be called before starting streams or after stopping
802 void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob)
804 amdtp_stream_destroy(&bebob->rx_stream);
805 amdtp_stream_destroy(&bebob->tx_stream);
807 destroy_both_connections(bebob);
811 * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’
812 * in Additional AVC commands (Nov 2003, BridgeCo)
813 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
816 parse_stream_formation(u8 *buf, unsigned int len,
817 struct snd_bebob_stream_formation *formation)
819 unsigned int i, e, channels, format;
822 * this module can support a hierarchy combination that:
823 * Root: Audio and Music (0x90)
824 * Level 1: AM824 Compound (0x40)
826 if ((buf[0] != 0x90) || (buf[1] != 0x40))
829 /* check sampling rate */
830 for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) {
831 if (buf[2] == bridgeco_freq_table[i])
834 if (i == ARRAY_SIZE(bridgeco_freq_table))
837 /* Avoid double count by different entries for the same rate. */
838 memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation));
840 for (e = 0; e < buf[4]; e++) {
841 channels = buf[5 + e * 2];
842 format = buf[6 + e * 2];
845 /* IEC 60958 Conformant, currently handled as MBLA */
847 /* Multi bit linear audio */
849 formation[i].pcm += channels;
851 /* MIDI Conformant */
853 formation[i].midi += channels;
855 /* IEC 61937-3 to 7 */
861 /* Multi bit linear audio */
862 case 0x07: /* DVD-Audio */
863 case 0x0c: /* High Precision */
865 case 0x08: /* (Plain) Raw */
866 case 0x09: /* (Plain) SACD */
867 case 0x0a: /* (Encoded) Raw */
868 case 0x0b: /* (Encoded) SACD */
869 /* Synchronization Stream (Stereo Raw audio) */
874 return -ENOSYS; /* not supported */
878 if (formation[i].pcm > AM824_MAX_CHANNELS_FOR_PCM ||
879 formation[i].midi > AM824_MAX_CHANNELS_FOR_MIDI)
886 fill_stream_formations(struct snd_bebob *bebob, enum avc_bridgeco_plug_dir dir,
890 struct snd_bebob_stream_formation *formations;
891 unsigned int len, eid;
892 u8 addr[AVC_BRIDGECO_ADDR_BYTES];
895 buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL);
899 if (dir == AVC_BRIDGECO_PLUG_DIR_IN)
900 formations = bebob->rx_stream_formations;
902 formations = bebob->tx_stream_formations;
904 for (eid = 0; eid < SND_BEBOB_STRM_FMT_ENTRIES; eid++) {
905 len = FORMAT_MAXIMUM_LENGTH;
906 avc_bridgeco_fill_unit_addr(addr, dir,
907 AVC_BRIDGECO_PLUG_UNIT_ISOC, pid);
908 err = avc_bridgeco_get_plug_strm_fmt(bebob->unit, addr, buf,
910 /* No entries remained. */
911 if (err == -EINVAL && eid > 0) {
914 } else if (err < 0) {
915 dev_err(&bebob->unit->device,
916 "fail to get stream format %d for isoc %s plug %d:%d\n",
918 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
924 err = parse_stream_formation(buf, len, formations);
934 seek_msu_sync_input_plug(struct snd_bebob *bebob)
936 u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
938 enum avc_bridgeco_plug_type type;
941 /* Get the number of Music Sub Unit for both direction. */
942 err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs);
944 dev_err(&bebob->unit->device,
945 "fail to get info for MSU in/out plugs: %d\n",
950 /* seek destination plugs for 'MSU sync input' */
951 bebob->sync_input_plug = -1;
952 for (i = 0; i < plugs[0]; i++) {
953 avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, i);
954 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
956 dev_err(&bebob->unit->device,
957 "fail to get type for MSU in plug %d: %d\n",
962 if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
963 bebob->sync_input_plug = i;
971 int snd_bebob_stream_discover(struct snd_bebob *bebob)
973 const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
974 u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
975 enum avc_bridgeco_plug_type type;
979 /* the number of plugs for isoc in/out, ext in/out */
980 err = avc_general_get_plug_info(bebob->unit, 0x1f, 0x07, 0x00, plugs);
982 dev_err(&bebob->unit->device,
983 "fail to get info for isoc/external in/out plugs: %d\n",
989 * This module supports at least one isoc input plug and one isoc
992 if ((plugs[0] == 0) || (plugs[1] == 0)) {
997 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
998 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
999 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
1001 dev_err(&bebob->unit->device,
1002 "fail to get type for isoc in plug 0: %d\n", err);
1004 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
1008 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_IN, 0);
1012 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
1013 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
1014 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
1016 dev_err(&bebob->unit->device,
1017 "fail to get type for isoc out plug 0: %d\n", err);
1019 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
1023 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_OUT, 0);
1027 /* count external input plugs for MIDI */
1028 bebob->midi_input_ports = 0;
1029 for (i = 0; i < plugs[2]; i++) {
1030 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
1031 AVC_BRIDGECO_PLUG_UNIT_EXT, i);
1032 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
1034 dev_err(&bebob->unit->device,
1035 "fail to get type for external in plug %d: %d\n",
1038 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
1039 bebob->midi_input_ports++;
1043 /* count external output plugs for MIDI */
1044 bebob->midi_output_ports = 0;
1045 for (i = 0; i < plugs[3]; i++) {
1046 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
1047 AVC_BRIDGECO_PLUG_UNIT_EXT, i);
1048 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
1050 dev_err(&bebob->unit->device,
1051 "fail to get type for external out plug %d: %d\n",
1054 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
1055 bebob->midi_output_ports++;
1059 /* for check source of clock later */
1061 err = seek_msu_sync_input_plug(bebob);
1066 void snd_bebob_stream_lock_changed(struct snd_bebob *bebob)
1068 bebob->dev_lock_changed = true;
1069 wake_up(&bebob->hwdep_wait);
1072 int snd_bebob_stream_lock_try(struct snd_bebob *bebob)
1076 spin_lock_irq(&bebob->lock);
1078 /* user land lock this */
1079 if (bebob->dev_lock_count < 0) {
1084 /* this is the first time */
1085 if (bebob->dev_lock_count++ == 0)
1086 snd_bebob_stream_lock_changed(bebob);
1089 spin_unlock_irq(&bebob->lock);
1093 void snd_bebob_stream_lock_release(struct snd_bebob *bebob)
1095 spin_lock_irq(&bebob->lock);
1097 if (WARN_ON(bebob->dev_lock_count <= 0))
1099 if (--bebob->dev_lock_count == 0)
1100 snd_bebob_stream_lock_changed(bebob);
1102 spin_unlock_irq(&bebob->lock);