linux/sound/firewire/fireworks/fireworks_stream.c
<<
>>
Prefs
   1/*
   2 * fireworks_stream.c - a part of driver for Fireworks 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#include "./fireworks.h"
   9
  10#define CALLBACK_TIMEOUT        100
  11
  12static int
  13init_stream(struct snd_efw *efw, struct amdtp_stream *stream)
  14{
  15        struct cmp_connection *conn;
  16        enum cmp_direction c_dir;
  17        enum amdtp_stream_direction s_dir;
  18        int err;
  19
  20        if (stream == &efw->tx_stream) {
  21                conn = &efw->out_conn;
  22                c_dir = CMP_OUTPUT;
  23                s_dir = AMDTP_IN_STREAM;
  24        } else {
  25                conn = &efw->in_conn;
  26                c_dir = CMP_INPUT;
  27                s_dir = AMDTP_OUT_STREAM;
  28        }
  29
  30        err = cmp_connection_init(conn, efw->unit, c_dir, 0);
  31        if (err < 0)
  32                goto end;
  33
  34        err = amdtp_am824_init(stream, efw->unit, s_dir, CIP_BLOCKING);
  35        if (err < 0) {
  36                amdtp_stream_destroy(stream);
  37                cmp_connection_destroy(conn);
  38        }
  39end:
  40        return err;
  41}
  42
  43static void
  44stop_stream(struct snd_efw *efw, struct amdtp_stream *stream)
  45{
  46        amdtp_stream_pcm_abort(stream);
  47        amdtp_stream_stop(stream);
  48
  49        if (stream == &efw->tx_stream)
  50                cmp_connection_break(&efw->out_conn);
  51        else
  52                cmp_connection_break(&efw->in_conn);
  53}
  54
  55static int
  56start_stream(struct snd_efw *efw, struct amdtp_stream *stream,
  57             unsigned int sampling_rate)
  58{
  59        struct cmp_connection *conn;
  60        unsigned int mode, pcm_channels, midi_ports;
  61        int err;
  62
  63        err = snd_efw_get_multiplier_mode(sampling_rate, &mode);
  64        if (err < 0)
  65                goto end;
  66        if (stream == &efw->tx_stream) {
  67                conn = &efw->out_conn;
  68                pcm_channels = efw->pcm_capture_channels[mode];
  69                midi_ports = efw->midi_out_ports;
  70        } else {
  71                conn = &efw->in_conn;
  72                pcm_channels = efw->pcm_playback_channels[mode];
  73                midi_ports = efw->midi_in_ports;
  74        }
  75
  76        err = amdtp_am824_set_parameters(stream, sampling_rate,
  77                                         pcm_channels, midi_ports, false);
  78        if (err < 0)
  79                goto end;
  80
  81        /*  establish connection via CMP */
  82        err = cmp_connection_establish(conn,
  83                                amdtp_stream_get_max_payload(stream));
  84        if (err < 0)
  85                goto end;
  86
  87        /* start amdtp stream */
  88        err = amdtp_stream_start(stream,
  89                                 conn->resources.channel,
  90                                 conn->speed);
  91        if (err < 0) {
  92                stop_stream(efw, stream);
  93                goto end;
  94        }
  95
  96        /* wait first callback */
  97        if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) {
  98                stop_stream(efw, stream);
  99                err = -ETIMEDOUT;
 100        }
 101end:
 102        return err;
 103}
 104
 105/*
 106 * This function should be called before starting the stream or after stopping
 107 * the streams.
 108 */
 109static void
 110destroy_stream(struct snd_efw *efw, struct amdtp_stream *stream)
 111{
 112        struct cmp_connection *conn;
 113
 114        if (stream == &efw->tx_stream)
 115                conn = &efw->out_conn;
 116        else
 117                conn = &efw->in_conn;
 118
 119        amdtp_stream_destroy(stream);
 120        cmp_connection_destroy(conn);
 121}
 122
 123static int
 124check_connection_used_by_others(struct snd_efw *efw, struct amdtp_stream *s)
 125{
 126        struct cmp_connection *conn;
 127        bool used;
 128        int err;
 129
 130        if (s == &efw->tx_stream)
 131                conn = &efw->out_conn;
 132        else
 133                conn = &efw->in_conn;
 134
 135        err = cmp_connection_check_used(conn, &used);
 136        if ((err >= 0) && used && !amdtp_stream_running(s)) {
 137                dev_err(&efw->unit->device,
 138                        "Connection established by others: %cPCR[%d]\n",
 139                        (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
 140                        conn->pcr_index);
 141                err = -EBUSY;
 142        }
 143
 144        return err;
 145}
 146
 147int snd_efw_stream_init_duplex(struct snd_efw *efw)
 148{
 149        int err;
 150
 151        err = init_stream(efw, &efw->tx_stream);
 152        if (err < 0)
 153                goto end;
 154        /* Fireworks transmits NODATA packets with TAG0. */
 155        efw->tx_stream.flags |= CIP_EMPTY_WITH_TAG0;
 156        /* Fireworks has its own meaning for dbc. */
 157        efw->tx_stream.flags |= CIP_DBC_IS_END_EVENT;
 158        /* Fireworks reset dbc at bus reset. */
 159        efw->tx_stream.flags |= CIP_SKIP_DBC_ZERO_CHECK;
 160        /*
 161         * But Recent firmwares starts packets with non-zero dbc.
 162         * Driver version 5.7.6 installs firmware version 5.7.3.
 163         */
 164        if (efw->is_fireworks3 &&
 165            (efw->firmware_version == 0x5070000 ||
 166             efw->firmware_version == 0x5070300 ||
 167             efw->firmware_version == 0x5080000))
 168                efw->tx_stream.tx_first_dbc = 0x02;
 169        /* AudioFire9 always reports wrong dbs. */
 170        if (efw->is_af9)
 171                efw->tx_stream.flags |= CIP_WRONG_DBS;
 172        /* Firmware version 5.5 reports fixed interval for dbc. */
 173        if (efw->firmware_version == 0x5050000)
 174                efw->tx_stream.tx_dbc_interval = 8;
 175
 176        err = init_stream(efw, &efw->rx_stream);
 177        if (err < 0) {
 178                destroy_stream(efw, &efw->tx_stream);
 179                goto end;
 180        }
 181
 182        /* set IEC61883 compliant mode (actually not fully compliant...) */
 183        err = snd_efw_command_set_tx_mode(efw, SND_EFW_TRANSPORT_MODE_IEC61883);
 184        if (err < 0) {
 185                destroy_stream(efw, &efw->tx_stream);
 186                destroy_stream(efw, &efw->rx_stream);
 187        }
 188end:
 189        return err;
 190}
 191
 192int snd_efw_stream_start_duplex(struct snd_efw *efw, unsigned int rate)
 193{
 194        unsigned int curr_rate;
 195        int err = 0;
 196
 197        /* Need no substreams */
 198        if (efw->playback_substreams == 0 && efw->capture_substreams  == 0)
 199                goto end;
 200
 201        /*
 202         * Considering JACK/FFADO streaming:
 203         * TODO: This can be removed hwdep functionality becomes popular.
 204         */
 205        err = check_connection_used_by_others(efw, &efw->rx_stream);
 206        if (err < 0)
 207                goto end;
 208
 209        /* packet queueing error */
 210        if (amdtp_streaming_error(&efw->tx_stream))
 211                stop_stream(efw, &efw->tx_stream);
 212        if (amdtp_streaming_error(&efw->rx_stream))
 213                stop_stream(efw, &efw->rx_stream);
 214
 215        /* stop streams if rate is different */
 216        err = snd_efw_command_get_sampling_rate(efw, &curr_rate);
 217        if (err < 0)
 218                goto end;
 219        if (rate == 0)
 220                rate = curr_rate;
 221        if (rate != curr_rate) {
 222                stop_stream(efw, &efw->tx_stream);
 223                stop_stream(efw, &efw->rx_stream);
 224        }
 225
 226        /* master should be always running */
 227        if (!amdtp_stream_running(&efw->rx_stream)) {
 228                err = snd_efw_command_set_sampling_rate(efw, rate);
 229                if (err < 0)
 230                        goto end;
 231
 232                err = start_stream(efw, &efw->rx_stream, rate);
 233                if (err < 0) {
 234                        dev_err(&efw->unit->device,
 235                                "fail to start AMDTP master stream:%d\n", err);
 236                        goto end;
 237                }
 238        }
 239
 240        /* start slave if needed */
 241        if (efw->capture_substreams > 0 &&
 242            !amdtp_stream_running(&efw->tx_stream)) {
 243                err = start_stream(efw, &efw->tx_stream, rate);
 244                if (err < 0) {
 245                        dev_err(&efw->unit->device,
 246                                "fail to start AMDTP slave stream:%d\n", err);
 247                        stop_stream(efw, &efw->rx_stream);
 248                }
 249        }
 250end:
 251        return err;
 252}
 253
 254void snd_efw_stream_stop_duplex(struct snd_efw *efw)
 255{
 256        if (efw->capture_substreams == 0) {
 257                stop_stream(efw, &efw->tx_stream);
 258
 259                if (efw->playback_substreams == 0)
 260                        stop_stream(efw, &efw->rx_stream);
 261        }
 262}
 263
 264void snd_efw_stream_update_duplex(struct snd_efw *efw)
 265{
 266        if (cmp_connection_update(&efw->out_conn) < 0 ||
 267            cmp_connection_update(&efw->in_conn) < 0) {
 268                stop_stream(efw, &efw->rx_stream);
 269                stop_stream(efw, &efw->tx_stream);
 270        } else {
 271                amdtp_stream_update(&efw->rx_stream);
 272                amdtp_stream_update(&efw->tx_stream);
 273        }
 274}
 275
 276void snd_efw_stream_destroy_duplex(struct snd_efw *efw)
 277{
 278        destroy_stream(efw, &efw->rx_stream);
 279        destroy_stream(efw, &efw->tx_stream);
 280}
 281
 282void snd_efw_stream_lock_changed(struct snd_efw *efw)
 283{
 284        efw->dev_lock_changed = true;
 285        wake_up(&efw->hwdep_wait);
 286}
 287
 288int snd_efw_stream_lock_try(struct snd_efw *efw)
 289{
 290        int err;
 291
 292        spin_lock_irq(&efw->lock);
 293
 294        /* user land lock this */
 295        if (efw->dev_lock_count < 0) {
 296                err = -EBUSY;
 297                goto end;
 298        }
 299
 300        /* this is the first time */
 301        if (efw->dev_lock_count++ == 0)
 302                snd_efw_stream_lock_changed(efw);
 303        err = 0;
 304end:
 305        spin_unlock_irq(&efw->lock);
 306        return err;
 307}
 308
 309void snd_efw_stream_lock_release(struct snd_efw *efw)
 310{
 311        spin_lock_irq(&efw->lock);
 312
 313        if (WARN_ON(efw->dev_lock_count <= 0))
 314                goto end;
 315        if (--efw->dev_lock_count == 0)
 316                snd_efw_stream_lock_changed(efw);
 317end:
 318        spin_unlock_irq(&efw->lock);
 319}
 320