firewire-lib: Add sort function for transmitted packet

The aim of this commit is to assure the continuity of timestamp in received
packets. 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 it 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. 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

The register for CMP plugs has 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. This seems to
be enough but there are some devices which needs to change samplling frequency
before trying to establish connections. For these devices, the functionality to
check connections is needed.

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

This commit add three commands. I think many devices use these commands.
These commands are defined in 'AV/C Digital Interface Command Set General
Specification Version 4.2 (2004006, 1394TA)'.

1. INPUT PLUG SIGNAL FORMAT command
2. OUTPUT PLUG SIGNAL FORMAT command
3. PLUG INFO command

By the command 1 and 2, the drivers can get/set sampling frequency.
By the command 3, the drivers can get information about plugs.

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 for this quirk.
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, 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 this 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 have no
investigation for the other versions.

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

This commit add a new driver with no functionality. This driver just
create/remove card instance according to callbacks.

Current supported devices:
 - Mackie Onyx 400F/1200F
 - Echo AudioFire8(until 2009 July)/AudioFire12
 - Echo AudioFire2/4/8(since 2009 July)/Pre8
 - 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.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 stream with
a few quirks. This commit adds functionality to start/stop streams.

As long as I know, major Fireworks products don't support 'SYT-Match' mode,
except for AudioFire12/8(till 2009 July) with firmware version 1.0. So this
commit add support for no '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
multiplexed at different sampling rate, then AMDTP streams are stopped once and
started again after changing sampling rate.

Fireworks quirks I confirm with firmware version 4.8 or later with Windows:

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 transmits 'no data' packet with 0x00
('No CIP header is included' = TAG0) but 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.
fireworks: Add proc interface for debugging purpose

This commit adds proc interface to output infomation for debugging.
 - firmware information
 - sampling rate and clock source
 - physical metering (linear value)
fireworks: Add MIDI interface

This commit adds a functionality to capture/playback MIDI messages.

When no AMDTP streams are running, this module starts AMDTP stream at current
sampling rate for MIDI stream.
fireworks: Add PCM interface

This commit adds a functionality to capture/playback PCM samples.

When AMDTP stream is already running for PCM or the source of clock is not
internal, available sampling rate is limited at current one.
fireworks: Add hwdep interface

This interface is designed for mixer/control application. To use hwdep
interface, the application can get information about firewire node, can
lock/unlock kernel streaming and can get notification at starting/stopping
kernel streaming.
fireworks: Add command/response functionality into hwdep interface

This commit adds two functionality for hwdep interface, adds two parameters for
this module, add a node for proc interface.

To receive responses from devices, this module already allocate own callback
into private address area in host controller. This means no one can allocate
its own callback to the address. So this module must give a way for user
applications to receive responses.

This commit adds a functionality to receive responses via hwdep interface. The
application can receive responses to read from this interface. To achieve this,
this commit adds a buffer to queue responses. The default size of this buffer is
1024 bytes. This size can be changed to give preferrable size to
'resp_buf_size' parameter for this module. The application should notice rest
of space in this buffer because this module don't push responses when this
buffer has no space.

Additionaly, this commit adds a functionality to transmit commands via hwdep
interface. The application can transmit commands to write into this interface.
I note that the application can transmit one command at once, but can receive
as many responses as possible untill the user-buffer is full.

When using these interfaces, the application must keep maximum number of
sequence number within the number in firewire.h because this module uses this
number to distinguish the response is against the command by the application or
this module.

Usually responses against commands which the application transmits are pushed
into this buffer. But to enable 'resp_buf_debug' parameter for this module, all
responses are pushed into the buffer. When using this mode, I reccomend to
expand the size of buffer.

Finally this commit adds a new node into proc interface to output status of the
buffer.
bebob: Add skelton for BeBoB based devices

This commit add a new driver for BeBoB based devices with no functionality. This
driver just create/remove card instance according to callbacks.

Current supported devices:
 - Edirol FA-66/FA-101
 - PreSonus FIREBOX/FIREPOD
 - BridgeCo RDAudio1/Audio5
 - Mackie Onyx 820/1220/1620/1640 (Firewire I/O Card)
 - Mackie d.2 (Firewire Option)
 - Stanton FinalScratch 2 (ScratchAmp)
 - Tascam  IF-FW DM
 - Apogee Rosetta 200/Rosetta 400 (X-FireWire card)
 - Apogee DA-16X/AD-16X/DD-16X (X-FireWire card)
 - ESI Quotafire610
 - AcousticReality eARMasterOne
 - CME MatrixKFW
 - Phonic HB12/12 MkII/12 Universal
 - Phonic HB18/18 MkII/18 Universal
 - Phonic HB24/24 MkII/24 Universal
 - Phonic FireFly 202/302
 - Lynx Aurora 8/16 (LT-FW)
 - ICON FireXon
 - PrismSound, Orpheus/ADA-8XR

Devices possible to be supported if identifying IDs:
 - PreSonus Inspire 1394
 - Mackie Digital X Bus x.200/400
 - CME UF400e
 - Infrasonic DewX/Windy6
 - Rolf Spuler Firewire Guitar
bebob: Add command and stream management

This commit adds BridgeCo's AV/C extension commands and connection/stream
management. The driver can get channel combination with the commands.
bebob: Add proc interface for debug

This commit adds proc interface to get information for debugging.
 - firmware information
 - stream formation
 - current clock source and sampling rate