2 * GraphLCD driver library
4 * port.c - parallel port class with low level routines
6 * This file is released under the GNU General Public License. Refer
7 * to the COPYING file distributed with this package.
9 * (c) 2004 Andreas Regel <andreas.regel AT powarman.de>
21 #include <sys/ioctl.h>
22 #include <linux/ppdev.h>
23 #include <linux/parport.h>
35 static inline int port_in(int port)
38 __asm__ volatile ("inb %1,%0"
40 : "d" ((unsigned short) port));
44 static inline void port_out(unsigned short int port, unsigned char val)
46 __asm__ volatile ("outb %0,%1\n"
48 : "a" (val), "d" (port));
51 cParallelPort::cParallelPort()
58 cParallelPort::~cParallelPort()
62 int cParallelPort::Open(int portIO)
69 if (ioperm(port, 3, 255) == -1)
71 syslog(LOG_ERR, "glcd drivers: ERROR ioperm(0x%X) failed! Err:%s (cParallelPort::Open)\n",
72 port, strerror(errno));
80 syslog(LOG_ERR, "glcd drivers: ERROR iopl failed! Err:%s (cParallelPort::Init)\n",
88 int cParallelPort::Open(const char * device)
92 fd = open(device, O_RDWR);
95 syslog(LOG_ERR, "glcd drivers: ERROR cannot open %s. Err:%s (cParallelPort::Init)\n",
96 device, strerror(errno));
100 if (ioctl(fd, PPCLAIM, NULL) == -1)
102 syslog(LOG_ERR, "glcd drivers: ERROR cannot claim %s. Err:%s (cParallelPort::Init)\n",
103 device, strerror(errno));
108 int mode = PARPORT_MODE_PCSPP;
109 if (ioctl(fd, PPSETMODE, &mode) == -1)
111 syslog(LOG_ERR, "glcd drivers: ERROR cannot setmode %s. Err:%s (cParallelPort::Init)\n",
112 device, strerror(errno));
120 int cParallelPort::Close()
126 ioctl(fd, PPRELEASE);
139 if (ioperm(port, 3, 0) == -1)
155 void cParallelPort::Claim()
161 void cParallelPort::Release()
164 ioctl(fd, PPRELEASE);
167 void cParallelPort::SetDirection(int direction)
171 if (ioctl(fd, PPDATADIR, &direction) == -1)
173 perror("ioctl(PPDATADIR)");
179 if (direction == kForward)
180 port_out(port + 2, port_in(port + 2) & 0xdf);
182 port_out(port + 2, port_in(port + 2) | 0x20);
186 unsigned char cParallelPort::ReadControl()
192 if (ioctl(fd, PPRCONTROL, &value) == -1)
194 perror("ioctl(PPRCONTROL)");
200 value = port_in(port + 2);
206 void cParallelPort::WriteControl(unsigned char value)
210 if (ioctl(fd, PPWCONTROL, &value) == -1)
212 perror("ioctl(PPWCONTROL)");
218 port_out(port + 2, value);
222 unsigned char cParallelPort::ReadStatus()
228 if (ioctl(fd, PPRSTATUS, &value) == -1)
230 perror("ioctl(PPRSTATUS)");
236 value = port_in(port + 1);
242 unsigned char cParallelPort::ReadData()
248 if (ioctl(fd, PPRDATA, &data) == -1)
250 perror("ioctl(PPRDATA)");
256 data = port_in(port);
262 void cParallelPort::WriteData(unsigned char data)
266 if (ioctl(fd, PPWDATA, &data) == -1)
268 perror("ioctl(PPWDATA)");
274 port_out(port, data);
280 cSerialPort::cSerialPort()
285 cSerialPort::~cSerialPort()
289 int cSerialPort::Open(const char * device, void (*FP)(int))
291 struct termios options;
292 struct sigaction saio;
294 fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
297 printf("error opening port\n");
302 // install the signal handler before making the device asynchronous/
303 saio.sa_handler = FP;
304 sigemptyset (&saio.sa_mask);
306 saio.sa_restorer = NULL;
307 sigaction(SIGIO,&saio,NULL);
309 // allow the process to receive SIGIO
310 fcntl(fd, F_SETOWN, getpid());
311 // Make the file descriptor asynchronous
312 fcntl(fd, F_SETFL, FASYNC);
314 tcgetattr(fd, &options);
316 cfsetispeed(&options, B921600);
317 cfsetospeed(&options, B921600);
319 options.c_cflag &= ~PARENB;
320 options.c_cflag &= ~CSTOPB;
321 options.c_cflag &= ~CSIZE;
322 options.c_cflag |= CS8;
324 options.c_cflag &= ~CRTSCTS;
326 options.c_cflag |= (CLOCAL | CREAD);
328 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
330 options.c_iflag &= ~(IXON | IXOFF | IXANY);
332 options.c_oflag &= ~OPOST;
334 tcsetattr(fd, TCSANOW, &options);
339 int cSerialPort::Close()
347 int cSerialPort::ReadData(unsigned char * data)
351 return read(fd, data, 1);
354 void cSerialPort::WriteData(unsigned char data)
359 void cSerialPort::WriteData(unsigned char * data, unsigned short length)
363 write(fd, data, length);
367 } // end of namespace