Created
November 15, 2012 12:06
-
-
Save anonymous/4078331 to your computer and use it in GitHub Desktop.
patch graphlcd-base port.c to protect access to port
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff -Naur graphlcd-base-9999_orig/glcddrivers/noritake800.c graphlcd-base-9999/glcddrivers/noritake800.c | |
--- graphlcd-base-9999_orig/glcddrivers/noritake800.c 2012-11-15 00:07:15.000000000 +0100 | |
+++ graphlcd-base-9999/glcddrivers/noritake800.c 2012-11-15 00:08:52.604326957 +0100 | |
@@ -244,19 +244,25 @@ | |
{ | |
// use DirectIO | |
if (m_pport->Open(config->port) != 0) | |
+ { | |
+ syslog(LOG_ERR, "%s: INFO: cannot open configured port %x, Err: %s (cDriverNoritake800::Init)\n", config->name.c_str(), config->port, strerror(errno)); | |
return -1; | |
+ } | |
uSleep(10); | |
} | |
else | |
{ | |
// use ppdev | |
if (m_pport->Open(config->device.c_str()) != 0) | |
+ { | |
+ syslog(LOG_ERR, "%s: INFO: cannot open configured device %s, Err: %s (cDriverNoritake800::Init)\n", config->name.c_str(), config->device.c_str(), strerror(errno)); | |
return -1; | |
+ } | |
} | |
if (nSleepInit() != 0) | |
{ | |
- syslog(LOG_ERR, "%s: INFO: cannot change wait parameters Err: %s (cDriver::Init)\n", config->name.c_str(), strerror(errno)); | |
+ syslog(LOG_ERR, "%s: INFO: cannot change wait parameters Err: %s (cDriverNoritake800::Init)\n", config->name.c_str(), strerror(errno)); | |
m_bSleepIsInit = false; | |
} | |
else | |
@@ -265,7 +271,11 @@ | |
} | |
// benchmark port access | |
- m_pport->Claim(); | |
+ if (!m_pport->Claim()) | |
+ { | |
+ syslog(LOG_ERR, "%s: INFO: cannot claim port Err: %s (cDriverNoritake800::Init)\n", config->name.c_str(), strerror(errno)); | |
+ return -1; | |
+ } | |
syslog(LOG_DEBUG, "%s: benchmark started.\n", config->name.c_str()); | |
gettimeofday(&tv1, 0); | |
int nBenchIterations = 10000; | |
@@ -338,29 +348,31 @@ | |
refreshAll = true; | |
} | |
- m_pport->Claim(); | |
- for (xb = 0; xb < width; ++xb) | |
+ if (m_pport->Claim()) | |
{ | |
- for (yb = 0; yb < m_iSizeYb; ++yb) | |
+ for (xb = 0; xb < width; ++xb) | |
{ | |
- // if differenet or explicitly refresh all | |
- if ( m_pVFDMem[xb][yb] != m_pDrawMem[xb][yb] || | |
- refreshAll ) | |
+ for (yb = 0; yb < m_iSizeYb; ++yb) | |
{ | |
- m_pVFDMem[xb][yb] = m_pDrawMem[xb][yb]; | |
- // reset RefreshCounter if doing a full refresh | |
- if (refreshAll) | |
- m_nRefreshCounter = 0; | |
- // actually write to display | |
- N800WriteByte( | |
- (m_pVFDMem[xb][yb]) ^ ((config->invert != 0) ? 0xff : 0x00), | |
- xb, | |
- yb, | |
- 0); | |
+ // if differenet or explicitly refresh all | |
+ if ( m_pVFDMem[xb][yb] != m_pDrawMem[xb][yb] || | |
+ refreshAll ) | |
+ { | |
+ m_pVFDMem[xb][yb] = m_pDrawMem[xb][yb]; | |
+ // reset RefreshCounter if doing a full refresh | |
+ if (refreshAll) | |
+ m_nRefreshCounter = 0; | |
+ // actually write to display | |
+ N800WriteByte( | |
+ (m_pVFDMem[xb][yb]) ^ ((config->invert != 0) ? 0xff : 0x00), | |
+ xb, | |
+ yb, | |
+ 0); | |
+ } | |
} | |
} | |
+ m_pport->Release(); | |
} | |
- m_pport->Release(); | |
} | |
void cDriverNoritake800::N800Cmd(unsigned char data) | |
@@ -458,9 +470,11 @@ | |
} | |
unsigned int darkness = 16 - (unsigned int)((double)percent * 16.0 / 100.0); | |
- m_pport->Claim(); | |
- N800Cmd(0x40 + (darkness & 0xf)); | |
- m_pport->Release(); | |
+ if (m_pport->Claim()) | |
+ { | |
+ N800Cmd(0x40 + (darkness & 0xf)); | |
+ m_pport->Release(); | |
+ } | |
} | |
unsigned char cDriverNoritake800::N800LptWiringMask(unsigned char ctrl_bits) | |
diff -Naur graphlcd-base-9999_orig/glcddrivers/port.c graphlcd-base-9999/glcddrivers/port.c | |
--- graphlcd-base-9999_orig/glcddrivers/port.c 2012-11-15 00:07:15.000000000 +0100 | |
+++ graphlcd-base-9999/glcddrivers/port.c 2012-11-15 00:07:34.946321677 +0100 | |
@@ -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__ | |
@@ -168,16 +171,20 @@ | |
return 0; | |
} | |
-void cParallelPort::Claim() | |
+bool cParallelPort::Claim() | |
{ | |
if (usePPDev) | |
- ioctl(fd, PPCLAIM); | |
+ return (ioctl(fd, PPCLAIM) == 0); | |
+ else | |
+ return (pthread_mutex_lock(&claimport_mutex) == 0); | |
} | |
void cParallelPort::Release() | |
{ | |
if (usePPDev) | |
ioctl(fd, PPRELEASE); | |
+ else | |
+ pthread_mutex_unlock(&claimport_mutex); | |
} | |
void cParallelPort::SetDirection(int direction) | |
diff -Naur graphlcd-base-9999_orig/glcddrivers/port.h graphlcd-base-9999/glcddrivers/port.h | |
--- graphlcd-base-9999_orig/glcddrivers/port.h 2012-11-15 00:07:15.000000000 +0100 | |
+++ graphlcd-base-9999/glcddrivers/port.h 2012-11-15 00:07:34.947321678 +0100 | |
@@ -45,7 +45,7 @@ | |
bool IsDirectIO() const { return (!usePPDev); } | |
int GetPortHandle() const { return ((usePPDev) ? fd : port); } | |
- void Claim(); | |
+ bool Claim(); | |
void Release(); | |
void SetDirection(int direction); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment