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)
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.
119 int snd_bebob_stream_get_clock_src(struct snd_bebob *bebob,
120 enum snd_bebob_clock_type *src)
122 struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
123 u8 addr[AVC_BRIDGECO_ADDR_BYTES], input[7];
125 enum avc_bridgeco_plug_type type;
128 /* 1.The device has its own operation to switch source of clock */
130 err = clk_spec->get(bebob, &id);
132 dev_err(&bebob->unit->device,
133 "fail to get clock source: %d\n", err);
137 if (id >= clk_spec->num) {
138 dev_err(&bebob->unit->device,
139 "clock source %d out of range 0..%d\n",
140 id, clk_spec->num - 1);
145 *src = clk_spec->types[id];
150 * 2.The device don't support to switch source of clock then assumed
151 * to use internal clock always
153 if (bebob->sync_input_plug < 0) {
154 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
159 * 3.The device supports to switch source of clock by an usual way.
160 * Let's check input for 'Music Sub Unit Sync Input' plug.
162 avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
163 bebob->sync_input_plug);
164 err = avc_bridgeco_get_plug_input(bebob->unit, addr, input);
166 dev_err(&bebob->unit->device,
167 "fail to get an input for MSU in plug %d: %d\n",
168 bebob->sync_input_plug, err);
173 * If there are no input plugs, all of fields are 0xff.
174 * Here check the first field. This field is used for direction.
176 if (input[0] == 0xff) {
177 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
181 /* The source from any output plugs is for one purpose only. */
182 if (input[0] == AVC_BRIDGECO_PLUG_DIR_OUT) {
184 * In BeBoB architecture, the source from music subunit may
185 * bypass from oPCR[0]. This means that this source gives
186 * synchronization to IEEE 1394 cycle start packet.
188 if (input[1] == AVC_BRIDGECO_PLUG_MODE_SUBUNIT &&
190 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
193 /* The source from any input units is for several purposes. */
194 } else if (input[1] == AVC_BRIDGECO_PLUG_MODE_UNIT) {
195 if (input[2] == AVC_BRIDGECO_PLUG_UNIT_ISOC) {
196 if (input[3] == 0x00) {
198 * This source comes from iPCR[0]. This means
199 * that presentation timestamp calculated by
200 * SYT series of the received packets. In
201 * short, this driver is the master of
204 *src = SND_BEBOB_CLOCK_TYPE_SYT;
208 * This source comes from iPCR[1-29]. This
209 * means that the synchronization stream is not
210 * the Audio/MIDI compound stream.
212 *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
215 } else if (input[2] == AVC_BRIDGECO_PLUG_UNIT_EXT) {
216 /* Check type of this plug. */
217 avc_bridgeco_fill_unit_addr(addr,
218 AVC_BRIDGECO_PLUG_DIR_IN,
219 AVC_BRIDGECO_PLUG_UNIT_EXT,
221 err = avc_bridgeco_get_plug_type(bebob->unit, addr,
226 if (type == AVC_BRIDGECO_PLUG_TYPE_DIG) {
228 * SPDIF/ADAT or sometimes (not always) word
231 *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
233 } else if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
234 /* Often word clock. */
235 *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
237 } else if (type == AVC_BRIDGECO_PLUG_TYPE_ADDITION) {
240 * Mostly, additional internal clock.
242 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
255 map_data_channels(struct snd_bebob *bebob, struct amdtp_stream *s)
257 unsigned int sec, sections, ch, channels;
258 unsigned int pcm, midi, location;
259 unsigned int stm_pos, sec_loc, pos;
260 u8 *buf, addr[AVC_BRIDGECO_ADDR_BYTES], type;
261 enum avc_bridgeco_plug_dir dir;
265 * The length of return value of this command cannot be expected. Here
266 * use the maximum length of FCP.
268 buf = kzalloc(256, GFP_KERNEL);
272 if (s == &bebob->tx_stream)
273 dir = AVC_BRIDGECO_PLUG_DIR_OUT;
275 dir = AVC_BRIDGECO_PLUG_DIR_IN;
277 avc_bridgeco_fill_unit_addr(addr, dir, AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
278 err = avc_bridgeco_get_plug_ch_pos(bebob->unit, addr, buf, 256);
280 dev_err(&bebob->unit->device,
281 "fail to get channel position for isoc %s plug 0: %d\n",
282 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" : "out",
288 /* positions in I/O buffer */
292 /* the number of sections in AMDTP packet */
293 sections = buf[pos++];
295 for (sec = 0; sec < sections; sec++) {
296 /* type of this section */
297 avc_bridgeco_fill_unit_addr(addr, dir,
298 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
299 err = avc_bridgeco_get_plug_section_type(bebob->unit, addr,
302 dev_err(&bebob->unit->device,
303 "fail to get section type for isoc %s plug 0: %d\n",
304 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
315 /* the number of channels in this section */
316 channels = buf[pos++];
318 for (ch = 0; ch < channels; ch++) {
319 /* position of this channel in AMDTP packet */
320 stm_pos = buf[pos++] - 1;
321 /* location of this channel in this section */
322 sec_loc = buf[pos++] - 1;
325 * Basically the number of location is within the
326 * number of channels in this section. But some models
327 * of M-Audio don't follow this. Its location for MIDI
328 * is the position of MIDI channels in AMDTP packet.
330 if (sec_loc >= channels)
334 /* for MIDI conformant data channel */
336 /* AMDTP_MAX_CHANNELS_FOR_MIDI is 1. */
337 if ((midi > 0) && (stm_pos != midi)) {
341 s->midi_position = stm_pos;
344 /* for PCM data channel */
345 case 0x01: /* Headphone */
346 case 0x02: /* Microphone */
347 case 0x03: /* Line */
348 case 0x04: /* SPDIF */
349 case 0x05: /* ADAT */
350 case 0x06: /* TDIF */
351 case 0x07: /* MADI */
352 /* for undefined/changeable signal */
353 case 0x08: /* Analog */
354 case 0x09: /* Digital */
356 location = pcm + sec_loc;
357 if (location >= AMDTP_MAX_CHANNELS_FOR_PCM) {
361 s->pcm_positions[location] = stm_pos;
377 init_both_connections(struct snd_bebob *bebob)
381 err = cmp_connection_init(&bebob->in_conn,
382 bebob->unit, CMP_INPUT, 0);
386 err = cmp_connection_init(&bebob->out_conn,
387 bebob->unit, CMP_OUTPUT, 0);
389 cmp_connection_destroy(&bebob->in_conn);
395 check_connection_used_by_others(struct snd_bebob *bebob, struct amdtp_stream *s)
397 struct cmp_connection *conn;
401 if (s == &bebob->tx_stream)
402 conn = &bebob->out_conn;
404 conn = &bebob->in_conn;
406 err = cmp_connection_check_used(conn, &used);
407 if ((err >= 0) && used && !amdtp_stream_running(s)) {
408 dev_err(&bebob->unit->device,
409 "Connection established by others: %cPCR[%d]\n",
410 (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
419 make_both_connections(struct snd_bebob *bebob, unsigned int rate)
421 int index, pcm_channels, midi_channels, err = 0;
423 if (bebob->connected)
426 /* confirm params for both streams */
427 index = get_formation_index(rate);
428 pcm_channels = bebob->tx_stream_formations[index].pcm;
429 midi_channels = bebob->tx_stream_formations[index].midi;
430 err = amdtp_am824_set_parameters(&bebob->tx_stream, rate,
431 pcm_channels, midi_channels * 8,
436 pcm_channels = bebob->rx_stream_formations[index].pcm;
437 midi_channels = bebob->rx_stream_formations[index].midi;
438 err = amdtp_am824_set_parameters(&bebob->rx_stream, rate,
439 pcm_channels, midi_channels * 8,
444 /* establish connections for both streams */
445 err = cmp_connection_establish(&bebob->out_conn,
446 amdtp_stream_get_max_payload(&bebob->tx_stream));
449 err = cmp_connection_establish(&bebob->in_conn,
450 amdtp_stream_get_max_payload(&bebob->rx_stream));
452 cmp_connection_break(&bebob->out_conn);
456 bebob->connected = true;
462 break_both_connections(struct snd_bebob *bebob)
464 cmp_connection_break(&bebob->in_conn);
465 cmp_connection_break(&bebob->out_conn);
467 bebob->connected = false;
469 /* These models seems to be in transition state for a longer time. */
470 if (bebob->maudio_special_quirk != NULL)
475 destroy_both_connections(struct snd_bebob *bebob)
477 cmp_connection_destroy(&bebob->in_conn);
478 cmp_connection_destroy(&bebob->out_conn);
482 get_sync_mode(struct snd_bebob *bebob, enum cip_flags *sync_mode)
484 enum snd_bebob_clock_type src;
487 err = snd_bebob_stream_get_clock_src(bebob, &src);
492 case SND_BEBOB_CLOCK_TYPE_INTERNAL:
493 case SND_BEBOB_CLOCK_TYPE_EXTERNAL:
494 *sync_mode = CIP_SYNC_TO_DEVICE;
497 case SND_BEBOB_CLOCK_TYPE_SYT:
506 start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream,
509 struct cmp_connection *conn;
512 if (stream == &bebob->rx_stream)
513 conn = &bebob->in_conn;
515 conn = &bebob->out_conn;
517 /* channel mapping */
518 if (bebob->maudio_special_quirk == NULL) {
519 err = map_data_channels(bebob, stream);
524 /* start amdtp stream */
525 err = amdtp_stream_start(stream,
526 conn->resources.channel,
532 int snd_bebob_stream_init_duplex(struct snd_bebob *bebob)
536 err = init_both_connections(bebob);
540 err = amdtp_am824_init(&bebob->tx_stream, bebob->unit,
541 AMDTP_IN_STREAM, CIP_BLOCKING);
543 amdtp_stream_destroy(&bebob->tx_stream);
544 destroy_both_connections(bebob);
547 /* See comments in next function */
548 init_completion(&bebob->bus_reset);
549 bebob->tx_stream.flags |= CIP_SKIP_INIT_DBC_CHECK;
552 * BeBoB v3 transfers packets with these qurks:
553 * - In the beginning of streaming, the value of dbc is incremented
554 * even if no data blocks are transferred.
555 * - The value of dbc is reset suddenly.
557 if (bebob->version > 2)
558 bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC |
559 CIP_SKIP_DBC_ZERO_CHECK;
562 * At high sampling rate, M-Audio special firmware transmits empty
563 * packet with the value of dbc incremented by 8 but the others are
564 * valid to IEC 61883-1.
566 if (bebob->maudio_special_quirk)
567 bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC;
569 err = amdtp_am824_init(&bebob->rx_stream, bebob->unit,
570 AMDTP_OUT_STREAM, CIP_BLOCKING);
572 amdtp_stream_destroy(&bebob->tx_stream);
573 amdtp_stream_destroy(&bebob->rx_stream);
574 destroy_both_connections(bebob);
580 int snd_bebob_stream_start_duplex(struct snd_bebob *bebob, unsigned int rate)
582 struct snd_bebob_rate_spec *rate_spec = bebob->spec->rate;
583 struct amdtp_stream *master, *slave;
584 enum cip_flags sync_mode;
585 unsigned int curr_rate;
586 bool updated = false;
590 * Normal BeBoB firmware has a quirk at bus reset to transmits packets
591 * with discontinuous value in dbc field.
593 * This 'struct completion' is used to call .update() at first to update
594 * connections/streams. Next following codes handle streaming error.
596 if (amdtp_streaming_error(&bebob->tx_stream)) {
597 if (completion_done(&bebob->bus_reset))
598 reinit_completion(&bebob->bus_reset);
600 updated = (wait_for_completion_interruptible_timeout(
602 msecs_to_jiffies(FW_ISO_RESOURCE_DELAY)) > 0);
605 mutex_lock(&bebob->mutex);
607 /* Need no substreams */
608 if (atomic_read(&bebob->substreams_counter) == 0)
611 err = get_sync_mode(bebob, &sync_mode);
614 if (sync_mode == CIP_SYNC_TO_DEVICE) {
615 master = &bebob->tx_stream;
616 slave = &bebob->rx_stream;
618 master = &bebob->rx_stream;
619 slave = &bebob->tx_stream;
623 * Considering JACK/FFADO streaming:
624 * TODO: This can be removed hwdep functionality becomes popular.
626 err = check_connection_used_by_others(bebob, master);
631 * packet queueing error or detecting discontinuity
633 * At bus reset, connections should not be broken here. So streams need
634 * to be re-started. This is a reason to use SKIP_INIT_DBC_CHECK flag.
636 if (amdtp_streaming_error(master))
637 amdtp_stream_stop(master);
638 if (amdtp_streaming_error(slave))
639 amdtp_stream_stop(slave);
641 !amdtp_stream_running(master) && !amdtp_stream_running(slave))
642 break_both_connections(bebob);
644 /* stop streams if rate is different */
645 err = rate_spec->get(bebob, &curr_rate);
647 dev_err(&bebob->unit->device,
648 "fail to get sampling rate: %d\n", err);
653 if (rate != curr_rate) {
654 amdtp_stream_stop(master);
655 amdtp_stream_stop(slave);
656 break_both_connections(bebob);
659 /* master should be always running */
660 if (!amdtp_stream_running(master)) {
661 amdtp_stream_set_sync(sync_mode, master, slave);
662 bebob->master = master;
666 * If establishing connections at first, Yamaha GO46
667 * (and maybe Terratec X24) don't generate sound.
669 * For firmware customized by M-Audio, refer to next NOTE.
671 if (bebob->maudio_special_quirk == NULL) {
672 err = rate_spec->set(bebob, rate);
674 dev_err(&bebob->unit->device,
675 "fail to set sampling rate: %d\n",
681 err = make_both_connections(bebob, rate);
685 err = start_stream(bebob, master, rate);
687 dev_err(&bebob->unit->device,
688 "fail to run AMDTP master stream:%d\n", err);
689 break_both_connections(bebob);
695 * The firmware customized by M-Audio uses these commands to
696 * start transmitting stream. This is not usual way.
698 if (bebob->maudio_special_quirk != NULL) {
699 err = rate_spec->set(bebob, rate);
701 dev_err(&bebob->unit->device,
702 "fail to ensure sampling rate: %d\n",
704 amdtp_stream_stop(master);
705 break_both_connections(bebob);
710 /* wait first callback */
711 if (!amdtp_stream_wait_callback(master, CALLBACK_TIMEOUT)) {
712 amdtp_stream_stop(master);
713 break_both_connections(bebob);
719 /* start slave if needed */
720 if (!amdtp_stream_running(slave)) {
721 err = start_stream(bebob, slave, rate);
723 dev_err(&bebob->unit->device,
724 "fail to run AMDTP slave stream:%d\n", err);
725 amdtp_stream_stop(master);
726 break_both_connections(bebob);
730 /* wait first callback */
731 if (!amdtp_stream_wait_callback(slave, CALLBACK_TIMEOUT)) {
732 amdtp_stream_stop(slave);
733 amdtp_stream_stop(master);
734 break_both_connections(bebob);
739 mutex_unlock(&bebob->mutex);
743 void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob)
745 struct amdtp_stream *master, *slave;
747 if (bebob->master == &bebob->rx_stream) {
748 slave = &bebob->tx_stream;
749 master = &bebob->rx_stream;
751 slave = &bebob->rx_stream;
752 master = &bebob->tx_stream;
755 mutex_lock(&bebob->mutex);
757 if (atomic_read(&bebob->substreams_counter) == 0) {
758 amdtp_stream_pcm_abort(master);
759 amdtp_stream_stop(master);
761 amdtp_stream_pcm_abort(slave);
762 amdtp_stream_stop(slave);
764 break_both_connections(bebob);
767 mutex_unlock(&bebob->mutex);
770 void snd_bebob_stream_update_duplex(struct snd_bebob *bebob)
772 /* vs. XRUN recovery due to discontinuity at bus reset */
773 mutex_lock(&bebob->mutex);
775 if ((cmp_connection_update(&bebob->in_conn) < 0) ||
776 (cmp_connection_update(&bebob->out_conn) < 0)) {
777 amdtp_stream_pcm_abort(&bebob->rx_stream);
778 amdtp_stream_pcm_abort(&bebob->tx_stream);
779 amdtp_stream_stop(&bebob->rx_stream);
780 amdtp_stream_stop(&bebob->tx_stream);
781 break_both_connections(bebob);
783 amdtp_stream_update(&bebob->rx_stream);
784 amdtp_stream_update(&bebob->tx_stream);
787 /* wake up stream_start_duplex() */
788 if (!completion_done(&bebob->bus_reset))
789 complete_all(&bebob->bus_reset);
791 mutex_unlock(&bebob->mutex);
795 * This function should be called before starting streams or after stopping
798 void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob)
800 amdtp_stream_destroy(&bebob->rx_stream);
801 amdtp_stream_destroy(&bebob->tx_stream);
803 destroy_both_connections(bebob);
807 * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’
808 * in Additional AVC commands (Nov 2003, BridgeCo)
809 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
812 parse_stream_formation(u8 *buf, unsigned int len,
813 struct snd_bebob_stream_formation *formation)
815 unsigned int i, e, channels, format;
818 * this module can support a hierarchy combination that:
819 * Root: Audio and Music (0x90)
820 * Level 1: AM824 Compound (0x40)
822 if ((buf[0] != 0x90) || (buf[1] != 0x40))
825 /* check sampling rate */
826 for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) {
827 if (buf[2] == bridgeco_freq_table[i])
830 if (i == ARRAY_SIZE(bridgeco_freq_table))
833 /* Avoid double count by different entries for the same rate. */
834 memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation));
836 for (e = 0; e < buf[4]; e++) {
837 channels = buf[5 + e * 2];
838 format = buf[6 + e * 2];
841 /* IEC 60958 Conformant, currently handled as MBLA */
843 /* Multi bit linear audio */
845 formation[i].pcm += channels;
847 /* MIDI Conformant */
849 formation[i].midi += channels;
851 /* IEC 61937-3 to 7 */
857 /* Multi bit linear audio */
858 case 0x07: /* DVD-Audio */
859 case 0x0c: /* High Precision */
861 case 0x08: /* (Plain) Raw */
862 case 0x09: /* (Plain) SACD */
863 case 0x0a: /* (Encoded) Raw */
864 case 0x0b: /* (Encoded) SACD */
865 /* Synchronization Stream (Stereo Raw audio) */
870 return -ENOSYS; /* not supported */
874 if (formation[i].pcm > AMDTP_MAX_CHANNELS_FOR_PCM ||
875 formation[i].midi > AMDTP_MAX_CHANNELS_FOR_MIDI)
882 fill_stream_formations(struct snd_bebob *bebob, enum avc_bridgeco_plug_dir dir,
886 struct snd_bebob_stream_formation *formations;
887 unsigned int len, eid;
888 u8 addr[AVC_BRIDGECO_ADDR_BYTES];
891 buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL);
895 if (dir == AVC_BRIDGECO_PLUG_DIR_IN)
896 formations = bebob->rx_stream_formations;
898 formations = bebob->tx_stream_formations;
900 for (eid = 0; eid < SND_BEBOB_STRM_FMT_ENTRIES; eid++) {
901 len = FORMAT_MAXIMUM_LENGTH;
902 avc_bridgeco_fill_unit_addr(addr, dir,
903 AVC_BRIDGECO_PLUG_UNIT_ISOC, pid);
904 err = avc_bridgeco_get_plug_strm_fmt(bebob->unit, addr, buf,
906 /* No entries remained. */
907 if (err == -EINVAL && eid > 0) {
910 } else if (err < 0) {
911 dev_err(&bebob->unit->device,
912 "fail to get stream format %d for isoc %s plug %d:%d\n",
914 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
920 err = parse_stream_formation(buf, len, formations);
930 seek_msu_sync_input_plug(struct snd_bebob *bebob)
932 u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
934 enum avc_bridgeco_plug_type type;
937 /* Get the number of Music Sub Unit for both direction. */
938 err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs);
940 dev_err(&bebob->unit->device,
941 "fail to get info for MSU in/out plugs: %d\n",
946 /* seek destination plugs for 'MSU sync input' */
947 bebob->sync_input_plug = -1;
948 for (i = 0; i < plugs[0]; i++) {
949 avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, i);
950 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
952 dev_err(&bebob->unit->device,
953 "fail to get type for MSU in plug %d: %d\n",
958 if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
959 bebob->sync_input_plug = i;
967 int snd_bebob_stream_discover(struct snd_bebob *bebob)
969 struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
970 u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
971 enum avc_bridgeco_plug_type type;
975 /* the number of plugs for isoc in/out, ext in/out */
976 err = avc_general_get_plug_info(bebob->unit, 0x1f, 0x07, 0x00, plugs);
978 dev_err(&bebob->unit->device,
979 "fail to get info for isoc/external in/out plugs: %d\n",
985 * This module supports at least one isoc input plug and one isoc
988 if ((plugs[0] == 0) || (plugs[1] == 0)) {
993 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
994 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
995 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
997 dev_err(&bebob->unit->device,
998 "fail to get type for isoc in plug 0: %d\n", err);
1000 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
1004 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_IN, 0);
1008 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
1009 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
1010 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
1012 dev_err(&bebob->unit->device,
1013 "fail to get type for isoc out plug 0: %d\n", err);
1015 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
1019 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_OUT, 0);
1023 /* count external input plugs for MIDI */
1024 bebob->midi_input_ports = 0;
1025 for (i = 0; i < plugs[2]; i++) {
1026 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
1027 AVC_BRIDGECO_PLUG_UNIT_EXT, i);
1028 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
1030 dev_err(&bebob->unit->device,
1031 "fail to get type for external in plug %d: %d\n",
1034 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
1035 bebob->midi_input_ports++;
1039 /* count external output plugs for MIDI */
1040 bebob->midi_output_ports = 0;
1041 for (i = 0; i < plugs[3]; i++) {
1042 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
1043 AVC_BRIDGECO_PLUG_UNIT_EXT, i);
1044 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
1046 dev_err(&bebob->unit->device,
1047 "fail to get type for external out plug %d: %d\n",
1050 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
1051 bebob->midi_output_ports++;
1055 /* for check source of clock later */
1057 err = seek_msu_sync_input_plug(bebob);
1062 void snd_bebob_stream_lock_changed(struct snd_bebob *bebob)
1064 bebob->dev_lock_changed = true;
1065 wake_up(&bebob->hwdep_wait);
1068 int snd_bebob_stream_lock_try(struct snd_bebob *bebob)
1072 spin_lock_irq(&bebob->lock);
1074 /* user land lock this */
1075 if (bebob->dev_lock_count < 0) {
1080 /* this is the first time */
1081 if (bebob->dev_lock_count++ == 0)
1082 snd_bebob_stream_lock_changed(bebob);
1085 spin_unlock_irq(&bebob->lock);
1089 void snd_bebob_stream_lock_release(struct snd_bebob *bebob)
1091 spin_lock_irq(&bebob->lock);
1093 if (WARN_ON(bebob->dev_lock_count <= 0))
1095 if (--bebob->dev_lock_count == 0)
1096 snd_bebob_stream_lock_changed(bebob);
1098 spin_unlock_irq(&bebob->lock);