summaryrefslogtreecommitdiff
path: root/qdf/linux/src/qdf_event.c
blob: 06faa3c0a3c0a8ec6c42a70d6c17f5cc09cd4137 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright (c) 2014-2017 The Linux Foundation. All rights reserved.
 *
 * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
 *
 *
 * Permission to use, copy, modify, and/or distribute this software for
 * any purpose with or without fee is hereby granted, provided that the
 * above copyright notice and this permission notice appear in all
 * copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * This file was originally distributed by Qualcomm Atheros, Inc.
 * under proprietary terms before Copyright ownership was assigned
 * to the Linux Foundation.
 */

/**
 * DOC: qdf_event.c
 *
 * This source file contains linux specific definitions for QDF event APIs
 * The APIs mentioned in this file are used for initializing, setting,
 * resetting, destroying an event and waiting on an occurance of an event
 * among multiple events.
 */

/* Include Files */
#include "qdf_event.h"
#include <linux/export.h>
#include <qdf_module.h>

/* Function Definitions and Documentation */

/**
 * qdf_event_create() - initializes a QDF event
 * @event: Pointer to the opaque event object to initialize
 *
 * The qdf_event_create() function initializes the specified event. Upon
 * successful initialization, the state of the event becomes initialized
 * and not signalled.
 *
 * An event must be initialized before it may be used in any other event
 * functions.
 * Attempting to initialize an already initialized event results in
 * a failure.
 *
 * Return: QDF status
 */
QDF_STATUS qdf_event_create(qdf_event_t *event)
{
	/* check for null pointer */
	if (NULL == event) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "NULL event passed into %s", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_FAULT;
	}

	/* check for 'already initialized' event */
	if (LINUX_EVENT_COOKIE == event->cookie) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "Initialized event passed into %s", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_BUSY;
	}

	/* initialize new event */
	init_completion(&event->complete);
	event->cookie = LINUX_EVENT_COOKIE;

	return QDF_STATUS_SUCCESS;
}
qdf_export_symbol(qdf_event_create);

/**
 * qdf_event_set() - sets a QDF event
 * @event: The event to set to the signalled state
 *
 * The state of the specified event is set to signalled by calling
 * qdf_event_set().
 *
 * Any threads waiting on the event as a result of a qdf_event_wait() will
 * be unblocked and available to be scheduled for execution when the event
 * is signaled by a call to qdf_event_set().
 *
 * Return: QDF status
 */
QDF_STATUS qdf_event_set(qdf_event_t *event)
{
	/* check for null pointer */
	if (NULL == event) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "NULL event passed into %s", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_FAULT;
	}

	/* check if event refers to an initialized object */
	if (LINUX_EVENT_COOKIE != event->cookie) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "Uninitialized event passed into %s", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_INVAL;
	}

	complete(&event->complete);

	return QDF_STATUS_SUCCESS;
}
qdf_export_symbol(qdf_event_set);

/**
 * qdf_event_reset() - resets a QDF event
 * @event: The event to set to the NOT signalled state
 *
 * This function isn't required for Linux. Therefore, it doesn't do much.
 *
 * The state of the specified event is set to 'NOT signalled' by calling
 * qdf_event_reset().  The state of the event remains NOT signalled until an
 * explicit call to qdf_event_set().
 *
 * This function sets the event to a NOT signalled state even if the event was
 * signalled multiple times before being signaled.
 *
 * Return: QDF status
 */
QDF_STATUS qdf_event_reset(qdf_event_t *event)
{
	/* check for null pointer */
	if (NULL == event) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "NULL event passed into %s", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_FAULT;
	}

	/* check to make sure it is an 'already initialized' event */
	if (LINUX_EVENT_COOKIE != event->cookie) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "Uninitialized event passed into %s", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_INVAL;
	}

	/* (re)initialize event */
	INIT_COMPLETION(event->complete);
	return QDF_STATUS_SUCCESS;
}
qdf_export_symbol(qdf_event_reset);

/**
 * qdf_event_destroy() - Destroys a QDF event
 * @event: The event object to be destroyed.
 *
 * This function doesn't do much in Linux. There is no need for the caller
 * to explicitly destroy an event after use.
 *
 * The os_event_destroy() function shall destroy the event object
 * referenced by event.  After a successful return from qdf_event_destroy()
 * the event object becomes, in effect, uninitialized.
 *
 * A destroyed event object can be reinitialized using qdf_event_create();
 * the results of otherwise referencing the object after it has been destroyed
 * are undefined.  Calls to QDF event functions to manipulate the lock such
 * as qdf_event_set() will fail if the event is destroyed.  Therefore,
 * don't use the event after it has been destroyed until it has
 * been re-initialized.
 *
 * Return: QDF status
 */
QDF_STATUS qdf_event_destroy(qdf_event_t *event)
{
	/* check for null pointer */
	if (NULL == event) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "NULL event passed into %s", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_FAULT;
	}

	/* check to make sure it is an 'already initialized' event */
	if (LINUX_EVENT_COOKIE != event->cookie) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "Uninitialized event passed into %s", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_INVAL;
	}

	/* make sure nobody is waiting on the event */
	complete_all(&event->complete);

	/* destroy the event */
	memset(event, 0, sizeof(qdf_event_t));

	return QDF_STATUS_SUCCESS;
}
qdf_export_symbol(qdf_event_destroy);

/**
 * qdf_wait_single_event() - Waits for a single event to be set.
 * This API waits for the event to be set.
 *
 * @event: Pointer to an event to wait on.
 * @timeout: Timeout value (in milliseconds).  This function returns
 * if this interval elapses, regardless if any of the events have
 * been set.  An input value of 0 for this timeout parameter means
 * to wait infinitely, meaning a timeout will never occur.
 *
 * Return: QDF status
 */
QDF_STATUS qdf_wait_single_event(qdf_event_t *event, uint32_t timeout)
{
	if (in_interrupt()) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s cannot be called from interrupt context!!!",
			  __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_FAULT;
	}

	/* check for null pointer */
	if (NULL == event) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "NULL event passed into %s", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_FAULT;
	}

	/* check if cookie is same as that of initialized event */
	if (LINUX_EVENT_COOKIE != event->cookie) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "Uninitialized event passed into %s", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_INVAL;
	}

	if (timeout) {
		long ret;
		ret = wait_for_completion_timeout(&event->complete,
						  msecs_to_jiffies(timeout));
		if (0 >= ret)
			return QDF_STATUS_E_TIMEOUT;
	} else {
		wait_for_completion(&event->complete);
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_INFO,
			  "Signaled for completion %s", __func__);
		return QDF_STATUS_SUCCESS;
	}
	return QDF_STATUS_SUCCESS;
}
qdf_export_symbol(qdf_wait_single_event);