#include "linux/kernel.h"
#include "linux/unistd.h"
#include "region_mem.h"

//DECLARE_MUTEX(rm_mutex);

int sys_region_mem(unsigned int mode, char *buf, unsigned int offset, unsigned int length) {
    static char region[REGION_SIZE];
    int rval = -EPERM;
    
    if ((offset >= REGION_SIZE) || (length > REGION_SIZE) || (offset+length > REGION_SIZE))
	return -EFAULT; // Operation would have gone out of bounds
    else if (length == 0)
	return 0; // Nothing to copy, so stop here. Not an error, though.

    /*if (down_interruptible(&rm_mutex))
	return -EINTR;*/
    
    if (mode == REGION_MEM_READ) {
        rval = copy_to_user(buf, region+offset, length);
    } else if (mode == REGION_MEM_WRITE) {
        rval = copy_from_user(region+offset, buf, length);
    }
    //up(&rm_mutex);
    
    return rval;
}