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
5 # Usage: awk -f pci_db2c.awk pci.db
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).
15 # check for arguments:
16 print "Usage awk -f pci_db2c.awk pci.db (and make sure pci.db file exists first)";
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"
26 # print out head lines
27 print_head
(vendor_file
);
29 print_head
(name_file
);
30 print_head
(name_h_file
);
31 print_head
(dev_ids_file
);
32 print "#ifndef PCI_VENDORS_INCLUDED" >vendor_file
33 print "#define PCI_VENDORS_INCLUDED 1">vendor_file
35 print "#ifndef PCI_IDS_INCLUDED" >ids_file
36 print "#define PCI_IDS_INCLUDED 1">ids_file
38 print "#include \"pci_vendors.h\"">ids_file
41 print "#ifndef PCI_NAMES_INCLUDED" >name_h_file
42 print "#define PCI_NAMES_INCLUDED 1">name_h_file
44 print_name_struct
(name_h_file
);
45 print "#include <stddef.h>">name_file
46 print "#include \"pci_names.h\"">name_file
47 print "#include \"pci_dev_ids.c\"">name_file
49 print "static struct vendor_id_s vendor_ids[] = {">name_file
52 while(getline <in_file
)
56 n=
split($
0, field
, "[\t]");
57 name_field = kill_double_quoting
(field
[3])
58 if(field
[1] ==
"v" && length(field
[3])>0 && field
[4] ==
"0")
61 svend_name = get_short_vendor_name
(field
[3])
62 printf("#define VENDOR_%s\t", svend_name
) >vendor_file
;
63 if(length(svend_name
) < 9) printf("\t") >vendor_file
;
64 printf("0x%s /*%s*/\n",field
[2], name_field
) >vendor_file
;
65 printf("{ 0x%s, \"%s\", dev_lst_%s },\n",field
[2], name_field
, field
[2]) >name_file
;
66 printf("/* Vendor: %s: %s */\n", field
[2], name_field
) > ids_file
67 if(first_pass ==
1) { first_pass=
0; }
68 else { print "{ 0xFFFF, NULL }\n};" >dev_ids_file
; }
69 printf("static const struct device_id_s dev_lst_%s[]={\n", field
[2])>dev_ids_file
71 if(field
[1] ==
"d" && length(field
[3])>0 && field
[4] ==
"0")
73 sdev_name = get_short_device_name
(field
[3])
74 full_name =
sprintf("#define DEVICE_%s_%s", svend_name
, sdev_name
);
75 printf("%s\t", full_name
) >ids_file
76 if(length(full_name
) < 9) printf("\t") >ids_file
;
77 if(length(full_name
) < 17) printf("\t") >ids_file
;
78 if(length(full_name
) < 25) printf("\t") >ids_file
;
79 if(length(full_name
) < 32) printf("\t") >ids_file
;
80 if(length(full_name
) < 40) printf("\t") >ids_file
;
81 if(length(full_name
) < 48) printf("\t") >ids_file
;
82 printf("0x%s /*%s*/\n", substr(field
[2], 5), name_field
) >ids_file
83 printf("{ 0x%s, \"%s\" },\n", substr(field
[2], 5), name_field
) >dev_ids_file
85 if(field
[1] ==
"s" && length(field
[3])>0 && field
[4] ==
"0")
87 subdev_name = get_short_subdevice_name
(field
[3])
88 full_name =
sprintf("#define SUBDEVICE_%s_%s", svend_name
, subdev_name
)
89 printf("\t%s\t", full_name
) >ids_file
90 if(length(full_name
) < 9) printf("\t") >ids_file
;
91 if(length(full_name
) < 17) printf("\t") >ids_file
;
92 if(length(full_name
) < 25) printf("\t") >ids_file
;
93 if(length(full_name
) < 32) printf("\t") >ids_file
;
94 if(length(full_name
) < 40) printf("\t") >ids_file
;
95 printf("0x%s /*%s*/\n", substr(field
[2], 9), name_field
) >ids_file
98 print "Total lines parsed:", line
;
100 print "#endif/*PCI_VENDORS_INCLUDED*/">vendor_file
102 print "#endif/*PCI_IDS_INCLUDED*/">ids_file
104 print "#endif/*PCI_NAMES_INCLUDED*/">name_h_file
106 print "{ 0xFFFF, NULL }" >dev_ids_file
;
107 print "};">dev_ids_file
108 print_func_bodies
(name_file
);
111 function print_head
( out_file
)
113 print "/*" >out_file
;
114 printf(" * File: %s\n", out_file
) >out_file
;
115 printf(" * This file was generated automatically. Don't modify it.\n") >out_file
;
116 print "*/" >out_file
;
120 function print_name_struct
(out_file
)
122 print "#ifdef __cplusplus" >out_file
123 print "extern \"C\" {" >out_file
124 print "#endif" >out_file
126 print "struct device_id_s" >out_file
128 print "\tunsigned short\tid;" >out_file
129 print "\tconst char *\tname;" >out_file
132 print "struct vendor_id_s" >out_file
134 print "\tunsigned short\tid;" >out_file
135 print "\tconst char *\tname;" >out_file
136 print "\tconst struct device_id_s *\tdev_list;" >out_file
138 print "extern const char *pci_vendor_name(unsigned short id);">out_file
139 print "extern const char *pci_device_name(unsigned short vendor_id, unsigned short device_id);">out_file
141 print "#ifdef __cplusplus" >out_file
143 print "#endif" >out_file
147 function print_func_bodies
(out_file
)
150 print "const char *pci_vendor_name(unsigned short id)" >out_file
152 print " unsigned i;" >out_file
153 print " for(i=0;i<sizeof(vendor_ids)/sizeof(struct vendor_id_s);i++)">out_file
155 print "\tif(vendor_ids[i].id == id) return vendor_ids[i].name;" >out_file
157 print " return NULL;" >out_file
160 print "const char *pci_device_name(unsigned short vendor_id, unsigned short device_id)" >out_file
162 print " unsigned i, j;" >out_file
163 print " for(i=0;i<sizeof(vendor_ids)/sizeof(struct vendor_id_s);i++)">out_file
165 print "\tif(vendor_ids[i].id == vendor_id)" >out_file
166 print "\t{" >out_file
167 print "\t j=0;" >out_file
168 print "\t while(vendor_ids[i].dev_list[j].id != 0xFFFF)" >out_file
169 print "\t {">out_file
170 print "\t\tif(vendor_ids[i].dev_list[j].id == device_id) return vendor_ids[i].dev_list[j].name;">out_file
171 print "\t\tj++;">out_file
172 print "\t };">out_file
173 print "\t break;" >out_file
174 print "\t}" >out_file
176 print " return NULL;">out_file
181 function kill_double_quoting
(fld
)
183 n=
split(fld
,phrases
, "[\"]");
185 for(i=
2;i
<=n
;i
++) new_fld =
sprintf("%s\\\"%s", new_fld
, phrases
[i
])
189 function init_name_db
()
194 function init_device_db
()
196 # delete device_names
197 for( i in device_names
) delete device_names
[i
];
199 # delete subdevice_names
200 for( i in subdevice_names
) delete subdevice_names
[i
];
201 subdevice_names
[1] =
""
204 function get_short_vendor_name
(from
)
206 n=
split(from
, name
, "[ ]");
207 new_name =
toupper(name
[1]);
208 if(length(new_name
)<3) new_name =
sprintf("%s_%s", new_name
, toupper(name
[2]));
209 n=
split(new_name
, name
, "[^0-9A-Za-z]");
211 for(i=
2;i
<=n
;i
++) svendor=
sprintf("%s%s%s", svendor
, length(name
[i
])?
"_":"", name
[i
]);
215 while(new_name in vendor_names
)
217 new_name =
sprintf("%s%u", svendor
, vend_suffix
)
218 vend_suffix = vend_suffix
+ 1;
220 # Add new name in array of vendor's names
221 vendor_names
[new_name
] = new_name
225 function get_short_device_name
(from_name
)
227 n=
split(from_name
, name
, "[ ]");
228 new_name =
toupper(name
[1]);
229 if(length(name
[2])) new_name =
sprintf("%s_%s", new_name
, toupper(name
[2]));
230 if(length(name
[3])) new_name =
sprintf("%s_%s", new_name
, toupper(name
[3]));
231 n=
split(new_name
, name
, "[^0-9A-Za-z]");
233 for(i=
2;i
<=n
;i
++) sdevice=
sprintf("%s%s%s", sdevice
, length(name
[i
])?
"_":"", name
[i
]);
237 while(new_name in device_names
)
239 new_name =
sprintf("%s%u", sdevice
, dev_suffix
)
240 dev_suffix = dev_suffix
+ 1;
242 # Add new name in array of device names
243 device_names
[new_name
] = new_name
247 function get_short_subdevice_name
(from_name
)
249 n=
split(from_name
, name
, "[ ]");
250 new_name =
toupper(name
[1]);
251 if(length(name
[2])) new_name =
sprintf("%s_%s", new_name
, toupper(name
[2]));
252 if(length(name
[3])) new_name =
sprintf("%s_%s", new_name
, toupper(name
[3]));
253 n=
split(new_name
, name
, "[^0-9A-Za-z]");
255 for(i=
2;i
<=n
;i
++) ssdevice=
sprintf("%s%s%s", ssdevice
, length(name
[i
])?
"_":"", name
[i
]);
259 while(new_name in subdevice_names
)
261 new_name =
sprintf("%s%u", ssdevice
, sdev_suffix
)
262 sdev_suffix = sdev_suffix
+ 1;
264 # Add new name in array of subdevice names
265 subdevice_names
[new_name
] = new_name