1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (c) 2013 Google, Inc 4 * 5 * (C) Copyright 2012 6 * Pavel Herrmann <morpheus.ibis@gmail.com> 7 */ 8 9 #include <common.h> 10 #include <dm.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 26 if (!gd->dm_root) 27 return NULL; 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) { 59 debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n", 60 id); 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; 67 } 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); 81 list_add(&uc->sibling_node, &DM_UCLASS_ROOT_NON_CONST); 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; 107 struct udevice *dev; 108 int ret; 109 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); 119 ret = device_remove(dev, DM_REMOVE_NORMAL); 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 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 160 enum uclass_id uclass_get_by_name(const char *name) 161 { 162 int i; 163 164 for (i = 0; i < UCLASS_COUNT; i++) { 165 struct uclass_driver *uc_drv = lists_uclass_lookup(i); 166 167 if (uc_drv && !strcmp(uc_drv->name, name)) 168 return i; 169 } 170 171 return UCLASS_INVALID; 172 } 173 174 int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp) 175 { 176 struct udevice *iter; 177 struct uclass *uc = dev->uclass; 178 int i = 0; 179 180 if (list_empty(&uc->dev_head)) 181 return -ENODEV; 182 183 uclass_foreach_dev(iter, uc) { 184 if (iter == dev) { 185 if (ucp) 186 *ucp = uc; 187 return i; 188 } 189 i++; 190 } 191 192 return -ENODEV; 193 } 194 195 int uclass_find_device(enum uclass_id id, int index, struct udevice **devp) 196 { 197 struct uclass *uc; 198 struct udevice *dev; 199 int ret; 200 201 *devp = NULL; 202 ret = uclass_get(id, &uc); 203 if (ret) 204 return ret; 205 if (list_empty(&uc->dev_head)) 206 return -ENODEV; 207 208 uclass_foreach_dev(dev, uc) { 209 if (!index--) { 210 *devp = dev; 211 return 0; 212 } 213 } 214 215 return -ENODEV; 216 } 217 218 int uclass_find_first_device(enum uclass_id id, struct udevice **devp) 219 { 220 struct uclass *uc; 221 int ret; 222 223 *devp = NULL; 224 ret = uclass_get(id, &uc); 225 if (ret) 226 return ret; 227 if (list_empty(&uc->dev_head)) 228 return 0; 229 230 *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node); 231 232 return 0; 233 } 234 235 int uclass_find_next_device(struct udevice **devp) 236 { 237 struct udevice *dev = *devp; 238 239 *devp = NULL; 240 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head)) 241 return 0; 242 243 *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node); 244 245 return 0; 246 } 247 248 int uclass_find_device_by_name(enum uclass_id id, const char *name, 249 struct udevice **devp) 250 { 251 struct uclass *uc; 252 struct udevice *dev; 253 int ret; 254 255 *devp = NULL; 256 if (!name) 257 return -EINVAL; 258 ret = uclass_get(id, &uc); 259 if (ret) 260 return ret; 261 262 uclass_foreach_dev(dev, uc) { 263 if (!strncmp(dev->name, name, strlen(name))) { 264 *devp = dev; 265 return 0; 266 } 267 } 268 269 return -ENODEV; 270 } 271 272 int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq, 273 bool find_req_seq, struct udevice **devp) 274 { 275 struct uclass *uc; 276 struct udevice *dev; 277 int ret; 278 279 *devp = NULL; 280 debug("%s: %d %d\n", __func__, find_req_seq, seq_or_req_seq); 281 if (seq_or_req_seq == -1) 282 return -ENODEV; 283 ret = uclass_get(id, &uc); 284 if (ret) 285 return ret; 286 287 uclass_foreach_dev(dev, uc) { 288 debug(" - %d %d '%s'\n", dev->req_seq, dev->seq, dev->name); 289 if ((find_req_seq ? dev->req_seq : dev->seq) == 290 seq_or_req_seq) { 291 *devp = dev; 292 debug(" - found\n"); 293 return 0; 294 } 295 } 296 debug(" - not found\n"); 297 298 return -ENODEV; 299 } 300 301 int uclass_find_device_by_of_offset(enum uclass_id id, int node, 302 struct udevice **devp) 303 { 304 struct uclass *uc; 305 struct udevice *dev; 306 int ret; 307 308 *devp = NULL; 309 if (node < 0) 310 return -ENODEV; 311 ret = uclass_get(id, &uc); 312 if (ret) 313 return ret; 314 315 uclass_foreach_dev(dev, uc) { 316 if (dev_of_offset(dev) == node) { 317 *devp = dev; 318 return 0; 319 } 320 } 321 322 return -ENODEV; 323 } 324 325 int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node, 326 struct udevice **devp) 327 { 328 struct uclass *uc; 329 struct udevice *dev; 330 int ret; 331 332 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node)); 333 *devp = NULL; 334 if (!ofnode_valid(node)) 335 return -ENODEV; 336 ret = uclass_get(id, &uc); 337 if (ret) 338 return ret; 339 340 uclass_foreach_dev(dev, uc) { 341 log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n", 342 dev->name); 343 if (ofnode_equal(dev_ofnode(dev), node)) { 344 *devp = dev; 345 goto done; 346 } 347 } 348 ret = -ENODEV; 349 350 done: 351 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n", 352 ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret); 353 return ret; 354 } 355 356 #if CONFIG_IS_ENABLED(OF_CONTROL) 357 int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent, 358 const char *name, struct udevice **devp) 359 { 360 struct udevice *dev; 361 struct uclass *uc; 362 int find_phandle; 363 int ret; 364 365 *devp = NULL; 366 find_phandle = dev_read_u32_default(parent, name, -1); 367 if (find_phandle <= 0) 368 return -ENOENT; 369 ret = uclass_get(id, &uc); 370 if (ret) 371 return ret; 372 373 uclass_foreach_dev(dev, uc) { 374 uint phandle; 375 376 phandle = dev_read_phandle(dev); 377 378 if (phandle == find_phandle) { 379 *devp = dev; 380 return 0; 381 } 382 } 383 384 return -ENODEV; 385 } 386 #endif 387 388 int uclass_get_device_by_driver(enum uclass_id id, 389 const struct driver *find_drv, 390 struct udevice **devp) 391 { 392 struct udevice *dev; 393 struct uclass *uc; 394 int ret; 395 396 ret = uclass_get(id, &uc); 397 if (ret) 398 return ret; 399 400 uclass_foreach_dev(dev, uc) { 401 if (dev->driver == find_drv) 402 return uclass_get_device_tail(dev, 0, devp); 403 } 404 405 return -ENODEV; 406 } 407 408 int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp) 409 { 410 if (ret) 411 return ret; 412 413 assert(dev); 414 ret = device_probe(dev); 415 if (ret) 416 return ret; 417 418 *devp = dev; 419 420 return 0; 421 } 422 423 int uclass_get_device(enum uclass_id id, int index, struct udevice **devp) 424 { 425 struct udevice *dev; 426 int ret; 427 428 *devp = NULL; 429 ret = uclass_find_device(id, index, &dev); 430 return uclass_get_device_tail(dev, ret, devp); 431 } 432 433 int uclass_get_device_by_name(enum uclass_id id, const char *name, 434 struct udevice **devp) 435 { 436 struct udevice *dev; 437 int ret; 438 439 *devp = NULL; 440 ret = uclass_find_device_by_name(id, name, &dev); 441 return uclass_get_device_tail(dev, ret, devp); 442 } 443 444 int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp) 445 { 446 struct udevice *dev; 447 int ret; 448 449 *devp = NULL; 450 ret = uclass_find_device_by_seq(id, seq, false, &dev); 451 if (ret == -ENODEV) { 452 /* 453 * We didn't find it in probed devices. See if there is one 454 * that will request this seq if probed. 455 */ 456 ret = uclass_find_device_by_seq(id, seq, true, &dev); 457 } 458 return uclass_get_device_tail(dev, ret, devp); 459 } 460 461 int uclass_get_device_by_of_offset(enum uclass_id id, int node, 462 struct udevice **devp) 463 { 464 struct udevice *dev; 465 int ret; 466 467 *devp = NULL; 468 ret = uclass_find_device_by_of_offset(id, node, &dev); 469 return uclass_get_device_tail(dev, ret, devp); 470 } 471 472 int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node, 473 struct udevice **devp) 474 { 475 struct udevice *dev; 476 int ret; 477 478 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node)); 479 *devp = NULL; 480 ret = uclass_find_device_by_ofnode(id, node, &dev); 481 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n", 482 ofnode_get_name(node), dev ? dev->name : "(none)", ret); 483 484 return uclass_get_device_tail(dev, ret, devp); 485 } 486 487 #if CONFIG_IS_ENABLED(OF_CONTROL) 488 int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id, 489 struct udevice **devp) 490 { 491 struct udevice *dev; 492 struct uclass *uc; 493 int ret; 494 495 *devp = NULL; 496 ret = uclass_get(id, &uc); 497 if (ret) 498 return ret; 499 500 uclass_foreach_dev(dev, uc) { 501 uint phandle; 502 503 phandle = dev_read_phandle(dev); 504 505 if (phandle == phandle_id) { 506 *devp = dev; 507 return uclass_get_device_tail(dev, ret, devp); 508 } 509 } 510 511 return -ENODEV; 512 } 513 514 int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent, 515 const char *name, struct udevice **devp) 516 { 517 struct udevice *dev; 518 int ret; 519 520 *devp = NULL; 521 ret = uclass_find_device_by_phandle(id, parent, name, &dev); 522 return uclass_get_device_tail(dev, ret, devp); 523 } 524 #endif 525 526 int uclass_first_device(enum uclass_id id, struct udevice **devp) 527 { 528 struct udevice *dev; 529 int ret; 530 531 *devp = NULL; 532 ret = uclass_find_first_device(id, &dev); 533 if (!dev) 534 return 0; 535 return uclass_get_device_tail(dev, ret, devp); 536 } 537 538 int uclass_first_device_err(enum uclass_id id, struct udevice **devp) 539 { 540 int ret; 541 542 ret = uclass_first_device(id, devp); 543 if (ret) 544 return ret; 545 else if (!*devp) 546 return -ENODEV; 547 548 return 0; 549 } 550 551 int uclass_next_device(struct udevice **devp) 552 { 553 struct udevice *dev = *devp; 554 int ret; 555 556 *devp = NULL; 557 ret = uclass_find_next_device(&dev); 558 if (!dev) 559 return 0; 560 return uclass_get_device_tail(dev, ret, devp); 561 } 562 563 int uclass_next_device_err(struct udevice **devp) 564 { 565 int ret; 566 567 ret = uclass_next_device(devp); 568 if (ret) 569 return ret; 570 else if (!*devp) 571 return -ENODEV; 572 573 return 0; 574 } 575 576 int uclass_first_device_check(enum uclass_id id, struct udevice **devp) 577 { 578 int ret; 579 580 *devp = NULL; 581 ret = uclass_find_first_device(id, devp); 582 if (ret) 583 return ret; 584 if (!*devp) 585 return 0; 586 587 return device_probe(*devp); 588 } 589 590 int uclass_next_device_check(struct udevice **devp) 591 { 592 int ret; 593 594 ret = uclass_find_next_device(devp); 595 if (ret) 596 return ret; 597 if (!*devp) 598 return 0; 599 600 return device_probe(*devp); 601 } 602 603 int uclass_bind_device(struct udevice *dev) 604 { 605 struct uclass *uc; 606 int ret; 607 608 uc = dev->uclass; 609 list_add_tail(&dev->uclass_node, &uc->dev_head); 610 611 if (dev->parent) { 612 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv; 613 614 if (uc_drv->child_post_bind) { 615 ret = uc_drv->child_post_bind(dev); 616 if (ret) 617 goto err; 618 } 619 } 620 621 return 0; 622 err: 623 /* There is no need to undo the parent's post_bind call */ 624 list_del(&dev->uclass_node); 625 626 return ret; 627 } 628 629 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) 630 int uclass_unbind_device(struct udevice *dev) 631 { 632 struct uclass *uc; 633 int ret; 634 635 uc = dev->uclass; 636 if (uc->uc_drv->pre_unbind) { 637 ret = uc->uc_drv->pre_unbind(dev); 638 if (ret) 639 return ret; 640 } 641 642 list_del(&dev->uclass_node); 643 return 0; 644 } 645 #endif 646 647 int uclass_resolve_seq(struct udevice *dev) 648 { 649 struct udevice *dup; 650 int seq; 651 int ret; 652 653 assert(dev->seq == -1); 654 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq, 655 false, &dup); 656 if (!ret) { 657 dm_warn("Device '%s': seq %d is in use by '%s'\n", 658 dev->name, dev->req_seq, dup->name); 659 } else if (ret == -ENODEV) { 660 /* Our requested sequence number is available */ 661 if (dev->req_seq != -1) 662 return dev->req_seq; 663 } else { 664 return ret; 665 } 666 667 for (seq = 0; seq < DM_MAX_SEQ; seq++) { 668 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq, 669 false, &dup); 670 if (ret == -ENODEV) 671 break; 672 if (ret) 673 return ret; 674 } 675 return seq; 676 } 677 678 int uclass_pre_probe_device(struct udevice *dev) 679 { 680 struct uclass_driver *uc_drv; 681 int ret; 682 683 uc_drv = dev->uclass->uc_drv; 684 if (uc_drv->pre_probe) { 685 ret = uc_drv->pre_probe(dev); 686 if (ret) 687 return ret; 688 } 689 690 if (!dev->parent) 691 return 0; 692 uc_drv = dev->parent->uclass->uc_drv; 693 if (uc_drv->child_pre_probe) 694 return uc_drv->child_pre_probe(dev); 695 696 return 0; 697 } 698 699 int uclass_post_probe_device(struct udevice *dev) 700 { 701 struct uclass_driver *uc_drv; 702 int ret; 703 704 if (dev->parent) { 705 uc_drv = dev->parent->uclass->uc_drv; 706 if (uc_drv->child_post_probe) { 707 ret = uc_drv->child_post_probe(dev); 708 if (ret) 709 return ret; 710 } 711 } 712 713 uc_drv = dev->uclass->uc_drv; 714 if (uc_drv->post_probe) 715 return uc_drv->post_probe(dev); 716 717 return 0; 718 } 719 720 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) 721 int uclass_pre_remove_device(struct udevice *dev) 722 { 723 struct uclass *uc; 724 int ret; 725 726 uc = dev->uclass; 727 if (uc->uc_drv->pre_remove) { 728 ret = uc->uc_drv->pre_remove(dev); 729 if (ret) 730 return ret; 731 } 732 733 return 0; 734 } 735 #endif 736