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) { |
| 59 | dm_warn("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n", |
| 60 | id); |
| 61 | return -ENOENT; |
| 62 | } |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 63 | uc = calloc(1, sizeof(*uc)); |
| 64 | if (!uc) |
| 65 | return -ENOMEM; |
| 66 | if (uc_drv->priv_auto_alloc_size) { |
| 67 | uc->priv = calloc(1, uc_drv->priv_auto_alloc_size); |
| 68 | if (!uc->priv) { |
| 69 | ret = -ENOMEM; |
| 70 | goto fail_mem; |
| 71 | } |
| 72 | } |
| 73 | uc->uc_drv = uc_drv; |
| 74 | INIT_LIST_HEAD(&uc->sibling_node); |
| 75 | INIT_LIST_HEAD(&uc->dev_head); |
Simon Glass | 89876a5 | 2014-06-11 23:29:49 -0600 | [diff] [blame] | 76 | list_add(&uc->sibling_node, &DM_UCLASS_ROOT_NON_CONST); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 77 | |
| 78 | if (uc_drv->init) { |
| 79 | ret = uc_drv->init(uc); |
| 80 | if (ret) |
| 81 | goto fail; |
| 82 | } |
| 83 | |
| 84 | *ucp = uc; |
| 85 | |
| 86 | return 0; |
| 87 | fail: |
| 88 | if (uc_drv->priv_auto_alloc_size) { |
| 89 | free(uc->priv); |
| 90 | uc->priv = NULL; |
| 91 | } |
| 92 | list_del(&uc->sibling_node); |
| 93 | fail_mem: |
| 94 | free(uc); |
| 95 | |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | int uclass_destroy(struct uclass *uc) |
| 100 | { |
| 101 | struct uclass_driver *uc_drv; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 102 | struct udevice *dev, *tmp; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 103 | int ret; |
| 104 | |
| 105 | list_for_each_entry_safe(dev, tmp, &uc->dev_head, uclass_node) { |
| 106 | ret = device_remove(dev); |
| 107 | if (ret) |
| 108 | return ret; |
| 109 | ret = device_unbind(dev); |
| 110 | if (ret) |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | uc_drv = uc->uc_drv; |
| 115 | if (uc_drv->destroy) |
| 116 | uc_drv->destroy(uc); |
| 117 | list_del(&uc->sibling_node); |
| 118 | if (uc_drv->priv_auto_alloc_size) |
| 119 | free(uc->priv); |
| 120 | free(uc); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | int uclass_get(enum uclass_id id, struct uclass **ucp) |
| 126 | { |
| 127 | struct uclass *uc; |
| 128 | |
| 129 | *ucp = NULL; |
| 130 | uc = uclass_find(id); |
| 131 | if (!uc) |
| 132 | return uclass_add(id, ucp); |
| 133 | *ucp = uc; |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 138 | 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] | 139 | { |
| 140 | struct uclass *uc; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 141 | struct udevice *dev; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 142 | int ret; |
| 143 | |
| 144 | *devp = NULL; |
| 145 | ret = uclass_get(id, &uc); |
| 146 | if (ret) |
| 147 | return ret; |
| 148 | |
| 149 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
| 150 | if (!index--) { |
| 151 | *devp = dev; |
| 152 | return 0; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return -ENODEV; |
| 157 | } |
| 158 | |
Przemyslaw Marczak | c1d6f91 | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 159 | int uclass_find_first_device(enum uclass_id id, struct udevice **devp) |
| 160 | { |
| 161 | struct uclass *uc; |
| 162 | int ret; |
| 163 | |
| 164 | *devp = NULL; |
| 165 | ret = uclass_get(id, &uc); |
| 166 | if (ret) |
| 167 | return ret; |
| 168 | if (list_empty(&uc->dev_head)) |
| 169 | return 0; |
| 170 | |
| 171 | *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node); |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | int uclass_find_next_device(struct udevice **devp) |
| 177 | { |
| 178 | struct udevice *dev = *devp; |
| 179 | |
| 180 | *devp = NULL; |
| 181 | if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head)) |
| 182 | return 0; |
| 183 | |
| 184 | *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Przemyslaw Marczak | e0735a4 | 2015-04-15 13:07:22 +0200 | [diff] [blame] | 189 | int uclass_find_device_by_name(enum uclass_id id, const char *name, |
| 190 | struct udevice **devp) |
| 191 | { |
| 192 | struct uclass *uc; |
| 193 | struct udevice *dev; |
| 194 | int ret; |
| 195 | |
| 196 | *devp = NULL; |
| 197 | if (!name) |
| 198 | return -EINVAL; |
| 199 | ret = uclass_get(id, &uc); |
| 200 | if (ret) |
| 201 | return ret; |
| 202 | |
| 203 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
| 204 | if (!strncmp(dev->name, name, strlen(name))) { |
| 205 | *devp = dev; |
| 206 | return 0; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return -ENODEV; |
| 211 | } |
| 212 | |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 213 | int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq, |
| 214 | bool find_req_seq, struct udevice **devp) |
| 215 | { |
| 216 | struct uclass *uc; |
| 217 | struct udevice *dev; |
| 218 | int ret; |
| 219 | |
| 220 | *devp = NULL; |
| 221 | debug("%s: %d %d\n", __func__, find_req_seq, seq_or_req_seq); |
| 222 | if (seq_or_req_seq == -1) |
| 223 | return -ENODEV; |
| 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 | debug(" - %d %d\n", dev->req_seq, dev->seq); |
| 230 | if ((find_req_seq ? dev->req_seq : dev->seq) == |
| 231 | seq_or_req_seq) { |
| 232 | *devp = dev; |
| 233 | debug(" - found\n"); |
| 234 | return 0; |
| 235 | } |
| 236 | } |
| 237 | debug(" - not found\n"); |
| 238 | |
| 239 | return -ENODEV; |
| 240 | } |
| 241 | |
Simon Glass | f4cdead | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 242 | static int uclass_find_device_by_of_offset(enum uclass_id id, int node, |
| 243 | struct udevice **devp) |
| 244 | { |
| 245 | struct uclass *uc; |
| 246 | struct udevice *dev; |
| 247 | int ret; |
| 248 | |
| 249 | *devp = NULL; |
| 250 | if (node < 0) |
| 251 | return -ENODEV; |
| 252 | ret = uclass_get(id, &uc); |
| 253 | if (ret) |
| 254 | return ret; |
| 255 | |
| 256 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
| 257 | if (dev->of_offset == node) { |
| 258 | *devp = dev; |
| 259 | return 0; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | return -ENODEV; |
| 264 | } |
| 265 | |
Simon Glass | 9ca296a | 2014-07-23 06:55:08 -0600 | [diff] [blame] | 266 | /** |
| 267 | * uclass_get_device_tail() - handle the end of a get_device call |
| 268 | * |
| 269 | * This handles returning an error or probing a device as needed. |
| 270 | * |
| 271 | * @dev: Device that needs to be probed |
| 272 | * @ret: Error to return. If non-zero then the device is not probed |
| 273 | * @devp: Returns the value of 'dev' if there is no error |
| 274 | * @return ret, if non-zero, else the result of the device_probe() call |
| 275 | */ |
| 276 | static int uclass_get_device_tail(struct udevice *dev, int ret, |
| 277 | struct udevice **devp) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 278 | { |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 279 | if (ret) |
| 280 | return ret; |
| 281 | |
| 282 | ret = device_probe(dev); |
| 283 | if (ret) |
| 284 | return ret; |
| 285 | |
| 286 | *devp = dev; |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
Simon Glass | 9ca296a | 2014-07-23 06:55:08 -0600 | [diff] [blame] | 291 | int uclass_get_device(enum uclass_id id, int index, struct udevice **devp) |
| 292 | { |
| 293 | struct udevice *dev; |
| 294 | int ret; |
| 295 | |
| 296 | *devp = NULL; |
| 297 | ret = uclass_find_device(id, index, &dev); |
| 298 | return uclass_get_device_tail(dev, ret, devp); |
| 299 | } |
| 300 | |
Przemyslaw Marczak | b7af1a2 | 2015-04-15 13:07:23 +0200 | [diff] [blame] | 301 | int uclass_get_device_by_name(enum uclass_id id, const char *name, |
| 302 | struct udevice **devp) |
| 303 | { |
| 304 | struct udevice *dev; |
| 305 | int ret; |
| 306 | |
| 307 | *devp = NULL; |
| 308 | ret = uclass_find_device_by_name(id, name, &dev); |
| 309 | return uclass_get_device_tail(dev, ret, devp); |
| 310 | } |
| 311 | |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 312 | int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp) |
| 313 | { |
| 314 | struct udevice *dev; |
| 315 | int ret; |
| 316 | |
| 317 | *devp = NULL; |
| 318 | ret = uclass_find_device_by_seq(id, seq, false, &dev); |
| 319 | if (ret == -ENODEV) { |
| 320 | /* |
| 321 | * We didn't find it in probed devices. See if there is one |
| 322 | * that will request this seq if probed. |
| 323 | */ |
| 324 | ret = uclass_find_device_by_seq(id, seq, true, &dev); |
| 325 | } |
| 326 | return uclass_get_device_tail(dev, ret, devp); |
| 327 | } |
| 328 | |
Simon Glass | f4cdead | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 329 | int uclass_get_device_by_of_offset(enum uclass_id id, int node, |
| 330 | struct udevice **devp) |
| 331 | { |
| 332 | struct udevice *dev; |
| 333 | int ret; |
| 334 | |
| 335 | *devp = NULL; |
| 336 | ret = uclass_find_device_by_of_offset(id, node, &dev); |
| 337 | return uclass_get_device_tail(dev, ret, devp); |
| 338 | } |
| 339 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 340 | int uclass_first_device(enum uclass_id id, struct udevice **devp) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 341 | { |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 342 | struct udevice *dev; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 343 | int ret; |
| 344 | |
| 345 | *devp = NULL; |
Przemyslaw Marczak | c1d6f91 | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 346 | ret = uclass_find_first_device(id, &dev); |
| 347 | return uclass_get_device_tail(dev, ret, devp); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 350 | int uclass_next_device(struct udevice **devp) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 351 | { |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 352 | struct udevice *dev = *devp; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 353 | int ret; |
| 354 | |
| 355 | *devp = NULL; |
Przemyslaw Marczak | c1d6f91 | 2015-04-15 13:07:17 +0200 | [diff] [blame] | 356 | ret = uclass_find_next_device(&dev); |
| 357 | return uclass_get_device_tail(dev, ret, devp); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 360 | int uclass_bind_device(struct udevice *dev) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 361 | { |
| 362 | struct uclass *uc; |
| 363 | int ret; |
| 364 | |
| 365 | uc = dev->uclass; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 366 | list_add_tail(&dev->uclass_node, &uc->dev_head); |
| 367 | |
Simon Glass | 081f2fc | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 368 | if (dev->parent) { |
| 369 | struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv; |
| 370 | |
| 371 | if (uc_drv->child_post_bind) { |
| 372 | ret = uc_drv->child_post_bind(dev); |
| 373 | if (ret) |
| 374 | goto err; |
| 375 | } |
| 376 | } |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 377 | if (uc->uc_drv->post_bind) { |
| 378 | ret = uc->uc_drv->post_bind(dev); |
Simon Glass | 081f2fc | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 379 | if (ret) |
| 380 | goto err; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | return 0; |
Simon Glass | 081f2fc | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 384 | err: |
| 385 | /* There is no need to undo the parent's post_bind call */ |
| 386 | list_del(&dev->uclass_node); |
| 387 | |
| 388 | return ret; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 391 | int uclass_unbind_device(struct udevice *dev) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 392 | { |
| 393 | struct uclass *uc; |
| 394 | int ret; |
| 395 | |
| 396 | uc = dev->uclass; |
| 397 | if (uc->uc_drv->pre_unbind) { |
| 398 | ret = uc->uc_drv->pre_unbind(dev); |
| 399 | if (ret) |
| 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | list_del(&dev->uclass_node); |
| 404 | return 0; |
| 405 | } |
| 406 | |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 407 | int uclass_resolve_seq(struct udevice *dev) |
| 408 | { |
| 409 | struct udevice *dup; |
| 410 | int seq; |
| 411 | int ret; |
| 412 | |
| 413 | assert(dev->seq == -1); |
| 414 | ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq, |
| 415 | false, &dup); |
| 416 | if (!ret) { |
| 417 | dm_warn("Device '%s': seq %d is in use by '%s'\n", |
| 418 | dev->name, dev->req_seq, dup->name); |
| 419 | } else if (ret == -ENODEV) { |
| 420 | /* Our requested sequence number is available */ |
| 421 | if (dev->req_seq != -1) |
| 422 | return dev->req_seq; |
| 423 | } else { |
| 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | for (seq = 0; seq < DM_MAX_SEQ; seq++) { |
| 428 | ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq, |
| 429 | false, &dup); |
| 430 | if (ret == -ENODEV) |
| 431 | break; |
| 432 | if (ret) |
| 433 | return ret; |
| 434 | } |
| 435 | return seq; |
| 436 | } |
| 437 | |
Simon Glass | 02c07b3 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 438 | int uclass_pre_probe_device(struct udevice *dev) |
Simon Glass | 83c7e43 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 439 | { |
| 440 | struct uclass_driver *uc_drv; |
Simon Glass | 02c07b3 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 441 | int ret; |
| 442 | |
| 443 | uc_drv = dev->uclass->uc_drv; |
| 444 | if (uc_drv->pre_probe) { |
| 445 | ret = uc_drv->pre_probe(dev); |
| 446 | if (ret) |
| 447 | return ret; |
| 448 | } |
Simon Glass | 83c7e43 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 449 | |
| 450 | if (!dev->parent) |
| 451 | return 0; |
| 452 | uc_drv = dev->parent->uclass->uc_drv; |
| 453 | if (uc_drv->child_pre_probe) |
| 454 | return uc_drv->child_pre_probe(dev); |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 459 | int uclass_post_probe_device(struct udevice *dev) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 460 | { |
| 461 | struct uclass_driver *uc_drv = dev->uclass->uc_drv; |
| 462 | |
| 463 | if (uc_drv->post_probe) |
| 464 | return uc_drv->post_probe(dev); |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 469 | int uclass_pre_remove_device(struct udevice *dev) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 470 | { |
| 471 | struct uclass_driver *uc_drv; |
| 472 | struct uclass *uc; |
| 473 | int ret; |
| 474 | |
| 475 | uc = dev->uclass; |
| 476 | uc_drv = uc->uc_drv; |
| 477 | if (uc->uc_drv->pre_remove) { |
| 478 | ret = uc->uc_drv->pre_remove(dev); |
| 479 | if (ret) |
| 480 | return ret; |
| 481 | } |
| 482 | if (uc_drv->per_device_auto_alloc_size) { |
| 483 | free(dev->uclass_priv); |
| 484 | dev->uclass_priv = NULL; |
| 485 | } |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 486 | dev->seq = -1; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 487 | |
| 488 | return 0; |
| 489 | } |