GNU Linux-libre 4.4.288-gnu1
[releases.git] / sound / firewire / bebob / bebob_stream.c
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        2000
12 #define FW_ISO_RESOURCE_DELAY   1000
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
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,
34 };
35
36 /*
37  * See: Table 51: Extended Stream Format Info ‘Sampling Frequency’
38  * in Additional AVC commands (Nov 2003, BridgeCo)
39  */
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,
48 };
49
50 static int
51 get_formation_index(unsigned int rate, unsigned int *index)
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                         *index = i;
58                         return 0;
59                 }
60         }
61         return -EINVAL;
62 }
63
64 int
65 snd_bebob_stream_get_rate(struct snd_bebob *bebob, unsigned int *curr_rate)
66 {
67         unsigned int tx_rate, rx_rate, trials;
68         int err;
69
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;
77
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;
85
86         *curr_rate = rx_rate;
87         if (rx_rate == tx_rate)
88                 goto end;
89
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;
95 }
96
97 int
98 snd_bebob_stream_set_rate(struct snd_bebob *bebob, unsigned int rate)
99 {
100         int err;
101
102         err = avc_general_set_sig_fmt(bebob->unit, rate,
103                                       AVC_GENERAL_PLUG_DIR_OUT, 0);
104         if (err < 0)
105                 goto end;
106
107         err = avc_general_set_sig_fmt(bebob->unit, rate,
108                                       AVC_GENERAL_PLUG_DIR_IN, 0);
109         if (err < 0)
110                 goto end;
111
112         /*
113          * Some devices need a bit time for transition.
114          * 300msec is got by some experiments.
115          */
116         msleep(300);
117 end:
118         return err;
119 }
120
121 int snd_bebob_stream_get_clock_src(struct snd_bebob *bebob,
122                                    enum snd_bebob_clock_type *src)
123 {
124         const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
125         u8 addr[AVC_BRIDGECO_ADDR_BYTES], input[7];
126         unsigned int id;
127         enum avc_bridgeco_plug_type type;
128         int err = 0;
129
130         /* 1.The device has its own operation to switch source of clock */
131         if (clk_spec) {
132                 err = clk_spec->get(bebob, &id);
133                 if (err < 0) {
134                         dev_err(&bebob->unit->device,
135                                 "fail to get clock source: %d\n", err);
136                         goto end;
137                 }
138
139                 if (id >= clk_spec->num) {
140                         dev_err(&bebob->unit->device,
141                                 "clock source %d out of range 0..%d\n",
142                                 id, clk_spec->num - 1);
143                         err = -EIO;
144                         goto end;
145                 }
146
147                 *src = clk_spec->types[id];
148                 goto end;
149         }
150
151         /*
152          * 2.The device don't support to switch source of clock then assumed
153          *   to use internal clock always
154          */
155         if (bebob->sync_input_plug < 0) {
156                 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
157                 goto end;
158         }
159
160         /*
161          * 3.The device supports to switch source of clock by an usual way.
162          *   Let's check input for 'Music Sub Unit Sync Input' plug.
163          */
164         avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
165                                    bebob->sync_input_plug);
166         err = avc_bridgeco_get_plug_input(bebob->unit, addr, input);
167         if (err < 0) {
168                 dev_err(&bebob->unit->device,
169                         "fail to get an input for MSU in plug %d: %d\n",
170                         bebob->sync_input_plug, err);
171                 goto end;
172         }
173
174         /*
175          * If there are no input plugs, all of fields are 0xff.
176          * Here check the first field. This field is used for direction.
177          */
178         if (input[0] == 0xff) {
179                 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
180                 goto end;
181         }
182
183         /* The source from any output plugs is for one purpose only. */
184         if (input[0] == AVC_BRIDGECO_PLUG_DIR_OUT) {
185                 /*
186                  * In BeBoB architecture, the source from music subunit may
187                  * bypass from oPCR[0]. This means that this source gives
188                  * synchronization to IEEE 1394 cycle start packet.
189                  */
190                 if (input[1] == AVC_BRIDGECO_PLUG_MODE_SUBUNIT &&
191                     input[2] == 0x0c) {
192                         *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
193                         goto end;
194                 }
195         /* The source from any input units is for several purposes. */
196         } else if (input[1] == AVC_BRIDGECO_PLUG_MODE_UNIT) {
197                 if (input[2] == AVC_BRIDGECO_PLUG_UNIT_ISOC) {
198                         if (input[3] == 0x00) {
199                                 /*
200                                  * This source comes from iPCR[0]. This means
201                                  * that presentation timestamp calculated by
202                                  * SYT series of the received packets. In
203                                  * short, this driver is the master of
204                                  * synchronization.
205                                  */
206                                 *src = SND_BEBOB_CLOCK_TYPE_SYT;
207                                 goto end;
208                         } else {
209                                 /*
210                                  * This source comes from iPCR[1-29]. This
211                                  * means that the synchronization stream is not
212                                  * the Audio/MIDI compound stream.
213                                  */
214                                 *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
215                                 goto end;
216                         }
217                 } else if (input[2] == AVC_BRIDGECO_PLUG_UNIT_EXT) {
218                         /* Check type of this plug.  */
219                         avc_bridgeco_fill_unit_addr(addr,
220                                                     AVC_BRIDGECO_PLUG_DIR_IN,
221                                                     AVC_BRIDGECO_PLUG_UNIT_EXT,
222                                                     input[3]);
223                         err = avc_bridgeco_get_plug_type(bebob->unit, addr,
224                                                          &type);
225                         if (err < 0)
226                                 goto end;
227
228                         if (type == AVC_BRIDGECO_PLUG_TYPE_DIG) {
229                                 /*
230                                  * SPDIF/ADAT or sometimes (not always) word
231                                  * clock.
232                                  */
233                                 *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
234                                 goto end;
235                         } else if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
236                                 /* Often word clock. */
237                                 *src = SND_BEBOB_CLOCK_TYPE_EXTERNAL;
238                                 goto end;
239                         } else if (type == AVC_BRIDGECO_PLUG_TYPE_ADDITION) {
240                                 /*
241                                  * Not standard.
242                                  * Mostly, additional internal clock.
243                                  */
244                                 *src = SND_BEBOB_CLOCK_TYPE_INTERNAL;
245                                 goto end;
246                         }
247                 }
248         }
249
250         /* Not supported. */
251         err = -EIO;
252 end:
253         return err;
254 }
255
256 static int map_data_channels(struct snd_bebob *bebob, struct amdtp_stream *s)
257 {
258         unsigned int sec, sections, ch, channels;
259         unsigned int pcm, midi, location;
260         unsigned int stm_pos, sec_loc, pos;
261         u8 *buf, addr[AVC_BRIDGECO_ADDR_BYTES], type;
262         enum avc_bridgeco_plug_dir dir;
263         int err;
264
265         /*
266          * The length of return value of this command cannot be expected. Here
267          * use the maximum length of FCP.
268          */
269         buf = kzalloc(256, GFP_KERNEL);
270         if (buf == NULL)
271                 return -ENOMEM;
272
273         if (s == &bebob->tx_stream)
274                 dir = AVC_BRIDGECO_PLUG_DIR_OUT;
275         else
276                 dir = AVC_BRIDGECO_PLUG_DIR_IN;
277
278         avc_bridgeco_fill_unit_addr(addr, dir, AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
279         err = avc_bridgeco_get_plug_ch_pos(bebob->unit, addr, buf, 256);
280         if (err < 0) {
281                 dev_err(&bebob->unit->device,
282                         "fail to get channel position for isoc %s plug 0: %d\n",
283                         (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" : "out",
284                         err);
285                 goto end;
286         }
287         pos = 0;
288
289         /* positions in I/O buffer */
290         pcm = 0;
291         midi = 0;
292
293         /* the number of sections in AMDTP packet */
294         sections = buf[pos++];
295
296         for (sec = 0; sec < sections; sec++) {
297                 /* type of this section */
298                 avc_bridgeco_fill_unit_addr(addr, dir,
299                                             AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
300                 err = avc_bridgeco_get_plug_section_type(bebob->unit, addr,
301                                                          sec, &type);
302                 if (err < 0) {
303                         dev_err(&bebob->unit->device,
304                         "fail to get section type for isoc %s plug 0: %d\n",
305                                 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
306                                                                     "out",
307                                 err);
308                         goto end;
309                 }
310                 /* NoType */
311                 if (type == 0xff) {
312                         err = -ENOSYS;
313                         goto end;
314                 }
315
316                 /* the number of channels in this section */
317                 channels = buf[pos++];
318
319                 for (ch = 0; ch < channels; ch++) {
320                         /* position of this channel in AMDTP packet */
321                         stm_pos = buf[pos++] - 1;
322                         /* location of this channel in this section */
323                         sec_loc = buf[pos++] - 1;
324
325                         /*
326                          * Basically the number of location is within the
327                          * number of channels in this section. But some models
328                          * of M-Audio don't follow this. Its location for MIDI
329                          * is the position of MIDI channels in AMDTP packet.
330                          */
331                         if (sec_loc >= channels)
332                                 sec_loc = ch;
333
334                         switch (type) {
335                         /* for MIDI conformant data channel */
336                         case 0x0a:
337                                 /* AMDTP_MAX_CHANNELS_FOR_MIDI is 1. */
338                                 if ((midi > 0) && (stm_pos != midi)) {
339                                         err = -ENOSYS;
340                                         goto end;
341                                 }
342                                 amdtp_am824_set_midi_position(s, stm_pos);
343                                 midi = stm_pos;
344                                 break;
345                         /* for PCM data channel */
346                         case 0x01:      /* Headphone */
347                         case 0x02:      /* Microphone */
348                         case 0x03:      /* Line */
349                         case 0x04:      /* SPDIF */
350                         case 0x05:      /* ADAT */
351                         case 0x06:      /* TDIF */
352                         case 0x07:      /* MADI */
353                         /* for undefined/changeable signal  */
354                         case 0x08:      /* Analog */
355                         case 0x09:      /* Digital */
356                         default:
357                                 location = pcm + sec_loc;
358                                 if (location >= AM824_MAX_CHANNELS_FOR_PCM) {
359                                         err = -ENOSYS;
360                                         goto end;
361                                 }
362                                 amdtp_am824_set_pcm_position(s, location,
363                                                              stm_pos);
364                                 break;
365                         }
366                 }
367
368                 if (type != 0x0a)
369                         pcm += channels;
370                 else
371                         midi += channels;
372         }
373 end:
374         kfree(buf);
375         return err;
376 }
377
378 static int
379 init_both_connections(struct snd_bebob *bebob)
380 {
381         int err;
382
383         err = cmp_connection_init(&bebob->in_conn,
384                                   bebob->unit, CMP_INPUT, 0);
385         if (err < 0)
386                 goto end;
387
388         err = cmp_connection_init(&bebob->out_conn,
389                                   bebob->unit, CMP_OUTPUT, 0);
390         if (err < 0)
391                 cmp_connection_destroy(&bebob->in_conn);
392 end:
393         return err;
394 }
395
396 static int
397 check_connection_used_by_others(struct snd_bebob *bebob, struct amdtp_stream *s)
398 {
399         struct cmp_connection *conn;
400         bool used;
401         int err;
402
403         if (s == &bebob->tx_stream)
404                 conn = &bebob->out_conn;
405         else
406                 conn = &bebob->in_conn;
407
408         err = cmp_connection_check_used(conn, &used);
409         if ((err >= 0) && used && !amdtp_stream_running(s)) {
410                 dev_err(&bebob->unit->device,
411                         "Connection established by others: %cPCR[%d]\n",
412                         (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
413                         conn->pcr_index);
414                 err = -EBUSY;
415         }
416
417         return err;
418 }
419
420 static int
421 make_both_connections(struct snd_bebob *bebob, unsigned int rate)
422 {
423         int index, pcm_channels, midi_channels, err = 0;
424
425         if (bebob->connected)
426                 goto end;
427
428         /* confirm params for both streams */
429         err = get_formation_index(rate, &index);
430         if (err < 0)
431                 goto end;
432         pcm_channels = bebob->tx_stream_formations[index].pcm;
433         midi_channels = bebob->tx_stream_formations[index].midi;
434         err = amdtp_am824_set_parameters(&bebob->tx_stream, rate,
435                                          pcm_channels, midi_channels * 8,
436                                          false);
437         if (err < 0)
438                 goto end;
439
440         pcm_channels = bebob->rx_stream_formations[index].pcm;
441         midi_channels = bebob->rx_stream_formations[index].midi;
442         err = amdtp_am824_set_parameters(&bebob->rx_stream, rate,
443                                          pcm_channels, midi_channels * 8,
444                                          false);
445         if (err < 0)
446                 goto end;
447
448         /* establish connections for both streams */
449         err = cmp_connection_establish(&bebob->out_conn,
450                         amdtp_stream_get_max_payload(&bebob->tx_stream));
451         if (err < 0)
452                 goto end;
453         err = cmp_connection_establish(&bebob->in_conn,
454                         amdtp_stream_get_max_payload(&bebob->rx_stream));
455         if (err < 0) {
456                 cmp_connection_break(&bebob->out_conn);
457                 goto end;
458         }
459
460         bebob->connected = true;
461 end:
462         return err;
463 }
464
465 static void
466 break_both_connections(struct snd_bebob *bebob)
467 {
468         cmp_connection_break(&bebob->in_conn);
469         cmp_connection_break(&bebob->out_conn);
470
471         bebob->connected = false;
472
473         /* These models seems to be in transition state for a longer time. */
474         if (bebob->maudio_special_quirk != NULL)
475                 msleep(200);
476 }
477
478 static void
479 destroy_both_connections(struct snd_bebob *bebob)
480 {
481         cmp_connection_destroy(&bebob->in_conn);
482         cmp_connection_destroy(&bebob->out_conn);
483 }
484
485 static int
486 get_sync_mode(struct snd_bebob *bebob, enum cip_flags *sync_mode)
487 {
488         enum snd_bebob_clock_type src;
489         int err;
490
491         err = snd_bebob_stream_get_clock_src(bebob, &src);
492         if (err < 0)
493                 return err;
494
495         switch (src) {
496         case SND_BEBOB_CLOCK_TYPE_INTERNAL:
497         case SND_BEBOB_CLOCK_TYPE_EXTERNAL:
498                 *sync_mode = CIP_SYNC_TO_DEVICE;
499                 break;
500         default:
501         case SND_BEBOB_CLOCK_TYPE_SYT:
502                 *sync_mode = 0;
503                 break;
504         }
505
506         return 0;
507 }
508
509 static int
510 start_stream(struct snd_bebob *bebob, struct amdtp_stream *stream,
511              unsigned int rate)
512 {
513         struct cmp_connection *conn;
514         int err = 0;
515
516         if (stream == &bebob->rx_stream)
517                 conn = &bebob->in_conn;
518         else
519                 conn = &bebob->out_conn;
520
521         /* channel mapping */
522         if (bebob->maudio_special_quirk == NULL) {
523                 err = map_data_channels(bebob, stream);
524                 if (err < 0)
525                         goto end;
526         }
527
528         /* start amdtp stream */
529         err = amdtp_stream_start(stream,
530                                  conn->resources.channel,
531                                  conn->speed);
532 end:
533         return err;
534 }
535
536 int snd_bebob_stream_init_duplex(struct snd_bebob *bebob)
537 {
538         int err;
539
540         err = init_both_connections(bebob);
541         if (err < 0)
542                 goto end;
543
544         err = amdtp_am824_init(&bebob->tx_stream, bebob->unit,
545                                AMDTP_IN_STREAM, CIP_BLOCKING);
546         if (err < 0) {
547                 amdtp_stream_destroy(&bebob->tx_stream);
548                 destroy_both_connections(bebob);
549                 goto end;
550         }
551         /* See comments in next function */
552         init_completion(&bebob->bus_reset);
553         bebob->tx_stream.flags |= CIP_SKIP_INIT_DBC_CHECK;
554
555         /*
556          * BeBoB v3 transfers packets with these qurks:
557          *  - In the beginning of streaming, the value of dbc is incremented
558          *    even if no data blocks are transferred.
559          *  - The value of dbc is reset suddenly.
560          */
561         if (bebob->version > 2)
562                 bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC |
563                                           CIP_SKIP_DBC_ZERO_CHECK;
564
565         /*
566          * At high sampling rate, M-Audio special firmware transmits empty
567          * packet with the value of dbc incremented by 8 but the others are
568          * valid to IEC 61883-1.
569          */
570         if (bebob->maudio_special_quirk)
571                 bebob->tx_stream.flags |= CIP_EMPTY_HAS_WRONG_DBC;
572
573         err = amdtp_am824_init(&bebob->rx_stream, bebob->unit,
574                                AMDTP_OUT_STREAM, CIP_BLOCKING);
575         if (err < 0) {
576                 amdtp_stream_destroy(&bebob->tx_stream);
577                 amdtp_stream_destroy(&bebob->rx_stream);
578                 destroy_both_connections(bebob);
579         }
580 end:
581         return err;
582 }
583
584 int snd_bebob_stream_start_duplex(struct snd_bebob *bebob, unsigned int rate)
585 {
586         const struct snd_bebob_rate_spec *rate_spec = bebob->spec->rate;
587         struct amdtp_stream *master, *slave;
588         enum cip_flags sync_mode;
589         unsigned int curr_rate;
590         bool updated = false;
591         int err = 0;
592
593         /*
594          * Normal BeBoB firmware has a quirk at bus reset to transmits packets
595          * with discontinuous value in dbc field.
596          *
597          * This 'struct completion' is used to call .update() at first to update
598          * connections/streams. Next following codes handle streaming error.
599          */
600         if (amdtp_streaming_error(&bebob->tx_stream)) {
601                 if (completion_done(&bebob->bus_reset))
602                         reinit_completion(&bebob->bus_reset);
603
604                 updated = (wait_for_completion_interruptible_timeout(
605                                 &bebob->bus_reset,
606                                 msecs_to_jiffies(FW_ISO_RESOURCE_DELAY)) > 0);
607         }
608
609         mutex_lock(&bebob->mutex);
610
611         /* Need no substreams */
612         if (atomic_read(&bebob->substreams_counter) == 0)
613                 goto end;
614
615         err = get_sync_mode(bebob, &sync_mode);
616         if (err < 0)
617                 goto end;
618         if (sync_mode == CIP_SYNC_TO_DEVICE) {
619                 master = &bebob->tx_stream;
620                 slave  = &bebob->rx_stream;
621         } else {
622                 master = &bebob->rx_stream;
623                 slave  = &bebob->tx_stream;
624         }
625
626         /*
627          * Considering JACK/FFADO streaming:
628          * TODO: This can be removed hwdep functionality becomes popular.
629          */
630         err = check_connection_used_by_others(bebob, master);
631         if (err < 0)
632                 goto end;
633
634         /*
635          * packet queueing error or detecting discontinuity
636          *
637          * At bus reset, connections should not be broken here. So streams need
638          * to be re-started. This is a reason to use SKIP_INIT_DBC_CHECK flag.
639          */
640         if (amdtp_streaming_error(master))
641                 amdtp_stream_stop(master);
642         if (amdtp_streaming_error(slave))
643                 amdtp_stream_stop(slave);
644         if (!updated &&
645             !amdtp_stream_running(master) && !amdtp_stream_running(slave))
646                 break_both_connections(bebob);
647
648         /* stop streams if rate is different */
649         err = rate_spec->get(bebob, &curr_rate);
650         if (err < 0) {
651                 dev_err(&bebob->unit->device,
652                         "fail to get sampling rate: %d\n", err);
653                 goto end;
654         }
655         if (rate == 0)
656                 rate = curr_rate;
657         if (rate != curr_rate) {
658                 amdtp_stream_stop(master);
659                 amdtp_stream_stop(slave);
660                 break_both_connections(bebob);
661         }
662
663         /* master should be always running */
664         if (!amdtp_stream_running(master)) {
665                 amdtp_stream_set_sync(sync_mode, master, slave);
666                 bebob->master = master;
667
668                 /*
669                  * NOTE:
670                  * If establishing connections at first, Yamaha GO46
671                  * (and maybe Terratec X24) don't generate sound.
672                  *
673                  * For firmware customized by M-Audio, refer to next NOTE.
674                  */
675                 if (bebob->maudio_special_quirk == NULL) {
676                         err = rate_spec->set(bebob, rate);
677                         if (err < 0) {
678                                 dev_err(&bebob->unit->device,
679                                         "fail to set sampling rate: %d\n",
680                                         err);
681                                 goto end;
682                         }
683                 }
684
685                 err = make_both_connections(bebob, rate);
686                 if (err < 0)
687                         goto end;
688
689                 err = start_stream(bebob, master, rate);
690                 if (err < 0) {
691                         dev_err(&bebob->unit->device,
692                                 "fail to run AMDTP master stream:%d\n", err);
693                         break_both_connections(bebob);
694                         goto end;
695                 }
696
697                 /*
698                  * NOTE:
699                  * The firmware customized by M-Audio uses these commands to
700                  * start transmitting stream. This is not usual way.
701                  */
702                 if (bebob->maudio_special_quirk != NULL) {
703                         err = rate_spec->set(bebob, rate);
704                         if (err < 0) {
705                                 dev_err(&bebob->unit->device,
706                                         "fail to ensure sampling rate: %d\n",
707                                         err);
708                                 amdtp_stream_stop(master);
709                                 break_both_connections(bebob);
710                                 goto end;
711                         }
712                 }
713
714                 /* wait first callback */
715                 if (!amdtp_stream_wait_callback(master, CALLBACK_TIMEOUT)) {
716                         amdtp_stream_stop(master);
717                         break_both_connections(bebob);
718                         err = -ETIMEDOUT;
719                         goto end;
720                 }
721         }
722
723         /* start slave if needed */
724         if (!amdtp_stream_running(slave)) {
725                 err = start_stream(bebob, slave, rate);
726                 if (err < 0) {
727                         dev_err(&bebob->unit->device,
728                                 "fail to run AMDTP slave stream:%d\n", err);
729                         amdtp_stream_stop(master);
730                         break_both_connections(bebob);
731                         goto end;
732                 }
733
734                 /* wait first callback */
735                 if (!amdtp_stream_wait_callback(slave, CALLBACK_TIMEOUT)) {
736                         amdtp_stream_stop(slave);
737                         amdtp_stream_stop(master);
738                         break_both_connections(bebob);
739                         err = -ETIMEDOUT;
740                 }
741         }
742 end:
743         mutex_unlock(&bebob->mutex);
744         return err;
745 }
746
747 void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob)
748 {
749         struct amdtp_stream *master, *slave;
750
751         if (bebob->master == &bebob->rx_stream) {
752                 slave  = &bebob->tx_stream;
753                 master = &bebob->rx_stream;
754         } else {
755                 slave  = &bebob->rx_stream;
756                 master = &bebob->tx_stream;
757         }
758
759         mutex_lock(&bebob->mutex);
760
761         if (atomic_read(&bebob->substreams_counter) == 0) {
762                 amdtp_stream_pcm_abort(master);
763                 amdtp_stream_stop(master);
764
765                 amdtp_stream_pcm_abort(slave);
766                 amdtp_stream_stop(slave);
767
768                 break_both_connections(bebob);
769         }
770
771         mutex_unlock(&bebob->mutex);
772 }
773
774 void snd_bebob_stream_update_duplex(struct snd_bebob *bebob)
775 {
776         /* vs. XRUN recovery due to discontinuity at bus reset */
777         mutex_lock(&bebob->mutex);
778
779         if ((cmp_connection_update(&bebob->in_conn) < 0) ||
780             (cmp_connection_update(&bebob->out_conn) < 0)) {
781                 amdtp_stream_pcm_abort(&bebob->rx_stream);
782                 amdtp_stream_pcm_abort(&bebob->tx_stream);
783                 amdtp_stream_stop(&bebob->rx_stream);
784                 amdtp_stream_stop(&bebob->tx_stream);
785                 break_both_connections(bebob);
786         } else {
787                 amdtp_stream_update(&bebob->rx_stream);
788                 amdtp_stream_update(&bebob->tx_stream);
789         }
790
791         /* wake up stream_start_duplex() */
792         if (!completion_done(&bebob->bus_reset))
793                 complete_all(&bebob->bus_reset);
794
795         mutex_unlock(&bebob->mutex);
796 }
797
798 /*
799  * This function should be called before starting streams or after stopping
800  * streams.
801  */
802 void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob)
803 {
804         amdtp_stream_destroy(&bebob->rx_stream);
805         amdtp_stream_destroy(&bebob->tx_stream);
806
807         destroy_both_connections(bebob);
808 }
809
810 /*
811  * See: Table 50: Extended Stream Format Info Format Hierarchy Level 2’
812  * in Additional AVC commands (Nov 2003, BridgeCo)
813  * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
814  */
815 static int
816 parse_stream_formation(u8 *buf, unsigned int len,
817                        struct snd_bebob_stream_formation *formation)
818 {
819         unsigned int i, e, channels, format;
820
821         /*
822          * this module can support a hierarchy combination that:
823          *  Root:       Audio and Music (0x90)
824          *  Level 1:    AM824 Compound  (0x40)
825          */
826         if ((buf[0] != 0x90) || (buf[1] != 0x40))
827                 return -ENOSYS;
828
829         /* check sampling rate */
830         for (i = 0; i < ARRAY_SIZE(bridgeco_freq_table); i++) {
831                 if (buf[2] == bridgeco_freq_table[i])
832                         break;
833         }
834         if (i == ARRAY_SIZE(bridgeco_freq_table))
835                 return -ENOSYS;
836
837         /* Avoid double count by different entries for the same rate. */
838         memset(&formation[i], 0, sizeof(struct snd_bebob_stream_formation));
839
840         for (e = 0; e < buf[4]; e++) {
841                 channels = buf[5 + e * 2];
842                 format = buf[6 + e * 2];
843
844                 switch (format) {
845                 /* IEC 60958 Conformant, currently handled as MBLA */
846                 case 0x00:
847                 /* Multi bit linear audio */
848                 case 0x06:      /* Raw */
849                         formation[i].pcm += channels;
850                         break;
851                 /* MIDI Conformant */
852                 case 0x0d:
853                         formation[i].midi += channels;
854                         break;
855                 /* IEC 61937-3 to 7 */
856                 case 0x01:
857                 case 0x02:
858                 case 0x03:
859                 case 0x04:
860                 case 0x05:
861                 /* Multi bit linear audio */
862                 case 0x07:      /* DVD-Audio */
863                 case 0x0c:      /* High Precision */
864                 /* One Bit Audio */
865                 case 0x08:      /* (Plain) Raw */
866                 case 0x09:      /* (Plain) SACD */
867                 case 0x0a:      /* (Encoded) Raw */
868                 case 0x0b:      /* (Encoded) SACD */
869                 /* Synchronization Stream (Stereo Raw audio) */
870                 case 0x40:
871                 /* Don't care */
872                 case 0xff:
873                 default:
874                         return -ENOSYS; /* not supported */
875                 }
876         }
877
878         if (formation[i].pcm  > AM824_MAX_CHANNELS_FOR_PCM ||
879             formation[i].midi > AM824_MAX_CHANNELS_FOR_MIDI)
880                 return -ENOSYS;
881
882         return 0;
883 }
884
885 static int
886 fill_stream_formations(struct snd_bebob *bebob, enum avc_bridgeco_plug_dir dir,
887                        unsigned short pid)
888 {
889         u8 *buf;
890         struct snd_bebob_stream_formation *formations;
891         unsigned int len, eid;
892         u8 addr[AVC_BRIDGECO_ADDR_BYTES];
893         int err;
894
895         buf = kmalloc(FORMAT_MAXIMUM_LENGTH, GFP_KERNEL);
896         if (buf == NULL)
897                 return -ENOMEM;
898
899         if (dir == AVC_BRIDGECO_PLUG_DIR_IN)
900                 formations = bebob->rx_stream_formations;
901         else
902                 formations = bebob->tx_stream_formations;
903
904         for (eid = 0; eid < SND_BEBOB_STRM_FMT_ENTRIES; eid++) {
905                 len = FORMAT_MAXIMUM_LENGTH;
906                 avc_bridgeco_fill_unit_addr(addr, dir,
907                                             AVC_BRIDGECO_PLUG_UNIT_ISOC, pid);
908                 err = avc_bridgeco_get_plug_strm_fmt(bebob->unit, addr, buf,
909                                                      &len, eid);
910                 /* No entries remained. */
911                 if (err == -EINVAL && eid > 0) {
912                         err = 0;
913                         break;
914                 } else if (err < 0) {
915                         dev_err(&bebob->unit->device,
916                         "fail to get stream format %d for isoc %s plug %d:%d\n",
917                                 eid,
918                                 (dir == AVC_BRIDGECO_PLUG_DIR_IN) ? "in" :
919                                                                     "out",
920                                 pid, err);
921                         break;
922                 }
923
924                 err = parse_stream_formation(buf, len, formations);
925                 if (err < 0)
926                         break;
927         }
928
929         kfree(buf);
930         return err;
931 }
932
933 static int
934 seek_msu_sync_input_plug(struct snd_bebob *bebob)
935 {
936         u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
937         unsigned int i;
938         enum avc_bridgeco_plug_type type;
939         int err;
940
941         /* Get the number of Music Sub Unit for both direction. */
942         err = avc_general_get_plug_info(bebob->unit, 0x0c, 0x00, 0x00, plugs);
943         if (err < 0) {
944                 dev_err(&bebob->unit->device,
945                         "fail to get info for MSU in/out plugs: %d\n",
946                         err);
947                 goto end;
948         }
949
950         /* seek destination plugs for 'MSU sync input' */
951         bebob->sync_input_plug = -1;
952         for (i = 0; i < plugs[0]; i++) {
953                 avc_bridgeco_fill_msu_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN, i);
954                 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
955                 if (err < 0) {
956                         dev_err(&bebob->unit->device,
957                                 "fail to get type for MSU in plug %d: %d\n",
958                                 i, err);
959                         goto end;
960                 }
961
962                 if (type == AVC_BRIDGECO_PLUG_TYPE_SYNC) {
963                         bebob->sync_input_plug = i;
964                         break;
965                 }
966         }
967 end:
968         return err;
969 }
970
971 int snd_bebob_stream_discover(struct snd_bebob *bebob)
972 {
973         const struct snd_bebob_clock_spec *clk_spec = bebob->spec->clock;
974         u8 plugs[AVC_PLUG_INFO_BUF_BYTES], addr[AVC_BRIDGECO_ADDR_BYTES];
975         enum avc_bridgeco_plug_type type;
976         unsigned int i;
977         int err;
978
979         /* the number of plugs for isoc in/out, ext in/out  */
980         err = avc_general_get_plug_info(bebob->unit, 0x1f, 0x07, 0x00, plugs);
981         if (err < 0) {
982                 dev_err(&bebob->unit->device,
983                 "fail to get info for isoc/external in/out plugs: %d\n",
984                         err);
985                 goto end;
986         }
987
988         /*
989          * This module supports at least one isoc input plug and one isoc
990          * output plug.
991          */
992         if ((plugs[0] == 0) || (plugs[1] == 0)) {
993                 err = -ENOSYS;
994                 goto end;
995         }
996
997         avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
998                                     AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
999         err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
1000         if (err < 0) {
1001                 dev_err(&bebob->unit->device,
1002                         "fail to get type for isoc in plug 0: %d\n", err);
1003                 goto end;
1004         } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
1005                 err = -ENOSYS;
1006                 goto end;
1007         }
1008         err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_IN, 0);
1009         if (err < 0)
1010                 goto end;
1011
1012         avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
1013                                     AVC_BRIDGECO_PLUG_UNIT_ISOC, 0);
1014         err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
1015         if (err < 0) {
1016                 dev_err(&bebob->unit->device,
1017                         "fail to get type for isoc out plug 0: %d\n", err);
1018                 goto end;
1019         } else if (type != AVC_BRIDGECO_PLUG_TYPE_ISOC) {
1020                 err = -ENOSYS;
1021                 goto end;
1022         }
1023         err = fill_stream_formations(bebob, AVC_BRIDGECO_PLUG_DIR_OUT, 0);
1024         if (err < 0)
1025                 goto end;
1026
1027         /* count external input plugs for MIDI */
1028         bebob->midi_input_ports = 0;
1029         for (i = 0; i < plugs[2]; i++) {
1030                 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_IN,
1031                                             AVC_BRIDGECO_PLUG_UNIT_EXT, i);
1032                 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
1033                 if (err < 0) {
1034                         dev_err(&bebob->unit->device,
1035                         "fail to get type for external in plug %d: %d\n",
1036                                 i, err);
1037                         goto end;
1038                 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
1039                         bebob->midi_input_ports++;
1040                 }
1041         }
1042
1043         /* count external output plugs for MIDI */
1044         bebob->midi_output_ports = 0;
1045         for (i = 0; i < plugs[3]; i++) {
1046                 avc_bridgeco_fill_unit_addr(addr, AVC_BRIDGECO_PLUG_DIR_OUT,
1047                                             AVC_BRIDGECO_PLUG_UNIT_EXT, i);
1048                 err = avc_bridgeco_get_plug_type(bebob->unit, addr, &type);
1049                 if (err < 0) {
1050                         dev_err(&bebob->unit->device,
1051                         "fail to get type for external out plug %d: %d\n",
1052                                 i, err);
1053                         goto end;
1054                 } else if (type == AVC_BRIDGECO_PLUG_TYPE_MIDI) {
1055                         bebob->midi_output_ports++;
1056                 }
1057         }
1058
1059         /* for check source of clock later */
1060         if (!clk_spec)
1061                 err = seek_msu_sync_input_plug(bebob);
1062 end:
1063         return err;
1064 }
1065
1066 void snd_bebob_stream_lock_changed(struct snd_bebob *bebob)
1067 {
1068         bebob->dev_lock_changed = true;
1069         wake_up(&bebob->hwdep_wait);
1070 }
1071
1072 int snd_bebob_stream_lock_try(struct snd_bebob *bebob)
1073 {
1074         int err;
1075
1076         spin_lock_irq(&bebob->lock);
1077
1078         /* user land lock this */
1079         if (bebob->dev_lock_count < 0) {
1080                 err = -EBUSY;
1081                 goto end;
1082         }
1083
1084         /* this is the first time */
1085         if (bebob->dev_lock_count++ == 0)
1086                 snd_bebob_stream_lock_changed(bebob);
1087         err = 0;
1088 end:
1089         spin_unlock_irq(&bebob->lock);
1090         return err;
1091 }
1092
1093 void snd_bebob_stream_lock_release(struct snd_bebob *bebob)
1094 {
1095         spin_lock_irq(&bebob->lock);
1096
1097         if (WARN_ON(bebob->dev_lock_count <= 0))
1098                 goto end;
1099         if (--bebob->dev_lock_count == 0)
1100                 snd_bebob_stream_lock_changed(bebob);
1101 end:
1102         spin_unlock_irq(&bebob->lock);
1103 }