3 * Copyright 2001,2003,2004 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
26 #include <ppio_ppdev.h>
27 #include <sys/types.h>
35 #ifdef HAVE_LINUX_PPDEV_H
36 #include <sys/ioctl.h>
37 #include <linux/ppdev.h>
38 #include <linux/parport.h>
41 // #warn "ppio_ppdev is not functional on this platform"
44 // These control port bits are active low.
45 // We toggle them so that this weirdness doesn't get get propagated
46 // through our interface.
48 static int CP_ACTIVE_LOW_BITS = 0x0B;
50 // These status port bits are active low.
51 // We toggle them so that this weirdness doesn't get get propagated
52 // through our interface.
54 static int SP_ACTIVE_LOW_BITS = 0x80;
56 #ifndef HAVE_LINUX_PPDEV_H // use stubs
58 ppio_ppdev::ppio_ppdev (int which)
60 std::cerr << "ppio_ppdev: Not implemented on this platform\n";
61 throw std::runtime_error ("not implmeneted");
64 ppio_ppdev::~ppio_ppdev ()
69 ppio_ppdev::write_data (unsigned char v)
74 ppio_ppdev::read_data ()
80 ppio_ppdev::write_control (unsigned char v)
85 ppio_ppdev::read_control ()
91 ppio_ppdev::read_status ()
102 ppio_ppdev::unlock ()
110 ppio_ppdev::ppio_ppdev (int which)
112 std::ostringstream filename;
113 filename << "/dev/parport" << which;
114 const char *c_filename = filename.str().c_str();
116 if ((d_fd = open (c_filename, O_RDWR)) < 0){
117 int my_errno = errno;
119 if (my_errno == ENOENT){
120 std::cerr << "Does the device file " << c_filename << " exist?\n";
121 std::cerr << "If not, as root execute: \n";
122 std::cerr << " # mknod " << c_filename << " c 99 0\n";
123 std::cerr << " # chmod 666 " << c_filename << std::endl;
125 throw std::runtime_error ("open");
128 int mode = IEEE1284_MODE_COMPAT;
129 if (ioctl (d_fd, PPSETMODE, &mode) != 0){
130 perror ("ppio_ppdev: PPSETMODE");
132 throw std::runtime_error ("PPSETMODE");
136 ppio_ppdev::~ppio_ppdev ()
143 ppio_ppdev::write_data (unsigned char v)
145 if (ioctl (d_fd, PPWDATA, &v) != 0){
146 perror ("ppio_ppdev: PPWDATA");
147 throw std::runtime_error ("PPWDATA");
152 ppio_ppdev::read_data ()
156 if (ioctl (d_fd, PPRDATA, &v) != 0){
157 perror ("ppio_ppdev: PPRDATA");
158 throw std::runtime_error ("PPRDATA");
164 ppio_ppdev::write_control (unsigned char v)
166 unsigned char ctrl = v ^ CP_ACTIVE_LOW_BITS;
167 if (ioctl (d_fd, PPWCONTROL, &ctrl) != 0){
168 perror ("ppio_ppdev: PPWCONTROL");
169 throw std::runtime_error ("PPWCONTROL");
174 ppio_ppdev::read_control ()
177 if (ioctl (d_fd, PPRCONTROL, &ctrl) != 0){
178 perror ("ppio_ppdev: PPRCONTROL");
179 throw std::runtime_error ("PPRCONTROL");
182 return ctrl ^ CP_ACTIVE_LOW_BITS;
186 ppio_ppdev::read_status ()
188 unsigned char status;
189 if (ioctl (d_fd, PPRSTATUS, &status) != 0){
190 perror ("ppio_ppdev: PPRSTATUS");
191 throw std::runtime_error ("PPRSTATUS");
194 return status ^ SP_ACTIVE_LOW_BITS;
200 if (ioctl (d_fd, PPCLAIM) != 0){
201 perror ("ppio_ppdev: PPCLAIM");
202 throw std::runtime_error ("PPCLAIM");
207 ppio_ppdev::unlock ()
209 if (ioctl (d_fd, PPRELEASE) != 0){
210 perror ("ppio_ppdev: PPRELEASE");
211 throw std::runtime_error ("PPRELEASE");
218 make_ppio_ppdev (int which)
220 return ppio_ppdev_sptr (new ppio_ppdev (which));