1 /*
2  * xen/arch/arm/device.c
3  *
4  * Helpers to use a device retrieved via the device tree.
5  *
6  * Julien Grall <julien.grall@linaro.org>
7  * Copyright (C) 2013 Linaro Limited.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19 
20 #include <asm/device.h>
21 #include <xen/errno.h>
22 #include <xen/init.h>
23 #include <xen/lib.h>
24 
25 extern const struct device_desc _sdevice[], _edevice[];
26 extern const struct acpi_device_desc _asdevice[], _aedevice[];
27 
device_init(struct dt_device_node * dev,enum device_class class,const void * data)28 int __init device_init(struct dt_device_node *dev, enum device_class class,
29                        const void *data)
30 {
31     const struct device_desc *desc;
32 
33     ASSERT(dev != NULL);
34 
35     if ( !dt_device_is_available(dev) || dt_device_for_passthrough(dev) )
36         return  -ENODEV;
37 
38     for ( desc = _sdevice; desc != _edevice; desc++ )
39     {
40         if ( desc->class != class )
41             continue;
42 
43         if ( dt_match_node(desc->dt_match, dev) )
44         {
45             ASSERT(desc->init != NULL);
46 
47             return desc->init(dev, data);
48         }
49 
50     }
51 
52     return -EBADF;
53 }
54 
acpi_device_init(enum device_class class,const void * data,int class_type)55 int __init acpi_device_init(enum device_class class, const void *data, int class_type)
56 {
57     const struct acpi_device_desc *desc;
58 
59     for ( desc = _asdevice; desc != _aedevice; desc++ )
60     {
61         if ( ( desc->class != class ) || ( desc->class_type != class_type ) )
62             continue;
63 
64         ASSERT(desc->init != NULL);
65 
66         return desc->init(data);
67     }
68 
69     return -EBADF;
70 }
71 
device_get_class(const struct dt_device_node * dev)72 enum device_class device_get_class(const struct dt_device_node *dev)
73 {
74     const struct device_desc *desc;
75 
76     ASSERT(dev != NULL);
77 
78     for ( desc = _sdevice; desc != _edevice; desc++ )
79     {
80         if ( dt_match_node(desc->dt_match, dev) )
81             return desc->class;
82     }
83 
84     return DEVICE_UNKNOWN;
85 }
86 
87 /*
88  * Local variables:
89  * mode: C
90  * c-file-style: "BSD"
91  * c-basic-offset: 4
92  * indent-tabs-mode: nil
93  * End:
94  */
95