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 obj_handle_t
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
;
78 unsigned int readinterval
;
79 unsigned int readconst
;
80 unsigned int readmult
;
81 unsigned int writeconst
;
82 unsigned int writemult
;
84 unsigned int eventmask
;
85 unsigned int generation
; /* event mask change counter */
86 unsigned int pending_write
: 1;
87 unsigned int pending_wait
: 1;
89 struct termios original
;
91 /* FIXME: add dcb, comm status, handler module, sharing */
94 static const struct object_ops serial_ops
=
96 sizeof(struct serial
), /* size */
97 serial_dump
, /* dump */
98 no_get_type
, /* get_type */
99 add_queue
, /* add_queue */
100 remove_queue
, /* remove_queue */
101 default_fd_signaled
, /* signaled */
102 no_satisfied
, /* satisfied */
103 no_signal
, /* signal */
104 serial_get_fd
, /* get_fd */
105 default_fd_map_access
, /* map_access */
106 default_get_sd
, /* get_sd */
107 default_set_sd
, /* set_sd */
108 no_lookup_name
, /* lookup_name */
109 no_link_name
, /* link_name */
110 NULL
, /* unlink_name */
111 no_open_file
, /* open_file */
112 fd_close_handle
, /* close_handle */
113 serial_destroy
/* destroy */
116 static const struct fd_ops serial_fd_ops
=
118 default_fd_get_poll_events
, /* get_poll_events */
119 default_poll_event
, /* poll_event */
120 serial_get_fd_type
, /* get_fd_type */
121 no_fd_read
, /* read */
122 no_fd_write
, /* write */
123 no_fd_flush
, /* flush */
124 serial_ioctl
, /* ioctl */
125 serial_queue_async
, /* queue_async */
126 serial_reselect_async
/* reselect_async */
129 /* check if the given fd is a serial port */
130 int is_serial_fd( struct fd
*fd
)
134 return !tcgetattr( get_unix_fd(fd
), &tios
);
137 /* create a serial object for a given fd */
138 struct object
*create_serial( struct fd
*fd
)
140 struct serial
*serial
;
142 if (!(serial
= alloc_object( &serial_ops
))) return NULL
;
144 serial
->read_timer
= NULL
;
145 serial
->readinterval
= 0;
146 serial
->readmult
= 0;
147 serial
->readconst
= 0;
148 serial
->writemult
= 0;
149 serial
->writeconst
= 0;
150 serial
->eventmask
= 0;
151 serial
->generation
= 0;
152 serial
->pending_write
= 0;
153 serial
->pending_wait
= 0;
154 serial
->fd
= (struct fd
*)grab_object( fd
);
155 set_fd_user( fd
, &serial_fd_ops
, &serial
->obj
);
159 static struct fd
*serial_get_fd( struct object
*obj
)
161 struct serial
*serial
= (struct serial
*)obj
;
162 return (struct fd
*)grab_object( serial
->fd
);
165 static void serial_destroy( struct object
*obj
)
167 struct serial
*serial
= (struct serial
*)obj
;
168 if (serial
->read_timer
) remove_timeout_user( serial
->read_timer
);
169 release_object( serial
->fd
);
172 static void serial_dump( struct object
*obj
, int verbose
)
174 struct serial
*serial
= (struct serial
*)obj
;
175 assert( obj
->ops
== &serial_ops
);
176 fprintf( stderr
, "Port fd=%p mask=%x\n", serial
->fd
, serial
->eventmask
);
179 static struct serial
*get_serial_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
181 return (struct serial
*)get_handle_obj( process
, handle
, access
, &serial_ops
);
184 static enum server_fd_type
serial_get_fd_type( struct fd
*fd
)
186 return FD_TYPE_SERIAL
;
189 static obj_handle_t
serial_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
191 struct serial
*serial
= get_fd_user( fd
);
195 case IOCTL_SERIAL_GET_TIMEOUTS
:
196 if (get_reply_max_size() >= sizeof(SERIAL_TIMEOUTS
))
198 SERIAL_TIMEOUTS
*timeouts
= set_reply_data_size( sizeof(*timeouts
));
200 timeouts
->ReadIntervalTimeout
= serial
->readinterval
;
201 timeouts
->ReadTotalTimeoutConstant
= serial
->readconst
;
202 timeouts
->ReadTotalTimeoutMultiplier
= serial
->readmult
;
203 timeouts
->WriteTotalTimeoutConstant
= serial
->writeconst
;
204 timeouts
->WriteTotalTimeoutMultiplier
= serial
->writemult
;
206 else set_error( STATUS_BUFFER_TOO_SMALL
);
209 case IOCTL_SERIAL_SET_TIMEOUTS
:
210 if (get_req_data_size() >= sizeof(SERIAL_TIMEOUTS
))
212 const SERIAL_TIMEOUTS
*timeouts
= get_req_data();
214 serial
->readinterval
= timeouts
->ReadIntervalTimeout
;
215 serial
->readconst
= timeouts
->ReadTotalTimeoutConstant
;
216 serial
->readmult
= timeouts
->ReadTotalTimeoutMultiplier
;
217 serial
->writeconst
= timeouts
->WriteTotalTimeoutConstant
;
218 serial
->writemult
= timeouts
->WriteTotalTimeoutMultiplier
;
220 else set_error( STATUS_BUFFER_TOO_SMALL
);
223 case IOCTL_SERIAL_GET_WAIT_MASK
:
224 if (get_reply_max_size() >= sizeof(serial
->eventmask
))
225 set_reply_data( &serial
->eventmask
, sizeof(serial
->eventmask
) );
227 set_error( STATUS_BUFFER_TOO_SMALL
);
230 case IOCTL_SERIAL_SET_WAIT_MASK
:
231 if (get_req_data_size() >= sizeof(serial
->eventmask
))
233 serial
->eventmask
= *(unsigned int *)get_req_data();
234 serial
->generation
++;
235 fd_async_wake_up( serial
->fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
237 else set_error( STATUS_BUFFER_TOO_SMALL
);
241 set_error( STATUS_NOT_SUPPORTED
);
246 static void serial_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
248 struct serial
*serial
= get_fd_user( fd
);
249 timeout_t timeout
= 0;
251 assert(serial
->obj
.ops
== &serial_ops
);
255 case ASYNC_TYPE_READ
:
256 timeout
= serial
->readconst
+ (timeout_t
)serial
->readmult
*count
;
258 case ASYNC_TYPE_WRITE
:
259 timeout
= serial
->writeconst
+ (timeout_t
)serial
->writemult
*count
;
263 if (fd_queue_async( fd
, async
, type
))
265 if (timeout
) async_set_timeout( async
, timeout
* -10000, STATUS_TIMEOUT
);
266 set_error( STATUS_PENDING
);
270 static void serial_read_timeout( void *arg
)
272 struct serial
*serial
= arg
;
274 serial
->read_timer
= NULL
;
275 fd_async_wake_up( serial
->fd
, ASYNC_TYPE_READ
, STATUS_TIMEOUT
);
278 static void serial_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
280 struct serial
*serial
= get_fd_user( fd
);
282 if (serial
->read_timer
)
284 if (!(default_fd_get_poll_events( fd
) & POLLIN
))
286 remove_timeout_user( serial
->read_timer
);
287 serial
->read_timer
= NULL
;
290 else if (serial
->readinterval
&& (default_fd_get_poll_events( fd
) & POLLIN
))
292 serial
->read_timer
= add_timeout_user( (timeout_t
)serial
->readinterval
* -10000,
293 serial_read_timeout
, serial
);
295 default_fd_reselect_async( fd
, queue
);
298 DECL_HANDLER(get_serial_info
)
300 struct serial
*serial
;
302 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
304 if (req
->flags
& SERIALINFO_PENDING_WAIT
)
306 if (serial
->pending_wait
)
308 release_object( serial
);
309 set_error( STATUS_INVALID_PARAMETER
);
312 serial
->pending_wait
= 1;
316 reply
->eventmask
= serial
->eventmask
;
317 reply
->cookie
= serial
->generation
;
320 reply
->pending_write
= serial
->pending_write
;
321 if (req
->flags
& SERIALINFO_PENDING_WRITE
)
322 serial
->pending_write
= 0;
324 release_object( serial
);
328 DECL_HANDLER(set_serial_info
)
330 struct serial
*serial
;
332 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
334 if (req
->flags
& SERIALINFO_PENDING_WAIT
)
336 if (!serial
->pending_wait
)
338 release_object( serial
);
339 set_error( STATUS_INVALID_PARAMETER
);
342 serial
->pending_wait
= 0;
346 if (req
->flags
& SERIALINFO_PENDING_WRITE
)
347 serial
->pending_write
= 1;
349 release_object( serial
);