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

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