2 * Linux-specific abstractions to gain some independence from linux kernel versions.
3 * Pave over some 2.2 versus 2.4 versus 2.6 kernel differences.
5 * Copyright (C) 2010, Broadcom Corporation. All Rights Reserved.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
14 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
16 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
17 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * $Id: linuxver.h,v 13.53.12.4 2010-10-12 23:10:02 Exp $
25 #include <linux/version.h>
26 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
27 #include <linux/config.h>
30 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33))
31 #include <linux/autoconf.h>
33 #include <generated/autoconf.h>
37 #include <linux/module.h>
39 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 0))
40 /* __NO_VERSION__ must be defined for all linkables except one in 2.2 */
41 #ifdef __UNDEF_NO_VERSION__
44 #define __NO_VERSION__
46 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 0) */
48 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
49 #define module_param(_name_, _type_, _perm_) MODULE_PARM(_name_, "i")
50 #define module_param_string(_name_, _string_, _size_, _perm_) \
51 MODULE_PARM(_string_, "c" __MODULE_STRING(_size_))
54 /* linux/malloc.h is deprecated, use linux/slab.h instead. */
55 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 9))
56 #include <linux/malloc.h>
58 #include <linux/slab.h>
61 #include <linux/types.h>
62 #include <linux/init.h>
64 #include <linux/string.h>
65 #include <linux/pci.h>
66 #include <linux/interrupt.h>
67 #include <linux/netdevice.h>
68 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
73 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 41))
74 #include <linux/workqueue.h>
76 #include <linux/tqueue.h>
78 #define work_struct tq_struct
81 #define INIT_WORK(_work, _func, _data) INIT_TQUEUE((_work), (_func), (_data))
84 #define schedule_work(_work) schedule_task((_work))
86 #ifndef flush_scheduled_work
87 #define flush_scheduled_work() flush_scheduled_tasks()
89 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 41) */
91 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
92 #define MY_INIT_WORK(_work, _func) INIT_WORK(_work, _func)
94 #define MY_INIT_WORK(_work, _func) INIT_WORK(_work, _func, _work)
95 typedef void (*work_func_t
)(void *work
);
96 #endif /* >= 2.6.20 */
98 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
99 /* Some distributions have their own 2.6.x compatibility layers */
101 typedef void irqreturn_t
;
104 #define IRQ_RETVAL(x)
107 typedef irqreturn_t(*FN_ISR
) (int irq
, void *dev_id
, struct pt_regs
*ptregs
);
108 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) */
110 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
111 #define IRQF_SHARED SA_SHIRQ
112 #endif /* < 2.6.18 */
114 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
115 #ifdef CONFIG_NET_RADIO
116 #define CONFIG_WIRELESS_EXT
118 #endif /* < 2.6.17 */
120 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 67)
122 #define MOD_INC_USE_COUNT
123 #define MOD_DEC_USE_COUNT
124 #endif /* not SANDGATE2G */
125 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 67) */
127 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32)
128 #include <linux/sched.h>
131 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
132 #include <net/lib80211.h>
134 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
135 #include <linux/ieee80211.h>
137 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 14)
138 #include <net/ieee80211.h>
140 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30) */
142 #if defined(CONFIG_PCMCIA) || defined(CONFIG_PCMCIA_MODULE)
144 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
145 #include <pcmcia/version.h>
146 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27) */
148 #include <pcmcia/cs_types.h>
149 #include <pcmcia/cs.h>
150 #include <pcmcia/cistpl.h>
151 #include <pcmcia/cisreg.h>
152 #include <pcmcia/ds.h>
154 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 69))
155 /* In 2.5 (as of 2.5.69 at least) there is a cs_error exported which
156 * does this, but it's not in 2.4 so we do our own for now.
159 cs_error(client_handle_t handle
, int func
, int ret
)
161 error_info_t err
= { func
, ret
};
162 CardServices(ReportError
, handle
, &err
);
166 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 16))
168 typedef struct pcmcia_device dev_link_t
;
172 #endif /* CONFIG_PCMCIA */
181 #define __devinit __init
183 #ifndef __devinitdata
184 #define __devinitdata
187 #define __devexit_p(x) x
190 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0))
192 #define pci_get_drvdata(dev) (dev)->sysdata
193 #define pci_set_drvdata(dev, value) (dev)->sysdata = (value)
196 * New-style (2.4.x) PCI/hot-pluggable PCI/CardBus registration
199 struct pci_device_id
{
200 unsigned int vendor
, device
; /* Vendor and device ID or PCI_ANY_ID */
201 unsigned int subvendor
, subdevice
; /* Subsystem ID's or PCI_ANY_ID */
202 unsigned int class, class_mask
; /* (class,subclass,prog-if) triplet */
203 unsigned long driver_data
; /* Data private to the driver */
207 struct list_head node
;
209 const struct pci_device_id
*id_table
; /* NULL if wants all devices */
210 int (*probe
)(struct pci_dev
*dev
,
211 const struct pci_device_id
*id
); /* New device inserted */
212 void (*remove
)(struct pci_dev
*dev
); /* Device removed (NULL if not a hot-plug
215 void (*suspend
)(struct pci_dev
*dev
); /* Device suspended */
216 void (*resume
)(struct pci_dev
*dev
); /* Device woken up */
219 #define MODULE_DEVICE_TABLE(type, name)
220 #define PCI_ANY_ID (~0)
223 #define pci_module_init pci_register_driver
224 extern int pci_register_driver(struct pci_driver
*drv
);
225 extern void pci_unregister_driver(struct pci_driver
*drv
);
227 #endif /* PCI registration */
229 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18))
230 #define pci_module_init pci_register_driver
233 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 2, 18))
235 #define module_init(x) int init_module(void) { return x(); }
236 #define module_exit(x) void cleanup_module(void) { x(); }
238 #define module_init(x) __initcall(x);
239 #define module_exit(x) __exitcall(x);
241 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 2, 18) */
243 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
244 #define WL_USE_NETDEV_OPS
246 #undef WL_USE_NETDEV_OPS
249 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)) && defined(CONFIG_RFKILL)
250 #define WL_CONFIG_RFKILL
252 #undef WL_CONFIG_RFKILL
255 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 48))
256 #define list_for_each(pos, head) \
257 for (pos = (head)->next; pos != (head); pos = pos->next)
260 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 13))
261 #define pci_resource_start(dev, bar) ((dev)->base_address[(bar)])
262 #elif (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 44))
263 #define pci_resource_start(dev, bar) ((dev)->resource[(bar)].start)
266 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 23))
267 #define pci_enable_device(dev) do { } while (0)
270 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 14))
271 #define net_device device
274 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 42))
279 * See linux/Documentation/DMA-mapping.txt
282 #ifndef PCI_DMA_TODEVICE
283 #define PCI_DMA_TODEVICE 1
284 #define PCI_DMA_FROMDEVICE 2
287 typedef u32 dma_addr_t
;
289 /* Pure 2^n version of get_order */
290 static inline int get_order(unsigned long size
)
294 size
= (size
-1) >> (PAGE_SHIFT
-1);
303 static inline void *pci_alloc_consistent(struct pci_dev
*hwdev
, size_t size
,
304 dma_addr_t
*dma_handle
)
307 int gfp
= GFP_ATOMIC
| GFP_DMA
;
309 ret
= (void *)__get_free_pages(gfp
, get_order(size
));
312 memset(ret
, 0, size
);
313 *dma_handle
= virt_to_bus(ret
);
317 static inline void pci_free_consistent(struct pci_dev
*hwdev
, size_t size
,
318 void *vaddr
, dma_addr_t dma_handle
)
320 free_pages((unsigned long)vaddr
, get_order(size
));
323 extern uint
pci_map_single(void *dev
, void *va
, uint size
, int direction
);
324 extern void pci_unmap_single(void *dev
, uint pa
, uint size
, int direction
);
326 #define pci_map_single(cookie, address, size, dir) virt_to_bus(address)
327 #define pci_unmap_single(cookie, address, size, dir)
330 #endif /* DMA mapping */
332 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 43))
334 #define dev_kfree_skb_any(a) dev_kfree_skb(a)
335 #define netif_down(dev) do { (dev)->start = 0; } while (0)
337 /* pcmcia-cs provides its own netdevice compatibility layer */
338 #ifndef _COMPAT_NETDEVICE_H
343 * For pre-softnet kernels we need to tell the upper layer not to
344 * re-enter start_xmit() while we are in there. However softnet
345 * guarantees not to enter while we are in there so there is no need
346 * to do the netif_stop_queue() dance unless the transmit queue really
347 * gets stuck. This should also improve performance according to tests
348 * done by Aman Singla.
351 #define dev_kfree_skb_irq(a) dev_kfree_skb(a)
352 #define netif_wake_queue(dev) \
353 do { clear_bit(0, &(dev)->tbusy); mark_bh(NET_BH); } while (0)
354 #define netif_stop_queue(dev) set_bit(0, &(dev)->tbusy)
356 static inline void netif_start_queue(struct net_device
*dev
)
363 #define netif_queue_stopped(dev) (dev)->tbusy
364 #define netif_running(dev) (dev)->start
366 #endif /* _COMPAT_NETDEVICE_H */
368 #define netif_device_attach(dev) netif_start_queue(dev)
369 #define netif_device_detach(dev) netif_stop_queue(dev)
371 /* 2.4.x renamed bottom halves to tasklets */
372 #define tasklet_struct tq_struct
373 static inline void tasklet_schedule(struct tasklet_struct
*tasklet
)
375 queue_task(tasklet
, &tq_immediate
);
376 mark_bh(IMMEDIATE_BH
);
379 static inline void tasklet_init(struct tasklet_struct
*tasklet
,
380 void (*func
)(unsigned long),
383 tasklet
->next
= NULL
;
385 tasklet
->routine
= (void (*)(void *))func
;
386 tasklet
->data
= (void *)data
;
388 #define tasklet_kill(tasklet) { do {} while (0); }
390 /* 2.4.x introduced del_timer_sync() */
391 #define del_timer_sync(timer) del_timer(timer)
395 #define netif_down(dev)
399 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22))
400 #define skb_reset_mac_header(skb) (skb)->mac.raw = (skb)->data
401 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 3) */
403 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 9))
404 #define eth_hdr(skb) ((struct ethhdr *)((skb)->mac.raw))
405 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 9) */
407 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 3))
410 * Emit code to initialise a tq_struct's routine and data pointers
412 #define PREPARE_TQUEUE(_tq, _routine, _data) \
414 (_tq)->routine = _routine; \
415 (_tq)->data = _data; \
419 * Emit code to initialise all of a tq_struct
421 #define INIT_TQUEUE(_tq, _routine, _data) \
423 INIT_LIST_HEAD(&(_tq)->list); \
425 PREPARE_TQUEUE((_tq), (_routine), (_data)); \
428 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 3) */
430 /* Power management related macro & routines */
431 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 9)
432 #define PCI_SAVE_STATE(a, b) pci_save_state(a)
433 #define PCI_RESTORE_STATE(a, b) pci_restore_state(a)
435 #define PCI_SAVE_STATE(a, b) pci_save_state(a, b)
436 #define PCI_RESTORE_STATE(a, b) pci_restore_state(a, b)
439 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 6))
441 pci_save_state(struct pci_dev
*dev
, u32
*buffer
)
445 for (i
= 0; i
< 16; i
++)
446 pci_read_config_dword(dev
, i
* 4, &buffer
[i
]);
452 pci_restore_state(struct pci_dev
*dev
, u32
*buffer
)
457 for (i
= 0; i
< 16; i
++)
458 pci_write_config_dword(dev
, i
* 4, buffer
[i
]);
461 * otherwise, write the context information we know from bootup.
462 * This works around a problem where warm-booting from Windows
463 * combined with a D3(hot)->D0 transition causes PCI config
464 * header data to be forgotten.
467 for (i
= 0; i
< 6; i
++)
468 pci_write_config_dword(dev
,
469 PCI_BASE_ADDRESS_0
+ (i
* 4),
470 pci_resource_start(dev
, i
));
471 pci_write_config_byte(dev
, PCI_INTERRUPT_LINE
, dev
->irq
);
475 #endif /* PCI power management */
477 /* Old cp0 access macros deprecated in 2.4.19 */
478 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 19))
479 #define read_c0_count() read_32bit_cp0_register(CP0_COUNT)
482 /* Module refcount handled internally in 2.6.x */
483 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24))
484 #ifndef SET_MODULE_OWNER
485 #define SET_MODULE_OWNER(dev) do {} while (0)
486 #define OLD_MOD_INC_USE_COUNT MOD_INC_USE_COUNT
487 #define OLD_MOD_DEC_USE_COUNT MOD_DEC_USE_COUNT
489 #define OLD_MOD_INC_USE_COUNT do {} while (0)
490 #define OLD_MOD_DEC_USE_COUNT do {} while (0)
492 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24) */
493 #ifndef SET_MODULE_OWNER
494 #define SET_MODULE_OWNER(dev) do {} while (0)
496 #ifndef MOD_INC_USE_COUNT
497 #define MOD_INC_USE_COUNT do {} while (0)
499 #ifndef MOD_DEC_USE_COUNT
500 #define MOD_DEC_USE_COUNT do {} while (0)
502 #define OLD_MOD_INC_USE_COUNT MOD_INC_USE_COUNT
503 #define OLD_MOD_DEC_USE_COUNT MOD_DEC_USE_COUNT
504 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24) */
506 #ifndef SET_NETDEV_DEV
507 #define SET_NETDEV_DEV(net, pdev) do {} while (0)
510 #ifndef HAVE_FREE_NETDEV
511 #define free_netdev(dev) kfree(dev)
514 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
515 /* struct packet_type redefined in 2.6.x */
516 #define af_packet_priv data
520 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
521 #define DRV_SUSPEND_STATE_TYPE pm_message_t
523 #define DRV_SUSPEND_STATE_TYPE uint32
526 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
527 #define CHECKSUM_HW CHECKSUM_PARTIAL
530 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
531 #include <linux/time.h>
532 #include <linux/wait.h>
534 #include <linux/sched.h>
536 #define __wait_event_interruptible_timeout(wq, condition, ret) \
538 wait_queue_t __wait; \
539 init_waitqueue_entry(&__wait, current); \
541 add_wait_queue(&wq, &__wait); \
543 set_current_state(TASK_INTERRUPTIBLE); \
546 if (!signal_pending(current)) { \
547 ret = schedule_timeout(ret); \
552 ret = -ERESTARTSYS; \
555 current->state = TASK_RUNNING; \
556 remove_wait_queue(&wq, &__wait); \
559 #define wait_event_interruptible_timeout(wq, condition, timeout) \
561 long __ret = timeout; \
563 __wait_event_interruptible_timeout(wq, condition, __ret); \
567 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)) */
569 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
570 #define KILL_PROC(pid, sig) \
572 struct task_struct *tsk; \
573 tsk = find_task_by_vpid(pid); \
574 if (tsk) send_sig(sig, tsk, 1); \
577 #define KILL_PROC(pid, sig) \
579 kill_proc(pid, sig, 1); \
583 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
584 #define WL_DEV_IF(dev) ((wl_if_t*)netdev_priv(dev))
586 #define WL_DEV_IF(dev) ((wl_if_t*)(dev)->priv)
589 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
590 #define WL_ISR(i, d, p) wl_isr((i), (d))
592 #define WL_ISR(i, d, p) wl_isr((i), (d), (p))
593 #endif /* < 2.6.20 */
595 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
596 #define NETDEV_PRIV(dev) (netdev_priv(dev))
598 #define NETDEV_PRIV(dev) ((dev)->priv)
601 #endif /* _linuxver_h_ */