2974b6c8784a2ad3733e0fdf8715f50fd21aa942
[openwrt/svn-archive/archive.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,11)
127 #define files_fdtable(files) (files)
128 #endif
129
130 #ifdef MODULE_PARM
131 #undef module_param /* just in case */
132 #define module_param(a,b,c) MODULE_PARM(a,"i")
133 #endif
134
135 #define bzero(s,l) memset(s,0,l)
136 #define bcopy(s,d,l) memcpy(d,s,l)
137 #define bcmp(x, y, l) memcmp(x,y,l)
138
139 #define MIN(x,y) ((x) < (y) ? (x) : (y))
140
141 #define device_printf(dev, a...) ({ \
142 printk("%s: ", device_get_nameunit(dev)); printk(a); \
143 })
144
145 #undef printf
146 #define printf(fmt...) printk(fmt)
147
148 #define KASSERT(c,p) if (!(c)) { printk p ; } else
149
150 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
151 #define ocf_daemonize(str) \
152 daemonize(); \
153 spin_lock_irq(&current->sigmask_lock); \
154 sigemptyset(&current->blocked); \
155 recalc_sigpending(current); \
156 spin_unlock_irq(&current->sigmask_lock); \
157 sprintf(current->comm, str);
158 #else
159 #define ocf_daemonize(str) daemonize(str);
160 #endif
161
162 #define TAILQ_INSERT_TAIL(q,d,m) list_add_tail(&(d)->m, (q))
163 #define TAILQ_EMPTY(q) list_empty(q)
164 #define TAILQ_FOREACH(v, q, m) list_for_each_entry(v, q, m)
165
166 #define read_random(p,l) get_random_bytes(p,l)
167
168 #define DELAY(x) ((x) > 2000 ? mdelay((x)/1000) : udelay(x))
169 #define strtoul simple_strtoul
170
171 #define pci_get_vendor(dev) ((dev)->vendor)
172 #define pci_get_device(dev) ((dev)->device)
173
174 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
175 #define pci_set_consistent_dma_mask(dev, mask) (0)
176 #endif
177 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
178 #define pci_dma_sync_single_for_cpu pci_dma_sync_single
179 #endif
180
181 #ifndef DMA_32BIT_MASK
182 #define DMA_32BIT_MASK 0x00000000ffffffffULL
183 #endif
184
185 #define htole32(x) cpu_to_le32(x)
186 #define htobe32(x) cpu_to_be32(x)
187 #define htole16(x) cpu_to_le16(x)
188 #define htobe16(x) cpu_to_be16(x)
189
190 /* older kernels don't have these */
191
192 #ifndef IRQ_NONE
193 #define IRQ_NONE
194 #define IRQ_HANDLED
195 #define irqreturn_t void
196 #endif
197 #ifndef IRQF_SHARED
198 #define IRQF_SHARED SA_SHIRQ
199 #endif
200
201 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
202 # define strlcpy(dest,src,len) \
203 ({strncpy(dest,src,(len)-1); ((char *)dest)[(len)-1] = '\0'; })
204 #endif
205
206 #ifndef MAX_ERRNO
207 #define MAX_ERRNO 4095
208 #endif
209 #ifndef IS_ERR_VALUE
210 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
211 #endif
212
213 /*
214 * common debug for all
215 */
216 #if 1
217 #define dprintk(a...) do { if (debug) printk(a); } while(0)
218 #else
219 #define dprintk(a...)
220 #endif
221
222 #ifndef SLAB_ATOMIC
223 /* Changed in 2.6.20, must use GFP_ATOMIC now */
224 #define SLAB_ATOMIC GFP_ATOMIC
225 #endif
226
227 /*
228 * need some additional support for older kernels */
229 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,2)
230 #define pci_register_driver_compat(driver, rc) \
231 do { \
232 if ((rc) > 0) { \
233 (rc) = 0; \
234 } else if (rc == 0) { \
235 (rc) = -ENODEV; \
236 } else { \
237 pci_unregister_driver(driver); \
238 } \
239 } while (0)
240 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
241 #define pci_register_driver_compat(driver,rc) ((rc) = (rc) < 0 ? (rc) : 0)
242 #else
243 #define pci_register_driver_compat(driver,rc)
244 #endif
245
246 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
247
248 #include <asm/scatterlist.h>
249
250 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
251 unsigned int len, unsigned int offset)
252 {
253 sg->page = page;
254 sg->offset = offset;
255 sg->length = len;
256 }
257
258 static inline void *sg_virt(struct scatterlist *sg)
259 {
260 return page_address(sg->page) + sg->offset;
261 }
262
263 #endif
264
265 #endif /* __KERNEL__ */
266
267 /****************************************************************************/
268 #endif /* _BSD_COMPAT_H_ */