xref: /haiku/src/apps/devices/pci-header.awk (revision f2b4344867e97c3f4e742a1b4a15e6879644601a)
1# PCI Header script
2#
3# Copyright 2006, Haiku.
4# Distributed under the terms of the MIT License.
5#
6# Authors:
7#		John Drinkwater, john@nextraweb.com
8#
9# Use with http://pciids.sourceforge.net/pci.ids
10# run as: awk -v HEADERFILE=pcihdr.h -f pci-header.awk pci.ids
11
12BEGIN {
13
14	# field separator, defining because user could have overridden
15	FS = " "
16
17	# Pass this in from outside with -v HEADERFILE=filenametouse
18	# we require pcihdr.h for our system
19	ofile = HEADERFILE
20
21	# possibly use this in the future
22	cleanvalues = "[^A-Za-z0-9{}\"'&@?!*.,:;+<> \\t\\/_\\[\\]=#()-]"
23	# ToDo: currently IDs aren't checked, we dumbly assume the source is clean
24
25	# descriptive output header
26	print "#if 0" > ofile
27	print "#\tPCIHDR.H: PCI Vendors, Devices, and Class Type information\n#" > ofile
28	print "#\tGenerated by pci-header.awk, source data from the following URI:\n#\thttp://pciids.sourceforge.net/pci.ids\n#" > ofile
29	print "#\tHeader created on " strftime( "%A, %d %b %Y %H:%M:%S %Z", systime() ) > ofile
30	print "#endif" > ofile
31
32	# and we start with vendors..
33	print "\ntypedef struct _PCI_VENTABLE\n{\n\tunsigned short\tVenId ;\n\tconst char *\tVenFull ;\n\tconst char *\tVenShort ;\n}  PCI_VENTABLE, *PPCI_VENTABLE ;\n" > ofile
34	print "PCI_VENTABLE\tPciVenTable [] =\n{" > ofile
35}
36
37# matches vendor - starts with an id as first thing on the line
38# because this occurs first in the header file, we output it without worry
39/^[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]] / {
40
41	if ( vendorcount++ > 0 ) {
42		formatting = ",\n"
43	} else {
44		formatting = ""
45	}
46
47	# store vendor ID for possible devices afterwards
48	vendorid = $1
49	vendor = substr($0, 7)
50	gsub( /\"/, "\\\"", vendor )
51
52	printf formatting "\t{ 0x" vendorid ", \"" vendor "\", \"\" }" > ofile
53}
54
55# matches device
56/^\t[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]] / {
57
58	device = substr($0, 8)
59	gsub( /\"/, "\\\"", device )
60
61	# store device ID for possible devices afterwards
62	deviceid = $1
63	devicecount++
64	devices[devicecount, 1] = vendorid
65	devices[devicecount, 2] = $1
66	devices[devicecount, 3] = 0
67	devices[devicecount, 4] = 0
68	devices[devicecount, 5] = device
69}
70
71# matches subvendor device
72/^\t\t[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]] / {
73
74	device = substr($0, 14)
75	gsub( /\"/, "\\\"", device )
76
77	devicecount++
78	devices[devicecount, 1] = vendorid
79	devices[devicecount, 2] = deviceid
80	devices[devicecount, 3] = $1
81	devices[devicecount, 4] = $2
82	devices[devicecount, 5] = device
83}
84
85# match device class - store data for later
86/^C [[:xdigit:]][[:xdigit:]]  / {
87
88	class = $2
89	classname = substr($0, 7)
90	gsub( /\"/, "\\\"", classname )
91}
92
93# match subclass, use device class data captured earlier, and output
94/^\t[[:xdigit:]][[:xdigit:]]  / {
95
96	subclass = $1
97	subclassname = substr($0, 6)
98	gsub( /\"/, "\\\"", subclassname )
99
100	classcount++
101	classes[classcount, 1] = class
102	classes[classcount, 2] = subclass
103	classes[classcount, 3] = "00"
104	classes[classcount, 4] = classname
105	classes[classcount, 5] = subclassname
106	classes[classcount, 6] = ""
107}
108
109# match programming interface
110/^\t\t[[:xdigit:]][[:xdigit:]]  / {
111
112	proginterface = $1
113	proginterfacename = substr($0, 7)
114	gsub( /\"/, "\\\"", proginterfacename )
115
116	classcount++
117	classes[classcount, 1] = class
118	classes[classcount, 2] = subclass
119	classes[classcount, 3] = proginterface
120	classes[classcount, 4] = classname
121	classes[classcount, 5] = subclassname
122	classes[classcount, 6] = proginterfacename
123}
124
125# We've processed the file, now output.
126END {
127
128	print "\n};\n\n// Use this value for loop control during searching:\n#define\tPCI_VENTABLE_LEN\t(sizeof(PciVenTable)/sizeof(PCI_VENTABLE))\n" > ofile
129
130	if ( devicecount > 0 ) {
131
132		print "typedef struct _PCI_DEVTABLE\n{\n\tunsigned short	VenId ;\n\tunsigned short	DevId ;\n\tunsigned short\tSubVenId ;\n\tunsigned short\tSubDevId ;\n\tconst char *\tChipDesc ;\n\tconst char *\tChip;\n}  PCI_DEVTABLE, *PPCI_DEVTABLE ;\n"  > ofile
133		print "PCI_DEVTABLE\tPciDevTable [] =\n{" > ofile
134		for (i = 1; i <= devicecount; i++) {
135
136			if (i != 1) {
137				formatting = ",\n"
138			} else {
139				formatting = ""
140			}
141			printf formatting "\t{ 0x" devices[i, 1] ", 0x" devices[i, 2] ", 0x" devices[i, 3] ", 0x" devices[i, 4] ", \"" devices[i, 5] "\" }" > ofile
142		}
143		print "\n} ;\n\n// Use this value for loop control during searching:\n#define	PCI_DEVTABLE_LEN	(sizeof(PciDevTable)/sizeof(PCI_DEVTABLE))\n" > ofile
144
145	}
146
147	if ( classcount > 0 ) {
148		print "typedef struct _PCI_CLASSCODETABLE\n{\n\tunsigned char	BaseClass ;\n\tunsigned char	SubClass ;\n\tunsigned char	ProgIf ;" > ofile
149		print "\tconst char *\t\tBaseDesc ;\n\tconst char *\t\tSubDesc ;\n\tconst char *\t\tProgDesc ;\n}  PCI_CLASSCODETABLE, *PPCI_CLASSCODETABLE ;\n" > ofile
150		print "PCI_CLASSCODETABLE PciClassCodeTable [] =\n{" > ofile
151		currentclass = classes[1, 1]
152		for (i = 1; i <= classcount; i++) {
153
154			if (i != 1) {
155				formatting = ",\n"
156			} else {
157				formatting = ""
158			}
159
160			# pretty print separate classes
161			if ( currentclass != classes[i, 1] ) {
162				formatting = formatting "\n"
163				currentclass = classes[i, 1]
164			}
165
166			# if the next item has the same details, we know we're to skip ourselves
167			# this is because the programming interface name needs to be used, and we dont have it ourselves
168			if ( ( classes[i, 1] != classes[i+1, 1] ) || ( classes[i, 2] != classes[i+1, 2] ) || ( classes[i, 3] != classes[i+1, 3] ) ) {
169				printf formatting "\t{ 0x" classes[i, 1] ", 0x" classes[i, 2] ", 0x" classes[i, 3] ", \"" classes[i, 4] "\", \"" classes[i, 5]  "\", \"" classes[i, 6] "\" }" > ofile
170			}
171		}
172		print "\n} ;\n\n// Use this value for loop control during searching:\n#define	PCI_CLASSCODETABLE_LEN	(sizeof(PciClassCodeTable)/sizeof(PCI_CLASSCODETABLE))\n" > ofile
173
174	}
175
176	# this is rather ugly, maybe we should include this in a seperate file, and pull it in ?
177	print "const char *\tPciCommandFlags [] =\n{\n\t\"I/O Access\",\n\t\"Memory Access\",\n\t\"Bus Mastering\",\n\t\"Special Cycles\",\n\t\"Memory Write & Invalidate\",\n\t\"Palette Snoop\",\n\t\"Parity Errors\",\n\t\"Wait Cycles\",\n\t\"System Errors\",\n\t\"Fast Back-To-Back\",\n\t\"Reserved 10\",\n\t\"Reserved 11\",\n\t\"Reserved 12\",\n\t\"Reserved 13\",\n\t\"Reserved 14\",\n\t\"Reserved 15\"\n} ;\n" > ofile
178	print "// Use this value for loop control during searching:\n#define	PCI_COMMANDFLAGS_LEN	(sizeof(PciCommandFlags)/sizeof(char *))\n" > ofile
179	print "const char *\tPciStatusFlags [] =\n{\n\t\"Reserved 0\",\n\t\"Reserved 1\",\n\t\"Reserved 2\",\n\t\"Reserved 3\",\n\t\"Reserved 4\",\n\t\"66 MHz Capable\",\n\t\"User-Defined Features\",\n\t\"Fast Back-To-Back\",\n\t\"Data Parity Reported\",\n\t\"\",\n\t\"\",\n\t\"Signalled Target Abort\",\n\t\"Received Target Abort\",\n\t\"Received Master Abort\",\n\t\"Signalled System Error\",\n\t\"Detected Parity Error\"\n} ;\n" > ofile
180	print "// Use this value for loop control during searching:\n#define	PCI_STATUSFLAGS_LEN	(sizeof(PciStatusFlags)/sizeof(char *))\n" > ofile
181	print "const char *\tPciDevSelFlags [] =\n{\n\t\"Fast Devsel Speed\",\n\t\"Medium Devsel Speed\",\n\t\"Slow Devsel Speed\",\n\t\"Reserved 9&10\"\n} ;\n" > ofile
182	print "// Use this value for loop control during searching:\n#define	PCI_DEVSELFLAGS_LEN	(sizeof(PciDevSelFlags)/sizeof(char *))\n\n" > ofile
183
184	close(ofile)
185}
186
187
188