Simon Glass | 6494d70 | 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> |
| 11 | #include <errno.h> |
| 12 | #include <malloc.h> |
| 13 | #include <dm/device.h> |
| 14 | #include <dm/device-internal.h> |
| 15 | #include <dm/lists.h> |
| 16 | #include <dm/uclass.h> |
| 17 | #include <dm/uclass-internal.h> |
| 18 | #include <dm/util.h> |
| 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
| 22 | struct uclass *uclass_find(enum uclass_id key) |
| 23 | { |
| 24 | struct uclass *uc; |
| 25 | |
Simon Glass | c910e2e | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 26 | if (!gd->dm_root) |
| 27 | return NULL; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 28 | /* |
| 29 | * TODO(sjg@chromium.org): Optimise this, perhaps moving the found |
| 30 | * node to the start of the list, or creating a linear array mapping |
| 31 | * id to node. |
| 32 | */ |
| 33 | list_for_each_entry(uc, &gd->uclass_root, sibling_node) { |
| 34 | if (uc->uc_drv->id == key) |
| 35 | return uc; |
| 36 | } |
| 37 | |
| 38 | return NULL; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * uclass_add() - Create new uclass in list |
| 43 | * @id: Id number to create |
| 44 | * @ucp: Returns pointer to uclass, or NULL on error |
| 45 | * @return 0 on success, -ve on error |
| 46 | * |
| 47 | * The new uclass is added to the list. There must be only one uclass for |
| 48 | * each id. |
| 49 | */ |
| 50 | static int uclass_add(enum uclass_id id, struct uclass **ucp) |
| 51 | { |
| 52 | struct uclass_driver *uc_drv; |
| 53 | struct uclass *uc; |
| 54 | int ret; |
| 55 | |
| 56 | *ucp = NULL; |
| 57 | uc_drv = lists_uclass_lookup(id); |
| 58 | if (!uc_drv) { |
Masahiro Yamada | 643e690 | 2015-07-07 18:51:32 +0900 | [diff] [blame] | 59 | debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n", |
| 60 | id); |
Simon Glass | 3346c87 | 2015-08-30 16:55:16 -0600 | [diff] [blame] | 61 | /* |
| 62 | * Use a strange error to make this case easier to find. When |
| 63 | * a uclass is not available it can prevent driver model from |
| 64 | * starting up and this failure is otherwise hard to debug. |
| 65 | */ |
| 66 | return -EPFNOSUPPORT; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 67 | } |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 68 | uc = calloc(1, sizeof(*uc)); |
| 69 | if (!uc) |
| 70 | return -ENOMEM; |
| 71 | if (uc_drv->priv_auto_alloc_size) { |
| 72 | uc->priv = calloc(1, uc_drv->priv_auto_alloc_size); |
| 73 | if (!uc->priv) { |
| 74 | ret = -ENOMEM; |
| 75 | goto fail_mem; |
| 76 | } |
| 77 | } |
| 78 | uc->uc_drv = uc_drv; |
| 79 | INIT_LIST_HEAD(&uc->sibling_node); |
| 80 | INIT_LIST_HEAD(&uc->dev_head); |
Simon Glass | 89876a5 | 2014-06-11 23:29:49 -0600 | [diff] [blame] | 81 | list_add(&uc->sibling_node, &DM_UCLASS_ROOT_NON_CONST); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 82 | |
| 83 | if (uc_drv->init) { |
| 84 | ret = uc_drv->init(uc); |
| 85 | if (ret) |
| 86 | goto fail; |
| 87 | } |
| 88 | |
| 89 | *ucp = uc; |
| 90 | |
| 91 | return 0; |
| 92 | fail: |
| 93 | if (uc_drv->priv_auto_alloc_size) { |
| 94 | free(uc->priv); |
| 95 | uc->priv = NULL; |
| 96 | } |
| 97 | list_del(&uc->sibling_node); |
| 98 | fail_mem: |
| 99 | free(uc); |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | int uclass_destroy(struct uclass *uc) |
| 105 | { |
| 106 | struct uclass_driver *uc_drv; |
Simon Glass | 07d260e | 2015-04-19 07:20:58 -0600 | [diff] [blame] | 107 | struct udevice *dev; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 108 | int ret; |
| 109 | |
Simon Glass | 07d260e | 2015-04-19 07:20:58 -0600 | [diff] [blame] | 110 | /* |
| 111 | * We cannot use list_for_each_entry_safe() here. If a device in this |
| 112 | * uclass has a child device also in this uclass, it will be also be |
| 113 | * unbound (by the recursion in the call to device_unbind() below). |
| 114 | * We can loop until the list is empty. |
| 115 | */ |
| 116 | while (!list_empty(&uc->dev_head)) { |
| 117 | dev = list_first_entry(&uc->dev_head, struct udevice, |
| 118 | uclass_node); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 119 | ret = device_remove(dev); |
| 120 | if (ret) |
| 121 | return ret; |
| 122 | ret = device_unbind(dev); |
| 123 | if (ret) |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | uc_drv = uc->uc_drv; |
| 128 | if (uc_drv->destroy) |
| 129 | uc_drv->destroy(uc); |
| 130 | list_del(&uc->sibling_node); |
| 131 | if (uc_drv->priv_auto_alloc_size) |
| 132 | free(uc->priv); |
| 133 | free(uc); |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | int uclass_get(enum uclass_id id, struct uclass **ucp) |
| 139 | { |
| 140 | struct uclass *uc; |
| 141 | |
| 142 | *ucp = NULL; |
| 143 | uc = uclass_find(id); |
| 144 | if (!uc) |
| 145 | return uclass_add(id, ucp); |
| 146 | *ucp = uc; |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 151 | int uclass_find_device(enum uclass_id id, int index, struct udevice **devp) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 152 | { |
| 153 | struct uclass *uc; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 154 | struct udevice *dev; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 155 | int ret; |
| 156 | |
| 157 | *devp = NULL; |
| 158 | ret = uclass_get(id, &uc); |
| 159 | if (ret) |
| 160 | return ret; |
Simon Glass | 2fda14a | 2015-07-31 09:31:19 -0600 | [diff] [blame] | 161 | if (list_empty(&uc->dev_head)) |
| 162 | return -ENODEV; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 163 | |
| 164 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
| 165 | if (!index--) { |
| 166 | *devp = dev; |
| 167 | return 0; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return -ENODEV; |
| 172 | } |
| 173 | |
Przemyslaw Marczak | c1d6f91 | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 174 | int uclass_find_first_device(enum uclass_id id, struct udevice **devp) |
| 175 | { |
| 176 | struct uclass *uc; |
| 177 | int ret; |
| 178 | |
| 179 | *devp = NULL; |
| 180 | ret = uclass_get(id, &uc); |
| 181 | if (ret) |
| 182 | return ret; |
| 183 | if (list_empty(&uc->dev_head)) |
| 184 | return 0; |
| 185 | |
| 186 | *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | int uclass_find_next_device(struct udevice **devp) |
| 192 | { |
| 193 | struct udevice *dev = *devp; |
| 194 | |
| 195 | *devp = NULL; |
| 196 | if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head)) |
| 197 | return 0; |
| 198 | |
| 199 | *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node); |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
Przemyslaw Marczak | e0735a4 | 2015-04-15 13:07:22 +0200 | [diff] [blame] | 204 | int uclass_find_device_by_name(enum uclass_id id, const char *name, |
| 205 | struct udevice **devp) |
| 206 | { |
| 207 | struct uclass *uc; |
| 208 | struct udevice *dev; |
| 209 | int ret; |
| 210 | |
| 211 | *devp = NULL; |
| 212 | if (!name) |
| 213 | return -EINVAL; |
| 214 | ret = uclass_get(id, &uc); |
| 215 | if (ret) |
| 216 | return ret; |
| 217 | |
| 218 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
| 219 | if (!strncmp(dev->name, name, strlen(name))) { |
| 220 | *devp = dev; |
| 221 | return 0; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return -ENODEV; |
| 226 | } |
| 227 | |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 228 | int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq, |
| 229 | bool find_req_seq, struct udevice **devp) |
| 230 | { |
| 231 | struct uclass *uc; |
| 232 | struct udevice *dev; |
| 233 | int ret; |
| 234 | |
| 235 | *devp = NULL; |
| 236 | debug("%s: %d %d\n", __func__, find_req_seq, seq_or_req_seq); |
| 237 | if (seq_or_req_seq == -1) |
| 238 | return -ENODEV; |
| 239 | ret = uclass_get(id, &uc); |
| 240 | if (ret) |
| 241 | return ret; |
| 242 | |
| 243 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
| 244 | debug(" - %d %d\n", dev->req_seq, dev->seq); |
| 245 | if ((find_req_seq ? dev->req_seq : dev->seq) == |
| 246 | seq_or_req_seq) { |
| 247 | *devp = dev; |
| 248 | debug(" - found\n"); |
| 249 | return 0; |
| 250 | } |
| 251 | } |
| 252 | debug(" - not found\n"); |
| 253 | |
| 254 | return -ENODEV; |
| 255 | } |
| 256 | |
Simon Glass | f4cdead | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 257 | static int uclass_find_device_by_of_offset(enum uclass_id id, int node, |
| 258 | struct udevice **devp) |
| 259 | { |
| 260 | struct uclass *uc; |
| 261 | struct udevice *dev; |
| 262 | int ret; |
| 263 | |
| 264 | *devp = NULL; |
| 265 | if (node < 0) |
| 266 | return -ENODEV; |
| 267 | ret = uclass_get(id, &uc); |
| 268 | if (ret) |
| 269 | return ret; |
| 270 | |
| 271 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
| 272 | if (dev->of_offset == node) { |
| 273 | *devp = dev; |
| 274 | return 0; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return -ENODEV; |
| 279 | } |
| 280 | |
Simon Glass | c275dfe | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 281 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | d82ba4c | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 282 | static int uclass_find_device_by_phandle(enum uclass_id id, |
| 283 | struct udevice *parent, |
| 284 | const char *name, |
| 285 | struct udevice **devp) |
| 286 | { |
| 287 | struct udevice *dev; |
| 288 | struct uclass *uc; |
| 289 | int find_phandle; |
| 290 | int ret; |
| 291 | |
| 292 | *devp = NULL; |
| 293 | find_phandle = fdtdec_get_int(gd->fdt_blob, parent->of_offset, name, |
| 294 | -1); |
| 295 | if (find_phandle <= 0) |
| 296 | return -ENOENT; |
| 297 | ret = uclass_get(id, &uc); |
| 298 | if (ret) |
| 299 | return ret; |
| 300 | |
| 301 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
| 302 | uint phandle = fdt_get_phandle(gd->fdt_blob, dev->of_offset); |
| 303 | |
| 304 | if (phandle == find_phandle) { |
| 305 | *devp = dev; |
| 306 | return 0; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | return -ENODEV; |
| 311 | } |
Simon Glass | c275dfe | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 312 | #endif |
Simon Glass | d82ba4c | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 313 | |
Przemyslaw Marczak | 794d521 | 2015-04-20 13:32:32 +0200 | [diff] [blame] | 314 | int uclass_get_device_tail(struct udevice *dev, int ret, |
Simon Glass | 9ca296a | 2014-07-23 06:55:08 -0600 | [diff] [blame] | 315 | struct udevice **devp) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 316 | { |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 317 | if (ret) |
| 318 | return ret; |
| 319 | |
Simon Glass | f66529f | 2015-04-24 22:33:07 -0600 | [diff] [blame] | 320 | assert(dev); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 321 | ret = device_probe(dev); |
| 322 | if (ret) |
| 323 | return ret; |
| 324 | |
| 325 | *devp = dev; |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
Simon Glass | 9ca296a | 2014-07-23 06:55:08 -0600 | [diff] [blame] | 330 | int uclass_get_device(enum uclass_id id, int index, struct udevice **devp) |
| 331 | { |
| 332 | struct udevice *dev; |
| 333 | int ret; |
| 334 | |
| 335 | *devp = NULL; |
| 336 | ret = uclass_find_device(id, index, &dev); |
| 337 | return uclass_get_device_tail(dev, ret, devp); |
| 338 | } |
| 339 | |
Przemyslaw Marczak | b7af1a2 | 2015-04-15 13:07:23 +0200 | [diff] [blame] | 340 | int uclass_get_device_by_name(enum uclass_id id, const char *name, |
| 341 | struct udevice **devp) |
| 342 | { |
| 343 | struct udevice *dev; |
| 344 | int ret; |
| 345 | |
| 346 | *devp = NULL; |
| 347 | ret = uclass_find_device_by_name(id, name, &dev); |
| 348 | return uclass_get_device_tail(dev, ret, devp); |
| 349 | } |
| 350 | |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 351 | int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp) |
| 352 | { |
| 353 | struct udevice *dev; |
| 354 | int ret; |
| 355 | |
| 356 | *devp = NULL; |
| 357 | ret = uclass_find_device_by_seq(id, seq, false, &dev); |
| 358 | if (ret == -ENODEV) { |
| 359 | /* |
| 360 | * We didn't find it in probed devices. See if there is one |
| 361 | * that will request this seq if probed. |
| 362 | */ |
| 363 | ret = uclass_find_device_by_seq(id, seq, true, &dev); |
| 364 | } |
| 365 | return uclass_get_device_tail(dev, ret, devp); |
| 366 | } |
| 367 | |
Simon Glass | f4cdead | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 368 | int uclass_get_device_by_of_offset(enum uclass_id id, int node, |
| 369 | struct udevice **devp) |
| 370 | { |
| 371 | struct udevice *dev; |
| 372 | int ret; |
| 373 | |
| 374 | *devp = NULL; |
| 375 | ret = uclass_find_device_by_of_offset(id, node, &dev); |
| 376 | return uclass_get_device_tail(dev, ret, devp); |
| 377 | } |
| 378 | |
Simon Glass | c275dfe | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 379 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | d82ba4c | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 380 | int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent, |
| 381 | const char *name, struct udevice **devp) |
| 382 | { |
| 383 | struct udevice *dev; |
| 384 | int ret; |
| 385 | |
| 386 | *devp = NULL; |
| 387 | ret = uclass_find_device_by_phandle(id, parent, name, &dev); |
| 388 | return uclass_get_device_tail(dev, ret, devp); |
| 389 | } |
Simon Glass | c275dfe | 2015-12-19 19:38:55 -0700 | [diff] [blame] | 390 | #endif |
Simon Glass | d82ba4c | 2015-07-02 18:15:38 -0600 | [diff] [blame] | 391 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 392 | int uclass_first_device(enum uclass_id id, struct udevice **devp) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 393 | { |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 394 | struct udevice *dev; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 395 | int ret; |
| 396 | |
| 397 | *devp = NULL; |
Przemyslaw Marczak | c1d6f91 | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 398 | ret = uclass_find_first_device(id, &dev); |
Simon Glass | f66529f | 2015-04-24 22:33:07 -0600 | [diff] [blame] | 399 | if (!dev) |
| 400 | return 0; |
Przemyslaw Marczak | c1d6f91 | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 401 | return uclass_get_device_tail(dev, ret, devp); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 404 | int uclass_next_device(struct udevice **devp) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 405 | { |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 406 | struct udevice *dev = *devp; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 407 | int ret; |
| 408 | |
| 409 | *devp = NULL; |
Przemyslaw Marczak | c1d6f91 | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 410 | ret = uclass_find_next_device(&dev); |
Simon Glass | f66529f | 2015-04-24 22:33:07 -0600 | [diff] [blame] | 411 | if (!dev) |
| 412 | return 0; |
Przemyslaw Marczak | c1d6f91 | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 413 | return uclass_get_device_tail(dev, ret, devp); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 416 | int uclass_bind_device(struct udevice *dev) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 417 | { |
| 418 | struct uclass *uc; |
| 419 | int ret; |
| 420 | |
| 421 | uc = dev->uclass; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 422 | list_add_tail(&dev->uclass_node, &uc->dev_head); |
| 423 | |
Simon Glass | 081f2fc | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 424 | if (dev->parent) { |
| 425 | struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv; |
| 426 | |
| 427 | if (uc_drv->child_post_bind) { |
| 428 | ret = uc_drv->child_post_bind(dev); |
| 429 | if (ret) |
| 430 | goto err; |
| 431 | } |
| 432 | } |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 433 | |
| 434 | return 0; |
Simon Glass | 081f2fc | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 435 | err: |
| 436 | /* There is no need to undo the parent's post_bind call */ |
| 437 | list_del(&dev->uclass_node); |
| 438 | |
| 439 | return ret; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Masahiro Yamada | 0a5804b | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 442 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 443 | int uclass_unbind_device(struct udevice *dev) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 444 | { |
| 445 | struct uclass *uc; |
| 446 | int ret; |
| 447 | |
| 448 | uc = dev->uclass; |
| 449 | if (uc->uc_drv->pre_unbind) { |
| 450 | ret = uc->uc_drv->pre_unbind(dev); |
| 451 | if (ret) |
| 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | list_del(&dev->uclass_node); |
| 456 | return 0; |
| 457 | } |
Simon Glass | 7f9875e | 2015-02-27 22:06:31 -0700 | [diff] [blame] | 458 | #endif |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 459 | |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 460 | int uclass_resolve_seq(struct udevice *dev) |
| 461 | { |
| 462 | struct udevice *dup; |
| 463 | int seq; |
| 464 | int ret; |
| 465 | |
| 466 | assert(dev->seq == -1); |
| 467 | ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq, |
| 468 | false, &dup); |
| 469 | if (!ret) { |
| 470 | dm_warn("Device '%s': seq %d is in use by '%s'\n", |
| 471 | dev->name, dev->req_seq, dup->name); |
| 472 | } else if (ret == -ENODEV) { |
| 473 | /* Our requested sequence number is available */ |
| 474 | if (dev->req_seq != -1) |
| 475 | return dev->req_seq; |
| 476 | } else { |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | for (seq = 0; seq < DM_MAX_SEQ; seq++) { |
| 481 | ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq, |
| 482 | false, &dup); |
| 483 | if (ret == -ENODEV) |
| 484 | break; |
| 485 | if (ret) |
| 486 | return ret; |
| 487 | } |
| 488 | return seq; |
| 489 | } |
| 490 | |
Simon Glass | 02c07b3 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 491 | int uclass_pre_probe_device(struct udevice *dev) |
Simon Glass | 83c7e43 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 492 | { |
| 493 | struct uclass_driver *uc_drv; |
Simon Glass | 02c07b3 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 494 | int ret; |
| 495 | |
| 496 | uc_drv = dev->uclass->uc_drv; |
| 497 | if (uc_drv->pre_probe) { |
| 498 | ret = uc_drv->pre_probe(dev); |
| 499 | if (ret) |
| 500 | return ret; |
| 501 | } |
Simon Glass | 83c7e43 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 502 | |
| 503 | if (!dev->parent) |
| 504 | return 0; |
| 505 | uc_drv = dev->parent->uclass->uc_drv; |
| 506 | if (uc_drv->child_pre_probe) |
| 507 | return uc_drv->child_pre_probe(dev); |
| 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 512 | int uclass_post_probe_device(struct udevice *dev) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 513 | { |
| 514 | struct uclass_driver *uc_drv = dev->uclass->uc_drv; |
| 515 | |
| 516 | if (uc_drv->post_probe) |
| 517 | return uc_drv->post_probe(dev); |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
Masahiro Yamada | 0a5804b | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 522 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 523 | int uclass_pre_remove_device(struct udevice *dev) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 524 | { |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 525 | struct uclass *uc; |
| 526 | int ret; |
| 527 | |
| 528 | uc = dev->uclass; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 529 | if (uc->uc_drv->pre_remove) { |
| 530 | ret = uc->uc_drv->pre_remove(dev); |
| 531 | if (ret) |
| 532 | return ret; |
| 533 | } |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 534 | |
| 535 | return 0; |
| 536 | } |
Simon Glass | 7f9875e | 2015-02-27 22:06:31 -0700 | [diff] [blame] | 537 | #endif |