Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ACPI: glue: Use acpi_dev_for_each_child()

Instead of walking the list of children of an ACPI device directly,
use acpi_dev_for_each_child() to carry out an action for all of
the given ACPI device's children.

This will help to eliminate the children list head from struct
acpi_device as it is redundant and it is used in questionable ways
in some places (in particular, locking is needed for walking the
list pointed to it safely, but it is often missing).

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

+63 -40
+63 -40
drivers/acpi/glue.c
··· 105 105 return FIND_CHILD_MAX_SCORE; 106 106 } 107 107 108 + struct find_child_walk_data { 109 + struct acpi_device *adev; 110 + u64 address; 111 + int score; 112 + bool check_children; 113 + }; 114 + 115 + static int check_one_child(struct acpi_device *adev, void *data) 116 + { 117 + struct find_child_walk_data *wd = data; 118 + int score; 119 + 120 + if (!adev->pnp.type.bus_address || acpi_device_adr(adev) != wd->address) 121 + return 0; 122 + 123 + if (!wd->adev) { 124 + /* This is the first matching object. Save it and continue. */ 125 + wd->adev = adev; 126 + return 0; 127 + } 128 + 129 + /* 130 + * There is more than one matching device object with the same _ADR 131 + * value. That really is unexpected, so we are kind of beyond the scope 132 + * of the spec here. We have to choose which one to return, though. 133 + * 134 + * First, get the score for the previously found object and terminate 135 + * the walk if it is maximum. 136 + */ 137 + if (!wd->score) { 138 + score = find_child_checks(wd->adev, wd->check_children); 139 + if (score == FIND_CHILD_MAX_SCORE) 140 + return 1; 141 + 142 + wd->score = score; 143 + } 144 + /* 145 + * Second, if the object that has just been found has a better score, 146 + * replace the previously found one with it and terminate the walk if 147 + * the new score is maximum. 148 + */ 149 + score = find_child_checks(adev, wd->check_children); 150 + if (score > wd->score) { 151 + wd->adev = adev; 152 + if (score == FIND_CHILD_MAX_SCORE) 153 + return 1; 154 + 155 + wd->score = score; 156 + } 157 + 158 + /* Continue, because there may be better matches. */ 159 + return 0; 160 + } 161 + 108 162 struct acpi_device *acpi_find_child_device(struct acpi_device *parent, 109 163 u64 address, bool check_children) 110 164 { 111 - struct acpi_device *adev, *ret = NULL; 112 - int ret_score = 0; 165 + struct find_child_walk_data wd = { 166 + .address = address, 167 + .check_children = check_children, 168 + .adev = NULL, 169 + .score = 0, 170 + }; 113 171 114 - if (!parent) 115 - return NULL; 172 + if (parent) 173 + acpi_dev_for_each_child(parent, check_one_child, &wd); 116 174 117 - list_for_each_entry(adev, &parent->children, node) { 118 - acpi_bus_address addr = acpi_device_adr(adev); 119 - int score; 120 - 121 - if (!adev->pnp.type.bus_address || addr != address) 122 - continue; 123 - 124 - if (!ret) { 125 - /* This is the first matching object. Save it. */ 126 - ret = adev; 127 - continue; 128 - } 129 - /* 130 - * There is more than one matching device object with the same 131 - * _ADR value. That really is unexpected, so we are kind of 132 - * beyond the scope of the spec here. We have to choose which 133 - * one to return, though. 134 - * 135 - * First, check if the previously found object is good enough 136 - * and return it if so. Second, do the same for the object that 137 - * we've just found. 138 - */ 139 - if (!ret_score) { 140 - ret_score = find_child_checks(ret, check_children); 141 - if (ret_score == FIND_CHILD_MAX_SCORE) 142 - return ret; 143 - } 144 - score = find_child_checks(adev, check_children); 145 - if (score == FIND_CHILD_MAX_SCORE) { 146 - return adev; 147 - } else if (score > ret_score) { 148 - ret = adev; 149 - ret_score = score; 150 - } 151 - } 152 - return ret; 175 + return wd.adev; 153 176 } 154 177 EXPORT_SYMBOL_GPL(acpi_find_child_device); 155 178