compat.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. *
  3. * @file compat.h
  4. * @author Michael Stapelberg
  5. * @date 2009-04-05
  6. * @brief Contains compatibility definitions for the different linux kernel versions to avoid
  7. * putting ifdefs all over the driver code.
  8. *
  9. */
  10. #ifndef _COMPAT_H
  11. #define _COMPAT_H
  12. /*
  13. #if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0)
  14. # error "Linux 3.0 and latter are supported"
  15. #endif
  16. */
  17. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
  18. # define __devinit
  19. # define __devexit
  20. # define __devinitdata
  21. #endif
  22. /* dev_name is the wrapper one needs to use to access what was formerly called
  23. * bus_id in struct device. However, before 2.6.27, direct access was necessary,
  24. * so we provide our own version. */
  25. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
  26. static inline const char *dev_name(struct device *dev) {
  27. return dev->bus_id;
  28. }
  29. #endif
  30. /* SetPageLocked disappeared in v2.6.27 */
  31. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
  32. #define compat_lock_page SetPageLocked
  33. #define compat_unlock_page ClearPageLocked
  34. #else
  35. /* in v2.6.28, __set_page_locked and __clear_page_locked was introduced */
  36. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
  37. #define compat_lock_page __set_page_locked
  38. #define compat_unlock_page __clear_page_locked
  39. #else
  40. /* However, in v2.6.27 itself, neither of them is there, so
  41. * we need to use our own function fiddling with bits inside
  42. * the page struct :-\ */
  43. static inline void compat_lock_page(struct page *page) {
  44. __set_bit(PG_locked, &page->flags);
  45. }
  46. static inline void compat_unlock_page(struct page *page) {
  47. __clear_bit(PG_locked, &page->flags);
  48. }
  49. #endif
  50. #endif
  51. /* Before 2.6.13, simple_class was the standard interface. Nowadays, it's just called class */
  52. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,13)
  53. #define class_compat class_simple
  54. /* These functions are redirected to their old corresponding functions */
  55. #define class_create(module, name) class_simple_create(module, name)
  56. #define class_destroy(type) class_simple_destroy(type)
  57. #define class_device_destroy(unused, devno) class_simple_device_remove(devno)
  58. #define class_device_create(type, unused, devno, devpointer, nameformat, minor, unused) \
  59. class_simple_device_add(type, devno, devpointer, nameformat, minor)
  60. #define class_set_devdata(classdev, privdata) classdev->class_data = privdata
  61. #define DEVICE_ATTR_COMPAT
  62. #define sysfs_attr_def_name(name) class_device_attr_##name
  63. #define sysfs_attr_def_pointer privdata->class_dev
  64. #define SYSFS_GET_FUNCTION(name) ssize_t name(struct class_device *cls, char *buf)
  65. #define SYSFS_SET_FUNCTION(name) ssize_t name(struct class_device *cls, const char *buf, size_t count)
  66. #define SYSFS_GET_PRIVDATA (pcidriver_privdata_t*)cls->class_data
  67. #else
  68. /* In 2.6.26, device.h was changed quite significantly. Luckily, it only affected
  69. type/function names, for the most part. */
  70. //#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
  71. #define class_device_attribute device_attribute
  72. #define CLASS_DEVICE_ATTR DEVICE_ATTR
  73. #define class_device device
  74. #define class_data dev
  75. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
  76. #define class_device_create(type, parent, devno, devpointer, nameformat, minor, privdata) \
  77. device_create(type, parent, devno, privdata, nameformat, minor)
  78. #else
  79. #define class_device_create(type, parent, devno, devpointer, nameformat, minor, unused) \
  80. device_create(type, parent, devno, nameformat, minor)
  81. #endif
  82. #define class_device_create_file device_create_file
  83. #define class_device_remove_file device_remove_file
  84. #define class_device_destroy device_destroy
  85. #define DEVICE_ATTR_COMPAT struct device_attribute *attr,
  86. #define class_set_devdata dev_set_drvdata
  87. #define sysfs_attr_def_name(name) dev_attr_##name
  88. #define sysfs_attr_def_pointer privdata->class_dev
  89. #define SYSFS_GET_FUNCTION(name) ssize_t name(struct device *dev, struct device_attribute *attr, char *buf)
  90. #define SYSFS_SET_FUNCTION(name) ssize_t name(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  91. #define SYSFS_GET_PRIVDATA dev_get_drvdata(dev)
  92. //#endif
  93. #define class_compat class
  94. #endif
  95. /* The arguments of IRQ handlers have been changed in 2.6.19. It's very likely that
  96. int irq will disappear somewhen in the future (current is 2.6.29), too. */
  97. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
  98. #define IRQ_HANDLER_FUNC(name) irqreturn_t name(int irq, void *dev_id)
  99. #else
  100. #define IRQ_HANDLER_FUNC(name) irqreturn_t name(int irq, void *dev_id, struct pt_regs *regs)
  101. #endif
  102. /* atomic_inc_return appeared in 2.6.9, at least in CERN scientific linux, provide
  103. compatibility wrapper for older kernels */
  104. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,9)
  105. static int atomic_inc_return(atomic_t *variable) {
  106. atomic_inc(variable);
  107. return atomic_read(variable);
  108. }
  109. #endif
  110. /* sg_set_page is available starting at 2.6.24 */
  111. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
  112. #define sg_set_page(sg, set_page, set_length, set_offset) do { \
  113. (sg)->page = set_page; \
  114. (sg)->length = set_length; \
  115. (sg)->offset = set_offset; \
  116. } while (0)
  117. #endif
  118. /* Before 2.6.20, disable was not an atomic counter, so this check was needed */
  119. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
  120. #define pci_disable_device(pdev) do { \
  121. if (pdev->is_enabled) \
  122. pci_disable_device(pdev); \
  123. } while (0)
  124. #endif
  125. /* Before 2.6.24, scatter/gather lists did not need to be initialized */
  126. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
  127. #define sg_init_table(sg, nr_pages)
  128. #endif
  129. /* SA_SHIRQ was renamed to IRQF_SHARED in 2.6.24 */
  130. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  131. #define request_irq(irq, irq_handler, modname, privdata) request_irq(irq, irq_handler, IRQF_SHARED, modname, privdata)
  132. #else
  133. #define request_irq(irq, irq_handler, modname, privdata) request_irq(irq, irq_handler, SA_SHIRQ, modname, privdata)
  134. #endif
  135. /* In 2.6.13, io_remap_page_range was removed in favor for io_remap_pfn_range which works on
  136. more platforms and allows more memory space */
  137. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
  138. #define io_remap_pfn_range_compat(vmap, vm_start, bar_addr, bar_length, vm_page_prot) \
  139. io_remap_pfn_range(vmap, vm_start, (bar_addr >> PAGE_SHIFT), bar_length, vm_page_prot)
  140. #else
  141. #define io_remap_pfn_range_compat(vmap, vm_start, bar_addr, bar_length, vm_page_prot) \
  142. io_remap_page_range(vmap, vm_start, bar_addr, bar_length, vm_page_prot)
  143. #endif
  144. /* In 2.6.10, remap_pfn_range was introduced, see io_remap_pfn_range_compat */
  145. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10)
  146. #define remap_pfn_range_compat(vmap, vm_start, bar_addr, bar_length, vm_page_prot) \
  147. remap_pfn_range(vmap, vm_start, (bar_addr >> PAGE_SHIFT), bar_length, vm_page_prot)
  148. #define remap_pfn_range_cpua_compat(vmap, vm_start, cpua, size, vm_page_prot) \
  149. remap_pfn_range(vmap, vm_start, page_to_pfn(virt_to_page((void*)cpua)), size, vm_page_prot)
  150. #else
  151. #define remap_pfn_range_compat(vmap, vm_start, bar_addr, bar_length, vm_page_prot) \
  152. remap_page_range(vmap, vm_start, bar_addr, bar_length, vm_page_prot)
  153. #define remap_pfn_range_cpua_compat(vmap, vm_start, cpua, size, vm_page_prot) \
  154. remap_page_range(vmap, vm_start, virt_to_phys((void*)cpua), size, vm_page_prot)
  155. #endif
  156. /**
  157. * Go over the pages of the kmem buffer, and mark them as reserved.
  158. * This is needed, otherwise mmaping the kernel memory to user space
  159. * will fail silently (mmaping /dev/null) when using remap_xx_range.
  160. */
  161. static inline void set_pages_reserved_compat(unsigned long cpua, unsigned long size)
  162. {
  163. /* Starting in 2.6.15, the PG_RESERVED bit was removed.
  164. See also http://lwn.net/Articles/161204/ */
  165. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
  166. struct page *page, *last_page;
  167. page = virt_to_page(cpua);
  168. last_page = virt_to_page(cpua + size - 1);
  169. for (; page <= last_page; page++)
  170. SetPageReserved(page);
  171. #endif
  172. }
  173. int pcidriver_pcie_get_mps(struct pci_dev *dev);
  174. int pcidriver_pcie_set_mps(struct pci_dev *dev, int mps);
  175. #endif