summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLucian Muresan <lucianm@users.sourceforge.net>2012-11-16 02:20:39 +0100
committerLucian Muresan <lucianm@users.sourceforge.net>2012-11-16 02:20:39 +0100
commit6e3703d03fe1d425f396c8905179765491a4a623 (patch)
treec53a5893471db2d5c3d4cc2f4ad9a6d0cd6cf5da
parent6bbc07ed1dc8f617222f229df083c8cca60d1608 (diff)
downloadgraphlcd-base-6e3703d03fe1d425f396c8905179765491a4a623.tar.gz
graphlcd-base-6e3703d03fe1d425f396c8905179765491a4a623.tar.bz2
cParallelPort: safer Claim/Release, in both Direct I/O and PPDEV mode
-rw-r--r--glcddrivers/port.c31
-rw-r--r--glcddrivers/port.h4
2 files changed, 26 insertions, 9 deletions
diff --git a/glcddrivers/port.c b/glcddrivers/port.c
index 44d5956..cfb4156 100644
--- a/glcddrivers/port.c
+++ b/glcddrivers/port.c
@@ -17,6 +17,7 @@
#include <syslog.h>
#include <unistd.h>
#include <termios.h>
+#include <pthread.h>
#include <sys/io.h>
#include <sys/ioctl.h>
#include <linux/ppdev.h>
@@ -33,6 +34,8 @@
namespace GLCD
{
+static pthread_mutex_t claimport_mutex;
+
static inline int port_in(int port)
{
#ifdef __HAS_DIRECTIO__
@@ -58,7 +61,8 @@ static inline void port_out(unsigned short int port, unsigned char val)
cParallelPort::cParallelPort()
: fd(-1),
port(0),
- usePPDev(false)
+ usePPDev(false),
+ portClaimed(false)
{
}
@@ -109,7 +113,7 @@ int cParallelPort::Open(const char * device)
return -1;
}
- if (ioctl(fd, PPCLAIM, NULL) == -1)
+ if (!Claim())
{
syslog(LOG_ERR, "glcd drivers: ERROR cannot claim %s. Err:%s (cParallelPort::Init)\n",
device, strerror(errno));
@@ -118,7 +122,7 @@ int cParallelPort::Open(const char * device)
}
int mode = PARPORT_MODE_PCSPP;
- if (ioctl(fd, PPSETMODE, &mode) == -1)
+ if (ioctl(fd, PPSETMODE, &mode) != 0)
{
syslog(LOG_ERR, "glcd drivers: ERROR cannot setmode %s. Err:%s (cParallelPort::Init)\n",
device, strerror(errno));
@@ -168,16 +172,27 @@ int cParallelPort::Close()
return 0;
}
-void cParallelPort::Claim()
+bool cParallelPort::Claim()
{
- if (usePPDev)
- ioctl(fd, PPCLAIM);
+ if (!IsPortClaimed())
+ {
+ if (usePPDev)
+ portClaimed = (ioctl(fd, PPCLAIM) == 0);
+ else
+ portClaimed = (pthread_mutex_lock(&claimport_mutex) == 0);
+ }
+ return IsPortClaimed();
}
void cParallelPort::Release()
{
- if (usePPDev)
- ioctl(fd, PPRELEASE);
+ if (IsPortClaimed())
+ {
+ if (usePPDev)
+ portClaimed = !(ioctl(fd, PPRELEASE) == 0);
+ else
+ portClaimed = !(pthread_mutex_unlock(&claimport_mutex) == 0);
+ }
}
void cParallelPort::SetDirection(int direction)
diff --git a/glcddrivers/port.h b/glcddrivers/port.h
index 2920461..6fcd635 100644
--- a/glcddrivers/port.h
+++ b/glcddrivers/port.h
@@ -33,6 +33,7 @@ private:
int fd;
int port;
bool usePPDev;
+ bool portClaimed;
public:
cParallelPort();
@@ -45,8 +46,9 @@ public:
bool IsDirectIO() const { return (!usePPDev); }
int GetPortHandle() const { return ((usePPDev) ? fd : port); }
- void Claim();
+ bool Claim();
void Release();
+ bool IsPortClaimed() const { return (portClaimed); }
void SetDirection(int direction);
unsigned char ReadControl();