Modules/UDI - PIO and better instance startup
[tpg/acess2.git] / KernelLand / Modules / Interfaces / UDI / management_agent.c
1 /*
2  * Acess2 UDI Layer
3  * - By John Hodge (thePowersGang)
4  * 
5  * management_agent.c
6  * - Managment Agent
7  */
8 #define DEBUG   1
9 #include <udi.h>
10 #include <acess.h>
11 #include <udi_internal.h>
12 #include <udi_internal_ma.h>
13
14 // === CONSTANTS ===
15
16 // === PROTOTYPES ===
17
18 // === GLOBALS ===
19 tUDI_DriverInstance     *gpUDI_ActiveInstances;
20
21 // === CODE ===
22 tUDI_DriverInstance *UDI_MA_CreateInstance(tUDI_DriverModule *DriverModule,
23         tUDI_DriverInstance *ParentInstance, tUDI_ChildBinding *ChildBinding)
24 {
25         tUDI_DriverInstance     *inst = NEW_wA(tUDI_DriverInstance, Regions, DriverModule->nRegions);
26         udi_primary_init_t      *pri_init = DriverModule->InitInfo->primary_init_info;
27         inst->Module = DriverModule;
28         inst->Regions[0] = UDI_MA_InitRegion(inst, 0, 0, pri_init->rdata_size);
29         udi_secondary_init_t    *sec_init = DriverModule->InitInfo->secondary_init_list;
30         if( sec_init )
31         {
32                 for( int i = 0; sec_init[i].region_idx; i ++ )
33                 {
34                         inst->Regions[1+i] = UDI_MA_InitRegion(inst, i,
35                                 sec_init[i].region_idx, sec_init[i].rdata_size);
36                 }
37         }
38
39         if( ParentInstance ) {
40                 ASSERT(ChildBinding);
41                 ChildBinding->BoundInstance = inst;
42         }
43         inst->Parent = ParentInstance;
44         inst->ParentChildBinding = ChildBinding;
45
46         inst->ManagementChannel = UDI_CreateChannel_Blank(&cMetaLang_Management);
47         UDI_BindChannel_Raw(inst->ManagementChannel, true,
48                 inst, 0, 0, inst->Regions[0]->InitContext, pri_init->mgmt_ops);
49 //      UDI_BindChannel_Raw(inst->ManagementChannel, false,
50 //              NULL, 1, inst, &cUDI_MgmtOpsList);      // TODO: ops list for management agent?
51
52         for( int i = 0; i < DriverModule->nRegions; i ++ )
53                 Log("Rgn %i: %p", i, inst->Regions[i]);
54
55         inst->CurState = UDI_MASTATE_USAGEIND;
56         // Next State: _SECBIND
57
58         // Add to global list of active instances
59         inst->Next = gpUDI_ActiveInstances;
60         gpUDI_ActiveInstances = inst;
61
62         // Send usage indication
63         udi_usage_cb_t *cb = (void*)udi_cb_alloc_internal_v(&cMetaLang_Management, UDI_MGMT_USAGE_CB_NUM,
64                 0, pri_init->mgmt_scratch_requirement, inst->ManagementChannel
65                 );
66         UDI_GCB(cb)->initiator_context = inst;
67         udi_usage_ind(cb, UDI_RESOURCES_NORMAL);
68         // udi_usage_res causes next state transition
69         
70         return inst;
71 }
72
73 tUDI_DriverRegion *UDI_MA_InitRegion(tUDI_DriverInstance *Inst,
74         udi_ubit16_t Index, udi_ubit16_t Type, size_t RDataSize)
75 {
76 //      ASSERTCR( RDataSize, <=, UDI_MIN_ALLOC_LIMIT, NULL );
77         ASSERTCR( RDataSize, >=, sizeof(udi_init_context_t), NULL );
78         tUDI_DriverRegion       *rgn = NEW(tUDI_DriverRegion,+RDataSize);
79         rgn->InitContext = (void*)(rgn+1);
80         rgn->InitContext->region_idx = Type;
81 //      rgn->InitContext->limits
82         return rgn;
83 }
84
85 void UDI_MA_BeginEnumeration(tUDI_DriverInstance *Inst)
86 {
87         udi_primary_init_t *pri_init = Inst->Module->InitInfo->primary_init_info;
88         udi_enumerate_cb_t      *ecb = (void*)udi_cb_alloc_internal_v(
89                 &cMetaLang_Management, UDI_MGMT_ENUMERATE_CB_NUM,
90                 0, pri_init->mgmt_scratch_requirement, Inst->ManagementChannel);
91         UDI_GCB(ecb)->initiator_context = Inst;
92         ecb->child_data = malloc(pri_init->child_data_size);
93         ecb->attr_list = NEW(udi_instance_attr_list_t, *pri_init->enumeration_attr_list_length);
94         ecb->attr_valid_length = 0;
95         udi_enumerate_req(ecb, UDI_ENUMERATE_START);
96         Threads_Yield();        // Yield to allow udi_enumerate_req to run
97 }
98
99 /*
100  * Returns number of matching attributes
101  * If an attribute differs, returns 0
102  */
103 int UDI_MA_CheckDeviceMatch(int nDevAttr, udi_instance_attr_list_t *DevAttrs,
104         int nEnumAttr, udi_instance_attr_list_t *EnumAttrs)
105 {
106         // TODO: Ask metalangauge instead
107         int n_matches = 0;
108         for( int i = 0; i < nDevAttr; i ++ )
109         {
110                 udi_instance_attr_list_t *dev_attr = &DevAttrs[i];
111                 udi_instance_attr_list_t *enum_attr = NULL;
112                 for( int j = 0; j < nEnumAttr; j ++ )
113                 {
114                         if( strcmp(dev_attr->attr_name, EnumAttrs[j].attr_name) == 0 ) {
115                                 enum_attr = &EnumAttrs[j];
116                                 break;
117                         }
118                 }
119                 if( enum_attr )
120                 {
121                         //LOG("Match = '%s' (%i %x == %i %x)",
122                         //      dev_attr->attr_name,
123                         //      dev_attr->attr_length, UDI_ATTR32_GET(dev_attr->attr_value),
124                         //      enum_attr->attr_length, UDI_ATTR32_GET(enum_attr->attr_value)
125                         //      );
126                         if( enum_attr->attr_length != dev_attr->attr_length )
127                                 return 0;
128                         if( memcmp(enum_attr->attr_value, dev_attr->attr_value, dev_attr->attr_length) != 0 )
129                                 return 0;
130                         n_matches ++;
131                 }
132                 else
133                 {
134                         // Attribute desired is missing, error?
135                         //LOG("attr '%s' missing", dev_attr->attr_name);
136                 }
137         }
138         //LOG("n_matches = %i", n_matches);
139         return n_matches;
140 }
141
142 void UDI_MA_AddChild(udi_enumerate_cb_t *cb, udi_index_t ops_idx)
143 {
144         // Current side is MA, other is instance
145         // TODO: Get region index too?
146         tUDI_DriverInstance *inst = UDI_int_ChannelGetInstance( UDI_GCB(cb), true, NULL );
147         //LOG("inst = %p", inst);
148         
149         // Search for existing child with same child_ID and ops
150         for( tUDI_ChildBinding *child = inst->FirstChild; child; child = child->Next )
151         {
152                 if( child->ChildID == cb->child_ID && child->Ops->ops_idx == ops_idx ) {
153                         LOG("duplicate, not creating");
154                         return;
155                 }
156         }
157         
158         // Create a new child
159         tUDI_ChildBinding *child = NEW_wA(tUDI_ChildBinding, Attribs, cb->attr_valid_length);
160         child->Next = NULL;
161         child->ChildID = cb->child_ID;
162         child->Ops = UDI_int_GetOps(inst, ops_idx);
163         child->BoundInstance = NULL;    // currently unbound
164         child->BindOps = NULL;
165         child->nAttribs = cb->attr_valid_length;
166         memcpy(child->Attribs, cb->attr_list, sizeof(cb->attr_list[0])*cb->attr_valid_length);
167
168         // - Locate child_bind_ops definition
169         for( int i = 0; i < inst->Module->nChildBindOps; i ++ )
170         {
171                 if( inst->Module->ChildBindOps[i].ops_idx == ops_idx ) {
172                         child->BindOps = &inst->Module->ChildBindOps[i];
173                         break;
174                 }
175         }
176         if( !child->BindOps ) {
177                 Log_Error("UDI", "Driver '%s' doesn't have a 'child_bind_ops' for %i",
178                         inst->Module->ModuleName, ops_idx);
179                 free(child);
180                 return ;
181         }
182
183         child->Next = inst->FirstChild;
184         inst->FirstChild = child;
185         
186         // and search for a handler
187         tUDI_MetaLang   *metalang = UDI_int_GetMetaLang(inst, child->Ops->meta_idx);
188          int    best_level = 0;
189         tUDI_DriverModule *best_module = NULL;
190         for( tUDI_DriverModule *module = gpUDI_LoadedModules; module; module = module->Next )
191         {
192                 for( int i = 0; i < module->nDevices; i ++ )
193                 {
194                         if( module->Devices[i]->Metalang != metalang )
195                                 continue ;
196                         
197                         int level = UDI_MA_CheckDeviceMatch(
198                                 module->Devices[i]->nAttribs, module->Devices[i]->Attribs,
199                                 child->nAttribs, child->Attribs
200                                 );
201                         if( level > best_level ) {
202                                 best_level = level;
203                                 best_module = module;
204                         }
205                 }
206         }
207         if( best_module != NULL )
208         {
209                 UDI_MA_CreateInstance(best_module, inst, child);
210         }
211 }
212
213 void UDI_MA_BindParents(tUDI_DriverModule *Module)
214 {
215         // Scan active instances for enumerated children that can be handled by this module
216         for( int i = 0; i < Module->nDevices; i ++ )
217         {
218                 // TODO: Have list of unbound enumerated children
219                 for( tUDI_DriverInstance *inst = gpUDI_ActiveInstances; inst; inst = inst->Next )
220                 {
221                         // Loop children
222                         for( tUDI_ChildBinding *child = inst->FirstChild; child; child = child->Next )
223                         {
224                                 if( child->BoundInstance )
225                                         continue ;
226                                 // Check for match
227                                 int level = UDI_MA_CheckDeviceMatch(
228                                         Module->Devices[i]->nAttribs, Module->Devices[i]->Attribs,
229                                         child->nAttribs, child->Attribs
230                                         );
231                                 // No match: Continue
232                                 if( level == 0 )
233                                         continue ;
234                                 // Found a match, so create an instance (binding happens async)
235                                 UDI_MA_CreateInstance(Module, inst, child);
236                         }
237                 }
238         }
239 }
240
241 void UDI_MA_TransitionState(tUDI_DriverInstance *Inst, enum eUDI_MAState Src, enum eUDI_MAState Dst)
242 {
243         ASSERT(Inst);
244         if( Inst->CurState != Src )
245                 return ;
246         
247         switch(Dst)
248         {
249         case UDI_MASTATE_USAGEIND:
250                 ASSERT(Dst != UDI_MASTATE_USAGEIND);
251                 break;
252         case UDI_MASTATE_SECBIND:
253                 Inst->CurState = UDI_MASTATE_SECBIND;
254                 for( int i = 1; i < Inst->Module->nRegions; i ++ )
255                 {
256                         // TODO: Bind secondaries to primary
257                         Log_Warning("UDI", "TODO: Bind secondary channels");
258                         //inst->Regions[i]->PriChannel = UDI_CreateChannel_Blank(
259                 }
260                 //UDI_MA_TransitionState(Inst, UDI_MASTATE_SECBIND, UDI_MASTATE_PARENTBIND);
261                 //break;
262         case UDI_MASTATE_PARENTBIND:
263                 Inst->CurState = UDI_MASTATE_PARENTBIND;
264                 if( Inst->Parent )
265                 {
266                         tUDI_DriverModule       *Module = Inst->Module;
267                         tUDI_ChildBinding       *parent_bind = Inst->ParentChildBinding;
268                         // TODO: Handle multi-parent drivers
269                         ASSERTC(Module->nParents, ==, 1);
270                         
271                         // Bind to parent
272                         tUDI_BindOps    *parent = &Module->Parents[0];
273                         udi_channel_t channel = UDI_CreateChannel_Blank(UDI_int_GetMetaLang(Inst, parent->meta_idx));
274                         
275                         UDI_BindChannel(channel,true,  Inst, parent->ops_idx, parent->region_idx, NULL,false,0);
276                         UDI_BindChannel(channel,false,
277                                 Inst->Parent, parent_bind->Ops->ops_idx, parent_bind->BindOps->region_idx,
278                                 NULL, true, parent_bind->ChildID);
279
280                         udi_cb_t        *bind_cb;
281                         if( parent->bind_cb_idx == 0 )
282                                 bind_cb = NULL;
283                         else {
284                                 bind_cb = udi_cb_alloc_internal(Inst, parent->bind_cb_idx, channel);
285                                 if( !bind_cb ) {
286                                         Log_Warning("UDI", "Bind CB index is invalid");
287                                         break;
288                                 }
289                                 UDI_int_ChannelFlip( bind_cb );
290                         }
291
292                          int    n_handles = Module->InitInfo->primary_init_info->per_parent_paths;
293                         udi_buf_path_t  handles[n_handles];
294                         for( int i = 0; i < n_handles; i ++ ) {
295                                 //handles[i] = udi_buf_path_alloc_internal(Inst);
296                                 handles[i] = 0;
297                         }
298                         
299                         udi_channel_event_cb_t *ev_cb = (void*)udi_cb_alloc_internal_v(&cMetaLang_Management,
300                                 UDI_MGMT_CHANNEL_EVENT_CB_NUM, 0, 0, channel);
301                         UDI_GCB(ev_cb)->initiator_context = Inst;
302                         ev_cb->event = UDI_CHANNEL_BOUND;
303                         ev_cb->params.parent_bound.bind_cb = bind_cb;
304                         ev_cb->params.parent_bound.parent_ID = 1;
305                         ev_cb->params.parent_bound.path_handles = handles;
306                         
307                         udi_channel_event_ind(ev_cb);
308                         break;
309                 }
310                 //UDI_MA_TransitionState(Inst, UDI_MASTATE_PARENTBIND, UDI_MASTATE_ENUMCHILDREN);
311                 //break;
312         case UDI_MASTATE_ENUMCHILDREN:
313                 Inst->CurState = UDI_MASTATE_ENUMCHILDREN;
314                 UDI_MA_BeginEnumeration(Inst);
315                 break;
316         case UDI_MASTATE_ACTIVE:
317                 Inst->CurState = UDI_MASTATE_ACTIVE;
318                 Log_Log("UDI", "Driver instance %s %p entered active state",
319                         Inst->Module->ModuleName, Inst);
320                 break;
321         }
322 }
323

UCC git Repository :: git.ucc.asn.au