Skip to content

Commit d1bbd20

Browse files
cladischStefan Richter
authored andcommitted
firewire: allow explicit flushing of iso packet completions
Extend the kernel and userspace APIs to allow reporting all currently completed isochronous packets, even if the next interrupt packet has not yet been reached. This is required to determine the status of the packets at the end of a paused or stopped stream, and useful for more precise synchronization of audio streams. Signed-off-by: Clemens Ladisch <clemens@ladisch.de> Signed-off-by: Stefan Richter <stefanr@s5r6.in-berlin.de>
1 parent 18d6271 commit d1bbd20

7 files changed

Lines changed: 127 additions & 13 deletions

File tree

‎drivers/firewire/core-card.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,11 @@ static void dummy_flush_queue_iso(struct fw_iso_context *ctx)
650650
{
651651
}
652652

653+
static int dummy_flush_iso_completions(struct fw_iso_context *ctx)
654+
{
655+
return -ENODEV;
656+
}
657+
653658
static const struct fw_card_driver dummy_driver_template = {
654659
.read_phy_reg = dummy_read_phy_reg,
655660
.update_phy_reg = dummy_update_phy_reg,
@@ -662,6 +667,7 @@ static const struct fw_card_driver dummy_driver_template = {
662667
.set_iso_channels = dummy_set_iso_channels,
663668
.queue_iso = dummy_queue_iso,
664669
.flush_queue_iso = dummy_flush_queue_iso,
670+
.flush_iso_completions = dummy_flush_iso_completions,
665671
};
666672

667673
void fw_card_release(struct kref *kref)

‎drivers/firewire/core-cdev.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ union ioctl_arg {
438438
struct fw_cdev_send_phy_packet send_phy_packet;
439439
struct fw_cdev_receive_phy_packets receive_phy_packets;
440440
struct fw_cdev_set_iso_channels set_iso_channels;
441+
struct fw_cdev_flush_iso flush_iso;
441442
};
442443

443444
static int ioctl_get_info(struct client *client, union ioctl_arg *arg)
@@ -1168,6 +1169,16 @@ static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
11681169
return fw_iso_context_stop(client->iso_context);
11691170
}
11701171

1172+
static int ioctl_flush_iso(struct client *client, union ioctl_arg *arg)
1173+
{
1174+
struct fw_cdev_flush_iso *a = &arg->flush_iso;
1175+
1176+
if (client->iso_context == NULL || a->handle != 0)
1177+
return -EINVAL;
1178+
1179+
return fw_iso_context_flush_completions(client->iso_context);
1180+
}
1181+
11711182
static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
11721183
{
11731184
struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
@@ -1589,6 +1600,7 @@ static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
15891600
[0x15] = ioctl_send_phy_packet,
15901601
[0x16] = ioctl_receive_phy_packets,
15911602
[0x17] = ioctl_set_iso_channels,
1603+
[0x18] = ioctl_flush_iso,
15921604
};
15931605

15941606
static int dispatch_ioctl(struct client *client,

‎drivers/firewire/core-iso.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ void fw_iso_context_queue_flush(struct fw_iso_context *ctx)
191191
}
192192
EXPORT_SYMBOL(fw_iso_context_queue_flush);
193193

194+
int fw_iso_context_flush_completions(struct fw_iso_context *ctx)
195+
{
196+
return ctx->card->driver->flush_iso_completions(ctx);
197+
}
198+
EXPORT_SYMBOL(fw_iso_context_flush_completions);
199+
194200
int fw_iso_context_stop(struct fw_iso_context *ctx)
195201
{
196202
return ctx->card->driver->stop_iso(ctx);

‎drivers/firewire/core.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ struct fw_card_driver {
106106

107107
void (*flush_queue_iso)(struct fw_iso_context *ctx);
108108

109+
int (*flush_iso_completions)(struct fw_iso_context *ctx);
110+
109111
int (*stop_iso)(struct fw_iso_context *ctx);
110112
};
111113

‎drivers/firewire/ohci.c‎

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ struct iso_context {
172172
struct context context;
173173
void *header;
174174
size_t header_length;
175+
unsigned long flushing_completions;
176+
u32 mc_buffer_bus;
177+
u16 mc_completed;
175178
u16 last_timestamp;
176179
u8 sync;
177180
u8 tags;
@@ -2749,28 +2752,51 @@ static int handle_ir_buffer_fill(struct context *context,
27492752
{
27502753
struct iso_context *ctx =
27512754
container_of(context, struct iso_context, context);
2755+
unsigned int req_count, res_count, completed;
27522756
u32 buffer_dma;
27532757

2754-
if (last->res_count != 0)
2758+
req_count = le16_to_cpu(last->req_count);
2759+
res_count = le16_to_cpu(ACCESS_ONCE(last->res_count));
2760+
completed = req_count - res_count;
2761+
buffer_dma = le32_to_cpu(last->data_address);
2762+
2763+
if (completed > 0) {
2764+
ctx->mc_buffer_bus = buffer_dma;
2765+
ctx->mc_completed = completed;
2766+
}
2767+
2768+
if (res_count != 0)
27552769
/* Descriptor(s) not done yet, stop iteration */
27562770
return 0;
27572771

2758-
buffer_dma = le32_to_cpu(last->data_address);
27592772
dma_sync_single_range_for_cpu(context->ohci->card.device,
27602773
buffer_dma & PAGE_MASK,
27612774
buffer_dma & ~PAGE_MASK,
2762-
le16_to_cpu(last->req_count),
2763-
DMA_FROM_DEVICE);
2775+
completed, DMA_FROM_DEVICE);
27642776

2765-
if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS))
2777+
if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS)) {
27662778
ctx->base.callback.mc(&ctx->base,
2767-
le32_to_cpu(last->data_address) +
2768-
le16_to_cpu(last->req_count),
2779+
buffer_dma + completed,
27692780
ctx->base.callback_data);
2781+
ctx->mc_completed = 0;
2782+
}
27702783

27712784
return 1;
27722785
}
27732786

2787+
static void flush_ir_buffer_fill(struct iso_context *ctx)
2788+
{
2789+
dma_sync_single_range_for_cpu(ctx->context.ohci->card.device,
2790+
ctx->mc_buffer_bus & PAGE_MASK,
2791+
ctx->mc_buffer_bus & ~PAGE_MASK,
2792+
ctx->mc_completed, DMA_FROM_DEVICE);
2793+
2794+
ctx->base.callback.mc(&ctx->base,
2795+
ctx->mc_buffer_bus + ctx->mc_completed,
2796+
ctx->base.callback_data);
2797+
ctx->mc_completed = 0;
2798+
}
2799+
27742800
static inline void sync_it_packet_for_cpu(struct context *context,
27752801
struct descriptor *pd)
27762802
{
@@ -2925,8 +2951,10 @@ static struct fw_iso_context *ohci_allocate_iso_context(struct fw_card *card,
29252951
if (ret < 0)
29262952
goto out_with_header;
29272953

2928-
if (type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL)
2954+
if (type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL) {
29292955
set_multichannel_mask(ohci, 0);
2956+
ctx->mc_completed = 0;
2957+
}
29302958

29312959
return &ctx->base;
29322960

@@ -3388,6 +3416,39 @@ static void ohci_flush_queue_iso(struct fw_iso_context *base)
33883416
reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
33893417
}
33903418

3419+
static int ohci_flush_iso_completions(struct fw_iso_context *base)
3420+
{
3421+
struct iso_context *ctx = container_of(base, struct iso_context, base);
3422+
int ret = 0;
3423+
3424+
tasklet_disable(&ctx->context.tasklet);
3425+
3426+
if (!test_and_set_bit_lock(0, &ctx->flushing_completions)) {
3427+
context_tasklet((unsigned long)&ctx->context);
3428+
3429+
switch (base->type) {
3430+
case FW_ISO_CONTEXT_TRANSMIT:
3431+
case FW_ISO_CONTEXT_RECEIVE:
3432+
if (ctx->header_length != 0)
3433+
flush_iso_completions(ctx);
3434+
break;
3435+
case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
3436+
if (ctx->mc_completed != 0)
3437+
flush_ir_buffer_fill(ctx);
3438+
break;
3439+
default:
3440+
ret = -ENOSYS;
3441+
}
3442+
3443+
clear_bit_unlock(0, &ctx->flushing_completions);
3444+
smp_mb__after_clear_bit();
3445+
}
3446+
3447+
tasklet_enable(&ctx->context.tasklet);
3448+
3449+
return ret;
3450+
}
3451+
33913452
static const struct fw_card_driver ohci_driver = {
33923453
.enable = ohci_enable,
33933454
.read_phy_reg = ohci_read_phy_reg,
@@ -3405,6 +3466,7 @@ static const struct fw_card_driver ohci_driver = {
34053466
.set_iso_channels = ohci_set_iso_channels,
34063467
.queue_iso = ohci_queue_iso,
34073468
.flush_queue_iso = ohci_flush_queue_iso,
3469+
.flush_iso_completions = ohci_flush_iso_completions,
34083470
.start_iso = ohci_start_iso,
34093471
.stop_iso = ohci_stop_iso,
34103472
};

‎include/linux/firewire-cdev.h‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,11 @@ struct fw_cdev_event_request2 {
212212
* @header: Stripped headers, if any
213213
*
214214
* This event is sent when the controller has completed an &fw_cdev_iso_packet
215-
* with the %FW_CDEV_ISO_INTERRUPT bit set, or when there have been so many
216-
* completed packets without the interrupt bit set that the kernel's internal
217-
* buffer for @header is about to overflow. (In the latter case, kernels with
218-
* ABI version < 5 drop header data up to the next interrupt packet.)
215+
* with the %FW_CDEV_ISO_INTERRUPT bit set, when explicitly requested with
216+
* %FW_CDEV_IOC_FLUSH_ISO, or when there have been so many completed packets
217+
* without the interrupt bit set that the kernel's internal buffer for @header
218+
* is about to overflow. (In the last case, kernels with ABI version < 5 drop
219+
* header data up to the next interrupt packet.)
219220
*
220221
* Isochronous transmit events (context type %FW_CDEV_ISO_CONTEXT_TRANSMIT):
221222
*
@@ -271,7 +272,8 @@ struct fw_cdev_event_iso_interrupt {
271272
* This event is sent in multichannel contexts (context type
272273
* %FW_CDEV_ISO_CONTEXT_RECEIVE_MULTICHANNEL) for &fw_cdev_iso_packet buffer
273274
* chunks that have been completely filled and that have the
274-
* %FW_CDEV_ISO_INTERRUPT bit set.
275+
* %FW_CDEV_ISO_INTERRUPT bit set, or when explicitly requested with
276+
* %FW_CDEV_IOC_FLUSH_ISO.
275277
*
276278
* The buffer is continuously filled with the following data, per packet:
277279
* - the 1394 iso packet header as described at &fw_cdev_event_iso_interrupt,
@@ -421,6 +423,9 @@ union fw_cdev_event {
421423
#define FW_CDEV_IOC_RECEIVE_PHY_PACKETS _IOW('#', 0x16, struct fw_cdev_receive_phy_packets)
422424
#define FW_CDEV_IOC_SET_ISO_CHANNELS _IOW('#', 0x17, struct fw_cdev_set_iso_channels)
423425

426+
/* available since kernel version 3.4 */
427+
#define FW_CDEV_IOC_FLUSH_ISO _IOW('#', 0x18, struct fw_cdev_flush_iso)
428+
424429
/*
425430
* ABI version history
426431
* 1 (2.6.22) - initial version
@@ -445,6 +450,7 @@ union fw_cdev_event {
445450
* %FW_CDEV_IOC_SET_ISO_CHANNELS
446451
* 5 (3.4) - send %FW_CDEV_EVENT_ISO_INTERRUPT events when needed to
447452
* avoid dropping data
453+
* - added %FW_CDEV_IOC_FLUSH_ISO
448454
*/
449455

450456
/**
@@ -854,6 +860,25 @@ struct fw_cdev_stop_iso {
854860
__u32 handle;
855861
};
856862

863+
/**
864+
* struct fw_cdev_flush_iso - flush completed iso packets
865+
* @handle: handle of isochronous context to flush
866+
*
867+
* For %FW_CDEV_ISO_CONTEXT_TRANSMIT or %FW_CDEV_ISO_CONTEXT_RECEIVE contexts,
868+
* report any completed packets.
869+
*
870+
* For %FW_CDEV_ISO_CONTEXT_RECEIVE_MULTICHANNEL contexts, report the current
871+
* offset in the receive buffer, if it has changed; this is typically in the
872+
* middle of some buffer chunk.
873+
*
874+
* Any %FW_CDEV_EVENT_ISO_INTERRUPT or %FW_CDEV_EVENT_ISO_INTERRUPT_MULTICHANNEL
875+
* events generated by this ioctl are sent synchronously, i.e., are available
876+
* for reading from the file descriptor when this ioctl returns.
877+
*/
878+
struct fw_cdev_flush_iso {
879+
__u32 handle;
880+
};
881+
857882
/**
858883
* struct fw_cdev_get_cycle_timer - read cycle timer register
859884
* @local_time: system time, in microseconds since the Epoch

‎include/linux/firewire.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ int fw_iso_context_queue(struct fw_iso_context *ctx,
426426
struct fw_iso_buffer *buffer,
427427
unsigned long payload);
428428
void fw_iso_context_queue_flush(struct fw_iso_context *ctx);
429+
int fw_iso_context_flush_completions(struct fw_iso_context *ctx);
429430
int fw_iso_context_start(struct fw_iso_context *ctx,
430431
int cycle, int sync, int tags);
431432
int fw_iso_context_stop(struct fw_iso_context *ctx);

0 commit comments

Comments
 (0)