remove trailing whitespace
[coreboot.git] / util / sconfig / main.c
old mode 100755 (executable)
new mode 100644 (file)
index 062eedd..dab04db
@@ -59,6 +59,8 @@ static struct device *new_dev(struct device *parent, struct device *bus) {
        dev->id = ++devcount;
        dev->parent = parent;
        dev->bus = bus;
+       dev->subsystem_vendor = -1;
+       dev->subsystem_device = -1;
        head->next = dev;
        head = dev;
        return dev;
@@ -89,7 +91,9 @@ int yywrap(void) {
 
 void yyerror (char const *str)
 {
-       fprintf (stderr, "line %d: %s\n", linenum, str);
+       extern char *yytext;
+       fprintf (stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
+       exit(1);
 }
 
 void postprocess_devtree(void) {
@@ -127,6 +131,13 @@ struct device *new_chip(struct device *parent, struct device *bus, char *path) {
 
        struct stat st;
        char *chip_h = malloc(strlen(path)+12);
+       sprintf(chip_h, "src/%s", path);
+       if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
+               fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
+                               path);
+               exit(1);
+       }
+
        sprintf(chip_h, "src/%s/chip.h", path);
        if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
                new_chip->chiph_exists = 0;
@@ -223,8 +234,6 @@ void alias_siblings(struct device *d) {
                                if (device_match(d, cmp)) {
                                        d->multidev = 1;
 
-                                       cmp->aliased_name = malloc(12);
-                                       sprintf(cmp->aliased_name, "_dev%d", cmp->id);
                                        cmp->id = d->id;
                                        cmp->name = d->name;
                                        cmp->used = 1;
@@ -279,22 +288,34 @@ void add_register(struct device *dev, char *name, char *val) {
        }
 }
 
+void add_pci_subsystem_ids(struct device *dev, int vendor, int device, int inherit)
+{
+       if (dev->bustype != PCI && dev->bustype != PCI_DOMAIN) {
+               printf("ERROR: 'subsystem' only allowed for PCI devices\n");
+               exit(1);
+       }
+
+       dev->subsystem_vendor = vendor;
+       dev->subsystem_device = device;
+       dev->inherit_subsystem = inherit;
+}
+
 static void pass0(FILE *fil, struct device *ptr) {
        if (ptr->type == device && ptr->id == 0)
                fprintf(fil, "struct bus %s_links[];\n", ptr->name);
        if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used)) {
-               fprintf(fil, "struct device %s;\n", ptr->name);
+               fprintf(fil, "static struct device %s;\n", ptr->name);
                if (ptr->rescnt > 0)
                        fprintf(fil, "struct resource %s_res[];\n", ptr->name);
                if (ptr->children || ptr->multidev)
                        fprintf(fil, "struct bus %s_links[];\n", ptr->name);
        }
-       if ((ptr->type == device) && (ptr->id != 0) && ptr->used)
-               fprintf(fil, "struct device %s;\n", ptr->aliased_name);
 }
 
 static void pass1(FILE *fil, struct device *ptr) {
        if (!ptr->used && (ptr->type == device)) {
+               if (ptr->id != 0)
+                       fprintf(fil, "static ", ptr->name);
                fprintf(fil, "struct device %s = {\n", ptr->name);
                fprintf(fil, "\t.ops = %s,\n", (ptr->ops)?(ptr->ops):"0");
                fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->bus->name, ptr->bus->link);
@@ -303,6 +324,12 @@ static void pass1(FILE *fil, struct device *ptr) {
                fprintf(fil, "},\n");
                fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
                fprintf(fil, "\t.on_mainboard = 1,\n");
+               if (ptr->subsystem_vendor > 0)
+                       fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n", ptr->subsystem_vendor);
+
+               if (ptr->subsystem_device > 0)
+                       fprintf(fil, "\t.subsystem_device = 0x%04x,\n", ptr->subsystem_device);
+
                if (ptr->rescnt > 0) {
                        fprintf(fil, "\t.resource_list = &%s_res[0],\n", ptr->name);
                }
@@ -392,6 +419,28 @@ static void walk_device_tree(FILE *fil, struct device *ptr, void (*func)(FILE *,
        } while (ptr);
 }
 
+static void inherit_subsystem_ids(FILE *file, struct device *dev)
+{
+       struct device *p;
+
+       if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
+               /* user already gave us a subsystem vendor/device */
+               return;
+       }
+
+       for(p = dev; p && p != p->parent; p = p->parent) {
+
+               if (p->bustype != PCI && p->bustype != PCI_DOMAIN)
+                       continue;
+
+               if (p->inherit_subsystem) {
+                       dev->subsystem_vendor = p->subsystem_vendor;
+                       dev->subsystem_device = p->subsystem_device;
+                       break;
+               }
+       }
+}
+
 int main(int argc, char** argv) {
        if (argc != 3) {
                printf("usage: sconfig vendor/mainboard outputdir\n");
@@ -410,12 +459,18 @@ int main(int argc, char** argv) {
        sprintf(headers.next->name, "mainboard/%s", mainboard);
 
        FILE *filec = fopen(devtree, "r");
-       yyrestart(filec);
+       if (!filec) {
+               fprintf(stderr, "Could not open file '%s' for reading: ", devtree);
+               perror(NULL);
+               exit(1);
+       }
 
-       FILE *staticc = fopen(outputc, "w");
+       yyrestart(filec);
 
        lastdev = head = &root;
+
        yyparse();
+
        fclose(filec);
 
        if ((head->type == chip) && (!head->chiph_exists)) {
@@ -424,6 +479,13 @@ int main(int argc, char** argv) {
                while (head->next != tmp) head = head->next;
        }
 
+       FILE *staticc = fopen(outputc, "w");
+       if (!staticc) {
+               fprintf(stderr, "Could not open file '%s' for writing: ", outputc);
+               perror(NULL);
+               exit(1);
+       }
+
        fprintf(staticc, "#include <device/device.h>\n");
        fprintf(staticc, "#include <device/pci.h>\n");
        struct header *h = &headers;
@@ -431,11 +493,15 @@ int main(int argc, char** argv) {
                h = h->next;
                fprintf(staticc, "#include \"%s/chip.h\"\n", h->name);
        }
+
+       walk_device_tree(staticc, &root, inherit_subsystem_ids, NULL);
+
        fprintf(staticc, "\n/* pass 0 */\n");
        walk_device_tree(staticc, &root, pass0, NULL);
-       fprintf(staticc, "\n/* pass 1 */\nstruct mainboard_config mainboard_info_0;\nstruct device **last_dev_p = &%s.next;\n", lastdev->name);
+       fprintf(staticc, "\n/* pass 1 */\nstruct mainboard_config mainboard_info_0;\nstruct device *last_dev = &%s;\n", lastdev->name);
        walk_device_tree(staticc, &root, pass1, NULL);
 
        fclose(staticc);
+
        return 0;
 }