firewire-lib: Rename functions, structure, member for AMDTP

This patch renames some functions, structure and member to reuse them in both
AMDTP in/out stream.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
firewire-lib: Add macros instead of fixed value for AMDTP

This patch adds some macros instead of fixed value for AMDTP in IEC 61883-6.
These macros will also be used by followed patches.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
firewire-lib: Add 'direction' member to 'amdtp_stream' structure

This patch adds 'direction' member to amdtp_stream structure to indicate its
direction. This patch also adds 'direction' argument to amdtp_stream_init()
function to determine its direction.

The amdtp_stream_init() function is exported and used by firewire-speakers and
dice so this patch also affects them.

This patch just add them. Actual implementation will be done by followed
patches.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
firewire-lib: Split some codes into functions to reuse for both streams

Some codes can be reused to handle in stream. This commit adds new functions.
This commit also renames some functions to keep naming consistency.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
firewire-lib: Add support for AMDTP in-stream and PCM capture

For capturing PCM, this commit adds the functionality to handle in-stream.
This is also applied for dual-wire mode.

Currently, capturing 32bit samples are supported.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
firewire-lib: Add support for MIDI capture/playback

For capturing/playbacking MIDI messages, this commit adds one MIDI conformant
data channel. This channel supports 8 MPX-MIDI data streams then maximum number
of supported MIDI ports is limited by 8.

And this commit allows to set PCM format even if AMDTP stream already starts.
I suppose the case that PCM data starts to be joined into AMDTP streams after
AMDTP stream is already started for MIDI. To distinguish whether PCM or MIDI
functionality starts AMDTP stream, this commit adds amdtp_stream_pcm_running()
and amdtp_stream_midi_running(). I think this is better than reference-counter.

IEC 61883-6 refers to AMEI/MMA RP-027 for implementation of MIDI conformant
data. In the specification, 'MIDI1.0-2x/3x-SPEED' mode are described with
'negotiation procedure' and 'encapsulation details'. But there is no
specifications to define them. This module implement 'MIDI1.0-1x-SPEED' mode.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
firewire-lib: Give syt value as parameter to handle_out_packet()

For duplex streams with synchronization, this driver should pass 'presentation
timestamp' from in-packets to out-packets. This commit is preparation for this.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
firewire-lib: Add support for duplex streams synchronization in blocking mode

Generally, the devices can synchronize to handle 'presentation timestamp'
in CIP packets. This commit adds the functionality to pick up this timestamp
from in-packets transmitted by the device, then use it for out packets.

In previous implementation, this module generated the timestamp by itself. This
is 'SYT Match' mode. Then this drive acts as synchronization master. This
commit allows this module to act as synchronization slave.

This commit restricts this mechanism is only available in blocking mode because
handling the timestamp in non-blocking mode is more complicated than in
blocking mode.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
firewire-lib: Add sort function for transmitted packet

The aim of this commit is to assure the continuity of timestamp in in-packets
transmitted by the device.

When callback of receive-context is processed, the parameters of in-packets are
stored in sort table and sorted by its value of data block counter. The sort
algorism works faster in ordered sequence than in messy sequence. This is
convinient for this purpose because the sequence is assumed not to be so messy.
After sorting, packets are processed except for a last few packets in sort
table. These packets are stored in buffer once and used in next cycle.

Fireworks with firmware version 5.5 or former is a type of device which
transmits packets with a bit disorder. For example,

Index  Payload	CIP header 1  CIP header 2
023    210      3F1100F8      900478E9
024    002      3F1100F8      90FFFFFF
025    210      3F110000      900490E9
026    210      3F110008      9004A4E9
027    210      3F110010      9004B8E9
028    002      3F110010      90FFFFFF
029    210      3F110020      9004E4E8 <-
030    210      3F110018      9004D0E8 <-
031    210      3F110028      9004F8E8
032    002      3F110028      90FFFFFF
033    210      3F110030      900410E8
034    210      3F110038      900424E8
035    210      3F110040      900438E8
036    002      3F110040      90FFFFFF
037    210      3F110050      900464E8 <-
038    210      3F110048      900450E8 <-
039    210      3F110058      900478E8
040    002      3F110058      90FFFFFF
041    210      3F110068      9004A4E8 <-
042    210      3F110060      900490E8 <-
043    210      3F110070      9004B8E8
044    002      3F110070      90FFFFFF
045    210      3F110080      9004E4E7 <-
046    210      3F110078      9004D0E8 <-

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
firewire-lib: Add transfer delay to synchronized duplex streams

Currently, when duplex streams with synchronization mode, this module just pass
'presentation timestamp' from in-packets to out-packets. This is enough to
handle actual device but logically the timestamp should include
'transfer delay' and 'processing time'. This means the timestamp in out-packet
should be at future.

To be simple, this commit add only 'transfer delay'.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
firewire-lib: Add support for channel mapping

Some devices arrange the position of PCM/MIDI data in AMDTP packet. This commit
allows drivers to set channel mapping.

To be simple, the mapping table is an array with fixed length. Then the number
of channels for PCM is restricted by 64 channels.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
firewire-lib: Rename macros, variables and functions for CMP

Referring to IEC 61883-1, oMPR and iMPR, oPCR and iPCR have some fields with
the same role in the same position. This patch renames some macros, variables
and function arguments with "i" in its prefix to reuse them between oMPR and
iMPR, oPCR and iPCR.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
firewire-lib: Add 'direction' member to 'cmp_connection' structure

This patch adds 'direction' member to 'cmp_connection' structure to indicate
the direction of connection. This patch also adds 'direction' argument to
cmp_connection_init() function to determine the direction.

The cmp_connection_init() function is exported and used in snd-firewire-speakers
so this patch also affect it.

This patch just add them. Actual implementation will be done by followed
patches.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
firewire-lib: Add handling output connection by CMP

This patch adds some macros, codes with condition of direction and new functions
to handle output connection. Once cmp_connection_init() is executed with its
direction, CMP input and output connection can be handled by the same way.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
firewire-lib: Add a new function to check others' connection

Plug Control Registers have two fields related to the number of established
connections, one is 'Broadcast connection counter' and another is
'Point-to-point connection counter'. The driver can know there are established
connections or not to check these fields.

This is considering for JACK/FFADO streaming. Currently, when JACK/FFADO starts
its streaming to the device, cmp_connection_establish() is failed expectedly.
This seems to be enough but there are some devices which needs to change
samplling frequency before trying to establish connections. For such devices,
this functionality is needed.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
firewire-lib: Add some AV/C general commands

This commit adds three commands, which may be used by some firewire device
drivers. These commands are defined in 'AV/C Digital Interface Command Set
General Specification Version 4.2 (2004006, 1394TA)'.

1. PLUG INFO command (10.1)
2. INPUT PLUG SIGNAL FORMAT command (10.10)
3. OUTPUT PLUG SIGNAL FORMAT command (10.11)

By the command 1, the drivers can get the number of plugs for AV/C unit or
subunit.
By the command 2 and 3, the drivers can get/set sampling frequency.

The 'firewire-speakers' already uses INPUT PLUG SIGNAL FORMAT command to set
sampling rate. So this commit also affects the driver.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
firewire-lib: Add quirks for Fireworks

This commit adds three modifications for Echo Audio's Fireworks quirks.

1.Making this module to support both TAG0 and TAG1
In IEC 61883-1, each common isochronous packet (CIP) has 'tag' field in its
packet header. The value is generally 0x01 (CIP header is included = TAG1).
But Fireworks in 'IEC 61883 compliant' mode transmits 'no data' packet with
0x00 (No CIP header is included = TAG0) but the packet includes CIP structure.
To process 'presentation timestamp' for duplex streams synchronization
correctly, this packets needs to be handled.

2.Making this module to calculate data block size and not to check continuity
One of Fireworks device, AudioFirePre8, always reports 8 data block size in
in-packets and the data block counter is incremented by 8. But actual value is
wrong.

3.Making this module to have the numebr of first data blocks for MIDI messages
in out-packet
Fireworks can pick up MIDI messages in first 8 data blocks and ignore in other
data blocks.

I confirm these quirks in firmware version 4.8 to 5.8 for Windows. I didn't
investigate for the other versions. But according to contact person in Echo
Audio, there is no differences between firmwares installed by Windows or OS X.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
fireworks: Add skelton for Fireworks based devices

This commit adds a new driver with no functionality. This driver just
creates/removes card instance according to callbacks.

Current supported devices:
 - Mackie Onyx 400F/1200F
 - Echo AudioFire12/8(until 2009 July)
 - Echo AudioFire2/4/Pre8/8(since 2009 July)
 - Echo Fireworks 8/HDMI
 - Gibson Robot Interface pack/GoldTop
fireworks: Add transaction and some commands

Fireworks uses own command and response. This commit adds functionality to
transact and adds some commands required for sound card instance and kernel
streaming.

There are two ways to deliver substance of this transaction:
1.AV/C vendor dependent command for command/response
2.Async transaction to specific addresses for command/response

By way 1, I confirm AudioFire12 cannot correctly response to some commands with
firmware version 5.0 or later. This is also confirmed by FFADO. So this module
implement way 2.

The address for response gives an issue. When this driver allocate own callback
function into the address, then no one can allocate its own callback function.
This situation is not good for applications in user-land. Currently this commit
don't solve this issue. This issue is solved in later commit.

I note there is a command to change the address for response if the device
supports. But this module uses default value. So users should not execute this
command as long as hoping this module works correctly.
fireworks: Add connection and stream management

Fireworks manages connections by CMP and can transmit/receive AMDTP streams
with a few quirks. This commit adds functionality to start/stop the streams.

Major Fireworks products don't support 'SYT-Match' mode, except for
AudioFire12/8(till 2009 July) with firmware version 1.0. Already in previous
commit, this driver don't support such old firmwares in previous commit. So
this commit adds support for non 'SYT-Match' mode.

I note that there is a short gap for MIDI streams when starting PCM data. When
AMDTP streams are running only for MIDI data and PCM data is going to be joined
at different sampling rate, then AMDTP streams are stopped once and started
again after changing sampling rate.

Fireworks quirks:

1.Fireworks transmits packets with both TAG0 and TAG1
In IEC 61883-1, each common isochronous packet (CIP) has 'tag' field in its
packet header. The value is generally 0x01 ('CIP header is included' = TAG1).
But Fireworks in 'IEC 61883 compliant mode' transmits 'no data' packet with 0x00
('No CIP header is included' = TAG0) but actually the packet includes CIP
structure. To handle 'presentation timestamp' for duplex streams
synchronization, this packets must be handled.

2.Fireworks transmits 'no data' packet out of specification
Fireworks transmits 'no data' packet with 'NO-DATA' FDF and no dummy data. It
includes CIP headers only. In IEC 61883-6, 'no data' packet is generated by two
ways. One is empty CIP packet defined in IEC 61883-1. Another is
'special non-empty packet' defined in IEC 61883-6. Fireworks uses strange
combination of them.

3.Fireworks can handle MIDI messages in the first 8 data blocks of out-packet
Fireworks ignores MIDI messages in other data blocks.

4.AudioFirePre8 transmits packets with wrong data block size
One of Fireworks device, AudioFirePre8, always reports 8 data block size in
transmitted packets and increments the data block counter by 8. But this is not
actual value.

I confirm them with firmware version 4.8 or later with Windows. According to
a contact persion in Echo Audio, there is no difference between firmwares
installed by Windows/OS X.