00001 /*!@file Devices/ParPort.C Basic Parallel Port driver */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // 00034 // Primary maintainer for this file: Laurent Itti <itti@usc.edu> 00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Devices/ParPort.C $ 00036 // $Id: ParPort.C 7971 2007-02-22 00:42:23Z itti $ 00037 // 00038 00039 #include "Devices/ParPort.H" 00040 00041 #include <cstdlib> 00042 #include <cerrno> 00043 #include <csignal> 00044 #include <iostream> 00045 00046 // ###################################################################### 00047 ParPort::ParPort(OptionManager& mgr, const std::string& descrName, 00048 const std::string& tagName) : 00049 ModelComponent(mgr, descrName, tagName), 00050 itsDevName(tagName+"DevName", this, "/dev/parport0"), 00051 itsFd(-1), itsData(0), itsNullMode(false) 00052 { } 00053 00054 // ###################################################################### 00055 ParPort::~ParPort() 00056 { } 00057 00058 // ###################################################################### 00059 void ParPort::start1() 00060 { 00061 if (itsDevName.getVal().compare("/dev/null") == 0) 00062 { 00063 // as a special case, if the user specifies "/dev/null" for the 00064 // parallel device, then we run in "disabled" mode where we 00065 // silently ignore attempts to write data 00066 itsFd = -1; 00067 itsNullMode = true; 00068 return; 00069 } 00070 00071 #ifndef HAVE_LINUX_PARPORT_H 00072 LFATAL("Oops! I need to have <linux/parport.h>"); 00073 #else 00074 const char *devnam = itsDevName.getVal().c_str(); 00075 00076 // open the port: 00077 itsFd = open(devnam, O_RDWR); 00078 if (itsFd == -1) 00079 { PLERROR("Cannot open parallel port %s", devnam); return; } 00080 00081 // claim the exclusively for userland access: 00082 if (ioctl(itsFd, PPEXCL)) 00083 { 00084 PLERROR("Cannot get exclusive access on %s", devnam); 00085 close(itsFd); 00086 itsFd = -1; 00087 return; 00088 } 00089 00090 if (ioctl(itsFd, PPCLAIM)) 00091 { 00092 PLERROR("Cannot ioctl(PPCLAIM) on %s", devnam); 00093 close(itsFd); 00094 itsFd = -1; 00095 return; 00096 } 00097 00098 // set to compatibility mode: 00099 int mode = IEEE1284_MODE_COMPAT; 00100 if (ioctl(itsFd, PPSETMODE, &mode)) 00101 { 00102 PLERROR("Cannot switch to IEEE-1284 mode on %s", devnam); 00103 ioctl(itsFd, PPRELEASE); 00104 close(itsFd); 00105 itsFd = -1; 00106 return; 00107 } 00108 #endif // HAVE_LINUX_PARPORT_H 00109 } 00110 00111 // ###################################################################### 00112 void ParPort::stop2() 00113 { 00114 #ifdef HAVE_LINUX_PARPORT_H 00115 // release and close port if needed: 00116 if (itsFd != -1) 00117 { 00118 if (ioctl(itsFd, PPRELEASE)) 00119 PLFATAL("Cannot release parport %s", itsDevName.getVal().c_str()); 00120 00121 close(itsFd); 00122 itsFd = -1; 00123 } 00124 #endif 00125 00126 itsNullMode = false; 00127 } 00128 00129 // ###################################################################### 00130 void ParPort::WriteData(const byte mask, const byte newdata) 00131 { 00132 if (itsNullMode) 00133 { 00134 LERROR("Parallel port is disabled; " 00135 "attempt to write (data=0x%02x, mask=0x%02x) " 00136 "has been ignored", newdata, mask); 00137 return; 00138 } 00139 00140 if (itsFd < 0) 00141 LFATAL("Parallel port is not initialized"); 00142 00143 #ifndef HAVE_LINUX_PARPORT_H 00144 LFATAL("Oops! I need to have <linux/parport.h>"); 00145 #else 00146 // clear all bits in our data that were selected by the mask: 00147 itsData &= ~mask; 00148 00149 // now set those bits to their new values specified in newdata: 00150 itsData |= (newdata & mask); 00151 00152 // set the port: 00153 if (ioctl(itsFd, PPWDATA, &itsData)) 00154 PLFATAL("Writing to parport failed"); 00155 #endif // HAVE_LINUX_PARPORT_H 00156 } 00157 00158 // ###################################################################### 00159 byte ParPort::ReadStatus() 00160 { 00161 if (itsFd < 0) 00162 LFATAL("Parallel port is not initialized"); 00163 00164 #ifndef HAVE_LINUX_PARPORT_H 00165 LFATAL("Oops! I need to have <linux/parport.h>"); 00166 return 0; 00167 #else 00168 byte in; 00169 if (ioctl(itsFd, PPRSTATUS, &in)) 00170 PLFATAL("Failed reading status of parallel port"); 00171 return in; 00172 #endif // HAVE_LINUX_PARPORT_H 00173 } 00174 00175 // ###################################################################### 00176 bool ParPort::ReadStatusPaperout() 00177 { 00178 #ifndef HAVE_LINUX_PARPORT_H 00179 LFATAL("Oops! I need to have <linux/parport.h>"); 00180 return 0; 00181 #else 00182 return ( (this->ReadStatus() & PARPORT_STATUS_PAPEROUT) != 0 ); 00183 #endif 00184 } 00185 00186 // ###################################################################### 00187 /* So things look consistent in everyone's emacs... */ 00188 /* Local Variables: */ 00189 /* indent-tabs-mode: nil */ 00190 /* End: */