1 |
astrand |
963 |
/* -*- c-basic-offset: 8 -*- |
2 |
|
|
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
Copyright (C) Matthew Chapman 1999-2005 |
4 |
|
|
|
5 |
|
|
This program is free software; you can redistribute it and/or modify |
6 |
|
|
it under the terms of the GNU General Public License as published by |
7 |
|
|
the Free Software Foundation; either version 2 of the License, or |
8 |
|
|
(at your option) any later version. |
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, |
11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
GNU General Public License for more details. |
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU General Public License |
16 |
|
|
along with this program; if not, write to the Free Software |
17 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 |
|
|
*/ |
19 |
n-ki |
569 |
#define MAX_PARALLEL_DEVICES 1 |
20 |
|
|
|
21 |
|
|
#define FILE_DEVICE_PARALLEL 0x22 |
22 |
|
|
|
23 |
|
|
#define IOCTL_PAR_QUERY_RAW_DEVICE_ID 0x0c |
24 |
|
|
|
25 |
|
|
#include "rdesktop.h" |
26 |
|
|
#include <unistd.h> |
27 |
|
|
#include <fcntl.h> |
28 |
n-ki |
631 |
#include <sys/ioctl.h> |
29 |
|
|
#include <errno.h> |
30 |
n-ki |
569 |
|
31 |
stargo |
632 |
#if defined(__linux__) |
32 |
|
|
#include <linux/lp.h> |
33 |
|
|
#endif |
34 |
|
|
|
35 |
n-ki |
631 |
extern int errno; |
36 |
|
|
|
37 |
n-ki |
569 |
extern RDPDR_DEVICE g_rdpdr_device[]; |
38 |
|
|
|
39 |
|
|
|
40 |
|
|
/* Enumeration of devices from rdesktop.c */ |
41 |
|
|
/* returns numer of units found and initialized. */ |
42 |
|
|
/* optarg looks like ':LPT1=/dev/lp0' */ |
43 |
|
|
/* when it arrives to this function. */ |
44 |
|
|
int |
45 |
astrand |
608 |
parallel_enum_devices(uint32 * id, char *optarg) |
46 |
n-ki |
569 |
{ |
47 |
|
|
PARALLEL_DEVICE *ppar_info; |
48 |
|
|
|
49 |
|
|
char *pos = optarg; |
50 |
|
|
char *pos2; |
51 |
|
|
int count = 0; |
52 |
|
|
|
53 |
stargo |
865 |
/* skip the first colon */ |
54 |
n-ki |
569 |
optarg++; |
55 |
|
|
while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES) |
56 |
|
|
{ |
57 |
|
|
ppar_info = (PARALLEL_DEVICE *) xmalloc(sizeof(PARALLEL_DEVICE)); |
58 |
|
|
|
59 |
|
|
pos2 = next_arg(optarg, '='); |
60 |
|
|
strcpy(g_rdpdr_device[*id].name, optarg); |
61 |
|
|
|
62 |
stargo |
570 |
toupper_str(g_rdpdr_device[*id].name); |
63 |
n-ki |
569 |
|
64 |
|
|
g_rdpdr_device[*id].local_path = xmalloc(strlen(pos2) + 1); |
65 |
|
|
strcpy(g_rdpdr_device[*id].local_path, pos2); |
66 |
|
|
printf("PARALLEL %s to %s\n", optarg, pos2); |
67 |
|
|
|
68 |
stargo |
865 |
/* set device type */ |
69 |
n-ki |
569 |
g_rdpdr_device[*id].device_type = DEVICE_TYPE_PARALLEL; |
70 |
|
|
g_rdpdr_device[*id].pdevice_data = (void *) ppar_info; |
71 |
n-ki |
631 |
g_rdpdr_device[*id].handle = 0; |
72 |
n-ki |
569 |
count++; |
73 |
|
|
(*id)++; |
74 |
|
|
|
75 |
|
|
optarg = pos; |
76 |
|
|
} |
77 |
|
|
return count; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
static NTSTATUS |
81 |
n-ki |
592 |
parallel_create(uint32 device_id, uint32 access, uint32 share_mode, uint32 disposition, |
82 |
jsorg71 |
776 |
uint32 flags, char *filename, NTHANDLE * handle) |
83 |
n-ki |
569 |
{ |
84 |
|
|
int parallel_fd; |
85 |
|
|
|
86 |
n-ki |
589 |
parallel_fd = open(g_rdpdr_device[device_id].local_path, O_RDWR); |
87 |
n-ki |
592 |
if (parallel_fd == -1) |
88 |
|
|
{ |
89 |
n-ki |
589 |
perror("open"); |
90 |
n-ki |
569 |
return STATUS_ACCESS_DENIED; |
91 |
n-ki |
589 |
} |
92 |
n-ki |
569 |
|
93 |
n-ki |
631 |
/* all read and writes should be non blocking */ |
94 |
|
|
if (fcntl(parallel_fd, F_SETFL, O_NONBLOCK) == -1) |
95 |
|
|
perror("fcntl"); |
96 |
|
|
|
97 |
stargo |
632 |
#if defined(LPABORT) |
98 |
|
|
/* Retry on errors */ |
99 |
n-ki |
631 |
ioctl(parallel_fd, LPABORT, (int) 1); |
100 |
stargo |
632 |
#endif |
101 |
n-ki |
631 |
|
102 |
n-ki |
584 |
g_rdpdr_device[device_id].handle = parallel_fd; |
103 |
|
|
|
104 |
n-ki |
569 |
*handle = parallel_fd; |
105 |
n-ki |
592 |
|
106 |
n-ki |
569 |
return STATUS_SUCCESS; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
static NTSTATUS |
110 |
jsorg71 |
776 |
parallel_close(NTHANDLE handle) |
111 |
n-ki |
569 |
{ |
112 |
n-ki |
631 |
int i = get_device_index(handle); |
113 |
|
|
if (i >= 0) |
114 |
|
|
g_rdpdr_device[i].handle = 0; |
115 |
n-ki |
569 |
close(handle); |
116 |
|
|
return STATUS_SUCCESS; |
117 |
|
|
} |
118 |
|
|
|
119 |
astrand |
664 |
static NTSTATUS |
120 |
jsorg71 |
776 |
parallel_read(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) |
121 |
n-ki |
589 |
{ |
122 |
|
|
*result = read(handle, data, length); |
123 |
|
|
return STATUS_SUCCESS; |
124 |
|
|
} |
125 |
|
|
|
126 |
n-ki |
569 |
static NTSTATUS |
127 |
jsorg71 |
776 |
parallel_write(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) |
128 |
n-ki |
569 |
{ |
129 |
n-ki |
631 |
int rc = STATUS_SUCCESS; |
130 |
|
|
|
131 |
|
|
int n = write(handle, data, length); |
132 |
|
|
if (n < 0) |
133 |
|
|
{ |
134 |
stargo |
832 |
#if defined(LPGETSTATUS) |
135 |
n-ki |
631 |
int status; |
136 |
stargo |
832 |
#endif |
137 |
n-ki |
631 |
|
138 |
|
|
*result = 0; |
139 |
|
|
switch (errno) |
140 |
|
|
{ |
141 |
|
|
case EAGAIN: |
142 |
|
|
rc = STATUS_DEVICE_OFF_LINE; |
143 |
|
|
case ENOSPC: |
144 |
|
|
rc = STATUS_DEVICE_PAPER_EMPTY; |
145 |
|
|
case EIO: |
146 |
|
|
rc = STATUS_DEVICE_OFF_LINE; |
147 |
|
|
default: |
148 |
|
|
rc = STATUS_DEVICE_POWERED_OFF; |
149 |
|
|
} |
150 |
stargo |
632 |
#if defined(LPGETSTATUS) |
151 |
n-ki |
631 |
if (ioctl(handle, LPGETSTATUS, &status) == 0) |
152 |
|
|
{ |
153 |
|
|
/* coming soon: take care for the printer status */ |
154 |
|
|
printf("parallel_write: status = %d, errno = %d\n", status, errno); |
155 |
|
|
} |
156 |
stargo |
632 |
#endif |
157 |
n-ki |
631 |
} |
158 |
|
|
*result = n; |
159 |
|
|
return rc; |
160 |
n-ki |
569 |
} |
161 |
|
|
|
162 |
|
|
static NTSTATUS |
163 |
jsorg71 |
776 |
parallel_device_control(NTHANDLE handle, uint32 request, STREAM in, STREAM out) |
164 |
n-ki |
569 |
{ |
165 |
|
|
if ((request >> 16) != FILE_DEVICE_PARALLEL) |
166 |
|
|
return STATUS_INVALID_PARAMETER; |
167 |
|
|
|
168 |
|
|
/* extract operation */ |
169 |
|
|
request >>= 2; |
170 |
|
|
request &= 0xfff; |
171 |
|
|
|
172 |
|
|
printf("PARALLEL IOCTL %d: ", request); |
173 |
|
|
|
174 |
|
|
switch (request) |
175 |
|
|
{ |
176 |
|
|
case IOCTL_PAR_QUERY_RAW_DEVICE_ID: |
177 |
|
|
|
178 |
|
|
default: |
179 |
|
|
|
180 |
|
|
printf("\n"); |
181 |
|
|
unimpl("UNKNOWN IOCTL %d\n", request); |
182 |
|
|
} |
183 |
|
|
return STATUS_SUCCESS; |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
DEVICE_FNS parallel_fns = { |
187 |
|
|
parallel_create, |
188 |
|
|
parallel_close, |
189 |
n-ki |
589 |
parallel_read, |
190 |
n-ki |
569 |
parallel_write, |
191 |
|
|
parallel_device_control |
192 |
|
|
}; |