| 1 |
/* |
| 2 |
* cwdaemon - morse sounding daemon for the parallel or serial port |
| 3 |
* Copyright (C) 2002 - 2005 Joop Stakenborg <[email protected]> |
| 4 |
* and many authors, see the AUTHORS file. |
| 5 |
* Copyright (C) 2012 - 2015 Kamil Ignacak <[email protected]> |
| 6 |
* |
| 7 |
* This program 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 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program 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 Library General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 20 |
* 02110-1301, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
/* Comments from Wolf-Ruediger Juergens, DL2WRJ |
| 24 |
* very helpful: http://people.redhat.com/twaugh/parport/html/x916.html |
| 25 |
* and: http://www.xml.com/ldd/chapter/book/ch08.html |
| 26 |
* (Rubini et al. Linux Device Driver Book) |
| 27 |
*/ |
| 28 |
|
| 29 |
#define _BSD_SOURCE /* S_IFMT and related. */ |
| 30 |
|
| 31 |
#include "config.h" |
| 32 |
|
| 33 |
# if HAVE_STDIO_H |
| 34 |
# include <stdio.h> |
| 35 |
#endif |
| 36 |
#if STDC_HEADERS |
| 37 |
# include <stdlib.h> |
| 38 |
# include <stddef.h> |
| 39 |
#else |
| 40 |
# if HAVE_STDLIB_H |
| 41 |
# include <stdlib.h> |
| 42 |
# endif |
| 43 |
#endif |
| 44 |
#if HAVE_UNISTD_H |
| 45 |
# include <unistd.h> |
| 46 |
#endif |
| 47 |
#if HAVE_SYS_IOCTL_H |
| 48 |
# include <sys/ioctl.h> |
| 49 |
#endif |
| 50 |
#if HAVE_FCNTL_H |
| 51 |
# include <fcntl.h> |
| 52 |
#endif |
| 53 |
#ifdef HAVE_LINUX_PPDEV_H |
| 54 |
# include <linux/ppdev.h> |
| 55 |
# include <linux/parport.h> |
| 56 |
#endif |
| 57 |
#ifdef HAVE_DEV_PPBUS_PPI_H |
| 58 |
# include <dev/ppbus/ppi.h> |
| 59 |
# include <dev/ppbus/ppbconf.h> |
| 60 |
#endif |
| 61 |
#ifdef HAVE_SYS_STAT_H |
| 62 |
# include <sys/stat.h> |
| 63 |
#endif |
| 64 |
#if (defined(__unix__) || defined(unix)) && !defined(USG) |
| 65 |
# include <sys/param.h> |
| 66 |
#endif |
| 67 |
|
| 68 |
#include "cwdaemon.h" |
| 69 |
|
| 70 |
/** |
| 71 |
\file lp.c - parport functions |
| 72 |
*/ |
| 73 |
|
| 74 |
|
| 75 |
#if defined(HAVE_LINUX_PPDEV_H) /* Linux (ppdev) */ |
| 76 |
/** |
| 77 |
\brief Check to see whether \p fname is a parallel port type character device. |
| 78 |
|
| 79 |
Unfortunately, this is platform specific. |
| 80 |
|
| 81 |
\return -1 if the device is not suitable for use as parallel port based keyer |
| 82 |
\return a file descriptor if the device is suitable for use as a parallel port based keyer |
| 83 |
*/ |
| 84 |
int dev_get_parport(const char *fname) |
| 85 |
{ |
| 86 |
char nm[MAXPATHLEN]; |
| 87 |
struct stat st; |
| 88 |
int fd, m; |
| 89 |
|
| 90 |
m = snprintf(nm, sizeof(nm), "/dev/%s", fname); |
| 91 |
if (m >= sizeof(nm)) |
| 92 |
return (-1); |
| 93 |
if ((fd = open(nm, O_RDWR | O_NONBLOCK)) == -1) |
| 94 |
return (-1); |
| 95 |
if (fstat(fd, &st) == -1) |
| 96 |
goto out; |
| 97 |
if ((st.st_mode & S_IFMT) != S_IFCHR) |
| 98 |
goto out; |
| 99 |
if (ioctl(fd, PPGETMODE, &m) == -1) |
| 100 |
goto out; |
| 101 |
return (fd); |
| 102 |
out: |
| 103 |
close(fd); |
| 104 |
return (-1); |
| 105 |
} |
| 106 |
|
| 107 |
#elif defined(HAVE_DEV_PPBUS_PPI_H) /* FreeBSD (ppbus/ppi) */ |
| 108 |
|
| 109 |
int dev_get_parport(const char *fname) |
| 110 |
{ |
| 111 |
char nm[MAXPATHLEN]; |
| 112 |
struct stat st; |
| 113 |
unsigned char c; |
| 114 |
int fd, m; |
| 115 |
|
| 116 |
m = snprintf(nm, sizeof(nm), "/dev/%s", fname); |
| 117 |
if (m >= sizeof(nm)) |
| 118 |
return (-1); |
| 119 |
if ((fd = open(nm, O_RDWR | O_NONBLOCK)) == -1) |
| 120 |
return (-1); |
| 121 |
if (fstat(fd, &st) == -1) |
| 122 |
goto out; |
| 123 |
if ((st.st_mode & S_IFMT) != S_IFCHR) |
| 124 |
goto out; |
| 125 |
if (ioctl(fd, PPISSTATUS, &c) == -1) |
| 126 |
goto out; |
| 127 |
return (fd); |
| 128 |
out: |
| 129 |
close(fd); |
| 130 |
return (-1); |
| 131 |
} |
| 132 |
|
| 133 |
#else /* Fallback (nothing) */ |
| 134 |
|
| 135 |
int dev_get_parport(const char *fname) |
| 136 |
{ |
| 137 |
return (-1); |
| 138 |
} |
| 139 |
|
| 140 |
#endif |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
/* Linux wrapper around PPFCONTROL */ |
| 147 |
#ifdef HAVE_LINUX_PPDEV_H |
| 148 |
static void |
| 149 |
parport_control (int fd, unsigned char controlbits, int values) |
| 150 |
{ |
| 151 |
struct ppdev_frob_struct frob; |
| 152 |
frob.mask = controlbits; |
| 153 |
frob.val = values; |
| 154 |
|
| 155 |
if (ioctl (fd, PPFCONTROL, &frob) == -1) |
| 156 |
{ |
| 157 |
cwdaemon_errmsg("Parallel port PPFCONTROL"); |
| 158 |
exit (1); |
| 159 |
} |
| 160 |
} |
| 161 |
#endif |
| 162 |
|
| 163 |
/* FreeBSD wrapper around PPISCTRL */ |
| 164 |
#ifdef HAVE_DEV_PPBUS_PPI_H |
| 165 |
static void |
| 166 |
parport_control (int fd, unsigned char controlbits, int values) |
| 167 |
{ |
| 168 |
unsigned char val; |
| 169 |
|
| 170 |
if (ioctl (fd, PPIGCTRL, &val) == -1) |
| 171 |
{ |
| 172 |
cwdaemon_errmsg("Parallel port PPIGCTRL"); |
| 173 |
exit (1); |
| 174 |
} |
| 175 |
|
| 176 |
val &= ~controlbits; |
| 177 |
val |= values; |
| 178 |
|
| 179 |
if (ioctl (fd, PPISCTRL, &val) == -1) |
| 180 |
{ |
| 181 |
cwdaemon_errmsg("Parallel port PPISCTRL"); |
| 182 |
exit (1); |
| 183 |
} |
| 184 |
} |
| 185 |
#endif |
| 186 |
|
| 187 |
/* Linux wrapper around PPWDATA */ |
| 188 |
#ifdef HAVE_LINUX_PPDEV_H |
| 189 |
static void |
| 190 |
parport_write_data (int fd, unsigned char data) |
| 191 |
{ |
| 192 |
if (ioctl (fd, PPWDATA, &data) == -1) |
| 193 |
{ |
| 194 |
cwdaemon_errmsg("Parallel port PPWDATA"); |
| 195 |
exit (1); |
| 196 |
} |
| 197 |
} |
| 198 |
#endif |
| 199 |
|
| 200 |
/* FreeBSD wrapper around PPISDATA */ |
| 201 |
#ifdef HAVE_DEV_PPBUS_PPI_H |
| 202 |
static void |
| 203 |
parport_write_data (int fd, unsigned char data) |
| 204 |
{ |
| 205 |
if (ioctl (fd, PPISDATA, &data) == -1) |
| 206 |
{ |
| 207 |
cwdaemon_errmsg("Parallel port PPISDATA"); |
| 208 |
exit (1); |
| 209 |
} |
| 210 |
} |
| 211 |
#endif |
| 212 |
|
| 213 |
/* Linux wrapper around PPRSTATUS, reading the status port */ |
| 214 |
#ifdef HAVE_LINUX_PPDEV_H |
| 215 |
static unsigned char |
| 216 |
parport_read_data (int fd) |
| 217 |
{ |
| 218 |
unsigned char data = 0; |
| 219 |
if (ioctl (fd, PPRSTATUS, &data) == -1) |
| 220 |
{ |
| 221 |
cwdaemon_errmsg("Parallel port PPRSTATUS"); |
| 222 |
exit (1); |
| 223 |
} |
| 224 |
return data; |
| 225 |
} |
| 226 |
#endif |
| 227 |
|
| 228 |
/* FreeBSD wrapper around PPISSTATUS, reading the status port */ |
| 229 |
#ifdef HAVE_DEV_PPBUS_PPI_H |
| 230 |
static unsigned char |
| 231 |
parport_read_data (int fd) |
| 232 |
{ |
| 233 |
unsigned char data = 0; |
| 234 |
if (ioctl (fd, PPISSTATUS, &data) == -1) |
| 235 |
{ |
| 236 |
cwdaemon_errmsg("Parallel port PPISSTATUS"); |
| 237 |
exit (1); |
| 238 |
} |
| 239 |
return data; |
| 240 |
} |
| 241 |
#endif |
| 242 |
|
| 243 |
/* open port and setup ppdev */ |
| 244 |
int |
| 245 |
lp_init (cwdevice * dev, int fd) |
| 246 |
{ |
| 247 |
#ifdef HAVE_LINUX_PPDEV_H |
| 248 |
int mode; |
| 249 |
#endif |
| 250 |
|
| 251 |
#ifdef HAVE_LINUX_PPDEV_H |
| 252 |
mode = PARPORT_MODE_PCSPP; |
| 253 |
|
| 254 |
if (ioctl (fd, PPSETMODE, &mode) == -1) |
| 255 |
{ |
| 256 |
cwdaemon_errmsg("Setting parallel port mode"); |
| 257 |
close (fd); |
| 258 |
exit (1); |
| 259 |
} |
| 260 |
|
| 261 |
if (ioctl (fd, PPEXCL, NULL) == -1) |
| 262 |
{ |
| 263 |
cwdaemon_errmsg("Parallel port %s is already in use", dev->desc); |
| 264 |
close (fd); |
| 265 |
exit (1); |
| 266 |
} |
| 267 |
if (ioctl (fd, PPCLAIM, NULL) == -1) |
| 268 |
{ |
| 269 |
cwdaemon_errmsg("Claiming parallel port %s", dev->desc); |
| 270 |
cwdaemon_debug(CWDAEMON_VERBOSITY_W, __func__, __LINE__, "HINT: did you unload the lp kernel module?"); |
| 271 |
cwdaemon_debug(CWDAEMON_VERBOSITY_W, __func__, __LINE__, "HINT: perhaps there is another cwdaemon running?"); |
| 272 |
close (fd); |
| 273 |
exit (1); |
| 274 |
} |
| 275 |
|
| 276 |
/* Enable CW & PTT - /STROBE bit (pin 1) */ |
| 277 |
parport_control (fd, PARPORT_CONTROL_STROBE, PARPORT_CONTROL_STROBE); |
| 278 |
#endif |
| 279 |
#ifdef HAVE_DEV_PPBUS_PPI_H |
| 280 |
parport_control (fd, STROBE, STROBE); |
| 281 |
#endif |
| 282 |
dev->fd = fd; |
| 283 |
dev->reset (dev); |
| 284 |
return 0; |
| 285 |
} |
| 286 |
|
| 287 |
/* release ppdev and close port */ |
| 288 |
int |
| 289 |
lp_free (cwdevice * dev) |
| 290 |
{ |
| 291 |
#ifdef HAVE_LINUX_PPDEV_H |
| 292 |
dev->reset (dev); |
| 293 |
|
| 294 |
/* Disable CW & PTT - /STROBE bit (pin 1) */ |
| 295 |
parport_control (dev->fd, PARPORT_CONTROL_STROBE, 0); |
| 296 |
|
| 297 |
ioctl (dev->fd, PPRELEASE); |
| 298 |
#endif |
| 299 |
#ifdef HAVE_DEV_PPBUS_PPI_H |
| 300 |
/* Disable CW & PTT - /STROBE bit (pin 1) */ |
| 301 |
parport_control (dev->fd, STROBE, 0); |
| 302 |
#endif |
| 303 |
close (dev->fd); |
| 304 |
return 0; |
| 305 |
} |
| 306 |
|
| 307 |
/* set to a known state */ |
| 308 |
int |
| 309 |
lp_reset (cwdevice * dev) |
| 310 |
{ |
| 311 |
#if defined (HAVE_LINUX_PPDEV_H) || defined (HAVE_DEV_PPBUS_PPI_H) |
| 312 |
lp_cw (dev, 0); |
| 313 |
lp_ptt (dev, 0); |
| 314 |
lp_ssbway (dev, 0); |
| 315 |
lp_switchband (dev, 0); |
| 316 |
#endif |
| 317 |
return 0; |
| 318 |
} |
| 319 |
|
| 320 |
/* CW keying - /SELECT bit (pin 17) */ |
| 321 |
int |
| 322 |
lp_cw (cwdevice * dev, int onoff) |
| 323 |
{ |
| 324 |
#ifdef HAVE_LINUX_PPDEV_H |
| 325 |
if (onoff == 1) |
| 326 |
parport_control (dev->fd, PARPORT_CONTROL_SELECT, 0); |
| 327 |
else |
| 328 |
parport_control (dev->fd, PARPORT_CONTROL_SELECT, |
| 329 |
PARPORT_CONTROL_SELECT); |
| 330 |
#endif |
| 331 |
#ifdef HAVE_DEV_PPBUS_PPI_H |
| 332 |
if (onoff == 1) |
| 333 |
parport_control (dev->fd, SELECTIN, 0); |
| 334 |
else |
| 335 |
parport_control (dev->fd, SELECTIN, SELECTIN); |
| 336 |
#endif |
| 337 |
return 0; |
| 338 |
} |
| 339 |
|
| 340 |
/* SSB PTT keying - /INIT bit (pin 16) (inverted) */ |
| 341 |
int |
| 342 |
lp_ptt (cwdevice * dev, int onoff) |
| 343 |
{ |
| 344 |
#ifdef HAVE_LINUX_PPDEV_H |
| 345 |
if (onoff == 1) |
| 346 |
parport_control (dev->fd, PARPORT_CONTROL_INIT, |
| 347 |
PARPORT_CONTROL_INIT); |
| 348 |
else |
| 349 |
parport_control (dev->fd, PARPORT_CONTROL_INIT, 0); |
| 350 |
#endif |
| 351 |
#ifdef HAVE_DEV_PPBUS_PPI_H |
| 352 |
if (onoff == 1) |
| 353 |
parport_control (dev->fd, nINIT, |
| 354 |
nINIT); |
| 355 |
else |
| 356 |
parport_control (dev->fd, nINIT, 0); |
| 357 |
#endif |
| 358 |
return 0; |
| 359 |
} |
| 360 |
|
| 361 |
/* Foot switch reading / pin 15/bit 3 */ |
| 362 |
int |
| 363 |
lp_footswitch (cwdevice * dev) |
| 364 |
{ |
| 365 |
unsigned char footswitch = 0xff; |
| 366 |
/* we check for bit 3 low so FF is better then 0 */ |
| 367 |
#if defined (HAVE_LINUX_PPDEV_H) || defined (HAVE_DEV_PPBUS_PPI_H) |
| 368 |
footswitch = parport_read_data (dev->fd); |
| 369 |
/* returns decimal 8 if pin 15 is high */ |
| 370 |
#endif |
| 371 |
return (int)((footswitch & 0x08) >> 3); |
| 372 |
/* bit 0=1 footswitch up, bit 0=0 footswitch down*/ |
| 373 |
} |
| 374 |
|
| 375 |
/* SSB way from mic/soundcard - AUTOLF bit (pin 14) */ |
| 376 |
int |
| 377 |
lp_ssbway (cwdevice * dev, int onoff) |
| 378 |
{ |
| 379 |
#ifdef HAVE_LINUX_PPDEV_H |
| 380 |
if (onoff == 1) /* soundcard */ |
| 381 |
parport_control (dev->fd, PARPORT_CONTROL_AUTOFD, |
| 382 |
PARPORT_CONTROL_AUTOFD); |
| 383 |
else /* microphone */ |
| 384 |
parport_control (dev->fd, PARPORT_CONTROL_AUTOFD, 0); |
| 385 |
#endif |
| 386 |
#ifdef HAVE_DEV_PPBUS_PPI_H |
| 387 |
if (onoff == 1) /* soundcard */ |
| 388 |
parport_control (dev->fd, AUTOFEED, AUTOFEED); |
| 389 |
else /* microphone */ |
| 390 |
parport_control (dev->fd, AUTOFEED, 0); |
| 391 |
#endif |
| 392 |
return 0; |
| 393 |
} |
| 394 |
|
| 395 |
/* Bandswitch output on pins 9 (MSB), 8, 7, and 2 (LSB). */ |
| 396 |
int |
| 397 |
lp_switchband (cwdevice * dev, unsigned char bitpattern) |
| 398 |
{ |
| 399 |
#if defined (HAVE_LINUX_PPDEV_H) || defined (HAVE_DEV_PPBUS_PPI_H) |
| 400 |
parport_write_data (dev->fd, bitpattern); |
| 401 |
#endif |
| 402 |
return 0 ; |
| 403 |
} |