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
, 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.
122 snd_bebob_stream_check_internal_clock(struct snd_bebob
*bebob
, bool *internal
)
124 struct snd_bebob_clock_spec
*clk_spec
= bebob
->spec
->clock
;
125 u8 addr
[AVC_BRIDGECO_ADDR_BYTES
], input
[7];
131 /* 1.The device has its own operation to switch source of clock */
133 err
= clk_spec
->get(bebob
, &id
);
135 dev_err(&bebob
->unit
->device
,
136 "fail to get clock source: %d\n", err
);
140 if (id
>= clk_spec
->num
) {
141 dev_err(&bebob
->unit
->device
,
142 "clock source %d out of range 0..%d\n",
143 id
, clk_spec
->num
- 1);
148 if (strncmp(clk_spec
->labels
[id
], SND_BEBOB_CLOCK_INTERNAL
,
149 strlen(SND_BEBOB_CLOCK_INTERNAL
)) == 0)
156 * 2.The device don't support to switch source of clock then assumed
157 * to use internal clock always
159 if (bebob
->sync_input_plug
< 0) {
165 * 3.The device supports to switch source of clock by an usual way.
166 * Let's check input for 'Music Sub Unit Sync Input' plug.
168 avc_bridgeco_fill_msu_addr(addr
, AVC_BRIDGECO_PLUG_DIR_IN
,
169 bebob
->sync_input_plug
);
170 err
= avc_bridgeco_get_plug_input(bebob
->unit
, addr
, input
);
172 dev_err(&bebob
->unit
->device
,
173 "fail to get an input for MSU in plug %d: %d\n",
174 bebob
->sync_input_plug
, err
);
179 * If there are no input plugs, all of fields are 0xff.
180 * Here check the first field. This field is used for direction.
182 if (input
[0] == 0xff) {
188 * If source of clock is internal CSR, Music Sub Unit Sync Input is
189 * a destination of Music Sub Unit Sync Output.
191 *internal
= ((input
[0] == AVC_BRIDGECO_PLUG_DIR_OUT
) &&
192 (input
[1] == AVC_BRIDGECO_PLUG_MODE_SUBUNIT
) &&
193 (input
[2] == 0x0c) &&
200 map_data_channels(struct snd_bebob
*bebob
, struct amdtp_stream
*s
)
202 unsigned int sec
, sections
, ch
, channels
;
203 unsigned int pcm
, midi
, location
;
204 unsigned int stm_pos
, sec_loc
, pos
;
205 u8
*buf
, addr
[AVC_BRIDGECO_ADDR_BYTES
], type
;
206 enum avc_bridgeco_plug_dir dir
;
210 * The length of return value of this command cannot be expected. Here
211 * use the maximum length of FCP.
213 buf
= kzalloc(256, GFP_KERNEL
);
217 if (s
== &bebob
->tx_stream
)
218 dir
= AVC_BRIDGECO_PLUG_DIR_OUT
;
220 dir
= AVC_BRIDGECO_PLUG_DIR_IN
;
222 avc_bridgeco_fill_unit_addr(addr
, dir
, AVC_BRIDGECO_PLUG_UNIT_ISOC
, 0);
223 err
= avc_bridgeco_get_plug_ch_pos(bebob
->unit
, addr
, buf
, 256);
225 dev_err(&bebob
->unit
->device
,
226 "fail to get channel position for isoc %s plug 0: %d\n",
227 (dir
== AVC_BRIDGECO_PLUG_DIR_IN
) ? "in" : "out",
233 /* positions in I/O buffer */
237 /* the number of sections in AMDTP packet */
238 sections
= buf
[pos
++];
240 for (sec
= 0; sec
< sections
; sec
++) {
241 /* type of this section */
242 avc_bridgeco_fill_unit_addr(addr
, dir
,
243 AVC_BRIDGECO_PLUG_UNIT_ISOC
, 0);
244 err
= avc_bridgeco_get_plug_section_type(bebob
->unit
, addr
,
247 dev_err(&bebob
->unit
->device
,
248 "fail to get section type for isoc %s plug 0: %d\n",
249 (dir
== AVC_BRIDGECO_PLUG_DIR_IN
) ? "in" :
260 /* the number of channels in this section */
261 channels
= buf
[pos
++];
263 for (ch
= 0; ch
< channels
; ch
++) {
264 /* position of this channel in AMDTP packet */
265 stm_pos
= buf
[pos
++] - 1;
266 /* location of this channel in this section */
267 sec_loc
= buf
[pos
++] - 1;
270 * Basically the number of location is within the
271 * number of channels in this section. But some models
272 * of M-Audio don't follow this. Its location for MIDI
273 * is the position of MIDI channels in AMDTP packet.
275 if (sec_loc
>= channels
)
279 /* for MIDI conformant data channel */
281 /* AMDTP_MAX_CHANNELS_FOR_MIDI is 1. */
282 if ((midi
> 0) && (stm_pos
!= midi
)) {
286 s
->midi_position
= stm_pos
;
289 /* for PCM data channel */
290 case 0x01: /* Headphone */
291 case 0x02: /* Microphone */
292 case 0x03: /* Line */
293 case 0x04: /* SPDIF */
294 case 0x05: /* ADAT */
295 case 0x06: /* TDIF */
296 case 0x07: /* MADI */
297 /* for undefined/changeable signal */
298 case 0x08: /* Analog */
299 case 0x09: /* Digital */
301 location
= pcm
+ sec_loc
;
302 if (location
>= AMDTP_MAX_CHANNELS_FOR_PCM
) {
306 s
->pcm_positions
[location
] = stm_pos
;
322 init_both_connections(struct snd_bebob
*bebob
)
326 err
= cmp_connection_init(&bebob
->in_conn
,
327 bebob
->unit
, CMP_INPUT
, 0);
331 err
= cmp_connection_init(&bebob
->out_conn
,
332 bebob
->unit
, CMP_OUTPUT
, 0);
334 cmp_connection_destroy(&bebob
->in_conn
);
340 check_connection_used_by_others(struct snd_bebob
*bebob
, struct amdtp_stream
*s
)
342 struct cmp_connection
*conn
;
346 if (s
== &bebob
->tx_stream
)
347 conn
= &bebob
->out_conn
;
349 conn
= &bebob
->in_conn
;
351 err
= cmp_connection_check_used(conn
, &used
);
352 if ((err
>= 0) && used
&& !amdtp_stream_running(s
)) {
353 dev_err(&bebob
->unit
->device
,
354 "Connection established by others: %cPCR[%d]\n",
355 (conn
->direction
== CMP_OUTPUT
) ? 'o' : 'i',
364 make_both_connections(struct snd_bebob
*bebob
, unsigned int rate
)
366 int index
, pcm_channels
, midi_channels
, err
= 0;
368 if (bebob
->connected
)
371 /* confirm params for both streams */
372 err
= get_formation_index(rate
, &index
);
375 pcm_channels
= bebob
->tx_stream_formations
[index
].pcm
;
376 midi_channels
= bebob
->tx_stream_formations
[index
].midi
;
377 amdtp_stream_set_parameters(&bebob
->tx_stream
,
378 rate
, pcm_channels
, midi_channels
* 8);
379 pcm_channels
= bebob
->rx_stream_formations
[index
].pcm
;
380 midi_channels
= bebob
->rx_stream_formations
[index
].midi
;
381 amdtp_stream_set_parameters(&bebob
->rx_stream
,
382 rate
, pcm_channels
, midi_channels
* 8);
384 /* establish connections for both streams */
385 err
= cmp_connection_establish(&bebob
->out_conn
,
386 amdtp_stream_get_max_payload(&bebob
->tx_stream
));
389 err
= cmp_connection_establish(&bebob
->in_conn
,
390 amdtp_stream_get_max_payload(&bebob
->rx_stream
));
392 cmp_connection_break(&bebob
->out_conn
);
396 bebob
->connected
= true;
402 break_both_connections(struct snd_bebob
*bebob
)
404 cmp_connection_break(&bebob
->in_conn
);
405 cmp_connection_break(&bebob
->out_conn
);
407 bebob
->connected
= false;
409 /* These models seems to be in transition state for a longer time. */
410 if (bebob
->maudio_special_quirk
!= NULL
)
415 destroy_both_connections(struct snd_bebob
*bebob
)
417 break_both_connections(bebob
);
419 cmp_connection_destroy(&bebob
->in_conn
);
420 cmp_connection_destroy(&bebob
->out_conn
);
424 get_sync_mode(struct snd_bebob
*bebob
, enum cip_flags
*sync_mode
)
426 /* currently this module doesn't support SYT-Match mode */
427 *sync_mode
= CIP_SYNC_TO_DEVICE
;
432 start_stream(struct snd_bebob
*bebob
, struct amdtp_stream
*stream
,
435 struct cmp_connection
*conn
;
438 if (stream
== &bebob
->rx_stream
)
439 conn
= &bebob
->in_conn
;
441 conn
= &bebob
->out_conn
;
443 /* channel mapping */
444 if (bebob
->maudio_special_quirk
== NULL
) {
445 err
= map_data_channels(bebob
, stream
);
450 /* start amdtp stream */
451 err
= amdtp_stream_start(stream
,
452 conn
->resources
.channel
,
458 int snd_bebob_stream_init_duplex(struct snd_bebob
*bebob
)
462 err
= init_both_connections(bebob
);
466 err
= amdtp_stream_init(&bebob
->tx_stream
, bebob
->unit
,
467 AMDTP_IN_STREAM
, CIP_BLOCKING
);
469 amdtp_stream_destroy(&bebob
->tx_stream
);
470 destroy_both_connections(bebob
);
473 /* See comments in next function */
474 init_completion(&bebob
->bus_reset
);
475 bebob
->tx_stream
.flags
|= CIP_SKIP_INIT_DBC_CHECK
;
477 * At high sampling rate, M-Audio special firmware transmits empty
478 * packet with the value of dbc incremented by 8 but the others are
479 * valid to IEC 61883-1.
481 if (bebob
->maudio_special_quirk
)
482 bebob
->tx_stream
.flags
|= CIP_EMPTY_HAS_WRONG_DBC
;
484 err
= amdtp_stream_init(&bebob
->rx_stream
, bebob
->unit
,
485 AMDTP_OUT_STREAM
, CIP_BLOCKING
);
487 amdtp_stream_destroy(&bebob
->tx_stream
);
488 amdtp_stream_destroy(&bebob
->rx_stream
);
489 destroy_both_connections(bebob
);
492 * The firmware for these devices ignore MIDI messages in more than
493 * first 8 data blocks of an received AMDTP packet.
495 if (bebob
->spec
== &maudio_fw410_spec
||
496 bebob
->spec
== &maudio_special_spec
)
497 bebob
->rx_stream
.rx_blocks_for_midi
= 8;
502 int snd_bebob_stream_start_duplex(struct snd_bebob
*bebob
, unsigned int rate
)
504 struct snd_bebob_rate_spec
*rate_spec
= bebob
->spec
->rate
;
505 struct amdtp_stream
*master
, *slave
;
506 atomic_t
*slave_substreams
;
507 enum cip_flags sync_mode
;
508 unsigned int curr_rate
;
509 bool updated
= false;
513 * Normal BeBoB firmware has a quirk at bus reset to transmits packets
514 * with discontinuous value in dbc field.
516 * This 'struct completion' is used to call .update() at first to update
517 * connections/streams. Next following codes handle streaming error.
519 if (amdtp_streaming_error(&bebob
->tx_stream
)) {
520 if (completion_done(&bebob
->bus_reset
))
521 reinit_completion(&bebob
->bus_reset
);
523 updated
= (wait_for_completion_interruptible_timeout(
525 msecs_to_jiffies(FW_ISO_RESOURCE_DELAY
)) > 0);
528 mutex_lock(&bebob
->mutex
);
530 /* Need no substreams */
531 if (atomic_read(&bebob
->playback_substreams
) == 0 &&
532 atomic_read(&bebob
->capture_substreams
) == 0)
535 err
= get_sync_mode(bebob
, &sync_mode
);
538 if (sync_mode
== CIP_SYNC_TO_DEVICE
) {
539 master
= &bebob
->tx_stream
;
540 slave
= &bebob
->rx_stream
;
541 slave_substreams
= &bebob
->playback_substreams
;
543 master
= &bebob
->rx_stream
;
544 slave
= &bebob
->tx_stream
;
545 slave_substreams
= &bebob
->capture_substreams
;
549 * Considering JACK/FFADO streaming:
550 * TODO: This can be removed hwdep functionality becomes popular.
552 err
= check_connection_used_by_others(bebob
, master
);
557 * packet queueing error or detecting discontinuity
559 * At bus reset, connections should not be broken here. So streams need
560 * to be re-started. This is a reason to use SKIP_INIT_DBC_CHECK flag.
562 if (amdtp_streaming_error(master
))
563 amdtp_stream_stop(master
);
564 if (amdtp_streaming_error(slave
))
565 amdtp_stream_stop(slave
);
567 !amdtp_stream_running(master
) && !amdtp_stream_running(slave
))
568 break_both_connections(bebob
);
570 /* stop streams if rate is different */
571 err
= rate_spec
->get(bebob
, &curr_rate
);
573 dev_err(&bebob
->unit
->device
,
574 "fail to get sampling rate: %d\n", err
);
579 if (rate
!= curr_rate
) {
580 amdtp_stream_stop(master
);
581 amdtp_stream_stop(slave
);
582 break_both_connections(bebob
);
585 /* master should be always running */
586 if (!amdtp_stream_running(master
)) {
587 amdtp_stream_set_sync(sync_mode
, master
, slave
);
588 bebob
->master
= master
;
592 * If establishing connections at first, Yamaha GO46
593 * (and maybe Terratec X24) don't generate sound.
595 * For firmware customized by M-Audio, refer to next NOTE.
597 if (bebob
->maudio_special_quirk
== NULL
) {
598 err
= rate_spec
->set(bebob
, rate
);
600 dev_err(&bebob
->unit
->device
,
601 "fail to set sampling rate: %d\n",
607 err
= make_both_connections(bebob
, rate
);
611 err
= start_stream(bebob
, master
, rate
);
613 dev_err(&bebob
->unit
->device
,
614 "fail to run AMDTP master stream:%d\n", err
);
615 break_both_connections(bebob
);
621 * The firmware customized by M-Audio uses these commands to
622 * start transmitting stream. This is not usual way.
624 if (bebob
->maudio_special_quirk
!= NULL
) {
625 err
= rate_spec
->set(bebob
, rate
);
627 dev_err(&bebob
->unit
->device
,
628 "fail to ensure sampling rate: %d\n",
630 amdtp_stream_stop(master
);
631 break_both_connections(bebob
);
636 /* wait first callback */
637 if (!amdtp_stream_wait_callback(master
, CALLBACK_TIMEOUT
)) {
638 amdtp_stream_stop(master
);
639 break_both_connections(bebob
);
645 /* start slave if needed */
646 if (atomic_read(slave_substreams
) > 0 && !amdtp_stream_running(slave
)) {
647 err
= start_stream(bebob
, slave
, rate
);
649 dev_err(&bebob
->unit
->device
,
650 "fail to run AMDTP slave stream:%d\n", err
);
651 amdtp_stream_stop(master
);
652 break_both_connections(bebob
);
656 /* wait first callback */
657 if (!amdtp_stream_wait_callback(slave
, CALLBACK_TIMEOUT
)) {
658 amdtp_stream_stop(slave
);
659 amdtp_stream_stop(master
);
660 break_both_connections(bebob
);
665 mutex_unlock(&bebob
->mutex
);
669 void snd_bebob_stream_stop_duplex(struct snd_bebob
*bebob
)
671 struct amdtp_stream
*master
, *slave
;
672 atomic_t
*master_substreams
, *slave_substreams
;
674 if (bebob
->master
== &bebob
->rx_stream
) {
675 slave
= &bebob
->tx_stream
;
676 master
= &bebob
->rx_stream
;
677 slave_substreams
= &bebob
->capture_substreams
;
678 master_substreams
= &bebob
->playback_substreams
;
680 slave
= &bebob
->rx_stream
;
681 master
= &bebob
->tx_stream
;
682 slave_substreams
= &bebob
->playback_substreams
;
683 master_substreams
= &bebob
->capture_substreams
;
686 mutex_lock(&bebob
->mutex
);
688 if (atomic_read(slave_substreams
) == 0) {
689 amdtp_stream_pcm_abort(slave
);
690 amdtp_stream_stop(slave
);
692 if (atomic_read(master_substreams
) == 0) {
693 amdtp_stream_pcm_abort(master
);
694 amdtp_stream_stop(master
);
695 break_both_connections(bebob
);
699 mutex_unlock(&bebob
->mutex
);
702 void snd_bebob_stream_update_duplex(struct snd_bebob
*bebob
)
704 /* vs. XRUN recovery due to discontinuity at bus reset */
705 mutex_lock(&bebob
->mutex
);
707 if ((cmp_connection_update(&bebob
->in_conn
) < 0) ||
708 (cmp_connection_update(&bebob
->out_conn
) < 0)) {
709 amdtp_stream_pcm_abort(&bebob
->rx_stream
);
710 amdtp_stream_pcm_abort(&bebob
->tx_stream
);
711 amdtp_stream_stop(&bebob
->rx_stream
);
712 amdtp_stream_stop(&bebob
->tx_stream
);
713 break_both_connections(bebob
);
715 amdtp_stream_update(&bebob
->rx_stream
);
716 amdtp_stream_update(&bebob
->tx_stream
);
719 /* wake up stream_start_duplex() */
720 if (!completion_done(&bebob
->bus_reset
))
721 complete_all(&bebob
->bus_reset
);
723 mutex_unlock(&bebob
->mutex
);
726 void snd_bebob_stream_destroy_duplex(struct snd_bebob
*bebob
)
728 mutex_lock(&bebob
->mutex
);
730 amdtp_stream_pcm_abort(&bebob
->rx_stream
);
731 amdtp_stream_pcm_abort(&bebob
->tx_stream
);
733 amdtp_stream_stop(&bebob
->rx_stream
);
734 amdtp_stream_stop(&bebob
->tx_stream
);
736 amdtp_stream_destroy(&bebob
->rx_stream
);
737 amdtp_stream_destroy(&bebob
->tx_stream
);
739 destroy_both_connections(bebob
);
741 mutex_unlock(&bebob
->mutex
);
745 * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’
746 * in Additional AVC commands (Nov 2003, BridgeCo)
747 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
750 parse_stream_formation(u8
*buf
, unsigned int len
,
751 struct snd_bebob_stream_formation
*formation
)
753 unsigned int i
, e
, channels
, format
;
756 * this module can support a hierarchy combination that:
757 * Root: Audio and Music (0x90)
758 * Level 1: AM824 Compound (0x40)
760 if ((buf
[0] != 0x90) || (buf
[1] != 0x40))
763 /* check sampling rate */
764 for (i
= 0; i
< ARRAY_SIZE(bridgeco_freq_table
); i
++) {
765 if (buf
[2] == bridgeco_freq_table
[i
])
768 if (i
== ARRAY_SIZE(bridgeco_freq_table
))
771 /* Avoid double count by different entries for the same rate. */
772 memset(&formation
[i
], 0, sizeof(struct snd_bebob_stream_formation
));
774 for (e
= 0; e
< buf
[4]; e
++) {
775 channels
= buf
[5 + e
* 2];
776 format
= buf
[6 + e
* 2];
779 /* IEC 60958 Conformant, currently handled as MBLA */
781 /* Multi bit linear audio */
783 formation
[i
].pcm
+= channels
;
785 /* MIDI Conformant */
787 formation
[i
].midi
+= channels
;
789 /* IEC 61937-3 to 7 */
795 /* Multi bit linear audio */
796 case 0x07: /* DVD-Audio */
797 case 0x0c: /* High Precision */
799 case 0x08: /* (Plain) Raw */
800 case 0x09: /* (Plain) SACD */
801 case 0x0a: /* (Encoded) Raw */
802 case 0x0b: /* (Encoded) SACD */
803 /* Synchronization Stream (Stereo Raw audio) */
808 return -ENOSYS
; /* not supported */
812 if (formation
[i
].pcm
> AMDTP_MAX_CHANNELS_FOR_PCM
||
813 formation
[i
].midi
> AMDTP_MAX_CHANNELS_FOR_MIDI
)
820 fill_stream_formations(struct snd_bebob
*bebob
, enum avc_bridgeco_plug_dir dir
,
824 struct snd_bebob_stream_formation
*formations
;
825 unsigned int len
, eid
;
826 u8 addr
[AVC_BRIDGECO_ADDR_BYTES
];
829 buf
= kmalloc(FORMAT_MAXIMUM_LENGTH
, GFP_KERNEL
);
833 if (dir
== AVC_BRIDGECO_PLUG_DIR_IN
)
834 formations
= bebob
->rx_stream_formations
;
836 formations
= bebob
->tx_stream_formations
;
838 for (eid
= 0; eid
< SND_BEBOB_STRM_FMT_ENTRIES
; eid
++) {
839 len
= FORMAT_MAXIMUM_LENGTH
;
840 avc_bridgeco_fill_unit_addr(addr
, dir
,
841 AVC_BRIDGECO_PLUG_UNIT_ISOC
, pid
);
842 err
= avc_bridgeco_get_plug_strm_fmt(bebob
->unit
, addr
, buf
,
844 /* No entries remained. */
845 if (err
== -EINVAL
&& eid
> 0) {
848 } else if (err
< 0) {
849 dev_err(&bebob
->unit
->device
,
850 "fail to get stream format %d for isoc %s plug %d:%d\n",
852 (dir
== AVC_BRIDGECO_PLUG_DIR_IN
) ? "in" :
858 err
= parse_stream_formation(buf
, len
, formations
);
868 seek_msu_sync_input_plug(struct snd_bebob
*bebob
)
870 u8 plugs
[AVC_PLUG_INFO_BUF_BYTES
], addr
[AVC_BRIDGECO_ADDR_BYTES
];
872 enum avc_bridgeco_plug_type type
;
875 /* Get the number of Music Sub Unit for both direction. */
876 err
= avc_general_get_plug_info(bebob
->unit
, 0x0c, 0x00, 0x00, plugs
);
878 dev_err(&bebob
->unit
->device
,
879 "fail to get info for MSU in/out plugs: %d\n",
884 /* seek destination plugs for 'MSU sync input' */
885 bebob
->sync_input_plug
= -1;
886 for (i
= 0; i
< plugs
[0]; i
++) {
887 avc_bridgeco_fill_msu_addr(addr
, AVC_BRIDGECO_PLUG_DIR_IN
, i
);
888 err
= avc_bridgeco_get_plug_type(bebob
->unit
, addr
, &type
);
890 dev_err(&bebob
->unit
->device
,
891 "fail to get type for MSU in plug %d: %d\n",
896 if (type
== AVC_BRIDGECO_PLUG_TYPE_SYNC
) {
897 bebob
->sync_input_plug
= i
;
905 int snd_bebob_stream_discover(struct snd_bebob
*bebob
)
907 struct snd_bebob_clock_spec
*clk_spec
= bebob
->spec
->clock
;
908 u8 plugs
[AVC_PLUG_INFO_BUF_BYTES
], addr
[AVC_BRIDGECO_ADDR_BYTES
];
909 enum avc_bridgeco_plug_type type
;
913 /* the number of plugs for isoc in/out, ext in/out */
914 err
= avc_general_get_plug_info(bebob
->unit
, 0x1f, 0x07, 0x00, plugs
);
916 dev_err(&bebob
->unit
->device
,
917 "fail to get info for isoc/external in/out plugs: %d\n",
923 * This module supports at least one isoc input plug and one isoc
926 if ((plugs
[0] == 0) || (plugs
[1] == 0)) {
931 avc_bridgeco_fill_unit_addr(addr
, AVC_BRIDGECO_PLUG_DIR_IN
,
932 AVC_BRIDGECO_PLUG_UNIT_ISOC
, 0);
933 err
= avc_bridgeco_get_plug_type(bebob
->unit
, addr
, &type
);
935 dev_err(&bebob
->unit
->device
,
936 "fail to get type for isoc in plug 0: %d\n", err
);
938 } else if (type
!= AVC_BRIDGECO_PLUG_TYPE_ISOC
) {
942 err
= fill_stream_formations(bebob
, AVC_BRIDGECO_PLUG_DIR_IN
, 0);
946 avc_bridgeco_fill_unit_addr(addr
, AVC_BRIDGECO_PLUG_DIR_OUT
,
947 AVC_BRIDGECO_PLUG_UNIT_ISOC
, 0);
948 err
= avc_bridgeco_get_plug_type(bebob
->unit
, addr
, &type
);
950 dev_err(&bebob
->unit
->device
,
951 "fail to get type for isoc out plug 0: %d\n", err
);
953 } else if (type
!= AVC_BRIDGECO_PLUG_TYPE_ISOC
) {
957 err
= fill_stream_formations(bebob
, AVC_BRIDGECO_PLUG_DIR_OUT
, 0);
961 /* count external input plugs for MIDI */
962 bebob
->midi_input_ports
= 0;
963 for (i
= 0; i
< plugs
[2]; i
++) {
964 avc_bridgeco_fill_unit_addr(addr
, AVC_BRIDGECO_PLUG_DIR_IN
,
965 AVC_BRIDGECO_PLUG_UNIT_EXT
, i
);
966 err
= avc_bridgeco_get_plug_type(bebob
->unit
, addr
, &type
);
968 dev_err(&bebob
->unit
->device
,
969 "fail to get type for external in plug %d: %d\n",
972 } else if (type
== AVC_BRIDGECO_PLUG_TYPE_MIDI
) {
973 bebob
->midi_input_ports
++;
977 /* count external output plugs for MIDI */
978 bebob
->midi_output_ports
= 0;
979 for (i
= 0; i
< plugs
[3]; i
++) {
980 avc_bridgeco_fill_unit_addr(addr
, AVC_BRIDGECO_PLUG_DIR_OUT
,
981 AVC_BRIDGECO_PLUG_UNIT_EXT
, i
);
982 err
= avc_bridgeco_get_plug_type(bebob
->unit
, addr
, &type
);
984 dev_err(&bebob
->unit
->device
,
985 "fail to get type for external out plug %d: %d\n",
988 } else if (type
== AVC_BRIDGECO_PLUG_TYPE_MIDI
) {
989 bebob
->midi_output_ports
++;
993 /* for check source of clock later */
995 err
= seek_msu_sync_input_plug(bebob
);
1000 void snd_bebob_stream_lock_changed(struct snd_bebob
*bebob
)
1002 bebob
->dev_lock_changed
= true;
1003 wake_up(&bebob
->hwdep_wait
);
1006 int snd_bebob_stream_lock_try(struct snd_bebob
*bebob
)
1010 spin_lock_irq(&bebob
->lock
);
1012 /* user land lock this */
1013 if (bebob
->dev_lock_count
< 0) {
1018 /* this is the first time */
1019 if (bebob
->dev_lock_count
++ == 0)
1020 snd_bebob_stream_lock_changed(bebob
);
1023 spin_unlock_irq(&bebob
->lock
);
1027 void snd_bebob_stream_lock_release(struct snd_bebob
*bebob
)
1029 spin_lock_irq(&bebob
->lock
);
1031 if (WARN_ON(bebob
->dev_lock_count
<= 0))
1033 if (--bebob
->dev_lock_count
== 0)
1034 snd_bebob_stream_lock_changed(bebob
);
1036 spin_unlock_irq(&bebob
->lock
);