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>
56 static void serial_dump( struct object
*obj
, int verbose
);
57 static struct fd
*serial_get_fd( struct object
*obj
);
58 static void serial_destroy(struct object
*obj
);
60 static int serial_get_poll_events( struct fd
*fd
);
61 static void serial_poll_event( struct fd
*fd
, int event
);
62 static int serial_get_info( struct fd
*fd
, struct get_file_info_reply
*reply
, int *flags
);
63 static int serial_flush( struct fd
*fd
, struct event
**event
);
64 static void serial_queue_async(struct fd
*fd
, void *ptr
, unsigned int status
, int type
, int count
);
74 unsigned int readinterval
;
75 unsigned int readconst
;
76 unsigned int readmult
;
77 unsigned int writeconst
;
78 unsigned int writemult
;
80 unsigned int eventmask
;
81 unsigned int commerror
;
83 struct termios original
;
85 struct async_queue read_q
;
86 struct async_queue write_q
;
87 struct async_queue wait_q
;
89 /* FIXME: add dcb, comm status, handler module, sharing */
92 static const struct object_ops serial_ops
=
94 sizeof(struct serial
), /* size */
95 serial_dump
, /* dump */
96 default_fd_add_queue
, /* add_queue */
97 default_fd_remove_queue
, /* remove_queue */
98 default_fd_signaled
, /* signaled */
99 no_satisfied
, /* satisfied */
100 serial_get_fd
, /* get_fd */
101 serial_destroy
/* destroy */
104 static const struct fd_ops serial_fd_ops
=
106 serial_get_poll_events
, /* get_poll_events */
107 serial_poll_event
, /* poll_event */
108 serial_flush
, /* flush */
109 serial_get_info
, /* get_file_info */
110 serial_queue_async
/* queue_async */
113 static struct serial
*create_serial( const char *nameptr
, size_t len
, unsigned int access
, int attributes
)
115 struct serial
*serial
;
120 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
121 memcpy( name
, nameptr
, len
);
124 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
126 case GENERIC_READ
: flags
|= O_RDONLY
; break;
127 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
128 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
134 fd
= open( name
, flags
);
142 /* check its really a serial port */
143 if (tcgetattr(fd
,&tios
))
150 /* set the fd back to blocking if necessary */
151 if( ! (attributes
& FILE_FLAG_OVERLAPPED
) )
152 if(0>fcntl(fd
, F_SETFL
, 0))
155 if (!(serial
= alloc_object( &serial_ops
)))
160 serial
->attrib
= attributes
;
161 serial
->access
= access
;
162 serial
->readinterval
= 0;
163 serial
->readmult
= 0;
164 serial
->readconst
= 0;
165 serial
->writemult
= 0;
166 serial
->writeconst
= 0;
167 serial
->eventmask
= 0;
168 serial
->commerror
= 0;
169 init_async_queue(&serial
->read_q
);
170 init_async_queue(&serial
->write_q
);
171 init_async_queue(&serial
->wait_q
);
172 if (!(serial
->fd
= create_anonymous_fd( &serial_fd_ops
, fd
, &serial
->obj
)))
174 release_object( serial
);
180 static struct fd
*serial_get_fd( struct object
*obj
)
182 struct serial
*serial
= (struct serial
*)obj
;
183 return (struct fd
*)grab_object( serial
->fd
);
186 static void serial_destroy( struct object
*obj
)
188 struct serial
*serial
= (struct serial
*)obj
;
190 destroy_async_queue(&serial
->read_q
);
191 destroy_async_queue(&serial
->write_q
);
192 destroy_async_queue(&serial
->wait_q
);
193 if (serial
->fd
) release_object( serial
->fd
);
196 static void serial_dump( struct object
*obj
, int verbose
)
198 struct serial
*serial
= (struct serial
*)obj
;
199 assert( obj
->ops
== &serial_ops
);
200 fprintf( stderr
, "Port fd=%p mask=%x\n", serial
->fd
, serial
->eventmask
);
203 static struct serial
*get_serial_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
205 return (struct serial
*)get_handle_obj( process
, handle
, access
, &serial_ops
);
208 static int serial_get_poll_events( struct fd
*fd
)
210 struct serial
*serial
= get_fd_user( fd
);
212 assert( serial
->obj
.ops
== &serial_ops
);
214 if(IS_READY(serial
->read_q
))
216 if(IS_READY(serial
->write_q
))
218 if(IS_READY(serial
->wait_q
))
221 /* fprintf(stderr,"poll events are %04x\n",events); */
226 static int serial_get_info( struct fd
*fd
, struct get_file_info_reply
*reply
, int *flags
)
228 struct serial
*serial
= get_fd_user( fd
);
229 assert( serial
->obj
.ops
== &serial_ops
);
233 reply
->type
= FILE_TYPE_CHAR
;
235 reply
->access_time
= 0;
236 reply
->write_time
= 0;
237 reply
->size_high
= 0;
240 reply
->index_high
= 0;
241 reply
->index_low
= 0;
246 if(serial
->attrib
& FILE_FLAG_OVERLAPPED
)
247 *flags
|= FD_FLAG_OVERLAPPED
;
248 else if(!((serial
->readinterval
== MAXDWORD
) &&
249 (serial
->readmult
== 0) && (serial
->readconst
== 0)) )
250 *flags
|= FD_FLAG_TIMEOUT
;
252 return FD_TYPE_DEFAULT
;
255 static void serial_poll_event(struct fd
*fd
, int event
)
257 struct serial
*serial
= get_fd_user( fd
);
259 /* fprintf(stderr,"Poll event %02x\n",event); */
261 if(IS_READY(serial
->read_q
) && (POLLIN
& event
) )
262 async_notify(serial
->read_q
.head
,STATUS_ALERTED
);
264 if(IS_READY(serial
->write_q
) && (POLLOUT
& event
) )
265 async_notify(serial
->write_q
.head
,STATUS_ALERTED
);
267 if(IS_READY(serial
->wait_q
) && (POLLIN
& event
) )
268 async_notify(serial
->wait_q
.head
,STATUS_ALERTED
);
270 set_fd_events( fd
, serial_get_poll_events(fd
) );
273 static void serial_queue_async(struct fd
*fd
, void *ptr
, unsigned int status
, int type
, int count
)
275 struct serial
*serial
= get_fd_user( fd
);
276 struct async_queue
*q
;
280 assert(serial
->obj
.ops
== &serial_ops
);
284 case ASYNC_TYPE_READ
:
286 timeout
= serial
->readconst
+ serial
->readmult
*count
;
288 case ASYNC_TYPE_WAIT
:
292 case ASYNC_TYPE_WRITE
:
293 q
= &serial
->write_q
;
294 timeout
= serial
->writeconst
+ serial
->writemult
*count
;
297 set_error(STATUS_INVALID_PARAMETER
);
301 async
= find_async ( q
, current
, ptr
);
303 if ( status
== STATUS_PENDING
)
308 async
= create_async ( &serial
->obj
, current
, ptr
);
312 async
->status
= STATUS_PENDING
;
315 async_add_timeout(async
,timeout
);
316 async_insert(q
, async
);
319 /* Check if the new pending request can be served immediately */
320 events
= check_fd_events( fd
, serial_get_poll_events( fd
) );
323 /* serial_poll_event() calls set_select_events() */
324 serial_poll_event( fd
, events
);
328 else if ( async
) destroy_async ( async
);
329 else set_error ( STATUS_INVALID_PARAMETER
);
331 set_fd_events ( fd
, serial_get_poll_events( fd
));
334 static int serial_flush( struct fd
*fd
, struct event
**event
)
336 /* MSDN says: If hFile is a handle to a communications device,
337 * the function only flushes the transmit buffer.
339 int ret
= (tcflush( get_unix_fd(fd
), TCOFLUSH
) != -1);
340 if (!ret
) file_set_error();
344 /* create a serial */
345 DECL_HANDLER(create_serial
)
347 struct serial
*serial
;
350 if ((serial
= create_serial( get_req_data(), get_req_data_size(), req
->access
, req
->attributes
)))
352 reply
->handle
= alloc_handle( current
->process
, serial
, req
->access
, req
->inherit
);
353 release_object( serial
);
357 DECL_HANDLER(get_serial_info
)
359 struct serial
*serial
;
361 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
364 reply
->readinterval
= serial
->readinterval
;
365 reply
->readconst
= serial
->readconst
;
366 reply
->readmult
= serial
->readmult
;
367 reply
->writeconst
= serial
->writeconst
;
368 reply
->writemult
= serial
->writemult
;
371 reply
->eventmask
= serial
->eventmask
;
373 /* comm port error status */
374 reply
->commerror
= serial
->commerror
;
376 release_object( serial
);
380 DECL_HANDLER(set_serial_info
)
382 struct serial
*serial
;
384 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
387 if(req
->flags
& SERIALINFO_SET_TIMEOUTS
)
389 serial
->readinterval
= req
->readinterval
;
390 serial
->readconst
= req
->readconst
;
391 serial
->readmult
= req
->readmult
;
392 serial
->writeconst
= req
->writeconst
;
393 serial
->writemult
= req
->writemult
;
397 if(req
->flags
& SERIALINFO_SET_MASK
)
399 serial
->eventmask
= req
->eventmask
;
400 if(!serial
->eventmask
)
402 while(serial
->wait_q
.head
)
404 async_notify(serial
->wait_q
.head
, STATUS_SUCCESS
);
405 destroy_async(serial
->wait_q
.head
);
410 /* comm port error status */
411 if(req
->flags
& SERIALINFO_SET_ERROR
)
413 serial
->commerror
= req
->commerror
;
416 release_object( serial
);