Sync usage with man page.
[netbsd-mini2440.git] / sys / dev / pci / n8 / common / api / n8_API_Initialize.c
blobf1eb70ec42a3e9bef5b702d5690b86b947ca6f87
1 /*-
2 * Copyright (C) 2001-2003 by NBMK Encryption Technologies.
3 * All rights reserved.
5 * NBMK Encryption Technologies provides no support of any kind for
6 * this software. Questions or concerns about it may be addressed to
7 * the members of the relevant open-source community at
8 * <tech-crypto@netbsd.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are
12 * met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 /*****************************************************************************
36 * @(#) n8_API_Initialize.c 1.2@(#)
37 *****************************************************************************/
39 /*****************************************************************************/
40 /** @file n8_API_Initialize.c
41 * @brief user space icing layer file.
44 *****************************************************************************/
46 /*****************************************************************************
47 * Revision history:
48 * 08/01/03 brr In N8_TerminateAPI, only shutdown the callback if it has
49 * been initialized. (Bug 949)
50 * 06/23/03 brr Check & set API_init_g in N8_InitializeAPI to avoid
51 * duplicate initialization.
52 * 06/06/03 brr Fix Bug 928, Reset initialization parameters when NULL
53 * pointer is passed.
54 * 03/11/03 brr Modified N8_TerminateAPI to call N8_CloseDevice.
55 * 02/25/03 brr Added callback initialization & minor modifications to
56 * support both user & driver init in the same file.
57 * 10/22/02 brr Added openMode parameter to N8_OpenDevice call.
58 * 05/07/02 brr Pass bncAddress to allocateBNCConstants.
59 * 04/12/02 hml N8_InitializeAPI now returns N8_HARDWARE_UNAVAILABLE
60 * if the call to open the device driver returns
61 * N8_INVALID_OBJECT or the number of chips is 0.
62 * 03/18/02 brr Reinstate process_init_sem.
63 * 02/25/02 brr Retrieve driver info at initialization.
64 * 02/21/02 brr Perform BNC constant initialization once upon startup.
65 * 01/10/01 hml Include file changes and ifdef out of sem ops.
66 * 11/30/01 hml Original version
67 ****************************************************************************/
68 /** @defgroup icing Icing Layer
71 #include "n8_util.h"
72 #include "n8_API_Initialize.h"
73 #include "n8_pub_errors.h"
74 #include "n8_driver_api.h"
75 #include "nsp_ioctl.h"
76 #include "n8_semaphore.h"
77 #include "n8_cb_rsa.h"
80 /* The initialization global */
81 static N8_Boolean_t API_init_g = N8_FALSE;
82 static N8_Boolean_t deviceOpen_g = N8_FALSE;
83 static N8_ConfigAPI_t apiConfig_g = {0, 0, 0};
85 NSPdriverInfo_t nspDriverInfo;
86 /*****************************************************************************
87 * N8_preamble
88 *****************************************************************************/
89 /** @ingroup Init
90 * @brief Performs initialization status check.
93 * @return
94 * returnResult - returns N8_STATUS_OK if successful or Error value.
95 * @par Errors
96 * Errors returned by N8_Initialize()
97 * @par Assumptions
98 * None<br>
99 *****************************************************************************/
100 N8_Status_t N8_preamble()
102 N8_Status_t ret = N8_STATUS_OK;
104 /* Simply check the initialization flag and return if the API has already */
105 /* been initialized. This avoids the overhead of the semaphore once the */
106 /* API has been initialized. */
107 if (API_init_g == N8_FALSE)
109 ret = N8_InitializeAPI(NULL);
111 return ret;
113 } /* N8_preamble */
115 /*****************************************************************************
116 * N8_InitializeAPI
117 *****************************************************************************/
118 /** @ingroup Init
119 * @brief Performs required hardware and software initialization of the Simon
120 * hardware.
122 * This call must be made before any other API call is made. Until
123 * N8_API_Initialize has been called successfully, all other API calls will
124 * fail. This call checks that the hardware is present, addressable, and
125 * functioning at a minimal level. It establishes the queues in host memory
126 * used to communicate with the Simon hardware and all other software
127 * initialization. Parameters may be null, if which case all of the built-in
128 * system default configuration values and parameters are used. If non-null,
129 * Parameters specifies a file name of a file containing initialization and
130 * configuration values used to override the default values. Any parameter
131 * not explicitly overriden in this file retains its default value.
134 * @param parameters_p RO: If null, then no initialization/configuration
135 * parameters are specified. In this case,
136 * built in default values will be used.
137 * If non-null, this indicates a pointer to a
138 * configuration structure containing
139 * initialization/configuration parameters to be used.
141 * @return
142 * returnResult - returns N8_STATUS_OK if successful or Error value.
143 * @par Errors
144 * N8_INVALID_OBJECT - Parameters does not specify a valid pathname to a valid parameters file..
145 * N8_INVALID_VALUE - One or more of the initialization parameters in the Parameters structure is invalid.
146 * @par Assumptions
147 * None<br>
148 *****************************************************************************/
149 N8_Status_t N8_InitializeAPI(N8_ConfigAPI_t *parameters_p)
151 N8_Status_t ret = N8_STATUS_OK;
154 n8_acquire_process_init_sem();
156 /* Check this flag again, after the process init semphore has been taken */
157 /* to avoid a race condition with two concurrent initializers. */
158 if (API_init_g == N8_FALSE)
160 if (parameters_p)
162 if (parameters_p->structure_version != N8_INITIALIZATION_VERSION)
164 ret = N8_INVALID_VALUE;
166 else
168 apiConfig_g = *parameters_p;
171 else
173 memset(&apiConfig_g, 0, sizeof(N8_ConfigAPI_t));
175 if ((deviceOpen_g == N8_FALSE) && (ret == N8_STATUS_OK))
177 /* Open the driver */
178 nspDriverInfo.numChips = 0;
180 ret = N8_OpenDevice(&nspDriverInfo, N8_TRUE, N8_OPEN_APP);
182 if ((ret == N8_INVALID_OBJECT) || (nspDriverInfo.numChips == 0))
184 ret = N8_HARDWARE_UNAVAILABLE;
186 else if (ret == N8_STATUS_OK)
188 ret = allocateBNCConstants(nspDriverInfo.bncAddress);
189 if (ret == N8_STATUS_OK)
191 deviceOpen_g = N8_TRUE;
193 #ifdef SUPPORT_CALLBACK_THREAD
194 if (apiConfig_g.callbackEvents != 0)
196 callbackInit(apiConfig_g.callbackEvents, apiConfig_g.callbackTimeout);
198 #endif
202 /* Initialization has completed successfully */
203 if (ret == N8_STATUS_OK)
205 API_init_g = N8_TRUE;
209 n8_release_process_init_sem();
211 return ret;
215 /***************************************************************************n
216 * N8_TerminateAPI
217 *****************************************************************************/
218 /** @ingroup Init
219 * @brief Shutdowns and releases resources set up by N8_InitializeAPI.
221 * This call should be the last API call made. It closes the driver and performs
222 * any necessary cleanup to ensure an orderly shutdown.
224 * @return
225 * returnResult - returns N8_STATUS_OK if successful or Error value.
227 * @par Errors
229 * @par Assumptions
230 * None
231 *****************************************************************************/
232 N8_Status_t N8_TerminateAPI(void)
234 N8_Status_t ret = N8_STATUS_OK;
236 #ifdef SUPPORT_CALLBACK_THREAD
237 if (apiConfig_g.callbackEvents != 0)
239 ret = callbackShutdown();
241 #endif
243 N8_CloseDevice();
244 deviceOpen_g = N8_FALSE;
245 API_init_g = N8_FALSE;
247 return ret;
250 /***************************************************************************n
251 * n8_getConfigInfo
252 *****************************************************************************/
253 /** @ingroup Init
254 * @brief Retrieves the data structure passed to N8_InitializeAPI.
256 * This function retrieves the parameters that were used to configure the API.
258 * @return
259 * returnResult - returns N8_STATUS_OK if successful or Error value.
261 * @par Errors
263 * @par Assumptions
264 * None
265 *****************************************************************************/
266 N8_Status_t n8_getConfigInfo(N8_ConfigAPI_t *config_p)
268 *config_p = apiConfig_g;
269 return (N8_STATUS_OK);