From 89ba6d553572fe9177ae472170b8373e49f97953 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 22 Dec 2020 19:30:29 -0700 Subject: [PATCH] dm: core: Use access methods for dev/uclass private data Use these functions in the core code as much as possible. With this, there are only two places where each priv/plat pointer is accessed, one for read and one for write. Signed-off-by: Simon Glass --- drivers/core/device-remove.c | 8 ++--- drivers/core/device.c | 61 +++++++++++++++++++----------------- drivers/core/uclass.c | 13 +++++--- 3 files changed, 45 insertions(+), 37 deletions(-) diff --git a/drivers/core/device-remove.c b/drivers/core/device-remove.c index 8c12169771..e15ab051be 100644 --- a/drivers/core/device-remove.c +++ b/drivers/core/device-remove.c @@ -94,11 +94,11 @@ int device_unbind(struct udevice *dev) } if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) { free(dev_get_uclass_plat(dev)); - dev->uclass_plat = NULL; + dev_set_uclass_plat(dev, NULL); } if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) { free(dev_get_parent_plat(dev)); - dev->parent_plat = NULL; + dev_set_parent_plat(dev, NULL); } ret = uclass_unbind_device(dev); if (ret) @@ -131,7 +131,7 @@ void device_free(struct udevice *dev) size = dev->uclass->uc_drv->per_device_auto; if (size) { free(dev_get_uclass_priv(dev)); - dev->uclass_priv = NULL; + dev_set_uclass_priv(dev, NULL); } if (dev->parent) { size = dev->parent->driver->per_child_auto; @@ -141,7 +141,7 @@ void device_free(struct udevice *dev) } if (size) { free(dev_get_parent_priv(dev)); - dev->parent_priv = NULL; + dev_set_parent_priv(dev, NULL); } } dev->flags &= ~DM_FLAG_PLATDATA_VALID; diff --git a/drivers/core/device.c b/drivers/core/device.c index f2d750c8de..261c3b2793 100644 --- a/drivers/core/device.c +++ b/drivers/core/device.c @@ -42,6 +42,7 @@ static int device_bind_common(struct udevice *parent, const struct driver *drv, struct uclass *uc; int size, ret = 0; bool auto_seq = true; + void *ptr; if (devp) *devp = NULL; @@ -64,7 +65,7 @@ static int device_bind_common(struct udevice *parent, const struct driver *drv, #ifdef CONFIG_DEVRES INIT_LIST_HEAD(&dev->devres_head); #endif - dev->plat = plat; + dev_set_plat(dev, plat); dev->driver_data = driver_data; dev->name = name; dev->node = node; @@ -102,25 +103,26 @@ static int device_bind_common(struct udevice *parent, const struct driver *drv, } if (alloc) { dev->flags |= DM_FLAG_ALLOC_PDATA; - dev->plat = calloc(1, drv->plat_auto); - if (!dev->plat) { + ptr = calloc(1, drv->plat_auto); + if (!ptr) { ret = -ENOMEM; goto fail_alloc1; } - if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat) { - memcpy(dev->plat, plat, of_plat_size); - } + if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat) + memcpy(ptr, plat, of_plat_size); + dev_set_plat(dev, ptr); } } size = uc->uc_drv->per_device_plat_auto; if (size) { dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA; - dev->uclass_plat = calloc(1, size); - if (!dev->uclass_plat) { + ptr = calloc(1, size); + if (!ptr) { ret = -ENOMEM; goto fail_alloc2; } + dev_set_uclass_plat(dev, ptr); } if (parent) { @@ -130,11 +132,12 @@ static int device_bind_common(struct udevice *parent, const struct driver *drv, } if (size) { dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA; - dev->parent_plat = calloc(1, size); - if (!dev->parent_plat) { + ptr = calloc(1, size); + if (!ptr) { ret = -ENOMEM; goto fail_alloc3; } + dev_set_parent_plat(dev, ptr); } /* put dev into parent's successor list */ list_add_tail(&dev->sibling_node, &parent->child_head); @@ -191,19 +194,19 @@ fail_uclass_bind: if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) { list_del(&dev->sibling_node); if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) { - free(dev->parent_plat); - dev->parent_plat = NULL; + free(dev_get_parent_plat(dev)); + dev_set_parent_plat(dev, NULL); } } fail_alloc3: if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) { - free(dev->uclass_plat); - dev->uclass_plat = NULL; + free(dev_get_uclass_plat(dev)); + dev_set_uclass_plat(dev, NULL); } fail_alloc2: if (dev->flags & DM_FLAG_ALLOC_PDATA) { - free(dev->plat); - dev->plat = NULL; + free(dev_get_plat(dev)); + dev_set_plat(dev, NULL); } fail_alloc1: devres_release_all(dev); @@ -324,6 +327,7 @@ int device_of_to_plat(struct udevice *dev) { const struct driver *drv; int size = 0; + void *ptr; int ret; if (!dev) @@ -352,36 +356,37 @@ int device_of_to_plat(struct udevice *dev) assert(drv); /* Allocate private data if requested and not reentered */ - if (drv->priv_auto && !dev->priv) { - dev->priv = alloc_priv(drv->priv_auto, drv->flags); - if (!dev->priv) { + if (drv->priv_auto && !dev_get_priv(dev)) { + ptr = alloc_priv(drv->priv_auto, drv->flags); + if (!ptr) { ret = -ENOMEM; goto fail; } + dev_set_priv(dev, ptr); } /* Allocate private data if requested and not reentered */ size = dev->uclass->uc_drv->per_device_auto; - if (size && !dev->uclass_priv) { - dev->uclass_priv = alloc_priv(size, - dev->uclass->uc_drv->flags); - if (!dev->uclass_priv) { + if (size && !dev_get_uclass_priv(dev)) { + ptr = alloc_priv(size, dev->uclass->uc_drv->flags); + if (!ptr) { ret = -ENOMEM; goto fail; } + dev_set_uclass_priv(dev, ptr); } /* Allocate parent data for this child */ if (dev->parent) { size = dev->parent->driver->per_child_auto; - if (!size) { + if (!size) size = dev->parent->uclass->uc_drv->per_child_auto; - } - if (size && !dev->parent_priv) { - dev->parent_priv = alloc_priv(size, drv->flags); - if (!dev->parent_priv) { + if (size && !dev_get_parent_priv(dev)) { + ptr = alloc_priv(size, drv->flags); + if (!ptr) { ret = -ENOMEM; goto fail; } + dev_set_parent_priv(dev, ptr); } } diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c index 5e24927b34..e845f60472 100644 --- a/drivers/core/uclass.c +++ b/drivers/core/uclass.c @@ -72,11 +72,14 @@ static int uclass_add(enum uclass_id id, struct uclass **ucp) if (!uc) return -ENOMEM; if (uc_drv->priv_auto) { - uc->priv = calloc(1, uc_drv->priv_auto); - if (!uc->priv) { + void *ptr; + + ptr = calloc(1, uc_drv->priv_auto); + if (!ptr) { ret = -ENOMEM; goto fail_mem; } + uclass_set_priv(uc, ptr); } uc->uc_drv = uc_drv; INIT_LIST_HEAD(&uc->sibling_node); @@ -94,8 +97,8 @@ static int uclass_add(enum uclass_id id, struct uclass **ucp) return 0; fail: if (uc_drv->priv_auto) { - free(uc->priv); - uc->priv = NULL; + free(uclass_get_priv(uc)); + uclass_set_priv(uc, NULL); } list_del(&uc->sibling_node); fail_mem: @@ -132,7 +135,7 @@ int uclass_destroy(struct uclass *uc) uc_drv->destroy(uc); list_del(&uc->sibling_node); if (uc_drv->priv_auto) - free(uc->priv); + free(uclass_get_priv(uc)); free(uc); return 0; -- 2.39.5