linux/sound/firewire/oxfw/oxfw-stream.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * oxfw_stream.c - a part of driver for OXFW970/971 based devices
   4 *
   5 * Copyright (c) 2014 Takashi Sakamoto
   6 */
   7
   8#include "oxfw.h"
   9#include <linux/delay.h>
  10
  11#define AVC_GENERIC_FRAME_MAXIMUM_BYTES 512
  12#define READY_TIMEOUT_MS        600
  13
  14/*
  15 * According to datasheet of Oxford Semiconductor:
  16 *  OXFW970: 32.0/44.1/48.0/96.0 Khz, 8 audio channels I/O
  17 *  OXFW971: 32.0/44.1/48.0/88.2/96.0/192.0 kHz, 16 audio channels I/O, MIDI I/O
  18 */
  19static const unsigned int oxfw_rate_table[] = {
  20        [0] = 32000,
  21        [1] = 44100,
  22        [2] = 48000,
  23        [3] = 88200,
  24        [4] = 96000,
  25        [5] = 192000,
  26};
  27
  28/*
  29 * See Table 5.7 – Sampling frequency for Multi-bit Audio
  30 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
  31 */
  32static const unsigned int avc_stream_rate_table[] = {
  33        [0] = 0x02,
  34        [1] = 0x03,
  35        [2] = 0x04,
  36        [3] = 0x0a,
  37        [4] = 0x05,
  38        [5] = 0x07,
  39};
  40
  41static int set_rate(struct snd_oxfw *oxfw, unsigned int rate)
  42{
  43        int err;
  44
  45        err = avc_general_set_sig_fmt(oxfw->unit, rate,
  46                                      AVC_GENERAL_PLUG_DIR_IN, 0);
  47        if (err < 0)
  48                goto end;
  49
  50        if (oxfw->has_output)
  51                err = avc_general_set_sig_fmt(oxfw->unit, rate,
  52                                              AVC_GENERAL_PLUG_DIR_OUT, 0);
  53end:
  54        return err;
  55}
  56
  57static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s,
  58                             unsigned int rate, unsigned int pcm_channels)
  59{
  60        u8 **formats;
  61        struct snd_oxfw_stream_formation formation;
  62        enum avc_general_plug_dir dir;
  63        unsigned int len;
  64        int i, err;
  65
  66        if (s == &oxfw->tx_stream) {
  67                formats = oxfw->tx_stream_formats;
  68                dir = AVC_GENERAL_PLUG_DIR_OUT;
  69        } else {
  70                formats = oxfw->rx_stream_formats;
  71                dir = AVC_GENERAL_PLUG_DIR_IN;
  72        }
  73
  74        /* Seek stream format for requirements. */
  75        for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
  76                err = snd_oxfw_stream_parse_format(formats[i], &formation);
  77                if (err < 0)
  78                        return err;
  79
  80                if ((formation.rate == rate) && (formation.pcm == pcm_channels))
  81                        break;
  82        }
  83        if (i == SND_OXFW_STREAM_FORMAT_ENTRIES)
  84                return -EINVAL;
  85
  86        /* If assumed, just change rate. */
  87        if (oxfw->assumed)
  88                return set_rate(oxfw, rate);
  89
  90        /* Calculate format length. */
  91        len = 5 + formats[i][4] * 2;
  92
  93        err = avc_stream_set_format(oxfw->unit, dir, 0, formats[i], len);
  94        if (err < 0)
  95                return err;
  96
  97        /* Some requests just after changing format causes freezing. */
  98        msleep(100);
  99
 100        return 0;
 101}
 102
 103static int start_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
 104{
 105        struct cmp_connection *conn;
 106        int err;
 107
 108        if (stream == &oxfw->rx_stream)
 109                conn = &oxfw->in_conn;
 110        else
 111                conn = &oxfw->out_conn;
 112
 113        err = cmp_connection_establish(conn);
 114        if (err < 0)
 115                return err;
 116
 117        err = amdtp_domain_add_stream(&oxfw->domain, stream,
 118                                      conn->resources.channel, conn->speed);
 119        if (err < 0) {
 120                cmp_connection_break(conn);
 121                return err;
 122        }
 123
 124        return 0;
 125}
 126
 127static int check_connection_used_by_others(struct snd_oxfw *oxfw,
 128                                           struct amdtp_stream *stream)
 129{
 130        struct cmp_connection *conn;
 131        bool used;
 132        int err;
 133
 134        if (stream == &oxfw->tx_stream)
 135                conn = &oxfw->out_conn;
 136        else
 137                conn = &oxfw->in_conn;
 138
 139        err = cmp_connection_check_used(conn, &used);
 140        if ((err >= 0) && used && !amdtp_stream_running(stream)) {
 141                dev_err(&oxfw->unit->device,
 142                        "Connection established by others: %cPCR[%d]\n",
 143                        (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
 144                        conn->pcr_index);
 145                err = -EBUSY;
 146        }
 147
 148        return err;
 149}
 150
 151static int init_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
 152{
 153        struct cmp_connection *conn;
 154        enum cmp_direction c_dir;
 155        enum amdtp_stream_direction s_dir;
 156        unsigned int flags = 0;
 157        int err;
 158
 159        if (!(oxfw->quirks & SND_OXFW_QUIRK_BLOCKING_TRANSMISSION))
 160                flags |= CIP_NONBLOCKING;
 161        else
 162                flags |= CIP_BLOCKING;
 163
 164        // OXFW 970/971 has no function to generate playback timing according to the sequence
 165        // of value in syt field, thus the packet should include NO_INFO value in the field.
 166        // However, some models just ignore data blocks in packet with NO_INFO for audio data
 167        // processing.
 168        if (!(oxfw->quirks & SND_OXFW_QUIRK_IGNORE_NO_INFO_PACKET))
 169                flags |= CIP_UNAWARE_SYT;
 170
 171        if (stream == &oxfw->tx_stream) {
 172                conn = &oxfw->out_conn;
 173                c_dir = CMP_OUTPUT;
 174                s_dir = AMDTP_IN_STREAM;
 175
 176                if (oxfw->quirks & SND_OXFW_QUIRK_JUMBO_PAYLOAD)
 177                        flags |= CIP_JUMBO_PAYLOAD;
 178                if (oxfw->quirks & SND_OXFW_QUIRK_WRONG_DBS)
 179                        flags |= CIP_WRONG_DBS;
 180        } else {
 181                conn = &oxfw->in_conn;
 182                c_dir = CMP_INPUT;
 183                s_dir = AMDTP_OUT_STREAM;
 184        }
 185
 186        err = cmp_connection_init(conn, oxfw->unit, c_dir, 0);
 187        if (err < 0)
 188                return err;
 189
 190        err = amdtp_am824_init(stream, oxfw->unit, s_dir, flags);
 191        if (err < 0) {
 192                cmp_connection_destroy(conn);
 193                return err;
 194        }
 195
 196        return 0;
 197}
 198
 199static int keep_resources(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
 200{
 201        enum avc_general_plug_dir dir;
 202        u8 **formats;
 203        struct snd_oxfw_stream_formation formation;
 204        struct cmp_connection *conn;
 205        int i;
 206        int err;
 207
 208        if (stream == &oxfw->rx_stream) {
 209                dir = AVC_GENERAL_PLUG_DIR_IN;
 210                formats = oxfw->rx_stream_formats;
 211                conn = &oxfw->in_conn;
 212        } else {
 213                dir = AVC_GENERAL_PLUG_DIR_OUT;
 214                formats = oxfw->tx_stream_formats;
 215                conn = &oxfw->out_conn;
 216        }
 217
 218        err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
 219        if (err < 0)
 220                return err;
 221
 222        for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
 223                struct snd_oxfw_stream_formation fmt;
 224
 225                if (formats[i] == NULL)
 226                        break;
 227
 228                err = snd_oxfw_stream_parse_format(formats[i], &fmt);
 229                if (err < 0)
 230                        return err;
 231
 232                if (fmt.rate == formation.rate && fmt.pcm == formation.pcm &&
 233                    fmt.midi == formation.midi)
 234                        break;
 235        }
 236        if (i == SND_OXFW_STREAM_FORMAT_ENTRIES)
 237                return -EINVAL;
 238
 239        // The stream should have one pcm channels at least.
 240        if (formation.pcm == 0)
 241                return -EINVAL;
 242
 243        err = amdtp_am824_set_parameters(stream, formation.rate, formation.pcm,
 244                                         formation.midi * 8, false);
 245        if (err < 0)
 246                return err;
 247
 248        return cmp_connection_reserve(conn, amdtp_stream_get_max_payload(stream));
 249}
 250
 251int snd_oxfw_stream_reserve_duplex(struct snd_oxfw *oxfw,
 252                                   struct amdtp_stream *stream,
 253                                   unsigned int rate, unsigned int pcm_channels,
 254                                   unsigned int frames_per_period,
 255                                   unsigned int frames_per_buffer)
 256{
 257        struct snd_oxfw_stream_formation formation;
 258        enum avc_general_plug_dir dir;
 259        int err;
 260
 261        // Considering JACK/FFADO streaming:
 262        // TODO: This can be removed hwdep functionality becomes popular.
 263        err = check_connection_used_by_others(oxfw, &oxfw->rx_stream);
 264        if (err < 0)
 265                return err;
 266        if (oxfw->has_output) {
 267                err = check_connection_used_by_others(oxfw, &oxfw->tx_stream);
 268                if (err < 0)
 269                        return err;
 270        }
 271
 272        if (stream == &oxfw->tx_stream)
 273                dir = AVC_GENERAL_PLUG_DIR_OUT;
 274        else
 275                dir = AVC_GENERAL_PLUG_DIR_IN;
 276
 277        err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
 278        if (err < 0)
 279                return err;
 280        if (rate == 0) {
 281                rate = formation.rate;
 282                pcm_channels = formation.pcm;
 283        }
 284        if (formation.rate != rate || formation.pcm != pcm_channels) {
 285                amdtp_domain_stop(&oxfw->domain);
 286
 287                cmp_connection_break(&oxfw->in_conn);
 288                cmp_connection_release(&oxfw->in_conn);
 289
 290                if (oxfw->has_output) {
 291                        cmp_connection_break(&oxfw->out_conn);
 292                        cmp_connection_release(&oxfw->out_conn);
 293                }
 294        }
 295
 296        if (oxfw->substreams_count == 0 ||
 297            formation.rate != rate || formation.pcm != pcm_channels) {
 298                err = set_stream_format(oxfw, stream, rate, pcm_channels);
 299                if (err < 0) {
 300                        dev_err(&oxfw->unit->device,
 301                                "fail to set stream format: %d\n", err);
 302                        return err;
 303                }
 304
 305                err = keep_resources(oxfw, &oxfw->rx_stream);
 306                if (err < 0)
 307                        return err;
 308
 309                if (oxfw->has_output) {
 310                        err = keep_resources(oxfw, &oxfw->tx_stream);
 311                        if (err < 0) {
 312                                cmp_connection_release(&oxfw->in_conn);
 313                                return err;
 314                        }
 315                }
 316
 317                err = amdtp_domain_set_events_per_period(&oxfw->domain,
 318                                        frames_per_period, frames_per_buffer);
 319                if (err < 0) {
 320                        cmp_connection_release(&oxfw->in_conn);
 321                        if (oxfw->has_output)
 322                                cmp_connection_release(&oxfw->out_conn);
 323                        return err;
 324                }
 325        }
 326
 327        return 0;
 328}
 329
 330int snd_oxfw_stream_start_duplex(struct snd_oxfw *oxfw)
 331{
 332        int err;
 333
 334        if (oxfw->substreams_count == 0)
 335                return -EIO;
 336
 337        if (amdtp_streaming_error(&oxfw->rx_stream) ||
 338            amdtp_streaming_error(&oxfw->tx_stream)) {
 339                amdtp_domain_stop(&oxfw->domain);
 340
 341                cmp_connection_break(&oxfw->in_conn);
 342                if (oxfw->has_output)
 343                        cmp_connection_break(&oxfw->out_conn);
 344        }
 345
 346        if (!amdtp_stream_running(&oxfw->rx_stream)) {
 347                unsigned int tx_init_skip_cycles = 0;
 348                bool replay_seq = false;
 349
 350                err = start_stream(oxfw, &oxfw->rx_stream);
 351                if (err < 0) {
 352                        dev_err(&oxfw->unit->device,
 353                                "fail to prepare rx stream: %d\n", err);
 354                        goto error;
 355                }
 356
 357                if (oxfw->has_output &&
 358                    !amdtp_stream_running(&oxfw->tx_stream)) {
 359                        err = start_stream(oxfw, &oxfw->tx_stream);
 360                        if (err < 0) {
 361                                dev_err(&oxfw->unit->device,
 362                                        "fail to prepare tx stream: %d\n", err);
 363                                goto error;
 364                        }
 365
 366                        if (oxfw->quirks & SND_OXFW_QUIRK_JUMBO_PAYLOAD) {
 367                                // Just after changing sampling transfer frequency, many cycles are
 368                                // skipped for packet transmission.
 369                                tx_init_skip_cycles = 400;
 370                        } else if (oxfw->quirks & SND_OXFW_QUIRK_VOLUNTARY_RECOVERY) {
 371                                // It takes a bit time for target device to adjust event frequency
 372                                // according to nominal event frequency in isochronous packets from
 373                                // ALSA oxfw driver.
 374                                tx_init_skip_cycles = 4000;
 375                        } else {
 376                                replay_seq = true;
 377                        }
 378                }
 379
 380                // NOTE: The device ignores presentation time expressed by the value of syt field
 381                // of CIP header in received packets. The sequence of the number of data blocks per
 382                // packet is important for media clock recovery.
 383                err = amdtp_domain_start(&oxfw->domain, tx_init_skip_cycles, replay_seq, false);
 384                if (err < 0)
 385                        goto error;
 386
 387                if (!amdtp_domain_wait_ready(&oxfw->domain, READY_TIMEOUT_MS)) {
 388                        err = -ETIMEDOUT;
 389                        goto error;
 390                }
 391        }
 392
 393        return 0;
 394error:
 395        amdtp_domain_stop(&oxfw->domain);
 396
 397        cmp_connection_break(&oxfw->in_conn);
 398        if (oxfw->has_output)
 399                cmp_connection_break(&oxfw->out_conn);
 400
 401        return err;
 402}
 403
 404void snd_oxfw_stream_stop_duplex(struct snd_oxfw *oxfw)
 405{
 406        if (oxfw->substreams_count == 0) {
 407                amdtp_domain_stop(&oxfw->domain);
 408
 409                cmp_connection_break(&oxfw->in_conn);
 410                cmp_connection_release(&oxfw->in_conn);
 411
 412                if (oxfw->has_output) {
 413                        cmp_connection_break(&oxfw->out_conn);
 414                        cmp_connection_release(&oxfw->out_conn);
 415                }
 416        }
 417}
 418
 419static void destroy_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream)
 420{
 421        struct cmp_connection *conn;
 422
 423        if (stream == &oxfw->tx_stream)
 424                conn = &oxfw->out_conn;
 425        else
 426                conn = &oxfw->in_conn;
 427
 428        amdtp_stream_destroy(stream);
 429        cmp_connection_destroy(conn);
 430}
 431
 432int snd_oxfw_stream_init_duplex(struct snd_oxfw *oxfw)
 433{
 434        int err;
 435
 436        err = init_stream(oxfw, &oxfw->rx_stream);
 437        if (err < 0)
 438                return err;
 439
 440        if (oxfw->has_output) {
 441                err = init_stream(oxfw, &oxfw->tx_stream);
 442                if (err < 0) {
 443                        destroy_stream(oxfw, &oxfw->rx_stream);
 444                        return err;
 445                }
 446        }
 447
 448        err = amdtp_domain_init(&oxfw->domain);
 449        if (err < 0) {
 450                destroy_stream(oxfw, &oxfw->rx_stream);
 451                if (oxfw->has_output)
 452                        destroy_stream(oxfw, &oxfw->tx_stream);
 453        }
 454
 455        return err;
 456}
 457
 458// This function should be called before starting the stream or after stopping
 459// the streams.
 460void snd_oxfw_stream_destroy_duplex(struct snd_oxfw *oxfw)
 461{
 462        amdtp_domain_destroy(&oxfw->domain);
 463
 464        destroy_stream(oxfw, &oxfw->rx_stream);
 465
 466        if (oxfw->has_output)
 467                destroy_stream(oxfw, &oxfw->tx_stream);
 468}
 469
 470void snd_oxfw_stream_update_duplex(struct snd_oxfw *oxfw)
 471{
 472        amdtp_domain_stop(&oxfw->domain);
 473
 474        cmp_connection_break(&oxfw->in_conn);
 475
 476        amdtp_stream_pcm_abort(&oxfw->rx_stream);
 477
 478        if (oxfw->has_output) {
 479                cmp_connection_break(&oxfw->out_conn);
 480
 481                amdtp_stream_pcm_abort(&oxfw->tx_stream);
 482        }
 483}
 484
 485int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
 486                                enum avc_general_plug_dir dir,
 487                                struct snd_oxfw_stream_formation *formation)
 488{
 489        u8 *format;
 490        unsigned int len;
 491        int err;
 492
 493        len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
 494        format = kmalloc(len, GFP_KERNEL);
 495        if (format == NULL)
 496                return -ENOMEM;
 497
 498        err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len);
 499        if (err < 0)
 500                goto end;
 501        if (len < 3) {
 502                err = -EIO;
 503                goto end;
 504        }
 505
 506        err = snd_oxfw_stream_parse_format(format, formation);
 507end:
 508        kfree(format);
 509        return err;
 510}
 511
 512/*
 513 * See Table 6.16 - AM824 Stream Format
 514 *     Figure 6.19 - format_information field for AM824 Compound
 515 * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA)
 516 * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005
 517 */
 518int snd_oxfw_stream_parse_format(u8 *format,
 519                                 struct snd_oxfw_stream_formation *formation)
 520{
 521        unsigned int i, e, channels, type;
 522
 523        memset(formation, 0, sizeof(struct snd_oxfw_stream_formation));
 524
 525        /*
 526         * this module can support a hierarchy combination that:
 527         *  Root:       Audio and Music (0x90)
 528         *  Level 1:    AM824 Compound  (0x40)
 529         */
 530        if ((format[0] != 0x90) || (format[1] != 0x40))
 531                return -ENXIO;
 532
 533        /* check the sampling rate */
 534        for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) {
 535                if (format[2] == avc_stream_rate_table[i])
 536                        break;
 537        }
 538        if (i == ARRAY_SIZE(avc_stream_rate_table))
 539                return -ENXIO;
 540
 541        formation->rate = oxfw_rate_table[i];
 542
 543        for (e = 0; e < format[4]; e++) {
 544                channels = format[5 + e * 2];
 545                type = format[6 + e * 2];
 546
 547                switch (type) {
 548                /* IEC 60958 Conformant, currently handled as MBLA */
 549                case 0x00:
 550                /* Multi Bit Linear Audio (Raw) */
 551                case 0x06:
 552                        formation->pcm += channels;
 553                        break;
 554                /* MIDI Conformant */
 555                case 0x0d:
 556                        formation->midi = channels;
 557                        break;
 558                /* IEC 61937-3 to 7 */
 559                case 0x01:
 560                case 0x02:
 561                case 0x03:
 562                case 0x04:
 563                case 0x05:
 564                /* Multi Bit Linear Audio */
 565                case 0x07:      /* DVD-Audio */
 566                case 0x0c:      /* High Precision */
 567                /* One Bit Audio */
 568                case 0x08:      /* (Plain) Raw */
 569                case 0x09:      /* (Plain) SACD */
 570                case 0x0a:      /* (Encoded) Raw */
 571                case 0x0b:      /* (Encoded) SACD */
 572                /* SMPTE Time-Code conformant */
 573                case 0x0e:
 574                /* Sample Count */
 575                case 0x0f:
 576                /* Anciliary Data */
 577                case 0x10:
 578                /* Synchronization Stream (Stereo Raw audio) */
 579                case 0x40:
 580                /* Don't care */
 581                case 0xff:
 582                default:
 583                        return -ENXIO;  /* not supported */
 584                }
 585        }
 586
 587        if (formation->pcm  > AM824_MAX_CHANNELS_FOR_PCM ||
 588            formation->midi > AM824_MAX_CHANNELS_FOR_MIDI)
 589                return -ENXIO;
 590
 591        return 0;
 592}
 593
 594static int
 595assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir,
 596                      unsigned int pid, u8 *buf, unsigned int *len,
 597                      u8 **formats)
 598{
 599        struct snd_oxfw_stream_formation formation;
 600        unsigned int i, eid;
 601        int err;
 602
 603        /* get format at current sampling rate */
 604        err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len);
 605        if (err < 0) {
 606                dev_err(&oxfw->unit->device,
 607                "fail to get current stream format for isoc %s plug %d:%d\n",
 608                        (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
 609                        pid, err);
 610                goto end;
 611        }
 612
 613        /* parse and set stream format */
 614        eid = 0;
 615        err = snd_oxfw_stream_parse_format(buf, &formation);
 616        if (err < 0)
 617                goto end;
 618
 619        formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
 620                                    GFP_KERNEL);
 621        if (!formats[eid]) {
 622                err = -ENOMEM;
 623                goto end;
 624        }
 625
 626        /* apply the format for each available sampling rate */
 627        for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) {
 628                if (formation.rate == oxfw_rate_table[i])
 629                        continue;
 630
 631                err = avc_general_inquiry_sig_fmt(oxfw->unit,
 632                                                  oxfw_rate_table[i],
 633                                                  dir, pid);
 634                if (err < 0)
 635                        continue;
 636
 637                eid++;
 638                formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, *len,
 639                                            GFP_KERNEL);
 640                if (formats[eid] == NULL) {
 641                        err = -ENOMEM;
 642                        goto end;
 643                }
 644                formats[eid][2] = avc_stream_rate_table[i];
 645        }
 646
 647        err = 0;
 648        oxfw->assumed = true;
 649end:
 650        return err;
 651}
 652
 653static int fill_stream_formats(struct snd_oxfw *oxfw,
 654                               enum avc_general_plug_dir dir,
 655                               unsigned short pid)
 656{
 657        u8 *buf, **formats;
 658        unsigned int len, eid = 0;
 659        struct snd_oxfw_stream_formation dummy;
 660        int err;
 661
 662        buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL);
 663        if (buf == NULL)
 664                return -ENOMEM;
 665
 666        if (dir == AVC_GENERAL_PLUG_DIR_OUT)
 667                formats = oxfw->tx_stream_formats;
 668        else
 669                formats = oxfw->rx_stream_formats;
 670
 671        /* get first entry */
 672        len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
 673        err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0);
 674        if (err == -ENXIO) {
 675                /* LIST subfunction is not implemented */
 676                len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
 677                err = assume_stream_formats(oxfw, dir, pid, buf, &len,
 678                                            formats);
 679                goto end;
 680        } else if (err < 0) {
 681                dev_err(&oxfw->unit->device,
 682                        "fail to get stream format %d for isoc %s plug %d:%d\n",
 683                        eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out",
 684                        pid, err);
 685                goto end;
 686        }
 687
 688        /* LIST subfunction is implemented */
 689        while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) {
 690                /* The format is too short. */
 691                if (len < 3) {
 692                        err = -EIO;
 693                        break;
 694                }
 695
 696                /* parse and set stream format */
 697                err = snd_oxfw_stream_parse_format(buf, &dummy);
 698                if (err < 0)
 699                        break;
 700
 701                formats[eid] = devm_kmemdup(&oxfw->card->card_dev, buf, len,
 702                                            GFP_KERNEL);
 703                if (!formats[eid]) {
 704                        err = -ENOMEM;
 705                        break;
 706                }
 707
 708                /* get next entry */
 709                len = AVC_GENERIC_FRAME_MAXIMUM_BYTES;
 710                err = avc_stream_get_format_list(oxfw->unit, dir, 0,
 711                                                 buf, &len, ++eid);
 712                /* No entries remained. */
 713                if (err == -EINVAL) {
 714                        err = 0;
 715                        break;
 716                } else if (err < 0) {
 717                        dev_err(&oxfw->unit->device,
 718                        "fail to get stream format %d for isoc %s plug %d:%d\n",
 719                                eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" :
 720                                                                        "out",
 721                                pid, err);
 722                        break;
 723                }
 724        }
 725end:
 726        kfree(buf);
 727        return err;
 728}
 729
 730int snd_oxfw_stream_discover(struct snd_oxfw *oxfw)
 731{
 732        u8 plugs[AVC_PLUG_INFO_BUF_BYTES];
 733        struct snd_oxfw_stream_formation formation;
 734        u8 *format;
 735        unsigned int i;
 736        int err;
 737
 738        /* the number of plugs for isoc in/out, ext in/out  */
 739        err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs);
 740        if (err < 0) {
 741                dev_err(&oxfw->unit->device,
 742                "fail to get info for isoc/external in/out plugs: %d\n",
 743                        err);
 744                goto end;
 745        } else if ((plugs[0] == 0) && (plugs[1] == 0)) {
 746                err = -ENXIO;
 747                goto end;
 748        }
 749
 750        /* use oPCR[0] if exists */
 751        if (plugs[1] > 0) {
 752                err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0);
 753                if (err < 0) {
 754                        if (err != -ENXIO)
 755                                return err;
 756
 757                        // The oPCR is not available for isoc communication.
 758                        err = 0;
 759                } else {
 760                        for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
 761                                format = oxfw->tx_stream_formats[i];
 762                                if (format == NULL)
 763                                        continue;
 764                                err = snd_oxfw_stream_parse_format(format,
 765                                                                   &formation);
 766                                if (err < 0)
 767                                        continue;
 768
 769                                /* Add one MIDI port. */
 770                                if (formation.midi > 0)
 771                                        oxfw->midi_input_ports = 1;
 772                        }
 773
 774                        oxfw->has_output = true;
 775                }
 776        }
 777
 778        /* use iPCR[0] if exists */
 779        if (plugs[0] > 0) {
 780                err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0);
 781                if (err < 0) {
 782                        if (err != -ENXIO)
 783                                return err;
 784
 785                        // The iPCR is not available for isoc communication.
 786                        err = 0;
 787                } else {
 788                        for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
 789                                format = oxfw->rx_stream_formats[i];
 790                                if (format == NULL)
 791                                        continue;
 792                                err = snd_oxfw_stream_parse_format(format,
 793                                                                   &formation);
 794                                if (err < 0)
 795                                        continue;
 796
 797                                /* Add one MIDI port. */
 798                                if (formation.midi > 0)
 799                                        oxfw->midi_output_ports = 1;
 800                        }
 801
 802                        oxfw->has_input = true;
 803                }
 804        }
 805end:
 806        return err;
 807}
 808
 809void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw)
 810{
 811        oxfw->dev_lock_changed = true;
 812        wake_up(&oxfw->hwdep_wait);
 813}
 814
 815int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw)
 816{
 817        int err;
 818
 819        spin_lock_irq(&oxfw->lock);
 820
 821        /* user land lock this */
 822        if (oxfw->dev_lock_count < 0) {
 823                err = -EBUSY;
 824                goto end;
 825        }
 826
 827        /* this is the first time */
 828        if (oxfw->dev_lock_count++ == 0)
 829                snd_oxfw_stream_lock_changed(oxfw);
 830        err = 0;
 831end:
 832        spin_unlock_irq(&oxfw->lock);
 833        return err;
 834}
 835
 836void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw)
 837{
 838        spin_lock_irq(&oxfw->lock);
 839
 840        if (WARN_ON(oxfw->dev_lock_count <= 0))
 841                goto end;
 842        if (--oxfw->dev_lock_count == 0)
 843                snd_oxfw_stream_lock_changed(oxfw);
 844end:
 845        spin_unlock_irq(&oxfw->lock);
 846}
 847