/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool
403 by Suren A. Chilingaryan
Allow mapping of arbitrary memory areas
1
#include <stdio.h>
2
#include <string.h>
3
#include <strings.h>
4
#include <stdlib.h>
5
#include <stdint.h>
6
#include <fcntl.h>
7
#include <unistd.h>
8
#include <sys/ioctl.h>
9
#include <sys/mman.h>
10
#include <errno.h>
11
#include <assert.h>
12
13
#include "pcilib.h"
14
#include "locking.h"
15
#include "mem.h"
16
#include "error.h"
17
#include "pci.h"
18
19
20
21
void *pcilib_map_area(pcilib_t *ctx, uintptr_t addr, size_t size) {
22
    void *res;
23
    int err, ret; 
24
25
    err = pcilib_lock_global(ctx);
26
    if (err) {
27
	pcilib_error("Error (%i) acquiring mmap lock", err);
28
	return NULL;
29
    }
30
31
    ret = ioctl( ctx->handle, PCIDRIVER_IOC_MMAP_MODE, PCIDRIVER_MMAP_AREA );
32
    if (ret) {
33
	pcilib_unlock_global(ctx);
34
	pcilib_error("PCIDRIVER_IOC_MMAP_MODE ioctl have failed");
35
	return NULL;
36
    }
37
38
    res = mmap( 0, size, PROT_WRITE | PROT_READ, MAP_SHARED, ctx->handle, addr );
39
40
    pcilib_unlock_global(ctx);
41
42
    if ((!res)||(res == MAP_FAILED)) {
43
	pcilib_error("Failed to mmap area 0x%lx of size %zu bytes", addr, size);
44
	return NULL;
45
    }
46
47
    return res;
48
}
49
50
void pcilib_unmap_area(pcilib_t *ctx, void *addr, size_t size) {
51
    munmap(addr, size);
52
}