3 * - By John Hodge (thePowersGang)
6 * - UDI Entrypoint and Module loading
9 #define VERSION ((0<<8)|1)
13 #include <udi_internal.h>
14 #include <udi_internal_ma.h>
15 #include <trans_pci.h>
16 #include <trans_nsr.h>
19 int UDI_Install(char **Arguments);
20 int UDI_DetectDriver(void *Base);
21 int UDI_LoadDriver(void *Base);
22 tUDI_DriverModule *UDI_int_LoadDriver(void *LoadBase, const udi_init_t *info, const char *udiprops, size_t udiprops_size);
23 const tUDI_MetaLang *UDI_int_GetMetaLangByName(const char *Name);
26 MODULE_DEFINE(0, VERSION, UDI, UDI_Install, NULL, NULL);
27 tModuleLoader gUDI_Loader = {
28 NULL, "UDI", UDI_DetectDriver, UDI_LoadDriver, NULL
30 tUDI_DriverModule *gpUDI_LoadedModules;
34 * \fn int UDI_Install(char **Arguments)
35 * \brief Stub intialisation routine
37 int UDI_Install(char **Arguments)
39 Module_RegisterLoader( &gUDI_Loader );
41 Proc_SpawnWorker(UDI_int_DeferredThread, NULL);
43 UDI_int_LoadDriver(NULL, &pci_init, pci_udiprops, pci_udiprops_size);
44 UDI_int_LoadDriver(NULL, &acessnsr_init, acessnsr_udiprops, acessnsr_udiprops_size);
50 * \brief Detects if a driver should be loaded by the UDI subsystem
52 int UDI_DetectDriver(void *Base)
56 if( Binary_FindSymbol(Base, "udi_init_info", &unused) == 0) {
59 if( Binary_FindSymbol(Base, "_udiprops", &unused) == 0 ) {
60 Log_Warning("UDI", "Driver has udi_init_info, but no _udiprops symbol");
63 if( Binary_FindSymbol(Base, "_udiprops_end", &unused) == 0) {
64 Log_Warning("UDI", "Driver has udi_init_info, but no _udiprops_end symbol");
72 * \fn int UDI_LoadDriver(void *Base)
74 int UDI_LoadDriver(void *Base)
77 char *udiprops = NULL;
78 char *udiprops_end = 0;
80 if( Binary_FindSymbol(Base, "udi_init_info", (Uint*)&info) == 0) {
85 Binary_FindSymbol(Base, "_udiprops", (Uint*)&udiprops);
86 Binary_FindSymbol(Base, "_udiprops_end", (Uint*)&udiprops_end);
87 Log_Debug("UDI", "udiprops = %p, udiprops_end = %p", udiprops, udiprops_end);
89 UDI_int_LoadDriver(Base, info, udiprops, udiprops_end - udiprops);
94 static udi_boolean_t _get_token_bool(const char *str, const char **outstr)
111 while( isspace(*str) )
116 static udi_index_t _get_token_idx(const char *str, const char **outstr)
119 int ret = strtol(str, &end, 10);
120 if( ret < 0 || ret > 255 ) {
121 Log_Notice("UDI", "Value '%.*s' out of range for udi_index_t",
126 if( *end && !isspace(*end) ) {
127 Log_Notice("UDI", "No whitespace following '%.*s', got '%c'",
132 while( *end && isspace(*end) )
138 static udi_ubit16_t _get_token_uint16(const char *str, const char **outstr)
141 unsigned long ret = strtoul(str, &end, 10);
143 Log_Notice("UDI", "Value '%.*s' out of range for udi_ubit16_t",
148 if( *end && !isspace(*end) ) {
149 Log_Notice("UDI", "No whitespace following '%.*s', got '%c'",
154 while( *end && isspace(*end) )
160 static udi_ubit32_t _get_token_uint32(const char *str, const char **outstr)
163 udi_ubit32_t ret = strtoul(str, &end, 0);
164 if( *end && !isspace(*end) ) {
165 Log_Notice("UDI", "No whitespace following '%.*s', got '%c'",
170 while( *end && isspace(*end) )
176 static size_t _get_token_str(const char *str, const char **outstr, char *output)
179 const char *pos = str;
180 while( *pos && !isspace(*pos) )
197 case '\\': // backslash
202 // TODO: \p and \m<msgnum> (for message/disaster_message only)
216 while( isspace(*pos) )
225 static int _get_token_sym_v(const char *str, const char **outstr, bool printError, const char **syms)
228 for( int idx = 0; (sym = syms[idx]); idx ++ )
230 size_t len = strlen(sym);
231 if( memcmp(str, sym, len) != 0 )
233 if( str[len] && !isspace(str[len]) )
238 while( isspace(*str) )
244 // Unknown symbol, find the end of the symbol and error
245 const char *end = str;
246 while( !isspace(*end) )
250 Log_Notice("UDI", "Unknown token '%.*s'", end-str, str);
257 static int _get_token_sym(const char *str, const char **outstr, bool printError, ...)
262 va_start(args, printError);
263 for( ; (sym = va_arg(args, const char *)); count ++ )
267 const char *symlist[count+1];
268 va_start(args, printError);
269 for( int idx = 0; (sym = va_arg(args, const char *)); idx ++ )
271 symlist[count] = NULL;
273 return _get_token_sym_v(str, outstr, printError, symlist);
277 UDIPROPS__properties_version,
283 UDIPROPS__parent_bind_ops,
284 UDIPROPS__internal_bind_ops,
285 UDIPROPS__child_bind_ops,
294 UDIPROPS__enumerates,
298 #define _defpropname(name) [ UDIPROPS__##name ] = #name
299 const char *caUDI_UdipropsNames[] = {
300 _defpropname(properties_version),
301 _defpropname(module),
303 _defpropname(message),
304 _defpropname(locale),
305 _defpropname(region),
306 _defpropname(parent_bind_ops),
307 _defpropname(internal_bind_ops),
308 _defpropname(child_bind_ops),
309 _defpropname(supplier),
310 _defpropname(contact),
312 _defpropname(shortname),
313 _defpropname(release),
314 _defpropname(requires),
316 _defpropname(device),
317 _defpropname(enumerates),
319 [UDIPROPS_last] = NULL
323 tUDI_DriverModule *UDI_int_LoadDriver(void *LoadBase, const udi_init_t *info, const char *udiprops, size_t udiprops_size)
325 //UDI_int_DumpInitInfo(info);
327 // TODO: Multiple modules?
328 tUDI_DriverModule *driver_module = NEW(tUDI_DriverModule,);
329 driver_module->InitInfo = info;
332 const char **udipropsptrs;
335 for( int i = 0; i < udiprops_size; i++ )
337 if( udiprops[i] == '\0' )
341 Log_Debug("UDI", "nLines = %i", nLines);
343 udipropsptrs = NEW(const char*,*nLines);
345 udipropsptrs[line++] = udiprops;
346 for( int i = 0; i < udiprops_size; i++ )
348 if( udiprops[i] == '\0' ) {
349 udipropsptrs[line++] = &udiprops[i+1];
352 if(udipropsptrs[line-1] == &udiprops[udiprops_size])
356 // 'message' into driver_module->Messages
357 // 'region' into driver_module->RegionTypes
358 // 'module' into driver_module->ModuleName
361 for( int i = 0; i < nLines; i ++ )
363 const char *str = udipropsptrs[i];
364 int sym = _get_token_sym_v(str, &str, false, caUDI_UdipropsNames);
367 case UDIPROPS__module:
368 driver_module->ModuleName = str;
371 driver_module->nMetaLangs ++;
373 case UDIPROPS__message:
374 driver_module->nMessages ++;
376 case UDIPROPS__locale:
379 case UDIPROPS__region:
380 driver_module->nRegionTypes ++;
382 case UDIPROPS__device:
383 driver_module->nDevices ++;
385 case UDIPROPS__parent_bind_ops:
386 driver_module->nParents ++;
388 case UDIPROPS__child_bind_ops:
389 driver_module->nChildBindOps ++;
397 // Allocate structures
398 LOG("nMessages = %i, nMetaLangs = %i",
399 driver_module->nMessages,
400 driver_module->nMetaLangs);
401 driver_module->Messages = NEW(tUDI_PropMessage, * driver_module->nMessages);
402 driver_module->RegionTypes = NEW(tUDI_PropRegion, * driver_module->nRegionTypes);
403 driver_module->MetaLangs = NEW(tUDI_MetaLangRef, * driver_module->nMetaLangs);
404 driver_module->Parents = NEW(tUDI_BindOps, * driver_module->nParents);
405 driver_module->ChildBindOps = NEW(tUDI_BindOps, * driver_module->nChildBindOps);
406 driver_module->Devices = NEW(tUDI_PropDevSpec*,* driver_module->nDevices);
412 int parent_index = 0;
413 int child_bind_index = 0;
414 int device_index = 0;
415 int next_unpop_region = 1;
416 for( int i = 0; i < nLines; i ++ )
418 const char *str = udipropsptrs[i];
421 int sym = _get_token_sym_v(str, &str, true, caUDI_UdipropsNames);
424 case UDIPROPS__properties_version:
425 if( _get_token_uint32(str, &str) != 0x101 ) {
426 Log_Warning("UDI", "Properties version mismatch.");
429 case UDIPROPS__module:
430 driver_module->ModuleName = str;
434 tUDI_MetaLangRef *ml = &driver_module->MetaLangs[ml_index++];
435 ml->meta_idx = _get_token_idx(str, &str);
437 ml->interface_name = str;
438 // TODO: May need to trim trailing spaces
439 ml->metalang = UDI_int_GetMetaLangByName(ml->interface_name);
440 if( !ml->metalang ) {
441 Log_Error("UDI", "Module %s referenced unsupported metalang %s",
442 driver_module->ModuleName, ml->interface_name);
446 case UDIPROPS__message:
448 tUDI_PropMessage *msg = &driver_module->Messages[msg_index++];
449 msg->locale = cur_locale;
450 msg->index = _get_token_uint16(str, &str);
451 if( !str ) continue ;
453 //Log_Debug("UDI", "Message %i/%i: '%s'", msg->locale, msg->index, msg->string);
456 case UDIPROPS__locale:
460 case UDIPROPS__region:
462 udi_index_t rgn_idx = _get_token_idx(str, &str);
463 if( !str ) continue ;
464 // Search for region index (just in case internal_bind_ops appears earlier)
465 tUDI_PropRegion *rgn = &driver_module->RegionTypes[0];
469 for( int i = 1; i < next_unpop_region; i ++, rgn ++ ) {
470 if( rgn->RegionIdx == rgn_idx )
473 if(i == next_unpop_region) {
474 if( next_unpop_region == driver_module->nRegionTypes ) {
475 // TODO: warning if reigon types overflow
478 next_unpop_region ++;
479 rgn->RegionIdx = rgn_idx;
485 int sym = _get_token_sym(str, &str, true,
486 "type", "binding", "priority", "latency", "overrun_time", NULL
492 rgn->Type = _get_token_sym(str, &str, true,
493 "normal", "fp", NULL);
496 rgn->Binding = _get_token_sym(str, &str, true,
497 "static", "dynamic", NULL);
500 rgn->Priority = _get_token_sym(str, &str, true,
501 "med", "lo", "hi", NULL);
504 rgn->Latency = _get_token_sym(str, &str, true,
505 "non_overrunable", "powerfail_warning", "overrunable",
506 "retryable", "non_critical", NULL);
508 case 4: // overrun_time
509 rgn->OverrunTime = _get_token_uint32(str, &str);
516 case UDIPROPS__parent_bind_ops:
518 tUDI_BindOps *bind = &driver_module->Parents[parent_index++];
519 bind->meta_idx = _get_token_idx(str, &str);
520 if( !str ) continue ;
521 bind->region_idx = _get_token_idx(str, &str);
522 if( !str ) continue ;
523 bind->ops_idx = _get_token_idx(str, &str);
524 if( !str ) continue ;
525 bind->bind_cb_idx = _get_token_idx(str, &str);
527 // Expected EOL, didn't get it :(
529 Log_Debug("UDI", "Parent bind - meta:%i,rgn:%i,ops:%i,bind:%i",
530 bind->meta_idx, bind->region_idx, bind->ops_idx, bind->bind_cb_idx);
533 case UDIPROPS__internal_bind_ops:
535 // Get region using index
536 udi_index_t meta = _get_token_idx(str, &str);
537 if( !str ) continue ;
538 udi_index_t rgn_idx = _get_token_idx(str, &str);
539 if( !str ) continue ;
541 // Search for region index (just in case the relevant 'region' comes after)
542 tUDI_PropRegion *rgn = &driver_module->RegionTypes[0];
547 for( j = 1; j < next_unpop_region; j ++, rgn ++ ) {
548 if( rgn->RegionIdx == rgn_idx )
551 if( j == next_unpop_region ) {
552 if( next_unpop_region == driver_module->nRegionTypes ) {
553 // TODO: warning if reigon types overflow
556 next_unpop_region ++;
557 rgn->RegionIdx = rgn_idx;
562 rgn->BindMeta = meta;
564 rgn->PriBindOps = _get_token_idx(str, &str);
565 if( !str ) continue ;
566 rgn->SecBindOps = _get_token_idx(str, &str);
567 if( !str ) continue ;
568 rgn->BindCb = _get_token_idx(str, &str);
569 if( !str ) continue ;
571 // TODO: Please sir, I want an EOL
575 case UDIPROPS__child_bind_ops:
577 tUDI_BindOps *bind = &driver_module->ChildBindOps[child_bind_index++];
578 bind->meta_idx = _get_token_idx(str, &str);
579 if( !str ) continue ;
580 bind->region_idx = _get_token_idx(str, &str);
581 if( !str ) continue ;
582 bind->ops_idx = _get_token_idx(str, &str);
584 // Expected EOL, didn't get it :(
586 Log_Debug("UDI", "Child bind - meta:%i,rgn:%i,ops:%i",
587 bind->meta_idx, bind->region_idx, bind->ops_idx);
590 case UDIPROPS__supplier:
591 case UDIPROPS__contact:
593 case UDIPROPS__shortname:
594 case UDIPROPS__release:
596 //case UDIPROPS__requires:
599 case UDIPROPS__device:
602 // Count properties (and validate)
603 _get_token_idx(str, &str); // message
605 _get_token_idx(str, &str); // meta
609 _get_token_str(str, &str, NULL);
611 _get_token_sym(str, &str, true, "string", "ubit32", "boolean", "array", NULL);
613 _get_token_str(str, &str, NULL);
617 // Rewind and actually parse
618 _get_token_str(udipropsptrs[i], &str, NULL);
620 tUDI_PropDevSpec *dev = NEW_wA(tUDI_PropDevSpec, Attribs, n_attr);
621 driver_module->Devices[device_index++] = dev;;
622 dev->MessageNum = _get_token_idx(str, &str);
623 dev->MetaIdx = _get_token_idx(str, &str);
624 dev->nAttribs = n_attr;
628 udi_instance_attr_list_t *at = &dev->Attribs[n_attr];
629 _get_token_str(str, &str, at->attr_name);
631 at->attr_type = _get_token_sym(str, &str, true,
632 " ", "string", "array", "ubit32", "boolean", NULL);
635 switch( dev->Attribs[n_attr].attr_type )
638 at->attr_length = _get_token_str(str, &str, (char*)at->attr_value);
642 Log_Warning("UDI", "TODO: Parse 'array' attribute in 'device'");
643 _get_token_str(str, &str, NULL);
646 at->attr_length = sizeof(udi_ubit32_t);
647 val = _get_token_uint32(str, &str);
648 Log_Debug("UDI", "device %i: Value '%s'=%x", device_index,
650 UDI_ATTR32_SET(at->attr_value, val);
653 at->attr_length = sizeof(udi_boolean_t);
654 UDI_ATTR32_SET(at->attr_value, _get_token_bool(str, &str));
664 Log_Debug("UDI", "udipropsptrs[%i] = '%s'", i, udipropsptrs[i]);
670 for( int i = 0; i < driver_module->nDevices; i ++ )
671 driver_module->Devices[i]->Metalang = UDI_int_GetMetaLang(driver_module,
672 driver_module->Devices[i]->MetaIdx);
675 // TODO: Sort message list
677 int nSecondaryRgns = 0;
678 for( int i = 0; info->secondary_init_list && info->secondary_init_list[i].region_idx; i ++ )
680 driver_module->nRegions = 1+nSecondaryRgns;
683 // -- Add to loaded module list
684 driver_module->Next = gpUDI_LoadedModules;
685 gpUDI_LoadedModules = driver_module;
687 // Check for orphan drivers, and create an instance of them when loaded
688 if( driver_module->nParents == 0 )
690 UDI_MA_CreateInstance(driver_module, NULL, NULL);
694 // Search running instances for unbound children that can be bound to this driver
695 UDI_MA_BindParents(driver_module);
698 return driver_module;
701 void UDI_int_DumpInitInfo(udi_init_t *info)
703 Log("primary_init_info = %p = {", info->primary_init_info);
705 Log(" .mgmt_ops = %p = {", info->primary_init_info->mgmt_ops);
706 Log(" .usage_ind_op: %p() - 0x%02x",
707 info->primary_init_info->mgmt_ops->usage_ind_op,
708 info->primary_init_info->mgmt_op_flags[0]
710 Log(" .enumerate_req_op: %p() - 0x%02x",
711 info->primary_init_info->mgmt_ops->enumerate_req_op,
712 info->primary_init_info->mgmt_op_flags[1]
714 Log(" .devmgmt_req_op: %p() - 0x%02x",
715 info->primary_init_info->mgmt_ops->devmgmt_req_op,
716 info->primary_init_info->mgmt_op_flags[2]
718 Log(" .final_cleanup_req_op: %p() - 0x%02x",
719 info->primary_init_info->mgmt_ops->final_cleanup_req_op,
720 info->primary_init_info->mgmt_op_flags[3]
723 Log(" .mgmt_scratch_requirement = 0x%x", info->primary_init_info->mgmt_scratch_requirement);
724 Log(" .enumeration_attr_list_length = 0x%x", info->primary_init_info->enumeration_attr_list_length);
725 Log(" .rdata_size = 0x%x", info->primary_init_info->rdata_size);
726 Log(" .child_data_size = 0x%x", info->primary_init_info->child_data_size);
727 Log(" .per_parent_paths = 0x%x", info->primary_init_info->per_parent_paths);
730 Log("secondary_init_list = %p {", info->secondary_init_list);
731 for( int i = 0; info->secondary_init_list && info->secondary_init_list[i].region_idx; i ++ )
733 Log(" [%i] = { .region_idx=%i, .rdata_size=0x%x }",
734 info->secondary_init_list[i].region_idx, info->secondary_init_list[i].rdata_size);
737 Log("ops_init_list = %p {", info->ops_init_list);
738 for( int i = 0; info->ops_init_list[i].ops_idx; i++ )
741 Log(" .ops_idx = 0x%x", info->ops_init_list[i].ops_idx);
742 Log(" .meta_idx = 0x%x", info->ops_init_list[i].meta_idx);
743 Log(" .meta_ops_num = 0x%x", info->ops_init_list[i].meta_ops_num);
744 Log(" .chan_context_size = 0x%x", info->ops_init_list[i].chan_context_size);
745 Log(" .ops_vector = %p", info->ops_init_list[i].ops_vector);
746 // Log(" .op_flags = %p", info->ops_init_list[i].op_flags);
750 Log("cb_init_list = %p {", info->cb_init_list);
751 for( int i = 0; info->cb_init_list[i].cb_idx; i++ )
753 udi_cb_init_t *ent = &info->cb_init_list[i];
755 Log(" .cbidx = %i", ent->cb_idx);
756 Log(" .meta_idx = %i", ent->meta_idx);
757 Log(" .meta_cb_num = %i", ent->meta_cb_num);
758 Log(" .scratch_requirement = 0x%x", ent->scratch_requirement);
759 Log(" .inline_size = 0x%x", ent->inline_size);
760 Log(" .inline_layout = %p", ent->inline_layout);
765 // TODO: Move this stuff out
766 udi_ops_init_t *UDI_int_GetOps(tUDI_DriverInstance *Inst, udi_index_t index)
768 udi_ops_init_t *ops = Inst->Module->InitInfo->ops_init_list;
769 while( ops->ops_idx && ops->ops_idx != index )
771 if(ops->ops_idx == 0)
776 tUDI_MetaLang *UDI_int_GetMetaLang(tUDI_DriverModule *Module, udi_index_t index)
779 return &cMetaLang_Management;
781 for( int i = 0; i < Module->nMetaLangs; i ++ )
783 if( Module->MetaLangs[i].meta_idx == index )
784 return Module->MetaLangs[i].metalang;
789 const tUDI_MetaLang *UDI_int_GetMetaLangByName(const char *Name)
791 //extern tUDI_MetaLang cMetaLang_Management;
792 extern tUDI_MetaLang cMetaLang_BusBridge;
793 extern tUDI_MetaLang cMetaLang_NIC;
794 const tUDI_MetaLang *langs[] = {
795 &cMetaLang_BusBridge,
799 for( int i = 0; langs[i]; i ++ )
801 if( strcmp(Name, langs[i]->Name) == 0 )