summaryrefslogtreecommitdiffstats
path: root/private/ntos/ndis/digi/pcimac/util.c
blob: 448afe99075e97cce0c75cd78fd0b639f6104cef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * UTIL.C - some utility functions
 */

#include    <ndis.h>
#include	<mydefs.h>
#include	<mytypes.h>
#include    <util.h>

// Total Memory Allocated
ULONG		TotalMemory = 0;

/* constants */
#define     BUF_SIZE        512

#if !BINARY_COMPATIBLE
/* get current time, in seconds */
ULONG
ut_time_now(VOID)
{
    LARGE_INTEGER           curr_time;
    LARGE_INTEGER           sec;
    static LARGE_INTEGER    base = {0, 0};
    static BOOL             first_call = TRUE;

    /* get current system time */
    KeQuerySystemTime(&curr_time);

    /* on first call, store base */
    if ( first_call )
    {
        base = curr_time;
        first_call = FALSE;
    }
    /* make relative to base */
    curr_time.QuadPart -= base.QuadPart;
    
    /* convert to seconds */
    sec.QuadPart = curr_time.QuadPart/10000000L;
    
    /* return as a ULONG */
    return((ULONG)sec.LowPart);
}
#else
/* get current time, in seconds */
ULONG
ut_time_now(VOID)
{
    ULONG                   seconds;
    static BOOL             first_call = TRUE;

    static ULONG            base = 0;
    ULONG                   curr_time;


    // BUGBUG - Have to get real time somehow
     
    curr_time = base + 1;
     
    /* on first call, store base */
    if ( first_call )
    {
        base = curr_time;
        first_call = FALSE;
    }

    /* make relative to base */
    seconds = curr_time - base;

    /* return as a ULONG */
    return(seconds);
}
#endif

/* allocate a buffer */
CHAR*
ut_get_buf(VOID)
{
    CHAR    *ret;
    
    NDIS_PHYSICAL_ADDRESS   pa = NDIS_PHYSICAL_ADDRESS_CONST(-1, -1);

    NdisAllocateMemory((PVOID*)&ret, BUF_SIZE, 0, pa);

    return(ret);
}

/* free buffer */
VOID
ut_free_buf(CHAR *buf)
{
    if ( buf )
        NdisFreeMemory(buf, BUF_SIZE, 0);     
}

/* init */
VOID
sema_init(SEMA *s)
{
	s->signalled = FALSE;
	NdisAllocateSpinLock (&s->lock);
}

VOID
sema_term(SEMA *s)
{
	s->signalled = FALSE;
	NdisFreeSpinLock (&s->lock);
}

/* try to get semaphore */
BOOL
sema_get(SEMA *s)
{
    BOOL    ret;
//	KIRQL	NewIrql, OldIrql;
    
//	NewIrql = HIGH_LEVEL;

//	KeRaiseIrql (NewIrql, &OldIrql);

	NdisAcquireSpinLock (&s->lock);

    if ( !s->signalled )
        ret = s->signalled = TRUE;
    else
        ret = FALSE;
    
	NdisReleaseSpinLock (&s->lock);

//	KeLowerIrql (OldIrql);
    
    return(ret);
}

/* free semaphore */
VOID
sema_free(SEMA *s)
{
	NdisAcquireSpinLock (&s->lock);

    s->signalled = FALSE;

	NdisReleaseSpinLock (&s->lock);
}