History log of /linux-master/sound/firewire/amdtp-stream.c
Revision Date Author Comments
# 4a486439 18-Feb-2024 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: handle quirk to calculate payload quadlets as data block counter

Miglia Harmony Audio (OXFW970) has a quirk to put the number of
accumulated quadlets in CIP payload into the dbc field of CIP header.

This commit handles the quirk in the packet processing layer.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20240218074128.95210-4-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 77ce9654 17-Feb-2024 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: fix to check cycle continuity

The local helper function to compare the given pair of cycle count
evaluates them. If the left value is less than the right value, the
function returns negative value.

If the safe cycle is less than the current cycle, it is the case of
cycle lost. However, it is not currently handled properly.

This commit fixes the bug.

Cc: <stable@vger.kernel.org>
Fixes: 705794c53b00 ("ALSA: firewire-lib: check cycle continuity")
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20240218033026.72577-1-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# d8dc8720 30-Jan-2023 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: fix uninitialized local variable

The function local variable, curr_cycle_time is declared without
initialization. When tracepoints event is not probed, it looks to be
used as is. This commit fixes it. Fortunately, the value of local variable
is not used unless the event is probed, thus this commit is for better
coding.

Reported-by: Dan Carpenter <error27@gmail.com>
Fixes: fef4e61b0b76 ("ALSA: firewire-lib: extend tracepoints event including CYCLE_TIME of 1394 OHCI")
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20230130141532.102838-1-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# af13842c 10-Jan-2023 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: compute extra delay for runtime of PCM substream

All drivers in ALSA firewire stack have never reported extra delay for
the runtime of PCM substream. There is some reason, but the main reason
is that the meaning of extra delay differs depending on driver design,
especially for the packet-oriented driver.

Here I define the extra delay for the case of IEC 61883-1/6. It is the
number of PCM frames transferred or should be transferred between the
current isochronous cycle and the isochronous cycle to which the latest
isochronous packet arrived (in IR context) or is scheduled (in IT context).

A commit baa914cd81f5 ("firewire: add kernel API to access CYCLE_TIME
register") allow unit drivers to read CYCLE_TIME of 1394 OHCI controller.
It allows the drivers to compute the current isochronous cycle.

Additionally, a commit f0117128879b ("ALSA: firewire-lib: keep history to
process isochronous packet") enables to save the history processing
packets. It allows the driver to estimate the total number of data blocks
in packets arriving shortly, or calculate the total number of data blocks
in scheduled packets.

Now it is ready. This commit implements the computation of the extra delay.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20230110134933.322794-4-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 7fc693e4 10-Jan-2023 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: obsolete return value from context payload processing layer

This commit obsoletes return value from the context payload processing layer
since the multiplier between the data block count and PCM frame count was
moved to the packet streaming processing layer.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20230110134933.322794-3-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# a36183f6 10-Jan-2023 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: move parameter for pcm frame multiplier from context payload processing layer

The current implementation delegates the task to calculate the number of
processed PCM frames into the context payload processing layer. It looks
good as long as frame calculation is done for a single purpose.
Nevertheless, another purpose, the computation of extra delay for the
runtime of PCM substream, requires frame calculation, too.

This commit refactors the current implementation so that the packet stream
processing layer is responsible for the calculation of PCM frame, too. The
member is moved to stream structure for multiplier between data block
count and PCM frame count.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20230110134933.322794-2-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# fef4e61b 09-Jan-2023 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: extend tracepoints event including CYCLE_TIME of 1394 OHCI

A commit baa914cd81f5 ("firewire: add kernel API to access CYCLE_TIME
register") allow unit drivers to read CYCLE_TIME of 1394 OHCI controller.
The value expresses monotonic time with 42.195 Mhz resolution and wrapping
around every 128 seconds. The controller uses the time to govern
isochronous cycle.

This commit extends tracepoints event including the value so that event
parser can compute gap between current isochronous cycle and the latest
isochronous cycle in which packet is processed (in IR context) or scheduled
(in IT context). It loses backward compatibility to former format of the
tracepoints event.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20230109213231.138223-1-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# f0117128 08-Jan-2023 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: keep history to process isochronous packet

The history to process isochronous packets is useful when computing gap
between current isochronous cycle and the latest isochronous cycle in
which packet is processed (in IR context) and scheduled (in IT context).

This commit stores the most recent packet descriptors to keep the history.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20230109021738.75543-4-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 0cac60c7 08-Jan-2023 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: use circular linked list for context payload processing layer

The list of packet descriptor is passed to context payload processing
layer so that each driver can copy PCM frames, MIDI messages, and device
specific data between packet payload buffer and intermediate buffer for
user space application.

The list of packet descriptor was replaced by circular linked list in a
previous commit. This commit uses circular linked in context payload
processing layer as well.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20230109021738.75543-3-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# cec371ff 08-Jan-2023 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: use circular linked list to enumerate packet descriptors

Current implementation uses list of packet descriptor as template to
schedule isochronous packet. The packet descriptors are operated by
position and size, while circular linked list is convenient to enumerate
the packet descriptors.

This commit utilizes circular linked list for the purpose.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20230109021738.75543-2-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# c38d8cff 06-Jan-2023 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for cache position in sequence replay

When sequence replay is enabled for media clock recovery, current
implementation refers to cache of sequence descriptors in tx packets, then
fulfil sequence descriptors for rx packets. The initialization for rx
packets is done before starting packet streaming, while it can be postponed
till the cache has enough entries for the replay.

This commit refactors for the purpose as well as minor code change for
renaming of structure member.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20230107023214.29132-5-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# cccddec4 06-Jan-2023 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for cache position in tx packets

When sequence replay is enabled for media clock recovery, current
implementation caches sequence descriptors from packet descriptors in tx
packets. Helper function for the purpose do not necessarily have good
readability.

This commit refactors relevant functions by renaming structure members,
function name, and function local variables.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20230107023214.29132-4-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# f2bdee85 06-Jan-2023 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for pool position in rx packets

When scheduling transmission of rx packets, current implementation
fulfils packet descriptors after pooling sequence descriptors. It is for
packet queueing. Besides the implementations do not necessarily have
good readability.

This commit refactors them by adding function local variables and
function arguments.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20230107023214.29132-3-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 119c446a 06-Jan-2023 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for helper functions to pool sequence in rx packets

When scheduling transmission of rx packets, current implementation pools
sequence descriptors at first for media clock. Two methods are used for
the purpose depending on four cases, while the implementations do not
necessarily have good readability.

This commit refactors them by adding function pointers and functions
arguments.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20230107023214.29132-2-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 0cbbeaf3 24-Jun-2021 Christophe JAILLET <christophe.jaillet@wanadoo.fr>

ALSA: firewire-lib: Fix 'amdtp_domain_start()' when no AMDTP_OUT_STREAM stream is found

The intent here is to return an error code if we don't find what we are
looking for in the 'list_for_each_entry()' loop.

's' is not NULL if the list is empty or if we scan the complete list.
Introduce a new 'found' variable to handle such cases.

Fixes: 60dd49298ec5 ("ALSA: firewire-lib: handle several AMDTP streams in callback handler of IRQ target")
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Acked-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/9c9a53a4905984a570ba5672cbab84f2027dedc1.1624560484.git.christophe.jaillet@wanadoo.fr
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 3b86ec63 11-Jun-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: replace in_interrupt() with in_softirq()

Tasklet is used to handle isochronous context of 1394 OHCI. The explicit
usage of in_softirq() may be preferable than in_interrupt().

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210611083425.61204-1-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# b5b51996 09-Jun-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: obsolete workqueue for period update

The workqueue to notify PCM period elapse is not used anymore.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210610031733.56297-4-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 7ba5ca32 09-Jun-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: operate for period elapse event in process context

All of drivers in ALSA firewire stack processes two chances to process
isochronous packets in any isochronous context; in software IRQ context
for 1394 OHCI, and in process context of ALSA PCM application.

In the process context, callbacks of .pointer and .ack are utilized. The
callbacks are done by ALSA PCM core under acquiring lock of PCM substream,

In design of ALSA PCM core, call of snd_pcm_period_elapsed() is used for
drivers to awaken user processes from waiting for available frames. The
function voluntarily acquires lock of PCM substream, therefore it is not
called in the process context since it causes dead lock.

As a workaround to avoid the dead lock, all of drivers in ALSA firewire
stack uses workqueue to delegate the call. A variant of
snd_pcm_period_elapsed() without lock acquisition can obsolete the
workqueue.

An extra care is needed for the callback of .pointer since it's called
from snd_pcm_period_elapsed(). The isochronous context in Linux FireWire
subsystem is safe mostly for nested call except in software IRQ context.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210610031733.56297-3-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 987b705b 05-Jun-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: remove useless operations for kernel preemption

In all of drivers of ALSA firewire stack, the callback of .pointer and
.ack in snd_pcm_ops structure is done in acquired spin_lock of PCM
substream, therefore already under disabled kernel preemption.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210606025651.29970-1-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 8b6e2193 05-Jun-2021 Dan Carpenter <dan.carpenter@oracle.com>

ALSA: firewire-lib: fix error codes for allocation failure

Return -ENOMEM if kcalloc() fails. Currently the code returns success.

Fixes: f9e5ecdfc2c2 ("ALSA: firewire-lib: add replay target to cache sequence of packet")
Fixes: 6f24bb8a157c ("ALSA: firewire-lib: pool sequence of packet in IT context independently")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Acked-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/YLtyL4VoArwVLor1@mwanda
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# d360870a 27-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: support NO_PERIOD_WAKEUP in ALSA PCM runtime

Drivers of ALSA firewire stack can process packets for IT/IR context in
process context when the process operates ALSA PCM character device by
calling ioctl(2) with some requests. The ioctl requests are:

* SNDRV_PCM_IOCTL_HWSYNC
* SNDRV_PCM_IOCTL_SYNC_PTR
* SNDRV_PCM_IOCTL_REWIND
* SNDRV_PCM_IOCTL_FORWARD
* SNDRV_PCM_IOCTL_WRITEI_FRAMES
* SNDRV_PCM_IOCTL_READI_FRAMES
* SNDRV_PCM_IOCTL_WRITEN_FRAMES
* SNDRV_PCM_IOCTL_READN_FRAMES

This means that general application can process PCM frames apart from
hardware IRQ invocation, even if they are programmed by either IRQ-based
scheduling model or Timer-based scheduling model.

This commit add support for Timer-based scheduling model by allowing
PCM runtime to suppress both process wakeup per period and scheduling
hardware IRQ.

SNDRV_PCM_INFO_BATCH is obsoleted since ALSA IEC 61883-1/6 packet streaming
engine can report the number of transferred PCM frames within PCM period
boundary. The granularity equals to SYT_INTERVAL in blocking transmission.
In non-blocking transmission, it doesn't equal to SYT_INTERVAL but doesn't
exceed.

This patch is tested with PulseAudio, and --sched-model option of axfer
with fix against the issue reported at:

* https://lore.kernel.org/alsa-devel/687f9871-7484-1370-04d1-9c968e86f72b@linux.intel.com/#r

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210527123253.174315-1-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 2f21a177 27-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: transfer rx packets on-the-fly when replaying

Models in below series start transmission of packet after receiving the
sequence of packets:

* Digidesign Digi00x family
* RME Fireface series

Additionally, models in Tascam FireWire series start multiplexing PCM
frames into packets enough after receiving packets. It's required to
transfer packets on-the-fly for the above models according to nominal
sampling transfer frequency before starting sequence replay.

This commit allows drivers to decide whether the engine transfers packet
on-the-fly or not.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210527122611.173711-4-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 39c2649c 27-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: replay sequence of incoming packets for outgoing packets

ALSA IEC 61883-1/6 packet streaming engine uses pre-computed parameters
ideal for nominal sampling transfer frequency (STF) to transfer packets
to device since it was added 2011. As a result of user experience for a
decade, it is clear that the sequence is not suitable to some actual
devices. It takes the devices to generate noise, and causes any type of
discontinuity in the series of packet transferred from the device. It's
required for the engine to transfer packets according to effective STF.

The effective STF is given by media clock recovered by the sequence of
packet transferred from the target device. In the previous commit, the
sequence is already cached. The media clock recovery can be achieved by
analyzing the sequence.

In technological world, many ideas are proposed for media clock recovery.
However, the small part of them could be actually adopted in our case
since floating point arithmetic is not mostly available in Linux kernel
land.

This commit adopts the simple way from them; sequence replay, which means
that the sequence of parameters from incoming packet is used as is to
transfer outgoing packets. The media clock is not computed internally,
but the sequence of outgoing packet superficially looks to be generated by
the media clock.

The association between source and destination is decided when starting
AMDTP domain. When the target device supports a pair of isochronous packet
streams, the tx stream is source and the rx stream is destination. When it
supports two pair of streams, each of tx stream is associated to
corresponding rx stream in its order. When it supports less number of tx
streams than rx streams, the fist tx stream is selected for all of rx
streams. When it supports more tx streams than rx streams, the first tx
packet is associated to the rx stream.

As I noted in previous commit, the sequence of parameters from incoming
packet is different between devices, time to time. It is worse idea to
replay the sequence of parameters from a device for the sequence of
packet to the other devices even if they are in the same category of
device.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210527122611.173711-3-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# f9e5ecdf 27-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add replay target to cache sequence of packet

In design of audio and music unit in IEEE 1394 bus, feedback of
effective sampling transfer frequency (STF) is delivered by packets
transferred from device. The devices supported by ALSA firewire stack
are categorized to three groups regarding to it.

* Group 1:
* Echo Audio Fireworks board module
* Oxford Semiconductor OXFW971 ASIC
* Digidesign Digi00x family
* Tascam FireWire series
* RME Fireface series

* Group 2:
* BridgeCo. DM1000/DM1100/DM1500 ASICs for BeBoB solution
* TC Applied Technologies DICE ASICs

* Group 3:
* Mark of the Unicord FireWire series

In group 1, the effective STF is determined by the sequence of the number
of events per packet. In group 2, the sequence of presentation timestamp
expressed in syt field of CIP header is interpreted as well. In group 3,
the presentation timestamp is expressed in source packet header (SPH) of
each data block.

I note that some models doesn't take care of effective STF with large
internal buffer. It's reasonable to name it as group 0:

* Group 0
* Oxford Semiconductor OXFW970 ASIC

The effective STF is known to be slightly different from nominal STF for
all of devices, and to be different between the devices. Furthermore, the
effective STF is known to be shifted for long-period transmission. This
makes it hard for software to satisfy the effective STF when processing
packets to the device.

The effective STF is deterministic as a result of analyzing the batch of
packet transferred from the device. For the analysis, caching the sequence
of parameter in the packet is required.

This commit adds an option so that AMDTP domain structure takes AMDTP
stream structure to cache the sequence of parameters in packet transferred
from the device. The parameters are offset ticks of syt field against the
cycle to receive the packet and the number of data blocks per packet.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210527122611.173711-2-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# b7c7699b 23-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: obsolete callbacked member

The member of callbacked in AMDTP stream structure is not used anymore.
Instead, ready_processing member is used to wake up yielding task of user
process.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210524031346.50539-3-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# fb25dcc8 23-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: drop initial NODATA or empty packet

The devices based on BeBoB ASICs or the devices in Tascam FireWire
series transfer a batch of NODATA packet or empty packet in the beginning
of packet streaming. To avoid processing them, current implementation uses
an option to skip processing content of tx packet during some initial
cycles. However, the hard-coded number is not enough useful.

This commit drops content of packets till the packet includes any event
firstly. The function of option is to skip processing content of tx packet
with any event after dropping.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210524031346.50539-2-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 13d11f14 21-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for transfer delay

In later commit, transfer delay is used in both IR and IT contexts. This
commit refactors regardless of transfer delay.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210522013303.49596-7-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# c9f3ac2a 21-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for generation of data block sequence

This commit dissolves sequence generator in terms of the number of data
blocks per packet.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210522013303.49596-6-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# c79b7158 21-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for generation of syt sequence

This commit dissolves sequence generator in terms of syt offsets for
packet.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210522013303.49596-5-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 7ca7cdda 21-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for generation of packet descriptors

This commit refactors the arguments of helper function to generate the
descriptors of packet.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210522013303.49596-4-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 6f24bb8a 21-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: pool sequence of packet in IT context independently

Current implementation pools the sequence in AMDTP domain. This is convenient
regarding to memory usage and computation time, however inconvenient for the
devices such that several rx streams are expected to transfer timing
information independently.

This commit refactors to pool the sequence per rx packet stream.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210522013303.49596-3-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 8070d265 21-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add flag to unaware of syt in CIP header

Many devices are unaware of syt field in rx CIP for playback timing.

This commit adds a flag to cancel processing syt field. Actually,
syt calculation is required to decide the number of events per rx packet.
The flag put 0xffff to CIP header of rx packet. On the other hand,
The value of syt field in CIP header of tx packet is unavailable. The
sequence of packet descriptor for tx packet includes 0 for the offset
of syt field to avoid computation.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210522013303.49596-2-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 344f0f82 20-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: Fix uninitialized variable err issue

The check of error is just done for the case that CIP header is available.

This commit moves auto variable into the branch to process CIP header.

Addresses-Coverity: ("Uninitialized scalar variable")
Fixes: c09010eeb373 ("ALSA: firewire-lib: handle the case that empty isochronous packet payload for CIP")
Suggested-by: Colin Ian King <colin.king@canonical.com>
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210520130409.GA170303@workstation
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# bdaedca7 19-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: change waking up timing to process packets

When starting AMDTP domain, tasks in process context yields running CPU
till all of isochronous context get callback, with an assumption that
it's OK to process content of packet.

However several isochronous cycles are skipped to transfer rx packets, or
the content of rx packets are dropped, to manage the timing to start
processing the packets.

This commit changes the timing for tasks in process context to wake up
when processing content of packet is actually ready.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210520040154.80450-9-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 9b1fcd9b 19-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: start processing content of packet at the same cycle in several IT contexts

DICE ASICs support several pairs of isochronous packet streaming and
expect software to queue packets with the same timing information into
the same isochronous cycle.

This commit adds structure member to manage the cycle to start
processing packet in several IT contexts. The cycle is decided when
batch of isochronous cycle is skipped in callback to isochronous context
for IRQ target.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210520040154.80450-8-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# bd165079 19-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring to start several IT/IR contexts

It's several hundred cycles from starting isochronous contexts and the
actual cycle to start processing content of packet. It's useless to
start the context for IRQ target apart from the other contexts.

This commit refactors helper function to start AMDTP domain in the point.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210520040154.80450-7-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 26541cb1 19-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: skip initial packets instead of scheduling IR context

Current implementation of ALSA IEC 61883-1/6 packet streaming engine
allows drivers to decide isochronous cycle to start IR context. This
option is mainly used to avoid processing the sequence of packet with
some quirks; e.g. discontinuity of counter. However, it's inconvenient
to fail to continue packet processing when the target device doesn't
start transmission of packet till the decided cycle.

This commit changes the behaviour. As an alternative to the start cycle
for IR context, the cycle count to drop content of packet in the beginning
of IR context.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210520040154.80450-6-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# da3623ab 19-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: start processing content of packet at the same cycle in several IR contexts

DICE ASICs support several pairs of isochronous packet streaming. It's
convenient for drivers to process content of the packet in the same cycle
timing.

This commit adds structure member to manage the cycle to start
processing packet in several IR contexts. The cycle is decided in the
first callback of the IR contexts. The content of packet is dropped
till the cycle.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210520040154.80450-5-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 233dbbc7 19-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for selection of IT context header length

This commit refactors regarding to the size of CIP header.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210520040154.80450-4-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# c75f3678 19-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for calculation of context payload

It's convenient to calculate the size of context payload apart from
the size of isochronous packet payload.

This commit adds a helper function for it.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210520040154.80450-3-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 67d92ee7 19-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for size of CIP header

Some macros are added to refactor codes related to CIP header.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210520040154.80450-2-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 73246fc4 18-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: insert descriptor for skipped cycle

This commit fulfils sequence descriptors for skipped cycle when
it's one cycle. This is preparation for future integration.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210518130048.146596-9-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 705794c5 18-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: check cycle continuity

Within devices supported by drivers in ALSA firewire stack, OXFW-based
devices and Fireface devices are known to skip isochronous cycle for
packet transmission. The former is due to the jumbo payload quirk. The
latter is due to vendor protocol in which empty packet is not
transferred in blocking mode.

Although nothing to do just for handling events of the packet, packet
continuity is necessarily for media clock recovery. This commit checks
whether any cycle is continue or not.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210518130048.146596-8-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 4fd18787 18-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for check of CIP header about payload size

The size of CIP payload is now passed to helper function to parse CIP
header.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210518130048.146596-7-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# ebd2a647 18-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for parser of IR context header

This commit refactors regarding to function argument for the length of
isochronous packet payload.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210518130048.146596-6-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 3e106f4f 18-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for helper function to compute OHCI 1394 cycle

Some macros and functions are renamed so that they compute isochronous
cycle within maximum count of second in isochronous context of 1394
OHCI.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210518130048.146596-5-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# d32872f3 18-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for sequence descriptor'

A internal structure is used to gather parameters relevant to sequence
descriptor.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210518130048.146596-4-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# c09010ee 18-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: handle the case that empty isochronous packet payload for CIP

Two quadlets are at least included in isochronous packet payload for
Common Isochronous Packet (CIP) format in IEC 61883-1. However, it's
better to equip ALSA IEC 61883-1/6 packet streaming engine for contrary
packet.

This commit handles isochronous cycle to process such packet so that the
cycle is skipped.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210518130048.146596-3-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 6d60b7a3 18-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring to refer the same frame count per period in domain structure

The number of PCM frame per period is common between PCM substreams
handled in AMDTP stream in AMDTP domain.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210518130048.146596-2-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 6a3ce97d 18-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for jumbo payload quirk

A new macro is added to describe the maximum number of cycles to accept
cycle skip by jumbo payload quirk.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210518084557.102681-10-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# ffe66bbe 18-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib/motu: use int type for the value of bitwise OR with enumerator-constant

It brings some inconvenience in practice to use enumerated type for
variable to which bitwise OR with enumerator constant is assigned.

This commit replaces declarations of enumerated type with int type.

Suggested-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210518084557.102681-3-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 9981b20a 05-Jun-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: fix the context to call snd_pcm_stop_xrun()

In the workqueue to queue wake-up event, isochronous context is not
processed, thus it's useless to check context for the workqueue to switch
status of runtime for PCM substream to XRUN. On the other hand, in
software IRQ context of 1394 OHCI, it's needed.

This commit fixes the bug introduced when tasklet was replaced with
workqueue.

Cc: <stable@vger.kernel.org>
Fixes: 2b3d2987d800 ("ALSA: firewire: Replace tasklet with work")
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210605091054.68866-1-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 814b4312 13-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: fix amdtp_packet tracepoints event for packet_index field

The snd_firewire_lib:amdtp_packet tracepoints event includes index of
packet processed in a context handling. However in IR context, it is not
calculated as expected.

Cc: <stable@vger.kernel.org>
Fixes: 753e717986c2 ("ALSA: firewire-lib: use packet descriptor for IR context")
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210513125652.110249-6-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 1be4f21d 13-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: fix calculation for size of IR context payload

The quadlets for CIP header is handled as a part of IR context header,
thus it doesn't join in IR context payload. However current calculation
includes the quadlets in IR context payload.

Cc: <stable@vger.kernel.org>
Fixes: f11453c7cc01 ("ALSA: firewire-lib: use 16 bytes IR context header to separate CIP header")
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210513125652.110249-5-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 395f41e2 13-May-2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: fix check for the size of isochronous packet payload

The check for size of isochronous packet payload just cares of the size of
IR context payload without the size of CIP header.

Cc: <stable@vger.kernel.org>
Fixes: f11453c7cc01 ("ALSA: firewire-lib: use 16 bytes IR context header to separate CIP header")
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210513125652.110249-4-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 2b3d2987 09-Sep-2020 Takashi Iwai <tiwai@suse.de>

ALSA: firewire: Replace tasklet with work

The tasklet is an old API that should be deprecated, usually can be
converted to another decent API. In FireWire driver, a tasklet is
still used for offloading the AMDTP PCM stream handling. It can be
achieved gracefully with a work queued, too.

This patch replaces the tasklet usage in firewire-lib driver with a
simple work. The conversion is fairly straightforward but for the
in_interrupt() checks that are replaced with the check using the
current_work().

Note that in_interrupt() in amdtp_packet tracepoint is still kept as
is. This is the place that is probed by both softirq of 1394 OHCI and
a user task of a PCM application, and the work handling is already
filtered in amdtp_domain_stream_pcm_pointer().

Tested-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Acked-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20200909163659.21708-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 51bc581a 01-Sep-2020 Allen Pais <allen.lkml@gmail.com>

ALSA: firewire: convert tasklets to use new tasklet_setup() API

In preparation for unconditionally passing the
struct tasklet_struct pointer to all tasklet
callbacks, switch to using the new tasklet_setup()
and from_tasklet() to pass the tasklet pointer explicitly.

Signed-off-by: Romain Perier <romain.perier@gmail.com>
Signed-off-by: Allen Pais <allen.lkml@gmail.com>
Acked-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20200902040221.354941-3-allen.lkml@gmail.com
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 69efd5c4 07-May-2020 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: use sequence of syt offset and data block on pool in AMDTP domain

In previous commit, the sequence of syt offset and the number of data
blocks per packet is calculated for pool in AMDTP domain structure in
advance of processing outgoing packets.

This commit uses the sequence for outgoing packet processing to obsolete
per-stream processing of the sequence.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20200508043635.349339-11-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 1a4be183 07-May-2020 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: pool ideal sequence of syt offset and data block

In current implementation, sequence of syt offset and the number of data
blocks is generated when packets for outgoing stream are going to be
queued.

This commit generates and pools the sequence independently of the
processing of outgoing packets for future extension.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20200508043635.349339-10-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 25babf29 07-May-2020 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add cache for packet sequence to AMDTP domain structure

For future extension, storage is required to store packet sequence in
incoming AMDTP stream to recover media clock for outgoing AMDTP stream.

This commit adds the storage to AMDTP domain for this purpose. The
packet sequence is represented by 'struct seq_desc' which has two
members; syt_offset and the number of data blocks. The size of storage
is decided according to the size of packet queue.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20200508043635.349339-9-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 274fc355 07-May-2020 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for data block calculation

When calculating the number of data blocks per packet, some states are
stored in AMDTP stream structure. This is inconvenient when reuse the
calculation from non-stream structure.

This commit applies refactoring to helper function for the calculation
so that the function doesn't touch AMDTP stream structure.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20200508043635.349339-8-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 816d8482 07-May-2020 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for syt offset calculation

When calculating syt offset, some states are stored in AMDTP stream
structure. This is inconvenient when reuse the calculation from
non-stream structure.

This commit applies refactoring to helper function for the calculation
so that the function doesn't touch AMDTP stream structure.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20200508043635.349339-7-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 83cfb5c5 07-May-2020 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for syt computation

In current implementation for outgoing AMDTP packet, the value of syt
field in CIP header is computed when calculating syt offset. For
future extension, it's convenient to split the computation and
calculation.

This commit splits them.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20200508043635.349339-6-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# af86b0b1 07-May-2020 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for parameters of packet queue and IRQ timing

Although the parameter for packet queue and IRQ timing is calculated when
AMDTP stream starts, the calculated parameters are the same between
streams in AMDTP domain.

This commit moves the calculation and decide the parameters when AMDTP
domain starts.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20200508043635.349339-5-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 2472cfb3 07-May-2020 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add reference to domain structure from stream structure

In current implementation, AMDTP domain structure and AMDTP stream
structure has one way of reference from the former to the latter. For
future extension, bidirectional reference is needed.

This commit adds a member into stream structure to refer to domain
structure to which the stream belongs.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20200508043635.349339-4-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 10aa8e4a 07-May-2020 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: use macro for maximum value of second in 1394 OHCI isoc descriptor

In descriptor of isochronous context in 1394 OHCI, the field of second
has 3 bit, thus the maximum value is 8. The value is used for correct
cycle calculation.

This commit replaces hard-coded value with macro to obsolete magic
number.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20200508043635.349339-3-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# acfedcbe 18-Oct-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: postpone to start IR context

Some devices have a quirk to postpone transmission of isoc packet for
several dozen or hundred isoc cycles since configured to transmit.
Furthermore, some devices have a quirk to transmit isoc packet with
discontinued data of its header.

In 1394 OHCI specification, software allows to start isoc context with
certain isoc cycle. Linux firewire subsystem has kernel API to use it
as well.

This commit uses the functionality of 1394 OHCI controller to handle
the quirks. At present, this feature is convenient to ALSA bebob and
fireface driver. As a result, some devices can be safely handled, as
long as I know:
- MAudio FireWire solo
- MAudio ProFire Lightbridge
- MAudio FireWire 410
- Roland FA-66

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20191018061911.24909-7-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 60dd4929 18-Oct-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: handle several AMDTP streams in callback handler of IRQ target

This commit changes AMDTP domain to run on an IT context of 1394 OHCI as
IRQ target. No hardware interrupt is scheduled for the other isoc
contexts. All of the isoc context are processed in a callback for an isoc
context of IRQ target.

The IRQ target is automatically selected from a list of AMDTP streams,
thus users of AMDTP domain should add an AMDTP stream for IT context
at least.

The reason to select IT context as IRQ target is that the IT context
runs on local 1394 OHCI controller and it can be used as reliable,
constant IRQ generator. On the other hand, IR context can include skip
cycle according to isoc packet transferred by device.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20191018061911.24909-6-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 813dfbd6 18-Oct-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: cancel flushing isoc context in the laste step to process context callback

The aim of AMDTP domain is to process several isoc context in the same
time. However, current implementation is against this idea because it
flushes each isoc context in the end of processing context callback.

This commit cancels it.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20191018061911.24909-5-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# e6dcc92f 18-Oct-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: replace ack callback to flush isoc contexts in AMDTP domain

An isoc context for AMDTP stream is flushed to queue packet
by a call of pcm.ack. This commit extends this for AMDTP
domain.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20191018061911.24909-4-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# f890f9a0 18-Oct-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: replace pointer callback to flush isoc contexts in AMDTP domain

An isoc context for AMDTP stream is flushed to queue packet
by a call of pcm.pointer. This commit extends this for AMDTP
domain.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20191018061911.24909-3-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# f706df4f 17-Oct-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: tune the minimum available size of PCM period

In IEC 61883-1/6, one isoc packet can transfer events up to the value of
syt interval. This comes from the interval of isoc cycle. As 1394 OHCI
controller can generate hardware IRQ per isoc packet, the interval is
calculated as 125 usec.

In IEC 61883-1/6, two ways of transmission is described; blocking and
non-blocking methods. In blocking method, the sequence of packet includes
'empty' or 'NODATA' packets which include no events. In non-blocking
method, the number of events per packet is variable up to the syt
interval.

This commit uses double of the value of syt interval as minimum available
size of PCM period due to the above protocol design.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20191017155424.885-13-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 99921ec6 17-Oct-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: tune the maximum available size of PCM period

Linux driver for 1394 OHCI controller voluntarily flushes isoc context
when total size of accumulated context header reached PAGE_SIZE. This
kicks tasklet for the isoc context. This is inconvenient to process
runtime of PCM substream.

This commit adds a restriction of the maximum size of PCM period to
avoid this behaviour.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20191017155424.885-12-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# e229853d 17-Oct-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: schedule hardware IRQ according to the size of PCM period

ALSA IEC 61883-1/6 packet streaming engine controls 1394 OHCI controller
to generate hardware IRQ for fixed number of isochronous packets (=16)
since its first commit.

This commit allow the engine to generate it for variable period according
to the number of event to handle. For outgoing stream, internal
calculator is used to check the accumulated events. For incoming stream,
the number of data block in the packet of stream is used to check the
accumulated events. When it's unavailable, fixed number of packet
roughly calculated in advance is used instead of event counting.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20191017155424.885-11-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# a0e02331 17-Oct-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: use variable size of queue for isoc packets instead of fixed size

The number of packets in packet buffer has been fixed number (=48) since
first commit of ALSA IEC 61883-1/6 packet streaming engine.

This commit allows the engine to use variable number of packets in the
buffer. The size is calculated by a parameter in AMDTP domain structure
surely to store the number of events in the packets of buffer. Although
the value of parameter is expected to come from 'period size' parameter
of PCM substream, at present 48 is still used.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20191017155424.885-2-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# d68c3123 07-Oct-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add a member into AMDTP domain for events per period

In IEC 61883-6, it's called as 'event' what has presentation time
represented by timestamp in CIP header. Although the ratio of the number
of event against the number of data block is different depending on
event data type represented by the specific field in CIP header, it's
just one in the most cases supported by ALSA IEC 61883-1/6 engine.

In 1394 OHCI specification, applications can schedule hardware IRQ
by configuring descriptor with IRQ flag for packet against each
isochronous cycle. For future commit, I use the hardware IRQ for
isoc IT context to acknowledge the elapse of PCM period for both
playback/capture directions on AMDTP streams in the same domain.

This commit is a preparation for the above idea. This commit adds
a member into AMDTP domain structure to record the number of PCM frames.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20191007110532.30270-2-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 8d0d5c3f 06-Sep-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: remove WARN_ON() at destruction of AMDTP domain

The destructor of AMDTP domain has WARN_ON() for the list of associated
AMDTP stream. Although this reminds a case that developers forget to
program consumer drivers to stop AMDTP domain, it hits when AMDTP domain
is not initialized yet. This occurs when initialization of sound card
fails as well and it's superfluous.

This commit removes the WARN_ON. Although the API to AMDTP domain does
nothing, it's left for future usage.

Fixes: 3ec3d7a3ff106 ("ALSA: firewire-lib: add AMDTP domain structure to handle several isoc contexts")
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20190906131414.15370-1-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# f155e79a 29-Aug-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: fix isoc cycle count to which rx packet is scheduled

When introducing the list of packet descriptor, for rx packet, the
calculation of scheduled isoc cycle is omitted. This commit fixes the
bug.

Fixes: f4f6ae7b7c1f ("ALSA: firewire-lib: use packet descriptor for IT context")
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 74f94e41 04-Aug-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: localize kernel APIs to start/stop each AMDTP stream

As a result to support AMDTP domain, no drivers call kernel APIs to
start/stop each AMDTP stream. This commit localize these APIs.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 9b4702b0 04-Aug-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add a kernel API to start AMDTP streams in AMDTP domain

This commit adds a kernel API to start a couple of isochronous contexts
for some AMDTP streams.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 157a53ee 04-Aug-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add a kernel API to add AMDTP stream into AMDTP domain

This commit adds a kernel API to insert AMDTP stream to list in AMDTP
domain.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 6261f90b 04-Aug-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add a kernel API to stop a couple of AMDTP streams in AMDTP domain

This commit adds a kernel API to stop a couple of isochronous contexts
for AMDTP streams. The API is not protected with any lock primitive.
Callers should use this with enough lock against concurrent access.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 3ec3d7a3 04-Aug-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add AMDTP domain structure to handle several isoc contexts

This commit adds 'struct amdtp_domain' structure. This structure
has list of instance of AMDTP stream to handle a couple of
isochronous contexts.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 9a738ad1 21-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: process payload of isoc context according to packet descriptors

This commit changes each of data block processing layer so that it
receives list of packet descriptor.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# d2c104a3 21-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: pass packet descriptor to data block processing layer

This commit changes signature of callback function to call data block
processing layer with packet descriptor. At present, the layer is called
per packet.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 0f5cfcb2 21-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring to process context payloads

This is code refactoring for common processing for payloads of
ishocornous context.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 5e2ece0f 21-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring to process PCM substream

This is code refactoring to separate PCM substream processing from packet
queueing.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 753e7179 21-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: use packet descriptor for IR context

This commit uses packet descriptor to parse headers of IR context and
parse timing information for sequence transferred by target device.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# f4f6ae7b 21-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: use packet descriptor for IT context

This commit uses packet descriptor to parse headers of IT context and
generate timing information for ideal sequence.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 04130cf8 21-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add list of packet descriptor

In current implementation of ALSA IEC 61883-1/6 packet streaming engine,
16 packets are handled in one interrupt of isochronous context of OHCI
1394.

Overall packet processing runs for each. However, this is not better in
a point to split the processing into several parts.

This commit is an attempt to add intermediate representation for
parameters required for the parts.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 600c8018 21-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: pass no syt information to data block processing layer

In a previous commit, the variable passed from packet streaming layer
for syt variable is useless. This commit obsoletes it.

In my future work, the syt information is passed to data block processing
layer by another way.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 3baf3053 21-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add syt_override member for some protocols

Some protocols don't use syt field of CIP header to represent
presentation timestamp. For such protocol, ALSA IEC 61883-1/6
packet streaming engine uses a pointer into local variable for
the value of syt to call data block processing layer. However,
it can decide the value when initializing packet streaming
layer.

This commit adds 'syt_override' member for packet streaming
layer.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# a35463d1 21-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: operate data block counter in top level of processing for IR context

In ALSA IEC 61883-1/6 packet streaming engine, two types of data block
counter are supported. This commit applies code refactoring to make it
explicitly for IR context.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 860d798c 21-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: operate data block counter in top level of processing for IT context

In ALSA IEC 61883-1/6 packet streaming engine, two types of data block
counter are supported. This commit applies code refactoring to make it
explicitly for IT context.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# ab754812 21-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: pass data block counter to data block processing layer

This is a preparation for future commit that 'struct
amdtp_stream.data_block_count' does not represent the value of
data block count for current data block.

However, data block count is required for calculation of sequence index
in sequence-multiplied data channel. Some of data block processing layer
require it; e.g. for AM824 data block.

This commit passes data block count to the processing layer.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# cdefaa44 21-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: pass data block count as an argument to tracepoints event

This is a preparation for future commit that 'struct
amdtp_stream.data_block_count' does not represent the value of
data block count for current data block.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 588f2e2c 21-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: obsolete ctx_data.tx.first_dbc with CIP_UNALIGHED_DBC flag

Recent firmware for Fireworks board module have a quirk to start
transmission of CIP with non-zero value for its data block counter.
In current implementation of ALSA firewire stack, the quirk is handled
by 'struct amdtp_stream.ctx_data.tx.first_dbc' with value 0x02. However,
the value comes from reverse engineering. It's better to handle this
quirk without the explicit value.

In a process to parse CIP header, the quirk of data block counter
affects decision of sequence index in sequence-multiplexed data channel;
i.e. MIDI conformant data channel. In Fireworks, the index is decided
by the number of data blocks from top of the same CIP, thus the value
of data block counter is useless.

This commit adds CIP_UNALIGHED_DBC flag and obsoletes the explicit
value for this quirk.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 0dcb4efb 07-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for local variables

It's better to use int type for loop index. For consistency, the name
of local variable for the number of data block should be plural.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 02394af3 07-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for post operation to data block counter

As a result of former commits, post operation to data block count for
cases without CIP_DBC_IS_END_EVENT can be done just with
data_block_counter member of amdtp_stream structure.

This commit adds code refactoring to obsolete local variable for
data block counter.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# b8b0e24c 07-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring for error path of parser for CIP header

When a parser for CIP header returns -EAGAIN, no extra care is needed
to probe tracepoints event.

This commit adds code refactoring for the error path.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 213fa989 07-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: fix different data block counter between probed event and transferred isochronous packet

For IT context, tracepoints event is probed after calculating next data
block counter. This brings difference of data block counter between
the probed event and actual isochronous packet.

This commit fixes it.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 4ece7f6f 07-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: fix initial value of data block count for IR context without CIP_DBC_IS_END_EVENT

For IR context, ALSA IEC 61883-1/6 engine uses initial value of data
block counter as UINT_MAX, to detect first isochronous packet in the
middle of packet streaming.

At present, when CIP_DBC_IS_END_EVENT is not used (i.e. for drivers except
for ALSA fireworks driver), the initial value is used as is for
tracepoints event. However, the engine can detect the value of dbc field
in the payload of first isochronous packet and the value should be assigned
to the event.

This commit fixes the bug.

Fixes: 76864868dbab ("ALSA: firewire-lib: cache next data_block_counter after probing tracepoints event for IR context")
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 7fbf9096 07-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib/fireface: fix initial value of data block counter for IR context with CIP_NO_HEADER

For IR context, ALSA IEC 61883-1/6 engine uses initial value of data
block counter as UINT_MAX, to detect first isochronous packet in the
middle of packet streaming.

At present, when CIP_NO_HEADER is used (i.e. for ALSA fireface driver),
the initial value is used for tracepoints event. 0x00 should be
for the event when the initial value is UINT_MAX because isochronous
packets with CIP_NO_HEADER option has no field for data block count.

This commit fixes the bug.

Fixes: 76864868dbab ("ALSA: firewire-lib: cache next data_block_counter after probing tracepoints event for IR context")
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 0ebf3ceb 07-Jul-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: fix invalid length of rx packet payload for tracepoint events

Although CIP header is handled as context header, the length of isochronous
packet includes two quadlets for its payload. In tracepoints event the
value of payload_quadlets should includes the two quadlets. But at present
it doesn't.

This commit fixes the bug.

Fixes: b18f0cfaf16b ("ALSA: firewire-lib: use 8 byte packet header for IT context to separate CIP header from CIP payload")
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 973b059c 27-Jun-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: fix to process MIDI conformant data channel for AM824 format

In IEC 61883-6, 8 MIDI data streams are multiplexed into single MIDI
conformant data channel. The index of stream is calculated by modulo 8
of the value of data block counter. Therefore data block processing
layer requires valid value of data block counter.

In recent changes of ALSA IEC 61883-1/6 engine, the value of data block
counter is changed before calling data block processing layer. This
brings miss detection of MIDI messages in non-blocking transmission
method is used.

This commit fixes the bug by changing chached data block counter after
calling data block processing layer.

Fixes: e335425b6596 ("ALSA: firewire-lib: split helper function to check incoming CIP header")
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 76864868 27-Jun-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: cache next data_block_counter after probing tracepoints event for IR context

For debugging purpose, ALSA IEC 61883-1/6 engine has tracepoints event.
In current implementation, next data block counter is stored as current
data block counter before probing the event for IR isoc context. It's not
good to check current packet parameter.

This commit changes to assign the next data block counter after probing
the event.

Besides, Fireworks devices has a quirk to transfer isoc packet with
data block counter for the last data block. For this quirk, the
assignment is done before calling data block processing layer.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# da607e19 29-May-2019 Thomas Gleixner <tglx@linutronix.de>

treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 345

Based on 1 normalized pattern(s):

licensed under the terms of the gnu general public license version 2

extracted by the scancode license scanner the SPDX license identifier

GPL-2.0-only

has been chosen to replace the boilerplate/reference in 88 file(s).

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Alexios Zavras <alexios.zavras@intel.com>
Reviewed-by: Allison Randal <allison@lohutok.net>
Cc: linux-spdx@vger.kernel.org
Link: https://lkml.kernel.org/r/20190530000437.521539229@linutronix.de
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>


# 98e3e43b 24-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: refactoring to obsolete IR packet handler

As a result of heavy refactoring based on IR context header, the packet
handler becomes simpler.

This commit merges the packet handler into function for IR context
callback. The logic to parse IR context header and tracepoints event is
split to a function.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 6bc1a269 24-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: refactoring to obsolete IT packet handler

As a result of heavy refactoring based on IT packet header, the packet
handler becomes simpler.

This commit merges the packet handler into function for IT context
callback. The logic to build IT packet header and tracepoints event is
split to a function.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 3c194923 24-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: fix data block counter for incoming packet without CIP header

The value of data block counter is not calculated for incoming packet
without CIP header. This commit fixes the bug.

Fixes: 947b437e1263 ("ALSA: firewire-lib: unify packet handler for IR context")
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# b18f0cfa 23-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: use 8 byte packet header for IT context to separate CIP header from CIP payload

In Linux firewire subsystem, for IT context, some quadlets of isochronous
packet payload can be indicated as a part of packet header to queue to
the context.

This commit uses the packet header to split CIP headers from CIP
payload. As a result, regardless of CIP or non-CIP, context payload
includes data blocks only.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 6007bf54 23-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code refactoring to queueing packets

This commit is a preparation to queue IT packet with header. To enable
packet handler to fill the header, this commit uses kernel stack for
data structure of packet parameter in several part of this file.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 16be4589 23-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: unify packet handler for IT context

The handlers for packet with CIP and without CIP include common codes.
This commit unifies them and remove an member for pointer to callback
function from data structure.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 252219c7 23-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: split helper function to generate CIP header

This is minor code refactoring to split a function to generate CIP
header.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 947b437e 22-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: unify packet handler for IR context

Usage of 16 bytes IR context header allows to handle context payload by
the same code for with-CIP and without-CIP packets.

This commit unifies both handlers of with-CIP and without-CIP packets.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# f11453c7 22-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: use 16 bytes IR context header to separate CIP header

In IR context, some quadlets of packet payload can be included into
context header. This is good for packet with CIP header because the
context payload buffer can includes data blocks only for with-CIP and
without-CIP pakets.

This commit uses 16 bytes IR context header for this purpose.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# e335425b 22-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: split helper function to check incoming CIP header

A parser for CIP header in incoming packet is enough large.

This commit splits it into a helper function to better looks of packet
handler.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 8a400b99 22-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: compute pointer to payload buffer in context handler

The value of pointer to payload buffer is computed in each packet
handler, however the pointer can be decided before call of packet
handler.

This commit adds an argument for the pointer to the packet handler to
reduce codes to compute for the pointer.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 6f3c07d0 22-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: calculate the length of packet payload in packet handler

In current packet handler, the length of payload is given as an argument
of callback function, however this value is just required to process
payload of transferred isoc packet, thus just for IR context.

This commit replaces the argument for payload of packet with the
argument of context header. As a result, the length of payload is
computed in packet handler for IR context.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 07ea238c 22-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: use clear name for variable of CIP header

This commit is to distinguish variable of CIP header from variable of
isochronous context header.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 26cd1e58 21-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: use IT context header to compute cycle count for scheduling packet

In IT context, header includes information of cycle to have processed
queued packet.

This commit uses the per-packet information to compute the cycle for
packet to be queued, instead of callback argument. In current
implementaion of ALSA IEC 61883-1/6 packet streaming engine, 48 packets
are queued at first to skip recent cycle. When IT context calls handler,
cycle information in header plus 48 cycle means the cycle to be going
to queue packet.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# df9160b9 21-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: obsolete macro for header of IT context

The header size parameter of 1394 OHCI IT context has no effect.
Besides the header size of queued packet is different from context
header.

This commit cleans codes relevant to these two headers.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# fce9b013 21-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add helper function to cancel context inner callback handler

In callback handler of isochronous context for both direction, there're
common codes to cancel context.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# d3d10a4a 21-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: use union for directional parameters

Some parameters of struct amdtp_stream is dependent on direction.

This commit uses union for such parameters to distinguish from
common parameters.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 8d3f1fdf 19-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: unify tracing events to 'amdtp_packet' event

Now four events of this module have the same arguments and probe timing.
This commit adds a new event, 'amdtp_packet', and replace them. Filtering
functionality of tracing framework is available to pick up events for
inbound/outbound isochronous packets.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# a8520f3e 19-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: use the same unit for payload argument in tracing events

The most of tracing event in this module have the size of payload in
byte unit, however 'in_packet_without_header' event have the argument
in quadlet unit.

This commit change the unit for argument to be consistent.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 64d0bf4d 19-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add data_blocks/data_block_counter parameter to in_packet/out_packet tracing events

Tracing events for packets without CIP header have a parameter of
data_blocks/data_block_counter, but events for packets with CIP header
don't. This is not good to unify these events.

This commit adds the missing parameters to the events. In timing to
probe 'in_packet' event, data_blocks and data_block_counter are not
calculated yet. This commit also changes the timing.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 4a10cecc 19-May-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: use the same type of argument for CIP header for tracing event

An argument for CIP header in 'in_packet' event is not the same type in
'out_packet' event. This is not good to unify these events.

This commit uses the same type of argument for these events.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# cc4f8e91 17-Mar-2019 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: use 8 byte header for IR context to get isochronous cycle

In kernel API of Linux FireWire subsystem, handlers of isochronous
receive (IR) context can get context headers as an argument of
callback. When 4 byte header is used, the context header includes
isochronous packet header for each packet. When 8 byte header is
used, it includes isochronous cycle as well.

ALSA IEC 61883-1/6 engine uses 4 byte header, and computes isochronous
cycle from the cycle of interrupt. The usage of 8 byte header can
obsolete the computation.

Furthermore, this change works well for a case that a series of
packet in one interrupt includes skipped isochronous cycle,

This commit uses 8 byte header to handle isochronous cycle.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# ada79fa5 15-Dec-2018 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: fix wrong handling payload_length as payload_quadlet

In IEC 61883-1/6 engine of ALSA firewire stack, a packet handler has a
second argument for 'the number of bytes in payload of isochronous
packet'. However, an incoming packet handler without CIP header uses the
value as 'the number of quadlets in the payload'. This brings userspace
applications to receive the number of PCM frames as four times against
real time.

This commit fixes the bug.

Cc: <stable@vger.kernel.org> # v4.12+
Fixes: 3b196c394dd ('ALSA: firewire-lib: add no-header packet processing')
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 826b5de9 30-Oct-2018 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: fix insufficient PCM rule for period/buffer size

In a former commit, PCM constraint based on LCM of SYT_INTERVAL was
obsoleted with PCM rule. However, the new PCM rule brings -EINVAL in
some cases that max/min values of size of buffer/period is not
multiples of one of values of SYT_INTERVAL. For example, pulseaudio
always fail to configure PCM substream.

This commit changes strategy for the PCM rule. Although the buggy rules
had a single dependency (rate from period, period from rate, rate from
buffer, buffer from rate), a revised rule has double dependencies
(period from period/rate, buffer from buffer/rate). A step of value is
calculated with table of SYT_INTERVAL and list of available rates. This
prevents interval template which brings -EINVAL to a call of
snd_interval_refine().

Fixes: 5950229582bc('ALSA: firewire-lib: add PCM rules to obsolete PCM constraints based on LCM of SYT_INTERVAL')
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 59502295 30-Sep-2018 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add PCM rules to obsolete PCM constraints based on LCM of SYT_INTERVAL

In blocking mode of IEC 61883-1/6, when one isochronous packet includes
data for events, the data is for the same number of events as
SYT_INTERVAL decided according to sampling transmission frequency (SFC).

IEC 61883-1/6 engine of ALSA firewire stack applies constraints of
period and buffer size of PCM intermediate buffer of PCM substream.
At present, this constraint is designed to round the size up/down to
32 frames. This value comes from the least common multiple (LCM) of
SYT_INTERVAL. Although this looks to work well, in lower sampling
rate, applications are not allowed to set size of period quite near
period time constraint (at present 5 msec per period).

This commit adds PCM rules for period/buffer size and rate to obsoletes
the constraints based on LCM.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 52759c09 29-Apr-2018 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: dice: fix kernel NULL pointer dereference due to invalid calculation for array index

At a commit f91c9d7610a ('ALSA: firewire-lib: cache maximum length of
payload to reduce function calls'), maximum size of payload for tx
isochronous packet is cached to reduce the number of function calls.

This cache was programmed to updated at a first callback of ohci1394 IR
context. However, the maximum size is required to queueing packets before
starting the isochronous context.

As a result, the cached value is reused to queue packets in next time to
starting the isochronous context. Then the cache is updated in a first
callback of the isochronous context. This can cause kernel NULL pointer
dereference in a below call graph:

(sound/firewire/amdtp-stream.c)
amdtp_stream_start()
->queue_in_packet()
->queue_packet()
(drivers/firewire/core-iso.c)
->fw_iso_context_queue()
->struct fw_card_driver.queue_iso()
(drivers/firewire/ohci.c)
= ohci_queue_iso()
->queue_iso_packet_per_buffer()
buffer->pages[page]

The issued dereference occurs in a case that:
- target unit supports different stream formats for sampling transmission
frequency.
- maximum length of payload for tx stream in a first trial is bigger
than the length in a second trial.

In this case, correct number of pages are allocated for DMA and the 'pages'
array has enough elements, while index of the element is wrongly calculated
according to the old value of length of payload in a call of
'queue_in_packet()'. Then it causes the issue.

This commit fixes the critical bug. This affects all of drivers in ALSA
firewire stack in Linux kernel v4.12 or later.

[12665.302360] BUG: unable to handle kernel NULL pointer dereference at 0000000000000030
[12665.302415] IP: ohci_queue_iso+0x47c/0x800 [firewire_ohci]
[12665.302439] PGD 0
[12665.302440] P4D 0
[12665.302450]
[12665.302470] Oops: 0000 [#1] SMP PTI
[12665.302487] Modules linked in: ...
[12665.303096] CPU: 1 PID: 12760 Comm: jackd Tainted: P OE 4.13.0-38-generic #43-Ubuntu
[12665.303154] Hardware name: /DH77DF, BIOS KCH7710H.86A.0069.2012.0224.1825 02/24/2012
[12665.303215] task: ffff9ce87da2ae80 task.stack: ffffb5b8823d0000
[12665.303258] RIP: 0010:ohci_queue_iso+0x47c/0x800 [firewire_ohci]
[12665.303301] RSP: 0018:ffffb5b8823d3ab8 EFLAGS: 00010086
[12665.303337] RAX: ffff9ce4f4876930 RBX: 0000000000000008 RCX: ffff9ce88a3955e0
[12665.303384] RDX: 0000000000000000 RSI: 0000000034877f00 RDI: 0000000000000000
[12665.303427] RBP: ffffb5b8823d3b68 R08: ffff9ce8ccb390a0 R09: ffff9ce877639ab0
[12665.303475] R10: 0000000000000108 R11: 0000000000000000 R12: 0000000000000003
[12665.303513] R13: 0000000000000000 R14: ffff9ce4f4876950 R15: 0000000000000000
[12665.303554] FS: 00007f2ec467f8c0(0000) GS:ffff9ce8df280000(0000) knlGS:0000000000000000
[12665.303600] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[12665.303633] CR2: 0000000000000030 CR3: 00000002dcf90004 CR4: 00000000000606e0
[12665.303674] Call Trace:
[12665.303698] fw_iso_context_queue+0x18/0x20 [firewire_core]
[12665.303735] queue_packet+0x88/0xe0 [snd_firewire_lib]
[12665.303770] amdtp_stream_start+0x19b/0x270 [snd_firewire_lib]
[12665.303811] start_streams+0x276/0x3c0 [snd_dice]
[12665.303840] snd_dice_stream_start_duplex+0x1bf/0x480 [snd_dice]
[12665.303882] ? vma_gap_callbacks_rotate+0x1e/0x30
[12665.303914] ? __rb_insert_augmented+0xab/0x240
[12665.303936] capture_prepare+0x3c/0x70 [snd_dice]
[12665.303961] snd_pcm_do_prepare+0x1d/0x30 [snd_pcm]
[12665.303985] snd_pcm_action_single+0x3b/0x90 [snd_pcm]
[12665.304009] snd_pcm_action_nonatomic+0x68/0x70 [snd_pcm]
[12665.304035] snd_pcm_prepare+0x68/0x90 [snd_pcm]
[12665.304058] snd_pcm_common_ioctl1+0x4c0/0x940 [snd_pcm]
[12665.304083] snd_pcm_capture_ioctl1+0x19b/0x250 [snd_pcm]
[12665.304108] snd_pcm_capture_ioctl+0x27/0x40 [snd_pcm]
[12665.304131] do_vfs_ioctl+0xa8/0x630
[12665.304148] ? entry_SYSCALL_64_after_hwframe+0xe9/0x139
[12665.304172] ? entry_SYSCALL_64_after_hwframe+0xe2/0x139
[12665.304195] ? entry_SYSCALL_64_after_hwframe+0xdb/0x139
[12665.304218] ? entry_SYSCALL_64_after_hwframe+0xd4/0x139
[12665.304242] ? entry_SYSCALL_64_after_hwframe+0xcd/0x139
[12665.304265] ? entry_SYSCALL_64_after_hwframe+0xc6/0x139
[12665.304288] ? entry_SYSCALL_64_after_hwframe+0xbf/0x139
[12665.304312] ? entry_SYSCALL_64_after_hwframe+0xb8/0x139
[12665.304335] ? entry_SYSCALL_64_after_hwframe+0xb1/0x139
[12665.304358] SyS_ioctl+0x79/0x90
[12665.304374] ? entry_SYSCALL_64_after_hwframe+0x72/0x139
[12665.304397] entry_SYSCALL_64_fastpath+0x24/0xab
[12665.304417] RIP: 0033:0x7f2ec3750ef7
[12665.304433] RSP: 002b:00007fff99e31388 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
[12665.304465] RAX: ffffffffffffffda RBX: 00007fff99e312f0 RCX: 00007f2ec3750ef7
[12665.304494] RDX: 0000000000000000 RSI: 0000000000004140 RDI: 0000000000000007
[12665.304522] RBP: 0000556ebc63fd60 R08: 0000556ebc640560 R09: 0000000000000000
[12665.304553] R10: 0000000000000001 R11: 0000000000000246 R12: 0000556ebc63fcf0
[12665.304584] R13: 0000000000000000 R14: 0000000000000007 R15: 0000000000000000
[12665.304612] Code: 01 00 00 44 89 eb 45 31 ed 45 31 db 66 41 89 1e 66 41 89 5e 0c 66 45 89 5e 0e 49 8b 49 08 49 63 d4 4d 85 c0 49 63 ff 48 8b 14 d1 <48> 8b 72 30 41 8d 14 37 41 89 56 04 48 63 d3 0f 84 ce 00 00 00
[12665.304713] RIP: ohci_queue_iso+0x47c/0x800 [firewire_ohci] RSP: ffffb5b8823d3ab8
[12665.304743] CR2: 0000000000000030
[12665.317701] ---[ end trace 9d55b056dd52a19f ]---

Fixes: f91c9d7610a ('ALSA: firewire-lib: cache maximum length of payload to reduce function calls')
Cc: <stable@vger.kernel.org> # v4.12+
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 6aa7de05 23-Oct-2017 Mark Rutland <mark.rutland@arm.com>

locking/atomics: COCCINELLE/treewide: Convert trivial ACCESS_ONCE() patterns to READ_ONCE()/WRITE_ONCE()

Please do not apply this to mainline directly, instead please re-run the
coccinelle script shown below and apply its output.

For several reasons, it is desirable to use {READ,WRITE}_ONCE() in
preference to ACCESS_ONCE(), and new code is expected to use one of the
former. So far, there's been no reason to change most existing uses of
ACCESS_ONCE(), as these aren't harmful, and changing them results in
churn.

However, for some features, the read/write distinction is critical to
correct operation. To distinguish these cases, separate read/write
accessors must be used. This patch migrates (most) remaining
ACCESS_ONCE() instances to {READ,WRITE}_ONCE(), using the following
coccinelle script:

----
// Convert trivial ACCESS_ONCE() uses to equivalent READ_ONCE() and
// WRITE_ONCE()

// $ make coccicheck COCCI=/home/mark/once.cocci SPFLAGS="--include-headers" MODE=patch

virtual patch

@ depends on patch @
expression E1, E2;
@@

- ACCESS_ONCE(E1) = E2
+ WRITE_ONCE(E1, E2)

@ depends on patch @
expression E;
@@

- ACCESS_ONCE(E)
+ READ_ONCE(E)
----

Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: davem@davemloft.net
Cc: linux-arch@vger.kernel.org
Cc: mpe@ellerman.id.au
Cc: shuah@kernel.org
Cc: snitzer@redhat.com
Cc: thor.thayer@linux.intel.com
Cc: tj@kernel.org
Cc: viro@zeniv.linux.org.uk
Cc: will.deacon@arm.com
Link: http://lkml.kernel.org/r/1508792849-3115-19-git-send-email-paulmck@linux.vnet.ibm.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>


# 4a9bfafc 11-Jun-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: Fix stall of process context at packet error

At Linux v3.5, packet processing can be done in process context of ALSA
PCM application as well as software IRQ context for OHCI 1394. Below is
an example of the callgraph (some calls are omitted).

ioctl(2) with e.g. HWSYNC
(sound/core/pcm_native.c)
->snd_pcm_common_ioctl1()
->snd_pcm_hwsync()
->snd_pcm_stream_lock_irq
(sound/core/pcm_lib.c)
->snd_pcm_update_hw_ptr()
->snd_pcm_udpate_hw_ptr0()
->struct snd_pcm_ops.pointer()
(sound/firewire/*)
= Each handler on drivers in ALSA firewire stack
(sound/firewire/amdtp-stream.c)
->amdtp_stream_pcm_pointer()
(drivers/firewire/core-iso.c)
->fw_iso_context_flush_completions()
->struct fw_card_driver.flush_iso_completion()
(drivers/firewire/ohci.c)
= flush_iso_completions()
->struct fw_iso_context.callback.sc
(sound/firewire/amdtp-stream.c)
= in_stream_callback() or out_stream_callback()
->...
->snd_pcm_stream_unlock_irq

When packet queueing error occurs or detecting invalid packets in
'in_stream_callback()' or 'out_stream_callback()', 'snd_pcm_stop_xrun()'
is called on local CPU with disabled IRQ.

(sound/firewire/amdtp-stream.c)
in_stream_callback() or out_stream_callback()
->amdtp_stream_pcm_abort()
->snd_pcm_stop_xrun()
->snd_pcm_stream_lock_irqsave()
->snd_pcm_stop()
->snd_pcm_stream_unlock_irqrestore()

The process is stalled on the CPU due to attempt to acquire recursive lock.

[ 562.630853] INFO: rcu_sched detected stalls on CPUs/tasks:
[ 562.630861] 2-...: (1 GPs behind) idle=37d/140000000000000/0 softirq=38323/38323 fqs=7140
[ 562.630862] (detected by 3, t=15002 jiffies, g=21036, c=21035, q=5933)
[ 562.630866] Task dump for CPU 2:
[ 562.630867] alsa-source-OXF R running task 0 6619 1 0x00000008
[ 562.630870] Call Trace:
[ 562.630876] ? vt_console_print+0x79/0x3e0
[ 562.630880] ? msg_print_text+0x9d/0x100
[ 562.630883] ? up+0x32/0x50
[ 562.630885] ? irq_work_queue+0x8d/0xa0
[ 562.630886] ? console_unlock+0x2b6/0x4b0
[ 562.630888] ? vprintk_emit+0x312/0x4a0
[ 562.630892] ? dev_vprintk_emit+0xbf/0x230
[ 562.630895] ? do_sys_poll+0x37a/0x550
[ 562.630897] ? dev_printk_emit+0x4e/0x70
[ 562.630900] ? __dev_printk+0x3c/0x80
[ 562.630903] ? _raw_spin_lock+0x20/0x30
[ 562.630909] ? snd_pcm_stream_lock+0x31/0x50 [snd_pcm]
[ 562.630914] ? _snd_pcm_stream_lock_irqsave+0x2e/0x40 [snd_pcm]
[ 562.630918] ? snd_pcm_stop_xrun+0x16/0x70 [snd_pcm]
[ 562.630922] ? in_stream_callback+0x3e6/0x450 [snd_firewire_lib]
[ 562.630925] ? handle_ir_packet_per_buffer+0x8e/0x1a0 [firewire_ohci]
[ 562.630928] ? ohci_flush_iso_completions+0xa3/0x130 [firewire_ohci]
[ 562.630932] ? fw_iso_context_flush_completions+0x15/0x20 [firewire_core]
[ 562.630935] ? amdtp_stream_pcm_pointer+0x2d/0x40 [snd_firewire_lib]
[ 562.630938] ? pcm_capture_pointer+0x19/0x20 [snd_oxfw]
[ 562.630943] ? snd_pcm_update_hw_ptr0+0x47/0x3d0 [snd_pcm]
[ 562.630945] ? poll_select_copy_remaining+0x150/0x150
[ 562.630947] ? poll_select_copy_remaining+0x150/0x150
[ 562.630952] ? snd_pcm_update_hw_ptr+0x10/0x20 [snd_pcm]
[ 562.630956] ? snd_pcm_hwsync+0x45/0xb0 [snd_pcm]
[ 562.630960] ? snd_pcm_common_ioctl1+0x1ff/0xc90 [snd_pcm]
[ 562.630962] ? futex_wake+0x90/0x170
[ 562.630966] ? snd_pcm_capture_ioctl1+0x136/0x260 [snd_pcm]
[ 562.630970] ? snd_pcm_capture_ioctl+0x27/0x40 [snd_pcm]
[ 562.630972] ? do_vfs_ioctl+0xa3/0x610
[ 562.630974] ? vfs_read+0x11b/0x130
[ 562.630976] ? SyS_ioctl+0x79/0x90
[ 562.630978] ? entry_SYSCALL_64_fastpath+0x1e/0xad

This commit fixes the above bug. This assumes two cases:
1. Any error is detected in software IRQ context of OHCI 1394 context.
In this case, PCM substream should be aborted in packet handler. On the
other hand, it should not be done in any process context. TO distinguish
these two context, use 'in_interrupt()' macro.
2. Any error is detect in process context of ALSA PCM application.
In this case, PCM substream should not be aborted in packet handler
because PCM substream lock is acquired. The task to abort PCM substream
should be done in ALSA PCM core. For this purpose, SNDRV_PCM_POS_XRUN is
returned at 'struct snd_pcm_ops.pointer()'.

Suggested-by: Clemens Ladisch <clemens@ladisch.de>
Fixes: e9148dddc3c7("ALSA: firewire-lib: flush completed packets when reading PCM position")
Cc: <stable@vger.kernel.org> # 4.9+
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 55799c5a 07-Jun-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire: arrange common PCM info/constraints for AMDTP engine applications

In ALSA firewire stack, 8 drivers uses IEC 61883-1/6 engine for data
transmission. They have common PCM info/constraints and duplicated codes.

This commit unifies the codes into fireiwre-lib.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 875becf8 06-Jun-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire: process packets in 'struct snd_pcm_ops.ack' callback

In recent commit for ALSA PCM core, some arrangement is done for
'struct snd_pcm_ops.ack' callback. This is called when appl_ptr is
explicitly moved in intermediate buffer for PCM frames, except for
some cases described later.

For drivers in ALSA firewire stack, usage of this callback has a merit to
reduce latency between time of PCM frame queueing and handling actual
packets in recent isochronous cycle, because no need to wait for software
IRQ context from isochronous context of OHCI 1394.

If this works well in a case that mapped page frame is used for the
intermediate buffer, user process should execute some commands for ioctl(2)
to tell the number of handled PCM frames in the intermediate buffer just
after handling them. Therefore, at present, with a combination of below
conditions, this doesn't work as expected and user process should wait for
the software IRQ context as usual:
- when ALSA PCM core judges page frame mapping is available for status
data (struct snd_pcm_mmap_status) and control data
(struct snd_pcm_mmap_control).
- user process handles PCM frames by loop just with 'snd_pcm_mmap_begin()'
and 'snd_pcm_mmap_commit()'.
- user process uses PCM hw plugin in alsa-lib to operate I/O without
'sync_ptr_ioctl' option.

Unfortunately, major use case include these three conditions.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# f91c9d76 11-Apr-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: cache maximum length of payload to reduce function calls

During packet streaming, maximum length of payload for isochronous packet
is invariable, therefore no need to recalculate. Current ALSA IEC 61883-1/6
engine calls a function to calculate it 8,000 or more times per second
for incoming packet processing.

This commit adds a member to have maximum length of payload into 'struct
amdtp_stream', to reduces the function calls. At first callback from
isochronous context, the length is calculated and stored for later
processing.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# b164d2fd 09-Apr-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire_lib: add tracepoints for packets without CIP headers

Unique protocol is used for RME Fireface series. In this protocol,
payload format for isochronous packet is not compliant to CIP in
IEC 61883-1/6. The packet includes data blocks just with data channels,
without headers and any metadata.

In previous commits, ALSA IEC 61883-1/6 engine supports this protocol.
However, tracepoints are not supported yet, unlike implementation for
IEC 61883-1/6 protocol. This commit adds support of tracepoints for
the protocol.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 3b196c39 31-Mar-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add no-header packet processing

As long as investigating Fireface 400, IEC 61883-1/6 is not applied to
its packet streaming protocol. Remarks of the specific protocol are:
* Each packet doesn't include CIP headers.
* 64,0 and 128,0 kHz are supported.
* The device doesn't necessarily transmit 8,000 packets per second.
* 0, 1, 2, 3 are used as tag for rx isochronous packets, however 0 is
used for tx isochronous packets.

On the other hand, there's a common feature. The number of data blocks
transferred in a second is the same as sampling transmission frequency.
Current ALSA IEC 61883-1/6 engine already has a method to calculate it and
this driver can utilize it for rx packets, as well as tx packets.

This commit adds support for the transferring protocol. CIP_NO_HEADERS
flag is newly added. When this flag is set:
* Both of 0 (without CIP header) and 1 (with CIP header) are used as tag
to handle incoming isochronous packet.
* 0 (without CIP header) is used as tag to transfer outgoing isochronous
packet.
* Skip CIP header evaluation.
* Use unique way to calculate the quadlets of isochronous packet payload.

In ALSA PCM interface, 128.0 kHz is not supported, and the ALSA
IEC 61883-1/6 engine doesn't support 64.0 kHz. These modes are dropped.

The sequence of rx packet has a remarkable quirk about tag. This will be
described in later commits.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# ff0fb5aa 31-Mar-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: use the same prototype for functions to handle packet

Audio and music units of RME Fireface series use its own protocol for
isochronous packets to transfer data. This protocol requires ALSA IEC
61883-1/6 engine to have alternative functions.

This commit is a preparation for the protocol.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 2128f78f 22-Mar-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add a quirk of packet without valid EOH in CIP format

In IEC 61883-1, when two quadlets CIP header is used, the most significant
bit in second CIP header stands. However, packets from units with MOTU
protocol version 3 have a quirk without this flag. Current packet streaming
layer handles this as protocol error.

This commit adds a new enumeration constant for this quirk, to handle MOTU
protocol version 3.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 9dae017b 22-Mar-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: enable CIP_DBC_IS_END_EVENT for both directions of stream

Commit c8bdf49b9935("ALSA: fireworks/firewire-lib: Add a quirk for the
meaning of dbc") adds CIP_DBC_IS_END_EVENT flag just for tx packets.
However, MOTU FireWire series has this quirk for rx packets.

This commit allows both directions with the flag.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 9863874f 22-Mar-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add support for source packet header field in CIP header

In IEC 61883-1, CIP headers can have a SPH field. When a packet has 1 in
SPH field of its CIP header, the packet has a source packet headers. A
source packet header consists of 32 bit field (= 1 quadlet) and it
transfers time stamp, which is the same value as the lower 25 bits of the
IEEE 1394 CYCLE_TIMER register and the rest is zero.

This commit just supports source packet header field because IEC 61883-1
includes ambiguity the position of this header and its count. Each
protocol layer is allowed to have actual implementation according its
requirements.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# a04513f8 22-Mar-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: record cycle count for the first packet

Currently, packet streaming layer passes generated SYT value to data block
processing layer. However, this is not enough in a case that the data block
processing layer generates time stamps by its own ways.

For out-packet stream, the packet streaming layer guarantees 8,000 times
calls of data block processing layers per sec. Therefore, when cycle count
of the first packet is recorded, data block processing layers can calculate
own time stamps with the recorded value.

For the reason, this commit allows packet streaming layer to record the
first cycle count. Each data block processing layer can read the count by
accessing a member of structure for packet streaming layer.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# e4f34cf6 04-Jan-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>

Revert "ALSA: firewire-lib: change structure member with proper type"

This reverts commit 6b7e95d1336b9eb0d4c6db190ce756480496bd13. This commit
is based on a concern about value of the given parameter. It's expected
to be ORed value with some enumeration-constants, thus often it can not be
one of the enumeration-constants. I understood that this is out of
specification and causes implementation-dependent issues.

In C language specification, enumerated type can be interpreted as an
integer type, in which all of enumeration-constants in corresponding
enumerator-list can be stored. Implementations can select one of char,
signed int and unsigned int as its type, and this selection is
implementation-dependent.

In GCC, a signed integer is selected when at least one of
enumeration-constants has negative value, else an unsigned integer is
selected. This behaviour can be switched by -fshort-enums to short type.
Anyway, the type can be decided after scanning all of
enumeration-constants.

Totally, there's no rules to constrain the value of enumerated type to
be one of enumeration-constants. In short, in enumerated type, decision
of actual type for the type is the most important and
enumeration-constants are just used for the decision, thus it's permitted
to have an integer value in a range of enumeration-constants. In our case,
actual type for the type is currently deterministic to be either char or
unsigned int. Under GCC, it's unsigned int.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 6b7e95d1 02-Jan-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: change structure member with proper type

The 'amdtp_stream' structure is initialized by a call of
'amdtp_stream_init()'. Although a parameter of this function is for bit
flags of packet attributes, its type is enumerator.

This commit changes the type so that it's proper for a bit flags.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# ff38e0c7 10-May-2016 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: drop skip argument from helper functions to queue a packet

On most of audio and music units on IEEE 1394 bus which ALSA firewire stack
supports (or plans to support), CIP with two quadlets header is used.
Thus, there's no cases to queue packets with blank payload. If such packets
are going to be queued, it means that they're for skips of the cycle.

This commit simplifies helper functions to queue a packet.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Acked-by: Clemens Ladisch <clemens@ladisch.de>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# a9c4284b 10-May-2016 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add context information to tracepoints

In current implementation, packet processing is done in both of software
IRQ contexts of IR/IT contexts and process contexts.

This is usual interrupt handling of IR/IT context for 1394 OHCI.
(in hardware IRQ context)
irq_handler() (drivers/firewire/ohci.c)
->tasklet_schedule()
(in software IRQ context)
handle_it_packet() or handle_ir_packet_per_buffer() (drivers/firewire/ohci.c)
->flush_iso_completions()
->struct fw_iso_context.callback.sc()
= out_stream_callback() or in_stream_callback()

However, we have another chance for packet processing. It's done in PCM
frame handling via ALSA PCM interfaces.
(in process context)
ioctl(i.e. SNDRV_PCM_IOCTL_HWSYNC)
->snd_pcm_hwsync() (sound/core/pcm_native.c)
->snd_pcm_update_hw_ptr() (sound/core/pcm_lib.c)
->snd_pcm_update_hw_ptr0()
->struct snd_pcm_ops.pointer()
= amdtp_stream_pcm_pointer()
->fw_iso_context_flush_completions() (drivers/firewire/core-iso.c)
->struct fw_card_driver.flush_iso_completions()
= ohci_flush_iso_completions() (drivers/firewire/ohci.c)
->flush_iso_completions()
->struct fw_iso_context.callback.sc()
= out_stream_callback() or in_stream_callback()

This design is for a better granularity of PCM pointer. When ioctl(2) is
executed with some commands for ALSA PCM interface, queued packets are
handled at first. Then, the latest number of handled PCM frames is
reported. The number can represent PCM frames transferred in most near
isochronous cycle.

Current tracepoints include no information to distinguish running contexts.
When tracing the interval of software IRQ context, this is not good.

This commit adds more information for current context. Additionally, the
index of packet processed in one context is added in a case that packet
processing is executed in continuous context of the same kind,

As a result, the output includes 11 fields with additional two fields
to commit 0c95c1d6197f ("ALSA: firewire-lib: add tracepoints to dump a part
of isochronous packet data"):
17131.9186: out_packet: 07 7494 ffc0 ffc1 00 000700c0 9001a496 058 45 1 13
17131.9186: out_packet: 07 7495 ffc0 ffc1 00 000700c8 9001ba00 058 46 1 14
17131.9186: out_packet: 07 7496 ffc0 ffc1 00 000700d0 9001ffff 002 47 1 15
17131.9189: out_packet: 07 7497 ffc0 ffc1 00 000700d0 9001d36a 058 00 0 00
17131.9189: out_packet: 07 7498 ffc0 ffc1 00 000700d8 9001e8d4 058 01 0 01
17131.9189: out_packet: 07 7499 ffc0 ffc1 00 000700e0 9001023e 058 02 0 00
17131.9206: in_packet: 07 7447 ffc1 ffc0 01 3f070072 9001783d 058 32 1 00
17131.9206: in_packet: 07 7448 ffc1 ffc0 01 3f070072 90ffffff 002 33 1 01
17131.9206: in_packet: 07 7449 ffc1 ffc0 01 3f07007a 900191a8 058 34 1 02
(Here, some common fields are omitted so that a line is within 80
characters.)

The legend is:
- The second of cycle scheduled for the packet
- The count of cycle scheduled for the packet
- The ID of node as source (hex)
- The ID of node as destination (hex)
- The value of isochronous channel
- The first quadlet of CIP header (hex)
- The second quadlet of CIP header (hex)
- The number of included quadlets
- The index of packet in a buffer maintained by this module
- 0 in process context, 1 in IRQ context
- The index of packet processed in the context

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 1dba9db0 11-May-2016 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: permit to flush queued packets only in process context for better PCM period granularity

These three commits were merged to improve PCM pointer granularity.
commit 76fb87894828 ("ALSA: firewire-lib: taskletize the snd_pcm_period_elapsed() call")
commit e9148dddc3c7 ("ALSA: firewire-lib: flush completed packets when reading PCM position")
commit 92b862c7d685 ("ALSA: firewire-lib: optimize packet flushing")

The point of them is to handle queued packets not only in software IRQ
context of IR/IT contexts, but also in process context. As a result of
handling packets, period tasklet is scheduled when acrossing PCM period
boundary. This is to prevent recursive call of
'struct snd_pcm_ops.pointer()' in the same context.

When the pointer callback is executed in the process context, it's
better to avoid the second callback in the software IRQ context. The
software IRQ context runs immediately after scheduled in the process
context because few packets are queued yet.

For the aim, 'pointer_flush' is used, however it causes a race condition
between the process context and software IRQ context of IR/IT contexts.

Practically, this race is not so critical because it influences process
context to skip flushing queued packet and to get worse granularity of
PCM pointer. The race condition is quite rare but it should be improved
for stable service.

The similar effect can be achieved by using 'in_interrupt()' macro. This
commit obsoletes 'pointer_flush' with it.

Acked-by: Clemens Ladisch <clemens@ladisch.de>
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 62f00e40 09-May-2016 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: enable the same feature as CIP_SKIP_INIT_DBC_CHECK flag

In former commit, drivers in ALSA firewire stack always starts IT context
before IR context. If IR context starts after packets are transmitted by
peer unit, packet discontinuity may be detected because the context starts
in the middle of packet streaming. This situation is rare because IT
context usually starts immediately. However, it's better to solve this
issue. This is suppressed with CIP_SKIP_INIT_DBC_CHECK flag.

This commit enables the same feature as CIP_SKIP_INIT_DBC_CHECK.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 390a1512 09-May-2016 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code cleanup for outgoing packet handling

In previous commit, this module has no need to reuse parameters of
incoming packets for outgoing packets anymore. This commit arranges some
needless codes for outgoing packet processing.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# d9a16fc9 09-May-2016 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: code cleanup for incoming packet handling

In previous commit, this module has no need to reuse parameters of
incoming packets for outgoing packets anymore. This commit arranges some
needless codes for incoming packet processing.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# dec63cc8 09-May-2016 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: handle IT/IR contexts in each software interrupt context

In clause 6.3 of IEC 61883-6:2000, there's an explanation about processing
of presentation timestamp. In the clause, we can see "If a function block
receives a CIP, processes it and subsequently re-transmits it, then the
SYT of the outgoing CIP shall be the sum of the incoming SYT and the
processing delay." ALSA firewire stack has an implementation to partly
satisfy this specification. Developers assumed the stack to perform as an
Audio function block[1].

Following to the assumption, current implementation of ALSA firewire stack
use one software interrupt context to handle both of in/out packets. In
most case, this is processed in 1394 OHCI IR context independently of the
opposite context. Thus, this implementation uses longer CPU time in the
software interrupt context. This is not better for whole system.

Against the assumption, I confirmed that each ASIC for IEC 61883-1/6
doesn't necessarily expect it to the stack. Thus, current implementation
of ALSA firewire stack includes over-engineering.

This commit purges the implementation. As a result, packets of one
direction are handled in one software interrupt context and spends
minimum CPU time.

[1] [alsa-devel] [PATCH 0/8] [RFC] new driver for Echo Audio's Fireworks based devices
http://mailman.alsa-project.org/pipermail/alsa-devel/2013-June/062660.html

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 0c95c1d6 09-May-2016 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add tracepoints to dump a part of isochronous packet data

When audio and music units have some quirks in their sequence of packet,
it's really hard for non-owners to identify the quirks. Although developers
need dumps for sequence of packets, it's difficult for users who have no
knowledges and no equipments for this purpose.

This commit adds tracepoints for this situation. When users encounter
the issue, they can dump a part of packet data via Linux tracing framework
as long as using drivers in ALSA firewire stack.

Additionally, tracepoints for outgoing packets will be our help to check
and debug packet processing of ALSA firewire stack.

This commit newly adds 'snd_firewire_lib' subsystem with 'in_packet' and
'out_packet' events. In the events, some attributes of packets and the
index of packet managed by this module are recorded per packet.

This is an usage:

$ trace-cmd record -e snd_firewire_lib:out_packet \
-e snd_firewire_lib:in_packet
/sys/kernel/tracing/events/snd_firewire_lib/out_packet/filter
/sys/kernel/tracing/events/snd_firewire_lib/in_packet/filter
Hit Ctrl^C to stop recording
^C
$ trace-cmd report trace.dat
...
23647.033934: in_packet: 01 4073 ffc0 ffc1 00 000f0040 9001b2d1 122 44
23647.033936: in_packet: 01 4074 ffc0 ffc1 00 000f0048 9001c83b 122 45
23647.033937: in_packet: 01 4075 ffc0 ffc1 00 000f0050 9001ffff 002 46
23647.033938: in_packet: 01 4076 ffc0 ffc1 00 000f0050 9001e1a6 122 47
23647.035426: out_packet: 01 4123 ffc1 ffc0 01 010f00d0 9001fb40 122 17
23647.035428: out_packet: 01 4124 ffc1 ffc0 01 010f00d8 9001ffff 002 18
23647.035429: out_packet: 01 4125 ffc1 ffc0 01 010f00d8 900114aa 122 19
23647.035430: out_packet: 01 4126 ffc1 ffc0 01 010f00e0 90012a15 122 20
(Here, some common fields are omitted so that a line to be within 80
characters.)
...

One line represent one packet. The legend for the last nine fields is:
- The second of cycle scheduled for the packet
- The count of cycle scheduled for the packet
- The ID of node as source (hex)
- Some devices transfer packets with invalid source node ID in their CIP
header.
- The ID of node as destination (hex)
- The value is not in CIP header of packets.
- The value of isochronous channel
- The first quadlet of CIP header (hex)
- The second quadlet of CIP header (hex)
- The number of included quadlets
- The index of packet in a buffer maintained by this module

This is an example to parse these lines from text file by Python3 script:

\#!/usr/bin/env python3
import sys

def parse_ts(second, cycle, syt):
offset = syt & 0xfff
syt >>= 12
if cycle & 0x0f > syt:
cycle += 0x10
cycle &= 0x1ff0
cycle |= syt
second += cycle // 8000
cycle %= 8000
# In CYCLE_TIMER of 1394 OHCI, second is represented in 8 bit.
second %= 128
return (second, cycle, offset)

def calc_ts(second, cycle, offset):
ts = offset
ts += cycle * 3072
# In DMA descriptor of 1394 OHCI, second is represented in 3 bit.
ts += (second % 8) * 8000 * 3072
return ts

def subtract_ts(minuend, subtrahend):
# In DMA descriptor of 1394 OHCI, second is represented in 3 bit.
if minuend < subtrahend:
minuend += 8 * 8000 * 3072
return minuend - subtrahend

if len(sys.argv) != 2:
print('At least, one argument is required for packet dump.')
sys.exit()

filename = sys.argv[1]

data = []

prev = 0
with open(filename, 'r') as f:
for line in f:
pos = line.find('packet:')
if pos < 0:
continue

pos += len('packet:')
line = line[pos:].strip()
fields = line.split(' ')

datum = []

datum.append(fields[8])

syt = int(fields[6][4:], 16)

# Empty packet in IEC 61883-1, or NODATA in IEC 61883-6
if syt == 0xffff:
data_blocks = 0
else:
payload_size = int(fields[7], 10)
data_block_size = int(fields[5][2:4], 16)
data_blocks = (payload_size - 2) / data_block_size
datum.append(data_blocks)

second = int(fields[0], 10)
cycle = int(fields[1], 10)
start = (second << 25) | (cycle << 12)
datum.append('0x{0:08x}'.format(start))
start = calc_ts(second, cycle, 0)

datum.append("0x" + fields[5])
datum.append("0x" + fields[6])

if syt == 0xffff:
second = 0
cycle = 0
tick = 0
else:
second, cycle, tick = parse_ts(second, cycle, syt)
ts = calc_ts(second, cycle, tick)
datum.append(start)
datum.append(ts)
if ts == 0:
datum.append(0)
datum.append(0)
else:
# Usual case, or a case over 8 seconds.
if ts > start or start > 7 * 8000 * 3072:
datum.append(subtract_ts(ts, start))
if ts > prev or start > 7 * 8000 * 3072:
gap = subtract_ts(ts, prev)
datum.append(gap)
else:
datum.append('backward')
else:
datum.append('invalid')
prev = ts

data.append(datum)

sys.exit()

The data variable includes array with these elements:
- The index of the packet
- The number of data blocks in the packet
- The value of cycle count (hex)
- The value of CIP header 1 (hex)
- The value of CIP header 2 (hex)
- The value of cycle count (tick)
- The value of calculated presentation timestamp (tick)
- The offset between the cycle count and presentation timestamp
- The elapsed ticks from the previous presentation timestamp

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# f90e2ded 09-May-2016 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: compute the value of second field in cycle count for IR context

In callback function of isochronous context, modules can queue packets to
indicated isochronous cycles. Although the cycle to queue a packet is
deterministic by calculation, this module doesn't implement the calculation
because it's useless for processing.

In future, the cycle count is going to be printed with the other parameters
for debugging. This commit is the preparation. The cycle count is computed
by cycle unit, and correctly arranged to corresponding packets. The
calculated count is used in later commit.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 73fc7f08 09-May-2016 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: compute the value of second field in cycle count for IT context

In callback function of isochronous context, u32 variable is passed for
cycle count. The value of this variable comes from DMA descriptors of 1394
Open Host Controller Interface (1394 OHCI). In the specification, DMA
descriptors transport lower 3 bits for second field and full cycle field in
16 bits field, therefore 16 bits of the u32 variable are available. The
value for second is modulo 8, and the value for cycle is modulo 8,000.

Currently, ALSA firewire-lib module don't use the value of the second
field, because the value is useless to calculate presentation timestamp in
IEC 61883-6. However, the value may be useful for debugging. In later
commit, it will be printed with the other parameters for debugging.

This commit makes this module to handle the whole cycle count including
second. The value is calculated by cycle unit. The existed code is already
written with ignoring the value of second, thus this commit causes no
issues.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 44c376b9 30-Mar-2016 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: suppress kernel warnings when releasing uninitialized stream data

When any of AMDTP stream data are not initialized and private data is
going to be released, WARN_ON() in amdtp_stream_destroy() is hit and
dump messages. This may take users irritated.

This commit fixes the bug to skip releasing when it's not initialized.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 2a7e1713 11-Oct-2015 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: continue packet processing at detecting wrong CIP headers

In firewire-lib, isochronous packet streaming is stopped when detecting
wrong value for FMT field of CIP headers. Although this is appropriate
to IEC 61883-1 and 6, some BeBoB based devices with vendors' customization
use invalid value to FMT field of CIP headers in the beginning of
streaming.

$ journalctl
snd-bebob fw1.0: Detect unexpected protocol: 01000000 8000ffff

I got this log with M-Audio FireWire 1814. In this line, the value of FMT
field is 0x00, while it should be 0x10 in usual AMDTP.

Except for the beginning, these devices continue to transfer packets with
valid value for FMT field, except for the beginning. Therefore, in this
case, firewire-lib should continue to process packets. The former
implementation of firewire-lib performs it.

This commit loosens the handling of wrong value, to continue packet
processing in the case.

Fixes: 414ba022a528 ('ALSA: firewire-lib: add support arbitrary value for fmt/fdf fields in CIP header')
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# df075fee 18-Sep-2015 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: complete AM824 data block processing layer

This commit moves the codes related to data block processing from packet
streaming layer to AM824 layer.

Each driver initializes amdtp stream structure for AM824 data block by
calling amdtp_am824_init(). Then, a memory block is allocated for AM824
specific structure. This memory block is released by calling
amdtp_stream_destroy().

When setting streaming parameters, it calls amdtp_am824_set_parameters().
When starting packet streaming, it calls amdtp_stream_start(). When
stopping packet streaming, it calls amdtp_stream_stop().

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 49c7b3fc 18-Sep-2015 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: rename macros with AM824 prefix

This commit renames some macros just related to AM824 format. In later
commit, they're moved to AM824 layer.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 85130cb4 18-Sep-2015 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: rename PCM format helper function

Setting the format of PCM substream to AMDTP stream structure is important
to set a handler to copy actual PCM samples between buffers. The
processing should be in data block processing layer because essentially
it has no relationship to packet streaming.

This commit renames PCM format setting function to prepare for integrating
AM824 layer.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# bc8500da 18-Sep-2015 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: move PCM substream constraint to AM824 layer

In IEC 61883-6, PCM frames are transferred in Multi Bit Linear Audio data
channel. The data channel transfers 16/20/24 bit PCM samples. Thus, PCM
substream has a constrain about it.

This commit moves codes related to the constraint from packet streaming
layer to AM824 data block processing layer.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 51c29fd2 18-Sep-2015 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: rename parameter setting function for AM824 with FDF field

The value of FDF field in CIP header is protocol-dependent. Thus, it's
better to allow data block processing layer to decide the value in any
timing.

In AM824 data format, the value of FDF field in CIP header indicates
N-flag and Nominal Sampling Frequency Code (sfc). The N-flag is for
switching 'Clock-based rate control mode' and 'Command-based rate control
mode'. In our implementation, 'Clock-based rate control mode' is just
supported. Therefore, When sampling transfer frequency is decided, then
the FDF can be set.

This commit replaces 'amdtp_stream_set_parameters' with
'amdtp_am824_set_parameters' to set the FDF. This is the same timing
to decide the ration between the number of data blocks and the number of
PCM frames.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# 5955815e 18-Sep-2015 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: add data block processing layer for AM824 format

This commit adds data block processing layer for AM824 format. The new
layer initializes streaming layer with its value for fmt field.

Currently, most implementation of data block processing still remains
streaming layer. In later commits, these codes will be moved to the layer.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>


# d67c46b9 18-Sep-2015 Takashi Sakamoto <o-takashi@sakamocchi.jp>

ALSA: firewire-lib: rename 'amdtp' to 'amdtp-stream' to prepare for functional separation

In later commit, data block processing layer will be newly added. This
layer will be named as 'amdtp-am824'.

This commit renames current amdtp file to amdtp-stream, to distinguish it
from the new layer.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>