Commit 5aa0207a authored by Federico Vaga's avatar Federico Vaga

drv: add compatibility header file

Improve code readability by moving all #if to a dedicated file
Signed-off-by: Federico Vaga's avatarFederico Vaga <federico.vaga@cern.ch>
parent a5cbe2b0
// SPDX-License-Identifier: GPLv2
/*
* Copyright (C) 2017 CERN (www.cern.ch)
* Author: Federico Vaga <federico.vaga@cern.ch>
*/
#ifndef __SPEC_COMPAT_H__
#define __SPEC_COMPAT_H__
#include <linux/version.h>
/*
* The way this function is used in this code is to enable MSI. So,
* this compat function assumes that we always do that and so we
* ignore flags
*/
#if KERNEL_VERSION(4,11,0) > LINUX_VERSION_CODE
int pci_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,
unsigned int max_vecs, unsigned int flags) {
#if KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE
return pci_enable_msi_block(dev, min_vecs);
#else
return pci_enable_msi_exact(dev, min_vecs);
#endif
}
void pci_free_irq_vectors(struct pci_dev *pdev) {
pci_disable_msi(pdev);
}
#endif
#endif
// SPDX-License-Identifier: GPLv2
/*
* Copyright (C) 2017 CERN (www.cern.ch)
* Author: Federico Vaga <federico.vaga@cern.ch>
*/
#ifndef __SPEC_COMPAT_H__
#define __SPEC_COMPAT_H__
#include <linux/gpio.h>
#include <linux/version.h>
static inline struct device *gc_to_dev(struct gpio_chip *gc)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
return gc->dev;
#else
return gc->parent;
#endif
}
static inline void gc_assign_dev(struct gpio_chip *gc,
struct device *dev)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
gc->dev = &fmc->dev;
#else
gc->parent = &fmc->dev;
#endif
}
static inline void gpiochip_remove_compat(struct gpio_chip *gc)
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(3,17,0)
gpiochip_remove(gc);
#else
int ret;
ret = gpiochip_remove(gc);
if (ret)
dev_err(gc_to_dev(gc),
"DANGER %i! gpio chip can't be removed\n",
ret);
#endif
}
......@@ -20,12 +20,12 @@
#include <linux/pci.h>
#include <linux/io.h>
#include <asm/unaligned.h>
#include <linux/version.h>
#include <linux/fs.h>
#include <linux/vmalloc.h>
#include <linux/uaccess.h>
#include "spec.h"
#include "spec-compat.h"
#include "loader-ll.h"
static char *spec_fw_name_45t = "fmc/spec-init.bin";
......@@ -215,19 +215,8 @@ static int spec_probe(struct pci_dev *pdev,
spec->pdev = pdev;
if (spec_use_msi) {
/*
* This should be "4" but arch/x86/kernel/apic/io_apic.c
* says "x86 doesn't support multiple MSI yet".
*/
#if KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE
ret = pci_enable_msi_block(pdev, 1);
#else
#if KERNEL_VERSION(4,11,0) > LINUX_VERSION_CODE
ret = pci_enable_msi_exact(pdev, 1);
#else
ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI | PCI_IRQ_LEGACY);
#endif
#endif
ret = pci_alloc_irq_vectors(pdev, 1, 1,
PCI_IRQ_MSI | PCI_IRQ_LEGACY);
if (ret < 0)
dev_err(&pdev->dev, "%s: enable msi block: error %i\n",
__func__, ret);
......@@ -284,11 +273,7 @@ out_unmap:
}
pci_set_drvdata(pdev, NULL);
if (spec_use_msi)
#if KERNEL_VERSION(4,11,0) > LINUX_VERSION_CODE
pci_disable_msi(pdev);
#else
pci_free_irq_vectors(pdev);
#endif
pci_disable_device(pdev);
kfree(spec);
return ret;
......@@ -311,11 +296,7 @@ static void spec_remove(struct pci_dev *pdev)
}
pci_set_drvdata(pdev, NULL);
kfree(spec);
#if KERNEL_VERSION(4,11,0) > LINUX_VERSION_CODE
//pci_disable_msi(pdev);
#else
pci_free_irq_vectors(pdev);
#endif
pci_disable_device(pdev);
}
......
......@@ -10,7 +10,6 @@
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/version.h>
#include <linux/slab.h>
#include <linux/gpio.h>
#include <linux/fmc.h>
......@@ -18,12 +17,7 @@
static inline struct fmc_device *gc_to_fmc(struct gpio_chip *gc)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
struct device *dev = gc->dev;
#else
struct device *dev = gc->parent;
#endif
return container_of(dev, struct fmc_device, dev);
return container_of(gc_to_dev(gc), struct fmc_device, dev);
}
static int wrn_gpio_input(struct gpio_chip *chip, unsigned offset)
......@@ -77,11 +71,7 @@ int wrn_gpio_init(struct fmc_device *fmc)
if (!gc)
return -ENOMEM;
*gc = wrn_gpio_template;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
gc->dev = &fmc->dev;
#else
gc->parent = &fmc->dev;
#endif
gc_assign_dev(gc, &fmc->dev);
ret = gpiochip_add(gc);
if (ret < 0)
......@@ -99,17 +89,6 @@ out_free:
void wrn_gpio_exit(struct fmc_device *fmc)
{
struct wrn_drvdata *dd = fmc_get_drvdata(fmc);
struct gpio_chip *gc = dd->gc;
#if LINUX_VERSION_CODE > KERNEL_VERSION(3,17,0)
gpiochip_remove(gc);
#else
int ret;
ret = gpiochip_remove(gc);
if (ret)
dev_err(fmc->hwdev, "DANGER %i! gpio chip can't be removed\n",
ret);
#endif
return;
gpiochip_remove_compat(dd->gc);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment