2
* FreeRDP: A Remote Desktop Protocol client.
3
* Redirected Parallel Port Device Service
5
* Copyright 2010 O.S. Systems Software Ltda.
6
* Copyright 2010 Eduardo Fiss Beloni <beloni@ossystems.com.br>
8
* Licensed under the Apache License, Version 2.0 (the "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
30
#include <sys/ioctl.h>
32
#include <linux/ppdev.h>
33
#include <linux/parport.h>
36
#include <freerdp/types.h>
37
#include <freerdp/constants.h>
38
#include <freerdp/utils/list.h>
39
#include <freerdp/utils/thread.h>
40
#include <freerdp/utils/memory.h>
41
#include <freerdp/utils/stream.h>
42
#include <freerdp/utils/svc_plugin.h>
44
#include "rdpdr_constants.h"
45
#include "rdpdr_types.h"
47
struct _PARALLEL_DEVICE
56
freerdp_thread* thread;
58
typedef struct _PARALLEL_DEVICE PARALLEL_DEVICE;
60
static void parallel_process_irp_create(PARALLEL_DEVICE* parallel, IRP* irp)
66
stream_seek(irp->input, 28);
67
/* DesiredAccess(4) AllocationSize(8), FileAttributes(4) */
68
/* SharedAccess(4) CreateDisposition(4), CreateOptions(4) */
69
stream_read_uint32(irp->input, PathLength);
71
uniconv = freerdp_uniconv_new();
72
path = freerdp_uniconv_in(uniconv, stream_get_tail(irp->input), PathLength);
73
freerdp_uniconv_free(uniconv);
75
parallel->id = irp->devman->id_sequence++;
76
parallel->file = open(parallel->path, O_RDWR);
77
if (parallel->file < 0)
79
irp->IoStatus = STATUS_ACCESS_DENIED;
82
DEBUG_WARN("failed to create %s: %s", parallel->path, strerror(errno));
86
/* all read and write operations should be non-blocking */
87
if (fcntl(parallel->file, F_SETFL, O_NONBLOCK) == -1)
88
DEBUG_WARN("%s fcntl %s", path, strerror(errno));
90
DEBUG_SVC("%s(%d) created", parallel->path, parallel->file);
93
stream_write_uint32(irp->output, parallel->id);
94
stream_write_uint8(irp->output, 0);
101
static void parallel_process_irp_close(PARALLEL_DEVICE* parallel, IRP* irp)
103
if (close(parallel->file) < 0)
104
DEBUG_SVC("failed to close %s(%d)", parallel->path, parallel->id);
106
DEBUG_SVC("%s(%d) closed", parallel->path, parallel->id);
108
stream_write_zero(irp->output, 5); /* Padding(5) */
113
static void parallel_process_irp_read(PARALLEL_DEVICE* parallel, IRP* irp)
118
uint8* buffer = NULL;
120
stream_read_uint32(irp->input, Length);
121
stream_read_uint64(irp->input, Offset);
123
buffer = (uint8*) xmalloc(Length);
125
status = read(parallel->file, irp->output->p, Length);
129
irp->IoStatus = STATUS_UNSUCCESSFUL;
134
DEBUG_WARN("read %s(%d) failed", parallel->path, parallel->id);
138
DEBUG_SVC("read %llu-%llu from %d", Offset, Offset + Length, parallel->id);
141
stream_write_uint32(irp->output, Length);
144
stream_check_size(irp->output, Length);
145
stream_write(irp->output, buffer, Length);
152
static void parallel_process_irp_write(PARALLEL_DEVICE* parallel, IRP* irp)
159
stream_read_uint32(irp->input, Length);
160
stream_read_uint64(irp->input, Offset);
161
stream_seek(irp->input, 20); /* Padding */
163
DEBUG_SVC("Length %u Offset %llu", Length, Offset);
168
status = write(parallel->file, stream_get_tail(irp->input), len);
172
irp->IoStatus = STATUS_UNSUCCESSFUL;
175
DEBUG_WARN("write %s(%d) failed.", parallel->path, parallel->id);
179
stream_seek(irp->input, status);
183
stream_write_uint32(irp->output, Length);
184
stream_write_uint8(irp->output, 0); /* Padding */
189
static void parallel_process_irp_device_control(PARALLEL_DEVICE* parallel, IRP* irp)
192
stream_write_uint32(irp->output, 0); /* OutputBufferLength */
196
static void parallel_process_irp(PARALLEL_DEVICE* parallel, IRP* irp)
198
DEBUG_SVC("MajorFunction %u", irp->MajorFunction);
200
switch (irp->MajorFunction)
203
parallel_process_irp_create(parallel, irp);
207
parallel_process_irp_close(parallel, irp);
211
parallel_process_irp_read(parallel, irp);
215
parallel_process_irp_write(parallel, irp);
218
case IRP_MJ_DEVICE_CONTROL:
219
parallel_process_irp_device_control(parallel, irp);
223
DEBUG_WARN("MajorFunction 0x%X not supported", irp->MajorFunction);
224
irp->IoStatus = STATUS_NOT_SUPPORTED;
230
static void parallel_process_irp_list(PARALLEL_DEVICE* parallel)
236
if (freerdp_thread_is_stopped(parallel->thread))
239
freerdp_thread_lock(parallel->thread);
240
irp = (IRP*) list_dequeue(parallel->irp_list);
241
freerdp_thread_unlock(parallel->thread);
246
parallel_process_irp(parallel, irp);
250
static void* parallel_thread_func(void* arg)
252
PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*) arg;
256
freerdp_thread_wait(parallel->thread);
258
if (freerdp_thread_is_stopped(parallel->thread))
261
freerdp_thread_reset(parallel->thread);
262
parallel_process_irp_list(parallel);
265
freerdp_thread_quit(parallel->thread);
270
static void parallel_irp_request(DEVICE* device, IRP* irp)
272
PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*) device;
274
freerdp_thread_lock(parallel->thread);
275
list_enqueue(parallel->irp_list, irp);
276
freerdp_thread_unlock(parallel->thread);
278
freerdp_thread_signal(parallel->thread);
281
static void parallel_free(DEVICE* device)
284
PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*) device;
286
DEBUG_SVC("freeing device");
288
freerdp_thread_stop(parallel->thread);
289
freerdp_thread_free(parallel->thread);
291
while ((irp = (IRP*) list_dequeue(parallel->irp_list)) != NULL)
294
list_free(parallel->irp_list);
299
int DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
304
PARALLEL_DEVICE* parallel;
306
name = (char*) pEntryPoints->plugin_data->data[1];
307
path = (char*) pEntryPoints->plugin_data->data[2];
309
if (name[0] && path[0])
311
parallel = xnew(PARALLEL_DEVICE);
313
parallel->device.type = RDPDR_DTYP_PARALLEL;
314
parallel->device.name = name;
315
parallel->device.IRPRequest = parallel_irp_request;
316
parallel->device.Free = parallel_free;
318
length = strlen(name);
319
parallel->device.data = stream_new(length + 1);
321
for (i = 0; i <= length; i++)
322
stream_write_uint8(parallel->device.data, name[i] < 0 ? '_' : name[i]);
324
parallel->path = path;
326
parallel->irp_list = list_new();
327
parallel->thread = freerdp_thread_new();
329
pEntryPoints->RegisterDevice(pEntryPoints->devman, (DEVICE*) parallel);
331
freerdp_thread_start(parallel->thread, parallel_thread_func, parallel);