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
24 #include "wine/port.h"
33 #include <sys/types.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 #include <sys/ioctl.h>
50 #define WIN32_NO_STATUS
54 #include "ddk/ntddser.h"
61 static void serial_dump( struct object
*obj
, int verbose
);
62 static struct fd
*serial_get_fd( struct object
*obj
);
63 static void serial_destroy(struct object
*obj
);
65 static enum server_fd_type
serial_get_fd_type( struct fd
*fd
);
66 static int serial_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
67 static void serial_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
);
68 static void serial_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
75 struct timeout_user
*read_timer
;
76 SERIAL_TIMEOUTS timeouts
;
77 unsigned int eventmask
;
78 unsigned int generation
; /* event mask change counter */
79 unsigned int pending_write
: 1;
80 unsigned int pending_wait
: 1;
82 struct termios original
;
84 /* FIXME: add dcb, comm status, handler module, sharing */
87 static const struct object_ops serial_ops
=
89 sizeof(struct serial
), /* size */
90 serial_dump
, /* dump */
91 no_get_type
, /* get_type */
92 add_queue
, /* add_queue */
93 remove_queue
, /* remove_queue */
94 default_fd_signaled
, /* signaled */
95 no_satisfied
, /* satisfied */
96 no_signal
, /* signal */
97 serial_get_fd
, /* get_fd */
98 default_fd_map_access
, /* map_access */
99 default_get_sd
, /* get_sd */
100 default_set_sd
, /* set_sd */
101 no_get_full_name
, /* get_full_name */
102 no_lookup_name
, /* lookup_name */
103 no_link_name
, /* link_name */
104 NULL
, /* unlink_name */
105 no_open_file
, /* open_file */
106 no_kernel_obj_list
, /* get_kernel_obj_list */
107 fd_close_handle
, /* close_handle */
108 serial_destroy
/* destroy */
111 static const struct fd_ops serial_fd_ops
=
113 default_fd_get_poll_events
, /* get_poll_events */
114 default_poll_event
, /* poll_event */
115 serial_get_fd_type
, /* get_fd_type */
116 no_fd_read
, /* read */
117 no_fd_write
, /* write */
118 no_fd_flush
, /* flush */
119 default_fd_get_file_info
, /* get_file_info */
120 no_fd_get_volume_info
, /* get_volume_info */
121 serial_ioctl
, /* ioctl */
122 serial_queue_async
, /* queue_async */
123 serial_reselect_async
/* reselect_async */
126 /* check if the given fd is a serial port */
127 int is_serial_fd( struct fd
*fd
)
131 return !tcgetattr( get_unix_fd(fd
), &tios
);
134 /* create a serial object for a given fd */
135 struct object
*create_serial( struct fd
*fd
)
137 struct serial
*serial
;
139 if (!(serial
= alloc_object( &serial_ops
))) return NULL
;
141 serial
->read_timer
= NULL
;
142 serial
->eventmask
= 0;
143 serial
->generation
= 0;
144 serial
->pending_write
= 0;
145 serial
->pending_wait
= 0;
146 memset( &serial
->timeouts
, 0, sizeof(serial
->timeouts
) );
147 serial
->fd
= (struct fd
*)grab_object( fd
);
148 set_fd_user( fd
, &serial_fd_ops
, &serial
->obj
);
152 static struct fd
*serial_get_fd( struct object
*obj
)
154 struct serial
*serial
= (struct serial
*)obj
;
155 return (struct fd
*)grab_object( serial
->fd
);
158 static void serial_destroy( struct object
*obj
)
160 struct serial
*serial
= (struct serial
*)obj
;
161 if (serial
->read_timer
) remove_timeout_user( serial
->read_timer
);
162 release_object( serial
->fd
);
165 static void serial_dump( struct object
*obj
, int verbose
)
167 struct serial
*serial
= (struct serial
*)obj
;
168 assert( obj
->ops
== &serial_ops
);
169 fprintf( stderr
, "Port fd=%p mask=%x\n", serial
->fd
, serial
->eventmask
);
172 static struct serial
*get_serial_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
174 return (struct serial
*)get_handle_obj( process
, handle
, access
, &serial_ops
);
177 static enum server_fd_type
serial_get_fd_type( struct fd
*fd
)
179 return FD_TYPE_SERIAL
;
182 static int serial_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
184 struct serial
*serial
= get_fd_user( fd
);
188 case IOCTL_SERIAL_GET_TIMEOUTS
:
189 if (get_reply_max_size() < sizeof(serial
->timeouts
))
191 set_error( STATUS_BUFFER_TOO_SMALL
);
194 set_reply_data( &serial
->timeouts
, sizeof(serial
->timeouts
));
197 case IOCTL_SERIAL_SET_TIMEOUTS
:
198 if (get_req_data_size() < sizeof(serial
->timeouts
))
200 set_error( STATUS_BUFFER_TOO_SMALL
);
203 memcpy( &serial
->timeouts
, get_req_data(), sizeof(serial
->timeouts
) );
206 case IOCTL_SERIAL_GET_WAIT_MASK
:
207 if (get_reply_max_size() < sizeof(serial
->eventmask
))
209 set_error( STATUS_BUFFER_TOO_SMALL
);
212 set_reply_data( &serial
->eventmask
, sizeof(serial
->eventmask
) );
215 case IOCTL_SERIAL_SET_WAIT_MASK
:
216 if (get_req_data_size() < sizeof(serial
->eventmask
))
218 set_error( STATUS_BUFFER_TOO_SMALL
);
221 serial
->eventmask
= *(unsigned int *)get_req_data();
222 serial
->generation
++;
223 fd_async_wake_up( serial
->fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
227 set_error( STATUS_NOT_SUPPORTED
);
232 static void serial_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
234 struct serial
*serial
= get_fd_user( fd
);
235 timeout_t timeout
= 0;
237 assert(serial
->obj
.ops
== &serial_ops
);
241 case ASYNC_TYPE_READ
:
242 timeout
= serial
->timeouts
.ReadTotalTimeoutConstant
+
243 (timeout_t
)serial
->timeouts
.ReadTotalTimeoutMultiplier
* count
;
245 case ASYNC_TYPE_WRITE
:
246 timeout
= serial
->timeouts
.WriteTotalTimeoutConstant
+
247 (timeout_t
)serial
->timeouts
.WriteTotalTimeoutMultiplier
* count
;
251 fd_queue_async( fd
, async
, type
);
252 if (timeout
) async_set_timeout( async
, timeout
* -10000, STATUS_TIMEOUT
);
253 set_error( STATUS_PENDING
);
256 static void serial_read_timeout( void *arg
)
258 struct serial
*serial
= arg
;
260 serial
->read_timer
= NULL
;
261 fd_async_wake_up( serial
->fd
, ASYNC_TYPE_READ
, STATUS_TIMEOUT
);
264 static void serial_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
266 struct serial
*serial
= get_fd_user( fd
);
268 if (serial
->read_timer
)
270 if (!(default_fd_get_poll_events( fd
) & POLLIN
))
272 remove_timeout_user( serial
->read_timer
);
273 serial
->read_timer
= NULL
;
276 else if (serial
->timeouts
.ReadIntervalTimeout
&& (default_fd_get_poll_events( fd
) & POLLIN
))
278 serial
->read_timer
= add_timeout_user( (timeout_t
)serial
->timeouts
.ReadIntervalTimeout
* -10000,
279 serial_read_timeout
, serial
);
281 default_fd_reselect_async( fd
, queue
);
284 DECL_HANDLER(get_serial_info
)
286 struct serial
*serial
;
288 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
290 if (req
->flags
& SERIALINFO_PENDING_WAIT
)
292 if (serial
->pending_wait
)
294 release_object( serial
);
295 set_error( STATUS_INVALID_PARAMETER
);
298 serial
->pending_wait
= 1;
302 reply
->eventmask
= serial
->eventmask
;
303 reply
->cookie
= serial
->generation
;
306 reply
->pending_write
= serial
->pending_write
;
307 if (req
->flags
& SERIALINFO_PENDING_WRITE
)
308 serial
->pending_write
= 0;
310 release_object( serial
);
314 DECL_HANDLER(set_serial_info
)
316 struct serial
*serial
;
318 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
320 if (req
->flags
& SERIALINFO_PENDING_WAIT
)
322 if (!serial
->pending_wait
)
324 release_object( serial
);
325 set_error( STATUS_INVALID_PARAMETER
);
328 serial
->pending_wait
= 0;
332 if (req
->flags
& SERIALINFO_PENDING_WRITE
)
333 serial
->pending_write
= 1;
335 release_object( serial
);