2004-01-21 15:40:40 +01:00
|
|
|
#define MAX_PARALLEL_DEVICES 1
|
|
|
|
|
|
|
|
#define FILE_DEVICE_PARALLEL 0x22
|
|
|
|
|
|
|
|
#define IOCTL_PAR_QUERY_RAW_DEVICE_ID 0x0c
|
|
|
|
|
|
|
|
#include "rdesktop.h"
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2004-03-05 07:48:08 +01:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2004-03-05 10:36:49 +01:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include <linux/lp.h>
|
|
|
|
#endif
|
|
|
|
|
2004-03-05 07:48:08 +01:00
|
|
|
extern int errno;
|
2004-01-21 15:40:40 +01:00
|
|
|
|
|
|
|
extern RDPDR_DEVICE g_rdpdr_device[];
|
|
|
|
|
2004-04-17 09:14:41 +02:00
|
|
|
static PARALLEL_DEVICE *
|
2004-01-23 09:35:52 +01:00
|
|
|
get_parallel_data(HANDLE handle)
|
2004-01-21 15:40:40 +01:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
for (index = 0; index < RDPDR_MAX_DEVICES; index++)
|
|
|
|
{
|
|
|
|
if (handle == g_rdpdr_device[index].handle)
|
|
|
|
return (PARALLEL_DEVICE *) g_rdpdr_device[index].pdevice_data;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Enumeration of devices from rdesktop.c */
|
|
|
|
/* returns numer of units found and initialized. */
|
|
|
|
/* optarg looks like ':LPT1=/dev/lp0' */
|
|
|
|
/* when it arrives to this function. */
|
|
|
|
int
|
2004-02-15 22:19:28 +01:00
|
|
|
parallel_enum_devices(uint32 * id, char *optarg)
|
2004-01-21 15:40:40 +01:00
|
|
|
{
|
|
|
|
PARALLEL_DEVICE *ppar_info;
|
|
|
|
|
|
|
|
char *pos = optarg;
|
|
|
|
char *pos2;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
// skip the first colon
|
|
|
|
optarg++;
|
|
|
|
while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES)
|
|
|
|
{
|
|
|
|
ppar_info = (PARALLEL_DEVICE *) xmalloc(sizeof(PARALLEL_DEVICE));
|
|
|
|
|
|
|
|
pos2 = next_arg(optarg, '=');
|
|
|
|
strcpy(g_rdpdr_device[*id].name, optarg);
|
|
|
|
|
2004-01-21 19:02:38 +01:00
|
|
|
toupper_str(g_rdpdr_device[*id].name);
|
2004-01-21 15:40:40 +01:00
|
|
|
|
|
|
|
g_rdpdr_device[*id].local_path = xmalloc(strlen(pos2) + 1);
|
|
|
|
strcpy(g_rdpdr_device[*id].local_path, pos2);
|
|
|
|
printf("PARALLEL %s to %s\n", optarg, pos2);
|
|
|
|
|
|
|
|
// set device type
|
|
|
|
g_rdpdr_device[*id].device_type = DEVICE_TYPE_PARALLEL;
|
|
|
|
g_rdpdr_device[*id].pdevice_data = (void *) ppar_info;
|
2004-03-05 07:48:08 +01:00
|
|
|
g_rdpdr_device[*id].handle = 0;
|
2004-01-21 15:40:40 +01:00
|
|
|
count++;
|
|
|
|
(*id)++;
|
|
|
|
|
|
|
|
optarg = pos;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS
|
2004-01-30 15:10:32 +01:00
|
|
|
parallel_create(uint32 device_id, uint32 access, uint32 share_mode, uint32 disposition,
|
|
|
|
uint32 flags, char *filename, HANDLE * handle)
|
2004-01-21 15:40:40 +01:00
|
|
|
{
|
|
|
|
int parallel_fd;
|
|
|
|
|
2004-01-29 13:24:15 +01:00
|
|
|
parallel_fd = open(g_rdpdr_device[device_id].local_path, O_RDWR);
|
2004-01-30 15:10:32 +01:00
|
|
|
if (parallel_fd == -1)
|
|
|
|
{
|
2004-01-29 13:24:15 +01:00
|
|
|
perror("open");
|
2004-01-21 15:40:40 +01:00
|
|
|
return STATUS_ACCESS_DENIED;
|
2004-01-29 13:24:15 +01:00
|
|
|
}
|
2004-01-21 15:40:40 +01:00
|
|
|
|
2004-03-05 07:48:08 +01:00
|
|
|
/* all read and writes should be non blocking */
|
|
|
|
if (fcntl(parallel_fd, F_SETFL, O_NONBLOCK) == -1)
|
|
|
|
perror("fcntl");
|
|
|
|
|
2004-03-05 10:36:49 +01:00
|
|
|
#if defined(LPABORT)
|
|
|
|
/* Retry on errors */
|
2004-03-05 07:48:08 +01:00
|
|
|
ioctl(parallel_fd, LPABORT, (int) 1);
|
2004-03-05 10:36:49 +01:00
|
|
|
#endif
|
2004-03-05 07:48:08 +01:00
|
|
|
|
2004-01-27 22:06:22 +01:00
|
|
|
g_rdpdr_device[device_id].handle = parallel_fd;
|
|
|
|
|
2004-01-21 15:40:40 +01:00
|
|
|
*handle = parallel_fd;
|
2004-01-30 15:10:32 +01:00
|
|
|
|
2004-01-21 15:40:40 +01:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS
|
|
|
|
parallel_close(HANDLE handle)
|
|
|
|
{
|
2004-03-05 07:48:08 +01:00
|
|
|
int i = get_device_index(handle);
|
|
|
|
if (i >= 0)
|
|
|
|
g_rdpdr_device[i].handle = 0;
|
2004-01-21 15:40:40 +01:00
|
|
|
close(handle);
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-04-17 09:14:41 +02:00
|
|
|
static NTSTATUS
|
2004-01-29 13:24:15 +01:00
|
|
|
parallel_read(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
|
|
|
|
{
|
|
|
|
*result = read(handle, data, length);
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-01-21 15:40:40 +01:00
|
|
|
static NTSTATUS
|
|
|
|
parallel_write(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result)
|
|
|
|
{
|
2004-03-05 07:48:08 +01:00
|
|
|
int rc = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
int n = write(handle, data, length);
|
|
|
|
if (n < 0)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
*result = 0;
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EAGAIN:
|
|
|
|
rc = STATUS_DEVICE_OFF_LINE;
|
|
|
|
case ENOSPC:
|
|
|
|
rc = STATUS_DEVICE_PAPER_EMPTY;
|
|
|
|
case EIO:
|
|
|
|
rc = STATUS_DEVICE_OFF_LINE;
|
|
|
|
default:
|
|
|
|
rc = STATUS_DEVICE_POWERED_OFF;
|
|
|
|
}
|
2004-03-05 10:36:49 +01:00
|
|
|
#if defined(LPGETSTATUS)
|
2004-03-05 07:48:08 +01:00
|
|
|
if (ioctl(handle, LPGETSTATUS, &status) == 0)
|
|
|
|
{
|
|
|
|
/* coming soon: take care for the printer status */
|
|
|
|
printf("parallel_write: status = %d, errno = %d\n", status, errno);
|
|
|
|
}
|
2004-03-05 10:36:49 +01:00
|
|
|
#endif
|
2004-03-05 07:48:08 +01:00
|
|
|
}
|
|
|
|
*result = n;
|
|
|
|
return rc;
|
2004-01-21 15:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS
|
|
|
|
parallel_device_control(HANDLE handle, uint32 request, STREAM in, STREAM out)
|
|
|
|
{
|
|
|
|
if ((request >> 16) != FILE_DEVICE_PARALLEL)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
/* extract operation */
|
|
|
|
request >>= 2;
|
|
|
|
request &= 0xfff;
|
|
|
|
|
|
|
|
printf("PARALLEL IOCTL %d: ", request);
|
|
|
|
|
|
|
|
switch (request)
|
|
|
|
{
|
|
|
|
case IOCTL_PAR_QUERY_RAW_DEVICE_ID:
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
unimpl("UNKNOWN IOCTL %d\n", request);
|
|
|
|
}
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEVICE_FNS parallel_fns = {
|
|
|
|
parallel_create,
|
|
|
|
parallel_close,
|
2004-01-29 13:24:15 +01:00
|
|
|
parallel_read,
|
2004-01-21 15:40:40 +01:00
|
|
|
parallel_write,
|
|
|
|
parallel_device_control
|
|
|
|
};
|