5 #define VERSION ((0<<8)|1)
9 #include "udi_internal.h"
13 extern udi_init_t pci_init;
14 extern char pci_udiprops[];
15 extern size_t pci_udiprops_size;
18 int UDI_Install(char **Arguments);
19 int UDI_DetectDriver(void *Base);
20 int UDI_LoadDriver(void *Base);
21 tUDI_DriverModule *UDI_int_LoadDriver(void *LoadBase, udi_init_t *info, const char *udiprops, size_t udiprops_size);
22 const tUDI_MetaLang *UDI_int_GetMetaLangByName(const char *Name);
25 MODULE_DEFINE(0, VERSION, UDI, UDI_Install, NULL, NULL);
26 tModuleLoader gUDI_Loader = {
27 NULL, "UDI", UDI_DetectDriver, UDI_LoadDriver, NULL
29 tUDI_DriverModule *gpUDI_LoadedModules;
33 * \fn int UDI_Install(char **Arguments)
34 * \brief Stub intialisation routine
36 int UDI_Install(char **Arguments)
38 Module_RegisterLoader( &gUDI_Loader );
40 Proc_SpawnWorker(UDI_int_DeferredThread, NULL);
42 UDI_int_LoadDriver(NULL, &pci_init, pci_udiprops, pci_udiprops_size);
48 * \brief Detects if a driver should be loaded by the UDI subsystem
50 int UDI_DetectDriver(void *Base)
54 if( Binary_FindSymbol(Base, "udi_init_info", &unused) == 0) {
57 if( Binary_FindSymbol(Base, "_udiprops", &unused) == 0 ) {
58 Log_Warning("UDI", "Driver has udi_init_info, but no _udiprops symbol");
61 if( Binary_FindSymbol(Base, "_udiprops_end", &unused) == 0) {
62 Log_Warning("UDI", "Driver has udi_init_info, but no _udiprops_end symbol");
70 * \fn int UDI_LoadDriver(void *Base)
72 int UDI_LoadDriver(void *Base)
75 char *udiprops = NULL;
76 char *udiprops_end = 0;
78 if( Binary_FindSymbol(Base, "udi_init_info", (Uint*)&info) == 0) {
83 Binary_FindSymbol(Base, "_udiprops", (Uint*)&udiprops);
84 Binary_FindSymbol(Base, "_udiprops_end", (Uint*)&udiprops_end);
85 Log_Debug("UDI", "udiprops = %p, udiprops_end = %p", udiprops, udiprops_end);
87 UDI_int_LoadDriver(Base, info, udiprops, udiprops_end - udiprops);
92 static udi_boolean_t _get_token_bool(const char *str, const char **outstr)
109 while( isspace(*str) )
114 static udi_index_t _get_token_idx(const char *str, const char **outstr)
117 int ret = strtol(str, &end, 10);
118 if( ret < 0 || ret > 255 ) {
119 Log_Notice("UDI", "Value '%.*s' out of range for udi_index_t",
124 if( *end && !isspace(*end) ) {
125 Log_Notice("UDI", "No whitespace following '%.*s', got '%c'",
130 while( *end && isspace(*end) )
136 static udi_ubit16_t _get_token_uint16(const char *str, const char **outstr)
139 unsigned long ret = strtoul(str, &end, 10);
141 Log_Notice("UDI", "Value '%.*s' out of range for udi_ubit16_t",
146 if( *end && !isspace(*end) ) {
147 Log_Notice("UDI", "No whitespace following '%.*s', got '%c'",
152 while( *end && isspace(*end) )
158 static udi_ubit32_t _get_token_uint32(const char *str, const char **outstr)
161 udi_ubit32_t ret = strtoul(str, &end, 0);
162 if( *end && !isspace(*end) ) {
163 Log_Notice("UDI", "No whitespace following '%.*s', got '%c'",
168 while( *end && isspace(*end) )
174 static size_t _get_token_str(const char *str, const char **outstr, char *output)
177 const char *pos = str;
178 while( *pos && !isspace(*pos) )
195 case '\\': // backslash
200 // TODO: \p and \m<msgnum> (for message/disaster_message only)
214 while( isspace(*pos) )
223 static int _get_token_sym_v(const char *str, const char **outstr, bool printError, const char **syms)
226 for( int idx = 0; (sym = syms[idx]); idx ++ )
228 size_t len = strlen(sym);
229 if( memcmp(str, sym, len) != 0 )
231 if( str[len] && !isspace(str[len]) )
236 while( isspace(*str) )
242 // Unknown symbol, find the end of the symbol and error
243 const char *end = str;
244 while( !isspace(*end) )
248 Log_Notice("UDI", "Unknown token '%.*s'", end-str, str);
255 static int _get_token_sym(const char *str, const char **outstr, bool printError, ...)
260 va_start(args, printError);
261 for( ; (sym = va_arg(args, const char *)); count ++ )
265 const char *symlist[count+1];
266 va_start(args, printError);
267 for( int idx = 0; (sym = va_arg(args, const char *)); idx ++ )
269 symlist[count] = NULL;
271 return _get_token_sym_v(str, outstr, printError, symlist);
275 UDIPROPS__properties_version,
281 UDIPROPS__parent_bind_ops,
282 UDIPROPS__internal_bind_ops,
283 UDIPROPS__child_bind_ops,
292 UDIPROPS__enumerates,
296 #define _defpropname(name) [ UDIPROPS__##name ] = #name
297 const char *caUDI_UdipropsNames[] = {
298 _defpropname(properties_version),
299 _defpropname(module),
301 _defpropname(message),
302 _defpropname(locale),
303 _defpropname(region),
304 _defpropname(parent_bind_ops),
305 _defpropname(internal_bind_ops),
306 _defpropname(child_bind_ops),
307 _defpropname(supplier),
308 _defpropname(contact),
310 _defpropname(shortname),
311 _defpropname(release),
312 _defpropname(requires),
314 _defpropname(device),
315 _defpropname(enumerates),
317 [UDIPROPS_last] = NULL
321 tUDI_DriverModule *UDI_int_LoadDriver(void *LoadBase, udi_init_t *info, const char *udiprops, size_t udiprops_size)
323 //UDI_int_DumpInitInfo(info);
325 // TODO: Multiple modules?
326 tUDI_DriverModule *driver_module = NEW(tUDI_DriverModule,);
327 driver_module->InitInfo = info;
330 const char **udipropsptrs;
333 for( int i = 0; i < udiprops_size; i++ )
335 if( udiprops[i] == '\0' )
339 Log_Debug("UDI", "nLines = %i", nLines);
341 udipropsptrs = NEW(const char*,*nLines);
343 udipropsptrs[line++] = udiprops;
344 for( int i = 0; i < udiprops_size; i++ )
346 if( udiprops[i] == '\0' ) {
347 udipropsptrs[line++] = &udiprops[i+1];
350 if(udipropsptrs[line-1] == &udiprops[udiprops_size])
354 // 'message' into driver_module->Messages
355 // 'region' into driver_module->RegionTypes
356 // 'module' into driver_module->ModuleName
359 for( int i = 0; i < nLines; i ++ )
361 const char *str = udipropsptrs[i];
362 int sym = _get_token_sym_v(str, &str, false, caUDI_UdipropsNames);
365 case UDIPROPS__module:
366 driver_module->ModuleName = str;
369 driver_module->nMetaLangs ++;
371 case UDIPROPS__message:
372 driver_module->nMessages ++;
374 case UDIPROPS__locale:
377 case UDIPROPS__region:
378 driver_module->nRegionTypes ++;
380 case UDIPROPS__device:
381 driver_module->nDevices ++;
383 case UDIPROPS__parent_bind_ops:
384 driver_module->nParents ++;
386 case UDIPROPS__child_bind_ops:
387 driver_module->nChildBindOps ++;
395 // Allocate structures
396 LOG("nMessages = %i, nMetaLangs = %i",
397 driver_module->nMessages,
398 driver_module->nMetaLangs);
399 driver_module->Messages = NEW(tUDI_PropMessage, * driver_module->nMessages);
400 driver_module->RegionTypes = NEW(tUDI_PropRegion, * driver_module->nRegionTypes);
401 driver_module->MetaLangs = NEW(tUDI_MetaLangRef, * driver_module->nMetaLangs);
402 driver_module->Parents = NEW(tUDI_BindOps, * driver_module->nParents);
403 driver_module->ChildBindOps = NEW(tUDI_BindOps, * driver_module->nChildBindOps);
404 driver_module->Devices = NEW(tUDI_PropDevSpec*,* driver_module->nDevices);
410 int parent_index = 0;
411 int child_bind_index = 0;
412 int device_index = 0;
413 int next_unpop_region = 1;
414 for( int i = 0; i < nLines; i ++ )
416 const char *str = udipropsptrs[i];
419 int sym = _get_token_sym_v(str, &str, true, caUDI_UdipropsNames);
422 case UDIPROPS__properties_version:
423 if( _get_token_uint32(str, &str) != 0x101 ) {
424 Log_Warning("UDI", "Properties version mismatch.");
427 case UDIPROPS__module:
428 driver_module->ModuleName = str;
432 tUDI_MetaLangRef *ml = &driver_module->MetaLangs[ml_index++];
433 ml->meta_idx = _get_token_idx(str, &str);
435 ml->interface_name = str;
436 // TODO: May need to trim trailing spaces
437 ml->metalang = UDI_int_GetMetaLangByName(ml->interface_name);
438 if( !ml->metalang ) {
439 Log_Error("UDI", "Module %s referenced unsupported metalang %s",
440 driver_module->ModuleName, ml->interface_name);
444 case UDIPROPS__message:
446 tUDI_PropMessage *msg = &driver_module->Messages[msg_index++];
447 msg->locale = cur_locale;
448 msg->index = _get_token_uint16(str, &str);
449 if( !str ) continue ;
451 //Log_Debug("UDI", "Message %i/%i: '%s'", msg->locale, msg->index, msg->string);
454 case UDIPROPS__locale:
458 case UDIPROPS__region:
460 udi_index_t rgn_idx = _get_token_idx(str, &str);
461 if( !str ) continue ;
462 // Search for region index (just in case internal_bind_ops appears earlier)
463 tUDI_PropRegion *rgn = &driver_module->RegionTypes[0];
467 for( int i = 1; i < next_unpop_region; i ++, rgn ++ ) {
468 if( rgn->RegionIdx == rgn_idx )
471 if(i == next_unpop_region) {
472 if( next_unpop_region == driver_module->nRegionTypes ) {
473 // TODO: warning if reigon types overflow
476 next_unpop_region ++;
477 rgn->RegionIdx = rgn_idx;
483 int sym = _get_token_sym(str, &str, true,
484 "type", "binding", "priority", "latency", "overrun_time", NULL
490 rgn->Type = _get_token_sym(str, &str, true,
491 "normal", "fp", NULL);
494 rgn->Binding = _get_token_sym(str, &str, true,
495 "static", "dynamic", NULL);
498 rgn->Priority = _get_token_sym(str, &str, true,
499 "med", "lo", "hi", NULL);
502 rgn->Latency = _get_token_sym(str, &str, true,
503 "non_overrunable", "powerfail_warning", "overrunable",
504 "retryable", "non_critical", NULL);
506 case 4: // overrun_time
507 rgn->OverrunTime = _get_token_uint32(str, &str);
514 case UDIPROPS__parent_bind_ops:
516 tUDI_BindOps *bind = &driver_module->Parents[parent_index++];
517 bind->meta_idx = _get_token_idx(str, &str);
518 if( !str ) continue ;
519 bind->region_idx = _get_token_idx(str, &str);
520 if( !str ) continue ;
521 bind->ops_idx = _get_token_idx(str, &str);
522 if( !str ) continue ;
523 bind->bind_cb_idx = _get_token_idx(str, &str);
525 // Expected EOL, didn't get it :(
527 Log_Debug("UDI", "Parent bind - meta:%i,rgn:%i,ops:%i,bind:%i",
528 bind->meta_idx, bind->region_idx, bind->ops_idx, bind->bind_cb_idx);
531 case UDIPROPS__internal_bind_ops:
533 // Get region using index
534 udi_index_t meta = _get_token_idx(str, &str);
535 if( !str ) continue ;
536 udi_index_t rgn_idx = _get_token_idx(str, &str);
537 if( !str ) continue ;
539 // Search for region index (just in case the relevant 'region' comes after)
540 tUDI_PropRegion *rgn = &driver_module->RegionTypes[0];
545 for( j = 1; j < next_unpop_region; j ++, rgn ++ ) {
546 if( rgn->RegionIdx == rgn_idx )
549 if( j == next_unpop_region ) {
550 if( next_unpop_region == driver_module->nRegionTypes ) {
551 // TODO: warning if reigon types overflow
554 next_unpop_region ++;
555 rgn->RegionIdx = rgn_idx;
560 rgn->BindMeta = meta;
562 rgn->PriBindOps = _get_token_idx(str, &str);
563 if( !str ) continue ;
564 rgn->SecBindOps = _get_token_idx(str, &str);
565 if( !str ) continue ;
566 rgn->BindCb = _get_token_idx(str, &str);
567 if( !str ) continue ;
569 // TODO: Please sir, I want an EOL
573 case UDIPROPS__child_bind_ops:
575 tUDI_BindOps *bind = &driver_module->ChildBindOps[child_bind_index++];
576 bind->meta_idx = _get_token_idx(str, &str);
577 if( !str ) continue ;
578 bind->region_idx = _get_token_idx(str, &str);
579 if( !str ) continue ;
580 bind->ops_idx = _get_token_idx(str, &str);
582 // Expected EOL, didn't get it :(
584 Log_Debug("UDI", "Child bind - meta:%i,rgn:%i,ops:%i",
585 bind->meta_idx, bind->region_idx, bind->ops_idx);
588 case UDIPROPS__supplier:
589 case UDIPROPS__contact:
591 case UDIPROPS__shortname:
592 case UDIPROPS__release:
594 //case UDIPROPS__requires:
597 case UDIPROPS__device:
600 // Count properties (and validate)
601 _get_token_idx(str, &str); // message
603 _get_token_idx(str, &str); // meta
607 _get_token_str(str, &str, NULL);
609 _get_token_sym(str, &str, true, "string", "ubit32", "boolean", "array", NULL);
611 _get_token_str(str, &str, NULL);
615 // Rewind and actually parse
616 _get_token_str(udipropsptrs[i], &str, NULL);
618 tUDI_PropDevSpec *dev = NEW_wA(tUDI_PropDevSpec, Attribs, n_attr);
619 driver_module->Devices[device_index++] = dev;;
620 dev->MessageNum = _get_token_idx(str, &str);
621 dev->MetaIdx = _get_token_idx(str, &str);
622 dev->nAttribs = n_attr;
626 udi_instance_attr_list_t *at = &dev->Attribs[n_attr];
627 _get_token_str(str, &str, at->attr_name);
629 at->attr_type = _get_token_sym(str, &str, true,
630 " ", "string", "array", "ubit32", "boolean", NULL);
633 switch( dev->Attribs[n_attr].attr_type )
636 at->attr_length = _get_token_str(str, &str, (char*)at->attr_value);
640 Log_Warning("UDI", "TODO: Parse 'array' attribute in 'device'");
641 _get_token_str(str, &str, NULL);
644 at->attr_length = sizeof(udi_ubit32_t);
645 val = _get_token_uint32(str, &str);
646 Log_Debug("UDI", "device %i: Value '%s'=%x", device_index,
648 UDI_ATTR32_SET(at->attr_value, val);
651 at->attr_length = sizeof(udi_boolean_t);
652 UDI_ATTR32_SET(at->attr_value, _get_token_bool(str, &str));
662 Log_Debug("UDI", "udipropsptrs[%i] = '%s'", i, udipropsptrs[i]);
669 // TODO: Sort message list
671 int nSecondaryRgns = 0;
672 for( int i = 0; info->secondary_init_list && info->secondary_init_list[i].region_idx; i ++ )
674 driver_module->nRegions = 1+nSecondaryRgns;
677 // -- Add to loaded module list
678 driver_module->Next = gpUDI_LoadedModules;
679 gpUDI_LoadedModules = driver_module;
681 // Check for orphan drivers, and create an instance of them when loaded
682 if( driver_module->nParents == 0 )
684 tUDI_DriverInstance *inst = UDI_MA_CreateInstance(driver_module);
686 // Enumerate so any pre-loaded drivers are detected
687 UDI_MA_BeginEnumeration(inst);
691 // Search running instances for unbound children that can be bound to this driver
692 UDI_MA_BindParents(driver_module);
695 return driver_module;
698 void UDI_int_DumpInitInfo(udi_init_t *info)
700 Log("primary_init_info = %p = {", info->primary_init_info);
702 Log(" .mgmt_ops = %p = {", info->primary_init_info->mgmt_ops);
703 Log(" .usage_ind_op: %p() - 0x%02x",
704 info->primary_init_info->mgmt_ops->usage_ind_op,
705 info->primary_init_info->mgmt_op_flags[0]
707 Log(" .enumerate_req_op: %p() - 0x%02x",
708 info->primary_init_info->mgmt_ops->enumerate_req_op,
709 info->primary_init_info->mgmt_op_flags[1]
711 Log(" .devmgmt_req_op: %p() - 0x%02x",
712 info->primary_init_info->mgmt_ops->devmgmt_req_op,
713 info->primary_init_info->mgmt_op_flags[2]
715 Log(" .final_cleanup_req_op: %p() - 0x%02x",
716 info->primary_init_info->mgmt_ops->final_cleanup_req_op,
717 info->primary_init_info->mgmt_op_flags[3]
720 Log(" .mgmt_scratch_requirement = 0x%x", info->primary_init_info->mgmt_scratch_requirement);
721 Log(" .enumeration_attr_list_length = 0x%x", info->primary_init_info->enumeration_attr_list_length);
722 Log(" .rdata_size = 0x%x", info->primary_init_info->rdata_size);
723 Log(" .child_data_size = 0x%x", info->primary_init_info->child_data_size);
724 Log(" .per_parent_paths = 0x%x", info->primary_init_info->per_parent_paths);
727 Log("secondary_init_list = %p {", info->secondary_init_list);
728 for( int i = 0; info->secondary_init_list && info->secondary_init_list[i].region_idx; i ++ )
730 Log(" [%i] = { .region_idx=%i, .rdata_size=0x%x }",
731 info->secondary_init_list[i].region_idx, info->secondary_init_list[i].rdata_size);
734 Log("ops_init_list = %p {", info->ops_init_list);
735 for( int i = 0; info->ops_init_list[i].ops_idx; i++ )
738 Log(" .ops_idx = 0x%x", info->ops_init_list[i].ops_idx);
739 Log(" .meta_idx = 0x%x", info->ops_init_list[i].meta_idx);
740 Log(" .meta_ops_num = 0x%x", info->ops_init_list[i].meta_ops_num);
741 Log(" .chan_context_size = 0x%x", info->ops_init_list[i].chan_context_size);
742 Log(" .ops_vector = %p", info->ops_init_list[i].ops_vector);
743 // Log(" .op_flags = %p", info->ops_init_list[i].op_flags);
747 Log("cb_init_list = %p {", info->cb_init_list);
748 for( int i = 0; info->cb_init_list[i].cb_idx; i++ )
750 udi_cb_init_t *ent = &info->cb_init_list[i];
752 Log(" .cbidx = %i", ent->cb_idx);
753 Log(" .meta_idx = %i", ent->meta_idx);
754 Log(" .meta_cb_num = %i", ent->meta_cb_num);
755 Log(" .scratch_requirement = 0x%x", ent->scratch_requirement);
756 Log(" .inline_size = 0x%x", ent->inline_size);
757 Log(" .inline_layout = %p", ent->inline_layout);
762 // TODO: Move this stuff out
763 udi_ops_init_t *UDI_int_GetOps(tUDI_DriverInstance *Inst, udi_index_t index)
765 udi_ops_init_t *ops = Inst->Module->InitInfo->ops_init_list;
766 while( ops->ops_idx && ops->ops_idx != index )
768 if(ops->ops_idx == 0)
773 tUDI_MetaLang *UDI_int_GetMetaLang(tUDI_DriverInstance *Inst, udi_index_t index)
776 return &cMetaLang_Management;
778 for( int i = 0; i < Inst->Module->nMetaLangs; i ++ )
780 if( Inst->Module->MetaLangs[i].meta_idx == index )
781 return Inst->Module->MetaLangs[i].metalang;
786 const tUDI_MetaLang *UDI_int_GetMetaLangByName(const char *Name)
788 //extern tUDI_MetaLang cMetaLang_Management;
789 extern tUDI_MetaLang cMetaLang_BusBridge;
790 const tUDI_MetaLang *langs[] = {
791 &cMetaLang_BusBridge,
794 for( int i = 0; langs[i]; i ++ )
796 if( strcmp(Name, langs[i]->Name) == 0 )