fb50b41e9f8f49c50a10fba2fd8076b4b7bab24b
[15.05/openwrt.git] / package / ubsec_ssb / src / ocf-compat.h
1 #ifndef _BSD_COMPAT_H_
2 #define _BSD_COMPAT_H_ 1
3 /****************************************************************************/
4 /*
5  * Provide compat routines for older linux kernels and BSD kernels
6  *
7  * Written by David McCullough <david_mccullough@securecomputing.com>
8  * Copyright (C) 2007 David McCullough <david_mccullough@securecomputing.com>
9  *
10  * LICENSE TERMS
11  *
12  * The free distribution and use of this software in both source and binary
13  * form is allowed (with or without changes) provided that:
14  *
15  *   1. distributions of this source code include the above copyright
16  *      notice, this list of conditions and the following disclaimer;
17  *
18  *   2. distributions in binary form include the above copyright
19  *      notice, this list of conditions and the following disclaimer
20  *      in the documentation and/or other associated materials;
21  *
22  *   3. the copyright holder's name is not used to endorse products
23  *      built using this software without specific written permission.
24  *
25  * ALTERNATIVELY, provided that this notice is retained in full, this file
26  * may be distributed under the terms of the GNU General Public License (GPL),
27  * in which case the provisions of the GPL apply INSTEAD OF those given above.
28  *
29  * DISCLAIMER
30  *
31  * This software is provided 'as is' with no explicit or implied warranties
32  * in respect of its properties, including, but not limited to, correctness
33  * and/or fitness for purpose.
34  */
35 /****************************************************************************/
36 #ifdef __KERNEL__
37 /*
38  * fake some BSD driver interface stuff specifically for OCF use
39  */
40
41 typedef struct ocf_device *device_t;
42
43 typedef struct {
44         int (*cryptodev_newsession)(device_t dev, u_int32_t *sidp, struct cryptoini *cri);
45         int (*cryptodev_freesession)(device_t dev, u_int64_t tid);
46         int (*cryptodev_process)(device_t dev, struct cryptop *crp, int hint);
47         int (*cryptodev_kprocess)(device_t dev, struct cryptkop *krp, int hint);
48 } device_method_t;
49 #define DEVMETHOD(id, func)     id: func
50
51 struct ocf_device {
52         char name[32];          /* the driver name */
53         char nameunit[32];      /* the driver name + HW instance */
54         int  unit;
55         device_method_t methods;
56         void *softc;
57 };
58
59 #define CRYPTODEV_NEWSESSION(dev, sid, cri) \
60         ((*(dev)->methods.cryptodev_newsession)(dev,sid,cri))
61 #define CRYPTODEV_FREESESSION(dev, sid) \
62         ((*(dev)->methods.cryptodev_freesession)(dev, sid))
63 #define CRYPTODEV_PROCESS(dev, crp, hint) \
64         ((*(dev)->methods.cryptodev_process)(dev, crp, hint))
65 #define CRYPTODEV_KPROCESS(dev, krp, hint) \
66         ((*(dev)->methods.cryptodev_kprocess)(dev, krp, hint))
67
68 #define device_get_name(dev)    ((dev)->name)
69 #define device_get_nameunit(dev)        ((dev)->nameunit)
70 #define device_get_unit(dev)    ((dev)->unit)
71 #define device_get_softc(dev)   ((dev)->softc)
72
73 #define softc_device_decl \
74                 struct ocf_device _device; \
75                 device_t
76
77 #define softc_device_init(_sc, _name, _unit, _methods) \
78         if (1) {\
79         strncpy((_sc)->_device.name, _name, sizeof((_sc)->_device.name) - 1); \
80         snprintf((_sc)->_device.nameunit, sizeof((_sc)->_device.name), "%s%d", _name, _unit); \
81         (_sc)->_device.unit = _unit; \
82         (_sc)->_device.methods = _methods; \
83         (_sc)->_device.softc = (void *) _sc; \
84         *(device_t *)((softc_get_device(_sc))+1) = &(_sc)->_device; \
85         } else
86
87 #define softc_get_device(_sc)   (&(_sc)->_device)
88
89 /*
90  * iomem support for 2.4 and 2.6 kernels
91  */
92 #include <linux/version.h>
93 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
94 #define ocf_iomem_t     unsigned long
95
96 /*
97  * implement simple workqueue like support for older kernels
98  */
99
100 #include <linux/tqueue.h>
101
102 #define work_struct tq_struct
103
104 #define INIT_WORK(wp, fp, ap) \
105         do { \
106                 (wp)->sync = 0; \
107                 (wp)->routine = (fp); \
108                 (wp)->data = (ap); \
109         } while (0)
110
111 #define schedule_work(wp) \
112         do { \
113                 queue_task((wp), &tq_immediate); \
114                 mark_bh(IMMEDIATE_BH); \
115         } while (0)
116
117 #define flush_scheduled_work()  run_task_queue(&tq_immediate)
118
119 #else
120 #define ocf_iomem_t     void __iomem *
121
122 #include <linux/workqueue.h>
123
124 #endif
125
126 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
127 #include <linux/fdtable.h>
128 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
129 #define files_fdtable(files)    (files)
130 #endif
131
132 #ifdef MODULE_PARM
133 #undef module_param     /* just in case */
134 #define module_param(a,b,c)             MODULE_PARM(a,"i")
135 #endif
136
137 #define bzero(s,l)              memset(s,0,l)
138 #define bcopy(s,d,l)    memcpy(d,s,l)
139 #define bcmp(x, y, l)   memcmp(x,y,l)
140
141 #define MIN(x,y)        ((x) < (y) ? (x) : (y))
142
143 #define device_printf(dev, a...) ({ \
144                                 printk("%s: ", device_get_nameunit(dev)); printk(a); \
145                         })
146
147 #undef printf
148 #define printf(fmt...)  printk(fmt)
149
150 #define KASSERT(c,p)    if (!(c)) { printk p ; } else
151
152 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
153 #define ocf_daemonize(str) \
154         daemonize(); \
155         spin_lock_irq(&current->sigmask_lock); \
156         sigemptyset(&current->blocked); \
157         recalc_sigpending(current); \
158         spin_unlock_irq(&current->sigmask_lock); \
159         sprintf(current->comm, str);
160 #else
161 #define ocf_daemonize(str) daemonize(str);
162 #endif
163
164 #define TAILQ_INSERT_TAIL(q,d,m) list_add_tail(&(d)->m, (q))
165 #define TAILQ_EMPTY(q)  list_empty(q)
166 #define TAILQ_FOREACH(v, q, m) list_for_each_entry(v, q, m)
167
168 #define read_random(p,l) get_random_bytes(p,l)
169
170 #define DELAY(x)        ((x) > 2000 ? mdelay((x)/1000) : udelay(x))
171 #define strtoul simple_strtoul
172
173 #define pci_get_vendor(dev)     ((dev)->vendor)
174 #define pci_get_device(dev)     ((dev)->device)
175
176 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
177 #define pci_set_consistent_dma_mask(dev, mask) (0)
178 #endif
179 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
180 #define pci_dma_sync_single_for_cpu pci_dma_sync_single
181 #endif
182
183 #ifndef DMA_32BIT_MASK
184 #define DMA_32BIT_MASK  0x00000000ffffffffULL
185 #endif
186
187 #define htole32(x)      cpu_to_le32(x)
188 #define htobe32(x)      cpu_to_be32(x)
189 #define htole16(x)      cpu_to_le16(x)
190 #define htobe16(x)      cpu_to_be16(x)
191
192 /* older kernels don't have these */
193
194 #ifndef IRQ_NONE
195 #define IRQ_NONE
196 #define IRQ_HANDLED
197 #define irqreturn_t void
198 #endif
199 #ifndef IRQF_SHARED
200 #define IRQF_SHARED     SA_SHIRQ
201 #endif
202
203 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
204 # define strlcpy(dest,src,len) \
205                 ({strncpy(dest,src,(len)-1); ((char *)dest)[(len)-1] = '\0'; })
206 #endif
207
208 #ifndef MAX_ERRNO
209 #define MAX_ERRNO       4095
210 #endif
211 #ifndef IS_ERR_VALUE
212 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
213 #endif
214
215 /*
216  * common debug for all
217  */
218 #if 1
219 #define dprintk(a...)   do { if (debug) printk(a); } while(0)
220 #else
221 #define dprintk(a...)
222 #endif
223
224 #ifndef SLAB_ATOMIC
225 /* Changed in 2.6.20, must use GFP_ATOMIC now */
226 #define SLAB_ATOMIC     GFP_ATOMIC
227 #endif
228
229 /*
230  * need some additional support for older kernels */
231 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,2)
232 #define pci_register_driver_compat(driver, rc) \
233         do { \
234                 if ((rc) > 0) { \
235                         (rc) = 0; \
236                 } else if (rc == 0) { \
237                         (rc) = -ENODEV; \
238                 } else { \
239                         pci_unregister_driver(driver); \
240                 } \
241         } while (0)
242 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
243 #define pci_register_driver_compat(driver,rc) ((rc) = (rc) < 0 ? (rc) : 0)
244 #else
245 #define pci_register_driver_compat(driver,rc)
246 #endif
247
248 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
249
250 #include <asm/scatterlist.h>
251
252 static inline void sg_set_page(struct scatterlist *sg,  struct page *page,
253                                unsigned int len, unsigned int offset)
254 {
255         sg->page = page;
256         sg->offset = offset;
257         sg->length = len;
258 }
259
260 static inline void *sg_virt(struct scatterlist *sg)
261 {
262         return page_address(sg->page) + sg->offset;
263 }
264
265 #endif
266
267 #endif /* __KERNEL__ */
268
269 /****************************************************************************/
270 #endif /* _BSD_COMPAT_H_ */