/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool
330 by Suren A. Chilingaryan
Support for 64-bit registes
1
#define _POSIX_C_SOURCE 200112L
2
#define _GNU_SOURCE
3
4
#include <stdio.h>
5
#include <string.h>
6
#include <unistd.h>
7
#include <stdint.h>
8
#include <assert.h>
9
#include <ctype.h>
10
#include <time.h>
11
#include <sched.h>
12
#include <arpa/inet.h>
13
#include <sys/time.h>
14
15
#include "pci.h"
16
#include "tools.h"
17
#include "error.h"
18
19
void *pcilib_datacpy32(void * dst, void const * src, size_t n, pcilib_endianess_t endianess) {
20
    uint32_t * plDst = (uint32_t *) dst;
21
    uint32_t const * plSrc = (uint32_t const *) src;
22
23
    int swap = 0;
24
25
    if (endianess) 
26
        swap = (endianess == PCILIB_BIG_ENDIAN)?(ntohs(1)!=1):(ntohs(1)==1);
27
28
    if (swap) {
29
        while (n > 0) {
30
            *plDst = ntohl(*plSrc);
31
            ++plSrc;
32
            ++plDst;
33
            --n;
34
        }
35
    } else {
36
        while (n > 0) {
37
            *plDst = *plSrc;
38
            ++plSrc;
39
            ++plDst;
40
            --n;
41
        }
42
    }
43
44
    return dst;
45
} 
46
47
void *pcilib_datacpy64(void * dst, void const * src, size_t n, pcilib_endianess_t endianess) {
48
    uint64_t * plDst = (uint64_t *) dst;
49
    uint64_t const * plSrc = (uint64_t const *) src;
50
51
    int swap = 0;
52
53
    if (endianess) 
54
        swap = (endianess == PCILIB_BIG_ENDIAN)?(be64toh(1)!=1):(be64toh(1)==1);
55
56
    if (swap) {
57
        while (n > 0) {
58
            *plDst = ntohl(*plSrc);
59
            ++plSrc;
60
            ++plDst;
61
            --n;
62
        }
63
    } else {
64
        while (n > 0) {
65
            *plDst = *plSrc;
66
            ++plSrc;
67
            ++plDst;
68
            --n;
69
        }
70
    }
71
72
    return dst;
73
}
74
75
typedef void* (*pcilib_datacpy_routine_t)(void * dst, void const * src, size_t n, pcilib_endianess_t endianess);
76
static pcilib_datacpy_routine_t pcilib_datacpy_routines[4] = {
77
    NULL, NULL, pcilib_datacpy32, pcilib_datacpy64
78
};
79
80
void *pcilib_datacpy(void * dst, void const * src, uint8_t size, size_t n, pcilib_endianess_t endianess) {
81
    size_t pos = 0;
82
    pcilib_datacpy_routine_t routine;
83
345 by Suren A. Chilingaryan
64-bit access to BAR memory
84
    assert((size)&&(size <= 8));
330 by Suren A. Chilingaryan
Support for 64-bit registes
85
86
    while (size >>= 1) ++pos;
87
    routine = pcilib_datacpy_routines[pos];
88
89
    return routine(dst, src, n, endianess);
90
}