2 * Copyright (C) 2001-2003 by NBMK Encryption Technologies.
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>.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are
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 static char const n8_id
[] = "$Id: helper.c,v 1.2 2008/11/03 04:31:01 tls Exp $";
36 /*****************************************************************************/
38 * @brief BSDi System Call Abstraction. *
40 * This file implements the BSDi versions of some standard system calls *
41 * that drivers commonly make, in a variety of platforms. It also ensures *
42 * that the necessary system includes are included as well. *
43 *****************************************************************************/
45 /*****************************************************************************
47 * 02/12/04 bac Fixed n8_usleep to work when the tick interval changes.
48 * 05/05/03 brr Moved memory functions to n8_memory_bsd.c.
49 * 04/23/03 brr Remove warnings from n8_delay_ms.
50 * 03/12/03 jpw Added function n8_delay_ms - keep this spinlock safe!
51 * 12/10/02 brr Added function n8_gettime.
52 * 10/25/02 brr Clean up function prototypes & include files.
53 * 06/12/02 brr Added N8_WaitEventInt function.
54 * 04/02/02 spm Changed usec normalizer in n8_usleep from 1000 to 1000000.
55 * this assures that units of jiffies are passed to tsleep.
56 * Added case to sleep for minimum yield if usecs is less
58 * 03/11/02 brr Update for 2.1.
59 * 10/12/01 mmd Renamed Atomic*() routines to N8_Atomic*().
60 * 10/04/01 mmd Original version.
61 ****************************************************************************/
62 /** @defgroup NSP2000Driver BSDi System Call Abstraction.
67 #include "n8_pub_errors.h"
68 #include "n8_driver_main.h"
72 /* Since BSD does not give us a mechanism to update the write pointer */
73 /* and ensure this process is asleep before the interrupt fires, this */
74 /* fuction attempts to solve the problem. It goes to sleep for an */
75 /* arbitrary long wait. If the ISR wakes this process up, we check */
76 /* the request immediately and return on realizing the event has */
77 /* completed. If the ISR fails to wake us because we are not */
78 /* completely suspended, the timeout guarentees we wake up eventually */
79 /* and check to see if the ISR has marked our request as complete. In */
80 /* which case we return succes. Otherwise we exceed out timeout count */
81 /* and return FAILURE. The count value may need adjustment based on */
82 /* traffic load on the system. */
84 int N8_WaitEventInt(n8_WaitQueue_t
*waitQueue_p
, API_Request_t
*API_req_p
)
90 if (API_req_p
->qr
.requestStatus
== N8_QUEUE_REQUEST_FINISHED
)
92 /* The event has completed, return success. */
95 tsleep(waitQueue_p
, PWAIT
, "QMQueue", (1*HZ
));
103 /*****************************************************************************
104 * N8_BlockWithTimeout
105 *****************************************************************************/
106 /** @ingroup NSP2000Driver
107 * @brief Block for notification or specified timeout.
109 * This routine abstracts the Linux system call to block until release or
112 * @param *WaitQueue RO: Specifies the block element
113 * @param timeout RO: Specifies the timeout
114 * @param debug RO: Specifies whether to display debug messages
120 * 1 = success - was woken up by corresponding call to wakeup.
121 * 0 = failure - timed out.
124 * See return section for error information.
125 *****************************************************************************/
127 unsigned char N8_BlockWithTimeout( wait_queue_head_t
*WaitQueue
,
128 unsigned long timeout
,
129 unsigned char debug
)
133 /* BLOCK UNTIL RELEASE, TIMEOUT, OR SIGNAL */
134 rc
= tsleep(WaitQueue
, (PWAIT
| PCATCH
), "N8_BlockWithTimeout", (timeout
* hz
));
139 printf("NSP2000: Received desired interrupt.\n");
143 if (debug
&& (rc
== EWOULDBLOCK
))
145 printf("NSP2000: Timed out while awaiting interrupt.\n");
149 printf("NSP2000: An unexpected signal has interrupted N8_BlockWithTimeout.\n");
156 /*****************************************************************************
158 *****************************************************************************/
159 /** @ingroup NSP2000Driver
160 * @brief Sleep usec seconds.
162 * The minimum delay is N8_MINIMUM_YIELD_USECS, which is usually a system
166 * @param usecs RO: Microseconds to sleep
169 * HZ is the CPU clock speed in Hertz (defined in Linux
170 * kernel headers; usually 100 Hz).
173 * N8_STATUS_OK Success
176 * See return section.
183 *****************************************************************************/
186 n8_usleep(unsigned int usecs
)
190 /* the minimum amount to sleep is 1 tick. if requested sleep time is less
191 * than 1 tick, sleep for 1 tick. */
192 if (usecs
< N8_USECS_PER_TICK
)
194 tsleep(&tmp
,PZERO
,"NSP2000 udelay", 1 /* tick */);
198 tsleep(&tmp
,PZERO
,"NSP2000 udelay",((usecs
)*hz
/N8_USECS_PER_SEC
));
205 /*****************************************************************************
207 *****************************************************************************/
208 /** @ingroup NSP2000Driver
209 * @brief Sleep for given milliseconds.
212 * @param usecs RO: Microseconds to sleep
215 * HZ is the CPU clock speed in Hertz (defined in Linux
216 * kernel headers; usually 100 Hz).
219 * N8_STATUS_OK Success
222 * See return section.
229 *****************************************************************************/
232 n8_delay_ms(unsigned int milliseconds
)
235 DELAY((milliseconds
*1000));
240 /*****************************************************************************
242 *****************************************************************************/
243 /** @ingroup NSP2000Driver
244 * @brief Get current timeval
246 * @param n8_timeResults_p WO: Returned time value in n8_timeval_t,
253 * N8_STATUS_OK Success
264 *****************************************************************************/
267 n8_gettime( n8_timeval_t
*n8_timeResults_p
)
271 N8_Status_t returnResults
= N8_STATUS_OK
;
275 /* Timespec has a seconds portion and a nano seconds portion. */
276 /* Thus we need to divide to convert nanoseconds to microseconds. */
277 n8_timeResults_p
->tv_sec
= ts
.tv_sec
;
278 n8_timeResults_p
->tv_usec
= ts
.tv_nsec
/ 1000;
280 return returnResults
;