Fix long standing MTRR bug.
[platform/upstream/libpciaccess.git] / src / common_vgaarb.c
index b7ef021..86eceb5 100644 (file)
@@ -33,6 +33,7 @@
 #include <stdlib.h>
 #include <limits.h>
 
+#include "config.h"
 #include "pciaccess.h"
 #include "pciaccess_private.h"
 
@@ -42,7 +43,7 @@ static int
 parse_string_to_decodes_rsrc(char *input, int *vga_count, struct pci_slot_match *match)
 {
     char *tok;
-    char *input_sp, *count_sp, *pci_sp;
+    char *input_sp = NULL, *count_sp, *pci_sp;
     char tmp[32];
 
     tok = strtok_r(input,",",&input_sp);
@@ -124,6 +125,10 @@ pci_device_vgaarb_init(void)
     struct pci_slot_match match;
     char buf[BUFSIZE];
     int ret, rsrc;
+
+    if (!pci_sys)
+        return -1;
+
     if ((pci_sys->vgaarb_fd = open ("/dev/vga_arbiter", O_RDWR)) < 0) {
         return errno;
     }
@@ -146,6 +151,9 @@ pci_device_vgaarb_init(void)
 void
 pci_device_vgaarb_fini(void)
 {
+    if (!pci_sys)
+        return;
+
     close(pci_sys->vgaarb_fd);
 }
 
@@ -252,10 +260,17 @@ pci_device_vgaarb_decodes(int new_vgaarb_rsrc)
     if (dev->vgaarb_rsrc == new_vgaarb_rsrc)
         return 0;
 
-    len = snprintf(buf, BUFSIZE, "decodes %s", rsrc_to_str(dev->vgaarb_rsrc));
+    len = snprintf(buf, BUFSIZE, "decodes %s", rsrc_to_str(new_vgaarb_rsrc));
     ret = vgaarb_write(pci_sys->vgaarb_fd, buf, len);
     if (ret == 0)
         dev->vgaarb_rsrc = new_vgaarb_rsrc;
+
+    ret = read(pci_sys->vgaarb_fd, buf, BUFSIZE);
+    if (ret <= 0)
+        return -1;
+
+    parse_string_to_decodes_rsrc(buf, &pci_sys->vga_count, NULL);
+
     return ret;
 }
 
@@ -317,7 +332,7 @@ int pci_device_vgaarb_get_info(struct pci_device *dev, int *vga_count, int *rsrc
 {
     *vga_count = pci_sys->vga_count;
     if (!dev)
-        return -1;
+        return 0;
 
     *rsrc_decodes = dev->vgaarb_rsrc;
         return 0;