2000-09-07 20:39:51 +02:00
|
|
|
/*
|
|
|
|
* Server-side serial port communications management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Alexandre Julliard
|
2001-12-20 01:07:05 +01:00
|
|
|
* Copyright (C) 2000,2001 Mike McCormack
|
2000-09-07 20:39:51 +02:00
|
|
|
*
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2000-09-07 20:39:51 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2002-04-26 20:31:19 +02:00
|
|
|
#include "wine/port.h"
|
2000-09-07 20:39:51 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2000-09-07 20:39:51 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2003-02-11 23:27:13 +01:00
|
|
|
#ifdef HAVE_UTIME_H
|
2000-09-07 20:39:51 +02:00
|
|
|
#include <utime.h>
|
2003-02-11 23:27:13 +01:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_TERMIOS_H
|
2000-09-07 20:39:51 +02:00
|
|
|
#include <termios.h>
|
2003-02-11 23:27:13 +01:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
2000-09-07 20:39:51 +02:00
|
|
|
#include <sys/ioctl.h>
|
2003-02-11 23:27:13 +01:00
|
|
|
#endif
|
2005-03-04 13:38:36 +01:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
2000-09-07 20:39:51 +02:00
|
|
|
|
2005-11-28 17:32:54 +01:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2004-03-18 05:08:48 +01:00
|
|
|
#include "winternl.h"
|
2017-05-05 12:18:55 +02:00
|
|
|
#include "winioctl.h"
|
|
|
|
#include "ddk/ntddser.h"
|
2000-09-07 20:39:51 +02:00
|
|
|
|
2003-01-30 01:26:44 +01:00
|
|
|
#include "file.h"
|
2000-09-07 20:39:51 +02:00
|
|
|
#include "handle.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "request.h"
|
|
|
|
|
|
|
|
static void serial_dump( struct object *obj, int verbose );
|
2003-02-19 01:33:32 +01:00
|
|
|
static struct fd *serial_get_fd( struct object *obj );
|
2003-02-14 21:27:09 +01:00
|
|
|
static void serial_destroy(struct object *obj);
|
|
|
|
|
2007-04-10 22:26:23 +02:00
|
|
|
static enum server_fd_type serial_get_fd_type( struct fd *fd );
|
2021-09-03 02:08:54 +02:00
|
|
|
static void serial_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
|
2017-02-15 22:12:45 +01:00
|
|
|
static void serial_queue_async( struct fd *fd, struct async *async, int type, int count );
|
2016-02-02 07:18:31 +01:00
|
|
|
static void serial_reselect_async( struct fd *fd, struct async_queue *queue );
|
2000-09-07 20:39:51 +02:00
|
|
|
|
|
|
|
struct serial
|
|
|
|
{
|
|
|
|
struct object obj;
|
2003-02-19 01:33:32 +01:00
|
|
|
struct fd *fd;
|
2000-09-07 20:39:51 +02:00
|
|
|
|
2016-02-02 07:18:31 +01:00
|
|
|
struct timeout_user *read_timer;
|
2017-05-05 14:10:44 +02:00
|
|
|
SERIAL_TIMEOUTS timeouts;
|
2000-09-07 20:39:51 +02:00
|
|
|
unsigned int eventmask;
|
2013-09-10 04:29:57 +02:00
|
|
|
unsigned int generation; /* event mask change counter */
|
2013-10-31 08:58:19 +01:00
|
|
|
unsigned int pending_write : 1;
|
|
|
|
unsigned int pending_wait : 1;
|
2000-09-07 20:39:51 +02:00
|
|
|
|
|
|
|
struct termios original;
|
|
|
|
|
|
|
|
/* FIXME: add dcb, comm status, handler module, sharing */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct object_ops serial_ops =
|
|
|
|
{
|
|
|
|
sizeof(struct serial), /* size */
|
2021-02-04 13:44:18 +01:00
|
|
|
&file_type, /* type */
|
2000-09-07 20:39:51 +02:00
|
|
|
serial_dump, /* dump */
|
2007-04-04 19:39:29 +02:00
|
|
|
add_queue, /* add_queue */
|
|
|
|
remove_queue, /* remove_queue */
|
2003-01-30 01:26:44 +01:00
|
|
|
default_fd_signaled, /* signaled */
|
2000-09-07 20:39:51 +02:00
|
|
|
no_satisfied, /* satisfied */
|
2005-04-24 19:35:52 +02:00
|
|
|
no_signal, /* signal */
|
2003-02-19 01:33:32 +01:00
|
|
|
serial_get_fd, /* get_fd */
|
2021-02-05 12:10:44 +01:00
|
|
|
default_map_access, /* map_access */
|
2007-10-03 14:10:37 +02:00
|
|
|
default_get_sd, /* get_sd */
|
|
|
|
default_set_sd, /* set_sd */
|
2020-09-22 16:52:31 +02:00
|
|
|
no_get_full_name, /* get_full_name */
|
2005-11-22 15:55:42 +01:00
|
|
|
no_lookup_name, /* lookup_name */
|
2016-02-04 13:07:19 +01:00
|
|
|
no_link_name, /* link_name */
|
|
|
|
NULL, /* unlink_name */
|
2007-03-22 11:44:29 +01:00
|
|
|
no_open_file, /* open_file */
|
2019-03-25 14:37:09 +01:00
|
|
|
no_kernel_obj_list, /* get_kernel_obj_list */
|
2021-03-23 05:04:30 +01:00
|
|
|
no_close_handle, /* close_handle */
|
2003-02-14 21:27:09 +01:00
|
|
|
serial_destroy /* destroy */
|
2003-01-30 01:26:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct fd_ops serial_fd_ops =
|
|
|
|
{
|
2007-04-02 12:49:09 +02:00
|
|
|
default_fd_get_poll_events, /* get_poll_events */
|
|
|
|
default_poll_event, /* poll_event */
|
2010-08-28 13:14:37 +02:00
|
|
|
serial_get_fd_type, /* get_fd_type */
|
2015-05-05 05:17:45 +02:00
|
|
|
no_fd_read, /* read */
|
|
|
|
no_fd_write, /* write */
|
|
|
|
no_fd_flush, /* flush */
|
2018-10-23 17:45:06 +02:00
|
|
|
default_fd_get_file_info, /* get_file_info */
|
2017-10-02 16:42:16 +02:00
|
|
|
no_fd_get_volume_info, /* get_volume_info */
|
2017-05-05 12:18:55 +02:00
|
|
|
serial_ioctl, /* ioctl */
|
2021-09-04 06:11:38 +02:00
|
|
|
default_fd_cancel_async, /* cancel_async */
|
2005-01-14 20:54:38 +01:00
|
|
|
serial_queue_async, /* queue_async */
|
2016-12-01 12:10:34 +01:00
|
|
|
serial_reselect_async /* reselect_async */
|
2000-09-07 20:39:51 +02:00
|
|
|
};
|
|
|
|
|
2004-03-18 05:08:48 +01:00
|
|
|
/* check if the given fd is a serial port */
|
|
|
|
int is_serial_fd( struct fd *fd )
|
2000-09-07 20:39:51 +02:00
|
|
|
{
|
2000-10-15 02:40:25 +02:00
|
|
|
struct termios tios;
|
|
|
|
|
2004-03-18 05:08:48 +01:00
|
|
|
return !tcgetattr( get_unix_fd(fd), &tios );
|
|
|
|
}
|
2001-08-21 19:01:48 +02:00
|
|
|
|
2004-03-18 05:08:48 +01:00
|
|
|
/* create a serial object for a given fd */
|
2007-04-10 22:25:07 +02:00
|
|
|
struct object *create_serial( struct fd *fd )
|
2004-03-18 05:08:48 +01:00
|
|
|
{
|
|
|
|
struct serial *serial;
|
2001-10-08 22:40:57 +02:00
|
|
|
|
2006-01-24 13:31:48 +01:00
|
|
|
if (!(serial = alloc_object( &serial_ops ))) return NULL;
|
|
|
|
|
2016-02-02 07:18:31 +01:00
|
|
|
serial->read_timer = NULL;
|
2003-03-12 23:38:14 +01:00
|
|
|
serial->eventmask = 0;
|
2013-09-10 04:29:57 +02:00
|
|
|
serial->generation = 0;
|
2013-10-25 05:14:42 +02:00
|
|
|
serial->pending_write = 0;
|
2013-10-31 08:58:19 +01:00
|
|
|
serial->pending_wait = 0;
|
2017-05-05 14:10:44 +02:00
|
|
|
memset( &serial->timeouts, 0, sizeof(serial->timeouts) );
|
2006-01-24 13:31:48 +01:00
|
|
|
serial->fd = (struct fd *)grab_object( fd );
|
|
|
|
set_fd_user( fd, &serial_fd_ops, &serial->obj );
|
2004-03-18 05:08:48 +01:00
|
|
|
return &serial->obj;
|
2000-09-07 20:39:51 +02:00
|
|
|
}
|
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
static struct fd *serial_get_fd( struct object *obj )
|
|
|
|
{
|
|
|
|
struct serial *serial = (struct serial *)obj;
|
|
|
|
return (struct fd *)grab_object( serial->fd );
|
|
|
|
}
|
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
static void serial_destroy( struct object *obj)
|
2001-12-20 01:07:05 +01:00
|
|
|
{
|
|
|
|
struct serial *serial = (struct serial *)obj;
|
2016-02-02 07:18:31 +01:00
|
|
|
if (serial->read_timer) remove_timeout_user( serial->read_timer );
|
2007-04-02 12:49:09 +02:00
|
|
|
release_object( serial->fd );
|
2001-12-20 01:07:05 +01:00
|
|
|
}
|
|
|
|
|
2000-10-15 02:40:25 +02:00
|
|
|
static void serial_dump( struct object *obj, int verbose )
|
2000-09-07 20:39:51 +02:00
|
|
|
{
|
2000-10-15 02:40:25 +02:00
|
|
|
struct serial *serial = (struct serial *)obj;
|
2000-09-07 20:39:51 +02:00
|
|
|
assert( obj->ops == &serial_ops );
|
2003-02-19 01:33:32 +01:00
|
|
|
fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
|
2000-09-07 20:39:51 +02:00
|
|
|
}
|
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
|
2000-09-07 20:39:51 +02:00
|
|
|
{
|
|
|
|
return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
|
|
|
|
}
|
|
|
|
|
2007-04-10 22:26:23 +02:00
|
|
|
static enum server_fd_type serial_get_fd_type( struct fd *fd )
|
2000-09-07 20:39:51 +02:00
|
|
|
{
|
2006-11-20 14:14:04 +01:00
|
|
|
return FD_TYPE_SERIAL;
|
2000-09-07 20:39:51 +02:00
|
|
|
}
|
|
|
|
|
2021-09-03 02:08:54 +02:00
|
|
|
static void serial_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
|
2017-05-05 12:18:55 +02:00
|
|
|
{
|
|
|
|
struct serial *serial = get_fd_user( fd );
|
|
|
|
|
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case IOCTL_SERIAL_GET_TIMEOUTS:
|
2017-06-26 12:20:10 +02:00
|
|
|
if (get_reply_max_size() < sizeof(serial->timeouts))
|
|
|
|
{
|
2017-05-05 14:10:44 +02:00
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
2021-09-03 02:08:54 +02:00
|
|
|
return;
|
2017-06-26 12:20:10 +02:00
|
|
|
}
|
|
|
|
set_reply_data( &serial->timeouts, sizeof(serial->timeouts ));
|
2021-09-03 02:08:54 +02:00
|
|
|
return;
|
2017-05-05 12:18:55 +02:00
|
|
|
|
2017-05-05 12:21:09 +02:00
|
|
|
case IOCTL_SERIAL_SET_TIMEOUTS:
|
2017-06-26 12:20:10 +02:00
|
|
|
if (get_req_data_size() < sizeof(serial->timeouts))
|
|
|
|
{
|
2017-05-05 14:10:44 +02:00
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
2021-09-03 02:08:54 +02:00
|
|
|
return;
|
2017-06-26 12:20:10 +02:00
|
|
|
}
|
|
|
|
memcpy( &serial->timeouts, get_req_data(), sizeof(serial->timeouts) );
|
2021-09-03 02:08:54 +02:00
|
|
|
return;
|
2017-05-05 12:21:09 +02:00
|
|
|
|
2017-05-05 12:34:16 +02:00
|
|
|
case IOCTL_SERIAL_GET_WAIT_MASK:
|
2017-06-26 12:20:10 +02:00
|
|
|
if (get_reply_max_size() < sizeof(serial->eventmask))
|
|
|
|
{
|
2017-05-05 12:34:16 +02:00
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
2021-09-03 02:08:54 +02:00
|
|
|
return;
|
2017-06-26 12:20:10 +02:00
|
|
|
}
|
|
|
|
set_reply_data( &serial->eventmask, sizeof(serial->eventmask) );
|
2021-09-03 02:08:54 +02:00
|
|
|
return;
|
2017-05-05 12:34:16 +02:00
|
|
|
|
2017-05-05 12:28:19 +02:00
|
|
|
case IOCTL_SERIAL_SET_WAIT_MASK:
|
2017-06-26 12:20:10 +02:00
|
|
|
if (get_req_data_size() < sizeof(serial->eventmask))
|
2017-05-05 12:28:19 +02:00
|
|
|
{
|
2017-06-26 12:20:10 +02:00
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
2021-09-03 02:08:54 +02:00
|
|
|
return;
|
2017-05-05 12:28:19 +02:00
|
|
|
}
|
2017-06-26 12:20:10 +02:00
|
|
|
serial->eventmask = *(unsigned int *)get_req_data();
|
|
|
|
serial->generation++;
|
|
|
|
fd_async_wake_up( serial->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
|
2021-09-03 02:08:54 +02:00
|
|
|
return;
|
2017-05-05 12:28:19 +02:00
|
|
|
|
2017-05-05 12:18:55 +02:00
|
|
|
default:
|
|
|
|
set_error( STATUS_NOT_SUPPORTED );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 22:12:45 +01:00
|
|
|
static void serial_queue_async( struct fd *fd, struct async *async, int type, int count )
|
2000-11-13 20:27:21 +01:00
|
|
|
{
|
2003-02-14 21:27:09 +01:00
|
|
|
struct serial *serial = get_fd_user( fd );
|
2007-04-17 20:08:59 +02:00
|
|
|
timeout_t timeout = 0;
|
2000-11-13 20:27:21 +01:00
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
assert(serial->obj.ops == &serial_ops);
|
2000-11-13 20:27:21 +01:00
|
|
|
|
2005-01-14 20:54:38 +01:00
|
|
|
switch (type)
|
2000-11-13 20:27:21 +01:00
|
|
|
{
|
|
|
|
case ASYNC_TYPE_READ:
|
2017-05-05 14:10:44 +02:00
|
|
|
timeout = serial->timeouts.ReadTotalTimeoutConstant +
|
|
|
|
(timeout_t)serial->timeouts.ReadTotalTimeoutMultiplier * count;
|
2001-12-20 01:07:05 +01:00
|
|
|
break;
|
2000-11-13 20:27:21 +01:00
|
|
|
case ASYNC_TYPE_WRITE:
|
2017-05-05 14:10:44 +02:00
|
|
|
timeout = serial->timeouts.WriteTotalTimeoutConstant +
|
|
|
|
(timeout_t)serial->timeouts.WriteTotalTimeoutMultiplier * count;
|
2001-12-20 01:07:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-07-04 15:26:35 +02:00
|
|
|
fd_queue_async( fd, async, type );
|
|
|
|
if (timeout) async_set_timeout( async, timeout * -10000, STATUS_TIMEOUT );
|
|
|
|
set_error( STATUS_PENDING );
|
2001-12-20 01:07:05 +01:00
|
|
|
}
|
2000-11-13 20:27:21 +01:00
|
|
|
|
2016-02-02 07:18:31 +01:00
|
|
|
static void serial_read_timeout( void *arg )
|
|
|
|
{
|
|
|
|
struct serial *serial = arg;
|
|
|
|
|
|
|
|
serial->read_timer = NULL;
|
|
|
|
fd_async_wake_up( serial->fd, ASYNC_TYPE_READ, STATUS_TIMEOUT );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_reselect_async( struct fd *fd, struct async_queue *queue )
|
|
|
|
{
|
|
|
|
struct serial *serial = get_fd_user( fd );
|
|
|
|
|
|
|
|
if (serial->read_timer)
|
|
|
|
{
|
|
|
|
if (!(default_fd_get_poll_events( fd ) & POLLIN))
|
|
|
|
{
|
|
|
|
remove_timeout_user( serial->read_timer );
|
|
|
|
serial->read_timer = NULL;
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 14:10:44 +02:00
|
|
|
else if (serial->timeouts.ReadIntervalTimeout && (default_fd_get_poll_events( fd ) & POLLIN))
|
2016-02-02 07:18:31 +01:00
|
|
|
{
|
2017-05-05 14:10:44 +02:00
|
|
|
serial->read_timer = add_timeout_user( (timeout_t)serial->timeouts.ReadIntervalTimeout * -10000,
|
2016-02-02 07:18:31 +01:00
|
|
|
serial_read_timeout, serial );
|
|
|
|
}
|
|
|
|
default_fd_reselect_async( fd, queue );
|
|
|
|
}
|
|
|
|
|
2000-09-16 22:55:12 +02:00
|
|
|
DECL_HANDLER(get_serial_info)
|
|
|
|
{
|
|
|
|
struct serial *serial;
|
|
|
|
|
|
|
|
if ((serial = get_serial_obj( current->process, req->handle, 0 )))
|
|
|
|
{
|
2013-10-31 08:58:19 +01:00
|
|
|
if (req->flags & SERIALINFO_PENDING_WAIT)
|
|
|
|
{
|
|
|
|
if (serial->pending_wait)
|
|
|
|
{
|
|
|
|
release_object( serial );
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
serial->pending_wait = 1;
|
|
|
|
}
|
|
|
|
|
2000-09-16 22:55:12 +02:00
|
|
|
/* event mask */
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->eventmask = serial->eventmask;
|
2013-09-10 04:29:57 +02:00
|
|
|
reply->cookie = serial->generation;
|
2000-09-16 22:55:12 +02:00
|
|
|
|
2013-10-25 05:14:42 +02:00
|
|
|
/* pending write */
|
|
|
|
reply->pending_write = serial->pending_write;
|
|
|
|
if (req->flags & SERIALINFO_PENDING_WRITE)
|
|
|
|
serial->pending_write = 0;
|
|
|
|
|
2000-09-16 22:55:12 +02:00
|
|
|
release_object( serial );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DECL_HANDLER(set_serial_info)
|
|
|
|
{
|
|
|
|
struct serial *serial;
|
|
|
|
|
|
|
|
if ((serial = get_serial_obj( current->process, req->handle, 0 )))
|
|
|
|
{
|
2013-10-31 08:58:19 +01:00
|
|
|
if (req->flags & SERIALINFO_PENDING_WAIT)
|
|
|
|
{
|
|
|
|
if (!serial->pending_wait)
|
|
|
|
{
|
|
|
|
release_object( serial );
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
serial->pending_wait = 0;
|
|
|
|
}
|
|
|
|
|
2013-10-25 05:14:42 +02:00
|
|
|
/* pending write */
|
|
|
|
if (req->flags & SERIALINFO_PENDING_WRITE)
|
|
|
|
serial->pending_write = 1;
|
|
|
|
|
2000-09-16 22:55:12 +02:00
|
|
|
release_object( serial );
|
|
|
|
}
|
|
|
|
}
|