3 * - By John Hodge (thePowersGang)
11 #include <udi_internal.h>
12 #include <udi_internal_ma.h>
19 tUDI_DriverInstance *gpUDI_ActiveInstances;
22 tUDI_DriverInstance *UDI_MA_CreateInstance(tUDI_DriverModule *DriverModule,
23 tUDI_DriverInstance *ParentInstance, tUDI_ChildBinding *ChildBinding)
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;
32 for( int i = 0; sec_init[i].region_idx; i ++ )
34 inst->Regions[1+i] = UDI_MA_InitRegion(inst, i,
35 sec_init[i].region_idx, sec_init[i].rdata_size);
39 if( ParentInstance ) {
41 ChildBinding->BoundInstance = inst;
43 inst->Parent = ParentInstance;
44 inst->ParentChildBinding = ChildBinding;
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?
52 // for( int i = 0; i < DriverModule->nRegions; i ++ )
53 // Log("Rgn %i: %p", i, inst->Regions[i]);
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
60 // Add to global list of active instances
61 inst->Next = gpUDI_ActiveInstances;
62 gpUDI_ActiveInstances = inst;
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
68 UDI_GCB(cb)->initiator_context = inst;
69 udi_usage_ind(cb, UDI_RESOURCES_NORMAL);
70 // udi_usage_res causes next state transition
75 tUDI_DriverRegion *UDI_MA_InitRegion(tUDI_DriverInstance *Inst,
76 udi_ubit16_t Index, udi_ubit16_t Type, size_t RDataSize)
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
87 void UDI_MA_BeginEnumeration(tUDI_DriverInstance *Inst)
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
102 * Returns number of matching attributes
103 * If an attribute differs, returns 0
105 int UDI_MA_CheckDeviceMatch(int nDevAttr, udi_instance_attr_list_t *DevAttrs,
106 int nEnumAttr, udi_instance_attr_list_t *EnumAttrs)
108 // TODO: Ask metalangauge instead
110 for( int i = 0; i < nDevAttr; i ++ )
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 ++ )
116 if( strcmp(dev_attr->attr_name, EnumAttrs[j].attr_name) == 0 ) {
117 enum_attr = &EnumAttrs[j];
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)
128 if( enum_attr->attr_length != dev_attr->attr_length )
130 if( memcmp(enum_attr->attr_value, dev_attr->attr_value, dev_attr->attr_length) != 0 )
136 // Attribute desired is missing, error?
137 //LOG("attr '%s' missing", dev_attr->attr_name);
140 //LOG("n_matches = %i", n_matches);
144 void UDI_MA_AddChild(udi_enumerate_cb_t *cb, udi_index_t ops_idx)
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);
151 // Search for existing child with same child_ID and ops
152 for( tUDI_ChildBinding *child = inst->FirstChild; child; child = child->Next )
154 if( child->ChildID == cb->child_ID && child->Ops->ops_idx == ops_idx ) {
155 LOG("duplicate, not creating");
160 // Create a new child
161 tUDI_ChildBinding *child = NEW_wA(tUDI_ChildBinding, Attribs, cb->attr_valid_length);
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);
170 // - Locate child_bind_ops definition
171 for( int i = 0; i < inst->Module->nChildBindOps; i ++ )
173 if( inst->Module->ChildBindOps[i].ops_idx == ops_idx ) {
174 child->BindOps = &inst->Module->ChildBindOps[i];
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);
185 child->Next = inst->FirstChild;
186 inst->FirstChild = child;
188 // and search for a handler
189 child->Metalang = UDI_int_GetMetaLang(inst->Module, child->Ops->meta_idx);
191 tUDI_DriverModule *best_module = NULL;
192 for( tUDI_DriverModule *module = gpUDI_LoadedModules; module; module = module->Next )
194 for( int i = 0; i < module->nDevices; i ++ )
196 //LOG("%s:%i %p ?== %p",
197 // module->ModuleName, i,
198 // module->Devices[i]->Metalang, metalang);
199 if( module->Devices[i]->Metalang != child->Metalang )
202 int level = UDI_MA_CheckDeviceMatch(
203 module->Devices[i]->nAttribs, module->Devices[i]->Attribs,
204 child->nAttribs, child->Attribs
206 if( level > best_level ) {
208 best_module = module;
212 if( best_module != NULL )
214 UDI_MA_CreateInstance(best_module, inst, child);
218 void UDI_MA_BindParents(tUDI_DriverModule *Module)
220 // Scan active instances for enumerated children that can be handled by this module
221 for( int i = 0; i < Module->nDevices; i ++ )
223 // TODO: Have list of unbound enumerated children
224 for( tUDI_DriverInstance *inst = gpUDI_ActiveInstances; inst; inst = inst->Next )
227 for( tUDI_ChildBinding *child = inst->FirstChild; child; child = child->Next )
229 if( child->BoundInstance )
231 if( Module->Devices[i]->Metalang != child->Metalang )
234 int level = UDI_MA_CheckDeviceMatch(
235 Module->Devices[i]->nAttribs, Module->Devices[i]->Attribs,
236 child->nAttribs, child->Attribs
238 // No match: Continue
241 // Found a match, so create an instance (binding happens async)
242 UDI_MA_CreateInstance(Module, inst, child);
248 void UDI_MA_TransitionState(tUDI_DriverInstance *Inst, enum eUDI_MAState Src, enum eUDI_MAState Dst)
251 if( Inst->CurState != Src )
254 LOG("Inst %s %p MA state %i->%i",
255 Inst->Module->ModuleName, Inst, Src, Dst);
259 case UDI_MASTATE_USAGEIND:
260 ASSERT(Dst != UDI_MASTATE_USAGEIND);
262 case UDI_MASTATE_SECBIND:
263 Inst->CurState = UDI_MASTATE_SECBIND;
264 for( int i = 1; i < Inst->Module->nRegions; i ++ )
266 // TODO: Bind secondaries to primary
267 Log_Warning("UDI", "TODO: Bind secondary channels");
268 //inst->Regions[i]->PriChannel = UDI_CreateChannel_Blank(
270 //UDI_MA_TransitionState(Inst, UDI_MASTATE_SECBIND, UDI_MASTATE_PARENTBIND);
272 case UDI_MASTATE_PARENTBIND:
273 Inst->CurState = UDI_MASTATE_PARENTBIND;
276 tUDI_DriverModule *Module = Inst->Module;
277 tUDI_ChildBinding *parent_bind = Inst->ParentChildBinding;
278 // TODO: Handle multi-parent drivers
279 ASSERTC(Module->nParents, ==, 1);
282 tUDI_BindOps *parent = &Module->Parents[0];
283 udi_channel_t channel = UDI_CreateChannel_Blank(UDI_int_GetMetaLang(Inst->Module, parent->meta_idx));
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);
291 if( parent->bind_cb_idx == 0 )
294 bind_cb = udi_cb_alloc_internal(Inst, parent->bind_cb_idx, channel);
296 Log_Warning("UDI", "Bind CB index is invalid");
299 UDI_int_ChannelFlip( bind_cb );
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);
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;
317 udi_channel_event_ind(ev_cb);
320 //UDI_MA_TransitionState(Inst, UDI_MASTATE_PARENTBIND, UDI_MASTATE_ENUMCHILDREN);
322 case UDI_MASTATE_ENUMCHILDREN:
323 Inst->CurState = UDI_MASTATE_ENUMCHILDREN;
324 UDI_MA_BeginEnumeration(Inst);
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);