blob: 3113d6a56ba334866f4f355fe65c9b25fafae1fa [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassdd6ab882014-02-26 15:59:18 -07002/*
3 * Copyright (c) 2013 Google, Inc
4 *
5 * (C) Copyright 2012
6 * Pavel Herrmann <morpheus.ibis@gmail.com>
Simon Glassdd6ab882014-02-26 15:59:18 -07007 */
8
9#include <common.h>
Simon Glassee145d62017-05-18 20:09:09 -060010#include <dm.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070011#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
20DECLARE_GLOBAL_DATA_PTR;
21
22struct uclass *uclass_find(enum uclass_id key)
23{
24 struct uclass *uc;
25
Simon Glassde708672014-07-23 06:55:15 -060026 if (!gd->dm_root)
27 return NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070028 /*
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 */
50static 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 Yamada1d06ced2015-07-07 18:51:32 +090059 debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
60 id);
Simon Glass43313de2015-08-30 16:55:16 -060061 /*
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 Glassdd6ab882014-02-26 15:59:18 -070067 }
Simon Glassdd6ab882014-02-26 15:59:18 -070068 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 Glass34a1d352014-06-11 23:29:49 -060081 list_add(&uc->sibling_node, &DM_UCLASS_ROOT_NON_CONST);
Simon Glassdd6ab882014-02-26 15:59:18 -070082
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;
92fail:
93 if (uc_drv->priv_auto_alloc_size) {
94 free(uc->priv);
95 uc->priv = NULL;
96 }
97 list_del(&uc->sibling_node);
98fail_mem:
99 free(uc);
100
101 return ret;
102}
103
104int uclass_destroy(struct uclass *uc)
105{
106 struct uclass_driver *uc_drv;
Simon Glassf33663d2015-04-19 07:20:58 -0600107 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700108 int ret;
109
Simon Glassf33663d2015-04-19 07:20:58 -0600110 /*
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 Roese80b5bc92017-03-20 12:51:48 +0100119 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassdd6ab882014-02-26 15:59:18 -0700120 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
138int 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 Glassd19d0732016-10-05 20:42:13 -0600151const 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
Simon Glass70e35b42017-12-28 13:14:15 -0700160enum 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
Jean-Jacques Hiblotc4f02482018-08-09 16:17:42 +0200174int 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 list_for_each_entry(iter, &uc->dev_head, uclass_node) {
184 if (iter == dev) {
185 if (ucp)
186 *ucp = uc;
187 return i;
188 }
189 i++;
190 }
191
192 return -ENODEV;
193}
194
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200195int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700196{
197 struct uclass *uc;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200198 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700199 int ret;
200
201 *devp = NULL;
202 ret = uclass_get(id, &uc);
203 if (ret)
204 return ret;
Simon Glass46766df2015-07-31 09:31:19 -0600205 if (list_empty(&uc->dev_head))
206 return -ENODEV;
Simon Glassdd6ab882014-02-26 15:59:18 -0700207
208 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
209 if (!index--) {
210 *devp = dev;
211 return 0;
212 }
213 }
214
215 return -ENODEV;
216}
217
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200218int 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
235int 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
Przemyslaw Marczak2ffdf142015-04-15 13:07:22 +0200248int 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 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
263 if (!strncmp(dev->name, name, strlen(name))) {
264 *devp = dev;
265 return 0;
266 }
267 }
268
269 return -ENODEV;
270}
271
Simon Glassdb6f0202014-07-23 06:55:12 -0600272int 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 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
Alexandru Gagniuc4490f072017-04-04 10:46:56 -0700288 debug(" - %d %d '%s'\n", dev->req_seq, dev->seq, dev->name);
Simon Glassdb6f0202014-07-23 06:55:12 -0600289 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
Simon Glass96f04442016-01-21 19:43:57 -0700301int uclass_find_device_by_of_offset(enum uclass_id id, int node,
302 struct udevice **devp)
Simon Glassc1464ab2014-07-23 06:55:14 -0600303{
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 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700316 if (dev_of_offset(dev) == node) {
Simon Glassc1464ab2014-07-23 06:55:14 -0600317 *devp = dev;
318 return 0;
319 }
320 }
321
322 return -ENODEV;
323}
324
Simon Glassee145d62017-05-18 20:09:09 -0600325int 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
Simon Glass5ab533c2018-06-11 13:07:19 -0600332 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600333 *devp = NULL;
334 if (!ofnode_valid(node))
335 return -ENODEV;
336 ret = uclass_get(id, &uc);
337 if (ret)
338 return ret;
339
340 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
Simon Glass5ab533c2018-06-11 13:07:19 -0600341 log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n",
342 dev->name);
Simon Glassee145d62017-05-18 20:09:09 -0600343 if (ofnode_equal(dev_ofnode(dev), node)) {
344 *devp = dev;
Simon Glass5ab533c2018-06-11 13:07:19 -0600345 goto done;
Simon Glassee145d62017-05-18 20:09:09 -0600346 }
347 }
Simon Glass5ab533c2018-06-11 13:07:19 -0600348 ret = -ENODEV;
Simon Glassee145d62017-05-18 20:09:09 -0600349
Simon Glass5ab533c2018-06-11 13:07:19 -0600350done:
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;
Simon Glassee145d62017-05-18 20:09:09 -0600354}
355
Simon Glass359c4872015-12-19 19:38:55 -0700356#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass75f00df2015-07-02 18:15:38 -0600357static int uclass_find_device_by_phandle(enum uclass_id id,
358 struct udevice *parent,
359 const char *name,
360 struct udevice **devp)
361{
362 struct udevice *dev;
363 struct uclass *uc;
364 int find_phandle;
365 int ret;
366
367 *devp = NULL;
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600368 find_phandle = dev_read_u32_default(parent, name, -1);
Simon Glass75f00df2015-07-02 18:15:38 -0600369 if (find_phandle <= 0)
370 return -ENOENT;
371 ret = uclass_get(id, &uc);
372 if (ret)
373 return ret;
374
375 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700376 uint phandle;
377
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600378 phandle = dev_read_phandle(dev);
Simon Glass75f00df2015-07-02 18:15:38 -0600379
380 if (phandle == find_phandle) {
381 *devp = dev;
382 return 0;
383 }
384 }
385
386 return -ENODEV;
387}
Simon Glass359c4872015-12-19 19:38:55 -0700388#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600389
Simon Glass32d8ab62016-07-17 15:23:15 -0600390int uclass_get_device_by_driver(enum uclass_id id,
391 const struct driver *find_drv,
392 struct udevice **devp)
393{
394 struct udevice *dev;
395 struct uclass *uc;
396 int ret;
397
398 ret = uclass_get(id, &uc);
399 if (ret)
400 return ret;
401
402 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
403 if (dev->driver == find_drv)
404 return uclass_get_device_tail(dev, 0, devp);
405 }
406
407 return -ENODEV;
408}
409
Simon Glass98c9fb02017-04-23 20:10:43 -0600410int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700411{
Simon Glassdd6ab882014-02-26 15:59:18 -0700412 if (ret)
413 return ret;
414
Simon Glass46cc8ba92015-04-24 22:33:07 -0600415 assert(dev);
Simon Glassdd6ab882014-02-26 15:59:18 -0700416 ret = device_probe(dev);
417 if (ret)
418 return ret;
419
420 *devp = dev;
421
422 return 0;
423}
424
Simon Glass4b67cef2014-07-23 06:55:08 -0600425int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
426{
427 struct udevice *dev;
428 int ret;
429
430 *devp = NULL;
431 ret = uclass_find_device(id, index, &dev);
432 return uclass_get_device_tail(dev, ret, devp);
433}
434
Przemyslaw Marczaka12a1f52015-04-15 13:07:23 +0200435int uclass_get_device_by_name(enum uclass_id id, const char *name,
436 struct udevice **devp)
437{
438 struct udevice *dev;
439 int ret;
440
441 *devp = NULL;
442 ret = uclass_find_device_by_name(id, name, &dev);
443 return uclass_get_device_tail(dev, ret, devp);
444}
445
Simon Glassdb6f0202014-07-23 06:55:12 -0600446int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
447{
448 struct udevice *dev;
449 int ret;
450
451 *devp = NULL;
452 ret = uclass_find_device_by_seq(id, seq, false, &dev);
453 if (ret == -ENODEV) {
454 /*
455 * We didn't find it in probed devices. See if there is one
456 * that will request this seq if probed.
457 */
458 ret = uclass_find_device_by_seq(id, seq, true, &dev);
459 }
460 return uclass_get_device_tail(dev, ret, devp);
461}
462
Simon Glassc1464ab2014-07-23 06:55:14 -0600463int uclass_get_device_by_of_offset(enum uclass_id id, int node,
464 struct udevice **devp)
465{
466 struct udevice *dev;
467 int ret;
468
469 *devp = NULL;
470 ret = uclass_find_device_by_of_offset(id, node, &dev);
471 return uclass_get_device_tail(dev, ret, devp);
472}
473
Simon Glassee145d62017-05-18 20:09:09 -0600474int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
475 struct udevice **devp)
476{
477 struct udevice *dev;
478 int ret;
479
Simon Glass5ab533c2018-06-11 13:07:19 -0600480 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600481 *devp = NULL;
482 ret = uclass_find_device_by_ofnode(id, node, &dev);
Simon Glass5ab533c2018-06-11 13:07:19 -0600483 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
484 ofnode_get_name(node), dev ? dev->name : "(none)", ret);
Simon Glassee145d62017-05-18 20:09:09 -0600485
486 return uclass_get_device_tail(dev, ret, devp);
487}
488
Simon Glass359c4872015-12-19 19:38:55 -0700489#if CONFIG_IS_ENABLED(OF_CONTROL)
Kever Yang66a0b5a2018-02-09 10:56:23 +0800490int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
491 struct udevice **devp)
492{
493 struct udevice *dev;
494 struct uclass *uc;
495 int ret;
496
497 *devp = NULL;
498 ret = uclass_get(id, &uc);
499 if (ret)
500 return ret;
501
502 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
503 uint phandle;
504
505 phandle = dev_read_phandle(dev);
506
507 if (phandle == phandle_id) {
508 *devp = dev;
509 return uclass_get_device_tail(dev, ret, devp);
510 }
511 }
512
513 return -ENODEV;
514}
515
Simon Glass75f00df2015-07-02 18:15:38 -0600516int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
517 const char *name, struct udevice **devp)
518{
519 struct udevice *dev;
520 int ret;
521
522 *devp = NULL;
523 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
524 return uclass_get_device_tail(dev, ret, devp);
525}
Simon Glass359c4872015-12-19 19:38:55 -0700526#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600527
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200528int uclass_first_device(enum uclass_id id, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700529{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200530 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700531 int ret;
532
533 *devp = NULL;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200534 ret = uclass_find_first_device(id, &dev);
Simon Glass46cc8ba92015-04-24 22:33:07 -0600535 if (!dev)
536 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200537 return uclass_get_device_tail(dev, ret, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700538}
539
Simon Glass832c3f02016-02-11 13:23:25 -0700540int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
541{
542 int ret;
543
544 ret = uclass_first_device(id, devp);
545 if (ret)
546 return ret;
547 else if (!*devp)
548 return -ENODEV;
549
550 return 0;
551}
552
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200553int uclass_next_device(struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700554{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200555 struct udevice *dev = *devp;
Simon Glassdd6ab882014-02-26 15:59:18 -0700556 int ret;
557
558 *devp = NULL;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200559 ret = uclass_find_next_device(&dev);
Simon Glass46cc8ba92015-04-24 22:33:07 -0600560 if (!dev)
561 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200562 return uclass_get_device_tail(dev, ret, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700563}
564
Simon Glass3ff83bc2017-04-23 20:10:45 -0600565int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
566{
567 int ret;
568
569 *devp = NULL;
570 ret = uclass_find_first_device(id, devp);
571 if (ret)
572 return ret;
573 if (!*devp)
574 return 0;
575
576 return device_probe(*devp);
577}
578
579int uclass_next_device_check(struct udevice **devp)
580{
581 int ret;
582
583 ret = uclass_find_next_device(devp);
584 if (ret)
585 return ret;
586 if (!*devp)
587 return 0;
588
589 return device_probe(*devp);
590}
591
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200592int uclass_bind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700593{
594 struct uclass *uc;
595 int ret;
596
597 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700598 list_add_tail(&dev->uclass_node, &uc->dev_head);
599
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700600 if (dev->parent) {
601 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
602
603 if (uc_drv->child_post_bind) {
604 ret = uc_drv->child_post_bind(dev);
605 if (ret)
606 goto err;
607 }
608 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700609
610 return 0;
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700611err:
612 /* There is no need to undo the parent's post_bind call */
613 list_del(&dev->uclass_node);
614
615 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700616}
617
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900618#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200619int uclass_unbind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700620{
621 struct uclass *uc;
622 int ret;
623
624 uc = dev->uclass;
625 if (uc->uc_drv->pre_unbind) {
626 ret = uc->uc_drv->pre_unbind(dev);
627 if (ret)
628 return ret;
629 }
630
631 list_del(&dev->uclass_node);
632 return 0;
633}
Simon Glass8914c8a2015-02-27 22:06:31 -0700634#endif
Simon Glassdd6ab882014-02-26 15:59:18 -0700635
Simon Glassdb6f0202014-07-23 06:55:12 -0600636int uclass_resolve_seq(struct udevice *dev)
637{
638 struct udevice *dup;
639 int seq;
640 int ret;
641
642 assert(dev->seq == -1);
643 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq,
644 false, &dup);
645 if (!ret) {
646 dm_warn("Device '%s': seq %d is in use by '%s'\n",
647 dev->name, dev->req_seq, dup->name);
648 } else if (ret == -ENODEV) {
649 /* Our requested sequence number is available */
650 if (dev->req_seq != -1)
651 return dev->req_seq;
652 } else {
653 return ret;
654 }
655
656 for (seq = 0; seq < DM_MAX_SEQ; seq++) {
657 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq,
658 false, &dup);
659 if (ret == -ENODEV)
660 break;
661 if (ret)
662 return ret;
663 }
664 return seq;
665}
666
Simon Glass9c1f3822015-03-05 12:25:22 -0700667int uclass_pre_probe_device(struct udevice *dev)
Simon Glass5104b982015-01-25 08:27:10 -0700668{
669 struct uclass_driver *uc_drv;
Simon Glass9c1f3822015-03-05 12:25:22 -0700670 int ret;
671
672 uc_drv = dev->uclass->uc_drv;
673 if (uc_drv->pre_probe) {
674 ret = uc_drv->pre_probe(dev);
675 if (ret)
676 return ret;
677 }
Simon Glass5104b982015-01-25 08:27:10 -0700678
679 if (!dev->parent)
680 return 0;
681 uc_drv = dev->parent->uclass->uc_drv;
682 if (uc_drv->child_pre_probe)
683 return uc_drv->child_pre_probe(dev);
684
685 return 0;
686}
687
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200688int uclass_post_probe_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700689{
690 struct uclass_driver *uc_drv = dev->uclass->uc_drv;
691
692 if (uc_drv->post_probe)
693 return uc_drv->post_probe(dev);
694
695 return 0;
696}
697
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900698#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200699int uclass_pre_remove_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700700{
Simon Glassdd6ab882014-02-26 15:59:18 -0700701 struct uclass *uc;
702 int ret;
703
704 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700705 if (uc->uc_drv->pre_remove) {
706 ret = uc->uc_drv->pre_remove(dev);
707 if (ret)
708 return ret;
709 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700710
711 return 0;
712}
Simon Glass8914c8a2015-02-27 22:06:31 -0700713#endif