Blame channels/parallel/client/parallel_main.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * Redirected Parallel Port Device Service
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2010 O.S. Systems Software Ltda.
Packit 1fb8d4
 * Copyright 2010 Eduardo Fiss Beloni <beloni@ossystems.com.br>
Packit 1fb8d4
 * Copyright 2015 Thincast Technologies GmbH
Packit 1fb8d4
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <stdio.h>
Packit 1fb8d4
#include <stdlib.h>
Packit 1fb8d4
#include <string.h>
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_UNISTD_H
Packit 1fb8d4
#include <unistd.h>
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <fcntl.h>
Packit 1fb8d4
#include <errno.h>
Packit 1fb8d4
Packit 1fb8d4
#ifndef _WIN32
Packit 1fb8d4
#include <termios.h>
Packit 1fb8d4
#include <strings.h>
Packit 1fb8d4
#include <sys/ioctl.h>
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#ifdef __LINUX__
Packit 1fb8d4
#include <linux/ppdev.h>
Packit 1fb8d4
#include <linux/parport.h>
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/synch.h>
Packit 1fb8d4
#include <winpr/thread.h>
Packit 1fb8d4
#include <winpr/stream.h>
Packit 1fb8d4
#include <winpr/collections.h>
Packit 1fb8d4
#include <winpr/interlocked.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/types.h>
Packit 1fb8d4
#include <freerdp/constants.h>
Packit 1fb8d4
#include <freerdp/channels/rdpdr.h>
Packit 1fb8d4
#include <freerdp/channels/log.h>
Packit 1fb8d4
Packit 1fb8d4
#define TAG CHANNELS_TAG("drive.client")
Packit 1fb8d4
Packit 1fb8d4
struct _PARALLEL_DEVICE
Packit 1fb8d4
{
Packit 1fb8d4
	DEVICE device;
Packit 1fb8d4
Packit 1fb8d4
	int file;
Packit 1fb8d4
	char* path;
Packit 1fb8d4
	UINT32 id;
Packit 1fb8d4
Packit 1fb8d4
	HANDLE thread;
Packit 1fb8d4
	wMessageQueue* queue;
Packit 1fb8d4
	rdpContext* rdpcontext;
Packit 1fb8d4
};
Packit 1fb8d4
typedef struct _PARALLEL_DEVICE PARALLEL_DEVICE;
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT parallel_process_irp_create(PARALLEL_DEVICE* parallel, IRP* irp)
Packit 1fb8d4
{
Packit 1fb8d4
	char* path = NULL;
Packit 1fb8d4
	int status;
Packit Service 5a9772
	WCHAR* ptr;
Packit 1fb8d4
	UINT32 PathLength;
Packit Service 5a9772
	if (!Stream_SafeSeek(irp->input, 28))
Packit Service 5a9772
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	/* DesiredAccess(4) AllocationSize(8), FileAttributes(4) */
Packit 1fb8d4
	/* SharedAccess(4) CreateDisposition(4), CreateOptions(4) */
Packit Service 5a9772
	if (Stream_GetRemainingLength(irp->input) < 4)
Packit Service 5a9772
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	Stream_Read_UINT32(irp->input, PathLength);
Packit Service 5a9772
	ptr = (WCHAR*)Stream_Pointer(irp->input);
Packit Service 5a9772
	if (!Stream_SafeSeek(irp->input, PathLength))
Packit Service 5a9772
		return ERROR_INVALID_DATA;
Packit Service 5a9772
	status = ConvertFromUnicode(CP_UTF8, 0, ptr, PathLength / 2, &path, 0, NULL, NULL);
Packit 1fb8d4
Packit 1fb8d4
	if (status < 1)
Packit Service 5a9772
		if (!(path = (char*)calloc(1, 1)))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "calloc failed!");
Packit 1fb8d4
			return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
	parallel->id = irp->devman->id_sequence++;
Packit 1fb8d4
	parallel->file = open(parallel->path, O_RDWR);
Packit 1fb8d4
Packit 1fb8d4
	if (parallel->file < 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		irp->IoStatus = STATUS_ACCESS_DENIED;
Packit 1fb8d4
		parallel->id = 0;
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		/* all read and write operations should be non-blocking */
Packit 1fb8d4
		if (fcntl(parallel->file, F_SETFL, O_NONBLOCK) == -1)
Packit 1fb8d4
		{
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write_UINT32(irp->output, parallel->id);
Packit 1fb8d4
	Stream_Write_UINT8(irp->output, 0);
Packit 1fb8d4
	free(path);
Packit 1fb8d4
	return irp->Complete(irp);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT parallel_process_irp_close(PARALLEL_DEVICE* parallel, IRP* irp)
Packit 1fb8d4
{
Packit 1fb8d4
	if (close(parallel->file) < 0)
Packit 1fb8d4
	{
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Zero(irp->output, 5); /* Padding(5) */
Packit 1fb8d4
	return irp->Complete(irp);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT parallel_process_irp_read(PARALLEL_DEVICE* parallel, IRP* irp)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 Length;
Packit 1fb8d4
	UINT64 Offset;
Packit 1fb8d4
	ssize_t status;
Packit 1fb8d4
	BYTE* buffer = NULL;
Packit Service 5a9772
	if (Stream_GetRemainingLength(irp->input) < 12)
Packit Service 5a9772
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	Stream_Read_UINT32(irp->input, Length);
Packit 1fb8d4
	Stream_Read_UINT64(irp->input, Offset);
Packit Service 5a9772
	buffer = (BYTE*)malloc(Length);
Packit 1fb8d4
Packit 1fb8d4
	if (!buffer)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "malloc failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	status = read(parallel->file, buffer, Length);
Packit 1fb8d4
Packit 1fb8d4
	if (status < 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		irp->IoStatus = STATUS_UNSUCCESSFUL;
Packit 1fb8d4
		free(buffer);
Packit 1fb8d4
		buffer = NULL;
Packit 1fb8d4
		Length = 0;
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write_UINT32(irp->output, Length);
Packit 1fb8d4
Packit 1fb8d4
	if (Length > 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!Stream_EnsureRemainingCapacity(irp->output, Length))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
Packit 1fb8d4
			free(buffer);
Packit 1fb8d4
			return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		Stream_Write(irp->output, buffer, Length);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	free(buffer);
Packit 1fb8d4
	return irp->Complete(irp);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT parallel_process_irp_write(PARALLEL_DEVICE* parallel, IRP* irp)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 len;
Packit 1fb8d4
	UINT32 Length;
Packit 1fb8d4
	UINT64 Offset;
Packit 1fb8d4
	ssize_t status;
Packit Service 5a9772
	void* ptr;
Packit Service 5a9772
	if (Stream_GetRemainingLength(irp->input) > 12)
Packit Service 5a9772
		return ERROR_INVALID_DATA;
Packit Service 5a9772
Packit 1fb8d4
	Stream_Read_UINT32(irp->input, Length);
Packit 1fb8d4
	Stream_Read_UINT64(irp->input, Offset);
Packit Service 5a9772
	if (!Stream_SafeSeek(irp->input, 20)) /* Padding */
Packit Service 5a9772
		return ERROR_INVALID_DATA;
Packit Service 5a9772
	ptr = Stream_Pointer(irp->input);
Packit Service 5a9772
	if (!Stream_SafeSeek(irp->input, Length))
Packit Service 5a9772
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	len = Length;
Packit 1fb8d4
Packit 1fb8d4
	while (len > 0)
Packit 1fb8d4
	{
Packit Service 5a9772
		status = write(parallel->file, ptr, len);
Packit 1fb8d4
Packit 1fb8d4
		if (status < 0)
Packit 1fb8d4
		{
Packit 1fb8d4
			irp->IoStatus = STATUS_UNSUCCESSFUL;
Packit 1fb8d4
			Length = 0;
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		Stream_Seek(irp->input, status);
Packit 1fb8d4
		len -= status;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write_UINT32(irp->output, Length);
Packit 1fb8d4
	Stream_Write_UINT8(irp->output, 0); /* Padding */
Packit 1fb8d4
	return irp->Complete(irp);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT parallel_process_irp_device_control(PARALLEL_DEVICE* parallel, IRP* irp)
Packit 1fb8d4
{
Packit 1fb8d4
	Stream_Write_UINT32(irp->output, 0); /* OutputBufferLength */
Packit 1fb8d4
	return irp->Complete(irp);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT parallel_process_irp(PARALLEL_DEVICE* parallel, IRP* irp)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT error;
Packit 1fb8d4
Packit 1fb8d4
	switch (irp->MajorFunction)
Packit 1fb8d4
	{
Packit 1fb8d4
		case IRP_MJ_CREATE:
Packit 1fb8d4
			if ((error = parallel_process_irp_create(parallel, irp)))
Packit 1fb8d4
			{
Packit Service 5a9772
				WLog_ERR(TAG, "parallel_process_irp_create failed with error %" PRIu32 "!", error);
Packit 1fb8d4
				return error;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case IRP_MJ_CLOSE:
Packit 1fb8d4
			if ((error = parallel_process_irp_close(parallel, irp)))
Packit 1fb8d4
			{
Packit Service 5a9772
				WLog_ERR(TAG, "parallel_process_irp_close failed with error %" PRIu32 "!", error);
Packit 1fb8d4
				return error;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case IRP_MJ_READ:
Packit 1fb8d4
			if ((error = parallel_process_irp_read(parallel, irp)))
Packit 1fb8d4
			{
Packit Service 5a9772
				WLog_ERR(TAG, "parallel_process_irp_read failed with error %" PRIu32 "!", error);
Packit 1fb8d4
				return error;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case IRP_MJ_WRITE:
Packit 1fb8d4
			if ((error = parallel_process_irp_write(parallel, irp)))
Packit 1fb8d4
			{
Packit Service 5a9772
				WLog_ERR(TAG, "parallel_process_irp_write failed with error %" PRIu32 "!", error);
Packit 1fb8d4
				return error;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case IRP_MJ_DEVICE_CONTROL:
Packit 1fb8d4
			if ((error = parallel_process_irp_device_control(parallel, irp)))
Packit 1fb8d4
			{
Packit Service 5a9772
				WLog_ERR(TAG, "parallel_process_irp_device_control failed with error %" PRIu32 "!",
Packit 1fb8d4
				         error);
Packit 1fb8d4
				return error;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		default:
Packit 1fb8d4
			irp->IoStatus = STATUS_NOT_SUPPORTED;
Packit 1fb8d4
			return irp->Complete(irp);
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static DWORD WINAPI parallel_thread_func(LPVOID arg)
Packit 1fb8d4
{
Packit 1fb8d4
	IRP* irp;
Packit 1fb8d4
	wMessage message;
Packit Service 5a9772
	PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*)arg;
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
Packit 1fb8d4
	while (1)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!MessageQueue_Wait(parallel->queue))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "MessageQueue_Wait failed!");
Packit 1fb8d4
			error = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (!MessageQueue_Peek(parallel->queue, &message, TRUE))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "MessageQueue_Peek failed!");
Packit 1fb8d4
			error = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (message.id == WMQ_QUIT)
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit Service 5a9772
		irp = (IRP*)message.wParam;
Packit 1fb8d4
Packit 1fb8d4
		if ((error = parallel_process_irp(parallel, irp)))
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "parallel_process_irp failed with error %" PRIu32 "!", error);
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (error && parallel->rdpcontext)
Packit Service 5a9772
		setChannelError(parallel->rdpcontext, error, "parallel_thread_func reported an error");
Packit 1fb8d4
Packit 1fb8d4
	ExitThread(error);
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT parallel_irp_request(DEVICE* device, IRP* irp)
Packit 1fb8d4
{
Packit Service 5a9772
	PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*)device;
Packit 1fb8d4
Packit Service 5a9772
	if (!MessageQueue_Post(parallel->queue, NULL, 0, (void*)irp, NULL))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "MessageQueue_Post failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT parallel_free(DEVICE* device)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT error;
Packit Service 5a9772
	PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*)device;
Packit 1fb8d4
Packit Service 5a9772
	if (!MessageQueue_PostQuit(parallel->queue, 0) ||
Packit Service 5a9772
	    (WaitForSingleObject(parallel->thread, INFINITE) == WAIT_FAILED))
Packit 1fb8d4
	{
Packit 1fb8d4
		error = GetLastError();
Packit Service 5a9772
		WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", error);
Packit 1fb8d4
		return error;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	CloseHandle(parallel->thread);
Packit 1fb8d4
	Stream_Free(parallel->device.data, TRUE);
Packit 1fb8d4
	MessageQueue_Free(parallel->queue);
Packit 1fb8d4
	free(parallel);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#ifdef BUILTIN_CHANNELS
Packit Service 5a9772
#define DeviceServiceEntry parallel_DeviceServiceEntry
Packit 1fb8d4
#else
Packit Service 5a9772
#define DeviceServiceEntry FREERDP_API DeviceServiceEntry
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
UINT DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
Packit 1fb8d4
{
Packit 1fb8d4
	char* name;
Packit 1fb8d4
	char* path;
Packit 1fb8d4
	size_t i;
Packit 1fb8d4
	size_t length;
Packit 1fb8d4
	RDPDR_PARALLEL* device;
Packit 1fb8d4
	PARALLEL_DEVICE* parallel;
Packit 1fb8d4
	UINT error;
Packit Service 5a9772
	device = (RDPDR_PARALLEL*)pEntryPoints->device;
Packit 1fb8d4
	name = device->Name;
Packit 1fb8d4
	path = device->Path;
Packit 1fb8d4
Packit 1fb8d4
	if (!name || (name[0] == '*') || !path)
Packit 1fb8d4
	{
Packit 1fb8d4
		/* TODO: implement auto detection of parallel ports */
Packit 1fb8d4
		return CHANNEL_RC_INITIALIZATION_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (name[0] && path[0])
Packit 1fb8d4
	{
Packit Service 5a9772
		parallel = (PARALLEL_DEVICE*)calloc(1, sizeof(PARALLEL_DEVICE));
Packit 1fb8d4
Packit 1fb8d4
		if (!parallel)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "calloc failed!");
Packit 1fb8d4
			return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		parallel->device.type = RDPDR_DTYP_PARALLEL;
Packit 1fb8d4
		parallel->device.name = name;
Packit 1fb8d4
		parallel->device.IRPRequest = parallel_irp_request;
Packit 1fb8d4
		parallel->device.Free = parallel_free;
Packit 1fb8d4
		parallel->rdpcontext = pEntryPoints->rdpcontext;
Packit 1fb8d4
		length = strlen(name);
Packit 1fb8d4
		parallel->device.data = Stream_New(NULL, length + 1);
Packit 1fb8d4
Packit 1fb8d4
		if (!parallel->device.data)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
			error = CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
			goto error_out;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		for (i = 0; i <= length; i++)
Packit 1fb8d4
			Stream_Write_UINT8(parallel->device.data, name[i] < 0 ? '_' : name[i]);
Packit 1fb8d4
Packit 1fb8d4
		parallel->path = path;
Packit 1fb8d4
		parallel->queue = MessageQueue_New(NULL);
Packit 1fb8d4
Packit 1fb8d4
		if (!parallel->queue)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "MessageQueue_New failed!");
Packit 1fb8d4
			error = CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
			goto error_out;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		if ((error = pEntryPoints->RegisterDevice(pEntryPoints->devman, (DEVICE*)parallel)))
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "RegisterDevice failed with error %" PRIu32 "!", error);
Packit 1fb8d4
			goto error_out;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		if (!(parallel->thread =
Packit Service 5a9772
		          CreateThread(NULL, 0, parallel_thread_func, (void*)parallel, 0, NULL)))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "CreateThread failed!");
Packit 1fb8d4
			error = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
			goto error_out;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
error_out:
Packit 1fb8d4
	MessageQueue_Free(parallel->queue);
Packit 1fb8d4
	Stream_Free(parallel->device.data, TRUE);
Packit 1fb8d4
	free(parallel);
Packit 1fb8d4
	return error;
Packit 1fb8d4
}