| 1 | /* |
| 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. |
| 4 | * |
| 5 | * Copyright 2007, Broadcom Corporation |
| 6 | * All Rights Reserved. |
| 7 | * |
| 8 | * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY |
| 9 | * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM |
| 10 | * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS |
| 11 | * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #ifndef _linuxver_h_ |
| 16 | #define _linuxver_h_ |
| 17 | |
| 18 | #include <linux/version.h> |
| 19 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)) |
| 20 | #include <linux/config.h> |
| 21 | #else |
| 22 | #include <linux/autoconf.h> |
| 23 | #endif |
| 24 | #include <linux/module.h> |
| 25 | |
| 26 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 0)) |
| 27 | /* __NO_VERSION__ must be defined for all linkables except one in 2.2 */ |
| 28 | #ifdef __UNDEF_NO_VERSION__ |
| 29 | #undef __NO_VERSION__ |
| 30 | #else |
| 31 | #define __NO_VERSION__ |
| 32 | #endif |
| 33 | #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 0) */ |
| 34 | |
| 35 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) |
| 36 | #define module_param(_name_, _type_, _perm_) MODULE_PARM(_name_, "i") |
| 37 | #define module_param_string(_name_, _string_, _size_, _perm_) \ |
| 38 | MODULE_PARM(_string_, "c" __MODULE_STRING(_size_)) |
| 39 | #endif |
| 40 | |
| 41 | /* linux/malloc.h is deprecated, use linux/slab.h instead. */ |
| 42 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 9)) |
| 43 | #include <linux/malloc.h> |
| 44 | #else |
| 45 | #include <linux/slab.h> |
| 46 | #endif |
| 47 | |
| 48 | #include <linux/types.h> |
| 49 | #include <linux/init.h> |
| 50 | #include <linux/mm.h> |
| 51 | #include <linux/string.h> |
| 52 | #include <linux/pci.h> |
| 53 | #include <linux/interrupt.h> |
| 54 | #include <linux/netdevice.h> |
| 55 | #include <asm/io.h> |
| 56 | |
| 57 | #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 41)) |
| 58 | #include <linux/workqueue.h> |
| 59 | #else |
| 60 | #include <linux/tqueue.h> |
| 61 | #ifndef work_struct |
| 62 | #define work_struct tq_struct |
| 63 | #endif |
| 64 | #ifndef INIT_WORK |
| 65 | #define INIT_WORK(_work, _func, _data) INIT_TQUEUE((_work), (_func), (_data)) |
| 66 | #endif |
| 67 | #ifndef schedule_work |
| 68 | #define schedule_work(_work) schedule_task((_work)) |
| 69 | #endif |
| 70 | #ifndef flush_scheduled_work |
| 71 | #define flush_scheduled_work() flush_scheduled_tasks() |
| 72 | #endif |
| 73 | #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 41) */ |
| 74 | |
| 75 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20) |
| 76 | #define MY_INIT_WORK(_work, _func, _data) INIT_WORK(_work, _func) |
| 77 | #else |
| 78 | #define MY_INIT_WORK(_work, _func, _data) INIT_WORK(_work, _func, _data) |
| 79 | typedef void (*work_func_t)(void *work); |
| 80 | #endif /* < 2.6.20 */ |
| 81 | |
| 82 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)) |
| 83 | /* Some distributions have their own 2.6.x compatibility layers */ |
| 84 | #ifndef IRQ_NONE |
| 85 | typedef void irqreturn_t; |
| 86 | #define IRQ_NONE |
| 87 | #define IRQ_HANDLED |
| 88 | #define IRQ_RETVAL(x) |
| 89 | #endif |
| 90 | #else |
| 91 | typedef irqreturn_t(*FN_ISR) (int irq, void *dev_id, struct pt_regs *ptregs); |
| 92 | #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0) */ |
| 93 | |
| 94 | #if defined(CONFIG_PCMCIA) || defined(CONFIG_PCMCIA_MODULE) |
| 95 | |
| 96 | #include <pcmcia/version.h> |
| 97 | #include <pcmcia/cs_types.h> |
| 98 | #include <pcmcia/cs.h> |
| 99 | #include <pcmcia/cistpl.h> |
| 100 | #include <pcmcia/cisreg.h> |
| 101 | #include <pcmcia/ds.h> |
| 102 | |
| 103 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 69)) |
| 104 | /* In 2.5 (as of 2.5.69 at least) there is a cs_error exported which |
| 105 | * does this, but it's not in 2.4 so we do our own for now. |
| 106 | */ |
| 107 | static inline void |
| 108 | cs_error(client_handle_t handle, int func, int ret) |
| 109 | { |
| 110 | error_info_t err = { func, ret }; |
| 111 | CardServices(ReportError, handle, &err); |
| 112 | } |
| 113 | #endif |
| 114 | |
| 115 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 15)) |
| 116 | |
| 117 | typedef struct pcmcia_device dev_link_t; |
| 118 | |
| 119 | #endif |
| 120 | |
| 121 | #endif /* CONFIG_PCMCIA */ |
| 122 | |
| 123 | #ifndef __exit |
| 124 | #define __exit |
| 125 | #endif |
| 126 | #ifndef __devexit |
| 127 | #define __devexit |
| 128 | #endif |
| 129 | #ifndef __devinit |
| 130 | #define __devinit __init |
| 131 | #endif |
| 132 | #ifndef __devinitdata |
| 133 | #define __devinitdata |
| 134 | #endif |
| 135 | #ifndef __devexit_p |
| 136 | #define __devexit_p(x) x |
| 137 | #endif |
| 138 | |
| 139 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0)) |
| 140 | |
| 141 | #define pci_get_drvdata(dev) (dev)->sysdata |
| 142 | #define pci_set_drvdata(dev, value) (dev)->sysdata = (value) |
| 143 | |
| 144 | /* |
| 145 | * New-style (2.4.x) PCI/hot-pluggable PCI/CardBus registration |
| 146 | */ |
| 147 | |
| 148 | struct pci_device_id { |
| 149 | unsigned int vendor, device; /* Vendor and device ID or PCI_ANY_ID */ |
| 150 | unsigned int subvendor, subdevice; /* Subsystem ID's or PCI_ANY_ID */ |
| 151 | unsigned int class, class_mask; /* (class,subclass,prog-if) triplet */ |
| 152 | unsigned long driver_data; /* Data private to the driver */ |
| 153 | }; |
| 154 | |
| 155 | struct pci_driver { |
| 156 | struct list_head node; |
| 157 | char *name; |
| 158 | const struct pci_device_id *id_table; /* NULL if wants all devices */ |
| 159 | int (*probe)(struct pci_dev *dev, |
| 160 | const struct pci_device_id *id); /* New device inserted */ |
| 161 | void (*remove)(struct pci_dev *dev); /* Device removed (NULL if not a hot-plug |
| 162 | * capable driver) |
| 163 | */ |
| 164 | void (*suspend)(struct pci_dev *dev); /* Device suspended */ |
| 165 | void (*resume)(struct pci_dev *dev); /* Device woken up */ |
| 166 | }; |
| 167 | |
| 168 | #define MODULE_DEVICE_TABLE(type, name) |
| 169 | #define PCI_ANY_ID (~0) |
| 170 | |
| 171 | /* compatpci.c */ |
| 172 | #define pci_module_init pci_register_driver |
| 173 | extern int pci_register_driver(struct pci_driver *drv); |
| 174 | extern void pci_unregister_driver(struct pci_driver *drv); |
| 175 | |
| 176 | #endif /* PCI registration */ |
| 177 | |
| 178 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 2, 18)) |
| 179 | #ifdef MODULE |
| 180 | #define module_init(x) int init_module(void) { return x(); } |
| 181 | #define module_exit(x) void cleanup_module(void) { x(); } |
| 182 | #else |
| 183 | #define module_init(x) __initcall(x); |
| 184 | #define module_exit(x) __exitcall(x); |
| 185 | #endif |
| 186 | #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 2, 18) */ |
| 187 | |
| 188 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 48)) |
| 189 | #define list_for_each(pos, head) \ |
| 190 | for (pos = (head)->next; pos != (head); pos = pos->next) |
| 191 | #endif |
| 192 | |
| 193 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 13)) |
| 194 | #define pci_resource_start(dev, bar) ((dev)->base_address[(bar)]) |
| 195 | #elif (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 44)) |
| 196 | #define pci_resource_start(dev, bar) ((dev)->resource[(bar)].start) |
| 197 | #endif |
| 198 | |
| 199 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 23)) |
| 200 | #define pci_enable_device(dev) do { } while (0) |
| 201 | #endif |
| 202 | |
| 203 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 14)) |
| 204 | #define net_device device |
| 205 | #endif |
| 206 | |
| 207 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 42)) |
| 208 | |
| 209 | /* |
| 210 | * DMA mapping |
| 211 | * |
| 212 | * See linux/Documentation/DMA-mapping.txt |
| 213 | */ |
| 214 | |
| 215 | #ifndef PCI_DMA_TODEVICE |
| 216 | #define PCI_DMA_TODEVICE 1 |
| 217 | #define PCI_DMA_FROMDEVICE 2 |
| 218 | #endif |
| 219 | |
| 220 | typedef u32 dma_addr_t; |
| 221 | |
| 222 | /* Pure 2^n version of get_order */ |
| 223 | static inline int get_order(unsigned long size) |
| 224 | { |
| 225 | int order; |
| 226 | |
| 227 | size = (size-1) >> (PAGE_SHIFT-1); |
| 228 | order = -1; |
| 229 | do { |
| 230 | size >>= 1; |
| 231 | order++; |
| 232 | } while (size); |
| 233 | return order; |
| 234 | } |
| 235 | |
| 236 | static inline void *pci_alloc_consistent(struct pci_dev *hwdev, size_t size, |
| 237 | dma_addr_t *dma_handle) |
| 238 | { |
| 239 | void *ret; |
| 240 | int gfp = GFP_ATOMIC | GFP_DMA; |
| 241 | |
| 242 | ret = (void *)__get_free_pages(gfp, get_order(size)); |
| 243 | |
| 244 | if (ret != NULL) { |
| 245 | memset(ret, 0, size); |
| 246 | *dma_handle = virt_to_bus(ret); |
| 247 | } |
| 248 | return ret; |
| 249 | } |
| 250 | static inline void pci_free_consistent(struct pci_dev *hwdev, size_t size, |
| 251 | void *vaddr, dma_addr_t dma_handle) |
| 252 | { |
| 253 | free_pages((unsigned long)vaddr, get_order(size)); |
| 254 | } |
| 255 | #ifdef ILSIM |
| 256 | extern uint pci_map_single(void *dev, void *va, uint size, int direction); |
| 257 | extern void pci_unmap_single(void *dev, uint pa, uint size, int direction); |
| 258 | #else |
| 259 | #define pci_map_single(cookie, address, size, dir) virt_to_bus(address) |
| 260 | #define pci_unmap_single(cookie, address, size, dir) |
| 261 | #endif |
| 262 | |
| 263 | #endif /* DMA mapping */ |
| 264 | |
| 265 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 43)) |
| 266 | |
| 267 | #define dev_kfree_skb_any(a) dev_kfree_skb(a) |
| 268 | #define netif_down(dev) do { (dev)->start = 0; } while (0) |
| 269 | |
| 270 | /* pcmcia-cs provides its own netdevice compatibility layer */ |
| 271 | #ifndef _COMPAT_NETDEVICE_H |
| 272 | |
| 273 | /* |
| 274 | * SoftNet |
| 275 | * |
| 276 | * For pre-softnet kernels we need to tell the upper layer not to |
| 277 | * re-enter start_xmit() while we are in there. However softnet |
| 278 | * guarantees not to enter while we are in there so there is no need |
| 279 | * to do the netif_stop_queue() dance unless the transmit queue really |
| 280 | * gets stuck. This should also improve performance according to tests |
| 281 | * done by Aman Singla. |
| 282 | */ |
| 283 | |
| 284 | #define dev_kfree_skb_irq(a) dev_kfree_skb(a) |
| 285 | #define netif_wake_queue(dev) \ |
| 286 | do { clear_bit(0, &(dev)->tbusy); mark_bh(NET_BH); } while (0) |
| 287 | #define netif_stop_queue(dev) set_bit(0, &(dev)->tbusy) |
| 288 | |
| 289 | static inline void netif_start_queue(struct net_device *dev) |
| 290 | { |
| 291 | dev->tbusy = 0; |
| 292 | dev->interrupt = 0; |
| 293 | dev->start = 1; |
| 294 | } |
| 295 | |
| 296 | #define netif_queue_stopped(dev) (dev)->tbusy |
| 297 | #define netif_running(dev) (dev)->start |
| 298 | |
| 299 | #endif /* _COMPAT_NETDEVICE_H */ |
| 300 | |
| 301 | #define netif_device_attach(dev) netif_start_queue(dev) |
| 302 | #define netif_device_detach(dev) netif_stop_queue(dev) |
| 303 | |
| 304 | /* 2.4.x renamed bottom halves to tasklets */ |
| 305 | #define tasklet_struct tq_struct |
| 306 | static inline void tasklet_schedule(struct tasklet_struct *tasklet) |
| 307 | { |
| 308 | queue_task(tasklet, &tq_immediate); |
| 309 | mark_bh(IMMEDIATE_BH); |
| 310 | } |
| 311 | |
| 312 | static inline void tasklet_init(struct tasklet_struct *tasklet, |
| 313 | void (*func)(unsigned long), |
| 314 | unsigned long data) |
| 315 | { |
| 316 | tasklet->next = NULL; |
| 317 | tasklet->sync = 0; |
| 318 | tasklet->routine = (void (*)(void *))func; |
| 319 | tasklet->data = (void *)data; |
| 320 | } |
| 321 | #define tasklet_kill(tasklet) { do{} while (0); } |
| 322 | |
| 323 | /* 2.4.x introduced del_timer_sync() */ |
| 324 | #define del_timer_sync(timer) del_timer(timer) |
| 325 | |
| 326 | #else |
| 327 | |
| 328 | #define netif_down(dev) |
| 329 | |
| 330 | #endif /* SoftNet */ |
| 331 | |
| 332 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 3)) |
| 333 | |
| 334 | /* |
| 335 | * Emit code to initialise a tq_struct's routine and data pointers |
| 336 | */ |
| 337 | #define PREPARE_TQUEUE(_tq, _routine, _data) \ |
| 338 | do { \ |
| 339 | (_tq)->routine = _routine; \ |
| 340 | (_tq)->data = _data; \ |
| 341 | } while (0) |
| 342 | |
| 343 | /* |
| 344 | * Emit code to initialise all of a tq_struct |
| 345 | */ |
| 346 | #define INIT_TQUEUE(_tq, _routine, _data) \ |
| 347 | do { \ |
| 348 | INIT_LIST_HEAD(&(_tq)->list); \ |
| 349 | (_tq)->sync = 0; \ |
| 350 | PREPARE_TQUEUE((_tq), (_routine), (_data)); \ |
| 351 | } while (0) |
| 352 | |
| 353 | #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 3) */ |
| 354 | |
| 355 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 6)) |
| 356 | |
| 357 | /* Power management related routines */ |
| 358 | |
| 359 | static inline int |
| 360 | pci_save_state(struct pci_dev *dev, u32 *buffer) |
| 361 | { |
| 362 | int i; |
| 363 | if (buffer) { |
| 364 | for (i = 0; i < 16; i++) |
| 365 | pci_read_config_dword(dev, i * 4, &buffer[i]); |
| 366 | } |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static inline int |
| 371 | pci_restore_state(struct pci_dev *dev, u32 *buffer) |
| 372 | { |
| 373 | int i; |
| 374 | |
| 375 | if (buffer) { |
| 376 | for (i = 0; i < 16; i++) |
| 377 | pci_write_config_dword(dev, i * 4, buffer[i]); |
| 378 | } |
| 379 | /* |
| 380 | * otherwise, write the context information we know from bootup. |
| 381 | * This works around a problem where warm-booting from Windows |
| 382 | * combined with a D3(hot)->D0 transition causes PCI config |
| 383 | * header data to be forgotten. |
| 384 | */ |
| 385 | else { |
| 386 | for (i = 0; i < 6; i ++) |
| 387 | pci_write_config_dword(dev, |
| 388 | PCI_BASE_ADDRESS_0 + (i * 4), |
| 389 | pci_resource_start(dev, i)); |
| 390 | pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq); |
| 391 | } |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | #endif /* PCI power management */ |
| 396 | |
| 397 | /* Old cp0 access macros deprecated in 2.4.19 */ |
| 398 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 19)) |
| 399 | #define read_c0_count() read_32bit_cp0_register(CP0_COUNT) |
| 400 | #endif |
| 401 | |
| 402 | /* Module refcount handled internally in 2.6.x */ |
| 403 | #ifndef SET_MODULE_OWNER |
| 404 | #define SET_MODULE_OWNER(dev) do {} while (0) |
| 405 | #define OLD_MOD_INC_USE_COUNT MOD_INC_USE_COUNT |
| 406 | #define OLD_MOD_DEC_USE_COUNT MOD_DEC_USE_COUNT |
| 407 | #else |
| 408 | #define OLD_MOD_INC_USE_COUNT do {} while (0) |
| 409 | #define OLD_MOD_DEC_USE_COUNT do {} while (0) |
| 410 | #endif |
| 411 | |
| 412 | #ifndef SET_NETDEV_DEV |
| 413 | #define SET_NETDEV_DEV(net, pdev) do {} while (0) |
| 414 | #endif |
| 415 | |
| 416 | #ifndef HAVE_FREE_NETDEV |
| 417 | #define free_netdev(dev) kfree(dev) |
| 418 | #endif |
| 419 | |
| 420 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)) |
| 421 | /* struct packet_type redefined in 2.6.x */ |
| 422 | #define af_packet_priv data |
| 423 | #endif |
| 424 | |
| 425 | /* suspend args */ |
| 426 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11) |
| 427 | #define DRV_SUSPEND_STATE_TYPE pm_message_t |
| 428 | #else |
| 429 | #define DRV_SUSPEND_STATE_TYPE uint32 |
| 430 | #endif |
| 431 | |
| 432 | #endif /* _linuxver_h_ */ |
| 433 | |