Browse Source

ALSA: add LaCie FireWire Speakers/Griffin FireWave Surround driver

Add a driver for two playback-only FireWire devices based on the OXFW970
chip.

v2: better AMDTP API abstraction; fix fw_unit leak; small fixes
v3: cache the iPCR value
v4: FireWave constraints; fix fw_device reference counting;
    fix PCR caching; small changes and fixes
v5: volume/mute support; fix crashing due to pcm stop races
v6: fix build; one-channel volume for LaCie
v7: use signed values to make volume (range checks) work; fix function
    block IDs for volume/mute; always use channel 0 for LaCie volume

Signed-off-by: Clemens Ladisch <clemens@ladisch.de>
Acked-by: Stefan Richter <stefanr@s5r6.in-berlin.de>
Tested-by: Jay Fenlason <fenlason@redhat.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
imx_3.10.17_1.0.0_ga
Clemens Ladisch 10 years ago
committed by Takashi Iwai
parent
commit
31ef9134eb
20 changed files with 2651 additions and 4 deletions
  1. +1
    -0
      drivers/firewire/core-iso.c
  2. +0
    -3
      drivers/firewire/core.h
  3. +7
    -0
      include/linux/firewire.h
  4. +2
    -0
      sound/Kconfig
  5. +1
    -1
      sound/Makefile
  6. +25
    -0
      sound/firewire/Kconfig
  7. +6
    -0
      sound/firewire/Makefile
  8. +549
    -0
      sound/firewire/amdtp.c
  9. +157
    -0
      sound/firewire/amdtp.h
  10. +305
    -0
      sound/firewire/cmp.c
  11. +41
    -0
      sound/firewire/cmp.h
  12. +223
    -0
      sound/firewire/fcp.c
  13. +12
    -0
      sound/firewire/fcp.h
  14. +224
    -0
      sound/firewire/iso-resources.c
  15. +39
    -0
      sound/firewire/iso-resources.h
  16. +85
    -0
      sound/firewire/lib.c
  17. +19
    -0
      sound/firewire/lib.h
  18. +74
    -0
      sound/firewire/packets-buffer.c
  19. +26
    -0
      sound/firewire/packets-buffer.h
  20. +855
    -0
      sound/firewire/speakers.c

+ 1
- 0
drivers/firewire/core-iso.c View File

@ -362,3 +362,4 @@ void fw_iso_resource_manage(struct fw_card *card, int generation,
*channel = ret;
}
}
EXPORT_SYMBOL(fw_iso_resource_manage);

+ 0
- 3
drivers/firewire/core.h View File

@ -147,9 +147,6 @@ void fw_node_event(struct fw_card *card, struct fw_node *node, int event);
/* -iso */
int fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma);
void fw_iso_resource_manage(struct fw_card *card, int generation,
u64 channels_mask, int *channel, int *bandwidth,
bool allocate, __be32 buffer[2]);
/* -topology */


+ 7
- 0
include/linux/firewire.h View File

@ -42,6 +42,10 @@
#define CSR_BROADCAST_CHANNEL 0x234
#define CSR_CONFIG_ROM 0x400
#define CSR_CONFIG_ROM_END 0x800
#define CSR_OMPR 0x900
#define CSR_OPCR(i) (0x904 + (i) * 4)
#define CSR_IMPR 0x980
#define CSR_IPCR(i) (0x984 + (i) * 4)
#define CSR_FCP_COMMAND 0xB00
#define CSR_FCP_RESPONSE 0xD00
#define CSR_FCP_END 0xF00
@ -441,5 +445,8 @@ int fw_iso_context_start(struct fw_iso_context *ctx,
int cycle, int sync, int tags);
int fw_iso_context_stop(struct fw_iso_context *ctx);
void fw_iso_context_destroy(struct fw_iso_context *ctx);
void fw_iso_resource_manage(struct fw_card *card, int generation,
u64 channels_mask, int *channel, int *bandwidth,
bool allocate, __be32 buffer[2]);
#endif /* _LINUX_FIREWIRE_H */

+ 2
- 0
sound/Kconfig View File

@ -97,6 +97,8 @@ source "sound/sh/Kconfig"
# here assuming USB is defined before ALSA
source "sound/usb/Kconfig"
source "sound/firewire/Kconfig"
# the following will depend on the order of config.
# here assuming PCMCIA is defined before ALSA
source "sound/pcmcia/Kconfig"


+ 1
- 1
sound/Makefile View File

@ -6,7 +6,7 @@ obj-$(CONFIG_SOUND_PRIME) += sound_firmware.o
obj-$(CONFIG_SOUND_PRIME) += oss/
obj-$(CONFIG_DMASOUND) += oss/
obj-$(CONFIG_SND) += core/ i2c/ drivers/ isa/ pci/ ppc/ arm/ sh/ synth/ usb/ \
sparc/ spi/ parisc/ pcmcia/ mips/ soc/ atmel/
firewire/ sparc/ spi/ parisc/ pcmcia/ mips/ soc/ atmel/
obj-$(CONFIG_SND_AOA) += aoa/
# This one must be compilable even if sound is configured out


+ 25
- 0
sound/firewire/Kconfig View File

@ -0,0 +1,25 @@
menuconfig SND_FIREWIRE
bool "FireWire sound devices"
depends on FIREWIRE
default y
help
Support for IEEE-1394/FireWire/iLink sound devices.
if SND_FIREWIRE && FIREWIRE
config SND_FIREWIRE_LIB
tristate
depends on SND_PCM
config SND_FIREWIRE_SPEAKERS
tristate "FireWire speakers"
select SND_PCM
select SND_FIREWIRE_LIB
help
Say Y here to include support for the Griffin FireWave Surround
and the LaCie FireWire Speakers.
To compile this driver as a module, choose M here: the module
will be called snd-firewire-speakers.
endif # SND_FIREWIRE

+ 6
- 0
sound/firewire/Makefile View File

@ -0,0 +1,6 @@
snd-firewire-lib-objs := lib.o iso-resources.o packets-buffer.o \
fcp.o cmp.o amdtp.o
snd-firewire-speakers-objs := speakers.o
obj-$(CONFIG_SND_FIREWIRE_LIB) += snd-firewire-lib.o
obj-$(CONFIG_SND_FIREWIRE_SPEAKERS) += snd-firewire-speakers.o

+ 549
- 0
sound/firewire/amdtp.c View File

@ -0,0 +1,549 @@
/*
* Audio and Music Data Transmission Protocol (IEC 61883-6) streams
* with Common Isochronous Packet (IEC 61883-1) headers
*
* Copyright (c) Clemens Ladisch <clemens@ladisch.de>
* Licensed under the terms of the GNU General Public License, version 2.
*/
#include <linux/device.h>
#include <linux/err.h>
#include <linux/firewire.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <sound/pcm.h>
#include "amdtp.h"
#define TICKS_PER_CYCLE 3072
#define CYCLES_PER_SECOND 8000
#define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 µs */
#define TAG_CIP 1
#define CIP_EOH (1u << 31)
#define CIP_FMT_AM (0x10 << 24)
#define AMDTP_FDF_AM824 (0 << 19)
#define AMDTP_FDF_SFC_SHIFT 16
/* TODO: make these configurable */
#define INTERRUPT_INTERVAL 16
#define QUEUE_LENGTH 48
/**
* amdtp_out_stream_init - initialize an AMDTP output stream structure
* @s: the AMDTP output stream to initialize
* @unit: the target of the stream
* @flags: the packet transmission method to use
*/
int amdtp_out_stream_init(struct amdtp_out_stream *s, struct fw_unit *unit,
enum cip_out_flags flags)
{
if (flags != CIP_NONBLOCKING)
return -EINVAL;
s->unit = fw_unit_get(unit);
s->flags = flags;
s->context = ERR_PTR(-1);
mutex_init(&s->mutex);
return 0;
}
EXPORT_SYMBOL(amdtp_out_stream_init);
/**
* amdtp_out_stream_destroy - free stream resources
* @s: the AMDTP output stream to destroy
*/
void amdtp_out_stream_destroy(struct amdtp_out_stream *s)
{
WARN_ON(!IS_ERR(s->context));
mutex_destroy(&s->mutex);
fw_unit_put(s->unit);
}
EXPORT_SYMBOL(amdtp_out_stream_destroy);
/**
* amdtp_out_stream_set_rate - set the sample rate
* @s: the AMDTP output stream to configure
* @rate: the sample rate
*
* The sample rate must be set before the stream is started, and must not be
* changed while the stream is running.
*/
void amdtp_out_stream_set_rate(struct amdtp_out_stream *s, unsigned int rate)
{
static const struct {
unsigned int rate;
unsigned int syt_interval;
} rate_info[] = {
[CIP_SFC_32000] = { 32000, 8, },
[CIP_SFC_44100] = { 44100, 8, },
[CIP_SFC_48000] = { 48000, 8, },
[CIP_SFC_88200] = { 88200, 16, },
[CIP_SFC_96000] = { 96000, 16, },
[CIP_SFC_176400] = { 176400, 32, },
[CIP_SFC_192000] = { 192000, 32, },
};
unsigned int sfc;
if (WARN_ON(!IS_ERR(s->context)))
return;
for (sfc = 0; sfc < ARRAY_SIZE(rate_info); ++sfc)
if (rate_info[sfc].rate == rate) {
s->sfc = sfc;
s->syt_interval = rate_info[sfc].syt_interval;
return;
}
WARN_ON(1);
}
EXPORT_SYMBOL(amdtp_out_stream_set_rate);
/**
* amdtp_out_stream_get_max_payload - get the stream's packet size
* @s: the AMDTP output stream
*
* This function must not be called before the stream has been configured
* with amdtp_out_stream_set_hw_params(), amdtp_out_stream_set_pcm(), and
* amdtp_out_stream_set_midi().
*/
unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream *s)
{
static const unsigned int max_data_blocks[] = {
[CIP_SFC_32000] = 4,
[CIP_SFC_44100] = 6,
[CIP_SFC_48000] = 6,
[CIP_SFC_88200] = 12,
[CIP_SFC_96000] = 12,
[CIP_SFC_176400] = 23,
[CIP_SFC_192000] = 24,
};
s->data_block_quadlets = s->pcm_channels;
s->data_block_quadlets += DIV_ROUND_UP(s->midi_ports, 8);
return 8 + max_data_blocks[s->sfc] * 4 * s->data_block_quadlets;
}
EXPORT_SYMBOL(amdtp_out_stream_get_max_payload);
static void amdtp_write_s16(struct amdtp_out_stream *s,
struct snd_pcm_substream *pcm,
__be32 *buffer, unsigned int frames);
static void amdtp_write_s32(struct amdtp_out_stream *s,
struct snd_pcm_substream *pcm,
__be32 *buffer, unsigned int frames);
/**
* amdtp_out_stream_set_pcm_format - set the PCM format
* @s: the AMDTP output stream to configure
* @format: the format of the ALSA PCM device
*
* The sample format must be set before the stream is started, and must not be
* changed while the stream is running.
*/
void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s,
snd_pcm_format_t format)
{
if (WARN_ON(!IS_ERR(s->context)))
return;
switch (format) {
default:
WARN_ON(1);
/* fall through */
case SNDRV_PCM_FORMAT_S16:
s->transfer_samples = amdtp_write_s16;
break;
case SNDRV_PCM_FORMAT_S32:
s->transfer_samples = amdtp_write_s32;
break;
}
}
EXPORT_SYMBOL(amdtp_out_stream_set_pcm_format);
static unsigned int calculate_data_blocks(struct amdtp_out_stream *s)
{
unsigned int phase, data_blocks;
if (!cip_sfc_is_base_44100(s->sfc)) {
/* Sample_rate / 8000 is an integer, and precomputed. */
data_blocks = s->data_block_state;
} else {
phase = s->data_block_state;
/*
* This calculates the number of data blocks per packet so that
* 1) the overall rate is correct and exactly synchronized to
* the bus clock, and
* 2) packets with a rounded-up number of blocks occur as early
* as possible in the sequence (to prevent underruns of the
* device's buffer).
*/
if (s->sfc == CIP_SFC_44100)
/* 6 6 5 6 5 6 5 ... */
data_blocks = 5 + ((phase & 1) ^
(phase == 0 || phase >= 40));
else
/* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
if (++phase >= (80 >> (s->sfc >> 1)))
phase = 0;
s->data_block_state = phase;
}
return data_blocks;
}
static unsigned int calculate_syt(struct amdtp_out_stream *s,
unsigned int cycle)
{
unsigned int syt_offset, phase, index, syt;
if (s->last_syt_offset < TICKS_PER_CYCLE) {
if (!cip_sfc_is_base_44100(s->sfc))
syt_offset = s->last_syt_offset + s->syt_offset_state;
else {
/*
* The time, in ticks, of the n'th SYT_INTERVAL sample is:
* n * SYT_INTERVAL * 24576000 / sample_rate
* Modulo TICKS_PER_CYCLE, the difference between successive
* elements is about 1386.23. Rounding the results of this
* formula to the SYT precision results in a sequence of
* differences that begins with:
* 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
* This code generates _exactly_ the same sequence.
*/
phase = s->syt_offset_state;
index = phase % 13;
syt_offset = s->last_syt_offset;
syt_offset += 1386 + ((index && !(index & 3)) ||
phase == 146);
if (++phase >= 147)
phase = 0;
s->syt_offset_state = phase;
}
} else
syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
s->last_syt_offset = syt_offset;
syt_offset += TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
syt += syt_offset % TICKS_PER_CYCLE;
return syt & 0xffff;
}
static void amdtp_write_s32(struct amdtp_out_stream *s,
struct snd_pcm_substream *pcm,
__be32 *buffer, unsigned int frames)
{
struct snd_pcm_runtime *runtime = pcm->runtime;
unsigned int channels, remaining_frames, frame_step, i, c;
const u32 *src;
channels = s->pcm_channels;
src = (void *)runtime->dma_area +
s->pcm_buffer_pointer * (runtime->frame_bits / 8);
remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
frame_step = s->data_block_quadlets - channels;
for (i = 0; i < frames; ++i) {
for (c = 0; c < channels; ++c) {
*buffer = cpu_to_be32((*src >> 8) | 0x40000000);
src++;
buffer++;
}
buffer += frame_step;
if (--remaining_frames == 0)
src = (void *)runtime->dma_area;
}
}
static void amdtp_write_s16(struct amdtp_out_stream *s,
struct snd_pcm_substream *pcm,
__be32 *buffer, unsigned int frames)
{
struct snd_pcm_runtime *runtime = pcm->runtime;
unsigned int channels, remaining_frames, frame_step, i, c;
const u16 *src;
channels = s->pcm_channels;
src = (void *)runtime->dma_area +
s->pcm_buffer_pointer * (runtime->frame_bits / 8);
remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
frame_step = s->data_block_quadlets - channels;
for (i = 0; i < frames; ++i) {
for (c = 0; c < channels; ++c) {
*buffer = cpu_to_be32((*src << 8) | 0x40000000);
src++;
buffer++;
}
buffer += frame_step;
if (--remaining_frames == 0)
src = (void *)runtime->dma_area;
}
}
static void amdtp_fill_pcm_silence(struct amdtp_out_stream *s,
__be32 *buffer, unsigned int frames)
{
unsigned int i, c;
for (i = 0; i < frames; ++i) {
for (c = 0; c < s->pcm_channels; ++c)
buffer[c] = cpu_to_be32(0x40000000);
buffer += s->data_block_quadlets;
}
}
static void amdtp_fill_midi(struct amdtp_out_stream *s,
__be32 *buffer, unsigned int frames)
{
unsigned int i;
for (i = 0; i < frames; ++i)
buffer[s->pcm_channels + i * s->data_block_quadlets] =
cpu_to_be32(0x80000000);
}
static void queue_out_packet(struct amdtp_out_stream *s, unsigned int cycle)
{
__be32 *buffer;
unsigned int data_blocks, syt, ptr;
struct snd_pcm_substream *pcm;
struct fw_iso_packet packet;
int err;
data_blocks = calculate_data_blocks(s);
syt = calculate_syt(s, cycle);
buffer = s->buffer.packets[s->packet_counter].buffer;
buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
(s->data_block_quadlets << 16) |
s->data_block_counter);
buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
(s->sfc << AMDTP_FDF_SFC_SHIFT) | syt);
buffer += 2;
pcm = ACCESS_ONCE(s->pcm);
if (pcm)
s->transfer_samples(s, pcm, buffer, data_blocks);
else
amdtp_fill_pcm_silence(s, buffer, data_blocks);
if (s->midi_ports)
amdtp_fill_midi(s, buffer, data_blocks);
s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
packet.payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
packet.interrupt = IS_ALIGNED(s->packet_counter + 1,
INTERRUPT_INTERVAL);
packet.skip = 0;
packet.tag = TAG_CIP;
packet.sy = 0;
packet.header_length = 0;
err = fw_iso_context_queue(s->context, &packet, &s->buffer.iso_buffer,
s->buffer.packets[s->packet_counter].offset);
if (err < 0)
dev_err(&s->unit->device, "queueing error: %d\n", err);
if (++s->packet_counter >= QUEUE_LENGTH)
s->packet_counter = 0;
if (pcm) {
ptr = s->pcm_buffer_pointer + data_blocks;
if (ptr >= pcm->runtime->buffer_size)
ptr -= pcm->runtime->buffer_size;
ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
s->pcm_period_pointer += data_blocks;
if (s->pcm_period_pointer >= pcm->runtime->period_size) {
s->pcm_period_pointer -= pcm->runtime->period_size;
snd_pcm_period_elapsed(pcm);
}
}
}
static void out_packet_callback(struct fw_iso_context *context, u32 cycle,
size_t header_length, void *header, void *data)
{
struct amdtp_out_stream *s = data;
unsigned int i, packets = header_length / 4;
/*
* Compute the cycle of the last queued packet.
* (We need only the four lowest bits for the SYT, so we can ignore
* that bits 0-11 must wrap around at 3072.)
*/
cycle += QUEUE_LENGTH - packets;
for (i = 0; i < packets; ++i)
queue_out_packet(s, ++cycle);
}
static int queue_initial_skip_packets(struct amdtp_out_stream *s)
{
struct fw_iso_packet skip_packet = {
.skip = 1,
};
unsigned int i;
int err;
for (i = 0; i < QUEUE_LENGTH; ++i) {
skip_packet.interrupt = IS_ALIGNED(s->packet_counter + 1,
INTERRUPT_INTERVAL);
err = fw_iso_context_queue(s->context, &skip_packet, NULL, 0);
if (err < 0)
return err;
if (++s->packet_counter >= QUEUE_LENGTH)
s->packet_counter = 0;
}
return 0;
}
/**
* amdtp_out_stream_start - start sending packets
* @s: the AMDTP output stream to start
* @channel: the isochronous channel on the bus
* @speed: firewire speed code
*
* The stream cannot be started until it has been configured with
* amdtp_out_stream_set_hw_params(), amdtp_out_stream_set_pcm(), and
* amdtp_out_stream_set_midi(); and it must be started before any
* PCM or MIDI device can be started.
*/
int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed)
{
static const struct {
unsigned int data_block;
unsigned int syt_offset;
} initial_state[] = {
[CIP_SFC_32000] = { 4, 3072 },
[CIP_SFC_48000] = { 6, 1024 },
[CIP_SFC_96000] = { 12, 1024 },
[CIP_SFC_192000] = { 24, 1024 },
[CIP_SFC_44100] = { 0, 67 },
[CIP_SFC_88200] = { 0, 67 },
[CIP_SFC_176400] = { 0, 67 },
};
int err;
mutex_lock(&s->mutex);
if (WARN_ON(!IS_ERR(s->context) ||
(!s->pcm_channels && !s->midi_ports))) {
err = -EBADFD;
goto err_unlock;
}
s->data_block_state = initial_state[s->sfc].data_block;
s->syt_offset_state = initial_state[s->sfc].syt_offset;
s->last_syt_offset = TICKS_PER_CYCLE;
err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
amdtp_out_stream_get_max_payload(s),
DMA_TO_DEVICE);
if (err < 0)
goto err_unlock;
s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
FW_ISO_CONTEXT_TRANSMIT,
channel, speed, 0,
out_packet_callback, s);
if (IS_ERR(s->context)) {
err = PTR_ERR(s->context);
if (err == -EBUSY)
dev_err(&s->unit->device,
"no free output stream on this controller\n");
goto err_buffer;
}
amdtp_out_stream_update(s);
s->packet_counter = 0;
s->data_block_counter = 0;
err = queue_initial_skip_packets(s);
if (err < 0)
goto err_context;
err = fw_iso_context_start(s->context, -1, 0, 0);
if (err < 0)
goto err_context;
mutex_unlock(&s->mutex);
return 0;
err_context:
fw_iso_context_destroy(s->context);
s->context = ERR_PTR(-1);
err_buffer:
iso_packets_buffer_destroy(&s->buffer, s->unit);
err_unlock:
mutex_unlock(&s->mutex);
return err;
}
EXPORT_SYMBOL(amdtp_out_stream_start);
/**
* amdtp_out_stream_update - update the stream after a bus reset
* @s: the AMDTP output stream
*/
void amdtp_out_stream_update(struct amdtp_out_stream *s)
{
ACCESS_ONCE(s->source_node_id_field) =
(fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
}
EXPORT_SYMBOL(amdtp_out_stream_update);
/**
* amdtp_out_stream_stop - stop sending packets
* @s: the AMDTP output stream to stop
*
* All PCM and MIDI devices of the stream must be stopped before the stream
* itself can be stopped.
*/
void amdtp_out_stream_stop(struct amdtp_out_stream *s)
{
mutex_lock(&s->mutex);
if (IS_ERR(s->context)) {
mutex_unlock(&s->mutex);
return;
}
fw_iso_context_stop(s->context);
fw_iso_context_destroy(s->context);
s->context = ERR_PTR(-1);
iso_packets_buffer_destroy(&s->buffer, s->unit);
mutex_unlock(&s->mutex);
}
EXPORT_SYMBOL(amdtp_out_stream_stop);
/**
* amdtp_out_stream_pcm_abort - abort the running PCM device
* @s: the AMDTP stream about to be stopped
*
* If the isochronous stream needs to be stopped asynchronously, call this
* function first to stop the PCM device.
*/
void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s)
{
struct snd_pcm_substream *pcm;
pcm = ACCESS_ONCE(s->pcm);
if (pcm) {
snd_pcm_stream_lock_irq(pcm);
if (snd_pcm_running(pcm))
snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
snd_pcm_stream_unlock_irq(pcm);
}
}
EXPORT_SYMBOL(amdtp_out_stream_pcm_abort);

+ 157
- 0
sound/firewire/amdtp.h View File

@ -0,0 +1,157 @@
#ifndef SOUND_FIREWIRE_AMDTP_H_INCLUDED
#define SOUND_FIREWIRE_AMDTP_H_INCLUDED
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include "packets-buffer.h"
/**
* enum cip_out_flags - describes details of the streaming protocol
* @CIP_NONBLOCKING: In non-blocking mode, each packet contains
* sample_rate/8000 samples, with rounding up or down to adjust
* for clock skew and left-over fractional samples. This should
* be used if supported by the device.
*/
enum cip_out_flags {
CIP_NONBLOCKING = 0,
};
/**
* enum cip_sfc - a stream's sample rate
*/
enum cip_sfc {
CIP_SFC_32000 = 0,
CIP_SFC_44100 = 1,
CIP_SFC_48000 = 2,
CIP_SFC_88200 = 3,
CIP_SFC_96000 = 4,
CIP_SFC_176400 = 5,
CIP_SFC_192000 = 6,
};
#define AMDTP_OUT_PCM_FORMAT_BITS (SNDRV_PCM_FMTBIT_S16 | \
SNDRV_PCM_FMTBIT_S32)
struct fw_unit;
struct fw_iso_context;
struct snd_pcm_substream;
struct amdtp_out_stream {
struct fw_unit *unit;
enum cip_out_flags flags;
struct fw_iso_context *context;
struct mutex mutex;
enum cip_sfc sfc;
unsigned int data_block_quadlets;
unsigned int pcm_channels;
unsigned int midi_ports;
void (*transfer_samples)(struct amdtp_out_stream *s,
struct snd_pcm_substream *pcm,
__be32 *buffer, unsigned int frames);
unsigned int syt_interval;
unsigned int source_node_id_field;
struct iso_packets_buffer buffer;
struct snd_pcm_substream *pcm;
unsigned int packet_counter;
unsigned int data_block_counter;
unsigned int data_block_state;
unsigned int last_syt_offset;
unsigned int syt_offset_state;
unsigned int pcm_buffer_pointer;
unsigned int pcm_period_pointer;
};
int amdtp_out_stream_init(struct amdtp_out_stream *s, struct fw_unit *unit,
enum cip_out_flags flags);
void amdtp_out_stream_destroy(struct amdtp_out_stream *s);
void amdtp_out_stream_set_rate(struct amdtp_out_stream *s, unsigned int rate);
unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream *s);
int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed);
void amdtp_out_stream_update(struct amdtp_out_stream *s);
void amdtp_out_stream_stop(struct amdtp_out_stream *s);
void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s,
snd_pcm_format_t format);
void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s);
/**
* amdtp_out_stream_set_pcm - configure format of PCM samples
* @s: the AMDTP output stream to be configured
* @pcm_channels: the number of PCM samples in each data block, to be encoded
* as AM824 multi-bit linear audio
*
* This function must not be called while the stream is running.
*/
static inline void amdtp_out_stream_set_pcm(struct amdtp_out_stream *s,
unsigned int pcm_channels)
{
s->pcm_channels = pcm_channels;
}
/**
* amdtp_out_stream_set_midi - configure format of MIDI data
* @s: the AMDTP output stream to be configured
* @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
*
* This function must not be called while the stream is running.
*/
static inline void amdtp_out_stream_set_midi(struct amdtp_out_stream *s,
unsigned int midi_ports)
{
s->midi_ports = midi_ports;
}
/**
* amdtp_out_stream_pcm_prepare - prepare PCM device for running
* @s: the AMDTP output stream
*
* This function should be called from the PCM device's .prepare callback.
*/
static inline void amdtp_out_stream_pcm_prepare(struct amdtp_out_stream *s)
{
s->pcm_buffer_pointer = 0;
s->pcm_period_pointer = 0;
}
/**
* amdtp_out_stream_pcm_trigger - start/stop playback from a PCM device
* @s: the AMDTP output stream
* @pcm: the PCM device to be started, or %NULL to stop the current device
*
* Call this function on a running isochronous stream to enable the actual
* transmission of PCM data. This function should be called from the PCM
* device's .trigger callback.
*/
static inline void amdtp_out_stream_pcm_trigger(struct amdtp_out_stream *s,
struct snd_pcm_substream *pcm)
{
ACCESS_ONCE(s->pcm) = pcm;
}
/**
* amdtp_out_stream_pcm_pointer - get the PCM buffer position
* @s: the AMDTP output stream that transports the PCM data
*
* Returns the current buffer position, in frames.
*/
static inline unsigned long
amdtp_out_stream_pcm_pointer(struct amdtp_out_stream *s)
{
return ACCESS_ONCE(s->pcm_buffer_pointer);
}
static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
{
return sfc & 1;
}
#endif

+ 305
- 0
sound/firewire/cmp.c View File

@ -0,0 +1,305 @@
/*
* Connection Management Procedures (IEC 61883-1) helper functions
*
* Copyright (c) Clemens Ladisch <clemens@ladisch.de>
* Licensed under the terms of the GNU General Public License, version 2.
*/
#include <linux/device.h>
#include <linux/firewire.h>
#include <linux/firewire-constants.h>
#include <linux/module.h>
#include <linux/sched.h>
#include "lib.h"
#include "iso-resources.h"
#include "cmp.h"
#define IMPR_SPEED_MASK 0xc0000000
#define IMPR_SPEED_SHIFT 30
#define IMPR_XSPEED_MASK 0x00000060
#define IMPR_XSPEED_SHIFT 5
#define IMPR_PLUGS_MASK 0x0000001f
#define IPCR_ONLINE 0x80000000
#define IPCR_BCAST_CONN 0x40000000
#define IPCR_P2P_CONN_MASK 0x3f000000
#define IPCR_P2P_CONN_SHIFT 24
#define IPCR_CHANNEL_MASK 0x003f0000
#define IPCR_CHANNEL_SHIFT 16
enum bus_reset_handling {
ABORT_ON_BUS_RESET,
SUCCEED_ON_BUS_RESET,
};
static __attribute__((format(printf, 2, 3)))
void cmp_error(struct cmp_connection *c, const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
dev_err(&c->resources.unit->device, "%cPCR%u: %pV",
'i', c->pcr_index, &(struct va_format){ fmt, &va });
va_end(va);
}
static int pcr_modify(struct cmp_connection *c,
__be32 (*modify)(struct cmp_connection *c, __be32 old),
int (*check)(struct cmp_connection *c, __be32 pcr),
enum bus_reset_handling bus_reset_handling)
{
struct fw_device *device = fw_parent_device(c->resources.unit);
__be32 *buffer = c->resources.buffer;
int generation = c->resources.generation;
int rcode, errors = 0;
__be32 old_arg;
int err;
buffer[0] = c->last_pcr_value;
for (;;) {
old_arg = buffer[0];
buffer[1] = modify(c, buffer[0]);
rcode = fw_run_transaction(
device->card, TCODE_LOCK_COMPARE_SWAP,
device->node_id, generation, device->max_speed,
CSR_REGISTER_BASE + CSR_IPCR(c->pcr_index),
buffer, 8);
if (rcode == RCODE_COMPLETE) {
if (buffer[0] == old_arg) /* success? */
break;
if (check) {
err = check(c, buffer[0]);
if (err < 0)
return err;
}
} else if (rcode == RCODE_GENERATION)
goto bus_reset;
else if (rcode_is_permanent_error(rcode) || ++errors >= 3)
goto io_error;
}
c->last_pcr_value = buffer[1];
return 0;
io_error:
cmp_error(c, "transaction failed: %s\n", rcode_string(rcode));
return -EIO;
bus_reset:
return bus_reset_handling == ABORT_ON_BUS_RESET ? -EAGAIN : 0;
}
/**
* cmp_connection_init - initializes a connection manager
* @c: the connection manager to initialize
* @unit: a unit of the target device
* @ipcr_index: the index of the iPCR on the target device
*/
int cmp_connection_init(struct cmp_connection *c,
struct fw_unit *unit,
unsigned int ipcr_index)
{
__be32 impr_be;
u32 impr;
int err;
err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
CSR_REGISTER_BASE + CSR_IMPR,
&impr_be, 4);
if (err < 0)
return err;
impr = be32_to_cpu(impr_be);
if (ipcr_index >= (impr & IMPR_PLUGS_MASK))
return -EINVAL;
c->connected = false;
mutex_init(&c->mutex);
fw_iso_resources_init(&c->resources, unit);
c->last_pcr_value = cpu_to_be32(0x80000000);
c->pcr_index = ipcr_index;
c->max_speed = (impr & IMPR_SPEED_MASK) >> IMPR_SPEED_SHIFT;
if (c->max_speed == SCODE_BETA)
c->max_speed += (impr & IMPR_XSPEED_MASK) >> IMPR_XSPEED_SHIFT;
return 0;
}
EXPORT_SYMBOL(cmp_connection_init);
/**
* cmp_connection_destroy - free connection manager resources
* @c: the connection manager
*/
void cmp_connection_destroy(struct cmp_connection *c)
{
WARN_ON(c->connected);
mutex_destroy(&c->mutex);
fw_iso_resources_destroy(&c->resources);
}
EXPORT_SYMBOL(cmp_connection_destroy);
static __be32 ipcr_set_modify(struct cmp_connection *c, __be32 ipcr)
{
ipcr &= ~cpu_to_be32(IPCR_BCAST_CONN |
IPCR_P2P_CONN_MASK |
IPCR_CHANNEL_MASK);
ipcr |= cpu_to_be32(1 << IPCR_P2P_CONN_SHIFT);
ipcr |= cpu_to_be32(c->resources.channel << IPCR_CHANNEL_SHIFT);
return ipcr;
}
static int ipcr_set_check(struct cmp_connection *c, __be32 ipcr)
{
if (ipcr & cpu_to_be32(IPCR_BCAST_CONN |
IPCR_P2P_CONN_MASK)) {
cmp_error(c, "plug is already in use\n");
return -EBUSY;
}
if (!(ipcr & cpu_to_be32(IPCR_ONLINE))) {
cmp_error(c, "plug is not on-line\n");
return -ECONNREFUSED;
}
return 0;
}
/**
* cmp_connection_establish - establish a connection to the target
* @c: the connection manager
* @max_payload_bytes: the amount of data (including CIP headers) per packet
*
* This function establishes a point-to-point connection from the local
* computer to the target by allocating isochronous resources (channel and
* bandwidth) and setting the target's input plug control register. When this
* function succeeds, the caller is responsible for starting transmitting
* packets.
*/
int cmp_connection_establish(struct cmp_connection *c,
unsigned int max_payload_bytes)
{
int err;
if (WARN_ON(c->connected))
return -EISCONN;
c->speed = min(c->max_speed,
fw_parent_device(c->resources.unit)->max_speed);
mutex_lock(&c->mutex);
retry_after_bus_reset:
err = fw_iso_resources_allocate(&c->resources,
max_payload_bytes, c->speed);
if (err < 0)
goto err_mutex;
err = pcr_modify(c, ipcr_set_modify, ipcr_set_check,
ABORT_ON_BUS_RESET);
if (err == -EAGAIN) {
fw_iso_resources_free(&c->resources);
goto retry_after_bus_reset;
}
if (err < 0)
goto err_resources;
c->connected = true;
mutex_unlock(&c->mutex);
return 0;
err_resources:
fw_iso_resources_free(&c->resources);
err_mutex:
mutex_unlock(&c->mutex);
return err;
}
EXPORT_SYMBOL(cmp_connection_establish);
/**
* cmp_connection_update - update the connection after a bus reset
* @c: the connection manager
*
* This function must be called from the driver's .update handler to reestablish
* any connection that might have been active.
*
* Returns zero on success, or a negative error code. On an error, the
* connection is broken and the caller must stop transmitting iso packets.
*/
int cmp_connection_update(struct cmp_connection *c)
{
int err;
mutex_lock(&c->mutex);
if (!c->connected) {
mutex_unlock(&c->mutex);
return 0;
}
err = fw_iso_resources_update(&c->resources);
if (err < 0)
goto err_unconnect;
err = pcr_modify(c, ipcr_set_modify, ipcr_set_check,
SUCCEED_ON_BUS_RESET);
if (err < 0)
goto err_resources;
mutex_unlock(&c->mutex);
return 0;
err_resources:
fw_iso_resources_free(&c->resources);
err_unconnect:
c->connected = false;
mutex_unlock(&c->mutex);
return err;
}
EXPORT_SYMBOL(cmp_connection_update);
static __be32 ipcr_break_modify(struct cmp_connection *c, __be32 ipcr)
{
return ipcr & ~cpu_to_be32(IPCR_BCAST_CONN | IPCR_P2P_CONN_MASK);
}
/**
* cmp_connection_break - break the connection to the target
* @c: the connection manager
*
* This function deactives the connection in the target's input plug control
* register, and frees the isochronous resources of the connection. Before
* calling this function, the caller should cease transmitting packets.
*/
void cmp_connection_break(struct cmp_connection *c)
{
int err;
mutex_lock(&c->mutex);
if (!c->connected) {
mutex_unlock(&c->mutex);
return;
}
err = pcr_modify(c, ipcr_break_modify, NULL, SUCCEED_ON_BUS_RESET);
if (err < 0)
cmp_error(c, "plug is still connected\n");
fw_iso_resources_free(&c->resources);
c->connected = false;
mutex_unlock(&c->mutex);
}
EXPORT_SYMBOL(cmp_connection_break);

+ 41
- 0
sound/firewire/cmp.h View File

@ -0,0 +1,41 @@
#ifndef SOUND_FIREWIRE_CMP_H_INCLUDED
#define SOUND_FIREWIRE_CMP_H_INCLUDED
#include <linux/mutex.h>
#include <linux/types.h>
#include "iso-resources.h"
struct fw_unit;
/**
* struct cmp_connection - manages an isochronous connection to a device
* @speed: the connection's actual speed
*
* This structure manages (using CMP) an isochronous stream from the local
* computer to a device's input plug (iPCR).
*
* There is no corresponding oPCR created on the local computer, so it is not
* possible to overlay connections on top of this one.
*/
struct cmp_connection {
int speed;
/* private: */
bool connected;
struct mutex mutex;
struct fw_iso_resources resources;
__be32 last_pcr_value;
unsigned int pcr_index;
unsigned int max_speed;
};
int cmp_connection_init(struct cmp_connection *connection,
struct fw_unit *unit,
unsigned int ipcr_index);
void cmp_connection_destroy(struct cmp_connection *connection);
int cmp_connection_establish(struct cmp_connection *connection,
unsigned int max_payload);
int cmp_connection_update(struct cmp_connection *connection);
void cmp_connection_break(struct cmp_connection *connection);
#endif

+ 223
- 0
sound/firewire/fcp.c View File

@ -0,0 +1,223 @@
/*
* Function Control Protocol (IEC 61883-1) helper functions
*
* Copyright (c) Clemens Ladisch <clemens@ladisch.de>
* Licensed under the terms of the GNU General Public License, version 2.
*/
#include <linux/device.h>
#include <linux/firewire.h>
#include <linux/firewire-constants.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
#include <linux/wait.h>
#include "fcp.h"
#include "lib.h"
#define CTS_AVC 0x00
#define ERROR_RETRIES 3
#define ERROR_DELAY_MS 5
#define FCP_TIMEOUT_MS 125
static DEFINE_SPINLOCK(transactions_lock);
static LIST_HEAD(transactions);
enum fcp_state {
STATE_PENDING,
STATE_BUS_RESET,
STATE_COMPLETE,
};
struct fcp_transaction {
struct list_head list;
struct fw_unit *unit;
void *response_buffer;
unsigned int response_size;
unsigned int response_match_bytes;
enum fcp_state state;
wait_queue_head_t wait;
};
/**
* fcp_avc_transaction - send an AV/C command and wait for its response
* @unit: a unit on the target device
* @command: a buffer containing the command frame; must be DMA-able
* @command_size: the size of @command
* @response: a buffer for the response frame
* @response_size: the maximum size of @response
* @response_match_bytes: a bitmap specifying the bytes used to detect the
* correct response frame
*
* This function sends a FCP command frame to the target and waits for the
* corresponding response frame to be returned.
*
* Because it is possible for multiple FCP transactions to be active at the
* same time, the correct response frame is detected by the value of certain
* bytes. These bytes must be set in @response before calling this function,
* and the corresponding bits must be set in @response_match_bytes.
*
* @command and @response can point to the same buffer.
*
* Asynchronous operation (INTERIM, NOTIFY) is not supported at the moment.
*
* Returns the actual size of the response frame, or a negative error code.
*/
int fcp_avc_transaction(struct fw_unit *unit,
const void *command, unsigned int command_size,
void *response, unsigned int response_size,
unsigned int response_match_bytes)
{
struct fcp_transaction t;
int tcode, ret, tries = 0;
t.unit = unit;
t.response_buffer = response;
t.response_size = response_size;
t.response_match_bytes = response_match_bytes;
t.state = STATE_PENDING;
init_waitqueue_head(&t.wait);
spin_lock_irq(&transactions_lock);
list_add_tail(&t.list, &transactions);
spin_unlock_irq(&transactions_lock);
for (;;) {
tcode = command_size == 4 ? TCODE_WRITE_QUADLET_REQUEST
: TCODE_WRITE_BLOCK_REQUEST;
ret = snd_fw_transaction(t.unit, tcode,
CSR_REGISTER_BASE + CSR_FCP_COMMAND,
(void *)command, command_size);
if (ret < 0)
break;
wait_event_timeout(t.wait, t.state != STATE_PENDING,
msecs_to_jiffies(FCP_TIMEOUT_MS));
if (t.state == STATE_COMPLETE) {
ret = t.response_size;
break;
} else if (t.state == STATE_BUS_RESET) {
msleep(ERROR_DELAY_MS);
} else if (++tries >= ERROR_RETRIES) {
dev_err(&t.unit->device, "FCP command timed out\n");
ret = -EIO;
break;
}
}
spin_lock_irq(&transactions_lock);
list_del(&t.list);
spin_unlock_irq(&transactions_lock);
return ret;
}
EXPORT_SYMBOL(fcp_avc_transaction);
/**
* fcp_bus_reset - inform the target handler about a bus reset
* @unit: the unit that might be used by fcp_avc_transaction()
*
* This function must be called from the driver's .update handler to inform
* the FCP transaction handler that a bus reset has happened. Any pending FCP
* transactions are retried.
*/
void fcp_bus_reset(struct fw_unit *unit)
{
struct fcp_transaction *t;
spin_lock_irq(&transactions_lock);
list_for_each_entry(t, &transactions, list) {
if (t->unit == unit &&
t->state == STATE_PENDING) {
t->state = STATE_BUS_RESET;
wake_up(&t->wait);
}
}
spin_unlock_irq(&transactions_lock);
}
EXPORT_SYMBOL(fcp_bus_reset);
/* checks whether the response matches the masked bytes in response_buffer */
static bool is_matching_response(struct fcp_transaction *transaction,
const void *response, size_t length)
{
const u8 *p1, *p2;
unsigned int mask, i;
p1 = response;
p2 = transaction->response_buffer;
mask = transaction->response_match_bytes;
for (i = 0; ; ++i) {
if ((mask & 1) && p1[i] != p2[i])
return false;
mask >>= 1;
if (!mask)
return true;
if (--length == 0)
return false;
}
}
static void fcp_response(struct fw_card *card, struct fw_request *request,
int tcode, int destination, int source,
int generation, unsigned long long offset,
void *data, size_t length, void *callback_data)
{
struct fcp_transaction *t;
unsigned long flags;
if (length < 1 || (*(const u8 *)data & 0xf0) != CTS_AVC)
return;
spin_lock_irqsave(&transactions_lock, flags);
list_for_each_entry(t, &transactions, list) {
struct fw_device *device = fw_parent_device(t->unit);
if (device->card != card ||
device->generation != generation)
continue;
smp_rmb(); /* node_id vs. generation */
if (device->node_id != source)
continue;
if (t->state == STATE_PENDING &&
is_matching_response(t, data, length)) {
t->state = STATE_COMPLETE;
t->response_size = min((unsigned int)length,
t->response_size);
memcpy(t->response_buffer, data, t->response_size);
wake_up(&t->wait);
}
}
spin_unlock_irqrestore(&transactions_lock, flags);
}
static struct fw_address_handler response_register_handler = {
.length = 0x200,
.address_callback = fcp_response,
};
static int __init fcp_module_init(void)
{
static const struct fw_address_region response_register_region = {
.start = CSR_REGISTER_BASE + CSR_FCP_RESPONSE,
.end = CSR_REGISTER_BASE + CSR_FCP_END,
};
fw_core_add_address_handler(&response_register_handler,
&response_register_region);
return 0;
}
static void __exit fcp_module_exit(void)
{
WARN_ON(!list_empty(&transactions));
fw_core_remove_address_handler(&response_register_handler);
}
module_init(fcp_module_init);
module_exit(fcp_module_exit);

+ 12
- 0
sound/firewire/fcp.h View File

@ -0,0 +1,12 @@
#ifndef SOUND_FIREWIRE_FCP_H_INCLUDED
#define SOUND_FIREWIRE_FCP_H_INCLUDED
struct fw_unit;
int fcp_avc_transaction(struct fw_unit *unit,
const void *command, unsigned int command_size,
void *response, unsigned int response_size,
unsigned int response_match_bytes);
void fcp_bus_reset(struct fw_unit *unit);
#endif

+ 224
- 0
sound/firewire/iso-resources.c View File

@ -0,0 +1,224 @@
/*
* isochronous resources helper functions
*
* Copyright (c) Clemens Ladisch <clemens@ladisch.de>
* Licensed under the terms of the GNU General Public License, version 2.
*/
#include <linux/device.h>
#include <linux/firewire.h>
#include <linux/firewire-constants.h>
#include <linux/jiffies.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
#include "iso-resources.h"
/**
* fw_iso_resources_init - initializes a &struct fw_iso_resources
* @r: the resource manager to initialize
* @unit: the device unit for which the resources will be needed
*
* If the device does not support all channel numbers, change @r->channels_mask
* after calling this function.
*/
void fw_iso_resources_init(struct fw_iso_resources *r, struct fw_unit *unit)
{
r->channels_mask = ~0uLL;
r->unit = fw_unit_get(unit);
mutex_init(&r->mutex);
r->allocated = false;
}
/**
* fw_iso_resources_destroy - destroy a resource manager
* @r: the resource manager that is no longer needed
*/
void fw_iso_resources_destroy(struct fw_iso_resources *r)
{
WARN_ON(r->allocated);
mutex_destroy(&r->mutex);
fw_unit_put(r->unit);
}
static unsigned int packet_bandwidth(unsigned int max_payload_bytes, int speed)
{
unsigned int bytes, s400_bytes;
/* iso packets have three header quadlets and quadlet-aligned payload */
bytes = 3 * 4 + ALIGN(max_payload_bytes, 4);
/* convert to bandwidth units (quadlets at S1600 = bytes at S400) */
if (speed <= SCODE_400)
s400_bytes = bytes * (1 << (SCODE_400 - speed));
else
s400_bytes = DIV_ROUND_UP(bytes, 1 << (speed - SCODE_400));
return s400_bytes;
}
static int current_bandwidth_overhead(struct fw_card *card)
{
/*
* Under the usual pessimistic assumption (cable length 4.5 m), the
* isochronous overhead for N cables is 1.797 µs + N * 0.494 µs, or
* 88.3 + N * 24.3 in bandwidth units.
*
* The calculation below tries to deduce N from the current gap count.
* If the gap count has been optimized by measuring the actual packet
* transmission time, this derived overhead should be near the actual
* overhead as well.
*/
return card->gap_count < 63 ? card->gap_count * 97 / 10 + 89 : 512;
}
static int wait_isoch_resource_delay_after_bus_reset(struct fw_card *card)
{
for (;;) {
s64 delay = (card->reset_jiffies + HZ) - get_jiffies_64();
if (delay <= 0)
return 0;
if (schedule_timeout_interruptible(delay) > 0)
return -ERESTARTSYS;
}
}
/**
* fw_iso_resources_allocate - allocate isochronous channel and bandwidth
* @r: the resource manager
* @max_payload_bytes: the amount of data (including CIP headers) per packet
* @speed: the speed (e.g., SCODE_400) at which the packets will be sent
*
* This function allocates one isochronous channel and enough bandwidth for the
* specified packet size.
*
* Returns the channel number that the caller must use for streaming, or
* a negative error code. Due to potentionally long delays, this function is
* interruptible and can return -ERESTARTSYS. On success, the caller is
* responsible for calling fw_iso_resources_update() on bus resets, and
* fw_iso_resources_free() when the resources are not longer needed.
*/
int fw_iso_resources_allocate(struct fw_iso_resources *r,
unsigned int max_payload_bytes, int speed)
{
struct fw_card *card = fw_parent_device(r->unit)->card;
int bandwidth, channel, err;
if (WARN_ON(r->allocated))
return -EBADFD;
r->bandwidth = packet_bandwidth(max_payload_bytes, speed);
retry_after_bus_reset:
spin_lock_irq(&card->lock);
r->generation = card->generation;
r->bandwidth_overhead = current_bandwidth_overhead(card);
spin_unlock_irq(&card->lock);
err = wait_isoch_resource_delay_after_bus_reset(card);
if (err < 0)
return err;
mutex_lock(&r->mutex);
bandwidth = r->bandwidth + r->bandwidth_overhead;
fw_iso_resource_manage(card, r->generation, r->channels_mask,
&channel, &bandwidth, true, r->buffer);
if (channel == -EAGAIN) {
mutex_unlock(&r->mutex);
goto retry_after_bus_reset;
}
if (channel >= 0) {
r->channel = channel;
r->allocated = true;
} else {
if (channel == -EBUSY)
dev_err(&r->unit->device,
"isochronous resources exhausted\n");
else
dev_err(&r->unit->device,
"isochronous resource allocation failed\n");
}
mutex_unlock(&r->mutex);
return channel;
}
/**
* fw_iso_resources_update - update resource allocations after a bus reset
* @r: the resource manager
*
* This function must be called from the driver's .update handler to reallocate
* any resources that were allocated before the bus reset. It is safe to call
* this function if no resources are currently allocated.
*
* Returns a negative error code on failure. If this happens, the caller must
* stop streaming.
*/
int fw_iso_resources_update(struct fw_iso_resources *r)
{
struct fw_card *card = fw_parent_device(r->unit)->card;
int bandwidth, channel;
mutex_lock(&r->mutex);
if (!r->allocated) {
mutex_unlock(&r->mutex);
return 0;
}
spin_lock_irq(&card->lock);
r->generation = card->generation;
r->bandwidth_overhead = current_bandwidth_overhead(card);
spin_unlock_irq(&card->lock);
bandwidth = r->bandwidth + r->bandwidth_overhead;
fw_iso_resource_manage(card, r->generation, 1uLL << r->channel,
&channel, &bandwidth, true, r->buffer);
/*
* When another bus reset happens, pretend that the allocation
* succeeded; we will try again for the new generation later.
*/
if (channel < 0 && channel != -EAGAIN) {
r->allocated = false;
if (channel == -EBUSY)
dev_err(&r->unit->device,
"isochronous resources exhausted\n");
else
dev_err(&r->unit->device,
"isochronous resource allocation failed\n");
}
mutex_unlock(&r->mutex);
return channel;
}
/**
* fw_iso_resources_free - frees allocated resources
* @r: the resource manager
*
* This function deallocates the channel and bandwidth, if allocated.
*/
void fw_iso_resources_free(struct fw_iso_resources *r)
{
struct fw_card *card = fw_parent_device(r->unit)->card;
int bandwidth, channel;
mutex_lock(&r->mutex);
if (r->allocated) {
bandwidth = r->bandwidth + r->bandwidth_overhead;
fw_iso_resource_manage(card, r->generation, 1uLL << r->channel,
&channel, &bandwidth, false, r->buffer);
if (channel < 0)
dev_err(&r->unit->device,
"isochronous resource deallocation failed\n");
r->allocated = false;
}
mutex_unlock(&r->mutex);
}

+ 39
- 0
sound/firewire/iso-resources.h View File

@ -0,0 +1,39 @@
#ifndef SOUND_FIREWIRE_ISO_RESOURCES_H_INCLUDED
#define SOUND_FIREWIRE_ISO_RESOURCES_H_INCLUDED
#include <linux/mutex.h>
#include <linux/types.h>
struct fw_unit;
/**
* struct fw_iso_resources - manages channel/<