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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
33 #include <sys/types.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 #include <sys/ioctl.h>
57 static void serial_dump( struct object
*obj
, int verbose
);
58 static struct fd
*serial_get_fd( struct object
*obj
);
59 static void serial_destroy(struct object
*obj
);
61 static int serial_get_poll_events( struct fd
*fd
);
62 static void serial_poll_event( struct fd
*fd
, int event
);
63 static int serial_get_info( struct fd
*fd
);
64 static int serial_flush( struct fd
*fd
, struct event
**event
);
65 static void serial_queue_async( struct fd
*fd
, void *apc
, void *user
, void *iosb
, int type
, int count
);
66 static void serial_cancel_async( struct fd
*fd
);
75 unsigned int readinterval
;
76 unsigned int readconst
;
77 unsigned int readmult
;
78 unsigned int writeconst
;
79 unsigned int writemult
;
81 unsigned int eventmask
;
82 unsigned int commerror
;
84 struct termios original
;
90 /* FIXME: add dcb, comm status, handler module, sharing */
93 static const struct object_ops serial_ops
=
95 sizeof(struct serial
), /* size */
96 serial_dump
, /* dump */
97 default_fd_add_queue
, /* add_queue */
98 default_fd_remove_queue
, /* remove_queue */
99 default_fd_signaled
, /* signaled */
100 no_satisfied
, /* satisfied */
101 serial_get_fd
, /* get_fd */
102 serial_destroy
/* destroy */
105 static const struct fd_ops serial_fd_ops
=
107 serial_get_poll_events
, /* get_poll_events */
108 serial_poll_event
, /* poll_event */
109 serial_flush
, /* flush */
110 serial_get_info
, /* get_file_info */
111 serial_queue_async
, /* queue_async */
112 serial_cancel_async
/* cancel_async */
115 /* check if the given fd is a serial port */
116 int is_serial_fd( struct fd
*fd
)
120 return !tcgetattr( get_unix_fd(fd
), &tios
);
123 /* create a serial object for a given fd */
124 struct object
*create_serial( struct fd
*fd
, unsigned int options
)
126 struct serial
*serial
;
129 if ((unix_fd
= dup( get_unix_fd(fd
) )) == -1) return NULL
;
131 /* set the fd back to blocking if necessary */
132 if (options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
))
133 fcntl( unix_fd
, F_SETFL
, 0 );
135 if (!(serial
= alloc_object( &serial_ops
)))
140 serial
->options
= options
;
141 serial
->readinterval
= 0;
142 serial
->readmult
= 0;
143 serial
->readconst
= 0;
144 serial
->writemult
= 0;
145 serial
->writeconst
= 0;
146 serial
->eventmask
= 0;
147 serial
->commerror
= 0;
148 list_init( &serial
->read_q
);
149 list_init( &serial
->write_q
);
150 list_init( &serial
->wait_q
);
151 if (!(serial
->fd
= create_anonymous_fd( &serial_fd_ops
, unix_fd
, &serial
->obj
)))
153 release_object( serial
);
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
;
169 async_terminate_queue( &serial
->read_q
, STATUS_CANCELLED
);
170 async_terminate_queue( &serial
->write_q
, STATUS_CANCELLED
);
171 async_terminate_queue( &serial
->wait_q
, STATUS_CANCELLED
);
172 if (serial
->fd
) release_object( serial
->fd
);
175 static void serial_dump( struct object
*obj
, int verbose
)
177 struct serial
*serial
= (struct serial
*)obj
;
178 assert( obj
->ops
== &serial_ops
);
179 fprintf( stderr
, "Port fd=%p mask=%x\n", serial
->fd
, serial
->eventmask
);
182 static struct serial
*get_serial_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
184 return (struct serial
*)get_handle_obj( process
, handle
, access
, &serial_ops
);
187 static int serial_get_poll_events( struct fd
*fd
)
189 struct serial
*serial
= get_fd_user( fd
);
191 assert( serial
->obj
.ops
== &serial_ops
);
193 if (!list_empty( &serial
->read_q
)) events
|= POLLIN
;
194 if (!list_empty( &serial
->write_q
)) events
|= POLLOUT
;
195 if (!list_empty( &serial
->wait_q
)) events
|= POLLIN
;
197 /* fprintf(stderr,"poll events are %04x\n",events); */
202 static int serial_get_info( struct fd
*fd
)
205 struct serial
*serial
= get_fd_user( fd
);
206 assert( serial
->obj
.ops
== &serial_ops
);
208 if (!(serial
->options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
209 flags
|= FD_FLAG_OVERLAPPED
;
210 else if (!(serial
->readinterval
== MAXDWORD
&&
211 serial
->readmult
== 0 && serial
->readconst
== 0))
212 flags
|= FD_FLAG_TIMEOUT
;
213 if (serial
->readinterval
== MAXDWORD
&&
214 serial
->readmult
== 0 && serial
->readconst
== 0)
215 flags
|= FD_FLAG_AVAILABLE
;
220 static void serial_poll_event(struct fd
*fd
, int event
)
222 struct serial
*serial
= get_fd_user( fd
);
224 /* fprintf(stderr,"Poll event %02x\n",event); */
226 if (!list_empty( &serial
->read_q
) && (POLLIN
& event
) )
227 async_terminate_head( &serial
->read_q
, STATUS_ALERTED
);
229 if (!list_empty( &serial
->write_q
) && (POLLOUT
& event
) )
230 async_terminate_head( &serial
->write_q
, STATUS_ALERTED
);
232 if (!list_empty( &serial
->wait_q
) && (POLLIN
& event
) )
233 async_terminate_head( &serial
->wait_q
, STATUS_ALERTED
);
235 set_fd_events( fd
, serial_get_poll_events(fd
) );
238 static void serial_queue_async( struct fd
*fd
, void *apc
, void *user
, void *iosb
,
239 int type
, int count
)
241 struct serial
*serial
= get_fd_user( fd
);
246 assert(serial
->obj
.ops
== &serial_ops
);
250 case ASYNC_TYPE_READ
:
251 queue
= &serial
->read_q
;
252 timeout
= serial
->readconst
+ serial
->readmult
*count
;
254 case ASYNC_TYPE_WAIT
:
255 queue
= &serial
->wait_q
;
258 case ASYNC_TYPE_WRITE
:
259 queue
= &serial
->write_q
;
260 timeout
= serial
->writeconst
+ serial
->writemult
*count
;
263 set_error(STATUS_INVALID_PARAMETER
);
267 if (!create_async( fd
, current
, timeout
, queue
, apc
, user
, iosb
))
270 /* Check if the new pending request can be served immediately */
271 events
= check_fd_events( fd
, serial_get_poll_events( fd
) );
274 /* serial_poll_event() calls set_select_events() */
275 serial_poll_event( fd
, events
);
279 set_fd_events( fd
, serial_get_poll_events( fd
) );
282 static void serial_cancel_async( struct fd
*fd
)
284 struct serial
*serial
= get_fd_user( fd
);
285 assert(serial
->obj
.ops
== &serial_ops
);
287 async_terminate_queue( &serial
->read_q
, STATUS_CANCELLED
);
288 async_terminate_queue( &serial
->write_q
, STATUS_CANCELLED
);
289 async_terminate_queue( &serial
->wait_q
, STATUS_CANCELLED
);
292 static int serial_flush( struct fd
*fd
, struct event
**event
)
294 /* MSDN says: If hFile is a handle to a communications device,
295 * the function only flushes the transmit buffer.
297 int ret
= (tcflush( get_unix_fd(fd
), TCOFLUSH
) != -1);
298 if (!ret
) file_set_error();
302 DECL_HANDLER(get_serial_info
)
304 struct serial
*serial
;
306 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
309 reply
->readinterval
= serial
->readinterval
;
310 reply
->readconst
= serial
->readconst
;
311 reply
->readmult
= serial
->readmult
;
312 reply
->writeconst
= serial
->writeconst
;
313 reply
->writemult
= serial
->writemult
;
316 reply
->eventmask
= serial
->eventmask
;
318 /* comm port error status */
319 reply
->commerror
= serial
->commerror
;
321 release_object( serial
);
325 DECL_HANDLER(set_serial_info
)
327 struct serial
*serial
;
329 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
332 if (req
->flags
& SERIALINFO_SET_TIMEOUTS
)
334 serial
->readinterval
= req
->readinterval
;
335 serial
->readconst
= req
->readconst
;
336 serial
->readmult
= req
->readmult
;
337 serial
->writeconst
= req
->writeconst
;
338 serial
->writemult
= req
->writemult
;
342 if (req
->flags
& SERIALINFO_SET_MASK
)
344 serial
->eventmask
= req
->eventmask
;
345 if (!serial
->eventmask
)
347 async_terminate_queue( &serial
->wait_q
, STATUS_SUCCESS
);
351 /* comm port error status */
352 if (req
->flags
& SERIALINFO_SET_ERROR
)
354 serial
->commerror
= req
->commerror
;
357 release_object( serial
);