]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/drivers/of/include/of.h
b72339172bc2af9163c94c73da9a56038e9eb212
[l4.git] / l4 / pkg / drivers / of / include / of.h
1 #ifndef L4_PPC32_OF_H__
2 #define L4_PPC32_OF_H__
3
4 #include <stdarg.h>
5 #include <string.h>
6
7 namespace L4_drivers
8 {
9 class Of
10 {
11 private:
12   /* declarations */
13   struct prom_args
14     {
15       const char *service;
16       int nargs;
17       int nret;
18       void *args[10];
19       prom_args(const char *s, int na, int nr) : service(s), nargs(na), nret(nr) {}
20     };
21
22   typedef int (*prom_entry)(struct prom_args *);
23
24   //int prom_call(const char *service, int nargs, int nret, ...) const;
25 protected:
26   enum {
27       PROM_ERROR = -1u
28   };
29
30
31   typedef void *ihandle_t;
32   typedef void *phandle_t;
33
34   typedef struct
35     {
36       unsigned long len;
37       char          data[];
38     } of_item_t;
39
40   /* methods */
41   unsigned prom_call(const char *service, int nargs, int nret, ...) const
42     {
43       struct prom_args args = prom_args(service, nargs, nret);
44       va_list list;
45
46       va_start(list, nret);
47       for(int i = 0; i < nargs; i++)
48         args.args[i] = va_arg(list, void*);
49       va_end(list);
50
51       for(int i = 0; i < nret; i++)
52         args.args[nargs + i] = 0;
53
54       if(_prom()(&args) < 0)
55         return PROM_ERROR;
56
57       return (nret > 0) ? (unsigned long)args.args[nargs] : 0;
58     }
59
60
61   int prom_getprop(phandle_t node, const char *pname,  void *value, 
62       size_t size)
63     {
64       return prom_call("getprop", 4, 1, node, pname, (unsigned long)value,
65           (unsigned long)size);
66     }
67
68   int prom_next_node(phandle_t *nodep)
69     {
70       phandle_t node;
71
72       if ((node = *nodep) != 0
73           && (*nodep = (phandle_t)prom_call("child", 1, 1, node)) != 0)
74         return 1;
75       if ((*nodep = (phandle_t)prom_call("peer", 1, 1, node)) != 0)
76         return 1;
77       for (;;) {
78           if ((node = (phandle_t)prom_call("parent", 1, 1, node)) == 0)
79             return 0;
80           if ((*nodep = (phandle_t)prom_call("peer", 1, 1, node)) != 0)
81             return 1;
82       }
83     }
84
85   template<typename T>
86     static inline bool handle_valid(T p)
87       {
88         return ((unsigned long)p != 0 && (unsigned long)p != PROM_ERROR);
89       }
90
91   static prom_entry _prom(unsigned long prom = 0)
92     {
93       static prom_entry local_prom;
94
95       if(prom)
96         local_prom = reinterpret_cast<prom_entry>(prom);
97
98       return local_prom;
99     }
100
101 public:
102   Of() {};
103
104   static void set_prom(unsigned long prom)
105     {
106       _prom(prom);
107     }
108
109   static unsigned long get_prom()
110     {
111       return reinterpret_cast<unsigned long>(_prom());
112     }
113
114 };
115 }
116
117 #endif /* L4_PPC32_OF_H__*/