3 * - By John Hodge (thePowersGang)
10 #include "udi_internal.h"
17 const udi_cb_init_t cUDI_MgmtCbInitList[] = {
18 {MGMT_CB_ENUM,0,0, 0,0,NULL},
23 tUDI_DriverInstance *UDI_MA_CreateChildInstance(tUDI_DriverModule *Module,
24 tUDI_DriverInstance *ParentInstance, tUDI_ChildBinding *ChildBinding);
27 tUDI_DriverInstance *gpUDI_ActiveInstances;
30 tUDI_DriverInstance *UDI_MA_CreateInstance(tUDI_DriverModule *DriverModule)
32 tUDI_DriverInstance *inst = NEW_wA(tUDI_DriverInstance, Regions, DriverModule->nRegions);
33 udi_primary_init_t *pri_init = DriverModule->InitInfo->primary_init_info;
34 inst->Module = DriverModule;
35 inst->Regions[0] = UDI_MA_InitRegion(inst, 0, 0, pri_init->rdata_size);
36 udi_secondary_init_t *sec_init = DriverModule->InitInfo->secondary_init_list;
39 for( int i = 0; sec_init[i].region_idx; i ++ )
41 inst->Regions[1+i] = UDI_MA_InitRegion(inst, i,
42 sec_init[i].region_idx, sec_init[i].rdata_size);
46 inst->ManagementChannel = UDI_CreateChannel_Blank(&cMetaLang_Management);
47 UDI_BindChannel_Raw(inst->ManagementChannel, true,
48 inst, 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 // Send usage indication
56 char scratch[pri_init->mgmt_scratch_requirement];
59 memset(&ucb, 0, sizeof(ucb));
60 ucb.gcb.scratch = scratch;
61 ucb.gcb.channel = inst->ManagementChannel;
62 udi_usage_ind(&ucb, UDI_RESOURCES_NORMAL);
63 // TODO: Ensure that udi_usage_res is called
66 for( int i = 1; i < DriverModule->nRegions; i ++ )
68 // TODO: Bind secondaries to primary
69 Log_Warning("UDI", "TODO: Bind secondary channels");
70 //inst->Regions[i]->PriChannel = UDI_CreateChannel_Blank(
73 // Add to global list of active instances
74 inst->Next = gpUDI_ActiveInstances;
75 gpUDI_ActiveInstances = inst;
80 tUDI_DriverRegion *UDI_MA_InitRegion(tUDI_DriverInstance *Inst,
81 udi_ubit16_t Index, udi_ubit16_t Type, size_t RDataSize)
83 // ASSERTCR( RDataSize, <=, UDI_MIN_ALLOC_LIMIT, NULL );
84 ASSERTCR( RDataSize, >=, sizeof(udi_init_context_t), NULL );
85 tUDI_DriverRegion *rgn = NEW(tUDI_DriverRegion,+RDataSize);
86 rgn->InitContext = (void*)(rgn+1);
87 rgn->InitContext->region_idx = Type;
88 // rgn->InitContext->limits
92 void UDI_MA_BeginEnumeration(tUDI_DriverInstance *Inst)
94 udi_primary_init_t *pri_init = Inst->Module->InitInfo->primary_init_info;
95 udi_enumerate_cb_t *ecb = udi_cb_alloc_internal(NULL, MGMT_CB_ENUM, Inst->ManagementChannel);
96 memset(ecb, 0, sizeof(ecb));
97 ecb->gcb.scratch = malloc(pri_init->mgmt_scratch_requirement);
98 ecb->gcb.channel = Inst->ManagementChannel;
99 ecb->child_data = malloc(pri_init->child_data_size);
100 ecb->attr_list = NEW(udi_instance_attr_list_t, *pri_init->enumeration_attr_list_length);
101 ecb->attr_valid_length = 0;
102 udi_enumerate_req(ecb, UDI_ENUMERATE_START);
103 Threads_Yield(); // Yield to allow udi_enumerate_req to run
107 * Returns number of matching attributes
108 * If an attribute differs, returns 0
110 int UDI_MA_CheckDeviceMatch(int nDevAttr, udi_instance_attr_list_t *DevAttrs,
111 int nEnumAttr, udi_instance_attr_list_t *EnumAttrs)
114 for( int i = 0; i < nDevAttr; i ++ )
116 udi_instance_attr_list_t *dev_attr = &DevAttrs[i];
117 udi_instance_attr_list_t *enum_attr = NULL;
118 for( int j = 0; j < nEnumAttr; j ++ )
120 if( strcmp(dev_attr->attr_name, EnumAttrs[j].attr_name) == 0 ) {
121 enum_attr = &EnumAttrs[j];
127 if( enum_attr->attr_length != dev_attr->attr_length )
129 if( memcmp(enum_attr->attr_value, dev_attr->attr_value, dev_attr->attr_length) != 0 )
135 // Attribute desired is missing, error?
141 void UDI_MA_AddChild(udi_enumerate_cb_t *cb, udi_index_t ops_idx)
143 // Current side is MA, other is instance
144 tUDI_DriverInstance *inst = UDI_int_ChannelGetInstance( UDI_GCB(cb), true );
145 LOG("inst = %p", inst);
147 // Search for existing child with same child_ID and ops
148 for( tUDI_ChildBinding *child = inst->FirstChild; child; child = child->Next )
150 if( child->ChildID == cb->child_ID && child->Ops->ops_idx == ops_idx ) {
151 LOG("duplicate, not creating");
156 // Create a new child
157 tUDI_ChildBinding *child = NEW_wA(tUDI_ChildBinding, Attribs, cb->attr_valid_length);
159 child->ChildID = cb->child_ID;
160 child->Ops = UDI_int_GetOps(inst, ops_idx);
161 child->BoundInstance = NULL; // currently unbound
162 child->BindOps = NULL;
163 child->nAttribs = cb->attr_valid_length;
164 memcpy(child->Attribs, cb->attr_list, sizeof(cb->attr_list[0])*cb->attr_valid_length);
166 // - Locate child_bind_ops definition
167 for( int i = 0; i < inst->Module->nChildBindOps; i ++ )
169 LOG(" %i == %i?", inst->Module->ChildBindOps[i].ops_idx, ops_idx);
170 if( inst->Module->ChildBindOps[i].ops_idx == ops_idx ) {
171 child->BindOps = &inst->Module->ChildBindOps[i];
175 if( !child->BindOps ) {
176 Log_Error("UDI", "Driver '%s' doesn't have a 'child_bind_ops' for %i",
177 inst->Module->ModuleName, ops_idx);
182 child->Next = inst->FirstChild;
183 inst->FirstChild = child;
185 // and search for a handler
186 tUDI_MetaLang *metalang = UDI_int_GetMetaLang(inst, child->Ops->meta_idx);
188 tUDI_DriverModule *best_module = NULL;
189 for( tUDI_DriverModule *module = gpUDI_LoadedModules; module; module = module->Next )
191 for( int i = 0; i < module->nDevices; i ++ )
193 if( module->Devices[i]->Metalang != metalang )
196 int level = UDI_MA_CheckDeviceMatch(
197 module->Devices[i]->nAttribs, module->Devices[i]->Attribs,
198 child->nAttribs, child->Attribs
200 if( level > best_level ) {
202 best_module = module;
206 if( best_module != NULL )
208 UDI_MA_CreateChildInstance(best_module, inst, child);
212 void UDI_MA_BindParents(tUDI_DriverModule *Module)
217 tUDI_DriverInstance *UDI_MA_CreateChildInstance(tUDI_DriverModule *Module,
218 tUDI_DriverInstance *ParentInstance, tUDI_ChildBinding *ChildBinding)
220 // Found a match, so create an instance and bind it
221 tUDI_DriverInstance *inst = UDI_MA_CreateInstance(Module);
222 ChildBinding->BoundInstance = inst;
224 // TODO: Handle multi-parent drivers
225 ASSERTC(Module->nParents, ==, 1);
228 tUDI_BindOps *parent = &Module->Parents[0];
229 udi_channel_t channel = UDI_CreateChannel_Blank( UDI_int_GetMetaLang(inst, parent->meta_idx) );
231 UDI_BindChannel(channel,true, inst, parent->ops_idx, parent->region_idx);
232 UDI_BindChannel(channel,false,
233 ParentInstance, ChildBinding->Ops->ops_idx, ChildBinding->BindOps->region_idx);
235 udi_cb_t *bind_cb = udi_cb_alloc_internal(inst, parent->bind_cb_idx, channel);
237 Log_Warning("UDI", "Bind CB index is invalid");
241 udi_channel_event_cb_t ev_cb;
242 int n_handles = Module->InitInfo->primary_init_info->per_parent_paths;
243 udi_buf_path_t handles[n_handles];
244 memset(&ev_cb, 0, sizeof(ev_cb));
245 ev_cb.gcb.channel = channel;
246 ev_cb.event = UDI_CHANNEL_BOUND;
247 ev_cb.params.parent_bound.bind_cb = bind_cb;
248 ev_cb.params.parent_bound.parent_ID = 1;
249 ev_cb.params.parent_bound.path_handles = handles;
251 for( int i = 0; i < n_handles; i ++ ) {
252 //handles[i] = udi_buf_path_alloc_internal(inst);
256 udi_channel_event_ind(&ev_cb);