[lcdproc] Trouble using FreeBSD and HD44780 drivers

Philip Pokorny ppokorny@penguincomputing.com
Wed, 06 Jun 2001 17:17:32 -0700


--------------B42E12DA1F10C4EB70EE97E6
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Guillaume Filion wrote:
> >Guillaume Filion wrote:
> >>  I've made some corrections to the IOPerm code to work with FreeBSD
> >>  and I'd be ready to commit it to CVS. Who should I contact to get CVS
> >>  commit access?
> >
> >I'm trying to collect patches to make 0.4.1a...  Can you send me your
> >'cvs diff -u' and I'll add it to a "jumbo" patch for 0.4.1?
>
> Here it is!

Thanks!

I was looking at the man page for ioperm, and it recommends against
<sys/perm.h> in favor of <sys/io.h>.  Also, I was wondering if perhaps
it would better to create a "port_access" function to go with the
port_in and port_out in port.h that would encapsulate the OS differences
on getting access to a port.  Then each hd44780 driver could call
port_access for the ports it will be using and port_in/port_out to
access those ports.

Fewer #ifdef in each file and less duplicated code.

Any thoughts?

In attempting to implement this, I came across a couple of cases where
the hd44780 driver accesses a port it hasn't asked for with ioperm...

Attached is what I propose...

-- 
Philip Pokorny, Senior Engineer
http://www.penguincomputing.com

Penguin Computing - The World's Most Reliable Linux Systems
--------------B42E12DA1F10C4EB70EE97E6
Content-Type: text/plain; charset=us-ascii;
 name="patch_0.4.1-ioperm"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="patch_0.4.1-ioperm"

Index: configure.in
===================================================================
RCS file: /cvsroot/lcdproc/lcdproc/configure.in,v
retrieving revision 1.8
diff -u -r1.8 configure.in
--- configure.in	2001/05/25 03:14:27	1.8
+++ configure.in	2001/06/05 22:42:37
@@ -86,7 +86,7 @@
 dnl Checks for library functions.
 AC_PROG_GCC_TRADITIONAL
 AC_TYPE_SIGNAL
-AC_CHECK_FUNCS(select socket strdup strerror strtol uname)
+AC_CHECK_FUNCS(select socket strdup strerror strtol uname ioperm)
 
 dnl Check for various defines and features
 
Index: server/drivers/hd44780-4bit.c
===================================================================
RCS file: /cvsroot/lcdproc/lcdproc/server/drivers/hd44780-4bit.c,v
retrieving revision 1.5
diff -u -r1.5 hd44780-4bit.c
--- server/drivers/hd44780-4bit.c	2001/05/25 03:14:28	1.5
+++ server/drivers/hd44780-4bit.c	2001/06/06 23:57:04
@@ -40,7 +40,6 @@
 #include "port.h"
 
 #include "shared/str.h"
-#include <sys/perm.h>
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
@@ -83,7 +82,7 @@
 	for (i = 0; i < argc; ++i) {
 		if (strcmp (argv[i], "-e") == 0 || strcmp (argv[i], "--extended") == 0) {
 			extIF = 1;
-			if ((ioperm (port + 2, 1, 255)) == -1) {
+			if (port_access (port + 2)) {
 				fprintf (stderr, "HD44780_init: failed (%s)\n", strerror (errno));
 				return -1;
 			}
Index: server/drivers/hd44780-ext8bit.c
===================================================================
RCS file: /cvsroot/lcdproc/lcdproc/server/drivers/hd44780-ext8bit.c,v
retrieving revision 1.4
diff -u -r1.4 hd44780-ext8bit.c
--- server/drivers/hd44780-ext8bit.c	2000/04/03 22:13:57	1.4
+++ server/drivers/hd44780-ext8bit.c	2001/06/06 23:57:04
@@ -33,7 +33,6 @@
 #include "port.h"
 
 #include "lcd_sem.h"
-#include <sys/perm.h>
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
@@ -66,7 +65,7 @@
 
 	hd44780_functions->senddata = lcdtime_HD44780_senddata;
 
-	if ((ioperm (port + 2, 1, 255)) == -1) {
+	if (port_access (port + 2)) {
 		fprintf (stderr, "HD44780_init: failed (%s)\n", strerror (errno));
 		return -1;
 	}
Index: server/drivers/hd44780-serialLpt.c
===================================================================
RCS file: /cvsroot/lcdproc/lcdproc/server/drivers/hd44780-serialLpt.c,v
retrieving revision 1.4
diff -u -r1.4 hd44780-serialLpt.c
--- server/drivers/hd44780-serialLpt.c	2000/04/03 22:13:58	1.4
+++ server/drivers/hd44780-serialLpt.c	2001/06/06 23:57:04
@@ -27,6 +27,10 @@
  * COPYING file distributed with this package.
  */
 
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
 #include "hd44780-serialLpt.h"
 #include "port.h"
 
@@ -53,6 +57,10 @@
 	int displayID = EN1 | EN2;
 
 	lp = port;
+	if (port_access (lp) || port_access (lp +1)) {
+		fprintf (stderr, "HD44780_init: failed (%s)\n", strerror (errno));
+		return( -1 );
+	};
 
 	hd44780_functions->senddata = lcdserLpt_HD44780_senddata;
 
Index: server/drivers/hd44780-winamp.c
===================================================================
RCS file: /cvsroot/lcdproc/lcdproc/server/drivers/hd44780-winamp.c,v
retrieving revision 1.6
diff -u -r1.6 hd44780-winamp.c
--- server/drivers/hd44780-winamp.c	2001/05/25 03:14:28	1.6
+++ server/drivers/hd44780-winamp.c	2001/06/06 23:57:04
@@ -36,7 +36,6 @@
 #include "port.h"
 
 #include "shared/str.h"
-#include <sys/perm.h>
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
@@ -73,7 +72,7 @@
 		if (strcmp (argv[i], "-e") == 0 || strcmp (argv[i], "--extended") == 0)
 			extIF = 1;
 
-	if ((ioperm (lptPort + 2, 1, 255)) == -1) {
+	if (port_access (lptPort + 2) || port_access (lptPort)) {
 		fprintf (stderr, "HD44780_init: failed (%s)\n", strerror (errno));
 		return -1;
 	}
Index: server/drivers/hd44780.c
===================================================================
RCS file: /cvsroot/lcdproc/lcdproc/server/drivers/hd44780.c,v
retrieving revision 1.9
diff -u -r1.9 hd44780.c
--- server/drivers/hd44780.c	2001/05/25 03:14:28	1.9
+++ server/drivers/hd44780.c	2001/06/06 23:57:04
@@ -36,10 +36,10 @@
 #include <fcntl.h>
 #include <string.h>
 #include <errno.h>
-#include <sys/perm.h>
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
+#include "port.h"
 
 // Uncomment one of the lines below to select your desired delay generation
 // mechanism.  If both defines are commented, the original I/O read timing
@@ -254,7 +254,7 @@
 	}
 #endif
 	// Set up io port correctly, and open it...
-	if ((ioperm (port, 1, 255)) == -1) {
+	if (port_access (port)) {
 		fprintf (stderr, "HD44780_init: failed (%s)\n", strerror (errno));
 		return -1;
 	}
Index: server/drivers/port.h
===================================================================
RCS file: /cvsroot/lcdproc/lcdproc/server/drivers/port.h,v
retrieving revision 1.3
diff -u -r1.3 port.h
--- server/drivers/port.h	2000/04/03 22:13:58	1.3
+++ server/drivers/port.h	2001/06/06 23:57:04
@@ -1,3 +1,5 @@
+#ifndef PORT_H
+#define PORT_H
 /*-----------------------------------------------------------------------------
  * port.h (by damianf@wpi.edu)
  * Low level I/O functions taken from led-stat.txt
@@ -9,9 +11,8 @@
  */
 
 /* #define DOS */
-
-#ifndef PORT_H
-#define PORT_H
+#ifdef HAVE_CONFIG_H
+# include "config.h"
 #endif
 
 #ifndef DOS
@@ -30,6 +31,38 @@
 	__asm__ volatile ("outb %0,%1\n"::"a" (val), "d" (port)
 		 );
 }
+
+/***
+ *** Get access to a specific port
+ ***/
+#ifdef HAVE_IOPERM
+/* Assume this is a LINUX system with ioperm */
+#include <sys/io.h>
+
+static inline int
+port_access (unsigned short int port)
+{
+	return ioperm( port, 1, 255);
+}
+
+#else
+/* Assume this is a BSD system */
+#include <stdio.h>
+
+FILE *  port_access_handle = NULL ;
+
+static inline int
+port_access (unsigned short int port)
+{
+	if( port_access_handle
+	    || (port_access_handle = fopen("/dev/io", "rw")) != NULL ) {
+		return( 0 );  /* Success */
+	} else {
+		return( -1 );  /* Failure */
+	};
+}
+#endif
+
 #else
 #include <pc.h>
 
@@ -49,3 +82,4 @@
 #endif
 
 /**** END OF FILE ****/
+#endif


--------------B42E12DA1F10C4EB70EE97E6
Content-Type: text/plain; charset=


-----------------------------------------------------------
To unsubscribe from this list send a blank message to
lcdproc-unsubscribe@lists.omnipotent.net
--------------B42E12DA1F10C4EB70EE97E6--