1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
|
/*
* Copyright (c) 2013 Google, Inc
*
* (C) Copyright 2012
* Pavel Herrmann <morpheus.ibis@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <dm.h>
#include <errno.h>
#include <malloc.h>
#include <dm/device.h>
#include <dm/device-internal.h>
#include <dm/lists.h>
#include <dm/uclass.h>
#include <dm/uclass-internal.h>
#include <dm/util.h>
DECLARE_GLOBAL_DATA_PTR;
struct uclass *uclass_find(enum uclass_id key)
{
struct uclass *uc;
if (!gd->dm_root)
return NULL;
/*
* TODO(sjg@chromium.org): Optimise this, perhaps moving the found
* node to the start of the list, or creating a linear array mapping
* id to node.
*/
list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
if (uc->uc_drv->id == key)
return uc;
}
return NULL;
}
/**
* uclass_add() - Create new uclass in list
* @id: Id number to create
* @ucp: Returns pointer to uclass, or NULL on error
* @return 0 on success, -ve on error
*
* The new uclass is added to the list. There must be only one uclass for
* each id.
*/
static int uclass_add(enum uclass_id id, struct uclass **ucp)
{
struct uclass_driver *uc_drv;
struct uclass *uc;
int ret;
*ucp = NULL;
uc_drv = lists_uclass_lookup(id);
if (!uc_drv) {
debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
id);
/*
* Use a strange error to make this case easier to find. When
* a uclass is not available it can prevent driver model from
* starting up and this failure is otherwise hard to debug.
*/
return -EPFNOSUPPORT;
}
uc = calloc(1, sizeof(*uc));
if (!uc)
return -ENOMEM;
if (uc_drv->priv_auto_alloc_size) {
uc->priv = calloc(1, uc_drv->priv_auto_alloc_size);
if (!uc->priv) {
ret = -ENOMEM;
goto fail_mem;
}
}
uc->uc_drv = uc_drv;
INIT_LIST_HEAD(&uc->sibling_node);
INIT_LIST_HEAD(&uc->dev_head);
list_add(&uc->sibling_node, &DM_UCLASS_ROOT_NON_CONST);
if (uc_drv->init) {
ret = uc_drv->init(uc);
if (ret)
goto fail;
}
*ucp = uc;
return 0;
fail:
if (uc_drv->priv_auto_alloc_size) {
free(uc->priv);
uc->priv = NULL;
}
list_del(&uc->sibling_node);
fail_mem:
free(uc);
return ret;
}
int uclass_destroy(struct uclass *uc)
{
struct uclass_driver *uc_drv;
struct udevice *dev;
int ret;
/*
* We cannot use list_for_each_entry_safe() here. If a device in this
* uclass has a child device also in this uclass, it will be also be
* unbound (by the recursion in the call to device_unbind() below).
* We can loop until the list is empty.
*/
while (!list_empty(&uc->dev_head)) {
dev = list_first_entry(&uc->dev_head, struct udevice,
uclass_node);
ret = device_remove(dev, DM_REMOVE_NORMAL);
if (ret)
return ret;
ret = device_unbind(dev);
if (ret)
return ret;
}
uc_drv = uc->uc_drv;
if (uc_drv->destroy)
uc_drv->destroy(uc);
list_del(&uc->sibling_node);
if (uc_drv->priv_auto_alloc_size)
free(uc->priv);
free(uc);
return 0;
}
int uclass_get(enum uclass_id id, struct uclass **ucp)
{
struct uclass *uc;
*ucp = NULL;
uc = uclass_find(id);
if (!uc)
return uclass_add(id, ucp);
*ucp = uc;
return 0;
}
const char *uclass_get_name(enum uclass_id id)
{
struct uclass *uc;
if (uclass_get(id, &uc))
return NULL;
return uc->uc_drv->name;
}
int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
{
struct uclass *uc;
struct udevice *dev;
int ret;
*devp = NULL;
ret = uclass_get(id, &uc);
if (ret)
return ret;
if (list_empty(&uc->dev_head))
return -ENODEV;
list_for_each_entry(dev, &uc->dev_head, uclass_node) {
if (!index--) {
*devp = dev;
return 0;
}
}
return -ENODEV;
}
int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
{
struct uclass *uc;
int ret;
*devp = NULL;
ret = uclass_get(id, &uc);
if (ret)
return ret;
if (list_empty(&uc->dev_head))
return 0;
*devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
return 0;
}
int uclass_find_next_device(struct udevice **devp)
{
struct udevice *dev = *devp;
*devp = NULL;
if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
return 0;
*devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
return 0;
}
int uclass_find_device_by_name(enum uclass_id id, const char *name,
struct udevice **devp)
{
struct uclass *uc;
struct udevice *dev;
int ret;
*devp = NULL;
if (!name)
return -EINVAL;
ret = uclass_get(id, &uc);
if (ret)
return ret;
list_for_each_entry(dev, &uc->dev_head, uclass_node) {
if (!strncmp(dev->name, name, strlen(name))) {
*devp = dev;
return 0;
}
}
return -ENODEV;
}
int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq,
bool find_req_seq, struct udevice **devp)
{
struct uclass *uc;
struct udevice *dev;
int ret;
*devp = NULL;
debug("%s: %d %d\n", __func__, find_req_seq, seq_or_req_seq);
if (seq_or_req_seq == -1)
return -ENODEV;
ret = uclass_get(id, &uc);
if (ret)
return ret;
list_for_each_entry(dev, &uc->dev_head, uclass_node) {
debug(" - %d %d '%s'\n", dev->req_seq, dev->seq, dev->name);
if ((find_req_seq ? dev->req_seq : dev->seq) ==
seq_or_req_seq) {
*devp = dev;
debug(" - found\n");
return 0;
}
}
debug(" - not found\n");
return -ENODEV;
}
int uclass_find_device_by_of_offset(enum uclass_id id, int node,
struct udevice **devp)
{
struct uclass *uc;
struct udevice *dev;
int ret;
*devp = NULL;
if (node < 0)
return -ENODEV;
ret = uclass_get(id, &uc);
if (ret)
return ret;
list_for_each_entry(dev, &uc->dev_head, uclass_node) {
if (dev_of_offset(dev) == node) {
*devp = dev;
return 0;
}
}
return -ENODEV;
}
int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
struct udevice **devp)
{
struct uclass *uc;
struct udevice *dev;
int ret;
*devp = NULL;
if (!ofnode_valid(node))
return -ENODEV;
ret = uclass_get(id, &uc);
if (ret)
return ret;
list_for_each_entry(dev, &uc->dev_head, uclass_node) {
if (ofnode_equal(dev_ofnode(dev), node)) {
*devp = dev;
return 0;
}
}
return -ENODEV;
}
#if CONFIG_IS_ENABLED(OF_CONTROL)
static int uclass_find_device_by_phandle(enum uclass_id id,
struct udevice *parent,
const char *name,
struct udevice **devp)
{
struct udevice *dev;
struct uclass *uc;
int find_phandle;
int ret;
*devp = NULL;
find_phandle = dev_read_u32_default(parent, name, -1);
if (find_phandle <= 0)
return -ENOENT;
ret = uclass_get(id, &uc);
if (ret)
return ret;
list_for_each_entry(dev, &uc->dev_head, uclass_node) {
uint phandle;
phandle = dev_read_phandle(dev);
if (phandle == find_phandle) {
*devp = dev;
return 0;
}
}
return -ENODEV;
}
#endif
int uclass_get_device_by_driver(enum uclass_id id,
const struct driver *find_drv,
struct udevice **devp)
{
struct udevice *dev;
struct uclass *uc;
int ret;
ret = uclass_get(id, &uc);
if (ret)
return ret;
list_for_each_entry(dev, &uc->dev_head, uclass_node) {
if (dev->driver == find_drv)
return uclass_get_device_tail(dev, 0, devp);
}
return -ENODEV;
}
int uclass_get_device_tail(struct udevice *dev, int ret,
struct udevice **devp)
{
if (ret)
return ret;
assert(dev);
ret = device_probe(dev);
if (ret)
return ret;
*devp = dev;
return 0;
}
int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
{
struct udevice *dev;
int ret;
*devp = NULL;
ret = uclass_find_device(id, index, &dev);
return uclass_get_device_tail(dev, ret, devp);
}
int uclass_get_device_by_name(enum uclass_id id, const char *name,
struct udevice **devp)
{
struct udevice *dev;
int ret;
*devp = NULL;
ret = uclass_find_device_by_name(id, name, &dev);
return uclass_get_device_tail(dev, ret, devp);
}
int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
{
struct udevice *dev;
int ret;
*devp = NULL;
ret = uclass_find_device_by_seq(id, seq, false, &dev);
if (ret == -ENODEV) {
/*
* We didn't find it in probed devices. See if there is one
* that will request this seq if probed.
*/
ret = uclass_find_device_by_seq(id, seq, true, &dev);
}
return uclass_get_device_tail(dev, ret, devp);
}
int uclass_get_device_by_of_offset(enum uclass_id id, int node,
struct udevice **devp)
{
struct udevice *dev;
int ret;
*devp = NULL;
ret = uclass_find_device_by_of_offset(id, node, &dev);
return uclass_get_device_tail(dev, ret, devp);
}
int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
struct udevice **devp)
{
struct udevice *dev;
int ret;
*devp = NULL;
ret = uclass_find_device_by_ofnode(id, node, &dev);
return uclass_get_device_tail(dev, ret, devp);
}
#if CONFIG_IS_ENABLED(OF_CONTROL)
int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
const char *name, struct udevice **devp)
{
struct udevice *dev;
int ret;
*devp = NULL;
ret = uclass_find_device_by_phandle(id, parent, name, &dev);
return uclass_get_device_tail(dev, ret, devp);
}
#endif
int uclass_first_device(enum uclass_id id, struct udevice **devp)
{
struct udevice *dev;
int ret;
*devp = NULL;
ret = uclass_find_first_device(id, &dev);
if (!dev)
return 0;
return uclass_get_device_tail(dev, ret, devp);
}
int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
{
int ret;
ret = uclass_first_device(id, devp);
if (ret)
return ret;
else if (!*devp)
return -ENODEV;
return 0;
}
int uclass_next_device(struct udevice **devp)
{
struct udevice *dev = *devp;
int ret;
*devp = NULL;
ret = uclass_find_next_device(&dev);
if (!dev)
return 0;
return uclass_get_device_tail(dev, ret, devp);
}
int uclass_bind_device(struct udevice *dev)
{
struct uclass *uc;
int ret;
uc = dev->uclass;
list_add_tail(&dev->uclass_node, &uc->dev_head);
if (dev->parent) {
struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
if (uc_drv->child_post_bind) {
ret = uc_drv->child_post_bind(dev);
if (ret)
goto err;
}
}
return 0;
err:
/* There is no need to undo the parent's post_bind call */
list_del(&dev->uclass_node);
return ret;
}
#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
int uclass_unbind_device(struct udevice *dev)
{
struct uclass *uc;
int ret;
uc = dev->uclass;
if (uc->uc_drv->pre_unbind) {
ret = uc->uc_drv->pre_unbind(dev);
if (ret)
return ret;
}
list_del(&dev->uclass_node);
return 0;
}
#endif
int uclass_resolve_seq(struct udevice *dev)
{
struct udevice *dup;
int seq;
int ret;
assert(dev->seq == -1);
ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq,
false, &dup);
if (!ret) {
dm_warn("Device '%s': seq %d is in use by '%s'\n",
dev->name, dev->req_seq, dup->name);
} else if (ret == -ENODEV) {
/* Our requested sequence number is available */
if (dev->req_seq != -1)
return dev->req_seq;
} else {
return ret;
}
for (seq = 0; seq < DM_MAX_SEQ; seq++) {
ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq,
false, &dup);
if (ret == -ENODEV)
break;
if (ret)
return ret;
}
return seq;
}
int uclass_pre_probe_device(struct udevice *dev)
{
struct uclass_driver *uc_drv;
int ret;
uc_drv = dev->uclass->uc_drv;
if (uc_drv->pre_probe) {
ret = uc_drv->pre_probe(dev);
if (ret)
return ret;
}
if (!dev->parent)
return 0;
uc_drv = dev->parent->uclass->uc_drv;
if (uc_drv->child_pre_probe)
return uc_drv->child_pre_probe(dev);
return 0;
}
int uclass_post_probe_device(struct udevice *dev)
{
struct uclass_driver *uc_drv = dev->uclass->uc_drv;
if (uc_drv->post_probe)
return uc_drv->post_probe(dev);
return 0;
}
#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
int uclass_pre_remove_device(struct udevice *dev)
{
struct uclass *uc;
int ret;
uc = dev->uclass;
if (uc->uc_drv->pre_remove) {
ret = uc->uc_drv->pre_remove(dev);
if (ret)
return ret;
}
return 0;
}
#endif
|