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
32 #include <sys/types.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 #include <sys/ioctl.h>
47 #define WIN32_NO_STATUS
51 #include "ddk/ntddser.h"
58 static void serial_dump( struct object
*obj
, int verbose
);
59 static struct fd
*serial_get_fd( struct object
*obj
);
60 static void serial_destroy(struct object
*obj
);
62 static enum server_fd_type
serial_get_fd_type( struct fd
*fd
);
63 static void serial_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
64 static void serial_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
);
65 static void serial_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
72 struct timeout_user
*read_timer
;
73 SERIAL_TIMEOUTS timeouts
;
74 unsigned int eventmask
;
75 unsigned int generation
; /* event mask change counter */
76 unsigned int pending_write
: 1;
77 unsigned int pending_wait
: 1;
79 struct termios original
;
81 /* FIXME: add dcb, comm status, handler module, sharing */
84 static const struct object_ops serial_ops
=
86 sizeof(struct serial
), /* size */
87 &file_type
, /* type */
88 serial_dump
, /* dump */
89 add_queue
, /* add_queue */
90 remove_queue
, /* remove_queue */
91 default_fd_signaled
, /* signaled */
92 no_satisfied
, /* satisfied */
93 no_signal
, /* signal */
94 serial_get_fd
, /* get_fd */
95 default_map_access
, /* map_access */
96 default_get_sd
, /* get_sd */
97 default_set_sd
, /* set_sd */
98 no_get_full_name
, /* get_full_name */
99 no_lookup_name
, /* lookup_name */
100 no_link_name
, /* link_name */
101 NULL
, /* unlink_name */
102 no_open_file
, /* open_file */
103 no_kernel_obj_list
, /* get_kernel_obj_list */
104 no_close_handle
, /* close_handle */
105 serial_destroy
/* destroy */
108 static const struct fd_ops serial_fd_ops
=
110 default_fd_get_poll_events
, /* get_poll_events */
111 default_poll_event
, /* poll_event */
112 serial_get_fd_type
, /* get_fd_type */
113 no_fd_read
, /* read */
114 no_fd_write
, /* write */
115 no_fd_flush
, /* flush */
116 default_fd_get_file_info
, /* get_file_info */
117 no_fd_get_volume_info
, /* get_volume_info */
118 serial_ioctl
, /* ioctl */
119 default_fd_cancel_async
, /* cancel_async */
120 serial_queue_async
, /* queue_async */
121 serial_reselect_async
/* reselect_async */
124 /* check if the given fd is a serial port */
125 int is_serial_fd( struct fd
*fd
)
129 return !tcgetattr( get_unix_fd(fd
), &tios
);
132 /* create a serial object for a given fd */
133 struct object
*create_serial( struct fd
*fd
)
135 struct serial
*serial
;
137 if (!(serial
= alloc_object( &serial_ops
))) return NULL
;
139 serial
->read_timer
= NULL
;
140 serial
->eventmask
= 0;
141 serial
->generation
= 0;
142 serial
->pending_write
= 0;
143 serial
->pending_wait
= 0;
144 memset( &serial
->timeouts
, 0, sizeof(serial
->timeouts
) );
145 serial
->fd
= (struct fd
*)grab_object( fd
);
146 set_fd_user( fd
, &serial_fd_ops
, &serial
->obj
);
150 static struct fd
*serial_get_fd( struct object
*obj
)
152 struct serial
*serial
= (struct serial
*)obj
;
153 return (struct fd
*)grab_object( serial
->fd
);
156 static void serial_destroy( struct object
*obj
)
158 struct serial
*serial
= (struct serial
*)obj
;
159 if (serial
->read_timer
) remove_timeout_user( serial
->read_timer
);
160 release_object( serial
->fd
);
163 static void serial_dump( struct object
*obj
, int verbose
)
165 struct serial
*serial
= (struct serial
*)obj
;
166 assert( obj
->ops
== &serial_ops
);
167 fprintf( stderr
, "Port fd=%p mask=%x\n", serial
->fd
, serial
->eventmask
);
170 static struct serial
*get_serial_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
172 return (struct serial
*)get_handle_obj( process
, handle
, access
, &serial_ops
);
175 static enum server_fd_type
serial_get_fd_type( struct fd
*fd
)
177 return FD_TYPE_SERIAL
;
180 static void serial_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
182 struct serial
*serial
= get_fd_user( fd
);
186 case IOCTL_SERIAL_GET_TIMEOUTS
:
187 if (get_reply_max_size() < sizeof(serial
->timeouts
))
189 set_error( STATUS_BUFFER_TOO_SMALL
);
192 set_reply_data( &serial
->timeouts
, sizeof(serial
->timeouts
));
195 case IOCTL_SERIAL_SET_TIMEOUTS
:
196 if (get_req_data_size() < sizeof(serial
->timeouts
))
198 set_error( STATUS_BUFFER_TOO_SMALL
);
201 memcpy( &serial
->timeouts
, get_req_data(), sizeof(serial
->timeouts
) );
204 case IOCTL_SERIAL_GET_WAIT_MASK
:
205 if (get_reply_max_size() < sizeof(serial
->eventmask
))
207 set_error( STATUS_BUFFER_TOO_SMALL
);
210 set_reply_data( &serial
->eventmask
, sizeof(serial
->eventmask
) );
213 case IOCTL_SERIAL_SET_WAIT_MASK
:
214 if (get_req_data_size() < sizeof(serial
->eventmask
))
216 set_error( STATUS_BUFFER_TOO_SMALL
);
219 serial
->eventmask
= *(unsigned int *)get_req_data();
220 serial
->generation
++;
221 fd_async_wake_up( serial
->fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
225 set_error( STATUS_NOT_SUPPORTED
);
229 static void serial_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
231 struct serial
*serial
= get_fd_user( fd
);
232 timeout_t timeout
= 0;
234 assert(serial
->obj
.ops
== &serial_ops
);
238 case ASYNC_TYPE_READ
:
239 timeout
= serial
->timeouts
.ReadTotalTimeoutConstant
+
240 (timeout_t
)serial
->timeouts
.ReadTotalTimeoutMultiplier
* count
;
242 case ASYNC_TYPE_WRITE
:
243 timeout
= serial
->timeouts
.WriteTotalTimeoutConstant
+
244 (timeout_t
)serial
->timeouts
.WriteTotalTimeoutMultiplier
* count
;
248 fd_queue_async( fd
, async
, type
);
249 if (timeout
) async_set_timeout( async
, timeout
* -10000, STATUS_TIMEOUT
);
250 set_error( STATUS_PENDING
);
253 static void serial_read_timeout( void *arg
)
255 struct serial
*serial
= arg
;
257 serial
->read_timer
= NULL
;
258 fd_async_wake_up( serial
->fd
, ASYNC_TYPE_READ
, STATUS_TIMEOUT
);
261 static void serial_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
263 struct serial
*serial
= get_fd_user( fd
);
265 if (serial
->read_timer
)
267 if (!(default_fd_get_poll_events( fd
) & POLLIN
))
269 remove_timeout_user( serial
->read_timer
);
270 serial
->read_timer
= NULL
;
273 else if (serial
->timeouts
.ReadIntervalTimeout
&& (default_fd_get_poll_events( fd
) & POLLIN
))
275 serial
->read_timer
= add_timeout_user( (timeout_t
)serial
->timeouts
.ReadIntervalTimeout
* -10000,
276 serial_read_timeout
, serial
);
278 default_fd_reselect_async( fd
, queue
);
281 DECL_HANDLER(get_serial_info
)
283 struct serial
*serial
;
285 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
287 if (req
->flags
& SERIALINFO_PENDING_WAIT
)
289 if (serial
->pending_wait
)
291 release_object( serial
);
292 set_error( STATUS_INVALID_PARAMETER
);
295 serial
->pending_wait
= 1;
299 reply
->eventmask
= serial
->eventmask
;
300 reply
->cookie
= serial
->generation
;
303 reply
->pending_write
= serial
->pending_write
;
304 if (req
->flags
& SERIALINFO_PENDING_WRITE
)
305 serial
->pending_write
= 0;
307 release_object( serial
);
311 DECL_HANDLER(set_serial_info
)
313 struct serial
*serial
;
315 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
317 if (req
->flags
& SERIALINFO_PENDING_WAIT
)
319 if (!serial
->pending_wait
)
321 release_object( serial
);
322 set_error( STATUS_INVALID_PARAMETER
);
325 serial
->pending_wait
= 0;
329 if (req
->flags
& SERIALINFO_PENDING_WRITE
)
330 serial
->pending_write
= 1;
332 release_object( serial
);