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"
32 #include <sys/types.h>
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
54 static void serial_dump( struct object
*obj
, int verbose
);
55 static void serial_destroy(struct object
*obj
);
57 static int serial_get_poll_events( struct fd
*fd
);
58 static void serial_poll_event( struct fd
*fd
, int event
);
59 static int serial_get_info( struct fd
*fd
, struct get_file_info_reply
*reply
, int *flags
);
60 static int serial_flush( struct fd
*fd
);
61 static void serial_queue_async(struct fd
*fd
, void *ptr
, unsigned int status
, int type
, int count
);
70 unsigned int readinterval
;
71 unsigned int readconst
;
72 unsigned int readmult
;
73 unsigned int writeconst
;
74 unsigned int writemult
;
76 unsigned int eventmask
;
77 unsigned int commerror
;
79 struct termios original
;
81 struct async_queue read_q
;
82 struct async_queue write_q
;
83 struct async_queue wait_q
;
85 /* FIXME: add dcb, comm status, handler module, sharing */
88 static const struct object_ops serial_ops
=
90 sizeof(struct serial
), /* size */
91 serial_dump
, /* dump */
92 default_fd_add_queue
, /* add_queue */
93 default_fd_remove_queue
, /* remove_queue */
94 default_fd_signaled
, /* signaled */
95 no_satisfied
, /* satisfied */
96 default_get_fd
, /* get_fd */
97 serial_destroy
/* destroy */
100 static const struct fd_ops serial_fd_ops
=
102 serial_get_poll_events
, /* get_poll_events */
103 serial_poll_event
, /* poll_event */
104 serial_flush
, /* flush */
105 serial_get_info
, /* get_file_info */
106 serial_queue_async
/* queue_async */
109 static struct serial
*create_serial( const char *nameptr
, size_t len
, unsigned int access
, int attributes
)
111 struct serial
*serial
;
116 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
117 memcpy( name
, nameptr
, len
);
120 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
122 case GENERIC_READ
: flags
|= O_RDONLY
; break;
123 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
124 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
130 fd
= open( name
, flags
);
138 /* check its really a serial port */
139 if (tcgetattr(fd
,&tios
))
146 /* set the fd back to blocking if necessary */
147 if( ! (attributes
& FILE_FLAG_OVERLAPPED
) )
148 if(0>fcntl(fd
, F_SETFL
, 0))
151 if ((serial
= alloc_fd_object( &serial_ops
, &serial_fd_ops
, fd
)))
153 serial
->attrib
= attributes
;
154 serial
->access
= access
;
155 serial
->readinterval
= 0;
156 serial
->readmult
= 0;
157 serial
->readconst
= 0;
158 serial
->writemult
= 0;
159 serial
->writeconst
= 0;
160 serial
->eventmask
= 0;
161 serial
->commerror
= 0;
162 init_async_queue(&serial
->read_q
);
163 init_async_queue(&serial
->write_q
);
164 init_async_queue(&serial
->wait_q
);
169 static void serial_destroy( struct object
*obj
)
171 struct serial
*serial
= (struct serial
*)obj
;
173 destroy_async_queue(&serial
->read_q
);
174 destroy_async_queue(&serial
->write_q
);
175 destroy_async_queue(&serial
->wait_q
);
178 static void serial_dump( struct object
*obj
, int verbose
)
180 struct serial
*serial
= (struct serial
*)obj
;
181 assert( obj
->ops
== &serial_ops
);
182 fprintf( stderr
, "Port fd=%p mask=%x\n", serial
->obj
.fd_obj
, serial
->eventmask
);
185 struct serial
*get_serial_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
187 return (struct serial
*)get_handle_obj( process
, handle
, access
, &serial_ops
);
190 static int serial_get_poll_events( struct fd
*fd
)
192 struct serial
*serial
= get_fd_user( fd
);
194 assert( serial
->obj
.ops
== &serial_ops
);
196 if(IS_READY(serial
->read_q
))
198 if(IS_READY(serial
->write_q
))
200 if(IS_READY(serial
->wait_q
))
203 /* fprintf(stderr,"poll events are %04x\n",events); */
208 static int serial_get_info( struct fd
*fd
, struct get_file_info_reply
*reply
, int *flags
)
210 struct serial
*serial
= get_fd_user( fd
);
211 assert( serial
->obj
.ops
== &serial_ops
);
215 reply
->type
= FILE_TYPE_CHAR
;
217 reply
->access_time
= 0;
218 reply
->write_time
= 0;
219 reply
->size_high
= 0;
222 reply
->index_high
= 0;
223 reply
->index_low
= 0;
228 if(serial
->attrib
& FILE_FLAG_OVERLAPPED
)
229 *flags
|= FD_FLAG_OVERLAPPED
;
230 else if(!((serial
->readinterval
== MAXDWORD
) &&
231 (serial
->readmult
== 0) && (serial
->readconst
== 0)) )
232 *flags
|= FD_FLAG_TIMEOUT
;
234 return FD_TYPE_DEFAULT
;
237 static void serial_poll_event(struct fd
*fd
, int event
)
239 struct serial
*serial
= get_fd_user( fd
);
241 /* fprintf(stderr,"Poll event %02x\n",event); */
243 if(IS_READY(serial
->read_q
) && (POLLIN
& event
) )
244 async_notify(serial
->read_q
.head
,STATUS_ALERTED
);
246 if(IS_READY(serial
->write_q
) && (POLLOUT
& event
) )
247 async_notify(serial
->write_q
.head
,STATUS_ALERTED
);
249 if(IS_READY(serial
->wait_q
) && (POLLIN
& event
) )
250 async_notify(serial
->wait_q
.head
,STATUS_ALERTED
);
252 set_select_events( &serial
->obj
, serial_get_poll_events(fd
) );
255 static void serial_queue_async(struct fd
*fd
, void *ptr
, unsigned int status
, int type
, int count
)
257 struct serial
*serial
= get_fd_user( fd
);
258 struct async_queue
*q
;
262 assert(serial
->obj
.ops
== &serial_ops
);
266 case ASYNC_TYPE_READ
:
268 timeout
= serial
->readconst
+ serial
->readmult
*count
;
270 case ASYNC_TYPE_WAIT
:
274 case ASYNC_TYPE_WRITE
:
275 q
= &serial
->write_q
;
276 timeout
= serial
->writeconst
+ serial
->writemult
*count
;
279 set_error(STATUS_INVALID_PARAMETER
);
283 async
= find_async ( q
, current
, ptr
);
285 if ( status
== STATUS_PENDING
)
290 async
= create_async ( &serial
->obj
, current
, ptr
);
294 async
->status
= STATUS_PENDING
;
297 async_add_timeout(async
,timeout
);
298 async_insert(q
, async
);
301 /* Check if the new pending request can be served immediately */
302 events
= check_fd_events( fd
, serial_get_poll_events( fd
) );
305 /* serial_poll_event() calls set_select_events() */
306 serial_poll_event( fd
, events
);
310 else if ( async
) destroy_async ( async
);
311 else set_error ( STATUS_INVALID_PARAMETER
);
313 set_select_events ( &serial
->obj
, serial_get_poll_events( fd
));
316 static int serial_flush( struct fd
*fd
)
318 /* MSDN says: If hFile is a handle to a communications device,
319 * the function only flushes the transmit buffer.
321 int ret
= (tcflush( get_unix_fd(fd
), TCOFLUSH
) != -1);
322 if (!ret
) file_set_error();
326 /* create a serial */
327 DECL_HANDLER(create_serial
)
329 struct serial
*serial
;
332 if ((serial
= create_serial( get_req_data(), get_req_data_size(), req
->access
, req
->attributes
)))
334 reply
->handle
= alloc_handle( current
->process
, serial
, req
->access
, req
->inherit
);
335 release_object( serial
);
339 DECL_HANDLER(get_serial_info
)
341 struct serial
*serial
;
343 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
346 reply
->readinterval
= serial
->readinterval
;
347 reply
->readconst
= serial
->readconst
;
348 reply
->readmult
= serial
->readmult
;
349 reply
->writeconst
= serial
->writeconst
;
350 reply
->writemult
= serial
->writemult
;
353 reply
->eventmask
= serial
->eventmask
;
355 /* comm port error status */
356 reply
->commerror
= serial
->commerror
;
358 release_object( serial
);
362 DECL_HANDLER(set_serial_info
)
364 struct serial
*serial
;
366 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
369 if(req
->flags
& SERIALINFO_SET_TIMEOUTS
)
371 serial
->readinterval
= req
->readinterval
;
372 serial
->readconst
= req
->readconst
;
373 serial
->readmult
= req
->readmult
;
374 serial
->writeconst
= req
->writeconst
;
375 serial
->writemult
= req
->writemult
;
379 if(req
->flags
& SERIALINFO_SET_MASK
)
381 serial
->eventmask
= req
->eventmask
;
382 if(!serial
->eventmask
)
384 while(serial
->wait_q
.head
)
386 async_notify(serial
->wait_q
.head
, STATUS_SUCCESS
);
387 destroy_async(serial
->wait_q
.head
);
392 /* comm port error status */
393 if(req
->flags
& SERIALINFO_SET_ERROR
)
395 serial
->commerror
= req
->commerror
;
398 release_object( serial
);