| 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 |
* |
| 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 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 Foundation, |
| 19 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
/* "Home-made parallel" link cable unit */ |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <unistd.h> |
| 28 |
|
| 29 |
#include "../ticables.h" |
| 30 |
#include "../logging.h" |
| 31 |
#include "../error.h" |
| 32 |
#include "../gettext.h" |
| 33 |
#include "../internal.h" |
| 34 |
#include "detect.h" |
| 35 |
#include "ioports.h" |
| 36 |
#include "../bsd/detect.h" |
| 37 |
#include "../macos/detect.h" |
| 38 |
|
| 39 |
#define dev_fd (GPOINTER_TO_INT(h->priv)) |
| 40 |
|
| 41 |
static int par_prepare(CableHandle *h) |
| 42 |
{ |
| 43 |
const char * device; |
| 44 |
int ret; |
| 45 |
|
| 46 |
switch(h->port) |
| 47 |
{ |
| 48 |
case PORT_1: h->address = 0x378; device = "/dev/parport0"; break; |
| 49 |
case PORT_2: h->address = 0x278; device = "/dev/parport1"; break; |
| 50 |
case PORT_3: h->address = 0x3bc; device = "/dev/parport2"; break; |
| 51 |
default: return ERR_ILLEGAL_ARG; |
| 52 |
} |
| 53 |
|
| 54 |
if (h->device == NULL) |
| 55 |
{ |
| 56 |
h->device = strdup(device); |
| 57 |
} |
| 58 |
|
| 59 |
// detect stuffs |
| 60 |
#if defined(__MACOSX__) |
| 61 |
ret = macosx_check_parport(h->device); |
| 62 |
#elif defined(__BSD__) |
| 63 |
ret = bsd_check_parport(h->device); |
| 64 |
#else |
| 65 |
ret = linux_check_parport(h->device); |
| 66 |
#endif |
| 67 |
if (ret) |
| 68 |
{ |
| 69 |
free(h->device); h->device = NULL; |
| 70 |
return ret; |
| 71 |
} |
| 72 |
|
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
static int par_open(CableHandle *h) |
| 77 |
{ |
| 78 |
int ret; |
| 79 |
int fd; |
| 80 |
|
| 81 |
ret = par_io_open(h->device, &fd); |
| 82 |
if (!ret) |
| 83 |
{ |
| 84 |
h->priv = GINT_TO_POINTER(fd); |
| 85 |
} |
| 86 |
else |
| 87 |
{ |
| 88 |
close(fd); |
| 89 |
} |
| 90 |
|
| 91 |
return ret; |
| 92 |
} |
| 93 |
|
| 94 |
static int par_close(CableHandle *h) |
| 95 |
{ |
| 96 |
return par_io_close(dev_fd); |
| 97 |
} |
| 98 |
|
| 99 |
static int par_reset(CableHandle *h) |
| 100 |
{ |
| 101 |
tiTIME clk; |
| 102 |
|
| 103 |
// wait for releasing of lines |
| 104 |
TO_START(clk); |
| 105 |
do |
| 106 |
{ |
| 107 |
par_io_wr(dev_fd, 3); |
| 108 |
if (TO_ELAPSED(clk, h->timeout)) |
| 109 |
{ |
| 110 |
return 0; |
| 111 |
} |
| 112 |
} |
| 113 |
while ((par_io_rd(dev_fd) & 0x30) != 0x30); |
| 114 |
|
| 115 |
return 0; |
| 116 |
} |
| 117 |
|
| 118 |
static int par_put(CableHandle *h, uint8_t *data, uint32_t len) |
| 119 |
{ |
| 120 |
int bit; |
| 121 |
unsigned int i; |
| 122 |
uint32_t j; |
| 123 |
tiTIME clk; |
| 124 |
|
| 125 |
for(j = 0; j < len; j++) |
| 126 |
{ |
| 127 |
uint8_t byte = data[j]; |
| 128 |
|
| 129 |
for (bit = 0; bit < 8; bit++) |
| 130 |
{ |
| 131 |
if (byte & 1) |
| 132 |
{ |
| 133 |
par_io_wr(dev_fd, 2); |
| 134 |
|
| 135 |
TO_START(clk); |
| 136 |
while ((par_io_rd(dev_fd) & 0x10)) |
| 137 |
{ |
| 138 |
if (TO_ELAPSED(clk, h->timeout)) |
| 139 |
{ |
| 140 |
return ERR_WRITE_TIMEOUT; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
par_io_wr(dev_fd, 3); |
| 145 |
TO_START(clk); |
| 146 |
while ((par_io_rd(dev_fd) & 0x10) == 0x00) |
| 147 |
{ |
| 148 |
if (TO_ELAPSED(clk, h->timeout)) |
| 149 |
{ |
| 150 |
return ERR_WRITE_TIMEOUT; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
else |
| 155 |
{ |
| 156 |
par_io_wr(dev_fd, 1); |
| 157 |
TO_START(clk); |
| 158 |
while (par_io_rd(dev_fd) & 0x20) |
| 159 |
{ |
| 160 |
if (TO_ELAPSED(clk, h->timeout)) |
| 161 |
{ |
| 162 |
return ERR_WRITE_TIMEOUT; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
par_io_wr(dev_fd, 3); |
| 167 |
TO_START(clk); |
| 168 |
while ((par_io_rd(dev_fd) & 0x20) == 0x00) |
| 169 |
{ |
| 170 |
if (TO_ELAPSED(clk, h->timeout)) |
| 171 |
{ |
| 172 |
return ERR_WRITE_TIMEOUT; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
byte >>= 1; |
| 178 |
for (i = 0; i < h->delay; i++) |
| 179 |
{ |
| 180 |
par_io_rd(dev_fd); |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return 0; |
| 186 |
} |
| 187 |
|
| 188 |
static int par_get(CableHandle *h, uint8_t *data, uint32_t len) |
| 189 |
{ |
| 190 |
int bit; |
| 191 |
unsigned int i; |
| 192 |
uint32_t j; |
| 193 |
tiTIME clk; |
| 194 |
|
| 195 |
for(j = 0; j < len; j++) |
| 196 |
{ |
| 197 |
uint8_t v, byte = 0; |
| 198 |
|
| 199 |
for (bit = 0; bit < 8; bit++) |
| 200 |
{ |
| 201 |
TO_START(clk); |
| 202 |
while ((v = par_io_rd(dev_fd) & 0x30) == 0x30) |
| 203 |
{ |
| 204 |
if (TO_ELAPSED(clk, h->timeout)) |
| 205 |
{ |
| 206 |
return ERR_READ_TIMEOUT; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if (v == 0x10) |
| 211 |
{ |
| 212 |
byte = (byte >> 1) | 0x80; |
| 213 |
par_io_wr(dev_fd, 1); |
| 214 |
|
| 215 |
TO_START(clk); |
| 216 |
while ((par_io_rd(dev_fd) & 0x20) == 0x00) |
| 217 |
{ |
| 218 |
if (TO_ELAPSED(clk, h->timeout)) |
| 219 |
{ |
| 220 |
return ERR_READ_TIMEOUT; |
| 221 |
} |
| 222 |
} |
| 223 |
par_io_wr(dev_fd, 3); |
| 224 |
} |
| 225 |
else |
| 226 |
{ |
| 227 |
byte = (byte >> 1) & 0x7F; |
| 228 |
par_io_wr(dev_fd, 2); |
| 229 |
|
| 230 |
TO_START(clk); |
| 231 |
while ((par_io_rd(dev_fd) & 0x10) == 0x00) |
| 232 |
{ |
| 233 |
if (TO_ELAPSED(clk, h->timeout)) |
| 234 |
{ |
| 235 |
return ERR_READ_TIMEOUT; |
| 236 |
} |
| 237 |
} |
| 238 |
par_io_wr(dev_fd, 3); |
| 239 |
} |
| 240 |
|
| 241 |
for (i = 0; i < h->delay; i++) |
| 242 |
{ |
| 243 |
par_io_rd(dev_fd); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
data[j] = byte; |
| 248 |
} |
| 249 |
|
| 250 |
return 0; |
| 251 |
} |
| 252 |
|
| 253 |
static int par_probe(CableHandle *h) |
| 254 |
{ |
| 255 |
int timeout = 1; |
| 256 |
tiTIME clk; |
| 257 |
|
| 258 |
// 1 |
| 259 |
par_io_wr(dev_fd, 2); |
| 260 |
TO_START(clk); |
| 261 |
while ((par_io_rd(dev_fd) & 0x10)) |
| 262 |
{ |
| 263 |
if (TO_ELAPSED(clk, timeout)) |
| 264 |
{ |
| 265 |
return ERR_WRITE_TIMEOUT; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
par_io_wr(dev_fd, 3); |
| 270 |
TO_START(clk); |
| 271 |
while ((par_io_rd(dev_fd) & 0x10) == 0x00) |
| 272 |
{ |
| 273 |
if (TO_ELAPSED(clk, timeout)) |
| 274 |
{ |
| 275 |
return ERR_WRITE_TIMEOUT; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
// 0 |
| 280 |
par_io_wr(dev_fd, 1); |
| 281 |
TO_START(clk); |
| 282 |
while (par_io_rd(dev_fd) & 0x20) |
| 283 |
{ |
| 284 |
if (TO_ELAPSED(clk, timeout)) |
| 285 |
{ |
| 286 |
return ERR_WRITE_TIMEOUT; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
par_io_wr(dev_fd, 3); |
| 291 |
TO_START(clk); |
| 292 |
while ((par_io_rd(dev_fd) & 0x20) == 0x00) |
| 293 |
{ |
| 294 |
if (TO_ELAPSED(clk, timeout)) |
| 295 |
{ |
| 296 |
return ERR_WRITE_TIMEOUT; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
static int par_check(CableHandle *h, int *status) |
| 304 |
{ |
| 305 |
*status = STATUS_NONE; |
| 306 |
|
| 307 |
if (!((par_io_rd(dev_fd) & 0x30) == 0x30)) |
| 308 |
{ |
| 309 |
*status = (STATUS_RX | STATUS_TX); |
| 310 |
} |
| 311 |
|
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
#define swap_bits(a) (((a&2)>>1) | ((a&1)<<1)) // swap the 2 lowest bits |
| 316 |
|
| 317 |
static int par_set_red_wire(CableHandle *h, int b) |
| 318 |
{ |
| 319 |
int v = swap_bits(par_io_rd(dev_fd) >> 4); |
| 320 |
|
| 321 |
if (b) |
| 322 |
{ |
| 323 |
par_io_wr(dev_fd, v | 0x02); |
| 324 |
} |
| 325 |
else |
| 326 |
{ |
| 327 |
par_io_wr(dev_fd, v & ~0x02); |
| 328 |
} |
| 329 |
|
| 330 |
return 0; |
| 331 |
} |
| 332 |
|
| 333 |
static int par_set_white_wire(CableHandle *h, int b) |
| 334 |
{ |
| 335 |
int v = swap_bits(par_io_rd(dev_fd) >> 4); |
| 336 |
|
| 337 |
if (b) |
| 338 |
{ |
| 339 |
par_io_wr(dev_fd, v | 0x01); |
| 340 |
} |
| 341 |
else |
| 342 |
{ |
| 343 |
par_io_wr(dev_fd, v & ~0x01); |
| 344 |
} |
| 345 |
|
| 346 |
return 0; |
| 347 |
} |
| 348 |
|
| 349 |
static int par_get_red_wire(CableHandle *h) |
| 350 |
{ |
| 351 |
return ((0x10 & par_io_rd(dev_fd)) ? 1 : 0); |
| 352 |
} |
| 353 |
|
| 354 |
static int par_get_white_wire(CableHandle *h) |
| 355 |
{ |
| 356 |
return ((0x20 & par_io_rd(dev_fd)) ? 1 : 0); |
| 357 |
} |
| 358 |
|
| 359 |
static int par_set_raw(CableHandle *h, int state) |
| 360 |
{ |
| 361 |
par_io_wr(dev_fd, swap_bits(state)); |
| 362 |
return 0; |
| 363 |
} |
| 364 |
|
| 365 |
static int par_get_raw(CableHandle *h, int *state) |
| 366 |
{ |
| 367 |
*state = (par_io_rd(dev_fd) >> 4) & 3; |
| 368 |
return 0; |
| 369 |
} |
| 370 |
|
| 371 |
static int par_set_device(CableHandle *h, const char * device) |
| 372 |
{ |
| 373 |
if (device != NULL) |
| 374 |
{ |
| 375 |
char * device2 = strdup(device); |
| 376 |
if (device2 != NULL) |
| 377 |
{ |
| 378 |
free(h->device); |
| 379 |
h->device = device2; |
| 380 |
} |
| 381 |
else |
| 382 |
{ |
| 383 |
ticables_warning(_("unable to set device %s.\n"), device); |
| 384 |
} |
| 385 |
return 0; |
| 386 |
} |
| 387 |
return ERR_ILLEGAL_ARG; |
| 388 |
} |
| 389 |
|
| 390 |
extern const CableFncts cable_par = |
| 391 |
{ |
| 392 |
CABLE_PAR, |
| 393 |
"PAR", |
| 394 |
N_("Parallel"), |
| 395 |
N_("Home-made parallel cable"), |
| 396 |
!0, |
| 397 |
&par_prepare, |
| 398 |
&par_open, &par_close, &par_reset, &par_probe, NULL, |
| 399 |
&par_put, &par_get, &par_check, |
| 400 |
&par_set_red_wire, &par_set_white_wire, |
| 401 |
&par_get_red_wire, &par_get_white_wire, |
| 402 |
&par_set_raw, &par_get_raw, |
| 403 |
&par_set_device, |
| 404 |
NULL |
| 405 |
}; |