Skip to content

Commit a0e0233

Browse files
takaswietiwai
authored andcommitted
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>
1 parent 3aac326 commit a0e0233

10 files changed

Lines changed: 41 additions & 26 deletions

File tree

‎sound/firewire/amdtp-stream.c‎

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454

5555
/* TODO: make these configurable */
5656
#define INTERRUPT_INTERVAL 16
57-
#define QUEUE_LENGTH 48
5857

5958
// For iso header, tstamp and 2 CIP header.
6059
#define IR_CTX_HEADER_SIZE_CIP 16
@@ -451,7 +450,7 @@ static int queue_packet(struct amdtp_stream *s, struct fw_iso_packet *params)
451450
goto end;
452451
}
453452

454-
if (++s->packet_index >= QUEUE_LENGTH)
453+
if (++s->packet_index >= s->queue_size)
455454
s->packet_index = 0;
456455
end:
457456
return err;
@@ -669,13 +668,14 @@ static inline u32 increment_cycle_count(u32 cycle, unsigned int addend)
669668
}
670669

671670
// Align to actual cycle count for the packet which is going to be scheduled.
672-
// This module queued the same number of isochronous cycle as QUEUE_LENGTH to
673-
// skip isochronous cycle, therefore it's OK to just increment the cycle by
674-
// QUEUE_LENGTH for scheduled cycle.
675-
static inline u32 compute_it_cycle(const __be32 ctx_header_tstamp)
671+
// This module queued the same number of isochronous cycle as the size of queue
672+
// to kip isochronous cycle, therefore it's OK to just increment the cycle by
673+
// the size of queue for scheduled cycle.
674+
static inline u32 compute_it_cycle(const __be32 ctx_header_tstamp,
675+
unsigned int queue_size)
676676
{
677677
u32 cycle = compute_cycle_count(ctx_header_tstamp);
678-
return increment_cycle_count(cycle, QUEUE_LENGTH);
678+
return increment_cycle_count(cycle, queue_size);
679679
}
680680

681681
static int generate_device_pkt_descs(struct amdtp_stream *s,
@@ -689,7 +689,7 @@ static int generate_device_pkt_descs(struct amdtp_stream *s,
689689

690690
for (i = 0; i < packets; ++i) {
691691
struct pkt_desc *desc = descs + i;
692-
unsigned int index = (s->packet_index + i) % QUEUE_LENGTH;
692+
unsigned int index = (s->packet_index + i) % s->queue_size;
693693
unsigned int cycle;
694694
unsigned int payload_length;
695695
unsigned int data_blocks;
@@ -730,9 +730,9 @@ static void generate_ideal_pkt_descs(struct amdtp_stream *s,
730730

731731
for (i = 0; i < packets; ++i) {
732732
struct pkt_desc *desc = descs + i;
733-
unsigned int index = (s->packet_index + i) % QUEUE_LENGTH;
733+
unsigned int index = (s->packet_index + i) % s->queue_size;
734734

735-
desc->cycle = compute_it_cycle(*ctx_header);
735+
desc->cycle = compute_it_cycle(*ctx_header, s->queue_size);
736736
desc->syt = calculate_syt(s, desc->cycle);
737737
desc->data_blocks = calculate_data_blocks(s, desc->syt);
738738

@@ -779,12 +779,15 @@ static void out_stream_callback(struct fw_iso_context *context, u32 tstamp,
779779
{
780780
struct amdtp_stream *s = private_data;
781781
const __be32 *ctx_header = header;
782-
unsigned int packets = header_length / sizeof(*ctx_header);
782+
unsigned int packets;
783783
int i;
784784

785785
if (s->packet_index < 0)
786786
return;
787787

788+
// Calculate the number of packets in buffer and check XRUN.
789+
packets = header_length / sizeof(*ctx_header);
790+
788791
generate_ideal_pkt_descs(s, s->pkt_descs, ctx_header, packets);
789792

790793
process_ctx_payloads(s, s->pkt_descs, packets);
@@ -828,7 +831,7 @@ static void in_stream_callback(struct fw_iso_context *context, u32 tstamp,
828831
if (s->packet_index < 0)
829832
return;
830833

831-
// The number of packets in buffer.
834+
// Calculate the number of packets in buffer and check XRUN.
832835
packets = header_length / s->ctx_data.tx.ctx_header_size;
833836

834837
err = generate_device_pkt_descs(s, s->pkt_descs, ctx_header, packets);
@@ -874,7 +877,7 @@ static void amdtp_stream_first_callback(struct fw_iso_context *context,
874877

875878
context->callback.sc = in_stream_callback;
876879
} else {
877-
cycle = compute_it_cycle(*ctx_header);
880+
cycle = compute_it_cycle(*ctx_header, s->queue_size);
878881

879882
context->callback.sc = out_stream_callback;
880883
}
@@ -894,7 +897,8 @@ static void amdtp_stream_first_callback(struct fw_iso_context *context,
894897
* amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
895898
* device can be started.
896899
*/
897-
static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
900+
static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed,
901+
struct amdtp_domain *d)
898902
{
899903
static const struct {
900904
unsigned int data_block;
@@ -908,6 +912,7 @@ static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
908912
[CIP_SFC_88200] = { 0, 67 },
909913
[CIP_SFC_176400] = { 0, 67 },
910914
};
915+
unsigned int events_per_buffer = d->events_per_buffer;
911916
unsigned int ctx_header_size;
912917
unsigned int max_ctx_payload_size;
913918
enum dma_data_direction dir;
@@ -953,7 +958,13 @@ static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
953958
max_ctx_payload_size -= IT_PKT_HEADER_SIZE_CIP;
954959
}
955960

956-
err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
961+
if (events_per_buffer == 0)
962+
events_per_buffer = INTERRUPT_INTERVAL * 3;
963+
964+
s->queue_size = DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_buffer,
965+
amdtp_rate_table[s->sfc]);
966+
967+
err = iso_packets_buffer_init(&s->buffer, s->unit, s->queue_size,
957968
max_ctx_payload_size, dir);
958969
if (err < 0)
959970
goto err_unlock;
@@ -981,7 +992,7 @@ static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
981992
else
982993
s->tag = TAG_CIP;
983994

984-
s->pkt_descs = kcalloc(INTERRUPT_INTERVAL, sizeof(*s->pkt_descs),
995+
s->pkt_descs = kcalloc(s->queue_size, sizeof(*s->pkt_descs),
985996
GFP_KERNEL);
986997
if (!s->pkt_descs) {
987998
err = -ENOMEM;
@@ -1196,7 +1207,7 @@ int amdtp_domain_start(struct amdtp_domain *d)
11961207
int err = 0;
11971208

11981209
list_for_each_entry(s, &d->streams, list) {
1199-
err = amdtp_stream_start(s, s->channel, s->speed);
1210+
err = amdtp_stream_start(s, s->channel, s->speed, d);
12001211
if (err < 0)
12011212
break;
12021213
}

‎sound/firewire/amdtp-stream.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ struct amdtp_stream {
117117
/* For packet processing. */
118118
struct fw_iso_context *context;
119119
struct iso_packets_buffer buffer;
120+
unsigned int queue_size;
120121
int packet_index;
121122
struct pkt_desc *pkt_descs;
122123
int tag;
@@ -274,6 +275,7 @@ struct amdtp_domain {
274275
struct list_head streams;
275276

276277
unsigned int events_per_period;
278+
unsigned int events_per_buffer;
277279
};
278280

279281
int amdtp_domain_init(struct amdtp_domain *d);
@@ -286,9 +288,11 @@ int amdtp_domain_start(struct amdtp_domain *d);
286288
void amdtp_domain_stop(struct amdtp_domain *d);
287289

288290
static inline int amdtp_domain_set_events_per_period(struct amdtp_domain *d,
289-
unsigned int events_per_period)
291+
unsigned int events_per_period,
292+
unsigned int events_per_buffer)
290293
{
291294
d->events_per_period = events_per_period;
295+
d->events_per_buffer = events_per_buffer;
292296

293297
return 0;
294298
}

‎sound/firewire/bebob/bebob_stream.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ int snd_bebob_stream_reserve_duplex(struct snd_bebob *bebob, unsigned int rate,
610610
}
611611

612612
err = amdtp_domain_set_events_per_period(&bebob->domain,
613-
frames_per_period);
613+
frames_per_period, 0);
614614
if (err < 0) {
615615
cmp_connection_release(&bebob->out_conn);
616616
cmp_connection_release(&bebob->in_conn);

‎sound/firewire/dice/dice-stream.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ int snd_dice_stream_reserve_duplex(struct snd_dice *dice, unsigned int rate,
327327
goto error;
328328

329329
err = amdtp_domain_set_events_per_period(&dice->domain,
330-
events_per_period);
330+
events_per_period, 0);
331331
if (err < 0)
332332
goto error;
333333
}

‎sound/firewire/digi00x/digi00x-stream.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ int snd_dg00x_stream_reserve_duplex(struct snd_dg00x *dg00x, unsigned int rate,
318318
}
319319

320320
err = amdtp_domain_set_events_per_period(&dg00x->domain,
321-
frames_per_period);
321+
frames_per_period, 0);
322322
if (err < 0) {
323323
fw_iso_resources_free(&dg00x->rx_resources);
324324
fw_iso_resources_free(&dg00x->tx_resources);

‎sound/firewire/fireface/ff-stream.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ int snd_ff_stream_reserve_duplex(struct snd_ff *ff, unsigned int rate,
153153
return err;
154154

155155
err = amdtp_domain_set_events_per_period(&ff->domain,
156-
frames_per_period);
156+
frames_per_period, 0);
157157
if (err < 0) {
158158
fw_iso_resources_free(&ff->tx_resources);
159159
fw_iso_resources_free(&ff->rx_resources);

‎sound/firewire/fireworks/fireworks_stream.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ int snd_efw_stream_reserve_duplex(struct snd_efw *efw, unsigned int rate,
231231
}
232232

233233
err = amdtp_domain_set_events_per_period(&efw->domain,
234-
frames_per_period);
234+
frames_per_period, 0);
235235
if (err < 0) {
236236
cmp_connection_release(&efw->in_conn);
237237
cmp_connection_release(&efw->out_conn);

‎sound/firewire/motu/motu-stream.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ int snd_motu_stream_reserve_duplex(struct snd_motu *motu, unsigned int rate,
174174
}
175175

176176
err = amdtp_domain_set_events_per_period(&motu->domain,
177-
frames_per_period);
177+
frames_per_period, 0);
178178
if (err < 0) {
179179
fw_iso_resources_free(&motu->tx_resources);
180180
fw_iso_resources_free(&motu->rx_resources);

‎sound/firewire/oxfw/oxfw-stream.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ int snd_oxfw_stream_reserve_duplex(struct snd_oxfw *oxfw,
308308
}
309309

310310
err = amdtp_domain_set_events_per_period(&oxfw->domain,
311-
frames_per_period);
311+
frames_per_period, 0);
312312
if (err < 0) {
313313
cmp_connection_release(&oxfw->in_conn);
314314
if (oxfw->has_output)

‎sound/firewire/tascam/tascam-stream.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ int snd_tscm_stream_reserve_duplex(struct snd_tscm *tscm, unsigned int rate,
416416
}
417417

418418
err = amdtp_domain_set_events_per_period(&tscm->domain,
419-
frames_per_period);
419+
frames_per_period, 0);
420420
if (err < 0) {
421421
fw_iso_resources_free(&tscm->tx_resources);
422422
fw_iso_resources_free(&tscm->rx_resources);

0 commit comments

Comments
 (0)