]> git.kernelconcepts.de Git - karo-tx-uboot.git/commitdiff
dm: Introduce per-child data for devices
authorSimon Glass <sjg@chromium.org>
Wed, 23 Jul 2014 12:55:20 +0000 (06:55 -0600)
committerSimon Glass <sjg@chromium.org>
Wed, 23 Jul 2014 13:08:37 +0000 (14:08 +0100)
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 <sjg@chromium.org>
doc/driver-model/README.txt
drivers/core/device.c
include/dm/device.h
include/dm/test.h
test/dm/bus.c

index 59ef05cf2b5405f36de05c8e50f1e90ea34d7426..11af35dcbaeda05e3dda2840c814b6f585281fe8 100644 (file)
@@ -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
index 74bb5f0f8efb8b568885a2a10ba09f118f052a9f..42d250f4a48200705ff246769cce69ecc03cdbef 100644 (file)
@@ -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)
 {
index 3f0f7111f7cd136b3b54527a334bfafe2a3229ed..20207ce61a05dd4525fab56441690c2176307429 100644 (file)
@@ -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
  *
index e8e1c0b24a38ffd8e4ca92e41d3a47d7cdacd7c8..7b04850035c283492bcf536d159fdc69dd907dec 100644 (file)
@@ -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
index 08a4725648e1da7f3c2bf0d4f690652fb125f622..df8edcb37dd16652aeaa97f6319b1b1ebadc6618 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <common.h>
 #include <dm.h>
+#include <dm/device-internal.h>
 #include <dm/root.h>
 #include <dm/test.h>
 #include <dm/ut.h>
@@ -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);