[PATCH v3 3/6] staging: kpc2000: simplified kp2000_device retrieval in device attributes call-backs.

Jeremy Sowden jeremy at azazel.net
Fri May 17 11:03:12 UTC 2019


The call-backs used the same recipe to get the pcard from dev:

  struct pci_dev *pdev = to_pci_dev(dev);
  struct kp2000_device *pcard;

  if (!pdev) return -ENXIO;
  pcard = pci_get_drvdata(pdev);
  if (!pcard) return -ENXIO;

where to_pci_dev is a wrapper for container_of.

However, pci_set_drvdata is called before the sysfs files are created:

  int  kp2000_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  {
    // ...

    pcard = kzalloc(sizeof(struct kp2000_device), GFP_KERNEL);

    // ...

    pcard->pdev = pdev;
    pci_set_drvdata(pdev, pcard);

    // ...

    err = sysfs_create_files(&(pdev->dev.kobj), kp_attr_list);

Therefore, to_pci_dev and pci_get_drvdata cannot return NULL, and pcard
can be initialized directly from dev:

  struct kp2000_device *pcard = dev_get_drvdata(dev);

Signed-off-by: Jeremy Sowden <jeremy at azazel.net>
---
 drivers/staging/kpc2000/kpc2000/core.c | 24 +++---------------------
 1 file changed, 3 insertions(+), 21 deletions(-)

diff --git a/drivers/staging/kpc2000/kpc2000/core.c b/drivers/staging/kpc2000/kpc2000/core.c
index eb8bac62d33d..9425c4dbc2f2 100644
--- a/drivers/staging/kpc2000/kpc2000/core.c
+++ b/drivers/staging/kpc2000/kpc2000/core.c
@@ -24,12 +24,7 @@
   ******************************************************/
 static ssize_t  show_attr(struct device *dev, struct device_attribute *attr, char *buf)
 {
-    struct pci_dev *pdev = to_pci_dev(dev);
-    struct kp2000_device *pcard;
-
-    if (!pdev)  return -ENXIO;
-    pcard = pci_get_drvdata(pdev);
-    if (!pcard)  return -ENXIO;
+    struct kp2000_device *pcard = dev_get_drvdata(dev);
 
     if (strcmp("ssid", attr->attr.name) == 0){         return scnprintf(buf, PAGE_SIZE, "%016llx\n", pcard->ssid);  } else
     if (strcmp("ddna", attr->attr.name) == 0){         return scnprintf(buf, PAGE_SIZE, "%016llx\n", pcard->ddna);  } else
@@ -43,31 +38,18 @@ static ssize_t  show_attr(struct device *dev, struct device_attribute *attr, cha
 
 static ssize_t  show_cpld_config_reg(struct device *dev, struct device_attribute *attr, char *buf)
 {
-	struct pci_dev *pdev = to_pci_dev(dev);
-	struct kp2000_device *pcard;
+	struct kp2000_device *pcard = dev_get_drvdata(dev);
 	u64 val;
 
-	if (!pdev)
-		return -ENXIO;
-
-	pcard = pci_get_drvdata(pdev);
-	if (!pcard)
-		return -ENXIO;
-
 	val = readq(pcard->sysinfo_regs_base + REG_CPLD_CONFIG);
 	return scnprintf(buf, PAGE_SIZE, "%016llx\n", val);
 }
 static ssize_t cpld_reconfigure(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
 {
-    struct pci_dev *pdev = to_pci_dev(dev);
+    struct kp2000_device *pcard = dev_get_drvdata(dev);
     long wr_val;
-    struct kp2000_device *pcard;
     int rv;
 
-    if (!pdev)  return -ENXIO;
-    pcard = pci_get_drvdata(pdev);
-    if (!pcard)  return -ENXIO;
-
     rv = kstrtol(buf, 0, &wr_val);
     if (rv < 0)  return rv;
     if (wr_val > 7)  return -EINVAL;
-- 
2.20.1



More information about the devel mailing list