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