Как получить доступ к членам вложенной структуры и присвоить им значения, считанные из дерева устройств? - PullRequest
0 голосов
/ 13 декабря 2018

Вот мое дерево устройств:

Кто-нибудь может исправить этот код, я новичок в разработке c и драйверов, как получить доступ к членам вложенной структуры, назначить значения, прочитанные из дерева устройств, и заполнить егозначения в классе sys.

Я ожидаю следующую структуру:

$ cd sys / class / my_driver / x / такой же, как для узла a.

/ {
my_features: my_features {
    compatible = "my_driver";
            airplane {
                getAirplaneUIStatus = <0>;
                setAirplaneUIStatus=<2>;
                turnonairpane=<1>;
            };
            hotspot {
                getHotspotUIStatus = <1>;
                setHotspotUIStatus = <3>;
            };

    };

};

Я написал следующий код:

#include <linux/device.h>
#include <linux/of_gpio.h>
#include <linux/platform_device.h>
#include <linux/of_platform.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/slab.h>

//int airplane;
//static struct class *my_feature_driver_class;
static struct myConfig *my_pdata = NULL;

typedef struct  {
     int setAirplaneUIStatus;
     int turnonairpane;
} airplane;

typedef struct {
    int setHotspotUIStatus;
    int getHotspotUIStatus;
} hotspot;

typedef struct myConfig {
    struct airplane *ap;
    struct hotspot *hp;   
  } myConfig;

enum my_configuration_t {
    my_CONFIG_AIRPLAN = 0,
    my_CONFIG_HOTSPOT = 1,

};

static int process_my_entry(struct device_node *np, char *name) {
    int value = 0, ret = 0;
    ret = of_property_read_u32(np, name, &value);
    pr_err("my ret value : %d",  ret);
    pr_err("my value : %d", value );
    return value;
  };

struct myConfig *my_entries_parse_dt(struct device *dev) {

    struct device_node *np, *pp;
    struct myConfig *pdata;
    struct airplane *ap;
    struct hotspot *hp;

    //struct my_apis_keys *api; 
    int error;
    int napis;
    int i;

    np = dev->of_node;

    if(!np)
    {
        error = -ENODEV;
        goto err_out;
    }

    napis = of_get_child_count(np);
    pr_err("my child nodes : %d",  napis);
    if (napis == 0) {
        error = -ENODEV;
        goto err_out;
    }

    pdata = devm_kzalloc(dev, sizeof(*pdata) + napis ,
            GFP_KERNEL);

    if (!pdata) {
        error = -ENOMEM;
        goto err_out;
    }

    //pdata->airplane = (struct my_apis_keys *)(pdata + 1);
    //pdata->napis = napis;

    pdata->ap = ap;
    pdata->hp = hp;

    i = 0;
    for_each_child_of_node(np, pp){
        //api = &pdata->airplane[i++];
        ap->setAirplaneUIStatus = process_my_entry(pp, "setAirplaneUIStatus");
        ap->turnonairpane = process_my_entry(pp, "turnonairpane");
        hp->setHotspotUIStatus = process_my_entry(pp, "setHotspotUIStatus");
        hp->getHotspotUIStatus = process_my_entry(pp, "getHotspotUIStatus");
        pr_err("my_entries_parse_dt ");
    }

    if (ap->turnonairpane == 0) {
        error = -EINVAL;
        goto err_free_pdata;
    }

    return pdata;

err_free_pdata:
    kfree(pdata);
    kfree(ap);
    kfree(hp);
err_out:
    return ERR_PTR(error);

};


int GetmyConfiguration(enum my_configuration_t type)
  {
    int ret=0;

    switch (type) {
        case my_CONFIG_AIRPLAN:
            return my_pdata->ap->setAirplaneUIStatus;
        case my_CONFIG_HOTSPOT:
            return my_pdata->hp->getHotspotUIStatus;
        default:
            return ret;
        }
  };

static ssize_t get_airplan(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  {
    return sprintf(buf,"%d",GetmyConfiguration(my_CONFIG_AIRPLAN));
  }

  static ssize_t get_hotspot(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  {
    return sprintf(buf,"%d",GetmyConfiguration(my_CONFIG_HOTSPOT));
  }

static struct kobj_attribute airplane_attribute = __ATTR(airplane, 0444, get_airplan, NULL);
static struct kobj_attribute hotspot_attribute = __ATTR(hotspot, 0444, get_hotspot, NULL);
static struct attribute *myconfig_attrs[] = {
    &airplane_attribute.attr,
    &hotspot_attribute.attr,
    NULL,
    };
static struct attribute_group myconfig_attr_group = {
    .attrs = myconfig_attrs,
  };
int my_feature_driver_probe(struct platform_device * pdev)
{
    int ret = 0;
   struct device *dev = &pdev->dev;
   struct myConfig *pdata= my_entries_parse_dt(dev);
   struct kobject *myconfig_kobj;

   if(pdata == NULL)
    {
        dev_err(dev, "SW version is not included in device tree\n");
        return -EINVAL;
    }
    /* setup pdata */
  my_pdata = pdata;
  platform_set_drvdata(pdev, pdata);
  myconfig_kobj = kobject_create_and_add("my_config", NULL);
  if (!myconfig_kobj)
  {
       pr_err("myconfig_kobj null ");
        return -ENOMEM;
  }
  ret = sysfs_create_group(myconfig_kobj, &myconfig_attr_group);

    if (ret)
    {
        pr_err("my sys_fs_create if %d", ret);
        kobject_put(myconfig_kobj);
    }
    pr_err("my sys_fs_create out %d", ret);

  return 0;

}

int my_feature_driver_remove(struct platform_device * pdev)
{
  int err=0;
  pr_err("my Feature  driver remove :");
  return err;

}

static struct of_device_id bs_of_match[] = {
    { .compatible = "my_driver", },
    { },
};

MODULE_DEVICE_TABLE(of, bs_of_match);

static struct platform_driver my_feature_device_driver = {
    .probe          = my_feature_driver_probe,
    .remove         = my_feature_driver_remove,
    .driver         = {
        .name   = "my_driver",
        .owner  = THIS_MODULE,
        .of_match_table = of_match_ptr(bs_of_match),
    }
};

static int __init my_feature_init(void)
{
    pr_err("my feature Driver init");
    return platform_driver_register(&my_feature_device_driver);
}

static void __exit my_feature_exit(void)
{
    pr_err("my feature driver exit");
    platform_driver_unregister(&my_feature_device_driver);
}

late_initcall(my_feature_init);
module_exit(my_feature_exit);

MODULE_LICENSE("GPL v1");
MODULE_AUTHOR("SKY ");
MODULE_DESCRIPTION("my  Feature Configatrion ");
MODULE_DEVICE_TABLE(of, bs_of_match);

Теперь я получаю следующую ошибку, может кто-нибудь сказать мне, как решить эту проблему.

error: dereferencing pointer to incomplete type
   ap->setAirplaneUIStatus = process_osx_entry(pp, "setAirplaneUIStatus");
     ^
 error: dereferencing pointer to incomplete type
   ap->turnonairpane = process_osx_entry(pp, "turnonairpane");
     ^
 error: dereferencing pointer to incomplete type
   hp->setHotspotUIStatus = process_osx_entry(pp, "setHotspotUIStatus");
     ^
 error: dereferencing pointer to incomplete type
   hp->getHotspotUIStatus = process_osx_entry(pp, "getHotspotUIStatus");
     ^
 error: dereferencing pointer to incomplete type
  if (ap->turnonairpane == 0) {
        ^
 In function 'GetOSXConfiguration':
 error: dereferencing pointer to incomplete type
    return osx_pdata->ap->setAirplaneUIStatus;
                        ^
 error: dereferencing pointer to incomplete type
             return osx_pdata->hp->getHotspotUIStatus;

1 Ответ

0 голосов
/ 18 декабря 2018

Ваши объявления pdata, ap и hp неверны.Измените:

struct myConfig *pdata;
struct airplane *ap;
struct hotspot *hp;

на:

myConfig *pdata;
airplane *ap;
hotspot *hp;

Это потому, что myConfig, airplane и hotspot определены как:

typedef struct {
    ...
} XX;

Что приводит к тому, что объявление struct myConfig *pdata неправильно интерпретируется как struct struct ....

...