Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 Google, Inc |
| 3 | * |
| 4 | * (C) Copyright 2012 |
| 5 | * Pavel Herrmann <morpheus.ibis@gmail.com> |
| 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 12 | #include <errno.h> |
| 13 | #include <malloc.h> |
| 14 | #include <dm/device.h> |
| 15 | #include <dm/device-internal.h> |
| 16 | #include <dm/lists.h> |
| 17 | #include <dm/uclass.h> |
| 18 | #include <dm/uclass-internal.h> |
| 19 | #include <dm/util.h> |
| 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
| 23 | struct uclass *uclass_find(enum uclass_id key) |
| 24 | { |
| 25 | struct uclass *uc; |
| 26 | |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 27 | if (!gd->dm_root) |
| 28 | return NULL; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 29 | /* |
| 30 | * TODO(sjg@chromium.org): Optimise this, perhaps moving the found |
| 31 | * node to the start of the list, or creating a linear array mapping |
| 32 | * id to node. |
| 33 | */ |
| 34 | list_for_each_entry(uc, &gd->uclass_root, sibling_node) { |
| 35 | if (uc->uc_drv->id == key) |
| 36 | return uc; |
| 37 | } |
| 38 | |
| 39 | return NULL; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * uclass_add() - Create new uclass in list |
| 44 | * @id: Id number to create |
| 45 | * @ucp: Returns pointer to uclass, or NULL on error |
| 46 | * @return 0 on success, -ve on error |
| 47 | * |
| 48 | * The new uclass is added to the list. There must be only one uclass for |
| 49 | * each id. |
| 50 | */ |
| 51 | static int uclass_add(enum uclass_id id, struct uclass **ucp) |
| 52 | { |
| 53 | struct uclass_driver *uc_drv; |
| 54 | struct uclass *uc; |
| 55 | int ret; |
| 56 | |
| 57 | *ucp = NULL; |
| 58 | uc_drv = lists_uclass_lookup(id); |
| 59 | if (!uc_drv) { |
Masahiro Yamada | 1d06ced | 2015-07-07 18:51:32 +0900 | [diff] [blame] | 60 | debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n", |
| 61 | id); |
Simon Glass | 43313de | 2015-08-30 16:55:16 -0600 | [diff] [blame] | 62 | /* |
| 63 | * Use a strange error to make this case easier to find. When |
| 64 | * a uclass is not available it can prevent driver model from |
| 65 | * starting up and this failure is otherwise hard to debug. |
| 66 | */ |
| 67 | return -EPFNOSUPPORT; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 68 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 69 | uc = calloc(1, sizeof(*uc)); |
| 70 | if (!uc) |
| 71 | return -ENOMEM; |
| 72 | if (uc_drv->priv_auto_alloc_size) { |
| 73 | uc->priv = calloc(1, uc_drv->priv_auto_alloc_size); |
| 74 | if (!uc->priv) { |
| 75 | ret = -ENOMEM; |
| 76 | goto fail_mem; |
| 77 | } |
| 78 | } |
| 79 | uc->uc_drv = uc_drv; |
| 80 | INIT_LIST_HEAD(&uc->sibling_node); |
| 81 | INIT_LIST_HEAD(&uc->dev_head); |
Simon Glass | 34a1d35 | 2014-06-11 23:29:49 -0600 | [diff] [blame] | 82 | list_add(&uc->sibling_node, &DM_UCLASS_ROOT_NON_CONST); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 83 | |
| 84 | if (uc_drv->init) { |
| 85 | ret = uc_drv->init(uc); |
| 86 | if (ret) |
| 87 | goto fail; |
| 88 | } |
| 89 | |
| 90 | *ucp = uc; |
| 91 | |
| 92 | return 0; |
| 93 | fail: |
| 94 | if (uc_drv->priv_auto_alloc_size) { |
| 95 | free(uc->priv); |
| 96 | uc->priv = NULL; |
| 97 | } |
| 98 | list_del(&uc->sibling_node); |
| 99 | fail_mem: |
| 100 | free(uc); |
| 101 | |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | int uclass_destroy(struct uclass *uc) |
| 106 | { |
| 107 | struct uclass_driver *uc_drv; |
Simon Glass | f33663d | 2015-04-19 07:20:58 -0600 | [diff] [blame] | 108 | struct udevice *dev; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 109 | int ret; |
| 110 | |
Simon Glass | f33663d | 2015-04-19 07:20:58 -0600 | [diff] [blame] | 111 | /* |
| 112 | * We cannot use list_for_each_entry_safe() here. If a device in this |
| 113 | * uclass has a child device also in this uclass, it will be also be |
| 114 | * unbound (by the recursion in the call to device_unbind() below). |
| 115 | * We can loop until the list is empty. |
| 116 | */ |
| 117 | while (!list_empty(&uc->dev_head)) { |
| 118 | dev = list_first_entry(&uc->dev_head, struct udevice, |
| 119 | uclass_node); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 120 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 121 | if (ret) |
| 122 | return ret; |
| 123 | ret = device_unbind(dev); |
| 124 | if (ret) |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | uc_drv = uc->uc_drv; |
| 129 | if (uc_drv->destroy) |
| 130 | uc_drv->destroy(uc); |
| 131 | list_del(&uc->sibling_node); |
| 132 | if (uc_drv->priv_auto_alloc_size) |
| 133 | free(uc->priv); |
| 134 | free(uc); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | int uclass_get(enum uclass_id id, struct uclass **ucp) |
| 140 | { |
| 141 | struct uclass *uc; |
| 142 | |
| 143 | *ucp = NULL; |
| 144 | uc = uclass_find(id); |
| 145 | if (!uc) |
| 146 | return uclass_add(id, ucp); |
| 147 | *ucp = uc; |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
Simon Glass | d19d073 | 2016-10-05 20:42:13 -0600 | [diff] [blame] | 152 | const char *uclass_get_name(enum uclass_id id) |
| 153 | { |
| 154 | struct uclass *uc; |
| 155 | |
| 156 | if (uclass_get(id, &uc)) |
| 157 | return NULL; |
| 158 | return uc->uc_drv->name; |
| 159 | } |
| 160 | |
Simon Glass | 70e35b4 | 2017-12-28 13:14:15 -0700 | [diff] [blame] | 161 | enum uclass_id uclass_get_by_name(const char *name) |
| 162 | { |
| 163 | int i; |
| 164 | |
| 165 | for (i = 0; i < UCLASS_COUNT; i++) { |
| 166 | struct uclass_driver *uc_drv = lists_uclass_lookup(i); |
| 167 | |
| 168 | if (uc_drv && !strcmp(uc_drv->name, name)) |
| 169 | return i; |
| 170 | } |
| 171 | |
| 172 | return UCLASS_INVALID; |
| 173 | } |
| 174 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 175 | int uclass_find_device(enum uclass_id id, int index, struct udevice **devp) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 176 | { |
| 177 | struct uclass *uc; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 178 | struct udevice *dev; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 179 | int ret; |
| 180 | |
| 181 | *devp = NULL; |
| 182 | ret = uclass_get(id, &uc); |
| 183 | if (ret) |
| 184 | return ret; |
Simon Glass | 46766df | 2015-07-31 09:31:19 -0600 | [diff] [blame] | 185 | if (list_empty(&uc->dev_head)) |
| 186 | return -ENODEV; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 187 | |
| 188 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
| 189 | if (!index--) { |
| 190 | *devp = dev; |
| 191 | return 0; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return -ENODEV; |
| 196 | } |
| 197 | |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 198 | int uclass_find_first_device(enum uclass_id id, struct udevice **devp) |
| 199 | { |
| 200 | struct uclass *uc; |
| 201 | int ret; |
| 202 | |
| 203 | *devp = NULL; |
| 204 | ret = uclass_get(id, &uc); |
| 205 | if (ret) |
| 206 | return ret; |
| 207 | if (list_empty(&uc->dev_head)) |
| 208 | return 0; |
| 209 | |
| 210 | *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node); |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | int uclass_find_next_device(struct udevice **devp) |
| 216 | { |
| 217 | struct udevice *dev = *devp; |
| 218 | |
| 219 | *devp = NULL; |
| 220 | if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head)) |
| 221 | return 0; |
| 222 | |
| 223 | *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node); |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
Przemyslaw Marczak | 2ffdf14 | 2015-04-15 13:07:22 +0200 | [diff] [blame] | 228 | int uclass_find_device_by_name(enum uclass_id id, const char *name, |
| 229 | struct udevice **devp) |
| 230 | { |
| 231 | struct uclass *uc; |
| 232 | struct udevice *dev; |
| 233 | int ret; |
| 234 | |
| 235 | *devp = NULL; |
| 236 | if (!name) |
| 237 | return -EINVAL; |
| 238 | ret = uclass_get(id, &uc); |
| 239 | if (ret) |
| 240 | return ret; |
| 241 | |
| 242 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
| 243 | if (!strncmp(dev->name, name, strlen(name))) { |
| 244 | *devp = dev; |
| 245 | return 0; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | return -ENODEV; |
| 250 | } |
| 251 | |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 252 | int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq, |
| 253 | bool find_req_seq, struct udevice **devp) |
| 254 | { |
| 255 | struct uclass *uc; |
| 256 | struct udevice *dev; |
| 257 | int ret; |
| 258 | |
| 259 | *devp = NULL; |
| 260 | debug("%s: %d %d\n", __func__, find_req_seq, seq_or_req_seq); |
| 261 | if (seq_or_req_seq == -1) |
| 262 | return -ENODEV; |
| 263 | ret = uclass_get(id, &uc); |
| 264 | if (ret) |
| 265 | return ret; |
| 266 | |
| 267 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
Alexandru Gagniuc | 4490f07 | 2017-04-04 10:46:56 -0700 | [diff] [blame] | 268 | debug(" - %d %d '%s'\n", dev->req_seq, dev->seq, dev->name); |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 269 | if ((find_req_seq ? dev->req_seq : dev->seq) == |
| 270 | seq_or_req_seq) { |
| 271 | *devp = dev; |
| 272 | debug(" - found\n"); |
| 273 | return 0; |
| 274 | } |
| 275 | } |
| 276 | debug(" - not found\n"); |
| 277 | |
| 278 | return -ENODEV; |
| 279 | } |
| 280 | |
Simon Glass | 96f0444 | 2016-01-21 19:43:57 -0700 | [diff] [blame] | 281 | int uclass_find_device_by_of_offset(enum uclass_id id, int node, |
| 282 | struct udevice **devp) |
Simon Glass | c1464ab | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 283 | { |
| 284 | struct uclass *uc; |
| 285 | struct udevice *dev; |
| 286 | int ret; |
| 287 | |
| 288 | *devp = NULL; |
| 289 | if (node < 0) |
| 290 | return -ENODEV; |
| 291 | ret = uclass_get(id, &uc); |
| 292 | if (ret) |
| 293 | return ret; |
| 294 | |
| 295 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 296 | if (dev_of_offset(dev) == node) { |
Simon Glass | c1464ab | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 297 | *devp = dev; |
| 298 | return 0; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | return -ENODEV; |
| 303 | } |
| 304 | |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 305 | int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node, |
| 306 | struct udevice **devp) |
| 307 | { |
| 308 | struct uclass *uc; |
| 309 | struct udevice *dev; |
| 310 | int ret; |
| 311 | |
| 312 | *devp = NULL; |
| 313 | if (!ofnode_valid(node)) |
| 314 | return -ENODEV; |
| 315 | ret = uclass_get(id, &uc); |
| 316 | if (ret) |
| 317 | return ret; |
| 318 | |
| 319 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
| 320 | if (ofnode_equal(dev_ofnode(dev), node)) { |
| 321 | *devp = dev; |
| 322 | return 0; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | return -ENODEV; |
| 327 | } |
| 328 | |
Simon Glass | 359c487 | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 329 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 330 | static int uclass_find_device_by_phandle(enum uclass_id id, |
| 331 | struct udevice *parent, |
| 332 | const char *name, |
| 333 | struct udevice **devp) |
| 334 | { |
| 335 | struct udevice *dev; |
| 336 | struct uclass *uc; |
| 337 | int find_phandle; |
| 338 | int ret; |
| 339 | |
| 340 | *devp = NULL; |
Simon Glass | bfb6e3d | 2017-05-18 20:09:12 -0600 | [diff] [blame] | 341 | find_phandle = dev_read_u32_default(parent, name, -1); |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 342 | if (find_phandle <= 0) |
| 343 | return -ENOENT; |
| 344 | ret = uclass_get(id, &uc); |
| 345 | if (ret) |
| 346 | return ret; |
| 347 | |
| 348 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 349 | uint phandle; |
| 350 | |
Simon Glass | bfb6e3d | 2017-05-18 20:09:12 -0600 | [diff] [blame] | 351 | phandle = dev_read_phandle(dev); |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 352 | |
| 353 | if (phandle == find_phandle) { |
| 354 | *devp = dev; |
| 355 | return 0; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | return -ENODEV; |
| 360 | } |
Simon Glass | 359c487 | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 361 | #endif |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 362 | |
Simon Glass | 32d8ab6 | 2016-07-17 15:23:15 -0600 | [diff] [blame] | 363 | int uclass_get_device_by_driver(enum uclass_id id, |
| 364 | const struct driver *find_drv, |
| 365 | struct udevice **devp) |
| 366 | { |
| 367 | struct udevice *dev; |
| 368 | struct uclass *uc; |
| 369 | int ret; |
| 370 | |
| 371 | ret = uclass_get(id, &uc); |
| 372 | if (ret) |
| 373 | return ret; |
| 374 | |
| 375 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
| 376 | if (dev->driver == find_drv) |
| 377 | return uclass_get_device_tail(dev, 0, devp); |
| 378 | } |
| 379 | |
| 380 | return -ENODEV; |
| 381 | } |
| 382 | |
Simon Glass | 98c9fb0 | 2017-04-23 20:10:43 -0600 | [diff] [blame] | 383 | int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 384 | { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 385 | if (ret) |
| 386 | return ret; |
| 387 | |
Simon Glass | 46cc8ba9 | 2015-04-24 22:33:07 -0600 | [diff] [blame] | 388 | assert(dev); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 389 | ret = device_probe(dev); |
| 390 | if (ret) |
| 391 | return ret; |
| 392 | |
| 393 | *devp = dev; |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
Simon Glass | 4b67cef | 2014-07-23 06:55:08 -0600 | [diff] [blame] | 398 | int uclass_get_device(enum uclass_id id, int index, struct udevice **devp) |
| 399 | { |
| 400 | struct udevice *dev; |
| 401 | int ret; |
| 402 | |
| 403 | *devp = NULL; |
| 404 | ret = uclass_find_device(id, index, &dev); |
| 405 | return uclass_get_device_tail(dev, ret, devp); |
| 406 | } |
| 407 | |
Przemyslaw Marczak | a12a1f5 | 2015-04-15 13:07:23 +0200 | [diff] [blame] | 408 | int uclass_get_device_by_name(enum uclass_id id, const char *name, |
| 409 | struct udevice **devp) |
| 410 | { |
| 411 | struct udevice *dev; |
| 412 | int ret; |
| 413 | |
| 414 | *devp = NULL; |
| 415 | ret = uclass_find_device_by_name(id, name, &dev); |
| 416 | return uclass_get_device_tail(dev, ret, devp); |
| 417 | } |
| 418 | |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 419 | int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp) |
| 420 | { |
| 421 | struct udevice *dev; |
| 422 | int ret; |
| 423 | |
| 424 | *devp = NULL; |
| 425 | ret = uclass_find_device_by_seq(id, seq, false, &dev); |
| 426 | if (ret == -ENODEV) { |
| 427 | /* |
| 428 | * We didn't find it in probed devices. See if there is one |
| 429 | * that will request this seq if probed. |
| 430 | */ |
| 431 | ret = uclass_find_device_by_seq(id, seq, true, &dev); |
| 432 | } |
| 433 | return uclass_get_device_tail(dev, ret, devp); |
| 434 | } |
| 435 | |
Simon Glass | c1464ab | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 436 | int uclass_get_device_by_of_offset(enum uclass_id id, int node, |
| 437 | struct udevice **devp) |
| 438 | { |
| 439 | struct udevice *dev; |
| 440 | int ret; |
| 441 | |
| 442 | *devp = NULL; |
| 443 | ret = uclass_find_device_by_of_offset(id, node, &dev); |
| 444 | return uclass_get_device_tail(dev, ret, devp); |
| 445 | } |
| 446 | |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 447 | int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node, |
| 448 | struct udevice **devp) |
| 449 | { |
| 450 | struct udevice *dev; |
| 451 | int ret; |
| 452 | |
| 453 | *devp = NULL; |
| 454 | ret = uclass_find_device_by_ofnode(id, node, &dev); |
| 455 | |
| 456 | return uclass_get_device_tail(dev, ret, devp); |
| 457 | } |
| 458 | |
Simon Glass | 359c487 | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 459 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Kever Yang | 66a0b5a | 2018-02-09 10:56:23 +0800 | [diff] [blame^] | 460 | int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id, |
| 461 | struct udevice **devp) |
| 462 | { |
| 463 | struct udevice *dev; |
| 464 | struct uclass *uc; |
| 465 | int ret; |
| 466 | |
| 467 | *devp = NULL; |
| 468 | ret = uclass_get(id, &uc); |
| 469 | if (ret) |
| 470 | return ret; |
| 471 | |
| 472 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
| 473 | uint phandle; |
| 474 | |
| 475 | phandle = dev_read_phandle(dev); |
| 476 | |
| 477 | if (phandle == phandle_id) { |
| 478 | *devp = dev; |
| 479 | return uclass_get_device_tail(dev, ret, devp); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | return -ENODEV; |
| 484 | } |
| 485 | |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 486 | int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent, |
| 487 | const char *name, struct udevice **devp) |
| 488 | { |
| 489 | struct udevice *dev; |
| 490 | int ret; |
| 491 | |
| 492 | *devp = NULL; |
| 493 | ret = uclass_find_device_by_phandle(id, parent, name, &dev); |
| 494 | return uclass_get_device_tail(dev, ret, devp); |
| 495 | } |
Simon Glass | 359c487 | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 496 | #endif |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 497 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 498 | int uclass_first_device(enum uclass_id id, struct udevice **devp) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 499 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 500 | struct udevice *dev; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 501 | int ret; |
| 502 | |
| 503 | *devp = NULL; |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 504 | ret = uclass_find_first_device(id, &dev); |
Simon Glass | 46cc8ba9 | 2015-04-24 22:33:07 -0600 | [diff] [blame] | 505 | if (!dev) |
| 506 | return 0; |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 507 | return uclass_get_device_tail(dev, ret, devp); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Simon Glass | 832c3f0 | 2016-02-11 13:23:25 -0700 | [diff] [blame] | 510 | int uclass_first_device_err(enum uclass_id id, struct udevice **devp) |
| 511 | { |
| 512 | int ret; |
| 513 | |
| 514 | ret = uclass_first_device(id, devp); |
| 515 | if (ret) |
| 516 | return ret; |
| 517 | else if (!*devp) |
| 518 | return -ENODEV; |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 523 | int uclass_next_device(struct udevice **devp) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 524 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 525 | struct udevice *dev = *devp; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 526 | int ret; |
| 527 | |
| 528 | *devp = NULL; |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 529 | ret = uclass_find_next_device(&dev); |
Simon Glass | 46cc8ba9 | 2015-04-24 22:33:07 -0600 | [diff] [blame] | 530 | if (!dev) |
| 531 | return 0; |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 532 | return uclass_get_device_tail(dev, ret, devp); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Simon Glass | 3ff83bc | 2017-04-23 20:10:45 -0600 | [diff] [blame] | 535 | int uclass_first_device_check(enum uclass_id id, struct udevice **devp) |
| 536 | { |
| 537 | int ret; |
| 538 | |
| 539 | *devp = NULL; |
| 540 | ret = uclass_find_first_device(id, devp); |
| 541 | if (ret) |
| 542 | return ret; |
| 543 | if (!*devp) |
| 544 | return 0; |
| 545 | |
| 546 | return device_probe(*devp); |
| 547 | } |
| 548 | |
| 549 | int uclass_next_device_check(struct udevice **devp) |
| 550 | { |
| 551 | int ret; |
| 552 | |
| 553 | ret = uclass_find_next_device(devp); |
| 554 | if (ret) |
| 555 | return ret; |
| 556 | if (!*devp) |
| 557 | return 0; |
| 558 | |
| 559 | return device_probe(*devp); |
| 560 | } |
| 561 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 562 | int uclass_bind_device(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 563 | { |
| 564 | struct uclass *uc; |
| 565 | int ret; |
| 566 | |
| 567 | uc = dev->uclass; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 568 | list_add_tail(&dev->uclass_node, &uc->dev_head); |
| 569 | |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 570 | if (dev->parent) { |
| 571 | struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv; |
| 572 | |
| 573 | if (uc_drv->child_post_bind) { |
| 574 | ret = uc_drv->child_post_bind(dev); |
| 575 | if (ret) |
| 576 | goto err; |
| 577 | } |
| 578 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 579 | |
| 580 | return 0; |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 581 | err: |
| 582 | /* There is no need to undo the parent's post_bind call */ |
| 583 | list_del(&dev->uclass_node); |
| 584 | |
| 585 | return ret; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Masahiro Yamada | 04aa00d | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 588 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 589 | int uclass_unbind_device(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 590 | { |
| 591 | struct uclass *uc; |
| 592 | int ret; |
| 593 | |
| 594 | uc = dev->uclass; |
| 595 | if (uc->uc_drv->pre_unbind) { |
| 596 | ret = uc->uc_drv->pre_unbind(dev); |
| 597 | if (ret) |
| 598 | return ret; |
| 599 | } |
| 600 | |
| 601 | list_del(&dev->uclass_node); |
| 602 | return 0; |
| 603 | } |
Simon Glass | 8914c8a | 2015-02-27 22:06:31 -0700 | [diff] [blame] | 604 | #endif |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 605 | |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 606 | int uclass_resolve_seq(struct udevice *dev) |
| 607 | { |
| 608 | struct udevice *dup; |
| 609 | int seq; |
| 610 | int ret; |
| 611 | |
| 612 | assert(dev->seq == -1); |
| 613 | ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq, |
| 614 | false, &dup); |
| 615 | if (!ret) { |
| 616 | dm_warn("Device '%s': seq %d is in use by '%s'\n", |
| 617 | dev->name, dev->req_seq, dup->name); |
| 618 | } else if (ret == -ENODEV) { |
| 619 | /* Our requested sequence number is available */ |
| 620 | if (dev->req_seq != -1) |
| 621 | return dev->req_seq; |
| 622 | } else { |
| 623 | return ret; |
| 624 | } |
| 625 | |
| 626 | for (seq = 0; seq < DM_MAX_SEQ; seq++) { |
| 627 | ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq, |
| 628 | false, &dup); |
| 629 | if (ret == -ENODEV) |
| 630 | break; |
| 631 | if (ret) |
| 632 | return ret; |
| 633 | } |
| 634 | return seq; |
| 635 | } |
| 636 | |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 637 | int uclass_pre_probe_device(struct udevice *dev) |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 638 | { |
| 639 | struct uclass_driver *uc_drv; |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 640 | int ret; |
| 641 | |
| 642 | uc_drv = dev->uclass->uc_drv; |
| 643 | if (uc_drv->pre_probe) { |
| 644 | ret = uc_drv->pre_probe(dev); |
| 645 | if (ret) |
| 646 | return ret; |
| 647 | } |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 648 | |
| 649 | if (!dev->parent) |
| 650 | return 0; |
| 651 | uc_drv = dev->parent->uclass->uc_drv; |
| 652 | if (uc_drv->child_pre_probe) |
| 653 | return uc_drv->child_pre_probe(dev); |
| 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 658 | int uclass_post_probe_device(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 659 | { |
| 660 | struct uclass_driver *uc_drv = dev->uclass->uc_drv; |
| 661 | |
| 662 | if (uc_drv->post_probe) |
| 663 | return uc_drv->post_probe(dev); |
| 664 | |
| 665 | return 0; |
| 666 | } |
| 667 | |
Masahiro Yamada | 04aa00d | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 668 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 669 | int uclass_pre_remove_device(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 670 | { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 671 | struct uclass *uc; |
| 672 | int ret; |
| 673 | |
| 674 | uc = dev->uclass; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 675 | if (uc->uc_drv->pre_remove) { |
| 676 | ret = uc->uc_drv->pre_remove(dev); |
| 677 | if (ret) |
| 678 | return ret; |
| 679 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 680 | |
| 681 | return 0; |
| 682 | } |
Simon Glass | 8914c8a | 2015-02-27 22:06:31 -0700 | [diff] [blame] | 683 | #endif |