1
2
3
4
5
6
7
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
20DECLARE_GLOBAL_DATA_PTR;
21
22struct uclass *uclass_find(enum uclass_id key)
23{
24 struct uclass *uc;
25
26 if (!gd->dm_root)
27 return NULL;
28
29
30
31
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
43
44
45
46
47
48
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) {
59 debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
60 id);
61
62
63
64
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;
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;
107 struct udevice *dev;
108 int ret;
109
110
111
112
113
114
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);
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
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
151const 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
160int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
161{
162 struct uclass *uc;
163 struct udevice *dev;
164 int ret;
165
166 *devp = NULL;
167 ret = uclass_get(id, &uc);
168 if (ret)
169 return ret;
170 if (list_empty(&uc->dev_head))
171 return -ENODEV;
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
183int 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
200int 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
213int 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
237int 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) {
253 debug(" - %d %d\n", dev->req_seq, dev->seq);
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
266int uclass_find_device_by_of_offset(enum uclass_id id, int node,
267 struct udevice **devp)
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) {
281 if (dev->of_offset == node) {
282 *devp = dev;
283 return 0;
284 }
285 }
286
287 return -ENODEV;
288}
289
290#if CONFIG_IS_ENABLED(OF_CONTROL)
291static 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;
302 find_phandle = fdtdec_get_int(gd->fdt_blob, parent->of_offset, name,
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) {
311 uint phandle = fdt_get_phandle(gd->fdt_blob, dev->of_offset);
312
313 if (phandle == find_phandle) {
314 *devp = dev;
315 return 0;
316 }
317 }
318
319 return -ENODEV;
320}
321#endif
322
323int uclass_get_device_by_driver(enum uclass_id id,
324 const struct driver *find_drv,
325 struct udevice **devp)
326{
327 struct udevice *dev;
328 struct uclass *uc;
329 int ret;
330
331 ret = uclass_get(id, &uc);
332 if (ret)
333 return ret;
334
335 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
336 if (dev->driver == find_drv)
337 return uclass_get_device_tail(dev, 0, devp);
338 }
339
340 return -ENODEV;
341}
342
343int uclass_get_device_tail(struct udevice *dev, int ret,
344 struct udevice **devp)
345{
346 if (ret)
347 return ret;
348
349 assert(dev);
350 ret = device_probe(dev);
351 if (ret)
352 return ret;
353
354 *devp = dev;
355
356 return 0;
357}
358
359int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
360{
361 struct udevice *dev;
362 int ret;
363
364 *devp = NULL;
365 ret = uclass_find_device(id, index, &dev);
366 return uclass_get_device_tail(dev, ret, devp);
367}
368
369int uclass_get_device_by_name(enum uclass_id id, const char *name,
370 struct udevice **devp)
371{
372 struct udevice *dev;
373 int ret;
374
375 *devp = NULL;
376 ret = uclass_find_device_by_name(id, name, &dev);
377 return uclass_get_device_tail(dev, ret, devp);
378}
379
380int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
381{
382 struct udevice *dev;
383 int ret;
384
385 *devp = NULL;
386 ret = uclass_find_device_by_seq(id, seq, false, &dev);
387 if (ret == -ENODEV) {
388
389
390
391
392 ret = uclass_find_device_by_seq(id, seq, true, &dev);
393 }
394 return uclass_get_device_tail(dev, ret, devp);
395}
396
397int uclass_get_device_by_of_offset(enum uclass_id id, int node,
398 struct udevice **devp)
399{
400 struct udevice *dev;
401 int ret;
402
403 *devp = NULL;
404 ret = uclass_find_device_by_of_offset(id, node, &dev);
405 return uclass_get_device_tail(dev, ret, devp);
406}
407
408#if CONFIG_IS_ENABLED(OF_CONTROL)
409int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
410 const char *name, struct udevice **devp)
411{
412 struct udevice *dev;
413 int ret;
414
415 *devp = NULL;
416 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
417 return uclass_get_device_tail(dev, ret, devp);
418}
419#endif
420
421int uclass_first_device(enum uclass_id id, struct udevice **devp)
422{
423 struct udevice *dev;
424 int ret;
425
426 *devp = NULL;
427 ret = uclass_find_first_device(id, &dev);
428 if (!dev)
429 return 0;
430 return uclass_get_device_tail(dev, ret, devp);
431}
432
433int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
434{
435 int ret;
436
437 ret = uclass_first_device(id, devp);
438 if (ret)
439 return ret;
440 else if (!*devp)
441 return -ENODEV;
442
443 return 0;
444}
445
446int uclass_next_device(struct udevice **devp)
447{
448 struct udevice *dev = *devp;
449 int ret;
450
451 *devp = NULL;
452 ret = uclass_find_next_device(&dev);
453 if (!dev)
454 return 0;
455 return uclass_get_device_tail(dev, ret, devp);
456}
457
458int uclass_bind_device(struct udevice *dev)
459{
460 struct uclass *uc;
461 int ret;
462
463 uc = dev->uclass;
464 list_add_tail(&dev->uclass_node, &uc->dev_head);
465
466 if (dev->parent) {
467 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
468
469 if (uc_drv->child_post_bind) {
470 ret = uc_drv->child_post_bind(dev);
471 if (ret)
472 goto err;
473 }
474 }
475
476 return 0;
477err:
478
479 list_del(&dev->uclass_node);
480
481 return ret;
482}
483
484#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
485int uclass_unbind_device(struct udevice *dev)
486{
487 struct uclass *uc;
488 int ret;
489
490 uc = dev->uclass;
491 if (uc->uc_drv->pre_unbind) {
492 ret = uc->uc_drv->pre_unbind(dev);
493 if (ret)
494 return ret;
495 }
496
497 list_del(&dev->uclass_node);
498 return 0;
499}
500#endif
501
502int uclass_resolve_seq(struct udevice *dev)
503{
504 struct udevice *dup;
505 int seq;
506 int ret;
507
508 assert(dev->seq == -1);
509 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq,
510 false, &dup);
511 if (!ret) {
512 dm_warn("Device '%s': seq %d is in use by '%s'\n",
513 dev->name, dev->req_seq, dup->name);
514 } else if (ret == -ENODEV) {
515
516 if (dev->req_seq != -1)
517 return dev->req_seq;
518 } else {
519 return ret;
520 }
521
522 for (seq = 0; seq < DM_MAX_SEQ; seq++) {
523 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq,
524 false, &dup);
525 if (ret == -ENODEV)
526 break;
527 if (ret)
528 return ret;
529 }
530 return seq;
531}
532
533int uclass_pre_probe_device(struct udevice *dev)
534{
535 struct uclass_driver *uc_drv;
536 int ret;
537
538 uc_drv = dev->uclass->uc_drv;
539 if (uc_drv->pre_probe) {
540 ret = uc_drv->pre_probe(dev);
541 if (ret)
542 return ret;
543 }
544
545 if (!dev->parent)
546 return 0;
547 uc_drv = dev->parent->uclass->uc_drv;
548 if (uc_drv->child_pre_probe)
549 return uc_drv->child_pre_probe(dev);
550
551 return 0;
552}
553
554int uclass_post_probe_device(struct udevice *dev)
555{
556 struct uclass_driver *uc_drv = dev->uclass->uc_drv;
557
558 if (uc_drv->post_probe)
559 return uc_drv->post_probe(dev);
560
561 return 0;
562}
563
564#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
565int uclass_pre_remove_device(struct udevice *dev)
566{
567 struct uclass *uc;
568 int ret;
569
570 uc = dev->uclass;
571 if (uc->uc_drv->pre_remove) {
572 ret = uc->uc_drv->pre_remove(dev);
573 if (ret)
574 return ret;
575 }
576
577 return 0;
578}
579#endif
580