port.hpp

00001 /* libcqcam - shared Color Quickcam routines
00002  * Copyright (C) 1996-1998 by Patrick Reynolds
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  */
00019 
00020 // I/O ports wrapper definitions and prototypes
00021 // This file might need tweaking if you're trying to port my code to other
00022 // x86 Unix platforms.  Code is already available for Linux, FreeBSD, and 
00023 // QNX; see the Makefile.
00024 //
00025 // QNX code by: Anders Arpteg <aa11ac@hik.se>
00026 // FreeBSD code by: Patrick Reynolds <reynolds@cs.duke.edu> and Charles 
00027 //                  Henrich <henrich@msu.edu>
00028 // Inlining implemented by: Philip Blundell <philip.blundell@pobox.com>
00029 
00030 #ifndef PORT_H
00031 #define PORT_H
00032 
00033 //#include "config.h"
00034 
00035 #include <unistd.h>
00036 
00037 #ifdef LINUX
00038   #ifndef arm
00039   #include <sys/io.h>
00040   #endif /* !arm */
00041 #elif defined(QNX)
00042 #include <conio.h>
00043 #elif defined(FREEBSD)
00044 #include <machine/cpufunc.h>
00045 #include <stdio.h>
00046 #elif defined(BSDI)
00047 #include <machine/inline.h>
00048 #elif defined(OPENBSD)
00049 #include <machine/pio.h>
00050 #elif defined(LYNX)
00051 #include "lynx-io.h"
00052 #elif defined(SOLARIS)
00053 #include "solaris-io.h"
00054 #else
00055 #error Please define a platform in the Makefile
00056 #endif
00057 
00058 #ifdef arm
00059 static char ports_temp;
00060 
00061 #ifdef inb
00062 #undef inb
00063 #endif /* inb */
00064 #define inb(port) \
00065   lseek(devport, port, SEEK_SET), \
00066   read(devport, &ports_temp, 1), \
00067   ports_temp
00068 
00069 #ifdef outb
00070 #undef outb
00071 #endif /* inb */
00072 #define outb(data, port) \
00073   lseek(devport, port, SEEK_SET); \
00074   ports_temp = data; \
00075   write(devport, &ports_temp, 1);
00076 
00077 #endif /* arm */
00078 
00079 class port_t {
00080 public:
00081   port_t(int iport);
00082   ~port_t(void);
00083 
00084   inline int read_data(void) { return inb(port); }
00085   inline int read_status(void) { return inb(port1); }
00086   inline int read_control(void) { return inb(port2); }
00087 
00088 #if defined(LINUX) || defined(LYNX)
00089   inline void write_data(int data) { outb(data, port); }
00090   inline void write_control(int data) { outb(control_reg = data, port2); }
00091   inline void setbit_control(int data) { outb(control_reg |= data, port2); }
00092   inline void clearbit_control(int data) { outb(control_reg &= ~data, port2); }
00093 #else // Solaris, QNX, and *BSD use (port, data) instead
00094   inline void write_data(int data) { outb(port, data); }
00095   inline void write_control(int data) { outb(port2, control_reg = data); }
00096   inline void setbit_control(int data) { outb(port2, control_reg |= data); }
00097   inline void clearbit_control(int data) { outb(port2, control_reg &= ~data); }
00098 #endif
00099 
00100   inline int get_port() { return port; }
00101   inline operator bool () const { return port != -1; }
00102 
00103 private:
00104   int port;        // number of the base port
00105   int port1;       // port+1, precalculated for speed
00106   int port2;       // port+2, precalculated for speed
00107   int control_reg; // current contents of the control register
00108 #ifdef LOCKING
00109   int lock_fd;
00110   int lock(int portnum);
00111   void unlock(int portnum);
00112 #endif
00113 
00114 #ifdef FREEBSD
00115   FILE *devio;
00116 #endif
00117 #if defined(LINUX) && defined(arm)
00118   int devport;
00119 #endif
00120 };
00121 
00122 #endif

Generated on Sat Oct 27 09:21:03 2007 for QastroCam by  doxygen 1.5.1