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>
50 #define WIN32_NO_STATUS
59 static void serial_dump( struct object
*obj
, int verbose
);
60 static struct fd
*serial_get_fd( struct object
*obj
);
61 static unsigned int serial_map_access( struct object
*obj
, unsigned int access
);
62 static void serial_destroy(struct object
*obj
);
64 static int serial_get_poll_events( struct fd
*fd
);
65 static void serial_poll_event( struct fd
*fd
, int event
);
66 static int serial_get_info( struct fd
*fd
);
67 static int serial_flush( struct fd
*fd
, struct event
**event
);
68 static void serial_queue_async( struct fd
*fd
, void *apc
, void *user
, void *iosb
, int type
, int count
);
69 static void serial_cancel_async( struct fd
*fd
);
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 commerror
;
87 struct termios original
;
93 /* FIXME: add dcb, comm status, handler module, sharing */
96 static const struct object_ops serial_ops
=
98 sizeof(struct serial
), /* size */
99 serial_dump
, /* dump */
100 default_fd_add_queue
, /* add_queue */
101 default_fd_remove_queue
, /* remove_queue */
102 default_fd_signaled
, /* signaled */
103 no_satisfied
, /* satisfied */
104 no_signal
, /* signal */
105 serial_get_fd
, /* get_fd */
106 serial_map_access
, /* map_access */
107 no_lookup_name
, /* lookup_name */
108 no_close_handle
, /* close_handle */
109 serial_destroy
/* destroy */
112 static const struct fd_ops serial_fd_ops
=
114 serial_get_poll_events
, /* get_poll_events */
115 serial_poll_event
, /* poll_event */
116 serial_flush
, /* flush */
117 serial_get_info
, /* get_file_info */
118 serial_queue_async
, /* queue_async */
119 serial_cancel_async
/* cancel_async */
122 /* check if the given fd is a serial port */
123 int is_serial_fd( struct fd
*fd
)
127 return !tcgetattr( get_unix_fd(fd
), &tios
);
130 /* create a serial object for a given fd */
131 struct object
*create_serial( struct fd
*fd
, unsigned int options
)
133 struct serial
*serial
;
136 if ((unix_fd
= dup( get_unix_fd(fd
) )) == -1) return NULL
;
138 /* set the fd back to blocking if necessary */
139 if (options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
))
140 fcntl( unix_fd
, F_SETFL
, 0 );
142 if (!(serial
= alloc_object( &serial_ops
)))
147 serial
->options
= options
;
148 serial
->readinterval
= 0;
149 serial
->readmult
= 0;
150 serial
->readconst
= 0;
151 serial
->writemult
= 0;
152 serial
->writeconst
= 0;
153 serial
->eventmask
= 0;
154 serial
->commerror
= 0;
155 list_init( &serial
->read_q
);
156 list_init( &serial
->write_q
);
157 list_init( &serial
->wait_q
);
158 if (!(serial
->fd
= create_anonymous_fd( &serial_fd_ops
, unix_fd
, &serial
->obj
)))
160 release_object( serial
);
166 static struct fd
*serial_get_fd( struct object
*obj
)
168 struct serial
*serial
= (struct serial
*)obj
;
169 return (struct fd
*)grab_object( serial
->fd
);
172 static unsigned int serial_map_access( struct object
*obj
, unsigned int access
)
174 if (access
& GENERIC_READ
) access
|= FILE_GENERIC_READ
;
175 if (access
& GENERIC_WRITE
) access
|= FILE_GENERIC_WRITE
;
176 if (access
& GENERIC_EXECUTE
) access
|= FILE_GENERIC_EXECUTE
;
177 if (access
& GENERIC_ALL
) access
|= FILE_ALL_ACCESS
;
178 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
181 static void serial_destroy( struct object
*obj
)
183 struct serial
*serial
= (struct serial
*)obj
;
185 async_terminate_queue( &serial
->read_q
, STATUS_CANCELLED
);
186 async_terminate_queue( &serial
->write_q
, STATUS_CANCELLED
);
187 async_terminate_queue( &serial
->wait_q
, STATUS_CANCELLED
);
188 if (serial
->fd
) release_object( serial
->fd
);
191 static void serial_dump( struct object
*obj
, int verbose
)
193 struct serial
*serial
= (struct serial
*)obj
;
194 assert( obj
->ops
== &serial_ops
);
195 fprintf( stderr
, "Port fd=%p mask=%x\n", serial
->fd
, serial
->eventmask
);
198 static struct serial
*get_serial_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
200 return (struct serial
*)get_handle_obj( process
, handle
, access
, &serial_ops
);
203 static int serial_get_poll_events( struct fd
*fd
)
205 struct serial
*serial
= get_fd_user( fd
);
207 assert( serial
->obj
.ops
== &serial_ops
);
209 if (!list_empty( &serial
->read_q
)) events
|= POLLIN
;
210 if (!list_empty( &serial
->write_q
)) events
|= POLLOUT
;
211 if (!list_empty( &serial
->wait_q
)) events
|= POLLIN
;
213 /* fprintf(stderr,"poll events are %04x\n",events); */
218 static int serial_get_info( struct fd
*fd
)
221 struct serial
*serial
= get_fd_user( fd
);
222 assert( serial
->obj
.ops
== &serial_ops
);
224 if (!(serial
->options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
225 flags
|= FD_FLAG_OVERLAPPED
;
226 else if (!(serial
->readinterval
== MAXDWORD
&&
227 serial
->readmult
== 0 && serial
->readconst
== 0))
228 flags
|= FD_FLAG_TIMEOUT
;
229 if (serial
->readinterval
== MAXDWORD
&&
230 serial
->readmult
== 0 && serial
->readconst
== 0)
231 flags
|= FD_FLAG_AVAILABLE
;
236 static void serial_poll_event(struct fd
*fd
, int event
)
238 struct serial
*serial
= get_fd_user( fd
);
240 /* fprintf(stderr,"Poll event %02x\n",event); */
242 if (!list_empty( &serial
->read_q
) && (POLLIN
& event
) )
243 async_terminate_head( &serial
->read_q
, STATUS_ALERTED
);
245 if (!list_empty( &serial
->write_q
) && (POLLOUT
& event
) )
246 async_terminate_head( &serial
->write_q
, STATUS_ALERTED
);
248 if (!list_empty( &serial
->wait_q
) && (POLLIN
& event
) )
249 async_terminate_head( &serial
->wait_q
, STATUS_ALERTED
);
251 set_fd_events( fd
, serial_get_poll_events(fd
) );
254 static void serial_queue_async( struct fd
*fd
, void *apc
, void *user
, void *iosb
,
255 int type
, int count
)
257 struct serial
*serial
= get_fd_user( fd
);
262 assert(serial
->obj
.ops
== &serial_ops
);
266 case ASYNC_TYPE_READ
:
267 queue
= &serial
->read_q
;
268 timeout
= serial
->readconst
+ serial
->readmult
*count
;
270 case ASYNC_TYPE_WAIT
:
271 queue
= &serial
->wait_q
;
274 case ASYNC_TYPE_WRITE
:
275 queue
= &serial
->write_q
;
276 timeout
= serial
->writeconst
+ serial
->writemult
*count
;
279 set_error(STATUS_INVALID_PARAMETER
);
283 if (!create_async( current
, &timeout
, queue
, apc
, user
, iosb
))
286 /* Check if the new pending request can be served immediately */
287 events
= check_fd_events( fd
, serial_get_poll_events( fd
) );
290 /* serial_poll_event() calls set_select_events() */
291 serial_poll_event( fd
, events
);
295 set_fd_events( fd
, serial_get_poll_events( fd
) );
298 static void serial_cancel_async( struct fd
*fd
)
300 struct serial
*serial
= get_fd_user( fd
);
301 assert(serial
->obj
.ops
== &serial_ops
);
303 async_terminate_queue( &serial
->read_q
, STATUS_CANCELLED
);
304 async_terminate_queue( &serial
->write_q
, STATUS_CANCELLED
);
305 async_terminate_queue( &serial
->wait_q
, STATUS_CANCELLED
);
308 static int serial_flush( struct fd
*fd
, struct event
**event
)
310 /* MSDN says: If hFile is a handle to a communications device,
311 * the function only flushes the transmit buffer.
313 int ret
= (tcflush( get_unix_fd(fd
), TCOFLUSH
) != -1);
314 if (!ret
) file_set_error();
318 DECL_HANDLER(get_serial_info
)
320 struct serial
*serial
;
322 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
325 reply
->readinterval
= serial
->readinterval
;
326 reply
->readconst
= serial
->readconst
;
327 reply
->readmult
= serial
->readmult
;
328 reply
->writeconst
= serial
->writeconst
;
329 reply
->writemult
= serial
->writemult
;
332 reply
->eventmask
= serial
->eventmask
;
334 /* comm port error status */
335 reply
->commerror
= serial
->commerror
;
337 release_object( serial
);
341 DECL_HANDLER(set_serial_info
)
343 struct serial
*serial
;
345 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
348 if (req
->flags
& SERIALINFO_SET_TIMEOUTS
)
350 serial
->readinterval
= req
->readinterval
;
351 serial
->readconst
= req
->readconst
;
352 serial
->readmult
= req
->readmult
;
353 serial
->writeconst
= req
->writeconst
;
354 serial
->writemult
= req
->writemult
;
358 if (req
->flags
& SERIALINFO_SET_MASK
)
360 serial
->eventmask
= req
->eventmask
;
361 if (!serial
->eventmask
)
363 async_terminate_queue( &serial
->wait_q
, STATUS_SUCCESS
);
367 /* comm port error status */
368 if (req
->flags
& SERIALINFO_SET_ERROR
)
370 serial
->commerror
= req
->commerror
;
373 release_object( serial
);