ALSA: bebob: Add hwdep interface
[linux-2.6-block.git] / sound / firewire / bebob / bebob_stream.c
CommitLineData
eb7b3a05
TS
1/*
2 * bebob_stream.c - a part of driver for BeBoB based devices
3 *
4 * Copyright (c) 2013-2014 Takashi Sakamoto
5 *
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
8
9#include "./bebob.h"
10
11#define CALLBACK_TIMEOUT 1000
b6bc8123 12#define FW_ISO_RESOURCE_DELAY 1000
eb7b3a05
TS
13
14/*
15 * NOTE;
16 * For BeBoB streams, Both of input and output CMP connection are important.
17 *
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'.
21 */
22
23/* 128 is an arbitrary length but it seems to be enough */
24#define FORMAT_MAXIMUM_LENGTH 128
25
26const 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,
34};
35
36/*
37 * See: Table 51: Extended Stream Format Info ‘Sampling Frequency’
38 * in Additional AVC commands (Nov 2003, BridgeCo)
39 */
40static 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,
48};
49
50static unsigned int
51get_formation_index(unsigned int rate)
52{
53 unsigned int i;
54
55 for (i = 0; i < ARRAY_SIZE(snd_bebob_rate_table); i++) {
56 if (snd_bebob_rate_table[i] == rate)
57 return i;
58 }
59 return -EINVAL;
60}
61
62int
63snd_bebob_stream_get_rate(struct snd_bebob *bebob, unsigned int *curr_rate)
64{
65 unsigned int tx_rate, rx_rate, trials;
66 int err;
67
68 trials = 0;
69 do {
70 err = avc_general_get_sig_fmt(bebob->unit, &tx_rate,
71 AVC_GENERAL_PLUG_DIR_OUT, 0);
72 } while (err == -EAGAIN && ++trials < 3);
73 if (err < 0)
74 goto end;
75
76 trials = 0;
77 do {
78 err = avc_general_get_sig_fmt(bebob->unit, &rx_rate,
79 AVC_GENERAL_PLUG_DIR_IN, 0);
80 } while (err == -EAGAIN && ++trials < 3);
81 if (err < 0)
82 goto end;
83
84 *curr_rate = rx_rate;
85 if (rx_rate == tx_rate)
86 goto end;
87
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);
91end:
92 return err;
93}
94
95int
96snd_bebob_stream_set_rate(struct snd_bebob *bebob, unsigned int rate)
97{
98 int err;
99
100 err = avc_general_set_sig_fmt(bebob->unit, rate,
101 AVC_GENERAL_PLUG_DIR_OUT, 0);
102 if (err < 0)
103 goto end;
104
105 err = avc_general_set_sig_fmt(bebob->unit, rate,
106 AVC_GENERAL_PLUG_DIR_IN, 0);
107 if (err < 0)
108 goto end;
109
110 /*
111 * Some devices need a bit time for transition.
112 * 300msec is got by some experiments.
113 */
114 msleep(300);
115end:
116 return err;
117}
118
119int
120snd_bebob_stream_check_internal_clock(struct snd_bebob *bebob, bool *internal)
121{
122 u8 addr[AVC_BRIDGECO_ADDR_BYTES], input[7];
123 int err = 0;
124
125 *internal = false;
126
127 /*
128 * 1.The device don't support to switch source of clock then assumed
129 * to use internal clock always
130 */
131 if (bebob->sync_input_plug < 0) {
132 *internal = true;
133 goto end;
134 }
135
136 /*
137 * 2.The device supports to switch source of clock by an usual way.
138 * Let's check input for 'Music Sub Unit Sync Input' plug.
139 */
140 avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
141 bebob->sync_input_plug);
142 err = avc_bridgeco_get_plug_input(bebob->unit, addr, input);
143 if (err < 0) {
144 dev_err(&bebob->unit->device,
145 "fail to get an input for MSU in plug %d: %d\n",
146 bebob->sync_input_plug, err);
147 goto end;
148 }
149
150 /*
151 * If there are no input plugs, all of fields are 0xff.
152 * Here check the first field. This field is used for direction.
153 */
154 if (input[0] == 0xff) {
155 *internal = true;
156 goto end;
157 }
158
159 /*
160 * If source of clock is internal CSR, Music Sub Unit Sync Input is
161 * a destination of Music Sub Unit Sync Output.
162 */
163 *internal = ((input[0] == AVC_BRIDGECO_PLUG_DIR_OUT) &&
164 (input[1] == AVC_BRIDGECO_PLUG_MODE_SUBUNIT) &&
165 (input[2] == 0x0c) &&
166 (input[3] == 0x00));
167end:
168 return err;
169}
170
171static unsigned int
172map_data_channels(struct snd_bebob *bebob, struct amdtp_stream *s)
173{
174 unsigned int sec, sections, ch, channels;
175 unsigned int pcm, midi, location;
176 unsigned int stm_pos, sec_loc, pos;
177 u8 *buf, addr[AVC_BRIDGECO_ADDR_BYTES], type;
178 enum avc_bridgeco_plug_dir dir;
179 int err;
180
181 /*
182 * The length of return value of this command cannot be expected. Here
183 * use the maximum length of FCP.
184 */
185 buf = kzalloc(256, GFP_KERNEL);
186 if (buf == NULL)
187 return -ENOMEM;
188
189 if (s == &bebob->tx_stream)
190 dir = AVC_BRIDGECO_PLUG_DIR_OUT;
191 else
192 dir = AVC_BRIDGECO_PLUG_DIR_IN;
193
194 avc_bridgeco_fill_unit_addr(addr, dir, AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
195 err = avc_bridgeco_get_plug_ch_pos(bebob->unit, addr, buf, 256);
196 if (err < 0) {
197 dev_err(&bebob->unit->device,
198 "fail to get channel position for isoc %s plug 0: %d\n",
199 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" : "out",
200 err);
201 goto end;
202 }
203 pos = 0;
204
205 /* positions in I/O buffer */
206 pcm = 0;
207 midi = 0;
208
209 /* the number of sections in AMDTP packet */
210 sections = buf[pos++];
211
212 for (sec = 0; sec < sections; sec++) {
213 /* type of this section */
214 avc_bridgeco_fill_unit_addr(addr, dir,
215 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
216 err = avc_bridgeco_get_plug_section_type(bebob->unit, addr,
217 sec, &type);
218 if (err < 0) {
219 dev_err(&bebob->unit->device,
220 "fail to get section type for isoc %s plug 0: %d\n",
221 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
222 "out",
223 err);
224 goto end;
225 }
226 /* NoType */
227 if (type == 0xff) {
228 err = -ENOSYS;
229 goto end;
230 }
231
232 /* the number of channels in this section */
233 channels = buf[pos++];
234
235 for (ch = 0; ch < channels; ch++) {
236 /* position of this channel in AMDTP packet */
237 stm_pos = buf[pos++] - 1;
238 /* location of this channel in this section */
239 sec_loc = buf[pos++] - 1;
240
241 switch (type) {
242 /* for MIDI conformant data channel */
243 case 0x0a:
244 /* AMDTP_MAX_CHANNELS_FOR_MIDI is 1. */
245 if ((midi > 0) && (stm_pos != midi)) {
246 err = -ENOSYS;
247 goto end;
248 }
249 s->midi_position = stm_pos;
250 midi = stm_pos;
251 break;
252 /* for PCM data channel */
253 case 0x01: /* Headphone */
254 case 0x02: /* Microphone */
255 case 0x03: /* Line */
256 case 0x04: /* SPDIF */
257 case 0x05: /* ADAT */
258 case 0x06: /* TDIF */
259 case 0x07: /* MADI */
260 /* for undefined/changeable signal */
261 case 0x08: /* Analog */
262 case 0x09: /* Digital */
263 default:
264 location = pcm + sec_loc;
265 if (location >= AMDTP_MAX_CHANNELS_FOR_PCM) {
266 err = -ENOSYS;
267 goto end;
268 }
269 s->pcm_positions[location] = stm_pos;
270 break;
271 }
272 }
273
274 if (type != 0x0a)
275 pcm += channels;
276 else
277 midi += channels;
278 }
279end:
280 kfree(buf);
281 return err;
282}
283
284static int
285init_both_connections(struct snd_bebob *bebob)
286{
287 int err;
288
289 err = cmp_connection_init(&bebob->in_conn,
290 bebob->unit, CMP_INPUT, 0);
291 if (err < 0)
292 goto end;
293
294 err = cmp_connection_init(&bebob->out_conn,
295 bebob->unit, CMP_OUTPUT, 0);
296 if (err < 0)
297 cmp_connection_destroy(&bebob->in_conn);
298end:
299 return err;
300}
301
302static int
303check_connection_used_by_others(struct snd_bebob *bebob, struct amdtp_stream *s)
304{
305 struct cmp_connection *conn;
306 bool used;
307 int err;
308
309 if (s == &bebob->tx_stream)
310 conn = &bebob->out_conn;
311 else
312 conn = &bebob->in_conn;
313
314 err = cmp_connection_check_used(conn, &used);
315 if ((err >= 0) && used && !amdtp_stream_running(s)) {
316 dev_err(&bebob->unit->device,
317 "Connection established by others: %cPCR[%d]\n",
318 (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
319 conn->pcr_index);
320 err = -EBUSY;
321 }
322
323 return err;
324}
325
326static int
327make_both_connections(struct snd_bebob *bebob, unsigned int rate)
328{
b6bc8123
TS
329 int index, pcm_channels, midi_channels, err = 0;
330
331 if (bebob->connected)
332 goto end;
eb7b3a05
TS
333
334 /* confirm params for both streams */
335 index = get_formation_index(rate);
336 pcm_channels = bebob->tx_stream_formations[index].pcm;
337 midi_channels = bebob->tx_stream_formations[index].midi;
338 amdtp_stream_set_parameters(&bebob->tx_stream,
339 rate, pcm_channels, midi_channels * 8);
340 pcm_channels = bebob->rx_stream_formations[index].pcm;
341 midi_channels = bebob->rx_stream_formations[index].midi;
342 amdtp_stream_set_parameters(&bebob->rx_stream,
343 rate, pcm_channels, midi_channels * 8);
344
345 /* establish connections for both streams */
346 err = cmp_connection_establish(&bebob->out_conn,
347 amdtp_stream_get_max_payload(&bebob->tx_stream));
348 if (err < 0)
349 goto end;
350 err = cmp_connection_establish(&bebob->in_conn,
351 amdtp_stream_get_max_payload(&bebob->rx_stream));
b6bc8123 352 if (err < 0) {
eb7b3a05 353 cmp_connection_break(&bebob->out_conn);
b6bc8123
TS
354 goto end;
355 }
356
357 bebob->connected = true;
eb7b3a05
TS
358end:
359 return err;
360}
361
362static void
363break_both_connections(struct snd_bebob *bebob)
364{
365 cmp_connection_break(&bebob->in_conn);
366 cmp_connection_break(&bebob->out_conn);
b6bc8123
TS
367
368 bebob->connected = false;
eb7b3a05
TS
369}
370
371static void
372destroy_both_connections(struct snd_bebob *bebob)
373{
374 break_both_connections(bebob);
375
376 cmp_connection_destroy(&bebob->in_conn);
377 cmp_connection_destroy(&bebob->out_conn);
378}
379
380static int
381get_sync_mode(struct snd_bebob *bebob, enum cip_flags *sync_mode)
382{
383 /* currently this module doesn't support SYT-Match mode */
384 *sync_mode = CIP_SYNC_TO_DEVICE;
385 return 0;
386}
387
388static int
389start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream,
390 unsigned int rate)
391{
392 struct cmp_connection *conn;
393 int err = 0;
394
395 if (stream == &bebob->rx_stream)
396 conn = &bebob->in_conn;
397 else
398 conn = &bebob->out_conn;
399
400 /* channel mapping */
401 err = map_data_channels(bebob, stream);
402 if (err < 0)
403 goto end;
404
405 /* start amdtp stream */
406 err = amdtp_stream_start(stream,
407 conn->resources.channel,
408 conn->speed);
409end:
410 return err;
411}
412
413int snd_bebob_stream_init_duplex(struct snd_bebob *bebob)
414{
415 int err;
416
417 err = init_both_connections(bebob);
418 if (err < 0)
419 goto end;
420
421 err = amdtp_stream_init(&bebob->tx_stream, bebob->unit,
422 AMDTP_IN_STREAM, CIP_BLOCKING);
423 if (err < 0) {
424 amdtp_stream_destroy(&bebob->tx_stream);
425 destroy_both_connections(bebob);
426 goto end;
427 }
b6bc8123
TS
428 /* See comments in next function */
429 init_completion(&bebob->bus_reset);
430 bebob->tx_stream.flags |= CIP_SKIP_INIT_DBC_CHECK;
eb7b3a05
TS
431
432 err = amdtp_stream_init(&bebob->rx_stream, bebob->unit,
433 AMDTP_OUT_STREAM, CIP_BLOCKING);
434 if (err < 0) {
435 amdtp_stream_destroy(&bebob->tx_stream);
436 amdtp_stream_destroy(&bebob->rx_stream);
437 destroy_both_connections(bebob);
438 }
439end:
440 return err;
441}
442
443int snd_bebob_stream_start_duplex(struct snd_bebob *bebob, int rate)
444{
445 struct amdtp_stream *master, *slave;
446 atomic_t *slave_substreams;
447 enum cip_flags sync_mode;
448 unsigned int curr_rate;
b6bc8123 449 bool updated = false;
eb7b3a05
TS
450 int err = 0;
451
b6bc8123
TS
452 /*
453 * Normal BeBoB firmware has a quirk at bus reset to transmits packets
454 * with discontinuous value in dbc field.
455 *
456 * This 'struct completion' is used to call .update() at first to update
457 * connections/streams. Next following codes handle streaming error.
458 */
459 if (amdtp_streaming_error(&bebob->tx_stream)) {
460 if (completion_done(&bebob->bus_reset))
461 reinit_completion(&bebob->bus_reset);
462
463 updated = (wait_for_completion_interruptible_timeout(
464 &bebob->bus_reset,
465 msecs_to_jiffies(FW_ISO_RESOURCE_DELAY)) > 0);
466 }
467
eb7b3a05
TS
468 mutex_lock(&bebob->mutex);
469
470 /* Need no substreams */
471 if (atomic_read(&bebob->playback_substreams) == 0 &&
472 atomic_read(&bebob->capture_substreams) == 0)
473 goto end;
474
475 err = get_sync_mode(bebob, &sync_mode);
476 if (err < 0)
477 goto end;
478 if (sync_mode == CIP_SYNC_TO_DEVICE) {
479 master = &bebob->tx_stream;
480 slave = &bebob->rx_stream;
481 slave_substreams = &bebob->playback_substreams;
482 } else {
483 master = &bebob->rx_stream;
484 slave = &bebob->tx_stream;
485 slave_substreams = &bebob->capture_substreams;
486 }
487
488 /*
489 * Considering JACK/FFADO streaming:
490 * TODO: This can be removed hwdep functionality becomes popular.
491 */
492 err = check_connection_used_by_others(bebob, master);
493 if (err < 0)
494 goto end;
495
b6bc8123
TS
496 /*
497 * packet queueing error or detecting discontinuity
498 *
499 * At bus reset, connections should not be broken here. So streams need
500 * to be re-started. This is a reason to use SKIP_INIT_DBC_CHECK flag.
501 */
502 if (amdtp_streaming_error(master))
eb7b3a05 503 amdtp_stream_stop(master);
eb7b3a05
TS
504 if (amdtp_streaming_error(slave))
505 amdtp_stream_stop(slave);
b6bc8123
TS
506 if (!updated &&
507 !amdtp_stream_running(master) && !amdtp_stream_running(slave))
508 break_both_connections(bebob);
eb7b3a05
TS
509
510 /* stop streams if rate is different */
511 err = snd_bebob_stream_get_rate(bebob, &curr_rate);
512 if (err < 0) {
513 dev_err(&bebob->unit->device,
514 "fail to get sampling rate: %d\n", err);
515 goto end;
516 }
517 if (rate == 0)
518 rate = curr_rate;
519 if (rate != curr_rate) {
520 amdtp_stream_stop(master);
521 amdtp_stream_stop(slave);
522 break_both_connections(bebob);
523 }
524
525 /* master should be always running */
526 if (!amdtp_stream_running(master)) {
527 amdtp_stream_set_sync(sync_mode, master, slave);
528 bebob->master = master;
529
530 /*
531 * NOTE:
532 * If establishing connections at first, Yamaha GO46
533 * (and maybe Terratec X24) don't generate sound.
534 */
535 err = snd_bebob_stream_set_rate(bebob, rate);
536 if (err < 0) {
537 dev_err(&bebob->unit->device,
538 "fail to set sampling rate: %d\n",
539 err);
540 goto end;
541 }
542
543 err = make_both_connections(bebob, rate);
544 if (err < 0)
545 goto end;
546
547 err = start_stream(bebob, master, rate);
548 if (err < 0) {
549 dev_err(&bebob->unit->device,
550 "fail to run AMDTP master stream:%d\n", err);
551 break_both_connections(bebob);
552 goto end;
553 }
554
555 /* wait first callback */
556 if (!amdtp_stream_wait_callback(master, CALLBACK_TIMEOUT)) {
557 amdtp_stream_stop(master);
558 break_both_connections(bebob);
559 err = -ETIMEDOUT;
560 goto end;
561 }
562 }
563
564 /* start slave if needed */
565 if (atomic_read(slave_substreams) > 0 && !amdtp_stream_running(slave)) {
566 err = start_stream(bebob, slave, rate);
567 if (err < 0) {
568 dev_err(&bebob->unit->device,
569 "fail to run AMDTP slave stream:%d\n", err);
570 amdtp_stream_stop(master);
571 break_both_connections(bebob);
572 goto end;
573 }
574
575 /* wait first callback */
576 if (!amdtp_stream_wait_callback(slave, CALLBACK_TIMEOUT)) {
577 amdtp_stream_stop(slave);
578 amdtp_stream_stop(master);
579 break_both_connections(bebob);
580 err = -ETIMEDOUT;
581 }
582 }
583end:
584 mutex_unlock(&bebob->mutex);
585 return err;
586}
587
588void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob)
589{
590 struct amdtp_stream *master, *slave;
591 atomic_t *master_substreams, *slave_substreams;
592
593 mutex_lock(&bebob->mutex);
594
595 if (bebob->master == &bebob->rx_stream) {
596 slave = &bebob->tx_stream;
597 master = &bebob->rx_stream;
598 slave_substreams = &bebob->capture_substreams;
599 master_substreams = &bebob->playback_substreams;
600 } else {
601 slave = &bebob->rx_stream;
602 master = &bebob->tx_stream;
603 slave_substreams = &bebob->playback_substreams;
604 master_substreams = &bebob->capture_substreams;
605 }
606
607 if (atomic_read(slave_substreams) == 0) {
608 amdtp_stream_pcm_abort(slave);
609 amdtp_stream_stop(slave);
610
611 if (atomic_read(master_substreams) == 0) {
612 amdtp_stream_pcm_abort(master);
613 amdtp_stream_stop(master);
614 break_both_connections(bebob);
615 }
616 }
617
618 mutex_unlock(&bebob->mutex);
619}
620
621void snd_bebob_stream_update_duplex(struct snd_bebob *bebob)
622{
623 /* vs. XRUN recovery due to discontinuity at bus reset */
624 mutex_lock(&bebob->mutex);
625
626 if ((cmp_connection_update(&bebob->in_conn) < 0) ||
627 (cmp_connection_update(&bebob->out_conn) < 0)) {
628 amdtp_stream_pcm_abort(&bebob->rx_stream);
629 amdtp_stream_pcm_abort(&bebob->tx_stream);
630 amdtp_stream_stop(&bebob->rx_stream);
631 amdtp_stream_stop(&bebob->tx_stream);
632 break_both_connections(bebob);
633 } else {
634 amdtp_stream_update(&bebob->rx_stream);
635 amdtp_stream_update(&bebob->tx_stream);
636 }
637
b6bc8123
TS
638 /* wake up stream_start_duplex() */
639 if (!completion_done(&bebob->bus_reset))
640 complete_all(&bebob->bus_reset);
641
eb7b3a05
TS
642 mutex_unlock(&bebob->mutex);
643}
644
645void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob)
646{
647 mutex_lock(&bebob->mutex);
648
649 amdtp_stream_pcm_abort(&bebob->rx_stream);
650 amdtp_stream_pcm_abort(&bebob->tx_stream);
651
652 amdtp_stream_stop(&bebob->rx_stream);
653 amdtp_stream_stop(&bebob->tx_stream);
654
655 amdtp_stream_destroy(&bebob->rx_stream);
656 amdtp_stream_destroy(&bebob->tx_stream);
657
658 destroy_both_connections(bebob);
659
660 mutex_unlock(&bebob->mutex);
661}
662
663/*
664 * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’
665 * in Additional AVC commands (Nov 2003, BridgeCo)
666 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
667 */
668static int
669parse_stream_formation(u8 *buf, unsigned int len,
670 struct snd_bebob_stream_formation *formation)
671{
672 unsigned int i, e, channels, format;
673
674 /*
675 * this module can support a hierarchy combination that:
676 * Root: Audio and Music (0x90)
677 * Level 1: AM824 Compound (0x40)
678 */
679 if ((buf[0] != 0x90) || (buf[1] != 0x40))
680 return -ENOSYS;
681
682 /* check sampling rate */
683 for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) {
684 if (buf[2] == bridgeco_freq_table[i])
685 break;
686 }
687 if (i == sizeof(bridgeco_freq_table))
688 return -ENOSYS;
689
690 /* Avoid double count by different entries for the same rate. */
691 memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation));
692
693 for (e = 0; e < buf[4]; e++) {
694 channels = buf[5 + e * 2];
695 format = buf[6 + e * 2];
696
697 switch (format) {
698 /* IEC 60958-3, currently handle as MBLA */
699 case 0x00:
700 /* Multi bit linear audio */
701 case 0x06: /* Raw */
702 formation[i].pcm += channels;
703 break;
704 /* MIDI Conformant */
705 case 0x0d:
706 formation[i].midi += channels;
707 break;
708 /* IEC 61937-3 to 7 */
709 case 0x01:
710 case 0x02:
711 case 0x03:
712 case 0x04:
713 case 0x05:
714 /* Multi bit linear audio */
715 case 0x07: /* DVD-Audio */
716 case 0x0c: /* High Precision */
717 /* One Bit Audio */
718 case 0x08: /* (Plain) Raw */
719 case 0x09: /* (Plain) SACD */
720 case 0x0a: /* (Encoded) Raw */
721 case 0x0b: /* (Encoded) SACD */
722 /* Synchronization Stream (Stereo Raw audio) */
723 case 0x40:
724 /* Don't care */
725 case 0xff:
726 default:
727 return -ENOSYS; /* not supported */
728 }
729 }
730
731 if (formation[i].pcm > AMDTP_MAX_CHANNELS_FOR_PCM ||
732 formation[i].midi > AMDTP_MAX_CHANNELS_FOR_MIDI)
733 return -ENOSYS;
734
735 return 0;
736}
737
738static int
739fill_stream_formations(struct snd_bebob *bebob, enum avc_bridgeco_plug_dir dir,
740 unsigned short pid)
741{
742 u8 *buf;
743 struct snd_bebob_stream_formation *formations;
744 unsigned int len, eid;
745 u8 addr[AVC_BRIDGECO_ADDR_BYTES];
746 int err;
747
748 buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL);
749 if (buf == NULL)
750 return -ENOMEM;
751
752 if (dir == AVC_BRIDGECO_PLUG_DIR_IN)
753 formations = bebob->rx_stream_formations;
754 else
755 formations = bebob->tx_stream_formations;
756
757 for (eid = 0; eid < SND_BEBOB_STRM_FMT_ENTRIES; eid++) {
758 len = FORMAT_MAXIMUM_LENGTH;
759 avc_bridgeco_fill_unit_addr(addr, dir,
760 AVC_BRIDGECO_PLUG_UNIT_ISOC, pid);
761 err = avc_bridgeco_get_plug_strm_fmt(bebob->unit, addr, buf,
762 &len, eid);
763 /* No entries remained. */
764 if (err == -EINVAL && eid > 0) {
765 err = 0;
766 break;
767 } else if (err < 0) {
768 dev_err(&bebob->unit->device,
769 "fail to get stream format %d for isoc %s plug %d:%d\n",
770 eid,
771 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
772 "out",
773 pid, err);
774 break;
775 }
776
777 err = parse_stream_formation(buf, len, formations);
778 if (err < 0)
779 break;
780 }
781
782 kfree(buf);
783 return err;
784}
785
786static int
787seek_msu_sync_input_plug(struct snd_bebob *bebob)
788{
789 u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
790 unsigned int i, type;
791 int err;
792
793 /* Get the number of Music Sub Unit for both direction. */
794 err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs);
795 if (err < 0) {
796 dev_err(&bebob->unit->device,
797 "fail to get info for MSU in/out plugs: %d\n",
798 err);
799 goto end;
800 }
801
802 /* seek destination plugs for 'MSU sync input' */
803 bebob->sync_input_plug = -1;
804 for (i = 0; i < plugs[0]; i++) {
805 avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, i);
806 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
807 if (err < 0) {
808 dev_err(&bebob->unit->device,
809 "fail to get type for MSU in plug %d: %d\n",
810 i, err);
811 goto end;
812 }
813
814 if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
815 bebob->sync_input_plug = i;
816 break;
817 }
818 }
819end:
820 return err;
821}
822
823int snd_bebob_stream_discover(struct snd_bebob *bebob)
824{
825 u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
826 enum avc_bridgeco_plug_type type;
827 unsigned int i;
828 int err;
829
830 /* the number of plugs for isoc in/out, ext in/out */
831 err = avc_general_get_plug_info(bebob->unit, 0x1f, 0x07, 0x00, plugs);
832 if (err < 0) {
833 dev_err(&bebob->unit->device,
834 "fail to get info for isoc/external in/out plugs: %d\n",
835 err);
836 goto end;
837 }
838
839 /*
840 * This module supports at least one isoc input plug and one isoc
841 * output plug.
842 */
843 if ((plugs[0] == 0) || (plugs[1] == 0)) {
844 err = -ENOSYS;
845 goto end;
846 }
847
848 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
849 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
850 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
851 if (err < 0) {
852 dev_err(&bebob->unit->device,
853 "fail to get type for isoc in plug 0: %d\n", err);
854 goto end;
855 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
856 err = -ENOSYS;
857 goto end;
858 }
859 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_IN, 0);
860 if (err < 0)
861 goto end;
862
863 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
864 AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
865 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
866 if (err < 0) {
867 dev_err(&bebob->unit->device,
868 "fail to get type for isoc out plug 0: %d\n", err);
869 goto end;
870 } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
871 err = -ENOSYS;
872 goto end;
873 }
874 err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_OUT, 0);
875 if (err < 0)
876 goto end;
877
878 /* count external input plugs for MIDI */
879 bebob->midi_input_ports = 0;
880 for (i = 0; i < plugs[2]; i++) {
881 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
882 AVC_BRIDGECO_PLUG_UNIT_EXT, i);
883 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
884 if (err < 0) {
885 dev_err(&bebob->unit->device,
886 "fail to get type for external in plug %d: %d\n",
887 i, err);
888 goto end;
889 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
890 bebob->midi_input_ports++;
891 }
892 }
893
894 /* count external output plugs for MIDI */
895 bebob->midi_output_ports = 0;
896 for (i = 0; i < plugs[3]; i++) {
897 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
898 AVC_BRIDGECO_PLUG_UNIT_EXT, i);
899 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
900 if (err < 0) {
901 dev_err(&bebob->unit->device,
902 "fail to get type for external out plug %d: %d\n",
903 i, err);
904 goto end;
905 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
906 bebob->midi_output_ports++;
907 }
908 }
909
910 /* for check source of clock later */
911 err = seek_msu_sync_input_plug(bebob);
912end:
913 return err;
914}
618eabea
TS
915
916void snd_bebob_stream_lock_changed(struct snd_bebob *bebob)
917{
918 bebob->dev_lock_changed = true;
919 wake_up(&bebob->hwdep_wait);
920}
921
922int snd_bebob_stream_lock_try(struct snd_bebob *bebob)
923{
924 int err;
925
926 spin_lock_irq(&bebob->lock);
927
928 /* user land lock this */
929 if (bebob->dev_lock_count < 0) {
930 err = -EBUSY;
931 goto end;
932 }
933
934 /* this is the first time */
935 if (bebob->dev_lock_count++ == 0)
936 snd_bebob_stream_lock_changed(bebob);
937 err = 0;
938end:
939 spin_unlock_irq(&bebob->lock);
940 return err;
941}
942
943void snd_bebob_stream_lock_release(struct snd_bebob *bebob)
944{
945 spin_lock_irq(&bebob->lock);
946
947 if (WARN_ON(bebob->dev_lock_count <= 0))
948 goto end;
949 if (--bebob->dev_lock_count == 0)
950 snd_bebob_stream_lock_changed(bebob);
951end:
952 spin_unlock_irq(&bebob->lock);
953}