home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lxapi32.zip / Lib32 / lxmodule.c < prev    next >
C/C++ Source or Header  |  2002-04-26  |  8KB  |  364 lines

  1. /* $Id: lxmodule.c,v 1.2 2002/04/26 23:09:24 smilcke Exp $ */
  2.  
  3. /*
  4.  * module.c
  5.  * Autor:               Stefan Milcke
  6.  * Erstellt am:         09.12.2001
  7.  * Letzte Aenderung am: 18.02.2002
  8.  *
  9. */
  10.  
  11. #include <linux/types.h>
  12. #include <linux/kernel.h>
  13. //#include <linux/major.h>
  14. #include <linux/slab.h>
  15. #include <linux/mm.h>
  16. #include <linux/version.h>
  17. #include <linux/module.h>
  18. #include <linux/delay.h>
  19. #include <linux/pci.h>
  20. #include <linux/ioctl.h>
  21. #include <linux/timer.h>
  22. #include <linux/module.h>
  23. #ifndef FAR
  24. #define FAR_LDEFOS2
  25. #endif
  26. #include <ldefos2.h>
  27. #ifdef FAR_LDEFOS2
  28. #undef FAR
  29. #undef FAR_LDEFOS2
  30. #endif
  31. #include <lxapilib.h>
  32.  
  33. struct os2lx_module_entry
  34. {
  35.  int external;
  36.  struct os2lx_module_entry *next;
  37.  struct os2lx_module *module;
  38. };
  39.  
  40. // Linked list of all available modules
  41. struct os2lx_module_entry *root_module=NULL;
  42. int num_modules=0;
  43.  
  44. //----------------------------- OS2_do_add_module ------------------------------
  45. int OS2_do_add_module(struct os2lx_module *pModule,int external)
  46. {
  47.  struct os2lx_module_entry *module_entry;
  48.  struct os2lx_module_entry *last=root_module;
  49.  module_entry=kmalloc(sizeof(struct os2lx_module_entry),GFP_KERNEL);
  50.  if(module_entry)
  51.  {
  52.   module_entry->external=external;
  53.   module_entry->next=NULL;
  54.   module_entry->module=pModule;
  55.   while(last && NULL !=last->next)
  56.    last=last->next;
  57.   if(last)
  58.    last->next=module_entry;
  59.   else
  60.    root_module=module_entry;
  61.   return 0;
  62.  }
  63.  return -ENOMEM;
  64. }
  65.  
  66. //------------------------------- OS2_add_module -------------------------------
  67. int OS2_add_module(struct os2lx_module *pModule)
  68. {
  69.  return OS2_do_add_module(pModule,1);
  70. }
  71.  
  72. // Linked list of all active modules
  73. struct os2lx_module_entry *active_root_module=NULL;
  74.  
  75. //------------------------------- request_module -------------------------------
  76. int request_module(char *modName)
  77. {
  78.  int rc=0;
  79.  struct os2lx_module_entry *p=root_module;
  80.  struct os2lx_module_entry *ap=active_root_module;
  81.  struct os2lx_module *m=NULL;
  82.  // First search in the available module list for the entry
  83.  while(p)
  84.  {
  85.   if(0==strcmp(modName,p->module->name))
  86.   {
  87.    m=p->module;
  88.    break;
  89.   }
  90.   p=p->next;
  91.  }
  92.  // Found one, so check, if it is already active
  93.  if(m)
  94.  {
  95.   while(ap)
  96.   {
  97.    if(m==ap->module)
  98.     return -EBUSY;
  99.    ap=ap->next;
  100.   }
  101.   // Not active, initialize
  102.   ap=kmalloc(sizeof(struct os2lx_module_entry),GFP_KERNEL);
  103.   if(ap)
  104.   {
  105.    ap->external=p->external;
  106.    ap->next=active_root_module;
  107.    ap->module=m;
  108.    active_root_module=ap;
  109.    m->active=1;
  110.    num_modules++;
  111.    if((m->init_fn) && (rc=m->init_fn()))
  112.    {
  113.     m->active=0;
  114.     num_modules--;
  115.     active_root_module=ap->next;
  116.     kfree(ap);
  117.    }
  118.   }
  119.  }
  120.  return rc;
  121. }
  122.  
  123. //------------------------------- release_module -------------------------------
  124. int release_module(char *modName)
  125. {
  126.  int rc=-ENOENT;
  127.  struct os2lx_module_entry *p=active_root_module;
  128.  struct os2lx_module_entry *pp=NULL;
  129.  struct os2lx_module *m=NULL;
  130.  while(p)
  131.  {
  132.   if(!strcmp(modName,p->module->name))
  133.   {
  134.    m=p->module;
  135.    if(p==active_root_module)
  136.     active_root_module=p->next;
  137.    else
  138.     pp->next=p->next;
  139.    kfree(p);
  140.    if(m->cleanup_fn)
  141.     m->cleanup_fn();
  142.    m->active=0;
  143.    rc=0;
  144.    break;
  145.   }
  146.   pp=p;
  147.   p=p->next;
  148.  }
  149.  return rc;
  150. }
  151.  
  152. //---------------------- do_cleanup_all_requested_modules ----------------------
  153. // external: 0 = internal modules
  154. //           1 = external modules
  155. //           2 = both
  156. void do_cleanup_all_requested_modules(int external)
  157. {
  158.  struct os2lx_module_entry *p=active_root_module;
  159.  while(p)
  160.  {
  161.   if((2==external) || p->external==external)
  162.   {
  163.    release_module(p->module->name);
  164.    p=active_root_module;
  165.   }
  166.   else
  167.    p=p->next;
  168.  }
  169. }
  170.  
  171. //----------------------- cleanup_all_requested_modules ------------------------
  172. void cleanup_all_requested_modules(void)
  173. {
  174.  do_cleanup_all_requested_modules(1);
  175.  do_cleanup_all_requested_modules(0);
  176. }
  177.  
  178. //---------------------------- get_value_from_str ------------------------------
  179. char* get_value_from_str(char *parm,int *v)
  180. {
  181.  int base=10;
  182.  *v=0;
  183.  if(*parm=='0')
  184.  {
  185.   if(*(parm+1)=='x' || *(parm+1)=='X')
  186.   {
  187.    base=16;
  188.    parm++;
  189.    parm++;
  190.   }
  191.  }
  192.  while(*parm && *parm!=' ' && *parm!=',')
  193.  {
  194.   if(16==base && (*parm>='a' && *parm<='f'))
  195.    *v=(*v)*base+((*parm)-'a'+10);
  196.   else if(16==base && (*parm>='A' && *parm<='F'))
  197.    *v=(*v)*base+((*parm)-'A'+10);
  198.   else if(*parm>='0' && *parm<='9')
  199.    *v=(*v)*base+((*parm)-'0');
  200.   else
  201.    break;
  202.   parm++;
  203.  }
  204.  return parm;
  205. }
  206.  
  207. //---------------------------- get_minmax_from_parm ----------------------------
  208. char* get_minmax_from_parm(char *parm,char *type,int *min,int *max,int *subscr)
  209. {
  210.  int v;
  211.  *min=-1;
  212.  *max=-1;
  213.  *subscr=-1;
  214.  if(strlen(type)>1)
  215.  {
  216.   type=get_value_from_str(type,&v);
  217.   if(*type)
  218.   {
  219.    *min=v;
  220.    *max=v;
  221.    *subscr=0;
  222.    if(*type=='-')
  223.    {
  224.     type++;
  225.     type=get_value_from_str(type,max);
  226.    }
  227.   }
  228.  }
  229.  if(-1!=*min)
  230.   parm=get_value_from_str(parm,subscr);
  231.  while(*parm && *parm=='=') parm++;
  232.  return parm;
  233. }
  234.  
  235. //----------------------------- OS2_apply_mod_parm -----------------------------
  236. int OS2_apply_mod_parm(char *parm,struct os2lx_parm *parmList,int num)
  237. {
  238.  char cp[200];
  239.  char *p;
  240.  int rc=0;
  241.  int i;
  242.  int v;
  243.  while(*parm)
  244.  {
  245.   p=cp;
  246.   while(*parm && *parm!=';')
  247.   {
  248.    *p=*parm;
  249.    p++;
  250.    parm++;
  251.   }
  252.   *p=(char)0;
  253.   rc=-EINVAL;
  254.   for(i=0;i<num;i++)
  255.   {
  256.    if(!strncmp(parmList[i].name,cp,strlen(parmList[i].name)))
  257.    {
  258.     int min=-1,max=-1,subscr=-1;
  259.     int *ip=(int *)parmList[i].adress;
  260.     p=&(cp[strlen(parmList[i].name)]);
  261.     p=get_minmax_from_parm(p,parmList[i].type,&min,&max,&subscr);
  262.     if(-1!=subscr)
  263.     {
  264.      while(subscr+1>=min && subscr+1<=max && *p)
  265.      {
  266.       p=get_value_from_str(p,&v);
  267.       ip[subscr]=v;
  268.       rc=0;
  269.       if(*p==',')
  270.       {
  271.        p++;
  272.        subscr++;
  273.       }
  274.       else
  275.        break;
  276.      }
  277.     }
  278.     else
  279.     {
  280.      p=get_value_from_str(p,&v);
  281.      *ip=v;
  282.      rc=0;
  283.     }
  284.     break;
  285.    }
  286.   }
  287.   if(rc)
  288.    return rc;
  289.   while(*parm && *parm==';') parm++;
  290.  }
  291.  return rc;
  292. }
  293.  
  294. //---------------------------- OS2_set_module_parm -----------------------------
  295. int OS2_set_module_parm(char *param)
  296. {
  297.  struct os2lx_module_entry *p=root_module;
  298.  struct os2lx_module *m;
  299.  int rc=-ENOENT;
  300.  while(p)
  301.  {
  302.   if(p->module->modParms && !strncmp(p->module->name,param,strlen(p->module->name)))
  303.    return OS2_apply_mod_parm(&(param[strlen(p->module->name)+1])
  304.                             ,(struct os2lx_parm *)(p->module->modParms)
  305.                             ,*(p->module->numModParms));
  306.   p=p->next;
  307.  }
  308.  return -ENOENT;
  309. }
  310.  
  311. //---------------------------- OS2_get_num_modules -----------------------------
  312. int OS2_get_num_modules(void)
  313. {
  314.  return num_modules;
  315. }
  316.  
  317. //---------------------------- OS2_get_module_name -----------------------------
  318. char *OS2_get_module_name(int nr)
  319. {
  320.  struct os2lx_module_entry *p=root_module;
  321.  while(p && nr)
  322.  {
  323.   p=p->next;
  324.   nr--;
  325.  }
  326.  if(p)
  327.   return p->module->name;
  328.  else
  329.   return "";
  330. }
  331.  
  332. //------------------------- OS2_get_total_num_modules --------------------------
  333. int OS2_get_total_num_modules(void)
  334. {
  335.  int n=0;
  336.  struct os2lx_module_entry *p=root_module;
  337.  while(p)
  338.  {
  339.   p=p->next;
  340.   n++;
  341.  }
  342.  return n;
  343. }
  344.  
  345. //------------------------------ OS2_enum_modules ------------------------------
  346. int OS2_enum_modules(void *buffer,unsigned long buffer_len)
  347. {
  348.  int c=0,i;
  349.  struct os2_enum_module *pd=(struct os2_enum_module *)buffer;
  350.  struct os2lx_module_entry *p=root_module;
  351.  while(p && buffer_len>=sizeof(struct os2_enum_module))
  352.  {
  353.   memcpy(pd->name,p->module->name,32);
  354.   pd->active=p->module->active;
  355.   pd++;
  356.   c++;
  357.   p=p->next;
  358.   if(buffer_len>=sizeof(struct os2_enum_module))
  359.    buffer_len-=sizeof(struct os2_enum_module);
  360.   else
  361.    buffer_len=0;
  362.  }
  363.  return c;
  364. }