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

  1. /* $Id: pci-nc.c,v 1.2 2002/04/26 23:09:35 smilcke Exp $ */
  2.  
  3. /*
  4.  * pci-nc.c
  5.  * Autor:               Stefan Milcke
  6.  * Erstellt am:         25.10.2001
  7.  * Letzte Aenderung am: 10.04.2002
  8.  *
  9. */
  10.  
  11. #include <linux/types.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mm.h>
  14. #include <linux/pci.h>
  15. #include <linux/init.h>
  16.  
  17. #define INCL_NOPMAPI
  18. #include <os2.h>
  19. #include <kee.h>
  20. #include <mmintrin.h>
  21.  
  22. #include <ldefos2.h>
  23. #include <stacktoflat.h>
  24.  
  25. #ifdef CONFIG_PCI_NAMES
  26.  
  27. struct pci_subsys_info
  28. {
  29.  unsigned short vendor;
  30.  unsigned short device;
  31.  const char *name;
  32.  struct  pci_subsys_info *next;
  33. };
  34.  
  35. struct pci_device_info
  36. {
  37.  unsigned short device;
  38.  unsigned short seen;
  39.  const char *name;
  40.  struct pci_subsys_info *subsyses;
  41.  struct pci_device_info *next;
  42. };
  43.  
  44. struct pci_vendor_info
  45. {
  46.  unsigned short vendor;
  47.  unsigned short nr;
  48.  const char *name;
  49.  struct pci_device_info *devices;
  50.  struct pci_vendor_info *next;
  51. };
  52.  
  53. static struct pci_vendor_info *vendors=0;
  54.  
  55. static struct pci_vendor_info *plvi=0;
  56. static struct pci_device_info *pldi=0;
  57. static struct pci_subsys_info *plsi=0;
  58.  
  59. #define MAX_LINE_LENGTH 1024
  60.  
  61. static char* line=(char*)NULL;
  62.  
  63. struct KEEfileptr
  64. {
  65.  HFILE hFile;
  66.  KEEhfile khFile;
  67.  QWORD off;
  68.  KEEfilesize sz;
  69.  char *contents;
  70. };
  71.  
  72. //------------------------- get_next_line_from_keefile -------------------------
  73. static char* get_next_line_from_keefile(struct KEEfileptr *fp
  74.                                         ,ULONG maxLen,PULONG pBytesRead)
  75. {
  76.  line[0]='#';
  77.  do
  78.  {
  79.   ULONG c;
  80.   char *s=(fp->contents+fp->off.ulLo);
  81.   if(maxLen>MAX_LINE_LENGTH)
  82.    maxLen=MAX_LINE_LENGTH;
  83.   if(maxLen>(fp->sz.ulLo - fp->off.ulLo))
  84.    maxLen=fp->sz.ulLo - fp->off.ulLo;
  85.   *pBytesRead=0;
  86.   for(c=0;c<maxLen;c++)
  87.   {
  88.    fp->off.ulLo++;
  89.    if(0==fp->off.ulLo)
  90.     fp->off.ulHi++;
  91.    if(s[c]==0x0a)
  92.    {
  93.     c++;
  94.     break;
  95.    }
  96.   }
  97.   if(c>maxLen)
  98.    c=maxLen;
  99.   if(0==c)
  100.    return (char *)NULL;
  101.   if(c!=maxLen)
  102.    line[c]=(char)0;
  103.   memcpy(line,s,c);
  104.   *pBytesRead=c;
  105.   if((char)0!=line[0])
  106.    line[strlen(line)-1]=(char)0;
  107.  }while(line[0]=='#' || line[0]==' ' || line[0]==0x0a || line[0]==(char)0);
  108.  return line;
  109. }
  110.  
  111. //---------------------------------- get_hex -----------------------------------
  112. static int get_hex(char **p)
  113. {
  114.  register int c;
  115.  register int v=0;
  116.  while(**p)
  117.  {
  118.   c=**p;
  119.   if((c)>='0' && (c)<='9')
  120.    v=v*16+(int)(c-'0');
  121.   else if((c)>='A' && (c)<='F')
  122.    v=v*16+(int)(c-('A'-10));
  123.   else if((c)>='a' && (c)<='f')
  124.    v=v*16+(int)(c-('a'-10));
  125.   else
  126.    break;
  127.   (*p)++;
  128.  }
  129.  return v;
  130. }
  131.  
  132. //--------------------------------- get_entry ----------------------------------
  133. static char* get_entry(char *p,int *pId,int *pType,int *pId2)
  134. {
  135.  char *n;
  136.  *pType=0;
  137.  while(p && *p=='\t')
  138.  {
  139.   (*pType)++;
  140.   p++;
  141.  }
  142.  *pId=get_hex(&p);
  143.  while(*p==' ' || *p=='\t')
  144.   p++;
  145.  if(*pType==2)
  146.  {
  147.   *pId2=get_hex(&p);
  148.   while(*p==' ' || *p=='\t')
  149.    p++;
  150.  }
  151.  n=(char *)kmalloc(strlen(p)+1,0);
  152.  memcpy(n,p,strlen(p)+1);
  153.  return n;
  154. }
  155.  
  156. //----------------------------- pci_generate_infos -----------------------------
  157. static void pci_generate_infos(struct KEEfileptr *fp)
  158. {
  159.  register char f='f';
  160.  ULONG sz;
  161.  char *s=(char *)1;
  162.  static int vnr=0;
  163.  char *name;
  164.  int id,id2;
  165.  int type;
  166.  struct pci_vendor_info *vinfo;
  167.  struct pci_device_info *dinfo;
  168.  struct pci_subsys_info *sinfo;
  169.  while(s)
  170.  {
  171.   s=get_next_line_from_keefile(fp,MAX_LINE_LENGTH,&sz);
  172.   if(s)
  173.   {
  174.    if('f'==s[0])
  175.     if('f'==s[1])
  176.      if('f'==s[2])
  177.       if('f'==s[3])
  178.        break;
  179.    {
  180.     name=get_entry(s,&id,&type,&id2);
  181.     switch(type)
  182.     {
  183.      case 0:
  184.       {
  185.        vinfo=(struct pci_vendor_info*)kmalloc(sizeof(struct pci_vendor_info),0);
  186.        vinfo->vendor=id;
  187.        vinfo->nr=vnr;
  188.        vinfo->name=name;
  189.        vinfo->next=0;
  190.        vinfo->devices=0;
  191.        if(0==vendors)
  192.         vendors=vinfo;
  193.        else
  194.         plvi->next=vinfo;
  195.        plvi=vinfo;
  196.        pldi=0;
  197.        plsi=0;
  198.       }
  199.       break;
  200.      case 1:
  201.       {
  202.        dinfo=(struct pci_device_info*)kmalloc(sizeof(struct pci_device_info),0);
  203.        dinfo->device=id;
  204.        dinfo->seen=0;
  205.        dinfo->name=name;
  206.        dinfo->next=0;
  207.        dinfo->subsyses=0;
  208.        if(plvi)
  209.        {
  210.         if(0==plvi->devices)
  211.          plvi->devices=dinfo;
  212.         else
  213.          pldi->next=dinfo;
  214.         pldi=dinfo;
  215.         plsi=0;
  216.        }
  217.       }
  218.       break;
  219.      case 2:
  220.       {
  221.        sinfo=(struct pci_subsys_info*)kmalloc(sizeof(struct pci_subsys_info),0);
  222.        sinfo->vendor=id;
  223.        sinfo->device=id2;
  224.        sinfo->name=name;
  225.        sinfo->next=0;
  226.        if(pldi)
  227.        {
  228.         if(0==pldi->subsyses)
  229.          pldi->subsyses=sinfo;
  230.         else
  231.          plsi->next=sinfo;
  232.         plsi=sinfo;
  233.        }
  234.       }
  235.       break;
  236.      default:
  237.       kfree(name);
  238.       break;
  239.     }
  240.    }
  241.   }
  242.  }
  243. }
  244.  
  245. extern unsigned long PHPCIID;
  246.  
  247. //------------------------------- close_kee_file -------------------------------
  248. static unsigned long close_kee_file(struct KEEfileptr *fp)
  249. {
  250.  unsigned long rc=1;
  251.  if(fp->khFile)
  252.  {
  253.   if(fp->contents)
  254.   {
  255.    kfree(fp->contents);
  256.    fp->contents=(char *)NULL;
  257.   }
  258.   if(fp->khFile)
  259.   {
  260.    rc=KernUnLockFile(fp->khFile);
  261.    if(0==rc)
  262.     fp->khFile=(unsigned long)NULL;
  263.   }
  264.   fp->off.ulLo=0;
  265.   fp->off.ulHi=0;
  266.   fp->sz.ulLo=0;
  267.   fp->sz.ulHi=0;
  268.  }
  269.  return rc;
  270. }
  271.  
  272. //------------------------------- open_kee_file --------------------------------
  273. static unsigned long open_kee_file(struct KEEfileptr *fp)
  274. {
  275.  unsigned long rc=1;
  276.  if(fp->hFile && !(fp->khFile))
  277.  {
  278.   rc=KernLockFile(fp->hFile,&(fp->khFile));
  279.   if(0==rc)
  280.   {
  281.    rc=KernGetFileSize(fp->khFile,&(fp->sz));
  282.    if(0==rc)
  283.    {
  284.     fp->contents=(char *)kmalloc(fp->sz.ulLo+10,0);
  285.     if(fp->contents)
  286.     {
  287.      unsigned long br=0;
  288.      fp->off.ulLo=0;
  289.      fp->off.ulHi=0;
  290.      rc=KernReadFileAt(fp->khFile,(PVOID)fp->contents,fp->off,fp->sz.ulLo,&br);
  291.     }
  292.     if(!fp->contents)
  293.      rc=1;
  294.    }
  295.    else
  296.     rc=1;
  297.   }
  298.   if(0!=rc)
  299.    close_kee_file(fp);
  300.  }
  301.  return rc;
  302. }
  303.  
  304. //---------------------------- pci_name_device_init ----------------------------
  305. void pci_name_device_init(void)
  306. {
  307.  unsigned long rc;
  308.  unsigned short __far *phFile=(unsigned short __far *)__Make48Pointer(PHPCIID);
  309.  HFILE hf=(HFILE)*phFile;
  310.  struct KEEfileptr fp={0};
  311.  fp.hFile=hf;
  312.  rc=open_kee_file(&fp);
  313.  if(0==rc)
  314.  {
  315.   line=(char *)kmalloc(MAX_LINE_LENGTH,0);
  316.   if(line)
  317.   {
  318.    pci_generate_infos(&fp);
  319.    kfree(line);
  320.   }
  321.   rc=close_kee_file(&fp);
  322.  }
  323. }
  324.  
  325. //---------------------------- pci_name_device_free ----------------------------
  326. void pci_name_device_free()
  327. {
  328.  struct pci_vendor_info *pven=vendors;
  329.  struct pci_device_info *pdev=0;
  330.  struct pci_subsys_info *psub;
  331.  while(pven)
  332.  {
  333.   struct pci_vendor_info *pv=pven;
  334.   pdev=pven->devices;
  335.   while(pdev)
  336.   {
  337.    struct pci_device_info *pd=pdev;
  338.    psub=pdev->subsyses;
  339.    while(psub)
  340.    {
  341.     struct pci_subsys_info *ps=psub;
  342.     psub=psub->next;
  343.     kfree(ps->name);
  344.     kfree(ps);
  345.    }
  346.    pdev=pdev->next;
  347.    kfree(pd->name);
  348.    kfree(pd);
  349.   }
  350.   pven=pven->next;
  351.   kfree(pv->name);
  352.   kfree(pv);
  353.  }
  354.  vendors=0;
  355. }
  356.  
  357. unsigned long OS2_pci_name_device(struct pci_dev *dev,char *vname,char *dname,char *sname);
  358. //------------------------------ pci_name_device -------------------------------
  359. void pci_name_device(struct pci_dev *dev)
  360. {
  361.  struct pci_vendor_info *pven=vendors;
  362.  struct pci_device_info *pdev=0;
  363.  struct pci_subsys_info *psub=0;
  364.  char vname[80]={0};
  365.  char dname[80]={0};
  366.  char sname[80]={0};
  367.  while(pven && pven->vendor!=dev->vendor)
  368.   pven=pven->next;
  369.  if(0==pven)
  370.  {
  371.   sprintf(dev->name,"PCI device %04x:%04x",dev->vendor,dev->device);
  372.   sprintf(vname,"%04x",dev->vendor);
  373.  }
  374.  else
  375.  {
  376.   sprintf(vname,"%s",pven->name);
  377.   pdev=pven->devices;
  378.   while(pdev && pdev->device!=dev->device)
  379.    pdev=pdev->next;
  380.   if(0==pdev)
  381.   {
  382.    sprintf(dev->name,"PCI device %04x:%04x (%s)",dev->vendor,dev->device,pven->name);
  383.    sprintf(dname,"%04x",dev->device);
  384.   }
  385.   else
  386.   {
  387.    sprintf(dev->name,"%s %s",pven->name,pdev->name);
  388.    sprintf(dname,"%s",pdev->name);
  389.    pdev->seen=pdev->seen+1;
  390.    psub=pdev->subsyses;
  391.    while(psub && (psub->vendor!=dev->subsystem_vendor || psub->device!=dev->subsystem_device))
  392.     psub=psub->next;
  393.    if(psub)
  394.     sprintf(sname,"%s",psub->name);
  395.    else
  396.     sprintf(sname,"SUB %04x:%04x",dev->subsystem_vendor,dev->subsystem_device);
  397.   }
  398.  }
  399.  {
  400.   char tmp[85];
  401.   sprintf(tmp,"%s",dev->name);
  402.   sprintf(dev->name,"B:%02x DFN:%04x (%s)"
  403.           ,dev->bus->number
  404.           ,dev->devfn
  405.           ,tmp);
  406.  }
  407.  OS2_pci_name_device(dev,vname,dname,sname);
  408. }
  409.  
  410. #endif // CONFIG_PCI_NAMES