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) 2012, 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 338951 2012-06-14 23:30:48Z $
25 #include <linux/version.h>
26 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
27 #include <linux/config.h>
29 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33))
30 #include <generated/autoconf.h>
32 #include <linux/autoconf.h>
34 #endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)) */
36 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 1, 0))
37 #include <linux/kconfig.h>
40 #include <linux/module.h>
42 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 0))
43 /* __NO_VERSION__ must be defined for all linkables except one in 2.2 */
44 #ifdef __UNDEF_NO_VERSION__
47 #define __NO_VERSION__
49 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 0) */
51 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
52 #define module_param(_name_, _type_, _perm_) MODULE_PARM(_name_, "i")
53 #define module_param_string(_name_, _string_, _size_, _perm_) \
54 MODULE_PARM(_string_, "c" __MODULE_STRING(_size_))
57 /* linux/malloc.h is deprecated, use linux/slab.h instead. */
58 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 9))
59 #include <linux/malloc.h>
61 #include <linux/slab.h>
64 #include <linux/types.h>
65 #include <linux/init.h>
67 #include <linux/string.h>
68 #include <linux/pci.h>
69 #include <linux/interrupt.h>
70 #include <linux/netdevice.h>
71 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
72 #include <linux/semaphore.h>
74 #include <asm/semaphore.h>
75 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) */
76 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
78 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)) */
81 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 41))
82 #include <linux/workqueue.h>
84 #include <linux/tqueue.h>
86 #define work_struct tq_struct
89 #define INIT_WORK(_work, _func, _data) INIT_TQUEUE((_work), (_func), (_data))
92 #define schedule_work(_work) schedule_task((_work))
94 #ifndef flush_scheduled_work
95 #define flush_scheduled_work() flush_scheduled_tasks()
97 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 41) */
99 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
100 #define MY_INIT_WORK(_work, _func) INIT_WORK(_work, _func)
102 #define MY_INIT_WORK(_work, _func) INIT_WORK(_work, _func, _work)
103 typedef void (*work_func_t
)(void *work
);
104 #endif /* >= 2.6.20 */
106 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
107 /* Some distributions have their own 2.6.x compatibility layers */
109 typedef void irqreturn_t
;
112 #define IRQ_RETVAL(x)
115 typedef irqreturn_t(*FN_ISR
) (int irq
, void *dev_id
, struct pt_regs
*ptregs
);
116 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) */
118 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
119 #define IRQF_SHARED SA_SHIRQ
120 #endif /* < 2.6.18 */
122 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
123 #ifdef CONFIG_NET_RADIO
124 #define CONFIG_WIRELESS_EXT
126 #endif /* < 2.6.17 */
128 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 67)
129 #define MOD_INC_USE_COUNT
130 #define MOD_DEC_USE_COUNT
131 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 67) */
133 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32)
134 #include <linux/sched.h>
137 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
138 #include <net/lib80211.h>
140 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
141 #include <linux/ieee80211.h>
143 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 14)
144 #include <net/ieee80211.h>
146 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30) */
148 #if defined(CONFIG_PCMCIA) || defined(CONFIG_PCMCIA_MODULE)
150 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
151 #include <pcmcia/version.h>
152 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27) */
154 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
155 #include <pcmcia/cs_types.h>
156 #include <pcmcia/cs.h>
158 #include <pcmcia/cistpl.h>
159 #include <pcmcia/cisreg.h>
160 #include <pcmcia/ds.h>
162 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 69))
163 /* In 2.5 (as of 2.5.69 at least) there is a cs_error exported which
164 * does this, but it's not in 2.4 so we do our own for now.
167 cs_error(client_handle_t handle
, int func
, int ret
)
169 error_info_t err
= { func
, ret
};
170 CardServices(ReportError
, handle
, &err
);
174 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 16))
176 typedef struct pcmcia_device dev_link_t
;
180 #endif /* CONFIG_PCMCIA */
189 #define __devinit __init
191 #ifndef __devinitdata
192 #define __devinitdata
195 #define __devexit_p(x) x
198 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0))
200 #define pci_get_drvdata(dev) (dev)->sysdata
201 #define pci_set_drvdata(dev, value) (dev)->sysdata = (value)
204 * New-style (2.4.x) PCI/hot-pluggable PCI/CardBus registration
207 struct pci_device_id
{
208 unsigned int vendor
, device
; /* Vendor and device ID or PCI_ANY_ID */
209 unsigned int subvendor
, subdevice
; /* Subsystem ID's or PCI_ANY_ID */
210 unsigned int class, class_mask
; /* (class,subclass,prog-if) triplet */
211 unsigned long driver_data
; /* Data private to the driver */
215 struct list_head node
;
217 const struct pci_device_id
*id_table
; /* NULL if wants all devices */
218 int (*probe
)(struct pci_dev
*dev
,
219 const struct pci_device_id
*id
); /* New device inserted */
220 void (*remove
)(struct pci_dev
*dev
); /* Device removed (NULL if not a hot-plug
223 void (*suspend
)(struct pci_dev
*dev
); /* Device suspended */
224 void (*resume
)(struct pci_dev
*dev
); /* Device woken up */
227 #define MODULE_DEVICE_TABLE(type, name)
228 #define PCI_ANY_ID (~0)
231 #define pci_module_init pci_register_driver
232 extern int pci_register_driver(struct pci_driver
*drv
);
233 extern void pci_unregister_driver(struct pci_driver
*drv
);
235 #endif /* PCI registration */
237 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18))
238 #define pci_module_init pci_register_driver
241 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 2, 18))
243 #define module_init(x) int init_module(void) { return x(); }
244 #define module_exit(x) void cleanup_module(void) { x(); }
246 #define module_init(x) __initcall(x);
247 #define module_exit(x) __exitcall(x);
249 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 2, 18) */
251 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)
252 #define WL_USE_NETDEV_OPS
254 #undef WL_USE_NETDEV_OPS
257 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31)) && defined(CONFIG_RFKILL)
258 #define WL_CONFIG_RFKILL
260 #undef WL_CONFIG_RFKILL
263 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 48))
264 #define list_for_each(pos, head) \
265 for (pos = (head)->next; pos != (head); pos = pos->next)
268 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 13))
269 #define pci_resource_start(dev, bar) ((dev)->base_address[(bar)])
270 #elif (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 44))
271 #define pci_resource_start(dev, bar) ((dev)->resource[(bar)].start)
274 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 23))
275 #define pci_enable_device(dev) do { } while (0)
278 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 14))
279 #define net_device device
282 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 42))
287 * See linux/Documentation/DMA-mapping.txt
290 #ifndef PCI_DMA_TODEVICE
291 #define PCI_DMA_TODEVICE 1
292 #define PCI_DMA_FROMDEVICE 2
295 typedef u32 dma_addr_t
;
297 /* Pure 2^n version of get_order */
298 static inline int get_order(unsigned long size
)
302 size
= (size
-1) >> (PAGE_SHIFT
-1);
311 static inline void *pci_alloc_consistent(struct pci_dev
*hwdev
, size_t size
,
312 dma_addr_t
*dma_handle
)
315 int gfp
= GFP_ATOMIC
| GFP_DMA
;
317 ret
= (void *)__get_free_pages(gfp
, get_order(size
));
320 memset(ret
, 0, size
);
321 *dma_handle
= virt_to_bus(ret
);
325 static inline void pci_free_consistent(struct pci_dev
*hwdev
, size_t size
,
326 void *vaddr
, dma_addr_t dma_handle
)
328 free_pages((unsigned long)vaddr
, get_order(size
));
331 extern uint
pci_map_single(void *dev
, void *va
, uint size
, int direction
);
332 extern void pci_unmap_single(void *dev
, uint pa
, uint size
, int direction
);
334 #define pci_map_single(cookie, address, size, dir) virt_to_bus(address)
335 #define pci_unmap_single(cookie, address, size, dir)
338 #endif /* DMA mapping */
340 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 43))
342 #define dev_kfree_skb_any(a) dev_kfree_skb(a)
343 #define netif_down(dev) do { (dev)->start = 0; } while (0)
345 /* pcmcia-cs provides its own netdevice compatibility layer */
346 #ifndef _COMPAT_NETDEVICE_H
351 * For pre-softnet kernels we need to tell the upper layer not to
352 * re-enter start_xmit() while we are in there. However softnet
353 * guarantees not to enter while we are in there so there is no need
354 * to do the netif_stop_queue() dance unless the transmit queue really
355 * gets stuck. This should also improve performance according to tests
356 * done by Aman Singla.
359 #define dev_kfree_skb_irq(a) dev_kfree_skb(a)
360 #define netif_wake_queue(dev) \
361 do { clear_bit(0, &(dev)->tbusy); mark_bh(NET_BH); } while (0)
362 #define netif_stop_queue(dev) set_bit(0, &(dev)->tbusy)
364 static inline void netif_start_queue(struct net_device
*dev
)
371 #define netif_queue_stopped(dev) (dev)->tbusy
372 #define netif_running(dev) (dev)->start
374 #endif /* _COMPAT_NETDEVICE_H */
376 #define netif_device_attach(dev) netif_start_queue(dev)
377 #define netif_device_detach(dev) netif_stop_queue(dev)
379 /* 2.4.x renamed bottom halves to tasklets */
380 #define tasklet_struct tq_struct
381 static inline void tasklet_schedule(struct tasklet_struct
*tasklet
)
383 queue_task(tasklet
, &tq_immediate
);
384 mark_bh(IMMEDIATE_BH
);
387 static inline void tasklet_init(struct tasklet_struct
*tasklet
,
388 void (*func
)(unsigned long),
391 tasklet
->next
= NULL
;
393 tasklet
->routine
= (void (*)(void *))func
;
394 tasklet
->data
= (void *)data
;
396 #define tasklet_kill(tasklet) { do {} while (0); }
398 /* 2.4.x introduced del_timer_sync() */
399 #define del_timer_sync(timer) del_timer(timer)
403 #define netif_down(dev)
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
531 void *parent
; /* some external entity that the thread supposed to work for */
532 struct task_struct
*p_task
;
534 int prio
; /* priority */
535 struct semaphore sema
;
537 struct completion completed
;
541 /* requires tsk_ctl_t tsk argument, the caller's priv data is passed in owner ptr */
542 /* note this macro assumes there may be only one context waiting on thread's completion */
544 #define DBG_THR(x) printk x
549 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
550 #define SMP_RD_BARRIER_DEPENDS(x) smp_read_barrier_depends(x)
552 #define SMP_RD_BARRIER_DEPENDS(x) smp_rmb(x)
556 #define PROC_START(thread_func, owner, tsk_ctl, flags) \
558 sema_init(&((tsk_ctl)->sema), 0); \
559 init_completion(&((tsk_ctl)->completed)); \
560 (tsk_ctl)->parent = owner; \
561 (tsk_ctl)->terminated = FALSE; \
562 (tsk_ctl)->thr_pid = kernel_thread(thread_func, tsk_ctl, flags); \
563 if ((tsk_ctl)->thr_pid > 0) \
564 wait_for_completion(&((tsk_ctl)->completed)); \
565 DBG_THR(("%s thr:%lx started\n", __FUNCTION__, (tsk_ctl)->thr_pid)); \
568 #define PROC_STOP(tsk_ctl) \
570 (tsk_ctl)->terminated = TRUE; \
572 up(&((tsk_ctl)->sema)); \
573 wait_for_completion(&((tsk_ctl)->completed)); \
574 DBG_THR(("%s thr:%lx terminated OK\n", __FUNCTION__, (tsk_ctl)->thr_pid)); \
575 (tsk_ctl)->thr_pid = -1; \
578 /* ----------------------- */
580 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31))
581 #define KILL_PROC(nr, sig) \
583 struct task_struct *tsk; \
585 pid = find_get_pid((pid_t)nr); \
586 tsk = pid_task(pid, PIDTYPE_PID); \
587 if (tsk) send_sig(sig, tsk, 1); \
590 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) && (LINUX_VERSION_CODE <= \
591 KERNEL_VERSION(2, 6, 30))
592 #define KILL_PROC(pid, sig) \
594 struct task_struct *tsk; \
595 tsk = find_task_by_vpid(pid); \
596 if (tsk) send_sig(sig, tsk, 1); \
599 #define KILL_PROC(pid, sig) \
601 kill_proc(pid, sig, 1); \
604 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31) */
606 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
607 #include <linux/time.h>
608 #include <linux/wait.h>
610 #include <linux/sched.h>
612 #define __wait_event_interruptible_timeout(wq, condition, ret) \
614 wait_queue_t __wait; \
615 init_waitqueue_entry(&__wait, current); \
617 add_wait_queue(&wq, &__wait); \
619 set_current_state(TASK_INTERRUPTIBLE); \
622 if (!signal_pending(current)) { \
623 ret = schedule_timeout(ret); \
628 ret = -ERESTARTSYS; \
631 current->state = TASK_RUNNING; \
632 remove_wait_queue(&wq, &__wait); \
635 #define wait_event_interruptible_timeout(wq, condition, timeout) \
637 long __ret = timeout; \
639 __wait_event_interruptible_timeout(wq, condition, __ret); \
643 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)) */
646 For < 2.6.24, wl creates its own netdev but doesn't
647 align the priv area like the genuine alloc_netdev().
648 Since netdev_priv() always gives us the aligned address, it will
649 not match our unaligned address for < 2.6.24
651 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24))
652 #define DEV_PRIV(dev) (dev->priv)
654 #define DEV_PRIV(dev) netdev_priv(dev)
657 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
658 #define WL_ISR(i, d, p) wl_isr((i), (d))
660 #define WL_ISR(i, d, p) wl_isr((i), (d), (p))
661 #endif /* < 2.6.20 */
663 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
664 #define netdev_priv(dev) dev->priv
665 #endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)) */
667 #endif /* _linuxver_h_ */