]> rtime.felk.cvut.cz Git - mf6xx.git/blob - src/hudaqlib/opendevh.c
QEMU mf624.c formatted to make QEMU checkpatch.pl mostly happy.
[mf6xx.git] / src / hudaqlib / opendevh.c
1 /******************************************************************/
2 /**@file opendev.c:
3  * Desctiption: Humusoft data acquisition Linux library
4  implementation.                                   *
5  * Dependency: Linux                                              *
6  *                Copyright 2006-2007 Jaroslav Fojtik             *
7  ******************************************************************/
8 #include <stdio.h>
9 #include <malloc.h>
10 #include <string.h>
11 #include <dirent.h>
12
13 #include "hudaqlib.h"
14 #include "hudaq_internal.h"
15
16
17 static int sysfs_get_value(const char *FileName)
18 {
19         FILE *F;
20         int RetVal;
21
22         F = fopen(FileName, "rb");
23         if(F == NULL)
24                 return -1;
25
26         fscanf(F, "%X", &RetVal);
27         fclose(F);
28         return RetVal;
29 }
30
31 static int ScanSys(DWORD DeviceID, int deviceorder, DriverRecord *Rec)
32 {
33         DIR *dir_uio, *dir_memmaps;
34         FILE *F;
35         char ch;
36         struct dirent *direntry_uio, *direntry_memmaps;
37         unsigned vendor_id;
38         unsigned device_id;
39         char FileName[1024];
40         const char *SysDir="/sys/class/uio/";
41         unsigned long long start, end, size;
42         unsigned int index;
43         DWORD Resources[7 * 3];
44         int i;
45         int j = 0;
46         int RetVal = 0;
47
48         if(deviceorder <= 0)
49                 return -1;
50         if(Rec == NULL)
51                 return -2;
52
53         Rec->NumPhysMemResources = Rec->Resources.NumIOResources = 0;
54
55         sprintf(FileName, "%s", SysDir);
56         dir_uio = opendir(FileName);
57         if(!dir_uio)
58         {
59                 perror("opendir()");
60                 return -3;
61         }
62
63         while (direntry_uio = readdir(dir_uio))
64         {
65                 if((strcmp((direntry_uio->d_name), ".") == 0) || 
66                         (strcmp((direntry_uio->d_name), "..") == 0))
67                 {
68                         continue;
69                 }
70
71                 sprintf(FileName, "%s/%s/device/vendor", SysDir, direntry_uio->d_name);
72                 vendor_id = sysfs_get_value(FileName);
73                 sprintf(FileName, "%s/%s/device/device", SysDir, direntry_uio->d_name);
74                 device_id = sysfs_get_value(FileName);
75
76
77                 //Fixme search also through iomaps; add into the same Resources array   
78                 memset(Resources, 0, sizeof(Resources)); 
79                 sprintf(FileName, "%s/%s/maps", SysDir, direntry_uio->d_name);
80                 dir_memmaps = opendir(FileName);
81                 if (!dir_memmaps) {
82                         perror("opendir()");
83                         printf("%s\n", FileName);
84                         return -3;
85                 }
86                 while (direntry_memmaps = readdir(dir_memmaps)) {
87                         if((strcmp(direntry_memmaps->d_name, ".") == 0) || (strcmp(direntry_memmaps->d_name, "..") == 0))
88                                 continue;
89
90                         //printf("maps: %s\n", direntry_memmaps->d_name);
91                         sprintf(FileName, "%s/%s/maps/%s/addr", SysDir, direntry_uio->d_name, direntry_memmaps->d_name);
92                         F = fopen(FileName, "rb");
93                         if(F)
94                         {
95                                 if(fscanf(F, "%llx", &start) != 1)
96                                 {
97                                         fprintf(stderr, "Syntax error in %s", FileName);
98                                         break;
99                                 }
100
101                                 Resources[j] = start;
102                                 fclose(F);
103                         }
104
105                         sprintf(FileName, "%s/%s/maps/%s/size", SysDir, direntry_uio->d_name, direntry_memmaps->d_name);
106                         F = fopen(FileName, "rb");
107                         if(F)
108                         {
109                                 if(fscanf(F, "%llx", &size) != 1)
110                                 {
111                                         fprintf(stderr, "Syntax error in %s", FileName);
112                                         break;
113                                 }
114
115                                 Resources[j + 7] = size;
116                 
117                                 fclose(F);
118                         }
119                                 
120
121                         index = atoi((direntry_memmaps->d_name) + 3); // "+3", because we have to skip over the word "map"
122                         Resources[j + 7 + 7] = index;
123                         j ++;
124                 }
125                 
126                 closedir(dir_memmaps);    
127
128                 if(DeviceID == (device_id + (vendor_id << 16)))
129                 {
130                         //printf("DEVICE HAS BEEN FOUND!\n");
131                         if(--deviceorder < 1)
132                         {
133                                 for(i = 0; i < 7; i++)
134                                 {
135                                         if(Resources[i] != 0)
136                                         {
137                                                 if(Resources[i] > 0xFFFF)
138                                                 {
139                                                         Rec->PhysMemResources[Rec->NumPhysMemResources].Base = Resources[i];
140                                                         Rec->PhysMemResources[Rec->NumPhysMemResources].Length = Resources[i + 7];
141                                                         Rec->PhysMemResources[Rec->NumPhysMemResources].UioMappingIndex = 
142                                                                 Resources[i + 7 + 7];
143                                                         Rec->NumPhysMemResources ++;
144                                                 }
145                                                 else
146                                                 {
147                                                         Rec->Resources.IOResources[Rec->Resources.NumIOResources].Base = Resources[i];
148                                                         Rec->Resources.IOResources[Rec->Resources.NumIOResources].Length = Resources[i + 7];
149                                                         Rec->Resources.IOResources[Rec->Resources.NumIOResources].UioMappingIndex =
150                                                                  Resources[i + 7 + 7];
151                                                         Rec->Resources.NumIOResources ++;
152                                                 }
153                                         }
154                                 }
155
156                                 Rec->UioDevNum = atoi((direntry_uio->d_name) + 3); //number of uio device
157
158                                 RetVal = 1;    //device has been found!!
159                                 break;
160                         }
161                 }    
162
163                 //printf("%X %X %s\n", vendor_id, device_id, direntry_uio->d_name);
164         }
165
166         closedir(dir_uio);
167         return RetVal;
168 }
169
170
171
172
173
174
175 /*****************************************************************************
176   ;*
177   ;*              OpenDeviceHandle
178   ;*              opens device handle by device name and order
179   ;*
180   ;*              Input:  device name
181   ;*                      device order number
182   ;*              Output: positive number when success; 0 or negative when fails.
183   ;*
184   ;****************************************************************************/
185 int OpenDeviceHandle(const char *devicename, int deviceorder, DriverRecord *Rec)
186 {
187         DWORD DeviceID = 0;
188         int RetVal;
189
190         if(deviceorder <= 0)
191                 return -1;
192         if(Rec == NULL)
193                 return -2;
194
195         /* Humusoft */
196 #ifdef MF61X
197         if(!strcmp(devicename,"AD612")) DeviceID = 0x186C0612; /*DeviceID = (Device_ID & (Vendor_ID << 16))*/
198         if(!strcmp(devicename,"MF614")) DeviceID = 0x186C0614;
199 #endif
200 #ifdef MF62X
201         if(!strcmp(devicename,"AD622")) DeviceID = 0x186C0622;
202         if(!strcmp(devicename,"MF624")) DeviceID = 0x186C0624;
203         if(!strcmp(devicename,"MF625")) DeviceID = 0x186C0625;
204 #endif  
205
206         /* Advantech */
207 #ifdef PCI1753
208         if(!strcmp(devicename,"PCI1753")) DeviceID = 0x13FE1753;
209 #endif
210
211         /* Tedia */  
212 #ifdef PCD7004
213         if(!strcmp(devicename,"PCD7004")) DeviceID = 0x17600101;
214 #endif
215 #ifdef PCT7303B
216         if(!strcmp(devicename,"PCT7303B")) DeviceID = 0x17600201;
217 #endif
218
219         //printf("\n%s deviceID = %X", devicename, DeviceID);
220
221         if(DeviceID == 0)
222                 return -3;
223
224         RetVal = ScanSys(DeviceID, deviceorder, Rec);
225         return RetVal;
226 }
227
228