#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>

int main( int argc, char *argv[] )
{
    uint32_t latency = 0;
    int fd;

    if( argc > 2 )
    {
        fprintf( stderr, "Usage: %s [latency limit in microseconds - default is 0]\n", argv[0] );
        exit( EXIT_FAILURE );
    }
    else if( argc == 2 )
    {
        latency = strtoul( argv[1], NULL, 0 );
    }

    fd = open( "/dev/cpu_dma_latency", O_WRONLY );
    if( fd == -1 )
    {
        perror( "Cannot open /dev/cpu_dma_latency" );
        exit( EXIT_FAILURE );
    }
    write( fd, &latency, sizeof(latency) );

    for(;;)
    {
        sleep( UINT_MAX );
    }
}