From patchwork Wed Jul 23 12:55:20 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 372932 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 06BD314019E for ; Wed, 23 Jul 2014 23:01:27 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 7A2C94B6E6; Wed, 23 Jul 2014 14:59:58 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 5hnCilojV74m; Wed, 23 Jul 2014 14:59:58 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 7C3B9A761E; Wed, 23 Jul 2014 14:57:35 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 78976A74A6 for ; Wed, 23 Jul 2014 14:56:21 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ilhOa+CLb96L for ; Wed, 23 Jul 2014 14:56:20 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-ie0-f201.google.com (mail-ie0-f201.google.com [209.85.223.201]) by theia.denx.de (Postfix) with ESMTPS id 39AAAA744B for ; Wed, 23 Jul 2014 14:55:56 +0200 (CEST) Received: by mail-ie0-f201.google.com with SMTP id tr6so322680ieb.0 for ; Wed, 23 Jul 2014 05:55:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=4Qc85rVQh7Ql1fLlorRCpq3bIpemCWIbdHrverBPWYE=; b=CFQFnypEQ2bac8PmJWa6m+l3Tjtm3kPVwGxVIJZ09rbOfiyUtMLr/9q/mDBNQM+Cfs FgMnsOzoIB9oisAmwQ3W7qJF+RkxrNwsqNOZ/IIYnHR55iB+MxIpEI8VUsvHDDCGXOSq 2e6Anp3VMGLjsbag7dpwvcEJ+gEjF0gSbhDHRux14Wer9Ao0y/edhNZopsoRDrXZmloJ B/GQsHPHt7MbsU8R8LtgIbkmvAGy+D3koT1MdQC4h7Aol1SKw0Ev7k4xsxUBARx4miZT BKCu/nrzcQanFv91oLbT6mYSGBC6AbypIuwYlci70poJNShrKZrktyulieeb+BcXBHnJ donw== X-Gm-Message-State: ALoCoQmPxV7fH0VHw1Thwwq22HugUxr62N4Hn63f87GdUDzzSF6KJ8SajN6TVbWv8svOgGBlKPiK X-Received: by 10.42.207.5 with SMTP id fw5mr706976icb.20.1406120155662; Wed, 23 Jul 2014 05:55:55 -0700 (PDT) Received: from corp2gmr1-1.hot.corp.google.com (corp2gmr1-1.hot.corp.google.com [172.24.189.92]) by gmr-mx.google.com with ESMTPS id z50si391392yhb.3.2014.07.23.05.55.55 for (version=TLSv1.1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 23 Jul 2014 05:55:55 -0700 (PDT) Received: from kaki.bld.corp.google.com (kaki.bld.corp.google.com [172.29.216.32]) by corp2gmr1-1.hot.corp.google.com (Postfix) with ESMTP id 75C2B31C27E; Wed, 23 Jul 2014 05:55:55 -0700 (PDT) Received: by kaki.bld.corp.google.com (Postfix, from userid 121222) id 32721220828; Wed, 23 Jul 2014 06:55:55 -0600 (MDT) From: Simon Glass To: U-Boot Mailing List Date: Wed, 23 Jul 2014 06:55:20 -0600 Message-Id: <1406120124-9373-26-git-send-email-sjg@chromium.org> X-Mailer: git-send-email 2.0.0.526.g5318336 In-Reply-To: <1406120124-9373-1-git-send-email-sjg@chromium.org> References: <1406120124-9373-1-git-send-email-sjg@chromium.org> Subject: [U-Boot] [PATCH v3 25/29] dm: Introduce per-child data for devices X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de 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 --- Changes in v3: None Changes in v2: None 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 59ef05c..11af35d 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 74bb5f0..42d250f 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 3f0f711..20207ce 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; }; @@ -159,6 +165,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 * * This checks that dev is not NULL, but no other checks for now diff --git a/include/dm/test.h b/include/dm/test.h index e8e1c0b..7b04850 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 08a4725..df8edcb3 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);