| 1 |
/* Hey EMACS -*- linux-c -*- */ |
| 2 |
/* $Id$ */ |
| 3 |
|
| 4 |
/* libticables2 - link cable library, a part of the TiLP project |
| 5 |
* Copyright (C) 1999-2005 Romain Lievin |
| 6 |
* Copyright (c) 2002, Kevin Kofler for the __MINGW32__ & __GNUC__ extensions. |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program; if not, write to the Free Software Foundation, |
| 20 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
*/ |
| 22 |
|
| 23 |
/* |
| 24 |
* This unit manages direct low-level and user-landI/O operations. |
| 25 |
*/ |
| 26 |
|
| 27 |
#ifdef HAVE_CONFIG_H |
| 28 |
#include <config.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
#include <stdio.h> |
| 32 |
#include <stdint.h> |
| 33 |
#include <unistd.h> |
| 34 |
#include <termios.h> |
| 35 |
#include <sys/ioctl.h> |
| 36 |
#include <fcntl.h> |
| 37 |
#include <sys/types.h> |
| 38 |
#include <sys/stat.h> |
| 39 |
#ifdef HAVE_LINUX_PARPORT_H |
| 40 |
#include <linux/parport.h> |
| 41 |
#include <linux/ppdev.h> |
| 42 |
#endif |
| 43 |
#ifdef HAVE_LINUX_SERIAL_H |
| 44 |
#include <linux/serial.h> |
| 45 |
#endif |
| 46 |
#include <errno.h> |
| 47 |
#include <string.h> |
| 48 |
|
| 49 |
#include "../gettext.h" |
| 50 |
#include "../error.h" |
| 51 |
#include "../logging.h" |
| 52 |
#include "ioports.h" |
| 53 |
|
| 54 |
#ifdef HAVE_LINUX_PARPORT_H |
| 55 |
|
| 56 |
int par_io_open(const char *dev_name, int *dev_fd) |
| 57 |
{ |
| 58 |
int mode; |
| 59 |
|
| 60 |
// Open device |
| 61 |
*dev_fd = open(dev_name, O_RDWR); |
| 62 |
if (*dev_fd == -1) |
| 63 |
{ |
| 64 |
ticables_warning("unable to open parallel device '%s'.", dev_name); |
| 65 |
return ERR_PPT_OPEN; |
| 66 |
} |
| 67 |
|
| 68 |
// Ask for exclusive access |
| 69 |
if (ioctl(*dev_fd, PPEXCL) == -1) |
| 70 |
{ |
| 71 |
ticables_warning("%s", _("ioctl failed on parallel device: can't claim exclusive access.")); |
| 72 |
ticables_warning("%s", _("we will do without that.")); |
| 73 |
} |
| 74 |
|
| 75 |
// Claim access |
| 76 |
if (ioctl(*dev_fd, PPCLAIM) == -1) |
| 77 |
{ |
| 78 |
ticables_warning("%s", _("ioctl failed on parallel device: can't claim parport.")); |
| 79 |
return ERR_PPT_IOCTL; |
| 80 |
} |
| 81 |
|
| 82 |
// Change transfer mode |
| 83 |
//mode = PARPORT_MODE_PCSPP; |
| 84 |
mode = IEEE1284_MODE_COMPAT; |
| 85 |
if (ioctl(*dev_fd, PPSETMODE, &mode) == -1) |
| 86 |
{ |
| 87 |
ticables_warning("%s", _("ioctl failed on parallel device: can't change transfer mode.")); |
| 88 |
return ERR_PPT_IOCTL; |
| 89 |
} |
| 90 |
|
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
int par_io_close(int dev_fd) |
| 95 |
{ |
| 96 |
// release access |
| 97 |
if (ioctl(dev_fd, PPRELEASE) == -1) |
| 98 |
{ |
| 99 |
ticables_warning("%s", _("ioctl failed on parallel device: can't release parport.")); |
| 100 |
return ERR_PPT_IOCTL; |
| 101 |
} |
| 102 |
|
| 103 |
// and close |
| 104 |
return close(dev_fd); |
| 105 |
} |
| 106 |
|
| 107 |
int par_io_rd(int dev_fd) |
| 108 |
{ |
| 109 |
uint8_t data; |
| 110 |
|
| 111 |
if (ioctl(dev_fd, PPRSTATUS, &data) == -1) |
| 112 |
{ |
| 113 |
ticables_warning("%s", _("ioctl failed on parallel device: can't read status lines.")); |
| 114 |
return ERR_PPT_IOCTL; |
| 115 |
} |
| 116 |
|
| 117 |
return data; |
| 118 |
} |
| 119 |
|
| 120 |
int par_io_wr(int dev_fd, uint8_t data) |
| 121 |
{ |
| 122 |
if(ioctl (dev_fd, PPWDATA, &data) == -1) |
| 123 |
{ |
| 124 |
ticables_warning("%s", _("ioctl failed on parallel device: can't write on data lines.")); |
| 125 |
return ERR_WRITE_ERROR; |
| 126 |
} |
| 127 |
|
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
#endif |
| 132 |
|
| 133 |
// --- |
| 134 |
|
| 135 |
#ifdef HAVE_TERMIOS_H |
| 136 |
|
| 137 |
int ser_io_open(const char *dev_name, int *dev_fd) |
| 138 |
{ |
| 139 |
#ifndef O_SYNC |
| 140 |
# define O_SYNC O_FSYNC |
| 141 |
#endif |
| 142 |
*dev_fd = open(dev_name, O_RDWR | O_SYNC); |
| 143 |
if (*dev_fd == -1) |
| 144 |
{ |
| 145 |
ticables_warning("unable to open serial device '%s'", dev_name); |
| 146 |
return ERR_TTY_OPEN; |
| 147 |
} |
| 148 |
|
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
int ser_io_close(int dev_fd) |
| 153 |
{ |
| 154 |
return close(dev_fd); |
| 155 |
} |
| 156 |
|
| 157 |
int ser_io_rd(int dev_fd) |
| 158 |
{ |
| 159 |
unsigned int flags = 0; |
| 160 |
|
| 161 |
if (ioctl(dev_fd, TIOCMGET, &flags) == -1) |
| 162 |
{ |
| 163 |
ticables_warning("%s", _("ioctl failed on serial device.")); |
| 164 |
return ERR_TTY_IOCTL; |
| 165 |
} |
| 166 |
|
| 167 |
return (flags & TIOCM_CTS ? 0x10 : 0) | (flags & TIOCM_DSR ? 0x20 : 0); |
| 168 |
} |
| 169 |
|
| 170 |
int ser_io_wr(int dev_fd, uint8_t data) |
| 171 |
{ |
| 172 |
unsigned int flags = 0; |
| 173 |
|
| 174 |
flags |= (data & 2) ? TIOCM_RTS : 0; |
| 175 |
flags |= (data & 1) ? TIOCM_DTR : 0; |
| 176 |
|
| 177 |
if (ioctl(dev_fd, TIOCMSET, &flags) == -1) |
| 178 |
{ |
| 179 |
ticables_warning("%s", _("ioctl failed on serial device.")); |
| 180 |
return ERR_TTY_IOCTL; |
| 181 |
} |
| 182 |
|
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
#endif |