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 ;
456455end :
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
681681static 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 }
0 commit comments