/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool
321 by Suren A. Chilingaryan
Support computed (property-based) registers
1
#include <sys/time.h>
2
#include <arpa/inet.h>
3
#include <assert.h>
4
5
#include "pci.h"
6
#include "tools.h"
7
#include "model.h"
8
#include "error.h"
9
10
324 by Suren A. Chilingaryan
Documentation update
11
int pcilib_property_registers_read(pcilib_t *ctx, pcilib_register_bank_context_t *bank_ctx, pcilib_register_addr_t addr, pcilib_register_value_t *regval) {
321 by Suren A. Chilingaryan
Support computed (property-based) registers
12
    int err;
13
324 by Suren A. Chilingaryan
Documentation update
14
    const pcilib_register_bank_description_t *b = bank_ctx->bank;
15
    int access = b->access / 8;
16
17
    pcilib_view_t view = addr / access;
321 by Suren A. Chilingaryan
Support computed (property-based) registers
18
    pcilib_value_t val = {0};
19
325 by Suren A. Chilingaryan
Fix access to the property-based registers
20
    if (addr % access) {
21
        pcilib_error("Can't perform unaligned access to property register (the address is 0x%lx and alligment requirement is %u)", addr, access);
22
        return PCILIB_ERROR_INVALID_ARGUMENT;
23
    }
24
25
    if ((view == PCILIB_VIEW_INVALID)||(view >= ctx->num_views))
26
        return PCILIB_ERROR_INVALID_ARGUMENT;
27
28
    if ((ctx->views[view]->flags&PCILIB_VIEW_FLAG_REGISTER) == 0) {
29
        pcilib_error("Accessing invalid register %u (associated view %u does not provide register functionality)", addr, view);
30
        return PCILIB_ERROR_INVALID_REQUEST;
31
    }
32
33
    if ((ctx->views[view]->mode&PCILIB_ACCESS_R) == 0) {
34
        pcilib_error("Read access is not allowed to register %u (view %u)", addr, view);
35
        return PCILIB_ERROR_NOTPERMITED;
36
    }
321 by Suren A. Chilingaryan
Support computed (property-based) registers
37
38
    err = pcilib_get_property(ctx, ctx->views[view]->name, &val);
39
    if (err) return err;
40
41
    *regval = pcilib_get_value_as_register_value(ctx, &val, &err);
42
43
    return err;
44
}
45
46
324 by Suren A. Chilingaryan
Documentation update
47
int pcilib_property_registers_write(pcilib_t *ctx, pcilib_register_bank_context_t *bank_ctx, pcilib_register_addr_t addr, pcilib_register_value_t regval) {
321 by Suren A. Chilingaryan
Support computed (property-based) registers
48
    int err;
49
324 by Suren A. Chilingaryan
Documentation update
50
    const pcilib_register_bank_description_t *b = bank_ctx->bank;
51
    int access = b->access / 8;
52
53
    pcilib_view_t view = addr / access;
321 by Suren A. Chilingaryan
Support computed (property-based) registers
54
    pcilib_value_t val = {0};
55
325 by Suren A. Chilingaryan
Fix access to the property-based registers
56
    if (addr % access) {
57
        pcilib_error("Can't perform unaligned access to property register (the address is 0x%lx and alligment requirement is %u)", addr, access);
58
        return PCILIB_ERROR_INVALID_ARGUMENT;
59
    }
60
61
    if ((view == PCILIB_VIEW_INVALID)||(view >= ctx->num_views))
62
        return PCILIB_ERROR_INVALID_ARGUMENT;
63
64
65
    if ((ctx->views[view]->flags&PCILIB_VIEW_FLAG_REGISTER) == 0) {
66
        pcilib_error("Accessing invalid register %u (associated view %u does not provide register functionality)", addr, view);
67
        return PCILIB_ERROR_INVALID_REQUEST;
68
    }
69
70
    if ((ctx->views[view]->mode&PCILIB_ACCESS_W) == 0) {
71
        pcilib_error("Write access is not allowed to register %u (view %u)", addr, view);
72
        return PCILIB_ERROR_NOTPERMITED;
73
    }
74
321 by Suren A. Chilingaryan
Support computed (property-based) registers
75
76
    err = pcilib_set_value_from_register_value(ctx, &val, regval);
77
    if (err) return err;
78
79
    return pcilib_set_property(ctx, ctx->views[view]->name, &val);
80
}