1 |
#define MAX_PARALLEL_DEVICES 1 |
2 |
|
3 |
#define FILE_DEVICE_PARALLEL 0x22 |
4 |
|
5 |
#define IOCTL_PAR_QUERY_RAW_DEVICE_ID 0x0c |
6 |
|
7 |
#include "rdesktop.h" |
8 |
#include <unistd.h> |
9 |
#include <fcntl.h> |
10 |
#include <sys/ioctl.h> |
11 |
#include <linux/lp.h> |
12 |
#include <errno.h> |
13 |
|
14 |
extern int errno; |
15 |
|
16 |
extern RDPDR_DEVICE g_rdpdr_device[]; |
17 |
|
18 |
PARALLEL_DEVICE * |
19 |
get_parallel_data(HANDLE handle) |
20 |
{ |
21 |
int index; |
22 |
|
23 |
for (index = 0; index < RDPDR_MAX_DEVICES; index++) |
24 |
{ |
25 |
if (handle == g_rdpdr_device[index].handle) |
26 |
return (PARALLEL_DEVICE *) g_rdpdr_device[index].pdevice_data; |
27 |
} |
28 |
return NULL; |
29 |
} |
30 |
|
31 |
|
32 |
/* Enumeration of devices from rdesktop.c */ |
33 |
/* returns numer of units found and initialized. */ |
34 |
/* optarg looks like ':LPT1=/dev/lp0' */ |
35 |
/* when it arrives to this function. */ |
36 |
int |
37 |
parallel_enum_devices(uint32 * id, char *optarg) |
38 |
{ |
39 |
PARALLEL_DEVICE *ppar_info; |
40 |
|
41 |
char *pos = optarg; |
42 |
char *pos2; |
43 |
int count = 0; |
44 |
|
45 |
// skip the first colon |
46 |
optarg++; |
47 |
while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES) |
48 |
{ |
49 |
ppar_info = (PARALLEL_DEVICE *) xmalloc(sizeof(PARALLEL_DEVICE)); |
50 |
|
51 |
pos2 = next_arg(optarg, '='); |
52 |
strcpy(g_rdpdr_device[*id].name, optarg); |
53 |
|
54 |
toupper_str(g_rdpdr_device[*id].name); |
55 |
|
56 |
g_rdpdr_device[*id].local_path = xmalloc(strlen(pos2) + 1); |
57 |
strcpy(g_rdpdr_device[*id].local_path, pos2); |
58 |
printf("PARALLEL %s to %s\n", optarg, pos2); |
59 |
|
60 |
// set device type |
61 |
g_rdpdr_device[*id].device_type = DEVICE_TYPE_PARALLEL; |
62 |
g_rdpdr_device[*id].pdevice_data = (void *) ppar_info; |
63 |
g_rdpdr_device[*id].handle = 0; |
64 |
count++; |
65 |
(*id)++; |
66 |
|
67 |
optarg = pos; |
68 |
} |
69 |
return count; |
70 |
} |
71 |
|
72 |
static NTSTATUS |
73 |
parallel_create(uint32 device_id, uint32 access, uint32 share_mode, uint32 disposition, |
74 |
uint32 flags, char *filename, HANDLE * handle) |
75 |
{ |
76 |
int parallel_fd; |
77 |
|
78 |
parallel_fd = open(g_rdpdr_device[device_id].local_path, O_RDWR); |
79 |
if (parallel_fd == -1) |
80 |
{ |
81 |
perror("open"); |
82 |
return STATUS_ACCESS_DENIED; |
83 |
} |
84 |
|
85 |
/* all read and writes should be non blocking */ |
86 |
if (fcntl(parallel_fd, F_SETFL, O_NONBLOCK) == -1) |
87 |
perror("fcntl"); |
88 |
|
89 |
ioctl(parallel_fd, LPABORT, (int) 1); |
90 |
|
91 |
g_rdpdr_device[device_id].handle = parallel_fd; |
92 |
|
93 |
*handle = parallel_fd; |
94 |
|
95 |
return STATUS_SUCCESS; |
96 |
} |
97 |
|
98 |
static NTSTATUS |
99 |
parallel_close(HANDLE handle) |
100 |
{ |
101 |
int i = get_device_index(handle); |
102 |
if (i >= 0) |
103 |
g_rdpdr_device[i].handle = 0; |
104 |
close(handle); |
105 |
return STATUS_SUCCESS; |
106 |
} |
107 |
|
108 |
NTSTATUS |
109 |
parallel_read(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) |
110 |
{ |
111 |
*result = read(handle, data, length); |
112 |
return STATUS_SUCCESS; |
113 |
} |
114 |
|
115 |
static NTSTATUS |
116 |
parallel_write(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) |
117 |
{ |
118 |
int rc = STATUS_SUCCESS; |
119 |
|
120 |
int n = write(handle, data, length); |
121 |
if (n < 0) |
122 |
{ |
123 |
int status; |
124 |
|
125 |
*result = 0; |
126 |
switch (errno) |
127 |
{ |
128 |
case EAGAIN: |
129 |
rc = STATUS_DEVICE_OFF_LINE; |
130 |
case ENOSPC: |
131 |
rc = STATUS_DEVICE_PAPER_EMPTY; |
132 |
case EIO: |
133 |
rc = STATUS_DEVICE_OFF_LINE; |
134 |
default: |
135 |
rc = STATUS_DEVICE_POWERED_OFF; |
136 |
} |
137 |
if (ioctl(handle, LPGETSTATUS, &status) == 0) |
138 |
{ |
139 |
/* coming soon: take care for the printer status */ |
140 |
printf("parallel_write: status = %d, errno = %d\n", status, errno); |
141 |
} |
142 |
} |
143 |
*result = n; |
144 |
return rc; |
145 |
} |
146 |
|
147 |
static NTSTATUS |
148 |
parallel_device_control(HANDLE handle, uint32 request, STREAM in, STREAM out) |
149 |
{ |
150 |
if ((request >> 16) != FILE_DEVICE_PARALLEL) |
151 |
return STATUS_INVALID_PARAMETER; |
152 |
|
153 |
/* extract operation */ |
154 |
request >>= 2; |
155 |
request &= 0xfff; |
156 |
|
157 |
printf("PARALLEL IOCTL %d: ", request); |
158 |
|
159 |
switch (request) |
160 |
{ |
161 |
case IOCTL_PAR_QUERY_RAW_DEVICE_ID: |
162 |
|
163 |
default: |
164 |
|
165 |
printf("\n"); |
166 |
unimpl("UNKNOWN IOCTL %d\n", request); |
167 |
} |
168 |
return STATUS_SUCCESS; |
169 |
} |
170 |
|
171 |
DEVICE_FNS parallel_fns = { |
172 |
parallel_create, |
173 |
parallel_close, |
174 |
parallel_read, |
175 |
parallel_write, |
176 |
parallel_device_control |
177 |
}; |