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 #ifdef HAVE_SYS_ERRNO_H
33 #include <sys/errno.h>
36 #include <sys/types.h>
41 #include <sys/ioctl.h>
51 static void serial_dump( struct object
*obj
, int verbose
);
52 static int serial_get_fd( struct object
*obj
);
53 static int serial_get_info( struct object
*obj
, struct get_file_info_reply
*reply
, int *flags
);
54 static int serial_get_poll_events( struct object
*obj
);
55 static void serial_queue_async(struct object
*obj
, void *ptr
, unsigned int status
, int type
, int count
);
56 static void destroy_serial(struct object
*obj
);
57 static void serial_poll_event( struct object
*obj
, int event
);
58 static int serial_flush( struct object
*obj
);
67 unsigned int readinterval
;
68 unsigned int readconst
;
69 unsigned int readmult
;
70 unsigned int writeconst
;
71 unsigned int writemult
;
73 unsigned int eventmask
;
74 unsigned int commerror
;
76 struct termios original
;
78 struct async_queue read_q
;
79 struct async_queue write_q
;
80 struct async_queue wait_q
;
82 /* FIXME: add dcb, comm status, handler module, sharing */
85 static const struct object_ops serial_ops
=
87 sizeof(struct serial
), /* size */
88 serial_dump
, /* dump */
89 default_poll_add_queue
, /* add_queue */
90 default_poll_remove_queue
, /* remove_queue */
91 default_poll_signaled
, /* signaled */
92 no_satisfied
, /* satisfied */
93 serial_get_poll_events
, /* get_poll_events */
94 serial_poll_event
, /* poll_event */
95 serial_get_fd
, /* get_fd */
96 serial_flush
, /* flush */
97 serial_get_info
, /* get_file_info */
98 serial_queue_async
, /* queue_async */
99 destroy_serial
/* destroy */
102 static struct serial
*create_serial( const char *nameptr
, size_t len
, unsigned int access
, int attributes
)
104 struct serial
*serial
;
109 if (!(name
= mem_alloc( len
+ 1 ))) return NULL
;
110 memcpy( name
, nameptr
, len
);
113 switch(access
& (GENERIC_READ
| GENERIC_WRITE
))
115 case GENERIC_READ
: flags
|= O_RDONLY
; break;
116 case GENERIC_WRITE
: flags
|= O_WRONLY
; break;
117 case GENERIC_READ
|GENERIC_WRITE
: flags
|= O_RDWR
; break;
123 fd
= open( name
, flags
);
131 /* check its really a serial port */
132 if (tcgetattr(fd
,&tios
))
139 /* set the fd back to blocking if necessary */
140 if( ! (attributes
& FILE_FLAG_OVERLAPPED
) )
141 if(0>fcntl(fd
, F_SETFL
, 0))
144 if ((serial
= alloc_object( &serial_ops
, fd
)))
146 serial
->attrib
= attributes
;
147 serial
->access
= access
;
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 init_async_queue(&serial
->read_q
);
156 init_async_queue(&serial
->write_q
);
157 init_async_queue(&serial
->wait_q
);
162 static void destroy_serial( struct object
*obj
)
164 struct serial
*serial
= (struct serial
*)obj
;
166 destroy_async_queue(&serial
->read_q
);
167 destroy_async_queue(&serial
->write_q
);
168 destroy_async_queue(&serial
->wait_q
);
171 static void serial_dump( struct object
*obj
, int verbose
)
173 struct serial
*serial
= (struct serial
*)obj
;
174 assert( obj
->ops
== &serial_ops
);
175 fprintf( stderr
, "Port fd=%d mask=%x\n", serial
->obj
.fd
, serial
->eventmask
);
178 struct serial
*get_serial_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
180 return (struct serial
*)get_handle_obj( process
, handle
, access
, &serial_ops
);
183 static int serial_get_poll_events( struct object
*obj
)
185 struct serial
*serial
= (struct serial
*)obj
;
187 assert( obj
->ops
== &serial_ops
);
189 if(IS_READY(serial
->read_q
))
191 if(IS_READY(serial
->write_q
))
193 if(IS_READY(serial
->wait_q
))
196 /* fprintf(stderr,"poll events are %04x\n",events); */
201 static int serial_get_fd( struct object
*obj
)
203 struct serial
*serial
= (struct serial
*)obj
;
204 assert( obj
->ops
== &serial_ops
);
205 return serial
->obj
.fd
;
208 static int serial_get_info( struct object
*obj
, struct get_file_info_reply
*reply
, int *flags
)
210 struct serial
*serial
= (struct serial
*) obj
;
211 assert( 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 object
*obj
, int event
)
239 struct serial
*serial
= (struct serial
*)obj
;
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(obj
,obj
->ops
->get_poll_events(obj
));
255 static void serial_queue_async(struct object
*obj
, void *ptr
, unsigned int status
, int type
, int count
)
257 struct serial
*serial
= (struct serial
*)obj
;
258 struct async_queue
*q
;
262 assert(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 ( 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 */
303 pfd
.events
= serial_get_poll_events ( obj
);
308 /* serial_poll_event() calls set_select_events() */
309 serial_poll_event ( obj
, pfd
.revents
);
311 set_select_events ( obj
, pfd
.events
);
314 else if ( async
) destroy_async ( async
);
315 else set_error ( STATUS_INVALID_PARAMETER
);
317 set_select_events ( obj
, serial_get_poll_events ( obj
));
320 static int serial_flush( struct object
*obj
)
323 struct serial
*serial
= (struct serial
*)grab_object(obj
);
324 assert( obj
->ops
== &serial_ops
);
326 /* MSDN says: If hFile is a handle to a communications device,
327 * the function only flushes the transmit buffer.
329 ret
= (tcflush( serial
->obj
.fd
, TCOFLUSH
) != -1);
330 if (!ret
) file_set_error();
331 release_object( serial
);
335 /* create a serial */
336 DECL_HANDLER(create_serial
)
338 struct serial
*serial
;
341 if ((serial
= create_serial( get_req_data(), get_req_data_size(), req
->access
, req
->attributes
)))
343 reply
->handle
= alloc_handle( current
->process
, serial
, req
->access
, req
->inherit
);
344 release_object( serial
);
348 DECL_HANDLER(get_serial_info
)
350 struct serial
*serial
;
352 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
355 reply
->readinterval
= serial
->readinterval
;
356 reply
->readconst
= serial
->readconst
;
357 reply
->readmult
= serial
->readmult
;
358 reply
->writeconst
= serial
->writeconst
;
359 reply
->writemult
= serial
->writemult
;
362 reply
->eventmask
= serial
->eventmask
;
364 /* comm port error status */
365 reply
->commerror
= serial
->commerror
;
367 release_object( serial
);
371 DECL_HANDLER(set_serial_info
)
373 struct serial
*serial
;
375 if ((serial
= get_serial_obj( current
->process
, req
->handle
, 0 )))
378 if(req
->flags
& SERIALINFO_SET_TIMEOUTS
)
380 serial
->readinterval
= req
->readinterval
;
381 serial
->readconst
= req
->readconst
;
382 serial
->readmult
= req
->readmult
;
383 serial
->writeconst
= req
->writeconst
;
384 serial
->writemult
= req
->writemult
;
388 if(req
->flags
& SERIALINFO_SET_MASK
)
390 serial
->eventmask
= req
->eventmask
;
391 if(!serial
->eventmask
)
393 while(serial
->wait_q
.head
)
395 async_notify(serial
->wait_q
.head
, STATUS_SUCCESS
);
396 destroy_async(serial
->wait_q
.head
);
401 /* comm port error status */
402 if(req
->flags
& SERIALINFO_SET_ERROR
)
404 serial
->commerror
= req
->commerror
;
407 release_object( serial
);