summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTiago Vignatti <vignatti@freedesktop.org>2009-05-14 17:28:05 -0300
committerTiago Vignatti <vignatti@freedesktop.org>2009-05-14 17:28:05 -0300
commit6ae00a992714639fd725efd8817dca69b28276c8 (patch)
treea03d4f8bcb3b9569a3641f632d2ec69aaf35657f
parent6ef4e7aebfd2b83e2346e9f8f4ef3c890a6ea73d (diff)
Reorganize the code.
-rw-r--r--include/pciaccess.h15
-rw-r--r--src/common_vgaarb.c97
-rw-r--r--src/common_vgaarb_stub.c44
3 files changed, 48 insertions, 108 deletions
diff --git a/include/pciaccess.h b/include/pciaccess.h
index b7dad13..fa6787f 100644
--- a/include/pciaccess.h
+++ b/include/pciaccess.h
@@ -377,8 +377,8 @@ struct pci_device {
intptr_t user_data;
/**
- * Used by the VGA arbiter. Kind of resource decoded by the device and
- * the file descriptor. */
+ * Used by the VGA arbiter. Type of resource decoded by the device and
+ * the file descriptor (/dev/vga_arbiter). */
int vgaarb_rsrc;
int vgaarb_fd;
};
@@ -493,17 +493,6 @@ struct pci_pcmcia_bridge_info {
#define VGA_ARB_RSRC_NORMAL_IO 0x04
#define VGA_ARB_RSRC_NORMAL_MEM 0x08
-/*
- * With exception of vga_arb_trylock(), all functions bellow return 1 on success
- * and 0 if something goes wrong.
- * vga_arb_trylock returns 1 on success, 0 if the lock failed and -1 if
- * something went wrong.
- *
- * But I really don't think you should be checking the return values. The lib
- * checks for these errors but they should never happen, and when they happen
- * it will print error messages at stderr.
- *
- */
int pci_device_vgaarb_init (struct pci_device *dev);
void pci_device_vgaarb_fini (struct pci_device *dev);
int pci_device_vgaarb_set_target (struct pci_device *dev);
diff --git a/src/common_vgaarb.c b/src/common_vgaarb.c
index 9cf7385..3ac3779 100644
--- a/src/common_vgaarb.c
+++ b/src/common_vgaarb.c
@@ -25,33 +25,23 @@
*
*/
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <string.h>
#include <stdio.h>
-#include <stdlib.h>
#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "pciaccess.h"
-/* ALL messages *should* fit in this buffer */
-#define BUFSIZE 128
-#define VGAARB_DEV "/dev/vga_arbiter"
+#define BUFSIZE 32
int
pci_device_vgaarb_init(struct pci_device *dev)
{
dev->vgaarb_rsrc = VGA_ARB_RSRC_NONE;
- if ((dev->vgaarb_fd = open (VGAARB_DEV, O_RDWR)) < 0) {
- perror("device open failed");
- return 1;
+ if ((dev->vgaarb_fd = open ("/dev/vga_arbiter", O_RDWR)) < 0) {
+ fprintf(stderr, "device open failed");
+ return errno;
}
return 0;
@@ -60,49 +50,50 @@ pci_device_vgaarb_init(struct pci_device *dev)
void
pci_device_vgaarb_fini(struct pci_device *dev)
{
- if (close(dev->vgaarb_fd) == -1)
- perror("device close failed");
+ if (close(dev->vgaarb_fd) != 0)
+ fprintf(stderr, "device close failed");
}
-/*
- * Writes the message on the device.
+/**
+ * Writes messages on vga device. The messages is defined by the kernel
+ * implementation.
*
- * Returns: 0 if something went wrong
- * 1 if everything is ok
- * 2 if the device returned EBUSY (used ONLY by trylock)
+ * \param fd vga arbiter device.
+ * \param buf message itself.
+ * \param len message length.
+ *
+ * \return
+ * Zero on success, 1 if something gets wrong and 2 if fd is busy (only for
+ * 'trylock')
*/
static int
vgaarb_write(int fd, char *buf, int len)
{
int ret;
- /* Just to make sure... */
+
buf[len] = '\0';
ret = write(fd, buf, len);
-
if (ret == -1) {
- /* Check for EBUSY: the user may have called "trylock" and didn't get
- * the lock. */
+ /* the user may have called "trylock" and didn't get the lock */
if (errno == EBUSY)
return 2;
- perror("write error");
- return 0;
+ fprintf(stderr, "write error");
+ return 1;
}
else if (ret != len) {
- /* The VGA arbiter implementation shouldn't receive less than one
- * single message. It also shouldn't receive more. */
- fprintf(stderr, "%s: write error: "
- "wrote less than expected\n", __FUNCTION__);
- return 0;
+ /* it's need to receive the exactly amount required. */
+ fprintf(stderr, "write error: wrote different than expected\n");
+ return 1;
}
#ifdef DEBUG
fprintf(stderr, "%s: successfully wrote: '%s'\n", __FUNCTION__, buf);
#endif
- return 1;
+ return 0;
}
static const char *
@@ -119,35 +110,7 @@ rsrc_to_str(int iostate)
return "none";
}
-#if 0
-int
-vga_arb_read(vga_arb_ptr vgaDev)
-{
- int ret;
- char buf[BUFSIZE];
-
- ret = read (vgaDev->fd, buf, BUFSIZE);
-
- /* Just to make sure... */
- buf[ret]='\0';
-
- if (ret == 0) {
- /* It always has something to be read. */
- fprintf(stderr, "%s: error: there is nothing to read\n", __FUNCTION__);
- return 0;
- }
- else if (ret == -1) {
- perror("read error");
- return 0;
- }
-
-#ifdef DEBUG
- fprintf(stderr, "%s: sucessfully read: '%s'\n", __FUCNTION__, buf);
-#endif
- return 1;
-}
-#endif
int
pci_device_vgaarb_set_target(struct pci_device *dev)
{
@@ -185,20 +148,12 @@ pci_device_vgaarb_lock(struct pci_device *dev)
int
pci_device_vgaarb_trylock(struct pci_device *dev)
{
- int len, write_ret;
+ int len;
char buf[BUFSIZE];
len = snprintf(buf, BUFSIZE, "trylock %s", rsrc_to_str(dev->vgaarb_rsrc));
- write_ret = vgaarb_write(dev->vgaarb_fd, buf, len);
-
- if (write_ret == 0)
- return -1;
- else if (write_ret == 1)
- return 1;
- else
- /* write_ret == 2 and the lock failed */
- return 0;
+ return vgaarb_write(dev->vgaarb_fd, buf, len);
}
int
diff --git a/src/common_vgaarb_stub.c b/src/common_vgaarb_stub.c
index e849214..5fc5dfe 100644
--- a/src/common_vgaarb_stub.c
+++ b/src/common_vgaarb_stub.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2007 Paulo R. Zanoni, Tiago Vignatti
+ * Copyright (c) 2009 Tiago Vignatti
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
@@ -24,54 +24,50 @@
*
*/
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
#include <stdio.h>
#include "pciaccess.h"
-
int
-pci_device_vgaarb_target(struct pci_device *dev)
+pci_device_vgaarb_init(struct pci_device *dev)
{
- return 1;
+#ifdef DEBUG
+ fprintf(stderr, "%s: You're using VGA arbiter stub functions!\n",
+ __FUNCTION__);
+#endif
+ return 0;
}
-int
-pci_device_vgaarb_lock(struct pci_device *dev)
+void
+pci_device_vgaarb_fini(struct pci_device *dev)
{
- return 1;
}
int
-pci_device_vgaarb_trylock(struct pci_device *dev)
+pci_device_vgaarb_set_target(struct pci_device *dev)
{
- return 1;
+ return 0;
}
int
-pci_device_vgaarb_unlock(struct pci_device *dev)
+pci_device_vgaarb_decodes(struct pci_device *dev)
{
- return 1;
+ return 0;
}
int
-pci_device_vgaarb_decodes(struct pci_device *dev)
+pci_device_vgaarb_lock(struct pci_device *dev)
{
- return 1;
+ return 0;
}
int
-pci_device_vgaarb_init(struct pci_device *dev)
+pci_device_vgaarb_trylock(struct pci_device *dev)
{
-#ifdef DEBUG
- fprintf(stderr, "%s: YOU'RE USING THE STUB FUNCTIONS!\n", __FUNCTION__);
-#endif
- return 1;
+ return 0;
}
-void
-pci_device_vgaarb_fini(struct pci_device *dev)
+int
+pci_device_vgaarb_unlock(struct pci_device *dev)
{
+ return 0;
}