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 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 161 | 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] | 162 | { |
| 163 | struct uclass *uc; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 164 | struct udevice *dev; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 165 | int ret; |
| 166 | |
| 167 | *devp = NULL; |
| 168 | ret = uclass_get(id, &uc); |
| 169 | if (ret) |
| 170 | return ret; |
Simon Glass | 46766df | 2015-07-31 09:31:19 -0600 | [diff] [blame] | 171 | if (list_empty(&uc->dev_head)) |
| 172 | return -ENODEV; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 173 | |
| 174 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
| 175 | if (!index--) { |
| 176 | *devp = dev; |
| 177 | return 0; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return -ENODEV; |
| 182 | } |
| 183 | |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 184 | int uclass_find_first_device(enum uclass_id id, struct udevice **devp) |
| 185 | { |
| 186 | struct uclass *uc; |
| 187 | int ret; |
| 188 | |
| 189 | *devp = NULL; |
| 190 | ret = uclass_get(id, &uc); |
| 191 | if (ret) |
| 192 | return ret; |
| 193 | if (list_empty(&uc->dev_head)) |
| 194 | return 0; |
| 195 | |
| 196 | *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node); |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | int uclass_find_next_device(struct udevice **devp) |
| 202 | { |
| 203 | struct udevice *dev = *devp; |
| 204 | |
| 205 | *devp = NULL; |
| 206 | if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head)) |
| 207 | return 0; |
| 208 | |
| 209 | *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Przemyslaw Marczak | 2ffdf14 | 2015-04-15 13:07:22 +0200 | [diff] [blame] | 214 | int uclass_find_device_by_name(enum uclass_id id, const char *name, |
| 215 | struct udevice **devp) |
| 216 | { |
| 217 | struct uclass *uc; |
| 218 | struct udevice *dev; |
| 219 | int ret; |
| 220 | |
| 221 | *devp = NULL; |
| 222 | if (!name) |
| 223 | return -EINVAL; |
| 224 | ret = uclass_get(id, &uc); |
| 225 | if (ret) |
| 226 | return ret; |
| 227 | |
| 228 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
| 229 | if (!strncmp(dev->name, name, strlen(name))) { |
| 230 | *devp = dev; |
| 231 | return 0; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return -ENODEV; |
| 236 | } |
| 237 | |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 238 | int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq, |
| 239 | bool find_req_seq, struct udevice **devp) |
| 240 | { |
| 241 | struct uclass *uc; |
| 242 | struct udevice *dev; |
| 243 | int ret; |
| 244 | |
| 245 | *devp = NULL; |
| 246 | debug("%s: %d %d\n", __func__, find_req_seq, seq_or_req_seq); |
| 247 | if (seq_or_req_seq == -1) |
| 248 | return -ENODEV; |
| 249 | ret = uclass_get(id, &uc); |
| 250 | if (ret) |
| 251 | return ret; |
| 252 | |
| 253 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
Alexandru Gagniuc | 4490f07 | 2017-04-04 10:46:56 -0700 | [diff] [blame] | 254 | debug(" - %d %d '%s'\n", dev->req_seq, dev->seq, dev->name); |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 255 | if ((find_req_seq ? dev->req_seq : dev->seq) == |
| 256 | seq_or_req_seq) { |
| 257 | *devp = dev; |
| 258 | debug(" - found\n"); |
| 259 | return 0; |
| 260 | } |
| 261 | } |
| 262 | debug(" - not found\n"); |
| 263 | |
| 264 | return -ENODEV; |
| 265 | } |
| 266 | |
Simon Glass | 96f0444 | 2016-01-21 19:43:57 -0700 | [diff] [blame] | 267 | int uclass_find_device_by_of_offset(enum uclass_id id, int node, |
| 268 | struct udevice **devp) |
Simon Glass | c1464ab | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 269 | { |
| 270 | struct uclass *uc; |
| 271 | struct udevice *dev; |
| 272 | int ret; |
| 273 | |
| 274 | *devp = NULL; |
| 275 | if (node < 0) |
| 276 | return -ENODEV; |
| 277 | ret = uclass_get(id, &uc); |
| 278 | if (ret) |
| 279 | return ret; |
| 280 | |
| 281 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 282 | if (dev_of_offset(dev) == node) { |
Simon Glass | c1464ab | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 283 | *devp = dev; |
| 284 | return 0; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return -ENODEV; |
| 289 | } |
| 290 | |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 291 | int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node, |
| 292 | struct udevice **devp) |
| 293 | { |
| 294 | struct uclass *uc; |
| 295 | struct udevice *dev; |
| 296 | int ret; |
| 297 | |
| 298 | *devp = NULL; |
| 299 | if (!ofnode_valid(node)) |
| 300 | return -ENODEV; |
| 301 | ret = uclass_get(id, &uc); |
| 302 | if (ret) |
| 303 | return ret; |
| 304 | |
| 305 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
| 306 | if (ofnode_equal(dev_ofnode(dev), node)) { |
| 307 | *devp = dev; |
| 308 | return 0; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | return -ENODEV; |
| 313 | } |
| 314 | |
Simon Glass | 359c487 | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 315 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 316 | static int uclass_find_device_by_phandle(enum uclass_id id, |
| 317 | struct udevice *parent, |
| 318 | const char *name, |
| 319 | struct udevice **devp) |
| 320 | { |
| 321 | struct udevice *dev; |
| 322 | struct uclass *uc; |
| 323 | int find_phandle; |
| 324 | int ret; |
| 325 | |
| 326 | *devp = NULL; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 327 | find_phandle = fdtdec_get_int(gd->fdt_blob, dev_of_offset(parent), name, |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 328 | -1); |
| 329 | if (find_phandle <= 0) |
| 330 | return -ENOENT; |
| 331 | ret = uclass_get(id, &uc); |
| 332 | if (ret) |
| 333 | return ret; |
| 334 | |
| 335 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 336 | uint phandle; |
| 337 | |
| 338 | phandle = fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev)); |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 339 | |
| 340 | if (phandle == find_phandle) { |
| 341 | *devp = dev; |
| 342 | return 0; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | return -ENODEV; |
| 347 | } |
Simon Glass | 359c487 | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 348 | #endif |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 349 | |
Simon Glass | 32d8ab6 | 2016-07-17 15:23:15 -0600 | [diff] [blame] | 350 | int uclass_get_device_by_driver(enum uclass_id id, |
| 351 | const struct driver *find_drv, |
| 352 | struct udevice **devp) |
| 353 | { |
| 354 | struct udevice *dev; |
| 355 | struct uclass *uc; |
| 356 | int ret; |
| 357 | |
| 358 | ret = uclass_get(id, &uc); |
| 359 | if (ret) |
| 360 | return ret; |
| 361 | |
| 362 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
| 363 | if (dev->driver == find_drv) |
| 364 | return uclass_get_device_tail(dev, 0, devp); |
| 365 | } |
| 366 | |
| 367 | return -ENODEV; |
| 368 | } |
| 369 | |
Przemyslaw Marczak | fa277fc | 2015-04-20 13:32:32 +0200 | [diff] [blame] | 370 | int uclass_get_device_tail(struct udevice *dev, int ret, |
Simon Glass | 4b67cef | 2014-07-23 06:55:08 -0600 | [diff] [blame] | 371 | struct udevice **devp) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 372 | { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 373 | if (ret) |
| 374 | return ret; |
| 375 | |
Simon Glass | 46cc8ba9 | 2015-04-24 22:33:07 -0600 | [diff] [blame] | 376 | assert(dev); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 377 | ret = device_probe(dev); |
| 378 | if (ret) |
| 379 | return ret; |
| 380 | |
| 381 | *devp = dev; |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
Simon Glass | 4b67cef | 2014-07-23 06:55:08 -0600 | [diff] [blame] | 386 | int uclass_get_device(enum uclass_id id, int index, struct udevice **devp) |
| 387 | { |
| 388 | struct udevice *dev; |
| 389 | int ret; |
| 390 | |
| 391 | *devp = NULL; |
| 392 | ret = uclass_find_device(id, index, &dev); |
| 393 | return uclass_get_device_tail(dev, ret, devp); |
| 394 | } |
| 395 | |
Przemyslaw Marczak | a12a1f5 | 2015-04-15 13:07:23 +0200 | [diff] [blame] | 396 | int uclass_get_device_by_name(enum uclass_id id, const char *name, |
| 397 | struct udevice **devp) |
| 398 | { |
| 399 | struct udevice *dev; |
| 400 | int ret; |
| 401 | |
| 402 | *devp = NULL; |
| 403 | ret = uclass_find_device_by_name(id, name, &dev); |
| 404 | return uclass_get_device_tail(dev, ret, devp); |
| 405 | } |
| 406 | |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 407 | int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp) |
| 408 | { |
| 409 | struct udevice *dev; |
| 410 | int ret; |
| 411 | |
| 412 | *devp = NULL; |
| 413 | ret = uclass_find_device_by_seq(id, seq, false, &dev); |
| 414 | if (ret == -ENODEV) { |
| 415 | /* |
| 416 | * We didn't find it in probed devices. See if there is one |
| 417 | * that will request this seq if probed. |
| 418 | */ |
| 419 | ret = uclass_find_device_by_seq(id, seq, true, &dev); |
| 420 | } |
| 421 | return uclass_get_device_tail(dev, ret, devp); |
| 422 | } |
| 423 | |
Simon Glass | c1464ab | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 424 | int uclass_get_device_by_of_offset(enum uclass_id id, int node, |
| 425 | struct udevice **devp) |
| 426 | { |
| 427 | struct udevice *dev; |
| 428 | int ret; |
| 429 | |
| 430 | *devp = NULL; |
| 431 | ret = uclass_find_device_by_of_offset(id, node, &dev); |
| 432 | return uclass_get_device_tail(dev, ret, devp); |
| 433 | } |
| 434 | |
Simon Glass | ee145d6 | 2017-05-18 20:09:09 -0600 | [diff] [blame] | 435 | int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node, |
| 436 | struct udevice **devp) |
| 437 | { |
| 438 | struct udevice *dev; |
| 439 | int ret; |
| 440 | |
| 441 | *devp = NULL; |
| 442 | ret = uclass_find_device_by_ofnode(id, node, &dev); |
| 443 | |
| 444 | return uclass_get_device_tail(dev, ret, devp); |
| 445 | } |
| 446 | |
Simon Glass | 359c487 | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 447 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 448 | int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent, |
| 449 | const char *name, struct udevice **devp) |
| 450 | { |
| 451 | struct udevice *dev; |
| 452 | int ret; |
| 453 | |
| 454 | *devp = NULL; |
| 455 | ret = uclass_find_device_by_phandle(id, parent, name, &dev); |
| 456 | return uclass_get_device_tail(dev, ret, devp); |
| 457 | } |
Simon Glass | 359c487 | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 458 | #endif |
Simon Glass | 75f00df | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 459 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 460 | int uclass_first_device(enum uclass_id id, struct udevice **devp) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 461 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 462 | struct udevice *dev; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 463 | int ret; |
| 464 | |
| 465 | *devp = NULL; |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 466 | ret = uclass_find_first_device(id, &dev); |
Simon Glass | 46cc8ba9 | 2015-04-24 22:33:07 -0600 | [diff] [blame] | 467 | if (!dev) |
| 468 | return 0; |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 469 | return uclass_get_device_tail(dev, ret, devp); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Simon Glass | 832c3f0 | 2016-02-11 13:23:25 -0700 | [diff] [blame] | 472 | int uclass_first_device_err(enum uclass_id id, struct udevice **devp) |
| 473 | { |
| 474 | int ret; |
| 475 | |
| 476 | ret = uclass_first_device(id, devp); |
| 477 | if (ret) |
| 478 | return ret; |
| 479 | else if (!*devp) |
| 480 | return -ENODEV; |
| 481 | |
| 482 | return 0; |
| 483 | } |
| 484 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 485 | int uclass_next_device(struct udevice **devp) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 486 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 487 | struct udevice *dev = *devp; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 488 | int ret; |
| 489 | |
| 490 | *devp = NULL; |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 491 | ret = uclass_find_next_device(&dev); |
Simon Glass | 46cc8ba9 | 2015-04-24 22:33:07 -0600 | [diff] [blame] | 492 | if (!dev) |
| 493 | return 0; |
Przemyslaw Marczak | f9d156e | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 494 | return uclass_get_device_tail(dev, ret, devp); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 497 | int uclass_bind_device(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 498 | { |
| 499 | struct uclass *uc; |
| 500 | int ret; |
| 501 | |
| 502 | uc = dev->uclass; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 503 | list_add_tail(&dev->uclass_node, &uc->dev_head); |
| 504 | |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 505 | if (dev->parent) { |
| 506 | struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv; |
| 507 | |
| 508 | if (uc_drv->child_post_bind) { |
| 509 | ret = uc_drv->child_post_bind(dev); |
| 510 | if (ret) |
| 511 | goto err; |
| 512 | } |
| 513 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 514 | |
| 515 | return 0; |
Simon Glass | f4c9b3e | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 516 | err: |
| 517 | /* There is no need to undo the parent's post_bind call */ |
| 518 | list_del(&dev->uclass_node); |
| 519 | |
| 520 | return ret; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Masahiro Yamada | 04aa00d | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 523 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 524 | int uclass_unbind_device(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 525 | { |
| 526 | struct uclass *uc; |
| 527 | int ret; |
| 528 | |
| 529 | uc = dev->uclass; |
| 530 | if (uc->uc_drv->pre_unbind) { |
| 531 | ret = uc->uc_drv->pre_unbind(dev); |
| 532 | if (ret) |
| 533 | return ret; |
| 534 | } |
| 535 | |
| 536 | list_del(&dev->uclass_node); |
| 537 | return 0; |
| 538 | } |
Simon Glass | 8914c8a | 2015-02-27 22:06:31 -0700 | [diff] [blame] | 539 | #endif |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 540 | |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 541 | int uclass_resolve_seq(struct udevice *dev) |
| 542 | { |
| 543 | struct udevice *dup; |
| 544 | int seq; |
| 545 | int ret; |
| 546 | |
| 547 | assert(dev->seq == -1); |
| 548 | ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq, |
| 549 | false, &dup); |
| 550 | if (!ret) { |
| 551 | dm_warn("Device '%s': seq %d is in use by '%s'\n", |
| 552 | dev->name, dev->req_seq, dup->name); |
| 553 | } else if (ret == -ENODEV) { |
| 554 | /* Our requested sequence number is available */ |
| 555 | if (dev->req_seq != -1) |
| 556 | return dev->req_seq; |
| 557 | } else { |
| 558 | return ret; |
| 559 | } |
| 560 | |
| 561 | for (seq = 0; seq < DM_MAX_SEQ; seq++) { |
| 562 | ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq, |
| 563 | false, &dup); |
| 564 | if (ret == -ENODEV) |
| 565 | break; |
| 566 | if (ret) |
| 567 | return ret; |
| 568 | } |
| 569 | return seq; |
| 570 | } |
| 571 | |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 572 | int uclass_pre_probe_device(struct udevice *dev) |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 573 | { |
| 574 | struct uclass_driver *uc_drv; |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 575 | int ret; |
| 576 | |
| 577 | uc_drv = dev->uclass->uc_drv; |
| 578 | if (uc_drv->pre_probe) { |
| 579 | ret = uc_drv->pre_probe(dev); |
| 580 | if (ret) |
| 581 | return ret; |
| 582 | } |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 583 | |
| 584 | if (!dev->parent) |
| 585 | return 0; |
| 586 | uc_drv = dev->parent->uclass->uc_drv; |
| 587 | if (uc_drv->child_pre_probe) |
| 588 | return uc_drv->child_pre_probe(dev); |
| 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 593 | int uclass_post_probe_device(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 594 | { |
| 595 | struct uclass_driver *uc_drv = dev->uclass->uc_drv; |
| 596 | |
| 597 | if (uc_drv->post_probe) |
| 598 | return uc_drv->post_probe(dev); |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
Masahiro Yamada | 04aa00d | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 603 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 604 | int uclass_pre_remove_device(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 605 | { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 606 | struct uclass *uc; |
| 607 | int ret; |
| 608 | |
| 609 | uc = dev->uclass; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 610 | if (uc->uc_drv->pre_remove) { |
| 611 | ret = uc->uc_drv->pre_remove(dev); |
| 612 | if (ret) |
| 613 | return ret; |
| 614 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 615 | |
| 616 | return 0; |
| 617 | } |
Simon Glass | 8914c8a | 2015-02-27 22:06:31 -0700 | [diff] [blame] | 618 | #endif |