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