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