From e59f458de6999b8a786da857e653db6777f675ca Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Wed, 23 Jul 2014 06:55:20 -0600 Subject: [PATCH] dm: Introduce per-child data for devices Some device types can have child devices and want to store information about them. For example a USB flash stick attached to a USB host controller would likely use this space. The controller can hold information about the USB state of each of its children. The data is stored attached to the child device in the 'parent_priv' member. It can be auto-allocated by dm when the child is probed. To do this, add a per_child_auto_alloc_size value to the parent driver. Signed-off-by: Simon Glass --- doc/driver-model/README.txt | 25 +++++++++----- drivers/core/device.c | 26 +++++++++++++++ include/dm/device.h | 20 ++++++++++++ include/dm/test.h | 9 +++++ test/dm/bus.c | 65 +++++++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+), 8 deletions(-) diff --git a/doc/driver-model/README.txt b/doc/driver-model/README.txt index 59ef05cf2b..11af35dcba 100644 --- a/doc/driver-model/README.txt +++ b/doc/driver-model/README.txt @@ -95,7 +95,7 @@ are provided in test/dm. To run them, try: You should see something like this: <...U-Boot banner...> - Running 19 driver model tests + Running 20 driver model tests Test: dm_test_autobind Test: dm_test_autoprobe Test: dm_test_bus_children @@ -103,6 +103,7 @@ You should see something like this: Device 'c-test@0': seq 0 is in use by 'a-test' Device 'c-test@1': seq 1 is in use by 'd-test' Test: dm_test_bus_children_funcs + Test: dm_test_bus_parent_data Test: dm_test_children Test: dm_test_fdt Device 'd-test': seq 3 is in use by 'b-test' @@ -489,16 +490,23 @@ steps (see device_probe()): stored in the device, but it is uclass data. owned by the uclass driver. It is possible for the device to access it. - d. All parent devices are probed. It is not possible to activate a device + d. If the device's immediate parent specifies a per_child_auto_alloc_size + then this space is allocated. This is intended for use by the parent + device to keep track of things related to the child. For example a USB + flash stick attached to a USB host controller would likely use this + space. The controller can hold information about the USB state of each + of its children. + + e. All parent devices are probed. It is not possible to activate a device unless its predecessors (all the way up to the root device) are activated. This means (for example) that an I2C driver will require that its bus be activated. - e. The device's sequence number is assigned, either the requested one + f. The device's sequence number is assigned, either the requested one (assuming no conflicts) or the next available one if there is a conflict or nothing particular is requested. - f. If the driver provides an ofdata_to_platdata() method, then this is + g. If the driver provides an ofdata_to_platdata() method, then this is called to convert the device tree data into platform data. This should do various calls like fdtdec_get_int(gd->fdt_blob, dev->of_offset, ...) to access the node and store the resulting information into dev->platdata. @@ -514,7 +522,7 @@ steps (see device_probe()): data, one day it is possible that U-Boot will cache platformat data for devices which are regularly de/activated). - g. The device's probe() method is called. This should do anything that + h. The device's probe() method is called. This should do anything that is required by the device to get it going. This could include checking that the hardware is actually present, setting up clocks for the hardware and setting up hardware registers to initial values. The code @@ -529,9 +537,9 @@ steps (see device_probe()): allocate the priv space here yourself. The same applies also to platdata_auto_alloc_size. Remember to free them in the remove() method. - h. The device is marked 'activated' + i. The device is marked 'activated' - i. The uclass's post_probe() method is called, if one exists. This may + j. The uclass's post_probe() method is called, if one exists. This may cause the uclass to do some housekeeping to record the device as activated and 'known' by the uclass. @@ -562,7 +570,8 @@ remove it. This performs the probe steps in reverse: to be sure that no hardware is running, it should be enough to remove all devices. - d. The device memory is freed (platform data, private data, uclass data). + d. The device memory is freed (platform data, private data, uclass data, + parent data). Note: Because the platform data for a U_BOOT_DEVICE() is defined with a static pointer, it is not de-allocated during the remove() method. For diff --git a/drivers/core/device.c b/drivers/core/device.c index 74bb5f0f8e..42d250f4a4 100644 --- a/drivers/core/device.c +++ b/drivers/core/device.c @@ -218,6 +218,13 @@ static void device_free(struct udevice *dev) free(dev->uclass_priv); dev->uclass_priv = NULL; } + if (dev->parent) { + size = dev->parent->driver->per_child_auto_alloc_size; + if (size) { + free(dev->parent_priv); + dev->parent_priv = NULL; + } + } } int device_probe(struct udevice *dev) @@ -263,6 +270,15 @@ int device_probe(struct udevice *dev) /* Ensure all parents are probed */ if (dev->parent) { + size = dev->parent->driver->per_child_auto_alloc_size; + if (size) { + dev->parent_priv = calloc(1, size); + if (!dev->parent_priv) { + ret = -ENOMEM; + goto fail; + } + } + ret = device_probe(dev->parent); if (ret) goto fail; @@ -377,6 +393,16 @@ void *dev_get_priv(struct udevice *dev) return dev->priv; } +void *dev_get_parentdata(struct udevice *dev) +{ + if (!dev) { + dm_warn("%s: null device", __func__); + return NULL; + } + + return dev->parent_priv; +} + static int device_get_device_tail(struct udevice *dev, int ret, struct udevice **devp) { diff --git a/include/dm/device.h b/include/dm/device.h index 3f0f7111f7..20207ce61a 100644 --- a/include/dm/device.h +++ b/include/dm/device.h @@ -51,6 +51,7 @@ struct driver_info; * @priv: Private data for this device * @uclass: Pointer to uclass for this device * @uclass_priv: The uclass's private data for this device + * @parent_priv: The parent's private data for this device * @uclass_node: Used by uclass to link its devices * @child_head: List of children of this device * @sibling_node: Next device in list of all devices @@ -67,6 +68,7 @@ struct udevice { void *priv; struct uclass *uclass; void *uclass_priv; + void *parent_priv; struct list_head uclass_node; struct list_head child_head; struct list_head sibling_node; @@ -124,6 +126,9 @@ struct udevice_id { * This is typically only useful for device-tree-aware drivers (those with * an of_match), since drivers which use platdata will have the data * provided in the U_BOOT_DEVICE() instantiation. + * @per_child_auto_alloc_size: Each device can hold private data owned by + * its parent. If required this will be automatically allocated if this + * value is non-zero. * @ops: Driver-specific operations. This is typically a list of function * pointers defined by the driver, to implement driver functions required by * the uclass. @@ -140,6 +145,7 @@ struct driver { int (*ofdata_to_platdata)(struct udevice *dev); int priv_auto_alloc_size; int platdata_auto_alloc_size; + int per_child_auto_alloc_size; const void *ops; /* driver-specific operations */ uint32_t flags; }; @@ -158,6 +164,20 @@ struct driver { */ void *dev_get_platdata(struct udevice *dev); +/** + * dev_get_parentdata() - Get the parent data for a device + * + * The parent data is data stored in the device but owned by the parent. + * For example, a USB device may have parent data which contains information + * about how to talk to the device over USB. + * + * This checks that dev is not NULL, but no other checks for now + * + * @dev Device to check + * @return parent data, or NULL if none + */ +void *dev_get_parentdata(struct udevice *dev); + /** * dev_get_priv() - Get the private data for a device * diff --git a/include/dm/test.h b/include/dm/test.h index e8e1c0b24a..7b04850035 100644 --- a/include/dm/test.h +++ b/include/dm/test.h @@ -82,6 +82,15 @@ struct dm_test_uclass_priv { int total_add; }; +/** + * struct dm_test_parent_data - parent's information on each child + * + * @sum: Test value used to check parent data works correctly + */ +struct dm_test_parent_data { + int sum; +}; + /* * Operation counts for the test driver, used to check that each method is * called correctly diff --git a/test/dm/bus.c b/test/dm/bus.c index 08a4725648..df8edcb37d 100644 --- a/test/dm/bus.c +++ b/test/dm/bus.c @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -32,6 +33,7 @@ U_BOOT_DRIVER(testbus_drv) = { .probe = testbus_drv_probe, .priv_auto_alloc_size = sizeof(struct dm_test_priv), .platdata_auto_alloc_size = sizeof(struct dm_test_pdata), + .per_child_auto_alloc_size = sizeof(struct dm_test_parent_data), }; UCLASS_DRIVER(testbus) = { @@ -107,3 +109,66 @@ static int dm_test_bus_children_funcs(struct dm_test_state *dms) return 0; } DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); + +/* Test that the bus can store data about each child */ +static int dm_test_bus_parent_data(struct dm_test_state *dms) +{ + struct dm_test_parent_data *parent_data; + struct udevice *bus, *dev; + struct uclass *uc; + int value; + + ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); + + /* Check that parent data is allocated */ + ut_assertok(device_find_child_by_seq(bus, 0, true, &dev)); + ut_asserteq_ptr(NULL, dev_get_parentdata(dev)); + ut_assertok(device_get_child_by_seq(bus, 0, &dev)); + parent_data = dev_get_parentdata(dev); + ut_assert(NULL != parent_data); + + /* Check that it starts at 0 and goes away when device is removed */ + parent_data->sum += 5; + ut_asserteq(5, parent_data->sum); + device_remove(dev); + ut_asserteq_ptr(NULL, dev_get_parentdata(dev)); + + /* Check that we can do this twice */ + ut_assertok(device_get_child_by_seq(bus, 0, &dev)); + parent_data = dev_get_parentdata(dev); + ut_assert(NULL != parent_data); + parent_data->sum += 5; + ut_asserteq(5, parent_data->sum); + + /* Add parent data to all children */ + ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc)); + value = 5; + uclass_foreach_dev(dev, uc) { + /* Ignore these if they are not on this bus */ + if (dev->parent != bus) { + ut_asserteq_ptr(NULL, dev_get_parentdata(dev)); + continue; + } + ut_assertok(device_probe(dev)); + parent_data = dev_get_parentdata(dev); + + parent_data->sum = value; + value += 5; + } + + /* Check it is still there */ + value = 5; + uclass_foreach_dev(dev, uc) { + /* Ignore these if they are not on this bus */ + if (dev->parent != bus) + continue; + parent_data = dev_get_parentdata(dev); + + ut_asserteq(value, parent_data->sum); + value += 5; + } + + return 0; +} + +DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); -- 2.39.5