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 inst->CurState = UDI_MASTATE_USAGEIND;
56 // Next State: _SECBIND
58 // Add to global list of active instances
59 inst->Next = gpUDI_ActiveInstances;
60 gpUDI_ActiveInstances = inst;
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
66 UDI_GCB(cb)->initiator_context = inst;
67 udi_usage_ind(cb, UDI_RESOURCES_NORMAL);
68 // udi_usage_res causes next state transition
73 tUDI_DriverRegion *UDI_MA_InitRegion(tUDI_DriverInstance *Inst,
74 udi_ubit16_t Index, udi_ubit16_t Type, size_t RDataSize)
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
85 void UDI_MA_BeginEnumeration(tUDI_DriverInstance *Inst)
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
100 * Returns number of matching attributes
101 * If an attribute differs, returns 0
103 int UDI_MA_CheckDeviceMatch(int nDevAttr, udi_instance_attr_list_t *DevAttrs,
104 int nEnumAttr, udi_instance_attr_list_t *EnumAttrs)
106 // TODO: Ask metalangauge instead
108 for( int i = 0; i < nDevAttr; i ++ )
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 ++ )
114 if( strcmp(dev_attr->attr_name, EnumAttrs[j].attr_name) == 0 ) {
115 enum_attr = &EnumAttrs[j];
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)
126 if( enum_attr->attr_length != dev_attr->attr_length )
128 if( memcmp(enum_attr->attr_value, dev_attr->attr_value, dev_attr->attr_length) != 0 )
134 // Attribute desired is missing, error?
135 //LOG("attr '%s' missing", dev_attr->attr_name);
138 //LOG("n_matches = %i", n_matches);
142 void UDI_MA_AddChild(udi_enumerate_cb_t *cb, udi_index_t ops_idx)
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);
149 // Search for existing child with same child_ID and ops
150 for( tUDI_ChildBinding *child = inst->FirstChild; child; child = child->Next )
152 if( child->ChildID == cb->child_ID && child->Ops->ops_idx == ops_idx ) {
153 LOG("duplicate, not creating");
158 // Create a new child
159 tUDI_ChildBinding *child = NEW_wA(tUDI_ChildBinding, Attribs, cb->attr_valid_length);
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);
168 // - Locate child_bind_ops definition
169 for( int i = 0; i < inst->Module->nChildBindOps; i ++ )
171 if( inst->Module->ChildBindOps[i].ops_idx == ops_idx ) {
172 child->BindOps = &inst->Module->ChildBindOps[i];
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);
183 child->Next = inst->FirstChild;
184 inst->FirstChild = child;
186 // and search for a handler
187 tUDI_MetaLang *metalang = UDI_int_GetMetaLang(inst, child->Ops->meta_idx);
189 tUDI_DriverModule *best_module = NULL;
190 for( tUDI_DriverModule *module = gpUDI_LoadedModules; module; module = module->Next )
192 for( int i = 0; i < module->nDevices; i ++ )
194 if( module->Devices[i]->Metalang != metalang )
197 int level = UDI_MA_CheckDeviceMatch(
198 module->Devices[i]->nAttribs, module->Devices[i]->Attribs,
199 child->nAttribs, child->Attribs
201 if( level > best_level ) {
203 best_module = module;
207 if( best_module != NULL )
209 UDI_MA_CreateInstance(best_module, inst, child);
213 void UDI_MA_BindParents(tUDI_DriverModule *Module)
215 // Scan active instances for enumerated children that can be handled by this module
216 for( int i = 0; i < Module->nDevices; i ++ )
218 // TODO: Have list of unbound enumerated children
219 for( tUDI_DriverInstance *inst = gpUDI_ActiveInstances; inst; inst = inst->Next )
222 for( tUDI_ChildBinding *child = inst->FirstChild; child; child = child->Next )
224 if( child->BoundInstance )
227 int level = UDI_MA_CheckDeviceMatch(
228 Module->Devices[i]->nAttribs, Module->Devices[i]->Attribs,
229 child->nAttribs, child->Attribs
231 // No match: Continue
234 // Found a match, so create an instance (binding happens async)
235 UDI_MA_CreateInstance(Module, inst, child);
241 void UDI_MA_TransitionState(tUDI_DriverInstance *Inst, enum eUDI_MAState Src, enum eUDI_MAState Dst)
244 if( Inst->CurState != Src )
249 case UDI_MASTATE_USAGEIND:
250 ASSERT(Dst != UDI_MASTATE_USAGEIND);
252 case UDI_MASTATE_SECBIND:
253 Inst->CurState = UDI_MASTATE_SECBIND;
254 for( int i = 1; i < Inst->Module->nRegions; i ++ )
256 // TODO: Bind secondaries to primary
257 Log_Warning("UDI", "TODO: Bind secondary channels");
258 //inst->Regions[i]->PriChannel = UDI_CreateChannel_Blank(
260 //UDI_MA_TransitionState(Inst, UDI_MASTATE_SECBIND, UDI_MASTATE_PARENTBIND);
262 case UDI_MASTATE_PARENTBIND:
263 Inst->CurState = UDI_MASTATE_PARENTBIND;
266 tUDI_DriverModule *Module = Inst->Module;
267 tUDI_ChildBinding *parent_bind = Inst->ParentChildBinding;
268 // TODO: Handle multi-parent drivers
269 ASSERTC(Module->nParents, ==, 1);
272 tUDI_BindOps *parent = &Module->Parents[0];
273 udi_channel_t channel = UDI_CreateChannel_Blank(UDI_int_GetMetaLang(Inst, parent->meta_idx));
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);
281 if( parent->bind_cb_idx == 0 )
284 bind_cb = udi_cb_alloc_internal(Inst, parent->bind_cb_idx, channel);
286 Log_Warning("UDI", "Bind CB index is invalid");
289 UDI_int_ChannelFlip( bind_cb );
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);
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;
307 udi_channel_event_ind(ev_cb);
310 //UDI_MA_TransitionState(Inst, UDI_MASTATE_PARENTBIND, UDI_MASTATE_ENUMCHILDREN);
312 case UDI_MASTATE_ENUMCHILDREN:
313 Inst->CurState = UDI_MASTATE_ENUMCHILDREN;
314 UDI_MA_BeginEnumeration(Inst);
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);