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