KEYS: add missing permission check for request_key() destination
[linux/fpc-iii.git] / sound / firewire / bebob / bebob_stream.c
blobc05b0f6c1bc2b9399ab77ba65de9d571b195614e
1 /*
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.
7 */
9 #include "./bebob.h"
11 #define CALLBACK_TIMEOUT 1000
12 #define FW_ISO_RESOURCE_DELAY 1000
15 * NOTE;
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] = {
27 [0] = 32000,
28 [1] = 44100,
29 [2] = 48000,
30 [3] = 88200,
31 [4] = 96000,
32 [5] = 176400,
33 [6] = 192000,
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[] = {
41 [0] = 0x02,
42 [1] = 0x03,
43 [2] = 0x04,
44 [3] = 0x0a,
45 [4] = 0x05,
46 [5] = 0x06,
47 [6] = 0x07,
50 static int
51 get_formation_index(unsigned int rate, unsigned int *index)
53 unsigned int i;
55 for (i = 0; i < ARRAY_SIZE(snd_bebob_rate_table); i++) {
56 if (snd_bebob_rate_table[i] == rate) {
57 *index = i;
58 return 0;
61 return -EINVAL;
64 int
65 snd_bebob_stream_get_rate(struct snd_bebob *bebob, unsigned int *curr_rate)
67 unsigned int tx_rate, rx_rate, trials;
68 int err;
70 trials = 0;
71 do {
72 err = avc_general_get_sig_fmt(bebob->unit, &tx_rate,
73 AVC_GENERAL_PLUG_DIR_OUT, 0);
74 } while (err == -EAGAIN && ++trials < 3);
75 if (err < 0)
76 goto end;
78 trials = 0;
79 do {
80 err = avc_general_get_sig_fmt(bebob->unit, &rx_rate,
81 AVC_GENERAL_PLUG_DIR_IN, 0);
82 } while (err == -EAGAIN && ++trials < 3);
83 if (err < 0)
84 goto end;
86 *curr_rate = rx_rate;
87 if (rx_rate == tx_rate)
88 goto end;
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);
93 end:
94 return err;
97 int
98 snd_bebob_stream_set_rate(struct snd_bebob *bebob, unsigned int rate)
100 int err;
102 err = avc_general_set_sig_fmt(bebob->unit, rate,
103 AVC_GENERAL_PLUG_DIR_OUT, 0);
104 if (err < 0)
105 goto end;
107 err = avc_general_set_sig_fmt(bebob->unit, rate,
108 AVC_GENERAL_PLUG_DIR_IN, 0);
109 if (err < 0)
110 goto end;
113 * Some devices need a bit time for transition.
114 * 300msec is got by some experiments.
116 msleep(300);
117 end:
118 return err;
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];
126 unsigned int id;
127 int err = 0;
129 *internal = false;
131 /* 1.The device has its own operation to switch source of clock */
132 if (clk_spec) {
133 err = clk_spec->get(bebob, &id);
134 if (err < 0) {
135 dev_err(&bebob->unit->device,
136 "fail to get clock source: %d\n", err);
137 goto end;
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);
144 err = -EIO;
145 goto end;
148 if (strncmp(clk_spec->labels[id], SND_BEBOB_CLOCK_INTERNAL,
149 strlen(SND_BEBOB_CLOCK_INTERNAL)) == 0)
150 *internal = true;
152 goto end;
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) {
160 *internal = true;
161 goto end;
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);
171 if (err < 0) {
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);
175 goto end;
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) {
183 *internal = true;
184 goto end;
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) &&
194 (input[3] == 0x00));
195 end:
196 return err;
199 static unsigned int
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;
207 int err;
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);
214 if (buf == NULL)
215 return -ENOMEM;
217 if (s == &bebob->tx_stream)
218 dir = AVC_BRIDGECO_PLUG_DIR_OUT;
219 else
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);
224 if (err < 0) {
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",
228 err);
229 goto end;
231 pos = 0;
233 /* positions in I/O buffer */
234 pcm = 0;
235 midi = 0;
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,
245 sec, &type);
246 if (err < 0) {
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" :
250 "out",
251 err);
252 goto end;
254 /* NoType */
255 if (type == 0xff) {
256 err = -ENOSYS;
257 goto end;
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)
276 sec_loc = ch;
278 switch (type) {
279 /* for MIDI conformant data channel */
280 case 0x0a:
281 /* AMDTP_MAX_CHANNELS_FOR_MIDI is 1. */
282 if ((midi > 0) && (stm_pos != midi)) {
283 err = -ENOSYS;
284 goto end;
286 s->midi_position = stm_pos;
287 midi = stm_pos;
288 break;
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 */
300 default:
301 location = pcm + sec_loc;
302 if (location >= AMDTP_MAX_CHANNELS_FOR_PCM) {
303 err = -ENOSYS;
304 goto end;
306 s->pcm_positions[location] = stm_pos;
307 break;
311 if (type != 0x0a)
312 pcm += channels;
313 else
314 midi += channels;
316 end:
317 kfree(buf);
318 return err;
321 static int
322 init_both_connections(struct snd_bebob *bebob)
324 int err;
326 err = cmp_connection_init(&bebob->in_conn,
327 bebob->unit, CMP_INPUT, 0);
328 if (err < 0)
329 goto end;
331 err = cmp_connection_init(&bebob->out_conn,
332 bebob->unit, CMP_OUTPUT, 0);
333 if (err < 0)
334 cmp_connection_destroy(&bebob->in_conn);
335 end:
336 return err;
339 static int
340 check_connection_used_by_others(struct snd_bebob *bebob, struct amdtp_stream *s)
342 struct cmp_connection *conn;
343 bool used;
344 int err;
346 if (s == &bebob->tx_stream)
347 conn = &bebob->out_conn;
348 else
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',
356 conn->pcr_index);
357 err = -EBUSY;
360 return err;
363 static int
364 make_both_connections(struct snd_bebob *bebob, unsigned int rate)
366 int index, pcm_channels, midi_channels, err = 0;
368 if (bebob->connected)
369 goto end;
371 /* confirm params for both streams */
372 err = get_formation_index(rate, &index);
373 if (err < 0)
374 goto end;
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));
387 if (err < 0)
388 goto end;
389 err = cmp_connection_establish(&bebob->in_conn,
390 amdtp_stream_get_max_payload(&bebob->rx_stream));
391 if (err < 0) {
392 cmp_connection_break(&bebob->out_conn);
393 goto end;
396 bebob->connected = true;
397 end:
398 return err;
401 static void
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)
411 msleep(200);
414 static void
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);
423 static int
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;
428 return 0;
431 static int
432 start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream,
433 unsigned int rate)
435 struct cmp_connection *conn;
436 int err = 0;
438 if (stream == &bebob->rx_stream)
439 conn = &bebob->in_conn;
440 else
441 conn = &bebob->out_conn;
443 /* channel mapping */
444 if (bebob->maudio_special_quirk == NULL) {
445 err = map_data_channels(bebob, stream);
446 if (err < 0)
447 goto end;
450 /* start amdtp stream */
451 err = amdtp_stream_start(stream,
452 conn->resources.channel,
453 conn->speed);
454 end:
455 return err;
458 int snd_bebob_stream_init_duplex(struct snd_bebob *bebob)
460 int err;
462 err = init_both_connections(bebob);
463 if (err < 0)
464 goto end;
466 err = amdtp_stream_init(&bebob->tx_stream, bebob->unit,
467 AMDTP_IN_STREAM, CIP_BLOCKING);
468 if (err < 0) {
469 amdtp_stream_destroy(&bebob->tx_stream);
470 destroy_both_connections(bebob);
471 goto end;
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);
486 if (err < 0) {
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;
498 end:
499 return err;
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;
510 int err = 0;
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(
524 &bebob->bus_reset,
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)
533 goto end;
535 err = get_sync_mode(bebob, &sync_mode);
536 if (err < 0)
537 goto end;
538 if (sync_mode == CIP_SYNC_TO_DEVICE) {
539 master = &bebob->tx_stream;
540 slave = &bebob->rx_stream;
541 slave_substreams = &bebob->playback_substreams;
542 } else {
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);
553 if (err < 0)
554 goto end;
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);
566 if (!updated &&
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);
572 if (err < 0) {
573 dev_err(&bebob->unit->device,
574 "fail to get sampling rate: %d\n", err);
575 goto end;
577 if (rate == 0)
578 rate = curr_rate;
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;
591 * NOTE:
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);
599 if (err < 0) {
600 dev_err(&bebob->unit->device,
601 "fail to set sampling rate: %d\n",
602 err);
603 goto end;
607 err = make_both_connections(bebob, rate);
608 if (err < 0)
609 goto end;
611 err = start_stream(bebob, master, rate);
612 if (err < 0) {
613 dev_err(&bebob->unit->device,
614 "fail to run AMDTP master stream:%d\n", err);
615 break_both_connections(bebob);
616 goto end;
620 * NOTE:
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);
626 if (err < 0) {
627 dev_err(&bebob->unit->device,
628 "fail to ensure sampling rate: %d\n",
629 err);
630 amdtp_stream_stop(master);
631 break_both_connections(bebob);
632 goto end;
636 /* wait first callback */
637 if (!amdtp_stream_wait_callback(master, CALLBACK_TIMEOUT)) {
638 amdtp_stream_stop(master);
639 break_both_connections(bebob);
640 err = -ETIMEDOUT;
641 goto end;
645 /* start slave if needed */
646 if (atomic_read(slave_substreams) > 0 && !amdtp_stream_running(slave)) {
647 err = start_stream(bebob, slave, rate);
648 if (err < 0) {
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);
653 goto end;
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);
661 err = -ETIMEDOUT;
664 end:
665 mutex_unlock(&bebob->mutex);
666 return err;
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;
679 } else {
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);
714 } else {
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
749 static int
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))
761 return -ENOSYS;
763 /* check sampling rate */
764 for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) {
765 if (buf[2] == bridgeco_freq_table[i])
766 break;
768 if (i == ARRAY_SIZE(bridgeco_freq_table))
769 return -ENOSYS;
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];
778 switch (format) {
779 /* IEC 60958 Conformant, currently handled as MBLA */
780 case 0x00:
781 /* Multi bit linear audio */
782 case 0x06: /* Raw */
783 formation[i].pcm += channels;
784 break;
785 /* MIDI Conformant */
786 case 0x0d:
787 formation[i].midi += channels;
788 break;
789 /* IEC 61937-3 to 7 */
790 case 0x01:
791 case 0x02:
792 case 0x03:
793 case 0x04:
794 case 0x05:
795 /* Multi bit linear audio */
796 case 0x07: /* DVD-Audio */
797 case 0x0c: /* High Precision */
798 /* One Bit Audio */
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) */
804 case 0x40:
805 /* Don't care */
806 case 0xff:
807 default:
808 return -ENOSYS; /* not supported */
812 if (formation[i].pcm > AMDTP_MAX_CHANNELS_FOR_PCM ||
813 formation[i].midi > AMDTP_MAX_CHANNELS_FOR_MIDI)
814 return -ENOSYS;
816 return 0;
819 static int
820 fill_stream_formations(struct snd_bebob *bebob, enum avc_bridgeco_plug_dir dir,
821 unsigned short pid)
823 u8 *buf;
824 struct snd_bebob_stream_formation *formations;
825 unsigned int len, eid;
826 u8 addr[AVC_BRIDGECO_ADDR_BYTES];
827 int err;
829 buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL);
830 if (buf == NULL)
831 return -ENOMEM;
833 if (dir == AVC_BRIDGECO_PLUG_DIR_IN)
834 formations = bebob->rx_stream_formations;
835 else
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,
843 &len, eid);
844 /* No entries remained. */
845 if (err == -EINVAL && eid > 0) {
846 err = 0;
847 break;
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",
851 eid,
852 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
853 "out",
854 pid, err);
855 break;
858 err = parse_stream_formation(buf, len, formations);
859 if (err < 0)
860 break;
863 kfree(buf);
864 return err;
867 static int
868 seek_msu_sync_input_plug(struct snd_bebob *bebob)
870 u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
871 unsigned int i;
872 enum avc_bridgeco_plug_type type;
873 int err;
875 /* Get the number of Music Sub Unit for both direction. */
876 err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs);
877 if (err < 0) {
878 dev_err(&bebob->unit->device,
879 "fail to get info for MSU in/out plugs: %d\n",
880 err);
881 goto end;
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);
889 if (err < 0) {
890 dev_err(&bebob->unit->device,
891 "fail to get type for MSU in plug %d: %d\n",
892 i, err);
893 goto end;
896 if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
897 bebob->sync_input_plug = i;
898 break;
901 end:
902 return err;
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;
910 unsigned int i;
911 int err;
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);
915 if (err < 0) {
916 dev_err(&bebob->unit->device,
917 "fail to get info for isoc/external in/out plugs: %d\n",
918 err);
919 goto end;
923 * This module supports at least one isoc input plug and one isoc
924 * output plug.
926 if ((plugs[0] == 0) || (plugs[1] == 0)) {
927 err = -ENOSYS;
928 goto end;
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);
934 if (err < 0) {
935 dev_err(&bebob->unit->device,
936 "fail to get type for isoc in plug 0: %d\n", err);
937 goto end;
938 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
939 err = -ENOSYS;
940 goto end;
942 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_IN, 0);
943 if (err < 0)
944 goto end;
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);
949 if (err < 0) {
950 dev_err(&bebob->unit->device,
951 "fail to get type for isoc out plug 0: %d\n", err);
952 goto end;
953 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
954 err = -ENOSYS;
955 goto end;
957 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_OUT, 0);
958 if (err < 0)
959 goto end;
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);
967 if (err < 0) {
968 dev_err(&bebob->unit->device,
969 "fail to get type for external in plug %d: %d\n",
970 i, err);
971 goto end;
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);
983 if (err < 0) {
984 dev_err(&bebob->unit->device,
985 "fail to get type for external out plug %d: %d\n",
986 i, err);
987 goto end;
988 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
989 bebob->midi_output_ports++;
993 /* for check source of clock later */
994 if (!clk_spec)
995 err = seek_msu_sync_input_plug(bebob);
996 end:
997 return err;
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)
1008 int err;
1010 spin_lock_irq(&bebob->lock);
1012 /* user land lock this */
1013 if (bebob->dev_lock_count < 0) {
1014 err = -EBUSY;
1015 goto end;
1018 /* this is the first time */
1019 if (bebob->dev_lock_count++ == 0)
1020 snd_bebob_stream_lock_changed(bebob);
1021 err = 0;
1022 end:
1023 spin_unlock_irq(&bebob->lock);
1024 return err;
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))
1032 goto end;
1033 if (--bebob->dev_lock_count == 0)
1034 snd_bebob_stream_lock_changed(bebob);
1035 end:
1036 spin_unlock_irq(&bebob->lock);