summaryrefslogtreecommitdiffstats
path: root/vidix/pci_db2c.awk
blob: ccab1c6708a11aa02aa741efdfd7d106e804c4eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/awk -f
# This file converts given pci.db to "C" source and header files
# For latest version of pci ids see: http://pciids.sf.net
# Copyright 2002 Nick Kurshev
#
# Tested with Gawk v 3.0.x and Mawk 1.3.3
# But it should work with standard Awk implementations (hopefully).
# (Nobody tested it with Nawk, but it should work, too).
#
# This file is part of MPlayer.
#
# MPlayer is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# MPlayer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with MPlayer; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

BEGIN {

    if (ARGC != 3) {
        # check for arguments:
        print "Usage ./pci_db2c.awk pci.db (and make sure pci.db file exists first)";
        exit(1);
    }
    in_file = ARGV[1];
    with_pci_db = ARGV[2];
    vendor_file  = "vidix/pci_vendors.h";
    ids_file     = "vidix/pci_ids.h"
    name_file    = "vidix/pci_names.c"
    name_h_file  = "vidix/pci_names.h"
    dev_ids_file = "vidix/pci_dev_ids.c"
    line = 0;
    # print out head lines
    print_head(vendor_file);
    print_head(ids_file);
    print_head(name_file);
    print_head(name_h_file);
    print_head(dev_ids_file);
    print_includes(dev_ids_file);
    print "#ifndef MPLAYER_PCI_VENDORS_H" > vendor_file
    print "#define MPLAYER_PCI_VENDORS_H" > vendor_file
    print "" > vendor_file
    print "#ifndef MPLAYER_PCI_IDS_H" > ids_file
    print "#define MPLAYER_PCI_IDS_H" > ids_file
    print "" > ids_file
    print "#include \"pci_vendors.h\"" > ids_file
    print "" > ids_file

    print "#ifndef MPLAYER_PCI_NAMES_H" > name_h_file
    print "#define MPLAYER_PCI_NAMES_H" > name_h_file
    print "" > name_h_file
    print_name_struct(name_h_file);
    print "#include <stddef.h>" > name_file
    print "#include \"pci_names.h\"" > name_file
    if (with_pci_db) {
        print "#include \"pci_dev_ids.c\"" > name_file
        print "" > name_file
        print "static struct vendor_id_s vendor_ids[] = {" > name_file
    }
    first_pass = 1;
    init_name_db();
    while (getline < in_file) {
        # count up lines
        line++;
        n = split($0, field, "[\t]");
        name_field = kill_double_quoting(field[3])
        if (field[1] == "v" && length(field[3]) > 0 && field[4] == "0") {
            init_device_db()
            svend_name = get_short_vendor_name(field[3])
            printf("#define VENDOR_%s\t", svend_name) > vendor_file;
            if (length(svend_name) < 9) printf("\t") > vendor_file;
            printf("0x%s /*%s*/\n", field[2], name_field) > vendor_file;
            if (with_pci_db) printf("{ 0x%s, \"%s\", dev_lst_%s },\n", field[2], name_field, field[2]) > name_file;
            printf("/* Vendor: %s: %s */\n", field[2], name_field) > ids_file
            if (first_pass == 1) first_pass = 0;
            else print "{ 0xFFFF, NULL }\n};" > dev_ids_file;
            printf("static const struct device_id_s dev_lst_%s[] = {\n", field[2])> dev_ids_file
        }
        if (field[1] == "d" && length(field[3]) > 0 && field[4] == "0") {
            sdev_name = get_short_device_name(field[3])
            full_name = sprintf("#define DEVICE_%s_%s", svend_name, sdev_name);
            printf("%s\t", full_name) > ids_file
            if (length(full_name) <  9) printf("\t") > ids_file;
            if (length(full_name) < 17) printf("\t") > ids_file;
            if (length(full_name) < 25) printf("\t") > ids_file;
            if (length(full_name) < 32) printf("\t") > ids_file;
            if (length(full_name) < 40) printf("\t") > ids_file;
            if (length(full_name) < 48) printf("\t") > ids_file;
            printf("0x%s /*%s*/\n", substr(field[2], 5), name_field) > ids_file
            printf("{ 0x%s, \"%s\" },\n", substr(field[2], 5), name_field) > dev_ids_file
        }
        if (field[1] == "s" && length(field[3]) > 0 && field[4] == "0") {
            subdev_name = get_short_subdevice_name(field[3])
            full_name = sprintf("#define SUBDEVICE_%s_%s", svend_name, subdev_name)
            printf("\t%s\t", full_name) > ids_file
            if (length(full_name) <  9) printf("\t") > ids_file;
            if (length(full_name) < 17) printf("\t") > ids_file;
            if (length(full_name) < 25) printf("\t") > ids_file;
            if (length(full_name) < 32) printf("\t") > ids_file;
            if (length(full_name) < 40) printf("\t") > ids_file;
            printf("0x%s /*%s*/\n", substr(field[2], 9), name_field) > ids_file
        }
    }
    #print "Total lines parsed:", line;
    print "" > vendor_file
    print "#endif /* MPLAYER_PCI_VENDORS_H */" > vendor_file
    print "" > ids_file
    print "#endif /* MPLAYER_PCI_IDS_H */" > ids_file
    print "" > name_h_file
    print "#endif /* MPLAYER_PCI_NAMES_H */" > name_h_file
    if (with_pci_db) print "};" > name_file
    print "{ 0xFFFF, NULL }" > dev_ids_file;
    print "};" > dev_ids_file
    print_func_bodies(name_file);
}

function print_includes(out_file)
{
    print "#include <stdlib.h>" > out_file;
    print "#include \"pci_names.h\"" > out_file;
    return;
}

function print_head(out_file)
{
    printf("/* File: %s\n", out_file) > out_file;
    printf(" * This file was generated automatically. Don't modify it. */\n") > out_file;
    print "" > out_file
    return;
}

function print_name_struct(out_file)
{
    print "struct device_id_s {" > out_file
    print "    unsigned short id;" > out_file
    print "    const char *name;" > out_file
    print "};" > out_file
    print "" > out_file
    print "struct vendor_id_s {" > out_file
    print "    unsigned short id;" > out_file
    print "    const char *name;" > out_file
    print "    const struct device_id_s *dev_list;" > out_file
    print "};" > out_file
    print "const char *pci_vendor_name(unsigned short id);" > out_file
    print "const char *pci_device_name(unsigned short vendor_id, unsigned short device_id);" > out_file
    return
}

function print_func_bodies(out_file)
{
    print "" > out_file
    print "const char *pci_vendor_name(unsigned short id)" > out_file
    print "{" > out_file
    if (with_pci_db) {
        print "    unsigned i;" > out_file
        print "    for (i = 0; i < sizeof(vendor_ids) / sizeof(struct vendor_id_s); i++) {" > out_file
        print "        if (vendor_ids[i].id == id)" > out_file
        print "            return vendor_ids[i].name;" > out_file
        print "    }" > out_file
    }
    print "    return NULL;" > out_file
    print "}" > out_file
    print "" > out_file
    print "const char *pci_device_name(unsigned short vendor_id, unsigned short device_id)" > out_file
    print "{" > out_file
    if (with_pci_db) {
        print "    unsigned i, j;" > out_file
        print "    for (i = 0; i < sizeof(vendor_ids) / sizeof(struct vendor_id_s); i++) {" > out_file
        print "        if (vendor_ids[i].id == vendor_id) {" > out_file
        print "            j = 0;" > out_file
        print "            while (vendor_ids[i].dev_list[j].id != 0xFFFF) {" > out_file
        print "                if (vendor_ids[i].dev_list[j].id == device_id)" > out_file
        print "                    return vendor_ids[i].dev_list[j].name;" > out_file
        print "                j++;" > out_file
        print "            };" > out_file
        print "            break;" > out_file
        print "        }" > out_file
        print "    }" > out_file
    }
    print "    return NULL;" > out_file
    print "}" > out_file
    return
}

function kill_double_quoting(fld)
{
    n = split(fld, phrases, "[\"]");
    new_fld = phrases[1]
    for (i = 2; i <= n; i++) new_fld = sprintf("%s\\\"%s", new_fld, phrases[i])
    return new_fld
}

function init_name_db()
{
  vendor_names[1] = ""
}

function init_device_db()
{
    # delete device_names
    for (i in device_names) delete device_names[i];
    device_names[1] = ""
    # delete subdevice_names
    for (i in subdevice_names) delete subdevice_names[i];
    subdevice_names[1] = ""
}

function get_short_vendor_name(from)
{
    n = split(from, name, "[ ]");
    new_name = toupper(name[1]);
    if (length(new_name) < 3) new_name = sprintf("%s_%s", new_name, toupper(name[2]));
    n = split(new_name, name, "[^0-9A-Za-z]");
    svendor = name[1];
    for (i = 2; i <= n; i++) svendor = sprintf("%s%s%s", svendor, length(name[i]) ? "_" : "", name[i]);
    new_name = svendor;
    vend_suffix = 2;
    # check for unique
    while (new_name in vendor_names) {
        new_name = sprintf("%s%u", svendor, vend_suffix)
        vend_suffix = vend_suffix + 1;
    }
    # Add new name in array of vendor's names
    vendor_names[new_name] = new_name
    return new_name;
}

function get_short_device_name(from_name)
{
    n = split(from_name, name, "[ ]");
    new_name = toupper(name[1]);
    if (length(name[2])) new_name = sprintf("%s_%s", new_name, toupper(name[2]));
    if (length(name[3])) new_name = sprintf("%s_%s", new_name, toupper(name[3]));
    n = split(new_name, name, "[^0-9A-Za-z]");
    sdevice = name[1];
    for (i = 2; i <= n; i++) sdevice = sprintf("%s%s%s", sdevice, length(name[i]) ? "_" : "", name[i]);
    new_name = sdevice;
    dev_suffix = 2;
    # check for unique
    while (new_name in device_names) {
        new_name = sprintf("%s%u", sdevice, dev_suffix)
        dev_suffix = dev_suffix + 1;
    }
    # Add new name in array of device names
    device_names[new_name] = new_name
    return new_name;
}

function get_short_subdevice_name(from_name)
{
    n = split(from_name, name, "[ ]");
    new_name = toupper(name[1]);
    if (length(name[2])) new_name = sprintf("%s_%s", new_name, toupper(name[2]));
    if (length(name[3])) new_name = sprintf("%s_%s", new_name, toupper(name[3]));
    n = split(new_name, name, "[^0-9A-Za-z]");
    ssdevice = name[1];
    for (i = 2; i <= n; i++) ssdevice = sprintf("%s%s%s", ssdevice, length(name[i]) ? "_" : "", name[i]);
    new_name = ssdevice;
    sdev_suffix = 2;
    # check for unique
    while (new_name in subdevice_names) {
        new_name = sprintf("%s%u", ssdevice, sdev_suffix)
        sdev_suffix = sdev_suffix + 1;
    }
    # Add new name in array of subdevice names
    subdevice_names[new_name] = new_name
    return new_name;
}