comparison vidix/pci_db2c.awk @ 22900:a9e111b88c4a

merged libdha and libvidix, moved all files from libdha to vidix directory
author ben
date Fri, 06 Apr 2007 15:20:49 +0000
parents
children c3567df39aff
comparison
equal deleted inserted replaced
22899:515545f81186 22900:a9e111b88c4a
1 # This file converts given pci.db to "C" source and header files
2 # For latest version of pci ids see: http://pciids.sf.net
3 # Copyright 2002 Nick Kurshev
4 #
5 # Usage: awk -f pci_db2c.awk pci.db
6 #
7 # Tested with Gawk v 3.0.x and Mawk 1.3.3
8 # But it should work with standard Awk implementations (hopefully).
9 # (Nobody tested it with Nawk, but it should work, too).
10 #
11
12 BEGIN {
13
14 if(ARGC != 2) {
15 # check for arguments:
16 print "Usage awk -f pci_db2c.awk pci.db (and make sure pci.db file exists first)";
17 exit(1);
18 }
19 in_file = ARGV[1];
20 vendor_file = "pci_vendors.h";
21 ids_file = "pci_ids.h"
22 name_file = "pci_names.c"
23 name_h_file = "pci_names.h"
24 dev_ids_file = "pci_dev_ids.c"
25 line=0;
26 # print out head lines
27 print_head(vendor_file);
28 print_head(ids_file);
29 print_head(name_file);
30 print_head(name_h_file);
31 print_head(dev_ids_file);
32 print_includes(dev_ids_file);
33 print "#ifndef PCI_VENDORS_INCLUDED" >vendor_file
34 print "#define PCI_VENDORS_INCLUDED 1">vendor_file
35 print "" >vendor_file
36 print "#ifndef PCI_IDS_INCLUDED" >ids_file
37 print "#define PCI_IDS_INCLUDED 1">ids_file
38 print "" >ids_file
39 print "#include \"pci_vendors.h\"">ids_file
40 print "" >ids_file
41
42 print "#ifndef PCI_NAMES_INCLUDED" >name_h_file
43 print "#define PCI_NAMES_INCLUDED 1">name_h_file
44 print "" >name_h_file
45 print_name_struct(name_h_file);
46 print "#include <stddef.h>">name_file
47 print "#include \"pci_names.h\"">name_file
48 print "#include \"pci_dev_ids.c\"">name_file
49 print "">name_file
50 print "static struct vendor_id_s vendor_ids[] = {">name_file
51 first_pass=1;
52 init_name_db();
53 while(getline <in_file)
54 {
55 # count up lines
56 line++;
57 n=split($0, field, "[\t]");
58 name_field = kill_double_quoting(field[3])
59 if(field[1] == "v" && length(field[3])>0 && field[4] == "0")
60 {
61 init_device_db()
62 svend_name = get_short_vendor_name(field[3])
63 printf("#define VENDOR_%s\t", svend_name) >vendor_file;
64 if(length(svend_name) < 9) printf("\t") >vendor_file;
65 printf("0x%s /*%s*/\n",field[2], name_field) >vendor_file;
66 printf("{ 0x%s, \"%s\", dev_lst_%s },\n",field[2], name_field, field[2]) >name_file;
67 printf("/* Vendor: %s: %s */\n", field[2], name_field) > ids_file
68 if(first_pass == 1) { first_pass=0; }
69 else { print "{ 0xFFFF, NULL }\n};" >dev_ids_file; }
70 printf("static const struct device_id_s dev_lst_%s[]={\n", field[2])>dev_ids_file
71 }
72 if(field[1] == "d" && length(field[3])>0 && field[4] == "0")
73 {
74 sdev_name = get_short_device_name(field[3])
75 full_name = sprintf("#define DEVICE_%s_%s", svend_name, sdev_name);
76 printf("%s\t", full_name) >ids_file
77 if(length(full_name) < 9) printf("\t") >ids_file;
78 if(length(full_name) < 17) printf("\t") >ids_file;
79 if(length(full_name) < 25) printf("\t") >ids_file;
80 if(length(full_name) < 32) printf("\t") >ids_file;
81 if(length(full_name) < 40) printf("\t") >ids_file;
82 if(length(full_name) < 48) printf("\t") >ids_file;
83 printf("0x%s /*%s*/\n", substr(field[2], 5), name_field) >ids_file
84 printf("{ 0x%s, \"%s\" },\n", substr(field[2], 5), name_field) >dev_ids_file
85 }
86 if(field[1] == "s" && length(field[3])>0 && field[4] == "0")
87 {
88 subdev_name = get_short_subdevice_name(field[3])
89 full_name = sprintf("#define SUBDEVICE_%s_%s", svend_name, subdev_name)
90 printf("\t%s\t", full_name) >ids_file
91 if(length(full_name) < 9) printf("\t") >ids_file;
92 if(length(full_name) < 17) printf("\t") >ids_file;
93 if(length(full_name) < 25) printf("\t") >ids_file;
94 if(length(full_name) < 32) printf("\t") >ids_file;
95 if(length(full_name) < 40) printf("\t") >ids_file;
96 printf("0x%s /*%s*/\n", substr(field[2], 9), name_field) >ids_file
97 }
98 }
99 print "Total lines parsed:", line;
100 print "">vendor_file
101 print "#endif/*PCI_VENDORS_INCLUDED*/">vendor_file
102 print "">ids_file
103 print "#endif/*PCI_IDS_INCLUDED*/">ids_file
104 print "">name_h_file
105 print "#endif/*PCI_NAMES_INCLUDED*/">name_h_file
106 print "};">name_file
107 print "{ 0xFFFF, NULL }" >dev_ids_file;
108 print "};">dev_ids_file
109 print_func_bodies(name_file);
110 }
111
112 function print_includes(out_file)
113 {
114 print "#include <stdlib.h>" >out_file;
115 print "#include \"pci_names.h\"" >out_file;
116 return;
117 }
118
119 function print_head( out_file)
120 {
121 print "/*" >out_file;
122 printf(" * File: %s\n", out_file) >out_file;
123 printf(" * This file was generated automatically. Don't modify it.\n") >out_file;
124 print "*/" >out_file;
125 return;
126 }
127
128 function print_name_struct(out_file)
129 {
130 print "#ifdef __cplusplus" >out_file
131 print "extern \"C\" {" >out_file
132 print "#endif" >out_file
133 print "">out_file
134 print "struct device_id_s" >out_file
135 print "{" >out_file
136 print "\tunsigned short\tid;" >out_file
137 print "\tconst char *\tname;" >out_file
138 print "};" >out_file
139 print "">out_file
140 print "struct vendor_id_s" >out_file
141 print "{" >out_file
142 print "\tunsigned short\tid;" >out_file
143 print "\tconst char *\tname;" >out_file
144 print "\tconst struct device_id_s *\tdev_list;" >out_file
145 print "};" >out_file
146 print "extern const char *pci_vendor_name(unsigned short id);">out_file
147 print "extern const char *pci_device_name(unsigned short vendor_id, unsigned short device_id);">out_file
148 print "">out_file
149 print "#ifdef __cplusplus" >out_file
150 print "}" >out_file
151 print "#endif" >out_file
152 return
153 }
154
155 function print_func_bodies(out_file)
156 {
157 print "">out_file
158 print "const char *pci_vendor_name(unsigned short id)" >out_file
159 print "{" >out_file
160 print " unsigned i;" >out_file
161 print " for(i=0;i<sizeof(vendor_ids)/sizeof(struct vendor_id_s);i++)">out_file
162 print " {" >out_file
163 print "\tif(vendor_ids[i].id == id) return vendor_ids[i].name;" >out_file
164 print " }" >out_file
165 print " return NULL;" >out_file
166 print "}">out_file
167 print "" >out_file
168 print "const char *pci_device_name(unsigned short vendor_id, unsigned short device_id)" >out_file
169 print "{" >out_file
170 print " unsigned i, j;" >out_file
171 print " for(i=0;i<sizeof(vendor_ids)/sizeof(struct vendor_id_s);i++)">out_file
172 print " {" >out_file
173 print "\tif(vendor_ids[i].id == vendor_id)" >out_file
174 print "\t{" >out_file
175 print "\t j=0;" >out_file
176 print "\t while(vendor_ids[i].dev_list[j].id != 0xFFFF)" >out_file
177 print "\t {">out_file
178 print "\t\tif(vendor_ids[i].dev_list[j].id == device_id) return vendor_ids[i].dev_list[j].name;">out_file
179 print "\t\tj++;">out_file
180 print "\t };">out_file
181 print "\t break;" >out_file
182 print "\t}" >out_file
183 print " }" >out_file
184 print " return NULL;">out_file
185 print "}">out_file
186 return
187 }
188
189 function kill_double_quoting(fld)
190 {
191 n=split(fld,phrases, "[\"]");
192 new_fld = phrases[1]
193 for(i=2;i<=n;i++) new_fld = sprintf("%s\\\"%s", new_fld, phrases[i])
194 return new_fld
195 }
196
197 function init_name_db()
198 {
199 vendor_names[1]=""
200 }
201
202 function init_device_db()
203 {
204 # delete device_names
205 for( i in device_names ) delete device_names[i];
206 device_names[1]=""
207 # delete subdevice_names
208 for( i in subdevice_names ) delete subdevice_names[i];
209 subdevice_names[1] = ""
210 }
211
212 function get_short_vendor_name(from)
213 {
214 n=split(from, name, "[ ]");
215 new_name = toupper(name[1]);
216 if(length(new_name)<3) new_name = sprintf("%s_%s", new_name, toupper(name[2]));
217 n=split(new_name, name, "[^0-9A-Za-z]");
218 svendor = name[1];
219 for(i=2;i<=n;i++) svendor=sprintf("%s%s%s", svendor, length(name[i])?"_":"", name[i]);
220 new_name = svendor;
221 vend_suffix = 2;
222 # check for unique
223 while(new_name in vendor_names)
224 {
225 new_name = sprintf("%s%u", svendor, vend_suffix)
226 vend_suffix = vend_suffix + 1;
227 }
228 # Add new name in array of vendor's names
229 vendor_names[new_name] = new_name
230 return new_name;
231 }
232
233 function get_short_device_name(from_name)
234 {
235 n=split(from_name, name, "[ ]");
236 new_name = toupper(name[1]);
237 if(length(name[2])) new_name = sprintf("%s_%s", new_name, toupper(name[2]));
238 if(length(name[3])) new_name = sprintf("%s_%s", new_name, toupper(name[3]));
239 n=split(new_name, name, "[^0-9A-Za-z]");
240 sdevice = name[1];
241 for(i=2;i<=n;i++) sdevice=sprintf("%s%s%s", sdevice, length(name[i])?"_":"", name[i]);
242 new_name = sdevice;
243 dev_suffix = 2;
244 # check for unique
245 while(new_name in device_names)
246 {
247 new_name = sprintf("%s%u", sdevice, dev_suffix)
248 dev_suffix = dev_suffix + 1;
249 }
250 # Add new name in array of device names
251 device_names[new_name] = new_name
252 return new_name;
253 }
254
255 function get_short_subdevice_name(from_name)
256 {
257 n=split(from_name, name, "[ ]");
258 new_name = toupper(name[1]);
259 if(length(name[2])) new_name = sprintf("%s_%s", new_name, toupper(name[2]));
260 if(length(name[3])) new_name = sprintf("%s_%s", new_name, toupper(name[3]));
261 n=split(new_name, name, "[^0-9A-Za-z]");
262 ssdevice = name[1];
263 for(i=2;i<=n;i++) ssdevice=sprintf("%s%s%s", ssdevice, length(name[i])?"_":"", name[i]);
264 new_name = ssdevice;
265 sdev_suffix = 2;
266 # check for unique
267 while(new_name in subdevice_names)
268 {
269 new_name = sprintf("%s%u", ssdevice, sdev_suffix)
270 sdev_suffix = sdev_suffix + 1;
271 }
272 # Add new name in array of subdevice names
273 subdevice_names[new_name] = new_name
274 return new_name;
275 }