wined3d: Don't use persistent BOs from the client thread if we might need to do verte...
[wine.git] / server / serial.c
blobd665eb7fa35a935cb530421cbd7a8624b7d914b6
1 /*
2 * Server-side serial port communications management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2000,2001 Mike McCormack
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #include <assert.h>
26 #include <fcntl.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
34 #include <time.h>
35 #include <termios.h>
36 #include <unistd.h>
37 #include <poll.h>
38 #ifdef HAVE_UTIME_H
39 #include <utime.h>
40 #endif
42 #include "ntstatus.h"
43 #define WIN32_NO_STATUS
44 #include "windef.h"
45 #include "winternl.h"
46 #include "winioctl.h"
47 #include "ddk/ntddser.h"
49 #include "file.h"
50 #include "handle.h"
51 #include "thread.h"
52 #include "request.h"
54 static void serial_dump( struct object *obj, int verbose );
55 static struct fd *serial_get_fd( struct object *obj );
56 static void serial_destroy(struct object *obj);
58 static enum server_fd_type serial_get_fd_type( struct fd *fd );
59 static void serial_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
60 static void serial_queue_async( struct fd *fd, struct async *async, int type, int count );
61 static void serial_reselect_async( struct fd *fd, struct async_queue *queue );
63 struct serial
65 struct object obj;
66 struct fd *fd;
68 struct timeout_user *read_timer;
69 SERIAL_TIMEOUTS timeouts;
70 unsigned int eventmask;
71 unsigned int generation; /* event mask change counter */
72 unsigned int pending_write : 1;
73 unsigned int pending_wait : 1;
75 struct termios original;
77 /* FIXME: add dcb, comm status, handler module, sharing */
80 static const struct object_ops serial_ops =
82 sizeof(struct serial), /* size */
83 &file_type, /* type */
84 serial_dump, /* dump */
85 add_queue, /* add_queue */
86 remove_queue, /* remove_queue */
87 default_fd_signaled, /* signaled */
88 no_satisfied, /* satisfied */
89 no_signal, /* signal */
90 serial_get_fd, /* get_fd */
91 default_map_access, /* map_access */
92 default_get_sd, /* get_sd */
93 default_set_sd, /* set_sd */
94 no_get_full_name, /* get_full_name */
95 no_lookup_name, /* lookup_name */
96 no_link_name, /* link_name */
97 NULL, /* unlink_name */
98 no_open_file, /* open_file */
99 no_kernel_obj_list, /* get_kernel_obj_list */
100 no_close_handle, /* close_handle */
101 serial_destroy /* destroy */
104 static const struct fd_ops serial_fd_ops =
106 default_fd_get_poll_events, /* get_poll_events */
107 default_poll_event, /* poll_event */
108 serial_get_fd_type, /* get_fd_type */
109 no_fd_read, /* read */
110 no_fd_write, /* write */
111 no_fd_flush, /* flush */
112 default_fd_get_file_info, /* get_file_info */
113 no_fd_get_volume_info, /* get_volume_info */
114 serial_ioctl, /* ioctl */
115 default_fd_cancel_async, /* cancel_async */
116 serial_queue_async, /* queue_async */
117 serial_reselect_async /* reselect_async */
120 /* check if the given fd is a serial port */
121 int is_serial_fd( struct fd *fd )
123 struct termios tios;
125 return !tcgetattr( get_unix_fd(fd), &tios );
128 /* create a serial object for a given fd */
129 struct object *create_serial( struct fd *fd )
131 struct serial *serial;
133 if (!(serial = alloc_object( &serial_ops ))) return NULL;
135 serial->read_timer = NULL;
136 serial->eventmask = 0;
137 serial->generation = 0;
138 serial->pending_write = 0;
139 serial->pending_wait = 0;
140 memset( &serial->timeouts, 0, sizeof(serial->timeouts) );
141 serial->fd = (struct fd *)grab_object( fd );
142 set_fd_user( fd, &serial_fd_ops, &serial->obj );
143 return &serial->obj;
146 static struct fd *serial_get_fd( struct object *obj )
148 struct serial *serial = (struct serial *)obj;
149 return (struct fd *)grab_object( serial->fd );
152 static void serial_destroy( struct object *obj)
154 struct serial *serial = (struct serial *)obj;
155 if (serial->read_timer) remove_timeout_user( serial->read_timer );
156 release_object( serial->fd );
159 static void serial_dump( struct object *obj, int verbose )
161 struct serial *serial = (struct serial *)obj;
162 assert( obj->ops == &serial_ops );
163 fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
166 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
168 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
171 static enum server_fd_type serial_get_fd_type( struct fd *fd )
173 return FD_TYPE_SERIAL;
176 static void serial_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
178 struct serial *serial = get_fd_user( fd );
180 switch (code)
182 case IOCTL_SERIAL_GET_TIMEOUTS:
183 if (get_reply_max_size() < sizeof(serial->timeouts))
185 set_error( STATUS_BUFFER_TOO_SMALL );
186 return;
188 set_reply_data( &serial->timeouts, sizeof(serial->timeouts ));
189 return;
191 case IOCTL_SERIAL_SET_TIMEOUTS:
192 if (get_req_data_size() < sizeof(serial->timeouts))
194 set_error( STATUS_BUFFER_TOO_SMALL );
195 return;
197 memcpy( &serial->timeouts, get_req_data(), sizeof(serial->timeouts) );
198 return;
200 case IOCTL_SERIAL_GET_WAIT_MASK:
201 if (get_reply_max_size() < sizeof(serial->eventmask))
203 set_error( STATUS_BUFFER_TOO_SMALL );
204 return;
206 set_reply_data( &serial->eventmask, sizeof(serial->eventmask) );
207 return;
209 case IOCTL_SERIAL_SET_WAIT_MASK:
210 if (get_req_data_size() < sizeof(serial->eventmask))
212 set_error( STATUS_BUFFER_TOO_SMALL );
213 return;
215 serial->eventmask = *(unsigned int *)get_req_data();
216 serial->generation++;
217 fd_async_wake_up( serial->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
218 return;
220 default:
221 set_error( STATUS_NOT_SUPPORTED );
225 static void serial_queue_async( struct fd *fd, struct async *async, int type, int count )
227 struct serial *serial = get_fd_user( fd );
228 timeout_t timeout = 0;
230 assert(serial->obj.ops == &serial_ops);
232 switch (type)
234 case ASYNC_TYPE_READ:
235 timeout = serial->timeouts.ReadTotalTimeoutConstant +
236 (timeout_t)serial->timeouts.ReadTotalTimeoutMultiplier * count;
237 break;
238 case ASYNC_TYPE_WRITE:
239 timeout = serial->timeouts.WriteTotalTimeoutConstant +
240 (timeout_t)serial->timeouts.WriteTotalTimeoutMultiplier * count;
241 break;
244 fd_queue_async( fd, async, type );
245 if (timeout) async_set_timeout( async, timeout * -10000, STATUS_TIMEOUT );
246 set_error( STATUS_PENDING );
249 static void serial_read_timeout( void *arg )
251 struct serial *serial = arg;
253 serial->read_timer = NULL;
254 fd_async_wake_up( serial->fd, ASYNC_TYPE_READ, STATUS_TIMEOUT );
257 static void serial_reselect_async( struct fd *fd, struct async_queue *queue )
259 struct serial *serial = get_fd_user( fd );
261 if (serial->read_timer)
263 if (!(default_fd_get_poll_events( fd ) & POLLIN))
265 remove_timeout_user( serial->read_timer );
266 serial->read_timer = NULL;
269 else if (serial->timeouts.ReadIntervalTimeout && (default_fd_get_poll_events( fd ) & POLLIN))
271 serial->read_timer = add_timeout_user( (timeout_t)serial->timeouts.ReadIntervalTimeout * -10000,
272 serial_read_timeout, serial );
274 default_fd_reselect_async( fd, queue );
277 DECL_HANDLER(get_serial_info)
279 struct serial *serial;
281 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
283 if (req->flags & SERIALINFO_PENDING_WAIT)
285 if (serial->pending_wait)
287 release_object( serial );
288 set_error( STATUS_INVALID_PARAMETER );
289 return;
291 serial->pending_wait = 1;
294 /* event mask */
295 reply->eventmask = serial->eventmask;
296 reply->cookie = serial->generation;
298 /* pending write */
299 reply->pending_write = serial->pending_write;
300 if (req->flags & SERIALINFO_PENDING_WRITE)
301 serial->pending_write = 0;
303 release_object( serial );
307 DECL_HANDLER(set_serial_info)
309 struct serial *serial;
311 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
313 if (req->flags & SERIALINFO_PENDING_WAIT)
315 if (!serial->pending_wait)
317 release_object( serial );
318 set_error( STATUS_INVALID_PARAMETER );
319 return;
321 serial->pending_wait = 0;
324 /* pending write */
325 if (req->flags & SERIALINFO_PENDING_WRITE)
326 serial->pending_write = 1;
328 release_object( serial );